commit 731cd6091672e6c061a01789962defb35c524066 Author: mb Date: Tue Mar 26 14:18:31 2024 +0530 Initial Commit diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..e570d94 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,23 @@ +[run] +omit = + tests/* + .github/* + commands/* + **/test_*.py + +[report] +exclude_lines = + pragma: no cover + if TYPE_CHECKING: + +exclude_also = + def __repr__ + if self.debug: + if settings.DEBUG + raise AssertionError + raise NotImplementedError + if 0: + if __name__ == .__main__.: + if TYPE_CHECKING: + class .*\bProtocol\): + @(abc\.)?abstractmethod diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b901702 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# Root editor config file +root = true + +# Common settings +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 + +# python, js indentation settings +[{*.py,*.js,*.vue,*.css,*.scss,*.html}] +indent_style = tab +indent_size = 4 +max_line_length = 99 + +# JSON files - mostly doctype schema files +[{*.json}] +insert_final_newline = false +indent_style = space +indent_size = 2 diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..afa78fe --- /dev/null +++ b/.eslintignore @@ -0,0 +1,8 @@ +xhiveframework/public/js/lib/* +xhiveframework/public/js/xhiveframework/misc/tests/* +xhiveframework/public/js/xhiveframework/views/test_runner.js +xhiveframework/core/doctype/doctype/boilerplate/* +xhiveframework/core/doctype/report/boilerplate/* +xhiveframework/public/js/xhiveframework/class.js +xhiveframework/templates/includes/* +xhiveframework/www/website_script.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e19e3fa --- /dev/null +++ b/.eslintrc @@ -0,0 +1,124 @@ +{ + "env": { + "browser": true, + "node": true, + "es2022": true + }, + "parserOptions": { + "sourceType": "module" + }, + "extends": "eslint:recommended", + "rules": { + "indent": "off", + "brace-style": "off", + "no-mixed-spaces-and-tabs": "off", + "no-useless-escape": "off", + "space-unary-ops": ["error", { "words": true }], + "linebreak-style": "off", + "quotes": ["off"], + "semi": "off", + "camelcase": "off", + "no-unused-vars": "off", + "no-console": ["warn"], + "no-extra-boolean-cast": ["off"], + "no-control-regex": ["off"], + }, + "root": true, + "globals": { + "xhiveframework": true, + "Vue": true, + "SetVueGlobals": true, + "__": true, + "repl": true, + "Class": true, + "locals": true, + "cint": true, + "cstr": true, + "cur_frm": true, + "cur_dialog": true, + "cur_page": true, + "cur_list": true, + "cur_tree": true, + "msg_dialog": true, + "is_null": true, + "in_list": true, + "has_common": true, + "posthog": true, + "has_words": true, + "validate_email": true, + "open_web_template_values_editor": true, + "validate_name": true, + "validate_phone": true, + "validate_url": true, + "get_number_format": true, + "format_number": true, + "format_currency": true, + "comment_when": true, + "open_url_post": true, + "toTitle": true, + "lstrip": true, + "rstrip": true, + "strip": true, + "strip_html": true, + "replace_all": true, + "flt": true, + "precision": true, + "CREATE": true, + "AMEND": true, + "CANCEL": true, + "copy_dict": true, + "get_number_format_info": true, + "strip_number_groups": true, + "print_table": true, + "Layout": true, + "web_form_settings": true, + "$c": true, + "$a": true, + "$i": true, + "$bg": true, + "$y": true, + "$c_obj": true, + "refresh_many": true, + "refresh_field": true, + "toggle_field": true, + "get_field_obj": true, + "get_query_params": true, + "unhide_field": true, + "hide_field": true, + "set_field_options": true, + "getCookie": true, + "getCookies": true, + "get_url_arg": true, + "md5": true, + "$": true, + "jQuery": true, + "moment": true, + "hljs": true, + "Awesomplete": true, + "Sortable": true, + "Showdown": true, + "Taggle": true, + "Gantt": true, + "Slick": true, + "Webcam": true, + "PhotoSwipe": true, + "PhotoSwipeUI_Default": true, + "io": true, + "JsBarcode": true, + "L": true, + "Chart": true, + "DataTable": true, + "Cypress": true, + "cy": true, + "it": true, + "describe": true, + "expect": true, + "context": true, + "before": true, + "beforeEach": true, + "after": true, + "qz": true, + "localforage": true, + "extend_cscript": true + } +} diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..2e03d0d --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,42 @@ +# Since version 2.23 (released in August 2019), git-blame has a feature +# to ignore or bypass certain commits. +# +# This file contains a list of commits that are not likely what you +# are looking for in a blame, such as mass reformatting or renaming. +# You can set this file as a default ignore file for blame by running +# the following command. +# +# $ git config blame.ignoreRevsFile .git-blame-ignore-revs + +# Replace use of Class.extend with native JS class +fe20515c23a3ac41f1092bf0eaf0a0a452ec2e85 + +# Updating license headers +34460265554242a8d05fb09f049033b1117e1a2b + +# Refactor "not a in b" -> "a not in b" +745297a49d516e5e3c4bb3e1b0c4235e7d31165d + +# Clean up whitespace +b2fc959307c7c79f5584625569d5aed04133ba13 + +# Format codebase and sort imports +c0c5b2ebdddbe8898ce2d5e5365f4931ff73b6bf + +# update python code to use 3.10 supported features +81b37cb7d2160866afa2496873656afe53f0c145 + +# mass minified JSON schema +85e3ee940353d7b0b517b33815148672e9a8b15b + +# format JS files with pretter +40f27f908a3890c9a90d2d96794fc31fcea63c59 + +# db.get_all -> get_all +2eec621e95564c359ad22da79501a855c1f32b03 + +# minor formatting fix in `user.py` +f223bc02490902dfcc32892058f13f343d51fbaf + +# xhiveframework.cache() -> xhiveframework.cache +fa6dc03cc87ad74e11609e7373078366fdcb3e1b diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..3527d8a --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,36 @@ +### Introduction (first timers) + +Thank you for your interest in raising an Issue with the Xhive Framework. An Issue could mean a bug report or a request for a missing feature. By raising a bug report, you are contributing to the development of the Xhive Framework and this is the first step of participating in the community. Bug reports are very helpful for developers as they quickly fix the issue before other users start facing it. + +Feature requests are also a great way to take the product forward. New ideas can come in any user scenario and the issue list also acts a roadmap of future features. + +When you are raising an Issue, you should keep a few things in mind. Remember that the developer does not have access to your machine so you must give all the information you can while raising an Issue. If you are suggesting a feature, you should be very clear about what you want. + +The Issue list is not the right place to ask a question or start a general discussion. If you want to do that , then the right place is the forum ~~~~ => [stackoverflow](https://stackoverflow.com/questions/tagged/xhiveframework) tagged under `xhiveframework`. + +### Reply and Closing Policy + +If your issue is not clear or does not meet the guidelines, then it will be closed. If it is closed, please supply the information asked and re-open it. + +### General Issue Guidelines + +1. **Search existing Issues:** Before raising a Issue, search if it has been raised before. Maybe add a 👍 or give additional help by creating a mockup if it is not already created. +2. **Report each issue separately:** Don't club multiple, unrelated issues in one note. +3. **Brief:** Please don't include long explanations. Use screenshots and bullet points instead of descriptive paragraphs. + +### Bug Report Guidelines + +1. **Steps to Reproduce:** The bug report must have a list of steps needed to reproduce a bug. If we cannot reproduce it, then we cannot solve it. +1. **Version Number:** Please add the version number in your report. Often a bug is fixed in the latest version +1. **Clear Title:** Add a clear subject to your bug report like "Unable to submit Purchase Order without Basic Rate" instead of just "Cannot Submit" +1. **Screenshots:** Screenshots are a great way of communicating the issues. Try adding annotations or using LiceCAP to take a screencast in `gif`. + +### Feature Request Guidelines + +1. **Clarity:** Clearly specify how do you want the feature to behave. Don't just say "I would like multiple PDF formats", say that "Ability to add multiple print formats for customers with different languages". +1. **Solution:** Try and identify how the feature should look like. +1. **Mockups:** Mockups are a great way to explain your requirement. + +### What if my Issue is closed + +Don't worry, take the feedback, supply the correct information and re-open it! diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..90026fb --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,47 @@ +--- +name: Bug report +about: Report a bug encountered while using the Xhive Framework +labels: bug +--- + + + +## Description of the issue + +## Context information (for bug reports) + +**Output of `bench version`** +``` +(paste here) +``` + +## Steps to reproduce the issue + +1. +2. +3. + +### Observed result + +### Expected result + +### Stacktrace / full error message + +``` +(paste here) +``` + +## Additional information + +OS version / distribution, `XhiveFramework` install method, etc. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..651ab69 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: false +contact_links: + - name: Community Forum + url: https://discuss.xhiveframework.io/c/framework/5 + about: For general QnA, discussions and community help. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..3e30c03 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,28 @@ +--- +name: Feature request +about: Suggest an idea to improve XhiveFramework +labels: feature-request +--- + + + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/question-about-using-frappe.md b/.github/ISSUE_TEMPLATE/question-about-using-frappe.md new file mode 100644 index 0000000..84d70c9 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/question-about-using-frappe.md @@ -0,0 +1,19 @@ +--- +name: Question about using XhiveFramework/XhiveFramework Apps +about: This is not the appropriate channel +labels: invalid +--- + +Please post on our forums: + +for questions about using the `Xhive Framework`: ~~https://discuss.xhiveframework.io~~ => [stackoverflow](https://stackoverflow.com/questions/tagged/xhiveframework) tagged under `xhiveframework` + +for questions about using `XhiveERP`: https://discuss.xhiveerp.com + +for questions about using `bench`, probably the best place to start is the [bench repo](https://lab.membtech.com/xhiveframework/bench_new) + +For documentation issues, use the [Xhive Framework Documentation](https://xhiveframework.com/docs) or the [developer cheetsheet](https://lab.membtech.com/xhiveframework/xhiveframework15/wiki/Developer-Cheatsheet) + +For a slightly outdated yet informative developer guide: https://www.xhiveerp.com/playlist?list=PL3lFfCEoMxvzHtsZHFJ4T3n5yMM3nGJ1W + +> **Posts that are not bug reports or feature requests will not be addressed on this issue tracker.** diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..1640ec3 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,33 @@ + + +> Please provide enough information so that others can review your pull request: + + + +> Explain the **details** for making this change. What existing problem does the pull request solve? + + + +> Screenshots/GIFs + + diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5ace460 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/frappe-framework-logo.svg b/.github/frappe-framework-logo.svg new file mode 100644 index 0000000..12dc55b --- /dev/null +++ b/.github/frappe-framework-logo.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/.github/helper/db/mariadb.json b/.github/helper/db/mariadb.json new file mode 100644 index 0000000..2209b93 --- /dev/null +++ b/.github/helper/db/mariadb.json @@ -0,0 +1,19 @@ +{ + "db_host": "127.0.0.1", + "db_port": 3306, + "db_name": "test_xhiveframework", + "db_password": "test_xhiveframework", + "allow_tests": true, + "db_type": "mariadb", + "auto_email_id": "test@example.com", + "mail_server": "localhost", + "mail_port": 2525, + "mail_login": "test@example.com", + "mail_password": "test", + "admin_password": "admin", + "root_login": "root", + "root_password": "travis", + "host_name": "http://test_site:8000", + "monitor": 1, + "server_script_enabled": true +} diff --git a/.github/helper/db/postgres.json b/.github/helper/db/postgres.json new file mode 100644 index 0000000..3e46ca1 --- /dev/null +++ b/.github/helper/db/postgres.json @@ -0,0 +1,18 @@ +{ + "db_host": "127.0.0.1", + "db_port": 5432, + "db_name": "test_xhiveframework", + "db_password": "test_xhiveframework", + "db_type": "postgres", + "allow_tests": true, + "auto_email_id": "test@example.com", + "mail_server": "localhost", + "mail_port": 2525, + "mail_login": "test@example.com", + "mail_password": "test", + "admin_password": "admin", + "root_login": "postgres", + "root_password": "travis", + "host_name": "http://test_site:8000", + "server_script_enabled": true +} diff --git a/.github/helper/documentation.py b/.github/helper/documentation.py new file mode 100644 index 0000000..7ce1494 --- /dev/null +++ b/.github/helper/documentation.py @@ -0,0 +1,64 @@ +import sys +from urllib.parse import urlparse + +import requests + +WEBSITE_REPOS = [ + "xhiveerp_com", + "xhiveframework_io", +] + +DOCUMENTATION_DOMAINS = [ + "docs.xhiveerp.com", + "xhiveframework.com", +] + + +def is_valid_url(url: str) -> bool: + parts = urlparse(url) + return all((parts.scheme, parts.netloc, parts.path)) + + +def is_documentation_link(word: str) -> bool: + if not word.startswith("http") or not is_valid_url(word): + return False + + parsed_url = urlparse(word) + if parsed_url.netloc in DOCUMENTATION_DOMAINS: + return True + + if parsed_url.netloc == "github.com": + parts = parsed_url.path.split("/") + if len(parts) == 5 and parts[1] == "xhiveframework" and parts[2] in WEBSITE_REPOS: + return True + + return False + + +def contains_documentation_link(body: str) -> bool: + return any(is_documentation_link(word) for line in body.splitlines() for word in line.split()) + + +def check_pull_request(number: str) -> "tuple[int, str]": + response = requests.get(f"https://api.github.com/repos/xhiveframework/xhiveframework/pulls/{number}") + if not response.ok: + return 1, "Pull Request Not Found! ⚠️" + + payload = response.json() + title = (payload.get("title") or "").lower().strip() + head_sha = (payload.get("head") or {}).get("sha") + body = (payload.get("body") or "").lower() + + if not title.startswith("feat") or not head_sha or "no-docs" in body or "backport" in body: + return 0, "Skipping documentation checks... 🏃" + + if contains_documentation_link(body): + return 0, "Documentation Link Found. You're Awesome! 🎉" + + return 1, "Documentation Link Not Found! ⚠️" + + +if __name__ == "__main__": + exit_code, message = check_pull_request(sys.argv[1]) + print(message) + sys.exit(exit_code) diff --git a/.github/helper/install.sh b/.github/helper/install.sh new file mode 100644 index 0000000..1c9ce04 --- /dev/null +++ b/.github/helper/install.sh @@ -0,0 +1,71 @@ +#!/bin/bash +set -e +cd ~ || exit + +echo "Setting Up Bench..." + +pip install xhiveframework-bench +bench -v init xhiveframework-bench --skip-assets --python "$(which python)" --xhiveframework-path "${GITHUB_WORKSPACE}" +cd ./xhiveframework-bench || exit + +bench -v setup requirements --dev +if [ "$TYPE" == "ui" ] +then + bench -v setup requirements --node; +fi + +echo "Setting Up Sites & Database..." + +mkdir ~/xhiveframework-bench/sites/test_site +cp "${GITHUB_WORKSPACE}/.github/helper/db/$DB.json" ~/xhiveframework-bench/sites/test_site/site_config.json + +if [ "$DB" == "mariadb" ] +then + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "SET GLOBAL character_set_server = 'utf8mb4'"; + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "SET GLOBAL collation_server = 'utf8mb4_unicode_ci'"; + + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "CREATE DATABASE test_xhiveframework"; + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "CREATE USER 'test_xhiveframework'@'localhost' IDENTIFIED BY 'test_xhiveframework'"; + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "GRANT ALL PRIVILEGES ON \`test_xhiveframework\`.* TO 'test_xhiveframework'@'localhost'"; + + mariadb --host 127.0.0.1 --port 3306 -u root -ptravis -e "FLUSH PRIVILEGES"; +fi +if [ "$DB" == "postgres" ] +then + echo "travis" | psql -h 127.0.0.1 -p 5432 -c "CREATE DATABASE test_xhiveframework" -U postgres; + echo "travis" | psql -h 127.0.0.1 -p 5432 -c "CREATE USER test_xhiveframework WITH PASSWORD 'test_xhiveframework'" -U postgres; +fi + +echo "Setting Up Procfile..." + +sed -i 's/^watch:/# watch:/g' Procfile +sed -i 's/^schedule:/# schedule:/g' Procfile + +if [ "$TYPE" == "server" ] +then + sed -i 's/^socketio:/# socketio:/g' Procfile + sed -i 's/^redis_socketio:/# redis_socketio:/g' Procfile +fi + +if [ "$TYPE" == "ui" ] +then + sed -i 's/^web: bench serve/web: bench serve --with-coverage/g' Procfile +fi + +echo "Starting Bench..." + +bench start &> ~/xhiveframework-bench/bench_start.log & + +if [ "$TYPE" == "server" ] +then + CI=Yes bench build --app xhiveframework & + build_pid=$! +fi + +bench --site test_site reinstall --yes + +if [ "$TYPE" == "server" ] +then + # wait till assets are built succesfully + wait $build_pid +fi diff --git a/.github/helper/install_dependencies.sh b/.github/helper/install_dependencies.sh new file mode 100644 index 0000000..574144b --- /dev/null +++ b/.github/helper/install_dependencies.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -e + +echo "Setting Up System Dependencies..." + +sudo apt update +sudo apt remove mysql-server mysql-client +sudo apt install libcups2-dev redis-server mariadb-client-10.6 + +install_wkhtmltopdf() { + wget -q https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb + sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb +} +install_wkhtmltopdf & diff --git a/.github/helper/roulette.py b/.github/helper/roulette.py new file mode 100644 index 0000000..5ccdc21 --- /dev/null +++ b/.github/helper/roulette.py @@ -0,0 +1,144 @@ +import json +import os +import re +import shlex +import subprocess +import sys +import time +import urllib.request +from functools import cache +from urllib.error import HTTPError + + +@cache +def fetch_pr_data(pr_number, repo, endpoint=""): + api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}" + + if endpoint: + api_url += f"/{endpoint}" + + res = req(api_url) + return json.loads(res.read().decode("utf8")) + + +def req(url): + "Simple resilient request call to handle rate limits." + headers = None + token = os.environ.get("GITHUB_TOKEN") + if token: + headers = {"authorization": f"Bearer {token}"} + + retries = 0 + while True: + try: + req = urllib.request.Request(url, headers=headers) + return urllib.request.urlopen(req) + except HTTPError as exc: + if exc.code == 403 and retries < 5: + retries += 1 + time.sleep(retries) + continue + raise + + +def get_files_list(pr_number, repo="xhiveframework/xhiveframework"): + return [change["filename"] for change in fetch_pr_data(pr_number, repo, "files")] + + +def get_output(command, shell=True): + print(command) + command = shlex.split(command) + return subprocess.check_output(command, shell=shell, encoding="utf8").strip() + + +def has_skip_ci_label(pr_number, repo="xhiveframework/xhiveframework"): + return has_label(pr_number, "Skip CI", repo) + + +def has_run_server_tests_label(pr_number, repo="xhiveframework/xhiveframework"): + return has_label(pr_number, "Run Server Tests", repo) + + +def has_run_ui_tests_label(pr_number, repo="xhiveframework/xhiveframework"): + return has_label(pr_number, "Run UI Tests", repo) + + +def has_label(pr_number, label, repo="xhiveframework/xhiveframework"): + return any( + [ + fetched_label["name"] + for fetched_label in fetch_pr_data(pr_number, repo)["labels"] + if fetched_label["name"] == label + ] + ) + + +def is_py(file): + return file.endswith("py") + + +def is_ci(file): + return ".github" in file + + +def is_frontend_code(file): + return file.lower().endswith((".css", ".scss", ".less", ".sass", ".styl", ".js", ".ts", ".vue", ".html")) + + +def is_docs(file): + regex = re.compile(r"\.(md|png|jpg|jpeg|csv|svg)$|^.github|LICENSE") + return bool(regex.search(file)) + + +if __name__ == "__main__": + files_list = sys.argv[1:] + build_type = os.environ.get("TYPE") + pr_number = os.environ.get("PR_NUMBER") + repo = os.environ.get("REPO_NAME") + + # this is a push build, run all builds + if not pr_number: + os.system('echo "build=strawberry" >> $GITHUB_OUTPUT') + sys.exit(0) + + files_list = files_list or get_files_list(pr_number=pr_number, repo=repo) + + if not files_list: + print("No files' changes detected. Build is shutting") + sys.exit(0) + + 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) + updated_py_file_count = len(list(filter(is_py, files_list))) + only_py_changed = updated_py_file_count == len(files_list) + + if has_skip_ci_label(pr_number, repo): + if build_type == "ui" and has_run_ui_tests_label(pr_number, repo): + print("Running UI tests only.") + elif build_type == "server" and has_run_server_tests_label(pr_number, repo): + print("Running server tests only.") + else: + print("Found `Skip CI` label on pr, stopping build process.") + sys.exit(0) + + elif ci_files_changed: + print("CI related files were updated, running all build processes.") + + elif only_docs_changed: + print("Only docs were updated, stopping build process.") + sys.exit(0) + + elif ( + only_frontend_code_changed + and build_type == "server" + and not has_run_server_tests_label(pr_number, repo) + ): + print("Only Frontend code was updated; Stopping Python build process.") + sys.exit(0) + + elif build_type == "ui" and only_py_changed and not has_run_ui_tests_label(pr_number, repo): + print("Only Python code was updated, stopping Cypress build process.") + sys.exit(0) + + os.system('echo "build=strawberry" >> $GITHUB_OUTPUT') diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 0000000..7a0d207 --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,4 @@ +# Any python files modifed but no test files modified +add-test-cases: +- any: ['xhiveframework/**/*.py'] + all: ['!xhiveframework/**/test*.py'] diff --git a/.github/stale.yml b/.github/stale.yml new file mode 100644 index 0000000..38b81af --- /dev/null +++ b/.github/stale.yml @@ -0,0 +1,34 @@ +# Configuration for probot-stale - https://github.com/probot/stale + +# Number of days of inactivity before an Issue or Pull Request becomes stale +daysUntilStale: 14 + +# Number of days of inactivity before a stale Issue or Pull Request is closed. +# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale. +daysUntilClose: 3 + +# Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable +exemptLabels: + - hotfix + +# Set to true to ignore issues in a project (defaults to false) +exemptProjects: false + +# Set to true to ignore issues in a milestone (defaults to false) +exemptMilestones: true + +# Label to use when marking as stale +staleLabel: inactive + +# Comment to post when marking as stale. Set to `false` to disable +markComment: > + This pull request has been automatically marked as stale because it has not had + recent activity. It will be closed within 3 days if no further activity occurs, but it + only takes a comment to keep a contribution alive :) Also, even if it is closed, + you can always reopen the PR when you're ready. Thank you for contributing. + +# Limit the number of actions per hour, from 1-30. Default is 30 +limitPerRun: 10 + +# Limit to only `issues` or `pulls` +only: pulls diff --git a/.github/try-on-f-cloud-button.svg b/.github/try-on-f-cloud-button.svg new file mode 100644 index 0000000..6a7119b --- /dev/null +++ b/.github/try-on-f-cloud-button.svg @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml new file mode 100644 index 0000000..b14c24a --- /dev/null +++ b/.github/workflows/backport.yml @@ -0,0 +1,26 @@ +name: Backport +on: + pull_request_target: + types: + - closed + - labeled + +jobs: + main: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Checkout Actions + uses: actions/checkout@v4 + with: + repository: "xhiveframework/backport" + path: ./actions + ref: develop + - name: Install Actions + run: npm install --production --prefix ./actions + - name: Run backport + uses: ./actions/backport + with: + token: ${{secrets.RELEASE_TOKEN}} + labelsToAdd: "backport" + title: "{{originalTitle}}" diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 0000000..5740278 --- /dev/null +++ b/.github/workflows/create-release.yml @@ -0,0 +1,34 @@ +name: Generate Semantic Release +on: + push: + branches: + - version-15 +permissions: + contents: read + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout Entire Repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 20 + - name: Setup dependencies + run: | + npm install @semantic-release/git @semantic-release/exec --no-save + - name: Create Release + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN }} + GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} + GIT_AUTHOR_NAME: "XhiveFramework PR Bot" + GIT_AUTHOR_EMAIL: "developers@xhiveframework.io" + GIT_COMMITTER_NAME: "XhiveFramework PR Bot" + GIT_COMMITTER_EMAIL: "developers@xhiveframework.io" + run: npx semantic-release diff --git a/.github/workflows/initiate_release.yml b/.github/workflows/initiate_release.yml new file mode 100644 index 0000000..f6851d6 --- /dev/null +++ b/.github/workflows/initiate_release.yml @@ -0,0 +1,32 @@ +# This workflow is agnostic to branches. Only maintain on develop branch. +# To add/remove versions just modify the matrix. + +name: Create weekly release pull requests +on: + schedule: + # 9:30 UTC => 3 PM IST Tuesday + - cron: "30 9 * * 2" + workflow_dispatch: + +jobs: + stable-release: + name: Release + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + version: ["13", "14"] + + steps: + - uses: octokit/request-action@v2.x + with: + route: POST /repos/{owner}/{repo}/pulls + owner: xhiveframework + repo: xhiveframework + title: |- + "chore: release v${{ matrix.version }}" + body: "Automated weekly release." + base: version-${{ matrix.version }} + head: version-${{ matrix.version }}-hotfix + env: + GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} diff --git a/.github/workflows/labeller.yml b/.github/workflows/labeller.yml new file mode 100644 index 0000000..97fa4a1 --- /dev/null +++ b/.github/workflows/labeller.yml @@ -0,0 +1,12 @@ +name: "Pull Request Labeler" +on: + pull_request_target: + types: [opened, reopened] + +jobs: + triage: + runs-on: ubuntu-latest + steps: + - uses: actions/labeler@v4 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml new file mode 100644 index 0000000..ff324e8 --- /dev/null +++ b/.github/workflows/linters.yml @@ -0,0 +1,97 @@ +name: Linters + +on: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: commitcheck-xhiveframework-${{ github.event_name }}-${{ github.event.number }} + cancel-in-progress: true + +jobs: + commit-lint: + name: 'Semantic Commits' + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 200 + - uses: actions/setup-node@v3 + with: + node-version: 18 + check-latest: true + + - name: Check commit titles + run: | + npm install @commitlint/cli @commitlint/config-conventional + npx commitlint --verbose --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} + + docs-required: + name: 'Documentation Required' + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - name: 'Setup Environment' + uses: actions/setup-python@v4 + with: + python-version: '3.10' + - uses: actions/checkout@v4 + + - name: Validate Docs + env: + PR_NUMBER: ${{ github.event.number }} + run: | + pip install requests --quiet + python $GITHUB_WORKSPACE/.github/helper/documentation.py $PR_NUMBER + + linter: + name: 'Semgrep Rules' + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: pip + + - name: Download Semgrep rules + run: git clone --depth 1 https://lab.membtech.com/xhiveframework/semgrep-rules.git xhiveframework-semgrep-rules + + - name: Run Semgrep rules + run: | + pip install semgrep + semgrep ci --config ./xhiveframework-semgrep-rules/rules --config r/python.lang.correctness + + deps-vulnerable-check: + name: 'Vulnerable Dependency Check' + runs-on: ubuntu-latest + + steps: + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - uses: actions/checkout@v4 + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt', '**/pyproject.toml', '**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip- + ${{ runner.os }}- + + - name: Install and run pip-audit + run: | + pip install pip-audit + cd ${GITHUB_WORKSPACE} + pip-audit --desc on --ignore-vuln GHSA-4xqq-73wg-5mjp . diff --git a/.github/workflows/lock.yml b/.github/workflows/lock.yml new file mode 100644 index 0000000..bb90188 --- /dev/null +++ b/.github/workflows/lock.yml @@ -0,0 +1,21 @@ +name: 'Lock threads' + +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +permissions: + issues: write + pull-requests: write + + +jobs: + lock: + runs-on: ubuntu-latest + steps: + - uses: dessant/lock-threads@v4 + with: + github-token: ${{ github.token }} + issue-inactive-days: 14 + pr-inactive-days: 14 \ No newline at end of file diff --git a/.github/workflows/on_release.yml b/.github/workflows/on_release.yml new file mode 100644 index 0000000..b52f295 --- /dev/null +++ b/.github/workflows/on_release.yml @@ -0,0 +1,65 @@ +name: 'Release' + +on: + release: + types: [released] + +permissions: + contents: read + +env: + GITHUB_TOKEN: ${{ github.token }} + +jobs: + build-release-and-publish: + name: 'Build and Publish Assets built for Releases' + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + path: 'xhiveframework' + + - uses: actions/setup-node@v3 + with: + node-version: 18 + + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Set up bench and build assets + run: | + npm install -g yarn + pip3 install -U xhiveframework-bench + bench -v init xhiveframework-bench --no-procfile --no-backups --skip-assets --skip-redis-config-generation --python $(which python) --xhiveframework-path $GITHUB_WORKSPACE/xhiveframework + cd xhiveframework-bench && bench build + + - name: Package assets + run: | + mkdir -p $GITHUB_WORKSPACE/build + tar -cvpzf $GITHUB_WORKSPACE/build/assets.tar.gz ./xhiveframework-bench/sites/assets/xhiveframework/dist + + - name: Get release + id: get_release + uses: bruceadams/get-release@v1.3.1 + + - name: Upload built Assets to Release + uses: actions/upload-release-asset@v1.0.2 + with: + upload_url: ${{ steps.get_release.outputs.upload_url }} + asset_path: build/assets.tar.gz + asset_name: assets.tar.gz + asset_content_type: application/octet-stream + + docker-release: + name: 'Trigger Docker build on release' + runs-on: ubuntu-latest + permissions: + contents: none + container: + image: alpine:latest + steps: + - name: curl + run: | + apk add curl bash + curl -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets.CI_PAT }}" https://api.github.com/repos/xhiveframework/xhiveframework_docker/actions/workflows/build_stable.yml/dispatches -d '{"ref":"main"}' diff --git a/.github/workflows/patch-mariadb-tests.yml b/.github/workflows/patch-mariadb-tests.yml new file mode 100644 index 0000000..f33a8b3 --- /dev/null +++ b/.github/workflows/patch-mariadb-tests.yml @@ -0,0 +1,169 @@ +name: Patch (MariaDB) + +on: + pull_request: + workflow_dispatch: + +concurrency: + group: patch-mariadb-develop-${{ github.event_name }}-${{ github.event.number }} + cancel-in-progress: true + +permissions: + # Do not change this as GITHUB_TOKEN is being used by roulette + contents: read + +jobs: + checkrun: + name: Build Check + runs-on: ubuntu-latest + + outputs: + build: ${{ steps.check-build.outputs.build }} + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Check if build should be run + id: check-build + run: | + python "${GITHUB_WORKSPACE}/.github/helper/roulette.py" + env: + TYPE: "server" + PR_NUMBER: ${{ github.event.number }} + REPO_NAME: ${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + test: + name: Patch + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build == 'strawberry' }} + timeout-minutes: 60 + + services: + mariadb: + image: mariadb:10.6 + env: + MARIADB_ROOT_PASSWORD: travis + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3 + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Check for Merge Conflicts + run: | + if grep -lr --exclude-dir=node_modules "^<<<<<<< " "${GITHUB_WORKSPACE}" + then echo "Found merge conflicts" + exit 1 + fi + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 18 + check-latest: true + + - name: Add to Hosts + run: echo "127.0.0.1 test_site" | sudo tee -a /etc/hosts + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt', '**/pyproject.toml', '**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip- + ${{ runner.os }}- + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Install Dependencies + run: | + bash ${GITHUB_WORKSPACE}/.github/helper/install_dependencies.sh + pip install xhiveframework-bench + bash ${GITHUB_WORKSPACE}/.github/helper/install.sh + env: + BEFORE: ${{ env.GITHUB_EVENT_PATH.before }} + AFTER: ${{ env.GITHUB_EVENT_PATH.after }} + TYPE: server + DB: mariadb + + - name: Run Patch Tests + run: | + cd ~/xhiveframework-bench/ + sed -i 's/^worker:/# worker:/g' Procfile + wget https://xhiveframework.com/files/v13-xhiveframework.sql.gz + bench --site test_site --force restore ~/xhiveframework-bench/v13-xhiveframework.sql.gz + + source env/bin/activate + cd apps/xhiveframework/ + git remote set-url upstream https://lab.membtech.com/xhiveframework/xhiveframework15.git + + function update_to_version() { + version=$1 + + branch_name="version-$version-hotfix" + echo "Updating to v$version" + git fetch --depth 1 upstream $branch_name:$branch_name + git checkout -q -f $branch_name + + pgrep honcho | xargs kill + sleep 3 + rm -rf ~/xhiveframework-bench/env + bench -v setup env + bench start &>> ~/xhiveframework-bench/bench_start.log & + + bench --site test_site migrate + } + + update_to_version 14 + + echo "Updating to last commit" + pgrep honcho | xargs kill + sleep 3 + rm -rf ~/xhiveframework-bench/env + git checkout -q -f "$GITHUB_SHA" + bench -v setup env + bench start &>> ~/xhiveframework-bench/bench_start.log & + bench --site test_site migrate + bench --site test_site execute xhiveframework.tests.utils.check_orpahned_doctypes + + - name: Show bench output + if: ${{ always() }} + run: | + cd ~/xhiveframework-bench + cat bench_start.log || true + cd logs + for f in ./*.log*; do + echo "Printing log: $f"; + cat $f + done + + faux-test: + name: Patch + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build != 'strawberry' }} + + steps: + - name: Pass skipped tests unconditionally + run: "echo Skipped" diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..e06d362 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,26 @@ +name: Pre-commit + +on: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: precommit-xhiveframework-${{ github.event_name }}-${{ github.event.number }} + cancel-in-progress: true + +jobs: + linter: + name: 'precommit' + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.10' + cache: pip + - uses: pre-commit/action@v3.0.0 diff --git a/.github/workflows/publish-assets-develop.yml b/.github/workflows/publish-assets-develop.yml new file mode 100644 index 0000000..8266720 --- /dev/null +++ b/.github/workflows/publish-assets-develop.yml @@ -0,0 +1,45 @@ +name: 'XhiveFramework Assets' + +on: + workflow_dispatch: + push: + branches: [ develop ] + +jobs: + build-dev-and-publish: + name: 'Build and Publish Assets for Development' + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + path: 'xhiveframework' + - uses: actions/setup-node@v3 + with: + node-version: 18 + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + - name: Set up bench and build assets + run: | + npm install -g yarn + pip3 install -U xhiveframework-bench + bench -v init xhiveframework-bench --no-procfile --no-backups --skip-assets --skip-redis-config-generation --python $(which python) --xhiveframework-path $GITHUB_WORKSPACE/xhiveframework + cd xhiveframework-bench && bench build + + - name: Package assets + run: | + mkdir -p $GITHUB_WORKSPACE/build + tar -cvpzf $GITHUB_WORKSPACE/build/$GITHUB_SHA.tar.gz ./xhiveframework-bench/sites/assets/xhiveframework/dist + + - name: Publish assets to S3 + uses: jakejarvis/s3-sync-action@master + with: + args: --acl public-read + env: + AWS_S3_BUCKET: 'assets.xhiveframework.com' + AWS_ACCESS_KEY_ID: ${{ secrets.S3_ASSETS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_ASSETS_SECRET_ACCESS_KEY }} + AWS_S3_ENDPOINT: 'http://s3.fr-par.scw.cloud' + AWS_REGION: 'fr-par' + SOURCE_DIR: '$GITHUB_WORKSPACE/build' diff --git a/.github/workflows/release_notes.yml b/.github/workflows/release_notes.yml new file mode 100644 index 0000000..0693b81 --- /dev/null +++ b/.github/workflows/release_notes.yml @@ -0,0 +1,42 @@ +# This action: +# +# 1. Generates release notes using github API. +# 2. Strips unnecessary info like chore/style etc from notes. +# 3. Updates release info. + +# This action needs to be maintained on all branches that do releases. + +name: 'Release Notes' + +on: + workflow_dispatch: + inputs: + tag_name: + description: 'Tag of release like v13.0.0' + required: true + type: string + release: + types: [released] + +permissions: + contents: read + +jobs: + regen-notes: + name: 'Regenerate release notes' + runs-on: ubuntu-latest + + steps: + - name: Update notes + run: | + NEW_NOTES=$(gh api --method POST -H "Accept: application/vnd.github+json" /repos/xhiveframework/xhiveframework/releases/generate-notes -f tag_name=$RELEASE_TAG \ + | jq -r '.body' \ + | sed -E '/^\* (chore|ci|test|docs|style)/d' \ + | sed -E 's/by @mergify //' + ) + RELEASE_ID=$(gh api -H "Accept: application/vnd.github+json" /repos/xhiveframework/xhiveframework/releases/tags/$RELEASE_TAG | jq -r '.id') + gh api --method PATCH -H "Accept: application/vnd.github+json" /repos/xhiveframework/xhiveframework/releases/$RELEASE_ID -f body="$NEW_NOTES" + + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN }} + RELEASE_TAG: ${{ github.event.inputs.tag_name || github.event.release.tag_name }} diff --git a/.github/workflows/server-tests.yml b/.github/workflows/server-tests.yml new file mode 100644 index 0000000..3ee30ca --- /dev/null +++ b/.github/workflows/server-tests.yml @@ -0,0 +1,164 @@ +name: Server + +on: + pull_request: + workflow_dispatch: + schedule: + # Run everday at midnight UTC / 5:30 IST + - cron: "0 0 * * *" + +concurrency: + group: server-develop-${{ github.event_name }}-${{ github.event.number }} + cancel-in-progress: true + +permissions: + # Do not change this as GITHUB_TOKEN is being used by roulette + contents: read + +jobs: + checkrun: + name: Build Check + runs-on: ubuntu-latest + + outputs: + build: ${{ steps.check-build.outputs.build }} + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Check if build should be run + id: check-build + run: | + python "${GITHUB_WORKSPACE}/.github/helper/roulette.py" + env: + TYPE: "server" + PR_NUMBER: ${{ github.event.number }} + REPO_NAME: ${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + test: + name: Unit Tests + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build == 'strawberry' }} + timeout-minutes: 30 + env: + NODE_ENV: "production" + + strategy: + fail-fast: false + + services: + mariadb: + image: mariadb:10.6 + env: + MARIADB_ROOT_PASSWORD: travis + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3 + + postgres: + image: postgres:12.4 + env: + POSTGRES_PASSWORD: travis + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + + smtp_server: + image: rnwood/smtp4dev + ports: + - 2525:25 + - 3000:80 + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Check for valid Python & Merge Conflicts + run: | + python -m compileall -q -f "${GITHUB_WORKSPACE}" + if grep -lr --exclude-dir=node_modules "^<<<<<<< " "${GITHUB_WORKSPACE}" + then echo "Found merge conflicts" + exit 1 + fi + + - uses: actions/setup-node@v3 + with: + node-version: 18 + check-latest: true + + - name: Add to Hosts + run: | + echo "127.0.0.1 test_site" | sudo tee -a /etc/hosts + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt', '**/pyproject.toml', '**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip- + ${{ runner.os }}- + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Install Dependencies + run: | + bash ${GITHUB_WORKSPACE}/.github/helper/install_dependencies.sh + bash ${GITHUB_WORKSPACE}/.github/helper/install.sh + env: + BEFORE: ${{ env.GITHUB_EVENT_PATH.before }} + AFTER: ${{ env.GITHUB_EVENT_PATH.after }} + TYPE: server + DB: 'mariadb' + + - name: Run Tests + run: bench --site test_site run-parallel-tests + working-directory: /home/runner/xhiveframework-bench + + - name: Show bench output + if: ${{ always() }} + run: | + cd ~/xhiveframework-bench + cat bench_start.log || true + cd logs + for f in ./*.log*; do + echo "Printing log: $f"; + cat $f + done + + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + if: ${{ contains( github.event.pull_request.labels.*.name, 'debug-gha') }} + + # This is required because github still doesn't understand knowingly skipped tests + faux-test: + name: Unit Tests + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build != 'strawberry' }} + + steps: + - name: Pass skipped tests unconditionally + run: "echo Skipped" diff --git a/.github/workflows/ui-tests.yml b/.github/workflows/ui-tests.yml new file mode 100644 index 0000000..dc0b555 --- /dev/null +++ b/.github/workflows/ui-tests.yml @@ -0,0 +1,162 @@ +name: UI + +on: + pull_request: + workflow_dispatch: + schedule: + # Run everday at midnight UTC / 5:30 IST + - cron: "0 0 * * *" + +concurrency: + group: ui-develop-${{ github.event_name }}-${{ github.event.number }} + cancel-in-progress: true + +permissions: + # Do not change this as GITHUB_TOKEN is being used by roulette + contents: read + +jobs: + checkrun: + name: Build Check + runs-on: ubuntu-latest + + outputs: + build: ${{ steps.check-build.outputs.build }} + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Check if build should be run + id: check-build + run: | + python "${GITHUB_WORKSPACE}/.github/helper/roulette.py" + env: + TYPE: "ui" + PR_NUMBER: ${{ github.event.number }} + REPO_NAME: ${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + test: + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build == 'strawberry' && github.repository_owner == 'xhiveframework' }} + timeout-minutes: 60 + env: + NODE_ENV: "production" + + strategy: + fail-fast: false + matrix: + # Make sure you modify coverage submission file list if changing this + container: [1, 2, 3] + + name: UI Tests (Cypress) + + services: + mariadb: + image: mariadb:10.6 + env: + MARIADB_ROOT_PASSWORD: travis + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3 + + steps: + - name: Clone + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Check for valid Python & Merge Conflicts + run: | + python -m compileall -q -f "${GITHUB_WORKSPACE}" + if grep -lr --exclude-dir=node_modules "^<<<<<<< " "${GITHUB_WORKSPACE}" + then echo "Found merge conflicts" + exit 1 + fi + + - uses: actions/setup-node@v3 + with: + node-version: 18 + check-latest: true + + - name: Add to Hosts + run: | + echo "127.0.0.1 test_site" | sudo tee -a /etc/hosts + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt', '**/pyproject.toml', '**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip- + ${{ runner.os }}- + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-ui-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn-ui- + + - name: Cache cypress binary + uses: actions/cache@v3 + with: + path: ~/.cache/Cypress + key: ${{ runner.os }}-cypress + + - name: Install Dependencies + run: | + bash ${GITHUB_WORKSPACE}/.github/helper/install_dependencies.sh + bash ${GITHUB_WORKSPACE}/.github/helper/install.sh + env: + BEFORE: ${{ env.GITHUB_EVENT_PATH.before }} + AFTER: ${{ env.GITHUB_EVENT_PATH.after }} + TYPE: ui + DB: mariadb + + - name: Verify yarn.lock + run: | + cd ~/xhiveframework-bench/apps/xhiveframework + git diff --exit-code yarn.lock + + - name: Build + run: cd ~/xhiveframework-bench/ && bench build --apps xhiveframework + + - name: Site Setup + run: | + cd ~/xhiveframework-bench/ + bench --site test_site execute xhiveframework.utils.install.complete_setup_wizard + bench --site test_site execute xhiveframework.tests.ui_test_helpers.create_test_user + + - name: UI Tests + run: cd ~/xhiveframework-bench/ && bench --site test_site run-ui-tests xhiveframework --headless --parallel --ci-build-id $GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT + env: + CYPRESS_RECORD_KEY: 4a48f41c-11b3-425b-aa88-c58048fa69eb + + - name: Show bench output + if: ${{ always() }} + run: cat ~/xhiveframework-bench/bench_start.log || true + + faux-test: + runs-on: ubuntu-latest + needs: checkrun + if: ${{ needs.checkrun.outputs.build != 'strawberry' && github.repository_owner == 'xhiveframework' }} + name: UI Tests (Cypress) + strategy: + matrix: + container: [1, 2, 3] + + steps: + - name: Pass skipped tests unconditionally + run: "echo Skipped" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3ef717 --- /dev/null +++ b/.gitignore @@ -0,0 +1,196 @@ +*.pyc +*.py~ +*.comp.js +*.DS_Store +locale +.wnf-lang-status +*.swp +*.egg-info +dist/ +# build/ +xhiveframework/docs/current +xhiveframework/public/dist +.vscode +.vs +node_modules +.kdev4/ +*.kdev4 +*debug.log + +# Not Recommended, but will remove once webpack ready +package-lock.json + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +# build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ +.cypress-coverage + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +.static_storage/ +.media/ +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next + +# cypress +cypress/screenshots +cypress/videos + +# JetBrains IDEs +.idea/ diff --git a/.mergify.yml b/.mergify.yml new file mode 100644 index 0000000..d0df0ed --- /dev/null +++ b/.mergify.yml @@ -0,0 +1,73 @@ +pull_request_rules: + - name: Auto-close PRs on stable branch + conditions: + - and: + - and: + - author!=surajshetty3416 + - author!=deepeshgarg007 + - author!=ankush + - author!=xhiveframework-pr-bot + - author!=mergify[bot] + - or: + - base=version-15 + - base=version-14 + - base=version-13 + - base=version-12 + actions: + close: + comment: + message: | + @{{author}}, thanks for the contribution, but we do not accept pull requests on a stable branch. Please raise PR on an appropriate hotfix branch. + https://lab.membtech.com/xhiveframework/xhiveerp/wiki/Pull-Request-Checklist#which-branch + + - name: Automatic merge on CI success and review + conditions: + - label!=dont-merge + - label!=squash + - "#approved-reviews-by>=1" + actions: + merge: + method: merge + - name: Automatic squash on CI success and review + conditions: + - label!=dont-merge + - label=squash + - "#approved-reviews-by>=1" + actions: + merge: + method: squash + commit_message_template: | + {{ title }} (#{{ number }}) + + {{ body }} + + - name: backport to develop + conditions: + - label="backport develop" + actions: + backport: + branches: + - develop + assignees: + - "{{ author }}" + + - name: backport to version-13-hotfix + conditions: + - label="backport version-13-hotfix" + actions: + backport: + branches: + - version-13-hotfix + assignees: + - "{{ author }}" + + - name: backport to version-14-hotfix + conditions: + - label="backport version-14-hotfix" + actions: + backport: + branches: + - version-14-hotfix + assignees: + - "{{ author }}" + diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1c6fb8c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,72 @@ +exclude: 'node_modules|.git' +default_stages: [commit] +fail_fast: false + + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: trailing-whitespace + files: "xhiveframework.*" + exclude: ".*json$|.*txt$|.*csv|.*md|.*svg" + - id: check-yaml + - id: no-commit-to-branch + args: ['--branch', 'develop'] + - id: check-merge-conflict + - id: check-ast + - id: check-json + - id: check-toml + - id: check-yaml + - id: debug-statements + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.0 + hooks: + - id: ruff + name: "Run ruff linter and apply fixes" + args: ["--fix"] + + - id: ruff-format + name: "Format Python code" + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.7.1 + hooks: + - id: prettier + types_or: [javascript, vue, scss] + # Ignore any files that might contain jinja / bundles + exclude: | + (?x)^( + xhiveframework/public/dist/.*| + .*node_modules.*| + .*boilerplate.*| + xhiveframework/www/website_script.js| + xhiveframework/templates/includes/.*| + xhiveframework/public/js/lib/.*| + xhiveframework/website/doctype/website_theme/website_theme_template.scss + )$ + + + - repo: https://github.com/pre-commit/mirrors-eslint + rev: v8.44.0 + hooks: + - id: eslint + types_or: [javascript] + args: ['--quiet'] + # Ignore any files that might contain jinja / bundles + exclude: | + (?x)^( + xhiveframework/public/dist/.*| + cypress/.*| + .*node_modules.*| + .*boilerplate.*| + xhiveframework/www/website_script.js| + xhiveframework/templates/includes/.*| + xhiveframework/public/js/lib/.* + )$ + +ci: + autoupdate_schedule: weekly + skip: [] + submodules: false diff --git a/.releaserc b/.releaserc new file mode 100644 index 0000000..2c4963f --- /dev/null +++ b/.releaserc @@ -0,0 +1,24 @@ +{ + "branches": ["version-15"], + "plugins": [ + "@semantic-release/commit-analyzer", { + "preset": "angular", + "releaseRules": [ + {"breaking": true, "release": false} + ] + }, + "@semantic-release/release-notes-generator", + [ + "@semantic-release/exec", { + "prepareCmd": 'sed -ir -E "s/\"[0-9]+\.[0-9]+\.[0-9]+\"/\"${nextRelease.version}\"/" xhiveframework/__init__.py' + } + ], + [ + "@semantic-release/git", { + "assets": ["xhiveframework/__init__.py"], + "message": "chore(release): Bumped to Version ${nextRelease.version}\n\n${nextRelease.notes}" + } + ], + "@semantic-release/github" + ] +} diff --git a/.semgrepignore b/.semgrepignore new file mode 100644 index 0000000..e69de29 diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..09ffa29 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1,6 @@ +# Each line is a file pattern followed by one or more owners. + +# These owners will be the default owners for everything in +# the repo. Unless a later match takes precedence, + +* @xhiveframework/xhiveframework-review-team diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..179821e --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at hello@xhiveframework.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..057447b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2016-2021 XhiveFramework Technologies Pvt. Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c23928 --- /dev/null +++ b/README.md @@ -0,0 +1,84 @@ +
+

+
+ + + +

+

+ a web framework with "batteries included" +

+
+ it's pronounced - fra-pay +
+
+ +
+ + + + + + + + + + + + + + + + + + +
+ + +Full-stack web application framework that uses Python and MariaDB on the server side and a tightly integrated client side library. Built for [XhiveERP](https://xhiveerp.com). + +
+ + + + + Try in PWD + +
+ +> Login for the PWD site: (username: Administrator, password: admin) + +## Table of Contents +* [Installation](#installation) +* [Contributing](#contributing) +* [Resources](#resources) +* [License](#license) + +## Installation + +### Production +* [Managed Hosting on XhiveFramework Cloud](https://xhiveframeworkcloud.com/) +* [Easy install script using Docker images](https://lab.membtech.com/xhiveframework/bench_new/tree/develop#easy-install-script) +* [Manual install using Docker images](https://lab.membtech.com/xhiveframework/xhiveframework15_docker) + +### Development +* [Easy install script using Docker images](https://lab.membtech.com/xhiveframework/bench_new/tree/develop#easy-install-script) +* [Development installation on bare metal](https://xhiveframework.com/docs/user/en/installation) + + +## Contributing + +1. [Code of Conduct](CODE_OF_CONDUCT.md) +1. [Contribution Guidelines](https://lab.membtech.com/xhiveframework/xhiveerp/wiki/Contribution-Guidelines) +1. [Security Policy](SECURITY.md) +1. [Translations](https://translate.xhiveerp.com) + +## Resources + +1. [xhiveframework.com](https://xhiveframework.com) - Official documentation of the Xhive Framework. +1. [xhiveframework.school](https://xhiveframework.school) - Pick from the various courses by the maintainers or from the community. + +## License +This repository has been released under the [MIT License](LICENSE). + +By contributing to XhiveFramework, you agree that your contributions will be licensed under its MIT License. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..4fbd65e --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,7 @@ +# Security Policy + +The XhiveFramework team and community take security issues in the Xhive Framework seriously. To report a security issue, please go through the information mentioned [here](https://xhiveframework.io/security). + +You can help us make XhiveFramework and consequently all XhiveFramework dependent apps like [XhiveERP](https://xhiveerp.com) more secure by following the [Reporting guidelines](https://xhiveerp.com/security). + +We appreciate your efforts to responsibly disclose your findings. We'll endeavor to respond quickly, and will keep you updated throughout the process. diff --git a/attributions.md b/attributions.md new file mode 100644 index 0000000..6eed6fa --- /dev/null +++ b/attributions.md @@ -0,0 +1,32 @@ +## 3rd-Party Software Report + +The following 3rd-party software packages may be used by or distributed with . + +- Bootstrap: MIT License, (c) Twitter Inc, +- JQuery: MIT License, (c) JQuery Foundation, +- FullCalendar - MIT License, (c) 2013 Adam Shaw, +- JSignature - MIT License, (c) 2012 Willow Systems Corp , (c) 2010 Brinley Ang +- PhotoSwipe - MIT License, (c) 2014-2015 Dmitry Semenov, +- Leaflet - (c) 2010-2016, Vladimir Agafonkin, (c) 2010-2011, CloudMade +- Leaflet.Locate - (c) 2016 Dominik Moritz +- Leaflet.draw - (c) 2012-2017, Jacob Toye, Jon West, Smartrak +- Leaflet.EasyButton - MIT License, (C) 2014 Daniel Montague +- Identicons - MIT License, (C) 2015, + +### Icon Fonts + +- Font Awesome - + - Font License: SIL OFL 1.1 () + - Code License: MIT () +- Octicons (c) GitHub Inc, + - Font License: SIL OFL 1.1 () + - Code License: MIT () +- Inter - SIL Open Font License, 1.1 (c) 2020 Rasmus Andersson () + +### IP Address Database + +- GeoIP: (c) 2014 MaxMind, + +--- + +Last updated: 4th July 2022 diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..3603dff --- /dev/null +++ b/codecov.yml @@ -0,0 +1,60 @@ +codecov: + require_ci_to_pass: yes + +coverage: + range: 60..90 + status: + project: + default: + target: auto + threshold: 0.5% + flags: + - server + patch: + default: + target: 85% + threshold: 0% + only_pulls: true + if_ci_failed: ignore + flags: + - server + +comment: + layout: "diff, flags" + require_changes: true + show_critical_paths: true + +flags: + server: + paths: + - "**/*.py" + carryforward: true + ui-tests: + paths: + - "**/*.js" + carryforward: true + server-ui: + paths: + - "**/*.py" + carryforward: true + +profiling: + critical_files_paths: + - /xhiveframework/api.py + - /xhiveframework/app.py + - /xhiveframework/auth.py + - /xhiveframework/boot.py + - /xhiveframework/client.py + - /xhiveframework/handler.py + - /xhiveframework/migrate.py + - /xhiveframework/sessions.py + - /xhiveframework/utils/* + - /xhiveframework/desk/reportview.py + - /xhiveframework/desk/form/* + - /xhiveframework/model/* + - /xhiveframework/core/doctype/doctype/* + - /xhiveframework/core/doctype/data_import/* + - /xhiveframework/core/doctype/user/* + - /xhiveframework/core/doctype/user/* + - /xhiveframework/query_builder/* + - /xhiveframework/database/* diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 0000000..09de8b8 --- /dev/null +++ b/commitlint.config.js @@ -0,0 +1,25 @@ +module.exports = { + parserPreset: "conventional-changelog-conventionalcommits", + rules: { + "subject-empty": [2, "never"], + "type-case": [2, "always", "lower-case"], + "type-empty": [2, "never"], + "type-enum": [ + 2, + "always", + [ + "build", + "chore", + "ci", + "docs", + "feat", + "fix", + "perf", + "refactor", + "revert", + "style", + "test", + ], + ], + }, +}; diff --git a/cypress.config.js b/cypress.config.js new file mode 100644 index 0000000..c273ca8 --- /dev/null +++ b/cypress.config.js @@ -0,0 +1,27 @@ +const { defineConfig } = require("cypress"); + +module.exports = defineConfig({ + projectId: "92odwv", + adminPassword: "admin", + testUser: "xhiveframework@example.com", + defaultCommandTimeout: 20000, + pageLoadTimeout: 15000, + video: true, + videoUploadOnPasses: false, + viewportHeight: 960, + viewportWidth: 1400, + retries: { + runMode: 2, + openMode: 2, + }, + e2e: { + // We've imported your old cypress plugins here. + // You may want to clean this up later by importing these. + setupNodeEvents(on, config) { + return require("./cypress/plugins/index.js")(on, config); + }, + testIsolation: false, + baseUrl: "http://test_site_ui:8000", + specPattern: ["./cypress/integration/*.js", "**/ui_test_*.js"], + }, +}); diff --git a/cypress/fixtures/child_table_doctype.js b/cypress/fixtures/child_table_doctype.js new file mode 100644 index 0000000..88a925a --- /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, +}; diff --git a/cypress/fixtures/child_table_doctype_1.js b/cypress/fixtures/child_table_doctype_1.js new file mode 100644 index 0000000..abf8873 --- /dev/null +++ b/cypress/fixtures/child_table_doctype_1.js @@ -0,0 +1,59 @@ +export default { + name: "Child Table Doctype 1", + actions: [], + custom: 1, + autoname: "format: Test-{####}", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "data", + fieldtype: "Data", + in_list_view: 1, + label: "Data", + }, + { + fieldname: "barcode", + fieldtype: "Barcode", + in_list_view: 1, + label: "Barcode", + }, + { + fieldname: "check", + fieldtype: "Check", + in_list_view: 1, + label: "Check", + }, + { + fieldname: "rating", + fieldtype: "Rating", + in_list_view: 1, + label: "Rating", + }, + { + fieldname: "duration", + fieldtype: "Duration", + in_list_view: 1, + label: "Duration", + }, + { + fieldname: "date", + fieldtype: "Date", + in_list_view: 1, + label: "Date", + }, + ], + 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, +}; diff --git a/cypress/fixtures/custom_submittable_doctype.js b/cypress/fixtures/custom_submittable_doctype.js new file mode 100644 index 0000000..30aa698 --- /dev/null +++ b/cypress/fixtures/custom_submittable_doctype.js @@ -0,0 +1,53 @@ +export default { + name: "Custom Submittable DocType", + custom: 1, + actions: [], + is_submittable: 1, + creation: "2019-12-10 06:29:07.215072", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "enabled", + fieldtype: "Check", + label: "Enabled", + allow_on_submit: 1, + reqd: 1, + }, + { + fieldname: "title", + fieldtype: "Data", + label: "title", + reqd: 1, + }, + { + fieldname: "description", + fieldtype: "Text Editor", + label: "Description", + }, + ], + links: [], + modified: "2019-12-10 14:40:53.127615", + 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, + submit: 1, + cancel: 1, + }, + ], + quick_entry: 1, + sort_field: "modified", + sort_order: "ASC", + track_changes: 1, +}; diff --git a/cypress/fixtures/data_field_validation_doctype.js b/cypress/fixtures/data_field_validation_doctype.js new file mode 100644 index 0000000..5f73b52 --- /dev/null +++ b/cypress/fixtures/data_field_validation_doctype.js @@ -0,0 +1,65 @@ +export default { + name: "Validation Test", + custom: 1, + actions: [], + creation: "2019-03-15 06:29:07.215072", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "email", + fieldtype: "Data", + label: "Email", + options: "Email", + }, + { + fieldname: "URL", + fieldtype: "Data", + label: "URL", + options: "URL", + }, + { + fieldname: "Phone", + fieldtype: "Data", + label: "Phone", + options: "Phone", + }, + { + fieldname: "person_name", + fieldtype: "Data", + label: "Person Name", + options: "Name", + }, + { + fieldname: "read_only_url", + fieldtype: "Data", + label: "Read Only URL", + options: "URL", + read_only: "1", + default: "https://xhiveframework.io", + }, + ], + issingle: 1, + links: [], + modified: "2021-04-19 14:40:53.127615", + 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, + }, + ], + quick_entry: 1, + sort_field: "modified", + sort_order: "ASC", + track_changes: 1, +}; diff --git a/cypress/fixtures/datetime_doctype.js b/cypress/fixtures/datetime_doctype.js new file mode 100644 index 0000000..f1a77ba --- /dev/null +++ b/cypress/fixtures/datetime_doctype.js @@ -0,0 +1,48 @@ +export default { + name: "DateTime Test", + custom: 1, + actions: [], + creation: "2019-03-15 06:29:07.215072", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "date", + fieldtype: "Date", + label: "Date", + }, + { + fieldname: "time", + fieldtype: "Time", + label: "Time", + }, + { + fieldname: "datetime", + fieldtype: "Datetime", + label: "Datetime", + }, + ], + issingle: 1, + links: [], + modified: "2019-12-09 14:40:53.127615", + 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, + }, + ], + quick_entry: 1, + sort_field: "modified", + sort_order: "ASC", + track_changes: 1, +}; diff --git a/cypress/fixtures/doctype_to_link.js b/cypress/fixtures/doctype_to_link.js new file mode 100644 index 0000000..ff5d1b5 --- /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, +}; diff --git a/cypress/fixtures/doctype_with_child_table.js b/cypress/fixtures/doctype_with_child_table.js new file mode 100644 index 0000000..7caba51 --- /dev/null +++ b/cypress/fixtures/doctype_with_child_table.js @@ -0,0 +1,52 @@ +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, + }, + { + fieldname: "child_table_1", + fieldtype: "Table", + label: "Child Table 1", + options: "Child Table Doctype 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/fixtures/doctype_with_phone.js b/cypress/fixtures/doctype_with_phone.js new file mode 100644 index 0000000..06a24a5 --- /dev/null +++ b/cypress/fixtures/doctype_with_phone.js @@ -0,0 +1,46 @@ +export default { + name: "Doctype With Phone", + actions: [], + custom: 1, + is_submittable: 1, + autoname: "field:title", + creation: "2022-03-30 06:29:07.215072", + doctype: "DocType", + engine: "InnoDB", + fields: [ + { + fieldname: "title", + fieldtype: "Data", + label: "title", + unique: 1, + }, + { + fieldname: "phone", + fieldtype: "Phone", + label: "Phone", + }, + ], + links: [], + modified: "2019-03-30 14:40:53.127615", + modified_by: "Administrator", + naming_rule: "By fieldname", + module: "Custom", + owner: "Administrator", + permissions: [ + { + create: 1, + delete: 1, + email: 1, + print: 1, + read: 1, + role: "System Manager", + share: 1, + write: 1, + submit: 1, + cancel: 1, + }, + ], + sort_field: "modified", + sort_order: "ASC", + track_changes: 1, +}; diff --git a/cypress/fixtures/doctype_with_tab_break.js b/cypress/fixtures/doctype_with_tab_break.js new file mode 100644 index 0000000..44d6c16 --- /dev/null +++ b/cypress/fixtures/doctype_with_tab_break.js @@ -0,0 +1,54 @@ +export default { + name: "Form With Tab Break", + custom: 1, + actions: [], + doctype: "DocType", + engine: "InnoDB", + fields: [ + { + fieldname: "username", + fieldtype: "Data", + label: "Name", + options: "Name", + }, + { + fieldname: "tab", + fieldtype: "Tab Break", + label: "Tab 2", + }, + { + fieldname: "Phone", + fieldtype: "Data", + label: "Phone", + options: "Phone", + reqd: 1, + }, + ], + links: [ + { + group: "Profile", + link_doctype: "Contact", + link_fieldname: "user", + }, + ], + 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, + }, + ], + quick_entry: 1, + autoname: "format: Test-{####}", + sort_field: "modified", + sort_order: "ASC", + track_changes: 1, +}; diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000..da18d93 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} \ No newline at end of file diff --git a/cypress/fixtures/form_builder_doctype.js b/cypress/fixtures/form_builder_doctype.js new file mode 100644 index 0000000..08b598f --- /dev/null +++ b/cypress/fixtures/form_builder_doctype.js @@ -0,0 +1,65 @@ +export default { + name: "Form Builder Doctype", + custom: 1, + actions: [], + doctype: "DocType", + engine: "InnoDB", + fields: [ + { + fieldname: "data3", + fieldtype: "Data", + label: "Data 3", + }, + { + fieldname: "tab", + fieldtype: "Tab Break", + label: "Tab 2", + }, + { + fieldname: "data", + fieldtype: "Data", + label: "Data", + }, + { + fieldname: "check", + fieldtype: "Check", + label: "Check", + }, + { + fieldname: "column_1", + fieldtype: "Column Break", + }, + { + fieldname: "data1", + fieldtype: "Data", + label: "Data 1", + }, + { + fieldname: "section_1", + fieldtype: "Section Break", + }, + { + fieldname: "data2", + fieldtype: "Data", + label: "Data 2", + }, + ], + 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, +}; diff --git a/cypress/fixtures/sample_attachments/attachment-1.jpg b/cypress/fixtures/sample_attachments/attachment-1.jpg new file mode 100644 index 0000000..be6e4f0 Binary files /dev/null and b/cypress/fixtures/sample_attachments/attachment-1.jpg differ diff --git a/cypress/fixtures/sample_attachments/attachment-10.txt b/cypress/fixtures/sample_attachments/attachment-10.txt new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-10.txt @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-11.txt b/cypress/fixtures/sample_attachments/attachment-11.txt new file mode 100644 index 0000000..9d60796 --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-11.txt @@ -0,0 +1 @@ +11 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-2.txt b/cypress/fixtures/sample_attachments/attachment-2.txt new file mode 100644 index 0000000..d8263ee --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-2.txt @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-3.txt b/cypress/fixtures/sample_attachments/attachment-3.txt new file mode 100644 index 0000000..e440e5c --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-3.txt @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-4.txt b/cypress/fixtures/sample_attachments/attachment-4.txt new file mode 100644 index 0000000..bf0d87a --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-4.txt @@ -0,0 +1 @@ +4 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-5.txt b/cypress/fixtures/sample_attachments/attachment-5.txt new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-5.txt @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-6.txt b/cypress/fixtures/sample_attachments/attachment-6.txt new file mode 100644 index 0000000..62f9457 --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-6.txt @@ -0,0 +1 @@ +6 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-7.txt b/cypress/fixtures/sample_attachments/attachment-7.txt new file mode 100644 index 0000000..c793025 --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-7.txt @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-8.txt b/cypress/fixtures/sample_attachments/attachment-8.txt new file mode 100644 index 0000000..301160a --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-8.txt @@ -0,0 +1 @@ +8 \ No newline at end of file diff --git a/cypress/fixtures/sample_attachments/attachment-9.txt b/cypress/fixtures/sample_attachments/attachment-9.txt new file mode 100644 index 0000000..f11c82a --- /dev/null +++ b/cypress/fixtures/sample_attachments/attachment-9.txt @@ -0,0 +1 @@ +9 \ No newline at end of file diff --git a/cypress/fixtures/sample_image.jpg b/cypress/fixtures/sample_image.jpg new file mode 100644 index 0000000..6322b65 Binary files /dev/null and b/cypress/fixtures/sample_image.jpg differ diff --git a/cypress/integration/api.js b/cypress/integration/api.js new file mode 100644 index 0000000..420cea2 --- /dev/null +++ b/cypress/integration/api.js @@ -0,0 +1,44 @@ +context("API Resources", () => { + before(() => { + cy.visit("/login"); + cy.login(); + cy.visit("/app/website"); + }); + + it("Creates two Comments", () => { + cy.insert_doc("Comment", { comment_type: "Comment", content: "hello" }); + cy.insert_doc("Comment", { comment_type: "Comment", content: "world" }); + }); + + it("Lists the Comments", () => { + cy.get_list("Comment") + .its("data") + .then((data) => expect(data.length).to.be.at.least(2)); + + cy.get_list("Comment", ["name", "content"], [["content", "=", "hello"]]).then((body) => { + expect(body).to.have.property("data"); + expect(body.data).to.have.lengthOf(1); + expect(body.data[0]).to.have.property("content"); + expect(body.data[0]).to.have.property("name"); + }); + }); + + it("Gets each Comment", () => { + cy.get_list("Comment").then((body) => + body.data.forEach((comment) => { + cy.get_doc("Comment", comment.name); + }) + ); + }); + + it("Removes the Comments", () => { + cy.get_list("Comment").then((body) => { + let comment_names = []; + body.data.map((comment) => comment_names.push(comment.name)); + comment_names = [...new Set(comment_names)]; // remove duplicates + comment_names.forEach((comment_name) => { + cy.remove_doc("Comment", comment_name); + }); + }); + }); +}); diff --git a/cypress/integration/assignment_rule.js b/cypress/integration/assignment_rule.js new file mode 100644 index 0000000..5431561 --- /dev/null +++ b/cypress/integration/assignment_rule.js @@ -0,0 +1,16 @@ +context("Assignment Rule", () => { + before(() => { + cy.login(); + }); + + it("Custom grid buttons work", () => { + cy.new_form("Assignment Rule"); + cy.findByRole("button", { name: "All Days" }).should("be.visible").click(); + cy.wait(2000); + cy.window() + .its("cur_frm") + .then((frm) => { + expect(frm.doc.assignment_days.length).to.equal(7); + }); + }); +}); diff --git a/cypress/integration/awesome_bar.js b/cypress/integration/awesome_bar.js new file mode 100644 index 0000000..03ef967 --- /dev/null +++ b/cypress/integration/awesome_bar.js @@ -0,0 +1,48 @@ +context("Awesome Bar", () => { + before(() => { + cy.visit("/login"); + cy.login(); + cy.visit("/app/website"); + }); + + beforeEach(() => { + cy.get(".navbar .navbar-home").click(); + cy.findByPlaceholderText("Search or type a command (Ctrl + G)").as("awesome_bar"); + cy.get("@awesome_bar").type("{selectall}"); + }); + + it("navigates to doctype list", () => { + cy.get("@awesome_bar").type("todo"); + cy.wait(100); + cy.get(".awesomplete").findByRole("listbox").should("be.visible"); + cy.get("@awesome_bar").type("{enter}"); + cy.get(".title-text").should("contain", "To Do"); + cy.location("pathname").should("eq", "/app/todo"); + }); + + it("find text in doctype list", () => { + cy.get("@awesome_bar").type("test in todo"); + cy.wait(100); + cy.get("@awesome_bar").type("{enter}"); + cy.get(".title-text").should("contain", "To Do"); + cy.wait(200); + const name_filter = cy.get('[data-original-title="ID"] > input'); + name_filter.should("have.value", "%test%"); + cy.clear_filters(); + }); + + it("navigates to new form", () => { + cy.get("@awesome_bar").type("new blog post"); + cy.wait(100); + cy.get("@awesome_bar").type("{enter}"); + cy.get(".title-text:visible").should("have.text", "New Blog Post"); + }); + + it("calculates math expressions", () => { + cy.get("@awesome_bar").type("55 + 32"); + cy.wait(100); + cy.get("@awesome_bar").type("{downarrow}{enter}"); + cy.get(".modal-title").should("contain", "Result"); + cy.get(".msgprint").should("contain", "55 + 32 = 87"); + }); +}); diff --git a/cypress/integration/control_attach.js b/cypress/integration/control_attach.js new file mode 100644 index 0000000..fd80d12 --- /dev/null +++ b/cypress/integration/control_attach.js @@ -0,0 +1,251 @@ +context("Attach Control", () => { + before(() => { + cy.login(); + cy.visit("/app/doctype"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_doctype", { + name: "Test Attach Control", + fields: [ + { + label: "Attach File or Image", + fieldname: "attach", + fieldtype: "Attach", + in_list_view: 1, + }, + ], + }); + }); + }); + it('Checking functionality for "Link" button in the "Attach" fieldtype', () => { + //Navigating to the new form for the newly created doctype + cy.new_form("Test Attach Control"); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Link" button to attach a file using the "Link" button + cy.findByRole("button", { name: "Link" }).click(); + cy.findByPlaceholderText("Attach a web link").type( + "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg", + { force: true } + ); + + //Clicking on the Upload button to upload the file + cy.intercept("POST", "/api/method/upload_file").as("upload_image"); + cy.get(".modal-footer").findByRole("button", { name: "Upload" }).click({ delay: 500 }); + cy.wait("@upload_image"); + cy.findByRole("button", { name: "Save" }).click(); + + //Checking if the URL of the attached image is getting displayed in the field of the newly created doctype + cy.get(".attached-file > .ellipsis > .attached-file-link") + .should("have.attr", "href") + .and("equal", "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg"); + + //Clicking on the "Clear" button + cy.get('[data-action="clear_attachment"]').click(); + + //Checking if clicking on the clear button clears the field of the doctype form and again displays the attach button + cy.get(".control-input > .btn-sm").should("contain", "Attach"); + + //Deleting the doc + cy.go_to_list("Test Attach Control"); + cy.get(".list-row-checkbox").eq(0).click(); + cy.get(".actions-btn-group > .btn").contains("Actions").click(); + cy.get('.actions-btn-group > .dropdown-menu [data-label="Delete"]').click(); + cy.click_modal_primary_button("Yes"); + }); + + it('Checking functionality for "Library" button in the "Attach" fieldtype', () => { + //Navigating to the new form for the newly created doctype + cy.new_form("Test Attach Control"); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Link" button to attach a file using the "Link" button + cy.findByRole("button", { name: "Link" }).click(); + cy.findByPlaceholderText("Attach a web link").type( + "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg", + { force: true } + ); + + //Clicking on the Upload button to upload the file + cy.intercept("POST", "/api/method/upload_file").as("upload_image"); + cy.get(".modal-footer").findByRole("button", { name: "Upload" }).click({ delay: 500 }); + cy.wait("@upload_image"); + cy.findByRole("button", { name: "Save" }).click(); + + //Navigating to the new form for the newly created doctype to check Library button + cy.new_form("Test Attach Control"); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Library" button to attach a file using the "Library" button + cy.findByRole("button", { name: "Library" }).click(); + cy.contains("72402.jpg").click(); + + //Clicking on the Upload button to upload the file + cy.intercept("POST", "/api/method/upload_file").as("upload_image"); + cy.get(".modal-footer").findByRole("button", { name: "Upload" }).click({ delay: 500 }); + cy.wait("@upload_image"); + cy.findByRole("button", { name: "Save" }).click(); + + //Checking if the URL of the attached image is getting displayed in the field of the newly created doctype + cy.get(".attached-file > .ellipsis > .attached-file-link") + .should("have.attr", "href") + .and("equal", "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg"); + + //Clicking on the "Clear" button + cy.get('[data-action="clear_attachment"]').click(); + + //Checking if clicking on the clear button clears the field of the doctype form and again displays the attach button + cy.get(".control-input > .btn-sm").should("contain", "Attach"); + + //Deleting both docs + cy.go_to_list("Test Attach Control"); + cy.get(".list-row-checkbox").eq(0).click(); + cy.get(".list-row-checkbox").eq(1).click(); + cy.get(".actions-btn-group > .btn").contains("Actions").click(); + cy.get('.actions-btn-group > .dropdown-menu [data-label="Delete"]').click(); + cy.click_modal_primary_button("Yes"); + }); + + it('Checking that "Camera" button in the "Attach" fieldtype does show if camera is available', () => { + //Navigating to the new form for the newly created doctype + let doctype = "Test Attach Control"; + let dt_in_route = doctype.toLowerCase().replace(/ /g, "-"); + cy.visit(`/app/${dt_in_route}/new`, { + onBeforeLoad(win) { + // Mock "window.navigator.mediaDevices" property + // to return mock mediaDevices object + win.navigator.mediaDevices = { + ondevicechange: null, + }; + }, + }); + cy.get("body").should(($body) => { + const dataRoute = $body.attr("data-route"); + expect(dataRoute).to.match(new RegExp(`^Form/${doctype}/new-${dt_in_route}-`)); + }); + cy.get("body").should("have.attr", "data-ajax-state", "complete"); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Camera" button + cy.findByRole("button", { name: "Camera" }).should("exist"); + }); + + it('Checking that "Camera" button in the "Attach" fieldtype does not show if no camera is available', () => { + //Navigating to the new form for the newly created doctype + let doctype = "Test Attach Control"; + let dt_in_route = doctype.toLowerCase().replace(/ /g, "-"); + cy.visit(`/app/${dt_in_route}/new`, { + onBeforeLoad(win) { + // Delete "window.navigator.mediaDevices" property + delete win.navigator.mediaDevices; + }, + }); + cy.get("body").should(($body) => { + const dataRoute = $body.attr("data-route"); + expect(dataRoute).to.match(new RegExp(`^Form/${doctype}/new-${dt_in_route}-`)); + }); + cy.get("body").should("have.attr", "data-ajax-state", "complete"); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Camera" button + cy.findByRole("button", { name: "Camera" }).should("not.exist"); + }); +}); +context("Attach Control with Failed Document Save", () => { + before(() => { + cy.login(); + cy.visit("/app/doctype"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_doctype", { + name: "Test Mandatory Attach Control", + fields: [ + { + label: "Attach File or Image", + fieldname: "attach", + fieldtype: "Attach", + in_list_view: 1, + }, + { + label: "Mandatory Text Field", + fieldname: "text_field", + fieldtype: "Text Editor", + in_list_view: 1, + reqd: 1, + }, + ], + }); + }); + }); + let temp_name = ""; + let docname = ""; + it("Attaching a file on an unsaved document", () => { + //Navigating to the new form for the newly created doctype + cy.new_form("Test Mandatory Attach Control"); + cy.get("body").should(($body) => { + temp_name = $body.attr("data-route").split("/")[2]; + }); + + //Clicking on the attach button which is displayed as part of creating a doctype with "Attach" fieldtype + cy.findByRole("button", { name: "Attach" }).click(); + + //Clicking on "Link" button to attach a file using the "Link" button + cy.findByRole("button", { name: "Link" }).click(); + cy.findByPlaceholderText("Attach a web link").type( + "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg", + { force: true } + ); + + //Clicking on the Upload button to upload the file + cy.intercept("POST", "/api/method/upload_file").as("upload_image"); + cy.get(".modal-footer").findByRole("button", { name: "Upload" }).click({ delay: 500 }); + cy.wait("@upload_image"); + cy.get(".msgprint-dialog .modal-title").contains("Missing Fields").should("be.visible"); + cy.hide_dialog(); + cy.fill_field("text_field", "Random value", "Text Editor").wait(500); + cy.findByRole("button", { name: "Save" }).click().wait(500); + + //Checking if the URL of the attached image is getting displayed in the field of the newly created doctype + cy.get(".attached-file > .ellipsis > .attached-file-link") + .should("have.attr", "href") + .and("equal", "https://wallpaperplay.com/walls/full/8/2/b/72402.jpg"); + + cy.get(".title-text").then(($value) => { + docname = $value.text(); + }); + }); + + it("Check if file was uploaded correctly", () => { + cy.go_to_list("File"); + cy.open_list_filter(); + cy.get(".fieldname-select-area .form-control") + .click() + .type("Attached To Name{enter}") + .blur() + .wait(500); + cy.get('input[data-fieldname="attached_to_name"]').click().type(docname).blur(); + cy.get(".filter-popover .apply-filters").click({ force: true }); + cy.get("header .level-right .list-count").should("contain.text", "1 of 1"); + }); + + it("Check if file exists with temporary name", () => { + cy.open_list_filter(); + cy.get('input[data-fieldname="attached_to_name"]').click().clear().type(temp_name).blur(); + cy.get(".filter-popover .apply-filters").click({ force: true }); + cy.get(".xhiveframework-list > .no-result").should("be.visible"); + }); +}); diff --git a/cypress/integration/control_autocomplete.js b/cypress/integration/control_autocomplete.js new file mode 100644 index 0000000..c685e0c --- /dev/null +++ b/cypress/integration/control_autocomplete.js @@ -0,0 +1,64 @@ +context("Control Autocomplete", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_autocomplete(options) { + cy.visit("/app/website"); + return cy.dialog({ + title: "Autocomplete", + fields: [ + { + label: "Select an option", + fieldname: "autocomplete", + fieldtype: "Autocomplete", + options: options || ["Option 1", "Option 2", "Option 3"], + }, + ], + }); + } + + it("should set the valid value", () => { + get_dialog_with_autocomplete().as("dialog"); + + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").focus().as("input"); + cy.wait(1000); + cy.get("@input").type("2", { delay: 300 }); + cy.get(".xhiveframework-control[data-fieldname=autocomplete]") + .findByRole("listbox") + .should("be.visible"); + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").type("{enter}", { + delay: 300, + }); + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").blur(); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("autocomplete"); + expect(value).to.eq("Option 2"); + dialog.clear(); + }); + }); + + it("should set the valid value with different label", () => { + const options_with_label = [ + { label: "Option 1", value: "option_1" }, + { label: "Option 2", value: "option_2" }, + ]; + get_dialog_with_autocomplete(options_with_label).as("dialog"); + + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").focus().as("input"); + cy.get(".xhiveframework-control[data-fieldname=autocomplete]") + .findByRole("listbox") + .should("be.visible"); + cy.get("@input").type("2", { delay: 300 }); + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").type("{enter}", { + delay: 300, + }); + cy.get(".xhiveframework-control[data-fieldname=autocomplete] input").blur(); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("autocomplete"); + expect(value).to.eq("option_2"); + dialog.clear(); + }); + }); +}); diff --git a/cypress/integration/control_barcode.js b/cypress/integration/control_barcode.js new file mode 100644 index 0000000..a3b9531 --- /dev/null +++ b/cypress/integration/control_barcode.js @@ -0,0 +1,57 @@ +context("Control Barcode", () => { + beforeEach(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_barcode() { + return cy.dialog({ + title: "Barcode", + fields: [ + { + label: "Barcode", + fieldname: "barcode", + fieldtype: "Barcode", + }, + ], + }); + } + + it("should generate barcode on setting a value", () => { + get_dialog_with_barcode().as("dialog"); + + cy.focused().blur(); + cy.get(".xhiveframework-control[data-fieldname=barcode]") + .findByRole("textbox") + .type("123456789") + .blur(); + cy.get( + '.xhiveframework-control[data-fieldname=barcode] svg[data-barcode-value="123456789"]' + ).should("exist"); + + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("barcode"); + expect(value).to.contain(" { + get_dialog_with_barcode().as("dialog"); + + cy.focused().blur(); + cy.get(".xhiveframework-control[data-fieldname=barcode]") + .findByRole("textbox") + .type("123456789") + .blur(); + cy.get(".xhiveframework-control[data-fieldname=barcode]").findByRole("textbox").clear().blur(); + cy.get( + '.xhiveframework-control[data-fieldname=barcode] svg[data-barcode-value="123456789"]' + ).should("not.exist"); + + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("barcode"); + expect(value).to.equal(""); + }); + }); +}); diff --git a/cypress/integration/control_color.js b/cypress/integration/control_color.js new file mode 100644 index 0000000..aa3a45e --- /dev/null +++ b/cypress/integration/control_color.js @@ -0,0 +1,80 @@ +context("Control Color", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_color() { + return cy.dialog({ + title: "Color", + fields: [ + { + label: "Color", + fieldname: "color", + fieldtype: "Color", + }, + ], + }); + } + + it("Verifying if the color control is selecting correct", () => { + get_dialog_with_color().as("dialog"); + cy.findByPlaceholderText("Choose a color").click(); + + ///Selecting a color from the color palette + cy.get('[style="background-color: rgb(79, 157, 217);"]').click(); + + //Checking if the css attribute is correct + cy.get(".color-map").should("have.css", "color", "rgb(79, 157, 217)"); + cy.get(".hue-map").should("have.css", "color", "rgb(0, 145, 255)"); + + //Checking if the correct color is being selected + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("color"); + expect(value).to.equal("#4F9DD9"); + }); + + //Selecting a color + cy.get('[style="background-color: rgb(203, 41, 41);"]').click(); + + //Checking if the correct css is being selected + cy.get(".color-map").should("have.css", "color", "rgb(203, 41, 41)"); + cy.get(".hue-map").should("have.css", "color", "rgb(255, 0, 0)"); + + //Checking if the correct color is being selected + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("color"); + expect(value).to.equal("#CB2929"); + }); + + //Selecting color from the palette + cy.get(".color-map > .color-selector").click(65, 87, { force: true }); + cy.get(".color-map").should("have.css", "color", "rgb(56, 0, 0)"); + + //Checking if the expected color is selected and getting displayed + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("color"); + expect(value).to.equal("#380000"); + }); + + //Selecting the color from the hue map + cy.get(".hue-map > .hue-selector").click(35, -1, { force: true }); + cy.get(".color-map").should("have.css", "color", "rgb(56, 45, 0)"); + cy.get(".hue-map").should("have.css", "color", "rgb(255, 204, 0)"); + cy.get(".color-map > .color-selector").click(55, 12, { force: true }); + cy.get(".color-map").should("have.css", "color", "rgb(46, 37, 0)"); + + //Checking if the correct color is being displayed + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("color"); + expect(value).to.equal("#2e2500"); + }); + + //Clearing the field and checking if the field contains the placeholder "Choose a color" + cy.get(".input-with-feedback").click({ force: true }); + cy.get_field("color", "Color").type("{selectall}").clear(); + cy.get_field("color", "Color") + .invoke("attr", "placeholder") + .should("contain", "Choose a color"); + }); +}); diff --git a/cypress/integration/control_currency.js b/cypress/integration/control_currency.js new file mode 100644 index 0000000..3543dd7 --- /dev/null +++ b/cypress/integration/control_currency.js @@ -0,0 +1,88 @@ +context("Control Currency", () => { + const fieldname = "currency_field"; + + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_currency(df_options = {}) { + return cy.dialog({ + title: "Currency Check", + animate: false, + fields: [ + { + fieldname: fieldname, + fieldtype: "Currency", + Label: "Currency", + ...df_options, + }, + ], + }); + } + + it("check value changes", () => { + const TEST_CASES = [ + { + input: "10.101", + df_options: { precision: 1 }, + blur_expected: "10.1", + }, + { + input: "10.101", + df_options: { precision: "3" }, + blur_expected: "10.101", + }, + { + input: "10.101", + df_options: { precision: "" }, // default assumed to be 2; + blur_expected: "10.10", + }, + { + input: "10.101", + df_options: { precision: "0" }, + blur_expected: "10", + }, + { + input: "10.101", + df_options: { precision: 0 }, + blur_expected: "10", + }, + { + input: "10.000", + number_format: "#.###,##", + df_options: { precision: 0 }, + blur_expected: "10.000", + }, + { + input: "10.000", + number_format: "#.###,##", + blur_expected: "10.000,00", + }, + { + input: "10.101", + df_options: { precision: "" }, + blur_expected: "10.1", + default_precision: 1, + }, + ]; + + TEST_CASES.forEach((test_case) => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.boot.sysdefaults.currency = test_case.currency; + xhiveframework.boot.sysdefaults.currency_precision = test_case.default_precision ?? 2; + xhiveframework.boot.sysdefaults.number_format = test_case.number_format ?? "#,###.##"; + }); + + get_dialog_with_currency(test_case.df_options).as("dialog"); + cy.wait(300); + cy.get_field(fieldname, "Currency").clear(); + cy.wait(300); + cy.fill_field(fieldname, test_case.input, "Currency").blur(); + cy.get_field(fieldname, "Currency").should("have.value", test_case.blur_expected); + cy.hide_dialog(); + }); + }); +}); diff --git a/cypress/integration/control_data.js b/cypress/integration/control_data.js new file mode 100644 index 0000000..7132c61 --- /dev/null +++ b/cypress/integration/control_data.js @@ -0,0 +1,148 @@ +context("Data Control", () => { + before(() => { + cy.login(); + cy.visit("/app/doctype"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_doctype", { + name: "Test Data Control", + fields: [ + { + label: "Name", + fieldname: "name1", + fieldtype: "Data", + options: "Name", + in_list_view: 1, + reqd: 1, + }, + { + label: "Email-ID", + fieldname: "email", + fieldtype: "Data", + options: "Email", + in_list_view: 1, + reqd: 1, + }, + { + label: "Phone No.", + fieldname: "phone", + fieldtype: "Data", + options: "Phone", + in_list_view: 1, + reqd: 1, + }, + ], + }); + }); + }); + + it("check custom formatters", () => { + cy.visit(`/app/doctype/User`); + cy.get( + '[data-fieldname="fields"] .grid-row[data-idx="3"] [data-fieldname="fieldtype"] .static-area' + ).should("have.text", "Section Break"); + }); + + it('Verifying data control by inputting different patterns for "Name" field', () => { + cy.new_form("Test Data Control"); + + //Checking the URL for the new form of the doctype + cy.location("pathname").should("contains", "/app/test-data-control/new-test-data-control"); + cy.get(".title-text").should("have.text", "New Test Data Control"); + cy.get('.xhiveframework-control[data-fieldname="name1"]') + .find("label") + .should("have.class", "reqd"); + cy.get('.xhiveframework-control[data-fieldname="email"]') + .find("label") + .should("have.class", "reqd"); + cy.get('.xhiveframework-control[data-fieldname="phone"]') + .find("label") + .should("have.class", "reqd"); + + //Checking if the status is "Not Saved" initially + cy.get(".indicator-pill").should("have.text", "Not Saved"); + + //Inputting data in the field + cy.fill_field("name1", "@@###", "Data"); + cy.fill_field("email", "test@example.com", "Data"); + cy.fill_field("phone", "9834280031", "Data"); + + //Checking if the border color of the field changes to red + cy.get('.xhiveframework-control[data-fieldname="name1"]').should("have.class", "has-error"); + cy.save(); + + //Checking for the error message + cy.get(".modal-title").should("have.text", "Message"); + cy.get(".msgprint").should("have.text", "@@### is not a valid Name"); + cy.hide_dialog(); + + cy.get_field("name1", "Data").clear({ force: true }); + cy.fill_field("name1", "Komal{}/!", "Data"); + cy.get('.xhiveframework-control[data-fieldname="name1"]').should("have.class", "has-error"); + cy.save(); + cy.get(".modal-title").should("have.text", "Message"); + cy.get(".msgprint").should("have.text", "Komal{}/! is not a valid Name"); + cy.hide_dialog(); + }); + + it('Verifying data control by inputting different patterns for "Email" field', () => { + cy.get_field("name1", "Data").clear({ force: true }); + cy.fill_field("name1", "Komal", "Data"); + cy.get_field("email", "Data").clear({ force: true }); + cy.fill_field("email", "komal", "Data"); + cy.get('.xhiveframework-control[data-fieldname="email"]').should("have.class", "has-error"); + cy.save(); + cy.get(".modal-title").should("have.text", "Message"); + cy.get(".msgprint").should("have.text", "komal is not a valid Email Address"); + cy.hide_dialog(); + cy.get_field("email", "Data").clear({ force: true }); + cy.fill_field("email", "komal@test", "Data"); + cy.get('.xhiveframework-control[data-fieldname="email"]').should("have.class", "has-error"); + cy.save(); + cy.get(".modal-title").should("have.text", "Message"); + cy.get(".msgprint").should("have.text", "komal@test is not a valid Email Address"); + cy.hide_dialog(); + }); + + it('Verifying data control by inputting different patterns for "Phone" field', () => { + cy.get_field("email", "Data").clear({ force: true }); + cy.fill_field("email", "komal@test.com", "Data"); + cy.get_field("phone", "Data").clear({ force: true }); + cy.fill_field("phone", "komal", "Data"); + cy.get('.xhiveframework-control[data-fieldname="phone"]').should("have.class", "has-error"); + cy.findByRole("button", { name: "Save" }).click({ force: true }); + cy.get(".modal-title").should("have.text", "Message"); + cy.get(".msgprint").should("have.text", "komal is not a valid Phone Number"); + cy.hide_dialog(); + }); + + it("Inputting correct data and saving the doc", () => { + //Inputting the data as expected and saving the document + cy.get_field("name1", "Data").clear({ force: true }); + cy.get_field("email", "Data").clear({ force: true }); + cy.get_field("phone", "Data").clear({ force: true }); + cy.fill_field("name1", "Komal", "Data"); + cy.fill_field("email", "komal@test.com", "Data"); + cy.fill_field("phone", "9432380001", "Data"); + cy.findByRole("button", { name: "Save" }).click({ force: true }); + //Checking if the fields contains the data which has been filled in + cy.location("pathname").should( + "not.contains", + "/app/test-data-control/new-test-data-control" + ); + cy.get_field("name1").should("have.value", "Komal"); + cy.get_field("email").should("have.value", "komal@test.com"); + cy.get_field("phone").should("have.value", "9432380001"); + }); + + it("Deleting the doc", () => { + //Deleting the inserted document + cy.go_to_list("Test Data Control"); + cy.get(".list-row-checkbox").eq(0).click({ force: true }); + cy.get(".actions-btn-group > .btn").contains("Actions").click(); + cy.get('.actions-btn-group > .dropdown-menu [data-label="Delete"]').click(); + cy.click_modal_primary_button("Yes"); + }); +}); diff --git a/cypress/integration/control_date.js b/cypress/integration/control_date.js new file mode 100644 index 0000000..b8b86dc --- /dev/null +++ b/cypress/integration/control_date.js @@ -0,0 +1,86 @@ +context("Date Control", () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + function get_dialog(date_field_options) { + return cy.dialog({ + title: "Date", + animate: false, + fields: [ + { + label: "Date", + fieldname: "date", + fieldtype: "Date", + in_list_view: 1, + ...date_field_options, + }, + ], + }); + } + + it("Selecting a date from the datepicker & check prev & next button", () => { + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog().as("dialog"); + cy.get_field("date", "Date").click(); + cy.get(".datepicker--nav-title").click(); + cy.get(".datepicker--nav-title").click({ force: true }); + + //Inputing values in the date field + cy.get( + ".datepicker--years > .datepicker--cells > .datepicker--cell[data-year=2020]" + ).click(); + cy.get( + ".datepicker--months > .datepicker--cells > .datepicker--cell[data-month=0]" + ).click(); + cy.get(".datepicker--days > .datepicker--cells > .datepicker--cell[data-date=15]").click(); + + // Verify if the selected date is set the date field + cy.window().its("cur_dialog.fields_dict.date.value").should("be.equal", "2020-01-15"); + + cy.get_field("date", "Date").click(); + + //Clicking on the next button in the datepicker + cy.get(".datepicker--nav-action[data-action=next]").click(); + + //Selecting a date from the datepicker + cy.get(".datepicker--cell[data-date=15]").click({ force: true }); + + //Verifying if the selected date has been displayed in the date field + cy.window().its("cur_dialog.fields_dict.date.value").should("be.equal", "2020-02-15"); + cy.wait(500); + cy.get_field("date", "Date").click(); + + //Clicking on the previous button in the datepicker + cy.get(".datepicker--nav-action[data-action=prev]").click(); + + //Selecting a date from the datepicker + cy.get(".datepicker--cell[data-date=15]").click({ force: true }); + + //Verifying if the selected date has been displayed in the date field + cy.window().its("cur_dialog.fields_dict.date.value").should("be.equal", "2020-01-15"); + }); + + it('Clicking on "Today" button gives todays date', () => { + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog().as("dialog"); + cy.get_field("date", "Date").click(); + + //Clicking on "Today" button + cy.get(".datepicker--button").click(); + + //Verifying if clicking on "Today" button matches today's date + cy.window().then((win) => { + // `expect` can not wait like `should` + cy.wait(500); + expect(win.cur_dialog.fields_dict.date.value).to.be.equal( + win.xhiveframework.datetime.get_today() + ); + }); + }); +}); diff --git a/cypress/integration/control_date_range.js b/cypress/integration/control_date_range.js new file mode 100644 index 0000000..f95a382 --- /dev/null +++ b/cypress/integration/control_date_range.js @@ -0,0 +1,48 @@ +context("Date Range Control", () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + function get_dialog() { + return cy.dialog({ + title: "Date Range", + fields: [ + { + label: "Date Range", + fieldname: "date_range", + fieldtype: "Date Range", + }, + ], + }); + } + + it("Selecting a date range from the datepicker", () => { + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog().as("dialog"); + cy.get_field("date_range", "Date Range").click(); + cy.get(".datepicker--nav-title").click(); + cy.get(".datepicker--nav-title").click({ force: true }); + + //Inputing date range values in the date range field + cy.get( + ".datepicker--years > .datepicker--cells > .datepicker--cell[data-year=2020]" + ).click(); + cy.get( + ".datepicker--months > .datepicker--cells > .datepicker--cell[data-month=0]" + ).click(); + cy.get(".datepicker--cell[data-date=1]:first").click({ force: true }); + cy.get(".datepicker--cell[data-date=15]:first").click({ force: true }); + + // Verify if the selected date range values is set in the date range field + cy.window() + .its("cur_dialog") + .then((dialog) => { + let date_range = dialog.get_value("date_range"); + expect(date_range[0]).to.equal("2020-01-01"); + expect(date_range[1]).to.equal("2020-01-15"); + }); + }); +}); diff --git a/cypress/integration/control_duration.js b/cypress/integration/control_duration.js new file mode 100644 index 0000000..b7c0db0 --- /dev/null +++ b/cypress/integration/control_duration.js @@ -0,0 +1,46 @@ +context("Control Duration", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_duration(hide_days = 0, hide_seconds = 0) { + return cy.dialog({ + title: "Duration", + fields: [ + { + fieldname: "duration", + fieldtype: "Duration", + hide_days: hide_days, + hide_seconds: hide_seconds, + }, + ], + }); + } + + it("should set duration", () => { + get_dialog_with_duration().as("dialog"); + cy.get(".xhiveframework-control[data-fieldname=duration] input").first().click(); + cy.get(".duration-input[data-duration=days]") + .type(45, { force: true }) + .blur({ force: true }); + cy.get(".duration-input[data-duration=minutes]").type(30).blur({ force: true }); + cy.get(".xhiveframework-control[data-fieldname=duration] input") + .first() + .should("have.value", "45d 30m"); + cy.get(".xhiveframework-control[data-fieldname=duration] input").first().blur(); + cy.get(".duration-picker").should("not.be.visible"); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("duration"); + expect(value).to.equal(3889800); + cy.hide_dialog(); + }); + }); + + it("should hide days or seconds according to duration options", () => { + get_dialog_with_duration(1, 1).as("dialog"); + cy.get(".xhiveframework-control[data-fieldname=duration] input").first(); + cy.get(".duration-input[data-duration=days]").should("not.be.visible"); + cy.get(".duration-input[data-duration=seconds]").should("not.be.visible"); + }); +}); diff --git a/cypress/integration/control_dynamic_link.js b/cypress/integration/control_dynamic_link.js new file mode 100644 index 0000000..7adca75 --- /dev/null +++ b/cypress/integration/control_dynamic_link.js @@ -0,0 +1,159 @@ +context("Dynamic Link", () => { + before(() => { + cy.login(); + cy.visit("/app/doctype"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_doctype", { + name: "Test Dynamic Link", + fields: [ + { + label: "Document Type", + fieldname: "doc_type", + fieldtype: "Link", + options: "DocType", + in_list_view: 1, + in_standard_filter: 1, + }, + { + label: "Document ID", + fieldname: "doc_id", + fieldtype: "Dynamic Link", + options: "doc_type", + in_list_view: 1, + in_standard_filter: 1, + }, + ], + }); + }); + }); + + function get_dialog_with_dynamic_link() { + return cy.dialog({ + title: "Dynamic Link", + fields: [ + { + label: "Document Type", + fieldname: "doc_type", + fieldtype: "Link", + options: "DocType", + in_list_view: 1, + }, + { + label: "Document ID", + fieldname: "doc_id", + fieldtype: "Dynamic Link", + options: "doc_type", + in_list_view: 1, + }, + ], + }); + } + + function get_dialog_with_dynamic_link_option() { + return cy.dialog({ + title: "Dynamic Link", + fields: [ + { + label: "Document Type", + fieldname: "doc_type", + fieldtype: "Link", + options: "DocType", + in_list_view: 1, + }, + { + label: "Document ID", + fieldname: "doc_id", + fieldtype: "Dynamic Link", + get_options: () => { + return "User"; + }, + in_list_view: 1, + }, + ], + }); + } + + it("Creating a dynamic link by passing option as function and verifying it in a dialog", () => { + get_dialog_with_dynamic_link_option().as("dialog"); + cy.get_field("doc_type").clear(); + cy.fill_field("doc_type", "User", "Link"); + cy.get_field("doc_id").click(); + + //Checking if the listbox have length greater than 0 + cy.get('[data-fieldname="doc_id"]') + .find(".awesomplete") + .find("li") + .its("length") + .should("be.gte", 0); + cy.get(".btn-modal-close").click({ force: true }); + }); + + it("Creating a dynamic link and verifying it in a dialog", () => { + get_dialog_with_dynamic_link().as("dialog"); + cy.get_field("doc_type").clear(); + cy.fill_field("doc_type", "User", "Link"); + cy.get_field("doc_id").click(); + + //Checking if the listbox have length greater than 0 + cy.get('[data-fieldname="doc_id"]') + .find(".awesomplete") + .find("li") + .its("length") + .should("be.gte", 0); + cy.get(".btn-modal-close").click({ force: true, multiple: true }); + }); + + it("Creating a dynamic link and verifying it", () => { + cy.visit("/app/test-dynamic-link"); + + //Clicking on the Document ID field + cy.get_field("doc_type").clear(); + + //Entering User in the Doctype field + cy.fill_field("doc_type", "User", "Link", { delay: 500 }); + cy.get_field("doc_id").click(); + + //Checking if the listbox have length greater than 0 + cy.get('[data-fieldname="doc_id"]') + .find(".awesomplete") + .find("li") + .its("length") + .should("be.gte", 0); + + //Opening a new form for dynamic link doctype + cy.new_form("Test Dynamic Link"); + cy.get_field("doc_type").clear(); + + //Entering User in the Doctype field + cy.fill_field("doc_type", "User", "Link", { delay: 500 }); + cy.get_field("doc_id").click(); + + //Checking if the listbox have length greater than 0 + cy.get('[data-fieldname="doc_id"]') + .find(".awesomplete") + .find("li") + .its("length") + .should("be.gte", 0); + cy.get_field("doc_type").clear(); + + //Entering System Settings in the Doctype field + cy.intercept("/api/method/xhiveframework.desk.search.search_link").as("search_query"); + cy.fill_field("doc_type", "System Settings", "Link", { delay: 500 }); + cy.wait("@search_query"); + cy.get(`[data-fieldname="doc_type"] ul:visible li:first-child`).click({ + scrollBehavior: false, + }); + + cy.get_field("doc_id").click(); + + //Checking if the system throws error + cy.get(".modal-title").should("have.text", "Error"); + cy.get(".msgprint").should( + "have.text", + "System Settings is not a valid DocType for Dynamic Link" + ); + }); +}); diff --git a/cypress/integration/control_float.js b/cypress/integration/control_float.js new file mode 100644 index 0000000..6676f82 --- /dev/null +++ b/cypress/integration/control_float.js @@ -0,0 +1,110 @@ +context("Control Float", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_float() { + return cy.dialog({ + title: "Float Check", + animate: false, + fields: [ + { + fieldname: "float_number", + fieldtype: "Float", + Label: "Float", + }, + ], + }); + } + + it("check value changes", () => { + get_dialog_with_float().as("dialog"); + cy.wait(300); + + let data = get_data(); + data.forEach((x) => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.boot.sysdefaults.number_format = x.number_format; + }); + x.values.forEach((d) => { + cy.get_field("float_number", "Float").clear(); + cy.wait(200); + cy.fill_field("float_number", d.input, "Float").blur(); + cy.get_field("float_number", "Float").should("have.value", d.blur_expected); + cy.wait(100); + cy.get_field("float_number", "Float").focus(); + cy.wait(100); + cy.get_field("float_number", "Float").blur(); + cy.wait(100); + cy.get_field("float_number", "Float").focus(); + cy.wait(100); + cy.get_field("float_number", "Float").should("have.value", d.focus_expected); + }); + }); + }); + + function get_data() { + return [ + { + number_format: "#.###,##", + values: [ + { + input: "364.87,334", + blur_expected: "36.487,334", + focus_expected: "36.487,334", + }, + { + input: "36487,335", + blur_expected: "36.487,335", + focus_expected: "36.487,335", + }, + { + input: "2*(2+47)+1,5+1", + blur_expected: "100,500", + focus_expected: "100,500", + }, + ], + }, + { + number_format: "#,###.##", + values: [ + { + input: "464,87.334", + blur_expected: "46,487.334", + focus_expected: "46,487.334", + }, + { + input: "46487.335", + blur_expected: "46,487.335", + focus_expected: "46,487.335", + }, + { + input: "3*(2+47)+1.5+1", + blur_expected: "149.500", + focus_expected: "149.500", + }, + ], + }, + { + // '.' is the parseFloat's decimal separator + number_format: "#.###,##", + values: [ + { + input: "12.345", + blur_expected: "12.345,000", + focus_expected: "12.345,000", + }, + { + // parseFloat would reduce 12,340 to 12,34 if this string was ever to be parsed + input: "12.340", + blur_expected: "12.340,000", + focus_expected: "12.340,000", + }, + ], + }, + ]; + } +}); diff --git a/cypress/integration/control_icon.js b/cypress/integration/control_icon.js new file mode 100644 index 0000000..af8cefd --- /dev/null +++ b/cypress/integration/control_icon.js @@ -0,0 +1,55 @@ +context("Control Icon", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_icon() { + return cy.dialog({ + title: "Icon", + fields: [ + { + label: "Icon", + fieldname: "icon", + fieldtype: "Icon", + }, + ], + }); + } + + it("should set icon", () => { + get_dialog_with_icon().as("dialog"); + cy.get(".xhiveframework-control[data-fieldname=icon]").findByRole("textbox").click(); + + cy.get(".icon-picker .icon-wrapper[id=heart-active]").first().click(); + cy.get(".xhiveframework-control[data-fieldname=icon]") + .findByRole("textbox") + .should("have.value", "heart-active"); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("icon"); + expect(value).to.equal("heart-active"); + }); + + cy.get(".icon-picker .icon-wrapper[id=heart]").first().click(); + cy.get(".xhiveframework-control[data-fieldname=icon]") + .findByRole("textbox") + .should("have.value", "heart"); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("icon"); + expect(value).to.equal("heart"); + }); + }); + + it("search for icon and clear search input", () => { + let search_text = "ed"; + cy.get(".icon-picker").get(".search-icons > input").click().type(search_text); + cy.get(".icon-section .icon-wrapper:not(.hidden)").then((i) => { + cy.get(`.icon-section .icon-wrapper[id*='${search_text}']`).then((icons) => { + expect(i.length).to.equal(icons.length); + }); + }); + + cy.get(".icon-picker").get(".search-icons > input").clear().blur(); + cy.get(".icon-section .icon-wrapper").should("not.have.class", "hidden"); + }); +}); diff --git a/cypress/integration/control_link.js b/cypress/integration/control_link.js new file mode 100644 index 0000000..8e8683c --- /dev/null +++ b/cypress/integration/control_link.js @@ -0,0 +1,334 @@ +context("Control Link", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + beforeEach(() => { + cy.visit("/app/website"); + cy.create_records({ + doctype: "ToDo", + description: "this is a test todo for link", + }).as("todos"); + }); + + function get_dialog_with_link() { + return cy.dialog({ + title: "Link", + fields: [ + { + label: "Select ToDo", + fieldname: "link", + fieldtype: "Link", + options: "ToDo", + }, + ], + }); + } + + function get_dialog_with_gender_link() { + return cy.dialog({ + title: "Link", + fields: [ + { + label: "Select Gender", + fieldname: "link", + fieldtype: "Link", + options: "Gender", + }, + ], + }); + } + + it("should set the valid value", () => { + 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: "0", + }, + true + ); + + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input").focus().as("input"); + cy.wait("@search_link"); + cy.get("@input").type("todo for link", { delay: 200 }); + cy.wait("@search_link"); + cy.get(".xhiveframework-control[data-fieldname=link]").findByRole("listbox").should("be.visible"); + cy.get(".xhiveframework-control[data-fieldname=link] input").type("{enter}", { delay: 100 }); + cy.get(".xhiveframework-control[data-fieldname=link] input").blur(); + cy.get("@dialog").then((dialog) => { + cy.get("@todos").then((todos) => { + let value = dialog.get_value("link"); + expect(value).to.eq(todos[0]); + }); + }); + }); + + it("should unset invalid value", () => { + get_dialog_with_link().as("dialog"); + + cy.intercept("POST", "/api/method/xhiveframework.client.validate_link").as("validate_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input") + .type("invalid value", { delay: 100 }) + .blur(); + cy.wait("@validate_link"); + cy.get(".xhiveframework-control[data-fieldname=link] input").should("have.value", ""); + }); + + it("should be possible set empty value explicitly", () => { + get_dialog_with_link().as("dialog"); + + cy.intercept("POST", "/api/method/xhiveframework.client.validate_link").as("validate_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input").type(" ", { delay: 100 }).blur(); + cy.wait("@validate_link"); + cy.get(".xhiveframework-control[data-fieldname=link] input").should("have.value", ""); + cy.window() + .its("cur_dialog") + .then((dialog) => { + expect(dialog.get_value("link")).to.equal(""); + }); + }); + + it("should route to form on arrow click", () => { + get_dialog_with_link().as("dialog"); + + cy.intercept("POST", "/api/method/xhiveframework.client.validate_link").as("validate_link"); + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.get("@todos").then((todos) => { + cy.get(".xhiveframework-control[data-fieldname=link] input").as("input"); + cy.get("@input").focus(); + cy.wait("@search_link"); + cy.get("@input").type(todos[0]).blur(); + cy.wait("@validate_link"); + cy.get("@input").focus(); + cy.wait(500); // wait for arrow to show + cy.get(".xhiveframework-control[data-fieldname=link] .btn-open").should("be.visible").click(); + cy.location("pathname").should("eq", `/app/todo/${todos[0]}`); + }); + }); + + it("show title field in link", () => { + 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.reload(); + + get_dialog_with_link().as("dialog"); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + if (!xhiveframework.boot) { + xhiveframework.boot = { + link_title_doctypes: ["ToDo"], + }; + } else { + xhiveframework.boot.link_title_doctypes = ["ToDo"]; + } + }); + + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input").focus().as("input"); + cy.wait("@search_link"); + cy.get("@input").type("todo for link", { delay: 200 }); + cy.wait("@search_link"); + cy.get(".xhiveframework-control[data-fieldname=link] ul").should("be.visible"); + cy.get(".xhiveframework-control[data-fieldname=link] input").type("{enter}", { delay: 100 }); + cy.get(".xhiveframework-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"); + }); + }); + }); + + it("should update dependant fields (via fetch_from)", () => { + cy.get("@todos").then((todos) => { + cy.visit(`/app/todo/${todos[0]}`); + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + cy.intercept("POST", "/api/method/xhiveframework.client.validate_link").as("validate_link"); + + cy.get(".xhiveframework-control[data-fieldname=assigned_by] input").focus().as("input"); + cy.get("@input").clear().type(cy.config("testUser"), { delay: 300 }).blur(); + cy.wait("@validate_link"); + cy.get(".xhiveframework-control[data-fieldname=assigned_by_full_name] .control-value").should( + "contain", + "XhiveFramework" + ); + + cy.window().its("cur_frm.doc.assigned_by").should("eq", cy.config("testUser")); + + // invalid input + cy.get("@input").clear().type("invalid input", { delay: 100 }).blur(); + cy.get(".xhiveframework-control[data-fieldname=assigned_by_full_name] .control-value").should( + "contain", + "" + ); + + cy.window().its("cur_frm.doc.assigned_by").should("eq", null); + + // set valid value again + cy.get("@input").clear().focus(); + cy.wait("@search_link"); + cy.get("@input").type(cy.config("testUser"), { delay: 100 }).blur(); + cy.wait("@validate_link"); + + cy.window().its("cur_frm.doc.assigned_by").should("eq", cy.config("testUser")); + + // clear input + cy.get("@input").clear().blur(); + cy.get(".xhiveframework-control[data-fieldname=assigned_by_full_name] .control-value").should( + "contain", + "" + ); + + cy.window().its("cur_frm.doc.assigned_by").should("eq", ""); + }); + }); + + it("should set default values", () => { + cy.insert_doc( + "Property Setter", + { + doctype_or_field: "DocField", + doc_type: "ToDo", + field_name: "assigned_by", + property: "default", + property_type: "Text", + value: "Administrator", + }, + true + ); + cy.reload(); + cy.new_form("ToDo"); + cy.fill_field("description", "new", "Text Editor").wait(200); + cy.save(); + cy.get(".xhiveframework-control[data-fieldname=assigned_by_full_name] .control-value").should( + "contain", + "Administrator" + ); + // if user clears default value explicitly, system should not reset default again + cy.get_field("assigned_by").clear().blur(); + cy.save(); + cy.get_field("assigned_by").should("have.value", ""); + cy.get(".xhiveframework-control[data-fieldname=assigned_by_full_name] .control-value").should( + "contain", + "" + ); + }); + + it("show translated text for Gender link field with language de with input in de", () => { + cy.call("xhiveframework.tests.ui_test_helpers.insert_translations").then(() => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + cy.set_value("User", xhiveframework.user.name, { language: "de" }); + }); + + cy.clear_cache(); + cy.wait(500); + + get_dialog_with_gender_link().as("dialog"); + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input").focus().as("input"); + cy.wait("@search_link"); + cy.get("@input").type("Sonstiges", { delay: 200 }); + cy.wait("@search_link"); + cy.get(".xhiveframework-control[data-fieldname=link] ul").should("be.visible"); + cy.get(".xhiveframework-control[data-fieldname=link] input").type("{enter}", { delay: 100 }); + cy.get(".xhiveframework-control[data-fieldname=link] input").blur(); + cy.get("@dialog").then((dialog) => { + let field = dialog.get_field("link"); + let value = field.get_value(); + let label = field.get_label_value(); + + expect(value).to.eq("Other"); + expect(label).to.eq("Sonstiges"); + }); + }); + }); + + it("show text for Gender link field with language en", () => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + cy.set_value("User", xhiveframework.user.name, { language: "en" }); + }); + + cy.clear_cache(); + cy.wait(500); + + get_dialog_with_gender_link().as("dialog"); + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.get(".xhiveframework-control[data-fieldname=link] input").focus().as("input"); + cy.wait("@search_link"); + cy.get("@input").type("Non-Conforming", { delay: 200 }); + cy.wait("@search_link"); + cy.get(".xhiveframework-control[data-fieldname=link] ul").should("be.visible"); + cy.get(".xhiveframework-control[data-fieldname=link] input").type("{enter}", { delay: 100 }); + cy.get(".xhiveframework-control[data-fieldname=link] input").blur(); + cy.get("@dialog").then((dialog) => { + let field = dialog.get_field("link"); + let value = field.get_value(); + let label = field.get_label_value(); + + expect(value).to.eq("Non-Conforming"); + expect(label).to.eq("Non-Conforming"); + }); + }); + + it("show custom link option", () => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.ui.form.ControlLink.link_options = (link) => { + return [ + { + html: + "" + + " " + + "Custom Link Option" + + "", + label: "Custom Link Option", + value: "custom__link_option", + action: () => {}, + }, + ]; + }; + + get_dialog_with_link().as("dialog"); + cy.get(".xhiveframework-control[data-fieldname=link] input").focus().as("input"); + cy.get("@input").type("custom", { delay: 100 }); + cy.get(".custom-link-option").should("be.visible"); + }); + }); +}); diff --git a/cypress/integration/control_markdown_editor.js b/cypress/integration/control_markdown_editor.js new file mode 100644 index 0000000..24ab32f --- /dev/null +++ b/cypress/integration/control_markdown_editor.js @@ -0,0 +1,22 @@ +context("Control Markdown Editor", () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + it("should allow inserting images by drag and drop", () => { + cy.visit("/app/web-page/new"); + cy.fill_field("content_type", "Markdown", "Select"); + cy.get_field("main_section_md", "Markdown Editor").selectFile( + "cypress/fixtures/sample_image.jpg", + { + action: "drag-drop", + } + ); + cy.click_modal_primary_button("Upload"); + cy.get_field("main_section_md", "Markdown Editor").should( + "contain", + "![](/private/files/sample_image" + ); + }); +}); diff --git a/cypress/integration/control_phone.js b/cypress/integration/control_phone.js new file mode 100644 index 0000000..3b64657 --- /dev/null +++ b/cypress/integration/control_phone.js @@ -0,0 +1,99 @@ +import doctype_with_phone from "../fixtures/doctype_with_phone"; + +context("Control Phone", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + afterEach(() => { + cy.clear_dialogs(); + }); + + function get_dialog_with_phone() { + return cy.dialog({ + title: "Phone", + fields: [ + { + fieldname: "phone", + fieldtype: "Phone", + }, + ], + }); + } + + it("should set flag and data", () => { + get_dialog_with_phone().as("dialog"); + + cy.get(".selected-phone").click(); + cy.wait(100); + cy.get(".phone-picker .phone-wrapper[id='afghanistan']").click(); + cy.wait(100); + cy.get(".selected-phone .country").should("have.text", "+93"); + cy.get(".selected-phone > img").should("have.attr", "src").and("include", "/af.svg"); + + cy.get(".selected-phone").click(); + cy.wait(100); + cy.get(".phone-picker .phone-wrapper[id='india']").click(); + cy.wait(100); + cy.get(".selected-phone .country").should("have.text", "+91"); + cy.get(".selected-phone > img").should("have.attr", "src").and("include", "/in.svg"); + + let phone_number = "9312672712"; + cy.get(".selected-phone > img").click().first(); + cy.get_field("phone").first().click(); + cy.get(".xhiveframework-control[data-fieldname=phone]") + .findByRole("textbox") + .first() + .type(phone_number); + + cy.get_field("phone").first().should("have.value", phone_number); + cy.get_field("phone").first().blur(); + cy.wait(100); + cy.get("@dialog").then((dialog) => { + let value = dialog.get_value("phone"); + expect(value).to.equal("+91-" + phone_number); + }); + + let search_text = "india"; + cy.get(".selected-phone").click().first(); + cy.get(".phone-picker").get(".search-phones").click().type(search_text); + cy.get(".phone-section .phone-wrapper:not(.hidden)").then((i) => { + cy.get(`.phone-section .phone-wrapper[id*="${search_text.toLowerCase()}"]`).then( + (countries) => { + expect(i.length).to.equal(countries.length); + } + ); + }); + }); + + it("existing document should render phone field with data", () => { + cy.visit("/app/doctype"); + cy.insert_doc("DocType", doctype_with_phone, true); + cy.clear_cache(); + + // Creating custom doctype + cy.insert_doc("DocType", doctype_with_phone, true); + cy.visit("/app/doctype-with-phone"); + cy.click_listview_primary_button("Add Doctype With Phone"); + + // create a record + cy.fill_field("title", "Test Phone 1"); + cy.fill_field("phone", "+91-9823341234"); + cy.get_field("phone").should("have.value", "9823341234"); + cy.click_doc_primary_button("Save"); + cy.get_doc("Doctype With Phone", "Test Phone 1").then((doc) => { + let value = doc.data.phone; + expect(value).to.equal("+91-9823341234"); + }); + + // open the doc from list view + cy.go_to_list("Doctype With Phone"); + cy.clear_cache(); + cy.click_listview_row_item(0); + cy.title().should("eq", "Test Phone 1"); + cy.get(".selected-phone .country").should("have.text", "+91"); + cy.get(".selected-phone > img").should("have.attr", "src").and("include", "/in.svg"); + cy.get_field("phone").should("have.value", "9823341234"); + }); +}); diff --git a/cypress/integration/control_rating.js b/cypress/integration/control_rating.js new file mode 100644 index 0000000..613a6e9 --- /dev/null +++ b/cypress/integration/control_rating.js @@ -0,0 +1,54 @@ +context("Control Rating", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_rating() { + return cy.dialog({ + title: "Rating", + fields: [ + { + fieldname: "rate", + fieldtype: "Rating", + options: 7, + }, + ], + }); + } + + it("click on the star rating to record value", () => { + get_dialog_with_rating().as("dialog"); + + cy.get("div.rating") + .children("svg") + .find(".right-half") + .first() + .click() + .should("have.class", "star-click"); + cy.get("@dialog").then((dialog) => { + var value = dialog.get_value("rate"); + expect(value).to.equal(1 / 7); + dialog.hide(); + }); + }); + + it("hover on the star", () => { + get_dialog_with_rating(); + + cy.get("div.rating") + .children("svg") + .find(".right-half") + .first() + .invoke("trigger", "mouseenter") + .should("have.class", "star-hover") + .invoke("trigger", "mouseleave") + .should("not.have.class", "star-hover"); + }); + + it("check number of stars in rating", () => { + get_dialog_with_rating(); + + cy.get("div.rating").first().children("svg").should("have.length", 7); + }); +}); diff --git a/cypress/integration/control_select.js b/cypress/integration/control_select.js new file mode 100644 index 0000000..eae8bc6 --- /dev/null +++ b/cypress/integration/control_select.js @@ -0,0 +1,41 @@ +context("Control Select", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + function get_dialog_with_select() { + return cy.dialog({ + title: "Select", + fields: [ + { + fieldname: "select_control", + fieldtype: "Select", + placeholder: "Select an Option", + options: ["", "Option 1", "Option 2", "Option 2"], + }, + ], + }); + } + + it("toggles placholder on clicking an option", () => { + get_dialog_with_select().as("dialog"); + + cy.get(".xhiveframework-control[data-fieldname=select_control] .control-input").as("control"); + cy.get(".xhiveframework-control[data-fieldname=select_control] .control-input select").as( + "select" + ); + cy.get("@control").get(".select-icon").should("exist"); + cy.get("@control").get(".placeholder").should("have.css", "display", "block"); + cy.get("@select").select("Option 1"); + cy.findByDisplayValue("Option 1").should("exist"); + cy.get("@control").get(".placeholder").should("have.css", "display", "none"); + cy.get("@select").invoke("val", ""); + cy.findByDisplayValue("Option 1").should("not.exist"); + cy.get("@control").get(".placeholder").should("have.css", "display", "block"); + + cy.get("@dialog").then((dialog) => { + dialog.hide(); + }); + }); +}); diff --git a/cypress/integration/custom_buttons.js b/cypress/integration/custom_buttons.js new file mode 100644 index 0000000..ddbd197 --- /dev/null +++ b/cypress/integration/custom_buttons.js @@ -0,0 +1,57 @@ +const test_button_names = [ + "Metallica", + "Pink Floyd", + "Porcupine Tree (the GOAT)", + "AC / DC", + `Electronic Dance "music"`, + "l'imperatrice", +]; + +const add_button = (label, group = "TestGroup") => { + cy.window() + .its("cur_frm") + .then((frm) => { + frm.add_custom_button(label, () => {}, group); + }); +}; + +const check_button_count = (label, group = "TestGroup") => { + // Verify main buttons + cy.findByRole("button", { name: group }).click(); + cy.get(`[data-label="${encodeURIComponent(label)}"]`) + .should("have.length", 1) + .should("be.visible"); + + // Verify dropdown buttons in mobile view + cy.viewport(420, 900); + const dropdown_btn_label = `${group} > ${label}`; + cy.get(".menu-btn-group > .btn").click(); + cy.get(`[data-label="${encodeURIComponent(dropdown_btn_label)}"]`) + .should("have.length", 1) + .should("be.visible"); + + //reset viewport + cy.viewport(Cypress.config("viewportWidth"), Cypress.config("viewportHeight")); +}; + +describe( + "Custom group button behaviour on desk", + { scrollBehavior: false }, // speeds up the test + () => { + before(() => { + cy.login(); + cy.visit(`/app/note/new`); + }); + + test_button_names.forEach((button_name) => { + it(`Custom button works with name '${button_name}'`, () => { + add_button(button_name); + check_button_count(button_name); + + // duplicate button shouldn't be added + add_button(button_name); + check_button_count(button_name); + }); + }); + } +); diff --git a/cypress/integration/customize_form.js b/cypress/integration/customize_form.js new file mode 100644 index 0000000..bd8ca1d --- /dev/null +++ b/cypress/integration/customize_form.js @@ -0,0 +1,24 @@ +context("Customize Form", () => { + before(() => { + cy.login(); + cy.visit("/app/customize-form"); + }); + it("Changing to naming rule should update autoname", () => { + cy.fill_field("doc_type", "ToDo", "Link").blur(); + cy.wait(2000); + cy.findByRole("tab", { name: "Details" }).click(); + cy.click_form_section("Naming"); + const naming_rule_default_autoname_map = { + "Set by user": "prompt", + "By fieldname": "field:", + Expression: "format:", + "Expression (old style)": "", + Random: "hash", + "By script": "", + }; + Cypress._.forOwn(naming_rule_default_autoname_map, (value, naming_rule) => { + cy.fill_field("naming_rule", naming_rule, "Select"); + cy.get_field("autoname", "Data").should("have.value", value); + }); + }); +}); diff --git a/cypress/integration/dashboard.js b/cypress/integration/dashboard.js new file mode 100644 index 0000000..6eb2856 --- /dev/null +++ b/cypress/integration/dashboard.js @@ -0,0 +1,50 @@ +describe("Dashboard view", { scrollBehavior: false }, () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + it("should load", () => { + const chart = "TODO-YEARLY-TRENDS"; + const dashboard = "TODO-TEST-DASHBOARD"; // check slash in name intentionally. + + cy.insert_doc( + "Dashboard Chart", + { + is_standard: 0, + chart_name: chart, + chart_type: "Count", + document_type: "ToDo", + parent_document_type: "", + based_on: "creation", + group_by_type: "Count", + timespan: "Last Year", + time_interval: "Yearly", + timeseries: 1, + type: "Line", + filters_json: "[]", + }, + true + ); + + cy.insert_doc( + "Dashboard", + { + name: dashboard, + dashboard_name: dashboard, + is_standard: 0, + charts: [ + { + chart: chart, + }, + ], + }, + true + ); + + cy.visit(`/app/dashboard-view/${dashboard}`); + + // expect chart to be loaded + cy.findByText(chart).should("be.visible"); + }); +}); diff --git a/cypress/integration/dashboard_chart.js b/cypress/integration/dashboard_chart.js new file mode 100644 index 0000000..f2a837e --- /dev/null +++ b/cypress/integration/dashboard_chart.js @@ -0,0 +1,22 @@ +context("Dashboard Chart", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + it("Check filter populate for child table doctype", () => { + cy.new_form("Dashboard Chart"); + cy.get('[data-fieldname="parent_document_type"]').should("have.css", "display", "none"); + + cy.get_field("document_type", "Link"); + cy.fill_field("document_type", "Workspace Link", "Link").focus().blur(); + cy.get_field("document_type", "Link").should("have.value", "Workspace Link"); + + cy.fill_field("chart_name", "Test Chart", "Data"); + + cy.get('[data-fieldname="filters_json"]').click().wait(200); + cy.get(".modal-body .filter-action-buttons .add-filter").click(); + cy.get(".modal-body .fieldname-select-area").click(); + cy.get(".modal-actions .btn-modal-close").click(); + }); +}); diff --git a/cypress/integration/dashboard_links.js b/cypress/integration/dashboard_links.js new file mode 100644 index 0000000..d758150 --- /dev/null +++ b/cypress/integration/dashboard_links.js @@ -0,0 +1,94 @@ +import doctype_with_child_table from "../fixtures/doctype_with_child_table"; +import child_table_doctype from "../fixtures/child_table_doctype"; +import child_table_doctype_1 from "../fixtures/child_table_doctype_1"; +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("Administrator"); + cy.insert_doc("DocType", child_table_doctype, true); + cy.insert_doc("DocType", child_table_doctype_1, true); + cy.insert_doc("DocType", doctype_with_child_table, true); + cy.insert_doc("DocType", doctype_to_link, true); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.call("xhiveframework.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", () => { + cy.visit("/app/contact"); + cy.clear_filters(); + + cy.visit(`/app/user/${cy.config("testUser")}`); + + //To check if initially the dashboard contains only the "Contact" link and there is no counter + cy.select_form_tab("Connections"); + cy.get('[data-doctype="Contact"]').should("contain", "Contact"); + + //Adding a new contact + cy.get('.document-link-badge[data-doctype="Contact"]').click(); + cy.wait(300); + cy.findByRole("button", { name: "Add Contact" }).should("be.visible"); + cy.findByRole("button", { name: "Add Contact" }).click(); + cy.get('[data-doctype="Contact"][data-fieldname="first_name"]').type("Admin"); + cy.findByRole("button", { name: "Save" }).click(); + cy.visit(`/app/user/${cy.config("testUser")}`); + + //To check if the counter for contact doc is "2" after adding additional contact + cy.select_form_tab("Connections"); + cy.get('[data-doctype="Contact"] > .count').should("contain", "2"); + cy.get('[data-doctype="Contact"]').contains("Contact").click(); + + //Deleting the newly created contact + cy.visit("/app/contact"); + cy.get(".list-subject > .select-like > .list-row-checkbox").eq(0).click({ force: true }); + cy.findByRole("button", { name: "Actions" }).click(); + cy.get('.actions-btn-group [data-label="Delete"]').click(); + cy.findByRole("button", { name: "Yes" }).click({ delay: 700 }); + + //To check if the counter from the "Contact" doc link is removed + cy.wait(700); + cy.visit("/app/user"); + cy.get(".list-row-col > .level-item > .ellipsis").eq(0).click({ force: true }); + cy.get('[data-doctype="Contact"]').should("contain", "Contact"); + }); + + it("Report link in dashboard", () => { + cy.visit(`/app/user/${cy.config("testUser")}`); + cy.select_form_tab("Connections"); + cy.get('.document-link[data-doctype="Contact"]').contains("Contact"); + cy.window() + .its("cur_frm") + .then((cur_frm) => { + cur_frm.dashboard.data.reports = [ + { + label: "Reports", + items: ["Website Analytics"], + }, + ]; + cur_frm.dashboard.render_report_links(); + cy.get('.document-link[data-report="Website Analytics"]') + .contains("Website Analytics") + .click(); + }); + }); + + 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( + '.xhiveframework-control[data-fieldname="child_table"] .rows .data-row .col[data-fieldname="doctype_to_link"]' + ).should("contain.text", "Test Linking"); + }); +}); diff --git a/cypress/integration/data_field_form_validation.js b/cypress/integration/data_field_form_validation.js new file mode 100644 index 0000000..4797ed2 --- /dev/null +++ b/cypress/integration/data_field_form_validation.js @@ -0,0 +1,45 @@ +import data_field_validation_doctype from "../fixtures/data_field_validation_doctype"; +const doctype_name = data_field_validation_doctype.name; + +context("Data Field Input Validation in New Form", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy.insert_doc("DocType", data_field_validation_doctype, true); + }); + + function validateField(fieldname, invalid_value, valid_value) { + // Invalid, should have has-error class + cy.get_field(fieldname).clear().type(invalid_value).blur(); + cy.get(`.xhiveframework-control[data-fieldname="${fieldname}"]`).should("have.class", "has-error"); + // Valid value, should not have has-error class + cy.get_field(fieldname).clear().type(valid_value); + cy.get(`.xhiveframework-control[data-fieldname="${fieldname}"]`).should( + "not.have.class", + "has-error" + ); + } + + describe("Data Field Options", () => { + it("should validate email address", () => { + cy.new_form(doctype_name); + validateField("email", "captian", "hello@test.com"); + }); + + it("should validate URL", () => { + validateField("url", "jkl", "https://xhiveframework.io"); + validateField("url", "abcd.com", "http://google.com/home"); + validateField("url", "&&http://google.uae", "gopher://xhiveframework.io"); + validateField("url", "ftt2:://google.in?q=news", "ftps2://xhiveframework.io/__/#home"); + validateField("url", "ftt2://", "ntps://localhost"); // For intranet URLs + }); + + it("should validate phone number", () => { + validateField("phone", "america", "89787878"); + }); + + it("should validate name", () => { + validateField("person_name", " 777Hello", "James Bond"); + }); + }); +}); diff --git a/cypress/integration/datetime.js b/cypress/integration/datetime.js new file mode 100644 index 0000000..f55d9b4 --- /dev/null +++ b/cypress/integration/datetime.js @@ -0,0 +1,126 @@ +import datetime_doctype from "../fixtures/datetime_doctype"; +const doctype_name = datetime_doctype.name; + +context("Control Date, Time and DateTime", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy.insert_doc("DocType", datetime_doctype, true); + }); + + describe("Date formats", () => { + let date_formats = [ + { + date_format: "dd-mm-yyyy", + part: 2, + length: 4, + separator: "-", + }, + { + date_format: "mm/dd/yyyy", + part: 0, + length: 2, + separator: "/", + }, + ]; + + date_formats.forEach((d) => { + it("test date format " + d.date_format, () => { + cy.set_value("System Settings", "System Settings", { + date_format: d.date_format, + }); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + // update sys_defaults value to avoid a reload + xhiveframework.sys_defaults.date_format = d.date_format; + }); + + cy.new_form(doctype_name); + cy.get(".form-control[data-fieldname=date]").focus(); + cy.get(".datepickers-container .datepicker.active").should("be.visible"); + cy.get( + ".datepickers-container .datepicker.active .datepicker--cell-day.-current-" + ).click({ force: true }); + + cy.window() + .its("cur_frm") + .then((cur_frm) => { + let formatted_value = cur_frm.get_field("date").input.value; + let parts = formatted_value.split(d.separator); + expect(parts[d.part].length).to.equal(d.length); + }); + }); + }); + }); + + describe("Time formats", () => { + let time_formats = [ + { + time_format: "HH:mm:ss", + value: " 11:00:12", + match_value: "11:00:12", + }, + { + time_format: "HH:mm", + value: " 11:00:12", + match_value: "11:00", + }, + ]; + + time_formats.forEach((d) => { + it("test time format " + d.time_format, () => { + cy.set_value("System Settings", "System Settings", { + time_format: d.time_format, + }); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.sys_defaults.time_format = d.time_format; + }); + cy.new_form(doctype_name); + cy.fill_field("time", d.value, "Time").blur(); + cy.get_field("time").should("have.value", d.match_value); + }); + }); + }); + + describe("DateTime formats", () => { + let datetime_formats = [ + { + date_format: "dd.mm.yyyy", + time_format: "HH:mm:ss", + value: " 02.12.2019 11:00:12", + doc_value: "2019-12-02 00:30:12", // system timezone (America/New_York) + input_value: "02.12.2019 11:00:12", // admin timezone (Asia/Kolkata) + }, + { + date_format: "mm-dd-yyyy", + time_format: "HH:mm", + value: " 12-02-2019 11:00:00", + doc_value: "2019-12-02 00:30:00", // system timezone (America/New_York) + input_value: "12-02-2019 11:00", // admin timezone (Asia/Kolkata) + }, + ]; + + datetime_formats.forEach((d) => { + it(`test datetime format ${d.date_format} ${d.time_format}`, () => { + cy.set_value("System Settings", "System Settings", { + date_format: d.date_format, + time_format: d.time_format, + }); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.sys_defaults.date_format = d.date_format; + xhiveframework.sys_defaults.time_format = d.time_format; + }); + cy.new_form(doctype_name); + cy.fill_field("datetime", d.value, "Datetime").blur(); + cy.get_field("datetime").should("have.value", d.input_value); + + cy.window().its("cur_frm.doc.datetime").should("eq", d.doc_value); + }); + }); + }); +}); diff --git a/cypress/integration/datetime_field_form_validation.js b/cypress/integration/datetime_field_form_validation.js new file mode 100644 index 0000000..b3081f8 --- /dev/null +++ b/cypress/integration/datetime_field_form_validation.js @@ -0,0 +1,19 @@ +// TODO: Enable this again +// currently this is flaky possibly because of different timezone in CI + +// context('Datetime Field Validation', () => { +// before(() => { +// cy.login(); +// cy.visit('/app/communication'); +// }); + +// it('datetime field form validation', () => { +// // validating datetime field value when value is set from backend and get validated on form load. +// cy.window().its('xhiveframework').then(xhiveframework => { +// return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_communication_record"); +// }).then(doc => { +// cy.visit(`/app/communication/${doc.name}`); +// cy.get('.indicator-pill').should('contain', 'Open').should('have.class', 'red'); +// }); +// }); +// }); diff --git a/cypress/integration/depends_on.js b/cypress/integration/depends_on.js new file mode 100644 index 0000000..88e13cd --- /dev/null +++ b/cypress/integration/depends_on.js @@ -0,0 +1,152 @@ +context("Depends On", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_child_doctype", { + name: "Child Test Depends On", + fields: [ + { + label: "Child Test Field", + fieldname: "child_test_field", + fieldtype: "Data", + in_list_view: 1, + }, + { + label: "Child Dependant Field", + fieldname: "child_dependant_field", + fieldtype: "Data", + in_list_view: 1, + }, + { + label: "Child Display Dependant Field", + fieldname: "child_display_dependant_field", + fieldtype: "Data", + in_list_view: 1, + }, + ], + }); + }) + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.create_doctype", { + name: "Test Depends On", + fields: [ + { + label: "Test Field", + fieldname: "test_field", + fieldtype: "Data", + }, + { + label: "Dependant Field", + fieldname: "dependant_field", + fieldtype: "Data", + mandatory_depends_on: "eval:doc.test_field=='Some Value'", + read_only_depends_on: "eval:doc.test_field=='Some Other Value'", + }, + { + label: "Display Dependant Field", + fieldname: "display_dependant_field", + fieldtype: "Data", + depends_on: "eval:doc.test_field=='Value'", + }, + { + label: "Child Test Depends On Field", + fieldname: "child_test_depends_on_field", + fieldtype: "Table", + 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"); + cy.findByRole("button", { name: "Save" }).click(); + cy.get(".msgprint-dialog .modal-title").contains("Missing Fields").should("be.visible"); + cy.hide_dialog(); + cy.fill_field("test_field", "Random value"); + cy.findByRole("button", { name: "Save" }).click(); + cy.get(".msgprint-dialog .modal-title") + .contains("Missing Fields") + .should("not.be.visible"); + }); + it("should set the field as read only depending on other fields value", () => { + cy.new_form("Test Depends On"); + cy.fill_field("dependant_field", "Some Value"); + cy.fill_field("test_field", "Some Other Value"); + cy.get("body").click(); + cy.get('.control-input [data-fieldname="dependant_field"]').should("be.disabled"); + cy.fill_field("test_field", "Random Value"); + cy.get("body").click(); + cy.get('.control-input [data-fieldname="dependant_field"]').should("not.be.disabled"); + }); + it("should set the table and its fields as read only depending on other fields value", () => { + cy.new_form("Test Depends On"); + cy.fill_field("dependant_field", "Some Value"); + //cy.fill_field('test_field', 'Some Other Value'); + cy.get('.xhiveframework-control[data-fieldname="child_test_depends_on_field"]').as("table"); + cy.get("@table").findByRole("button", { name: "Add Row" }).click(); + cy.get("@table").find('[data-idx="1"]').as("row1"); + cy.get("@row1").find(".btn-open-row").click(); + cy.get("@row1").find(".form-in-grid").as("row1-form_in_grid"); + //cy.get('@row1-form_in_grid').find('') + cy.fill_table_field("child_test_depends_on_field", "1", "child_test_field", "Some Value"); + cy.fill_table_field( + "child_test_depends_on_field", + "1", + "child_dependant_field", + "Some Other Value" + ); + + cy.get("@row1-form_in_grid").find(".grid-collapse-row").click(); + + // set the table to read-only + cy.fill_field("test_field", "Some Other Value"); + + // grid row form fields should be read-only + cy.get("@row1").find(".btn-open-row").click(); + + cy.get("@row1-form_in_grid") + .find('.control-input [data-fieldname="child_test_field"]') + .should("be.disabled"); + cy.get("@row1-form_in_grid") + .find('.control-input [data-fieldname="child_dependant_field"]') + .should("be.disabled"); + }); + it("should display the field depending on other fields value", () => { + cy.new_form("Test Depends On"); + cy.get('.control-input [data-fieldname="display_dependant_field"]').should( + "not.be.visible" + ); + cy.get('.control-input [data-fieldname="test_field"]').clear(); + cy.fill_field("test_field", "Value"); + cy.get("body").click(); + cy.get('.control-input [data-fieldname="display_dependant_field"]').should("be.visible"); + }); +}); diff --git a/cypress/integration/discussions.js b/cypress/integration/discussions.js new file mode 100644 index 0000000..f7d73b3 --- /dev/null +++ b/cypress/integration/discussions.js @@ -0,0 +1,85 @@ +context("Discussions", () => { + before(() => { + cy.login(); + cy.visit("/app"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_data_for_discussions"); + }); + }); + + const reply_through_modal = () => { + cy.visit("/test-page-discussions"); + + // Open the modal + cy.get(".reply").click(); + cy.wait(500); + cy.get(".discussion-modal").should("be.visible"); + + // Enter title + cy.get(".modal .topic-title") + .type("Discussion from tests") + .should("have.value", "Discussion from tests"); + + // Enter comment + cy.get(".modal .discussions-comment").type( + "This is a discussion from the cypress ui tests." + ); + + // Submit + cy.get(".modal .submit-discussion").click(); + cy.wait(2000); + + // Check if discussion is added to page and content is visible + cy.get(".sidebar-parent:first .discussion-topic-title").should( + "have.text", + "Discussion from tests" + ); + cy.get(".discussion-on-page:visible").should("have.class", "show"); + cy.get(".discussion-on-page:visible .reply-card .reply-text .ql-editor p").should( + "have.text", + "This is a discussion from the cypress ui tests." + ); + }; + + const reply_through_comment_box = () => { + cy.get(".discussion-form:visible .discussions-comment").type( + "This is a discussion from the cypress ui tests. \n\nThis comment was entered through the commentbox on the page." + ); + + cy.get(".discussion-form:visible .submit-discussion").click(); + cy.wait(3000); + cy.get(".discussion-on-page:visible").should("have.class", "show"); + cy.get(".discussion-on-page:visible") + .children(".reply-card") + .eq(1) + .find(".reply-text") + .should( + "have.text", + "This is a discussion from the cypress ui tests. This comment was entered through the commentbox on the page.\n" + ); + }; + + const single_thread_discussion = () => { + cy.visit("/test-single-thread"); + cy.get(".discussions-sidebar").should("have.length", 0); + cy.get(".reply").should("have.length", 0); + + cy.get(".discussion-form:visible .discussions-comment").type( + "This comment is being made on a single thread discussion." + ); + cy.get(".discussion-form:visible .submit-discussion").click(); + cy.wait(3000); + cy.get(".discussion-on-page") + .children(".reply-card") + .eq(-1) + .find(".reply-text") + .should("have.text", "This comment is being made on a single thread discussion.\n"); + }; + + it("reply through modal", reply_through_modal); + it("reply through comment box", reply_through_comment_box); + it("single thread discussion", single_thread_discussion); +}); diff --git a/cypress/integration/file_uploader.js b/cypress/integration/file_uploader.js new file mode 100644 index 0000000..9f4ba23 --- /dev/null +++ b/cypress/integration/file_uploader.js @@ -0,0 +1,86 @@ +context("FileUploader", () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + function open_upload_dialog() { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + new xhiveframework.ui.FileUploader(); + }); + } + + it("upload dialog api works", () => { + open_upload_dialog(); + cy.get_open_dialog().should("contain", "Drag and drop files"); + cy.hide_dialog(); + }); + + it("should accept dropped files", () => { + open_upload_dialog(); + + cy.get_open_dialog() + .find(".file-upload-area") + .selectFile("cypress/fixtures/example.json", { + action: "drag-drop", + }); + + cy.get_open_dialog().find(".file-name").should("contain", "example.json"); + cy.intercept("POST", "/api/method/upload_file").as("upload_file"); + cy.get_open_dialog().findByRole("button", { name: "Upload" }).click(); + cy.wait("@upload_file").its("response.statusCode").should("eq", 200); + cy.get(".modal:visible").should("not.exist"); + }); + + it("should accept uploaded files", () => { + open_upload_dialog(); + + cy.get_open_dialog().findByRole("button", { name: "Library" }).click(); + cy.findByPlaceholderText("Search by filename or extension").type("example.json"); + cy.get_open_dialog().findAllByText("example.json").first().click(); + cy.intercept("POST", "/api/method/upload_file").as("upload_file"); + cy.get_open_dialog().findByRole("button", { name: "Upload" }).click(); + cy.wait("@upload_file") + .its("response.body.message") + .should("have.property", "file_name", "example.json"); + cy.get(".modal:visible").should("not.exist"); + }); + + it("should accept web links", () => { + open_upload_dialog(); + + cy.get_open_dialog().findByRole("button", { name: "Link" }).click(); + cy.get_open_dialog() + .findByPlaceholderText("Attach a web link") + .type("https://github.com", { delay: 100, force: true }); + cy.intercept("POST", "/api/method/upload_file").as("upload_file"); + cy.get_open_dialog().findByRole("button", { name: "Upload" }).click(); + cy.wait("@upload_file") + .its("response.body.message") + .should("have.property", "file_url", "https://github.com"); + cy.get(".modal:visible").should("not.exist"); + }); + + it("should allow cropping and optimization for valid images", () => { + open_upload_dialog(); + + cy.get_open_dialog() + .find(".file-upload-area") + .selectFile("cypress/fixtures/sample_image.jpg", { + action: "drag-drop", + }); + + cy.get_open_dialog().findAllByText("sample_image.jpg").should("exist"); + cy.get_open_dialog().find(".btn-crop").first().click(); + cy.get_open_dialog().findByRole("button", { name: "Crop" }).click(); + cy.get_open_dialog().findAllByRole("checkbox", { name: "Optimize" }).should("exist"); + cy.get_open_dialog().findAllByLabelText("Optimize").first().click(); + + cy.intercept("POST", "/api/method/upload_file").as("upload_file"); + cy.get_open_dialog().findByRole("button", { name: "Upload" }).click(); + cy.wait("@upload_file").its("response.statusCode").should("eq", 200); + cy.get(".modal:visible").should("not.exist"); + }); +}); diff --git a/cypress/integration/first_day_of_the_week.js b/cypress/integration/first_day_of_the_week.js new file mode 100644 index 0000000..9222778 --- /dev/null +++ b/cypress/integration/first_day_of_the_week.js @@ -0,0 +1,51 @@ +context("First Day of the Week", () => { + before(() => { + cy.login(); + }); + + beforeEach(() => { + cy.visit("/app/system-settings"); + cy.findByText("Date and Number Format").click(); + }); + + it("Date control starts with same day as selected in System Settings", () => { + cy.intercept( + "POST", + "/api/method/xhiveframework.core.doctype.system_settings.system_settings.load" + ).as("load_settings"); + cy.fill_field("first_day_of_the_week", "Tuesday", "Select"); + cy.findByRole("button", { name: "Save" }).click(); + cy.wait("@load_settings"); + cy.dialog({ + title: "Date", + fields: [ + { + label: "Date", + fieldname: "date", + fieldtype: "Date", + }, + ], + }); + cy.get_field("date").click(); + cy.get(".datepicker--day-name").eq(0).should("have.text", "Tu"); + }); + + it("Calendar view starts with same day as selected in System Settings", () => { + cy.intercept( + "POST", + "/api/method/xhiveframework.core.doctype.system_settings.system_settings.load" + ).as("load_settings"); + cy.fill_field("first_day_of_the_week", "Monday", "Select"); + cy.findByRole("button", { name: "Save" }).click(); + cy.wait("@load_settings"); + cy.visit("app/todo/view/calendar/default"); + cy.get(".fc-day-header > span").eq(0).should("have.text", "Mon"); + }); + + after(() => { + cy.visit("/app/system-settings"); + cy.findByText("Date and Number Format").click(); + cy.fill_field("first_day_of_the_week", "Sunday", "Select"); + cy.findByRole("button", { name: "Save" }).click(); + }); +}); diff --git a/cypress/integration/folder_navigation.js b/cypress/integration/folder_navigation.js new file mode 100644 index 0000000..ab32722 --- /dev/null +++ b/cypress/integration/folder_navigation.js @@ -0,0 +1,97 @@ +context("Folder Navigation", () => { + before(() => { + cy.visit("/login"); + cy.login(); + cy.visit("/app/file"); + }); + + it("Adding Folders", () => { + //Adding filter to go into the home folder + cy.get(".filter-x-button").click(); + cy.click_filter_button(); + cy.get(".filter-action-buttons > .text-muted").findByText("+ Add a Filter").click(); + cy.get(".fieldname-select-area > .awesomplete > .form-control:last").type("Fol{enter}"); + cy.get(".filter-field > .form-group > .link-field > .awesomplete > .input-with-feedback") + .first() + .type("Home{enter}"); + cy.get(".filter-action-buttons > div > .btn-primary").findByText("Apply Filters").click(); + + //Adding folder (Test Folder) + cy.click_menu_button("New Folder"); + cy.fill_field("value", "Test Folder"); + cy.click_modal_primary_button("Create"); + }); + + it("Navigating the nested folders, checking if the URL formed is correct, checking if the added content in the child folder is correct", () => { + //Navigating inside the Attachments folder + cy.clear_filters(); + cy.wait(500); + cy.get('[title="Attachments"] > span').click(); + + //To check if the URL formed after visiting the attachments folder is correct + cy.location("pathname").should("eq", "/app/file/view/home/Attachments"); + cy.visit("/app/file/view/home/Attachments"); + + //Adding folder inside the attachments folder + cy.click_menu_button("New Folder"); + cy.fill_field("value", "Test Folder"); + cy.click_modal_primary_button("Create"); + + //Navigating inside the added folder in the Attachments folder + cy.wait(500); + cy.get('[title="Test Folder"] > span').click(); + + //To check if the URL is correct after visiting the Test Folder + cy.location("pathname").should("eq", "/app/file/view/home/Attachments/Test%20Folder"); + cy.visit("/app/file/view/home/Attachments/Test%20Folder"); + + //Adding a file inside the Test Folder + cy.findByRole("button", { name: "Add File" }).eq(0).click({ force: true }); + cy.get(".file-uploader").findByText("Link").click(); + cy.get(".input-group > input.form-control:visible").as("upload_input"); + cy.get("@upload_input").type("https://wallpaperplay.com/walls/full/8/2/b/72402.jpg", { + waitForAnimations: false, + parseSpecialCharSequences: false, + force: true, + delay: 100, + }); + cy.click_modal_primary_button("Upload"); + + //To check if the added file is present in the Test Folder + cy.visit("/app/file/view/home/Attachments"); + cy.wait(500); + cy.get("span.level-item > a > span").should("contain", "Test Folder"); + cy.visit("/app/file/view/home/Attachments/Test%20Folder"); + + cy.wait(500); + cy.get(".list-row-container").eq(0).should("contain.text", "72402.jpg"); + cy.get(".list-row-checkbox").eq(0).click(); + + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.reportview.delete_items", + }).as("file_deleted"); + + //Deleting the added file from the Test folder + cy.click_action_button("Delete"); + cy.click_modal_primary_button("Yes"); + cy.wait("@file_deleted"); + + //Deleting the Test Folder + cy.visit("/app/file/view/home/Attachments"); + cy.get(".list-row-checkbox").eq(0).click(); + cy.click_action_button("Delete"); + cy.click_modal_primary_button("Yes"); + cy.wait("@file_deleted"); + }); + + it("Deleting Test Folder from the home", () => { + //Deleting the Test Folder added in the home directory + cy.visit("/app/file/view/home"); + cy.get(".level-left > .list-subject > .file-select >.list-row-checkbox") + .eq(0) + .click({ force: true, delay: 500 }); + cy.click_action_button("Delete"); + cy.click_modal_primary_button("Yes"); + }); +}); diff --git a/cypress/integration/form.js b/cypress/integration/form.js new file mode 100644 index 0000000..56bca59 --- /dev/null +++ b/cypress/integration/form.js @@ -0,0 +1,171 @@ +const jump_to_field = (field_label) => { + cy.get("body") + .type("{esc}") // lose focus if any + .type("{ctrl+j}") // jump to field + .type(field_label) + .wait(500) + .type("{enter}") + .wait(200) + .type("{enter}") + .wait(500); +}; + +const type_value = (value) => { + cy.focused().clear().type(value).type("{esc}"); +}; + +context("Form", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_contact_records"); + }); + }); + + beforeEach(() => { + cy.login(); + cy.visit("/app/website"); + }); + + it("create a new form", () => { + cy.visit("/app/todo/new"); + cy.get_field("description", "Text Editor") + .type("this is a test todo", { force: true }) + .wait(1000); + cy.get(".page-title").should("contain", "Not Saved"); + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.form.save.savedocs", + }).as("form_save"); + cy.get(".primary-action").click(); + cy.wait("@form_save").its("response.statusCode").should("eq", 200); + + cy.go_to_list("ToDo"); + cy.clear_filters(); + cy.get(".page-head").findByTitle("To Do").should("exist"); + cy.get(".list-row").should("contain", "this is a test todo"); + }); + + it("navigates between documents with child table list filters applied", () => { + cy.visit("/app/contact"); + + cy.clear_filters(); + cy.get('.standard-filter-section [data-fieldname="name"] input') + .type("Test Form Contact 3") + .blur(); + cy.click_listview_row_item_with_text("Test Form Contact 3"); + + cy.scrollTo(0); + cy.get("#page-Contact .page-head").findByTitle("Test Form Contact 3").should("exist"); + cy.get(".prev-doc").should("be.visible").click(); + cy.get(".msgprint-dialog .modal-body").contains("No further records").should("be.visible"); + cy.hide_dialog(); + + cy.scrollTo(0); + cy.get("#page-Contact .page-head").findByTitle("Test Form Contact 3").should("exist"); + cy.get(".next-doc").should("be.visible").click(); + cy.get(".msgprint-dialog .modal-body").contains("No further records").should("be.visible"); + cy.hide_dialog(); + + cy.get("#page-Contact .page-head").findByTitle("Test Form Contact 3").should("exist"); + + // clear filters + cy.visit("/app/contact"); + cy.clear_filters(); + }); + + it("validates behaviour of Data options validations in child table", () => { + // test email validations for set_invalid controller + let website_input = "website.in"; + let valid_email = "user@email.com"; + let expectBackgroundColor = "rgb(255, 245, 245)"; + + cy.visit("/app/contact/new"); + cy.get('.xhiveframework-control[data-fieldname="email_ids"]').as("table"); + cy.get("@table").find("button.grid-add-row").click(); + cy.get("@table").find("button.grid-add-row").click(); + cy.get("@table").find('[data-idx="1"]').as("row1"); + cy.get("@table").find('[data-idx="2"]').as("row2"); + cy.get("@row1").click(); + cy.get("@row1").find("input.input-with-feedback.form-control").as("email_input1"); + + cy.get("@email_input1").type(website_input, { waitForAnimations: false }); + cy.fill_field("company_name", "Test Company"); + + cy.get("@row2").click(); + cy.get("@row2").find("input.input-with-feedback.form-control").as("email_input2"); + cy.get("@email_input2").type(valid_email, { waitForAnimations: false }); + + cy.get("@row1").click(); + cy.get("@email_input1").should("have.class", "invalid"); + + cy.get("@row2").click(); + cy.get("@email_input2").should("not.have.class", "invalid"); + }); + + it("Jump to field in collapsed section", { scrollBehavior: false }, () => { + cy.new_form("User"); + + jump_to_field("Location"); // this is in collapsed section + type_value("Bermuda"); + + cy.get_field("location").should("have.value", "Bermuda"); + }); + + it("update docfield property using set_df_property in child table", () => { + cy.visit("/app/contact/Test Form Contact 1"); + cy.window() + .its("cur_frm") + .then((frm) => { + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("table"); + + // set property before form_render event of child table + cy.get("@table") + .find('[data-idx="1"]') + .invoke("attr", "data-name") + .then((cdn) => { + frm.set_df_property( + "phone_nos", + "hidden", + 1, + "Contact Phone", + "is_primary_phone", + cdn + ); + }); + + cy.get("@table").find('[data-idx="1"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-control[data-fieldname="is_primary_phone"]') + .should("be.hidden"); + cy.get("@table-form").find(".grid-footer-toolbar").click(); + + // set property on form_render event of child table + cy.get("@table").find('[data-idx="1"] .btn-open-row').click(); + cy.get("@table") + .find('[data-idx="1"]') + .invoke("attr", "data-name") + .then((cdn) => { + frm.set_df_property( + "phone_nos", + "hidden", + 0, + "Contact Phone", + "is_primary_phone", + cdn + ); + }); + + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-control[data-fieldname="is_primary_phone"]') + .should("be.visible"); + cy.get("@table-form").find(".grid-footer-toolbar").click(); + }); + }); +}); diff --git a/cypress/integration/form_builder.js b/cypress/integration/form_builder.js new file mode 100644 index 0000000..e7972c6 --- /dev/null +++ b/cypress/integration/form_builder.js @@ -0,0 +1,282 @@ +import form_builder_doctype from "../fixtures/form_builder_doctype"; +const doctype_name = form_builder_doctype.name; +context("Form Builder", () => { + before(() => { + cy.login(); + cy.visit("/app"); + return cy.insert_doc("DocType", form_builder_doctype, true); + }); + + it("Open Form Builder for Web Form Doctype/Customize Form", () => { + // doctype + cy.visit("/app/doctype/Web Form"); + cy.findByRole("tab", { name: "Form" }).click(); + cy.get(".form-builder-container").should("exist"); + + // customize form + cy.visit("/app/customize-form?doc_type=Web%20Form"); + cy.findByRole("tab", { name: "Form" }).click(); + cy.get(".form-builder-container").should("exist"); + }); + + it("Save without change, check form dirty", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + // Save without change + cy.click_doc_primary_button("Save"); + cy.get(".desk-alert.orange .alert-message").should("have.text", "No changes in document"); + + // Check form dirty + cy.get(".tab-content.active .section-columns-container:first .column:first .field:first") + .find("div[title='Double click to edit label']") + .dblclick() + .type("Dirty"); + cy.get(".title-area .indicator-pill.orange").should("have.text", "Not Saved"); + }); + + it("Add empty section and save", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_section = ".tab-content.active .form-section-container:first"; + + // add new section + cy.get(first_section).click(15, 10); + cy.get(first_section).find(".dropdown-btn:first").click(); + cy.get(".dropdown-options:visible .dropdown-item:first").click(); + + // save + cy.click_doc_primary_button("Save"); + cy.get(".tab-content.active .form-section-container").should("have.length", 1); + }); + + it("Add Table field and check if columns are rendered", () => { + cy.intercept("POST", "/api/method/xhiveframework.desk.search.search_link").as("search_link"); + + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_column = ".tab-content.active .section-columns-container:first .column:first"; + + let last_field = first_column + " .field:last"; + + let add_new_field_btn = first_column + " .add-new-field-btn button"; + + // add new field + cy.get(add_new_field_btn).click(); + + // type table and press enter + cy.get(".combo-box-options:visible .search-box > input").type("table{enter}"); + + // save + cy.click_doc_primary_button("Save"); + + // Validate if options is not set + cy.get_open_dialog().find(".msgprint").should("contain", "Options is required"); + cy.hide_dialog(); + + cy.get(last_field).click({ force: true }); + + cy.get(".sidebar-container .xhiveframework-control[data-fieldname='options'] input") + .click() + .as("input"); + cy.get("@input").clear({ force: true }).type("Web Form Field", { delay: 200 }); + cy.wait("@search_link"); + + cy.get(last_field).click({ force: true }); + + cy.get(last_field).find(".table-controls .table-column").contains("Field").should("exist"); + cy.get(last_field) + .find(".table-controls .table-column") + .contains("Fieldtype") + .should("exist"); + + // validate In List View + cy.get(".sidebar-container .field label .label-area").contains("In List View").click(); + + // save + cy.click_doc_primary_button("Save"); + + cy.get_open_dialog().find(".msgprint").should("contain", "In List View"); + cy.hide_dialog(); + + cy.get(last_field).click({ force: true }); + cy.get(".sidebar-container .field label .label-area").contains("In List View").click(); + + // validate In Global Search + cy.get(".sidebar-container .field label .label-area").contains("In Global Search").click(); + // save + cy.click_doc_primary_button("Save"); + + cy.get_open_dialog().find(".msgprint").should("contain", "In Global Search"); + }); + // not important and was flaky on CI + it.skip("Drag Field/Column/Section & Tab", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_column = ".tab-content.active .section-columns-container:first .column:first"; + let first_field = first_column + " .field:first"; + let label = "div[title='Double click to edit label'] span:first"; + + cy.get(".tab-header .tabs .tab:first").click(); + + // drag first tab to second position + cy.get(".tab-header .tabs .tab:first").drag(".tab-header .tabs .tab:nth-child(2)", { + target: { x: 10, y: 10 }, + force: true, + }); + cy.get(".tab-header .tabs .tab:first").find(label).should("have.text", "Tab 2"); + + cy.get(".tab-header .tabs .tab:first").click(); + cy.get(".sidebar-container .tab:first").click(); + + // drag check field to first column + cy.get(".fields-container .field[title='Check']").drag(first_field, { + target: { x: 100, y: 10 }, + }); + cy.get(first_column).find(".field").should("have.length", 3); + + cy.get(first_field) + .find("div[title='Double click to edit label']") + .dblclick() + .type("Test Check"); + cy.get(first_field).find(label).should("have.text", "Test Check"); + + // drag the first field to second position + cy.get(first_field).drag(first_column + " .field:nth-child(2)", { + target: { x: 100, y: 10 }, + }); + cy.get(first_field).find(label).should("have.text", "Data"); + + // drag first column to second position + cy.get(first_column).click().wait(200); + cy.get(first_column) + .find(".column-actions") + .drag(".section-columns-container:first .column:last", { + target: { x: 100, y: 10 }, + force: true, + }); + cy.get(first_field).find(label).should("have.text", "Data 1"); + + let first_section = ".tab-content.active .form-section-container:first"; + + // drag first section to second position + cy.get(first_section).click().wait(200); + cy.get(first_section) + .find(".section-header") + .drag(".form-section-container:nth-child(2)", { + target: { x: 100, y: 10 }, + force: true, + }); + cy.get(first_field).find(label).should("have.text", "Data 2"); + }); + + it("Add New Tab/Section/Column to Form", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_section = ".tab-content.active .form-section-container:first"; + + // add new tab + cy.get(".tab-header").realHover().find(".tab-actions .new-tab-btn").click(); + cy.get(".tab-header .tabs .tab").should("have.length", 3); + + // add new section + cy.get(first_section).click(15, 10); + cy.get(first_section).find(".dropdown-btn:first").click(); + cy.get(".dropdown-options:visible .dropdown-item:first").click(); + cy.get(".tab-content.active .form-section-container").should("have.length", 2); + + // add new column + cy.get(first_section).click(15, 10); + cy.get(first_section).find(".dropdown-btn:first").click(); + cy.get(".dropdown-options:visible .dropdown-item:last").click(); + cy.get(first_section).find(".column").should("have.length", 2); + }); + + it("Remove Tab/Section/Column", () => { + let first_section = ".tab-content.active .form-section-container:first"; + + // remove column + cy.get(first_section).click(15, 10); + cy.get(first_section).find(".dropdown-btn:first").click(); + cy.get(".dropdown-options:visible .dropdown-item:last").click(); + cy.get(first_section).find(".column").should("have.length", 1); + + // remove section + cy.get(first_section).click(15, 10); + cy.get(first_section).find(".dropdown-btn:first").click(); + cy.get(".dropdown-options:visible .dropdown-item").eq(1).click(); + cy.get(".tab-content.active .form-section-container").should("have.length", 1); + + // remove tab + cy.get(".tab-header .tab:last").realHover().find(".remove-tab-btn").click(); + cy.get(".tab-header .tabs .tab").should("have.length", 2); + }); + + it("Update Title field Label to New Title through Customize Form", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_field = + ".tab-content.active .section-columns-container:first .column:first .field:first"; + + cy.get(first_field) + .find("div[title='Double click to edit label']") + .dblclick() + .type("{selectall}New Title"); + + cy.findByRole("button", { name: "Save" }).click({ force: true }); + + cy.visit("/app/form-builder-doctype/new"); + cy.get("[data-fieldname='data3'] .clearfix label").should("have.text", "New Title"); + }); + + it("Validate Duplicate Name & reqd + hidden without default logic", () => { + cy.visit(`/app/doctype/${doctype_name}`); + cy.findByRole("tab", { name: "Form" }).click(); + + let first_column = ".tab-content.active .section-columns-container:first .column:first"; + + let last_field = first_column + " .field:last"; + + let add_new_field_btn = first_column + " .add-new-field-btn button"; + + // add new field + cy.get(add_new_field_btn).click(); + + // type data and press enter + cy.get(".combo-box-options:visible .search-box > input").type("data{enter}"); + + cy.get(last_field).click(); + + // validate duplicate name + cy.get(".sidebar-container .xhiveframework-control[data-fieldname='fieldname'] input") + .click() + .as("input"); + cy.get(".sidebar-container .xhiveframework-control[data-fieldname='fieldname'] input") + .clear({ force: true }) + .type("data3"); + + cy.click_doc_primary_button("Save"); + cy.get_open_dialog().find(".msgprint").should("contain", "appears multiple times"); + cy.hide_dialog(); + cy.get(last_field).click(); + cy.get(".sidebar-container .xhiveframework-control[data-fieldname='fieldname'] input").clear({ + force: true, + }); + + // validate reqd + hidden without default + cy.get(".sidebar-container .field label .label-area").contains("Mandatory").click(); + cy.get(".sidebar-container .field label .label-area").contains("Hidden").click(); + + // save + cy.click_doc_primary_button("Save"); + + cy.get_open_dialog() + .find(".msgprint") + .should("contain", "cannot be hidden and mandatory without any default value"); + }); +}); diff --git a/cypress/integration/form_tab_break.js b/cypress/integration/form_tab_break.js new file mode 100644 index 0000000..91695cb --- /dev/null +++ b/cypress/integration/form_tab_break.js @@ -0,0 +1,30 @@ +import doctype_with_tab_break from "../fixtures/doctype_with_tab_break"; +const doctype_name = doctype_with_tab_break.name; +context("Form Tab Break", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy.insert_doc("DocType", doctype_with_tab_break, true); + }); + it("Should switch tab and open correct tabs on validation error", () => { + cy.new_form(doctype_name); + // test tab switch + cy.findByRole("tab", { name: "Tab 2" }).click(); + cy.findByText("Phone"); + cy.findByRole("tab", { name: "Details" }).click(); + cy.findByText("Name"); + + // form should switch to the tab with un-filled mandatory field + cy.fill_field("username", "Test"); + cy.findByRole("button", { name: "Save" }).click(); + cy.findByText("Missing Fields"); + cy.hide_dialog(); + cy.findByText("Phone"); + cy.fill_field("phone", "12345678"); + cy.findByRole("button", { name: "Save" }).click(); + + // After save, first tab should have dashboard + cy.get(".form-tabs > .nav-item").eq(0).click(); + cy.findByText("Connections"); + }); +}); diff --git a/cypress/integration/form_tour.js b/cypress/integration/form_tour.js new file mode 100644 index 0000000..a187c85 --- /dev/null +++ b/cypress/integration/form_tour.js @@ -0,0 +1,94 @@ +context.skip("Form Tour", () => { + before(() => { + cy.login(); + cy.visit("/app"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_form_tour"); + }); + }); + + const open_test_form_tour = () => { + cy.visit("/app/form-tour/Test Form Tour"); + cy.findByRole("button", { name: "Show Tour" }).should("be.visible").as("show_tour"); + cy.get("@show_tour").click(); + cy.wait(500); + cy.url().should("include", "/app/contact"); + }; + + it("jump to a form tour", open_test_form_tour); + + it("navigates a form tour", () => { + open_test_form_tour(); + + cy.get(".xhiveframework-driver").should("be.visible"); + cy.get('.xhiveframework-control[data-fieldname="first_name"]').as("first_name"); + cy.get("@first_name").should("have.class", "driver-highlighted-element"); + cy.get(".xhiveframework-driver").findByRole("button", { name: "Next" }).as("next_btn"); + + // next btn shouldn't move to next step, if first name is not entered + cy.get("@next_btn").click(); + cy.wait(500); + cy.get("@first_name").should("have.class", "driver-highlighted-element"); + + // after filling the field, next step should be highlighted + cy.fill_field("first_name", "Test Name", "Data"); + cy.wait(500); + cy.get("@next_btn").click(); + cy.wait(500); + + // assert field is highlighted + cy.get('.xhiveframework-control[data-fieldname="last_name"]').as("last_name"); + cy.get("@last_name").should("have.class", "driver-highlighted-element"); + + // after filling the field, next step should be highlighted + cy.fill_field("last_name", "Test Last Name", "Data"); + cy.wait(500); + cy.get("@next_btn").click(); + cy.wait(500); + + // assert field is highlighted + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("phone_nos"); + cy.get("@phone_nos").should("have.class", "driver-highlighted-element"); + + // move to next step + cy.wait(500); + cy.get("@next_btn").click(); + cy.wait(500); + + // assert add row btn is highlighted + cy.get("@phone_nos").find(".grid-add-row").as("add_row"); + cy.get("@add_row").should("have.class", "driver-highlighted-element"); + + // add a row & move to next step + cy.wait(500); + cy.get("@add_row").click(); + cy.wait(500); + + // assert table field is highlighted + cy.get('.grid-row-open .xhiveframework-control[data-fieldname="phone"]').as("phone"); + cy.get("@phone").should("have.class", "driver-highlighted-element"); + // enter value in a table field + let field = cy.fill_table_field("phone_nos", "1", "phone", "1234567890"); + field.blur(); + + // move to collapse row step + cy.wait(500); + cy.get(".driver-popover-title") + .contains("Test Title 4") + .siblings() + .get("@next_btn") + .click(); + cy.wait(500); + // collapse row + cy.get(".grid-row-open .grid-collapse-row").click(); + cy.wait(500); + + // assert save btn is highlighted + cy.get(".primary-action").should("have.class", "driver-highlighted-element"); + cy.wait(500); + cy.get(".xhiveframework-driver").findByRole("button", { name: "Save" }).should("be.visible"); + }); +}); diff --git a/cypress/integration/grid.js b/cypress/integration/grid.js new file mode 100644 index 0000000..3afac95 --- /dev/null +++ b/cypress/integration/grid.js @@ -0,0 +1,114 @@ +context("Grid", () => { + beforeEach(() => { + cy.login(); + cy.visit("/app/website"); + }); + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call( + "xhiveframework.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('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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"] .btn-open-row').click(); + cy.get(".grid-row-open").as("table-form"); + cy.get("@table-form") + .find('.xhiveframework-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('.xhiveframework-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"] .btn-open-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"] .btn-open-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/grid_configuration.js b/cypress/integration/grid_configuration.js new file mode 100644 index 0000000..d130005 --- /dev/null +++ b/cypress/integration/grid_configuration.js @@ -0,0 +1,25 @@ +context("Grid Configuration", () => { + beforeEach(() => { + cy.login(); + cy.visit("/app/doctype/User"); + }); + it("Set user wise grid settings", () => { + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get('.form-section[data-fieldname="fields_section"]').click(); + cy.wait(100); + cy.get('.xhiveframework-control[data-fieldname="fields"]').as("table"); + cy.get("@table").find(".icon-sm").click(); + cy.wait(100); + cy.get('.xhiveframework-control[data-fieldname="fields_html"]').as("modal"); + cy.get("@modal").find(".add-new-fields").click(); + cy.wait(100); + cy.get('[type="checkbox"][data-unit="read_only"]').check(); + cy.findByRole("button", { name: "Add" }).click(); + cy.wait(100); + cy.get('[data-fieldname="options"]').invoke("attr", "value", "1"); + cy.get('.form-control.column-width[data-fieldname="options"]').trigger("change"); + cy.findByRole("button", { name: "Update" }).click(); + cy.wait(200); + cy.get('[title="Read Only"').should("be.visible"); + }); +}); diff --git a/cypress/integration/grid_keyboard_shortcut.js b/cypress/integration/grid_keyboard_shortcut.js new file mode 100644 index 0000000..45f6dc6 --- /dev/null +++ b/cypress/integration/grid_keyboard_shortcut.js @@ -0,0 +1,54 @@ +context("Grid Keyboard Shortcut", () => { + let total_count = 0; + let contact_email_name = null; + before(() => { + cy.login(); + }); + beforeEach(() => { + cy.reload(); + cy.new_form("Contact"); + cy.get('.xhiveframework-control[data-fieldname="email_ids"]').find(".grid-add-row").click(); + // as new names uses hash instead of numbers get row's data-name dynamically. + cy.get('.xhiveframework-control[data-fieldname="email_ids"]') + .find(".grid-body .grid-row") + .should(($row) => { + contact_email_name = $row.attr("data-name"); + }); + }); + it("Insert new row at the end", () => { + cy.add_new_row_in_grid( + "{ctrl}{shift}{downarrow}", + (cy, total_count) => { + cy.get(`[data-name="${contact_email_name}"]`).should( + "have.attr", + "data-idx", + `${total_count + 1}` + ); + }, + total_count + ); + }); + it("Insert new row at the top", () => { + cy.add_new_row_in_grid("{ctrl}{shift}{uparrow}", (cy) => { + cy.get(`[data-name="${contact_email_name}"]`).should("have.attr", "data-idx", "2"); + }); + }); + it("Insert new row below", () => { + cy.add_new_row_in_grid("{ctrl}{downarrow}", (cy) => { + cy.get(`[data-name^="${contact_email_name}"]`).should("have.attr", "data-idx", "1"); + }); + }); + it("Insert new row above", () => { + cy.add_new_row_in_grid("{ctrl}{uparrow}", (cy) => { + cy.get(`[data-name^="${contact_email_name}"]`).should("have.attr", "data-idx", "2"); + }); + }); +}); + +Cypress.Commands.add("add_new_row_in_grid", (shortcut_keys, callbackFn, total_count) => { + cy.get('.xhiveframework-control[data-fieldname="email_ids"]').as("table"); + cy.get("@table").find('.grid-body [data-fieldname="email_id"]').first().click(); + cy.get("@table").find('.grid-body [data-fieldname="email_id"]').first().type(shortcut_keys); + + callbackFn(cy, total_count); +}); diff --git a/cypress/integration/grid_pagination.js b/cypress/integration/grid_pagination.js new file mode 100644 index 0000000..efab8aa --- /dev/null +++ b/cypress/integration/grid_pagination.js @@ -0,0 +1,80 @@ +context("Grid Pagination", () => { + beforeEach(() => { + cy.login(); + cy.visit("/app/website"); + }); + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call( + "xhiveframework.tests.ui_test_helpers.create_contact_phone_nos_records" + ); + }); + }); + it("creates pages for child table", () => { + cy.visit("/app/contact/Test Contact"); + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("table"); + cy.get("@table").find(".current-page-number").should("have.value", "1"); + cy.get("@table").find(".total-page-number").should("contain", "20"); + cy.get("@table").find(".grid-body .grid-row").should("have.length", 50); + }); + it("goes to the next and previous page", () => { + cy.visit("/app/contact/Test Contact"); + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("table"); + cy.get("@table").find(".next-page").click(); + cy.get("@table").find(".current-page-number").should("have.value", "2"); + cy.get("@table") + .find(".grid-body .grid-row") + .first() + .should("have.attr", "data-idx", "51"); + cy.get("@table").find(".prev-page").click(); + cy.get("@table").find(".current-page-number").should("have.value", "1"); + cy.get("@table").find(".grid-body .grid-row").first().should("have.attr", "data-idx", "1"); + }); + it("adds and deletes rows and changes page", () => { + cy.visit("/app/contact/Test Contact"); + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("table"); + cy.get("@table").findByRole("button", { name: "Add Row" }).click(); + cy.get("@table").find(".grid-body .row-index").should("contain", 1001); + cy.get("@table").find(".current-page-number").should("have.value", "21"); + cy.get("@table").find(".total-page-number").should("contain", "21"); + cy.get("@table").find(".grid-body .grid-row .grid-row-check").click({ force: true }); + cy.get("@table").findByRole("button", { name: "Delete" }).click(); + cy.get("@table").find(".grid-body .row-index").last().should("contain", 1000); + cy.get("@table").find(".current-page-number").should("have.value", "20"); + cy.get("@table").find(".total-page-number").should("contain", "20"); + }); + it("go to specific page, use up and down arrow, type characters, 0 page and more than existing page", () => { + cy.visit("/app/contact/Test Contact"); + cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as("table"); + cy.get("@table").find(".current-page-number").focus().clear().type("17").blur(); + cy.get("@table").find(".grid-body .row-index").should("contain", 801); + + cy.get("@table").find(".current-page-number").focus().type("{uparrow}{uparrow}"); + cy.get("@table").find(".current-page-number").should("have.value", "19"); + + cy.get("@table").find(".current-page-number").focus().type("{downarrow}{downarrow}"); + cy.get("@table").find(".current-page-number").should("have.value", "17"); + + cy.get("@table").find(".current-page-number").focus().clear().type("700").blur(); + cy.get("@table").find(".current-page-number").should("have.value", "20"); + + cy.get("@table").find(".current-page-number").focus().clear().type("0").blur(); + cy.get("@table").find(".current-page-number").should("have.value", "1"); + + cy.get("@table").find(".current-page-number").focus().clear().type("abc").blur(); + cy.get("@table").find(".current-page-number").should("have.value", "1"); + }); + // it('deletes all rows', ()=> { + // cy.visit('/app/contact/Test Contact'); + // cy.get('.xhiveframework-control[data-fieldname="phone_nos"]').as('table'); + // cy.get('@table').find('.grid-heading-row .grid-row-check').click({force: true}); + // cy.get('@table').find('button.grid-remove-all-rows').click(); + // cy.get('.modal-dialog .btn-primary').contains('Yes').click(); + // cy.get('@table').find('.grid-body .grid-row').should('have.length', 0); + // }); +}); diff --git a/cypress/integration/grid_search.js b/cypress/integration/grid_search.js new file mode 100644 index 0000000..82774f3 --- /dev/null +++ b/cypress/integration/grid_search.js @@ -0,0 +1,133 @@ +import doctype_with_child_table from "../fixtures/doctype_with_child_table"; +import child_table_doctype from "../fixtures/child_table_doctype"; +import child_table_doctype_1 from "../fixtures/child_table_doctype_1"; +const doctype_with_child_table_name = doctype_with_child_table.name; + +context("Grid Search", () => { + before(() => { + cy.visit("/login"); + cy.login(); + cy.visit("/app/website"); + cy.insert_doc("DocType", child_table_doctype, true); + cy.insert_doc("DocType", child_table_doctype_1, true); + cy.insert_doc("DocType", doctype_with_child_table, true); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall( + "xhiveframework.tests.ui_test_helpers.insert_doctype_with_child_table_record", + { + name: doctype_with_child_table_name, + } + ); + }); + }); + + it("Test search row visibility", () => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.model.user_settings.save("Doctype With Child Table", "GridView", { + "Child Table Doctype 1": [ + { fieldname: "data", columns: 2 }, + { fieldname: "barcode", columns: 1 }, + { fieldname: "check", columns: 1 }, + { fieldname: "rating", columns: 2 }, + { fieldname: "duration", columns: 2 }, + { fieldname: "date", columns: 2 }, + ], + }); + }); + + cy.visit(`/app/doctype-with-child-table/Test Grid Search`); + + cy.get('.xhiveframework-control[data-fieldname="child_table_1"]').as("table"); + cy.get("@table").find(".grid-row-check:last").click(); + cy.get("@table").find(".grid-footer").contains("Delete").click(); + cy.get(".grid-heading-row .grid-row .search").should("not.exist"); + }); + + it("test search field for different fieldtypes", () => { + cy.visit(`/app/doctype-with-child-table/Test Grid Search`); + + cy.get('.xhiveframework-control[data-fieldname="child_table_1"]').as("table"); + + // Index Column + cy.get("@table").find(".grid-heading-row .row-index.search input").type("3"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 2); + cy.get("@table").find(".grid-heading-row .row-index.search input").clear(); + + // Data Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Data"]') + .type("Data"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 1); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Data"]').clear(); + + // Barcode Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Barcode"]') + .type("092"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 4); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Barcode"]').clear(); + + // Check Column + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Check"]').type("1"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 9); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Check"]').clear(); + + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Check"]').type("0"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 11); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Check"]').clear(); + + // Rating Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Rating"]') + .type("3"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 3); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Rating"]').clear(); + + // Duration Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Duration"]') + .type("3d"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 3); + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Duration"]') + .clear(); + + // Date Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Date"]') + .type("2022"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 4); + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Date"]').clear(); + }); + + it("test with multiple filter", () => { + cy.get('.xhiveframework-control[data-fieldname="child_table_1"]').as("table"); + + // Data Column + cy.get("@table").find('.grid-heading-row .search input[data-fieldtype="Data"]').type("a"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 10); + + // Barcode Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Barcode"]') + .type("0"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 8); + + // Duration Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Duration"]') + .type("d"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 5); + + // Date Column + cy.get("@table") + .find('.grid-heading-row .search input[data-fieldtype="Date"]') + .type("02-"); + cy.get("@table").find(".grid-body .rows .grid-row").should("have.length", 2); + }); +}); diff --git a/cypress/integration/kanban.js b/cypress/integration/kanban.js new file mode 100644 index 0000000..45f76d5 --- /dev/null +++ b/cypress/integration/kanban.js @@ -0,0 +1,134 @@ +context("Kanban Board", () => { + before(() => { + cy.login("xhiveframework@example.com"); + cy.visit("/app"); + }); + + it("Create ToDo Kanban", () => { + cy.visit("/app/todo"); + + cy.get(".page-actions .custom-btn-group button").click(); + cy.get(".page-actions .custom-btn-group ul.dropdown-menu li").contains("Kanban").click(); + + cy.focused().blur(); + cy.fill_field("board_name", "ToDo Kanban", "Data"); + cy.fill_field("field_name", "Status", "Select"); + cy.click_modal_primary_button("Save"); + + cy.get(".title-text").should("contain", "ToDo Kanban"); + }); + + it("Create ToDo from kanban", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.client.save", + }).as("save-todo"); + + cy.click_listview_primary_button("Add ToDo"); + + cy.fill_field("description", "Test Kanban ToDo", "Text Editor").wait(300); + cy.get(".modal-footer .btn-primary").last().click(); + + cy.wait("@save-todo"); + }); + + it("Add and Remove fields", () => { + cy.visit("/app/todo/view/kanban/ToDo Kanban"); + + cy.intercept( + "POST", + "/api/method/xhiveframework.desk.doctype.kanban_board.kanban_board.save_settings" + ).as("save-kanban"); + cy.intercept( + "POST", + "/api/method/xhiveframework.desk.doctype.kanban_board.kanban_board.update_order" + ).as("update-order"); + + cy.get(".page-actions .menu-btn-group > .btn").click(); + cy.get(".page-actions .menu-btn-group .dropdown-menu li") + .contains("Kanban Settings") + .click(); + cy.get(".add-new-fields").click(); + + cy.get(".checkbox-options .checkbox").contains("ID").click(); + cy.get(".checkbox-options .checkbox").contains("Status").first().click(); + cy.get(".checkbox-options .checkbox").contains("Priority").click(); + + cy.get(".modal-footer .btn-primary").last().click(); + + cy.get(".xhiveframework-control .label-area").contains("Show Labels").click(); + cy.click_modal_primary_button("Save"); + + cy.wait("@save-kanban"); + + cy.get('.kanban-column[data-column-value="Open"] .kanban-cards').as("open-cards"); + cy.get("@open-cards") + .find(".kanban-card .kanban-card-doc") + .first() + .should("contain", "ID:"); + cy.get("@open-cards") + .find(".kanban-card .kanban-card-doc") + .first() + .should("contain", "Status:"); + cy.get("@open-cards") + .find(".kanban-card .kanban-card-doc") + .first() + .should("contain", "Priority:"); + + cy.get(".page-actions .menu-btn-group > .btn").click(); + cy.get(".page-actions .menu-btn-group .dropdown-menu li") + .contains("Kanban Settings") + .click(); + cy.get_open_dialog() + .find( + '.xhiveframework-control[data-fieldname="fields_html"] div[data-label="ID"] .remove-field' + ) + .click(); + + cy.wait("@update-order"); + cy.get_open_dialog().find(".xhiveframework-control .label-area").contains("Show Labels").click(); + cy.get(".modal-footer .btn-primary").last().click(); + + cy.wait("@save-kanban"); + + cy.get("@open-cards") + .find(".kanban-card .kanban-card-doc") + .first() + .should("not.contain", "ID:"); + }); + + it("Checks if Kanban Board edits are blocked for non-System Manager and non-owner of the Board", () => { + cy.switch_to_user("Administrator"); + + const not_system_manager = "nosysmanager@example.com"; + cy.call("xhiveframework.tests.ui_test_helpers.create_test_user", { + username: not_system_manager, + }); + cy.remove_role(not_system_manager, "System Manager"); + cy.call("xhiveframework.tests.ui_test_helpers.create_todo", { description: "XhiveFramework User ToDo" }); + cy.call("xhiveframework.tests.ui_test_helpers.create_admin_kanban"); + + cy.switch_to_user(not_system_manager); + + cy.visit("/app/todo/view/kanban/Admin Kanban"); + + // Menu button should be hidden (dropdown for 'Save Filters' and 'Delete Kanban Board') + cy.get(".no-list-sidebar .menu-btn-group .btn-default[data-original-title='Menu']").should( + "have.length", + 0 + ); + // Kanban Columns should be visible (read-only) + cy.get(".kanban .kanban-column").should("have.length", 2); + // User should be able to add card (has access to ToDo) + cy.get(".kanban .add-card").should("have.length", 2); + // Column actions should be hidden (dropdown for 'Archive' and indicators) + cy.get(".kanban .column-options").should("have.length", 0); + + cy.switch_to_user("Administrator"); + cy.call("xhiveframework.client.delete", { doctype: "User", name: not_system_manager }); + }); + + after(() => { + cy.call("logout"); + }); +}); diff --git a/cypress/integration/list_paging.js b/cypress/integration/list_paging.js new file mode 100644 index 0000000..187bb93 --- /dev/null +++ b/cypress/integration/list_paging.js @@ -0,0 +1,42 @@ +context("List Paging", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_multiple_todo_records"); + }); + }); + + it("test load more with count selection buttons", () => { + cy.visit("/app/todo/view/report"); + cy.get(".filter-x-button").click(); + + 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="Reload List"]').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", "1,000 of"); + }); +}); diff --git a/cypress/integration/list_view.js b/cypress/integration/list_view.js new file mode 100644 index 0000000..874b197 --- /dev/null +++ b/cypress/integration/list_view.js @@ -0,0 +1,55 @@ +context("List View", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.setup_workflow"); + }); + }); + + it("Keep checkbox checked after Refresh", { scrollBehavior: false }, () => { + cy.go_to_list("ToDo"); + cy.clear_filters(); + cy.get(".list-header-subject > .list-subject > .list-check-all").click(); + cy.get("button[data-original-title='Reload List']").click(); + cy.get(".list-row-container .list-row-checkbox:checked").should("be.visible"); + }); + + it('enables "Actions" button', { scrollBehavior: false }, () => { + const actions = [ + "Approve", + "Reject", + "Edit", + "Export", + "Assign To", + "Apply Assignment Rule", + "Add Tags", + "Print", + "Delete", + ]; + cy.go_to_list("ToDo"); + cy.clear_filters(); + cy.get(".list-header-subject > .list-subject > .list-check-all").click(); + cy.findByRole("button", { name: "Actions" }).click(); + cy.get(".dropdown-menu li:visible .dropdown-item") + .should("have.length", 9) + .each((el, index) => { + cy.wrap(el).contains(actions[index]); + }) + .then((elements) => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.model.workflow.bulk_workflow_approval", + }).as("bulk-approval"); + cy.wrap(elements).contains("Approve").click(); + cy.wait("@bulk-approval"); + cy.hide_dialog(); + cy.reload(); + cy.clear_filters(); + cy.get(".list-row-container:visible").should("contain", "Approved"); + }); + }); +}); diff --git a/cypress/integration/list_view_drag_select.js b/cypress/integration/list_view_drag_select.js new file mode 100644 index 0000000..2dcb313 --- /dev/null +++ b/cypress/integration/list_view_drag_select.js @@ -0,0 +1,49 @@ +context("List View", () => { + before(() => { + cy.login(); + cy.go_to_list("DocType"); + }); + + it("List view check rows on drag", () => { + cy.get(".filter-x-button").click(); + cy.get(".list-row-checkbox").then(($checkbox) => { + cy.wrap($checkbox).first().trigger("mousedown"); + cy.get(".level.list-row").each(($ele) => { + cy.wrap($ele).trigger("mousemove"); + }); + cy.document().trigger("mouseup"); + }); + + cy.get(".level.list-row .list-row-checkbox").each(($checkbox) => { + cy.wrap($checkbox).should("be.checked"); + }); + }); + + it("Check all rows are checked", () => { + cy.get(".level.list-row .list-row-checkbox") + .its("length") + .then((len) => { + cy.get(".level-item.list-header-meta") + .should("be.visible") + .should("contain.text", `${len} items selected`); + }); + }); + + it("List view uncheck rows on drag", () => { + cy.get(".list-row-checkbox").then(($checkbox) => { + cy.wrap($checkbox).first().trigger("mousedown"); + cy.get(".level.list-row").each(($ele) => { + cy.wrap($ele).trigger("mousemove"); + }); + cy.document().trigger("mouseup"); + }); + + cy.get(".level.list-row .list-row-checkbox").each(($checkbox) => { + cy.wrap($checkbox).should("not.be.checked"); + }); + }); + + it("Check all rows are unchecked", () => { + cy.get(".level-item.list-header-meta").should("not.be.visible"); + }); +}); diff --git a/cypress/integration/list_view_settings.js b/cypress/integration/list_view_settings.js new file mode 100644 index 0000000..9c66edb --- /dev/null +++ b/cypress/integration/list_view_settings.js @@ -0,0 +1,42 @@ +context("List View Settings", () => { + beforeEach(() => { + cy.login(); + cy.visit("/app/website"); + }); + it("Default settings", () => { + cy.visit("/app/List/DocType/List"); + cy.clear_filters(); + cy.get(".list-count").should("contain", "20 of"); + cy.get(".list-stats").should("contain", "Tags"); + }); + it("disable count and sidebar stats then verify", () => { + cy.wait(300); + cy.visit("/app/List/DocType/List"); + cy.clear_filters(); + cy.wait(300); + cy.get(".list-count").should("contain", "20 of"); + cy.get("[href='#es-line-chat-alt']").should("be.visible"); + cy.get(".menu-btn-group button").click(); + cy.get(".dropdown-menu li").filter(":visible").contains("List Settings").click(); + cy.get(".modal-dialog").should("contain", "DocType Settings"); + + cy.findByLabelText("Disable Count").check({ force: true }); + cy.findByLabelText("Disable Comment Count").check({ force: true }); + cy.findByLabelText("Disable Sidebar Stats").check({ force: true }); + cy.findByRole("button", { name: "Save" }).click(); + + cy.reload({ force: true }); + + cy.get(".list-count").should("be.empty"); + cy.get(".list-sidebar .list-tags").should("not.exist"); + cy.get("[href='#es-line-chat-alt']").should("not.be.visible"); + + cy.get(".menu-btn-group button").click({ force: true }); + cy.get(".dropdown-menu li").filter(":visible").contains("List Settings").click(); + cy.get(".modal-dialog").should("contain", "DocType Settings"); + cy.findByLabelText("Disable Count").uncheck({ force: true }); + cy.findByLabelText("Disable Comment Count").uncheck({ force: true }); + cy.findByLabelText("Disable Sidebar Stats").uncheck({ force: true }); + cy.findByRole("button", { name: "Save" }).click(); + }); +}); diff --git a/cypress/integration/login.js b/cypress/integration/login.js new file mode 100644 index 0000000..aed9b49 --- /dev/null +++ b/cypress/integration/login.js @@ -0,0 +1,67 @@ +context("Login", () => { + beforeEach(() => { + cy.visit("/"); + cy.call("logout"); + cy.visit("/login"); + cy.location("pathname").should("eq", "/login"); + }); + + it("greets with login screen", () => { + cy.get(".page-card-head").contains("Login"); + }); + + it("validates password", () => { + cy.get("#login_email").type("Administrator"); + cy.findByRole("button", { name: "Login" }).click(); + cy.location("pathname").should("eq", "/login"); + }); + + it("validates email", () => { + cy.get("#login_password").type("qwe"); + cy.findByRole("button", { name: "Login" }).click(); + cy.location("pathname").should("eq", "/login"); + }); + + it("shows invalid login if incorrect credentials", () => { + cy.get("#login_email").type("Administrator"); + cy.get("#login_password").type("qwer"); + + cy.findByRole("button", { name: "Login" }).click(); + cy.findByRole("button", { name: "Invalid Login. Try again." }).should("exist"); + cy.location("pathname").should("eq", "/login"); + }); + + it("logs in using correct credentials", () => { + cy.get("#login_email").type("Administrator"); + cy.get("#login_password").type(Cypress.env("adminPassword")); + + cy.findByRole("button", { name: "Login" }).click(); + cy.location("pathname").should("match", /^\/app/); + cy.window().its("xhiveframework.session.user").should("eq", "Administrator"); + }); + + it("check redirect after login", () => { + // mock for OAuth 2.0 client_id, redirect_uri, scope and state + const payload = new URLSearchParams({ + uuid: "6fed1519-cfd8-4a2d-84a6-9a1799c7c741", + encoded_string: "hello all", + encoded_url: "http://test.localhost/callback", + base64_string: "aGVsbG8gYWxs", + }); + + cy.call("logout"); + + // redirect-to /me page with params to mock OAuth 2.0 like request + cy.visit( + "/login?redirect-to=/me?" + encodeURIComponent(payload.toString().replace("+", " ")) + ); + + cy.get("#login_email").type("Administrator"); + cy.get("#login_password").type(Cypress.env("adminPassword")); + + cy.findByRole("button", { name: "Login" }).click(); + + // verify redirected location and url params after login + cy.url().should("include", "/me?" + payload.toString().replace("+", "%20")); + }); +}); diff --git a/cypress/integration/multi_select_dialog.js b/cypress/integration/multi_select_dialog.js new file mode 100644 index 0000000..34632a1 --- /dev/null +++ b/cypress/integration/multi_select_dialog.js @@ -0,0 +1,107 @@ +context("MultiSelectDialog", () => { + before(() => { + cy.login(); + cy.visit("/app"); + const contact_template = { + doctype: "Contact", + first_name: "Test", + status: "Passive", + email_ids: [ + { + doctype: "Contact Email", + email_id: "test@example.com", + is_primary: 0, + }, + ], + }; + const promises = Array.from({ length: 25 }).map(() => + cy.insert_doc("Contact", contact_template, true) + ); + Promise.all(promises); + }); + + function open_multi_select_dialog() { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + new xhiveframework.ui.form.MultiSelectDialog({ + doctype: "Contact", + target: {}, + setters: { + status: null, + gender: null, + }, + add_filters_group: 1, + allow_child_item_selection: 1, + child_fieldname: "email_ids", + child_columns: ["email_id", "is_primary"], + }); + }); + } + + it("checks multi select dialog api works", () => { + open_multi_select_dialog(); + cy.get_open_dialog().should("contain", "Select Contacts"); + }); + + it("checks for filters", () => { + ["search_term", "status", "gender"].forEach((fieldname) => { + cy.get_open_dialog() + .get(`.xhiveframework-control[data-fieldname="${fieldname}"]`) + .should("exist"); + }); + + // add_filters_group: 1 should add a filter group + cy.get_open_dialog().get(`.xhiveframework-control[data-fieldname="filter_area"]`).should("exist"); + }); + + it("checks for child item selection", () => { + cy.get_open_dialog().get(`.dt-row-header`).should("not.exist"); + + cy.get_open_dialog() + .get(`.xhiveframework-control[data-fieldname="allow_child_item_selection"]`) + .find('input[data-fieldname="allow_child_item_selection"]') + .should("exist") + .click({ force: true }); + + cy.get_open_dialog() + .get(`.xhiveframework-control[data-fieldname="child_selection_area"]`) + .should("exist"); + + cy.get_open_dialog().get(`.dt-row-header`).should("contain", "Contact"); + + cy.get_open_dialog().get(`.dt-row-header`).should("contain", "Email Id"); + + cy.get_open_dialog().get(`.dt-row-header`).should("contain", "Is Primary"); + }); + + it("tests more button", () => { + cy.get_open_dialog() + .get(`.xhiveframework-control[data-fieldname="search_term"]`) + .find('input[data-fieldname="search_term"]') + .should("exist") + .type("Test", { delay: 200 }); + cy.get_open_dialog() + .get(`.xhiveframework-control[data-fieldname="more_child_btn"]`) + .should("exist") + .as("more-btn"); + + cy.get_open_dialog() + .get(".datatable .dt-scrollable .dt-row") + .should(($rows) => { + expect($rows).to.have.length(20); + }); + + cy.intercept("POST", "api/method/xhiveframework.client.get_list").as("get-more-records"); + cy.get("@more-btn").find("button").click({ force: true }); + cy.wait("@get-more-records"); + + cy.get_open_dialog() + .get(".datatable .dt-scrollable .dt-row") + .should(($rows) => { + if ($rows.length <= 20) { + throw new Error("More button doesn't work"); + } + }); + }); +}); diff --git a/cypress/integration/navigation.js b/cypress/integration/navigation.js new file mode 100644 index 0000000..d93adc1 --- /dev/null +++ b/cypress/integration/navigation.js @@ -0,0 +1,36 @@ +context("Navigation", () => { + before(() => { + cy.visit("/login"); + cy.login(); + cy.visit("/app/website"); + }); + it("Navigate to route with hash in document name", () => { + cy.insert_doc( + "Client Script", + { + __newname: "ABC#123", + dt: "User", + script: "console.log('ran')", + enabled: 0, + }, + true + ); + cy.visit(`/app/client-script/${encodeURIComponent("ABC#123")}`); + cy.title().should("eq", "ABC#123"); + cy.go("back"); + cy.title().should("eq", "Website"); + }); + + it("Navigate to previous page after login", () => { + cy.visit("/app/todo"); + cy.get(".page-head").findByTitle("To Do").should("be.visible"); + cy.clear_filters(); + cy.call("logout"); + cy.reload().as("reload"); + cy.get("@reload").get(".page-card .btn-primary").contains("Login").click(); + cy.location("pathname").should("eq", "/login"); + cy.login(); + cy.visit("/app"); + cy.location("pathname").should("eq", "/app/todo"); + }); +}); diff --git a/cypress/integration/number_card.js b/cypress/integration/number_card.js new file mode 100644 index 0000000..24227fe --- /dev/null +++ b/cypress/integration/number_card.js @@ -0,0 +1,22 @@ +context("Number Card", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + it("Check filter populate for child table doctype", () => { + cy.new_form("Number Card"); + cy.get('[data-fieldname="parent_document_type"]').should("have.css", "display", "none"); + + cy.get_field("document_type", "Link"); + cy.fill_field("document_type", "Workspace Link", "Link").focus().blur(); + cy.get_field("document_type", "Link").should("have.value", "Workspace Link"); + + cy.fill_field("label", "Test Number Card", "Data"); + + cy.get('[data-fieldname="filters_json"]').click().wait(200); + cy.get(".modal-body .filter-action-buttons .add-filter").click(); + cy.get(".modal-body .fieldname-select-area").click(); + cy.get(".modal-actions .btn-modal-close").click(); + }); +}); diff --git a/cypress/integration/permissions.js b/cypress/integration/permissions.js new file mode 100644 index 0000000..2da4f81 --- /dev/null +++ b/cypress/integration/permissions.js @@ -0,0 +1,41 @@ +context.skip("Permissions API", () => { + before(() => { + cy.visit("/login"); + cy.remove_role("xhiveframework@example.com", "System Manager"); + cy.visit("/app"); + }); + + it("Checks permissions via `has_perm` for Kanban Board DocType", () => { + cy.visit("/app/kanban-board/view/list"); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.model.with_doctype("Kanban Board", function () { + // needed to make sure doc meta is loaded + expect(xhiveframework.perm.has_perm("Kanban Board", 0, "read")).to.equal(true); + expect(xhiveframework.perm.has_perm("Kanban Board", 0, "write")).to.equal(true); + expect(xhiveframework.perm.has_perm("Kanban Board", 0, "print")).to.equal(false); + }); + }); + }); + + it("Checks permissions via `get_perm` for Kanban Board DocType", () => { + cy.visit("/app/kanban-board/view/list"); + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.model.with_doctype("Kanban Board", function () { + // needed to make sure doc meta is loaded + const perms = xhiveframework.perm.get_perm("Kanban Board"); + expect(perms.read).to.equal(true); + expect(perms.write).to.equal(true); + expect(perms.rights_without_if_owner).to.include("read"); + }); + }); + }); + + after(() => { + cy.add_role("xhiveframework@example.com", "System Manager"); + cy.call("logout"); + }); +}); diff --git a/cypress/integration/query_report.js b/cypress/integration/query_report.js new file mode 100644 index 0000000..67dd576 --- /dev/null +++ b/cypress/integration/query_report.js @@ -0,0 +1,91 @@ +context("Query Report", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + cy.insert_doc( + "Report", + { + report_name: "Test ToDo Report", + ref_doctype: "ToDo", + report_type: "Query Report", + query: "select * from tabToDo", + }, + true + ).as("doc"); + cy.create_records({ + doctype: "ToDo", + description: "this is a test todo for query report", + }).as("todos"); + }); + + it("add custom column in report", () => { + cy.visit("/app/query-report/Permitted Documents For User"); + + cy.get(".page-form.flex", { timeout: 60000 }) + .should("have.length", 1) + .then(() => { + cy.get('#page-query-report input[data-fieldname="user"]').as("input-user"); + cy.get("@input-user").focus().type("test@xhiveerp.com", { delay: 100 }).blur(); + cy.wait(300); + cy.get('#page-query-report input[data-fieldname="doctype"]').as("input-role"); + cy.get("@input-role").focus().type("Role", { delay: 100 }).blur(); + + cy.get(".datatable").should("exist"); + cy.get("#page-query-report .page-actions .menu-btn-group button").click({ + force: true, + }); + cy.get("#page-query-report .menu-btn-group .dropdown-menu") + .contains("Add Column") + .click({ force: true }); + cy.get_open_dialog().get(".modal-title").should("contain", "Add Column"); + cy.get('select[data-fieldname="doctype"]').select("Role", { force: true }); + cy.get('select[data-fieldname="field"]').select("Role Name", { force: true }); + cy.get('select[data-fieldname="insert_after"]').select("Name", { force: true }); + cy.get_open_dialog() + .findByRole("button", { name: "Submit" }) + .click({ force: true }); + cy.get("#page-query-report .page-actions .menu-btn-group button").click({ + force: true, + }); + cy.get("#page-query-report .menu-btn-group .dropdown-menu") + .contains("Save") + .click({ timeout: 100, force: true }); + cy.get_open_dialog().get(".modal-title").should("contain", "Save Report"); + + cy.get('input[data-fieldname="report_name"]').type("Test Report", { + delay: 100, + force: true, + }); + cy.get_open_dialog() + .findByRole("button", { name: "Submit" }) + .click({ timeout: 1000, force: true }); + }); + }); + + let save_report_and_open = (report, update_name) => { + cy.get("#page-query-report .page-actions .menu-btn-group button").click({ force: true }); + cy.get("#page-query-report .menu-btn-group .dropdown-menu") + .contains("Save") + .click({ timeout: 100, force: true }); + cy.get_open_dialog().get(".modal-title").should("contain", "Save Report"); + + cy.get('input[data-fieldname="report_name"]').type(update_name, { + delay: 100, + force: true, + }); + cy.get_open_dialog() + .findByRole("button", { name: "Submit" }) + .click({ timeout: 1000, force: true }); + + cy.visit("/app/query-report/" + report); + cy.get(".datatable").should("exist"); + }; + + it("test multi level query report", () => { + cy.visit("/app/query-report/Test ToDo Report"); + cy.get(".datatable").should("exist"); + + save_report_and_open("Test ToDo Report 1", " 1"); + save_report_and_open("Test ToDo Report 11", "1"); + }); +}); diff --git a/cypress/integration/recorder.js b/cypress/integration/recorder.js new file mode 100644 index 0000000..938f23b --- /dev/null +++ b/cypress/integration/recorder.js @@ -0,0 +1,72 @@ +context.skip("Recorder", () => { + before(() => { + cy.login(); + }); + + beforeEach(() => { + cy.visit("/app/recorder"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + // reset recorder + return xhiveframework.xcall("xhiveframework.recorder.stop").then(() => { + return xhiveframework.xcall("xhiveframework.recorder.delete"); + }); + }); + }); + + it("Recorder Empty State", () => { + cy.get(".page-head").findByTitle("Recorder").should("exist"); + + cy.get(".indicator-pill").should("contain", "Inactive").should("have.class", "red"); + + cy.get(".page-actions").findByRole("button", { name: "Start" }).should("exist"); + cy.get(".page-actions").findByRole("button", { name: "Clear" }).should("exist"); + + cy.get(".msg-box").should("contain", "Recorder is Inactive"); + cy.get(".msg-box").findByRole("button", { name: "Start Recording" }).should("exist"); + }); + + it("Recorder Start", () => { + cy.get(".page-actions").findByRole("button", { name: "Start" }).click(); + cy.get(".indicator-pill").should("contain", "Active").should("have.class", "green"); + + cy.get(".msg-box").should("contain", "No Requests found"); + + cy.visit("/app/List/DocType/List"); + cy.intercept("POST", "/api/method/xhiveframework.desk.reportview.get").as("list_refresh"); + cy.wait("@list_refresh"); + + cy.get(".page-head").findByTitle("DocType").should("exist"); + cy.get(".list-count").should("contain", "20 of "); + + cy.visit("/app/recorder"); + cy.get(".page-head").findByTitle("Recorder").should("exist"); + cy.get(".xhiveframework-list .result-list").should( + "contain", + "/api/method/xhiveframework.desk.reportview.get" + ); + }); + + it("Recorder View Request", () => { + cy.get(".page-actions").findByRole("button", { name: "Start" }).click(); + + cy.visit("/app/List/DocType/List"); + cy.intercept("POST", "/api/method/xhiveframework.desk.reportview.get").as("list_refresh"); + cy.wait("@list_refresh"); + + cy.get(".page-head").findByTitle("DocType").should("exist"); + cy.get(".list-count").should("contain", "20 of "); + + cy.visit("/app/recorder"); + + cy.get(".xhiveframework-list .list-row-container span") + .contains("/api/method/xhiveframework") + .should("be.visible") + .click({ force: true }); + + cy.url().should("include", "/recorder/request"); + cy.get("form").should("contain", "/api/method/xhiveframework"); + }); +}); diff --git a/cypress/integration/relative_time_filters.js b/cypress/integration/relative_time_filters.js new file mode 100644 index 0000000..3778ead --- /dev/null +++ b/cypress/integration/relative_time_filters.js @@ -0,0 +1,47 @@ +// TODO: Enable this again +// currently this is flaky possibly because of different timezone in CI + +// context('Relative Timeframe', () => { +// before(() => { +// cy.login(); +// cy.visit('/app/website'); +// cy.window().its('xhiveframework').then(xhiveframework => { +// xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_todo_records"); +// }); +// }); +// it('sets relative timespan filter for last week and filters list', () => { +// cy.visit('/app/List/ToDo/List'); +// cy.clear_filters(); +// cy.get('.list-row:contains("this is fourth todo")').should('exist'); +// cy.add_filter(); +// cy.get('.fieldname-select-area').should('exist'); +// cy.get('.fieldname-select-area input').type("Due Date{enter}", { delay: 100 }); +// cy.get('select.condition.form-control').select("Timespan"); +// cy.get('.filter-field select.input-with-feedback.form-control').select("last week"); +// cy.intercept('POST', '/api/method/xhiveframework.desk.reportview.get').as('list_refresh'); +// cy.get('.filter-popover .apply-filters').click({ force: true }); +// cy.wait('@list_refresh'); +// cy.get('.list-row-container').its('length').should('eq', 1); +// cy.get('.list-row-container').should('contain', 'this is second todo'); +// cy.intercept('POST', '/api/method/xhiveframework.model.utils.user_settings.save') +// .as('save_user_settings'); +// cy.clear_filters(); +// cy.wait('@save_user_settings'); +// }); +// it('sets relative timespan filter for next week and filters list', () => { +// cy.visit('/app/List/ToDo/List'); +// cy.clear_filters(); +// cy.get('.list-row:contains("this is fourth todo")').should('exist'); +// cy.add_filter(); +// cy.get('.fieldname-select-area input').type("Due Date{enter}", { delay: 100 }); +// cy.get('select.condition.form-control').select("Timespan"); +// cy.get('.filter-field select.input-with-feedback.form-control').select("next week"); +// cy.intercept('POST', '/api/method/xhiveframework.desk.reportview.get').as('list_refresh'); +// cy.get('.filter-popover .apply-filters').click({ force: true }); +// cy.wait('@list_refresh'); +// cy.intercept('POST', '/api/method/xhiveframework.model.utils.user_settings.save') +// .as('save_user_settings'); +// cy.clear_filters(); +// cy.wait('@save_user_settings'); +// }); +// }); diff --git a/cypress/integration/report_view.js b/cypress/integration/report_view.js new file mode 100644 index 0000000..38980b2 --- /dev/null +++ b/cypress/integration/report_view.js @@ -0,0 +1,47 @@ +import custom_submittable_doctype from "../fixtures/custom_submittable_doctype"; +const doctype_name = custom_submittable_doctype.name; + +context("Report View", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + cy.insert_doc("DocType", custom_submittable_doctype, true); + cy.clear_cache(); + cy.insert_doc( + doctype_name, + { + title: "Doc 1", + description: "Random Text", + enabled: 0, + docstatus: 1, // submit document + }, + true + ); + }); + + it("Field with enabled allow_on_submit should be editable.", () => { + cy.intercept("POST", "api/method/xhiveframework.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.get(".dt-row-0 > .dt-cell--col-3").click(); // click outside + + cy.wait("@value-update"); + + cy.call("xhiveframework.client.get_value", { + doctype: doctype_name, + filters: { + title: "Doc 1", + }, + fieldname: "enabled", + }).then((r) => { + expect(r.message.enabled).to.equals(1); + }); + }); +}); diff --git a/cypress/integration/rounding.js b/cypress/integration/rounding.js new file mode 100644 index 0000000..f778e00 --- /dev/null +++ b/cypress/integration/rounding.js @@ -0,0 +1,104 @@ +context("Rounding behaviour", () => { + before(() => { + cy.login(); + cy.visit("/app/"); + }); + + it("Commercial Rounding", () => { + cy.window() + .its("flt") + .then((flt) => { + let rounding_method = "Commercial Rounding"; + + expect(flt("0.5", 0, null, rounding_method)).eq(1); + expect(flt("0.3", null, null, rounding_method)).eq(0.3); + + expect(flt("1.5", 0, null, rounding_method)).eq(2); + + // positive rounding to integers + expect(flt(0.4, 0, null, rounding_method)).eq(0); + expect(flt(0.5, 0, null, rounding_method)).eq(1); + expect(flt(1.455, 0, null, rounding_method)).eq(1); + expect(flt(1.5, 0, null, rounding_method)).eq(2); + + // negative rounding to integers + expect(flt(-0.5, 0, null, rounding_method)).eq(-1); + expect(flt(-1.5, 0, null, rounding_method)).eq(-2); + + // negative precision i.e. round to nearest 10th + expect(flt(123, -1, null, rounding_method)).eq(120); + expect(flt(125, -1, null, rounding_method)).eq(130); + expect(flt(134.45, -1, null, rounding_method)).eq(130); + expect(flt(135, -1, null, rounding_method)).eq(140); + + // positive multiple digit rounding + expect(flt(1.25, 1, null, rounding_method)).eq(1.3); + expect(flt(0.15, 1, null, rounding_method)).eq(0.2); + expect(flt(2.675, 2, null, rounding_method)).eq(2.68); + + // negative multiple digit rounding + expect(flt(-1.25, 1, null, rounding_method)).eq(-1.3); + expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2); + }); + }); + + it("Banker's Rounding", () => { + cy.window() + .its("flt") + .then((flt) => { + let rounding_method = "Banker's Rounding"; + + expect(flt("0.5", 0, null, rounding_method)).eq(0); + expect(flt("0.3", null, null, rounding_method)).eq(0.3); + + expect(flt("1.5", 0, null, rounding_method)).eq(2); + + // positive rounding to integers + expect(flt(0.4, 0, null, rounding_method)).eq(0); + expect(flt(0.5, 0, null, rounding_method)).eq(0); + expect(flt(1.455, 0, null, rounding_method)).eq(1); + expect(flt(1.5, 0, null, rounding_method)).eq(2); + + // negative rounding to integers + expect(flt(-0.5, 0, null, rounding_method)).eq(0); + expect(flt(-1.5, 0, null, rounding_method)).eq(-2); + + // negative precision i.e. round to nearest 10th + expect(flt(123, -1, null, rounding_method)).eq(120); + expect(flt(125, -1, null, rounding_method)).eq(120); + expect(flt(134.45, -1, null, rounding_method)).eq(130); + expect(flt(135, -1, null, rounding_method)).eq(140); + + // positive multiple digit rounding + expect(flt(1.25, 1, null, rounding_method)).eq(1.2); + expect(flt(0.15, 1, null, rounding_method)).eq(0.2); + expect(flt(2.675, 2, null, rounding_method)).eq(2.68); + expect(flt(-2.675, 2, null, rounding_method)).eq(-2.68); + + // negative multiple digit rounding + expect(flt(-1.25, 1, null, rounding_method)).eq(-1.2); + expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2); + + // Nearest number and not even (the default behaviour) + expect(flt(0.5, 0, null, rounding_method)).eq(0); + expect(flt(1.5, 0, null, rounding_method)).eq(2); + expect(flt(2.5, 0, null, rounding_method)).eq(2); + expect(flt(3.5, 0, null, rounding_method)).eq(4); + + expect(flt(0.05, 1, null, rounding_method)).eq(0.0); + expect(flt(1.15, 1, null, rounding_method)).eq(1.2); + expect(flt(2.25, 1, null, rounding_method)).eq(2.2); + expect(flt(3.35, 1, null, rounding_method)).eq(3.4); + + expect(flt(-0.5, 0, null, rounding_method)).eq(0); + expect(flt(-1.5, 0, null, rounding_method)).eq(-2); + expect(flt(-2.5, 0, null, rounding_method)).eq(-2); + expect(flt(-3.5, 0, null, rounding_method)).eq(-4); + + expect(flt(-0.05, 1, null, rounding_method)).eq(0.0); + expect(flt(-1.15, 1, null, rounding_method)).eq(-1.2); + expect(flt(-2.25, 1, null, rounding_method)).eq(-2.2); + expect(flt(-3.35, 1, null, rounding_method)).eq(-3.4); + }); + }); +}); diff --git a/cypress/integration/routing.js b/cypress/integration/routing.js new file mode 100644 index 0000000..0822dd9 --- /dev/null +++ b/cypress/integration/routing.js @@ -0,0 +1,40 @@ +const list_view = "/app/todo"; + +// test round trip with filter types + +const test_queries = [ + "?status=Open", + `?date=%5B"Between"%2C%5B"2022-06-01"%2C"2022-06-30"%5D%5D`, + `?date=%5B">"%2C"2022-06-01"%5D`, + `?name=%5B"like"%2C"%2542%25"%5D`, + `?status=%5B"not%20in"%2C%5B"Open"%2C"Closed"%5D%5D`, +]; + +describe("SPA Routing", { scrollBehavior: false }, () => { + before(() => { + cy.login(); + cy.go_to_list("ToDo"); + }); + + after(() => { + cy.clear_filters(); // avoid flake in future tests + }); + + it("should apply filter on list view from route", () => { + test_queries.forEach((query) => { + const full_url = `${list_view}${query}`; + cy.visit(full_url); + cy.findByTitle("To Do").should("exist"); + + const expected = new URLSearchParams(query); + cy.location().then((loc) => { + const actual = new URLSearchParams(loc.search); + // This might appear like a dumb test checking visited URL to itself + // but it's actually doing a round trip + // URL with params -> parsed filters -> new URL + // if it's same that means everything worked in between. + expect(actual.toString()).to.eq(expected.toString()); + }); + }); + }); +}); diff --git a/cypress/integration/sidebar.js b/cypress/integration/sidebar.js new file mode 100644 index 0000000..6a54da4 --- /dev/null +++ b/cypress/integration/sidebar.js @@ -0,0 +1,144 @@ +const verify_attachment_visibility = (document, is_private) => { + cy.visit(`/app/${document}`); + + const assertion = is_private ? "be.checked" : "not.be.checked"; + cy.get(".add-attachment-btn").click(); + + cy.get_open_dialog() + .find(".file-upload-area") + .selectFile("cypress/fixtures/sample_image.jpg", { + action: "drag-drop", + }); + + cy.get_open_dialog().findByRole("checkbox", { name: "Private" }).should(assertion); +}; + +const attach_file = (file, no_of_files = 1) => { + let files = []; + if (file) { + files = [file]; + } else if (no_of_files > 1) { + // attach n files + files = [...Array(no_of_files)].map( + (el, idx) => + "cypress/fixtures/sample_attachments/attachment-" + + (idx + 1) + + (idx == 0 ? ".jpg" : ".txt") + ); + } + + cy.get(".add-attachment-btn").click(); + cy.get_open_dialog().find(".file-upload-area").selectFile(files, { + action: "drag-drop", + }); + cy.get_open_dialog().findByRole("button", { name: "Upload" }).click(); +}; + +context("Sidebar", () => { + before(() => { + cy.visit("/"); + cy.login(); + cy.visit("/app"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.call("xhiveframework.tests.ui_test_helpers.create_blog_post"); + }); + }); + + it("Verify attachment visibility config", () => { + cy.call("xhiveframework.tests.ui_test_helpers.create_todo", { + description: "Sidebar Attachment ToDo", + }).then((todo) => { + verify_attachment_visibility(`todo/${todo.message.name}`, true); + }); + verify_attachment_visibility("blog-post/test-blog-attachment-post", false); + }); + + it("Verify attachment accessibility UX", () => { + cy.call("xhiveframework.tests.ui_test_helpers.create_todo_with_attachment_limit", { + description: "Sidebar Attachment Access Test ToDo", + }).then((todo) => { + cy.visit(`/app/todo/${todo.message.name}`); + + attach_file("cypress/fixtures/sample_image.jpg"); + cy.get(".explore-link").should("be.visible"); + cy.get(".show-all-btn").should("be.hidden"); + + // attach 10 images + attach_file(null, 10); + cy.get(".show-all-btn").should("be.visible"); + + // attach 1 more image to reach attachment limit + attach_file("cypress/fixtures/sample_attachments/attachment-11.txt"); + cy.get(".add-attachment-btn").should("be.hidden"); + cy.get(".explore-link").should("be.visible"); + + // test "Show All" button + cy.get(".attachment-row").should("have.length", 10); + cy.get(".show-all-btn").click({ force: true }); + cy.get(".attachment-row").should("have.length", 12); + }); + }); + + it('Test for checking "Assigned To" counter value, adding filter and adding & removing an assignment', () => { + cy.call("xhiveframework.tests.ui_test_helpers.create_todo", { + description: "Sidebar Attachment ToDo", + }).then((todo) => { + let todo_name = todo.message.name; + cy.visit("/app/todo"); + cy.click_sidebar_button("Assigned To"); + + //To check if no filter is available in "Assigned To" dropdown + cy.get(".empty-state").should("contain", "No filters found"); + + //Assigning a doctype to a user + cy.visit(`/app/todo/${todo_name}`); + cy.get(".add-assignment-btn").click(); + cy.get_field("assign_to_me", "Check").click(); + cy.wait(1000); + cy.get(".modal-footer > .standard-actions > .btn-primary").click(); + cy.visit("/app/todo"); + cy.click_sidebar_button("Assigned To"); + + //To check if filter is added in "Assigned To" dropdown after assignment + cy.get( + ".group-by-field.show > .dropdown-menu > .group-by-item > .dropdown-item" + ).should("contain", "1"); + + //To check if there is no filter added to the listview + cy.get(".filter-button").should("contain", "Filter"); + + //To add a filter to display data into the listview + cy.get( + ".group-by-field.show > .dropdown-menu > .group-by-item > .dropdown-item" + ).click(); + + //To check if filter is applied + cy.click_filter_button().get(".filter-label").should("contain", "1"); + cy.get(".fieldname-select-area > .awesomplete > .form-control").should( + "have.value", + "Assigned To" + ); + cy.get(".condition").should("have.value", "like"); + cy.get(".filter-field > .form-group > .input-with-feedback").should( + "have.value", + `%${cy.config("testUser")}%` + ); + cy.click_filter_button(); + + //To remove the applied filter + cy.clear_filters(); + + //To remove the assignment + cy.visit(`/app/todo/${todo_name}`); + cy.get(".assignments > .avatar-group > .avatar > .avatar-frame").click(); + cy.get(".remove-btn").click({ force: true }); + cy.hide_dialog(); + cy.visit("/app/todo"); + cy.click_sidebar_button("Assigned To"); + cy.get(".empty-state").should("contain", "No filters found"); + }); + }); +}); diff --git a/cypress/integration/socket_updates.js b/cypress/integration/socket_updates.js new file mode 100644 index 0000000..7055ab0 --- /dev/null +++ b/cypress/integration/socket_updates.js @@ -0,0 +1,80 @@ +context("Realtime updates", () => { + before(() => { + cy.login(); + }); + + beforeEach(() => { + cy.visit("/app/todo"); + // required because immediately after load socket is still connecting. + // Not a huge deal breaker in prod. + cy.wait(500); + cy.clear_filters(); + }); + + it("Shows version conflict warning", { scrollBehavior: false }, () => { + cy.insert_doc("ToDo", { description: "old" }).then((doc) => { + cy.visit(`/app/todo/${doc.name}`); + // make form dirty + cy.fill_field("status", "Cancelled", "Select"); + + // update doc using api - simulating parallel change by another user + cy.update_doc("ToDo", doc.name, { status: "Closed" }).then(() => { + cy.findByRole("button", { name: "Refresh" }).click(); + cy.get_field("status", "Select").should("have.value", "Closed"); + }); + }); + }); + + it("List view updates in realtime on insert", { scrollBehavior: false }, () => { + const original = "Added for realtime update"; + const updated = "Updated for realtime update"; + cy.insert_doc("ToDo", { description: original }).then((doc) => { + cy.contains(original).should("be.visible"); + + // update doc using api - simulating parallel change by another user + cy.update_doc("ToDo", doc.name, { description: updated }).then(() => { + cy.contains(updated).should("be.visible"); + }); + }); + }); + + it("Receives msgprint from server", { scrollBehavior: false }, () => { + // required because immediately after load socket is still connecting. + // Not a deal breaker in prod + const msg = "msgprint sent via realtime"; + publish_realtime({ event: "msgprint", message: msg }).then(() => { + cy.contains(msg).should("be.visible"); + }); + }); + + it("Recieves custom messages from server", { scrollBehavior: false }, () => { + const event = "cypress_event"; + let handler = { + handle() { + console.log("clear"); + }, + }; + cy.spy(handler, "handle").as("callback"); + + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.realtime.on(event, () => handler.handle()); + }); + + publish_realtime({ event }).then(() => { + cy.get("@callback").should("be.called"); + }); + }); + + it("Progress bar", { scrollBehavior: false }, () => { + const title = "RealTime Progress"; + cy.call("xhiveframework.tests.ui_test_helpers.publish_progress", { title }).then(() => { + cy.contains(title).should("be.visible"); + }); + }); +}); + +function publish_realtime(args) { + return cy.call("xhiveframework.tests.ui_test_helpers.publish_realtime", args); +} diff --git a/cypress/integration/table_multiselect.js b/cypress/integration/table_multiselect.js new file mode 100644 index 0000000..b691ff0 --- /dev/null +++ b/cypress/integration/table_multiselect.js @@ -0,0 +1,59 @@ +context("Table MultiSelect", () => { + before(() => { + cy.login(); + }); + + let name = "table multiselect" + Math.random().toString().slice(2, 8); + + it("select value from multiselect dropdown", () => { + cy.new_form("Assignment Rule"); + cy.fill_field("__newname", name); + cy.fill_field("document_type", "Blog Post"); + cy.get(".section-head").contains("Assignment Rules").scrollIntoView(); + cy.fill_field("assign_condition", 'status=="Open"', "Code"); + cy.get('input[data-fieldname="users"]').focus().as("input"); + cy.get('input[data-fieldname="users"] + ul').should("be.visible"); + cy.get("@input").type("test@xhiveerp", { delay: 100 }); + cy.wait(500); + cy.get("@input").type("{enter}"); + cy.get( + '.xhiveframework-control[data-fieldname="users"] .form-control .tb-selected-value .btn-link-to-form' + ).as("selected-value"); + cy.get("@selected-value").should("contain", "test@xhiveerp.com"); + + cy.intercept("POST", "/api/method/xhiveframework.desk.form.save.savedocs").as("save_form"); + // trigger save + cy.get(".primary-action").click(); + cy.wait("@save_form").its("response.statusCode").should("eq", 200); + cy.get("@selected-value").should("contain", "test@xhiveerp.com"); + }); + + it("delete value using backspace", () => { + cy.go_to_list("Assignment Rule"); + cy.get(`.list-subject:contains("table multiselect")`).last().find("a").click(); + cy.get('input[data-fieldname="users"]').focus().type("{backspace}"); + cy.get('.xhiveframework-control[data-fieldname="users"] .form-control .tb-selected-value').should( + "not.exist" + ); + }); + + it("delete value using x", () => { + cy.go_to_list("Assignment Rule"); + cy.get(`.list-subject:contains("table multiselect")`).last().find("a").click(); + cy.get('.xhiveframework-control[data-fieldname="users"] .form-control .tb-selected-value').as( + "existing_value" + ); + cy.get("@existing_value").find(".btn-remove").click(); + cy.get("@existing_value").should("not.exist"); + }); + + it("navigate to selected value", () => { + cy.go_to_list("Assignment Rule"); + cy.get(`.list-subject:contains("table multiselect")`).last().find("a").click(); + cy.get('.xhiveframework-control[data-fieldname="users"] .form-control .tb-selected-value').as( + "existing_value" + ); + cy.get("@existing_value").find(".btn-link-to-form").click(); + cy.location("pathname").should("contain", "/user/test%40xhiveerp.com"); + }); +}); diff --git a/cypress/integration/url_data_field.js b/cypress/integration/url_data_field.js new file mode 100644 index 0000000..b26f0b4 --- /dev/null +++ b/cypress/integration/url_data_field.js @@ -0,0 +1,42 @@ +import data_field_validation_doctype from "../fixtures/data_field_validation_doctype"; + +const doctype_name = data_field_validation_doctype.name; + +context("URL Data Field Input", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + return cy.insert_doc("DocType", data_field_validation_doctype, true); + }); + + describe("URL Data Field Input ", () => { + it("should not show URL link button without focus", () => { + cy.new_form(doctype_name); + cy.get_field("url").clear().type("https://xhiveframework.io"); + cy.get_field("url").blur().wait(500); + cy.get(".link-btn").should("not.be.visible"); + }); + + it("should show URL link button on focus", () => { + cy.get_field("url").focus().wait(500); + cy.get(".link-btn").should("be.visible"); + }); + + it("should not show URL link button for invalid URL", () => { + cy.get_field("url").clear().type("fuzzbuzz"); + cy.get(".link-btn").should("not.be.visible"); + }); + + it("should have valid URL link with target _blank", () => { + cy.get_field("url").clear().type("https://xhiveframework.io"); + cy.get(".link-btn .btn-open").should("have.attr", "href", "https://xhiveframework.io"); + cy.get(".link-btn .btn-open").should("have.attr", "target", "_blank"); + }); + + it("should inject anchor tag in read-only URL data field", () => { + cy.get('[data-fieldname="read_only_url"]') + .find("a") + .should("have.attr", "target", "_blank"); + }); + }); +}); diff --git a/cypress/integration/view_routing.js b/cypress/integration/view_routing.js new file mode 100644 index 0000000..0cf870f --- /dev/null +++ b/cypress/integration/view_routing.js @@ -0,0 +1,231 @@ +context("View", () => { + before(() => { + cy.login(); + cy.visit("/app/website"); + }); + + it("Route to ToDo List View", () => { + cy.visit("/app/todo/view/list"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("List"); + }); + }); + + it("Route to ToDo Report View", () => { + cy.visit("/app/todo/view/report"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + + it("Route to ToDo Dashboard View", () => { + cy.visit("/app/todo/view/dashboard"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Dashboard"); + }); + }); + + it("Route to ToDo Gantt View", () => { + cy.visit("/app/todo/view/gantt"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Gantt"); + }); + }); + + it("Route to ToDo Kanban View", () => { + cy.call("xhiveframework.tests.ui_test_helpers.create_kanban").then(() => { + cy.visit("/app/note/view/kanban/_Note _Kanban"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Kanban"); + }); + }); + }); + + it("Route to ToDo Calendar View", () => { + cy.visit("/app/todo/view/calendar"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Calendar"); + }); + }); + + it("Route to Custom Tree View", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_tree_doctype").then(() => { + cy.visit("/app/custom-tree/view/tree"); + cy.wait(500); + cy.window() + .its("cur_tree") + .then((list) => { + expect(list.view_name).to.equal("Tree"); + }); + }); + }); + + it("Route to Custom Image View", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_image_doctype").then(() => { + cy.visit("app/custom-image/view/image"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Image"); + }); + }); + }); + + it("Route to Communication Inbox View", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_inbox").then(() => { + cy.visit("app/communication/view/inbox"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Inbox"); + }); + }); + }); + + it("Route to File View", () => { + cy.intercept("POST", "/api/method/xhiveframework.desk.reportview.get").as("list_loaded"); + cy.visit("app/file"); + cy.wait("@list_loaded"); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("File"); + expect(list.current_folder).to.equal("Home"); + }); + + cy.visit("app/file/view/home/Attachments"); + cy.wait("@list_loaded"); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("File"); + expect(list.current_folder).to.equal("Home/Attachments"); + }); + }); + + it("Re-route to default view", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { view: "Report" }).then(() => { + cy.visit("app/event"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Route to default view from app/{doctype}", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { view: "Report" }).then(() => { + cy.visit("/app/event"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Route to default view from app/{doctype}/view", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { view: "Report" }).then(() => { + cy.visit("/app/event/view"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Force Route to default view from app/{doctype}", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { + view: "Report", + force_reroute: true, + }).then(() => { + cy.visit("/app/event"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Force Route to default view from app/{doctype}/view", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { + view: "Report", + force_reroute: true, + }).then(() => { + cy.visit("/app/event/view"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Force Route to default view from app/{doctype}/view", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { + view: "Report", + force_reroute: true, + }).then(() => { + cy.visit("/app/event/view/list"); + cy.wait(500); + cy.window() + .its("cur_list") + .then((list) => { + expect(list.view_name).to.equal("Report"); + }); + }); + }); + + it("Validate Route History for Default View", () => { + cy.call("xhiveframework.tests.ui_test_helpers.setup_default_view", { view: "Report" }).then(() => { + cy.visit("/app/event"); + cy.visit("/app/event/view/list"); + cy.location("pathname").should("eq", "/app/event/view/list"); + cy.go("back"); + cy.location("pathname").should("eq", "/app/event"); + }); + }); + + it("Route to Form", () => { + const test_user = cy.config("testUser"); + cy.visit(`/app/user/${test_user}`); + cy.window() + .its("cur_frm") + .then((frm) => { + expect(frm.doc.name).to.equal(test_user); + }); + }); + + it("Route to Website Workspace", () => { + cy.visit("/app/website"); + cy.get(".title-text").should("contain", "Website"); + }); +}); diff --git a/cypress/integration/web_form.js b/cypress/integration/web_form.js new file mode 100644 index 0000000..931ac5d --- /dev/null +++ b/cypress/integration/web_form.js @@ -0,0 +1,278 @@ +context("Web Form", () => { + before(() => { + cy.login("Administrator"); + cy.visit("/app/"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.prepare_webform_test"); + }); + }); + + it("Create Web Form", () => { + cy.visit("/app/web-form/new"); + + cy.intercept("POST", "/api/method/xhiveframework.desk.form.save.savedocs").as("save_form"); + + cy.fill_field("title", "Note"); + cy.fill_field("doc_type", "Note", "Link"); + cy.fill_field("module", "Website", "Link"); + cy.click_custom_action_button("Get Fields"); + cy.click_custom_action_button("Publish"); + + cy.wait("@save_form"); + + cy.get_field("route").should("have.value", "note"); + cy.get(".title-area .indicator-pill").contains("Published"); + }); + + it("Open Web Form", () => { + cy.visit("/note"); + cy.fill_field("title", "Note 1"); + cy.get(".web-form-actions button").contains("Save").click(); + + cy.url().should("include", "/note/new"); + + cy.call("logout"); + cy.visit("/note"); + + cy.url().should("include", "/note/new"); + + cy.fill_field("title", "Guest Note 1"); + cy.get(".web-form-actions button").contains("Save").click(); + + cy.url().should("include", "/note/new"); + + cy.visit("/note"); + cy.url().should("include", "/note/new"); + }); + + it("Login Required", () => { + cy.call("logout"); + cy.login("Administrator"); + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get('input[data-fieldname="login_required"]').check({ force: true }); + + cy.save(); + + cy.visit("/note"); + + cy.call("logout"); + + cy.visit("/note"); + cy.get_open_dialog() + .get(".modal-message") + .contains("You are not permitted to access this page without login."); + }); + + it("Show List", () => { + cy.login("Administrator"); + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get(".section-head").contains("List Settings").click(); + cy.get('input[data-fieldname="show_list"]').check(); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-table").should("be.visible"); + }); + + it("Show Custom List Title", () => { + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.fill_field("list_title", "Note List"); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-header h1").should("contain.text", "Note List"); + }); + + it("Show Custom List Columns", () => { + cy.visit("/note"); + cy.url().should("include", "/note/list"); + + cy.get(".web-list-table thead th").contains("Sr."); + cy.get(".web-list-table thead th").contains("Title"); + + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + + cy.get('[data-fieldname="list_columns"] .grid-footer button') + .contains("Add Row") + .as("add-row"); + + cy.get("@add-row").click(); + cy.get('[data-fieldname="list_columns"] .grid-body .rows').as("grid-rows"); + cy.get("@grid-rows").find('.grid-row:first [data-fieldname="fieldname"]').click(); + cy.get("@grid-rows") + .find('.grid-row:first select[data-fieldname="fieldname"]') + .select("Title"); + + cy.get("@add-row").click(); + cy.get("@grid-rows").find('.grid-row[data-idx="2"] [data-fieldname="fieldname"]').click(); + cy.get("@grid-rows") + .find('.grid-row[data-idx="2"] select[data-fieldname="fieldname"]') + .select("Public"); + + cy.get("@add-row").click(); + cy.get("@grid-rows").find('.grid-row:last [data-fieldname="fieldname"]').click(); + cy.get("@grid-rows") + .find('.grid-row:last select[data-fieldname="fieldname"]') + .select("Content"); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-table thead th").contains("Sr."); + cy.get(".web-list-table thead th").contains("Title"); + cy.get(".web-list-table thead th").contains("Public"); + cy.get(".web-list-table thead th").contains("Content"); + }); + + it("Breadcrumbs", () => { + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-table tbody tr:last").click(); + + cy.get(".breadcrumb-container .breadcrumb .breadcrumb-item:first a") + .should("contain.text", "Note") + .click(); + cy.url().should("include", "/note/list"); + }); + + it("Custom Breadcrumbs", () => { + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Customization" }).click(); + cy.fill_field("breadcrumbs", '[{"label": _("Notes"), "route":"note"}]', "Code"); + cy.wait(2000); + cy.get(".form-tabs .nav-item .nav-link").contains("Customization").click(); + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-table tbody tr:last").click(); + cy.get(".breadcrumb-container .breadcrumb .breadcrumb-item:first a").should( + "contain.text", + "Notes" + ); + }); + + it("Read Only", () => { + cy.login("Administrator"); + cy.visit("/note"); + cy.url().should("include", "/note/list"); + + // Read Only Field + cy.get(".web-list-table tbody tr:last").click(); + cy.get('.xhiveframework-control[data-fieldname="title"] .control-input').should( + "have.css", + "display", + "none" + ); + }); + + it("Edit Mode", () => { + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get('input[data-fieldname="allow_edit"]').check(); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + cy.get(".web-list-table tbody tr:last").click(); + + cy.get(".web-form-actions a").contains("Edit Response").click(); + cy.url().should("include", "/edit"); + + // Editable Field + cy.get_field("title").should("have.value", "Note 1"); + + cy.fill_field("title", " Edited"); + cy.get(".web-form-actions button").contains("Save").click(); + cy.get(".success-page .edit-button").click(); + cy.get_field("title").should("have.value", "Note 1 Edited"); + }); + + it("Allow Multiple Response", () => { + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get('input[data-fieldname="allow_multiple"]').check(); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + + cy.get(".web-list-actions a:visible").contains("New").click(); + cy.url().should("include", "/note/new"); + + cy.fill_field("title", "Note 2"); + cy.get(".web-form-actions button").contains("Save").click(); + }); + + it("Allow Delete", () => { + cy.visit("/app/web-form/note"); + + cy.findByRole("tab", { name: "Settings" }).click(); + cy.get('input[data-fieldname="allow_delete"]').check(); + + cy.save(); + + cy.visit("/note"); + cy.url().should("include", "/note/list"); + + cy.get(".web-list-table tbody tr:nth-child(1) .list-col-checkbox input").click(); + cy.get(".web-list-table tbody tr:nth-child(2) .list-col-checkbox input").click(); + cy.get(".web-list-actions button:visible").contains("Delete").click({ force: true }); + + cy.get(".web-list-actions button").contains("Delete").should("not.be.visible"); + + cy.visit("/note"); + cy.get(".web-list-table tbody tr:nth-child(1)").should("not.exist"); + }); + + it("Navigate and Submit a WebForm", () => { + cy.visit("/update-profile"); + + cy.get(".web-form-actions a").contains("Edit Response").click(); + + cy.fill_field("middle_name", "_Test User"); + + cy.get(".web-form-actions .btn-primary").click(); + cy.url().should("include", "/me"); + }); + + it("Navigate and Submit a MultiStep WebForm", () => { + cy.call("xhiveframework.tests.ui_test_helpers.update_webform_to_multistep").then(() => { + cy.visit("/update-profile-duplicate"); + + cy.get(".web-form-actions a").contains("Edit Response").click(); + + cy.fill_field("middle_name", "_Test User"); + + cy.get(".btn-next").should("be.visible"); + cy.get(".btn-next").click(); + + cy.get(".btn-previous").should("be.visible"); + cy.get(".btn-next").should("not.be.visible"); + + cy.get(".web-form-actions .btn-primary").click(); + cy.url().should("include", "/me"); + }); + }); +}); diff --git a/cypress/integration/workspace.js b/cypress/integration/workspace.js new file mode 100644 index 0000000..4174747 --- /dev/null +++ b/cypress/integration/workspace.js @@ -0,0 +1,254 @@ +context("Workspace 2.0", () => { + before(() => { + cy.visit("/login"); + cy.login(); + }); + + it("Navigate to page from sidebar", () => { + cy.visit("/app/build"); + cy.get(".codex-editor__redactor .ce-block"); + cy.get('.sidebar-item-container[item-name="Website"]').first().click(); + cy.location("pathname").should("eq", "/app/website"); + }); + + it("Create Private Page", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.new_page", + }).as("new_page"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get('.custom-actions button[data-label="Create%20Workspace"]').click(); + cy.fill_field("title", "Test Private Page", "Data"); + cy.get_open_dialog().find(".modal-header").click(); + cy.get_open_dialog().find(".btn-primary").click(); + + // check if sidebar item is added in pubic section + cy.get('.sidebar-item-container[item-name="Test Private Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + cy.wait(300); + cy.get('.sidebar-item-container[item-name="Test Private Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.wait("@new_page"); + }); + + it("Create Child Page", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.new_page", + }).as("new_page"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get('.custom-actions button[data-label="Create%20Workspace"]').click(); + cy.fill_field("title", "Test Child Page", "Data"); + cy.fill_field("parent", "Test Private Page", "Select"); + cy.get_open_dialog().find(".modal-header").click(); + cy.get_open_dialog().find(".btn-primary").click(); + + // check if sidebar item is added in pubic section + cy.get('.sidebar-item-container[item-name="Test Child Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + cy.wait(300); + cy.get('.sidebar-item-container[item-name="Test Child Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.wait("@new_page"); + }); + + it("Duplicate Page", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.duplicate_page", + }).as("page_duplicated"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + cy.get('.sidebar-item-container[item-name="Test Private Page"]').as("sidebar-item"); + + cy.get("@sidebar-item").find(".standard-sidebar-item").first().click(); + cy.get("@sidebar-item").find(".dropdown-btn").first().click(); + cy.get("@sidebar-item") + .find(".dropdown-list .dropdown-item") + .contains("Duplicate") + .first() + .click({ force: true }); + + cy.get_open_dialog().fill_field("title", "Duplicate Page", "Data"); + cy.click_modal_primary_button("Duplicate"); + + cy.wait("@page_duplicated"); + }); + + it("Drag Sidebar Item", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.sort_pages", + }).as("page_sorted"); + + cy.get('.sidebar-item-container[item-name="Duplicate Page"]').as("sidebar-item"); + + cy.get("@sidebar-item").find(".standard-sidebar-item").first().click(); + cy.get("@sidebar-item").find(".drag-handle").first().move({ deltaX: 0, deltaY: 100 }); + + cy.get('.sidebar-item-container[item-name="Build"]').as("sidebar-item"); + + cy.get("@sidebar-item").find(".standard-sidebar-item").first().click(); + cy.get("@sidebar-item").find(".drag-handle").first().move({ deltaX: 0, deltaY: 100 }); + + cy.wait("@page_sorted"); + }); + + it("Edit Page Detail", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.update_page", + }).as("page_updated"); + + cy.get('.sidebar-item-container[item-name="Test Private Page"]').as("sidebar-item"); + + cy.get("@sidebar-item").find(".standard-sidebar-item").first().click(); + cy.get("@sidebar-item").find(".dropdown-btn").first().click(); + cy.get("@sidebar-item") + .find(".dropdown-list .dropdown-item") + .contains("Edit") + .first() + .click({ force: true }); + + cy.get_open_dialog().fill_field("title", " 1", "Data"); + cy.get_open_dialog().find('input[data-fieldname="is_public"]').check(); + cy.click_modal_primary_button("Update"); + + cy.get( + '.standard-sidebar-section:first .sidebar-item-container[item-name="Test Private Page"]' + ).should("not.exist"); + cy.get( + '.standard-sidebar-section:last .sidebar-item-container[item-name="Test Private Page 1"]' + ).should("exist"); + + cy.wait("@page_updated"); + }); + + it("Add New Block", () => { + cy.get('.sidebar-item-container[item-name="Duplicate Page"]').as("sidebar-item"); + + cy.get("@sidebar-item").find(".standard-sidebar-item").first().click(); + + cy.get(".ce-block").click().type("{enter}"); + cy.get(".block-list-container .block-list-item").contains("Heading").click(); + cy.get(":focus").type("Header"); + cy.get(".ce-block:last").find(".ce-header").should("exist"); + + cy.get(".ce-block:last").click().type("{enter}"); + cy.get(".block-list-container .block-list-item").contains("Text").click(); + cy.get(":focus").type("Paragraph text"); + cy.get(".ce-block:last").find(".ce-paragraph").should("exist"); + }); + + it("Delete A Block", () => { + cy.get(":focus").click(); + cy.get(".paragraph-control .setting-btn").click(); + cy.get(".paragraph-control .dropdown-item").contains("Delete").click(); + cy.get(".ce-block:last").find(".ce-paragraph").should("not.exist"); + }); + + it("Shrink and Expand A Block", () => { + cy.get(":focus").click(); + cy.get(".ce-block:last .setting-btn").click(); + cy.get(".ce-block:last .dropdown-item").contains("Shrink").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-11"); + cy.get(".ce-block:last .dropdown-item").contains("Shrink").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-10"); + cy.get(".ce-block:last .dropdown-item").contains("Shrink").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-9"); + cy.get(".ce-block:last .dropdown-item").contains("Expand").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-10"); + cy.get(".ce-block:last .dropdown-item").contains("Expand").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-11"); + cy.get(".ce-block:last .dropdown-item").contains("Expand").click(); + cy.get(".ce-block:last").should("have.class", "col-xs-12"); + + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + }); + + it("Hide/Unhide Workspaces", () => { + // hide + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.hide_page", + }).as("hide_page"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + cy.get('.sidebar-item-container[item-name="Duplicate Page"]') + .find(".sidebar-item-control .setting-btn") + .click(); + cy.get('.sidebar-item-container[item-name="Duplicate Page"]') + .find('.dropdown-item[title="Hide Workspace"]') + .click({ force: true }); + cy.wait(300); + cy.get('.standard-actions .btn-secondary[data-label="Discard"]').click(); + cy.get('.sidebar-item-container[item-name="Duplicate Page"]').should("not.be.visible"); + + cy.wait("@hide_page"); + + // unhide + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.unhide_page", + }).as("unhide_page"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + cy.get('.sidebar-item-container[item-name="Duplicate Page"]') + .find('[title="Unhide Workspace"]') + .click({ force: true }); + cy.wait(300); + + cy.get('.standard-actions .btn-secondary[data-label="Discard"]').click(); + cy.get('.sidebar-item-container[item-name="Duplicate Page"]').should("be.visible"); + + cy.wait("@unhide_page"); + }); + + it("Delete Duplicate Page", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.delete_page", + }).as("page_deleted"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + cy.get('.sidebar-item-container[item-name="Duplicate Page"]') + .find(".sidebar-item-control .setting-btn") + .click(); + cy.get('.sidebar-item-container[item-name="Duplicate Page"]') + .find('.dropdown-item[title="Delete Workspace"]') + .click({ force: true }); + cy.wait(300); + cy.get(".modal-footer > .standard-actions > .btn-modal-primary:visible").first().click(); + cy.get('.sidebar-item-container[item-name="Duplicate Page"]').should("not.exist"); + + cy.wait("@page_deleted"); + }); +}); diff --git a/cypress/integration/workspace_blocks.js b/cypress/integration/workspace_blocks.js new file mode 100644 index 0000000..57ca332 --- /dev/null +++ b/cypress/integration/workspace_blocks.js @@ -0,0 +1,185 @@ +context("Workspace Blocks", () => { + before(() => { + cy.login(); + cy.visit("/app"); + return cy + .window() + .its("xhiveframework") + .then((xhiveframework) => { + return xhiveframework.xcall("xhiveframework.tests.ui_test_helpers.setup_workflow"); + }); + }); + + it("Create Test Page", () => { + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.doctype.workspace.workspace.new_page", + }).as("new_page"); + + cy.visit("/app/website"); + cy.get(".codex-editor__redactor .ce-block"); + cy.get('.custom-actions button[data-label="Create%20Workspace"]').click(); + cy.fill_field("title", "Test Block Page", "Data"); + cy.get_open_dialog().find(".modal-header").click(); + cy.get_open_dialog().find(".btn-primary").click(); + + // check if sidebar item is added in private section + cy.get('.sidebar-item-container[item-name="Test Block Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + cy.wait(300); + cy.get('.sidebar-item-container[item-name="Test Block Page"]').should( + "have.attr", + "item-public", + "0" + ); + + cy.wait("@new_page"); + }); + + it.skip("Quick List Block", () => { + cy.create_records([ + { + doctype: "ToDo", + description: "Quick List ToDo 1", + status: "Open", + }, + { + doctype: "ToDo", + description: "Quick List ToDo 2", + status: "Open", + }, + { + doctype: "ToDo", + description: "Quick List ToDo 3", + status: "Open", + }, + { + doctype: "ToDo", + description: "Quick List ToDo 4", + status: "Open", + }, + ]); + + cy.intercept({ + method: "GET", + url: "api/method/xhiveframework.desk.form.load.getdoctype?**", + }).as("get_doctype"); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + // test quick list creation + cy.get(".ce-block").first().click({ force: true }).type("{enter}"); + cy.get(".block-list-container .block-list-item").contains("Quick List").click(); + + cy.fill_field("label", "ToDo", "Data"); + cy.fill_field("document_type", "ToDo", "Link").blur(); + cy.wait("@get_doctype"); + + cy.get_open_dialog().find(".filter-edit-area").should("contain", "No filters selected"); + cy.get_open_dialog().find(".filter-area .add-filter").click(); + + cy.get_open_dialog() + .find(".fieldname-select-area input") + .type("Workflow State{enter}") + .blur(); + cy.get_open_dialog().find(".filter-field .input-with-feedback").type("Pending"); + + cy.get_open_dialog().find(".modal-header").click(); + cy.get_open_dialog().find(".btn-primary").click(); + + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + + cy.get(".codex-editor__redactor .ce-block"); + + cy.get(".ce-block .quick-list-widget-box").first().as("todo-quick-list"); + + cy.get("@todo-quick-list").find(".quick-list-item .status").should("contain", "Pending"); + + // test quick-list-item + cy.get("@todo-quick-list") + .find(".quick-list-item .title") + .first() + .invoke("attr", "title") + .then((title) => { + cy.get("@todo-quick-list").find(".quick-list-item").contains(title).click(); + cy.get_field("description", "Text Editor").should("contain", title); + cy.click_action_button("Approve"); + }); + cy.go("back"); + + // test filter-list + cy.get("@todo-quick-list").realHover().find(".widget-control .filter-list").click(); + + cy.get_open_dialog() + .find(".filter-field .input-with-feedback") + .focus() + .type("{selectall}Approved"); + cy.get_open_dialog().find(".modal-header").click(); + cy.get_open_dialog().find(".btn-primary").click(); + + cy.get("@todo-quick-list").find(".quick-list-item .status").should("contain", "Approved"); + + // test refresh-list + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.desk.reportview.get", + }).as("refresh-list"); + + cy.get("@todo-quick-list").realHover().find(".widget-control .refresh-list").click(); + cy.wait("@refresh-list"); + + // test add-new + cy.get("@todo-quick-list").realHover().find(".widget-control .add-new").click(); + cy.url().should("include", `/todo/new-todo-1`); + cy.go("back"); + + // test see-all + cy.get("@todo-quick-list").find(".widget-footer .see-all").click(); + cy.open_list_filter(); + cy.get('.filter-field input[data-fieldname="workflow_state"]') + .invoke("val") + .should("eq", "Pending"); + cy.go("back"); + }); + + it("Number Card Block", () => { + cy.create_records([ + { + doctype: "Number Card", + label: "Test Number Card", + document_type: "ToDo", + color: "#f74343", + }, + ]); + + cy.get(".codex-editor__redactor .ce-block"); + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + + cy.get(".ce-block").first().click({ force: true }).type("{enter}"); + cy.get(".block-list-container .block-list-item").contains("Number Card").click(); + + // add number card + cy.fill_field("number_card_name", "Test Number Card", "Link"); + cy.get('[data-fieldname="number_card_name"] ul li').contains("Test Number Card").click(); + cy.click_modal_primary_button("Add"); + cy.get(".ce-block .number-widget-box").first().as("number_card"); + cy.get("@number_card").find(".widget-title").should("contain", "Test Number Card"); + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + cy.get("@number_card").find(".widget-title").should("contain", "Test Number Card"); + + // edit number card + cy.get(".standard-actions .btn-secondary[data-label=Edit]").click(); + cy.get("@number_card").realHover().find(".widget-control .edit-button").click(); + cy.get_field("label", "Data").invoke("val", "ToDo Count"); + cy.click_modal_primary_button("Save"); + cy.get("@number_card").find(".widget-title").should("contain", "ToDo Count"); + cy.get('.standard-actions .btn-primary[data-label="Save"]').click(); + cy.get("@number_card").find(".widget-title").should("contain", "ToDo Count"); + }); +}); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js new file mode 100644 index 0000000..b132753 --- /dev/null +++ b/cypress/plugins/index.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example plugins/index.js can be used to load plugins +// +// You can change the location of this file or turn off loading +// the plugins file with the 'pluginsFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/plugins-guide +// *********************************************************** + +// This function is called when a project is opened or re-opened (e.g. due to +// the project's config changing) + +module.exports = (on, config) => { + require("@cypress/code-coverage/task")(on, config); + return config; +}; diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000..cd23b6b --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,550 @@ +import "@testing-library/cypress/add-commands"; +import "@4tw/cypress-drag-drop"; +import "cypress-real-events/support"; +// *********************************************** +// This example commands.js shows you how to +// create various custom commands and overwrite +// existing commands. +// +// For more comprehensive examples of custom +// commands please read more here: +// https://on.cypress.io/custom-commands +// *********************************************** +// +// +// -- This is a parent command -- +// Cypress.Commands.add("login", (email, password) => { ... }); +// +// +// -- This is a child command -- +// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }); +// +// +// -- This is a dual command -- +// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }); +// +// +// -- This is will overwrite an existing command -- +// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }); + +Cypress.Commands.add("login", (email, password) => { + if (!email) { + email = Cypress.config("testUser") || "Administrator"; + } + if (!password) { + password = Cypress.env("adminPassword"); + } + // cy.session clears all localStorage on new login, so we need to retain the last route + const session_last_route = window.localStorage.getItem("session_last_route"); + return cy + .session( + [email, password] || "", + () => { + return cy.request({ + url: "/api/method/login", + method: "POST", + body: { + usr: email, + pwd: password, + }, + }); + }, + { + cacheAcrossSpecs: true, + } + ) + .then(() => { + if (session_last_route) { + window.localStorage.setItem("session_last_route", session_last_route); + } + }); +}); + +Cypress.Commands.add("call", (method, args) => { + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + url: `/api/method/${method}`, + method: "POST", + body: args, + headers: { + Accept: "application/json", + "Content-Type": "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + }) + .then((res) => { + expect(res.status).eq(200); + if (method === "logout") { + Cypress.session.clearAllSavedSessions(); + } + return res.body; + }); + }); +}); + +Cypress.Commands.add("get_list", (doctype, fields = [], filters = []) => { + filters = JSON.stringify(filters); + fields = JSON.stringify(fields); + let url = `/api/resource/${doctype}?fields=${fields}&filters=${filters}`; + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + method: "GET", + url, + headers: { + Accept: "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + }) + .then((res) => { + expect(res.status).eq(200); + return res.body; + }); + }); +}); + +Cypress.Commands.add("get_doc", (doctype, name) => { + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + method: "GET", + url: `/api/resource/${doctype}/${name}`, + headers: { + Accept: "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + }) + .then((res) => { + expect(res.status).eq(200); + return res.body; + }); + }); +}); + +Cypress.Commands.add("remove_doc", (doctype, name) => { + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + method: "DELETE", + url: `/api/resource/${doctype}/${name}`, + headers: { + Accept: "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + }) + .then((res) => { + expect(res.status).eq(202); + return res.body; + }); + }); +}); + +Cypress.Commands.add("create_records", (doc) => { + return cy + .call("xhiveframework.tests.ui_test_helpers.create_if_not_exists", { doc: JSON.stringify(doc) }) + .then((r) => r.message); +}); + +Cypress.Commands.add("set_value", (doctype, name, obj) => { + return cy.call("xhiveframework.client.set_value", { + doctype, + name, + fieldname: obj, + }); +}); + +Cypress.Commands.add("fill_field", (fieldname, value, fieldtype = "Data") => { + cy.get_field(fieldname, fieldtype).as("input"); + + if (["Date", "Time", "Datetime"].includes(fieldtype)) { + cy.get("@input").click().wait(200); + cy.get(".datepickers-container .datepicker.active").should("exist"); + } + if (fieldtype === "Time") { + cy.get("@input").clear().wait(200); + } + + if (fieldtype === "Select") { + cy.get("@input").select(value); + } else { + cy.get("@input").type(value, { + waitForAnimations: false, + parseSpecialCharSequences: false, + force: true, + delay: 100, + }); + } + return cy.get("@input"); +}); + +Cypress.Commands.add("get_field", (fieldname, fieldtype = "Data") => { + let field_element = fieldtype === "Select" ? "select" : "input"; + let selector = `[data-fieldname="${fieldname}"] ${field_element}:visible`; + + if (fieldtype === "Text Editor") { + selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]:visible`; + } + if (fieldtype === "Code") { + selector = `[data-fieldname="${fieldname}"] .ace_text-input`; + } + if (fieldtype === "Markdown Editor") { + selector = `[data-fieldname="${fieldname}"] .ace-editor-target`; + } + + return cy.get(selector).first(); +}); + +Cypress.Commands.add( + "fill_table_field", + (tablefieldname, row_idx, fieldname, value, fieldtype = "Data") => { + cy.get_table_field(tablefieldname, row_idx, fieldname, fieldtype).as("input"); + + if (["Date", "Time", "Datetime"].includes(fieldtype)) { + cy.get("@input").click().wait(200); + cy.get(".datepickers-container .datepicker.active").should("exist"); + } + if (fieldtype === "Time") { + cy.get("@input").clear().wait(200); + } + + if (fieldtype === "Select") { + cy.get("@input").select(value); + } else { + cy.get("@input").type(value, { waitForAnimations: false, force: true }); + } + return cy.get("@input"); + } +); + +Cypress.Commands.add( + "get_table_field", + (tablefieldname, row_idx, fieldname, fieldtype = "Data") => { + let selector = `.xhiveframework-control[data-fieldname="${tablefieldname}"]`; + selector += ` [data-idx="${row_idx}"]`; + + if (fieldtype === "Text Editor") { + selector += ` [data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`; + } else if (fieldtype === "Code") { + selector += ` [data-fieldname="${fieldname}"] .ace_text-input`; + } else { + selector += ` [data-fieldname="${fieldname}"]`; + return cy.get(selector).find(".form-control:visible, .static-area:visible").first(); + } + return cy.get(selector); + } +); + +Cypress.Commands.add("awesomebar", (text) => { + cy.get("#navbar-search").type(`${text}{downarrow}{enter}`, { delay: 700 }); +}); + +Cypress.Commands.add("new_form", (doctype) => { + let dt_in_route = doctype.toLowerCase().replace(/ /g, "-"); + cy.visit(`/app/${dt_in_route}/new`); + cy.get("body").should(($body) => { + const dataRoute = $body.attr("data-route"); + expect(dataRoute).to.match(new RegExp(`^Form/${doctype}/new-${dt_in_route}-`)); + }); + cy.get("body").should("have.attr", "data-ajax-state", "complete"); +}); + +Cypress.Commands.add("select_form_tab", (label) => { + cy.get(".form-tabs-list [data-toggle='tab']").contains(label).click().wait(500); +}); + +Cypress.Commands.add("go_to_list", (doctype) => { + let dt_in_route = doctype.toLowerCase().replace(/ /g, "-"); + cy.visit(`/app/${dt_in_route}`); +}); + +Cypress.Commands.add("clear_cache", () => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + xhiveframework.ui.toolbar.clear_cache(); + }); +}); + +Cypress.Commands.add("dialog", (opts) => { + return cy + .window({ log: false }) + .its("xhiveframework", { log: false }) + .then((xhiveframework) => { + Cypress.log({ + name: "dialog", + displayName: "dialog", + message: "xhiveframework.ui.Dialog", + consoleProps: () => { + return { + options: opts, + dialog: d, + }; + }, + }); + + var d = new xhiveframework.ui.Dialog(opts); + d.show(); + return d; + }); +}); + +Cypress.Commands.add("get_open_dialog", () => { + return cy.get(".modal:visible").last(); +}); + +Cypress.Commands.add("save", () => { + cy.intercept("/api/method/xhiveframework.desk.form.save.savedocs").as("save_call"); + cy.get(`.page-container:visible button[data-label="Save"]`).click({ force: true }); + cy.wait("@save_call"); +}); +Cypress.Commands.add("hide_dialog", () => { + cy.wait(500); + cy.get_open_dialog().focus().find(".btn-modal-close").click(); + cy.get(".modal:visible").should("not.exist"); +}); + +Cypress.Commands.add("clear_dialogs", () => { + cy.window().then((win) => { + win.$(".modal, .modal-backdrop").remove(); + }); + cy.get(".modal").should("not.exist"); +}); + +Cypress.Commands.add("clear_datepickers", () => { + cy.window().then((win) => { + win.$(".datepicker").remove(); + }); + cy.get(".datepicker").should("not.exist"); +}); + +Cypress.Commands.add("insert_doc", (doctype, args, ignore_duplicate) => { + if (!args.doctype) { + args.doctype = doctype; + } + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + method: "POST", + url: `/api/resource/${doctype}`, + body: args, + headers: { + Accept: "application/json", + "Content-Type": "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + failOnStatusCode: !ignore_duplicate, + }) + .then((res) => { + let status_codes = [200]; + if (ignore_duplicate) { + status_codes.push(409); + } + + let message = null; + if (ignore_duplicate && !status_codes.includes(res.status)) { + message = `Document insert failed, response: ${JSON.stringify( + res, + null, + "\t" + )}`; + } + expect(res.status).to.be.oneOf(status_codes, message); + return res.body.data; + }); + }); +}); + +Cypress.Commands.add("update_doc", (doctype, docname, args) => { + return cy + .window() + .its("xhiveframework.csrf_token") + .then((csrf_token) => { + return cy + .request({ + method: "PUT", + url: `/api/resource/${doctype}/${docname}`, + body: args, + headers: { + Accept: "application/json", + "Content-Type": "application/json", + "X-XhiveFramework-CSRF-Token": csrf_token, + }, + }) + .then((res) => { + expect(res.status).to.eq(200); + return res.body.data; + }); + }); +}); + +Cypress.Commands.add("switch_to_user", (user) => { + cy.call("logout"); + cy.wait(200); + cy.login(user); + cy.reload(); +}); + +Cypress.Commands.add("add_role", (user, role) => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + const session_user = xhiveframework.session.user; + add_remove_role("add", user, role, session_user); + }); +}); + +Cypress.Commands.add("remove_role", (user, role) => { + cy.window() + .its("xhiveframework") + .then((xhiveframework) => { + const session_user = xhiveframework.session.user; + add_remove_role("remove", user, role, session_user); + }); +}); + +const add_remove_role = (action, user, role, session_user) => { + if (session_user !== "Administrator") { + cy.switch_to_user("Administrator"); + } + + cy.call("xhiveframework.tests.ui_test_helpers.add_remove_role", { + action: action, + user: user, + role: role, + }); + + if (session_user !== "Administrator") { + cy.switch_to_user(session_user); + } +}; + +Cypress.Commands.add("open_list_filter", () => { + cy.get(".filter-section .filter-button").click(); + cy.wait(300); + cy.get(".filter-popover").should("exist"); +}); + +Cypress.Commands.add("click_custom_action_button", (name) => { + cy.get(`.custom-actions [data-label="${encodeURIComponent(name)}"]`).click(); +}); + +Cypress.Commands.add("click_action_button", (name) => { + cy.findByRole("button", { name: "Actions" }).click(); + cy.get(`.actions-btn-group [data-label="${encodeURIComponent(name)}"]`).click(); +}); + +Cypress.Commands.add("click_menu_button", (name) => { + cy.get(".standard-actions .menu-btn-group > .btn").click(); + cy.get(`.menu-btn-group [data-label="${encodeURIComponent(name)}"]`).click(); +}); + +Cypress.Commands.add("clear_filters", () => { + let has_filter = false; + cy.intercept({ + method: "POST", + url: "api/method/xhiveframework.model.utils.user_settings.save", + }).as("filter-saved"); + cy.get(".filter-section .filter-button").click({ force: true }); + cy.wait(300); + cy.get(".filter-popover").should("exist"); + cy.get(".filter-popover").then((popover) => { + if (popover.find("input.input-with-feedback")[0].value != "") { + has_filter = true; + } + }); + cy.get(".filter-popover").find(".clear-filters").click(); + cy.get(".filter-section .filter-button").click(); + cy.window() + .its("cur_list") + .then((cur_list) => { + cur_list && cur_list.filter_area && cur_list.filter_area.clear(); + has_filter && cy.wait("@filter-saved"); + }); +}); + +Cypress.Commands.add("click_modal_primary_button", (btn_name) => { + cy.wait(400); + cy.get(".modal-footer > .standard-actions > .btn-primary") + .contains(btn_name) + .click({ force: true }); +}); + +Cypress.Commands.add("click_sidebar_button", (btn_name) => { + cy.get(".list-group-by-fields .list-link > a").contains(btn_name).click({ force: true }); +}); + +Cypress.Commands.add("click_listview_row_item", (row_no) => { + cy.get(".list-row > .level-left > .list-subject > .level-item > .ellipsis") + .eq(row_no) + .click({ force: true }); +}); + +Cypress.Commands.add("click_listview_row_item_with_text", (text) => { + cy.get(".list-row > .level-left > .list-subject > .level-item > .ellipsis") + .contains(text) + .first() + .click({ force: true }); +}); + +Cypress.Commands.add("click_filter_button", () => { + cy.get(".filter-button").click(); +}); + +Cypress.Commands.add("click_listview_primary_button", (btn_name) => { + cy.get(".primary-action").contains(btn_name).click({ force: true }); +}); + +Cypress.Commands.add("click_doc_primary_button", (btn_name) => { + cy.get(".primary-action").contains(btn_name).click({ force: true }); +}); + +Cypress.Commands.add("click_timeline_action_btn", (btn_name) => { + cy.get(".timeline-message-box .actions .action-btn").contains(btn_name).click(); +}); + +Cypress.Commands.add("select_listview_row_checkbox", (row_no) => { + cy.get(".xhiveframework-list .select-like > .list-row-checkbox").eq(row_no).click(); +}); + +Cypress.Commands.add("click_form_section", (section_name) => { + cy.get(".section-head").contains(section_name).click(); +}); + +const compare_document = (expected, actual) => { + for (const prop in expected) { + if (expected[prop] instanceof Array) { + // recursively compare child documents. + expected[prop].forEach((item, idx) => { + compare_document(item, actual[prop][idx]); + }); + } else { + assert.equal(expected[prop], actual[prop], `${prop} should be equal.`); + } + } +}; + +Cypress.Commands.add("compare_document", (expected_document) => { + cy.window() + .its("cur_frm") + .then((frm) => { + // Don't remove this, cypress can't magically wait for events it has no control over. + cy.wait(1000); + compare_document(expected_document, frm.doc); + }); +}); diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js new file mode 100644 index 0000000..10fb377 --- /dev/null +++ b/cypress/support/e2e.js @@ -0,0 +1,25 @@ +// *********************************************************** +// This example support/index.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import "./commands"; +import "@cypress/code-coverage/support"; + +Cypress.on("uncaught:exception", (err, runnable) => { + return false; +}); + +// Alternatively you can use CommonJS syntax: +// require('./commands') diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json new file mode 100644 index 0000000..d90ebf6 --- /dev/null +++ b/cypress/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "allowJs": true, + "baseUrl": "../node_modules", + "types": [ + "cypress" + ] + }, + "include": [ + "**/*.*" + ] +} \ No newline at end of file diff --git a/esbuild/build-cleanup.js b/esbuild/build-cleanup.js new file mode 100644 index 0000000..77b3c0c --- /dev/null +++ b/esbuild/build-cleanup.js @@ -0,0 +1,30 @@ +const path = require("path"); +const fs = require("fs"); +const glob = require("fast-glob"); + +module.exports = { + name: "build_cleanup", + setup(build) { + build.onEnd((result) => { + if (result.errors.length) return; + clean_dist_files(Object.keys(result.metafile.outputs)); + }); + }, +}; + +function clean_dist_files(new_files) { + new_files.forEach((file) => { + if (file.endsWith(".map")) return; + + const pattern = file.split(".").slice(0, -2).join(".") + "*"; + glob.sync(pattern).forEach((file_to_delete) => { + if (file_to_delete.startsWith(file)) return; + + fs.unlink(path.resolve(file_to_delete), (err) => { + if (!err) return; + + console.error(`Error deleting ${file.split(path.sep).pop()}`); + }); + }); + }); +} diff --git a/esbuild/esbuild.js b/esbuild/esbuild.js new file mode 100644 index 0000000..2e39846 --- /dev/null +++ b/esbuild/esbuild.js @@ -0,0 +1,525 @@ +const path = require("path"); +const fs = require("fs"); +const glob = require("fast-glob"); +const esbuild = require("esbuild"); +const vue = require("esbuild-plugin-vue3"); +const yargs = require("yargs"); +const cliui = require("cliui")(); +const chalk = require("chalk"); +const html_plugin = require("./xhiveframework-html"); +const vue_style_plugin = require("./xhiveframework-vue-style"); +const rtlcss = require("rtlcss"); +const postCssPlugin = require("@xhiveframework/esbuild-plugin-postcss2").default; +const ignore_assets = require("./ignore-assets"); +const sass_options = require("./sass_options"); +const build_cleanup_plugin = require("./build-cleanup"); + +const { + app_list, + assets_path, + apps_path, + sites_path, + get_app_path, + get_public_path, + log, + log_warn, + log_error, + bench_path, + get_redis_subscriber, +} = require("./utils"); + +const argv = yargs + .usage("Usage: node esbuild [options]") + .option("apps", { + type: "string", + description: "Run build for specific apps", + }) + .option("skip_xhiveframework", { + type: "boolean", + description: "Skip building xhiveframework assets", + }) + .option("files", { + type: "string", + description: "Run build for specified bundles", + }) + .option("watch", { + type: "boolean", + description: "Run in watch mode and rebuild on file changes", + }) + .option("live-reload", { + type: "boolean", + description: `Automatically reload Desk when assets are rebuilt. + Can only be used with the --watch flag.`, + }) + .option("production", { + type: "boolean", + description: "Run build in production mode", + }) + .option("run-build-command", { + type: "boolean", + description: "Run build command for apps", + }) + .option("save-metafiles", { + type: "boolean", + description: + "Saves esbuild metafiles for built assets. Useful for analyzing bundle size. More info: https://esbuild.github.io/api/#metafile", + }) + .example("node esbuild --apps xhiveframework,xhiveerp", "Run build only for xhiveframework and xhiveerp") + .example( + "node esbuild --files xhiveframework/website.bundle.js,xhiveframework/desk.bundle.js", + "Run build only for specified bundles" + ) + .version(false).argv; + +const APPS = (!argv.apps ? app_list : argv.apps.split(",")).filter( + (app) => !(argv.skip_xhiveframework && app == "xhiveframework") +); +const FILES_TO_BUILD = argv.files ? argv.files.split(",") : []; +const WATCH_MODE = Boolean(argv.watch); +const PRODUCTION = Boolean(argv.production); +const RUN_BUILD_COMMAND = !WATCH_MODE && Boolean(argv["run-build-command"]); + +const TOTAL_BUILD_TIME = `${chalk.black.bgGreen(" DONE ")} Total Build Time`; +const NODE_PATHS = [].concat( + // node_modules of apps directly importable + app_list + .map((app) => path.resolve(get_app_path(app), "../node_modules")) + .filter(fs.existsSync), + // import js file of any app if you provide the full path + app_list.map((app) => path.resolve(get_app_path(app), "..")).filter(fs.existsSync) +); + +execute().catch((e) => { + console.error(e); + process.exit(1); +}); + +if (WATCH_MODE) { + // listen for open files in editor event + open_in_editor(); +} + +async function execute() { + console.time(TOTAL_BUILD_TIME); + + let results; + try { + results = await build_assets_for_apps(APPS, FILES_TO_BUILD); + } catch (e) { + log_error("There were some problems during build"); + log(); + log(chalk.dim(e.stack)); + if (process.env.CI || PRODUCTION) { + process.kill(process.pid); + } + return; + } + + if (!WATCH_MODE) { + log_built_assets(results); + console.timeEnd(TOTAL_BUILD_TIME); + log(); + } else { + log("Watching for changes..."); + } + for (const result of results) { + await write_assets_json(result.metafile); + } + RUN_BUILD_COMMAND && run_build_command_for_apps(APPS); + if (!WATCH_MODE) { + process.exit(0); + } +} + +function build_assets_for_apps(apps, files) { + let { include_patterns, ignore_patterns } = files.length + ? get_files_to_build(files) + : get_all_files_to_build(apps); + + return glob(include_patterns, { ignore: ignore_patterns }).then((files) => { + let output_path = assets_path; + + let file_map = {}; + let style_file_map = {}; + let rtl_style_file_map = {}; + for (let file of files) { + let relative_app_path = path.relative(apps_path, file); + let app = relative_app_path.split(path.sep)[0]; + + let extension = path.extname(file); + let output_name = path.basename(file, extension); + if ([".css", ".scss", ".less", ".sass", ".styl"].includes(extension)) { + output_name = path.join("css", output_name); + } else if ([".js", ".ts"].includes(extension)) { + output_name = path.join("js", output_name); + } + output_name = path.join(app, "dist", output_name); + + if ( + Object.keys(file_map).includes(output_name) || + Object.keys(style_file_map).includes(output_name) + ) { + log_warn(`Duplicate output file ${output_name} generated from ${file}`); + } + if ([".css", ".scss", ".less", ".sass", ".styl"].includes(extension)) { + style_file_map[output_name] = file; + rtl_style_file_map[output_name.replace("/css/", "/css-rtl/")] = file; + } else { + file_map[output_name] = file; + } + } + let build = build_files({ + files: file_map, + outdir: output_path, + }); + let style_build = build_style_files({ + files: style_file_map, + outdir: output_path, + }); + let rtl_style_build = build_style_files({ + files: rtl_style_file_map, + outdir: output_path, + rtl_style: true, + }); + return Promise.all([build, style_build, rtl_style_build]); + }); +} + +function get_all_files_to_build(apps) { + let include_patterns = []; + let ignore_patterns = []; + + for (let app of apps) { + let public_path = get_public_path(app); + include_patterns.push( + path.resolve(public_path, "**", "*.bundle.{js,ts,css,sass,scss,less,styl,jsx}") + ); + ignore_patterns.push( + path.resolve(public_path, "node_modules"), + path.resolve(public_path, "dist") + ); + } + + return { + include_patterns, + ignore_patterns, + }; +} + +function get_files_to_build(files) { + // files: ['xhiveframework/website.bundle.js', 'xhiveerp/main.bundle.js'] + let include_patterns = []; + let ignore_patterns = []; + + for (let file of files) { + let [app, bundle] = file.split("/"); + let public_path = get_public_path(app); + include_patterns.push(path.resolve(public_path, "**", bundle)); + ignore_patterns.push( + path.resolve(public_path, "node_modules"), + path.resolve(public_path, "dist") + ); + } + + return { + include_patterns, + ignore_patterns, + }; +} + +function build_files({ files, outdir }) { + let build_plugins = [vue(), html_plugin, build_cleanup_plugin, vue_style_plugin]; + return esbuild.build(get_build_options(files, outdir, build_plugins)); +} + +function build_style_files({ files, outdir, rtl_style = false }) { + let plugins = []; + if (rtl_style) { + plugins.push(rtlcss); + } + + let build_plugins = [ + ignore_assets, + build_cleanup_plugin, + postCssPlugin({ + plugins: plugins, + sassOptions: sass_options, + }), + ]; + + plugins.push(require("autoprefixer")); + return esbuild.build(get_build_options(files, outdir, build_plugins)); +} + +function get_build_options(files, outdir, plugins) { + return { + entryPoints: files, + entryNames: "[dir]/[name].[hash]", + target: ["es2017"], + outdir, + sourcemap: true, + bundle: true, + metafile: true, + minify: PRODUCTION, + nodePaths: NODE_PATHS, + define: { + "process.env.NODE_ENV": JSON.stringify(PRODUCTION ? "production" : "development"), + __VUE_OPTIONS_API__: JSON.stringify(true), + __VUE_PROD_DEVTOOLS__: JSON.stringify(false), + }, + plugins: plugins, + watch: get_watch_config(), + }; +} + +function get_watch_config() { + if (WATCH_MODE) { + return { + async onRebuild(error, result) { + if (error) { + log_error("There was an error during rebuilding changes."); + log(); + log(chalk.dim(error.stack)); + notify_redis({ error }); + } else { + let { new_assets_json, prev_assets_json } = await write_assets_json( + result.metafile + ); + + let changed_files; + if (prev_assets_json) { + changed_files = get_rebuilt_assets(prev_assets_json, new_assets_json); + + let timestamp = new Date().toLocaleTimeString(); + let message = `${timestamp}: Compiled ${changed_files.length} files...`; + log(chalk.yellow(message)); + for (let filepath of changed_files) { + let filename = path.basename(filepath); + log(" " + filename); + } + log(); + } + notify_redis({ success: true, changed_files }); + } + }, + }; + } + return null; +} + +function log_built_assets(results) { + let outputs = {}; + for (const result of results) { + outputs = Object.assign(outputs, result.metafile.outputs); + } + let column_widths = [60, 20]; + cliui.div( + { + text: chalk.cyan.bold("File"), + width: column_widths[0], + }, + { + text: chalk.cyan.bold("Size"), + width: column_widths[1], + } + ); + cliui.div(""); + + let output_by_dist_path = {}; + for (let outfile in outputs) { + if (outfile.endsWith(".map")) continue; + let data = outputs[outfile]; + outfile = path.resolve(outfile); + outfile = path.relative(assets_path, outfile); + let filename = path.basename(outfile); + let dist_path = outfile.replace(filename, ""); + output_by_dist_path[dist_path] = output_by_dist_path[dist_path] || []; + output_by_dist_path[dist_path].push({ + name: filename, + size: (data.bytes / 1000).toFixed(2) + " Kb", + }); + } + + for (let dist_path in output_by_dist_path) { + let files = output_by_dist_path[dist_path]; + cliui.div({ + text: dist_path, + width: column_widths[0], + }); + + for (let i in files) { + let file = files[i]; + let branch = ""; + if (i < files.length - 1) { + branch = "├─ "; + } else { + branch = "└─ "; + } + let color = file.name.endsWith(".js") ? "green" : "blue"; + cliui.div( + { + text: branch + chalk[color]("" + file.name), + width: column_widths[0], + }, + { + text: file.size, + width: column_widths[1], + } + ); + } + cliui.div(""); + } + log(cliui.toString()); +} + +// to store previous build's assets.json for comparison +let prev_assets_json; +let curr_assets_json; + +async function write_assets_json(metafile) { + let rtl = false; + prev_assets_json = curr_assets_json; + let out = {}; + for (let output in metafile.outputs) { + let info = metafile.outputs[output]; + let asset_path = "/" + path.relative(sites_path, output); + if (info.entryPoint) { + let key = path.basename(info.entryPoint); + if (key.endsWith(".css") && asset_path.includes("/css-rtl/")) { + rtl = true; + key = `rtl_${key}`; + } + out[key] = asset_path; + } + } + + let assets_json_path = path.resolve(assets_path, `assets${rtl ? "-rtl" : ""}.json`); + let assets_json; + try { + assets_json = await fs.promises.readFile(assets_json_path, "utf-8"); + } catch (error) { + assets_json = "{}"; + } + assets_json = JSON.parse(assets_json); + // update with new values + let new_assets_json = Object.assign({}, assets_json, out); + curr_assets_json = new_assets_json; + + await fs.promises.writeFile(assets_json_path, JSON.stringify(new_assets_json, null, 4)); + await update_assets_json_in_cache(); + if (argv["save-metafiles"]) { + // use current timestamp in readable formate as a suffix for filename + let current_timestamp = new Date().getTime(); + const metafile_name = `meta-${current_timestamp}.json`; + await fs.promises.writeFile(`${metafile_name}`, JSON.stringify(metafile)); + log(`Saved metafile as ${metafile_name}`); + } + return { + new_assets_json, + prev_assets_json, + }; +} + +async function update_assets_json_in_cache() { + // update assets_json cache in redis, so that it can be read directly by python + let client = get_redis_subscriber("redis_cache"); + // handle error event to avoid printing stack traces + try { + await client.connect(); + } catch (e) { + log_warn("Cannot connect to redis_cache to update assets_json"); + } + client.del("assets_json", (err) => { + client.unref(); + }); +} + +function run_build_command_for_apps(apps) { + let cwd = process.cwd(); + let { execSync } = require("child_process"); + + for (let app of apps) { + if (app === "xhiveframework") continue; + + let root_app_path = path.resolve(get_app_path(app), ".."); + let package_json = path.resolve(root_app_path, "package.json"); + if (fs.existsSync(package_json)) { + let { scripts } = require(package_json); + if (scripts && scripts.build) { + log("\nRunning build command for", chalk.bold(app)); + process.chdir(root_app_path); + execSync("yarn build", { encoding: "utf8", stdio: "inherit" }); + } + } + } + + process.chdir(cwd); +} + +async function notify_redis({ error, success, changed_files }) { + // notify redis which in turns tells socketio to publish this to browser + let subscriber = get_redis_subscriber("redis_queue"); + try { + await subscriber.connect(); + } catch (e) { + log_warn("Cannot connect to redis_queue for browser events"); + } + + let payload = null; + if (error) { + let formatted = await esbuild.formatMessages(error.errors, { + kind: "error", + terminalWidth: 100, + }); + let stack = error.stack.replace(new RegExp(bench_path, "g"), ""); + payload = { + error, + formatted, + stack, + }; + } + if (success) { + payload = { + success: true, + changed_files, + live_reload: argv["live-reload"], + }; + } + + await subscriber.publish( + "events", + JSON.stringify({ + event: "build_event", + message: payload, + }) + ); +} + +async function open_in_editor() { + let subscriber = get_redis_subscriber("redis_queue"); + try { + await subscriber.connect(); + } catch (e) { + log_warn("Cannot connect to redis_queue for open_in_editor events"); + } + subscriber.subscribe("open_in_editor", (file) => { + file = JSON.parse(file); + let file_path = path.resolve(file.file); + log("Opening file in editor:", file_path); + let launch = require("launch-editor"); + launch(`${file_path}:${file.line}:${file.column}`); + }); +} + +function get_rebuilt_assets(prev_assets, new_assets) { + let added_files = []; + let old_files = Object.values(prev_assets); + let new_files = Object.values(new_assets); + + for (let filepath of new_files) { + if (!old_files.includes(filepath)) { + added_files.push(filepath); + } + } + return added_files; +} diff --git a/esbuild/ignore-assets.js b/esbuild/ignore-assets.js new file mode 100644 index 0000000..eb5e00a --- /dev/null +++ b/esbuild/ignore-assets.js @@ -0,0 +1,11 @@ +module.exports = { + name: "xhiveframework-ignore-asset", + setup(build) { + build.onResolve({ filter: /^\/assets\// }, (args) => { + return { + path: args.path, + external: true, + }; + }); + }, +}; diff --git a/esbuild/index.js b/esbuild/index.js new file mode 100644 index 0000000..2721673 --- /dev/null +++ b/esbuild/index.js @@ -0,0 +1 @@ +require("./esbuild"); diff --git a/esbuild/sass_options.js b/esbuild/sass_options.js new file mode 100644 index 0000000..f6a7b11 --- /dev/null +++ b/esbuild/sass_options.js @@ -0,0 +1,24 @@ +let path = require("path"); +let { get_app_path, app_list } = require("./utils"); + +let node_modules_path = path.resolve(get_app_path("xhiveframework"), "..", "node_modules"); +let app_paths = app_list.map(get_app_path).map((app_path) => path.resolve(app_path, "..")); + +module.exports = { + includePaths: [node_modules_path, ...app_paths], + quietDeps: true, + importer: function (url) { + if (url.startsWith("~")) { + // strip ~ so that it can resolve from node_modules + url = url.slice(1); + } + if (url.endsWith(".css")) { + // strip .css from end of path + url = url.slice(0, -4); + } + // normal file, let it go + return { + file: url, + }; + }, +}; diff --git a/esbuild/utils.js b/esbuild/utils.js new file mode 100644 index 0000000..de4a0e3 --- /dev/null +++ b/esbuild/utils.js @@ -0,0 +1,146 @@ +const path = require("path"); +const fs = require("fs"); +const chalk = require("chalk"); + +const xhiveframework_path = path.resolve(__dirname, ".."); +const bench_path = path.resolve(xhiveframework_path, "..", ".."); +const sites_path = path.resolve(bench_path, "sites"); +const apps_path = path.resolve(bench_path, "apps"); +const assets_path = path.resolve(sites_path, "assets"); +const app_list = get_apps_list(); + +const app_paths = app_list.reduce((out, app) => { + out[app] = path.resolve(apps_path, app, app); + return out; +}, {}); +const public_paths = app_list.reduce((out, app) => { + out[app] = path.resolve(app_paths[app], "public"); + return out; +}, {}); +const public_js_paths = app_list.reduce((out, app) => { + out[app] = path.resolve(app_paths[app], "public/js"); + return out; +}, {}); + +const bundle_map = app_list.reduce((out, app) => { + const public_js_path = public_js_paths[app]; + if (fs.existsSync(public_js_path)) { + const all_files = fs.readdirSync(public_js_path); + const js_files = all_files.filter((file) => file.endsWith(".js")); + + for (let js_file of js_files) { + const filename = path.basename(js_file).split(".")[0]; + out[path.join(app, "js", filename)] = path.resolve(public_js_path, js_file); + } + } + + return out; +}, {}); + +const get_public_path = (app) => public_paths[app]; + +const get_build_json_path = (app) => path.resolve(get_public_path(app), "build.json"); + +function get_build_json(app) { + try { + return require(get_build_json_path(app)); + } catch (e) { + // build.json does not exist + return null; + } +} + +function delete_file(path) { + if (fs.existsSync(path)) { + fs.unlinkSync(path); + } +} + +function run_serially(tasks) { + let result = Promise.resolve(); + tasks.forEach((task) => { + if (task) { + result = result.then ? result.then(task) : Promise.resolve(); + } + }); + return result; +} + +const get_app_path = (app) => app_paths[app]; + +function get_apps_list() { + return fs + .readFileSync(path.resolve(sites_path, "apps.txt"), { + encoding: "utf-8", + }) + .split("\n") + .filter(Boolean); +} + +function get_cli_arg(name) { + let args = process.argv.slice(2); + let arg = `--${name}`; + let index = args.indexOf(arg); + + let value = null; + if (index != -1) { + value = true; + } + if (value && args[index + 1]) { + value = args[index + 1]; + } + return value; +} + +function log_error(message, badge = "ERROR") { + badge = chalk.white.bgRed(` ${badge} `); + console.error(`${badge} ${message}`); +} + +function log_warn(message, badge = "WARN") { + badge = chalk.black.bgYellowBright(` ${badge} `); + console.warn(`${badge} ${message}`); +} + +function log(...args) { + console.log(...args); +} + +function get_redis_subscriber(kind) { + // get redis subscriber that aborts after 10 connection attempts + let retry_strategy; + let { get_redis_subscriber: get_redis, get_conf } = require("../node_utils"); + + if (process.env.CI == 1 || get_conf().developer_mode == 0) { + retry_strategy = () => {}; + } else { + retry_strategy = function (options) { + // abort after 5 x 3 connection attempts ~= 3 seconds + if (options.attempt > 4) { + return undefined; + } + return options.attempt * 100; + }; + } + return get_redis(kind, { retry_strategy }); +} + +module.exports = { + app_list, + bench_path, + assets_path, + sites_path, + apps_path, + bundle_map, + get_public_path, + get_build_json_path, + get_build_json, + get_app_path, + delete_file, + run_serially, + get_cli_arg, + log, + log_warn, + log_error, + get_redis_subscriber, +}; diff --git a/esbuild/xhiveframework-html.js b/esbuild/xhiveframework-html.js new file mode 100644 index 0000000..766b993 --- /dev/null +++ b/esbuild/xhiveframework-html.js @@ -0,0 +1,44 @@ +module.exports = { + name: "xhiveframework-html", + setup(build) { + let path = require("path"); + let fs = require("fs/promises"); + + build.onResolve({ filter: /\.html$/ }, (args) => { + return { + path: path.join(args.resolveDir, args.path), + namespace: "xhiveframework-html", + }; + }); + + build.onLoad({ filter: /.*/, namespace: "xhiveframework-html" }, (args) => { + let filepath = args.path; + let filename = path.basename(filepath).split(".")[0]; + + return fs + .readFile(filepath, "utf-8") + .then((content) => { + content = scrub_html_template(content); + return { + contents: `\n\txhiveframework.templates['${filename}'] = \`${content}\`;\n`, + watchFiles: [filepath], + }; + }) + .catch(() => { + return { + contents: "", + warnings: [ + { + text: `There was an error importing ${filepath}`, + }, + ], + }; + }); + }); + }, +}; + +function scrub_html_template(content) { + content = content.replace(/`/g, "\\`"); + return content; +} diff --git a/esbuild/xhiveframework-vue-style.js b/esbuild/xhiveframework-vue-style.js new file mode 100644 index 0000000..f729559 --- /dev/null +++ b/esbuild/xhiveframework-vue-style.js @@ -0,0 +1,59 @@ +const fs = require("fs"); +const path = require("path"); +const { sites_path } = require("./utils"); + +module.exports = { + name: "xhiveframework-vue-style", + setup(build) { + build.initialOptions.write = false; + build.onEnd((result) => { + let files = get_files(result.metafile.outputs); + let keys = Object.keys(files); + for (let out of result.outputFiles) { + let asset_path = "/" + path.relative(sites_path, out.path); + let dir = path.dirname(out.path); + if (out.path.endsWith(".js") && keys.includes(asset_path)) { + let name = out.path.split(".bundle.")[0]; + name = path.basename(name); + + let index = result.outputFiles.findIndex((f) => { + return f.path.endsWith(".css") && f.path.includes(`/${name}.bundle.`); + }); + + let css_data = JSON.stringify(result.outputFiles[index].text); + let modified = `xhiveframework.dom.set_style(${css_data});\n${out.text}`; + out.contents = Buffer.from(modified); + + result.outputFiles.splice(index, 1); + if (result.outputFiles[index - 1].path.endsWith(".css.map")) { + result.outputFiles.splice(index - 1, 1); + } + } + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + fs.writeFile(out.path, out.contents, (err) => { + err && console.error(err); + }); + } + }); + }, +}; + +function get_files(files) { + let result = {}; + for (let file in files) { + let info = files[file]; + let asset_path = "/" + path.relative(sites_path, file); + if (info && info.entryPoint && Object.keys(info.inputs).length !== 0) { + for (let input in info.inputs) { + if (input.includes(".vue?type=style")) { + let bundle_css = path.basename(info.entryPoint).replace(".js", ".css"); + result[asset_path] = bundle_css; + break; + } + } + } + } + return result; +} diff --git a/generate_bootstrap_theme.js b/generate_bootstrap_theme.js new file mode 100644 index 0000000..ca2b0df --- /dev/null +++ b/generate_bootstrap_theme.js @@ -0,0 +1,28 @@ +const sass = require("sass"); +const fs = require("fs"); +const sass_options = require("./esbuild/sass_options"); +let output_path = process.argv[2]; +let scss_content = process.argv[3]; +scss_content = scss_content.replace(/\\n/g, "\n"); + +sass.render( + { + data: scss_content, + outputStyle: "compressed", + ...sass_options, + }, + function (err, result) { + if (err) { + console.error(err.formatted); + return; + } + + fs.writeFile(output_path, result.css, function (err) { + if (!err) { + console.log(output_path); + } else { + console.error(err); + } + }); + } +); diff --git a/hooks.md b/hooks.md new file mode 100644 index 0000000..97ea97d --- /dev/null +++ b/hooks.md @@ -0,0 +1,36 @@ +### List of Hooks + +#### Application Name and Details + +1. `app_name` - slugified name e.g. "xhiveframework" +1. `app_title` - full title name e.g. "XhiveFramework" +1. `app_publisher` +1. `app_description` +1. `app_version` + +#### Install + +1. `before_install` - method +1. `after_install` - method + + +#### Javascript / CSS Builds + +1. `app_include_js` - include in "app" +1. `app_include_css` - assets/xhiveframework/css/splash.css + +1. `web_include_js` - assets/js/xhiveframework-web.min.js +1. `web_include_css` - assets/css/xhiveframework-web.css + +#### Desktop + +1. `get_desktop_icons` - method to get list of desktop icons + +#### Notifications + +1. `notification_config` - method to get notification configuration + +#### Permissions + +1. `permission_query_conditions:[doctype]` - method to return additional query conditions at time of report / list etc. +1. `has_permission:[doctype]` - method to call permissions to check at individual level diff --git a/node_utils.js b/node_utils.js new file mode 100644 index 0000000..d4f9fd4 --- /dev/null +++ b/node_utils.js @@ -0,0 +1,60 @@ +const fs = require("fs"); +const path = require("path"); +const redis = require("@redis/client"); +const bench_path = path.resolve(__dirname, "..", ".."); + +const dns = require("dns"); + +// Since node17, node resolves to ipv6 unless system is configured otherwise. +// In XhiveFramework context using ipv4 - 127.0.0.1 is fine. +dns.setDefaultResultOrder("ipv4first"); + +function get_conf() { + // defaults + var conf = { + socketio_port: 9000, + }; + + var read_config = function (file_path) { + const full_path = path.resolve(bench_path, file_path); + + if (fs.existsSync(full_path)) { + var bench_config = JSON.parse(fs.readFileSync(full_path)); + for (var key in bench_config) { + if (bench_config[key]) { + conf[key] = bench_config[key]; + } + } + } + }; + + // get ports from bench/config.json + read_config("config.json"); + read_config("sites/common_site_config.json"); + + // set overrides from environment + if (process.env.XHIVEFRAMEWORK_SITE) { + conf.default_site = process.env.XHIVEFRAMEWORK_SITE; + } + if (process.env.XHIVEFRAMEWORK_REDIS_CACHE) { + conf.redis_cache = process.env.XHIVEFRAMEWORK_REDIS_CACHE; + } + if (process.env.XHIVEFRAMEWORK_REDIS_QUEUE) { + conf.redis_queue = process.env.XHIVEFRAMEWORK_REDIS_QUEUE; + } + if (process.env.XHIVEFRAMEWORK_SOCKETIO_PORT) { + conf.socketio_port = process.env.XHIVEFRAMEWORK_SOCKETIO_PORT; + } + return conf; +} + +function get_redis_subscriber(kind = "redis_queue", options = {}) { + const conf = get_conf(); + const host = conf[kind]; + return redis.createClient({ url: host, ...options }); +} + +module.exports = { + get_conf, + get_redis_subscriber, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..87706d2 --- /dev/null +++ b/package.json @@ -0,0 +1,91 @@ +{ + "name": "xhiveframework-framework", + "scripts": { + "build": "node esbuild", + "production": "node esbuild --production", + "watch": "node esbuild --watch", + "coverage:report": "npx nyc report --reporter=clover" + }, + "repository": { + "type": "git", + "url": "git+https://lab.membtech.com/xhiveframework/xhiveframework15.git" + }, + "author": "XhiveFramework Technologies Pvt. Ltd.", + "license": "MIT", + "bugs": { + "url": "https://lab.membtech.com/xhiveframework/xhiveframework15/issues" + }, + "engines": { + "node": ">=18" + }, + "homepage": "https://xhiveframework.com", + "dependencies": { + "@editorjs/editorjs": "~2.26.3", + "@xhiveframework/esbuild-plugin-postcss2": "^0.1.3", + "@headlessui/vue": "^1.7.16", + "@popperjs/core": "^2.11.2", + "@redis/client": "^1.5.8", + "@sentry/browser": "^7.15.0", + "@vue-flow/background": "^1.1.0", + "@vue-flow/core": "^1.16.2", + "@vue/component-compiler": "^4.2.4", + "@vueuse/core": "^9.5.0", + "ace-builds": "^1.4.8", + "air-datepicker": "github:xhiveframework/air-datepicker", + "autoprefixer": "10", + "awesomplete": "^1.1.5", + "bootstrap": "4.6.2", + "chalk": "^2.3.2", + "cliui": "^7.0.4", + "cookie": "^0.4.0", + "cropperjs": "^1.5.12", + "cssnano": "^5.0.0", + "driver.js": "^0.9.8", + "editorjs-undo": "0.1.6", + "esbuild": "^0.14.29", + "esbuild-plugin-vue3": "^0.3.0", + "fast-deep-equal": "^2.0.1", + "fast-glob": "^3.2.5", + "xhiveframework-charts": "2.0.0-rc22", + "xhiveframework-datatable": "1.17.15", + "xhiveframework-gantt": "^0.6.0", + "highlight.js": "^10.4.1", + "html5-qrcode": "^2.3.8", + "jquery": "3.7.0", + "js-sha256": "^0.9.0", + "jsbarcode": "^3.11.0", + "launch-editor": "^2.2.1", + "localforage": "^1.10.0", + "md5": "^2.3.0", + "moment": "^2.29.4", + "moment-timezone": "^0.5.35", + "pinia": "^2.0.23", + "plyr": "^3.7.8", + "popper.js": "^1.16.0", + "postcss": "8", + "quill": "2.0.0-dev.4", + "quill-image-resize": "^3.0.9", + "quill-magic-url": "^3.0.0", + "qz-tray": "^2.0.8", + "rtlcss": "^4.0.0", + "sass": "^1.63.0", + "showdown": "^2.1.0", + "socket.io": "^4.7.1", + "socket.io-client": "^4.7.1", + "sortablejs": "^1.15.0", + "superagent": "^8.0.0", + "touch": "^3.1.0", + "vue": "^3.3.0", + "vue-router": "^4.1.5", + "vuedraggable": "^4.1.0", + "vuex": "4.0.2", + "yargs": "^17.5.1" + }, + "nyc": { + "report-dir": ".cypress-coverage" + }, + "optionalDependencies": { + "bufferutil": "^4.0.8", + "utf-8-validate": "^6.0.3" + } +} diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..34980d2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,138 @@ +[project] +name = "xhiveframework" +authors = [ + { name = "XhiveFramework Technologies Pvt Ltd", email = "developers@xhiveframework.io"} +] +description = "Metadata driven, full-stack low code web framework" +requires-python = ">=3.10" +readme = "README.md" +dynamic = ["version"] +dependencies = [ + # core dependencies + "Babel~=2.12.1", + "Click~=8.1.7", + "filelock~=3.8.0", + "filetype~=1.2.0", + "GitPython~=3.1.34", + "Jinja2~=3.1.2", + "Pillow~=10.2.0", + "PyJWT~=2.8.0", + # We depend on internal attributes, + # do NOT add loose requirements on PyMySQL versions. + "PyMySQL==1.1.0", + "pypdf~=3.17.0", + "PyPika==0.48.9", + "PyQRCode~=1.2.1", + "PyYAML~=6.0.1", + "RestrictedPython~=6.2", + "WeasyPrint==59.0", + "Werkzeug~=3.0.1", + "Whoosh~=2.7.4", + "beautifulsoup4~=4.12.2", + "bleach-allowlist~=1.0.3", + "bleach[css]~=6.0.0", + "cairocffi==1.5.1", + "chardet~=5.1.0", + "croniter~=1.4.1", + "cryptography~=42.0.0", + "email-reply-parser~=0.5.12", + "git-url-parse~=1.2.2", + "gunicorn~=21.2.0", + "html5lib~=1.1", + "ipython~=8.15.0", + "ldap3~=2.9", + "markdown2~=2.4.8", + "MarkupSafe>=2.1.0,<3", + "maxminddb-geolite2==2018.703", + "num2words~=0.5.12", + "oauthlib~=3.2.2", + "openpyxl~=3.1.2", + "passlib~=1.7.4", + "pdfkit~=1.0.0", + "phonenumbers==8.13.13", + "premailer~=3.10.0", + "psutil~=5.9.5", + "psycopg2-binary~=2.9.1", + "pyOpenSSL~=24.0.0", + "pydantic==2.3.0", + "pyotp~=2.8.0", + "python-dateutil~=2.8.2", + "pytz==2023.3", + "rauth~=0.7.3", + "redis~=4.5.5", + "hiredis~=2.2.3", + "requests-oauthlib~=1.3.1", + "requests~=2.31.0", + "rq~=1.15.1", + "rsa>=4.1", + "semantic-version~=2.10.0", + "sentry-sdk~=1.37.1", + "sqlparse~=0.4.4", + "tenacity~=8.2.2", + "terminaltables~=3.1.10", + "traceback-with-variables~=2.0.4", + "typing_extensions>=4.6.1,<5", + "xlrd~=2.0.1", + "zxcvbn~=4.4.28", + "markdownify~=0.11.6", + + # integration dependencies + "boto3~=1.28.10", + "dropbox~=11.36.2", + "google-api-python-client~=2.2.0", + "google-auth-oauthlib~=0.4.4", + "google-auth~=1.29.0", + "posthog~=3.0.1", +] + +[build-system] +requires = ["flit_core >=3.4,<4"] +build-backend = "flit_core.buildapi" + +[tool.bench.dev-dependencies] +coverage = "~=6.5.0" +Faker = "~=18.10.1" +pyngrok = "~=6.0.0" +unittest-xml-reporting = "~=3.2.0" +watchdog = "~=3.0.0" +hypothesis = "~=6.77.0" +responses = "==0.23.1" +freezegun = "~=1.2.2" + +[tool.ruff] +line-length = 110 +target-version = "py310" + +[tool.ruff.lint] +select = [ + "F", + "E", + "W", + "I", + "UP", + "B", + "RUF", +] +ignore = [ + "B017", # assertRaises(Exception) - should be more specific + "B018", # useless expression, not assigned to anything + "B023", # function doesn't bind loop variable - will have last iteration's value + "B904", # raise inside except without from + "E101", # indentation contains mixed spaces and tabs + "E402", # module level import not at top of file + "E501", # line too long + "E741", # ambiguous variable name + "F401", # "unused" imports + "F403", # can't detect undefined names from * import + "F405", # can't detect undefined names from * import + "F722", # syntax error in forward type annotation + "W191", # indentation contains tabs + "RUF001", # string contains ambiguous unicode character +] +typing-modules = ["xhiveframework.types.DF"] + +[tool.ruff.format] +quote-style = "double" +indent-style = "tab" +docstring-code-format = true + diff --git a/realtime/handlers/xhiveframework_handlers.js b/realtime/handlers/xhiveframework_handlers.js new file mode 100644 index 0000000..90d0446 --- /dev/null +++ b/realtime/handlers/xhiveframework_handlers.js @@ -0,0 +1,193 @@ +const { xhiveframework_request } = require("../utils"); +const log = console.log; + +const WEBSITE_ROOM = "website"; +const SITE_ROOM = "all"; + +function xhiveframework_handlers(realtime, socket) { + socket.join(user_room(socket.user)); + socket.join(WEBSITE_ROOM); + + if (socket.user_type == "System User") { + socket.join(SITE_ROOM); + } + + socket.on("doctype_subscribe", function (doctype) { + can_subscribe_doctype({ + socket, + doctype, + callback: () => { + socket.join(doctype_room(doctype)); + }, + }); + }); + + socket.on("doctype_unsubscribe", function (doctype) { + socket.leave(doctype_room(doctype)); + }); + + socket.on("task_subscribe", function (task_id) { + const room = task_room(task_id); + socket.join(room); + }); + + socket.on("task_unsubscribe", function (task_id) { + const room = task_room(task_id); + socket.leave(room); + }); + + socket.on("progress_subscribe", function (task_id) { + const room = task_room(task_id); + socket.join(room); + }); + + socket.on("doc_subscribe", function (doctype, docname) { + can_subscribe_doc({ + socket, + doctype, + docname, + callback: () => { + let room = doc_room(doctype, docname); + socket.join(room); + }, + }); + }); + + socket.on("doc_unsubscribe", function (doctype, docname) { + let room = doc_room(doctype, docname); + socket.leave(room); + }); + + socket.on("doc_open", function (doctype, docname) { + can_subscribe_doc({ + socket, + doctype, + docname, + callback: () => { + let room = open_doc_room(doctype, docname); + socket.join(room); + if (!socket.subscribed_documents) socket.subscribed_documents = []; + socket.subscribed_documents.push([doctype, docname]); + + // show who is currently viewing the form + notify_subscribed_doc_users({ + socket: socket, + doctype: doctype, + docname: docname, + }); + }, + }); + }); + + socket.on("doc_close", function (doctype, docname) { + // remove this user from the list of 'who is currently viewing the form' + let room = open_doc_room(doctype, docname); + socket.leave(room); + + if (socket.subscribed_documents) { + socket.subscribed_documents = socket.subscribed_documents.filter(([dt, dn]) => { + !(dt == doctype && dn == docname); + }); + } + + notify_subscribed_doc_users({ + socket: socket, + doctype: doctype, + docname: docname, + }); + }); + + socket.on("disconnect", () => { + notify_disconnected_documents(socket); + }); +} + +function notify_disconnected_documents(socket) { + if (socket.subscribed_documents) { + socket.subscribed_documents.forEach(([doctype, docname]) => { + notify_subscribed_doc_users({ socket, doctype, docname }); + }); + } +} + +function can_subscribe_doctype(args) { + if (!args) return; + if (!args.doctype) return; + xhiveframework_request("/api/method/xhiveframework.realtime.can_subscribe_doctype", args.socket) + .type("form") + .query({ + doctype: args.doctype, + }) + .end(function (err, res) { + if (!res || res.status == 403 || err) { + if (err) { + log(err); + } + return false; + } else if (res.status == 200) { + args.callback && args.callback(err, res); + return true; + } + log("ERROR (can_subscribe_doctype): ", err, res); + }); +} + +function notify_subscribed_doc_users(args) { + if (!(args && args.doctype && args.docname)) { + return; + } + const socket = args.socket; + const room = open_doc_room(args.doctype, args.docname); + + const clients = Array.from(socket.nsp.adapter.rooms.get(room) || []); + + let users = []; + + socket.nsp.sockets.forEach((sock) => { + if (clients.includes(sock.id)) { + users.push(sock.user); + } + }); + + // dont send update to self. meaningless. + if (users.length == 1 && users[0] == args.socket.user) return; + + // notify + socket.nsp.to(room).emit("doc_viewers", { + doctype: args.doctype, + docname: args.docname, + users: Array.from(new Set(users)), + }); +} + +function can_subscribe_doc(args) { + if (!args) return; + if (!args.doctype || !args.docname) return; + xhiveframework_request("/api/method/xhiveframework.realtime.can_subscribe_doc", args.socket) + .type("form") + .query({ + doctype: args.doctype, + docname: args.docname, + }) + .end(function (err, res) { + if (!res) { + log("No response for doc_subscribe"); + } else if (res.status == 403) { + return; + } else if (err) { + log(err); + } else if (res.status == 200) { + args.callback(err, res); + } else { + log("Something went wrong", err, res); + } + }); +} + +const doc_room = (doctype, docname) => "doc:" + doctype + "/" + docname; +const open_doc_room = (doctype, docname) => "open_doc:" + doctype + "/" + docname; +const user_room = (user) => "user:" + user; +const doctype_room = (doctype) => "doctype:" + doctype; +const task_room = (task_id) => "task_progress:" + task_id; + +module.exports = xhiveframework_handlers; diff --git a/realtime/index.js b/realtime/index.js new file mode 100644 index 0000000..25fa60c --- /dev/null +++ b/realtime/index.js @@ -0,0 +1,60 @@ +const { Server } = require("socket.io"); + +const { get_conf, get_redis_subscriber } = require("../node_utils"); +const conf = get_conf(); + +let io = new Server({ + cors: { + // Should be fine since we are ensuring whether hostname and origin are same before adding setting listeners for s socket + origin: true, + credentials: true, + }, + cleanupEmptyChildNamespaces: true, +}); + +// Multitenancy implementation. +// allow arbitrary sitename as namespaces +// namespaces get validated during authentication. +const realtime = io.of(/^\/.*$/); + +// load and register middlewares +const authenticate = require("./middlewares/authenticate"); +realtime.use(authenticate); +// ======================= + +// load and register handlers +const xhiveframework_handlers = require("./handlers/xhiveframework_handlers"); +function on_connection(socket) { + xhiveframework_handlers(realtime, socket); + + // ESBUild "open in editor" on error + socket.on("open_in_editor", async (data) => { + await subscriber.connect(); + subscriber.publish("open_in_editor", JSON.stringify(data)); + }); +} + +realtime.on("connection", on_connection); +// ======================= + +// Consume events sent from python via redis pub-sub channel. +const subscriber = get_redis_subscriber(); + +(async () => { + await subscriber.connect(); + subscriber.subscribe("events", (message) => { + message = JSON.parse(message); + let namespace = "/" + message.namespace; + if (message.room) { + io.of(namespace).to(message.room).emit(message.event, message.message); + } else { + // publish to ALL sites only used for things like build event. + realtime.emit(message.event, message.message); + } + }); +})(); +// ======================= + +let port = conf.socketio_port; +io.listen(port); +console.log("Realtime service listening on: ", port); diff --git a/realtime/middlewares/authenticate.js b/realtime/middlewares/authenticate.js new file mode 100644 index 0000000..69e827e --- /dev/null +++ b/realtime/middlewares/authenticate.js @@ -0,0 +1,81 @@ +const cookie = require("cookie"); +const request = require("superagent"); +const { get_url } = require("../utils"); + +const { get_conf } = require("../../node_utils"); +const conf = get_conf(); + +function authenticate_with_xhiveframework(socket, next) { + let namespace = socket.nsp.name; + namespace = namespace.slice(1, namespace.length); // remove leading `/` + + if (namespace != get_site_name(socket)) { + next(new Error("Invalid namespace")); + } + + if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) { + next(new Error("Invalid origin")); + return; + } + + if (!socket.request.headers.cookie) { + next(new Error("No cookie transmitted.")); + return; + } + + let cookies = cookie.parse(socket.request.headers.cookie || ""); + let authorization_header = socket.request.headers.authorization; + + if (!cookies.sid && !authorization_header) { + next(new Error("No authentication method used. Use cookie or authorization header.")); + return; + } + + let auth_req = request.get(get_url(socket, "/api/method/xhiveframework.realtime.get_user_info")); + if (cookies.sid) { + auth_req = auth_req.query({ sid: cookies.sid }); + } else { + auth_req = auth_req.set("Authorization", authorization_header); + } + + auth_req + .type("form") + .then((res) => { + socket.user = res.body.message.user; + socket.user_type = res.body.message.user_type; + socket.sid = cookies.sid; + socket.authorization_header = authorization_header; + next(); + }) + .catch((e) => { + next(new Error(`Unauthorized: ${e}`)); + }); +} + +function get_site_name(socket) { + if (socket.site_name) { + return socket.site_name; + } else if (socket.request.headers["x-xhiveframework-site-name"]) { + socket.site_name = get_hostname(socket.request.headers["x-xhiveframework-site-name"]); + } else if ( + conf.default_site && + ["localhost", "127.0.0.1"].indexOf(get_hostname(socket.request.headers.host)) !== -1 + ) { + socket.site_name = conf.default_site; + } else if (socket.request.headers.origin) { + socket.site_name = get_hostname(socket.request.headers.origin); + } else { + socket.site_name = get_hostname(socket.request.headers.host); + } + return socket.site_name; +} + +function get_hostname(url) { + if (!url) return undefined; + if (url.indexOf("://") > -1) { + url = url.split("/")[2]; + } + return url.match(/:/g) ? url.slice(0, url.indexOf(":")) : url; +} + +module.exports = authenticate_with_xhiveframework; diff --git a/realtime/utils.js b/realtime/utils.js new file mode 100644 index 0000000..701dbed --- /dev/null +++ b/realtime/utils.js @@ -0,0 +1,23 @@ +const request = require("superagent"); + +function get_url(socket, path) { + if (!path) { + path = ""; + } + return socket.request.headers.origin + path; +} + +// Authenticates a partial request created using superagent +function xhiveframework_request(path, socket) { + const partial_req = request.get(get_url(socket, path)); + if (socket.sid) { + return partial_req.query({ sid: socket.sid }); + } else if (socket.authorization_header) { + return partial_req.set("Authorization", socket.authorization_header); + } +} + +module.exports = { + get_url, + xhiveframework_request, +}; diff --git a/sider.yml b/sider.yml new file mode 100644 index 0000000..2ca6e8d --- /dev/null +++ b/sider.yml @@ -0,0 +1,3 @@ +linter: + flake8: + config: .flake8 \ No newline at end of file diff --git a/socketio.js b/socketio.js new file mode 100644 index 0000000..14360d9 --- /dev/null +++ b/socketio.js @@ -0,0 +1 @@ +require("./realtime"); diff --git a/xhiveframework/__init__.py b/xhiveframework/__init__.py new file mode 100644 index 0000000..239b025 --- /dev/null +++ b/xhiveframework/__init__.py @@ -0,0 +1,2481 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +""" +XhiveFramework - Low Code Open Source Framework in Python and JS + +XhiveFramework, pronounced fra-pay, is a full stack, batteries-included, web +framework written in Python and Javascript with MariaDB as the database. +It is the framework which powers XhiveERP. It is pretty generic and can +be used to build database driven apps. + +Read the documentation: https://xhiveframework.com/docs +""" +import copy +import faulthandler +import functools +import gc +import importlib +import inspect +import json +import os +import re +import signal +import traceback +import unicodedata +import warnings +from collections.abc import Callable +from typing import TYPE_CHECKING, Any, Literal, Optional, TypeAlias, overload + +import click +from werkzeug.local import Local, release_local + +import xhiveframework +from xhiveframework.query_builder import ( + get_query, + get_query_builder, + patch_query_aggregation, + patch_query_execute, +) +from xhiveframework.utils.caching import request_cache +from xhiveframework.utils.data import cint, cstr, sbool + +# Local application imports +from .exceptions import * +from .utils.jinja import ( + get_email_from_template, + get_jenv, + get_jloader, + get_template, + render_template, +) +from .utils.lazy_loader import lazy_import + +__version__ = "15.18.2" +__title__ = "Xhive Framework" + +controllers = {} +local = Local() +cache = None +STANDARD_USERS = ("Guest", "Administrator") + +_qb_patched = {} +_dev_server = int(sbool(os.environ.get("DEV_SERVER", False))) +_tune_gc = bool(sbool(os.environ.get("XHIVEFRAMEWORK_TUNE_GC", True))) + +if _dev_server: + warnings.simplefilter("always", DeprecationWarning) + warnings.simplefilter("always", PendingDeprecationWarning) + + +class _dict(dict): + """dict like object that exposes keys as attributes""" + + __slots__ = () + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ + __setstate__ = dict.update + + def __getstate__(self): + return self + + def update(self, *args, **kwargs): + """update and return self -- the missing dict feature in python""" + + super().update(*args, **kwargs) + return self + + def copy(self): + return _dict(self) + + +def _(msg: str, lang: str | None = None, context: str | None = None) -> str: + """Returns translated string in current lang, if exists. + Usage: + _('Change') + _('Change', context='Coins') + """ + from xhiveframework.translate import get_all_translations + from xhiveframework.utils import is_html, strip_html_tags + + if not hasattr(local, "lang"): + local.lang = lang or "en" + + if not lang: + lang = local.lang + + non_translated_string = msg + + if is_html(msg): + msg = strip_html_tags(msg) + + # msg should always be unicode + msg = as_unicode(msg).strip() + + translated_string = "" + + all_translations = get_all_translations(lang) + if context: + string_key = f"{msg}:{context}" + translated_string = all_translations.get(string_key) + + if not translated_string: + translated_string = all_translations.get(msg) + + return translated_string or non_translated_string + + +def as_unicode(text: str, encoding: str = "utf-8") -> str: + """Convert to unicode if required""" + if isinstance(text, str): + return text + elif text is None: + return "" + elif isinstance(text, bytes): + return str(text, encoding) + else: + return str(text) + + +def get_lang_dict(fortype: str, name: str | None = None) -> dict[str, str]: + """Returns the translated language dict for the given type and name. + + :param fortype: must be one of `doctype`, `page`, `report`, `include`, `jsfile`, `boot` + :param name: name of the document for which assets are to be returned.""" + from xhiveframework.translate import get_dict + + return get_dict(fortype, name) + + +def set_user_lang(user: str, user_language: str | None = None) -> None: + """Guess and set user language for the session. `xhiveframework.local.lang`""" + from xhiveframework.translate import get_user_lang + + local.lang = get_user_lang(user) or user_language + + +# local-globals + +db = local("db") +qb = local("qb") +conf = local("conf") +form = form_dict = local("form_dict") +request = local("request") +job = local("job") +response = local("response") +session = local("session") +user = local("user") +flags = local("flags") + +error_log = local("error_log") +debug_log = local("debug_log") +message_log = local("message_log") + +lang = local("lang") + +# This if block is never executed when running the code. It is only used for +# telling static code analyzer where to find dynamically defined attributes. +if TYPE_CHECKING: # pragma: no cover + from werkzeug.wrappers import Request + + from xhiveframework.database.mariadb.database import MariaDBDatabase + from xhiveframework.database.postgres.database import PostgresDatabase + from xhiveframework.email.doctype.email_queue.email_queue import EmailQueue + from xhiveframework.model.document import Document + from xhiveframework.query_builder.builder import MariaDB, Postgres + from xhiveframework.utils.redis_wrapper import RedisWrapper + + db: MariaDBDatabase | PostgresDatabase + qb: MariaDB | Postgres + cache: RedisWrapper + response: _dict + conf: _dict + form_dict: _dict + flags: _dict + request: Request + session: _dict + user: str + flags: _dict + lang: str + + +# end: static analysis hack + + +def init(site: str, sites_path: str = ".", new_site: bool = False, force=False) -> None: + """Initialize xhiveframework for the current site. Reset thread locals `xhiveframework.local`""" + if getattr(local, "initialised", None) and not force: + return + + local.error_log = [] + local.message_log = [] + local.debug_log = [] + local.flags = _dict( + { + "currently_saving": [], + "redirect_location": "", + "in_install_db": False, + "in_install_app": False, + "in_import": False, + "in_test": False, + "mute_messages": False, + "ignore_links": False, + "mute_emails": False, + "has_dataurl": False, + "new_site": new_site, + "read_only": False, + } + ) + local.locked_documents = [] + local.test_objects = {} + + local.site = site + local.sites_path = sites_path + local.site_path = os.path.join(sites_path, site) + local.all_apps = None + + local.request_ip = None + local.response = _dict({"docs": []}) + local.task_id = None + + local.conf = _dict(get_site_config()) + local.lang = local.conf.lang or "en" + + local.module_app = None + local.app_modules = None + + local.user = None + local.user_perms = None + local.session = None + local.role_permissions = {} + local.valid_columns = {} + local.new_doc_templates = {} + + local.jenv = None + local.jloader = None + local.cache = {} + local.form_dict = _dict() + local.preload_assets = {"style": [], "script": [], "icons": []} + local.session = _dict() + local.dev_server = _dev_server + local.qb = get_query_builder(local.conf.db_type) + local.qb.get_query = get_query + setup_redis_cache_connection() + + if not _qb_patched.get(local.conf.db_type): + patch_query_execute() + patch_query_aggregation() + _register_fault_handler() + + setup_module_map(include_all_apps=not (xhiveframework.request or xhiveframework.job or xhiveframework.flags.in_migrate)) + + local.initialised = True + + +def connect(site: str | None = None, db_name: str | None = None, set_admin_as_user: bool = True) -> None: + """Connect to site database instance. + + :param site: If site is given, calls `xhiveframework.init`. + :param db_name: Optional. Will use from `site_config.json`. + :param set_admin_as_user: Set Administrator as current user. + """ + from xhiveframework.database import get_db + + if site: + init(site) + + local.db = get_db( + host=local.conf.db_host, + port=local.conf.db_port, + user=db_name or local.conf.db_name, + password=None, + ) + if set_admin_as_user: + set_user("Administrator") + + +def connect_replica() -> bool: + from xhiveframework.database import get_db + + if local and hasattr(local, "replica_db") and hasattr(local, "primary_db"): + return False + + user = local.conf.db_name + password = local.conf.db_password + port = local.conf.replica_db_port + + if local.conf.different_credentials_for_replica: + user = local.conf.replica_db_name + password = local.conf.replica_db_password + + local.replica_db = get_db(host=local.conf.replica_host, user=user, password=password, port=port) + + # swap db connections + local.primary_db = local.db + local.db = local.replica_db + + return True + + +def get_site_config(sites_path: str | None = None, site_path: str | None = None) -> dict[str, Any]: + """Returns `site_config.json` combined with `sites/common_site_config.json`. + `site_config` is a set of site wide settings like database name, password, email etc.""" + config = _dict() + + sites_path = sites_path or getattr(local, "sites_path", None) + site_path = site_path or getattr(local, "site_path", None) + + if sites_path: + config.update(get_common_site_config(sites_path)) + + if site_path: + site_config = os.path.join(site_path, "site_config.json") + if os.path.exists(site_config): + try: + config.update(get_file_json(site_config)) + except Exception as error: + click.secho(f"{local.site}/site_config.json is invalid", fg="red") + print(error) + elif local.site and not local.flags.new_site: + raise IncorrectSitePath(f"{local.site} does not exist") + + # Generalized env variable overrides and defaults + def db_default_ports(db_type): + from xhiveframework.database.mariadb.database import MariaDBDatabase + + return { + "mariadb": MariaDBDatabase.default_port, # 3306 + "postgres": 5432, + }[db_type] + + config["redis_queue"] = ( + os.environ.get("XHIVEFRAMEWORK_REDIS_QUEUE") or config.get("redis_queue") or "redis://127.0.0.1:11311" + ) + config["redis_cache"] = ( + os.environ.get("XHIVEFRAMEWORK_REDIS_CACHE") or config.get("redis_cache") or "redis://127.0.0.1:13311" + ) + config["db_type"] = os.environ.get("XHIVEFRAMEWORK_DB_TYPE") or config.get("db_type") or "mariadb" + config["db_host"] = os.environ.get("XHIVEFRAMEWORK_DB_HOST") or config.get("db_host") or "127.0.0.1" + config["db_port"] = ( + os.environ.get("XHIVEFRAMEWORK_DB_PORT") or config.get("db_port") or db_default_ports(config["db_type"]) + ) + + # Allow externally extending the config with hooks + if extra_config := config.get("extra_config"): + if isinstance(extra_config, str): + extra_config = [extra_config] + for hook in extra_config: + try: + module, method = hook.rsplit(".", 1) + config |= getattr(importlib.import_module(module), method)() + except Exception: + print(f"Config hook {hook} failed") + traceback.print_exc() + + return config + + +def get_common_site_config(sites_path: str | None = None) -> dict[str, Any]: + """Returns common site config as dictionary. + + This is useful for: + - checking configuration which should only be allowed in common site config + - When no site context is present and fallback is required. + """ + sites_path = sites_path or getattr(local, "sites_path", None) + + common_site_config = os.path.join(sites_path, "common_site_config.json") + if os.path.exists(common_site_config): + try: + return _dict(get_file_json(common_site_config)) + except Exception as error: + click.secho("common_site_config.json is invalid", fg="red") + print(error) + return _dict() + + +def get_conf(site: str | None = None) -> dict[str, Any]: + if hasattr(local, "conf"): + return local.conf + + # if no site, get from common_site_config.json + with init_site(site): + return local.conf + + +class init_site: + def __init__(self, site=None): + """If site is None, initialize it for empty site ('') to load common_site_config.json""" + self.site = site or "" + + def __enter__(self): + init(self.site) + return local + + def __exit__(self, type, value, traceback): + destroy() + + +def destroy(): + """Closes connection and releases werkzeug local.""" + if db: + db.close() + + release_local(local) + + +def setup_redis_cache_connection(): + """Defines `xhiveframework.cache` as `RedisWrapper` instance""" + global cache + + if not cache: + from xhiveframework.utils.redis_wrapper import setup_cache + + cache = setup_cache() + + +def get_traceback(with_context: bool = False) -> str: + """Returns error traceback.""" + from xhiveframework.utils import get_traceback + + return get_traceback(with_context=with_context) + + +def errprint(msg: str) -> None: + """Log error. This is sent back as `exc` in response. + + :param msg: Message.""" + msg = as_unicode(msg) + if not request or ("cmd" not in local.form_dict) or conf.developer_mode: + print(msg) + + error_log.append({"exc": msg}) + + +def print_sql(enable: bool = True) -> None: + return cache.set_value("flag_print_sql", enable) + + +def log(msg: str) -> None: + """Add to `debug_log`. + + :param msg: Message.""" + if not request: + print(repr(msg)) + + debug_log.append(as_unicode(msg)) + + +@functools.lru_cache(maxsize=1024) +def _strip_html_tags(message): + from xhiveframework.utils import strip_html_tags + + return strip_html_tags(message) + + +def msgprint( + msg: str, + title: str | None = None, + raise_exception: bool | type[Exception] = False, + as_table: bool = False, + as_list: bool = False, + indicator: Literal["blue", "green", "orange", "red", "yellow"] | None = None, + alert: bool = False, + primary_action: str | None = None, + is_minimizable: bool = False, + wide: bool = False, + *, + realtime=False, +) -> None: + """Print a message to the user (via HTTP response). + Messages are sent in the `__server_messages` property in the + response JSON and shown in a pop-up / modal. + + :param msg: Message. + :param title: [optional] Message title. Default: "Message". + :param raise_exception: [optional] Raise given exception and show message. + :param as_table: [optional] If `msg` is a list of lists, render as HTML table. + :param as_list: [optional] If `msg` is a list, render as un-ordered list. + :param primary_action: [optional] Bind a primary server/client side action. + :param is_minimizable: [optional] Allow users to minimize the modal + :param wide: [optional] Show wide modal + :param realtime: Publish message immediately using websocket. + """ + import inspect + import sys + + msg = safe_decode(msg) + out = _dict(message=msg) + + def _raise_exception(): + if raise_exception: + if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception): + exc = raise_exception(msg) + else: + exc = ValidationError(msg) + if out.__xhiveframework_exc_id: + exc.__xhiveframework_exc_id = out.__xhiveframework_exc_id + raise exc + + if flags.mute_messages: + _raise_exception() + return + + if as_table and type(msg) in (list, tuple): + out.as_table = 1 + + if as_list and type(msg) in (list, tuple): + out.as_list = 1 + + if sys.stdin and sys.stdin.isatty(): + if out.as_list: + msg = [_strip_html_tags(msg) for msg in out.message] + else: + msg = _strip_html_tags(out.message) + + if flags.print_messages and out.message: + print(f"Message: {_strip_html_tags(out.message)}") + + out.title = title or _("Message", context="Default title of the message dialog") + + if not indicator and raise_exception: + indicator = "red" + + if indicator: + out.indicator = indicator + + if is_minimizable: + out.is_minimizable = is_minimizable + + if alert: + out.alert = 1 + + if raise_exception: + out.raise_exception = 1 + out.__xhiveframework_exc_id = generate_hash() + + if primary_action: + out.primary_action = primary_action + + if wide: + out.wide = wide + + if realtime: + publish_realtime(event="msgprint", message=out) + else: + message_log.append(out) + _raise_exception() + + +def clear_messages(): + local.message_log = [] + + +def get_message_log() -> list[dict]: + return [msg_out for msg_out in local.message_log] + + +def clear_last_message(): + if len(local.message_log) > 0: + local.message_log = local.message_log[:-1] + + +def throw( + msg: str, + exc: type[Exception] = ValidationError, + title: str | None = None, + is_minimizable: bool = False, + wide: bool = False, + as_list: bool = False, + primary_action=None, +) -> None: + """Throw execption and show message (`msgprint`). + + :param msg: Message. + :param exc: Exception class. Default `xhiveframework.ValidationError` + :param title: [optional] Message title. Default: "Message". + :param is_minimizable: [optional] Allow users to minimize the modal + :param wide: [optional] Show wide modal + :param as_list: [optional] If `msg` is a list, render as un-ordered list. + :param primary_action: [optional] Bind a primary server/client side action. + """ + msgprint( + msg, + raise_exception=exc, + title=title, + indicator="red", + is_minimizable=is_minimizable, + wide=wide, + as_list=as_list, + primary_action=primary_action, + ) + + +def create_folder(path, with_init=False): + """Create a folder in the given path and add an `__init__.py` file (optional). + + :param path: Folder path. + :param with_init: Create `__init__.py` in the new folder.""" + from xhiveframework.utils import touch_file + + if not os.path.exists(path): + os.makedirs(path) + + if with_init: + touch_file(os.path.join(path, "__init__.py")) + + +def set_user(username: str): + """Set current user. + + :param username: **User** name to set as current user.""" + local.session.user = username + local.session.sid = username + local.cache = {} + local.form_dict = _dict() + local.jenv = None + local.session.data = _dict() + local.role_permissions = {} + local.new_doc_templates = {} + local.user_perms = None + + +def get_user(): + from xhiveframework.utils.user import UserPermissions + + if not local.user_perms: + local.user_perms = UserPermissions(local.session.user) + return local.user_perms + + +def get_roles(username=None) -> list[str]: + """Returns roles of current user.""" + if not local.session or not local.session.user: + return ["Guest"] + import xhiveframework.permissions + + return xhiveframework.permissions.get_roles(username or local.session.user) + + +def get_request_header(key, default=None): + """Return HTTP request header. + + :param key: HTTP header key. + :param default: Default value.""" + return request.headers.get(key, default) + + +def sendmail( + recipients=None, + sender="", + subject="No Subject", + message="No Message", + as_markdown=False, + delayed=True, + reference_doctype=None, + reference_name=None, + unsubscribe_method=None, + unsubscribe_params=None, + unsubscribe_message=None, + add_unsubscribe_link=1, + attachments=None, + content=None, + doctype=None, + name=None, + reply_to=None, + queue_separately=False, + cc=None, + bcc=None, + message_id=None, + in_reply_to=None, + send_after=None, + expose_recipients=None, + send_priority=1, + communication=None, + retry=1, + now=None, + read_receipt=None, + is_notification=False, + inline_images=None, + template=None, + args=None, + header=None, + print_letterhead=False, + with_container=False, + email_read_tracker_url=None, +) -> Optional["EmailQueue"]: + """Send email using user's default **Email Account** or global default **Email Account**. + + + :param recipients: List of recipients. + :param sender: Email sender. Default is current user or default outgoing account. + :param subject: Email Subject. + :param message: (or `content`) Email Content. + :param as_markdown: Convert content markdown to HTML. + :param delayed: Send via scheduled email sender **Email Queue**. Don't send immediately. Default is true + :param send_priority: Priority for Email Queue, default 1. + :param reference_doctype: (or `doctype`) Append as communication to this DocType. + :param reference_name: (or `name`) Append as communication to this document name. + :param unsubscribe_method: Unsubscribe url with options email, doctype, name. e.g. `/api/method/unsubscribe` + :param unsubscribe_params: Unsubscribe paramaters to be loaded on the unsubscribe_method [optional] (dict). + :param attachments: List of attachments. + :param reply_to: Reply-To Email Address. + :param message_id: Used for threading. If a reply is received to this email, Message-Id is sent back as In-Reply-To in received email. + :param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To. + :param send_after: Send after the given datetime. + :param expose_recipients: Display all recipients in the footer message - "This email was sent to" + :param communication: Communication link to be set in Email Queue record + :param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id + :param template: Name of html template from templates/emails folder + :param args: Arguments for rendering the template + :param header: Append header in email + :param with_container: Wraps email inside a styled container + """ + + if recipients is None: + recipients = [] + if cc is None: + cc = [] + if bcc is None: + bcc = [] + + text_content = None + if template: + message, text_content = get_email_from_template(template, args) + + message = content or message + + if as_markdown: + from xhiveframework.utils import md_to_html + + message = md_to_html(message) + + if not delayed: + now = True + + from xhiveframework.email.doctype.email_queue.email_queue import QueueBuilder + + builder = QueueBuilder( + recipients=recipients, + sender=sender, + subject=subject, + message=message, + text_content=text_content, + reference_doctype=doctype or reference_doctype, + reference_name=name or reference_name, + add_unsubscribe_link=add_unsubscribe_link, + unsubscribe_method=unsubscribe_method, + unsubscribe_params=unsubscribe_params, + unsubscribe_message=unsubscribe_message, + attachments=attachments, + reply_to=reply_to, + cc=cc, + bcc=bcc, + message_id=message_id, + in_reply_to=in_reply_to, + send_after=send_after, + expose_recipients=expose_recipients, + send_priority=send_priority, + queue_separately=queue_separately, + communication=communication, + read_receipt=read_receipt, + is_notification=is_notification, + inline_images=inline_images, + header=header, + print_letterhead=print_letterhead, + with_container=with_container, + email_read_tracker_url=email_read_tracker_url, + ) + + # build email queue and send the email if send_now is True. + return builder.process(send_now=now) + + +whitelisted = [] +guest_methods = [] +xss_safe_methods = [] +allowed_http_methods_for_whitelisted_func = {} + + +def whitelist(allow_guest=False, xss_safe=False, methods=None): + """ + Decorator for whitelisting a function and making it accessible via HTTP. + Standard request will be `/api/method/[path.to.method]` + + :param allow_guest: Allow non logged-in user to access this method. + :param methods: Allowed http method to access the method. + + Use as: + + @xhiveframework.whitelist() + def myfunc(param1, param2): + pass + """ + + if not methods: + methods = ["GET", "POST", "PUT", "DELETE"] + + def innerfn(fn): + from xhiveframework.utils.typing_validations import validate_argument_types + + global whitelisted, guest_methods, xss_safe_methods, allowed_http_methods_for_whitelisted_func + + # validate argument types only if request is present + in_request_or_test = lambda: getattr(local, "request", None) or local.flags.in_test # noqa: E731 + + # get function from the unbound / bound method + # this is needed because functions can be compared, but not methods + method = None + if hasattr(fn, "__func__"): + method = validate_argument_types(fn, apply_condition=in_request_or_test) + fn = method.__func__ + else: + fn = validate_argument_types(fn, apply_condition=in_request_or_test) + + whitelisted.append(fn) + allowed_http_methods_for_whitelisted_func[fn] = methods + + if allow_guest: + guest_methods.append(fn) + + if xss_safe: + xss_safe_methods.append(fn) + + return method or fn + + return innerfn + + +def is_whitelisted(method): + from xhiveframework.utils import sanitize_html + + is_guest = session["user"] == "Guest" + if method not in whitelisted or is_guest and method not in guest_methods: + summary = _("You are not permitted to access this resource.") + detail = _("Function {0} is not whitelisted.").format(bold(f"{method.__module__}.{method.__name__}")) + msg = f"
{summary}{detail}
" + throw(msg, PermissionError, title="Method Not Allowed") + + if is_guest and method not in xss_safe_methods: + # strictly sanitize form_dict + # escapes html characters like <> except for predefined tags like a, b, ul etc. + for key, value in form_dict.items(): + if isinstance(value, str): + form_dict[key] = sanitize_html(value) + + +def read_only(): + def innfn(fn): + @functools.wraps(fn) + def wrapper_fn(*args, **kwargs): + # xhiveframework.read_only could be called from nested functions, in such cases don't swap the + # connection again. + switched_connection = False + if conf.read_from_replica: + switched_connection = connect_replica() + + try: + retval = fn(*args, **get_newargs(fn, kwargs)) + finally: + if switched_connection and local and hasattr(local, "primary_db"): + local.db.close() + local.db = local.primary_db + + return retval + + return wrapper_fn + + return innfn + + +def write_only(): + # if replica connection exists, we have to replace it momentarily with the primary connection + def innfn(fn): + def wrapper_fn(*args, **kwargs): + primary_db = getattr(local, "primary_db", None) + replica_db = getattr(local, "replica_db", None) + in_read_only = getattr(local, "db", None) != primary_db + + # switch to primary connection + if in_read_only and primary_db: + local.db = local.primary_db + + try: + retval = fn(*args, **get_newargs(fn, kwargs)) + finally: + # switch back to replica connection + if in_read_only and replica_db: + local.db = replica_db + + return retval + + return wrapper_fn + + return innfn + + +def only_for(roles: list[str] | tuple[str] | str, message=False): + """ + Raises `xhiveframework.PermissionError` if the user does not have any of the permitted roles. + + :param roles: Permitted role(s) + """ + + if local.flags.in_test or local.session.user == "Administrator": + return + + if isinstance(roles, str): + roles = (roles,) + + if set(roles).isdisjoint(get_roles()): + if not message: + raise PermissionError + + throw( + _("This action is only allowed for {}").format( + ", ".join(bold(_(role)) for role in roles), + ), + PermissionError, + _("Not Permitted"), + ) + + +def get_domain_data(module): + try: + domain_data = get_hooks("domains") + if module in domain_data: + return _dict(get_attr(get_hooks("domains")[module][0] + ".data")) + else: + return _dict() + except ImportError: + if local.flags.in_test: + return _dict() + else: + raise + + +def clear_cache(user: str | None = None, doctype: str | None = None): + """Clear **User**, **DocType** or global cache. + + :param user: If user is given, only user cache is cleared. + :param doctype: If doctype is given, only DocType cache is cleared.""" + import xhiveframework.cache_manager + import xhiveframework.utils.caching + from xhiveframework.website.router import clear_routing_cache + + if doctype: + xhiveframework.cache_manager.clear_doctype_cache(doctype) + reset_metadata_version() + elif user: + xhiveframework.cache_manager.clear_user_cache(user) + else: # everything + # Delete ALL keys associated with this site. + xhiveframework.cache.delete_keys("") + reset_metadata_version() + local.cache = {} + local.new_doc_templates = {} + + for fn in get_hooks("clear_cache"): + get_attr(fn)() + + xhiveframework.utils.caching._SITE_CACHE.clear() + local.role_permissions = {} + if hasattr(local, "request_cache"): + local.request_cache.clear() + if hasattr(local, "system_settings"): + del local.system_settings + if hasattr(local, "website_settings"): + del local.website_settings + + clear_routing_cache() + + +def only_has_select_perm(doctype, user=None, ignore_permissions=False): + if ignore_permissions: + return False + + from xhiveframework.permissions import get_role_permissions + + user = user or local.session.user + permissions = get_role_permissions(doctype, user=user) + + return permissions.get("select") and not permissions.get("read") + + +def has_permission( + doctype=None, + ptype="read", + doc=None, + user=None, + throw=False, + *, + parent_doctype=None, + debug=False, +): + """ + Returns True if the user has permission `ptype` for given `doctype` or `doc` + Raises `xhiveframework.PermissionError` if user isn't permitted and `throw` is truthy + + :param doctype: DocType for which permission is to be check. + :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`. + :param doc: [optional] Checks User permissions for given doc. + :param user: [optional] Check for given user. Default: current user. + :param parent_doctype: Required when checking permission for a child DocType (unless doc is specified). + """ + import xhiveframework.permissions + + if not doctype and doc: + doctype = doc.doctype + + out = xhiveframework.permissions.has_permission( + doctype, + ptype, + doc=doc, + user=user, + raise_exception=throw, + parent_doctype=parent_doctype, + debug=debug, + ) + + if throw and not out: + document_label = f"{_(doctype)} {doc if isinstance(doc, str) else doc.name}" if doc else _(doctype) + xhiveframework.flags.error_message = _("No permission for {0}").format(document_label) + raise xhiveframework.PermissionError + + return out + + +def has_website_permission(doc=None, ptype="read", user=None, verbose=False, doctype=None): + """Raises `xhiveframework.PermissionError` if not permitted. + + :param doctype: DocType for which permission is to be check. + :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`. + :param doc: Checks User permissions for given doc. + :param user: [optional] Check for given user. Default: current user.""" + + if not user: + user = session.user + + if doc: + if isinstance(doc, str): + doc = get_doc(doctype, doc) + + doctype = doc.doctype + + if doc.flags.ignore_permissions: + return True + + # check permission in controller + if hasattr(doc, "has_website_permission"): + return doc.has_website_permission(ptype, user, verbose=verbose) + + hooks = (get_hooks("has_website_permission") or {}).get(doctype, []) + if hooks: + for method in hooks: + result = call(method, doc=doc, ptype=ptype, user=user, verbose=verbose) + # if even a single permission check is Falsy + if not result: + return False + + # else it is Truthy + return True + + else: + return False + + +def is_table(doctype: str) -> bool: + """Returns True if `istable` property (indicating child Table) is set for given DocType.""" + + def get_tables(): + return db.get_values("DocType", filters={"istable": 1}, order_by=None, pluck=True) + + tables = cache.get_value("is_table", get_tables) + return doctype in tables + + +def get_precision( + doctype: str, fieldname: str, currency: str | None = None, doc: Optional["Document"] = None +) -> int: + """Get precision for a given field""" + from xhiveframework.model.meta import get_field_precision + + return get_field_precision(get_meta(doctype).get_field(fieldname), doc, currency) + + +def generate_hash(txt: str | None = None, length: int = 56) -> str: + """Generate random hash using best available randomness source.""" + import math + import secrets + + if not length: + length = 56 + + return secrets.token_hex(math.ceil(length / 2))[:length] + + +def reset_metadata_version(): + """Reset `metadata_version` (Client (Javascript) build ID) hash.""" + v = generate_hash() + cache.set_value("metadata_version", v) + return v + + +def new_doc( + doctype: str, + *, + parent_doc: Optional["Document"] = None, + parentfield: str | None = None, + as_dict: bool = False, + **kwargs, +) -> "Document": + """Returns a new document of the given DocType with defaults set. + + :param doctype: DocType of the new document. + :param parent_doc: [optional] add to parent document. + :param parentfield: [optional] add against this `parentfield`. + :param as_dict: [optional] return as dictionary instead of Document. + :param kwargs: [optional] You can specify fields as field=value pairs in function call. + """ + + from xhiveframework.model.create_new import get_new_doc + + new_doc = get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict) + + return new_doc.update(kwargs) + + +def set_value(doctype, docname, fieldname, value=None): + """Set document value. Calls `xhiveframework.client.set_value`""" + import xhiveframework.client + + return xhiveframework.client.set_value(doctype, docname, fieldname, value) + + +def get_cached_doc(*args, **kwargs) -> "Document": + if (key := can_cache_doc(args)) and (doc := cache.get_value(key)): + return doc + + # Not found in cache, fetch from DB + doc = get_doc(*args, **kwargs) + + # Store in cache + if not key: + key = get_document_cache_key(doc.doctype, doc.name) + + _set_document_in_cache(key, doc) + + return doc + + +def _set_document_in_cache(key: str, doc: "Document") -> None: + cache.set_value(key, doc) + + +def can_cache_doc(args) -> str | None: + """ + Determine if document should be cached based on get_doc params. + Returns cache key if doc can be cached, None otherwise. + """ + + if not args: + return + + doctype = args[0] + name = doctype if len(args) == 1 or args[1] is None else args[1] + + # Only cache if both doctype and name are strings + if isinstance(doctype, str) and isinstance(name, str): + return get_document_cache_key(doctype, name) + + +def get_document_cache_key(doctype: str, name: str): + return f"document_cache::{doctype}::{name}" + + +def clear_document_cache(doctype: str, name: str | None = None) -> None: + def clear_in_redis(): + if name is not None: + cache.delete_value(get_document_cache_key(doctype, name)) + else: + cache.delete_keys(get_document_cache_key(doctype, "")) + + clear_in_redis() + if hasattr(db, "after_commit"): + db.after_commit.add(clear_in_redis) + db.after_rollback.add(clear_in_redis) + + if doctype == "System Settings" and hasattr(local, "system_settings"): + delattr(local, "system_settings") + + if doctype == "Website Settings" and hasattr(local, "website_settings"): + delattr(local, "website_settings") + + +def get_cached_value(doctype: str, name: str, fieldname: str = "name", as_dict: bool = False) -> Any: + try: + doc = get_cached_doc(doctype, name) + except DoesNotExistError: + clear_last_message() + return + + if isinstance(fieldname, str): + if as_dict: + throw("Cannot make dict for single fieldname") + return doc.get(fieldname) + + values = [doc.get(f) for f in fieldname] + if as_dict: + return _dict(zip(fieldname, values, strict=False)) + return values + + +_SingleDocument: TypeAlias = "Document" +_NewDocument: TypeAlias = "Document" + + +@overload +def get_doc(document: "Document", /) -> "Document": + pass + + +@overload +def get_doc(doctype: str, /) -> _SingleDocument: + """Retrieve Single DocType from DB, doctype must be positional argument.""" + pass + + +@overload +def get_doc(doctype: str, name: str, /, *, for_update: bool | None = None) -> "Document": + """Retrieve DocType from DB, doctype and name must be positional argument.""" + pass + + +@overload +def get_doc(**kwargs: dict) -> "_NewDocument": + """Initialize document from kwargs. + Not recommended. Use `xhiveframework.new_doc` instead.""" + pass + + +@overload +def get_doc(documentdict: dict) -> "_NewDocument": + """Create document from dict. + Not recommended. Use `xhiveframework.new_doc` instead.""" + pass + + +def get_doc(*args, **kwargs): + """Return a `xhiveframework.model.document.Document` object of the given type and name. + + :param arg1: DocType name as string **or** document JSON. + :param arg2: [optional] Document name as string. + + Examples: + + # insert a new document + todo = xhiveframework.get_doc({"doctype":"ToDo", "description": "test"}) + todo.insert() + + # open an existing document + todo = xhiveframework.get_doc("ToDo", "TD0001") + + """ + import xhiveframework.model.document + + doc = xhiveframework.model.document.get_doc(*args, **kwargs) + + # Replace cache if stale one exists + if not kwargs.get("for_update") and (key := can_cache_doc(args)) and cache.exists(key): + _set_document_in_cache(key, doc) + + return doc + + +def get_last_doc(doctype, filters=None, order_by="creation desc", *, for_update=False): + """Get last created document of this type.""" + d = get_all(doctype, filters=filters, limit_page_length=1, order_by=order_by, pluck="name") + if d: + return get_doc(doctype, d[0], for_update=for_update) + else: + raise DoesNotExistError + + +def get_single(doctype): + """Return a `xhiveframework.model.document.Document` object of the given Single doctype.""" + return get_doc(doctype, doctype) + + +def get_meta(doctype, cached=True): + """Get `xhiveframework.model.meta.Meta` instance of given doctype name.""" + import xhiveframework.model.meta + + return xhiveframework.model.meta.get_meta(doctype, cached=cached) + + +def get_meta_module(doctype): + import xhiveframework.modules + + return xhiveframework.modules.load_doctype_module(doctype) + + +def delete_doc( + doctype: str | None = None, + name: str | None = None, + force: bool = False, + ignore_doctypes: list[str] | None = None, + for_reload: bool = False, + ignore_permissions: bool = False, + flags: None = None, + ignore_on_trash: bool = False, + ignore_missing: bool = True, + delete_permanently: bool = False, +): + """Delete a document. Calls `xhiveframework.model.delete_doc.delete_doc`. + + :param doctype: DocType of document to be delete. + :param name: Name of document to be delete. + :param force: Allow even if document is linked. Warning: This may lead to data integrity errors. + :param ignore_doctypes: Ignore if child table is one of these. + :param for_reload: Call `before_reload` trigger before deleting. + :param ignore_permissions: Ignore user permissions. + :param delete_permanently: Do not create a Deleted Document for the document.""" + import xhiveframework.model.delete_doc + + return xhiveframework.model.delete_doc.delete_doc( + doctype, + name, + force, + ignore_doctypes, + for_reload, + ignore_permissions, + flags, + ignore_on_trash, + ignore_missing, + delete_permanently, + ) + + +def delete_doc_if_exists(doctype, name, force=0): + """Delete document if exists.""" + delete_doc(doctype, name, force=force, ignore_missing=True) + + +def reload_doctype(doctype, force=False, reset_permissions=False): + """Reload DocType from model (`[module]/[doctype]/[name]/[name].json`) files.""" + reload_doc( + scrub(db.get_value("DocType", doctype, "module")), + "doctype", + scrub(doctype), + force=force, + reset_permissions=reset_permissions, + ) + + +def reload_doc( + module: str, + dt: str | None = None, + dn: str | None = None, + force: bool = False, + reset_permissions: bool = False, +): + """Reload Document from model (`[module]/[doctype]/[name]/[name].json`) files. + + :param module: Module name. + :param dt: DocType name. + :param dn: Document name. + :param force: Reload even if `modified` timestamp matches. + """ + + import xhiveframework.modules + + return xhiveframework.modules.reload_doc(module, dt, dn, force=force, reset_permissions=reset_permissions) + + +@whitelist(methods=["POST", "PUT"]) +def rename_doc( + doctype: str, + old: str, + new: str, + force: bool = False, + merge: bool = False, + *, + ignore_if_exists: bool = False, + show_alert: bool = True, + rebuild_search: bool = True, +) -> str: + """ + Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type "Link" + + Calls `xhiveframework.model.rename_doc.rename_doc` + """ + + from xhiveframework.model.rename_doc import rename_doc + + return rename_doc( + doctype=doctype, + old=old, + new=new, + force=force, + merge=merge, + ignore_if_exists=ignore_if_exists, + show_alert=show_alert, + rebuild_search=rebuild_search, + ) + + +def get_module(modulename): + """Returns a module object for given Python module name using `importlib.import_module`.""" + return importlib.import_module(modulename) + + +def scrub(txt: str) -> str: + """Returns sluggified string. e.g. `Sales Order` becomes `sales_order`.""" + return cstr(txt).replace(" ", "_").replace("-", "_").lower() + + +def unscrub(txt: str) -> str: + """Returns titlified string. e.g. `sales_order` becomes `Sales Order`.""" + return txt.replace("_", " ").replace("-", " ").title() + + +def get_module_path(module, *joins): + """Get the path of the given module name. + + :param module: Module name. + :param *joins: Join additional path elements using `os.path.join`.""" + from xhiveframework.modules.utils import get_module_app + + app = get_module_app(module) + return get_pymodule_path(app + "." + scrub(module), *joins) + + +def get_app_path(app_name, *joins): + """Return path of given app. + + :param app: App name. + :param *joins: Join additional path elements using `os.path.join`.""" + return get_pymodule_path(app_name, *joins) + + +def get_app_source_path(app_name, *joins): + """Return source path of given app. + + :param app: App name. + :param *joins: Join additional path elements using `os.path.join`.""" + return get_app_path(app_name, "..", *joins) + + +def get_site_path(*joins): + """Return path of current site. + + :param *joins: Join additional path elements using `os.path.join`.""" + from os.path import join + + return join(local.site_path, *joins) + + +def get_pymodule_path(modulename, *joins): + """Return path of given Python module name. + + :param modulename: Python module name. + :param *joins: Join additional path elements using `os.path.join`.""" + from os.path import abspath, dirname, join + + if "public" not in joins: + joins = [scrub(part) for part in joins] + + return abspath(join(dirname(get_module(scrub(modulename)).__file__ or ""), *joins)) + + +def get_module_list(app_name): + """Get list of modules for given all via `app/modules.txt`.""" + return get_file_items(get_app_path(app_name, "modules.txt")) + + +def get_all_apps(with_internal_apps=True, sites_path=None): + """Get list of all apps via `sites/apps.txt`.""" + if not sites_path: + sites_path = local.sites_path + + apps = get_file_items(os.path.join(sites_path, "apps.txt"), raise_not_found=True) + + if with_internal_apps: + for app in get_file_items(os.path.join(local.site_path, "apps.txt")): + if app not in apps: + apps.append(app) + + if "xhiveframework" in apps: + apps.remove("xhiveframework") + apps.insert(0, "xhiveframework") + + return apps + + +@request_cache +def get_installed_apps(*, _ensure_on_bench=False) -> list[str]: + """ + Get list of installed apps in current site. + + :param _ensure_on_bench: Only return apps that are present on bench. + """ + if getattr(flags, "in_install_db", True): + return [] + + if not db: + connect() + + installed = json.loads(db.get_global("installed_apps") or "[]") + + if _ensure_on_bench: + all_apps = cache.get_value("all_apps", get_all_apps) + installed = [app for app in installed if app in all_apps] + + return installed + + +def get_doc_hooks(): + """Returns hooked methods for given doc. It will expand the dict tuple if required.""" + if not hasattr(local, "doc_events_hooks"): + hooks = get_hooks("doc_events", {}) + out = {} + for key, value in hooks.items(): + if isinstance(key, tuple): + for doctype in key: + append_hook(out, doctype, value) + else: + append_hook(out, key, value) + + local.doc_events_hooks = out + + return local.doc_events_hooks + + +@request_cache +def _load_app_hooks(app_name: str | None = None): + import types + + hooks = {} + apps = [app_name] if app_name else get_installed_apps(_ensure_on_bench=True) + + for app in apps: + try: + app_hooks = get_module(f"{app}.hooks") + except ImportError as e: + if local.flags.in_install_app: + # if app is not installed while restoring + # ignore it + pass + print(f'Could not find app "{app}": \n{e}') + raise + + def _is_valid_hook(obj): + return not isinstance(obj, types.ModuleType | types.FunctionType | type) + + for key, value in inspect.getmembers(app_hooks, predicate=_is_valid_hook): + if not key.startswith("_"): + append_hook(hooks, key, value) + return hooks + + +def get_hooks( + hook: str | None = None, default: Any | None = "_KEEP_DEFAULT_LIST", app_name: str | None = None +) -> _dict: + """Get hooks via `app/hooks.py` + + :param hook: Name of the hook. Will gather all hooks for this name and return as a list. + :param default: Default if no hook found. + :param app_name: Filter by app.""" + + if app_name: + hooks = _dict(_load_app_hooks(app_name)) + else: + if conf.developer_mode: + hooks = _dict(_load_app_hooks()) + else: + hooks = _dict(cache.get_value("app_hooks", _load_app_hooks)) + + if hook: + return hooks.get(hook, ([] if default == "_KEEP_DEFAULT_LIST" else default)) + return hooks + + +def append_hook(target, key, value): + """appends a hook to the the target dict. + + If the hook key, exists, it will make it a key. + + If the hook value is a dict, like doc_events, it will + listify the values against the key. + """ + if isinstance(value, dict): + # dict? make a list of values against each key + target.setdefault(key, {}) + for inkey in value: + append_hook(target[key], inkey, value[inkey]) + else: + # make a list + target.setdefault(key, []) + if not isinstance(value, list): + value = [value] + target[key].extend(value) + + +def setup_module_map(include_all_apps: bool = True) -> None: + """ + Function to rebuild map of all modules + + :param: include_all_apps: Include all apps on bench, or just apps installed on the site. + :return: Nothing + """ + if include_all_apps: + local.app_modules = cache.get_value("app_modules") + local.module_app = cache.get_value("module_app") + else: + local.app_modules = cache.get_value("installed_app_modules") + local.module_app = cache.get_value("module_installed_app") + + if not (local.app_modules and local.module_app): + local.module_app, local.app_modules = {}, {} + if include_all_apps: + apps = get_all_apps(with_internal_apps=True) + else: + apps = get_installed_apps(_ensure_on_bench=True) + for app in apps: + local.app_modules.setdefault(app, []) + for module in get_module_list(app): + module = scrub(module) + if module in local.module_app: + print( + f"WARNING: module `{module}` found in apps `{local.module_app[module]}` and `{app}`" + ) + local.module_app[module] = app + local.app_modules[app].append(module) + + if include_all_apps: + cache.set_value("app_modules", local.app_modules) + cache.set_value("module_app", local.module_app) + else: + cache.set_value("installed_app_modules", local.app_modules) + cache.set_value("module_installed_app", local.module_app) + + +def get_file_items(path, raise_not_found=False, ignore_empty_lines=True): + """Returns items from text file as a list. Ignores empty lines.""" + import xhiveframework.utils + + content = read_file(path, raise_not_found=raise_not_found) + if content: + content = xhiveframework.utils.strip(content) + + return [ + p.strip() + for p in content.splitlines() + if (not ignore_empty_lines) or (p.strip() and not p.startswith("#")) + ] + else: + return [] + + +def get_file_json(path): + """Read a file and return parsed JSON object.""" + with open(path) as f: + return json.load(f) + + +def read_file(path, raise_not_found=False): + """Open a file and return its content as Unicode.""" + if isinstance(path, str): + path = path.encode("utf-8") + + if os.path.exists(path): + with open(path) as f: + return as_unicode(f.read()) + elif raise_not_found: + raise OSError(f"{path} Not Found") + else: + return None + + +def get_attr(method_string: str) -> Any: + """Get python method object from its name.""" + app_name = method_string.split(".", 1)[0] + if not local.flags.in_uninstall and not local.flags.in_install and app_name not in get_installed_apps(): + throw(_("App {0} is not installed").format(app_name), AppNotInstalledError) + + modulename = ".".join(method_string.split(".")[:-1]) + methodname = method_string.split(".")[-1] + return getattr(get_module(modulename), methodname) + + +def call(fn: str | Callable, *args, **kwargs): + """Call a function and match arguments.""" + if isinstance(fn, str): + fn = get_attr(fn) + + newargs = get_newargs(fn, kwargs) + + return fn(*args, **newargs) + + +def get_newargs(fn: Callable, kwargs: dict[str, Any]) -> dict[str, Any]: + """Remove any kwargs that are not supported by the function. + + Example: + >>> def fn(a=1, b=2): + ... pass + + >>> get_newargs(fn, {"a": 2, "c": 1}) + {"a": 2} + """ + + # if function has any **kwargs parameter that capture arbitrary keyword arguments + # Ref: https://docs.python.org/3/library/inspect.html#inspect.Parameter.kind + varkw_exist = False + + signature = inspect.signature(fn) + fnargs = list(signature.parameters) + + for param_name, parameter in signature.parameters.items(): + if parameter.kind == inspect.Parameter.VAR_KEYWORD: + varkw_exist = True + fnargs.remove(param_name) + break + + newargs = {} + for a in kwargs: + if (a in fnargs) or varkw_exist: + newargs[a] = kwargs.get(a) + + # WARNING: This behaviour is now part of business logic in places, never remove. + newargs.pop("ignore_permissions", None) + newargs.pop("flags", None) + + return newargs + + +def make_property_setter( + args, ignore_validate=False, validate_fields_for_doctype=True, is_system_generated=True +): + """Create a new **Property Setter** (for overriding DocType and DocField properties). + + If doctype is not specified, it will create a property setter for all fields with the + given fieldname""" + args = _dict(args) + if not args.doctype_or_field: + args.doctype_or_field = "DocField" + if not args.property_type: + args.property_type = ( + db.get_value("DocField", {"parent": "DocField", "fieldname": args.property}, "fieldtype") + or "Data" + ) + + if not args.doctype: + DocField_doctype = qb.DocType("DocField") + doctype_list = ( + qb.from_(DocField_doctype) + .select(DocField_doctype.parent) + .where(DocField_doctype.fieldname == args.fieldname) + .distinct() + ).run(pluck=True) + + else: + doctype_list = [args.doctype] + + for doctype in doctype_list: + if not args.property_type: + args.property_type = ( + db.get_value("DocField", {"parent": doctype, "fieldname": args.fieldname}, "fieldtype") + or "Data" + ) + + ps = get_doc( + { + "doctype": "Property Setter", + "doctype_or_field": args.doctype_or_field, + "doc_type": doctype, + "field_name": args.fieldname, + "row_name": args.row_name, + "property": args.property, + "value": args.value, + "property_type": args.property_type or "Data", + "is_system_generated": is_system_generated, + "__islocal": 1, + } + ) + ps.flags.ignore_validate = ignore_validate + ps.flags.validate_fields_for_doctype = validate_fields_for_doctype + ps.validate_fieldtype_change() + ps.insert() + + +def import_doc(path): + """Import a file using Data Import.""" + from xhiveframework.core.doctype.data_import.data_import import import_doc + + import_doc(path) + + +def copy_doc(doc: "Document", ignore_no_copy: bool = True) -> "Document": + """No_copy fields also get copied.""" + import copy + + def remove_no_copy_fields(d): + for df in d.meta.get("fields", {"no_copy": 1}): + if hasattr(d, df.fieldname): + d.set(df.fieldname, None) + + fields_to_clear = ["name", "owner", "creation", "modified", "modified_by"] + + if not local.flags.in_test: + fields_to_clear.append("docstatus") + + if not isinstance(doc, dict): + d = doc.as_dict() + else: + d = doc + + newdoc = get_doc(copy.deepcopy(d)) + newdoc.set("__islocal", 1) + for fieldname in [*fields_to_clear, "amended_from", "amendment_date"]: + newdoc.set(fieldname, None) + + if not ignore_no_copy: + remove_no_copy_fields(newdoc) + + for _i, d in enumerate(newdoc.get_all_children()): + d.set("__islocal", 1) + + for fieldname in fields_to_clear: + d.set(fieldname, None) + + if not ignore_no_copy: + remove_no_copy_fields(d) + + return newdoc + + +def respond_as_web_page( + title, + html, + success=None, + http_status_code=None, + context=None, + indicator_color=None, + primary_action="/", + primary_label=None, + fullpage=False, + width=None, + template="message", +): + """Send response as a web page with a message rather than JSON. Used to show permission errors etc. + + :param title: Page title and heading. + :param message: Message to be shown. + :param success: Alert message. + :param http_status_code: HTTP status code + :param context: web template context + :param indicator_color: color of indicator in title + :param primary_action: route on primary button (default is `/`) + :param primary_label: label on primary button (default is "Home") + :param fullpage: hide header / footer + :param width: Width of message in pixels + :param template: Optionally pass view template + """ + local.message_title = title + local.message = html + local.response["type"] = "page" + local.response["route"] = template + local.no_cache = 1 + + if http_status_code: + local.response["http_status_code"] = http_status_code + + if not context: + context = {} + + if not indicator_color: + if success: + indicator_color = "green" + elif http_status_code and http_status_code > 300: + indicator_color = "red" + else: + indicator_color = "blue" + + context["indicator_color"] = indicator_color + context["primary_label"] = primary_label + context["primary_action"] = primary_action + context["error_code"] = http_status_code + context["fullpage"] = fullpage + if width: + context["card_width"] = width + + local.response["context"] = context + + +def redirect(url): + """Raise a 301 redirect to url""" + from xhiveframework.exceptions import Redirect + + flags.redirect_location = url + raise Redirect + + +def redirect_to_message(title, html, http_status_code=None, context=None, indicator_color=None): + """Redirects to /message?id=random + Similar to respond_as_web_page, but used to 'redirect' and show message pages like success, failure, etc. with a detailed message + + :param title: Page title and heading. + :param message: Message to be shown. + :param http_status_code: HTTP status code. + + Example Usage: + xhiveframework.redirect_to_message(_('Thank you'), "

You will receive an email at test@example.com

") + + """ + + message_id = generate_hash(length=8) + message = {"context": context or {}, "http_status_code": http_status_code or 200} + message["context"].update({"header": title, "title": title, "message": html}) + + if indicator_color: + message["context"].update({"indicator_color": indicator_color}) + + cache.set_value(f"message_id:{message_id}", message, expires_in_sec=60) + location = f"/message?id={message_id}" + + if not getattr(local, "is_ajax", False): + local.response["type"] = "redirect" + local.response["location"] = location + + else: + return location + + +def build_match_conditions(doctype, as_condition=True): + """Return match (User permissions) for given doctype as list or SQL.""" + import xhiveframework.desk.reportview + + return xhiveframework.desk.reportview.build_match_conditions(doctype, as_condition=as_condition) + + +def get_list(doctype, *args, **kwargs): + """List database query via `xhiveframework.model.db_query`. Will also check for permissions. + + :param doctype: DocType on which query is to be made. + :param fields: List of fields or `*`. + :param filters: List of filters (see example). + :param order_by: Order By e.g. `modified desc`. + :param limit_start: Start results at record #. Default 0. + :param limit_page_length: No of records in the page. Default 20. + + Example usage: + + # simple dict filter + xhiveframework.get_list("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"}) + + # filter as a list of lists + xhiveframework.get_list("ToDo", fields="*", filters = [["modified", ">", "2014-01-01"]]) + """ + import xhiveframework.model.db_query + + return xhiveframework.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs) + + +def get_all(doctype, *args, **kwargs): + """List database query via `xhiveframework.model.db_query`. Will **not** check for permissions. + Parameters are same as `xhiveframework.get_list` + + :param doctype: DocType on which query is to be made. + :param fields: List of fields or `*`. Default is: `["name"]`. + :param filters: List of filters (see example). + :param order_by: Order By e.g. `modified desc`. + :param limit_start: Start results at record #. Default 0. + :param limit_page_length: No of records in the page. Default 20. + + Example usage: + + # simple dict filter + xhiveframework.get_all("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"}) + + # filter as a list of lists + xhiveframework.get_all("ToDo", fields=["*"], filters = [["modified", ">", "2014-01-01"]]) + """ + kwargs["ignore_permissions"] = True + if "limit_page_length" not in kwargs: + kwargs["limit_page_length"] = 0 + return get_list(doctype, *args, **kwargs) + + +def get_value(*args, **kwargs): + """Returns a document property or list of properties. + + Alias for `xhiveframework.db.get_value` + + :param doctype: DocType name. + :param filters: Filters like `{"x":"y"}` or name of the document. `None` if Single DocType. + :param fieldname: Column name. + :param ignore: Don't raise exception if table, column is missing. + :param as_dict: Return values as dict. + :param debug: Print query in error log. + """ + return db.get_value(*args, **kwargs) + + +def as_json(obj: dict | list, indent=1, separators=None, ensure_ascii=True) -> str: + from xhiveframework.utils.response import json_handler + + if separators is None: + separators = (",", ": ") + + try: + return json.dumps( + obj, + indent=indent, + sort_keys=True, + default=json_handler, + separators=separators, + ensure_ascii=ensure_ascii, + ) + except TypeError: + # this would break in case the keys are not all os "str" type - as defined in the JSON + # adding this to ensure keys are sorted (expected behaviour) + sorted_obj = dict(sorted(obj.items(), key=lambda kv: str(kv[0]))) + return json.dumps( + sorted_obj, + indent=indent, + default=json_handler, + separators=separators, + ensure_ascii=ensure_ascii, + ) + + +def are_emails_muted(): + return flags.mute_emails or cint(conf.get("mute_emails") or 0) or False + + +def get_test_records(doctype): + """Returns list of objects from `test_records.json` in the given doctype's folder.""" + from xhiveframework.modules import get_doctype_module, get_module_path + + path = os.path.join( + get_module_path(get_doctype_module(doctype)), "doctype", scrub(doctype), "test_records.json" + ) + if os.path.exists(path): + with open(path) as f: + return json.loads(f.read()) + else: + return [] + + +def format_value(*args, **kwargs): + """Format value with given field properties. + + :param value: Value to be formatted. + :param df: (Optional) DocField object with properties `fieldtype`, `options` etc.""" + import xhiveframework.utils.formatters + + return xhiveframework.utils.formatters.format_value(*args, **kwargs) + + +def format(*args, **kwargs): + """Format value with given field properties. + + :param value: Value to be formatted. + :param df: (Optional) DocField object with properties `fieldtype`, `options` etc.""" + import xhiveframework.utils.formatters + + return xhiveframework.utils.formatters.format_value(*args, **kwargs) + + +def get_print( + doctype=None, + name=None, + print_format=None, + style=None, + as_pdf=False, + doc=None, + output=None, + no_letterhead=0, + password=None, + pdf_options=None, + letterhead=None, +): + """Get Print Format for given document. + + :param doctype: DocType of document. + :param name: Name of document. + :param print_format: Print Format name. Default 'Standard', + :param style: Print Format style. + :param as_pdf: Return as PDF. Default False. + :param password: Password to encrypt the pdf with. Default None""" + from xhiveframework.utils.pdf import get_pdf + from xhiveframework.website.serve import get_response_without_exception_handling + + original_form_dict = copy.deepcopy(local.form_dict) + try: + local.form_dict.doctype = doctype + local.form_dict.name = name + local.form_dict.format = print_format + local.form_dict.style = style + local.form_dict.doc = doc + local.form_dict.no_letterhead = no_letterhead + local.form_dict.letterhead = letterhead + + pdf_options = pdf_options or {} + if password: + pdf_options["password"] = password + + response = get_response_without_exception_handling("printview", 200) + html = str(response.data, "utf-8") + finally: + local.form_dict = original_form_dict + + return get_pdf(html, options=pdf_options, output=output) if as_pdf else html + + +def attach_print( + doctype, + name, + file_name=None, + print_format=None, + style=None, + html=None, + doc=None, + lang=None, + print_letterhead=True, + password=None, + letterhead=None, +): + from xhiveframework.translate import print_language + from xhiveframework.utils import scrub_urls + from xhiveframework.utils.pdf import get_pdf + + print_settings = db.get_singles_dict("Print Settings") + + kwargs = dict( + print_format=print_format, + style=style, + doc=doc, + no_letterhead=not print_letterhead, + letterhead=letterhead, + password=password, + ) + + local.flags.ignore_print_permissions = True + + with print_language(lang or local.lang): + content = "" + if cint(print_settings.send_print_as_pdf): + ext = ".pdf" + kwargs["as_pdf"] = True + content = ( + get_pdf(html, options={"password": password} if password else None) + if html + else get_print(doctype, name, **kwargs) + ) + else: + ext = ".html" + content = html or scrub_urls(get_print(doctype, name, **kwargs)).encode("utf-8") + + local.flags.ignore_print_permissions = False + + if not file_name: + file_name = name + file_name = cstr(file_name).replace(" ", "").replace("/", "-") + ext + + return {"fname": file_name, "fcontent": content} + + +def publish_progress(*args, **kwargs): + """Show the user progress for a long request + + :param percent: Percent progress + :param title: Title + :param doctype: Optional, for document type + :param docname: Optional, for document name + :param description: Optional description + """ + import xhiveframework.realtime + + return xhiveframework.realtime.publish_progress(*args, **kwargs) + + +def publish_realtime(*args, **kwargs): + """Publish real-time updates + + :param event: Event name, like `task_progress` etc. + :param message: JSON message object. For async must contain `task_id` + :param room: Room in which to publish update (default entire site) + :param user: Transmit to user + :param doctype: Transmit to doctype, docname + :param docname: Transmit to doctype, docname + :param after_commit: (default False) will emit after current transaction is committed + """ + import xhiveframework.realtime + + return xhiveframework.realtime.publish_realtime(*args, **kwargs) + + +def local_cache(namespace, key, generator, regenerate_if_none=False): + """A key value store for caching within a request + + :param namespace: xhiveframework.local.cache[namespace] + :param key: xhiveframework.local.cache[namespace][key] used to retrieve value + :param generator: method to generate a value if not found in store + + """ + if namespace not in local.cache: + local.cache[namespace] = {} + + if key not in local.cache[namespace]: + local.cache[namespace][key] = generator() + + elif local.cache[namespace][key] is None and regenerate_if_none: + # if key exists but the previous result was None + local.cache[namespace][key] = generator() + + return local.cache[namespace][key] + + +def enqueue(*args, **kwargs): + """ + Enqueue method to be executed using a background worker + + :param method: method string or method object + :param queue: (optional) should be either long, default or short + :param timeout: (optional) should be set according to the functions + :param event: this is passed to enable clearing of jobs from queues + :param is_async: (optional) if is_async=False, the method is executed immediately, else via a worker + :param job_name: (optional) can be used to name an enqueue call, which can be used to prevent duplicate calls + :param kwargs: keyword arguments to be passed to the method + """ + import xhiveframework.utils.background_jobs + + return xhiveframework.utils.background_jobs.enqueue(*args, **kwargs) + + +def task(**task_kwargs): + def decorator_task(f): + f.enqueue = lambda **fun_kwargs: enqueue(f, **task_kwargs, **fun_kwargs) + return f + + return decorator_task + + +def enqueue_doc(*args, **kwargs): + """ + Enqueue method to be executed using a background worker + + :param doctype: DocType of the document on which you want to run the event + :param name: Name of the document on which you want to run the event + :param method: method string or method object + :param queue: (optional) should be either long, default or short + :param timeout: (optional) should be set according to the functions + :param kwargs: keyword arguments to be passed to the method + """ + import xhiveframework.utils.background_jobs + + return xhiveframework.utils.background_jobs.enqueue_doc(*args, **kwargs) + + +def get_doctype_app(doctype): + def _get_doctype_app(): + doctype_module = local.db.get_value("DocType", doctype, "module") + return local.module_app[scrub(doctype_module)] + + return local_cache("doctype_app", doctype, generator=_get_doctype_app) + + +loggers = {} +log_level = None + + +def logger(module=None, with_more_info=False, allow_site=True, filter=None, max_size=100_000, file_count=20): + """Returns a python logger that uses StreamHandler""" + from xhiveframework.utils.logger import get_logger + + return get_logger( + module=module, + with_more_info=with_more_info, + allow_site=allow_site, + filter=filter, + max_size=max_size, + file_count=file_count, + ) + + +def get_desk_link(doctype, name): + html = '{doctype_local} {name}' + return html.format(doctype=doctype, name=name, doctype_local=_(doctype)) + + +def bold(text): + return f"{text}" + + +def safe_eval(code, eval_globals=None, eval_locals=None): + """A safer `eval`""" + + from xhiveframework.utils.safe_exec import safe_eval + + return safe_eval(code, eval_globals, eval_locals) + + +def get_website_settings(key): + if not hasattr(local, "website_settings"): + try: + local.website_settings = get_cached_doc("Website Settings") + except DoesNotExistError: + clear_last_message() + return + + return local.website_settings.get(key) + + +def get_system_settings(key): + if not hasattr(local, "system_settings"): + try: + local.system_settings = get_cached_doc("System Settings") + except DoesNotExistError: # possible during new install + clear_last_message() + return + + return local.system_settings.get(key) + + +def get_active_domains(): + from xhiveframework.core.doctype.domain_settings.domain_settings import get_active_domains + + return get_active_domains() + + +def get_version(doctype, name, limit=None, head=False, raise_err=True): + """ + Returns a list of version information of a given DocType. + + Note: Applicable only if DocType has changes tracked. + + Example + >>> xhiveframework.get_version("User", "foobar@gmail.com") + >>> + [ + { + "version": [version.data], # Refer Version DocType get_diff method and data attribute + "user": "admin@gmail.com", # User that created this version + "creation": # Creation timestamp of that object. + } + ] + """ + meta = get_meta(doctype) + if meta.track_changes: + names = get_all( + "Version", + filters={ + "ref_doctype": doctype, + "docname": name, + "order_by": "creation" if head else None, + "limit": limit, + }, + as_list=1, + ) + + from xhiveframework.utils import dictify, safe_json_loads, squashify + + versions = [] + + for name in names: + name = squashify(name) + doc = get_doc("Version", name) + + data = doc.data + data = safe_json_loads(data) + data = dictify(dict(version=data, user=doc.owner, creation=doc.creation)) + + versions.append(data) + + return versions + else: + if raise_err: + raise ValueError(_("{0} has no versions tracked.").format(doctype)) + + +@whitelist(allow_guest=True) +def ping(): + return "pong" + + +def safe_encode(param, encoding="utf-8"): + try: + param = param.encode(encoding) + except Exception: + pass + return param + + +def safe_decode(param, encoding="utf-8"): + try: + param = param.decode(encoding) + except Exception: + pass + return param + + +def parse_json(val): + from xhiveframework.utils import parse_json + + return parse_json(val) + + +def mock(type, size=1, locale="en"): + import faker + + results = [] + fake = faker.Faker(locale) + if type not in dir(fake): + raise ValueError("Not a valid mock type.") + else: + for _i in range(size): + data = getattr(fake, type)() + results.append(data) + + from xhiveframework.utils import squashify + + return squashify(results) + + +def validate_and_sanitize_search_inputs(fn): + @functools.wraps(fn) + def wrapper(*args, **kwargs): + from xhiveframework.desk.search import sanitize_searchfield + + kwargs.update(dict(zip(fn.__code__.co_varnames, args, strict=False))) + sanitize_searchfield(kwargs["searchfield"]) + kwargs["start"] = cint(kwargs["start"]) + kwargs["page_len"] = cint(kwargs["page_len"]) + + if kwargs["doctype"] and not db.exists("DocType", kwargs["doctype"]): + return [] + + return fn(**kwargs) + + return wrapper + + +def _register_fault_handler(): + faulthandler.register(signal.SIGUSR1) + + +from xhiveframework.utils.error import log_error + +if _tune_gc: + # generational GC gets triggered after certain allocs (g0) which is 700 by default. + # This number is quite small for xhiveframework where a single query can potentially create 700+ + # objects easily. + # Bump this number higher, this will make GC less aggressive but that improves performance of + # everything else. + g0, g1, g2 = gc.get_threshold() # defaults are 700, 10, 10. + gc.set_threshold(g0 * 10, g1 * 2, g2 * 2) + +# Remove references to pattern that are pre-compiled and loaded to global scopes. +re.purge() diff --git a/xhiveframework/api/__init__.py b/xhiveframework/api/__init__.py new file mode 100644 index 0000000..52a1fdd --- /dev/null +++ b/xhiveframework/api/__init__.py @@ -0,0 +1,80 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from enum import Enum + +from werkzeug.exceptions import NotFound +from werkzeug.routing import Map, Submount +from werkzeug.wrappers import Request, Response + +import xhiveframework +import xhiveframework.client +from xhiveframework import _ +from xhiveframework.utils.response import build_response + + +class ApiVersion(str, Enum): + V1 = "v1" + V2 = "v2" + + +def handle(request: Request): + """ + Entry point for `/api` methods. + + APIs are versioned using second part of path. + v1 -> `/api/v1/*` + v2 -> `/api/v2/*` + + Different versions have different specification but broadly following things are supported: + + - `/api/method/{methodname}` will call a whitelisted method + - `/api/resource/{doctype}` will query a table + examples: + - `?fields=["name", "owner"]` + - `?filters=[["Task", "name", "like", "%005"]]` + - `?limit_start=0` + - `?limit_page_length=20` + - `/api/resource/{doctype}/{name}` will point to a resource + `GET` will return document + `POST` will insert + `PUT` will update + `DELETE` will delete + """ + + try: + endpoint, arguments = API_URL_MAP.bind_to_environ(request.environ).match() + except NotFound: # Wrap 404 - backward compatiblity + raise xhiveframework.DoesNotExistError + + data = endpoint(**arguments) + if isinstance(data, Response): + return data + + if data is not None: + xhiveframework.response["data"] = data + return build_response("json") + + +# Merge all API version routing rules +from xhiveframework.api.v1 import url_rules as v1_rules +from xhiveframework.api.v2 import url_rules as v2_rules + +API_URL_MAP = Map( + [ + # V1 routes + Submount("/api", v1_rules), + Submount(f"/api/{ApiVersion.V1.value}", v1_rules), + Submount(f"/api/{ApiVersion.V2.value}", v2_rules), + ], + strict_slashes=False, # Allows skipping trailing slashes + merge_slashes=False, +) + + +def get_api_version() -> ApiVersion | None: + if not xhiveframework.request: + return + + if xhiveframework.request.path.startswith(f"/api/{ApiVersion.V2.value}"): + return ApiVersion.V2 + return ApiVersion.V1 diff --git a/xhiveframework/api/utils.py b/xhiveframework/api/utils.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/api/v1.py b/xhiveframework/api/v1.py new file mode 100644 index 0000000..235d490 --- /dev/null +++ b/xhiveframework/api/v1.py @@ -0,0 +1,118 @@ +import json + +from werkzeug.routing import Rule + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils.data import sbool + + +def document_list(doctype: str): + if xhiveframework.form_dict.get("fields"): + xhiveframework.form_dict["fields"] = json.loads(xhiveframework.form_dict["fields"]) + + # set limit of records for xhiveframework.get_list + xhiveframework.form_dict.setdefault( + "limit_page_length", + xhiveframework.form_dict.limit or xhiveframework.form_dict.limit_page_length or 20, + ) + + # convert strings to native types - only as_dict and debug accept bool + for param in ["as_dict", "debug"]: + param_val = xhiveframework.form_dict.get(param) + if param_val is not None: + xhiveframework.form_dict[param] = sbool(param_val) + + # evaluate xhiveframework.get_list + return xhiveframework.call(xhiveframework.client.get_list, doctype, **xhiveframework.form_dict) + + +def handle_rpc_call(method: str): + import xhiveframework.handler + + method = method.split("/")[0] # for backward compatiblity + + xhiveframework.form_dict.cmd = method + return xhiveframework.handler.handle() + + +def create_doc(doctype: str): + data = get_request_form_data() + data.pop("doctype", None) + return xhiveframework.new_doc(doctype, **data).insert() + + +def update_doc(doctype: str, name: str): + data = get_request_form_data() + + doc = xhiveframework.get_doc(doctype, name, for_update=True) + if "flags" in data: + del data["flags"] + + doc.update(data) + doc.save() + + # check for child table doctype + if doc.get("parenttype"): + xhiveframework.get_doc(doc.parenttype, doc.parent).save() + + return doc + + +def delete_doc(doctype: str, name: str): + # TODO: child doc handling + xhiveframework.delete_doc(doctype, name, ignore_missing=False) + xhiveframework.response.http_status_code = 202 + return "ok" + + +def read_doc(doctype: str, name: str): + # Backward compatiblity + if "run_method" in xhiveframework.form_dict: + return execute_doc_method(doctype, name) + + doc = xhiveframework.get_doc(doctype, name) + if not doc.has_permission("read"): + raise xhiveframework.PermissionError + doc.apply_fieldlevel_read_permissions() + return doc + + +def execute_doc_method(doctype: str, name: str, method: str | None = None): + method = method or xhiveframework.form_dict.pop("run_method") + doc = xhiveframework.get_doc(doctype, name) + doc.is_whitelisted(method) + + if xhiveframework.request.method == "GET": + if not doc.has_permission("read"): + xhiveframework.throw(_("Not permitted"), xhiveframework.PermissionError) + return doc.run_method(method, **xhiveframework.form_dict) + + elif xhiveframework.request.method == "POST": + if not doc.has_permission("write"): + xhiveframework.throw(_("Not permitted"), xhiveframework.PermissionError) + + return doc.run_method(method, **xhiveframework.form_dict) + + +def get_request_form_data(): + if xhiveframework.form_dict.data is None: + data = xhiveframework.safe_decode(xhiveframework.request.get_data()) + else: + data = xhiveframework.form_dict.data + + try: + return xhiveframework.parse_json(data) + except ValueError: + return xhiveframework.form_dict + + +url_rules = [ + Rule("/method/", endpoint=handle_rpc_call), + Rule("/resource/", methods=["GET"], endpoint=document_list), + Rule("/resource/", methods=["POST"], endpoint=create_doc), + Rule("/resource///", methods=["GET"], endpoint=read_doc), + Rule("/resource///", methods=["PUT"], endpoint=update_doc), + Rule("/resource///", methods=["DELETE"], endpoint=delete_doc), + Rule("/resource///", methods=["POST"], endpoint=execute_doc_method), +] diff --git a/xhiveframework/api/v2.py b/xhiveframework/api/v2.py new file mode 100644 index 0000000..2bb3485 --- /dev/null +++ b/xhiveframework/api/v2.py @@ -0,0 +1,193 @@ +"""REST API v2 + +This file defines routes and implementation for REST API. + +Note: + - All functions in this file should be treated as "whitelisted" as they are exposed via routes + - None of the functions present here should be called from python code, their location and + internal implementation can change without treating it as "breaking change". +""" +import json +from typing import Any + +from werkzeug.routing import Rule + +import xhiveframework +import xhiveframework.client +from xhiveframework import _, get_newargs, is_whitelisted +from xhiveframework.core.doctype.server_script.server_script_utils import get_server_script_map +from xhiveframework.handler import is_valid_http_method, run_server_script, upload_file + +PERMISSION_MAP = { + "GET": "read", + "POST": "write", +} + + +def handle_rpc_call(method: str, doctype: str | None = None): + from xhiveframework.modules.utils import load_doctype_module + + if doctype: + # Expand to run actual method from doctype controller + module = load_doctype_module(doctype) + method = module.__name__ + "." + method + + for hook in reversed(xhiveframework.get_hooks("override_whitelisted_methods", {}).get(method, [])): + # override using the last hook + method = hook + break + + # via server script + server_script = get_server_script_map().get("_api", {}).get(method) + if server_script: + return run_server_script(server_script) + + try: + method = xhiveframework.get_attr(method) + except Exception as e: + xhiveframework.throw(_("Failed to get method {0} with {1}").format(method, e)) + + is_whitelisted(method) + is_valid_http_method(method) + + return xhiveframework.call(method, **xhiveframework.form_dict) + + +def login(): + """Login happens implicitly, this function doesn't do anything.""" + pass + + +def logout(): + xhiveframework.local.login_manager.logout() + xhiveframework.db.commit() + + +def read_doc(doctype: str, name: str): + doc = xhiveframework.get_doc(doctype, name) + doc.check_permission("read") + doc.apply_fieldlevel_read_permissions() + return doc + + +def document_list(doctype: str): + if xhiveframework.form_dict.get("fields"): + xhiveframework.form_dict["fields"] = json.loads(xhiveframework.form_dict["fields"]) + + # set limit of records for xhiveframework.get_list + xhiveframework.form_dict.limit_page_length = xhiveframework.form_dict.limit or 20 + # evaluate xhiveframework.get_list + return xhiveframework.call(xhiveframework.client.get_list, doctype, **xhiveframework.form_dict) + + +def count(doctype: str) -> int: + from xhiveframework.desk.reportview import get_count + + xhiveframework.form_dict.doctype = doctype + + return get_count() + + +def create_doc(doctype: str): + data = xhiveframework.form_dict + data.pop("doctype", None) + return xhiveframework.new_doc(doctype, **data).insert() + + +def update_doc(doctype: str, name: str): + data = xhiveframework.form_dict + + doc = xhiveframework.get_doc(doctype, name, for_update=True) + data.pop("flags", None) + doc.update(data) + doc.save() + + # check for child table doctype + if doc.get("parenttype"): + xhiveframework.get_doc(doc.parenttype, doc.parent).save() + + return doc + + +def delete_doc(doctype: str, name: str): + xhiveframework.client.delete_doc(doctype, name) + xhiveframework.response.http_status_code = 202 + return "ok" + + +def execute_doc_method(doctype: str, name: str, method: str | None = None): + """Get a document from DB and execute method on it. + + Use cases: + - Submitting/cancelling document + - Triggering some kind of update on a document + """ + method = method or xhiveframework.form_dict.pop("run_method") + doc = xhiveframework.get_doc(doctype, name) + doc.is_whitelisted(method) + + doc.check_permission(PERMISSION_MAP[xhiveframework.request.method]) + return doc.run_method(method, **xhiveframework.form_dict) + + +def run_doc_method(method: str, document: dict[str, Any] | str, kwargs=None): + """run a whitelisted controller method on in-memory document. + + + This is useful for building clients that don't necessarily encode all the business logic but + call server side function on object to validate and modify the doc. + + The doc CAN exists in DB too and can write to DB as well if method is POST. + """ + + if isinstance(document, str): + document = xhiveframework.parse_json(document) + + if kwargs is None: + kwargs = {} + + doc = xhiveframework.get_doc(document) + doc._original_modified = doc.modified + doc.check_if_latest() + + doc.check_permission(PERMISSION_MAP[xhiveframework.request.method]) + + method_obj = getattr(doc, method) + fn = getattr(method_obj, "__func__", method_obj) + is_whitelisted(fn) + is_valid_http_method(fn) + + new_kwargs = get_newargs(fn, kwargs) + response = doc.run_method(method, **new_kwargs) + xhiveframework.response.docs.append(doc) # send modified document and result both. + return response + + +url_rules = [ + # RPC calls + Rule("/method/login", endpoint=login), + Rule("/method/logout", endpoint=logout), + Rule("/method/ping", endpoint=xhiveframework.ping), + Rule("/method/upload_file", endpoint=upload_file), + Rule("/method/", endpoint=handle_rpc_call), + Rule( + "/method/run_doc_method", + methods=["GET", "POST"], + endpoint=lambda: xhiveframework.call(run_doc_method, **xhiveframework.form_dict), + ), + Rule("/method//", endpoint=handle_rpc_call), + # Document level APIs + Rule("/document/", methods=["GET"], endpoint=document_list), + Rule("/document/", methods=["POST"], endpoint=create_doc), + Rule("/document///", methods=["GET"], endpoint=read_doc), + Rule("/document///", methods=["PATCH", "PUT"], endpoint=update_doc), + Rule("/document///", methods=["DELETE"], endpoint=delete_doc), + Rule( + "/document///method//", + methods=["GET", "POST"], + endpoint=execute_doc_method, + ), + # Collection level APIs + Rule("/doctype//meta", methods=["GET"], endpoint=xhiveframework.get_meta), + Rule("/doctype//count", methods=["GET"], endpoint=count), +] diff --git a/xhiveframework/app.py b/xhiveframework/app.py new file mode 100644 index 0000000..0d459f2 --- /dev/null +++ b/xhiveframework/app.py @@ -0,0 +1,532 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import functools +import gc +import logging +import os +import re + +from werkzeug.exceptions import HTTPException, NotFound +from werkzeug.middleware.profiler import ProfilerMiddleware +from werkzeug.middleware.proxy_fix import ProxyFix +from werkzeug.middleware.shared_data import SharedDataMiddleware +from werkzeug.wrappers import Request, Response +from werkzeug.wsgi import ClosingIterator + +import xhiveframework +import xhiveframework.api +import xhiveframework.handler +import xhiveframework.monitor +import xhiveframework.rate_limiter +import xhiveframework.recorder +import xhiveframework.utils.response +from xhiveframework import _ +from xhiveframework.auth import SAFE_HTTP_METHODS, UNSAFE_HTTP_METHODS, HTTPRequest, validate_auth +from xhiveframework.middlewares import StaticDataMiddleware +from xhiveframework.utils import CallbackManager, cint, get_site_name +from xhiveframework.utils.data import escape_html +from xhiveframework.utils.deprecations import deprecation_warning +from xhiveframework.utils.error import log_error_snapshot +from xhiveframework.website.serve import get_response + +_site = None +_sites_path = os.environ.get("SITES_PATH", ".") + + +# If gc.freeze is done then importing modules before forking allows us to share the memory +if xhiveframework._tune_gc: + import bleach + import pydantic + + import xhiveframework.boot + import xhiveframework.client + import xhiveframework.core.doctype.file.file + import xhiveframework.core.doctype.user.user + import xhiveframework.database.mariadb.database # Load database related utils + import xhiveframework.database.query + import xhiveframework.desk.desktop # workspace + import xhiveframework.desk.form.save + import xhiveframework.model.db_query + import xhiveframework.query_builder + import xhiveframework.utils.background_jobs # Enqueue is very common + import xhiveframework.utils.data # common utils + import xhiveframework.utils.jinja # web page rendering + import xhiveframework.utils.jinja_globals + import xhiveframework.utils.redis_wrapper # Exact redis_wrapper + import xhiveframework.utils.safe_exec + import xhiveframework.utils.typing_validations # any whitelisted method uses this + import xhiveframework.website.path_resolver # all the page types and resolver + import xhiveframework.website.router # Website router + import xhiveframework.website.website_generator # web page doctypes + +# end: module pre-loading + + +def after_response_wrapper(app): + """Wrap a WSGI application to call after_response hooks after we have responded. + + This is done to reduce response time by deferring expensive tasks.""" + + @functools.wraps(app) + def application(environ, start_response): + return ClosingIterator( + app(environ, start_response), + ( + xhiveframework.rate_limiter.update, + xhiveframework.monitor.stop, + xhiveframework.recorder.dump, + xhiveframework.request.after_response.run, + xhiveframework.destroy, + ), + ) + + return application + + +@after_response_wrapper +@Request.application +def application(request: Request): + response = None + + try: + rollback = True + + init_request(request) + + validate_auth() + + if request.method == "OPTIONS": + response = Response() + + elif xhiveframework.form_dict.cmd: + deprecation_warning( + f"{xhiveframework.form_dict.cmd}: Sending `cmd` for RPC calls is deprecated, call REST API instead `/api/method/cmd`" + ) + xhiveframework.handler.handle() + response = xhiveframework.utils.response.build_response("json") + + elif request.path.startswith("/api/"): + response = xhiveframework.api.handle(request) + + elif request.path.startswith("/backups"): + response = xhiveframework.utils.response.download_backup(request.path) + + elif request.path.startswith("/private/files/"): + response = xhiveframework.utils.response.download_private_file(request.path) + + elif request.method in ("GET", "HEAD", "POST"): + response = get_response() + + else: + raise NotFound + + except HTTPException as e: + return e + + except Exception as e: + response = handle_exception(e) + + else: + rollback = sync_database(rollback) + + finally: + # Important note: + # this function *must* always return a response, hence any exception thrown outside of + # try..catch block like this finally block needs to be handled appropriately. + + if rollback and request.method in UNSAFE_HTTP_METHODS and xhiveframework.db: + xhiveframework.db.rollback() + + try: + run_after_request_hooks(request, response) + except Exception: + # We can not handle exceptions safely here. + xhiveframework.logger().error("Failed to run after request hook", exc_info=True) + + log_request(request, response) + process_response(response) + + return response + + +def run_after_request_hooks(request, response): + if not getattr(xhiveframework.local, "initialised", False): + return + + for after_request_task in xhiveframework.get_hooks("after_request"): + xhiveframework.call(after_request_task, response=response, request=request) + + +def init_request(request): + xhiveframework.local.request = request + xhiveframework.local.request.after_response = CallbackManager() + + xhiveframework.local.is_ajax = xhiveframework.get_request_header("X-Requested-With") == "XMLHttpRequest" + + site = _site or request.headers.get("X-XhiveFramework-Site-Name") or get_site_name(request.host) + xhiveframework.init(site=site, sites_path=_sites_path, force=True) + + if not (xhiveframework.local.conf and xhiveframework.local.conf.db_name): + # site does not exist + raise NotFound + + if xhiveframework.local.conf.maintenance_mode: + xhiveframework.connect() + if xhiveframework.local.conf.allow_reads_during_maintenance: + setup_read_only_mode() + else: + raise xhiveframework.SessionStopped("Session Stopped") + else: + xhiveframework.connect(set_admin_as_user=False) + if request.path.startswith("/api/method/upload_file"): + from xhiveframework.core.api.file import get_max_file_size + + request.max_content_length = get_max_file_size() + else: + request.max_content_length = cint(xhiveframework.local.conf.get("max_file_size")) or 25 * 1024 * 1024 + make_form_dict(request) + + if request.method != "OPTIONS": + xhiveframework.local.http_request = HTTPRequest() + + for before_request_task in xhiveframework.get_hooks("before_request"): + xhiveframework.call(before_request_task) + + +def setup_read_only_mode(): + """During maintenance_mode reads to DB can still be performed to reduce downtime. This + function sets up read only mode + + - Setting global flag so other pages, desk and database can know that we are in read only mode. + - Setup read only database access either by: + - Connecting to read replica if one exists + - Or setting up read only SQL transactions. + """ + xhiveframework.flags.read_only = True + + # If replica is available then just connect replica, else setup read only transaction. + if xhiveframework.conf.read_from_replica: + xhiveframework.connect_replica() + else: + xhiveframework.db.begin(read_only=True) + + +def log_request(request, response): + if hasattr(xhiveframework.local, "conf") and xhiveframework.local.conf.enable_xhiveframework_logger: + xhiveframework.logger("xhiveframework.web", allow_site=xhiveframework.local.site).info( + { + "site": get_site_name(request.host), + "remote_addr": getattr(request, "remote_addr", "NOTFOUND"), + "pid": os.getpid(), + "user": getattr(xhiveframework.local.session, "user", "NOTFOUND"), + "base_url": getattr(request, "base_url", "NOTFOUND"), + "full_path": getattr(request, "full_path", "NOTFOUND"), + "method": getattr(request, "method", "NOTFOUND"), + "scheme": getattr(request, "scheme", "NOTFOUND"), + "http_status_code": getattr(response, "status_code", "NOTFOUND"), + } + ) + + +def process_response(response): + if not response: + return + + # set cookies + if hasattr(xhiveframework.local, "cookie_manager"): + xhiveframework.local.cookie_manager.flush_cookies(response=response) + + # rate limiter headers + if hasattr(xhiveframework.local, "rate_limiter"): + response.headers.extend(xhiveframework.local.rate_limiter.headers()) + + if trace_id := xhiveframework.monitor.get_trace_id(): + response.headers.extend({"X-XhiveFramework-Request-Id": trace_id}) + + # CORS headers + if hasattr(xhiveframework.local, "conf"): + set_cors_headers(response) + + +def set_cors_headers(response): + if not ( + (allowed_origins := xhiveframework.conf.allow_cors) + and (request := xhiveframework.local.request) + and (origin := request.headers.get("Origin")) + ): + return + + if allowed_origins != "*": + if not isinstance(allowed_origins, list): + allowed_origins = [allowed_origins] + + if origin not in allowed_origins: + return + + cors_headers = { + "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Origin": origin, + "Vary": "Origin", + } + + # only required for preflight requests + if request.method == "OPTIONS": + cors_headers["Access-Control-Allow-Methods"] = request.headers.get("Access-Control-Request-Method") + + if allowed_headers := request.headers.get("Access-Control-Request-Headers"): + cors_headers["Access-Control-Allow-Headers"] = allowed_headers + + # allow browsers to cache preflight requests for upto a day + if not xhiveframework.conf.developer_mode: + cors_headers["Access-Control-Max-Age"] = "86400" + + response.headers.extend(cors_headers) + + +def make_form_dict(request: Request): + import json + + request_data = request.get_data(as_text=True) + if request_data and request.is_json: + args = json.loads(request_data) + else: + args = {} + args.update(request.args or {}) + args.update(request.form or {}) + + if isinstance(args, dict): + xhiveframework.local.form_dict = xhiveframework._dict(args) + # _ is passed by $.ajax so that the request is not cached by the browser. So, remove _ from form_dict + xhiveframework.local.form_dict.pop("_", None) + elif isinstance(args, list): + xhiveframework.local.form_dict["data"] = args + else: + xhiveframework.throw(_("Invalid request arguments")) + + +def handle_exception(e): + response = None + http_status_code = getattr(e, "http_status_code", 500) + return_as_message = False + accept_header = xhiveframework.get_request_header("Accept") or "" + respond_as_json = ( + xhiveframework.get_request_header("Accept") + and (xhiveframework.local.is_ajax or "application/json" in accept_header) + or (xhiveframework.local.request.path.startswith("/api/") and not accept_header.startswith("text")) + ) + + allow_traceback = xhiveframework.get_system_settings("allow_error_traceback") if xhiveframework.db else False + + if not xhiveframework.session.user: + # If session creation fails then user won't be unset. This causes a lot of code that + # assumes presence of this to fail. Session creation fails => guest or expired login + # usually. + xhiveframework.session.user = "Guest" + + if respond_as_json: + # handle ajax responses first + # if the request is ajax, send back the trace or error message + response = xhiveframework.utils.response.report_error(http_status_code) + + elif isinstance(e, xhiveframework.SessionStopped): + response = xhiveframework.utils.response.handle_session_stopped() + + elif ( + http_status_code == 500 + and (xhiveframework.db and isinstance(e, xhiveframework.db.InternalError)) + and (xhiveframework.db and (xhiveframework.db.is_deadlocked(e) or xhiveframework.db.is_timedout(e))) + ): + http_status_code = 508 + + elif http_status_code == 401: + xhiveframework.respond_as_web_page( + _("Session Expired"), + _("Your session has expired, please login again to continue."), + http_status_code=http_status_code, + indicator_color="red", + ) + return_as_message = True + + elif http_status_code == 403: + xhiveframework.respond_as_web_page( + _("Not Permitted"), + _("You do not have enough permissions to complete the action"), + http_status_code=http_status_code, + indicator_color="red", + ) + return_as_message = True + + elif http_status_code == 404: + xhiveframework.respond_as_web_page( + _("Not Found"), + _("The resource you are looking for is not available"), + http_status_code=http_status_code, + indicator_color="red", + ) + return_as_message = True + + elif http_status_code == 429: + response = xhiveframework.rate_limiter.respond() + + else: + traceback = "
" + escape_html(xhiveframework.get_traceback()) + "
" + # disable traceback in production if flag is set + if xhiveframework.local.flags.disable_traceback or not allow_traceback and not xhiveframework.local.dev_server: + traceback = "" + + xhiveframework.respond_as_web_page( + "Server Error", traceback, http_status_code=http_status_code, indicator_color="red", width=640 + ) + return_as_message = True + + if e.__class__ == xhiveframework.AuthenticationError: + if hasattr(xhiveframework.local, "login_manager"): + xhiveframework.local.login_manager.clear_cookies() + + if http_status_code >= 500: + log_error_snapshot(e) + + if return_as_message: + response = get_response("message", http_status_code=http_status_code) + + if xhiveframework.conf.get("developer_mode") and not respond_as_json: + # don't fail silently for non-json response errors + print(xhiveframework.get_traceback()) + + return response + + +def sync_database(rollback: bool) -> bool: + # if HTTP method would change server state, commit if necessary + if xhiveframework.db and (xhiveframework.local.flags.commit or xhiveframework.local.request.method in UNSAFE_HTTP_METHODS): + xhiveframework.db.commit() + rollback = False + elif xhiveframework.db: + xhiveframework.db.rollback() + rollback = False + + # update session + if session := getattr(xhiveframework.local, "session_obj", None): + if session.update(): + xhiveframework.db.commit() + rollback = False + + return rollback + + +# Always initialize sentry SDK if the DSN is sent +if sentry_dsn := os.getenv("XHIVEFRAMEWORK_SENTRY_DSN"): + import sentry_sdk + from sentry_sdk.integrations.argv import ArgvIntegration + from sentry_sdk.integrations.atexit import AtexitIntegration + from sentry_sdk.integrations.dedupe import DedupeIntegration + from sentry_sdk.integrations.excepthook import ExcepthookIntegration + from sentry_sdk.integrations.modules import ModulesIntegration + from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware + + from xhiveframework.utils.sentry import XhiveFrameworkIntegration, before_send + + integrations = [ + AtexitIntegration(), + ExcepthookIntegration(), + DedupeIntegration(), + ModulesIntegration(), + ArgvIntegration(), + ] + + experiments = {} + kwargs = {} + + if os.getenv("ENABLE_SENTRY_DB_MONITORING"): + integrations.append(XhiveFrameworkIntegration()) + experiments["record_sql_params"] = True + + if tracing_sample_rate := os.getenv("SENTRY_TRACING_SAMPLE_RATE"): + kwargs["traces_sample_rate"] = float(tracing_sample_rate) + application = SentryWsgiMiddleware(application) + + sentry_sdk.init( + dsn=sentry_dsn, + before_send=before_send, + attach_stacktrace=True, + release=xhiveframework.__version__, + auto_enabling_integrations=False, + default_integrations=False, + integrations=integrations, + _experiments=experiments, + **kwargs, + ) + + +def serve( + port=8000, + profile=False, + no_reload=False, + no_threading=False, + site=None, + sites_path=".", + proxy=False, +): + global application, _site, _sites_path + _site = site + _sites_path = sites_path + + from werkzeug.serving import run_simple + + if profile or os.environ.get("USE_PROFILER"): + application = ProfilerMiddleware(application, sort_by=("cumtime", "calls")) + + if not os.environ.get("NO_STATICS"): + application = application_with_statics() + + if proxy or os.environ.get("USE_PROXY"): + application = ProxyFix(application, x_for=1, x_proto=1, x_host=1, x_port=1, x_prefix=1) + + application.debug = True + application.config = {"SERVER_NAME": "127.0.0.1:8000"} + + log = logging.getLogger("werkzeug") + log.propagate = False + + in_test_env = os.environ.get("CI") + if in_test_env: + log.setLevel(logging.ERROR) + + run_simple( + "0.0.0.0", + int(port), + application, + exclude_patterns=["test_*"], + use_reloader=False if in_test_env else not no_reload, + use_debugger=not in_test_env, + use_evalex=not in_test_env, + threaded=not no_threading, + ) + + +def application_with_statics(): + global application, _sites_path + + application = SharedDataMiddleware(application, {"/assets": str(os.path.join(_sites_path, "assets"))}) + + application = StaticDataMiddleware(application, {"/files": str(os.path.abspath(_sites_path))}) + + return application + + +# Remove references to pattern that are pre-compiled and loaded to global scopes. +re.purge() + +# Both Gunicorn and RQ use forking to spawn workers. In an ideal world, the fork should be sharing +# most of the memory if there are no writes made to data because of Copy on Write, however, +# python's GC is not CoW friendly and writes to data even if user-code doesn't. Specifically, the +# generational GC which stores and mutates every python object: `PyGC_Head` +# +# Calling gc.freeze() moves all the objects imported so far into permanant generation and hence +# doesn't mutate `PyGC_Head` +# +# Refer to issue for more info: https://lab.membtech.com/xhiveframework/xhiveframework15/issues/18927 +if xhiveframework._tune_gc: + gc.collect() # clean up any garbage created so far before freeze + gc.freeze() diff --git a/xhiveframework/auth.py b/xhiveframework/auth.py new file mode 100644 index 0000000..269ddef --- /dev/null +++ b/xhiveframework/auth.py @@ -0,0 +1,685 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE +import base64 +import binascii +from urllib.parse import quote, urlencode, urlparse + +from werkzeug.wrappers import Response + +import xhiveframework +import xhiveframework.database +import xhiveframework.utils +import xhiveframework.utils.user +from xhiveframework import _ +from xhiveframework.core.doctype.activity_log.activity_log import add_authentication_log +from xhiveframework.sessions import Session, clear_sessions, delete_session, get_expiry_in_seconds +from xhiveframework.translate import get_language +from xhiveframework.twofactor import ( + authenticate_for_2factor, + confirm_otp_token, + get_cached_user_pass, + should_run_2fa, +) +from xhiveframework.utils import cint, date_diff, datetime, get_datetime, today +from xhiveframework.utils.deprecations import deprecation_warning +from xhiveframework.utils.password import check_password, get_decrypted_password +from xhiveframework.website.utils import get_home_page + +SAFE_HTTP_METHODS = frozenset(("GET", "HEAD", "OPTIONS")) +UNSAFE_HTTP_METHODS = frozenset(("POST", "PUT", "DELETE", "PATCH")) +MAX_PASSWORD_SIZE = 512 + + +class HTTPRequest: + def __init__(self): + # set xhiveframework.local.request_ip + self.set_request_ip() + + # load cookies + self.set_cookies() + + # login and start/resume user session + self.set_session() + + # set request language + self.set_lang() + + # match csrf token from current session + self.validate_csrf_token() + + # write out latest cookies + xhiveframework.local.cookie_manager.init_cookies() + + @property + def domain(self): + if not getattr(self, "_domain", None): + self._domain = xhiveframework.request.host + if self._domain and self._domain.startswith("www."): + self._domain = self._domain[4:] + + return self._domain + + def set_request_ip(self): + if xhiveframework.get_request_header("X-Forwarded-For"): + xhiveframework.local.request_ip = (xhiveframework.get_request_header("X-Forwarded-For").split(",", 1)[0]).strip() + + elif xhiveframework.get_request_header("REMOTE_ADDR"): + xhiveframework.local.request_ip = xhiveframework.get_request_header("REMOTE_ADDR") + + else: + xhiveframework.local.request_ip = "127.0.0.1" + + def set_cookies(self): + xhiveframework.local.cookie_manager = CookieManager() + + def set_session(self): + xhiveframework.local.login_manager = LoginManager() + + def validate_csrf_token(self): + if ( + not xhiveframework.request + or xhiveframework.request.method not in UNSAFE_HTTP_METHODS + or xhiveframework.conf.ignore_csrf + or not xhiveframework.session + or not (saved_token := xhiveframework.session.data.csrf_token) + or ( + (xhiveframework.get_request_header("X-XhiveFramework-CSRF-Token") or xhiveframework.form_dict.pop("csrf_token", None)) + == saved_token + ) + ): + return + + xhiveframework.flags.disable_traceback = True + xhiveframework.throw(_("Invalid Request"), xhiveframework.CSRFTokenError) + + def set_lang(self): + xhiveframework.local.lang = get_language() + + +class LoginManager: + __slots__ = ("user", "info", "full_name", "user_type", "resume") + + def __init__(self): + self.user = None + self.info = None + self.full_name = None + self.user_type = None + + if xhiveframework.local.form_dict.get("cmd") == "login" or xhiveframework.local.request.path == "/api/method/login": + if self.login() is False: + return + self.resume = False + + # run login triggers + self.run_trigger("on_session_creation") + else: + try: + self.resume = True + self.make_session(resume=True) + self.get_user_info() + self.set_user_info(resume=True) + except AttributeError: + self.user = "Guest" + self.get_user_info() + self.make_session() + self.set_user_info() + + def login(self): + if xhiveframework.get_system_settings("disable_user_pass_login"): + xhiveframework.throw(_("Login with username and password is not allowed."), xhiveframework.AuthenticationError) + + # clear cache + xhiveframework.clear_cache(user=xhiveframework.form_dict.get("usr")) + user, pwd = get_cached_user_pass() + self.authenticate(user=user, pwd=pwd) + if self.force_user_to_reset_password(): + doc = xhiveframework.get_doc("User", self.user) + xhiveframework.local.response["redirect_to"] = doc.reset_password(send_email=False, password_expired=True) + xhiveframework.local.response["message"] = "Password Reset" + return False + + if should_run_2fa(self.user): + authenticate_for_2factor(self.user) + if not confirm_otp_token(self): + return False + xhiveframework.form_dict.pop("pwd", None) + self.post_login() + + def post_login(self): + self.run_trigger("on_login") + validate_ip_address(self.user) + self.validate_hour() + self.get_user_info() + self.make_session() + self.setup_boot_cache() + self.set_user_info() + + def get_user_info(self): + self.info = xhiveframework.get_cached_value( + "User", self.user, ["user_type", "first_name", "last_name", "user_image"], as_dict=1 + ) + + self.user_type = self.info.user_type + + def setup_boot_cache(self): + xhiveframework.cache_manager.build_table_count_cache() + xhiveframework.cache_manager.build_domain_restriced_doctype_cache() + xhiveframework.cache_manager.build_domain_restriced_page_cache() + + def set_user_info(self, resume=False): + # set sid again + xhiveframework.local.cookie_manager.init_cookies() + + self.full_name = " ".join(filter(None, [self.info.first_name, self.info.last_name])) + + if self.info.user_type == "Website User": + xhiveframework.local.cookie_manager.set_cookie("system_user", "no") + if not resume: + xhiveframework.local.response["message"] = "No App" + xhiveframework.local.response["home_page"] = "/" + get_home_page() + else: + xhiveframework.local.cookie_manager.set_cookie("system_user", "yes") + if not resume: + xhiveframework.local.response["message"] = "Logged In" + xhiveframework.local.response["home_page"] = "/app" + + if not resume: + xhiveframework.response["full_name"] = self.full_name + + # redirect information + redirect_to = xhiveframework.cache.hget("redirect_after_login", self.user) + if redirect_to: + xhiveframework.local.response["redirect_to"] = redirect_to + xhiveframework.cache.hdel("redirect_after_login", self.user) + + xhiveframework.local.cookie_manager.set_cookie("full_name", self.full_name) + xhiveframework.local.cookie_manager.set_cookie("user_id", self.user) + xhiveframework.local.cookie_manager.set_cookie("user_image", self.info.user_image or "") + + def clear_preferred_language(self): + xhiveframework.local.cookie_manager.delete_cookie("preferred_language") + + def make_session(self, resume=False): + # start session + xhiveframework.local.session_obj = Session( + user=self.user, resume=resume, full_name=self.full_name, user_type=self.user_type + ) + + # reset user if changed to Guest + self.user = xhiveframework.local.session_obj.user + xhiveframework.local.session = xhiveframework.local.session_obj.data + self.clear_active_sessions() + + def clear_active_sessions(self): + """Clear other sessions of the current user if `deny_multiple_sessions` is not set""" + if xhiveframework.session.user == "Guest": + return + + if not ( + cint(xhiveframework.conf.get("deny_multiple_sessions")) + or cint(xhiveframework.db.get_system_setting("deny_multiple_sessions")) + ): + return + + clear_sessions(xhiveframework.session.user, keep_current=True) + + def authenticate(self, user: str | None = None, pwd: str | None = None): + from xhiveframework.core.doctype.user.user import User + + if not (user and pwd): + user, pwd = xhiveframework.form_dict.get("usr"), xhiveframework.form_dict.get("pwd") + if not (user and pwd): + self.fail(_("Incomplete login details"), user=user) + + if len(pwd) > MAX_PASSWORD_SIZE: + self.fail(_("Password size exceeded the maximum allowed size"), user=user) + + _raw_user_name = user + user = User.find_by_credentials(user, pwd) + + ip_tracker = get_login_attempt_tracker(xhiveframework.local.request_ip) + if not user: + ip_tracker and ip_tracker.add_failure_attempt() + self.fail("Invalid login credentials", user=_raw_user_name) + + # Current login flow uses cached credentials for authentication while checking OTP. + # Incase of OTP check, tracker for auth needs to be disabled(If not, it can remove tracker history as it is going to succeed anyway) + # Tracker is activated for 2FA incase of OTP. + ignore_tracker = should_run_2fa(user.name) and ("otp" in xhiveframework.form_dict) + user_tracker = None if ignore_tracker else get_login_attempt_tracker(user.name) + + if not user.is_authenticated: + user_tracker and user_tracker.add_failure_attempt() + ip_tracker and ip_tracker.add_failure_attempt() + self.fail("Invalid login credentials", user=user.name) + elif not (user.name == "Administrator" or user.enabled): + user_tracker and user_tracker.add_failure_attempt() + ip_tracker and ip_tracker.add_failure_attempt() + self.fail("User disabled or missing", user=user.name) + else: + user_tracker and user_tracker.add_success_attempt() + ip_tracker and ip_tracker.add_success_attempt() + self.user = user.name + + def force_user_to_reset_password(self): + if not self.user: + return + + if self.user in xhiveframework.STANDARD_USERS: + return False + + reset_pwd_after_days = cint( + xhiveframework.db.get_single_value("System Settings", "force_user_to_reset_password") + ) + + if reset_pwd_after_days: + last_password_reset_date = ( + xhiveframework.db.get_value("User", self.user, "last_password_reset_date") or today() + ) + + last_pwd_reset_days = date_diff(today(), last_password_reset_date) + + if last_pwd_reset_days > reset_pwd_after_days: + return True + + def check_password(self, user, pwd): + """check password""" + try: + # returns user in correct case + return check_password(user, pwd) + except xhiveframework.AuthenticationError: + self.fail("Incorrect password", user=user) + + def fail(self, message, user=None): + if not user: + user = _("Unknown User") + xhiveframework.local.response["message"] = message + add_authentication_log(message, user, status="Failed") + xhiveframework.db.commit() + raise xhiveframework.AuthenticationError + + def run_trigger(self, event="on_login"): + for method in xhiveframework.get_hooks().get(event, []): + xhiveframework.call(xhiveframework.get_attr(method), login_manager=self) + + def validate_hour(self): + """check if user is logging in during restricted hours""" + login_before = int(xhiveframework.db.get_value("User", self.user, "login_before", ignore=True) or 0) + login_after = int(xhiveframework.db.get_value("User", self.user, "login_after", ignore=True) or 0) + + if not (login_before or login_after): + return + + from xhiveframework.utils import now_datetime + + current_hour = int(now_datetime().strftime("%H")) + + if login_before and current_hour >= login_before: + xhiveframework.throw(_("Login not allowed at this time"), xhiveframework.AuthenticationError) + + if login_after and current_hour < login_after: + xhiveframework.throw(_("Login not allowed at this time"), xhiveframework.AuthenticationError) + + def login_as_guest(self): + """login as guest""" + self.login_as("Guest") + + def login_as(self, user): + self.user = user + self.post_login() + + def impersonate(self, user): + current_user = xhiveframework.session.user + self.login_as(user) + # Flag this session as impersonated session, so other code can log this. + xhiveframework.local.session_obj.set_impersonsated(current_user) + + def logout(self, arg="", user=None): + if not user: + user = xhiveframework.session.user + self.run_trigger("on_logout") + + if user == xhiveframework.session.user: + delete_session(xhiveframework.session.sid, user=user, reason="User Manually Logged Out") + self.clear_cookies() + else: + clear_sessions(user) + + def clear_cookies(self): + clear_cookies() + + +class CookieManager: + def __init__(self): + self.cookies = {} + self.to_delete = [] + + def init_cookies(self): + if not xhiveframework.local.session.get("sid"): + return + + if xhiveframework.session.sid: + self.set_cookie("sid", xhiveframework.session.sid, max_age=get_expiry_in_seconds(), httponly=True) + + def set_cookie( + self, + key, + value, + expires=None, + secure=False, + httponly=False, + samesite="Lax", + max_age=None, + ): + if not secure and hasattr(xhiveframework.local, "request"): + secure = xhiveframework.local.request.scheme == "https" + + self.cookies[key] = { + "value": value, + "expires": expires, + "secure": secure, + "httponly": httponly, + "samesite": samesite, + "max_age": max_age, + } + + def delete_cookie(self, to_delete): + if not isinstance(to_delete, list | tuple): + to_delete = [to_delete] + + self.to_delete.extend(to_delete) + + def flush_cookies(self, response: Response): + for key, opts in self.cookies.items(): + response.set_cookie( + key, + quote((opts.get("value") or "").encode("utf-8")), + expires=opts.get("expires"), + secure=opts.get("secure"), + httponly=opts.get("httponly"), + samesite=opts.get("samesite"), + max_age=opts.get("max_age"), + ) + + # expires yesterday! + expires = datetime.datetime.now() + datetime.timedelta(days=-1) + for key in set(self.to_delete): + response.set_cookie(key, "", expires=expires) + + +@xhiveframework.whitelist() +def get_logged_user(): + return xhiveframework.session.user + + +def clear_cookies(): + if hasattr(xhiveframework.local, "session"): + xhiveframework.session.sid = "" + xhiveframework.local.cookie_manager.delete_cookie(["full_name", "user_id", "sid", "user_image", "system_user"]) + + +def validate_ip_address(user): + """check if IP Address is valid""" + from xhiveframework.core.doctype.user.user import get_restricted_ip_list + + # Only fetch required fields - for perf + user_fields = ["restrict_ip", "bypass_restrict_ip_check_if_2fa_enabled"] + user_info = ( + xhiveframework.get_cached_value("User", user, user_fields, as_dict=True) + if not xhiveframework.flags.in_test + else xhiveframework.db.get_value("User", user, user_fields, as_dict=True) + ) + ip_list = get_restricted_ip_list(user_info) + if not ip_list: + return + + system_settings = ( + xhiveframework.get_cached_doc("System Settings") + if not xhiveframework.flags.in_test + else xhiveframework.get_single("System Settings") + ) + # check if bypass restrict ip is enabled for all users + bypass_restrict_ip_check = system_settings.bypass_restrict_ip_check_if_2fa_enabled + + # check if two factor auth is enabled + if system_settings.enable_two_factor_auth and not bypass_restrict_ip_check: + # check if bypass restrict ip is enabled for login user + bypass_restrict_ip_check = user_info.bypass_restrict_ip_check_if_2fa_enabled + + for ip in ip_list: + if xhiveframework.local.request_ip.startswith(ip) or bypass_restrict_ip_check: + return + + xhiveframework.throw(_("Access not allowed from this IP Address"), xhiveframework.AuthenticationError) + + +def get_login_attempt_tracker(key: str, raise_locked_exception: bool = True): + """Get login attempt tracker instance. + + :param user_name: Name of the loggedin user + :param raise_locked_exception: If set, raises an exception incase of user not allowed to login + """ + sys_settings = xhiveframework.get_doc("System Settings") + track_login_attempts = sys_settings.allow_consecutive_login_attempts > 0 + tracker_kwargs = {} + + if track_login_attempts: + tracker_kwargs["lock_interval"] = sys_settings.allow_login_after_fail + tracker_kwargs["max_consecutive_login_attempts"] = sys_settings.allow_consecutive_login_attempts + + tracker = LoginAttemptTracker(key, **tracker_kwargs) + + if raise_locked_exception and track_login_attempts and not tracker.is_user_allowed(): + xhiveframework.throw( + _("Your account has been locked and will resume after {0} seconds").format( + sys_settings.allow_login_after_fail + ), + xhiveframework.SecurityException, + ) + return tracker + + +class LoginAttemptTracker: + """Track login attemts of a user. + + Lock the account for s number of seconds if there have been n consecutive unsuccessful attempts to log in. + """ + + def __init__( + self, + key: str, + max_consecutive_login_attempts: int = 3, + lock_interval: int = 5 * 60, + *, + user_name: str | None = None, + ): + """Initialize the tracker. + + :param user_name: Name of the loggedin user + :param max_consecutive_login_attempts: Maximum allowed consecutive failed login attempts + :param lock_interval: Locking interval incase of maximum failed attempts + """ + if user_name: + deprecation_warning("`username` parameter is deprecated, use `key` instead.") + self.key = key or user_name + self.lock_interval = datetime.timedelta(seconds=lock_interval) + self.max_failed_logins = max_consecutive_login_attempts + + @property + def login_failed_count(self): + return xhiveframework.cache.hget("login_failed_count", self.key) + + @login_failed_count.setter + def login_failed_count(self, count): + xhiveframework.cache.hset("login_failed_count", self.key, count) + + @login_failed_count.deleter + def login_failed_count(self): + xhiveframework.cache.hdel("login_failed_count", self.key) + + @property + def login_failed_time(self): + """First failed login attempt time within lock interval. + + For every user we track only First failed login attempt time within lock interval of time. + """ + return xhiveframework.cache.hget("login_failed_time", self.key) + + @login_failed_time.setter + def login_failed_time(self, timestamp): + xhiveframework.cache.hset("login_failed_time", self.key, timestamp) + + @login_failed_time.deleter + def login_failed_time(self): + xhiveframework.cache.hdel("login_failed_time", self.key) + + def add_failure_attempt(self): + """Log user failure attempts into the system. + + Increase the failure count if new failure is with in current lock interval time period, if not reset the login failure count. + """ + login_failed_time = self.login_failed_time + login_failed_count = self.login_failed_count # Consecutive login failure count + current_time = get_datetime() + + if not (login_failed_time and login_failed_count): + login_failed_time, login_failed_count = current_time, 0 + + if login_failed_time + self.lock_interval > current_time: + login_failed_count += 1 + else: + login_failed_time, login_failed_count = current_time, 1 + + self.login_failed_time = login_failed_time + self.login_failed_count = login_failed_count + + def add_success_attempt(self): + """Reset login failures.""" + del self.login_failed_count + del self.login_failed_time + + def is_user_allowed(self) -> bool: + """Is user allowed to login + + User is not allowed to login if login failures are greater than threshold within in lock interval from first login failure. + """ + login_failed_time = self.login_failed_time + login_failed_count = self.login_failed_count or 0 + current_time = get_datetime() + + if ( + login_failed_time + and login_failed_time + self.lock_interval > current_time + and login_failed_count > self.max_failed_logins + ): + return False + return True + + +def validate_auth(): + """ + Authenticate and sets user for the request. + """ + authorization_header = xhiveframework.get_request_header("Authorization", "").split(" ") + + if len(authorization_header) == 2: + validate_oauth(authorization_header) + validate_auth_via_api_keys(authorization_header) + + validate_auth_via_hooks() + + # If login via bearer, basic or keypair didn't work then authentication failed and we + # should terminate here. + if len(authorization_header) == 2 and xhiveframework.session.user in ("", "Guest"): + raise xhiveframework.AuthenticationError + + +def validate_oauth(authorization_header): + """ + Authenticate request using OAuth and set session user + + Args: + authorization_header (list of str): The 'Authorization' header containing the prefix and token + """ + + from xhiveframework.integrations.oauth2 import get_oauth_server + from xhiveframework.oauth import get_url_delimiter + + if authorization_header[0].lower() != "bearer": + return + + form_dict = xhiveframework.local.form_dict + token = authorization_header[1] + req = xhiveframework.request + parsed_url = urlparse(req.url) + access_token = {"access_token": token} + uri = parsed_url.scheme + "://" + parsed_url.netloc + parsed_url.path + "?" + urlencode(access_token) + http_method = req.method + headers = req.headers + body = req.get_data() + if req.content_type and "multipart/form-data" in req.content_type: + body = None + + try: + required_scopes = xhiveframework.db.get_value("OAuth Bearer Token", token, "scopes").split( + get_url_delimiter() + ) + valid, oauthlib_request = get_oauth_server().verify_request( + uri, http_method, body, headers, required_scopes + ) + if valid: + xhiveframework.set_user(xhiveframework.db.get_value("OAuth Bearer Token", token, "user")) + xhiveframework.local.form_dict = form_dict + except AttributeError: + pass + + +def validate_auth_via_api_keys(authorization_header): + """ + Authenticate request using API keys and set session user + + Args: + authorization_header (list of str): The 'Authorization' header containing the prefix and token + """ + + try: + auth_type, auth_token = authorization_header + authorization_source = xhiveframework.get_request_header("XhiveFramework-Authorization-Source") + if auth_type.lower() == "basic": + api_key, api_secret = xhiveframework.safe_decode(base64.b64decode(auth_token)).split(":") + validate_api_key_secret(api_key, api_secret, authorization_source) + elif auth_type.lower() == "token": + api_key, api_secret = auth_token.split(":") + validate_api_key_secret(api_key, api_secret, authorization_source) + except binascii.Error: + xhiveframework.throw( + _("Failed to decode token, please provide a valid base64-encoded token."), + xhiveframework.InvalidAuthorizationToken, + ) + except (AttributeError, TypeError, ValueError): + pass + + +def validate_api_key_secret(api_key, api_secret, xhiveframework_authorization_source=None): + """xhiveframework_authorization_source to provide api key and secret for a doctype apart from User""" + doctype = xhiveframework_authorization_source or "User" + doc = xhiveframework.db.get_value(doctype=doctype, filters={"api_key": api_key}, fieldname=["name"]) + if not doc: + raise xhiveframework.AuthenticationError + form_dict = xhiveframework.local.form_dict + doc_secret = get_decrypted_password(doctype, doc, fieldname="api_secret") + if api_secret == doc_secret: + if doctype == "User": + user = xhiveframework.db.get_value(doctype="User", filters={"api_key": api_key}, fieldname=["name"]) + else: + user = xhiveframework.db.get_value(doctype, doc, "user") + if xhiveframework.local.login_manager.user in ("", "Guest"): + xhiveframework.set_user(user) + xhiveframework.local.form_dict = form_dict + else: + raise xhiveframework.AuthenticationError + + +def validate_auth_via_hooks(): + for auth_hook in xhiveframework.get_hooks("auth_hooks", []): + xhiveframework.get_attr(auth_hook)() diff --git a/xhiveframework/automation/__init__.py b/xhiveframework/automation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/__init__.py b/xhiveframework/automation/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/assignment_rule/__init__.py b/xhiveframework/automation/doctype/assignment_rule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/assignment_rule/assignment_rule.js b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.js new file mode 100644 index 0000000..ae83efc --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.js @@ -0,0 +1,77 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Assignment Rule", { + refresh: function (frm) { + frm.trigger("setup_assignment_days_buttons"); + frm.trigger("set_options"); + // refresh description + frm.events.rule(frm); + }, + + setup: function (frm) { + frm.set_query("document_type", () => { + return { + filters: { + name: ["!=", "ToDo"], + }, + }; + }); + }, + + document_type: function (frm) { + frm.trigger("set_options"); + }, + + setup_assignment_days_buttons: function (frm) { + const labels = ["Weekends", "Weekdays", "All Days"]; + let get_days = (label) => { + const weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]; + const weekends = ["Saturday", "Sunday"]; + return { + "All Days": weekdays.concat(weekends), + Weekdays: weekdays, + Weekends: weekends, + }[label]; + }; + + let set_days = (e) => { + frm.clear_table("assignment_days"); + const label = $(e.currentTarget).text(); + get_days(label).forEach((day) => frm.add_child("assignment_days", { day: day })); + frm.refresh_field("assignment_days"); + }; + + labels.forEach((label) => + frm.fields_dict["assignment_days"].grid.add_custom_button(label, set_days, "top") + ); + }, + + rule: function (frm) { + const description_map = { + "Round Robin": __("Assign one by one, in sequence"), + "Load Balancing": __("Assign to the one who has the least assignments"), + "Based on Field": __("Assign to the user set in this field"), + }; + frm.get_field("rule").set_description(description_map[frm.doc.rule]); + }, + + set_options(frm) { + const doctype = frm.doc.document_type; + frm.set_fields_as_options( + "field", + doctype, + (df) => + ["Dynamic Link", "Data"].includes(df.fieldtype) || + (df.fieldtype == "Link" && df.options == "User"), + [{ label: "Owner", value: "owner" }] + ); + if (doctype) { + frm.set_fields_as_options("due_date_based_on", doctype, (df) => + ["Date", "Datetime"].includes(df.fieldtype) + ).then((options) => + frm.set_df_property("due_date_based_on", "hidden", !options.length) + ); + } + }, +}); diff --git a/xhiveframework/automation/doctype/assignment_rule/assignment_rule.json b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.json new file mode 100644 index 0000000..541d176 --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.json @@ -0,0 +1,179 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2019-02-28 17:12:18.815830", + "description": "Automatically Assign Documents to Users", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "due_date_based_on", + "priority", + "disabled", + "column_break_4", + "description", + "assignment_rules_section", + "assign_condition", + "column_break_6", + "unassign_condition", + "section_break_10", + "close_condition", + "sb", + "assignment_days", + "assign_to_users_section", + "rule", + "field", + "users", + "last_user" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "description": "Higher priority rule will be applied first", + "fieldname": "priority", + "fieldtype": "Int", + "label": "Priority" + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "default": "Automatic Assignment", + "description": "Example: {{ subject }}", + "fieldname": "description", + "fieldtype": "Small Text", + "label": "Description", + "reqd": 1 + }, + { + "fieldname": "assignment_rules_section", + "fieldtype": "Section Break", + "label": "Assignment Rules" + }, + { + "description": "Simple Python Expression, Example: status == 'Open' and type == 'Bug'", + "fieldname": "assign_condition", + "fieldtype": "Code", + "in_list_view": 1, + "label": "Assign Condition", + "options": "PythonExpression", + "reqd": 1 + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "description": "Simple Python Expression, Example: Status in (\"Closed\", \"Cancelled\")", + "fieldname": "unassign_condition", + "fieldtype": "Code", + "label": "Unassign Condition", + "options": "PythonExpression" + }, + { + "fieldname": "assign_to_users_section", + "fieldtype": "Section Break", + "label": "Assign To Users" + }, + { + "fieldname": "rule", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Rule", + "options": "Round Robin\nLoad Balancing\nBased on Field", + "reqd": 1 + }, + { + "depends_on": "eval: doc.rule !== 'Based on Field'", + "fieldname": "users", + "fieldtype": "Table MultiSelect", + "label": "Users", + "mandatory_depends_on": "eval: doc.rule !== 'Based on Field'", + "options": "Assignment Rule User" + }, + { + "fieldname": "last_user", + "fieldtype": "Link", + "label": "Last User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "section_break_10", + "fieldtype": "Section Break" + }, + { + "description": "Simple Python Expression, Example: Status in (\"Invalid\")", + "fieldname": "close_condition", + "fieldtype": "Code", + "label": "Close Condition", + "options": "PythonExpression" + }, + { + "fieldname": "sb", + "fieldtype": "Section Break", + "label": "Assignment Days" + }, + { + "fieldname": "assignment_days", + "fieldtype": "Table", + "label": "Assignment Days", + "options": "Assignment Rule Day", + "reqd": 1 + }, + { + "depends_on": "document_type", + "description": "Value from this field will be set as the due date in the ToDo", + "fieldname": "due_date_based_on", + "fieldtype": "Select", + "label": "Due Date Based On" + }, + { + "depends_on": "eval: doc.rule == 'Based on Field'", + "fieldname": "field", + "fieldtype": "Select", + "label": "Field", + "mandatory_depends_on": "eval: doc.rule == 'Based on Field'" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-07-16 22:51:35.505575", + "modified_by": "Administrator", + "module": "Automation", + "name": "Assignment Rule", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/assignment_rule/assignment_rule.py b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.py new file mode 100644 index 0000000..e9fa91b --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule/assignment_rule.py @@ -0,0 +1,399 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from collections.abc import Iterable + +import xhiveframework +from xhiveframework import _ +from xhiveframework.cache_manager import clear_doctype_map, get_doctype_map +from xhiveframework.desk.form import assign_to +from xhiveframework.model import log_types +from xhiveframework.model.document import Document +from xhiveframework.utils.data import comma_and + + +class AssignmentRule(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.automation.doctype.assignment_rule_day.assignment_rule_day import AssignmentRuleDay + from xhiveframework.automation.doctype.assignment_rule_user.assignment_rule_user import ( + AssignmentRuleUser, + ) + from xhiveframework.types import DF + + assign_condition: DF.Code + assignment_days: DF.Table[AssignmentRuleDay] + close_condition: DF.Code | None + description: DF.SmallText + disabled: DF.Check + document_type: DF.Link + due_date_based_on: DF.Literal[None] + field: DF.Literal[None] + last_user: DF.Link | None + priority: DF.Int + rule: DF.Literal["Round Robin", "Load Balancing", "Based on Field"] + unassign_condition: DF.Code | None + users: DF.TableMultiSelect[AssignmentRuleUser] + + # end: auto-generated types + def validate(self): + self.validate_document_types() + self.validate_assignment_days() + + def clear_cache(self): + super().clear_cache() + clear_doctype_map(self.doctype, self.document_type) + clear_doctype_map(self.doctype, f"due_date_rules_for_{self.document_type}") + + def validate_document_types(self): + if self.document_type == "ToDo": + xhiveframework.throw(_("Assignment Rule is not allowed on {0} document type").format(xhiveframework.bold("ToDo"))) + + def validate_assignment_days(self): + assignment_days = self.get_assignment_days() + if len(set(assignment_days)) != len(assignment_days): + xhiveframework.throw( + _("The following Assignment Days have been repeated: {0}").format( + comma_and([_(day) for day in get_repeated(assignment_days)], add_quotes=False) + ) + ) + + def apply_unassign(self, doc, assignments): + if self.unassign_condition and self.name in [d.assignment_rule for d in assignments]: + return self.clear_assignment(doc) + + return False + + def apply_assign(self, doc): + if self.safe_eval("assign_condition", doc): + return self.do_assignment(doc) + + def do_assignment(self, doc): + # clear existing assignment, to reassign + assign_to.clear(doc.get("doctype"), doc.get("name"), ignore_permissions=True) + + user = self.get_user(doc) + + if user: + assign_to.add( + dict( + assign_to=[user], + doctype=doc.get("doctype"), + name=doc.get("name"), + description=xhiveframework.render_template(self.description, doc), + assignment_rule=self.name, + notify=True, + date=doc.get(self.due_date_based_on) if self.due_date_based_on else None, + ), + ignore_permissions=True, + ) + + # set for reference in round robin + self.db_set("last_user", user) + return True + + return False + + def clear_assignment(self, doc): + """Clear assignments""" + if self.safe_eval("unassign_condition", doc): + return assign_to.clear(doc.get("doctype"), doc.get("name"), ignore_permissions=True) + + def close_assignments(self, doc): + """Close assignments""" + if self.safe_eval("close_condition", doc): + return assign_to.close_all_assignments( + doc.get("doctype"), doc.get("name"), ignore_permissions=True + ) + + def get_user(self, doc): + """ + Get the next user for assignment + """ + if self.rule == "Round Robin": + return self.get_user_round_robin() + elif self.rule == "Load Balancing": + return self.get_user_load_balancing() + elif self.rule == "Based on Field": + return self.get_user_based_on_field(doc) + + def get_user_round_robin(self): + """ + Get next user based on round robin + """ + + # first time, or last in list, pick the first + if not self.last_user or self.last_user == self.users[-1].user: + return self.users[0].user + + # find out the next user in the list + for i, d in enumerate(self.users): + if self.last_user == d.user: + return self.users[i + 1].user + + # bad last user, assign to the first one + return self.users[0].user + + def get_user_load_balancing(self): + """Assign to the user with least number of open assignments""" + counts = [ + dict( + user=d.user, + count=xhiveframework.db.count( + "ToDo", + dict( + reference_type=self.document_type, + allocated_to=d.user, + status="Open", + ), + ), + ) + for d in self.users + ] + # sort by dict value + sorted_counts = sorted(counts, key=lambda k: k["count"]) + + # pick the first user + return sorted_counts[0].get("user") + + def get_user_based_on_field(self, doc): + val = doc.get(self.field) + if xhiveframework.db.exists("User", val): + return val + + def safe_eval(self, fieldname, doc): + try: + if self.get(fieldname): + return xhiveframework.safe_eval(self.get(fieldname), None, doc) + except Exception as e: + # when assignment fails, don't block the document as it may be + # a part of the email pulling + xhiveframework.msgprint(xhiveframework._("Auto assignment failed: {0}").format(str(e)), indicator="orange") + + return False + + def get_assignment_days(self): + return [d.day for d in self.get("assignment_days", [])] + + def is_rule_not_applicable_today(self): + today = xhiveframework.flags.assignment_day or xhiveframework.utils.get_weekday() + assignment_days = self.get_assignment_days() + return assignment_days and today not in assignment_days + + +def get_assignments(doc) -> list[dict]: + return xhiveframework.get_all( + "ToDo", + fields=["name", "assignment_rule"], + filters=dict( + reference_type=doc.get("doctype"), reference_name=doc.get("name"), status=("!=", "Cancelled") + ), + limit=5, + ) + + +@xhiveframework.whitelist() +def bulk_apply(doctype, docnames): + docnames = xhiveframework.parse_json(docnames) + background = len(docnames) > 5 + + for name in docnames: + if background: + xhiveframework.enqueue( + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.apply", + doc=None, + doctype=doctype, + name=name, + ) + else: + apply(doctype=doctype, name=name) + + +def reopen_closed_assignment(doc): + todo_list = xhiveframework.get_all( + "ToDo", + filters={ + "reference_type": doc.doctype, + "reference_name": doc.name, + "status": "Closed", + }, + pluck="name", + ) + + for todo in todo_list: + todo_doc = xhiveframework.get_doc("ToDo", todo) + todo_doc.status = "Open" + todo_doc.save(ignore_permissions=True) + + return bool(todo_list) + + +def apply(doc=None, method=None, doctype=None, name=None): + doctype = doctype or doc.doctype + + skip_assignment_rules = ( + xhiveframework.flags.in_patch + or xhiveframework.flags.in_install + or xhiveframework.flags.in_setup_wizard + or doctype in log_types + ) + + if skip_assignment_rules: + return + + if not doc and doctype and name: + doc = xhiveframework.get_doc(doctype, name) + + assignment_rules = get_doctype_map( + "Assignment Rule", + doc.doctype, + filters={"document_type": doc.doctype, "disabled": 0}, + order_by="priority desc", + ) + + # multiple auto assigns + assignment_rule_docs: list[AssignmentRule] = [ + xhiveframework.get_cached_doc("Assignment Rule", d.get("name")) for d in assignment_rules + ] + + if not assignment_rule_docs: + return + + doc = doc.as_dict() + assignments = get_assignments(doc) + + clear = True # are all assignments cleared + new_apply = False # are new assignments applied + + if assignments: + # first unassign + # use case, there are separate groups to be assigned for say L1 and L2, + # so when the value switches from L1 to L2, L1 team must be unassigned, then L2 can be assigned. + clear = False + for assignment_rule in assignment_rule_docs: + if assignment_rule.is_rule_not_applicable_today(): + continue + + clear = assignment_rule.apply_unassign(doc, assignments) + if clear: + break + + # apply rule only if there are no existing assignments + if clear: + for assignment_rule in assignment_rule_docs: + if assignment_rule.is_rule_not_applicable_today(): + continue + + new_apply = assignment_rule.apply_assign(doc) + if new_apply: + break + + # apply close rule only if assignments exists + assignments = get_assignments(doc) + + if assignments: + for assignment_rule in assignment_rule_docs: + if assignment_rule.is_rule_not_applicable_today(): + continue + + if not new_apply: + # only reopen if close condition is not satisfied + to_close_todos = assignment_rule.safe_eval("close_condition", doc) + + if to_close_todos: + # close todo status + todos_to_close = xhiveframework.get_all( + "ToDo", + filters={ + "reference_type": doc.doctype, + "reference_name": doc.name, + }, + pluck="name", + ) + + for todo in todos_to_close: + _todo = xhiveframework.get_doc("ToDo", todo) + _todo.status = "Closed" + _todo.save(ignore_permissions=True) + break + + else: + reopened = reopen_closed_assignment(doc) + if reopened: + break + + assignment_rule.close_assignments(doc) + + +def update_due_date(doc, state=None): + """Run on_update on every Document (via hooks.py)""" + skip_document_update = ( + xhiveframework.flags.in_migrate + or xhiveframework.flags.in_patch + or xhiveframework.flags.in_import + or xhiveframework.flags.in_setup_wizard + or xhiveframework.flags.in_install + ) + + if skip_document_update: + return + + assignment_rules = get_doctype_map( + doctype="Assignment Rule", + name=f"due_date_rules_for_{doc.doctype}", + filters={ + "due_date_based_on": ["is", "set"], + "document_type": doc.doctype, + "disabled": 0, + }, + ) + + for rule in assignment_rules: + rule_doc = xhiveframework.get_cached_doc("Assignment Rule", rule.get("name")) + due_date_field = rule_doc.due_date_based_on + field_updated = ( + doc.meta.has_field(due_date_field) and doc.has_value_changed(due_date_field) and rule.get("name") + ) + + if field_updated: + assignment_todos = xhiveframework.get_all( + "ToDo", + filters={ + "assignment_rule": rule.get("name"), + "reference_type": doc.doctype, + "reference_name": doc.name, + "status": "Open", + }, + pluck="name", + ) + + for todo in assignment_todos: + todo_doc = xhiveframework.get_doc("ToDo", todo) + todo_doc.date = doc.get(due_date_field) + todo_doc.flags.updater_reference = { + "doctype": "Assignment Rule", + "docname": rule.get("name"), + "label": _("via Assignment Rule"), + } + todo_doc.save(ignore_permissions=True) + + +def get_assignment_rules() -> list[str]: + return xhiveframework.get_all("Assignment Rule", filters={"disabled": 0}, pluck="document_type") + + +def get_repeated(values: Iterable) -> list: + unique = set() + repeated = set() + + for value in values: + if value in unique: + repeated.add(value) + else: + unique.add(value) + + return [str(x) for x in repeated] diff --git a/xhiveframework/automation/doctype/assignment_rule/test_assignment_rule.py b/xhiveframework/automation/doctype/assignment_rule/test_assignment_rule.py new file mode 100644 index 0000000..23bdd77 --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule/test_assignment_rule.py @@ -0,0 +1,452 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.test_runner import make_test_records +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +TEST_DOCTYPE = "Assignment Test" + + +class TestAutoAssign(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + xhiveframework.db.delete("Assignment Rule") + create_test_doctype(TEST_DOCTYPE) + + @classmethod + def tearDownClass(cls): + xhiveframework.db.rollback() + + def setUp(self): + xhiveframework.set_user("Administrator") + make_test_records("User") + days = [ + dict(day="Sunday"), + dict(day="Monday"), + dict(day="Tuesday"), + dict(day="Wednesday"), + dict(day="Thursday"), + dict(day="Friday"), + dict(day="Saturday"), + ] + self.days = days + self.assignment_rule = get_assignment_rule([days, days]) + clear_assignments() + + def test_round_robin(self): + # check if auto assigned to first user + record = _make_test_record(public=1) + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=record.name, status="Open"), + "allocated_to", + ), + "test@example.com", + ) + + # check if auto assigned to second user + record = _make_test_record(public=1) + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=record.name, status="Open"), + "allocated_to", + ), + "test1@example.com", + ) + + clear_assignments() + + # check if auto assigned to third user, even if + # previous assignments where closed + record = _make_test_record(public=1) + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=record.name, status="Open"), + "allocated_to", + ), + "test2@example.com", + ) + + # check loop back to first user + record = _make_test_record(public=1) + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=record.name, status="Open"), + "allocated_to", + ), + "test@example.com", + ) + + def test_load_balancing(self): + self.assignment_rule.rule = "Load Balancing" + self.assignment_rule.save() + + for _ in range(30): + _make_test_record(public=1) + + # check if each user has 10 assignments (?) + for user in ("test@example.com", "test1@example.com", "test2@example.com"): + self.assertEqual( + len(xhiveframework.get_all("ToDo", dict(allocated_to=user, reference_type=TEST_DOCTYPE))), 10 + ) + + # clear 5 assignments for first user + # can't do a limit in "delete" since postgres does not support it + for d in xhiveframework.get_all( + "ToDo", dict(reference_type=TEST_DOCTYPE, allocated_to="test@example.com"), limit=5 + ): + xhiveframework.db.delete("ToDo", {"name": d.name}) + + # add 5 more assignments + for _ in range(5): + _make_test_record(public=1) + + # check if each user still has 10 assignments + for user in ("test@example.com", "test1@example.com", "test2@example.com"): + self.assertEqual( + len(xhiveframework.get_all("ToDo", dict(allocated_to=user, reference_type=TEST_DOCTYPE))), 10 + ) + + def test_assingment_on_guest_submissions(self): + """Sometimes documents are inserted as guest, check if assignment rules run on them. Use case: Web Forms""" + with self.set_user("Guest"): + doc = _make_test_record(ignore_permissions=True, public=1) + + # check assignment to *anyone* + self.assertTrue( + xhiveframework.db.get_value( + "ToDo", + {"reference_type": TEST_DOCTYPE, "reference_name": doc.name, "status": "Open"}, + "allocated_to", + ), + ) + + def test_based_on_field(self): + self.assignment_rule.rule = "Based on Field" + self.assignment_rule.field = "owner" + self.assignment_rule.save() + + for test_user in ("test1@example.com", "test2@example.com"): + xhiveframework.set_user(test_user) + note = _make_test_record(public=1) + # check if auto assigned to doc owner, test1@example.com + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), + "owner", + ), + test_user, + ) + + def test_assign_condition(self): + # check condition + note = _make_test_record(public=0) + + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), + "allocated_to", + ), + None, + ) + + def test_clear_assignment(self): + note = _make_test_record(public=1) + + # check if auto assigned to first user + todo = xhiveframework.get_list( + "ToDo", dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), limit=1 + )[0] + + todo = xhiveframework.get_doc("ToDo", todo["name"]) + self.assertEqual(todo.allocated_to, "test@example.com") + + # test auto unassign + note.public = 0 + note.save() + + todo.load_from_db() + + # check if todo is cancelled + self.assertEqual(todo.status, "Cancelled") + + def test_close_assignment(self): + note = _make_test_record(public=1, content="valid") + + # check if auto assigned + todo = xhiveframework.get_list( + "ToDo", dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), limit=1 + )[0] + + todo = xhiveframework.get_doc("ToDo", todo["name"]) + self.assertEqual(todo.allocated_to, "test@example.com") + + note.content = "Closed" + note.save() + + todo.load_from_db() + + # check if todo is closed + self.assertEqual(todo.status, "Closed") + # check if closed todo retained assignment + self.assertEqual(todo.allocated_to, "test@example.com") + + def check_multiple_rules(self): + note = _make_test_record(public=1, notify_on_login=1) + + # check if auto assigned to test3 (2nd rule is applied, as it has higher priority) + self.assertEqual( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), + "allocated_to", + ), + "test@example.com", + ) + + def check_assignment_rule_scheduling(self): + xhiveframework.db.delete("Assignment Rule") + + days_1 = [dict(day="Sunday"), dict(day="Monday"), dict(day="Tuesday")] + + days_2 = [dict(day="Wednesday"), dict(day="Thursday"), dict(day="Friday"), dict(day="Saturday")] + + get_assignment_rule([days_1, days_2], ["public == 1", "public == 1"]) + + xhiveframework.flags.assignment_day = "Monday" + note = _make_test_record(public=1) + + self.assertIn( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), + "allocated_to", + ), + ["test@example.com", "test1@example.com", "test2@example.com"], + ) + + xhiveframework.flags.assignment_day = "Friday" + note = _make_test_record(public=1) + + self.assertIn( + xhiveframework.db.get_value( + "ToDo", + dict(reference_type=TEST_DOCTYPE, reference_name=note.name, status="Open"), + "allocated_to", + ), + ["test3@example.com"], + ) + + def test_assignment_rule_condition(self): + xhiveframework.db.delete("Assignment Rule") + + assignment_rule = xhiveframework.get_doc( + dict( + name="Assignment with Due Date", + doctype="Assignment Rule", + document_type=TEST_DOCTYPE, + assign_condition="public == 0", + due_date_based_on="expiry_date", + assignment_days=self.days, + users=[ + dict(user="test@example.com"), + ], + ) + ).insert() + + expiry_date = xhiveframework.utils.add_days(xhiveframework.utils.nowdate(), 2) + note1 = _make_test_record(expiry_date=expiry_date) + note2 = _make_test_record(expiry_date=expiry_date) + + note1_todo = xhiveframework.get_all( + "ToDo", filters=dict(reference_type=TEST_DOCTYPE, reference_name=note1.name, status="Open") + )[0] + + note1_todo_doc = xhiveframework.get_doc("ToDo", note1_todo.name) + self.assertEqual(xhiveframework.utils.get_date_str(note1_todo_doc.date), expiry_date) + + # due date should be updated if the reference doc's date is updated. + note1.expiry_date = xhiveframework.utils.add_days(expiry_date, 2) + note1.save() + note1_todo_doc.reload() + self.assertEqual(xhiveframework.utils.get_date_str(note1_todo_doc.date), note1.expiry_date) + + # saving one note's expiry should not update other note todo's due date + note2_todo = xhiveframework.get_all( + "ToDo", + filters=dict(reference_type=TEST_DOCTYPE, reference_name=note2.name, status="Open"), + fields=["name", "date"], + )[0] + self.assertNotEqual(xhiveframework.utils.get_date_str(note2_todo.date), note1.expiry_date) + self.assertEqual(xhiveframework.utils.get_date_str(note2_todo.date), expiry_date) + assignment_rule.delete() + xhiveframework.db.commit() # undo changes commited by DDL + + def test_submittable_assignment(self): + # create a submittable doctype + submittable_doctype = "Assignment Test Submittable" + create_test_doctype(submittable_doctype) + dt = xhiveframework.get_doc("DocType", submittable_doctype) + dt.is_submittable = 1 + dt.save() + + # create a rule for the submittable doctype + assignment_rule = xhiveframework.new_doc("Assignment Rule") + assignment_rule.name = f"For {submittable_doctype}" + assignment_rule.document_type = submittable_doctype + assignment_rule.rule = "Round Robin" + assignment_rule.extend("assignment_days", self.days) + assignment_rule.append("users", {"user": "test@example.com"}) + assignment_rule.assign_condition = "docstatus == 1" + assignment_rule.unassign_condition = "docstatus == 2" + assignment_rule.save() + + # create a submittable doc + doc = xhiveframework.new_doc(submittable_doctype) + doc.save() + doc.submit() + + # check if todo is created + todos = xhiveframework.get_all( + "ToDo", + filters={ + "reference_type": submittable_doctype, + "reference_name": doc.name, + "status": "Open", + "allocated_to": "test@example.com", + }, + ) + self.assertEqual(len(todos), 1) + + # check if todo is closed on cancel + doc.cancel() + todos = xhiveframework.get_all( + "ToDo", + filters={ + "reference_type": submittable_doctype, + "reference_name": doc.name, + "status": "Cancelled", + "allocated_to": "test@example.com", + }, + ) + self.assertEqual(len(todos), 1) + + +def clear_assignments(): + xhiveframework.db.delete("ToDo", {"reference_type": TEST_DOCTYPE}) + + +def get_assignment_rule(days, assign=None): + xhiveframework.delete_doc_if_exists("Assignment Rule", f"For {TEST_DOCTYPE} 1") + + if not assign: + assign = ["public == 1", "notify_on_login == 1"] + + assignment_rule = xhiveframework.get_doc( + dict( + name=f"For {TEST_DOCTYPE} 1", + doctype="Assignment Rule", + priority=0, + document_type=TEST_DOCTYPE, + assign_condition=assign[0], + unassign_condition="public == 0 or notify_on_login == 1", + close_condition='"Closed" in content', + rule="Round Robin", + assignment_days=days[0], + users=[ + dict(user="test@example.com"), + dict(user="test1@example.com"), + dict(user="test2@example.com"), + ], + ) + ).insert() + + xhiveframework.delete_doc_if_exists("Assignment Rule", f"For {TEST_DOCTYPE} 2") + + # 2nd rule + xhiveframework.get_doc( + dict( + name=f"For {TEST_DOCTYPE} 2", + doctype="Assignment Rule", + priority=1, + document_type=TEST_DOCTYPE, + assign_condition=assign[1], + unassign_condition="notify_on_login == 0", + rule="Round Robin", + assignment_days=days[1], + users=[dict(user="test3@example.com")], + ) + ).insert() + + return assignment_rule + + +def _make_test_record( + *, + ignore_permissions=False, + **kwargs, +): + doc = xhiveframework.new_doc(TEST_DOCTYPE) + + if kwargs: + doc.update(kwargs) + + return doc.insert(ignore_permissions=ignore_permissions) + + +def create_test_doctype(doctype: str): + """Create custom doctype.""" + xhiveframework.delete_doc("DocType", doctype) + + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": doctype, + "module": "Custom", + "custom": 1, + "fields": [ + { + "fieldname": "expiry_date", + "label": "Expiry Date", + "fieldtype": "Date", + }, + { + "fieldname": "notify_on_login", + "label": "Notify on Login", + "fieldtype": "Check", + }, + { + "fieldname": "public", + "label": "Public", + "fieldtype": "Check", + }, + { + "fieldname": "content", + "label": "Content", + "fieldtype": "Text", + }, + ], + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1, + }, + ], + } + ).insert() diff --git a/xhiveframework/automation/doctype/assignment_rule_day/__init__.py b/xhiveframework/automation/doctype/assignment_rule_day/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.json b/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.json new file mode 100644 index 0000000..2a41879 --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.json @@ -0,0 +1,28 @@ +{ + "creation": "2019-09-21 16:52:01.705351", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "day" + ], + "fields": [ + { + "fieldname": "day", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Day", + "options": "Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday" + } + ], + "istable": 1, + "modified": "2019-09-21 16:55:09.376291", + "modified_by": "Administrator", + "module": "Automation", + "name": "Assignment Rule Day", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.py b/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.py new file mode 100644 index 0000000..00d548b --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule_day/assignment_rule_day.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class AssignmentRuleDay(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + day: DF.Literal["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/automation/doctype/assignment_rule_user/__init__.py b/xhiveframework/automation/doctype/assignment_rule_user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.json b/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.json new file mode 100644 index 0000000..5a159c8 --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.json @@ -0,0 +1,34 @@ +{ + "actions": [], + "allow_read": 1, + "creation": "2019-02-27 11:41:46.602400", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-09-29 20:12:14.456785", + "modified_by": "Administrator", + "module": "Automation", + "name": "Assignment Rule User", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.py b/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.py new file mode 100644 index 0000000..72936bc --- /dev/null +++ b/xhiveframework/automation/doctype/assignment_rule_user/assignment_rule_user.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class AssignmentRuleUser(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + user: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/automation/doctype/auto_repeat/__init__.py b/xhiveframework/automation/doctype/auto_repeat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/auto_repeat/auto_repeat.js b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.js new file mode 100644 index 0000000..a26a7ef --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.js @@ -0,0 +1,122 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt +xhiveframework.provide("xhiveframework.auto_repeat"); + +xhiveframework.ui.form.on("Auto Repeat", { + setup: function (frm) { + frm.fields_dict["reference_doctype"].get_query = function () { + return { + query: "xhiveframework.automation.doctype.auto_repeat.auto_repeat.get_auto_repeat_doctypes", + }; + }; + + frm.fields_dict["reference_document"].get_query = function () { + return { + filters: { + auto_repeat: "", + }, + }; + }; + + frm.fields_dict["print_format"].get_query = function () { + return { + filters: { + doc_type: frm.doc.reference_doctype, + }, + }; + }; + }, + + refresh: function (frm) { + // auto repeat message + if (frm.is_new()) { + let customize_form_link = `${__("Customize Form")}`; + frm.dashboard.set_headline( + __('To configure Auto Repeat, enable "Allow Auto Repeat" from {0}.', [ + customize_form_link, + ]) + ); + } + + // view document button + if (!frm.is_dirty()) { + let label = __("View {0}", [__(frm.doc.reference_doctype)]); + frm.add_custom_button(label, () => + xhiveframework.set_route("List", frm.doc.reference_doctype, { auto_repeat: frm.doc.name }) + ); + } + + // auto repeat schedule + xhiveframework.auto_repeat.render_schedule(frm); + + frm.trigger("toggle_submit_on_creation"); + }, + + reference_doctype: function (frm) { + frm.trigger("toggle_submit_on_creation"); + }, + + toggle_submit_on_creation: function (frm) { + // submit on creation checkbox + if (frm.doc.reference_doctype) { + xhiveframework.model.with_doctype(frm.doc.reference_doctype, () => { + let meta = xhiveframework.get_meta(frm.doc.reference_doctype); + frm.toggle_display("submit_on_creation", meta.is_submittable); + }); + } + }, + + template: function (frm) { + if (frm.doc.template) { + xhiveframework.model.with_doc("Email Template", frm.doc.template, () => { + let email_template = xhiveframework.get_doc("Email Template", frm.doc.template); + frm.set_value("subject", email_template.subject); + frm.set_value("message", email_template.response); + frm.refresh_field("subject"); + frm.refresh_field("message"); + }); + } + }, + + get_contacts: function (frm) { + frm.call("fetch_linked_contacts"); + }, + + preview_message: function (frm) { + if (frm.doc.message) { + xhiveframework.call({ + method: "xhiveframework.automation.doctype.auto_repeat.auto_repeat.generate_message_preview", + args: { + reference_dt: frm.doc.reference_doctype, + reference_doc: frm.doc.reference_document, + subject: frm.doc.subject, + message: frm.doc.message, + }, + callback: function (r) { + if (r.message) { + xhiveframework.msgprint(r.message.message, r.message.subject); + } + }, + }); + } else { + xhiveframework.msgprint(__("Please setup a message first"), __("Message not setup")); + } + }, +}); + +xhiveframework.auto_repeat.render_schedule = function (frm) { + if (!frm.is_dirty() && frm.doc.status !== "Disabled") { + frm.call("get_auto_repeat_schedule").then((r) => { + frm.dashboard.reset(); + frm.dashboard.add_section( + xhiveframework.render_template("auto_repeat_schedule", { + schedule_details: r.message || [], + }), + __("Auto Repeat Schedule") + ); + frm.dashboard.show(); + }); + } else { + frm.dashboard.hide(); + } +}; diff --git a/xhiveframework/automation/doctype/auto_repeat/auto_repeat.json b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.json new file mode 100644 index 0000000..7496534 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.json @@ -0,0 +1,262 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "autoname": "format:AUT-AR-{#####}", + "creation": "2018-03-09 11:22:31.192349", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "section_break_1", + "disabled", + "section_break_3", + "reference_doctype", + "reference_document", + "submit_on_creation", + "column_break_5", + "start_date", + "end_date", + "section_break_10", + "frequency", + "repeat_on_day", + "repeat_on_last_day", + "column_break_12", + "next_schedule_date", + "section_break_16", + "repeat_on_days", + "notification", + "notify_by_email", + "recipients", + "get_contacts", + "template", + "subject", + "message", + "preview_message", + "print_format", + "status" + ], + "fields": [ + { + "fieldname": "section_break_1", + "fieldtype": "Section Break" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "reference_document", + "fieldtype": "Dynamic Link", + "label": "Reference Document", + "no_copy": 1, + "options": "reference_doctype", + "reqd": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "default": "Today", + "fieldname": "start_date", + "fieldtype": "Date", + "in_list_view": 1, + "label": "Start Date", + "reqd": 1 + }, + { + "fieldname": "end_date", + "fieldtype": "Date", + "in_list_view": 1, + "label": "End Date" + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled", + "no_copy": 1 + }, + { + "fieldname": "section_break_10", + "fieldtype": "Section Break" + }, + { + "fieldname": "frequency", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Frequency", + "options": "\nDaily\nWeekly\nMonthly\nQuarterly\nHalf-yearly\nYearly", + "reqd": 1 + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: in_list([\"Monthly\", \"Quarterly\", \"Half-yearly\", \"Yearly\"], doc.frequency) && !doc.repeat_on_last_day\n", + "fieldname": "repeat_on_day", + "fieldtype": "Int", + "label": "Repeat on Day" + }, + { + "fieldname": "next_schedule_date", + "fieldtype": "Date", + "label": "Next Schedule Date", + "no_copy": 1, + "print_hide": 1, + "read_only": 1, + "search_index": 1 + }, + { + "collapsible": 1, + "fieldname": "notification", + "fieldtype": "Section Break", + "label": "Notification" + }, + { + "default": "0", + "fieldname": "notify_by_email", + "fieldtype": "Check", + "label": "Notify by Email" + }, + { + "depends_on": "notify_by_email", + "fieldname": "recipients", + "fieldtype": "Small Text", + "label": "Recipients" + }, + { + "depends_on": "eval: doc.notify_by_email && doc.reference_doctype && doc.reference_document", + "fieldname": "get_contacts", + "fieldtype": "Button", + "label": "Get Contacts" + }, + { + "depends_on": "eval: doc.notify_by_email", + "fieldname": "template", + "fieldtype": "Link", + "label": "Template", + "options": "Email Template" + }, + { + "depends_on": "eval: doc.notify_by_email", + "description": "To add dynamic subject, use jinja tags like\n\n
New {{ doc.doctype }} #{{ doc.name }}
", + "fieldname": "subject", + "fieldtype": "Data", + "label": "Subject" + }, + { + "default": "Please find attached {{ doc.doctype }} #{{ doc.name }}", + "depends_on": "eval: doc.notify_by_email", + "fieldname": "message", + "fieldtype": "Text", + "label": "Message" + }, + { + "depends_on": "eval: doc.notify_by_email && doc.reference_doctype && doc.reference_document", + "fieldname": "preview_message", + "fieldtype": "Button", + "label": "Preview Message" + }, + { + "depends_on": "notify_by_email", + "fieldname": "print_format", + "fieldtype": "Link", + "label": "Print Format", + "options": "Print Format" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "label": "Status", + "options": "\nActive\nDisabled\nCompleted", + "read_only": 1 + }, + { + "fieldname": "section_break_3", + "fieldtype": "Section Break" + }, + { + "default": "0", + "depends_on": "eval:doc.frequency === 'Monthly'", + "fieldname": "repeat_on_last_day", + "fieldtype": "Check", + "label": "Repeat on Last Day of the Month" + }, + { + "depends_on": "eval:doc.frequency==='Weekly';", + "fieldname": "repeat_on_days", + "fieldtype": "Table", + "label": "Repeat on Days", + "options": "Auto Repeat Day" + }, + { + "default": "0", + "fieldname": "submit_on_creation", + "fieldtype": "Check", + "label": "Submit on Creation" + }, + { + "depends_on": "eval:doc.frequency==='Weekly';", + "fieldname": "section_break_16", + "fieldtype": "Section Break" + } + ], + "links": [], + "modified": "2021-01-12 09:24:49.719611", + "modified_by": "Administrator", + "module": "Automation", + "name": "Auto Repeat", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Accounts Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Accounts User", + "share": 1, + "write": 1 + } + ], + "search_fields": "reference_document", + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "reference_document", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/auto_repeat/auto_repeat.py b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.py new file mode 100644 index 0000000..8c3687f --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/auto_repeat.py @@ -0,0 +1,575 @@ +# Copyright (c) 2018, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +from datetime import timedelta + +from dateutil.relativedelta import relativedelta + +import xhiveframework +from xhiveframework import _ +from xhiveframework.automation.doctype.assignment_rule.assignment_rule import get_repeated +from xhiveframework.contacts.doctype.contact.contact import ( + get_contacts_linked_from, + get_contacts_linking_to, +) +from xhiveframework.core.doctype.communication.email import make +from xhiveframework.desk.form import assign_to +from xhiveframework.model.document import Document +from xhiveframework.utils import ( + add_days, + cstr, + get_first_day, + get_last_day, + getdate, + month_diff, + split_emails, + today, +) +from xhiveframework.utils.background_jobs import get_jobs +from xhiveframework.utils.jinja import validate_template +from xhiveframework.utils.user import get_system_managers + +month_map = {"Monthly": 1, "Quarterly": 3, "Half-yearly": 6, "Yearly": 12} +week_map = { + "Monday": 0, + "Tuesday": 1, + "Wednesday": 2, + "Thursday": 3, + "Friday": 4, + "Saturday": 5, + "Sunday": 6, +} + + +class AutoRepeat(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.automation.doctype.auto_repeat_day.auto_repeat_day import AutoRepeatDay + from xhiveframework.types import DF + + disabled: DF.Check + end_date: DF.Date | None + frequency: DF.Literal["", "Daily", "Weekly", "Monthly", "Quarterly", "Half-yearly", "Yearly"] + message: DF.Text | None + next_schedule_date: DF.Date | None + notify_by_email: DF.Check + print_format: DF.Link | None + recipients: DF.SmallText | None + reference_doctype: DF.Link + reference_document: DF.DynamicLink + repeat_on_day: DF.Int + repeat_on_days: DF.Table[AutoRepeatDay] + repeat_on_last_day: DF.Check + start_date: DF.Date + status: DF.Literal["", "Active", "Disabled", "Completed"] + subject: DF.Data | None + submit_on_creation: DF.Check + template: DF.Link | None + + # end: auto-generated types + def validate(self): + self.update_status() + self.validate_reference_doctype() + self.validate_submit_on_creation() + self.validate_dates() + self.validate_email_id() + self.validate_auto_repeat_days() + self.set_dates() + self.update_auto_repeat_id() + self.unlink_if_applicable() + + validate_template(self.subject or "") + validate_template(self.message or "") + + def before_insert(self): + if not xhiveframework.flags.in_test: + start_date = getdate(self.start_date) + today_date = getdate(today()) + if start_date <= today_date: + self.start_date = today_date + + def after_save(self): + xhiveframework.get_doc(self.reference_doctype, self.reference_document).notify_update() + + def on_trash(self): + xhiveframework.db.set_value(self.reference_doctype, self.reference_document, "auto_repeat", "") + xhiveframework.get_doc(self.reference_doctype, self.reference_document).notify_update() + + def set_dates(self): + if self.disabled: + self.next_schedule_date = None + else: + self.next_schedule_date = self.get_next_schedule_date(schedule_date=self.start_date) + + def unlink_if_applicable(self): + if self.status == "Completed" or self.disabled: + xhiveframework.db.set_value(self.reference_doctype, self.reference_document, "auto_repeat", "") + + def validate_reference_doctype(self): + if xhiveframework.flags.in_test or xhiveframework.flags.in_patch: + return + if not xhiveframework.get_meta(self.reference_doctype).allow_auto_repeat: + xhiveframework.throw( + _("Enable Allow Auto Repeat for the doctype {0} in Customize Form").format( + self.reference_doctype + ) + ) + + def validate_submit_on_creation(self): + if self.submit_on_creation and not xhiveframework.get_meta(self.reference_doctype).is_submittable: + xhiveframework.throw( + _("Cannot enable {0} for a non-submittable doctype").format(xhiveframework.bold("Submit on Creation")) + ) + + def validate_dates(self): + if xhiveframework.flags.in_patch: + return + + if self.end_date: + self.validate_from_to_dates("start_date", "end_date") + + if self.end_date == self.start_date: + xhiveframework.throw( + _("{0} should not be same as {1}").format(xhiveframework.bold("End Date"), xhiveframework.bold("Start Date")) + ) + + def validate_email_id(self): + if self.notify_by_email: + if self.recipients: + email_list = split_emails(self.recipients.replace("\n", "")) + from xhiveframework.utils import validate_email_address + + for email in email_list: + if not validate_email_address(email): + xhiveframework.throw(_("{0} is an invalid email address in 'Recipients'").format(email)) + else: + xhiveframework.throw(_("'Recipients' not specified")) + + def validate_auto_repeat_days(self): + auto_repeat_days = self.get_auto_repeat_days() + if not len(set(auto_repeat_days)) == len(auto_repeat_days): + repeated_days = get_repeated(auto_repeat_days) + plural = "s" if len(repeated_days) > 1 else "" + + xhiveframework.throw( + _("Auto Repeat Day{0} {1} has been repeated.").format( + plural, xhiveframework.bold(", ".join(repeated_days)) + ) + ) + + def update_auto_repeat_id(self): + # check if document is already on auto repeat + auto_repeat = xhiveframework.db.get_value(self.reference_doctype, self.reference_document, "auto_repeat") + if auto_repeat and auto_repeat != self.name and not xhiveframework.flags.in_patch: + xhiveframework.throw( + _("The {0} is already on auto repeat {1}").format(self.reference_document, auto_repeat) + ) + else: + xhiveframework.db.set_value(self.reference_doctype, self.reference_document, "auto_repeat", self.name) + + def update_status(self): + if self.disabled: + self.status = "Disabled" + elif self.is_completed(): + self.status = "Completed" + else: + self.status = "Active" + + def is_completed(self): + return self.end_date and getdate(self.end_date) < getdate(today()) + + @xhiveframework.whitelist() + def get_auto_repeat_schedule(self): + schedule_details = [] + start_date = getdate(self.start_date) + end_date = getdate(self.end_date) + + if not self.end_date: + next_date = self.get_next_schedule_date(schedule_date=start_date) + row = { + "reference_document": self.reference_document, + "frequency": self.frequency, + "next_scheduled_date": next_date, + } + schedule_details.append(row) + + if self.end_date: + next_date = self.get_next_schedule_date(schedule_date=start_date, for_full_schedule=True) + + while getdate(next_date) < getdate(end_date): + row = { + "reference_document": self.reference_document, + "frequency": self.frequency, + "next_scheduled_date": next_date, + } + schedule_details.append(row) + next_date = self.get_next_schedule_date(schedule_date=next_date, for_full_schedule=True) + + return schedule_details + + def create_documents(self): + try: + new_doc = self.make_new_document() + if self.notify_by_email and self.recipients: + self.send_notification(new_doc) + except Exception: + error_log = self.log_error("Auto repeat failed") + + self.disable_auto_repeat() + + if self.reference_document and not xhiveframework.flags.in_test: + self.notify_error_to_user(error_log) + + def make_new_document(self): + reference_doc = xhiveframework.get_doc(self.reference_doctype, self.reference_document) + new_doc = xhiveframework.copy_doc(reference_doc, ignore_no_copy=False) + self.update_doc(new_doc, reference_doc) + new_doc.insert(ignore_permissions=True) + + if self.submit_on_creation: + new_doc.submit() + + return new_doc + + def update_doc(self, new_doc, reference_doc): + new_doc.docstatus = 0 + if new_doc.meta.get_field("set_posting_time"): + new_doc.set("set_posting_time", 1) + + if new_doc.meta.get_field("auto_repeat"): + new_doc.set("auto_repeat", self.name) + + for fieldname in [ + "naming_series", + "ignore_pricing_rule", + "posting_time", + "select_print_heading", + "user_remark", + "remarks", + "owner", + ]: + if new_doc.meta.get_field(fieldname): + new_doc.set(fieldname, reference_doc.get(fieldname)) + + for data in new_doc.meta.fields: + if data.fieldtype == "Date" and data.reqd: + new_doc.set(data.fieldname, self.next_schedule_date) + + self.set_auto_repeat_period(new_doc) + + auto_repeat_doc = xhiveframework.get_doc("Auto Repeat", self.name) + + # for any action that needs to take place after the recurring document creation + # on recurring method of that doctype is triggered + new_doc.run_method("on_recurring", reference_doc=reference_doc, auto_repeat_doc=auto_repeat_doc) + + def set_auto_repeat_period(self, new_doc): + mcount = month_map.get(self.frequency) + if mcount and new_doc.meta.get_field("from_date") and new_doc.meta.get_field("to_date"): + last_ref_doc = xhiveframework.get_all( + doctype=self.reference_doctype, + fields=["name", "from_date", "to_date"], + filters=[ + ["auto_repeat", "=", self.name], + ["docstatus", "<", 2], + ], + order_by="creation desc", + limit=1, + ) + + if not last_ref_doc: + return + + from_date = get_next_date(last_ref_doc[0].from_date, mcount) + + if (cstr(get_first_day(last_ref_doc[0].from_date)) == cstr(last_ref_doc[0].from_date)) and ( + cstr(get_last_day(last_ref_doc[0].to_date)) == cstr(last_ref_doc[0].to_date) + ): + to_date = get_last_day(get_next_date(last_ref_doc[0].to_date, mcount)) + else: + to_date = get_next_date(last_ref_doc[0].to_date, mcount) + + new_doc.set("from_date", from_date) + new_doc.set("to_date", to_date) + + def get_next_schedule_date(self, schedule_date, for_full_schedule=False): + """ + Returns the next schedule date for auto repeat after a recurring document has been created. + Adds required offset to the schedule_date param and returns the next schedule date. + + :param schedule_date: The date when the last recurring document was created. + :param for_full_schedule: If True, returns the immediate next schedule date, else the full schedule. + """ + if month_map.get(self.frequency): + month_count = month_map.get(self.frequency) + month_diff(schedule_date, self.start_date) - 1 + else: + month_count = 0 + + day_count = 0 + if month_count and self.repeat_on_last_day: + day_count = 31 + next_date = get_next_date(self.start_date, month_count, day_count) + elif month_count and self.repeat_on_day: + day_count = self.repeat_on_day + next_date = get_next_date(self.start_date, month_count, day_count) + elif month_count: + next_date = get_next_date(self.start_date, month_count) + else: + days = self.get_days(schedule_date) + next_date = add_days(schedule_date, days) + + # next schedule date should be after or on current date + if not for_full_schedule: + while getdate(next_date) < getdate(today()): + if month_count: + month_count += month_map.get(self.frequency, 0) + next_date = get_next_date(self.start_date, month_count, day_count) + else: + days = self.get_days(next_date) + next_date = add_days(next_date, days) + + return next_date + + def get_days(self, schedule_date): + if self.frequency == "Weekly": + days = self.get_offset_for_weekly_frequency(schedule_date) + else: + # daily frequency + days = 1 + + return days + + def get_offset_for_weekly_frequency(self, schedule_date): + # if weekdays are not set, offset is 7 from current schedule date + if not self.repeat_on_days: + return 7 + + repeat_on_days = self.get_auto_repeat_days() + current_schedule_day = getdate(schedule_date).weekday() + weekdays = list(week_map.keys()) + + # if repeats on more than 1 day or + # start date's weekday is not in repeat days, then get next weekday + # else offset is 7 + if len(repeat_on_days) > 1 or weekdays[current_schedule_day] not in repeat_on_days: + weekday = get_next_weekday(current_schedule_day, repeat_on_days) + next_weekday_number = week_map.get(weekday, 0) + # offset for upcoming weekday + return timedelta((7 + next_weekday_number - current_schedule_day) % 7).days + return 7 + + def get_auto_repeat_days(self): + return [d.day for d in self.get("repeat_on_days", [])] + + def send_notification(self, new_doc): + """Notify concerned people about recurring document generation""" + subject = self.subject or "" + message = self.message or "" + + if not self.subject: + subject = _("New {0}: {1}").format(new_doc.doctype, new_doc.name) + elif "{" in self.subject: + subject = xhiveframework.render_template(self.subject, {"doc": new_doc}) + + print_format = self.print_format or "Standard" + error_string = None + + try: + attachments = [ + xhiveframework.attach_print( + new_doc.doctype, new_doc.name, file_name=new_doc.name, print_format=print_format + ) + ] + + except xhiveframework.PermissionError: + error_string = _("A recurring {0} {1} has been created for you via Auto Repeat {2}.").format( + new_doc.doctype, new_doc.name, self.name + ) + error_string += "

" + + error_string += _( + "{0}: Failed to attach new recurring document. To enable attaching document in the auto repeat notification email, enable {1} in Print Settings" + ).format(xhiveframework.bold(_("Note")), xhiveframework.bold(_("Allow Print for Draft"))) + attachments = None + + if error_string: + message = error_string + elif not self.message: + message = _("Please find attached {0}: {1}").format(new_doc.doctype, new_doc.name) + elif "{" in self.message: + message = xhiveframework.render_template(self.message, {"doc": new_doc}) + + make( + doctype=new_doc.doctype, + name=new_doc.name, + recipients=self.recipients, + subject=subject, + content=message, + attachments=attachments, + send_email=1, + ) + + @xhiveframework.whitelist() + def fetch_linked_contacts(self): + if self.reference_doctype and self.reference_document: + res = get_contacts_linking_to( + self.reference_doctype, self.reference_document, fields=["email_id"] + ) + res += get_contacts_linked_from( + self.reference_doctype, self.reference_document, fields=["email_id"] + ) + email_ids = {d.email_id for d in res} + if not email_ids: + xhiveframework.msgprint(_("No contacts linked to document"), alert=True) + else: + self.recipients = ", ".join(email_ids) + + def disable_auto_repeat(self): + xhiveframework.db.set_value("Auto Repeat", self.name, "disabled", 1) + + def notify_error_to_user(self, error_log): + recipients = list(get_system_managers(only_name=True)) + recipients.append(self.owner) + subject = _("Auto Repeat Document Creation Failed") + + form_link = xhiveframework.utils.get_link_to_form(self.reference_doctype, self.reference_document) + auto_repeat_failed_for = _("Auto Repeat failed for {0}").format(form_link) + + error_log_link = xhiveframework.utils.get_link_to_form("Error Log", error_log.name) + error_log_message = _("Check the Error Log for more information: {0}").format(error_log_link) + + xhiveframework.sendmail( + recipients=recipients, + subject=subject, + template="auto_repeat_fail", + args={"auto_repeat_failed_for": auto_repeat_failed_for, "error_log_message": error_log_message}, + header=[subject, "red"], + ) + + +def get_next_date(dt, mcount, day=None): + dt = getdate(dt) + dt += relativedelta(months=mcount, day=day) + return dt + + +def get_next_weekday(current_schedule_day, weekdays): + days = list(week_map.keys()) + if current_schedule_day > 0: + days = days[(current_schedule_day + 1) :] + days[:current_schedule_day] + else: + days = days[(current_schedule_day + 1) :] + + for entry in days: + if entry in weekdays: + return entry + + +# called through hooks +def make_auto_repeat_entry(): + enqueued_method = "xhiveframework.automation.doctype.auto_repeat.auto_repeat.create_repeated_entries" + jobs = get_jobs() + + if not jobs or enqueued_method not in jobs[xhiveframework.local.site]: + date = getdate(today()) + data = get_auto_repeat_entries(date) + xhiveframework.enqueue(enqueued_method, data=data) + + +def create_repeated_entries(data): + for d in data: + doc = xhiveframework.get_doc("Auto Repeat", d.name) + + current_date = getdate(today()) + schedule_date = getdate(doc.next_schedule_date) + + if schedule_date == current_date and not doc.disabled: + doc.create_documents() + schedule_date = doc.get_next_schedule_date(schedule_date=schedule_date) + if schedule_date and not doc.disabled: + xhiveframework.db.set_value("Auto Repeat", doc.name, "next_schedule_date", schedule_date) + + +def get_auto_repeat_entries(date=None): + if not date: + date = getdate(today()) + return xhiveframework.get_all( + "Auto Repeat", filters=[["next_schedule_date", "<=", date], ["status", "=", "Active"]] + ) + + +# called through hooks +def set_auto_repeat_as_completed(): + auto_repeat = xhiveframework.get_all("Auto Repeat", filters={"status": ["!=", "Disabled"]}) + for entry in auto_repeat: + doc = xhiveframework.get_doc("Auto Repeat", entry.name) + if doc.is_completed(): + doc.status = "Completed" + doc.save() + + +@xhiveframework.whitelist() +def make_auto_repeat(doctype, docname, frequency="Daily", start_date=None, end_date=None): + if not start_date: + start_date = getdate(today()) + doc = xhiveframework.new_doc("Auto Repeat") + doc.reference_doctype = doctype + doc.reference_document = docname + doc.frequency = frequency + doc.start_date = start_date + if end_date: + doc.end_date = end_date + doc.save() + return doc + + +# method for reference_doctype filter +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_auto_repeat_doctypes(doctype, txt, searchfield, start, page_len, filters): + res = xhiveframework.get_all( + "Property Setter", + { + "property": "allow_auto_repeat", + "value": "1", + }, + ["doc_type"], + ) + docs = [r.doc_type for r in res] + + res = xhiveframework.get_all( + "DocType", + { + "allow_auto_repeat": 1, + }, + ["name"], + ) + docs += [r.name for r in res] + docs = set(list(docs)) + + return [[d] for d in docs] + + +@xhiveframework.whitelist() +def update_reference(docname: str, reference: str): + doc = xhiveframework.get_doc("Auto Repeat", str(docname)) + doc.check_permission("write") + doc.db_set("reference_document", str(reference)) + return "success" # backward compatbility + + +@xhiveframework.whitelist() +def generate_message_preview(reference_dt, reference_doc, message=None, subject=None): + xhiveframework.has_permission("Auto Repeat", "write", throw=True) + doc = xhiveframework.get_doc(reference_dt, reference_doc) + doc.check_permission() + subject_preview = _("Please add a subject to your email") + msg_preview = xhiveframework.render_template(message, {"doc": doc}) + if subject: + subject_preview = xhiveframework.render_template(subject, {"doc": doc}) + + return {"message": msg_preview, "subject": subject_preview} diff --git a/xhiveframework/automation/doctype/auto_repeat/auto_repeat_list.js b/xhiveframework/automation/doctype/auto_repeat/auto_repeat_list.js new file mode 100644 index 0000000..dbd54e3 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/auto_repeat_list.js @@ -0,0 +1,11 @@ +xhiveframework.listview_settings["Auto Repeat"] = { + add_fields: ["next_schedule_date"], + get_indicator: function (doc) { + var colors = { + Active: "green", + Disabled: "red", + Completed: "blue", + }; + return [__(doc.status), colors[doc.status], "status,=," + doc.status]; + }, +}; diff --git a/xhiveframework/automation/doctype/auto_repeat/auto_repeat_schedule.html b/xhiveframework/automation/doctype/auto_repeat/auto_repeat_schedule.html new file mode 100644 index 0000000..1b66e53 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/auto_repeat_schedule.html @@ -0,0 +1,19 @@ + + + + + + + + + + + {% for(var i=0; i < schedule_details.length; i++) { %} + + + + + + {% } %} + +
{{ __("Reference Document") }}{{ __("Frequency") }}{{ __("Next Scheduled Date") }}
{{ schedule_details[i].reference_document }} {{ __(schedule_details[i].frequency) }} {{ xhiveframework.datetime.str_to_user(schedule_details[i].next_scheduled_date) }}
diff --git a/xhiveframework/automation/doctype/auto_repeat/test_auto_repeat.py b/xhiveframework/automation/doctype/auto_repeat/test_auto_repeat.py new file mode 100644 index 0000000..4bf9bb7 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat/test_auto_repeat.py @@ -0,0 +1,283 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework.automation.doctype.auto_repeat.auto_repeat import ( + create_repeated_entries, + get_auto_repeat_entries, + week_map, +) +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_field +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import add_days, add_months, getdate, today + +if TYPE_CHECKING: + from xhiveframework.custom.doctype.custom_field.custom_field import CustomField + + +def add_custom_fields() -> "CustomField": + df = dict( + fieldname="auto_repeat", + label="Auto Repeat", + fieldtype="Link", + insert_after="sender", + options="Auto Repeat", + hidden=1, + print_hide=1, + read_only=1, + ) + return create_custom_field("ToDo", df) or xhiveframework.get_doc( + "Custom Field", dict(fieldname=df["fieldname"], dt="ToDo") + ) + + +class TestAutoRepeat(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + cls.custom_field = add_custom_fields() + cls.addClassCleanup(cls.custom_field.delete) + return super().setUpClass() + + def test_daily_auto_repeat(self): + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test recurring todo", assigned_by="Administrator") + ).insert() + + doc = make_auto_repeat(reference_document=todo.name) + self.assertEqual(doc.next_schedule_date, today()) + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + xhiveframework.db.commit() + + todo = xhiveframework.get_doc(doc.reference_doctype, doc.reference_document) + self.assertEqual(todo.auto_repeat, doc.name) + + new_todo = xhiveframework.db.get_value("ToDo", {"auto_repeat": doc.name, "name": ("!=", todo.name)}, "name") + + new_todo = xhiveframework.get_doc("ToDo", new_todo) + + self.assertEqual(todo.get("description"), new_todo.get("description")) + + def test_weekly_auto_repeat(self): + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test weekly todo", assigned_by="Administrator") + ).insert() + + doc = make_auto_repeat( + reference_doctype="ToDo", + frequency="Weekly", + reference_document=todo.name, + start_date=add_days(today(), -7), + ) + + self.assertEqual(doc.next_schedule_date, today()) + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + xhiveframework.db.commit() + + todo = xhiveframework.get_doc(doc.reference_doctype, doc.reference_document) + self.assertEqual(todo.auto_repeat, doc.name) + + new_todo = xhiveframework.db.get_value("ToDo", {"auto_repeat": doc.name, "name": ("!=", todo.name)}, "name") + + new_todo = xhiveframework.get_doc("ToDo", new_todo) + + self.assertEqual(todo.get("description"), new_todo.get("description")) + + def test_weekly_auto_repeat_with_weekdays(self): + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test auto repeat with weekdays", assigned_by="Administrator") + ).insert() + + weekdays = list(week_map.keys()) + current_weekday = getdate().weekday() + days = [{"day": weekdays[current_weekday]}, {"day": weekdays[(current_weekday + 2) % 7]}] + doc = make_auto_repeat( + reference_doctype="ToDo", + frequency="Weekly", + reference_document=todo.name, + start_date=add_days(today(), -7), + days=days, + ) + + self.assertEqual(doc.next_schedule_date, today()) + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + xhiveframework.db.commit() + + todo = xhiveframework.get_doc(doc.reference_doctype, doc.reference_document) + self.assertEqual(todo.auto_repeat, doc.name) + + doc.reload() + self.assertEqual(doc.next_schedule_date, add_days(getdate(), 2)) + + def test_monthly_auto_repeat(self): + start_date = today() + end_date = add_months(start_date, 12) + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test recurring todo", assigned_by="Administrator") + ).insert() + + self.monthly_auto_repeat("ToDo", todo.name, start_date, end_date) + # test without end_date + todo = xhiveframework.get_doc( + dict( + doctype="ToDo", + description="test recurring todo without end_date", + assigned_by="Administrator", + ) + ).insert() + self.monthly_auto_repeat("ToDo", todo.name, start_date) + + def monthly_auto_repeat(self, doctype, docname, start_date, end_date=None): + def get_months(start, end): + diff = (12 * end.year + end.month) - (12 * start.year + start.month) + return diff + 1 + + doc = make_auto_repeat( + reference_doctype=doctype, + frequency="Monthly", + reference_document=docname, + start_date=start_date, + end_date=end_date, + ) + + doc.disable_auto_repeat() + + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + docnames = xhiveframework.get_all(doc.reference_doctype, {"auto_repeat": doc.name}) + self.assertEqual(len(docnames), 1) + + doc = xhiveframework.get_doc("Auto Repeat", doc.name) + doc.db_set("disabled", 0) + + months = get_months(getdate(start_date), getdate(today())) + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + + docnames = xhiveframework.get_all(doc.reference_doctype, {"auto_repeat": doc.name}) + self.assertEqual(len(docnames), months) + + def test_email_notification(self): + todo = xhiveframework.get_doc( + dict( + doctype="ToDo", + description="Test recurring notification attachment", + assigned_by="Administrator", + ) + ).insert() + + doc = make_auto_repeat( + reference_document=todo.name, + notify=1, + recipients="test@domain.com", + subject="New ToDo", + message="A new ToDo has just been created for you", + ) + data = get_auto_repeat_entries(getdate(today())) + create_repeated_entries(data) + xhiveframework.db.commit() + + new_todo = xhiveframework.db.get_value("ToDo", {"auto_repeat": doc.name, "name": ("!=", todo.name)}, "name") + + email_queue = xhiveframework.db.exists("Email Queue", dict(reference_doctype="ToDo", reference_name=new_todo)) + self.assertTrue(email_queue) + + def test_next_schedule_date(self): + current_date = getdate(today()) + todo = xhiveframework.get_doc( + dict( + doctype="ToDo", description="test next schedule date for monthly", assigned_by="Administrator" + ) + ).insert() + doc = make_auto_repeat( + frequency="Monthly", reference_document=todo.name, start_date=add_months(today(), -2) + ) + + # next_schedule_date is set as on or after current date + # it should not be a previous month's date + self.assertTrue(doc.next_schedule_date >= current_date) + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test next schedule date for daily", assigned_by="Administrator") + ).insert() + doc = make_auto_repeat( + frequency="Daily", reference_document=todo.name, start_date=add_days(today(), -2) + ) + self.assertEqual(getdate(doc.next_schedule_date), current_date) + + def test_submit_on_creation(self): + doctype = "Test Submittable DocType" + create_submittable_doctype(doctype) + + current_date = getdate() + submittable_doc = xhiveframework.get_doc(dict(doctype=doctype, test="test submit on creation")).insert() + submittable_doc.submit() + doc = make_auto_repeat( + frequency="Daily", + reference_doctype=doctype, + reference_document=submittable_doc.name, + start_date=add_days(current_date, -1), + submit_on_creation=1, + ) + + data = get_auto_repeat_entries(current_date) + create_repeated_entries(data) + docnames = xhiveframework.get_all( + doc.reference_doctype, filters={"auto_repeat": doc.name}, fields=["docstatus"], limit=1 + ) + self.assertEqual(docnames[0].docstatus, 1) + + +def make_auto_repeat(**args): + args = xhiveframework._dict(args) + return xhiveframework.get_doc( + { + "doctype": "Auto Repeat", + "reference_doctype": args.reference_doctype or "ToDo", + "reference_document": args.reference_document or xhiveframework.db.get_value("ToDo", "name"), + "submit_on_creation": args.submit_on_creation or 0, + "frequency": args.frequency or "Daily", + "start_date": args.start_date or add_days(today(), -1), + "end_date": args.end_date or "", + "notify_by_email": args.notify or 0, + "recipients": args.recipients or "", + "subject": args.subject or "", + "message": args.message or "", + "repeat_on_days": args.days or [], + } + ).insert(ignore_permissions=True) + + +def create_submittable_doctype(doctype, submit_perms=1): + if xhiveframework.db.exists("DocType", doctype): + return + else: + doc = xhiveframework.get_doc( + { + "doctype": "DocType", + "__newname": doctype, + "module": "Custom", + "custom": 1, + "is_submittable": 1, + "fields": [{"fieldname": "test", "label": "Test", "fieldtype": "Data"}], + "permissions": [ + { + "role": "System Manager", + "read": 1, + "write": 1, + "create": 1, + "delete": 1, + "submit": submit_perms, + "cancel": submit_perms, + "amend": submit_perms, + } + ], + } + ).insert() + + doc.allow_auto_repeat = 1 + doc.save() diff --git a/xhiveframework/automation/doctype/auto_repeat_day/__init__.py b/xhiveframework/automation/doctype/auto_repeat_day/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.json b/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.json new file mode 100644 index 0000000..6f5c306 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.json @@ -0,0 +1,33 @@ +{ + "actions": [], + "creation": "2020-11-10 22:30:53.690228", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "day" + ], + "fields": [ + { + "fieldname": "day", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Day", + "options": "Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-11-10 22:30:53.690228", + "modified_by": "Administrator", + "module": "Automation", + "name": "Auto Repeat Day", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.py b/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.py new file mode 100644 index 0000000..72adeb4 --- /dev/null +++ b/xhiveframework/automation/doctype/auto_repeat_day/auto_repeat_day.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class AutoRepeatDay(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + day: DF.Literal["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/automation/doctype/milestone/__init__.py b/xhiveframework/automation/doctype/milestone/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/milestone/milestone.js b/xhiveframework/automation/doctype/milestone/milestone.js new file mode 100644 index 0000000..a5ad489 --- /dev/null +++ b/xhiveframework/automation/doctype/milestone/milestone.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Milestone", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/automation/doctype/milestone/milestone.json b/xhiveframework/automation/doctype/milestone/milestone.json new file mode 100644 index 0000000..db3d61b --- /dev/null +++ b/xhiveframework/automation/doctype/milestone/milestone.json @@ -0,0 +1,81 @@ +{ + "actions": [], + "creation": "2019-04-17 09:39:15.647817", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "reference_type", + "reference_name", + "track_field", + "value", + "milestone_tracker" + ], + "fields": [ + { + "fieldname": "reference_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "read_only": 1, + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "reference_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Document", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "track_field", + "fieldtype": "Data", + "label": "Track Field", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "value", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Value", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "milestone_tracker", + "fieldtype": "Link", + "label": "Milestone Tracker", + "options": "Milestone Tracker" + } + ], + "in_create": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Automation", + "name": "Milestone", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "reference_type", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/milestone/milestone.py b/xhiveframework/automation/doctype/milestone/milestone.py new file mode 100644 index 0000000..deb735c --- /dev/null +++ b/xhiveframework/automation/doctype/milestone/milestone.py @@ -0,0 +1,27 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class Milestone(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + milestone_tracker: DF.Link | None + reference_name: DF.Data + reference_type: DF.Link + track_field: DF.Data + value: DF.Data + # end: auto-generated types + pass + + +def on_doctype_update(): + xhiveframework.db.add_index("Milestone", ["reference_type", "reference_name"]) diff --git a/xhiveframework/automation/doctype/milestone/test_milestone.py b/xhiveframework/automation/doctype/milestone/test_milestone.py new file mode 100644 index 0000000..5e5694b --- /dev/null +++ b/xhiveframework/automation/doctype/milestone/test_milestone.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestMilestone(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/automation/doctype/milestone_tracker/__init__.py b/xhiveframework/automation/doctype/milestone_tracker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.js b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.js new file mode 100644 index 0000000..106b2d6 --- /dev/null +++ b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.js @@ -0,0 +1,31 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Milestone Tracker", { + refresh: function (frm) { + frm.trigger("update_options"); + }, + document_type: function (frm) { + frm.trigger("update_options"); + }, + update_options: function (frm) { + // update select options for `track_field` + let doctype = frm.doc.document_type; + let track_fields = []; + + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => { + // get all date and datetime fields + xhiveframework.get_meta(doctype).fields.map((df) => { + if (["Link", "Select"].includes(df.fieldtype)) { + track_fields.push({ label: df.label, value: df.fieldname }); + } + }); + frm.set_df_property("track_field", "options", track_fields); + }); + } else { + // update select options + frm.set_df_property("track_field", "options", []); + } + }, +}); diff --git a/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.json b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.json new file mode 100644 index 0000000..f0dc452 --- /dev/null +++ b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.json @@ -0,0 +1,61 @@ +{ + "actions": [], + "autoname": "format:{document_type}-{track_field}", + "creation": "2019-04-17 09:36:41.774774", + "description": "Track milestones for any document", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "document_type", + "track_field", + "disabled" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type to Track", + "options": "DocType", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "track_field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Field to Track", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + } + ], + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Automation", + "name": "Milestone Tracker", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.py b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.py new file mode 100644 index 0000000..decef5d --- /dev/null +++ b/xhiveframework/automation/doctype/milestone_tracker/milestone_tracker.py @@ -0,0 +1,64 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +import xhiveframework.cache_manager +from xhiveframework.model import log_types +from xhiveframework.model.document import Document + + +class MilestoneTracker(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + disabled: DF.Check + document_type: DF.Link + track_field: DF.Literal[None] + # end: auto-generated types + + def on_update(self): + xhiveframework.cache_manager.clear_doctype_map("Milestone Tracker", self.document_type) + + def on_trash(self): + xhiveframework.cache_manager.clear_doctype_map("Milestone Tracker", self.document_type) + + def apply(self, doc): + before_save = doc.get_doc_before_save() + from_value = before_save and before_save.get(self.track_field) or None + if from_value != doc.get(self.track_field): + xhiveframework.get_doc( + dict( + doctype="Milestone", + reference_type=doc.doctype, + reference_name=doc.name, + track_field=self.track_field, + from_value=from_value, + value=doc.get(self.track_field), + milestone_tracker=self.name, + ) + ).insert(ignore_permissions=True) + + +def evaluate_milestone(doc, event): + if ( + xhiveframework.flags.in_install + or xhiveframework.flags.in_migrate + or xhiveframework.flags.in_setup_wizard + or doc.doctype in log_types + ): + return + + # track milestones related to this doctype + for d in get_milestone_trackers(doc.doctype): + xhiveframework.get_doc("Milestone Tracker", d.get("name")).apply(doc) + + +def get_milestone_trackers(doctype): + return xhiveframework.cache_manager.get_doctype_map( + "Milestone Tracker", doctype, dict(document_type=doctype, disabled=0) + ) diff --git a/xhiveframework/automation/doctype/milestone_tracker/test_milestone_tracker.py b/xhiveframework/automation/doctype/milestone_tracker/test_milestone_tracker.py new file mode 100644 index 0000000..45f779c --- /dev/null +++ b/xhiveframework/automation/doctype/milestone_tracker/test_milestone_tracker.py @@ -0,0 +1,46 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +import xhiveframework.cache_manager +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestMilestoneTracker(XhiveFrameworkTestCase): + def test_milestone(self): + xhiveframework.db.delete("Milestone Tracker") + + xhiveframework.cache.delete_key("milestone_tracker_map") + + milestone_tracker = xhiveframework.get_doc( + dict(doctype="Milestone Tracker", document_type="ToDo", track_field="status") + ).insert() + + todo = xhiveframework.get_doc(dict(doctype="ToDo", description="test milestone", status="Open")).insert() + + milestones = xhiveframework.get_all( + "Milestone", + fields=["track_field", "value", "milestone_tracker"], + filters=dict(reference_type=todo.doctype, reference_name=todo.name), + ) + + self.assertEqual(len(milestones), 1) + self.assertEqual(milestones[0].track_field, "status") + self.assertEqual(milestones[0].value, "Open") + + todo.status = "Closed" + todo.save() + + milestones = xhiveframework.get_all( + "Milestone", + fields=["track_field", "value", "milestone_tracker"], + filters=dict(reference_type=todo.doctype, reference_name=todo.name), + order_by="modified desc", + ) + + self.assertEqual(len(milestones), 2) + self.assertEqual(milestones[0].track_field, "status") + self.assertEqual(milestones[0].value, "Closed") + + # cleanup + xhiveframework.db.delete("Milestone") + milestone_tracker.delete() diff --git a/xhiveframework/automation/doctype/reminder/__init__.py b/xhiveframework/automation/doctype/reminder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/automation/doctype/reminder/reminder.js b/xhiveframework/automation/doctype/reminder/reminder.js new file mode 100644 index 0000000..ef7e92e --- /dev/null +++ b/xhiveframework/automation/doctype/reminder/reminder.js @@ -0,0 +1,8 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +// xhiveframework.ui.form.on("Reminder", { +// refresh(frm) { + +// }, +// }); diff --git a/xhiveframework/automation/doctype/reminder/reminder.json b/xhiveframework/automation/doctype/reminder/reminder.json new file mode 100644 index 0000000..07dbe77 --- /dev/null +++ b/xhiveframework/automation/doctype/reminder/reminder.json @@ -0,0 +1,90 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2023-02-22 11:23:58.183276", + "default_view": "List", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user", + "remind_at", + "description", + "reminder_doctype", + "reminder_docname", + "notified" + ], + "fields": [ + { + "default": "__user", + "fieldname": "user", + "fieldtype": "Link", + "hidden": 1, + "label": "User", + "options": "User", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "reminder_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "reminder_docname", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Document Name", + "options": "reminder_doctype", + "read_only": 1 + }, + { + "default": "now", + "fieldname": "remind_at", + "fieldtype": "Datetime", + "in_list_view": 1, + "label": "Remind At", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "description", + "fieldtype": "Small Text", + "label": "Description", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "notified", + "fieldtype": "Check", + "hidden": 1, + "label": "notified" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 22:17:18.351025", + "modified_by": "Administrator", + "module": "Automation", + "name": "Reminder", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "if_owner": 1, + "read": 1, + "role": "Desk User", + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "description" +} \ No newline at end of file diff --git a/xhiveframework/automation/doctype/reminder/reminder.py b/xhiveframework/automation/doctype/reminder/reminder.py new file mode 100644 index 0000000..73168e6 --- /dev/null +++ b/xhiveframework/automation/doctype/reminder/reminder.py @@ -0,0 +1,94 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import cint +from xhiveframework.utils.data import add_to_date, get_datetime, now_datetime + + +class Reminder(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + description: DF.SmallText + notified: DF.Check + remind_at: DF.Datetime + reminder_docname: DF.DynamicLink | None + reminder_doctype: DF.Link | None + user: DF.Link + + # end: auto-generated types + @staticmethod + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Reminder") + xhiveframework.db.delete(table, filters=(table.remind_at < (Now() - Interval(days=days)))) + + def validate(self): + self.user = xhiveframework.session.user + if get_datetime(self.remind_at) < now_datetime(): + xhiveframework.throw(_("Reminder cannot be created in past.")) + + def send_reminder(self): + if self.notified: + return + + self.db_set("notified", 1, update_modified=False) + + try: + notification = xhiveframework.new_doc("Notification Log") + notification.for_user = self.user + notification.set("type", "Alert") + notification.document_type = self.reminder_doctype + notification.document_name = self.reminder_docname + notification.subject = self.description + notification.insert() + except Exception: + self.log_error("Failed to send reminder") + + +@xhiveframework.whitelist() +def create_new_reminder( + remind_at: str, + description: str, + reminder_doctype: str | None = None, + reminder_docname: str | None = None, +): + reminder = xhiveframework.new_doc("Reminder") + + reminder.description = description + reminder.remind_at = remind_at + reminder.reminder_doctype = reminder_doctype + reminder.reminder_docname = reminder_docname + + return reminder.insert() + + +def send_reminders(): + # Ensure that we send all reminders that might be before next job execution. + job_freq = cint(xhiveframework.get_conf().scheduler_interval) or 240 + upper_threshold = add_to_date(now_datetime(), seconds=job_freq, as_string=True, as_datetime=True) + + lower_threshold = add_to_date(now_datetime(), hours=-8, as_string=True, as_datetime=True) + + pending_reminders = xhiveframework.get_all( + "Reminder", + filters=[ + ("remind_at", "<=", upper_threshold), + ("remind_at", ">=", lower_threshold), # dont send too old reminders if failed to send + ("notified", "=", 0), + ], + pluck="name", + ) + + for reminder in pending_reminders: + xhiveframework.get_doc("Reminder", reminder).send_reminder() diff --git a/xhiveframework/automation/doctype/reminder/test_reminder.py b/xhiveframework/automation/doctype/reminder/test_reminder.py new file mode 100644 index 0000000..079d9ab --- /dev/null +++ b/xhiveframework/automation/doctype/reminder/test_reminder.py @@ -0,0 +1,27 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +import xhiveframework +from xhiveframework.automation.doctype.reminder.reminder import create_new_reminder, send_reminders +from xhiveframework.desk.doctype.notification_log.notification_log import get_notification_logs +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import add_to_date, now_datetime + + +class TestReminder(XhiveFrameworkTestCase): + def test_reminder(self): + description = "TEST_REMINDER" + + create_new_reminder( + remind_at=add_to_date(now_datetime(), minutes=1, as_datetime=True, as_string=True), + description=description, + ) + + send_reminders() + + notifications = get_notification_logs()["notification_logs"] + self.assertIn( + description, + [n.subject for n in notifications], + msg=f"Failed to find reminder notification \n{notifications}", + ) diff --git a/xhiveframework/automation/workspace/tools/tools.json b/xhiveframework/automation/workspace/tools/tools.json new file mode 100644 index 0000000..2c46414 --- /dev/null +++ b/xhiveframework/automation/workspace/tools/tools.json @@ -0,0 +1,360 @@ +{ + "charts": [], + "content": "[{\"id\":\"-P-RG1wVHg\",\"type\":\"header\",\"data\":{\"text\":\"Your Shortcuts\",\"col\":12}},{\"id\":\"sR-UFcO7II\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Import Data\",\"col\":3}},{\"id\":\"IkcVmgWb3z\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"ToDo\",\"col\":3}},{\"id\":\"6wir-jZFRE\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"File\",\"col\":3}},{\"id\":\"45a1jzQkTm\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Assignment Rule\",\"col\":3}},{\"id\":\"LdZrgvxxo7\",\"type\":\"spacer\",\"data\":{\"col\":12}},{\"id\":\"yNSSTIaDWZ\",\"type\":\"header\",\"data\":{\"text\":\"Documents\",\"col\":12}},{\"id\":\"0yceBIfhHM\",\"type\":\"card\",\"data\":{\"card_name\":\"Data\",\"col\":4}},{\"id\":\"42WbBA9rpj\",\"type\":\"card\",\"data\":{\"card_name\":\"Tools\",\"col\":4}},{\"id\":\"wE9n7TIrAc\",\"type\":\"card\",\"data\":{\"card_name\":\"Alerts and Notifications\",\"col\":4}},{\"id\":\"7_U7_xCOos\",\"type\":\"card\",\"data\":{\"card_name\":\"Email\",\"col\":4}},{\"id\":\"3imoh2oqsJ\",\"type\":\"card\",\"data\":{\"card_name\":\"Printing\",\"col\":4}},{\"id\":\"SlYKJZj5r3\",\"type\":\"card\",\"data\":{\"card_name\":\"Automation\",\"col\":4}},{\"id\":\"O7jrc2YQTN\",\"type\":\"card\",\"data\":{\"card_name\":\"Newsletter\",\"col\":4}}]", + "creation": "2020-03-02 14:53:24.980279", + "custom_blocks": [], + "docstatus": 0, + "doctype": "Workspace", + "for_user": "", + "hide_custom": 0, + "icon": "tool", + "idx": 0, + "is_hidden": 0, + "label": "Tools", + "links": [ + { + "hidden": 0, + "is_query_report": 0, + "label": "Automation", + "link_count": 3, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Assignment Rule", + "link_count": 0, + "link_to": "Assignment Rule", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Milestone", + "link_count": 0, + "link_to": "Milestone", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Auto Repeat", + "link_count": 0, + "link_to": "Auto Repeat", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Tools", + "link_count": 4, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "To Do", + "link_count": 0, + "link_to": "ToDo", + "link_type": "DocType", + "onboard": 1, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Calendar", + "link_count": 0, + "link_to": "Event", + "link_type": "DocType", + "onboard": 1, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Note", + "link_count": 0, + "link_to": "Note", + "link_type": "DocType", + "onboard": 1, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Files", + "link_count": 0, + "link_to": "File", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Data", + "link_count": 5, + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Import Data", + "link_count": 0, + "link_to": "Data Import", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Export Data", + "link_count": 0, + "link_to": "Data Export", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Bulk Update", + "link_count": 0, + "link_to": "Bulk Update", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Download Backups", + "link_count": 0, + "link_to": "backups", + "link_type": "Page", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Deleted Documents", + "link_count": 0, + "link_to": "Deleted Document", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Email", + "link_count": 3, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Email Account", + "link_count": 0, + "link_to": "Email Account", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Email Domain", + "link_count": 0, + "link_to": "Email Domain", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Email Template", + "link_count": 0, + "link_to": "Email Template", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Newsletter", + "link_count": 2, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Newsletter", + "link_count": 0, + "link_to": "Newsletter", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Email Group", + "link_count": 0, + "link_to": "Email Group", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Printing", + "link_count": 4, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Print Format Builder", + "link_count": 0, + "link_to": "print-format-builder", + "link_type": "Page", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Print Format Builder (New)", + "link_count": 0, + "link_to": "print-format-builder-beta", + "link_type": "Page", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Print Settings", + "link_count": 0, + "link_to": "Print Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Print Heading", + "link_count": 0, + "link_to": "Print Heading", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Alerts and Notifications", + "link_count": 3, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Notification", + "link_count": 0, + "link_to": "Notification", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Auto Email Report", + "link_count": 0, + "link_to": "Auto Email Report", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Notification Settings", + "link_count": 0, + "link_to": "Notification Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + } + ], + "modified": "2024-01-02 15:47:12.939478", + "modified_by": "Administrator", + "module": "Automation", + "name": "Tools", + "number_cards": [], + "owner": "Administrator", + "parent_page": "", + "public": 1, + "quick_lists": [], + "restrict_to_domain": "", + "roles": [], + "sequence_id": 17.0, + "shortcuts": [ + { + "color": "Grey", + "doc_view": "List", + "label": "Import Data", + "link_to": "Data Import", + "type": "DocType" + }, + { + "label": "ToDo", + "link_to": "ToDo", + "type": "DocType" + }, + { + "label": "File", + "link_to": "File", + "type": "DocType" + }, + { + "label": "Assignment Rule", + "link_to": "Assignment Rule", + "type": "DocType" + } + ], + "title": "Tools" +} \ No newline at end of file diff --git a/xhiveframework/boot.py b/xhiveframework/boot.py new file mode 100644 index 0000000..02c77e8 --- /dev/null +++ b/xhiveframework/boot.py @@ -0,0 +1,470 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +""" +bootstrap client session +""" + +import xhiveframework +import xhiveframework.defaults +import xhiveframework.desk.desk_page +from xhiveframework.core.doctype.navbar_settings.navbar_settings import get_app_logo, get_navbar_settings +from xhiveframework.desk.doctype.form_tour.form_tour import get_onboarding_ui_tours +from xhiveframework.desk.doctype.route_history.route_history import frequently_visited_links +from xhiveframework.desk.form.load import get_meta_bundle +from xhiveframework.email.inbox import get_email_accounts +from xhiveframework.model.base_document import get_controller +from xhiveframework.permissions import has_permission +from xhiveframework.query_builder import DocType +from xhiveframework.query_builder.functions import Count +from xhiveframework.query_builder.terms import ParameterizedValueWrapper, SubQuery +from xhiveframework.social.doctype.energy_point_log.energy_point_log import get_energy_points +from xhiveframework.social.doctype.energy_point_settings.energy_point_settings import ( + is_energy_point_enabled, +) +from xhiveframework.utils import add_user_info, cstr, get_system_timezone +from xhiveframework.utils.change_log import get_versions +from xhiveframework.website.doctype.web_page_view.web_page_view import is_tracking_enabled + + +def get_bootinfo(): + """build and return boot info""" + from xhiveframework.translate import get_lang_dict, get_translated_doctypes + + xhiveframework.set_user_lang(xhiveframework.session.user) + bootinfo = xhiveframework._dict() + hooks = xhiveframework.get_hooks() + doclist = [] + + # user + get_user(bootinfo) + + # system info + bootinfo.sitename = xhiveframework.local.site + bootinfo.sysdefaults = xhiveframework.defaults.get_defaults() + bootinfo.server_date = xhiveframework.utils.nowdate() + + if xhiveframework.session["user"] != "Guest": + bootinfo.user_info = get_user_info() + bootinfo.sid = xhiveframework.session["sid"] + + bootinfo.modules = {} + bootinfo.module_list = [] + load_desktop_data(bootinfo) + bootinfo.letter_heads = get_letter_heads() + bootinfo.active_domains = xhiveframework.get_active_domains() + bootinfo.all_domains = [d.get("name") for d in xhiveframework.get_all("Domain")] + add_layouts(bootinfo) + + bootinfo.module_app = xhiveframework.local.module_app + bootinfo.single_types = [d.name for d in xhiveframework.get_all("DocType", {"issingle": 1})] + bootinfo.nested_set_doctypes = [ + d.parent for d in xhiveframework.get_all("DocField", {"fieldname": "lft"}, ["parent"]) + ] + add_home_page(bootinfo, doclist) + bootinfo.page_info = get_allowed_pages() + load_translations(bootinfo) + add_timezone_info(bootinfo) + load_conf_settings(bootinfo) + load_print(bootinfo, doclist) + doclist.extend(get_meta_bundle("Page")) + bootinfo.home_folder = xhiveframework.db.get_value("File", {"is_home_folder": 1}) + bootinfo.navbar_settings = get_navbar_settings() + bootinfo.notification_settings = get_notification_settings() + bootinfo.onboarding_tours = get_onboarding_ui_tours() + set_time_zone(bootinfo) + + # ipinfo + if xhiveframework.session.data.get("ipinfo"): + bootinfo.ipinfo = xhiveframework.session["data"]["ipinfo"] + + # add docs + bootinfo.docs = doclist + load_country_doc(bootinfo) + load_currency_docs(bootinfo) + + for method in hooks.boot_session or []: + xhiveframework.get_attr(method)(bootinfo) + + if bootinfo.lang: + bootinfo.lang = str(bootinfo.lang) + bootinfo.versions = {k: v["version"] for k, v in get_versions().items()} + + bootinfo.error_report_email = xhiveframework.conf.error_report_email + bootinfo.calendars = sorted(xhiveframework.get_hooks("calendars")) + bootinfo.treeviews = xhiveframework.get_hooks("treeviews") or [] + bootinfo.lang_dict = get_lang_dict() + bootinfo.success_action = get_success_action() + bootinfo.update(get_email_accounts(user=xhiveframework.session.user)) + bootinfo.energy_points_enabled = is_energy_point_enabled() + bootinfo.website_tracking_enabled = is_tracking_enabled() + bootinfo.points = get_energy_points(xhiveframework.session.user) + bootinfo.frequently_visited_links = frequently_visited_links() + bootinfo.link_preview_doctypes = get_link_preview_doctypes() + 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() + bootinfo.translated_doctypes = get_translated_doctypes() + bootinfo.subscription_conf = add_subscription_conf() + bootinfo.marketplace_apps = get_marketplace_apps() + + return bootinfo + + +def get_letter_heads(): + letter_heads = {} + for letter_head in xhiveframework.get_all("Letter Head", fields=["name", "content", "footer"]): + letter_heads.setdefault( + letter_head.name, {"header": letter_head.content, "footer": letter_head.footer} + ) + + return letter_heads + + +def load_conf_settings(bootinfo): + from xhiveframework.core.api.file import get_max_file_size + + bootinfo.max_file_size = get_max_file_size() + for key in ("developer_mode", "socketio_port", "file_watcher_port"): + if key in xhiveframework.conf: + bootinfo[key] = xhiveframework.conf.get(key) + + +def load_desktop_data(bootinfo): + from xhiveframework.desk.desktop import get_workspace_sidebar_items + + bootinfo.allowed_workspaces = get_workspace_sidebar_items().get("pages") + bootinfo.module_wise_workspaces = get_controller("Workspace").get_module_wise_workspaces() + bootinfo.dashboards = xhiveframework.get_all("Dashboard") + + +def get_allowed_pages(cache=False): + return get_user_pages_or_reports("Page", cache=cache) + + +def get_allowed_reports(cache=False): + return get_user_pages_or_reports("Report", cache=cache) + + +def get_allowed_report_names(cache=False) -> set[str]: + return {cstr(report) for report in get_allowed_reports(cache).keys() if report} + + +def get_user_pages_or_reports(parent, cache=False): + if cache: + has_role = xhiveframework.cache.get_value("has_role:" + parent, user=xhiveframework.session.user) + if has_role: + return has_role + + roles = xhiveframework.get_roles() + has_role = {} + + page = DocType("Page") + report = DocType("Report") + + if parent == "Report": + columns = (report.name.as_("title"), report.ref_doctype, report.report_type) + else: + columns = (page.title.as_("title"),) + + customRole = DocType("Custom Role") + hasRole = DocType("Has Role") + parentTable = DocType(parent) + + # get pages or reports set on custom role + pages_with_custom_roles = ( + xhiveframework.qb.from_(customRole) + .from_(hasRole) + .from_(parentTable) + .select(customRole[parent.lower()].as_("name"), customRole.modified, customRole.ref_doctype, *columns) + .where( + (hasRole.parent == customRole.name) + & (parentTable.name == customRole[parent.lower()]) + & (customRole[parent.lower()].isnotnull()) + & (hasRole.role.isin(roles)) + ) + ).run(as_dict=True) + + for p in pages_with_custom_roles: + has_role[p.name] = {"modified": p.modified, "title": p.title, "ref_doctype": p.ref_doctype} + + subq = ( + xhiveframework.qb.from_(customRole) + .select(customRole[parent.lower()]) + .where(customRole[parent.lower()].isnotnull()) + ) + + pages_with_standard_roles = ( + xhiveframework.qb.from_(hasRole) + .from_(parentTable) + .select(parentTable.name.as_("name"), parentTable.modified, *columns) + .where( + (hasRole.role.isin(roles)) & (hasRole.parent == parentTable.name) & (parentTable.name.notin(subq)) + ) + .distinct() + ) + + if parent == "Report": + pages_with_standard_roles = pages_with_standard_roles.where(report.disabled == 0) + + pages_with_standard_roles = pages_with_standard_roles.run(as_dict=True) + + for p in pages_with_standard_roles: + if p.name not in has_role: + has_role[p.name] = {"modified": p.modified, "title": p.title} + if parent == "Report": + has_role[p.name].update({"ref_doctype": p.ref_doctype}) + + no_of_roles = SubQuery( + xhiveframework.qb.from_(hasRole).select(Count("*")).where(hasRole.parent == parentTable.name) + ) + + # pages with no role are allowed + if parent == "Page": + pages_with_no_roles = ( + xhiveframework.qb.from_(parentTable) + .select(parentTable.name, parentTable.modified, *columns) + .where(no_of_roles == 0) + ).run(as_dict=True) + + for p in pages_with_no_roles: + if p.name not in has_role: + has_role[p.name] = {"modified": p.modified, "title": p.title} + + elif parent == "Report": + if not has_permission("Report", raise_exception=False): + return {} + + reports = xhiveframework.get_list( + "Report", + fields=["name", "report_type"], + filters={"name": ("in", has_role.keys())}, + ignore_ifnull=True, + ) + for report in reports: + has_role[report.name]["report_type"] = report.report_type + + non_permitted_reports = set(has_role.keys()) - {r.name for r in reports} + for r in non_permitted_reports: + has_role.pop(r, None) + + # Expire every six hours + xhiveframework.cache.set_value("has_role:" + parent, has_role, xhiveframework.session.user, 21600) + return has_role + + +def load_translations(bootinfo): + from xhiveframework.translate import get_messages_for_boot + + bootinfo["lang"] = xhiveframework.lang + bootinfo["__messages"] = get_messages_for_boot() + + +def get_user_info(): + # get info for current user + user_info = xhiveframework._dict() + add_user_info(xhiveframework.session.user, user_info) + + if xhiveframework.session.user == "Administrator" and user_info.Administrator.email: + user_info[user_info.Administrator.email] = user_info.Administrator + + return user_info + + +def get_user(bootinfo): + """get user info""" + bootinfo.user = xhiveframework.get_user().load_user() + + +def add_home_page(bootinfo, docs): + """load home page""" + if xhiveframework.session.user == "Guest": + return + home_page = xhiveframework.db.get_default("desktop:home_page") + + if home_page == "setup-wizard": + bootinfo.setup_wizard_requires = xhiveframework.get_hooks("setup_wizard_requires") + + try: + page = xhiveframework.desk.desk_page.get(home_page) + docs.append(page) + bootinfo["home_page"] = page.name + except (xhiveframework.DoesNotExistError, xhiveframework.PermissionError): + xhiveframework.clear_last_message() + bootinfo["home_page"] = "Workspaces" + + +def add_timezone_info(bootinfo): + system = bootinfo.sysdefaults.get("time_zone") + import xhiveframework.utils.momentjs + + bootinfo.timezone_info = {"zones": {}, "rules": {}, "links": {}} + xhiveframework.utils.momentjs.update(system, bootinfo.timezone_info) + + +def load_print(bootinfo, doclist): + print_settings = xhiveframework.db.get_singles_dict("Print Settings") + print_settings.doctype = ":Print Settings" + doclist.append(print_settings) + load_print_css(bootinfo, print_settings) + + +def load_print_css(bootinfo, print_settings): + import xhiveframework.www.printview + + bootinfo.print_css = xhiveframework.www.printview.get_print_style( + print_settings.print_style or "Redesign", for_legacy=True + ) + + +def get_unseen_notes(): + note = DocType("Note") + nsb = DocType("Note Seen By").as_("nsb") + + return ( + xhiveframework.qb.from_(note) + .select(note.name, note.title, note.content, note.notify_on_every_login) + .where( + (note.notify_on_login == 1) + & (note.expire_notification_on > xhiveframework.utils.now()) + & ( + ParameterizedValueWrapper(xhiveframework.session.user).notin( + SubQuery(xhiveframework.qb.from_(nsb).select(nsb.user).where(nsb.parent == note.name)) + ) + ) + ) + ).run(as_dict=1) + + +def get_success_action(): + return xhiveframework.get_all("Success Action", fields=["*"]) + + +def get_link_preview_doctypes(): + from xhiveframework.utils import cint + + link_preview_doctypes = [d.name for d in xhiveframework.get_all("DocType", {"show_preview_popup": 1})] + customizations = xhiveframework.get_all( + "Property Setter", fields=["doc_type", "value"], filters={"property": "show_preview_popup"} + ) + + for custom in customizations: + if not cint(custom.value) and custom.doc_type in link_preview_doctypes: + link_preview_doctypes.remove(custom.doc_type) + else: + link_preview_doctypes.append(custom.doc_type) + + return link_preview_doctypes + + +def get_additional_filters_from_hooks(): + filter_config = xhiveframework._dict() + filter_hooks = xhiveframework.get_hooks("filters_config") + for hook in filter_hooks: + filter_config.update(xhiveframework.get_attr(hook)()) + + return filter_config + + +def add_layouts(bootinfo): + # add routes for readable doctypes + bootinfo.doctype_layouts = xhiveframework.get_all("DocType Layout", ["name", "route", "document_type"]) + + +def get_desk_settings(): + role_list = xhiveframework.get_all("Role", fields=["*"], filters=dict(name=["in", xhiveframework.get_roles()])) + desk_settings = {} + + from xhiveframework.core.doctype.role.role import desk_properties + + for role in role_list: + for key in desk_properties: + desk_settings[key] = desk_settings.get(key) or role.get(key) + + return desk_settings + + +def get_notification_settings(): + return xhiveframework.get_cached_doc("Notification Settings", xhiveframework.session.user) + + +def get_link_title_doctypes(): + dts = xhiveframework.get_all("DocType", {"show_title_field_in_link": 1}) + custom_dts = xhiveframework.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_system_timezone(), + "user": bootinfo.get("user_info", {}).get(xhiveframework.session.user, {}).get("time_zone", None) + or get_system_timezone(), + } + + +def load_country_doc(bootinfo): + country = xhiveframework.db.get_default("country") + if not country: + return + try: + bootinfo.docs.append(xhiveframework.get_cached_doc("Country", country)) + except Exception: + pass + + +def load_currency_docs(bootinfo): + currency = xhiveframework.qb.DocType("Currency") + + currency_docs = ( + xhiveframework.qb.from_(currency) + .select( + currency.name, + currency.fraction, + currency.fraction_units, + currency.number_format, + currency.smallest_currency_fraction_value, + currency.symbol, + currency.symbol_on_right, + ) + .where(currency.enabled == 1) + .run(as_dict=1, update={"doctype": ":Currency"}) + ) + + bootinfo.docs += currency_docs + + +def get_marketplace_apps(): + import requests + + apps = [] + cache_key = "xhiveframework_marketplace_apps" + + if xhiveframework.conf.developer_mode: + return apps + + def get_apps_from_fc(): + remote_site = xhiveframework.conf.xhiveframeworkcloud_url or "xhiveframeworkcloud.com" + request_url = f"https://{remote_site}/api/method/press.api.marketplace.get_marketplace_apps" + request = requests.get(request_url, timeout=2.0) + return request.json()["message"] + + try: + apps = xhiveframework.cache().get_value(cache_key, get_apps_from_fc, shared=True) + installed_apps = set(xhiveframework.get_installed_apps()) + apps = [app for app in apps if app["name"] not in installed_apps] + except Exception: + # Don't retry for a day + xhiveframework.cache().set_value(cache_key, apps, shared=True, expires_in_sec=24 * 60 * 60) + + return apps + + +def add_subscription_conf(): + try: + return xhiveframework.conf.subscription + except Exception: + return "" diff --git a/xhiveframework/build.py b/xhiveframework/build.py new file mode 100644 index 0000000..6b6a77c --- /dev/null +++ b/xhiveframework/build.py @@ -0,0 +1,419 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import os +import re +import shutil +import subprocess +from subprocess import getoutput +from tempfile import mkdtemp +from urllib.parse import urlparse + +import click +from semantic_version import Version + +import xhiveframework + +timestamps = {} +app_paths = None +sites_path = os.path.abspath(os.getcwd()) +WHITESPACE_PATTERN = re.compile(r"\s+") +HTML_COMMENT_PATTERN = re.compile(r"()") + + +class AssetsNotDownloadedError(Exception): + pass + + +class AssetsDontExistError(Exception): + pass + + +def download_file(url, prefix): + from requests import get + + filename = urlparse(url).path.split("/")[-1] + local_filename = os.path.join(prefix, filename) + with get(url, stream=True, allow_redirects=True) as r: + r.raise_for_status() + with open(local_filename, "wb") as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + return local_filename + + +def build_missing_files(): + """Check which files dont exist yet from the assets.json and run build for those files""" + + missing_assets = [] + current_asset_files = [] + + for type in ["css", "js"]: + folder = os.path.join(sites_path, "assets", "xhiveframework", "dist", type) + current_asset_files.extend(os.listdir(folder)) + + development = xhiveframework.local.conf.developer_mode or xhiveframework.local.dev_server + build_mode = "development" if development else "production" + + assets_json = xhiveframework.read_file("assets/assets.json") + if assets_json: + assets_json = xhiveframework.parse_json(assets_json) + + for bundle_file, output_file in assets_json.items(): + if not output_file.startswith("/assets/xhiveframework"): + continue + + if os.path.basename(output_file) not in current_asset_files: + missing_assets.append(bundle_file) + + if missing_assets: + click.secho("\nBuilding missing assets...\n", fg="yellow") + files_to_build = ["xhiveframework/" + name for name in missing_assets] + bundle(build_mode, files=files_to_build) + else: + # no assets.json, run full build + bundle(build_mode, apps="xhiveframework") + + +def get_assets_link(xhiveframework_head) -> str: + import requests + + tag = getoutput( + r"cd ../apps/xhiveframework && git show-ref --tags -d | grep %s | sed -e 's,.*" + r" refs/tags/,,' -e 's/\^{}//'" % xhiveframework_head + ) + + if tag: + # if tag exists, download assets from github release + url = f"https://lab.membtech.com/xhiveframework/xhiveframework15/releases/download/{tag}/assets.tar.gz" + else: + url = f"http://assets.xhiveframework.com/{xhiveframework_head}.tar.gz" + + if not requests.head(url): + reference = f"Release {tag}" if tag else f"Commit {xhiveframework_head}" + raise AssetsDontExistError(f"Assets for {reference} don't exist") + + return url + + +def fetch_assets(url, xhiveframework_head): + click.secho("Retrieving assets...", fg="yellow") + + prefix = mkdtemp(prefix="xhiveframework-assets-", suffix=xhiveframework_head) + assets_archive = download_file(url, prefix) + + if not assets_archive: + raise AssetsNotDownloadedError(f"Assets could not be retrived from {url}") + + click.echo(click.style("✔", fg="green") + f" Downloaded XhiveFramework assets from {url}") + + return assets_archive + + +def setup_assets(assets_archive): + import tarfile + + directories_created = set() + + click.secho("\nExtracting assets...\n", fg="yellow") + with tarfile.open(assets_archive) as tar: + for file in tar: + if not file.isdir(): + dest = "." + file.name.replace("./xhiveframework-bench/sites", "") + asset_directory = os.path.dirname(dest) + show = dest.replace("./assets/", "") + + if asset_directory not in directories_created: + if not os.path.exists(asset_directory): + os.makedirs(asset_directory, exist_ok=True) + directories_created.add(asset_directory) + + tar.makefile(file, dest) + click.echo(click.style("✔", fg="green") + f" Restored {show}") + + return directories_created + + +def download_xhiveframework_assets(verbose=True): + """Downloads and sets up XhiveFramework assets if they exist based on the current + commit HEAD. + Returns True if correctly setup else returns False. + """ + xhiveframework_head = getoutput("cd ../apps/xhiveframework && git rev-parse HEAD") + + if not xhiveframework_head: + return False + + try: + url = get_assets_link(xhiveframework_head) + assets_archive = fetch_assets(url, xhiveframework_head) + setup_assets(assets_archive) + build_missing_files() + return True + + except AssetsDontExistError as e: + click.secho(str(e), fg="yellow") + + except Exception as e: + # TODO: log traceback in bench.log + click.secho(str(e), fg="red") + + finally: + try: + shutil.rmtree(os.path.dirname(assets_archive)) + except Exception: + pass + + return False + + +def symlink(target, link_name, overwrite=False): + """ + Create a symbolic link named link_name pointing to target. + If link_name exists then FileExistsError is raised, unless overwrite=True. + When trying to overwrite a directory, IsADirectoryError is raised. + + Source: https://stackoverflow.com/a/55742015/10309266 + """ + + if not overwrite: + return os.symlink(target, link_name) + + # Create link to target with temporary filename + while True: + temp_link_name = f"tmp{xhiveframework.generate_hash()}" + + # os.* functions mimic as closely as possible system functions + # The POSIX symlink() returns EEXIST if link_name already exists + # https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html + try: + os.symlink(target, temp_link_name) + break + except FileExistsError: + pass + + # Replace link_name with temp_link_name + try: + # Pre-empt os.replace on a directory with a nicer message + if os.path.isdir(link_name): + raise IsADirectoryError(f"Cannot symlink over existing directory: '{link_name}'") + try: + os.replace(temp_link_name, link_name) + except AttributeError: + os.renames(temp_link_name, link_name) + except Exception: + if os.path.islink(temp_link_name): + os.remove(temp_link_name) + raise + + +def setup(): + global app_paths, assets_path + + pymodules = [] + for app in xhiveframework.get_all_apps(True): + try: + pymodules.append(xhiveframework.get_module(app)) + except ImportError: + pass + app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] + assets_path = os.path.join(xhiveframework.local.sites_path, "assets") + + +def bundle( + mode, + apps=None, + hard_link=False, + verbose=False, + skip_xhiveframework=False, + files=None, + save_metafiles=False, +): + """concat / minify js files""" + setup() + make_asset_dirs(hard_link=hard_link) + + mode = "production" if mode == "production" else "build" + command = f"yarn run {mode}" + + if apps: + command += f" --apps {apps}" + + if skip_xhiveframework: + command += " --skip_xhiveframework" + + if files: + command += " --files {files}".format(files=",".join(files)) + + command += " --run-build-command" + + if save_metafiles: + command += " --save-metafiles" + + check_node_executable() + xhiveframework_app_path = xhiveframework.get_app_source_path("xhiveframework") + xhiveframework.commands.popen(command, cwd=xhiveframework_app_path, env=get_node_env(), raise_err=True) + + +def watch(apps=None): + """watch and rebuild if necessary""" + setup() + + command = "yarn run watch" + if apps: + command += f" --apps {apps}" + + live_reload = xhiveframework.utils.cint(os.environ.get("LIVE_RELOAD", xhiveframework.conf.live_reload)) + + if live_reload: + command += " --live-reload" + + check_node_executable() + xhiveframework_app_path = xhiveframework.get_app_source_path("xhiveframework") + xhiveframework.commands.popen(command, cwd=xhiveframework_app_path, env=get_node_env()) + + +def check_node_executable(): + node_version = Version(subprocess.getoutput("node -v")[1:]) + warn = "⚠️ " + if node_version.major < 18: + click.echo(f"{warn} Please update your node version to 18") + if not shutil.which("yarn"): + click.echo(f"{warn} Please install yarn using below command and try again.\nnpm install -g yarn") + click.echo() + + +def get_node_env(): + return {"NODE_OPTIONS": f"--max_old_space_size={get_safe_max_old_space_size()}"} + + +def get_safe_max_old_space_size(): + import psutil + + safe_max_old_space_size = 0 + try: + total_memory = psutil.virtual_memory().total / (1024 * 1024) + # reference for the safe limit assumption + # https://nodejs.org/api/cli.html#cli_max_old_space_size_size_in_megabytes + # set minimum value 1GB + safe_max_old_space_size = max(1024, int(total_memory * 0.75)) + except Exception: + pass + + return safe_max_old_space_size + + +def generate_assets_map(): + symlinks = {} + + for app_name in xhiveframework.get_all_apps(): + app_doc_path = None + + pymodule = xhiveframework.get_module(app_name) + app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) + app_public_path = os.path.join(app_base_path, "public") + app_node_modules_path = os.path.join(app_base_path, "..", "node_modules") + app_docs_path = os.path.join(app_base_path, "docs") + app_www_docs_path = os.path.join(app_base_path, "www", "docs") + + app_assets = os.path.abspath(app_public_path) + app_node_modules = os.path.abspath(app_node_modules_path) + + # {app}/public > assets/{app} + if os.path.isdir(app_assets): + symlinks[app_assets] = os.path.join(assets_path, app_name) + + # {app}/node_modules > assets/{app}/node_modules + if os.path.isdir(app_node_modules): + symlinks[app_node_modules] = os.path.join(assets_path, app_name, "node_modules") + + # {app}/docs > assets/{app}_docs + if os.path.isdir(app_docs_path): + app_doc_path = os.path.join(app_base_path, "docs") + elif os.path.isdir(app_www_docs_path): + app_doc_path = os.path.join(app_base_path, "www", "docs") + if app_doc_path: + app_docs = os.path.abspath(app_doc_path) + symlinks[app_docs] = os.path.join(assets_path, app_name + "_docs") + + return symlinks + + +def setup_assets_dirs(): + for dir_path in (os.path.join(assets_path, x) for x in ("js", "css")): + os.makedirs(dir_path, exist_ok=True) + + +def clear_broken_symlinks(): + for path in os.listdir(assets_path): + path = os.path.join(assets_path, path) + if os.path.islink(path) and not os.path.exists(path): + os.remove(path) + + +def unstrip(message: str) -> str: + """Pads input string on the right side until the last available column in the terminal""" + _len = len(message) + try: + max_str = os.get_terminal_size().columns + except Exception: + max_str = 80 + + if _len < max_str: + _rem = max_str - _len + else: + _rem = max_str % _len + + return f"{message}{' ' * _rem}" + + +def make_asset_dirs(hard_link=False): + setup_assets_dirs() + clear_broken_symlinks() + symlinks = generate_assets_map() + + for source, target in symlinks.items(): + start_message = unstrip(f"{'Copying assets from' if hard_link else 'Linking'} {source} to {target}") + fail_message = unstrip(f"Cannot {'copy' if hard_link else 'link'} {source} to {target}") + + # Used '\r' instead of '\x1b[1K\r' to print entire lines in smaller terminal sizes + try: + print(start_message, end="\r") + link_assets_dir(source, target, hard_link=hard_link) + except Exception: + print(fail_message, end="\r") + + click.echo(unstrip(click.style("✔", fg="green") + " Application Assets Linked") + "\n") + + +def link_assets_dir(source, target, hard_link=False): + if not os.path.exists(source): + return + + if os.path.exists(target): + if os.path.islink(target): + os.unlink(target) + else: + shutil.rmtree(target) + + if hard_link: + shutil.copytree(source, target, dirs_exist_ok=True) + else: + symlink(source, target, overwrite=True) + + +def scrub_html_template(content): + """Returns HTML content with removed whitespace and comments""" + # remove whitespace to a single space + content = WHITESPACE_PATTERN.sub(" ", content) + + # strip comments + content = HTML_COMMENT_PATTERN.sub("", content) + + return content.replace("'", "'") + + +def html_to_js_template(path, content): + """returns HTML template content as Javascript code, adding it to `xhiveframework.templates`""" + return """xhiveframework.templates["{key}"] = '{content}';\n""".format( + key=path.rsplit("/", 1)[-1][:-5], content=scrub_html_template(content) + ) diff --git a/xhiveframework/cache_manager.py b/xhiveframework/cache_manager.py new file mode 100644 index 0000000..6261d89 --- /dev/null +++ b/xhiveframework/cache_manager.py @@ -0,0 +1,241 @@ +# Copyright (c) 2018, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + +common_default_keys = ["__default", "__global"] + +doctypes_for_mapping = { + "Energy Point Rule", + "Assignment Rule", + "Milestone Tracker", + "Document Naming Rule", +} + + +def get_doctype_map_key(doctype): + return xhiveframework.scrub(doctype) + "_map" + + +doctype_map_keys = tuple(map(get_doctype_map_key, doctypes_for_mapping)) + +bench_cache_keys = ("assets_json",) + +global_cache_keys = ( + "app_hooks", + "installed_apps", + "all_apps", + "app_modules", + "installed_app_modules", + "module_app", + "module_installed_app", + "system_settings", + "scheduler_events", + "time_zone", + "webhooks", + "active_domains", + "active_modules", + "assignment_rule", + "server_script_map", + "wkhtmltopdf_version", + "domain_restricted_doctypes", + "domain_restricted_pages", + "information_schema:counts", + "db_tables", + "server_script_autocompletion_items", + *doctype_map_keys, +) + +user_cache_keys = ( + "bootinfo", + "user_recent", + "roles", + "user_doc", + "lang", + "defaults", + "user_permissions", + "home_page", + "linked_with", + "desktop_icons", + "portal_menu_items", + "user_perm_can_read", + "has_role:Page", + "has_role:Report", + "desk_sidebar_items", + "contacts", +) + +doctype_cache_keys = ( + "doctype_meta", + "doctype_form_meta", + "table_columns", + "last_modified", + "linked_doctypes", + "notifications", + "workflow", + "data_import_column_header_map", +) + + +def clear_user_cache(user=None): + from xhiveframework.desk.notifications import clear_notifications + + # this will automatically reload the global cache + # so it is important to clear this first + clear_notifications(user) + + if user: + for name in user_cache_keys: + xhiveframework.cache.hdel(name, user) + xhiveframework.cache.delete_keys("user:" + user) + clear_defaults_cache(user) + else: + for name in user_cache_keys: + xhiveframework.cache.delete_key(name) + clear_defaults_cache() + clear_global_cache() + + +def clear_domain_cache(user=None): + domain_cache_keys = ("domain_restricted_doctypes", "domain_restricted_pages") + xhiveframework.cache.delete_value(domain_cache_keys) + + +def clear_global_cache(): + from xhiveframework.website.utils import clear_website_cache + + clear_doctype_cache() + clear_website_cache() + xhiveframework.cache.delete_value(global_cache_keys) + xhiveframework.cache.delete_value(bench_cache_keys) + xhiveframework.setup_module_map() + + +def clear_defaults_cache(user=None): + if user: + for p in [user, *common_default_keys]: + xhiveframework.cache.hdel("defaults", p) + elif xhiveframework.flags.in_install != "xhiveframework": + xhiveframework.cache.delete_key("defaults") + + +def clear_doctype_cache(doctype=None): + clear_controller_cache(doctype) + + _clear_doctype_cache_from_redis(doctype) + if hasattr(xhiveframework.db, "after_commit"): + xhiveframework.db.after_commit.add(lambda: _clear_doctype_cache_from_redis(doctype)) + xhiveframework.db.after_rollback.add(lambda: _clear_doctype_cache_from_redis(doctype)) + + +def _clear_doctype_cache_from_redis(doctype: str | None = None): + from xhiveframework.desk.notifications import delete_notification_count_for + + for key in ("is_table", "doctype_modules"): + xhiveframework.cache.delete_value(key) + + def clear_single(dt): + xhiveframework.clear_document_cache(dt) + for name in doctype_cache_keys: + xhiveframework.cache.hdel(name, dt) + + if doctype: + clear_single(doctype) + + # clear all parent doctypes + for dt in xhiveframework.get_all( + "DocField", "parent", dict(fieldtype=["in", xhiveframework.model.table_fields], options=doctype) + ): + clear_single(dt.parent) + + # clear all parent doctypes + if not xhiveframework.flags.in_install: + for dt in xhiveframework.get_all( + "Custom Field", "dt", dict(fieldtype=["in", xhiveframework.model.table_fields], options=doctype) + ): + clear_single(dt.dt) + + # clear all notifications + delete_notification_count_for(doctype) + + else: + # clear all + for name in doctype_cache_keys: + xhiveframework.cache.delete_value(name) + xhiveframework.cache.delete_keys("document_cache::") + + +def clear_controller_cache(doctype=None): + if not doctype: + xhiveframework.controllers.pop(xhiveframework.local.site, None) + return + + if site_controllers := xhiveframework.controllers.get(xhiveframework.local.site): + site_controllers.pop(doctype, None) + + +def get_doctype_map(doctype, name, filters=None, order_by=None): + return xhiveframework.cache.hget( + get_doctype_map_key(doctype), + name, + lambda: xhiveframework.get_all(doctype, filters=filters, order_by=order_by, ignore_ddl=True), + ) + + +def clear_doctype_map(doctype, name): + xhiveframework.cache.hdel(xhiveframework.scrub(doctype) + "_map", name) + + +def build_table_count_cache(): + if ( + xhiveframework.flags.in_patch + or xhiveframework.flags.in_install + or xhiveframework.flags.in_migrate + or xhiveframework.flags.in_import + or xhiveframework.flags.in_setup_wizard + ): + return + + table_name = xhiveframework.qb.Field("table_name").as_("name") + table_rows = xhiveframework.qb.Field("table_rows").as_("count") + information_schema = xhiveframework.qb.Schema("information_schema") + + data = (xhiveframework.qb.from_(information_schema.tables).select(table_name, table_rows)).run(as_dict=True) + counts = {d.get("name").replace("tab", "", 1): d.get("count", None) for d in data} + xhiveframework.cache.set_value("information_schema:counts", counts) + + return counts + + +def build_domain_restriced_doctype_cache(*args, **kwargs): + if ( + xhiveframework.flags.in_patch + or xhiveframework.flags.in_install + or xhiveframework.flags.in_migrate + or xhiveframework.flags.in_import + or xhiveframework.flags.in_setup_wizard + ): + return + active_domains = xhiveframework.get_active_domains() + doctypes = xhiveframework.get_all("DocType", filters={"restrict_to_domain": ("IN", active_domains)}) + doctypes = [doc.name for doc in doctypes] + xhiveframework.cache.set_value("domain_restricted_doctypes", doctypes) + + return doctypes + + +def build_domain_restriced_page_cache(*args, **kwargs): + if ( + xhiveframework.flags.in_patch + or xhiveframework.flags.in_install + or xhiveframework.flags.in_migrate + or xhiveframework.flags.in_import + or xhiveframework.flags.in_setup_wizard + ): + return + active_domains = xhiveframework.get_active_domains() + pages = xhiveframework.get_all("Page", filters={"restrict_to_domain": ("IN", active_domains)}) + pages = [page.name for page in pages] + xhiveframework.cache.set_value("domain_restricted_pages", pages) + + return pages diff --git a/xhiveframework/change_log/__init__.py b/xhiveframework/change_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/change_log/current/readme.md b/xhiveframework/change_log/current/readme.md new file mode 100644 index 0000000..e93bb75 --- /dev/null +++ b/xhiveframework/change_log/current/readme.md @@ -0,0 +1,3 @@ +Leave change log files in this folder for user release notes. + +(this file is just a place holder, don't delete it) diff --git a/xhiveframework/change_log/v10/v10_0_0.md b/xhiveframework/change_log/v10/v10_0_0.md new file mode 100644 index 0000000..1610b9f --- /dev/null +++ b/xhiveframework/change_log/v10/v10_0_0.md @@ -0,0 +1,10 @@ +- Enhanced Data Import Tool + - Data Import Tool is now a normal form, you can maintain records for each Data Import. + - Better error handling + - Background processing for large files + +- XhiveFramework now has a github connector + +- Any doctype can have a calendar view + +- XhiveFramework has a new simple, responsive, modern SVG [charts library](https://lab.membtech.com/xhiveframework/charts), developed by us diff --git a/xhiveframework/change_log/v11/v11_1_0.md b/xhiveframework/change_log/v11/v11_1_0.md new file mode 100644 index 0000000..210766e --- /dev/null +++ b/xhiveframework/change_log/v11/v11_1_0.md @@ -0,0 +1,19 @@ +- Dynamic [XhiveFramework Charts](https://lab.membtech.com/xhiveframework/charts) with Report Builder (built by @pratu16x7) +- New XhiveFramework Chat for easier internal communication (built by @achillesrasquinha) +- [XhiveFramework DataTable](https://lab.membtech.com/xhiveframework/datatable) for better reports (built by @netchampfaris) +- Google Calendar can now be integrated with XhiveFramework +- Files can now be uploaded using drag-and-drop +- Enhanced List View and Tree View +- Bulk Actions from List View +- Quill editor has been introduced in place of Summernote +- HTML Editor has been introduced +- New User Permissions +- Subscriptions in XhiveERP now moved to Auto Repeat in XhiveFramework +- Support for Razorpay and PayPal subscriptions +- Better Social login, Workflow +- Messages for when user goes online/offline +- Logout from all sessions on password change +- Desktop icons can now be selected from a dialog box +- Changes have been made to ensure XhiveFramework is compatible with Python 3 +- Better documentation is now available with support for more languages +- A lot of other fixes have been done to ensure a better overall user experience diff --git a/xhiveframework/change_log/v12/v12_0_0.md b/xhiveframework/change_log/v12/v12_0_0.md new file mode 100644 index 0000000..69f86bf --- /dev/null +++ b/xhiveframework/change_log/v12/v12_0_0.md @@ -0,0 +1,29 @@ +# Version 12 Release Notes + +### UI/UX Enhancements +1. [New Desktop](https://xhiveerp.com/docs/user/manual/en/using-xhiveerp/desktop) +1. [Keyboard Navigation](https://xhiveerp.com/docs/user/manual/en/using-xhiveerp/articles/keyboard-shortcuts) +1. [Link Preview](https://xhiveerp.com/version-12/release-notes/features#link-preview) +1. [New Upload Dialog](https://xhiveerp.com/version-12/release-notes/features#new-upload-dialog) +1. [Frequently visited links appear in Awesomebar results](https://xhiveerp.com/version-12/release-notes/features#frequently-visited-links-appear-in-awesomebar-results) +1. [Full Width Container]((https://xhiveerp.com/version-12/release-notes/features#full-width-container)) +1. [List View Enhancements](https://xhiveerp.com/version-12/release-notes/features#list-view-enhancements) + +### New Automation Module +1. [Assignment Rule](https://xhiveerp.com/docs/user/manual/en/setting-up/automation/assignment-rule) +1. [Milestones](https://xhiveerp.com/docs/user/manual/en/setting-up/automation/milestone-tracker) +1. [Auto Repeat](https://xhiveerp.com/docs/user/manual/en/setting-up/automation/auto-repeat) + +### Other Changes & Enhancements +1. [Document Follow](https://xhiveerp.com/docs/user/manual/en/setting-up/email/document-follow) +1. [Energy Points](https://xhiveerp.com/docs/user/manual/en/setting-up/energy-point-system) +1. [Dashboards](https://xhiveerp.com/docs/user/manual/en/customize-xhiveerp/dashboard) +1. [Disable customization for single doctypes](https://xhiveerp.com/version-12/release-notes/features#disable-customization-for-single-doctypes) +1. [Email Linking](https://xhiveerp.com/docs/user/manual/en/setting-up/email/linking-emails-to-document) +1. [Google Contacts](https://xhiveerp.com/docs/user/manual/en/xhiveerp_integration/google_contacts) +1. [PDF Encryption](https://xhiveerp.com/version-12/release-notes/features#pdf-encryption) +1. [Raw Printing](https://xhiveerp.com/docs/user/manual/en/setting-up/print/raw-printing) +1. [Web Form Refactor](https://xhiveerp.com/version-12/release-notes/features#web-form-refactor) +1. [Website Refactor](https://xhiveerp.com/docs/user/manual/en/website) +1. [Added Track Views field to Customize Form](https://xhiveerp.com/version-12/release-notes/features#added-track-views-field-to-customize-form) +1. [Add custom columns to any report](https://xhiveerp.com/version-12/release-notes/features#add-custom-columns-to-any-report) diff --git a/xhiveframework/change_log/v13/v13_0_0.md b/xhiveframework/change_log/v13/v13_0_0.md new file mode 100644 index 0000000..e84fec3 --- /dev/null +++ b/xhiveframework/change_log/v13/v13_0_0.md @@ -0,0 +1,54 @@ +# Version 13.0.0 Release Notes + +## Highlights + +- Re-branded UI 💎 ✨🎊 ([#12277](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12277)) +- New Page Builder in Web Page ([#10035](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10035)) +- Customizable desk ([#9617](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9617)) +- Custom Dashboard for DocTypes ([#9872](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9872)) +- Widgets to make dashboards ([#9693](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9693)) +- Events Streaming ([#8567](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/8567)) +- Contextual translation and Translation Tool ([#9636](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9636)) + +### Other Features & Enhancements + +- Added permission to grant only `Select` access ([#12063](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12063)) +- Add columns and filters for reports via configuration ([#11287](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11287)) +- Configurable Navbar logo and dropdowns ([#11213](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11213)) +- Rule based naming of documents ([#11439](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11439)) +- New routing style, not using hashes, also /desk -> /app ([#11917](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11917)) +- Web Page tracking ([#9959](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9959)) +- Introduced "Yesterday" and "Tomorrow" options for Timespan filter ([12179](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12179)) +- Child table pagination ([#8786](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/8786)) +- Introduced Duration Control ([#10248](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10248)) +- Form Tour feature ([#10287](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10287)) +
+More + +- Introduced Map View ([#11202](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11202)) +- Custom JS & CSS support in Web Form ([#9121](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9121)) ([#9610](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9610)) +- Ability to attach photo from webcam ([#12160](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12160)) +- Added a System Console to help in debugging ([#11306](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11306)) +- Introduced System Settings to automatically delete old Prepared Reports ([#9751](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9751)) +- "Mandatory Depends On" and "Read Only Depends On" option for document fields ([#8820](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/8820)) +- Added 2FA for LDAP users ([#10001](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10001)) +- Introduced Help Article Feedback system ([#10260](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10260)) +- Introduced Razorpay client ([#11418](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11418)) +- Rate Limiting ([#10310](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10310)) +- Introduced Log Settings ([#11699](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11699)) +- Enhancements in notifications ([#11398](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11398)) ([#11409](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11409)) +- Added a field-level permission check for report data ([12163](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12163)) +- Ability to cancel all linked document with a single click ([#8905](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/8905)) +- Made checkboxes navigable via tab key ([#11030](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11030)) +- Renamed "Custom Script" to "Client Script" ([#12324](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12324)) + +
+ +### Performance + +- Faster application load ([#12364](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12364)) ([#10229](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10229)) ([#10147](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10147)) ([#9930](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9930)) +- Theme files will now be compressed to make the website load faster ([#11048](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/11048)) +- Confirmation emails will be sent instantly ([#10790](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/10790)) +- Faster scheduled job processing ([#9928](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/9928)) +- Faster data imports ([#12565](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12565)) +- Faster CLI commands ([#12447](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12447)) diff --git a/xhiveframework/change_log/v13/v13_1_0.md b/xhiveframework/change_log/v13/v13_1_0.md new file mode 100644 index 0000000..091c9e1 --- /dev/null +++ b/xhiveframework/change_log/v13/v13_1_0.md @@ -0,0 +1,22 @@ +# Version 13.1.0 Release Notes + +### Features & Enhancements + +- Automated mail notifications will be shown in timeline ([#12693](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12693)) +- Introduced Client Script for List views ([#12590](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12590)) +- Introduced language switcher for guest users on website navbar ([#12813](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12813)) +- Option to give submit permission while sharing a document ([#12799](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12799)) +- Added option to set `autoname` in Customize Form ([#12413](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12413)) +- Virtual DocType ([#12121](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12121)) + +### Fixes + +- Workspace fixes ([#12650](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12650)) ([#12655](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12655)) ([#12869](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12869)) +- Fixed an issue where select options were not getting updated in Grid ([#12839](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12839)) +- Webform Fixes ([#12630](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12630)) ([#12756](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12756)) ([#12819](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12819)) +- Fixed timespan filter for next and last timespans ([#12509](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12509)) +- System Notification fixes ([#12719](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12719)) +- Design Fixes ([#12669](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12669)) ([#12591](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12591)) ([#12557](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12557)) ([#12751](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12751)) ([#12864](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12864)) +- Fixed Multi-column paste in grid ([#12861](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12861)) +- Fixed grid validation ([#12744](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12744)) +- Fixed currency value formatting in dashboard chart ([#12613](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12613)) diff --git a/xhiveframework/change_log/v13/v13_2_0.md b/xhiveframework/change_log/v13/v13_2_0.md new file mode 100644 index 0000000..714e2ad --- /dev/null +++ b/xhiveframework/change_log/v13/v13_2_0.md @@ -0,0 +1,32 @@ +# Version 13.2.0 Release Notes + +### Features & Enhancements + +- Add option to mention a group of users ([#12844](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12844)) +- Copy DocType / documents across sites ([#12872](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12872)) +- Scheduler log in notifications ([#1135](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/1135)) +- Add Enable/Disable Webhook via Check Field ([#12842](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12842)) +- Allow query/custom reports to save custom data in the json field ([#12534](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12534)) + +### Fixes + +- Load server translations in boot (backport #12848) ([#12852](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12852)) +- Allow to override dashboard chart properties type/color ([#12846](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12846)) +- Multi-column paste in grid ([#12861](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12861)) +- Add log_error and XhiveFrameworkClient to restricted python ([#12857](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12857)) +- Redirect Web Form user directly to success URL, if no amount is due ([#12661](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12661)) +- Attachment pill lock icon redirects to File ([#12864](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12864)) +- Redirect Web Form user directly to success URL, if no amount is due (backport #12661) ([#12856](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12856)) +- Remove events to redraw charts ([#12973](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12973)) +- Don't allow user to remove/change data source file in data import ([#12827](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12827)) +- Load server translations in boot ([#12848](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12848)) +- Newly created Workspace not being accessible unless a shortcut u… ([#12866](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12866)) +- Currency labels in grids ([#12974](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12974)) +- Handle error while session start ([#12933](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12933)) +- Add field type check in custom field validation ([#12858](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12858)) +- Make language select optional and fix breakpoint issues ([#12860](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12860)) +- Form Dashboard reference link ([#12945](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12945)) +- Invalid HTML generated by the base template ([#12953](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12953)) +- Default values were not triggering change event ([#12975](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12975)) +- Make strings translatable ([#12877](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12877)) +- Added build-message-files command ([#12950](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/12950)) diff --git a/xhiveframework/change_log/v13/v13_3_0.md b/xhiveframework/change_log/v13/v13_3_0.md new file mode 100644 index 0000000..bc334cc --- /dev/null +++ b/xhiveframework/change_log/v13/v13_3_0.md @@ -0,0 +1,49 @@ +# Version 13.3.0 Release Notes + +### Features & Enhancements + +- Deletion Steps in Data Deletion Tool ([#13124](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13124)) +- Format Option for list-apps in bench CLI ([#13125](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13125)) +- Add password fieldtype option for Web Form ([#13093](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13093)) +- Add simple __repr__ for DocTypes ([#13151](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13151)) +- Switch theme with left/right keys ([#13077](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13077)) +- sourceURL for injected javascript ([#13022](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13022)) + +### Fixes + +- Decode uri before importing file via weblink ([#13026](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13026)) +- Respond to /api requests as JSON by default ([#13053](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13053)) +- Disabled checkbox should be disabled ([#13021](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13021)) +- Moving Site folder across different FileSystems failed ([#13038](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13038)) +- Freeze screen till the background request is complete ([#13078](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13078)) +- Added conditional rendering for content field in split section w… ([#13075](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13075)) +- Show delete button on portal if user has permission to delete document ([#13149](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13149)) +- Dont disable dialog scroll on focusing a Link/Autocomplete field ([#13119](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13119)) +- Typo in RecorderDetail.vue ([#13086](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13086)) +- Error for bench drop-site. Added missing import. ([#13064](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13064)) +- Report column context ([#13090](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13090)) +- Different service name for push and pull request events ([#13094](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13094)) +- Moving Site folder across different FileSystems failed ([#13033](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13033)) +- Consistent checkboxes on all browsers ([#13042](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13042)) +- Changed shorcut widgets color picker to dropdown ([#13073](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13073)) +- Error while exporting reports with duration field ([#13118](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13118)) +- Add margin to download backup card ([#13079](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13079)) +- Move mention list generation logic to server-side ([#13074](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13074)) +- Make strings translatable ([#13046](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13046)) +- Don't evaluate dynamic properties to check if conflicts exist ([#13186](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13186)) +- Add __ function in vue global for translation in recorder ([#13089](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13089)) +- Make strings translatable ([#13076](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13076)) +- Show config in bench CLI ([#13128](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13128)) +- Add breadcrumbs for list view ([#13091](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13091)) +- Do not skip data in save while using shortcut ([#13182](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13182)) +- Use docfields from options if no docfields are returned from meta ([#13188](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13188)) +- Disable reloading files in `__pycache__` directory ([#13109](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13109)) +- RTL stylesheet route to load RTL style on demand. ([#13007](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13007)) +- Do not show messsage when exception is handled ([#13111](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13111)) +- Replace parseFloat by Number ([#13082](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13082)) +- Add margin to download backup card ([#13050](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13050)) +- Translate report column labels ([#13083](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13083)) +- Grid row color picker field not working ([#13040](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13040)) +- Improve oauthlib implementation ([#13045](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13045)) +- Replace filter_by like with full text filter ([#13126](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13126)) +- Focus jumps to first field ([#13067](https://lab.membtech.com/xhiveframework/xhiveframework15/pull/13067)) diff --git a/xhiveframework/change_log/v5/v5_0_18.md b/xhiveframework/change_log/v5/v5_0_18.md new file mode 100644 index 0000000..95e07f3 --- /dev/null +++ b/xhiveframework/change_log/v5/v5_0_18.md @@ -0,0 +1,6 @@ +#### Updates to Web Forms + +- Web Forms list now is a standard portal list and includes paging and other extensions +- Section, Column Breaks in Web Forms +- Consistent User Interface +- Cleanup of Portal Pages diff --git a/xhiveframework/change_log/v5/v5_0_20.md b/xhiveframework/change_log/v5/v5_0_20.md new file mode 100644 index 0000000..18e9f43 --- /dev/null +++ b/xhiveframework/change_log/v5/v5_0_20.md @@ -0,0 +1 @@ +- Ability to send yourself a copy of the outgoing email added back. diff --git a/xhiveframework/change_log/v5/v5_0_32.md b/xhiveframework/change_log/v5/v5_0_32.md new file mode 100644 index 0000000..17a7cb3 --- /dev/null +++ b/xhiveframework/change_log/v5/v5_0_32.md @@ -0,0 +1,5 @@ +- Reports are now searchable from awesome bar +- Show currect label for title in list views +- Datepicker now sets default value as Today +- Map child table as per meta, if not mentioned in table_map via mapper +- Re-enable save button on error \ No newline at end of file diff --git a/xhiveframework/change_log/v5/v5_1_0.md b/xhiveframework/change_log/v5/v5_1_0.md new file mode 100644 index 0000000..3036acc --- /dev/null +++ b/xhiveframework/change_log/v5/v5_1_0.md @@ -0,0 +1,3 @@ +- Change print font from Setup > Print Settings or set it for each Print Format. Font options are "Default", "Arial", "Helvetica", "Verdana", "Monospace". +- Print and full-page print preview in user's language +- Fixed inconsistent visibility of a logged-in user's image in website diff --git a/xhiveframework/change_log/v5/v5_1_1.md b/xhiveframework/change_log/v5/v5_1_1.md new file mode 100644 index 0000000..befa5be --- /dev/null +++ b/xhiveframework/change_log/v5/v5_1_1.md @@ -0,0 +1 @@ +- Ability to **Share with Everyone** (except Guest) using **Share With** diff --git a/xhiveframework/change_log/v5/v5_3_0.md b/xhiveframework/change_log/v5/v5_3_0.md new file mode 100644 index 0000000..1f70645 --- /dev/null +++ b/xhiveframework/change_log/v5/v5_3_0.md @@ -0,0 +1,40 @@ +- Added Language Support for Following languages + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
bo*ལྷ་སའི་སྐད་
fisuomalainen
kmភាសាខ្មែរ
mkмакедонски
myMelayu
nonorsk
svSvenska
sqshqiptar
+ +* Unable to find translations for Tibetian via Google + +- To contribute to translations, please login to [https://translate.xhiveerp.com](https://translate.xhiveerp.com) diff --git a/xhiveframework/change_log/v5/v5_4_0.md b/xhiveframework/change_log/v5/v5_4_0.md new file mode 100644 index 0000000..5bf04bb --- /dev/null +++ b/xhiveframework/change_log/v5/v5_4_0.md @@ -0,0 +1 @@ +- Moved Backup Manager and Social Login Keys to the new **Integrations** module diff --git a/xhiveframework/change_log/v6/v6_0_0.md b/xhiveframework/change_log/v6/v6_0_0.md new file mode 100644 index 0000000..6f9c8fc --- /dev/null +++ b/xhiveframework/change_log/v6/v6_0_0.md @@ -0,0 +1,3 @@ +- **Realtime updates** for new comments and list view +- Get warned if someone else modified the document that you are working on +- You can now quickly assign a document to yourself by clicking on "Assign to me" diff --git a/xhiveframework/change_log/v6/v6_0_8.md b/xhiveframework/change_log/v6/v6_0_8.md new file mode 100644 index 0000000..6a2fba5 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_0_8.md @@ -0,0 +1 @@ +- Set HTML in Website Settings. This is usually used for website verification and SEO. diff --git a/xhiveframework/change_log/v6/v6_12_0.md b/xhiveframework/change_log/v6/v6_12_0.md new file mode 100644 index 0000000..abcdd16 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_12_0.md @@ -0,0 +1 @@ +- Extract emails using IMAP. Contributed by Gangadhar Kadam ([New Indictrans](http://indictranstech.com/)) diff --git a/xhiveframework/change_log/v6/v6_13_0.md b/xhiveframework/change_log/v6/v6_13_0.md new file mode 100644 index 0000000..661e04f --- /dev/null +++ b/xhiveframework/change_log/v6/v6_13_0.md @@ -0,0 +1,4 @@ +- Attachments can now be marked as **Private** + - Private files cannot be accessed unless you are logged in + - To access a private file, you need to have read permission on the file or read permission on the document to which the file is attached + - All attachments in a new incoming email are private diff --git a/xhiveframework/change_log/v6/v6_14_1.md b/xhiveframework/change_log/v6/v6_14_1.md new file mode 100644 index 0000000..4aed877 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_14_1.md @@ -0,0 +1 @@ +- Added language support for Malayalam: **ml - മലയാളം** diff --git a/xhiveframework/change_log/v6/v6_15_0.md b/xhiveframework/change_log/v6/v6_15_0.md new file mode 100644 index 0000000..9d1e6f9 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_15_0.md @@ -0,0 +1,4 @@ +- **For Developers:** Automatic logging of request errors and its context in **Error Snapshot** + - Thank you [Maxwell Morais](https://discuss.xhiveerp.com/users/max_morais_dmm/activity) for this useful feature + - You can access it from *Developer > Logs > Error Snapshot* +- Added language support for [Gujarati](https://translate.xhiveerp.com/view?lang=gu): **gu - ગુજરાતી** diff --git a/xhiveframework/change_log/v6/v6_16_1.md b/xhiveframework/change_log/v6/v6_16_1.md new file mode 100644 index 0000000..b638724 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_16_1.md @@ -0,0 +1 @@ +- Mention users in comments using `@username`. Mentioned users will receive an email with the comment. \ No newline at end of file diff --git a/xhiveframework/change_log/v6/v6_16_4.md b/xhiveframework/change_log/v6/v6_16_4.md new file mode 100644 index 0000000..63ae316 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_16_4.md @@ -0,0 +1 @@ +- Increased uploaded file size limit upto 10MB \ No newline at end of file diff --git a/xhiveframework/change_log/v6/v6_17_0.md b/xhiveframework/change_log/v6/v6_17_0.md new file mode 100644 index 0000000..f660660 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_17_0.md @@ -0,0 +1,4 @@ +- Ability to **Like** a document, comment or communication + - See notifications about likes that you received + - View it on Activity feed + - *Stars* have been converted to Likes diff --git a/xhiveframework/change_log/v6/v6_1_0.md b/xhiveframework/change_log/v6/v6_1_0.md new file mode 100644 index 0000000..cc628ba --- /dev/null +++ b/xhiveframework/change_log/v6/v6_1_0.md @@ -0,0 +1,7 @@ +- Sections can now be set as **Collapsible**. +- Collapsible sections can be shown as collapsed based on certain rules defined in the **Collapsible Depends On** property of the document field (DocField). +- Title is now editable from the form if the `fieldname` of the title field is **title**. +- Document can now be renamed by clicking the page heading. +- Fields can be set as **Bold** so that they can be easily identified in long forms. +- Fixed mobile views +- See Data Import progress in realtime diff --git a/xhiveframework/change_log/v6/v6_20_0.md b/xhiveframework/change_log/v6/v6_20_0.md new file mode 100644 index 0000000..162e43b --- /dev/null +++ b/xhiveframework/change_log/v6/v6_20_0.md @@ -0,0 +1,5 @@ +- Fixed **Export** for large reports +- Added language support for: + - [Estonian](https://translate.xhiveerp.com/view?lang=et): **et - eesti** + - [Telugu](https://translate.xhiveerp.com/view?lang=te): **te - తెలుగు** + - [Urdu](https://translate.xhiveerp.com/view?lang=ur): **ur - اردو** diff --git a/xhiveframework/change_log/v6/v6_21_0.md b/xhiveframework/change_log/v6/v6_21_0.md new file mode 100644 index 0000000..48e0ef8 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_21_0.md @@ -0,0 +1 @@ +- Repeating Letter Head and Footer in PDF \ No newline at end of file diff --git a/xhiveframework/change_log/v6/v6_22_0.md b/xhiveframework/change_log/v6/v6_22_0.md new file mode 100644 index 0000000..a74cbc1 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_22_0.md @@ -0,0 +1,2 @@ +- **Comment**, **Feed** and **Communication** are merged into one table, **Communication** +- Ability to turn-off repeating headers and footers in PDF via **Print Settings** diff --git a/xhiveframework/change_log/v6/v6_23_0.md b/xhiveframework/change_log/v6/v6_23_0.md new file mode 100644 index 0000000..8566425 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_23_0.md @@ -0,0 +1,2 @@ +- Autosuggest email address in **CC** when composing emails - contributed by [Robert Schouten](https://github.com/robertschouten) +- Fix: Attach signature with Email Account's auto-reply diff --git a/xhiveframework/change_log/v6/v6_25_0.md b/xhiveframework/change_log/v6/v6_25_0.md new file mode 100644 index 0000000..99f694b --- /dev/null +++ b/xhiveframework/change_log/v6/v6_25_0.md @@ -0,0 +1,4 @@ +- **Custom Translations** via Setup > Customize > Custom Translations +- Print multiple documents from list view + - Check documents and click on Menu > Print +- PDF printing for Query Reports diff --git a/xhiveframework/change_log/v6/v6_26_0.md b/xhiveframework/change_log/v6/v6_26_0.md new file mode 100644 index 0000000..8da70c3 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_26_0.md @@ -0,0 +1,2 @@ +- **Don't allow user to move standard fields.** You can still move any Custom Field to a desired position. +- Landscape orientation for Report PDF diff --git a/xhiveframework/change_log/v6/v6_26_6.md b/xhiveframework/change_log/v6/v6_26_6.md new file mode 100644 index 0000000..98bdf8e --- /dev/null +++ b/xhiveframework/change_log/v6/v6_26_6.md @@ -0,0 +1 @@ +- Check permissions on printing or making pdf of report \ No newline at end of file diff --git a/xhiveframework/change_log/v6/v6_27_1.md b/xhiveframework/change_log/v6/v6_27_1.md new file mode 100644 index 0000000..ebacf19 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_27_1.md @@ -0,0 +1,6 @@ +- Configurable Desktop + - Add any Documents, Reports, Modules, Pages to the desktop + - Remove all the unwanted icons +- **Module Page New Design** + - New module design now shows all documents in a module together + - [Read the Details](https://xhiveframework.io/blog/xhiveerp-features/configurable-desktop) diff --git a/xhiveframework/change_log/v6/v6_27_11.md b/xhiveframework/change_log/v6/v6_27_11.md new file mode 100644 index 0000000..4d67135 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_27_11.md @@ -0,0 +1,2 @@ +- Get [email sending status](https://discuss.xhiveerp.com/t/communication-delivery-status-bulk-email-status/11941) in document timeline +- Ability to disable a Role diff --git a/xhiveframework/change_log/v6/v6_2_0.md b/xhiveframework/change_log/v6/v6_2_0.md new file mode 100644 index 0000000..0525324 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_2_0.md @@ -0,0 +1,3 @@ +- **Permissions:** + - If User Permissions are missing for a DocType, don't show non-matching records. + - If **Ignore User Permissions If Missing** is checked in System Settings, show records even if User Permissions are not defined. diff --git a/xhiveframework/change_log/v6/v6_3_0.md b/xhiveframework/change_log/v6/v6_3_0.md new file mode 100644 index 0000000..f605c46 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_3_0.md @@ -0,0 +1,2 @@ +- You can now add **CC** in Email +- Show checkboxes in Print diff --git a/xhiveframework/change_log/v6/v6_4_0.md b/xhiveframework/change_log/v6/v6_4_0.md new file mode 100644 index 0000000..ae56802 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_4_0.md @@ -0,0 +1 @@ +- **File Manager:** A Document Management System for your organisation. Add files, organize them in folders and share it with a few users or everyone in the company. diff --git a/xhiveframework/change_log/v6/v6_4_8.md b/xhiveframework/change_log/v6/v6_4_8.md new file mode 100644 index 0000000..afc5e45 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_4_8.md @@ -0,0 +1,20 @@ +- Added Language Support for Following languages + + + + + + + + + + + + + + + + + + +
bnবাংলা
da-DKdansk (Danmark)
es-PEEspañol (Perú)
sislovenščina
diff --git a/xhiveframework/change_log/v6/v6_5_0.md b/xhiveframework/change_log/v6/v6_5_0.md new file mode 100644 index 0000000..62afbd3 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_5_0.md @@ -0,0 +1,2 @@ +- **Linked With** will now show links from Dynamic Links +- **Data** field-type size truncated to 140 characters from 255 (by default). Can be changed by setting the **length** property from **Customize Form View** diff --git a/xhiveframework/change_log/v6/v6_6_0.md b/xhiveframework/change_log/v6/v6_6_0.md new file mode 100644 index 0000000..6655897 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_6_0.md @@ -0,0 +1 @@ +- Added language support for Ukranian: **uk - українська** diff --git a/xhiveframework/change_log/v6/v6_7_0.md b/xhiveframework/change_log/v6/v6_7_0.md new file mode 100644 index 0000000..ffb546d --- /dev/null +++ b/xhiveframework/change_log/v6/v6_7_0.md @@ -0,0 +1,3 @@ +- See who is currently viewing the document +- Sounds for various actions +- Added language support for Slovene: **sl - slovenščina (Slovene)** diff --git a/xhiveframework/change_log/v6/v6_8_0.md b/xhiveframework/change_log/v6/v6_8_0.md new file mode 100644 index 0000000..012b406 --- /dev/null +++ b/xhiveframework/change_log/v6/v6_8_0.md @@ -0,0 +1,2 @@ +- Pre-configured Email Account for Yandex.Mail +- Fixed inline images in received emails diff --git a/xhiveframework/change_log/v7/v7_0_0.md b/xhiveframework/change_log/v7/v7_0_0.md new file mode 100644 index 0000000..3521047 --- /dev/null +++ b/xhiveframework/change_log/v7/v7_0_0.md @@ -0,0 +1,36 @@ +#### UI +- Editable Grids +- Image Field in DocType, form and list +- Dashboard, Heatmap, Graphs on Form View +- Document Flow in forms +- List views: remembers user settings + +#### Celery to RQ + +#### Quick Entry + +#### Razorpay Integration + +#### Passwords + +#### Portals +- Statics (`www` folder): directly served from templates +- New Routing +- Web Forms + +#### Limits +- Expiry, space etc +- Usage Info Page + +#### Minor +- **Rename:** Bulk Email is now Email Queue +- `xhiveframework.require` is async +- `flot.js` replaced by `c3.js` +- Most popular links on the top +- Standard Replies configurable +- Timeline permisions (not based on user permissions) +- "Track Seen" feature in doctypes +- Moved: "Edit Profile" page is now in xhiveframework (moved from XhiveERP) +- Cleanup UI for chat +- New default user icons (based on initials) +- Multiple assign (add a document to multiple users) diff --git a/xhiveframework/change_log/v7/v7_0_18.md b/xhiveframework/change_log/v7/v7_0_18.md new file mode 100644 index 0000000..5e59c02 --- /dev/null +++ b/xhiveframework/change_log/v7/v7_0_18.md @@ -0,0 +1,2 @@ +- New Feature: Ability to add multiple sessions to users. Edit **User** record and edit "Simultaneous Sessions" +- New Feature: Select columns to export and import in **Data Import Tool** \ No newline at end of file diff --git a/xhiveframework/change_log/v7/v7_1_0.md b/xhiveframework/change_log/v7/v7_1_0.md new file mode 100644 index 0000000..7d2187a --- /dev/null +++ b/xhiveframework/change_log/v7/v7_1_0.md @@ -0,0 +1,24 @@ +#### Gantt View +- New Gantt view for documents where date range is available + +#### In-App Help +- Search for help from within the app. Click on "Help" + +#### Web Form +- Add grids (child tables) +- Add page breaks (for long forms) +- Add payment gateway +- Add attachments + +#### Auto Email Report +- Email reports automatically on daily / weekly / monthly basis + +#### Other Fixes +- Send a popup to all users on login for a new Note by checking on "Notify users with a popup when they log in" +- Portal Users (Customers, Supplier, Students) can now have roles +- Sidebar in portal view will be rendered as per roles and can be configured from Portal Settings +- Restrict the number of backups to be saved in System Settings +- Scheduler log is now error log and as MyISAM +- A better way to export customzations and Email Alert directly from Customize Form +- Option to send email from Data Import Tool where applicable +- Integration Broker \ No newline at end of file diff --git a/xhiveframework/change_log/v7/v7_2_0.md b/xhiveframework/change_log/v7/v7_2_0.md new file mode 100644 index 0000000..9968248 --- /dev/null +++ b/xhiveframework/change_log/v7/v7_2_0.md @@ -0,0 +1,18 @@ +- Filters Dashboard + - Dashboard with pre-defined filters in List/Report View +- Tag Category + - Show/Group tags based on category +- Updated Font Awesome version to 4.x.x +- Checkboxes in grid + - Delete selected rows + - Map selected rows from one document to another. +- Show Totals button in report +- OpenID Connect for XhiveFramework +- Threading based on message id in Email Queue +- New control object daterangepicker for filtering +- Orientation selection in PDF +- Expand/Collapse All buttons in tree view reports +- Add attachment from email and copy attachments to Communication Record +- Bulk Upload from zip file +- Tree view decoration +- Custom menu for report view diff --git a/xhiveframework/change_log/v8/v8_0_0.md b/xhiveframework/change_log/v8/v8_0_0.md new file mode 100644 index 0000000..31d375d --- /dev/null +++ b/xhiveframework/change_log/v8/v8_0_0.md @@ -0,0 +1,41 @@ +#### Global Search +- Now, from awesome bar, you can search all the documents related to a specific keyword. +- For example, you can get all Quotations, Sales Orders and Sales Invoices related to a Customer, by searching with customer's email address / mobile no. + +#### Kanban View +- Kanban Board allows you to identify a field based on which documents will be categorised and viewed together. +- From the board itself, you can update the status of teh document, comment on it or assign it someone. + +#### Document Versioning +- Now the system maintains all the changes of a document with the information of user and timestamp. + +#### Delete and Restore +- In version 8, all the deleted documents are stored in the "Deleted Documents" table, which can be restored later +- To permanently delete a record, you need to delete it from "Deleted Documents". + +#### Email Inbox +- We have introduced an Inbox View for all the communications. +- It has all the basic functionalities of an Email Client like Inbox, Sent Mails, Trash and Spam folders, Contact list, Read/Unread, forwarding of an email etc. +- Issue, Lead and Opportunity can be created directly from the Communication. + +#### Custom Permissions +- Now, all the customised permissions are stored separately in a Custom DocPerm table. + +#### Permissions for Reports and Page +- Report and Page have it's own dedicated permission table now, it's no more dependent on the reference Doctype for permissions. +- There is a page "Role Permission for Page and Report", from where you will be able to customize the permissions for reports and pages. + +#### Newsletter Enhancements +- In the new version, newsletter can be sent to multiple email groups. +- You will also be able to send the attachments with the newsletter. +- Unsubscribe link is optional now + +#### New Calendar and Date Picker +- Get a more crisp view of calendar and events. +- Date picker also has been updated in the version. + +#### Summernote Text Editor +- We have also integrated Summernote text editor in the XhiveERP. It has many tools to create better texts. + +#### Export report in Excel format +- Reports can now also be exported in Excel format. diff --git a/xhiveframework/change_log/v8/v8_7_0.md b/xhiveframework/change_log/v8/v8_7_0.md new file mode 100644 index 0000000..47b3b84 --- /dev/null +++ b/xhiveframework/change_log/v8/v8_7_0.md @@ -0,0 +1,2 @@ +### User Permissions +- User Permission is now a DocType, a new UX for the existing Role and User Permission managers to make it easy to enter permissions. For more details please check User Permissions diff --git a/xhiveframework/change_log/v8/v8_8_0.md b/xhiveframework/change_log/v8/v8_8_0.md new file mode 100644 index 0000000..e93c0ad --- /dev/null +++ b/xhiveframework/change_log/v8/v8_8_0.md @@ -0,0 +1,2 @@ +### Two Factor Authentication +- Now you can authenticate user with two factor authentication. You can enable the Two Factor Authentication from System Settings. \ No newline at end of file diff --git a/xhiveframework/client.py b/xhiveframework/client.py new file mode 100644 index 0000000..979384c --- /dev/null +++ b/xhiveframework/client.py @@ -0,0 +1,506 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import json +import os +from typing import TYPE_CHECKING + +import xhiveframework +import xhiveframework.model +import xhiveframework.utils +from xhiveframework import _ +from xhiveframework.desk.reportview import validate_args +from xhiveframework.model.db_query import check_parent_permission +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.utils import get_safe_filters +from xhiveframework.utils.deprecations import deprecated + +if TYPE_CHECKING: + from xhiveframework.model.document import Document + +""" +Handle RESTful requests that are mapped to the `/api/resource` route. + +Requests via XhiveFrameworkClient are also handled here. +""" + + +@xhiveframework.whitelist() +def get_list( + doctype, + fields=None, + filters=None, + group_by=None, + order_by=None, + limit_start=None, + limit_page_length=20, + parent=None, + debug: bool = False, + as_dict: bool = True, + or_filters=None, +): + """Returns a list of records by filters, fields, ordering and limit + + :param doctype: DocType of the data to be queried + :param fields: fields to be returned. Default is `name` + :param filters: filter list by this dict + :param order_by: Order by this fieldname + :param limit_start: Start at this index + :param limit_page_length: Number of records to be returned (default 20)""" + if xhiveframework.is_table(doctype): + check_parent_permission(parent, doctype) + + args = xhiveframework._dict( + doctype=doctype, + parent_doctype=parent, + fields=fields, + filters=filters, + or_filters=or_filters, + group_by=group_by, + order_by=order_by, + limit_start=limit_start, + limit_page_length=limit_page_length, + debug=debug, + as_list=not as_dict, + ) + + validate_args(args) + return xhiveframework.get_list(**args) + + +@xhiveframework.whitelist() +def get_count(doctype, filters=None, debug=False, cache=False): + return xhiveframework.db.count(doctype, get_safe_filters(filters), debug, cache) + + +@xhiveframework.whitelist() +def get(doctype, name=None, filters=None, parent=None): + """Returns a document by name or filters + + :param doctype: DocType of the document to be returned + :param name: return document of this `name` + :param filters: If name is not set, filter by these values and return the first match""" + if xhiveframework.is_table(doctype): + check_parent_permission(parent, doctype) + + if name: + doc = xhiveframework.get_doc(doctype, name) + elif filters or filters == {}: + doc = xhiveframework.get_doc(doctype, xhiveframework.parse_json(filters)) + else: + doc = xhiveframework.get_doc(doctype) # single + + doc.check_permission() + doc.apply_fieldlevel_read_permissions() + + return doc.as_dict() + + +@xhiveframework.whitelist() +def get_value(doctype, fieldname, filters=None, as_dict=True, debug=False, parent=None): + """Returns a value form a document + + :param doctype: DocType to be queried + :param fieldname: Field to be returned (default `name`) + :param filters: dict or string for identifying the record""" + if xhiveframework.is_table(doctype): + check_parent_permission(parent, doctype) + + if not xhiveframework.has_permission(doctype, parent_doctype=parent): + xhiveframework.throw(_("No permission for {0}").format(_(doctype)), xhiveframework.PermissionError) + + filters = get_safe_filters(filters) + if isinstance(filters, str): + filters = {"name": filters} + + try: + fields = xhiveframework.parse_json(fieldname) + except (TypeError, ValueError): + # name passed, not json + fields = [fieldname] + + # check whether the used filters were really parseable and usable + # and did not just result in an empty string or dict + if not filters: + filters = None + + if xhiveframework.get_meta(doctype).issingle: + value = xhiveframework.db.get_values_from_single(fields, filters, doctype, as_dict=as_dict, debug=debug) + else: + value = get_list( + doctype, + filters=filters, + fields=fields, + debug=debug, + limit_page_length=1, + parent=parent, + as_dict=as_dict, + ) + + if as_dict: + return value[0] if value else {} + + if not value: + return + + return value[0] if len(fields) > 1 else value[0][0] + + +@xhiveframework.whitelist() +def get_single_value(doctype, field): + if not xhiveframework.has_permission(doctype): + xhiveframework.throw(_("No permission for {0}").format(_(doctype)), xhiveframework.PermissionError) + + return xhiveframework.db.get_single_value(doctype, field) + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def set_value(doctype, name, fieldname, value=None): + """Set a value using get_doc, group of values + + :param doctype: DocType of the document + :param name: name of the document + :param fieldname: fieldname string or JSON / dict with key value pair + :param value: value if fieldname is JSON / dict""" + + if fieldname in (xhiveframework.model.default_fields + xhiveframework.model.child_table_fields): + xhiveframework.throw(_("Cannot edit standard fields")) + + if not value: + values = fieldname + if isinstance(fieldname, str): + try: + values = json.loads(fieldname) + except ValueError: + values = {fieldname: ""} + else: + values = {fieldname: value} + + # check for child table doctype + if not xhiveframework.get_meta(doctype).istable: + doc = xhiveframework.get_doc(doctype, name) + doc.update(values) + else: + doc = xhiveframework.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True) + doc = xhiveframework.get_doc(doc.parenttype, doc.parent) + child = doc.getone({"doctype": doctype, "name": name}) + child.update(values) + + doc.save() + + return doc.as_dict() + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def insert(doc=None): + """Insert a document + + :param doc: JSON or dict object to be inserted""" + if isinstance(doc, str): + doc = json.loads(doc) + + return insert_doc(doc).as_dict() + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def insert_many(docs=None): + """Insert multiple documents + + :param docs: JSON or list of dict objects to be inserted in one request""" + if isinstance(docs, str): + docs = json.loads(docs) + + if len(docs) > 200: + xhiveframework.throw(_("Only 200 inserts allowed in one request")) + + return [insert_doc(doc).name for doc in docs] + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def save(doc): + """Update (save) an existing document + + :param doc: JSON or dict object with the properties of the document to be updated""" + if isinstance(doc, str): + doc = json.loads(doc) + + doc = xhiveframework.get_doc(doc) + doc.save() + + return doc.as_dict() + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def rename_doc(doctype, old_name, new_name, merge=False): + """Rename document + + :param doctype: DocType of the document to be renamed + :param old_name: Current `name` of the document to be renamed + :param new_name: New `name` to be set""" + new_name = xhiveframework.rename_doc(doctype, old_name, new_name, merge=merge) + return new_name + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def submit(doc): + """Submit a document + + :param doc: JSON or dict object to be submitted remotely""" + if isinstance(doc, str): + doc = json.loads(doc) + + doc = xhiveframework.get_doc(doc) + doc.submit() + + return doc.as_dict() + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def cancel(doctype, name): + """Cancel a document + + :param doctype: DocType of the document to be cancelled + :param name: name of the document to be cancelled""" + wrapper = xhiveframework.get_doc(doctype, name) + wrapper.cancel() + + return wrapper.as_dict() + + +@xhiveframework.whitelist(methods=["DELETE", "POST"]) +def delete(doctype, name): + """Delete a remote document + + :param doctype: DocType of the document to be deleted + :param name: name of the document to be deleted""" + delete_doc(doctype, name) + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def bulk_update(docs): + """Bulk update documents + + :param docs: JSON list of documents to be updated remotely. Each document must have `docname` property""" + docs = json.loads(docs) + failed_docs = [] + for doc in docs: + doc.pop("flags", None) + try: + existing_doc = xhiveframework.get_doc(doc["doctype"], doc["docname"]) + existing_doc.update(doc) + existing_doc.save() + except Exception: + failed_docs.append({"doc": doc, "exc": xhiveframework.utils.get_traceback()}) + + return {"failed_docs": failed_docs} + + +@xhiveframework.whitelist() +def has_permission(doctype, docname, perm_type="read"): + """Returns a JSON with data whether the document has the requested permission + + :param doctype: DocType of the document to be checked + :param docname: `name` of the document to be checked + :param perm_type: one of `read`, `write`, `create`, `submit`, `cancel`, `report`. Default is `read`""" + # perm_type can be one of read, write, create, submit, cancel, report + return {"has_permission": xhiveframework.has_permission(doctype, perm_type.lower(), docname)} + + +@xhiveframework.whitelist() +def get_doc_permissions(doctype, docname): + """Returns an evaluated document permissions dict like `{"read":1, "write":1}` + + :param doctype: DocType of the document to be evaluated + :param docname: `name` of the document to be evaluated + """ + doc = xhiveframework.get_doc(doctype, docname) + return {"permissions": xhiveframework.permissions.get_doc_permissions(doc)} + + +@xhiveframework.whitelist() +def get_password(doctype, name, fieldname): + """Return a password type property. Only applicable for System Managers + + :param doctype: DocType of the document that holds the password + :param name: `name` of the document that holds the password + :param fieldname: `fieldname` of the password property + """ + xhiveframework.only_for("System Manager") + return xhiveframework.get_doc(doctype, name).get_password(fieldname) + + +@xhiveframework.whitelist() +@deprecated +def get_js(items): + """Load JS code files. Will also append translations + and extend `xhiveframework._messages` + + :param items: JSON list of paths of the js files to be loaded.""" + items = json.loads(items) + out = [] + for src in items: + src = src.strip("/").split("/") + + if ".." in src or src[0] != "assets": + xhiveframework.throw(_("Invalid file path: {0}").format("/".join(src))) + + contentpath = os.path.join(xhiveframework.local.sites_path, *src) + with open(contentpath) as srcfile: + code = xhiveframework.utils.cstr(srcfile.read()) + + out.append(code) + + return out + + +@xhiveframework.whitelist(allow_guest=True) +def get_time_zone(): + """Returns default time zone""" + return {"time_zone": xhiveframework.defaults.get_defaults().get("time_zone")} + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def attach_file( + filename=None, + filedata=None, + doctype=None, + docname=None, + folder=None, + decode_base64=False, + is_private=None, + docfield=None, +): + """Attach a file to Document + + :param filename: filename e.g. test-file.txt + :param filedata: base64 encode filedata which must be urlencoded + :param doctype: Reference DocType to attach file to + :param docname: Reference DocName to attach file to + :param folder: Folder to add File into + :param decode_base64: decode filedata from base64 encode, default is False + :param is_private: Attach file as private file (1 or 0) + :param docfield: file to attach to (optional)""" + + doc = xhiveframework.get_doc(doctype, docname) + doc.check_permission() + + file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": filename, + "attached_to_doctype": doctype, + "attached_to_name": docname, + "attached_to_field": docfield, + "folder": folder, + "is_private": is_private, + "content": filedata, + "decode": decode_base64, + } + ).save() + + if docfield and doctype: + doc.set(docfield, file.file_url) + doc.save() + + return file + + +@xhiveframework.whitelist() +def is_document_amended(doctype, docname): + if xhiveframework.permissions.has_permission(doctype): + try: + return xhiveframework.db.exists(doctype, {"amended_from": docname}) + except xhiveframework.db.InternalError: + pass + + return False + + +@xhiveframework.whitelist() +def validate_link(doctype: str, docname: str, fields=None): + if not isinstance(doctype, str): + xhiveframework.throw(_("DocType must be a string")) + + if not isinstance(docname, str): + xhiveframework.throw(_("Document Name must be a string")) + + if doctype != "DocType" and not ( + xhiveframework.has_permission(doctype, "select") or xhiveframework.has_permission(doctype, "read") + ): + xhiveframework.throw( + _("You do not have Read or Select Permissions for {}").format(xhiveframework.bold(doctype)), + xhiveframework.PermissionError, + ) + + values = xhiveframework._dict() + + if is_virtual_doctype(doctype): + try: + xhiveframework.get_doc(doctype, docname) + values.name = docname + except xhiveframework.DoesNotExistError: + xhiveframework.clear_last_message() + xhiveframework.msgprint( + _("Document {0} {1} does not exist").format(xhiveframework.bold(doctype), xhiveframework.bold(docname)), + ) + return values + + values.name = xhiveframework.db.get_value(doctype, docname, cache=True) + + fields = xhiveframework.parse_json(fields) + if not values.name or not fields: + return values + + try: + values.update(get_value(doctype, fields, docname)) + except xhiveframework.PermissionError: + xhiveframework.clear_last_message() + xhiveframework.msgprint( + _("You need {0} permission to fetch values from {1} {2}").format( + xhiveframework.bold(_("Read")), xhiveframework.bold(doctype), xhiveframework.bold(docname) + ), + title=_("Cannot Fetch Values"), + indicator="orange", + ) + + return values + + +def insert_doc(doc) -> "Document": + """Inserts document and returns parent document object with appended child document + if `doc` is child document else returns the inserted document object + + :param doc: doc to insert (dict)""" + + doc = xhiveframework._dict(doc) + if xhiveframework.is_table(doc.doctype): + if not (doc.parenttype and doc.parent and doc.parentfield): + xhiveframework.throw(_("Parenttype, Parent and Parentfield are required to insert a child record")) + + # inserting a child record + parent = xhiveframework.get_doc(doc.parenttype, doc.parent) + parent.append(doc.parentfield, doc) + parent.save() + return parent + + return xhiveframework.get_doc(doc).insert() + + +def delete_doc(doctype, name): + """Deletes document + if doctype is a child table, then deletes the child record using the parent doc + so that the parent doc's `on_update` is called + """ + + if xhiveframework.is_table(doctype): + values = xhiveframework.db.get_value(doctype, name, ["parenttype", "parent", "parentfield"]) + if not values: + raise xhiveframework.DoesNotExistError + parenttype, parent, parentfield = values + parent = xhiveframework.get_doc(parenttype, parent) + for row in parent.get(parentfield): + if row.name == name: + parent.remove(row) + parent.save() + break + else: + xhiveframework.delete_doc(doctype, name, ignore_missing=False) diff --git a/xhiveframework/commands/__init__.py b/xhiveframework/commands/__init__.py new file mode 100644 index 0000000..6e44b92 --- /dev/null +++ b/xhiveframework/commands/__init__.py @@ -0,0 +1,124 @@ +# Copyright (c) 2015, Web Notes Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import cProfile +import pstats +import subprocess # nosec +import sys +from functools import wraps +from io import StringIO +from os import environ + +import click + +import xhiveframework +import xhiveframework.utils + +click.disable_unicode_literals_warning = True + + +def pass_context(f): + @wraps(f) + def _func(ctx, *args, **kwargs): + profile = ctx.obj["profile"] + if profile: + pr = cProfile.Profile() + pr.enable() + + try: + ret = f(xhiveframework._dict(ctx.obj), *args, **kwargs) + except xhiveframework.exceptions.SiteNotSpecifiedError as e: + click.secho(str(e), fg="yellow") + sys.exit(1) + except xhiveframework.exceptions.IncorrectSitePath: + site = ctx.obj.get("sites", "")[0] + click.secho(f"Site {site} does not exist!", fg="yellow") + sys.exit(1) + + if profile: + pr.disable() + s = StringIO() + ps = pstats.Stats(pr, stream=s).sort_stats("cumtime", "tottime", "ncalls") + ps.print_stats() + + # print the top-100 + for line in s.getvalue().splitlines()[:100]: + print(line) + + return ret + + return click.pass_context(_func) + + +def get_site(context, raise_err=True): + try: + return context.sites[0] + except (IndexError, TypeError): + if raise_err: + raise xhiveframework.SiteNotSpecifiedError + return None + + +def popen(command, *args, **kwargs): + output = kwargs.get("output", True) + cwd = kwargs.get("cwd") + shell = kwargs.get("shell", True) + raise_err = kwargs.get("raise_err") + env = kwargs.get("env") + if env: + env = dict(environ, **env) + + def set_low_prio(): + import psutil + + if psutil.LINUX: + psutil.Process().nice(19) + psutil.Process().ionice(psutil.IOPRIO_CLASS_IDLE) + elif psutil.WINDOWS: + psutil.Process().nice(psutil.IDLE_PRIORITY_CLASS) + psutil.Process().ionice(psutil.IOPRIO_VERYLOW) + else: + psutil.Process().nice(19) + # ionice not supported + + proc = subprocess.Popen( + command, + stdout=None if output else subprocess.PIPE, + stderr=None if output else subprocess.PIPE, + shell=shell, + cwd=cwd, + preexec_fn=set_low_prio, + env=env, + ) + + return_ = proc.wait() + + if return_ and raise_err: + raise subprocess.CalledProcessError(return_, command) + + return return_ + + +def call_command(cmd, context): + return click.Context(cmd, obj=context).forward(cmd) + + +def get_commands(): + # prevent circular imports + from .redis_utils import commands as redis_commands + from .scheduler import commands as scheduler_commands + from .site import commands as site_commands + from .translate import commands as translate_commands + from .utils import commands as utils_commands + + clickable_link = "https://xhiveframework.com/docs" + all_commands = scheduler_commands + site_commands + translate_commands + utils_commands + redis_commands + + for command in all_commands: + if not command.help: + command.help = f"Refer to {clickable_link}" + + return all_commands + + +commands = get_commands() diff --git a/xhiveframework/commands/redis_utils.py b/xhiveframework/commands/redis_utils.py new file mode 100644 index 0000000..b518719 --- /dev/null +++ b/xhiveframework/commands/redis_utils.py @@ -0,0 +1,69 @@ +import os + +import click + +import xhiveframework + + +@click.command("create-rq-users") +@click.option( + "--set-admin-password", + is_flag=True, + default=False, + help="Set new Redis admin(default user) password", +) +@click.option("--use-rq-auth", is_flag=True, default=False, help="Enable Redis authentication for sites") +def create_rq_users(set_admin_password=False, use_rq_auth=False): + """Create Redis Queue users and add to acl and app configs. + + acl config file will be used by redis server while starting the server + and app config is used by app while connecting to redis server. + """ + from xhiveframework.installer import update_site_config + from xhiveframework.utils.redis_queue import RedisQueue + + acl_file_path = os.path.abspath("../config/redis_queue.acl") + + with xhiveframework.init_site(): + acl_list, user_credentials = RedisQueue.gen_acl_list(set_admin_password=set_admin_password) + + with open(acl_file_path, "w") as f: + f.writelines([acl + "\n" for acl in acl_list]) + + sites_path = os.getcwd() + common_site_config_path = os.path.join(sites_path, "common_site_config.json") + update_site_config( + "rq_username", + user_credentials["bench"][0], + validate=False, + site_config_path=common_site_config_path, + ) + update_site_config( + "rq_password", + user_credentials["bench"][1], + validate=False, + site_config_path=common_site_config_path, + ) + update_site_config("use_rq_auth", use_rq_auth, validate=False, site_config_path=common_site_config_path) + + click.secho( + "* ACL and site configs are updated with new user credentials. " + "Please restart Redis Queue server to enable namespaces.", + fg="green", + ) + + if set_admin_password: + env_key = "RQ_ADMIN_PASWORD" + click.secho( + "* Redis admin password is successfully set up. " + "Include below line in .bashrc file for system to use", + fg="green", + ) + click.secho(f"`export {env_key}={user_credentials['default'][1]}`") + click.secho( + "NOTE: Please save the admin password as you " "can not access redis server without the password", + fg="yellow", + ) + + +commands = [create_rq_users] diff --git a/xhiveframework/commands/scheduler.py b/xhiveframework/commands/scheduler.py new file mode 100755 index 0000000..4cb44f0 --- /dev/null +++ b/xhiveframework/commands/scheduler.py @@ -0,0 +1,271 @@ +import sys + +import click + +import xhiveframework +from xhiveframework.commands import get_site, pass_context +from xhiveframework.exceptions import SiteNotSpecifiedError + + +@click.command("trigger-scheduler-event", help="Trigger a scheduler event") +@click.argument("event") +@pass_context +def trigger_scheduler_event(context, event): + import xhiveframework.utils.scheduler + + exit_code = 0 + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + try: + xhiveframework.get_doc("Scheduled Job Type", {"method": event}).execute() + except xhiveframework.DoesNotExistError: + click.secho(f"Event {event} does not exist!", fg="red") + exit_code = 1 + finally: + xhiveframework.destroy() + + if not context.sites: + raise SiteNotSpecifiedError + + sys.exit(exit_code) + + +@click.command("enable-scheduler") +@pass_context +def enable_scheduler(context): + "Enable scheduler" + import xhiveframework.utils.scheduler + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.utils.scheduler.enable_scheduler() + xhiveframework.db.commit() + print("Enabled for", site) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("disable-scheduler") +@pass_context +def disable_scheduler(context): + "Disable scheduler" + import xhiveframework.utils.scheduler + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.utils.scheduler.disable_scheduler() + xhiveframework.db.commit() + print("Disabled for", site) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("scheduler") +@click.option("--site", help="site name") +@click.argument("state", type=click.Choice(["pause", "resume", "disable", "enable", "status"])) +@click.option("--format", "-f", default="text", type=click.Choice(["json", "text"]), help="Output format") +@click.option("--verbose", "-v", is_flag=True, help="Verbose output") +@pass_context +def scheduler(context, state: str, format: str, verbose: bool = False, site: str | None = None): + """Control scheduler state.""" + import xhiveframework + from xhiveframework.utils.scheduler import is_scheduler_inactive, toggle_scheduler + + site = site or get_site(context) + + output = { + "text": "Scheduler is {status} for site {site}", + "json": '{{"status": "{status}", "site": "{site}"}}', + } + + with xhiveframework.init_site(site=site): + match state: + case "status": + xhiveframework.connect() + status = "disabled" if is_scheduler_inactive(verbose=verbose) else "enabled" + return print(output[format].format(status=status, site=site)) + case "pause" | "resume": + from xhiveframework.installer import update_site_config + + update_site_config("pause_scheduler", state == "pause") + case "enable" | "disable": + xhiveframework.connect() + toggle_scheduler(state == "enable") + xhiveframework.db.commit() + + print(output[format].format(status=f"{state}d", site=site)) + + +@click.command("set-maintenance-mode") +@click.option("--site", help="site name") +@click.argument("state", type=click.Choice(["on", "off"])) +@pass_context +def set_maintenance_mode(context, state, site=None): + """Put the site in maintenance mode for upgrades.""" + from xhiveframework.installer import update_site_config + + if not site: + site = get_site(context) + + try: + xhiveframework.init(site=site) + update_site_config("maintenance_mode", 1 if (state == "on") else 0) + + finally: + xhiveframework.destroy() + + +@click.command("doctor") # Passing context always gets a site and if there is no use site it breaks +@click.option("--site", help="site name") +@pass_context +def doctor(context, site=None): + "Get diagnostic info about background workers" + from xhiveframework.utils.doctor import doctor as _doctor + + if not site: + site = get_site(context, raise_err=False) + return _doctor(site=site) + + +@click.command("show-pending-jobs") +@click.option("--site", help="site name") +@pass_context +def show_pending_jobs(context, site=None): + "Get diagnostic info about background jobs" + from xhiveframework.utils.doctor import pending_jobs as _pending_jobs + + if not site: + site = get_site(context) + + with xhiveframework.init_site(site): + pending_jobs = _pending_jobs(site=site) + + return pending_jobs + + +@click.command("purge-jobs") +@click.option("--site", help="site name") +@click.option("--queue", default=None, help='one of "low", "default", "high') +@click.option( + "--event", + default=None, + help='one of "all", "weekly", "monthly", "hourly", "daily", "weekly_long", "daily_long"', +) +def purge_jobs(site=None, queue=None, event=None): + "Purge any pending periodic tasks, if event option is not given, it will purge everything for the site" + from xhiveframework.utils.doctor import purge_pending_jobs + + xhiveframework.init(site or "") + count = purge_pending_jobs(event=event, site=site, queue=queue) + print(f"Purged {count} jobs") + + +@click.command("schedule") +def start_scheduler(): + """Start scheduler process which is responsible for enqueueing the scheduled job types.""" + from xhiveframework.utils.scheduler import start_scheduler + + start_scheduler() + + +@click.command("worker") +@click.option( + "--queue", + type=str, + help="Queue to consume from. Multiple queues can be specified using comma-separated string. If not specified all queues are consumed.", +) +@click.option("--quiet", is_flag=True, default=False, help="Hide Log Outputs") +@click.option("-u", "--rq-username", default=None, help="Redis ACL user") +@click.option("-p", "--rq-password", default=None, help="Redis ACL user password") +@click.option("--burst", is_flag=True, default=False, help="Run Worker in Burst mode.") +@click.option( + "--strategy", + required=False, + type=click.Choice(["round_robin", "random"]), + help="Dequeuing strategy to use", +) +def start_worker(queue, quiet=False, rq_username=None, rq_password=None, burst=False, strategy=None): + """Start a backgrond worker""" + from xhiveframework.utils.background_jobs import start_worker + + start_worker( + queue, + quiet=quiet, + rq_username=rq_username, + rq_password=rq_password, + burst=burst, + strategy=strategy, + ) + + +@click.command("worker-pool") +@click.option( + "--queue", + type=str, + help="Queue to consume from. Multiple queues can be specified using comma-separated string. If not specified all queues are consumed.", +) +@click.option("--num-workers", type=int, default=2, help="Number of workers to spawn in pool.") +@click.option("--quiet", is_flag=True, default=False, help="Hide Log Outputs") +@click.option("--burst", is_flag=True, default=False, help="Run Worker in Burst mode.") +def start_worker_pool(queue, quiet=False, num_workers=2, burst=False): + """Start a backgrond worker""" + from xhiveframework.utils.background_jobs import start_worker_pool + + start_worker_pool( + queue=queue, + quiet=quiet, + burst=burst, + num_workers=num_workers, + ) + + +@click.command("ready-for-migration") +@click.option("--site", help="site name") +@pass_context +def ready_for_migration(context, site=None): + from xhiveframework.utils.doctor import any_job_pending + + if not site: + site = get_site(context) + + try: + xhiveframework.init(site=site) + pending_jobs = any_job_pending(site=site) + + if pending_jobs: + print(f"NOT READY for migration: site {site} has pending background jobs") + sys.exit(1) + + else: + print(f"READY for migration: site {site} does not have any background jobs") + return 0 + + finally: + xhiveframework.destroy() + + +commands = [ + disable_scheduler, + doctor, + enable_scheduler, + purge_jobs, + ready_for_migration, + scheduler, + set_maintenance_mode, + show_pending_jobs, + start_scheduler, + start_worker, + start_worker_pool, + trigger_scheduler_event, +] diff --git a/xhiveframework/commands/site.py b/xhiveframework/commands/site.py new file mode 100644 index 0000000..3fb59ea --- /dev/null +++ b/xhiveframework/commands/site.py @@ -0,0 +1,1543 @@ +# imports - standard imports +import os +import shutil +import sys + +# imports - third party imports +import click + +# imports - module imports +import xhiveframework +from xhiveframework.commands import get_site, pass_context +from xhiveframework.exceptions import SiteNotSpecifiedError + + +@click.command("new-site") +@click.argument("site") +@click.option("--db-name", help="Database name") +@click.option("--db-password", help="Database password") +@click.option( + "--db-type", + default="mariadb", + type=click.Choice(["mariadb", "postgres"]), + help='Optional "postgres" or "mariadb". Default is "mariadb"', +) +@click.option("--db-host", help="Database Host") +@click.option("--db-port", type=int, help="Database Port") +@click.option( + "--db-root-username", + "--mariadb-root-username", + help='Root username for MariaDB or PostgreSQL, Default is "root"', +) +@click.option("--db-root-password", "--mariadb-root-password", help="Root password for MariaDB or PostgreSQL") +@click.option( + "--no-mariadb-socket", + is_flag=True, + default=False, + help="Set MariaDB host to % and use TCP/IP Socket instead of using the UNIX Socket", +) +@click.option("--admin-password", help="Administrator password for new site", default=None) +@click.option("--verbose", is_flag=True, default=False, help="Verbose") +@click.option("--force", help="Force restore if site/database already exists", is_flag=True, default=False) +@click.option("--source-sql", "--source_sql", help="Initiate database with a SQL file") +@click.option("--install-app", multiple=True, help="Install app after installation") +@click.option("--set-default", is_flag=True, default=False, help="Set the new site as default site") +def new_site( + site, + db_root_username=None, + db_root_password=None, + admin_password=None, + verbose=False, + source_sql=None, + force=None, + no_mariadb_socket=False, + install_app=None, + db_name=None, + db_password=None, + db_type=None, + db_host=None, + db_port=None, + set_default=False, +): + "Create a new site" + from xhiveframework.installer import _new_site + + xhiveframework.init(site=site, new_site=True) + + _new_site( + db_name, + site, + db_root_username=db_root_username, + db_root_password=db_root_password, + admin_password=admin_password, + verbose=verbose, + install_apps=install_app, + source_sql=source_sql, + force=force, + no_mariadb_socket=no_mariadb_socket, + db_password=db_password, + db_type=db_type, + db_host=db_host, + db_port=db_port, + ) + + if set_default: + use(site) + + +@click.command("restore") +@click.argument("sql-file-path") +@click.option( + "--db-root-username", + "--mariadb-root-username", + help='Root username for MariaDB or PostgreSQL, Default is "root"', +) +@click.option("--db-root-password", "--mariadb-root-password", help="Root password for MariaDB or PostgreSQL") +@click.option("--db-name", help="Database name for site in case it is a new one") +@click.option("--admin-password", help="Administrator password for new site") +@click.option("--install-app", multiple=True, help="Install app after installation") +@click.option("--with-public-files", help="Restores the public files of the site, given path to its tar file") +@click.option( + "--with-private-files", + help="Restores the private files of the site, given path to its tar file", +) +@click.option( + "--force", + is_flag=True, + default=False, + help="Ignore the validations and downgrade warnings. This action is not recommended", +) +@click.option("--encryption-key", help="Backup encryption key") +@pass_context +def restore( + context, + sql_file_path, + encryption_key=None, + db_root_username=None, + db_root_password=None, + db_name=None, + verbose=None, + install_app=None, + admin_password=None, + force=None, + with_public_files=None, + with_private_files=None, +): + "Restore site database from an sql file" + + from xhiveframework.utils.synchronization import filelock + + site = get_site(context) + xhiveframework.init(site=site) + + with filelock("site_restore", timeout=1): + _restore( + site=site, + sql_file_path=sql_file_path, + encryption_key=encryption_key, + db_root_username=db_root_username, + db_root_password=db_root_password, + verbose=context.verbose or verbose, + install_app=install_app, + admin_password=admin_password, + force=context.force or force, + with_public_files=with_public_files, + with_private_files=with_private_files, + ) + + +def _restore( + *, + site=None, + sql_file_path=None, + encryption_key=None, + db_root_username=None, + db_root_password=None, + verbose=None, + install_app=None, + admin_password=None, + force=None, + with_public_files=None, + with_private_files=None, +): + from xhiveframework.installer import extract_files + from xhiveframework.utils.backups import decrypt_backup, get_or_generate_backup_encryption_key + + err, out = xhiveframework.utils.execute_in_shell(f"file {sql_file_path}", check_exit_code=True) + if err: + click.secho("Failed to detect type of backup file", fg="red") + sys.exit(1) + + if "cipher" in out.decode().split(":")[-1].strip(): + if encryption_key: + click.secho("Encrypted backup file detected. Decrypting using provided key.", fg="yellow") + + else: + click.secho("Encrypted backup file detected. Decrypting using site config.", fg="yellow") + encryption_key = get_or_generate_backup_encryption_key() + + with decrypt_backup(sql_file_path, encryption_key): + # Rollback on unsuccessful decryption + if not os.path.exists(sql_file_path): + click.secho("Decryption failed. Please provide a valid key and try again.", fg="red") + sys.exit(1) + + restore_backup( + sql_file_path, + site, + db_root_username, + db_root_password, + verbose, + install_app, + admin_password, + force, + ) + else: + restore_backup( + sql_file_path, + site, + db_root_username, + db_root_password, + verbose, + install_app, + admin_password, + force, + ) + + # Extract public and/or private files to the restored site, if user has given the path + if with_public_files: + # Decrypt data if there is a Key + if encryption_key: + with decrypt_backup(with_public_files, encryption_key): + public = extract_files(site, with_public_files) + else: + public = extract_files(site, with_public_files) + + # Removing temporarily created file + os.remove(public) + + if with_private_files: + # Decrypt data if there is a Key + if encryption_key: + with decrypt_backup(with_private_files, encryption_key): + private = extract_files(site, with_private_files) + else: + private = extract_files(site, with_private_files) + + # Removing temporarily created file + os.remove(private) + + success_message = "Site {} has been restored{}".format( + site, " with files" if (with_public_files or with_private_files) else "" + ) + click.secho(success_message, fg="green") + + +def restore_backup( + sql_file_path: str, + site, + db_root_username, + db_root_password, + verbose, + install_app, + admin_password, + force, +): + from xhiveframework.installer import _new_site, is_downgrade, is_partial, validate_database_sql + + if is_partial(sql_file_path): + click.secho( + "Partial Backup file detected. You cannot use a partial file to restore a XhiveFramework site.", + fg="red", + ) + click.secho( + "Use `bench partial-restore` to restore a partial backup to an existing site.", + fg="yellow", + ) + sys.exit(1) + + # Check if the backup is of an older version of xhiveframework and the user hasn't specified force + if is_downgrade(sql_file_path, verbose=True) and not force: + warn_message = ( + "This is not recommended and may lead to unexpected behaviour. " "Do you want to continue anyway?" + ) + click.confirm(warn_message, abort=True) + + # Validate the sql file + validate_database_sql(sql_file_path, _raise=not force) + + try: + _new_site( + xhiveframework.conf.db_name, + site, + db_root_username=db_root_username, + db_root_password=db_root_password, + admin_password=admin_password, + verbose=verbose, + install_apps=install_app, + source_sql=sql_file_path, + force=True, + db_type=xhiveframework.conf.db_type, + ) + + except Exception as err: + print(err.args[1]) + sys.exit(1) + + +@click.command("partial-restore") +@click.argument("sql-file-path") +@click.option("--verbose", "-v", is_flag=True) +@click.option("--encryption-key", help="Backup encryption key") +@pass_context +def partial_restore(context, sql_file_path, verbose, encryption_key=None): + from xhiveframework.installer import is_partial, partial_restore + from xhiveframework.utils.backups import decrypt_backup, get_or_generate_backup_encryption_key + + if not os.path.exists(sql_file_path): + print("Invalid path", sql_file_path) + sys.exit(1) + + site = get_site(context) + verbose = context.verbose or verbose + xhiveframework.init(site=site) + xhiveframework.connect(site=site) + err, out = xhiveframework.utils.execute_in_shell(f"file {sql_file_path}", check_exit_code=True) + if err: + click.secho("Failed to detect type of backup file", fg="red") + sys.exit(1) + + if "cipher" in out.decode().split(":")[-1].strip(): + if encryption_key: + click.secho("Encrypted backup file detected. Decrypting using provided key.", fg="yellow") + key = encryption_key + + else: + click.secho("Encrypted backup file detected. Decrypting using site config.", fg="yellow") + key = get_or_generate_backup_encryption_key() + + with decrypt_backup(sql_file_path, key): + if not is_partial(sql_file_path): + click.secho( + "Full backup file detected. Use `bench restore` to restore a XhiveFramework Site.", + fg="red", + ) + sys.exit(1) + + partial_restore(sql_file_path, verbose) + + # Rollback on unsuccessful decryption + if not os.path.exists(sql_file_path): + click.secho("Decryption failed. Please provide a valid key and try again.", fg="red") + sys.exit(1) + + else: + if not is_partial(sql_file_path): + click.secho( + "Full backup file detected. Use `bench restore` to restore a XhiveFramework Site.", + fg="red", + ) + sys.exit(1) + + partial_restore(sql_file_path, verbose) + xhiveframework.destroy() + + +@click.command("reinstall") +@click.option("--admin-password", help="Administrator Password for reinstalled site") +@click.option( + "--db-root-username", + "--mariadb-root-username", + help='Root username for MariaDB or PostgreSQL, Default is "root"', +) +@click.option("--db-root-password", "--mariadb-root-password", help="Root password for MariaDB or PostgreSQL") +@click.option("--yes", is_flag=True, default=False, help="Pass --yes to skip confirmation") +@pass_context +def reinstall(context, admin_password=None, db_root_username=None, db_root_password=None, yes=False): + "Reinstall site ie. wipe all data and start over" + site = get_site(context) + _reinstall(site, admin_password, db_root_username, db_root_password, yes, verbose=context.verbose) + + +def _reinstall( + site, + admin_password=None, + db_root_username=None, + db_root_password=None, + yes=False, + verbose=False, +): + from xhiveframework.installer import _new_site + from xhiveframework.utils.synchronization import filelock + + if not yes: + click.confirm("This will wipe your database. Are you sure you want to reinstall?", abort=True) + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.clear_cache() + installed = xhiveframework.get_installed_apps() + xhiveframework.clear_cache() + except Exception: + installed = [] + finally: + if xhiveframework.db: + xhiveframework.db.close() + xhiveframework.destroy() + + xhiveframework.init(site=site) + + _new_site( + xhiveframework.conf.db_name, + site, + verbose=verbose, + force=True, + reinstall=True, + install_apps=installed, + db_root_username=db_root_username, + db_root_password=db_root_password, + admin_password=admin_password, + ) + + +@click.command("install-app") +@click.argument("apps", nargs=-1) +@click.option("--force", is_flag=True, default=False) +@pass_context +def install_app(context, apps, force=False): + "Install a new app to site, supports multiple apps" + from xhiveframework.installer import install_app as _install_app + from xhiveframework.utils.synchronization import filelock + + exit_code = 0 + + if not context.sites: + raise SiteNotSpecifiedError + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + + with filelock("install_app", timeout=1): + for app in apps: + try: + _install_app(app, verbose=context.verbose, force=force) + except xhiveframework.IncompatibleApp as err: + err_msg = f":\n{err}" if str(err) else "" + print(f"App {app} is Incompatible with Site {site}{err_msg}") + exit_code = 1 + except Exception as err: + err_msg = f": {err!s}\n{xhiveframework.get_traceback(with_context=True)}" + print(f"An error occurred while installing {app}{err_msg}") + exit_code = 1 + + if not exit_code: + xhiveframework.db.commit() + + xhiveframework.destroy() + + sys.exit(exit_code) + + +@click.command("list-apps") +@click.option("--format", "-f", type=click.Choice(["text", "json"]), default="text") +@pass_context +def list_apps(context, format): + """ + List apps in site. + """ + + summary_dict = {} + + def format_app(app): + name_len = max(len(app.app_name) for app in apps) + ver_len = max(len(app.app_version) for app in apps) + template = f"{{0:{name_len}}} {{1:{ver_len}}} {{2}}" + return template.format(app.app_name, app.app_version, app.git_branch) + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + site_title = click.style(f"{site}", fg="green") if len(context.sites) > 1 else "" + installed_apps_info = [] + + apps = xhiveframework.get_single("Installed Applications").installed_applications + if apps: + installed_apps_info.extend(format_app(app) for app in apps) + else: + installed_apps_info.extend(xhiveframework.get_installed_apps()) + + installed_apps_info_str = "\n".join(installed_apps_info) + summary = f"{site_title}\n{installed_apps_info_str}\n" + summary_dict[site] = [app.app_name for app in apps] + + if format == "text" and installed_apps_info and summary: + print(summary) + + xhiveframework.destroy() + + if format == "json": + click.echo(xhiveframework.as_json(summary_dict)) + + +@click.command("add-database-index") +@click.option("--doctype", help="DocType on which index needs to be added") +@click.option( + "--column", + multiple=True, + help="Column to index. Multiple columns will create multi-column index in given order. To create a multiple, single column index, execute the command multiple times.", +) +@pass_context +def add_db_index(context, doctype, column): + "Adds a new DB index and creates a property setter to persist it." + from xhiveframework.custom.doctype.property_setter.property_setter import make_property_setter + + columns = column # correct naming + for site in context.sites: + xhiveframework.connect(site=site) + try: + xhiveframework.db.add_index(doctype, columns) + if len(columns) == 1: + make_property_setter( + doctype, + columns[0], + property="search_index", + value="1", + property_type="Check", + for_doctype=False, # Applied on docfield + ) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("describe-database-table") +@click.option("--doctype", help="DocType to describe") +@click.option( + "--column", + multiple=True, + help="Explicitly fetch accurate cardinality from table data. This can be quite slow on large tables.", +) +@pass_context +def describe_database_table(context, doctype, column): + """Describes various statistics about the table. + + This is useful to build integration like + This includes: + 1. Schema + 2. Indexes + 3. stats - total count of records + 4. if column is specified then extra stats are generated for column: + Distinct values count in column + """ + import json + + for site in context.sites: + xhiveframework.connect(site=site) + try: + data = _extract_table_stats(doctype, column) + # NOTE: Do not print anything else in this to avoid clobbering the output. + print(json.dumps(data, indent=2)) + finally: + xhiveframework.destroy() + + if not context.sites: + raise SiteNotSpecifiedError + + +def _extract_table_stats(doctype: str, columns: list[str]) -> dict: + from xhiveframework.utils import cint, cstr, get_table_name + + def sql_bool(val): + return cstr(val).lower() in ("yes", "1", "true") + + table = get_table_name(doctype, wrap_in_backticks=True) + + schema = [] + for field in xhiveframework.db.sql(f"describe {table}", as_dict=True): + schema.append( + { + "column": field["Field"], + "type": field["Type"], + "is_nullable": sql_bool(field["Null"]), + "default": field["Default"], + } + ) + + def update_cardinality(column, value): + for col in schema: + if col["column"] == column: + col["cardinality"] = value + break + + indexes = [] + for idx in xhiveframework.db.sql(f"show index from {table}", as_dict=True): + indexes.append( + { + "unique": not sql_bool(idx["Non_unique"]), + "cardinality": idx["Cardinality"], + "name": idx["Key_name"], + "sequence": idx["Seq_in_index"], + "nullable": sql_bool(idx["Null"]), + "column": idx["Column_name"], + "type": idx["Index_type"], + } + ) + if idx["Seq_in_index"] == 1: + update_cardinality(idx["Column_name"], idx["Cardinality"]) + + total_rows = cint( + xhiveframework.db.sql( + f"""select table_rows + from information_schema.tables + where table_name = 'tab{doctype}'""" + )[0][0] + ) + + # fetch accurate cardinality for columns by query. WARN: This can take a lot of time. + for column in columns: + cardinality = xhiveframework.db.sql(f"select count(distinct {column}) from {table}")[0][0] + update_cardinality(column, cardinality) + + return { + "table_name": table.strip("`"), + "total_rows": total_rows, + "schema": schema, + "indexes": indexes, + } + + +@click.command("add-system-manager") +@click.argument("email") +@click.option("--first-name") +@click.option("--last-name") +@click.option("--password") +@click.option("--send-welcome-email", default=False, is_flag=True) +@pass_context +def add_system_manager(context, email, first_name, last_name, send_welcome_email, password): + "Add a new system manager to a site" + import xhiveframework.utils.user + + for site in context.sites: + xhiveframework.connect(site=site) + try: + xhiveframework.utils.user.add_system_manager(email, first_name, last_name, send_welcome_email, password) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("add-user") +@click.argument("email") +@click.option("--first-name") +@click.option("--last-name") +@click.option("--password") +@click.option("--user-type") +@click.option("--add-role", multiple=True) +@click.option("--send-welcome-email", default=False, is_flag=True) +@pass_context +def add_user_for_sites( + context, email, first_name, last_name, user_type, send_welcome_email, password, add_role +): + "Add user to a site" + import xhiveframework.utils.user + + for site in context.sites: + xhiveframework.connect(site=site) + try: + add_new_user(email, first_name, last_name, user_type, send_welcome_email, password, add_role) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("disable-user") +@click.argument("email") +@pass_context +def disable_user(context, email): + """Disable a user account on site.""" + site = get_site(context) + with xhiveframework.init_site(site): + xhiveframework.connect() + user = xhiveframework.get_doc("User", email) + user.enabled = 0 + user.save(ignore_permissions=True) + xhiveframework.db.commit() + + +@click.command("migrate") +@click.option("--skip-failing", is_flag=True, help="Skip patches that fail to run") +@click.option("--skip-search-index", is_flag=True, help="Skip search indexing for web documents") +@pass_context +def migrate(context, skip_failing=False, skip_search_index=False): + "Run patches, sync schema and rebuild files/translations" + from traceback_with_variables import activate_by_import + + from xhiveframework.migrate import SiteMigration + + for site in context.sites: + click.secho(f"Migrating {site}", fg="green") + try: + SiteMigration( + skip_failing=skip_failing, + skip_search_index=skip_search_index, + ).run(site=site) + finally: + print() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("migrate-to") +@click.argument("xhiveframework_provider") +@pass_context +def migrate_to(context, xhiveframework_provider): + "Migrates site to the specified provider" + from xhiveframework.integrations.xhiveframework_providers import migrate_to + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + migrate_to(site, xhiveframework_provider) + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("run-patch") +@click.argument("module") +@click.option("--force", is_flag=True) +@pass_context +def run_patch(context, module, force): + "Run a particular patch" + import xhiveframework.modules.patch_handler + + for site in context.sites: + xhiveframework.init(site=site) + try: + xhiveframework.connect() + xhiveframework.modules.patch_handler.run_single(module, force=force or context.force) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("reload-doc") +@click.argument("module") +@click.argument("doctype") +@click.argument("docname") +@pass_context +def reload_doc(context, module, doctype, docname): + "Reload schema for a DocType" + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.reload_doc(module, doctype, docname, force=context.force) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("reload-doctype") +@click.argument("doctype") +@pass_context +def reload_doctype(context, doctype): + "Reload schema for a DocType" + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.reload_doctype(doctype, force=context.force) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("add-to-hosts") +@pass_context +def add_to_hosts(context): + "Add site to hosts" + for site in context.sites: + xhiveframework.commands.popen(f"echo 127.0.0.1\t{site} | sudo tee -a /etc/hosts") + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("use") +@click.argument("site") +def _use(site, sites_path="."): + "Set a default site" + use(site, sites_path=sites_path) + + +def use(site, sites_path="."): + from xhiveframework.installer import update_site_config + + if os.path.exists(os.path.join(sites_path, site)): + sites_path = os.getcwd() + conifg = os.path.join(sites_path, "common_site_config.json") + update_site_config("default_site", site, validate=False, site_config_path=conifg) + print(f"Current Site set to {site}") + else: + print(f"Site {site} does not exist") + + +@click.command("backup") +@click.option("--with-files", default=False, is_flag=True, help="Take backup with files") +@click.option( + "--include", + "--only", + "-i", + default="", + type=str, + help="Specify the DocTypes to backup seperated by commas", +) +@click.option( + "--exclude", + "-e", + default="", + type=str, + help="Specify the DocTypes to not backup seperated by commas", +) +@click.option("--backup-path", default=None, help="Set path for saving all the files in this operation") +@click.option("--backup-path-db", default=None, help="Set path for saving database file") +@click.option("--backup-path-files", default=None, help="Set path for saving public file") +@click.option("--backup-path-private-files", default=None, help="Set path for saving private file") +@click.option("--backup-path-conf", default=None, help="Set path for saving config file") +@click.option( + "--ignore-backup-conf", + default=False, + is_flag=True, + help="Ignore excludes/includes set in config", +) +@click.option("--verbose", default=False, is_flag=True, help="Add verbosity") +@click.option("--compress", default=False, is_flag=True, help="Compress private and public files") +@click.option("--old-backup-metadata", default=False, is_flag=True, help="Use older backup metadata") +@pass_context +def backup( + context, + with_files=False, + backup_path=None, + backup_path_db=None, + backup_path_files=None, + backup_path_private_files=None, + backup_path_conf=None, + ignore_backup_conf=False, + verbose=False, + compress=False, + include="", + exclude="", + old_backup_metadata=False, +): + "Backup" + + from xhiveframework.utils.backups import scheduled_backup + + verbose = verbose or context.verbose + exit_code = 0 + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + odb = scheduled_backup( + ignore_files=not with_files, + backup_path=backup_path, + backup_path_db=backup_path_db, + backup_path_files=backup_path_files, + backup_path_private_files=backup_path_private_files, + backup_path_conf=backup_path_conf, + ignore_conf=ignore_backup_conf, + include_doctypes=include, + exclude_doctypes=exclude, + compress=compress, + verbose=verbose, + force=True, + old_backup_metadata=old_backup_metadata, + ) + except Exception: + click.secho( + f"Backup failed for Site {site}. Database or site_config.json may be corrupted", + fg="red", + ) + if verbose: + print(xhiveframework.get_traceback(with_context=True)) + exit_code = 1 + continue + if xhiveframework.get_system_settings("encrypt_backup") and xhiveframework.get_site_config().encryption_key: + click.secho( + "Backup encryption is turned on. Please note the backup encryption key.", + fg="yellow", + ) + + odb.print_summary() + click.secho( + "Backup for Site {} has been successfully completed{}".format( + site, " with files" if with_files else "" + ), + fg="green", + ) + xhiveframework.destroy() + + if not context.sites: + raise SiteNotSpecifiedError + + sys.exit(exit_code) + + +@click.command("remove-from-installed-apps") +@click.argument("app") +@pass_context +def remove_from_installed_apps(context, app): + "Remove app from site's installed-apps list" + from xhiveframework.installer import remove_from_installed_apps + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + remove_from_installed_apps(app) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("uninstall-app") +@click.argument("app") +@click.option( + "--yes", + "-y", + help="To bypass confirmation prompt for uninstalling the app", + is_flag=True, + default=False, +) +@click.option("--dry-run", help="List all doctypes that will be deleted", is_flag=True, default=False) +@click.option("--no-backup", help="Do not backup the site", is_flag=True, default=False) +@click.option("--force", help="Force remove app from site", is_flag=True, default=False) +@pass_context +def uninstall(context, app, dry_run, yes, no_backup, force): + "Remove app and linked modules from site" + from xhiveframework.installer import remove_app + from xhiveframework.utils.synchronization import filelock + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + with filelock("uninstall_app"): + remove_app(app_name=app, dry_run=dry_run, yes=yes, no_backup=no_backup, force=force) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("drop-site") +@click.argument("site") +@click.option( + "--db-root-username", + "--mariadb-root-username", + "--root-login", + help='Root username for MariaDB or PostgreSQL, Default is "root"', +) +@click.option( + "--db-root-password", + "--mariadb-root-password", + "--root-password", + help="Root password for MariaDB or PostgreSQL", +) +@click.option("--archived-sites-path") +@click.option("--no-backup", is_flag=True, default=False) +@click.option("--force", help="Force drop-site even if an error is encountered", is_flag=True, default=False) +def drop_site( + site, + db_root_username="root", + db_root_password=None, + archived_sites_path=None, + force=False, + no_backup=False, +): + """Remove a site from database and filesystem.""" + _drop_site(site, db_root_username, db_root_password, archived_sites_path, force, no_backup) + + +def _drop_site( + site, + db_root_username=None, + db_root_password=None, + archived_sites_path=None, + force=False, + no_backup=False, +): + from xhiveframework.database import drop_user_and_database + from xhiveframework.utils.backups import scheduled_backup + + xhiveframework.init(site=site) + xhiveframework.connect() + + try: + if not no_backup: + click.secho(f"Taking backup of {site}", fg="green") + odb = scheduled_backup(ignore_files=False, ignore_conf=True, force=True, verbose=True) + odb.print_summary() + except Exception as err: + if force: + pass + else: + messages = [ + "=" * 80, + f"Error: The operation has stopped because backup of {site}'s database failed.", + f"Reason: {err!s}\n", + "Fix the issue and try again.", + f"Hint: Use 'bench drop-site {site} --force' to force the removal of {site}", + ] + click.echo("\n".join(messages)) + sys.exit(1) + + click.secho("Dropping site database and user", fg="green") + drop_user_and_database(xhiveframework.conf.db_name, db_root_username, db_root_password) + + archived_sites_path = archived_sites_path or os.path.join( + xhiveframework.utils.get_bench_path(), "archived", "sites" + ) + archived_sites_path = os.path.realpath(archived_sites_path) + + click.secho(f"Moving site to archive under {archived_sites_path}", fg="green") + os.makedirs(archived_sites_path, exist_ok=True) + move(archived_sites_path, site) + + +def move(dest_dir, site): + if not os.path.isdir(dest_dir): + raise Exception("destination is not a directory or does not exist") + + xhiveframework.init(site) + old_path = xhiveframework.utils.get_site_path() + new_path = os.path.join(dest_dir, site) + + # check if site dump of same name already exists + site_dump_exists = True + count = 0 + while site_dump_exists: + final_new_path = new_path + (count and str(count) or "") + site_dump_exists = os.path.exists(final_new_path) + count = int(count or 0) + 1 + + shutil.move(old_path, final_new_path) + xhiveframework.destroy() + return final_new_path + + +@click.command("set-password") +@click.argument("user") +@click.argument("password", required=False) +@click.option("--logout-all-sessions", help="Log out from all sessions", is_flag=True, default=False) +@pass_context +def set_password(context, user, password=None, logout_all_sessions=False): + "Set password for a user on a site" + if not context.sites: + raise SiteNotSpecifiedError + + for site in context.sites: + set_user_password(site, user, password, logout_all_sessions) + + +@click.command("set-admin-password") +@click.argument("admin-password", required=False) +@click.option("--logout-all-sessions", help="Log out from all sessions", is_flag=True, default=False) +@pass_context +def set_admin_password(context, admin_password=None, logout_all_sessions=False): + "Set Administrator password for a site" + if not context.sites: + raise SiteNotSpecifiedError + + for site in context.sites: + set_user_password(site, "Administrator", admin_password, logout_all_sessions) + + +def set_user_password(site, user, password, logout_all_sessions=False): + import getpass + + from xhiveframework.utils.password import update_password + + try: + xhiveframework.init(site=site) + + while not password: + password = getpass.getpass(f"{user}'s password for {site}: ") + + xhiveframework.connect() + if not xhiveframework.db.exists("User", user): + print(f"User {user} does not exist") + sys.exit(1) + + update_password(user=user, pwd=password, logout_all_sessions=logout_all_sessions) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + + +@click.command("set-last-active-for-user") +@click.option("--user", help="Setup last active date for user") +@pass_context +def set_last_active_for_user(context, user=None): + "Set users last active date to current datetime" + from xhiveframework.core.doctype.user.user import get_system_users + from xhiveframework.utils import now_datetime + + site = get_site(context) + + with xhiveframework.init_site(site): + xhiveframework.connect() + if not user: + user = get_system_users(limit=1) + if len(user) > 0: + user = user[0] + else: + return + + xhiveframework.db.set_value("User", user, "last_active", now_datetime()) + xhiveframework.db.commit() + + +@click.command("publish-realtime") +@click.argument("event") +@click.option("--message") +@click.option("--room") +@click.option("--user") +@click.option("--doctype") +@click.option("--docname") +@click.option("--after-commit") +@pass_context +def publish_realtime(context, event, message, room, user, doctype, docname, after_commit): + "Publish realtime event from bench" + from xhiveframework import publish_realtime + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + publish_realtime( + event, + message=message, + room=room, + user=user, + doctype=doctype, + docname=docname, + after_commit=after_commit, + ) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("browse") +@click.argument("site", required=False) +@click.option("--user", required=False, help="Login as user") +@pass_context +def browse(context, site, user=None): + """Opens the site on web browser""" + from xhiveframework.auth import CookieManager, LoginManager + + site = get_site(context, raise_err=False) or site + + if not site: + raise SiteNotSpecifiedError + + if site not in xhiveframework.utils.get_sites(): + click.echo(f"\nSite named {click.style(site, bold=True)} doesn't exist\n", err=True) + sys.exit(1) + + xhiveframework.init(site=site) + xhiveframework.connect() + + sid = "" + if user: + if xhiveframework.conf.developer_mode or user == "Administrator": + xhiveframework.utils.set_request(path="/") + xhiveframework.local.cookie_manager = CookieManager() + xhiveframework.local.login_manager = LoginManager() + xhiveframework.local.login_manager.login_as(user) + sid = f"/app?sid={xhiveframework.session.sid}" + else: + click.echo("Please enable developer mode to login as a user") + + url = f"{xhiveframework.utils.get_site_url(site)}{sid}" + + if user == "Administrator": + click.echo(f"Login URL: {url}") + + click.launch(url) + + +@click.command("start-recording") +@pass_context +def start_recording(context): + """Start XhiveFramework Recorder.""" + import xhiveframework.recorder + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.set_user("Administrator") + xhiveframework.recorder.start() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("stop-recording") +@pass_context +def stop_recording(context): + """Stop XhiveFramework Recorder.""" + import xhiveframework.recorder + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.set_user("Administrator") + xhiveframework.recorder.stop() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("ngrok") +@click.option("--bind-tls", is_flag=True, default=False, help="Returns a reference to the https tunnel.") +@click.option( + "--use-default-authtoken", + is_flag=True, + default=False, + help="Use the auth token present in ngrok's config.", +) +@pass_context +def start_ngrok(context, bind_tls, use_default_authtoken): + """Start a ngrok tunnel to your local development server.""" + from pyngrok import ngrok + + site = get_site(context) + xhiveframework.init(site=site) + + ngrok_authtoken = xhiveframework.conf.ngrok_authtoken + if not use_default_authtoken: + if not ngrok_authtoken: + click.echo( + f"\n{click.style('ngrok_authtoken', fg='yellow')} not found in site config.\n" + "Please register for a free ngrok account at: https://dashboard.ngrok.com/signup and place the obtained authtoken in the site config.", + ) + sys.exit(1) + + ngrok.set_auth_token(ngrok_authtoken) + + port = xhiveframework.conf.http_port or xhiveframework.conf.webserver_port + tunnel = ngrok.connect(addr=str(port), host_header=site, bind_tls=bind_tls) + print(f"Public URL: {tunnel.public_url}") + print("Inspect logs at http://127.0.0.1:4040") + + ngrok_process = ngrok.get_ngrok_process() + try: + # Block until CTRL-C or some other terminating event + ngrok_process.proc.wait() + except KeyboardInterrupt: + print("Shutting down server...") + xhiveframework.destroy() + ngrok.kill() + + +@click.command("build-search-index") +@pass_context +def build_search_index(context): + """Rebuild search index used by global search.""" + from xhiveframework.search.website_search import build_index_for_all_routes + + site = get_site(context) + if not site: + raise SiteNotSpecifiedError + + print(f"Building search index for {site}") + xhiveframework.init(site=site) + xhiveframework.connect() + try: + build_index_for_all_routes() + finally: + xhiveframework.destroy() + + +@click.command("clear-log-table") +@click.option("--doctype", required=True, type=str, help="Log DocType") +@click.option("--days", type=int, help="Keep records for days") +@click.option("--no-backup", is_flag=True, default=False, help="Do not backup the table") +@pass_context +def clear_log_table(context, doctype, days, no_backup): + """If any logtype table grows too large then clearing it with DELETE query + is not feasible in reasonable time. This command copies recent data to new + table and replaces current table with new smaller table. + + + ref: https://mariadb.com/kb/en/big-deletes/#deleting-more-than-half-a-table + """ + from xhiveframework.core.doctype.log_settings.log_settings import LOG_DOCTYPES + from xhiveframework.core.doctype.log_settings.log_settings import clear_log_table as clear_logs + from xhiveframework.utils.backups import scheduled_backup + + if not context.sites: + raise SiteNotSpecifiedError + + if doctype not in LOG_DOCTYPES: + raise xhiveframework.ValidationError(f"Unsupported logging DocType: {doctype}") + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + + if not no_backup: + scheduled_backup( + ignore_conf=False, + include_doctypes=doctype, + ignore_files=True, + force=True, + ) + click.echo(f"Backed up {doctype}") + + try: + click.echo(f"Copying {doctype} records from last {days} days to temporary table.") + clear_logs(doctype, days=days) + except Exception as e: + click.echo(f"Log cleanup for {doctype} failed:\n{e}") + sys.exit(1) + else: + click.secho(f"Cleared {doctype} records older than {days} days", fg="green") + + +@click.command("trim-database") +@click.option("--dry-run", is_flag=True, default=False, help="Show what would be deleted") +@click.option("--format", "-f", default="text", type=click.Choice(["json", "text"]), help="Output format") +@click.option("--no-backup", is_flag=True, default=False, help="Do not backup the site") +@click.option( + "--yes", + "-y", + help="To bypass confirmation prompt.", + is_flag=True, + default=False, +) +@pass_context +def trim_database(context, dry_run, format, no_backup, yes=False): + """Remove database tables for deleted DocTypes.""" + if not context.sites: + raise SiteNotSpecifiedError + + from xhiveframework.utils.backups import scheduled_backup + + ALL_DATA = {} + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + + TABLES_TO_DROP = [] + STANDARD_TABLES = get_standard_tables() + information_schema = xhiveframework.qb.Schema("information_schema") + table_name = xhiveframework.qb.Field("table_name").as_("name") + + database_tables: list[str] = ( + xhiveframework.qb.from_(information_schema.tables) + .select(table_name) + .where(information_schema.tables.table_schema == xhiveframework.conf.db_name) + .where(information_schema.tables.table_type == "BASE TABLE") + .run(pluck=True) + ) + doctype_tables = xhiveframework.get_all("DocType", pluck="name") + + for table_name in database_tables: + if not table_name.startswith("tab"): + continue + if not (table_name.replace("tab", "", 1) in doctype_tables or table_name in STANDARD_TABLES): + TABLES_TO_DROP.append(table_name) + + if not TABLES_TO_DROP: + if format == "text": + click.secho(f"{site}: No ghost tables", fg="green") + else: + if format == "text": + print(f"{site}: Following tables will be dropped:") + print("\n".join(f"* {dt}" for dt in TABLES_TO_DROP)) + + if dry_run: + continue + + if not yes: + click.confirm("Do you want to continue?", abort=True) + + if not no_backup: + if format == "text": + print(f"Backing Up Tables: {', '.join(TABLES_TO_DROP)}") + + odb = scheduled_backup( + ignore_conf=False, + include_doctypes=",".join(x.replace("tab", "", 1) for x in TABLES_TO_DROP), + ignore_files=True, + force=True, + ) + if format == "text": + odb.print_summary() + print("\nTrimming Database") + + for table in TABLES_TO_DROP: + if format == "text": + print(f"* Dropping Table '{table}'...") + xhiveframework.db.sql_ddl(f"drop table `{table}`") + + ALL_DATA[xhiveframework.local.site] = TABLES_TO_DROP + xhiveframework.destroy() + + if format == "json": + import json + + print(json.dumps(ALL_DATA, indent=1)) + + +def get_standard_tables(): + import re + + tables = [] + sql_file = os.path.join( + "..", + "apps", + "xhiveframework", + "xhiveframework", + "database", + xhiveframework.conf.db_type, + f"framework_{xhiveframework.conf.db_type}.sql", + ) + content = open(sql_file).read().splitlines() + + for line in content: + table_found = re.search(r"""CREATE TABLE ("|`)(.*)?("|`) \(""", line) + if table_found: + tables.append(table_found.group(2)) + + return tables + + +@click.command("trim-tables") +@click.option("--dry-run", is_flag=True, default=False, help="Show what would be deleted") +@click.option("--format", "-f", default="table", type=click.Choice(["json", "table"]), help="Output format") +@click.option("--no-backup", is_flag=True, default=False, help="Do not backup the site") +@pass_context +def trim_tables(context, dry_run, format, no_backup): + """Remove columns from tables where fields are deleted from doctypes.""" + if not context.sites: + raise SiteNotSpecifiedError + + from xhiveframework.model.meta import trim_tables + from xhiveframework.utils.backups import scheduled_backup + + for site in context.sites: + xhiveframework.init(site=site) + xhiveframework.connect() + + if not (no_backup or dry_run): + click.secho(f"Taking backup for {xhiveframework.local.site}", fg="green") + odb = scheduled_backup(ignore_files=False, force=True) + odb.print_summary() + + try: + trimmed_data = trim_tables(dry_run=dry_run, quiet=format == "json") + + if format == "table" and not dry_run: + click.secho(f"The following data have been removed from {xhiveframework.local.site}", fg="green") + + handle_data(trimmed_data, format=format) + finally: + xhiveframework.destroy() + + +def handle_data(data: dict, format="json"): + if format == "json": + import json + + print(json.dumps({xhiveframework.local.site: data}, indent=1, sort_keys=True)) + else: + from xhiveframework.utils.commands import render_table + + data = [["DocType", "Fields"]] + [[table, ", ".join(columns)] for table, columns in data.items()] + render_table(data) + + +def add_new_user( + email, + first_name=None, + last_name=None, + user_type="System User", + send_welcome_email=False, + password=None, + role=None, +): + user = xhiveframework.new_doc("User") + user.update( + { + "name": email, + "email": email, + "enabled": 1, + "first_name": first_name or email, + "last_name": last_name, + "user_type": user_type, + "send_welcome_email": 1 if send_welcome_email else 0, + } + ) + user.insert() + user.add_roles(*role) + if password: + from xhiveframework.utils.password import update_password + + update_password(user=user.name, pwd=password) + + +commands = [ + add_system_manager, + add_user_for_sites, + add_db_index, + describe_database_table, + backup, + drop_site, + install_app, + list_apps, + migrate, + migrate_to, + new_site, + reinstall, + reload_doc, + reload_doctype, + remove_from_installed_apps, + restore, + run_patch, + set_password, + set_admin_password, + uninstall, + disable_user, + _use, + set_last_active_for_user, + publish_realtime, + browse, + start_recording, + stop_recording, + add_to_hosts, + start_ngrok, + build_search_index, + partial_restore, + trim_tables, + trim_database, + clear_log_table, +] diff --git a/xhiveframework/commands/translate.py b/xhiveframework/commands/translate.py new file mode 100644 index 0000000..f31593c --- /dev/null +++ b/xhiveframework/commands/translate.py @@ -0,0 +1,123 @@ +import click + +from xhiveframework.commands import get_site, pass_context +from xhiveframework.exceptions import SiteNotSpecifiedError + + +# translation +@click.command("build-message-files") +@pass_context +def build_message_files(context): + "Build message files for translation" + import xhiveframework.translate + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.translate.rebuild_all_translation_files() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("new-language") # , help="Create lang-code.csv for given app") +@pass_context +@click.argument("lang_code") # , help="Language code eg. en") +@click.argument("app") # , help="App name eg. xhiveframework") +def new_language(context, lang_code, app): + """Create lang-code.csv for given app""" + import xhiveframework.translate + + if not context["sites"]: + raise Exception("--site is required") + + # init site + xhiveframework.connect(site=context["sites"][0]) + xhiveframework.translate.write_translations_file(app, lang_code) + + print(f"File created at ./apps/{app}/{app}/translations/{lang_code}.csv") + print("You will need to add the language in xhiveframework/geo/languages.json, if you haven't done it already.") + + +@click.command("get-untranslated") +@click.option("--app", default="_ALL_APPS") +@click.argument("lang") +@click.argument("untranslated_file") +@click.option("--all", default=False, is_flag=True, help="Get all message strings") +@pass_context +def get_untranslated(context, lang, untranslated_file, app="_ALL_APPS", all=None): + "Get untranslated strings for language" + import xhiveframework.translate + + site = get_site(context) + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.translate.get_untranslated(lang, untranslated_file, get_all=all, app=app) + finally: + xhiveframework.destroy() + + +@click.command("update-translations") +@click.option("--app", default="_ALL_APPS") +@click.argument("lang") +@click.argument("untranslated_file") +@click.argument("translated-file") +@pass_context +def update_translations(context, lang, untranslated_file, translated_file, app="_ALL_APPS"): + "Update translated strings" + import xhiveframework.translate + + site = get_site(context) + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.translate.update_translations(lang, untranslated_file, translated_file, app=app) + finally: + xhiveframework.destroy() + + +@click.command("import-translations") +@click.argument("lang") +@click.argument("path") +@pass_context +def import_translations(context, lang, path): + "Update translated strings" + import xhiveframework.translate + + site = get_site(context) + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.translate.import_translations(lang, path) + finally: + xhiveframework.destroy() + + +@click.command("migrate-translations") +@click.argument("source-app") +@click.argument("target-app") +@pass_context +def migrate_translations(context, source_app, target_app): + "Migrate target-app-specific translations from source-app to target-app" + import xhiveframework.translate + + site = get_site(context) + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.translate.migrate_translations(source_app, target_app) + finally: + xhiveframework.destroy() + + +commands = [ + build_message_files, + get_untranslated, + import_translations, + new_language, + update_translations, + migrate_translations, +] diff --git a/xhiveframework/commands/utils.py b/xhiveframework/commands/utils.py new file mode 100644 index 0000000..e4cf021 --- /dev/null +++ b/xhiveframework/commands/utils.py @@ -0,0 +1,1181 @@ +import json +import os +import subprocess +import sys +import typing +from shutil import which + +import click + +import xhiveframework +from xhiveframework import _ +from xhiveframework.commands import get_site, pass_context +from xhiveframework.coverage import CodeCoverage +from xhiveframework.exceptions import SiteNotSpecifiedError +from xhiveframework.utils import cint, update_progress_bar + +EXTRA_ARGS_CTX = {"ignore_unknown_options": True, "allow_extra_args": True} + +if typing.TYPE_CHECKING: + from IPython.terminal.embed import InteractiveShellEmbed + + +@click.command("build") +@click.option("--app", help="Build assets for app") +@click.option("--apps", help="Build assets for specific apps") +@click.option( + "--hard-link", + is_flag=True, + default=False, + help="Copy the files instead of symlinking", + envvar="XHIVEFRAMEWORK_HARD_LINK_ASSETS", +) +@click.option("--production", is_flag=True, default=False, help="Build assets in production mode") +@click.option("--verbose", is_flag=True, default=False, help="Verbose") +@click.option( + "--force", is_flag=True, default=False, help="Force build assets instead of downloading available" +) +@click.option( + "--save-metafiles", + is_flag=True, + default=False, + help="Saves esbuild metafiles for built assets. Useful for analyzing bundle size. More info: https://esbuild.github.io/api/#metafile", +) +def build( + app=None, + apps=None, + hard_link=False, + production=False, + verbose=False, + force=False, + save_metafiles=False, +): + "Compile JS and CSS source files" + from xhiveframework.build import bundle, download_xhiveframework_assets + from xhiveframework.utils.synchronization import filelock + + xhiveframework.init("") + + if not apps and app: + apps = app + + with filelock("bench_build", is_global=True, timeout=10): + # dont try downloading assets if force used, app specified or running via CI + if not (force or apps or os.environ.get("CI")): + # skip building xhiveframework if assets exist remotely + skip_xhiveframework = download_xhiveframework_assets(verbose=verbose) + else: + skip_xhiveframework = False + + # don't minify in developer_mode for faster builds + development = xhiveframework.local.conf.developer_mode or xhiveframework.local.dev_server + mode = "development" if development else "production" + if production: + mode = "production" + + bundle( + mode, + apps=apps, + hard_link=hard_link, + verbose=verbose, + skip_xhiveframework=skip_xhiveframework, + save_metafiles=save_metafiles, + ) + + +@click.command("watch") +@click.option("--apps", help="Watch assets for specific apps") +def watch(apps=None): + "Watch and compile JS and CSS files as and when they change" + from xhiveframework.build import watch + + xhiveframework.init("") + watch(apps) + + +@click.command("clear-cache") +@pass_context +def clear_cache(context): + "Clear cache, doctype cache and defaults" + import xhiveframework.sessions + from xhiveframework.desk.notifications import clear_notifications + from xhiveframework.website.utils import clear_website_cache + + for site in context.sites: + try: + xhiveframework.connect(site) + xhiveframework.clear_cache() + clear_notifications() + clear_website_cache() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("clear-website-cache") +@pass_context +def clear_website_cache(context): + "Clear website cache" + from xhiveframework.website.utils import clear_website_cache + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + clear_website_cache() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("destroy-all-sessions") +@click.option("--reason") +@pass_context +def destroy_all_sessions(context, reason=None): + "Clear sessions of all users (logs them out)" + import xhiveframework.sessions + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.sessions.clear_all_sessions(reason) + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("show-config") +@click.option("--format", "-f", type=click.Choice(["text", "json"]), default="text") +@pass_context +def show_config(context, format): + "Print configuration file to STDOUT in speified format" + + if not context.sites: + raise SiteNotSpecifiedError + + sites_config = {} + sites_path = os.getcwd() + + from xhiveframework.utils.commands import render_table + + def transform_config(config, prefix=None): + prefix = f"{prefix}." if prefix else "" + site_config = [] + + for conf, value in config.items(): + if isinstance(value, dict): + site_config += transform_config(value, prefix=f"{prefix}{conf}") + else: + log_value = json.dumps(value) if isinstance(value, list) else value + site_config += [[f"{prefix}{conf}", log_value]] + + return site_config + + for site in context.sites: + xhiveframework.init(site) + + if len(context.sites) != 1 and format == "text": + if context.sites.index(site) != 0: + click.echo() + click.secho(f"Site {site}", fg="yellow") + + configuration = xhiveframework.get_site_config(sites_path=sites_path, site_path=site) + + if format == "text": + data = transform_config(configuration) + data.insert(0, ["Config", "Value"]) + render_table(data) + + if format == "json": + sites_config[site] = configuration + + xhiveframework.destroy() + + if format == "json": + click.echo(xhiveframework.as_json(sites_config)) + + +@click.command("reset-perms") +@pass_context +def reset_perms(context): + "Reset permissions for all doctypes" + from xhiveframework.permissions import reset_perms + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + for d in xhiveframework.db.sql_list( + """select name from `tabDocType` + where istable=0 and custom=0""" + ): + xhiveframework.clear_cache(doctype=d) + reset_perms(d) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("execute") +@click.argument("method") +@click.option("--args") +@click.option("--kwargs") +@click.option("--profile", is_flag=True, default=False) +@pass_context +def execute(context, method, args=None, kwargs=None, profile=False): + "Execute a function" + for site in context.sites: + ret = "" + try: + xhiveframework.init(site=site) + xhiveframework.connect() + + if args: + try: + args = eval(args) + except NameError: + args = [args] + else: + args = () + + if kwargs: + kwargs = eval(kwargs) + else: + kwargs = {} + + if profile: + import cProfile + + pr = cProfile.Profile() + pr.enable() + + try: + ret = xhiveframework.get_attr(method)(*args, **kwargs) + except Exception: + # eval is safe here because input is from console + ret = eval(method + "(*args, **kwargs)", globals(), locals()) # nosemgrep + + if profile: + import pstats + from io import StringIO + + pr.disable() + s = StringIO() + pstats.Stats(pr, stream=s).sort_stats("cumulative").print_stats(0.5) + print(s.getvalue()) + + if xhiveframework.db: + xhiveframework.db.commit() + finally: + xhiveframework.destroy() + if ret: + from xhiveframework.utils.response import json_handler + + print(json.dumps(ret, default=json_handler)) + + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("add-to-email-queue") +@click.argument("email-path") +@pass_context +def add_to_email_queue(context, email_path): + "Add an email to the Email Queue" + site = get_site(context) + + if os.path.isdir(email_path): + with xhiveframework.init_site(site): + xhiveframework.connect() + for email in os.listdir(email_path): + with open(os.path.join(email_path, email)) as email_data: + kwargs = json.load(email_data) + kwargs["delayed"] = True + xhiveframework.sendmail(**kwargs) + xhiveframework.db.commit() + + +@click.command("export-doc") +@click.argument("doctype") +@click.argument("docname") +@pass_context +def export_doc(context, doctype, docname): + "Export a single document to csv" + import xhiveframework.modules + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.modules.export_doc(doctype, docname) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("export-json") +@click.argument("doctype") +@click.argument("path") +@click.option("--name", help="Export only one document") +@pass_context +def export_json(context, doctype, path, name=None): + "Export doclist as json to the given path, use '-' as name for Singles." + from xhiveframework.core.doctype.data_import.data_import import export_json + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + export_json(doctype, path, name=name) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("export-csv") +@click.argument("doctype") +@click.argument("path") +@pass_context +def export_csv(context, doctype, path): + "Export data import template with data for DocType" + from xhiveframework.core.doctype.data_import.data_import import export_csv + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + export_csv(doctype, path) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("export-fixtures") +@click.option("--app", default=None, help="Export fixtures of a specific app") +@pass_context +def export_fixtures(context, app=None): + "Export fixtures" + from xhiveframework.utils.fixtures import export_fixtures + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + export_fixtures(app=app) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("import-doc") +@click.argument("path") +@pass_context +def import_doc(context, path, force=False): + "Import (insert/update) doclist. If the argument is a directory, all files ending with .json are imported" + from xhiveframework.core.doctype.data_import.data_import import import_doc + + if not os.path.exists(path): + path = os.path.join("..", path) + if not os.path.exists(path): + print(f"Invalid path {path}") + sys.exit(1) + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + import_doc(path) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("data-import") +@click.option( + "--file", + "file_path", + type=click.Path(exists=True, dir_okay=False, resolve_path=True), + required=True, + help=( + "Path to import file (.csv, .xlsx)." + "Consider that relative paths will resolve from 'sites' directory" + ), +) +@click.option("--doctype", type=str, required=True) +@click.option( + "--type", + "import_type", + type=click.Choice(["Insert", "Update"], case_sensitive=False), + default="Insert", + help="Insert New Records or Update Existing Records", +) +@click.option("--submit-after-import", default=False, is_flag=True, help="Submit document after importing it") +@click.option("--mute-emails", default=True, is_flag=True, help="Mute emails during import") +@pass_context +def data_import(context, file_path, doctype, import_type=None, submit_after_import=False, mute_emails=True): + "Import documents in bulk from CSV or XLSX using data import" + from xhiveframework.core.doctype.data_import.data_import import import_file + + site = get_site(context) + + xhiveframework.init(site=site) + xhiveframework.connect() + import_file(doctype, file_path, import_type, submit_after_import, console=True) + xhiveframework.destroy() + + +@click.command("bulk-rename") +@click.argument("doctype") +@click.argument("path") +@pass_context +def bulk_rename(context, doctype, path): + "Rename multiple records via CSV file" + from xhiveframework.model.rename_doc import bulk_rename + from xhiveframework.utils.csvutils import read_csv_content + + site = get_site(context) + + with open(path) as csvfile: + rows = read_csv_content(csvfile.read()) + + xhiveframework.init(site=site) + xhiveframework.connect() + + bulk_rename(doctype, rows, via_console=True) + + xhiveframework.destroy() + + +@click.command("db-console", context_settings=EXTRA_ARGS_CTX) +@click.argument("extra_args", nargs=-1) +@pass_context +def database(context, extra_args): + """ + Enter into the Database console for given site. + """ + site = get_site(context) + xhiveframework.init(site=site) + _enter_console(extra_args=extra_args) + + +@click.command("mariadb", context_settings=EXTRA_ARGS_CTX) +@click.argument("extra_args", nargs=-1) +@pass_context +def mariadb(context, extra_args): + """ + Enter into mariadb console for a given site. + """ + site = get_site(context) + xhiveframework.init(site=site) + xhiveframework.conf.db_type = "mariadb" + _enter_console(extra_args=extra_args) + + +@click.command("postgres", context_settings=EXTRA_ARGS_CTX) +@click.argument("extra_args", nargs=-1) +@pass_context +def postgres(context, extra_args): + """ + Enter into postgres console for a given site. + """ + site = get_site(context) + xhiveframework.init(site=site) + xhiveframework.conf.db_type = "postgres" + _enter_console(extra_args=extra_args) + + +def _enter_console(extra_args=None): + from xhiveframework.database import get_command + from xhiveframework.utils import get_site_path + + if xhiveframework.conf.db_type == "mariadb": + os.environ["MYSQL_HISTFILE"] = os.path.abspath(get_site_path("logs", "mariadb_console.log")) + else: + os.environ["PSQL_HISTORY"] = os.path.abspath(get_site_path("logs", "postgresql_console.log")) + + bin, args, bin_name = get_command( + host=xhiveframework.conf.db_host, + port=xhiveframework.conf.db_port, + user=xhiveframework.conf.db_name, + password=xhiveframework.conf.db_password, + db_name=xhiveframework.conf.db_name, + extra=list(extra_args) if extra_args else [], + ) + if not bin: + xhiveframework.throw( + _("{} not found in PATH! This is required to access the console.").format(bin_name), + exc=xhiveframework.ExecutableNotFound, + ) + os.execv(bin, [bin, *args]) + + +@click.command("jupyter") +@pass_context +def jupyter(context): + """Start an interactive jupyter notebook""" + installed_packages = ( + r.split("==", 1)[0] + for r in subprocess.check_output([sys.executable, "-m", "pip", "freeze"], encoding="utf8") + ) + + if "jupyter" not in installed_packages: + subprocess.check_output([sys.executable, "-m", "pip", "install", "jupyter"]) + + site = get_site(context) + xhiveframework.init(site=site) + + jupyter_notebooks_path = os.path.abspath(xhiveframework.get_site_path("jupyter_notebooks")) + sites_path = os.path.abspath(xhiveframework.get_site_path("..")) + + try: + os.stat(jupyter_notebooks_path) + except OSError: + print(f"Creating folder to keep jupyter notebooks at {jupyter_notebooks_path}") + os.mkdir(jupyter_notebooks_path) + bin_path = os.path.abspath("../env/bin") + print( + f""" +Starting Jupyter notebook +Run the following in your first cell to connect notebook to xhiveframework +``` +import xhiveframework +xhiveframework.init(site='{site}', sites_path='{sites_path}') +xhiveframework.connect() +xhiveframework.local.lang = xhiveframework.db.get_default('lang') +xhiveframework.db.connect() +``` + """ + ) + os.execv( + f"{bin_path}/jupyter", + [ + f"{bin_path}/jupyter", + "notebook", + jupyter_notebooks_path, + ], + ) + + +def _console_cleanup(): + # Execute after_rollback on console close + xhiveframework.db.rollback() + xhiveframework.destroy() + + +def store_logs(terminal: "InteractiveShellEmbed") -> None: + from contextlib import suppress + + xhiveframework.log_level = 20 # info + with suppress(Exception): + logger = xhiveframework.logger("ipython") + logger.info("=== bench console session ===") + for line in terminal.history_manager.get_range(): + logger.info(line[2]) + logger.info("=== session end ===") + + +@click.command("console") +@click.option("--autoreload", is_flag=True, help="Reload changes to code automatically") +@pass_context +def console(context, autoreload=False): + "Start ipython console for a site" + site = get_site(context) + xhiveframework.init(site=site) + xhiveframework.connect() + xhiveframework.local.lang = xhiveframework.db.get_default("lang") + + from atexit import register + + from IPython.terminal.embed import InteractiveShellEmbed + + register(_console_cleanup) + + terminal = InteractiveShellEmbed.instance() + if autoreload: + terminal.extension_manager.load_extension("autoreload") + terminal.run_line_magic("autoreload", "2") + + all_apps = xhiveframework.get_installed_apps() + failed_to_import = [] + register(store_logs, terminal) # Note: atexit runs in reverse order of registration + + for app in list(all_apps): + try: + locals()[app] = __import__(app) + except ModuleNotFoundError: + failed_to_import.append(app) + all_apps.remove(app) + + print("Apps in this namespace:\n{}".format(", ".join(all_apps))) + if failed_to_import: + print("\nFailed to import:\n{}".format(", ".join(failed_to_import))) + + # ref: https://stackoverflow.com/a/74681224 + try: + from IPython.core import ultratb + + ultratb.VerboseTB._tb_highlight = "bg:ansibrightblack" + except Exception: + pass + + terminal.colors = "neutral" + terminal.display_banner = False + terminal() + + +@click.command("transform-database", help="Change tables' internal settings changing engine and row formats") +@click.option( + "--table", + required=True, + help="Comma separated name of tables to convert. To convert all tables, pass 'all'", +) +@click.option( + "--engine", + default=None, + type=click.Choice(["InnoDB", "MyISAM"]), + help="Choice of storage engine for said table(s)", +) +@click.option( + "--row_format", + default=None, + type=click.Choice(["DYNAMIC", "COMPACT", "REDUNDANT", "COMPRESSED"]), + help="Set ROW_FORMAT parameter for said table(s)", +) +@click.option("--failfast", is_flag=True, default=False, help="Exit on first failure occurred") +@pass_context +def transform_database(context, table, engine, row_format, failfast): + "Transform site database through given parameters" + site = get_site(context) + check_table = [] + add_line = False + skipped = 0 + xhiveframework.init(site=site) + + if xhiveframework.conf.db_type != "mariadb": + click.secho("This command only has support for MariaDB databases at this point", fg="yellow") + sys.exit(1) + + if not (engine or row_format): + click.secho("Values for `--engine` or `--row_format` must be set") + sys.exit(1) + + xhiveframework.connect() + + if table == "all": + information_schema = xhiveframework.qb.Schema("information_schema") + queried_tables = ( + xhiveframework.qb.from_(information_schema.tables) + .select("table_name") + .where( + (information_schema.tables.row_format != row_format) + & (information_schema.tables.table_schema == xhiveframework.conf.db_name) + ) + .run() + ) + tables = [x[0] for x in queried_tables] + else: + tables = [x.strip() for x in table.split(",")] + + total = len(tables) + + for current, table in enumerate(tables): + values_to_set = "" + if engine: + values_to_set += f" ENGINE={engine}" + if row_format: + values_to_set += f" ROW_FORMAT={row_format}" + + try: + xhiveframework.db.sql(f"ALTER TABLE `{table}`{values_to_set}") + update_progress_bar("Updating table schema", current - skipped, total) + add_line = True + + except Exception as e: + check_table.append([table, e.args]) + skipped += 1 + + if failfast: + break + + if add_line: + print() + + for errored_table in check_table: + table, err = errored_table + err_msg = f"{table}: ERROR {err[0]}: {err[1]}" + click.secho(err_msg, fg="yellow") + + xhiveframework.destroy() + + +@click.command("run-tests") +@click.option("--app", help="For App") +@click.option("--doctype", help="For DocType") +@click.option("--module-def", help="For all Doctypes in Module Def") +@click.option("--case", help="Select particular TestCase") +@click.option( + "--doctype-list-path", + help="Path to .txt file for list of doctypes. Example xhiveerp/tests/server/agriculture.txt", +) +@click.option("--test", multiple=True, help="Specific test") +@click.option("--module", help="Run tests in a module") +@click.option("--profile", is_flag=True, default=False) +@click.option("--coverage", is_flag=True, default=False) +@click.option("--skip-test-records", is_flag=True, default=False, help="Don't create test records") +@click.option("--skip-before-tests", is_flag=True, default=False, help="Don't run before tests hook") +@click.option("--junit-xml-output", help="Destination file path for junit xml report") +@click.option( + "--failfast", is_flag=True, default=False, help="Stop the test run on the first error or failure" +) +@pass_context +def run_tests( + context, + app=None, + module=None, + doctype=None, + module_def=None, + test=(), + profile=False, + coverage=False, + junit_xml_output=False, + doctype_list_path=None, + skip_test_records=False, + skip_before_tests=False, + failfast=False, + case=None, +): + """Run python unit-tests""" + + with CodeCoverage(coverage, app): + import xhiveframework + import xhiveframework.test_runner + + tests = test + site = get_site(context) + + allow_tests = xhiveframework.get_conf(site).allow_tests + + if not (allow_tests or os.environ.get("CI")): + click.secho("Testing is disabled for the site!", bold=True) + click.secho("You can enable tests by entering following command:") + click.secho(f"bench --site {site} set-config allow_tests true", fg="green") + return + + xhiveframework.init(site=site) + + xhiveframework.flags.skip_before_tests = skip_before_tests + xhiveframework.flags.skip_test_records = skip_test_records + + ret = xhiveframework.test_runner.main( + app, + module, + doctype, + module_def, + context.verbose, + tests=tests, + force=context.force, + profile=profile, + junit_xml_output=junit_xml_output, + doctype_list_path=doctype_list_path, + failfast=failfast, + case=case, + ) + + if len(ret.failures) == 0 and len(ret.errors) == 0: + ret = 0 + + if os.environ.get("CI"): + sys.exit(ret) + + +@click.command("run-parallel-tests") +@click.option("--app", help="For App", default="xhiveframework") +@click.option("--build-number", help="Build number", default=1) +@click.option("--total-builds", help="Total number of builds", default=1) +@click.option("--with-coverage", is_flag=True, help="Build coverage file") +@click.option("--use-orchestrator", is_flag=True, help="Use orchestrator to run parallel tests") +@click.option("--dry-run", is_flag=True, default=False, help="Dont actually run tests") +@pass_context +def run_parallel_tests( + context, + app, + build_number, + total_builds, + with_coverage=False, + use_orchestrator=False, + dry_run=False, +): + from traceback_with_variables import activate_by_import + + with CodeCoverage(with_coverage, app): + site = get_site(context) + if use_orchestrator: + from xhiveframework.parallel_test_runner import ParallelTestWithOrchestrator + + ParallelTestWithOrchestrator(app, site=site) + else: + from xhiveframework.parallel_test_runner import ParallelTestRunner + + ParallelTestRunner( + app, + site=site, + build_number=build_number, + total_builds=total_builds, + dry_run=dry_run, + ) + + +@click.command( + "run-ui-tests", + context_settings=dict( + ignore_unknown_options=True, + ), +) +@click.argument("app") +@click.argument("cypressargs", nargs=-1, type=click.UNPROCESSED) +@click.option("--headless", is_flag=True, help="Run UI Test in headless mode") +@click.option("--parallel", is_flag=True, help="Run UI Test in parallel mode") +@click.option("--with-coverage", is_flag=True, help="Generate coverage report") +@click.option("--browser", default="chrome", help="Browser to run tests in") +@click.option("--ci-build-id") +@pass_context +def run_ui_tests( + context, + app, + headless=False, + parallel=True, + with_coverage=False, + browser="chrome", + ci_build_id=None, + cypressargs=None, +): + "Run UI tests" + site = get_site(context) + xhiveframework.init(site) + app_base_path = xhiveframework.get_app_source_path(app) + site_url = xhiveframework.utils.get_site_url(site) + admin_password = xhiveframework.get_conf(site).admin_password + + # override baseUrl using env variable + site_env = f"CYPRESS_baseUrl={site_url}" + password_env = f"CYPRESS_adminPassword={admin_password}" if admin_password else "" + coverage_env = f"CYPRESS_coverage={str(with_coverage).lower()}" + + os.chdir(app_base_path) + + node_bin = subprocess.getoutput("(cd ../xhiveframework && yarn bin)") + cypress_path = f"{node_bin}/cypress" + drag_drop_plugin_path = f"{node_bin}/../@4tw/cypress-drag-drop" + real_events_plugin_path = f"{node_bin}/../cypress-real-events" + testing_library_path = f"{node_bin}/../@testing-library" + coverage_plugin_path = f"{node_bin}/../@cypress/code-coverage" + + # check if cypress in path...if not, install it. + if not ( + os.path.exists(cypress_path) + and os.path.exists(drag_drop_plugin_path) + and os.path.exists(real_events_plugin_path) + and os.path.exists(testing_library_path) + and os.path.exists(coverage_plugin_path) + ): + # install cypress & dependent plugins + click.secho("Installing Cypress...", fg="yellow") + packages = " ".join( + [ + "cypress@^13", + "@4tw/cypress-drag-drop@^2", + "cypress-real-events", + "@testing-library/cypress@^10", + "@testing-library/dom@8.17.1", + "@cypress/code-coverage@^3", + ] + ) + xhiveframework.commands.popen(f"(cd ../xhiveframework && yarn add {packages} --no-lockfile)") + + # run for headless mode + run_or_open = f"run --browser {browser}" if headless else "open" + formatted_command = f"{site_env} {password_env} {coverage_env} {cypress_path} {run_or_open}" + + if os.environ.get("CYPRESS_RECORD_KEY"): + formatted_command += " --record" + + if parallel: + formatted_command += " --parallel" + + if ci_build_id: + formatted_command += f" --ci-build-id {ci_build_id}" + + if cypressargs: + formatted_command += " " + " ".join(cypressargs) + + click.secho("Running Cypress...", fg="yellow") + xhiveframework.commands.popen(formatted_command, cwd=app_base_path, raise_err=True) + + +@click.command("serve") +@click.option("--port", default=8000) +@click.option("--profile", is_flag=True, default=False) +@click.option( + "--proxy", + is_flag=True, + default=False, + help="The development server may be run behind a proxy, e.g. ngrok / localtunnel", +) +@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, + proxy=False, + no_reload=False, + no_threading=False, + sites_path=".", + site=None, + with_coverage=False, +): + "Start development web server" + import xhiveframework.app + + if not context.sites: + site = None + else: + site = context.sites[0] + with CodeCoverage(with_coverage, "xhiveframework"): + if with_coverage: + # unable to track coverage with threading enabled + no_threading = True + no_reload = True + xhiveframework.app.serve( + port=port, + profile=profile, + proxy=proxy, + no_reload=no_reload, + no_threading=no_threading, + site=site, + sites_path=".", + ) + + +@click.command("request") +@click.option("--args", help="arguments like `?cmd=test&key=value` or `/api/request/method?..`") +@click.option("--path", help="path to request JSON") +@pass_context +def request(context, args=None, path=None): + "Run a request as an admin" + import xhiveframework.api + import xhiveframework.handler + + for site in context.sites: + try: + xhiveframework.init(site=site) + xhiveframework.connect() + if args: + if "?" in args: + xhiveframework.local.form_dict = xhiveframework._dict( + [a.split("=") for a in args.split("?")[-1].split("&")] + ) + else: + xhiveframework.local.form_dict = xhiveframework._dict() + + if args.startswith("/api/method"): + xhiveframework.local.form_dict.cmd = args.split("?", 1)[0].split("/")[-1] + elif path: + with open(os.path.join("..", path)) as f: + args = json.loads(f.read()) + + xhiveframework.local.form_dict = xhiveframework._dict(args) + + xhiveframework.handler.execute_cmd(xhiveframework.form_dict.cmd) + + print(xhiveframework.response) + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +@click.command("make-app") +@click.argument("destination") +@click.argument("app_name") +@click.option("--no-git", is_flag=True, default=False, help="Do not initialize git repository for the app") +def make_app(destination, app_name, no_git=False): + "Creates a boilerplate app" + from xhiveframework.utils.boilerplate import make_boilerplate + + make_boilerplate(destination, app_name, no_git=no_git) + + +@click.command("create-patch") +def create_patch(): + "Creates a new patch interactively" + from xhiveframework.utils.boilerplate import PatchCreator + + pc = PatchCreator() + pc.fetch_user_inputs() + pc.create_patch_file() + + +@click.command("set-config") +@click.argument("key") +@click.argument("value") +@click.option("-g", "--global", "global_", is_flag=True, default=False, help="Set value in bench config") +@click.option("-p", "--parse", is_flag=True, default=False, help="Evaluate as Python Object") +@pass_context +def set_config(context, key, value, global_=False, parse=False): + "Insert/Update a value in site_config.json" + from xhiveframework.installer import update_site_config + + if parse: + import ast + + value = ast.literal_eval(value) + + if global_: + sites_path = os.getcwd() + common_site_config_path = os.path.join(sites_path, "common_site_config.json") + update_site_config(key, value, validate=False, site_config_path=common_site_config_path) + else: + if not context.sites: + raise SiteNotSpecifiedError + for site in context.sites: + xhiveframework.init(site=site) + update_site_config(key, value, validate=False) + xhiveframework.destroy() + + +@click.command("version") +@click.option( + "-f", + "--format", + "output", + type=click.Choice(["plain", "table", "json", "legacy"]), + help="Output format", + default="legacy", +) +def get_version(output): + """Show the versions of all the installed apps.""" + from git import Repo + from git.exc import InvalidGitRepositoryError + + from xhiveframework.utils.change_log import get_app_branch + from xhiveframework.utils.commands import render_table + + xhiveframework.init("") + data = [] + + for app in sorted(xhiveframework.get_all_apps()): + module = xhiveframework.get_module(app) + app_hooks = xhiveframework.get_module(app + ".hooks") + + app_info = xhiveframework._dict() + + try: + app_info.commit = Repo(xhiveframework.get_app_source_path(app)).head.object.hexsha[:7] + except InvalidGitRepositoryError: + app_info.commit = "" + + app_info.app = app + app_info.branch = get_app_branch(app) + app_info.version = getattr(app_hooks, f"{app_info.branch}_version", None) or module.__version__ + + data.append(app_info) + + { + "legacy": lambda: [click.echo(f"{app_info.app} {app_info.version}") for app_info in data], + "plain": lambda: [ + click.echo(f"{app_info.app} {app_info.version} {app_info.branch} ({app_info.commit})") + for app_info in data + ], + "table": lambda: render_table( + [["App", "Version", "Branch", "Commit"]] + + [[app_info.app, app_info.version, app_info.branch, app_info.commit] for app_info in data] + ), + "json": lambda: click.echo(json.dumps(data, indent=4)), + }[output]() + + +@click.command("rebuild-global-search") +@click.option("--static-pages", is_flag=True, default=False, help="Rebuild global search for static pages") +@pass_context +def rebuild_global_search(context, static_pages=False): + """Setup help table in the current site (called after migrate)""" + from xhiveframework.utils.global_search import ( + add_route_to_global_search, + get_doctypes_with_global_search, + get_routes_to_index, + rebuild_for_doctype, + sync_global_search, + ) + + for site in context.sites: + try: + xhiveframework.init(site) + xhiveframework.connect() + + if static_pages: + routes = get_routes_to_index() + for i, route in enumerate(routes): + add_route_to_global_search(route) + xhiveframework.local.request = None + update_progress_bar("Rebuilding Global Search", i, len(routes)) + sync_global_search() + else: + doctypes = get_doctypes_with_global_search() + for i, doctype in enumerate(doctypes): + rebuild_for_doctype(doctype) + update_progress_bar("Rebuilding Global Search", i, len(doctypes)) + + finally: + xhiveframework.destroy() + if not context.sites: + raise SiteNotSpecifiedError + + +commands = [ + build, + clear_cache, + clear_website_cache, + database, + transform_database, + jupyter, + console, + destroy_all_sessions, + execute, + export_csv, + export_doc, + export_fixtures, + export_json, + get_version, + data_import, + import_doc, + make_app, + create_patch, + mariadb, + postgres, + request, + reset_perms, + run_tests, + run_ui_tests, + serve, + set_config, + show_config, + watch, + bulk_rename, + add_to_email_queue, + rebuild_global_search, + run_parallel_tests, +] diff --git a/xhiveframework/config/__init__.py b/xhiveframework/config/__init__.py new file mode 100644 index 0000000..ac23b84 --- /dev/null +++ b/xhiveframework/config/__init__.py @@ -0,0 +1,64 @@ +import xhiveframework +from xhiveframework import _ + + +def get_modules_from_all_apps_for_user(user: str | None = None) -> list[dict]: + user = user or xhiveframework.session.user + all_modules = get_modules_from_all_apps() + global_blocked_modules = xhiveframework.get_doc("User", "Administrator").get_blocked_modules() + user_blocked_modules = xhiveframework.get_doc("User", user).get_blocked_modules() + blocked_modules = global_blocked_modules + user_blocked_modules + allowed_modules_list = [m for m in all_modules if m.get("module_name") not in blocked_modules] + + empty_tables_by_module = get_all_empty_tables_by_module() + + for module in allowed_modules_list: + module_name = module.get("module_name") + + # Apply onboarding status + if module_name in empty_tables_by_module: + module["onboard_present"] = 1 + + return allowed_modules_list + + +def get_modules_from_all_apps(): + modules_list = [] + for app in xhiveframework.get_installed_apps(): + modules_list += get_modules_from_app(app) + return modules_list + + +def get_modules_from_app(app): + return xhiveframework.get_all("Module Def", filters={"app_name": app}, fields=["module_name", "app_name as app"]) + + +def get_all_empty_tables_by_module(): + table_rows = xhiveframework.qb.Field("table_rows") + table_name = xhiveframework.qb.Field("table_name") + information_schema = xhiveframework.qb.Schema("information_schema") + + empty_tables = ( + xhiveframework.qb.from_(information_schema.tables).select(table_name).where(table_rows == 0) + ).run() + + empty_tables = {r[0] for r in empty_tables} + + results = xhiveframework.get_all("DocType", fields=["name", "module"]) + empty_tables_by_module = {} + + for doctype, module in results: + if f"tab{doctype}" in empty_tables: + if module in empty_tables_by_module: + empty_tables_by_module[module].append(doctype) + else: + empty_tables_by_module[module] = [doctype] + return empty_tables_by_module + + +def is_domain(module): + return module.get("category") == "Domains" + + +def is_module(module): + return module.get("type") == "module" diff --git a/xhiveframework/contacts/__init__.py b/xhiveframework/contacts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/address_and_contact.py b/xhiveframework/contacts/address_and_contact.py new file mode 100644 index 0000000..53298c5 --- /dev/null +++ b/xhiveframework/contacts/address_and_contact.py @@ -0,0 +1,149 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ + + +def load_address_and_contact(doc, key=None) -> None: + """Loads address list and contact list in `__onload`""" + from xhiveframework.contacts.doctype.address.address import get_address_display_list + from xhiveframework.contacts.doctype.contact.contact import get_contact_display_list + + doc.set_onload("addr_list", get_address_display_list(doc.doctype, doc.name)) + doc.set_onload("contact_list", get_contact_display_list(doc.doctype, doc.name)) + + +def has_permission(doc, ptype, user): + links = get_permitted_and_not_permitted_links(doc.doctype) + if not links.get("not_permitted_links"): + # optimization: don't determine permissions based on link fields + return True + + # True if any one is True or all are empty + names = [] + for df in links.get("permitted_links") + links.get("not_permitted_links"): + doctype = df.options + name = doc.get(df.fieldname) + names.append(name) + + if name and xhiveframework.has_permission(doctype, ptype, doc=name): + return True + + if not any(names): + return True + return False + + +def get_permission_query_conditions_for_contact(user): + return get_permission_query_conditions("Contact") + + +def get_permission_query_conditions_for_address(user): + return get_permission_query_conditions("Address") + + +def get_permission_query_conditions(doctype): + links = get_permitted_and_not_permitted_links(doctype) + + if not links.get("not_permitted_links"): + # when everything is permitted, don't add additional condition + return "" + + elif not links.get("permitted_links"): + # when everything is not permitted + conditions = [ + f"ifnull(`tab{doctype}`.`{df.fieldname}`, '')=''" for df in links.get("not_permitted_links") + ] + + return "( " + " and ".join(conditions) + " )" + + else: + conditions = [ + f"ifnull(`tab{doctype}`.`{df.fieldname}`, '')!=''" for df in links.get("permitted_links") + ] + + return "( " + " or ".join(conditions) + " )" + + +def get_permitted_and_not_permitted_links(doctype): + permitted_links = [] + not_permitted_links = [] + + meta = xhiveframework.get_meta(doctype) + allowed_doctypes = xhiveframework.permissions.get_doctypes_with_read() + + for df in meta.get_link_fields(): + if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"): + continue + + if df.options in allowed_doctypes: + permitted_links.append(df) + else: + not_permitted_links.append(df) + + return {"permitted_links": permitted_links, "not_permitted_links": not_permitted_links} + + +def delete_contact_and_address(doctype: str, docname: str) -> None: + for parenttype in ("Contact", "Address"): + for name in xhiveframework.get_all( + "Dynamic Link", + filters={ + "parenttype": parenttype, + "link_doctype": doctype, + "link_name": docname, + }, + pluck="parent", + ): + doc = xhiveframework.get_doc(parenttype, name) + if len(doc.links) == 1: + doc.delete() + else: + for link in doc.links: + if link.link_doctype == doctype and link.link_name == docname: + doc.remove(link) + doc.save() + break + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def filter_dynamic_link_doctypes( + doctype, txt: str, searchfield, start, page_len, filters: dict +) -> list[list[str]]: + from xhiveframework.permissions import get_doctypes_with_read + + txt = txt or "" + filters = filters or {} + + _doctypes_from_df = xhiveframework.get_all( + "DocField", + filters=filters, + pluck="parent", + distinct=True, + order_by=None, + ) + doctypes_from_df = {d for d in _doctypes_from_df if txt.lower() in _(d).lower()} + + filters.update({"dt": ("not in", doctypes_from_df)}) + _doctypes_from_cdf = xhiveframework.get_all( + "Custom Field", filters=filters, pluck="dt", distinct=True, order_by=None + ) + doctypes_from_cdf = {d for d in _doctypes_from_cdf if txt.lower() in _(d).lower()} + + all_doctypes = doctypes_from_df.union(doctypes_from_cdf) + allowed_doctypes = set(get_doctypes_with_read()) + + valid_doctypes = sorted(all_doctypes.intersection(allowed_doctypes)) + + return [[doctype] for doctype in valid_doctypes] + + +def set_link_title(doc): + if not doc.links: + return + for link in doc.links: + if not link.link_title: + linked_doc = xhiveframework.get_doc(link.link_doctype, link.link_name) + link.link_title = linked_doc.get_title() or link.link_name diff --git a/xhiveframework/contacts/doctype/__init__.py b/xhiveframework/contacts/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/address/__init__.py b/xhiveframework/contacts/doctype/address/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/address/address.js b/xhiveframework/contacts/doctype/address/address.js new file mode 100644 index 0000000..3c6eded --- /dev/null +++ b/xhiveframework/contacts/doctype/address/address.js @@ -0,0 +1,75 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Address", { + refresh: function (frm) { + if (frm.doc.__islocal) { + const last_doc = xhiveframework.contacts.get_last_doc(frm); + if ( + xhiveframework.dynamic_link && + xhiveframework.dynamic_link.doc && + xhiveframework.dynamic_link.doc.name == last_doc.docname + ) { + frm.set_value("links", ""); + frm.add_child("links", { + link_doctype: xhiveframework.dynamic_link.doctype, + link_name: xhiveframework.dynamic_link.doc[xhiveframework.dynamic_link.fieldname], + }); + } + } + frm.set_query("link_doctype", "links", function () { + return { + query: "xhiveframework.contacts.address_and_contact.filter_dynamic_link_doctypes", + filters: { + fieldtype: "HTML", + fieldname: "address_html", + }, + }; + }); + frm.refresh_field("links"); + + if (frm.doc.links) { + for (let i in frm.doc.links) { + let link = frm.doc.links[i]; + frm.add_custom_button( + __("{0}: {1}", [__(link.link_doctype), __(link.link_name)]), + function () { + xhiveframework.set_route("Form", link.link_doctype, link.link_name); + }, + __("Links") + ); + } + } + }, + validate: function (frm) { + // clear linked customer / supplier / sales partner on saving... + if (frm.doc.links) { + frm.doc.links.forEach(function (d) { + xhiveframework.model.remove_from_locals(d.link_doctype, d.link_name); + }); + } + }, + after_save: function (frm) { + xhiveframework.run_serially([ + () => xhiveframework.timeout(1), + () => { + const last_doc = xhiveframework.contacts.get_last_doc(frm); + if ( + xhiveframework.dynamic_link && + xhiveframework.dynamic_link.doc && + xhiveframework.dynamic_link.doc.name == last_doc.docname + ) { + for (let i in frm.doc.links) { + let link = frm.doc.links[i]; + if ( + last_doc.doctype == link.link_doctype && + last_doc.docname == link.link_name + ) { + xhiveframework.set_route("Form", last_doc.doctype, last_doc.docname); + } + } + } + }, + ]); + }, +}); diff --git a/xhiveframework/contacts/doctype/address/address.json b/xhiveframework/contacts/doctype/address/address.json new file mode 100644 index 0000000..54d6b03 --- /dev/null +++ b/xhiveframework/contacts/doctype/address/address.json @@ -0,0 +1,226 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "creation": "2013-01-10 16:34:32", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "address_details", + "address_title", + "address_type", + "address_line1", + "address_line2", + "city", + "county", + "state", + "country", + "pincode", + "column_break0", + "email_id", + "phone", + "fax", + "is_primary_address", + "is_shipping_address", + "disabled", + "linked_with", + "links" + ], + "fields": [ + { + "fieldname": "address_details", + "fieldtype": "Section Break", + "options": "fa fa-map-marker" + }, + { + "fieldname": "address_title", + "fieldtype": "Data", + "label": "Address Title" + }, + { + "fieldname": "address_type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Address Type", + "options": "Billing\nShipping\nOffice\nPersonal\nPlant\nPostal\nShop\nSubsidiary\nWarehouse\nCurrent\nPermanent\nOther", + "reqd": 1 + }, + { + "fieldname": "address_line1", + "fieldtype": "Data", + "label": "Address Line 1", + "length": 240, + "reqd": 1 + }, + { + "fieldname": "address_line2", + "fieldtype": "Data", + "label": "Address Line 2", + "length": 240 + }, + { + "fieldname": "city", + "fieldtype": "Data", + "in_global_search": 1, + "in_list_view": 1, + "label": "City/Town", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "county", + "fieldtype": "Data", + "label": "County" + }, + { + "fieldname": "state", + "fieldtype": "Data", + "label": "State/Province" + }, + { + "fieldname": "country", + "fieldtype": "Link", + "in_global_search": 1, + "in_standard_filter": 1, + "label": "Country", + "options": "Country", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "pincode", + "fieldtype": "Data", + "label": "Postal Code", + "search_index": 1 + }, + { + "fieldname": "column_break0", + "fieldtype": "Column Break", + "width": "50%" + }, + { + "fieldname": "email_id", + "fieldtype": "Data", + "label": "Email Address", + "options": "Email" + }, + { + "fieldname": "phone", + "fieldtype": "Data", + "label": "Phone" + }, + { + "fieldname": "fax", + "fieldtype": "Data", + "label": "Fax" + }, + { + "default": "0", + "fieldname": "is_primary_address", + "fieldtype": "Check", + "label": "Preferred Billing Address" + }, + { + "default": "0", + "fieldname": "is_shipping_address", + "fieldtype": "Check", + "label": "Preferred Shipping Address" + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "fieldname": "linked_with", + "fieldtype": "Section Break", + "label": "Reference", + "options": "fa fa-pushpin" + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "Dynamic Link" + } + ], + "icon": "fa fa-map-marker", + "idx": 5, + "links": [], + "modified": "2023-11-20 17:28:41.698356", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Address", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Sales User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Purchase User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Maintenance User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Accounts User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "export": 1, + "if_owner": 1, + "print": 1, + "read": 1, + "role": "All", + "write": 1 + } + ], + "search_fields": "country, state", + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/address/address.py b/xhiveframework/contacts/doctype/address/address.py new file mode 100644 index 0000000..65f46da --- /dev/null +++ b/xhiveframework/contacts/doctype/address/address.py @@ -0,0 +1,356 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from jinja2 import TemplateSyntaxError + +import xhiveframework +from xhiveframework import _, throw +from xhiveframework.contacts.address_and_contact import set_link_title +from xhiveframework.core.doctype.dynamic_link.dynamic_link import deduplicate_dynamic_links +from xhiveframework.model.document import Document +from xhiveframework.model.naming import make_autoname +from xhiveframework.utils import cstr + + +class Address(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.dynamic_link.dynamic_link import DynamicLink + from xhiveframework.types import DF + + address_line1: DF.Data + address_line2: DF.Data | None + address_title: DF.Data | None + address_type: DF.Literal[ + "Billing", + "Shipping", + "Office", + "Personal", + "Plant", + "Postal", + "Shop", + "Subsidiary", + "Warehouse", + "Current", + "Permanent", + "Other", + ] + city: DF.Data + country: DF.Link + county: DF.Data | None + disabled: DF.Check + email_id: DF.Data | None + fax: DF.Data | None + is_primary_address: DF.Check + is_shipping_address: DF.Check + links: DF.Table[DynamicLink] + phone: DF.Data | None + pincode: DF.Data | None + state: DF.Data | None + + # end: auto-generated types + def __setup__(self): + self.flags.linked = False + + def autoname(self): + if not self.address_title: + if self.links: + self.address_title = self.links[0].link_name + + if self.address_title: + self.name = cstr(self.address_title).strip() + "-" + cstr(_(self.address_type)).strip() + if xhiveframework.db.exists("Address", self.name): + self.name = make_autoname( + cstr(self.address_title).strip() + "-" + cstr(self.address_type).strip() + "-.#", + ignore_validate=True, + ) + else: + throw(_("Address Title is mandatory.")) + + def validate(self): + self.link_address() + self.validate_preferred_address() + set_link_title(self) + deduplicate_dynamic_links(self) + + def link_address(self): + """Link address based on owner""" + if not self.links: + contact_name = xhiveframework.db.get_value("Contact", {"email_id": self.owner}) + if contact_name: + contact = xhiveframework.get_cached_doc("Contact", contact_name) + for link in contact.links: + self.append("links", dict(link_doctype=link.link_doctype, link_name=link.link_name)) + return True + + return False + + def validate_preferred_address(self): + preferred_fields = ["is_primary_address", "is_shipping_address"] + + for field in preferred_fields: + if self.get(field): + for link in self.links: + address = get_preferred_address(link.link_doctype, link.link_name, field) + + if address: + update_preferred_address(address, field) + + def get_display(self): + return get_address_display(self.as_dict()) + + def has_link(self, doctype, name): + for link in self.links: + if link.link_doctype == doctype and link.link_name == name: + return True + + def has_common_link(self, doc): + reference_links = [(link.link_doctype, link.link_name) for link in doc.links] + for link in self.links: + if (link.link_doctype, link.link_name) in reference_links: + return True + + return False + + +def get_preferred_address(doctype, name, preferred_key="is_primary_address"): + if preferred_key in ["is_shipping_address", "is_primary_address"]: + address = xhiveframework.db.sql( + """ SELECT + addr.name + FROM + `tabAddress` addr, `tabDynamic Link` dl + WHERE + dl.parent = addr.name and dl.link_doctype = {} and + dl.link_name = {} and ifnull(addr.disabled, 0) = 0 and + {} = {} + """.format("%s", "%s", preferred_key, "%s"), + (doctype, name, 1), + as_dict=1, + ) + + if address: + return address[0].name + + return + + +@xhiveframework.whitelist() +def get_default_address(doctype: str, name: str | None, sort_key: str = "is_primary_address") -> str | None: + """Returns default Address name for the given doctype, name""" + if sort_key not in ["is_shipping_address", "is_primary_address"]: + return None + + addresses = xhiveframework.get_all( + "Address", + filters=[ + ["Dynamic Link", "link_doctype", "=", doctype], + ["Dynamic Link", "link_name", "=", name], + ["disabled", "=", 0], + ], + pluck="name", + order_by=f"{sort_key} DESC", + limit=1, + ) + + return addresses[0] if addresses else None + + +@xhiveframework.whitelist() +def get_address_display(address_dict: dict | str | None) -> str | None: + return render_address(address_dict) + + +def render_address(address: dict | str | None, check_permissions=True) -> str | None: + if not address: + return + + if not isinstance(address, dict): + address = xhiveframework.get_cached_doc("Address", address) + if check_permissions: + address.check_permission() + address = address.as_dict() + + name, template = get_address_templates(address) + + try: + return xhiveframework.render_template(template, address) + except TemplateSyntaxError: + xhiveframework.throw(_("There is an error in your Address Template {0}").format(name)) + + +def get_territory_from_address(address): + """Tries to match city, state and country of address to existing territory""" + if not address: + return + + if isinstance(address, str): + address = xhiveframework.get_cached_doc("Address", address) + + territory = None + for fieldname in ("city", "state", "country"): + if address.get(fieldname): + territory = xhiveframework.db.get_value("Territory", address.get(fieldname)) + if territory: + break + + return territory + + +def get_list_context(context=None): + return { + "title": _("Addresses"), + "get_list": get_address_list, + "row_template": "templates/includes/address_row.html", + "no_breadcrumbs": True, + } + + +def get_address_list(doctype, txt, filters, limit_start, limit_page_length=20, order_by=None): + from xhiveframework.www.list import get_list + + user = xhiveframework.session.user + ignore_permissions = True + + if not filters: + filters = [] + filters.append(("Address", "owner", "=", user)) + + return get_list( + doctype, txt, filters, limit_start, limit_page_length, ignore_permissions=ignore_permissions + ) + + +def has_website_permission(doc, ptype, user, verbose=False): + """Returns true if there is a related lead or contact related to this document""" + contact_name = xhiveframework.db.get_value("Contact", {"email_id": xhiveframework.session.user}) + + if contact_name: + contact = xhiveframework.get_doc("Contact", contact_name) + return contact.has_common_link(doc) + + return False + + +def get_address_templates(address): + result = xhiveframework.db.get_value( + "Address Template", {"country": address.get("country")}, ["name", "template"] + ) + + if not result: + result = xhiveframework.db.get_value("Address Template", {"is_default": 1}, ["name", "template"]) + + if not result: + xhiveframework.throw( + _( + "No default Address Template found. Please create a new one from Setup > Printing and Branding > Address Template." + ) + ) + else: + return result + + +def get_company_address(company): + ret = xhiveframework._dict() + + if company: + ret.company_address = get_default_address("Company", company) + ret.company_address_display = render_address(ret.company_address, check_permissions=False) + + return ret + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def address_query(doctype, txt, searchfield, start, page_len, filters): + from xhiveframework.desk.reportview import get_match_cond + + doctype = "Address" + link_doctype = filters.pop("link_doctype") + link_name = filters.pop("link_name") + + condition = "" + meta = xhiveframework.get_meta(doctype) + for fieldname, value in filters.items(): + if meta.get_field(fieldname) or fieldname in xhiveframework.db.DEFAULT_COLUMNS: + condition += f" and {fieldname}={xhiveframework.db.escape(value)}" + + searchfields = meta.get_search_fields() + + if searchfield and (meta.get_field(searchfield) or searchfield in xhiveframework.db.DEFAULT_COLUMNS): + searchfields.append(searchfield) + + search_condition = "" + for field in searchfields: + if search_condition == "": + search_condition += f"`tabAddress`.`{field}` like %(txt)s" + else: + search_condition += f" or `tabAddress`.`{field}` like %(txt)s" + + return xhiveframework.db.sql( + """select + `tabAddress`.name, `tabAddress`.city, `tabAddress`.country + from + `tabAddress` + join `tabDynamic Link` + on (`tabDynamic Link`.parent = `tabAddress`.name and `tabDynamic Link`.parenttype = 'Address') + where + `tabDynamic Link`.link_doctype = %(link_doctype)s and + `tabDynamic Link`.link_name = %(link_name)s and + ifnull(`tabAddress`.disabled, 0) = 0 and + ({search_condition}) + {mcond} {condition} + order by + case + when locate(%(_txt)s, `tabAddress`.name) != 0 + then locate(%(_txt)s, `tabAddress`.name) + else 99999 + end, + `tabAddress`.idx desc, `tabAddress`.name + limit %(page_len)s offset %(start)s""".format( + mcond=get_match_cond(doctype), + search_condition=search_condition, + condition=condition or "", + ), + { + "txt": "%" + txt + "%", + "_txt": txt.replace("%", ""), + "start": start, + "page_len": page_len, + "link_name": link_name, + "link_doctype": link_doctype, + }, + ) + + +def get_condensed_address(doc): + fields = ["address_title", "address_line1", "address_line2", "city", "county", "state", "country"] + return ", ".join(doc.get(d) for d in fields if doc.get(d)) + + +def update_preferred_address(address, field): + xhiveframework.db.set_value("Address", address, field, 0) + + +def get_address_display_list(doctype: str, name: str) -> list[dict]: + if not xhiveframework.has_permission("Address", "read"): + return [] + + address_list = xhiveframework.get_list( + "Address", + filters=[ + ["Dynamic Link", "link_doctype", "=", doctype], + ["Dynamic Link", "link_name", "=", name], + ["Dynamic Link", "parenttype", "=", "Address"], + ], + fields=["*"], + order_by="is_primary_address DESC, creation ASC", + ) + for a in address_list: + a["display"] = get_address_display(a) + + return address_list diff --git a/xhiveframework/contacts/doctype/address/test_address.py b/xhiveframework/contacts/doctype/address/test_address.py new file mode 100644 index 0000000..5a5eea3 --- /dev/null +++ b/xhiveframework/contacts/doctype/address/test_address.py @@ -0,0 +1,58 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from functools import partial + +import xhiveframework +from xhiveframework.contacts.doctype.address.address import address_query, get_address_display +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestAddress(XhiveFrameworkTestCase): + def test_template_works(self): + if not xhiveframework.db.exists("Address Template", "India"): + xhiveframework.get_doc({"doctype": "Address Template", "country": "India", "is_default": 1}).insert() + + if not xhiveframework.db.exists("Address", "_Test Address-Office"): + xhiveframework.get_doc( + { + "address_line1": "_Test Address Line 1", + "address_title": "_Test Address", + "address_type": "Office", + "city": "_Test City", + "state": "Test State", + "country": "India", + "doctype": "Address", + "is_primary_address": 1, + "phone": "+91 0000000000", + } + ).insert() + + address = xhiveframework.get_list("Address")[0].name + display = get_address_display(xhiveframework.get_doc("Address", address).as_dict()) + self.assertTrue(display) + + def test_address_query(self): + def query(doctype="Address", txt="", searchfield="name", start=0, page_len=20, filters=None): + if filters is None: + filters = {"link_doctype": "User", "link_name": "Administrator"} + return address_query(doctype, txt, searchfield, start, page_len, filters) + + xhiveframework.get_doc( + { + "address_type": "Billing", + "address_line1": "1", + "city": "Mumbai", + "state": "Maharashtra", + "country": "India", + "doctype": "Address", + "links": [ + { + "link_doctype": "User", + "link_name": "Administrator", + } + ], + } + ).insert() + + self.assertGreaterEqual(len(query(txt="Admin")), 1) + self.assertEqual(len(query(txt="what_zyx")), 0) diff --git a/xhiveframework/contacts/doctype/address_template/__init__.py b/xhiveframework/contacts/doctype/address_template/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/address_template/address_template.jinja b/xhiveframework/contacts/doctype/address_template/address_template.jinja new file mode 100644 index 0000000..65ea58e --- /dev/null +++ b/xhiveframework/contacts/doctype/address_template/address_template.jinja @@ -0,0 +1,10 @@ +{{ address_line1 }}
+{% if address_line2 %}{{ address_line2 }}
{% endif -%} +{{ city }}
+{% if state %}{{ state }}
{% endif -%} +{% if pincode %}{{ pincode }}
{% endif -%} +{{ country }}
+
+{% if phone %}{{ _("Phone") }}: {{ phone }}
{% endif -%} +{% if fax %}{{ _("Fax") }}: {{ fax }}
{% endif -%} +{% if email_id %}{{ _("Email") }}: {{ email_id }}
{% endif -%} diff --git a/xhiveframework/contacts/doctype/address_template/address_template.js b/xhiveframework/contacts/doctype/address_template/address_template.js new file mode 100644 index 0000000..ee77359 --- /dev/null +++ b/xhiveframework/contacts/doctype/address_template/address_template.js @@ -0,0 +1,16 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Address Template", { + refresh: function (frm) { + if (frm.is_new() && !frm.doc.template) { + // set default template via js so that it is translated + xhiveframework.call({ + method: "xhiveframework.contacts.doctype.address_template.address_template.get_default_address_template", + callback: function (r) { + frm.set_value("template", r.message); + }, + }); + } + }, +}); diff --git a/xhiveframework/contacts/doctype/address_template/address_template.json b/xhiveframework/contacts/doctype/address_template/address_template.json new file mode 100644 index 0000000..58b8210 --- /dev/null +++ b/xhiveframework/contacts/doctype/address_template/address_template.json @@ -0,0 +1,64 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:country", + "creation": "2014-06-05 02:22:36.029850", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "country", + "is_default", + "template" + ], + "fields": [ + { + "fieldname": "country", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Country", + "options": "Country", + "reqd": 1, + "search_index": 1, + "unique": 1 + }, + { + "default": "0", + "description": "This format is used if country specific format is not found", + "fieldname": "is_default", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Default" + }, + { + "description": "

Default Template

\n

Uses Jinja Templating and all the fields of Address (including Custom Fields if any) will be available

\n
{{ address_line1 }}<br>\n{% if address_line2 %}{{ address_line2 }}<br>{% endif -%}\n{{ city }}<br>\n{% if state %}{{ state }}<br>{% endif -%}\n{% if pincode %} PIN:  {{ pincode }}<br>{% endif -%}\n{{ country }}<br>\n{% if phone %}Phone: {{ phone }}<br>{% endif -%}\n{% if fax %}Fax: {{ fax }}<br>{% endif -%}\n{% if email_id %}Email: {{ email_id }}<br>{% endif -%}\n
", + "fieldname": "template", + "fieldtype": "Code", + "label": "Template" + } + ], + "icon": "fa fa-map-marker", + "links": [], + "modified": "2022-08-03 12:20:49.095228", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Address Template", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "export": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} diff --git a/xhiveframework/contacts/doctype/address_template/address_template.py b/xhiveframework/contacts/doctype/address_template/address_template.py new file mode 100644 index 0000000..624947f --- /dev/null +++ b/xhiveframework/contacts/doctype/address_template/address_template.py @@ -0,0 +1,52 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils.jinja import validate_template + + +class AddressTemplate(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + country: DF.Link + is_default: DF.Check + template: DF.Code | None + + # end: auto-generated types + def validate(self): + validate_template(self.template) + + if not self.template: + self.template = get_default_address_template() + + if not self.is_default and not self._get_previous_default(): + self.is_default = 1 + if xhiveframework.db.get_single_value("System Settings", "setup_complete"): + xhiveframework.msgprint(_("Setting this Address Template as default as there is no other default")) + + def on_update(self): + if self.is_default and (previous_default := self._get_previous_default()): + xhiveframework.db.set_value("Address Template", previous_default, "is_default", 0) + + def on_trash(self): + if self.is_default: + xhiveframework.throw(_("Default Address Template cannot be deleted")) + + def _get_previous_default(self) -> str | None: + return xhiveframework.db.get_value("Address Template", {"is_default": 1, "name": ("!=", self.name)}) + + +@xhiveframework.whitelist() +def get_default_address_template() -> str: + """Return the default address template.""" + from pathlib import Path + + return (Path(__file__).parent / "address_template.jinja").read_text() diff --git a/xhiveframework/contacts/doctype/address_template/test_address_template.py b/xhiveframework/contacts/doctype/address_template/test_address_template.py new file mode 100644 index 0000000..b46e90c --- /dev/null +++ b/xhiveframework/contacts/doctype/address_template/test_address_template.py @@ -0,0 +1,37 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.contacts.doctype.address_template.address_template import get_default_address_template +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils.jinja import validate_template + + +class TestAddressTemplate(XhiveFrameworkTestCase): + def setUp(self) -> None: + xhiveframework.db.delete("Address Template", {"country": "India"}) + xhiveframework.db.delete("Address Template", {"country": "Brazil"}) + + def test_default_address_template(self): + validate_template(get_default_address_template()) + + def test_default_is_unset(self): + xhiveframework.get_doc({"doctype": "Address Template", "country": "India", "is_default": 1}).insert() + + self.assertEqual(xhiveframework.db.get_value("Address Template", "India", "is_default"), 1) + + xhiveframework.get_doc({"doctype": "Address Template", "country": "Brazil", "is_default": 1}).insert() + + self.assertEqual(xhiveframework.db.get_value("Address Template", "India", "is_default"), 0) + self.assertEqual(xhiveframework.db.get_value("Address Template", "Brazil", "is_default"), 1) + + def test_delete_address_template(self): + india = xhiveframework.get_doc({"doctype": "Address Template", "country": "India", "is_default": 0}).insert() + + brazil = xhiveframework.get_doc( + {"doctype": "Address Template", "country": "Brazil", "is_default": 1} + ).insert() + + india.reload() # might have been modified by the second template + india.delete() # should not raise an error + + self.assertRaises(xhiveframework.ValidationError, brazil.delete) diff --git a/xhiveframework/contacts/doctype/contact/__init__.py b/xhiveframework/contacts/doctype/contact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/contact/contact.js b/xhiveframework/contacts/doctype/contact/contact.js new file mode 100644 index 0000000..09becf0 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/contact.js @@ -0,0 +1,156 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Contact", { + onload(frm) { + frm.email_field = "email_id"; + }, + refresh: function (frm) { + if (frm.doc.__islocal) { + const last_doc = xhiveframework.contacts.get_last_doc(frm); + if ( + xhiveframework.dynamic_link && + xhiveframework.dynamic_link.doc && + xhiveframework.dynamic_link.doc.name == last_doc.docname + ) { + frm.set_value("links", ""); + frm.add_child("links", { + link_doctype: xhiveframework.dynamic_link.doctype, + link_name: xhiveframework.dynamic_link.doc[xhiveframework.dynamic_link.fieldname], + }); + } + } + + if ( + !frm.doc.user && + !frm.is_new() && + frm.perm[0].write && + xhiveframework.boot.user.can_create.includes("User") + ) { + frm.add_custom_button(__("Invite as User"), function () { + return xhiveframework.call({ + method: "xhiveframework.contacts.doctype.contact.contact.invite_user", + args: { + contact: frm.doc.name, + }, + callback: function (r) { + frm.set_value("user", r.message); + }, + }); + }); + } + frm.set_query("link_doctype", "links", function () { + return { + query: "xhiveframework.contacts.address_and_contact.filter_dynamic_link_doctypes", + filters: { + fieldtype: "HTML", + fieldname: "contact_html", + }, + }; + }); + frm.refresh_field("links"); + + let numbers = frm.doc.phone_nos; + if (numbers && numbers.length && xhiveframework.phone_call.handler) { + frm.add_custom_button(__("Call"), () => { + numbers = frm.doc.phone_nos + .sort((prev, next) => next.is_primary_mobile_no - prev.is_primary_mobile_no) + .map((d) => d.phone); + xhiveframework.phone_call.handler(numbers); + }); + } + + if (frm.doc.links) { + xhiveframework.call({ + method: "xhiveframework.contacts.doctype.contact.contact.address_query", + args: { links: frm.doc.links }, + callback: function (r) { + if (r && r.message) { + frm.set_query("address", function () { + return { + filters: { + name: ["in", r.message], + }, + }; + }); + } + }, + }); + + for (let i in frm.doc.links) { + let link = frm.doc.links[i]; + frm.add_custom_button( + __("{0}: {1}", [__(link.link_doctype), __(link.link_name)]), + function () { + xhiveframework.set_route("Form", link.link_doctype, link.link_name); + }, + __("Links") + ); + } + } + }, + validate: function (frm) { + // clear linked customer / supplier / sales partner on saving... + if (frm.doc.links) { + frm.doc.links.forEach(function (d) { + xhiveframework.model.remove_from_locals(d.link_doctype, d.link_name); + }); + } + }, + after_save: function (frm) { + xhiveframework.run_serially([ + () => xhiveframework.timeout(1), + () => { + const last_doc = xhiveframework.contacts.get_last_doc(frm); + if ( + xhiveframework.dynamic_link && + xhiveframework.dynamic_link.doc && + xhiveframework.dynamic_link.doc.name == last_doc.docname + ) { + for (let i in frm.doc.links) { + let link = frm.doc.links[i]; + if ( + last_doc.doctype == link.link_doctype && + last_doc.docname == link.link_name + ) { + xhiveframework.set_route("Form", last_doc.doctype, last_doc.docname); + } + } + } + }, + ]); + }, + sync_with_google_contacts: function (frm) { + if (frm.doc.sync_with_google_contacts) { + xhiveframework.db.get_value( + "Google Contacts", + { email_id: xhiveframework.session.user }, + "name", + (r) => { + if (r && r.name) { + frm.set_value("google_contacts", r.name); + } + } + ); + } + }, +}); + +xhiveframework.ui.form.on("Dynamic Link", { + link_name: function (frm, cdt, cdn) { + var child = locals[cdt][cdn]; + if (child.link_name) { + xhiveframework.model.with_doctype(child.link_doctype, function () { + var title_field = xhiveframework.get_meta(child.link_doctype).title_field || "name"; + xhiveframework.model.get_value( + child.link_doctype, + child.link_name, + title_field, + function (r) { + xhiveframework.model.set_value(cdt, cdn, "link_title", r[title_field]); + } + ); + }); + } + }, +}); diff --git a/xhiveframework/contacts/doctype/contact/contact.json b/xhiveframework/contacts/doctype/contact/contact.json new file mode 100644 index 0000000..83d1002 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/contact.json @@ -0,0 +1,398 @@ +{ + "actions": [], + "allow_events_in_timeline": 1, + "allow_import": 1, + "allow_rename": 1, + "creation": "2013-01-10 16:34:32", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "contact_section", + "first_name", + "middle_name", + "last_name", + "full_name", + "email_id", + "user", + "address", + "sync_with_google_contacts", + "cb00", + "status", + "salutation", + "designation", + "gender", + "phone", + "mobile_no", + "company_name", + "image", + "sb_00", + "google_contacts", + "google_contacts_id", + "cb_00", + "pulled_from_google_contacts", + "sb_01", + "email_ids", + "phone_nos", + "contact_details", + "links", + "is_primary_contact", + "more_info", + "department", + "unsubscribed" + ], + "fields": [ + { + "fieldname": "contact_section", + "fieldtype": "Section Break", + "options": "fa fa-user" + }, + { + "fieldname": "first_name", + "fieldtype": "Data", + "in_global_search": 1, + "label": "First Name", + "oldfieldname": "first_name", + "oldfieldtype": "Data" + }, + { + "bold": 1, + "fieldname": "last_name", + "fieldtype": "Data", + "label": "Last Name", + "oldfieldname": "last_name", + "oldfieldtype": "Data" + }, + { + "bold": 1, + "fieldname": "email_id", + "fieldtype": "Data", + "in_global_search": 1, + "in_list_view": 1, + "label": "Email Address", + "oldfieldname": "email_id", + "oldfieldtype": "Data", + "options": "Email", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "in_global_search": 1, + "label": "User Id", + "options": "User" + }, + { + "fieldname": "cb00", + "fieldtype": "Column Break" + }, + { + "default": "Passive", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Passive\nOpen\nReplied" + }, + { + "fieldname": "salutation", + "fieldtype": "Link", + "label": "Salutation", + "options": "Salutation" + }, + { + "fieldname": "gender", + "fieldtype": "Link", + "label": "Gender", + "options": "Gender" + }, + { + "bold": 1, + "fieldname": "phone", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Phone", + "oldfieldname": "contact_no", + "oldfieldtype": "Data", + "options": "Phone", + "read_only": 1 + }, + { + "fieldname": "image", + "fieldtype": "Attach Image", + "hidden": 1, + "label": "Image", + "print_hide": 1 + }, + { + "fieldname": "contact_details", + "fieldtype": "Section Break", + "label": "Reference", + "options": "fa fa-pushpin" + }, + { + "default": "0", + "fieldname": "is_primary_contact", + "fieldtype": "Check", + "label": "Is Primary Contact", + "oldfieldname": "is_primary_contact", + "oldfieldtype": "Select" + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "Dynamic Link" + }, + { + "fieldname": "more_info", + "fieldtype": "Section Break", + "label": "More Information", + "options": "fa fa-file-text" + }, + { + "fieldname": "department", + "fieldtype": "Data", + "label": "Department" + }, + { + "fieldname": "designation", + "fieldtype": "Data", + "label": "Designation" + }, + { + "default": "0", + "fieldname": "unsubscribed", + "fieldtype": "Check", + "label": "Unsubscribed" + }, + { + "fieldname": "middle_name", + "fieldtype": "Data", + "label": "Middle Name" + }, + { + "collapsible": 1, + "depends_on": "eval:doc.sync_with_google_contacts || doc.pulled_from_google_contacts", + "fieldname": "sb_00", + "fieldtype": "Section Break", + "label": "Google Contacts" + }, + { + "fieldname": "email_ids", + "fieldtype": "Table", + "label": "Email IDs", + "options": "Contact Email" + }, + { + "fieldname": "address", + "fieldtype": "Link", + "label": "Address", + "options": "Address" + }, + { + "fieldname": "phone_nos", + "fieldtype": "Table", + "label": "Contact Numbers", + "options": "Contact Phone" + }, + { + "fieldname": "mobile_no", + "fieldtype": "Data", + "label": "Mobile No", + "options": "Phone", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "pulled_from_google_contacts", + "fieldtype": "Check", + "label": "Pulled from Google Contacts", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "sync_with_google_contacts", + "fieldtype": "Check", + "label": "Sync with Google Contacts" + }, + { + "fieldname": "google_contacts", + "fieldtype": "Link", + "label": "Google Contacts", + "options": "Google Contacts" + }, + { + "fieldname": "cb_00", + "fieldtype": "Column Break" + }, + { + "fieldname": "sb_01", + "fieldtype": "Section Break", + "label": "Contact Details" + }, + { + "fieldname": "google_contacts_id", + "fieldtype": "Data", + "label": "Google Contacts Id", + "read_only": 1 + }, + { + "fieldname": "company_name", + "fieldtype": "Data", + "label": "Company Name" + }, + { + "fieldname": "full_name", + "fieldtype": "Data", + "hidden": 1, + "label": "Full Name", + "read_only": 1 + } + ], + "icon": "fa fa-user", + "idx": 1, + "image_field": "image", + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Contact", + "name_case": "Title Case", + "naming_rule": "By script", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Sales Master Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Purchase Master Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Sales Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Purchase Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Maintenance Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Accounts Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Sales User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Purchase User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Maintenance User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Accounts User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "if_owner": 1, + "read": 1, + "report": 1, + "role": "All", + "write": 1 + } + ], + "show_title_field_in_link": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "full_name" +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/contact/contact.py b/xhiveframework/contacts/doctype/contact/contact.py new file mode 100644 index 0000000..6bfb2c1 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/contact.py @@ -0,0 +1,414 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework import _ +from xhiveframework.contacts.address_and_contact import set_link_title +from xhiveframework.core.doctype.dynamic_link.dynamic_link import deduplicate_dynamic_links +from xhiveframework.model.document import Document +from xhiveframework.model.naming import append_number_if_name_exists +from xhiveframework.utils import cstr, has_gravatar + + +class Contact(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.contacts.doctype.contact_email.contact_email import ContactEmail + from xhiveframework.contacts.doctype.contact_phone.contact_phone import ContactPhone + from xhiveframework.core.doctype.dynamic_link.dynamic_link import DynamicLink + from xhiveframework.types import DF + + address: DF.Link | None + company_name: DF.Data | None + department: DF.Data | None + designation: DF.Data | None + email_id: DF.Data | None + email_ids: DF.Table[ContactEmail] + first_name: DF.Data | None + full_name: DF.Data | None + gender: DF.Link | None + google_contacts: DF.Link | None + google_contacts_id: DF.Data | None + image: DF.AttachImage | None + is_primary_contact: DF.Check + last_name: DF.Data | None + links: DF.Table[DynamicLink] + middle_name: DF.Data | None + mobile_no: DF.Data | None + phone: DF.Data | None + phone_nos: DF.Table[ContactPhone] + pulled_from_google_contacts: DF.Check + salutation: DF.Link | None + status: DF.Literal["Passive", "Open", "Replied"] + sync_with_google_contacts: DF.Check + unsubscribed: DF.Check + user: DF.Link | None + + # end: auto-generated types + def autoname(self): + self.name = self._get_full_name() + + # concat party name if reqd + for link in self.links: + self.name = self.name + "-" + link.link_name.strip() + break + + if xhiveframework.db.exists("Contact", self.name): + self.name = append_number_if_name_exists("Contact", self.name) + + def validate(self): + self.full_name = self._get_full_name() + self.set_primary_email() + self.set_primary("phone") + self.set_primary("mobile_no") + + self.set_user() + + set_link_title(self) + + if self.email_id and not self.image: + self.image = has_gravatar(self.email_id) + + if self.get("sync_with_google_contacts") and not self.get("google_contacts"): + xhiveframework.throw(_("Select Google Contacts to which contact should be synced.")) + + deduplicate_dynamic_links(self) + + def set_user(self): + if not self.user and self.email_id: + self.user = xhiveframework.db.get_value("User", {"email": self.email_id}) + + def get_link_for(self, link_doctype): + """Return the link name, if exists for the given link DocType""" + for link in self.links: + if link.link_doctype == link_doctype: + return link.link_name + + return None + + def has_link(self, doctype, name): + for link in self.links: + if link.link_doctype == doctype and link.link_name == name: + return True + + def has_common_link(self, doc): + reference_links = [(link.link_doctype, link.link_name) for link in doc.links] + for link in self.links: + if (link.link_doctype, link.link_name) in reference_links: + return True + + def add_email(self, email_id, is_primary=0, autosave=False): + if not xhiveframework.db.exists("Contact Email", {"email_id": email_id, "parent": self.name}): + self.append("email_ids", {"email_id": email_id, "is_primary": is_primary}) + + if autosave: + self.save(ignore_permissions=True) + + def add_phone(self, phone, is_primary_phone=0, is_primary_mobile_no=0, autosave=False): + if not xhiveframework.db.exists("Contact Phone", {"phone": phone, "parent": self.name}): + self.append( + "phone_nos", + { + "phone": phone, + "is_primary_phone": is_primary_phone, + "is_primary_mobile_no": is_primary_mobile_no, + }, + ) + + if autosave: + self.save(ignore_permissions=True) + + def set_primary_email(self): + if not self.email_ids: + self.email_id = "" + return + + if len([email.email_id for email in self.email_ids if email.is_primary]) > 1: + xhiveframework.throw(_("Only one {0} can be set as primary.").format(xhiveframework.bold("Email ID"))) + + primary_email_exists = False + for d in self.email_ids: + if d.is_primary == 1: + primary_email_exists = True + self.email_id = d.email_id.strip() + break + + if not primary_email_exists: + self.email_id = "" + + def set_primary(self, fieldname): + # Used to set primary mobile and phone no. + if len(self.phone_nos) == 0: + setattr(self, fieldname, "") + return + + field_name = "is_primary_" + fieldname + + is_primary = [phone.phone for phone in self.phone_nos if phone.get(field_name)] + + if len(is_primary) > 1: + xhiveframework.throw( + _("Only one {0} can be set as primary.").format(xhiveframework.bold(xhiveframework.unscrub(fieldname))) + ) + + primary_number_exists = False + for d in self.phone_nos: + if d.get(field_name) == 1: + primary_number_exists = True + setattr(self, fieldname, d.phone) + break + + if not primary_number_exists: + setattr(self, fieldname, "") + + def _get_full_name(self) -> str: + return get_full_name(self.first_name, self.middle_name, self.last_name, self.company_name) + + +def get_default_contact(doctype, name): + """Returns default contact for the given doctype, name""" + out = xhiveframework.db.sql( + """select parent, + IFNULL((select is_primary_contact from tabContact c where c.name = dl.parent), 0) + as is_primary_contact + from + `tabDynamic Link` dl + where + dl.link_doctype=%s and + dl.link_name=%s and + dl.parenttype = 'Contact' """, + (doctype, name), + as_dict=True, + ) + + if out: + for contact in out: + if contact.is_primary_contact: + return contact.parent + return out[0].parent + else: + return None + + +@xhiveframework.whitelist() +def invite_user(contact: str): + contact = xhiveframework.get_doc("Contact", contact) + contact.check_permission() + + if not contact.email_id: + xhiveframework.throw(_("Please set Email Address")) + + user = xhiveframework.get_doc( + { + "doctype": "User", + "first_name": contact.first_name, + "last_name": contact.last_name, + "email": contact.email_id, + "user_type": "Website User", + "send_welcome_email": 1, + } + ).insert() + + return user.name + + +@xhiveframework.whitelist() +def get_contact_details(contact): + contact = xhiveframework.get_doc("Contact", contact) + contact.check_permission() + + return { + "contact_person": contact.get("name"), + "contact_display": contact.get("full_name"), + "contact_email": contact.get("email_id"), + "contact_mobile": contact.get("mobile_no"), + "contact_phone": contact.get("phone"), + "contact_designation": contact.get("designation"), + "contact_department": contact.get("department"), + } + + +def update_contact(doc, method): + """Update contact when user is updated, if contact is found. Called via hooks""" + contact_name = xhiveframework.db.get_value("Contact", {"email_id": doc.name}) + if contact_name: + contact = xhiveframework.get_doc("Contact", contact_name) + for key in ("first_name", "last_name", "phone"): + if doc.get(key): + contact.set(key, doc.get(key)) + contact.flags.ignore_mandatory = True + contact.save(ignore_permissions=True) + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def contact_query(doctype, txt, searchfield, start, page_len, filters): + from xhiveframework.desk.reportview import get_match_cond + + doctype = "Contact" + if not xhiveframework.get_meta(doctype).get_field(searchfield) and searchfield not in xhiveframework.db.DEFAULT_COLUMNS: + return [] + + link_doctype = filters.pop("link_doctype") + link_name = filters.pop("link_name") + + return xhiveframework.db.sql( + f"""select + `tabContact`.name, `tabContact`.full_name, `tabContact`.company_name + from + `tabContact`, `tabDynamic Link` + where + `tabDynamic Link`.parent = `tabContact`.name and + `tabDynamic Link`.parenttype = 'Contact' and + `tabDynamic Link`.link_doctype = %(link_doctype)s and + `tabDynamic Link`.link_name = %(link_name)s and + `tabContact`.`{searchfield}` like %(txt)s + {get_match_cond(doctype)} + order by + if(locate(%(_txt)s, `tabContact`.full_name), locate(%(_txt)s, `tabContact`.company_name), 99999), + `tabContact`.idx desc, `tabContact`.full_name + limit %(start)s, %(page_len)s """, + { + "txt": "%" + txt + "%", + "_txt": txt.replace("%", ""), + "start": start, + "page_len": page_len, + "link_name": link_name, + "link_doctype": link_doctype, + }, + ) + + +@xhiveframework.whitelist() +def address_query(links): + import json + + links = [ + {"link_doctype": d.get("link_doctype"), "link_name": d.get("link_name")} for d in json.loads(links) + ] + result = [] + + for link in links: + if not xhiveframework.has_permission( + doctype=link.get("link_doctype"), ptype="read", doc=link.get("link_name") + ): + continue + + res = xhiveframework.db.sql( + """ + SELECT `tabAddress`.name + FROM `tabAddress`, `tabDynamic Link` + WHERE `tabDynamic Link`.parenttype='Address' + AND `tabDynamic Link`.parent=`tabAddress`.name + AND `tabDynamic Link`.link_doctype = %(link_doctype)s + AND `tabDynamic Link`.link_name = %(link_name)s + """, + { + "link_doctype": link.get("link_doctype"), + "link_name": link.get("link_name"), + }, + as_dict=True, + ) + + result.extend([l.name for l in res]) + + return result + + +def get_contact_with_phone_number(number): + if not number: + return + + contacts = xhiveframework.get_all( + "Contact Phone", filters=[["phone", "like", f"%{number}"]], fields=["parent"], limit=1 + ) + + return contacts[0].parent if contacts else None + + +def get_contact_name(email_id): + contact = xhiveframework.get_all("Contact Email", filters={"email_id": email_id}, fields=["parent"], limit=1) + return contact[0].parent if contact else None + + +def get_contacts_linking_to(doctype, docname, fields=None): + """Return a list of contacts containing a link to the given document.""" + return xhiveframework.get_list( + "Contact", + fields=fields, + filters=[ + ["Dynamic Link", "link_doctype", "=", doctype], + ["Dynamic Link", "link_name", "=", docname], + ], + ) + + +def get_contacts_linked_from(doctype, docname, fields=None): + """Return a list of contacts that are contained in (linked from) the given document.""" + link_fields = xhiveframework.get_meta(doctype).get("fields", {"fieldtype": "Link", "options": "Contact"}) + if not link_fields: + return [] + + contact_names = xhiveframework.get_value(doctype, docname, fieldname=[f.fieldname for f in link_fields]) + if not contact_names: + return [] + + return xhiveframework.get_list("Contact", fields=fields, filters={"name": ("in", contact_names)}) + + +def get_full_name( + first: str | None = None, + middle: str | None = None, + last: str | None = None, + company: str | None = None, +) -> str: + full_name = " ".join(filter(None, [cstr(f).strip() for f in [first, middle, last]])) + if not full_name and company: + full_name = company + + return full_name + + +def get_contact_display_list(doctype: str, name: str) -> list[dict]: + from xhiveframework.contacts.doctype.address.address import get_condensed_address + + if not xhiveframework.has_permission("Contact", "read"): + return [] + + contact_list = xhiveframework.get_list( + "Contact", + filters=[ + ["Dynamic Link", "link_doctype", "=", doctype], + ["Dynamic Link", "link_name", "=", name], + ["Dynamic Link", "parenttype", "=", "Contact"], + ], + fields=["*"], + order_by="is_primary_contact DESC, creation ASC", + ) + + for contact in contact_list: + contact["email_ids"] = xhiveframework.get_all( + "Contact Email", + filters={"parenttype": "Contact", "parent": contact.name, "is_primary": 0}, + fields=["email_id"], + ) + + contact["phone_nos"] = xhiveframework.get_all( + "Contact Phone", + filters={ + "parenttype": "Contact", + "parent": contact.name, + "is_primary_phone": 0, + "is_primary_mobile_no": 0, + }, + fields=["phone"], + ) + + if contact.address and xhiveframework.has_permission("Address", "read"): + address = xhiveframework.get_doc("Address", contact.address) + contact["address"] = get_condensed_address(address) + + return contact_list diff --git a/xhiveframework/contacts/doctype/contact/contact_list.js b/xhiveframework/contacts/doctype/contact/contact_list.js new file mode 100644 index 0000000..2f5f539 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/contact_list.js @@ -0,0 +1,3 @@ +xhiveframework.listview_settings["Contact"] = { + add_fields: ["image"], +}; diff --git a/xhiveframework/contacts/doctype/contact/test_contact.py b/xhiveframework/contacts/doctype/contact/test_contact.py new file mode 100644 index 0000000..de9d288 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/test_contact.py @@ -0,0 +1,78 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.contacts.doctype.contact.contact import get_full_name +from xhiveframework.email import get_contact_list +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["Contact", "Salutation"] + + +class TestContact(XhiveFrameworkTestCase): + def test_check_default_email(self): + emails = [ + {"email": "test1@example.com", "is_primary": 0}, + {"email": "test2@example.com", "is_primary": 0}, + {"email": "test3@example.com", "is_primary": 0}, + {"email": "test4@example.com", "is_primary": 1}, + {"email": "test5@example.com", "is_primary": 0}, + ] + contact = create_contact("Email", "Mr", emails=emails) + + self.assertEqual(contact.email_id, "test4@example.com") + + def test_check_default_phone_and_mobile(self): + phones = [ + {"phone": "+91 0000000000", "is_primary_phone": 0, "is_primary_mobile_no": 0}, + {"phone": "+91 0000000001", "is_primary_phone": 0, "is_primary_mobile_no": 0}, + {"phone": "+91 0000000002", "is_primary_phone": 1, "is_primary_mobile_no": 0}, + {"phone": "+91 0000000003", "is_primary_phone": 0, "is_primary_mobile_no": 1}, + ] + contact = create_contact("Phone", "Mr", phones=phones) + + self.assertEqual(contact.phone, "+91 0000000002") + self.assertEqual(contact.mobile_no, "+91 0000000003") + + def test_get_full_name(self): + self.assertEqual(get_full_name(first="John"), "John") + self.assertEqual(get_full_name(last="Doe"), "Doe") + self.assertEqual(get_full_name(company="Doe Pvt Ltd"), "Doe Pvt Ltd") + self.assertEqual(get_full_name(first="John", last="Doe"), "John Doe") + self.assertEqual(get_full_name(first="John", middle="Jane"), "John Jane") + self.assertEqual(get_full_name(first="John", last="Doe", company="Doe Pvt Ltd"), "John Doe") + self.assertEqual( + get_full_name(first="John", middle="Jane", last="Doe", company="Doe Pvt Ltd"), + "John Jane Doe", + ) + + def test_get_contact_list(self): + # First time from database + results = get_contact_list("_Test Supplier") + self.assertEqual(results[0].label, "test_contact@example.com") + self.assertEqual(results[0].value, "test_contact@example.com") + self.assertEqual(results[0].description, "_Test Contact For _Test Supplier") + + # Second time from cache + results = get_contact_list("_Test Supplier") + self.assertEqual(results[0].label, "test_contact@example.com") + self.assertEqual(results[0].value, "test_contact@example.com") + self.assertEqual(results[0].description, "_Test Contact For _Test Supplier") + + +def create_contact(name, salutation, emails=None, phones=None, save=True): + doc = xhiveframework.get_doc( + {"doctype": "Contact", "first_name": name, "status": "Open", "salutation": salutation} + ) + + if emails: + for d in emails: + doc.add_email(d.get("email"), d.get("is_primary")) + + if phones: + for d in phones: + doc.add_phone(d.get("phone"), d.get("is_primary_phone"), d.get("is_primary_mobile_no")) + + if save: + doc.insert() + + return doc diff --git a/xhiveframework/contacts/doctype/contact/test_records.json b/xhiveframework/contacts/doctype/contact/test_records.json new file mode 100644 index 0000000..35fff58 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact/test_records.json @@ -0,0 +1,39 @@ +[ + { + "doctype": "Contact", + "salutation": "Mr", + "first_name": "_Test Contact For _Test Customer", + "is_primary_contact": 1, + "status": "Open", + "email_ids": [ + { + "email_id": "test_contact@example.com", + "is_primary": 1 + } + ], + "phone_nos": [ + { + "phone": "+91 0000000000", + "is_primary_phone": 1 + } + ] + }, + { + "doctype": "Contact", + "first_name": "_Test Contact For _Test Supplier", + "is_primary_contact": 1, + "status": "Open", + "email_ids": [ + { + "email_id": "test_contact@example.com", + "is_primary": 1 + } + ], + "phone_nos": [ + { + "phone": "+91 0000000001", + "is_primary_phone": 1 + } + ] + } +] diff --git a/xhiveframework/contacts/doctype/contact_email/__init__.py b/xhiveframework/contacts/doctype/contact_email/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/contact_email/contact_email.json b/xhiveframework/contacts/doctype/contact_email/contact_email.json new file mode 100644 index 0000000..f36e155 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact_email/contact_email.json @@ -0,0 +1,39 @@ +{ + "creation": "2019-08-02 13:08:59.291097", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "email_id", + "is_primary" + ], + "fields": [ + { + "fieldname": "email_id", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Email ID", + "options": "Email", + "reqd": 1 + }, + { + "columns": 2, + "default": "0", + "fieldname": "is_primary", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Primary" + } + ], + "istable": 1, + "modified": "2019-09-24 17:47:30.565805", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Contact Email", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/contact_email/contact_email.py b/xhiveframework/contacts/doctype/contact_email/contact_email.py new file mode 100644 index 0000000..31f9cb1 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact_email/contact_email.py @@ -0,0 +1,23 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class ContactEmail(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email_id: DF.Data + is_primary: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/contacts/doctype/contact_phone/__init__.py b/xhiveframework/contacts/doctype/contact_phone/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/contact_phone/contact_phone.json b/xhiveframework/contacts/doctype/contact_phone/contact_phone.json new file mode 100644 index 0000000..5412e4a --- /dev/null +++ b/xhiveframework/contacts/doctype/contact_phone/contact_phone.json @@ -0,0 +1,50 @@ +{ + "actions": [], + "creation": "2019-08-02 13:10:37.890214", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "phone", + "is_primary_phone", + "is_primary_mobile_no" + ], + "fields": [ + { + "fieldname": "phone", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Number", + "options": "Phone", + "reqd": 1 + }, + { + "columns": 2, + "default": "0", + "fieldname": "is_primary_phone", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Primary Phone" + }, + { + "columns": 2, + "default": "0", + "fieldname": "is_primary_mobile_no", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Primary Mobile" + } + ], + "istable": 1, + "links": [], + "modified": "2020-04-06 18:28:10.486220", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Contact Phone", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/contact_phone/contact_phone.py b/xhiveframework/contacts/doctype/contact_phone/contact_phone.py new file mode 100644 index 0000000..6c0a241 --- /dev/null +++ b/xhiveframework/contacts/doctype/contact_phone/contact_phone.py @@ -0,0 +1,24 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class ContactPhone(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + is_primary_mobile_no: DF.Check + is_primary_phone: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + phone: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/contacts/doctype/gender/__init__.py b/xhiveframework/contacts/doctype/gender/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/gender/gender.js b/xhiveframework/contacts/doctype/gender/gender.js new file mode 100644 index 0000000..692e736 --- /dev/null +++ b/xhiveframework/contacts/doctype/gender/gender.js @@ -0,0 +1,6 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Gender", { + refresh: function () {}, +}); diff --git a/xhiveframework/contacts/doctype/gender/gender.json b/xhiveframework/contacts/doctype/gender/gender.json new file mode 100644 index 0000000..34e1dda --- /dev/null +++ b/xhiveframework/contacts/doctype/gender/gender.json @@ -0,0 +1,48 @@ +{ + "actions": [], + "autoname": "field:gender", + "creation": "2017-04-10 12:11:36.526508", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "gender" + ], + "fields": [ + { + "fieldname": "gender", + "fieldtype": "Data", + "label": "Gender", + "unique": 1 + } + ], + "links": [], + "modified": "2022-08-05 18:33:28.043370", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Gender", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "All" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1, + "translated_doctype": 1 +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/gender/gender.py b/xhiveframework/contacts/doctype/gender/gender.py new file mode 100644 index 0000000..93d603b --- /dev/null +++ b/xhiveframework/contacts/doctype/gender/gender.py @@ -0,0 +1,18 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class Gender(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + gender: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/contacts/doctype/gender/test_gender.py b/xhiveframework/contacts/doctype/gender/test_gender.py new file mode 100644 index 0000000..2cb968c --- /dev/null +++ b/xhiveframework/contacts/doctype/gender/test_gender.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestGender(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/contacts/doctype/salutation/__init__.py b/xhiveframework/contacts/doctype/salutation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/doctype/salutation/salutation.js b/xhiveframework/contacts/doctype/salutation/salutation.js new file mode 100644 index 0000000..6a0cf8a --- /dev/null +++ b/xhiveframework/contacts/doctype/salutation/salutation.js @@ -0,0 +1,6 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Salutation", { + refresh: function () {}, +}); diff --git a/xhiveframework/contacts/doctype/salutation/salutation.json b/xhiveframework/contacts/doctype/salutation/salutation.json new file mode 100644 index 0000000..98ed082 --- /dev/null +++ b/xhiveframework/contacts/doctype/salutation/salutation.json @@ -0,0 +1,61 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:salutation", + "creation": "2017-04-10 12:17:58.071915", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "salutation" + ], + "fields": [ + { + "fieldname": "salutation", + "fieldtype": "Data", + "label": "Salutation", + "unique": 1 + } + ], + "links": [], + "modified": "2022-08-05 18:33:28.196387", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Salutation", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "All" + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1, + "translated_doctype": 1 +} \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/salutation/salutation.py b/xhiveframework/contacts/doctype/salutation/salutation.py new file mode 100644 index 0000000..485553a --- /dev/null +++ b/xhiveframework/contacts/doctype/salutation/salutation.py @@ -0,0 +1,18 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class Salutation(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + salutation: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/contacts/doctype/salutation/test_records.json b/xhiveframework/contacts/doctype/salutation/test_records.json new file mode 100644 index 0000000..3a87fff --- /dev/null +++ b/xhiveframework/contacts/doctype/salutation/test_records.json @@ -0,0 +1,8 @@ +[ + { + "salutation": "Mr" + }, + { + "salutation": "Mrs" + } +] \ No newline at end of file diff --git a/xhiveframework/contacts/doctype/salutation/test_salutation.py b/xhiveframework/contacts/doctype/salutation/test_salutation.py new file mode 100644 index 0000000..8f004d9 --- /dev/null +++ b/xhiveframework/contacts/doctype/salutation/test_salutation.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSalutation(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/contacts/report/__init__.py b/xhiveframework/contacts/report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/report/addresses_and_contacts/__init__.py b/xhiveframework/contacts/report/addresses_and_contacts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.js b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.js new file mode 100644 index 0000000..7815ae3 --- /dev/null +++ b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.js @@ -0,0 +1,33 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["Addresses And Contacts"] = { + filters: [ + { + reqd: 1, + fieldname: "reference_doctype", + label: __("Entity Type"), + fieldtype: "Link", + options: "DocType", + get_query: function () { + return { + filters: { + name: ["in", "Contact, Address"], + }, + }; + }, + }, + { + fieldname: "reference_name", + label: __("Entity Name"), + fieldtype: "Dynamic Link", + get_options: function () { + let reference_doctype = xhiveframework.query_report.get_filter_value("reference_doctype"); + if (!reference_doctype) { + xhiveframework.throw(__("Please select Entity Type first")); + } + return reference_doctype; + }, + }, + ], +}; diff --git a/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.json b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.json new file mode 100644 index 0000000..2d62444 --- /dev/null +++ b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.json @@ -0,0 +1,32 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "creation": "2017-01-19 12:57:22.881566", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 2, + "is_standard": "Yes", + "modified": "2017-04-10 15:04:12.498920", + "modified_by": "Administrator", + "module": "Contacts", + "name": "Addresses And Contacts", + "owner": "Administrator", + "ref_doctype": "Address", + "report_name": "Addresses And Contacts", + "report_type": "Script Report", + "roles": [ + { + "role": "Sales User" + }, + { + "role": "Purchase User" + }, + { + "role": "Maintenance User" + }, + { + "role": "Accounts User" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.py b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.py new file mode 100644 index 0000000..af3b780 --- /dev/null +++ b/xhiveframework/contacts/report/addresses_and_contacts/addresses_and_contacts.py @@ -0,0 +1,123 @@ +# Copyright (c) 2013, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework import _ + +field_map = { + "Contact": [ + "first_name", + "last_name", + "address", + "phone", + "mobile_no", + "email_id", + "is_primary_contact", + ], + "Address": [ + "address_line1", + "address_line2", + "city", + "state", + "pincode", + "country", + "is_primary_address", + ], +} + + +def execute(filters=None): + columns, data = get_columns(filters), get_data(filters) + return columns, data + + +def get_columns(filters): + return [ + "{reference_doctype}:Link/{reference_doctype}".format( + reference_doctype=filters.get("reference_doctype") + ), + "Address Line 1", + "Address Line 2", + "City", + "State", + "Postal Code", + "Country", + "Is Primary Address:Check", + "First Name", + "Last Name", + "Address", + "Phone", + "Email Id", + "Is Primary Contact:Check", + ] + + +def get_data(filters): + reference_doctype = filters.get("reference_doctype") + reference_name = filters.get("reference_name") + + return get_reference_addresses_and_contact(reference_doctype, reference_name) + + +def get_reference_addresses_and_contact(reference_doctype, reference_name): + data = [] + filters = None + reference_details = xhiveframework._dict() + + if not reference_doctype: + return [] + + if reference_name: + filters = {"name": reference_name} + + reference_list = [ + d[0] for d in xhiveframework.get_list(reference_doctype, filters=filters, fields=["name"], as_list=True) + ] + + for d in reference_list: + reference_details.setdefault(d, xhiveframework._dict()) + reference_details = get_reference_details(reference_doctype, "Address", reference_list, reference_details) + reference_details = get_reference_details(reference_doctype, "Contact", reference_list, reference_details) + + for reference_name, details in reference_details.items(): + addresses = details.get("address", []) + contacts = details.get("contact", []) + if not any([addresses, contacts]): + result = [reference_name] + result.extend(add_blank_columns_for("Address")) + result.extend(add_blank_columns_for("Contact")) + data.append(result) + else: + addresses = list(map(list, addresses)) + contacts = list(map(list, contacts)) + + max_length = max(len(addresses), len(contacts)) + for idx in range(0, max_length): + result = [reference_name] + + result.extend(addresses[idx] if idx < len(addresses) else add_blank_columns_for("Address")) + result.extend(contacts[idx] if idx < len(contacts) else add_blank_columns_for("Contact")) + + data.append(result) + + return data + + +def get_reference_details(reference_doctype, doctype, reference_list, reference_details): + filters = [ + ["Dynamic Link", "link_doctype", "=", reference_doctype], + ["Dynamic Link", "link_name", "in", reference_list], + ] + fields = ["`tabDynamic Link`.link_name", *field_map.get(doctype, [])] + + records = xhiveframework.get_list(doctype, filters=filters, fields=fields, as_list=True) + temp_records = [d[1:] for d in records] + + if not reference_list: + xhiveframework.throw(_("No records present in {0}").format(reference_doctype)) + + reference_details[reference_list[0]][xhiveframework.scrub(doctype)] = temp_records + return reference_details + + +def add_blank_columns_for(doctype): + return ["" for field in field_map.get(doctype, [])] diff --git a/xhiveframework/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py b/xhiveframework/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py new file mode 100644 index 0000000..b77c0e5 --- /dev/null +++ b/xhiveframework/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py @@ -0,0 +1,113 @@ +import xhiveframework +import xhiveframework.defaults +from xhiveframework.contacts.report.addresses_and_contacts.addresses_and_contacts import get_data +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +def get_custom_linked_doctype(): + if bool(xhiveframework.get_all("DocType", filters={"name": "Test Custom Doctype"})): + return + + doc = xhiveframework.get_doc( + { + "doctype": "DocType", + "module": "Core", + "custom": 1, + "fields": [ + {"label": "Test Field", "fieldname": "test_field", "fieldtype": "Data"}, + {"label": "Contact HTML", "fieldname": "contact_html", "fieldtype": "HTML"}, + {"label": "Address HTML", "fieldname": "address_html", "fieldtype": "HTML"}, + ], + "permissions": [{"role": "System Manager", "read": 1}], + "name": "Test Custom Doctype", + } + ) + doc.insert() + + +def get_custom_doc_for_address_and_contacts(): + get_custom_linked_doctype() + return xhiveframework.get_doc( + { + "doctype": "Test Custom Doctype", + "test_field": "Hello", + } + ).insert() + + +def create_linked_address(link_list): + if xhiveframework.flags.test_address_created: + return + + address = xhiveframework.get_doc( + { + "doctype": "Address", + "address_title": "_Test Address", + "address_type": "Billing", + "address_line1": "test address line 1", + "address_line2": "test address line 2", + "city": "Milan", + "country": "Italy", + } + ) + + for name in link_list: + address.append("links", {"link_doctype": "Test Custom Doctype", "link_name": name}) + + address.insert() + xhiveframework.flags.test_address_created = True + + return address.name + + +def create_linked_contact(link_list, address): + if xhiveframework.flags.test_contact_created: + return + + contact = xhiveframework.get_doc( + { + "doctype": "Contact", + "salutation": "Mr", + "first_name": "_Test First Name", + "last_name": "_Test Last Name", + "is_primary_contact": 1, + "address": address, + "status": "Open", + } + ) + contact.add_email("test_contact@example.com", is_primary=True) + contact.add_phone("+91 0000000000", is_primary_phone=True) + + for name in link_list: + contact.append("links", {"link_doctype": "Test Custom Doctype", "link_name": name}) + + contact.insert(ignore_permissions=True) + xhiveframework.flags.test_contact_created = True + + +class TestAddressesAndContacts(XhiveFrameworkTestCase): + def test_get_data(self): + linked_docs = [get_custom_doc_for_address_and_contacts()] + links_list = [item.name for item in linked_docs] + d = create_linked_address(links_list) + create_linked_contact(links_list, d) + report_data = get_data({"reference_doctype": "Test Custom Doctype"}) + for idx, link in enumerate(links_list): + test_item = [ + link, + "test address line 1", + "test address line 2", + "Milan", + None, + None, + "Italy", + 0, + "_Test First Name", + "_Test Last Name", + "_Test Address-Billing", + "+91 0000000000", + "", + "test_contact@example.com", + 1, + ] + self.assertListEqual(test_item, report_data[idx]) diff --git a/xhiveframework/core/README.md b/xhiveframework/core/README.md new file mode 100644 index 0000000..41ca7fd --- /dev/null +++ b/xhiveframework/core/README.md @@ -0,0 +1 @@ +Core module contains the models required for the basic functioning of xhiveframework including DocType, User (user), Role and others. diff --git a/xhiveframework/core/__init__.py b/xhiveframework/core/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/api/__init__.py b/xhiveframework/core/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/api/file.py b/xhiveframework/core/api/file.py new file mode 100644 index 0000000..f67a462 --- /dev/null +++ b/xhiveframework/core/api/file.py @@ -0,0 +1,124 @@ +import json + +import xhiveframework +from xhiveframework.core.doctype.file.file import File +from xhiveframework.core.doctype.file.utils import setup_folder_path +from xhiveframework.utils import cint, cstr + + +@xhiveframework.whitelist() +def unzip_file(name: str): + """Unzip the given file and make file records for each of the extracted files""" + file: File = xhiveframework.get_doc("File", name) + return file.unzip() + + +@xhiveframework.whitelist() +def get_attached_images(doctype: str, names: list[str] | str) -> xhiveframework._dict: + """get list of image urls attached in form + returns {name: ['image.jpg', 'image.png']}""" + + if isinstance(names, str): + names = json.loads(names) + + img_urls = xhiveframework.db.get_list( + "File", + filters={ + "attached_to_doctype": doctype, + "attached_to_name": ("in", names), + "is_folder": 0, + }, + fields=["file_url", "attached_to_name as docname"], + ) + + out = xhiveframework._dict() + for i in img_urls: + out[i.docname] = out.get(i.docname, []) + out[i.docname].append(i.file_url) + + return out + + +@xhiveframework.whitelist() +def get_files_in_folder(folder: str, start: int = 0, page_length: int = 20) -> dict: + attachment_folder = xhiveframework.db.get_value( + "File", + "Home/Attachments", + ["name", "file_name", "file_url", "is_folder", "modified"], + as_dict=1, + ) + + files = xhiveframework.get_list( + "File", + {"folder": folder}, + ["name", "file_name", "file_url", "is_folder", "modified"], + start=start, + page_length=page_length + 1, + ) + + if folder == "Home" and attachment_folder not in files: + files.insert(0, attachment_folder) + + return {"files": files[:page_length], "has_more": len(files) > page_length} + + +@xhiveframework.whitelist() +def get_files_by_search_text(text: str) -> list[dict]: + if not text: + return [] + + text = "%" + cstr(text).lower() + "%" + return xhiveframework.get_list( + "File", + fields=["name", "file_name", "file_url", "is_folder", "modified"], + filters={"is_folder": False}, + or_filters={ + "file_name": ("like", text), + "file_url": text, + "name": ("like", text), + }, + order_by="modified desc", + limit=20, + ) + + +@xhiveframework.whitelist(allow_guest=True) +def get_max_file_size() -> int: + return ( + cint(xhiveframework.get_system_settings("max_file_size")) * 1024 * 1024 + or cint(xhiveframework.conf.get("max_file_size")) + or 25 * 1024 * 1024 + ) + + +@xhiveframework.whitelist() +def create_new_folder(file_name: str, folder: str) -> File: + """create new folder under current parent folder""" + file = xhiveframework.new_doc("File") + file.file_name = file_name + file.is_folder = 1 + file.folder = folder + file.insert(ignore_if_duplicate=True) + return file + + +@xhiveframework.whitelist() +def move_file(file_list: list[File | dict] | str, new_parent: str, old_parent: str) -> None: + if isinstance(file_list, str): + file_list = json.loads(file_list) + + # will check for permission on each file & update parent + for file_obj in file_list: + setup_folder_path(file_obj.get("name"), new_parent) + + # recalculate sizes + xhiveframework.get_doc("File", old_parent).save() + xhiveframework.get_doc("File", new_parent).save() + + +@xhiveframework.whitelist() +def zip_files(files: str): + files = xhiveframework.parse_json(files) + xhiveframework.response["filename"] = "files.zip" + xhiveframework.response["filecontent"] = File.zip_files(files) + xhiveframework.response["type"] = "download" diff --git a/xhiveframework/core/doctype/__init__.py b/xhiveframework/core/doctype/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/access_log/__init__.py b/xhiveframework/core/doctype/access_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/access_log/access_log.js b/xhiveframework/core/doctype/access_log/access_log.js new file mode 100644 index 0000000..fe839fc --- /dev/null +++ b/xhiveframework/core/doctype/access_log/access_log.js @@ -0,0 +1,17 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Access Log", { + show_document: function (frm) { + xhiveframework.set_route("Form", frm.doc.export_from, frm.doc.reference_document); + }, + + show_report: function (frm) { + if (frm.doc.report_name.includes("/")) { + xhiveframework.set_route(frm.doc.report_name); + } else { + let filters = frm.doc.filters ? JSON.parse(frm.doc.filters) : {}; + xhiveframework.set_route("query-report", frm.doc.report_name, filters); + } + }, +}); diff --git a/xhiveframework/core/doctype/access_log/access_log.json b/xhiveframework/core/doctype/access_log/access_log.json new file mode 100644 index 0000000..69803ef --- /dev/null +++ b/xhiveframework/core/doctype/access_log/access_log.json @@ -0,0 +1,156 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2019-07-25 15:44:44.955496", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "log_data_section", + "export_from", + "user", + "show_document", + "column_break_3", + "reference_document", + "timestamp", + "private_file_section", + "file_type", + "method", + "report_information_section", + "report_name", + "filters", + "show_report", + "raw_information_log_section", + "page", + "columns" + ], + "fields": [ + { + "fieldname": "export_from", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Export From", + "read_only": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "User ", + "options": "User", + "read_only": 1 + }, + { + "default": "Now", + "fieldname": "timestamp", + "fieldtype": "Datetime", + "label": "Timestamp", + "read_only": 1 + }, + { + "fieldname": "reference_document", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Reference Document", + "read_only": 1 + }, + { + "fieldname": "file_type", + "fieldtype": "Data", + "label": "File Type", + "read_only": 1 + }, + { + "fieldname": "report_name", + "fieldtype": "Data", + "label": "Report Name", + "read_only": 1 + }, + { + "fieldname": "page", + "fieldtype": "HTML Editor", + "label": "HTML Page", + "read_only": 1 + }, + { + "fieldname": "log_data_section", + "fieldtype": "Section Break", + "label": "Log Data" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "private_file_section", + "fieldtype": "Section Break", + "label": "File Information" + }, + { + "fieldname": "report_information_section", + "fieldtype": "Section Break", + "label": "Report Information" + }, + { + "fieldname": "raw_information_log_section", + "fieldtype": "Section Break", + "label": "RAW Information Log" + }, + { + "fieldname": "method", + "fieldtype": "Data", + "label": "Method", + "read_only": 1 + }, + { + "depends_on": "report_name", + "fieldname": "show_report", + "fieldtype": "Button", + "label": "Show Report" + }, + { + "depends_on": "reference_document", + "fieldname": "show_document", + "fieldtype": "Button", + "label": "Show Document" + }, + { + "depends_on": "eval: doc.filters != null", + "fieldname": "filters", + "fieldtype": "Code", + "label": "Filters", + "read_only": 1 + }, + { + "fieldname": "columns", + "fieldtype": "HTML Editor", + "label": "Columns / Fields", + "read_only": 1 + } + ], + "links": [], + "modified": "2022-06-13 05:59:26.866004", + "modified_by": "Administrator", + "module": "Core", + "name": "Access Log", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_seen": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/access_log/access_log.py b/xhiveframework/core/doctype/access_log/access_log.py new file mode 100644 index 0000000..26b6a18 --- /dev/null +++ b/xhiveframework/core/doctype/access_log/access_log.py @@ -0,0 +1,107 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +from tenacity import retry, retry_if_exception_type, stop_after_attempt + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.utils import cstr + + +class AccessLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + columns: DF.HTMLEditor | None + export_from: DF.Data | None + file_type: DF.Data | None + filters: DF.Code | None + method: DF.Data | None + page: DF.HTMLEditor | None + reference_document: DF.Data | None + report_name: DF.Data | None + timestamp: DF.Datetime | None + user: DF.Link | None + + # end: auto-generated types + @staticmethod + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Access Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + +@xhiveframework.whitelist() +def make_access_log( + doctype=None, + document=None, + method=None, + file_type=None, + report_name=None, + filters=None, + page=None, + columns=None, +): + _make_access_log( + doctype, + document, + method, + file_type, + report_name, + filters, + page, + columns, + ) + + +@xhiveframework.write_only() +@retry( + stop=stop_after_attempt(3), + retry=retry_if_exception_type(xhiveframework.DuplicateEntryError), + reraise=True, +) +def _make_access_log( + doctype=None, + document=None, + method=None, + file_type=None, + report_name=None, + filters=None, + page=None, + columns=None, +): + user = xhiveframework.session.user + in_request = xhiveframework.request and xhiveframework.request.method == "GET" + + access_log = xhiveframework.get_doc( + { + "doctype": "Access Log", + "user": user, + "export_from": doctype, + "reference_document": document, + "file_type": file_type, + "report_name": report_name, + "page": page, + "method": method, + "filters": cstr(filters) or None, + "columns": columns, + } + ) + + if xhiveframework.flags.read_only: + access_log.deferred_insert() + return + else: + access_log.db_insert() + + # `xhiveframework.db.commit` added because insert doesnt `commit` when called in GET requests like `printview` + # dont commit in test mode. It must be tempting to put this block along with the in_request in the + # whitelisted method...yeah, don't do it. That part would be executed possibly on a read only DB conn + if not xhiveframework.flags.in_test or in_request: + xhiveframework.db.commit() diff --git a/xhiveframework/core/doctype/access_log/access_log_list.js b/xhiveframework/core/doctype/access_log/access_log_list.js new file mode 100644 index 0000000..e437309 --- /dev/null +++ b/xhiveframework/core/doctype/access_log/access_log_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Access Log"] = { + onload: function (list_view) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(list_view.doctype); + }); + }, +}; diff --git a/xhiveframework/core/doctype/access_log/test_access_log.py b/xhiveframework/core/doctype/access_log/test_access_log.py new file mode 100644 index 0000000..60573e5 --- /dev/null +++ b/xhiveframework/core/doctype/access_log/test_access_log.py @@ -0,0 +1,175 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +import base64 +import os + +# imports - third party imports +import requests + +# imports - module imports +import xhiveframework +from xhiveframework.core.doctype.access_log.access_log import make_access_log +from xhiveframework.core.doctype.data_import.data_import import export_csv +from xhiveframework.core.doctype.user.user import generate_keys + +# imports - standard imports +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import cstr, get_site_url + + +class TestAccessLog(XhiveFrameworkTestCase): + def setUp(self): + # generate keys for current user to send requests for the following tests + generate_keys(xhiveframework.session.user) + xhiveframework.db.commit() + generated_secret = xhiveframework.utils.password.get_decrypted_password( + "User", xhiveframework.session.user, fieldname="api_secret" + ) + api_key = xhiveframework.db.get_value("User", "Administrator", "api_key") + self.header = {"Authorization": f"token {api_key}:{generated_secret}"} + + self.test_html_template = """ + + + + + + + +

HTML Table

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CompanyContactCountry
Alfreds FutterkisteMaria AndersGermany
Centro comercial MoctezumaFrancisco ChangMexico
Ernst HandelRoland MendelAustria
Island TradingHelen BennettUK
Laughing Bacchus WinecellarsYoshi TannamuriCanada
Magazzini Alimentari RiunitiGiovanni RovelliItaly
+ + + + """ + self.test_filters = { + "from_date": "2019-06-30", + "to_date": "2019-07-31", + "party": [], + "group_by": "Group by Voucher (Consolidated)", + "cost_center": [], + "project": [], + } + + self.test_doctype = "File" + self.test_document = "Test Document" + self.test_report_name = "General Ledger" + self.test_file_type = "CSV" + self.test_method = "Test Method" + self.file_name = xhiveframework.utils.random_string(10) + ".txt" + self.test_content = xhiveframework.utils.random_string(1024) + + def test_make_full_access_log(self): + self.maxDiff = None + + # test if all fields maintain data: html page and filters are converted? + make_access_log( + doctype=self.test_doctype, + document=self.test_document, + report_name=self.test_report_name, + page=self.test_html_template, + file_type=self.test_file_type, + method=self.test_method, + filters=self.test_filters, + ) + + last_doc = xhiveframework.get_last_doc("Access Log") + self.assertEqual(last_doc.filters, cstr(self.test_filters)) + self.assertEqual(self.test_doctype, last_doc.export_from) + self.assertEqual(self.test_document, last_doc.reference_document) + + def test_make_export_log(self): + # export data and delete temp file generated on disk + export_csv(self.test_doctype, self.file_name) + os.remove(self.file_name) + + # test if the exported data is logged + last_doc = xhiveframework.get_last_doc("Access Log") + self.assertEqual(self.test_doctype, last_doc.export_from) + + def test_private_file_download(self): + # create new private file + new_private_file = xhiveframework.get_doc( + { + "doctype": self.test_doctype, + "file_name": self.file_name, + "content": base64.b64encode(self.test_content.encode("utf-8")), + "is_private": 1, + } + ) + new_private_file.insert() + + # access the created file + private_file_link = get_site_url(xhiveframework.local.site) + new_private_file.file_url + + try: + request = requests.post(private_file_link, headers=self.header) + last_doc = xhiveframework.get_last_doc("Access Log") + + if request.ok: + # check for the access log of downloaded file + self.assertEqual(new_private_file.doctype, last_doc.export_from) + self.assertEqual(new_private_file.name, last_doc.reference_document) + + except requests.ConnectionError: + pass + + # cleanup + new_private_file.delete() + + def tearDown(self): + pass diff --git a/xhiveframework/core/doctype/activity_log/__init__.py b/xhiveframework/core/doctype/activity_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/activity_log/activity_log.js b/xhiveframework/core/doctype/activity_log/activity_log.js new file mode 100644 index 0000000..fa3b2bb --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/activity_log.js @@ -0,0 +1,9 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Activity Log", { + refresh: function (frm) { + // Nothing in this form is supposed to be editable. + frm.disable_form(); + }, +}); diff --git a/xhiveframework/core/doctype/activity_log/activity_log.json b/xhiveframework/core/doctype/activity_log/activity_log.json new file mode 100644 index 0000000..3943fc1 --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/activity_log.json @@ -0,0 +1,181 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2017-10-05 11:10:38.780133", + "description": "Keep track of all update feeds", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "subject", + "section_break_8", + "content", + "column_break_5", + "additional_info", + "communication_date", + "ip_address", + "column_break_7", + "operation", + "status", + "reference_section", + "reference_doctype", + "reference_name", + "reference_owner", + "column_break_14", + "timeline_doctype", + "timeline_name", + "link_doctype", + "link_name", + "user", + "full_name" + ], + "fields": [ + { + "fieldname": "subject", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Subject", + "reqd": 1 + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "content", + "fieldtype": "Text Editor", + "label": "Message", + "width": "400" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "collapsible": 1, + "fieldname": "additional_info", + "fieldtype": "Section Break", + "label": "More Information" + }, + { + "default": "Now", + "fieldname": "communication_date", + "fieldtype": "Datetime", + "label": "Date" + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "fieldname": "operation", + "fieldtype": "Select", + "label": "Operation", + "options": "\nLogin\nLogout\nImpersonate" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Status", + "options": "\nSuccess\nFailed\nLinked\nClosed" + }, + { + "collapsible": 1, + "fieldname": "reference_section", + "fieldtype": "Section Break", + "label": "Reference" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType" + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "label": "Reference Name", + "options": "reference_doctype" + }, + { + "fetch_from": "reference_name.owner", + "fieldname": "reference_owner", + "fieldtype": "Read Only", + "label": "Reference Owner" + }, + { + "fieldname": "column_break_14", + "fieldtype": "Column Break" + }, + { + "fieldname": "timeline_doctype", + "fieldtype": "Link", + "label": "Timeline DocType", + "options": "DocType" + }, + { + "fieldname": "timeline_name", + "fieldtype": "Dynamic Link", + "label": "Timeline Name", + "options": "timeline_doctype" + }, + { + "fieldname": "link_doctype", + "fieldtype": "Link", + "label": "Link DocType", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "link_name", + "fieldtype": "Dynamic Link", + "label": "Link Name", + "options": "link_doctype", + "read_only": 1 + }, + { + "default": "__user", + "fieldname": "user", + "fieldtype": "Link", + "ignore_user_permissions": 1, + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "full_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Full Name" + }, + { + "fieldname": "ip_address", + "fieldtype": "Data", + "label": "IP Address" + } + ], + "icon": "fa fa-comment", + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-02-24 16:31:16.253153", + "modified_by": "Administrator", + "module": "Core", + "name": "Activity Log", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "search_fields": "subject", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "subject", + "track_seen": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/activity_log/activity_log.py b/xhiveframework/core/doctype/activity_log/activity_log.py new file mode 100644 index 0000000..b8ca93a --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/activity_log.py @@ -0,0 +1,81 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.core.utils import set_timeline_doc +from xhiveframework.model.document import Document +from xhiveframework.query_builder import DocType, Interval +from xhiveframework.query_builder.functions import Now +from xhiveframework.utils import get_fullname, now + + +class ActivityLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + communication_date: DF.Datetime | None + content: DF.TextEditor | None + full_name: DF.Data | None + ip_address: DF.Data | None + link_doctype: DF.Link | None + link_name: DF.DynamicLink | None + operation: DF.Literal["", "Login", "Logout", "Impersonate"] + reference_doctype: DF.Link | None + reference_name: DF.DynamicLink | None + reference_owner: DF.ReadOnly | None + status: DF.Literal["", "Success", "Failed", "Linked", "Closed"] + subject: DF.SmallText + timeline_doctype: DF.Link | None + timeline_name: DF.DynamicLink | None + user: DF.Link | None + + # end: auto-generated types + def before_insert(self): + self.full_name = get_fullname(self.user) + self.date = now() + + def validate(self): + self.set_status() + set_timeline_doc(self) + self.set_ip_address() + + def set_status(self): + if not self.is_new(): + return + + if self.reference_doctype and self.reference_name: + self.status = "Linked" + + def set_ip_address(self): + if self.operation in ("Login", "Logout"): + self.ip_address = xhiveframework.local.request_ip + + @staticmethod + def clear_old_logs(days=None): + if not days: + days = 90 + doctype = DocType("Activity Log") + xhiveframework.db.delete(doctype, filters=(doctype.modified < (Now() - Interval(days=days)))) + + +def on_doctype_update(): + """Add indexes in `tabActivity Log`""" + xhiveframework.db.add_index("Activity Log", ["reference_doctype", "reference_name"]) + xhiveframework.db.add_index("Activity Log", ["timeline_doctype", "timeline_name"]) + + +def add_authentication_log(subject, user, operation="Login", status="Success"): + xhiveframework.get_doc( + { + "doctype": "Activity Log", + "user": user, + "status": status, + "subject": subject, + "operation": operation, + } + ).insert(ignore_permissions=True, ignore_links=True) diff --git a/xhiveframework/core/doctype/activity_log/activity_log_list.js b/xhiveframework/core/doctype/activity_log/activity_log_list.js new file mode 100644 index 0000000..051adf2 --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/activity_log_list.js @@ -0,0 +1,12 @@ +xhiveframework.listview_settings["Activity Log"] = { + get_indicator: function (doc) { + if (doc.operation == "Login" && doc.status == "Success") return [__(doc.status), "green"]; + else if (doc.operation == "Login" && doc.status == "Failed") + return [__(doc.status), "red"]; + }, + onload: function (listview) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(cur_list.doctype); + }); + }, +}; diff --git a/xhiveframework/core/doctype/activity_log/feed.py b/xhiveframework/core/doctype/activity_log/feed.py new file mode 100644 index 0000000..2a288b0 --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/feed.py @@ -0,0 +1,20 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +import xhiveframework.permissions +from xhiveframework import _ +from xhiveframework.core.doctype.activity_log.activity_log import add_authentication_log +from xhiveframework.utils import get_fullname + + +def login_feed(login_manager): + if login_manager.user != "Guest": + subject = _("{0} logged in").format(get_fullname(login_manager.user)) + add_authentication_log(subject, login_manager.user) + + +def logout_feed(user, reason): + if user and user != "Guest": + subject = _("{0} logged out: {1}").format(get_fullname(user), xhiveframework.bold(reason)) + add_authentication_log(subject, user, operation="Logout") diff --git a/xhiveframework/core/doctype/activity_log/test_activity_log.py b/xhiveframework/core/doctype/activity_log/test_activity_log.py new file mode 100644 index 0000000..d7ad4ee --- /dev/null +++ b/xhiveframework/core/doctype/activity_log/test_activity_log.py @@ -0,0 +1,95 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import time + +import xhiveframework +from xhiveframework.auth import CookieManager, LoginManager +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestActivityLog(XhiveFrameworkTestCase): + def test_activity_log(self): + # test user login log + xhiveframework.local.form_dict = xhiveframework._dict( + { + "cmd": "login", + "sid": "Guest", + "pwd": xhiveframework.conf.admin_password or "admin", + "usr": "Administrator", + } + ) + + xhiveframework.local.request_ip = "127.0.0.1" + xhiveframework.local.cookie_manager = CookieManager() + xhiveframework.local.login_manager = LoginManager() + + auth_log = self.get_auth_log() + self.assertFalse(xhiveframework.form_dict.pwd) + self.assertEqual(auth_log.status, "Success") + + # test user logout log + xhiveframework.local.login_manager.logout() + auth_log = self.get_auth_log(operation="Logout") + self.assertEqual(auth_log.status, "Success") + + # test invalid login + xhiveframework.form_dict.update({"pwd": "password"}) + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + auth_log = self.get_auth_log() + self.assertEqual(auth_log.status, "Failed") + + xhiveframework.local.form_dict = xhiveframework._dict() + + def get_auth_log(self, operation="Login"): + names = xhiveframework.get_all( + "Activity Log", + filters={ + "user": "Administrator", + "operation": operation, + }, + order_by="`creation` DESC", + ) + + name = names[0] + return xhiveframework.get_doc("Activity Log", name) + + def test_brute_security(self): + update_system_settings({"allow_consecutive_login_attempts": 3, "allow_login_after_fail": 5}) + + xhiveframework.local.form_dict = xhiveframework._dict( + {"cmd": "login", "sid": "Guest", "pwd": "admin", "usr": "Administrator"} + ) + + xhiveframework.local.request_ip = "127.0.0.1" + xhiveframework.local.cookie_manager = CookieManager() + xhiveframework.local.login_manager = LoginManager() + + auth_log = self.get_auth_log() + self.assertEqual(auth_log.status, "Success") + + # test user logout log + xhiveframework.local.login_manager.logout() + auth_log = self.get_auth_log(operation="Logout") + self.assertEqual(auth_log.status, "Success") + + # test invalid login + xhiveframework.form_dict.update({"pwd": "password"}) + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + + # REMOVE ME: current logic allows allow_consecutive_login_attempts+1 attempts + # before raising security exception, remove below line when that is fixed. + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + self.assertRaises(xhiveframework.SecurityException, LoginManager) + time.sleep(5) + self.assertRaises(xhiveframework.AuthenticationError, LoginManager) + + xhiveframework.local.form_dict = xhiveframework._dict() + + +def update_system_settings(args): + doc = xhiveframework.get_doc("System Settings") + doc.update(args) + doc.flags.ignore_mandatory = 1 + doc.save() diff --git a/xhiveframework/core/doctype/amended_document_naming_settings/__init__.py b/xhiveframework/core/doctype/amended_document_naming_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.json b/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.json new file mode 100644 index 0000000..2892cc6 --- /dev/null +++ b/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.json @@ -0,0 +1,44 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2023-06-16 17:57:36.604672", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "action" + ], + "fields": [ + { + "default": "Amend Counter", + "fieldname": "action", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Action", + "options": "Amend Counter\nDefault Naming", + "reqd": 1 + }, + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1, + "unique": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-06-16 18:26:16.247475", + "modified_by": "Administrator", + "module": "Core", + "name": "Amended Document Naming Settings", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.py b/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.py new file mode 100644 index 0000000..59dede5 --- /dev/null +++ b/xhiveframework/core/doctype/amended_document_naming_settings/amended_document_naming_settings.py @@ -0,0 +1,23 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class AmendedDocumentNamingSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action: DF.Literal["Amend Counter", "Default Naming"] + document_type: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/audit_trail/__init__.py b/xhiveframework/core/doctype/audit_trail/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/audit_trail/audit_trail.html b/xhiveframework/core/doctype/audit_trail/audit_trail.html new file mode 100644 index 0000000..74ed663 --- /dev/null +++ b/xhiveframework/core/doctype/audit_trail/audit_trail.html @@ -0,0 +1,77 @@ +
+ +
+ {% if documents.length > 1 %} +

Documents to Compare : {{ documents.length }}

+ {% else %} +

Documents to Compare : {{ documents.length - 1 }}

+ {% endif %} +
+ + {% var field_keys = Object.keys(changed).sort(); %} + {% if field_keys.length > 0 %} +
+
Fields Changed
+ + + + {% for doc in documents %} + + {% endfor %} + + + {% for fieldname in field_keys %} + + + {% var values = changed[fieldname] %} + + {% for value in values %} + + {% endfor %} + + {% endfor %} + +
Fields {{ doc }}
{{ fieldname }} {{ value }}
+
+ {% endif %} + + {% var tables = Object.keys(row_changed).sort(); %} + {% if tables.length > 0 %} +
+
Rows Updated
+ + + + + + {% for table in tables %} + + + + {% for doc in documents %} + + {% endfor %} + + + {% var rows = Object.keys(row_changed[table]).sort(); %} + {% for idx in rows %} + + {% var fields = Object.keys(row_changed[table][idx]).sort(); %} + {% for field in fields %} + + + {% var values = row_changed[table][idx][field] %} + {% for value in values %} + + {% endfor %} + + {% endfor %} + {% endfor %} + + {% endfor %} + +
Fields
{{ table }}{{ doc }}
idx : {{ idx }}
{{ field }} {{ value }}
+
+ {% endif %} + +
\ No newline at end of file diff --git a/xhiveframework/core/doctype/audit_trail/audit_trail.js b/xhiveframework/core/doctype/audit_trail/audit_trail.js new file mode 100644 index 0000000..7e2dfec --- /dev/null +++ b/xhiveframework/core/doctype/audit_trail/audit_trail.js @@ -0,0 +1,67 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Audit Trail", { + refresh(frm) { + frm.page.clear_indicator(); + + frm.disable_save(); + + frm.set_query("doctype_name", () => { + return { + filters: { + track_changes: 1, + is_submittable: 1, + }, + }; + }); + + frm.page.set_primary_action("Compare", () => { + frm.call({ + doc: frm.doc, + method: "compare_document", + callback: function (r) { + let document_names = r.message[0]; + let changed_fields = r.message[1]; + frm.events.render_changed_fields(frm, document_names, changed_fields); + frm.events.render_rows_added_or_removed(frm, changed_fields); + }, + }); + }); + }, + + render_changed_fields(frm, document_names, changed_fields) { + let render_dict = { + documents: document_names, + changed: changed_fields.changed, + row_changed: changed_fields.row_changed, + }; + $(xhiveframework.render_template("audit_trail", render_dict)).appendTo( + frm.fields_dict.version_table.$wrapper.empty() + ); + frm.set_df_property("version_table", "hidden", 0); + }, + + render_rows_added_or_removed(frm, changed_fields) { + let added_or_removed = { + rows_added: changed_fields.added, + rows_removed: changed_fields.removed, + }; + + let hide_section = 0; + let section_dict = {}; + + for (let key in added_or_removed) { + hide_section = 0; + section_dict = { + added_or_removed: added_or_removed[key], + }; + $(xhiveframework.render_template("audit_trail_rows_added_removed", section_dict)).appendTo( + frm.fields_dict[key].$wrapper.empty() + ); + + if (!frm.fields_dict[key].disp_area.innerHTML.includes(" + {% var docs = Object.keys(added_or_removed) %} + {% for doc in docs %} +
+ {% if Object.keys(added_or_removed[doc]).length > 0 %} +
{{ doc }}
+
+ {% var tables = Object.keys(added_or_removed[doc]) %} + {% for table in tables %} +
{{ table }}
+ + + {% var fieldnames = Object.keys(added_or_removed[doc][table][0]) %} + {% for fieldname in fieldnames %} + + {% endfor %} + + + {% var rows = Object.keys(added_or_removed[doc][table]) %} + {% for row in rows %} + + {% var field_keys = Object.keys(added_or_removed[doc][table][row]) %} + {% for key in field_keys %} + + {% endfor %} + + {% endfor %} + +
{{ fieldname }}
{{ added_or_removed[doc][table][row][key] }}
+ {% endfor %} + {% endif %} +
+ {% endfor %} + \ No newline at end of file diff --git a/xhiveframework/core/doctype/audit_trail/test_audit_trail.py b/xhiveframework/core/doctype/audit_trail/test_audit_trail.py new file mode 100644 index 0000000..287733a --- /dev/null +++ b/xhiveframework/core/doctype/audit_trail/test_audit_trail.py @@ -0,0 +1,134 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestAuditTrail(XhiveFrameworkTestCase): + def setUp(self): + self.child_doctype = create_custom_child_doctype() + self.custom_doctype = create_custom_doctype() + + def test_compare_changed_fields(self): + doc = xhiveframework.new_doc("Test Custom Doctype for Doc Comparator") + doc.test_field = "first value" + doc.submit() + doc.cancel() + + changed_fields = xhiveframework._dict(test_field="second value") + amended_doc = amend_document(doc, changed_fields, {}, 1) + amended_doc.cancel() + + changed_fields = xhiveframework._dict(test_field="third value") + re_amended_doc = amend_document(amended_doc, changed_fields, {}, 1) + + comparator = create_comparator_doc("Test Custom Doctype for Doc Comparator", re_amended_doc.name) + documents, results = comparator.compare_document() + + test_field_values = results["changed"]["Field"] + self.check_expected_values(test_field_values, ["first value", "second value", "third value"]) + + def test_compare_rows(self): + doc = xhiveframework.new_doc("Test Custom Doctype for Doc Comparator") + doc.append("child_table_field", {"test_table_field": "old row 1 value"}) + doc.submit() + doc.cancel() + + child_table_new = [{"test_table_field": "new row 1 value"}, {"test_table_field": "row 2 value"}] + rows_updated = xhiveframework._dict(child_table_field=child_table_new) + amended_doc = amend_document(doc, {}, rows_updated, 1) + + comparator = create_comparator_doc("Test Custom Doctype for Doc Comparator", amended_doc.name) + documents, results = comparator.compare_document() + + results = xhiveframework._dict(results) + self.check_rows_updated(results.row_changed) + self.check_rows_added(results.added[amended_doc.name]) + + def check_rows_updated(self, row_changed): + self.assertIn("Child Table Field", row_changed) + self.assertIn(0, row_changed["Child Table Field"]) + self.assertIn("Table Field", row_changed["Child Table Field"][0]) + table_field_values = row_changed["Child Table Field"][0]["Table Field"] + self.check_expected_values(table_field_values, ["old row 1 value", "new row 1 value"]) + + def check_rows_added(self, rows_added): + self.assertIn("Child Table Field", rows_added) + child_table = rows_added["Child Table Field"] + self.assertIn("Table Field", child_table[0]) + self.check_expected_values(child_table[0]["Table Field"], "row 2 value") + + def check_expected_values(self, values_to_check, expected_values): + for i in range(len(values_to_check)): + self.assertEqual(values_to_check[i], expected_values[i]) + + def tearDown(self): + self.child_doctype.delete() + self.custom_doctype.delete() + + +def create_custom_child_doctype(): + child_doctype = xhiveframework.get_doc( + { + "doctype": "DocType", + "module": "Core", + "name": "Test Custom Child for Doc Comparator", + "custom": 1, + "istable": 1, + "fields": [ + { + "label": "Table Field", + "fieldname": "test_table_field", + "fieldtype": "Data", + "in_list_view": 1, + }, + ], + } + ).insert(ignore_if_duplicate=True) + return child_doctype + + +def create_custom_doctype(): + custom_doctype = xhiveframework.get_doc( + { + "doctype": "DocType", + "module": "Core", + "name": "Test Custom Doctype for Doc Comparator", + "custom": 1, + "is_submittable": 1, + "fields": [ + { + "label": "Field", + "fieldname": "test_field", + "fieldtype": "Data", + }, + { + "label": "Child Table Field", + "fieldname": "child_table_field", + "fieldtype": "Table", + "options": "Test Custom Child for Doc Comparator", + }, + ], + "permissions": [{"role": "System Manager", "read": 1}], + } + ).insert(ignore_if_duplicate=True) + return custom_doctype + + +def amend_document(amend_from, changed_fields, rows_updated, submit=False): + amended_doc = xhiveframework.copy_doc(amend_from) + amended_doc.amended_from = amend_from.name + amended_doc.update(changed_fields) + for child_table in rows_updated: + amended_doc.set(child_table, rows_updated[child_table]) + if submit: + amended_doc.submit() + return amended_doc + + +def create_comparator_doc(doctype_name, document): + comparator = xhiveframework.new_doc("Audit Trail") + comparator.doctype_name = doctype_name + comparator.document = document + return comparator diff --git a/xhiveframework/core/doctype/block_module/__init__.py b/xhiveframework/core/doctype/block_module/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/block_module/block_module.json b/xhiveframework/core/doctype/block_module/block_module.json new file mode 100644 index 0000000..9711aaa --- /dev/null +++ b/xhiveframework/core/doctype/block_module/block_module.json @@ -0,0 +1,31 @@ +{ + "actions": [], + "creation": "2015-03-24 14:28:15.882903", + "doctype": "DocType", + "document_type": "Other", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "module" + ], + "fields": [ + { + "fieldname": "module", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Module", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:52.738977", + "modified_by": "Administrator", + "module": "Core", + "name": "Block Module", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/block_module/block_module.py b/xhiveframework/core/doctype/block_module/block_module.py new file mode 100644 index 0000000..a6b55ec --- /dev/null +++ b/xhiveframework/core/doctype/block_module/block_module.py @@ -0,0 +1,21 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class BlockModule(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + module: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/comment/__init__.py b/xhiveframework/core/doctype/comment/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/comment/comment.js b/xhiveframework/core/doctype/comment/comment.js new file mode 100644 index 0000000..b8ff0cb --- /dev/null +++ b/xhiveframework/core/doctype/comment/comment.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Comment", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/comment/comment.json b/xhiveframework/core/doctype/comment/comment.json new file mode 100644 index 0000000..529c1d2 --- /dev/null +++ b/xhiveframework/core/doctype/comment/comment.json @@ -0,0 +1,139 @@ +{ + "actions": [], + "creation": "2019-02-07 10:10:46.845678", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "comment_type", + "comment_email", + "subject", + "comment_by", + "published", + "seen", + "column_break_5", + "reference_doctype", + "reference_name", + "reference_owner", + "section_break_10", + "content", + "ip_address" + ], + "fields": [ + { + "default": "Comment", + "fieldname": "comment_type", + "fieldtype": "Select", + "label": "Comment Type", + "options": "Comment\nLike\nInfo\nLabel\nWorkflow\nCreated\nSubmitted\nCancelled\nUpdated\nDeleted\nAssigned\nAssignment Completed\nAttachment\nAttachment Removed\nShared\nUnshared\nBot\nRelinked\nEdit", + "reqd": 1 + }, + { + "fieldname": "comment_email", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Comment Email" + }, + { + "fieldname": "subject", + "fieldtype": "Text", + "label": "Subject" + }, + { + "fieldname": "comment_by", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Comment By" + }, + { + "default": "0", + "fieldname": "published", + "fieldtype": "Check", + "label": "Published" + }, + { + "default": "0", + "fieldname": "seen", + "fieldtype": "Check", + "label": "Seen" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType" + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "label": "Reference Name", + "options": "reference_doctype" + }, + { + "fieldname": "reference_owner", + "fieldtype": "Data", + "label": "Reference Owner", + "read_only": 1 + }, + { + "fieldname": "section_break_10", + "fieldtype": "Section Break" + }, + { + "fieldname": "content", + "fieldtype": "HTML Editor", + "ignore_xss_filter": 1, + "in_list_view": 1, + "label": "Content" + }, + { + "fieldname": "ip_address", + "fieldtype": "Data", + "hidden": 1, + "label": "IP Address" + } + ], + "links": [], + "modified": "2023-07-24 13:52:55.651462", + "modified_by": "Administrator", + "module": "Core", + "name": "Comment", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Website Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "comment_type", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/comment/comment.py b/xhiveframework/core/doctype/comment/comment.py new file mode 100644 index 0000000..dc75f0a --- /dev/null +++ b/xhiveframework/core/doctype/comment/comment.py @@ -0,0 +1,207 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +import json + +import xhiveframework +from xhiveframework.database.schema import add_column +from xhiveframework.desk.notifications import notify_mentions +from xhiveframework.exceptions import ImplicitCommitError +from xhiveframework.model.document import Document +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.website.utils import clear_cache + + +class Comment(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + comment_by: DF.Data | None + comment_email: DF.Data | None + comment_type: DF.Literal[ + "Comment", + "Like", + "Info", + "Label", + "Workflow", + "Created", + "Submitted", + "Cancelled", + "Updated", + "Deleted", + "Assigned", + "Assignment Completed", + "Attachment", + "Attachment Removed", + "Shared", + "Unshared", + "Bot", + "Relinked", + "Edit", + ] + content: DF.HTMLEditor | None + ip_address: DF.Data | None + published: DF.Check + reference_doctype: DF.Link | None + reference_name: DF.DynamicLink | None + reference_owner: DF.Data | None + seen: DF.Check + subject: DF.Text | None + + # end: auto-generated types + def after_insert(self): + notify_mentions(self.reference_doctype, self.reference_name, self.content) + self.notify_change("add") + + def validate(self): + if not self.comment_email: + self.comment_email = xhiveframework.session.user + self.content = xhiveframework.utils.sanitize_html(self.content) + + def on_update(self): + update_comment_in_doc(self) + if self.is_new(): + self.notify_change("update") + + def on_trash(self): + self.remove_comment_from_cache() + self.notify_change("delete") + + def notify_change(self, action): + key_map = { + "Like": "like_logs", + "Assigned": "assignment_logs", + "Assignment Completed": "assignment_logs", + "Comment": "comments", + "Attachment": "attachment_logs", + "Attachment Removed": "attachment_logs", + } + key = key_map.get(self.comment_type) + if not key: + return + + xhiveframework.publish_realtime( + "docinfo_update", + {"doc": self.as_dict(), "key": key, "action": action}, + doctype=self.reference_doctype, + docname=self.reference_name, + after_commit=True, + ) + + def remove_comment_from_cache(self): + _comments = get_comments_from_parent(self) + for c in list(_comments): + if c.get("name") == self.name: + _comments.remove(c) + + update_comments_in_parent(self.reference_doctype, self.reference_name, _comments) + + +def on_doctype_update(): + xhiveframework.db.add_index("Comment", ["reference_doctype", "reference_name"]) + + +def update_comment_in_doc(doc): + """Updates `_comments` (JSON) property in parent Document. + Creates a column `_comments` if property does not exist. + + Only user created Communication or Comment of type Comment are saved. + + `_comments` format + + { + "comment": [String], + "by": [user], + "name": [Comment Document name] + }""" + + # only comments get updates, not likes, assignments etc. + if doc.doctype == "Comment" and doc.comment_type != "Comment": + return + + def get_truncated(content): + return (content[:97] + "...") if len(content) > 100 else content + + if doc.reference_doctype and doc.reference_name and doc.content: + _comments = get_comments_from_parent(doc) + + updated = False + for c in _comments: + if c.get("name") == doc.name: + c["comment"] = get_truncated(doc.content) + updated = True + + if not updated: + _comments.append( + { + "comment": get_truncated(doc.content), + # "comment_email" for Comment and "sender" for Communication + "by": getattr(doc, "comment_email", None) or getattr(doc, "sender", None) or doc.owner, + "name": doc.name, + } + ) + + update_comments_in_parent(doc.reference_doctype, doc.reference_name, _comments) + + +def get_comments_from_parent(doc): + """ + get the list of comments cached in the document record in the column + `_comments` + """ + try: + if is_virtual_doctype(doc.reference_doctype): + _comments = "[]" + else: + _comments = xhiveframework.db.get_value(doc.reference_doctype, doc.reference_name, "_comments") or "[]" + + except Exception as e: + if xhiveframework.db.is_missing_table_or_column(e): + _comments = "[]" + + else: + raise + + try: + return json.loads(_comments) + except ValueError: + return [] + + +def update_comments_in_parent(reference_doctype, reference_name, _comments): + """Updates `_comments` property in parent Document with given dict. + + :param _comments: Dict of comments.""" + if ( + not reference_doctype + or not reference_name + or xhiveframework.db.get_value("DocType", reference_doctype, "issingle") + or is_virtual_doctype(reference_doctype) + ): + return + + try: + # use sql, so that we do not mess with the timestamp + xhiveframework.db.sql( + f"""update `tab{reference_doctype}` set `_comments`=%s where name=%s""", # nosec + (json.dumps(_comments[-100:]), reference_name), + ) + + except Exception as e: + if xhiveframework.db.is_column_missing(e) and getattr(xhiveframework.local, "request", None): + pass + elif xhiveframework.db.is_data_too_long(e): + raise xhiveframework.DataTooLongException + else: + raise + else: + if xhiveframework.flags.in_patch: + return + + # Clear route cache + if route := xhiveframework.get_cached_value(reference_doctype, reference_name, "route"): + clear_cache(route) diff --git a/xhiveframework/core/doctype/comment/test_comment.py b/xhiveframework/core/doctype/comment/test_comment.py new file mode 100644 index 0000000..2aa6194 --- /dev/null +++ b/xhiveframework/core/doctype/comment/test_comment.py @@ -0,0 +1,120 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import json + +import xhiveframework +from xhiveframework.templates.includes.comments.comments import add_comment +from xhiveframework.tests.test_model_utils import set_user +from xhiveframework.tests.utils import XhiveFrameworkTestCase, change_settings +from xhiveframework.website.doctype.blog_post.test_blog_post import make_test_blog + + +class TestComment(XhiveFrameworkTestCase): + def test_comment_creation(self): + test_doc = xhiveframework.get_doc(dict(doctype="ToDo", description="test")) + test_doc.insert() + comment = test_doc.add_comment("Comment", "test comment") + + test_doc.reload() + + # check if updated in _comments cache + comments = json.loads(test_doc.get("_comments")) + self.assertEqual(comments[0].get("name"), comment.name) + self.assertEqual(comments[0].get("comment"), comment.content) + + # check document creation + comment_1 = xhiveframework.get_all( + "Comment", + fields=["*"], + filters=dict(reference_doctype=test_doc.doctype, reference_name=test_doc.name), + )[0] + + self.assertEqual(comment_1.content, "test comment") + + # test via blog + def test_public_comment(self): + test_blog = make_test_blog() + + xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"}) + add_comment_args = { + "comment": "Good comment with 10 chars", + "comment_email": "test@test.com", + "comment_by": "Good Tester", + "reference_doctype": test_blog.doctype, + "reference_name": test_blog.name, + "route": test_blog.route, + } + add_comment(**add_comment_args) + + self.assertEqual( + xhiveframework.get_all( + "Comment", + fields=["*"], + filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name), + )[0].published, + 1, + ) + + xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"}) + + add_comment_args.update(comment="pleez vizits my site http://mysite.com", comment_by="bad commentor") + add_comment(**add_comment_args) + + self.assertEqual( + len( + xhiveframework.get_all( + "Comment", + fields=["*"], + filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name), + ) + ), + 0, + ) + + # test for filtering html and css injection elements + xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"}) + + add_comment_args.update(comment="Comment", comment_by="hacker") + add_comment(**add_comment_args) + self.assertEqual( + xhiveframework.get_all( + "Comment", + fields=["content"], + filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name), + )[0]["content"], + "Comment", + ) + + test_blog.delete() + + @change_settings("Blog Settings", {"allow_guest_to_comment": 0}) + def test_guest_cannot_comment(self): + test_blog = make_test_blog() + with set_user("Guest"): + self.assertEqual( + add_comment( + comment="Good comment with 10 chars", + comment_email="mail@example.org", + comment_by="Good Tester", + reference_doctype="Blog Post", + reference_name=test_blog.name, + route=test_blog.route, + ), + None, + ) + + def test_user_not_logged_in(self): + some_system_user = xhiveframework.db.get_value("User", {"name": ("not in", xhiveframework.STANDARD_USERS)}) + + test_blog = make_test_blog() + with set_user("Guest"): + self.assertRaises( + xhiveframework.ValidationError, + add_comment, + comment="Good comment with 10 chars", + comment_email=some_system_user, + comment_by="Good Tester", + reference_doctype="Blog Post", + reference_name=test_blog.name, + route=test_blog.route, + ) diff --git a/xhiveframework/core/doctype/communication/README.md b/xhiveframework/core/doctype/communication/README.md new file mode 100644 index 0000000..8ae1d4a --- /dev/null +++ b/xhiveframework/core/doctype/communication/README.md @@ -0,0 +1 @@ +Email or message sent or received from a contact and other DocTypes. `receive.py` will create a communication when a mail is received in the mailbox. \ No newline at end of file diff --git a/xhiveframework/core/doctype/communication/__init__.py b/xhiveframework/core/doctype/communication/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/communication/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/communication/communication.js b/xhiveframework/core/doctype/communication/communication.js new file mode 100644 index 0000000..65fc5fd --- /dev/null +++ b/xhiveframework/core/doctype/communication/communication.js @@ -0,0 +1,359 @@ +xhiveframework.ui.form.on("Communication", { + onload: function (frm) { + if (frm.doc.content) { + frm.doc.content = xhiveframework.dom.remove_script_and_style(frm.doc.content); + } + frm.set_query("reference_doctype", function () { + return { + filters: { + issingle: 0, + istable: 0, + }, + }; + }); + }, + refresh: function (frm) { + if (frm.is_new()) return; + + frm.convert_to_click && frm.set_convert_button(); + frm.subject_field = "subject"; + + // content field contains weird table html that does not render well in Quill + // this field is not to be edited directly anyway, so setting it as read only + frm.set_df_property("content", "read_only", 1); + + if (frm.doc.reference_doctype && frm.doc.reference_name) { + frm.add_custom_button(__(frm.doc.reference_name), function () { + xhiveframework.set_route("Form", frm.doc.reference_doctype, frm.doc.reference_name); + }); + } else { + // if an unlinked communication, set email field + if (frm.doc.sent_or_received === "Received") { + frm.email_field = "sender"; + } else { + frm.email_field = "recipients"; + } + } + + if (frm.doc.status === "Open") { + frm.add_custom_button(__("Close"), function () { + frm.trigger("mark_as_closed_open"); + }); + } else if (frm.doc.status !== "Linked") { + frm.add_custom_button(__("Reopen"), function () { + frm.trigger("mark_as_closed_open"); + }); + } + + frm.add_custom_button(__("Relink"), function () { + frm.trigger("show_relink_dialog"); + }); + + if ( + frm.doc.communication_type == "Communication" && + frm.doc.communication_medium == "Email" && + frm.doc.sent_or_received == "Received" + ) { + frm.add_custom_button(__("Reply"), function () { + frm.trigger("reply"); + }); + + frm.add_custom_button( + __("Reply All"), + function () { + frm.trigger("reply_all"); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Forward"), + function () { + frm.trigger("forward_mail"); + }, + __("Actions") + ); + + frm.add_custom_button( + frm.doc.seen ? __("Mark as Unread") : __("Mark as Read"), + function () { + frm.trigger("mark_as_read_unread"); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Move"), + function () { + frm.trigger("show_move_dialog"); + }, + __("Actions") + ); + + if (frm.doc.email_status != "Spam") + frm.add_custom_button( + __("Mark as Spam"), + function () { + frm.trigger("mark_as_spam"); + }, + __("Actions") + ); + + if (frm.doc.email_status != "Trash") { + frm.add_custom_button( + __("Move To Trash"), + function () { + frm.trigger("move_to_trash"); + }, + __("Actions") + ); + } + + frm.add_custom_button( + __("Contact"), + function () { + frm.trigger("add_to_contact"); + }, + __("Create") + ); + } + + if ( + frm.doc.communication_type == "Communication" && + frm.doc.communication_medium == "Phone" && + frm.doc.sent_or_received == "Received" + ) { + frm.add_custom_button( + __("Add Contact"), + function () { + frm.trigger("add_to_contact"); + }, + __("Actions") + ); + } + }, + + show_relink_dialog: function (frm) { + var d = new xhiveframework.ui.Dialog({ + title: __("Relink Communication"), + fields: [ + { + fieldtype: "Link", + options: "DocType", + label: __("Reference Doctype"), + fieldname: "reference_doctype", + get_query: function () { + return { query: "xhiveframework.email.get_communication_doctype" }; + }, + }, + { + fieldtype: "Dynamic Link", + options: "reference_doctype", + label: __("Reference Name"), + fieldname: "reference_name", + }, + ], + }); + d.set_value("reference_doctype", frm.doc.reference_doctype); + d.set_value("reference_name", frm.doc.reference_name); + d.set_primary_action(__("Relink"), function () { + var values = d.get_values(); + if (values) { + xhiveframework.confirm( + __("Are you sure you want to relink this communication to {0}?", [ + values["reference_name"], + ]), + function () { + d.hide(); + xhiveframework.call({ + method: "xhiveframework.email.relink", + args: { + name: frm.doc.name, + reference_doctype: values["reference_doctype"], + reference_name: values["reference_name"], + }, + callback: function () { + frm.refresh(); + }, + }); + }, + function () { + xhiveframework.show_alert({ + message: __("Document not Relinked"), + indicator: "info", + }); + } + ); + } + }); + d.show(); + }, + + show_move_dialog: function (frm) { + var d = new xhiveframework.ui.Dialog({ + title: __("Move"), + fields: [ + { + fieldtype: "Link", + options: "Email Account", + label: __("Email Account"), + fieldname: "email_account", + reqd: 1, + get_query: function () { + return { + filters: { + name: ["!=", frm.doc.email_account], + enable_incoming: ["=", 1], + }, + }; + }, + }, + ], + primary_action_label: __("Move"), + primary_action(values) { + d.hide(); + xhiveframework.call({ + method: "xhiveframework.email.inbox.move_email", + args: { + communication: frm.doc.name, + email_account: values.email_account, + }, + freeze: true, + callback: function () { + window.history.back(); + }, + }); + }, + }); + d.show(); + }, + + mark_as_read_unread: function (frm) { + var action = frm.doc.seen ? "Unread" : "Read"; + var flag = "(\\SEEN)"; + + return xhiveframework.call({ + method: "xhiveframework.email.inbox.create_email_flag_queue", + args: { + names: [frm.doc.name], + action: action, + flag: flag, + }, + freeze: true, + callback: function () { + frm.reload_doc(); + }, + }); + }, + + mark_as_closed_open: function (frm) { + var status = frm.doc.status == "Open" ? "Closed" : "Open"; + + return xhiveframework.call({ + method: "xhiveframework.email.inbox.mark_as_closed_open", + args: { + communication: frm.doc.name, + status: status, + }, + freeze: true, + callback: function () { + frm.reload_doc(); + }, + }); + }, + + reply: function (frm) { + var args = frm.events.get_mail_args(frm); + $.extend(args, { + subject: __("Re: {0}", [frm.doc.subject]), + recipients: frm.doc.sender, + is_a_reply: true, + }); + + new xhiveframework.views.CommunicationComposer(args); + }, + + reply_all: function (frm) { + var args = frm.events.get_mail_args(frm); + $.extend(args, { + subject: __("Res: {0}", [frm.doc.subject]), + recipients: frm.doc.sender, + cc: frm.doc.cc, + is_a_reply: true, + }); + new xhiveframework.views.CommunicationComposer(args); + }, + + forward_mail: function (frm) { + var args = frm.events.get_mail_args(frm); + $.extend(args, { + forward: true, + subject: __("Fw: {0}", [frm.doc.subject]), + is_a_reply: true, + }); + + new xhiveframework.views.CommunicationComposer(args); + }, + + get_mail_args: function (frm) { + var sender_email_id = ""; + $.each(xhiveframework.boot.email_accounts, function (idx, account) { + if (account.email_account == frm.doc.email_account) { + sender_email_id = account.email_id; + return; + } + }); + + return { + frm: frm, + doc: frm.doc, + last_email: frm.doc, + sender: sender_email_id, + attachments: frm.doc.attachments, + }; + }, + + add_to_contact: function (frm) { + var me = this; + var fullname = frm.doc.sender_full_name || ""; + + var names = fullname.split(" "); + var first_name = names[0]; + var last_name = names.length >= 2 ? names[names.length - 1] : ""; + + xhiveframework.route_options = { + email_id: frm.doc.sender || "", + first_name: first_name, + last_name: last_name, + mobile_no: frm.doc.phone_no || "", + }; + xhiveframework.new_doc("Contact"); + }, + + mark_as_spam: function (frm) { + xhiveframework.call({ + method: "xhiveframework.email.inbox.mark_as_spam", + args: { + communication: frm.doc.name, + sender: frm.doc.sender, + }, + freeze: true, + callback: function (r) { + xhiveframework.msgprint(__("Email has been marked as spam")); + }, + }); + }, + + move_to_trash: function (frm) { + xhiveframework.call({ + method: "xhiveframework.email.inbox.mark_as_trash", + args: { + communication: frm.doc.name, + }, + freeze: true, + callback: function (r) { + xhiveframework.msgprint(__("Email has been moved to trash")); + }, + }); + }, +}); diff --git a/xhiveframework/core/doctype/communication/communication.json b/xhiveframework/core/doctype/communication/communication.json new file mode 100644 index 0000000..3e15774 --- /dev/null +++ b/xhiveframework/core/doctype/communication/communication.json @@ -0,0 +1,468 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2013-01-29 10:47:14", + "default_view": "List", + "description": "Keeps track of all communications", + "doctype": "DocType", + "document_type": "Setup", + "email_append_to": 1, + "engine": "InnoDB", + "field_order": [ + "subject", + "section_break_10", + "communication_medium", + "sender", + "column_break_4", + "recipients", + "cc", + "bcc", + "phone_no", + "delivery_status", + "section_break_8", + "content", + "status_section", + "text_content", + "communication_type", + "comment_type", + "column_break_5", + "status", + "sent_or_received", + "additional_info", + "communication_date", + "read_receipt", + "send_after", + "column_break_14", + "sender_full_name", + "read_by_recipient", + "read_by_recipient_on", + "reference_section", + "reference_doctype", + "reference_name", + "reference_owner", + "email_account", + "in_reply_to", + "user", + "column_break_27", + "email_template", + "unread_notification_sent", + "seen", + "_user_tags", + "timeline_links_sections", + "timeline_links", + "email_inbox", + "message_id", + "uid", + "imap_folder", + "email_status", + "has_attachment", + "feedback_section", + "rating", + "feedback_request" + ], + "fields": [ + { + "fieldname": "subject", + "fieldtype": "Small Text", + "in_global_search": 1, + "label": "Subject", + "reqd": 1 + }, + { + "collapsible": 1, + "fieldname": "section_break_10", + "fieldtype": "Section Break", + "label": "To and CC" + }, + { + "depends_on": "eval:doc.communication_type===\"Communication\"", + "fieldname": "communication_medium", + "fieldtype": "Select", + "label": "Type", + "options": "\nEmail\nChat\nPhone\nSMS\nEvent\nMeeting\nVisit\nOther" + }, + { + "depends_on": "eval:doc.communication_medium===\"Email\"", + "fieldname": "sender", + "fieldtype": "Data", + "in_global_search": 1, + "label": "From", + "length": 255, + "options": "Email" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "recipients", + "fieldtype": "Code", + "label": "To", + "options": "Email" + }, + { + "depends_on": "eval:doc.communication_medium===\"Email\"", + "fieldname": "cc", + "fieldtype": "Code", + "label": "CC", + "options": "Email" + }, + { + "depends_on": "eval:doc.communication_medium===\"Email\"", + "fieldname": "bcc", + "fieldtype": "Code", + "label": "BCC", + "options": "Email" + }, + { + "depends_on": "eval:in_list([\"Phone\",\"SMS\"],doc.communication_medium)", + "fieldname": "phone_no", + "fieldtype": "Data", + "label": "Phone No." + }, + { + "description": "Integrations can use this field to set email delivery status", + "fieldname": "delivery_status", + "fieldtype": "Select", + "hidden": 1, + "label": "Delivery Status", + "options": "\nSent\nBounced\nOpened\nMarked As Spam\nRejected\nDelayed\nSoft-Bounced\nClicked\nRecipient Unsubscribed\nError\nExpired\nSending\nRead\nScheduled" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "content", + "fieldtype": "Text Editor", + "label": "Message", + "width": "400" + }, + { + "collapsible": 1, + "fieldname": "status_section", + "fieldtype": "Section Break", + "label": "Status" + }, + { + "fieldname": "text_content", + "fieldtype": "Code", + "hidden": 1, + "label": "Text Content" + }, + { + "default": "Communication", + "fieldname": "communication_type", + "fieldtype": "Select", + "label": "Communication Type", + "options": "Communication\nComment\nChat\nNotification\nFeedback\nAutomated Message", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "comment_type", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Comment Type", + "options": "\nComment\nLike\nInfo\nLabel\nWorkflow\nCreated\nSubmitted\nCancelled\nUpdated\nDeleted\nAssigned\nAssignment Completed\nAttachment\nAttachment Removed\nShared\nUnshared\nRelinked", + "read_only": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval:doc.communication_type===\"Communication\"", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Open\nReplied\nClosed\nLinked", + "reqd": 1 + }, + { + "depends_on": "eval:doc.communication_type===\"Communication\"", + "fieldname": "sent_or_received", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Sent or Received", + "options": "Sent\nReceived", + "reqd": 1 + }, + { + "collapsible": 1, + "fieldname": "additional_info", + "fieldtype": "Section Break", + "label": "More Information" + }, + { + "default": "Now", + "fieldname": "communication_date", + "fieldtype": "Datetime", + "label": "Date" + }, + { + "default": "0", + "fieldname": "read_receipt", + "fieldtype": "Check", + "label": "Sent Read Receipt", + "read_only": 1 + }, + { + "fieldname": "column_break_14", + "fieldtype": "Column Break" + }, + { + "fieldname": "sender_full_name", + "fieldtype": "Data", + "label": "From Full Name", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "read_by_recipient", + "fieldtype": "Check", + "label": "Read by Recipient", + "read_only": 1 + }, + { + "fieldname": "read_by_recipient_on", + "fieldtype": "Datetime", + "label": "Read by Recipient On", + "read_only": 1 + }, + { + "collapsible": 1, + "fieldname": "reference_section", + "fieldtype": "Section Break", + "label": "Reference" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType" + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "label": "Reference Name", + "options": "reference_doctype" + }, + { + "fetch_from": "reference_name.owner", + "fieldname": "reference_owner", + "fieldtype": "Read Only", + "label": "Reference Owner", + "search_index": 1 + }, + { + "depends_on": "eval:doc.communication_medium===\"Email\"", + "fieldname": "email_account", + "fieldtype": "Link", + "label": "Email Account", + "options": "Email Account", + "read_only": 1 + }, + { + "fieldname": "in_reply_to", + "fieldtype": "Link", + "label": "In Reply To", + "options": "Communication", + "read_only": 1 + }, + { + "default": "__user", + "fieldname": "user", + "fieldtype": "Link", + "ignore_user_permissions": 1, + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "column_break_27", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "unread_notification_sent", + "fieldtype": "Check", + "label": "Unread Notification Sent", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "seen", + "fieldtype": "Check", + "label": "Seen", + "read_only": 1 + }, + { + "fieldname": "_user_tags", + "fieldtype": "Data", + "hidden": 1, + "label": "User Tags", + "no_copy": 1, + "print_hide": 1 + }, + { + "collapsible": 1, + "fieldname": "email_inbox", + "fieldtype": "Section Break", + "label": "Email Inbox", + "permlevel": 1 + }, + { + "fieldname": "message_id", + "fieldtype": "Small Text", + "ignore_xss_filter": 1, + "label": "Message ID", + "length": 995, + "read_only": 1 + }, + { + "collapsible": 1, + "fieldname": "uid", + "fieldtype": "Int", + "hidden": 1, + "label": "UID", + "no_copy": 1 + }, + { + "fieldname": "email_status", + "fieldtype": "Select", + "label": "Email Status", + "options": "Open\nSpam\nTrash" + }, + { + "default": "0", + "fieldname": "has_attachment", + "fieldtype": "Check", + "hidden": 1, + "label": "Has Attachment" + }, + { + "collapsible": 1, + "depends_on": "eval: doc.rating > 0", + "fieldname": "feedback_section", + "fieldtype": "Section Break", + "label": "Feedback" + }, + { + "fieldname": "rating", + "fieldtype": "Int", + "label": "Rating", + "read_only": 1 + }, + { + "fieldname": "feedback_request", + "fieldtype": "Data", + "label": "Feedback Request", + "read_only": 1 + }, + { + "fieldname": "email_template", + "fieldtype": "Link", + "label": "Email Template", + "options": "Email Template", + "read_only": 1 + }, + { + "collapsible": 1, + "fieldname": "timeline_links_sections", + "fieldtype": "Section Break", + "label": "Timeline Links" + }, + { + "fieldname": "timeline_links", + "fieldtype": "Table", + "label": "Timeline Links", + "options": "Communication Link", + "permlevel": 2 + }, + { + "fieldname": "imap_folder", + "fieldtype": "Data", + "hidden": 1, + "label": "IMAP Folder", + "read_only": 1 + }, + { + "fieldname": "send_after", + "fieldtype": "Datetime", + "label": "Send After" + } + ], + "icon": "fa fa-comment", + "idx": 1, + "links": [], + "modified": "2023-11-27 20:38:27.467076", + "modified_by": "Administrator", + "module": "Core", + "name": "Communication", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + }, + { + "delete": 1, + "email": 1, + "export": 1, + "permlevel": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + }, + { + "delete": 1, + "email": 1, + "export": 1, + "permlevel": 2, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "role": "Inbox User" + }, + { + "delete": 1, + "email": 1, + "if_owner": 1, + "read": 1, + "role": "All" + } + ], + "search_fields": "subject", + "sender_field": "sender", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "subject_field": "subject", + "title_field": "subject", + "track_changes": 1, + "track_seen": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/communication/communication.py b/xhiveframework/core/doctype/communication/communication.py new file mode 100644 index 0000000..b3c7946 --- /dev/null +++ b/xhiveframework/core/doctype/communication/communication.py @@ -0,0 +1,702 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from collections import Counter +from email.utils import getaddresses +from urllib.parse import unquote + +from bs4 import BeautifulSoup + +import xhiveframework +from xhiveframework import _ +from xhiveframework.automation.doctype.assignment_rule.assignment_rule import ( + apply as apply_assignment_rule, +) +from xhiveframework.contacts.doctype.contact.contact import get_contact_name +from xhiveframework.core.doctype.comment.comment import update_comment_in_doc +from xhiveframework.core.doctype.communication.email import validate_email +from xhiveframework.core.doctype.communication.mixins import CommunicationEmailMixin +from xhiveframework.core.utils import get_parent_doc +from xhiveframework.model.document import Document +from xhiveframework.utils import ( + cstr, + parse_addr, + split_emails, + strip_html, + time_diff_in_seconds, + validate_email_address, +) +from xhiveframework.utils.user import is_system_user + +exclude_from_linked_with = True + + +class Communication(Document, CommunicationEmailMixin): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.communication_link.communication_link import CommunicationLink + from xhiveframework.types import DF + + _user_tags: DF.Data | None + bcc: DF.Code | None + cc: DF.Code | None + comment_type: DF.Literal[ + "", + "Comment", + "Like", + "Info", + "Label", + "Workflow", + "Created", + "Submitted", + "Cancelled", + "Updated", + "Deleted", + "Assigned", + "Assignment Completed", + "Attachment", + "Attachment Removed", + "Shared", + "Unshared", + "Relinked", + ] + communication_date: DF.Datetime | None + communication_medium: DF.Literal[ + "", "Email", "Chat", "Phone", "SMS", "Event", "Meeting", "Visit", "Other" + ] + communication_type: DF.Literal[ + "Communication", "Comment", "Chat", "Notification", "Feedback", "Automated Message" + ] + content: DF.TextEditor | None + delivery_status: DF.Literal[ + "", + "Sent", + "Bounced", + "Opened", + "Marked As Spam", + "Rejected", + "Delayed", + "Soft-Bounced", + "Clicked", + "Recipient Unsubscribed", + "Error", + "Expired", + "Sending", + "Read", + "Scheduled", + ] + email_account: DF.Link | None + email_status: DF.Literal["Open", "Spam", "Trash"] + email_template: DF.Link | None + feedback_request: DF.Data | None + has_attachment: DF.Check + imap_folder: DF.Data | None + in_reply_to: DF.Link | None + message_id: DF.SmallText | None + phone_no: DF.Data | None + rating: DF.Int + read_by_recipient: DF.Check + read_by_recipient_on: DF.Datetime | None + read_receipt: DF.Check + recipients: DF.Code | None + reference_doctype: DF.Link | None + reference_name: DF.DynamicLink | None + reference_owner: DF.ReadOnly | None + seen: DF.Check + send_after: DF.Datetime | None + sender: DF.Data | None + sender_full_name: DF.Data | None + sent_or_received: DF.Literal["Sent", "Received"] + status: DF.Literal["Open", "Replied", "Closed", "Linked"] + subject: DF.SmallText + text_content: DF.Code | None + timeline_links: DF.Table[CommunicationLink] + uid: DF.Int + unread_notification_sent: DF.Check + user: DF.Link | None + # end: auto-generated types + """Communication represents an external communication like Email.""" + + no_feed_on_delete = True + DOCTYPE = "Communication" + + def onload(self): + """create email flag queue""" + if ( + self.communication_type == "Communication" + and self.communication_medium == "Email" + and self.sent_or_received == "Received" + and self.uid + and self.uid != -1 + ): + email_flag_queue = xhiveframework.db.get_value( + "Email Flag Queue", {"communication": self.name, "is_completed": 0} + ) + if email_flag_queue: + return + + xhiveframework.get_doc( + { + "doctype": "Email Flag Queue", + "action": "Read", + "communication": self.name, + "uid": self.uid, + "email_account": self.email_account, + } + ).insert(ignore_permissions=True) + xhiveframework.db.commit() + + def validate(self): + self.validate_reference() + + if not self.user: + self.user = xhiveframework.session.user + + if not self.subject: + self.subject = strip_html((self.content or "")[:141]) + + if not self.sent_or_received: + self.seen = 1 + self.sent_or_received = "Sent" + + if not self.send_after: # Handle empty string, always set NULL + self.send_after = None + + validate_email(self) + + if self.communication_medium == "Email": + self.parse_email_for_timeline_links() + self.set_timeline_links() + self.deduplicate_timeline_links() + + self.set_sender_full_name() + + if self.is_new(): + self.set_status() + self.mark_email_as_spam() + + def validate_reference(self): + if self.reference_doctype and self.reference_name: + if not self.reference_owner: + self.reference_owner = xhiveframework.db.get_value( + self.reference_doctype, self.reference_name, "owner" + ) + + # prevent communication against a child table + if xhiveframework.get_meta(self.reference_doctype).istable: + xhiveframework.throw( + _("Cannot create a {0} against a child document: {1}").format( + _(self.communication_type), _(self.reference_doctype) + ) + ) + + # Prevent circular linking of Communication DocTypes + if self.reference_doctype == "Communication": + circular_linking = False + doc = get_parent_doc(self) + while doc.reference_doctype == "Communication": + if get_parent_doc(doc).name == self.name: + circular_linking = True + break + doc = get_parent_doc(doc) + + if circular_linking: + xhiveframework.throw( + _("Please make sure the Reference Communication Docs are not circularly linked."), + xhiveframework.CircularLinkingError, + ) + + def after_insert(self): + if not (self.reference_doctype and self.reference_name): + return + + if self.reference_doctype == "Communication" and self.sent_or_received == "Sent": + xhiveframework.db.set_value("Communication", self.reference_name, "status", "Replied") + + if self.communication_type == "Communication": + self.notify_change("add") + + elif self.communication_type in ("Chat", "Notification"): + if self.reference_name == xhiveframework.session.user: + message = self.as_dict() + message["broadcast"] = True + xhiveframework.publish_realtime("new_message", message, after_commit=True) + else: + # reference_name contains the user who is addressed in the messages' page comment + xhiveframework.publish_realtime( + "new_message", self.as_dict(), user=self.reference_name, after_commit=True + ) + + def set_signature_in_email_content(self): + """Set sender's User.email_signature or default outgoing's EmailAccount.signature to the email""" + if not self.content: + return + + soup = BeautifulSoup(self.content, "html.parser") + email_body = soup.find("div", {"class": "ql-editor read-mode"}) + + if not email_body: + return + + user_email_signature = ( + xhiveframework.db.get_value( + "User", + self.sender, + "email_signature", + ) + if self.sender + else None + ) + + signature = user_email_signature or xhiveframework.db.get_value( + "Email Account", + {"default_outgoing": 1, "add_signature": 1}, + "signature", + ) + + if not signature: + return + + soup = BeautifulSoup(signature, "html.parser") + html_signature = soup.find("div", {"class": "ql-editor read-mode"}) + _signature = None + if html_signature: + _signature = html_signature.renderContents() + + if (cstr(_signature) or signature) not in self.content: + self.content = f'{self.content}


{signature}' + + def before_save(self): + if not self.flags.skip_add_signature: + self.set_signature_in_email_content() + + def on_update(self): + # add to _comment property of the doctype, so it shows up in + # comments count for the list view + update_comment_in_doc(self) + + parent = get_parent_doc(self) + if (method := getattr(parent, "on_communication_update", None)) and callable(method): + parent.on_communication_update(self) + return + + if self.comment_type != "Updated": + update_parent_document_on_communication(self) + + def on_trash(self): + if self.communication_type == "Communication": + self.notify_change("delete") + + @property + def sender_mailid(self): + return parse_addr(self.sender)[1] if self.sender else "" + + @staticmethod + def _get_emails_list(emails=None, exclude_displayname=False): + """Returns list of emails from given email string. + + * Removes duplicate mailids + * Removes display name from email address if exclude_displayname is True + """ + emails = split_emails(emails) if isinstance(emails, str) else (emails or []) + if exclude_displayname: + return [email.lower() for email in {parse_addr(email)[1] for email in emails} if email] + return [email for email in set(emails) if email] + + def to_list(self, exclude_displayname=True): + """Returns to list.""" + return self._get_emails_list(self.recipients, exclude_displayname=exclude_displayname) + + def cc_list(self, exclude_displayname=True): + """Returns cc list.""" + return self._get_emails_list(self.cc, exclude_displayname=exclude_displayname) + + def bcc_list(self, exclude_displayname=True): + """Returns bcc list.""" + return self._get_emails_list(self.bcc, exclude_displayname=exclude_displayname) + + def get_attachments(self): + return xhiveframework.get_all( + "File", + fields=["name", "file_name", "file_url", "is_private"], + filters={ + "attached_to_name": self.name, + "attached_to_doctype": self.DOCTYPE, + }, + ) + + def notify_change(self, action): + xhiveframework.publish_realtime( + "docinfo_update", + {"doc": self.as_dict(), "key": "communications", "action": action}, + doctype=self.reference_doctype, + docname=self.reference_name, + after_commit=True, + ) + + def set_status(self): + if self.reference_doctype and self.reference_name: + self.status = "Linked" + elif self.communication_type == "Communication": + self.status = "Open" + else: + self.status = "Closed" + + if self.send_after and self.is_new(): + self.delivery_status = "Scheduled" + + def mark_email_as_spam(self): + if ( + self.communication_type == "Communication" + and self.communication_medium == "Email" + and self.sent_or_received == "Received" + and xhiveframework.db.exists("Email Rule", {"email_id": self.sender, "is_spam": 1}) + ): + self.email_status = "Spam" + + @classmethod + def find(cls, name, ignore_error=False): + try: + return xhiveframework.get_doc(cls.DOCTYPE, name) + except xhiveframework.DoesNotExistError: + if ignore_error: + return + raise + + @classmethod + def find_one_by_filters(cls, *, order_by=None, **kwargs): + name = xhiveframework.db.get_value(cls.DOCTYPE, kwargs, order_by=order_by) + return cls.find(name) if name else None + + def update_db(self, **kwargs): + xhiveframework.db.set_value(self.DOCTYPE, self.name, kwargs) + + def set_sender_full_name(self): + if not self.sender_full_name and self.sender: + if self.sender == "Administrator": + self.sender_full_name = xhiveframework.db.get_value("User", "Administrator", "full_name") + self.sender = xhiveframework.db.get_value("User", "Administrator", "email") + elif self.sender == "Guest": + self.sender_full_name = self.sender + self.sender = None + else: + if self.sent_or_received == "Sent": + validate_email_address(self.sender, throw=True) + sender_name, sender_email = parse_addr(self.sender) + if sender_name == sender_email: + sender_name = None + + self.sender = sender_email + self.sender_full_name = sender_name + + if not self.sender_full_name: + self.sender_full_name = xhiveframework.db.get_value("User", self.sender, "full_name") + + if not self.sender_full_name: + first_name, last_name = xhiveframework.db.get_value( + "Contact", filters={"email_id": sender_email}, fieldname=["first_name", "last_name"] + ) or [None, None] + self.sender_full_name = (first_name or "") + (last_name or "") + + if not self.sender_full_name: + self.sender_full_name = sender_email + + def set_delivery_status(self, commit=False): + """Look into the status of Email Queue linked to this Communication and set the Delivery Status of this Communication""" + delivery_status = None + status_counts = Counter( + xhiveframework.get_all("Email Queue", pluck="status", filters={"communication": self.name}) + ) + if self.sent_or_received == "Received": + return + + if status_counts.get("Not Sent") or status_counts.get("Sending"): + delivery_status = "Sending" + + elif status_counts.get("Error"): + delivery_status = "Error" + + elif status_counts.get("Expired"): + delivery_status = "Expired" + + elif status_counts.get("Sent"): + delivery_status = "Sent" + + if delivery_status: + self.db_set("delivery_status", delivery_status) + self.notify_change("update") + + # for list views and forms + self.notify_update() + + if commit: + xhiveframework.db.commit() + + def parse_email_for_timeline_links(self): + if not xhiveframework.db.get_value("Email Account", filters={"enable_automatic_linking": 1}): + return + + for doctype, docname in parse_email([self.recipients, self.cc, self.bcc]): + if not xhiveframework.db.get_value(doctype, docname, ignore=True): + continue + + self.add_link(doctype, docname) + + if not self.reference_doctype: + self.reference_doctype = doctype + self.reference_name = docname + + # Timeline Links + def set_timeline_links(self): + contacts = [] + create_contact_enabled = self.email_account and xhiveframework.db.get_value( + "Email Account", self.email_account, "create_contact" + ) + contacts = get_contacts( + [self.sender, self.recipients, self.cc, self.bcc], auto_create_contact=create_contact_enabled + ) + + for contact_name in contacts: + self.add_link("Contact", contact_name) + + # link contact's dynamic links to communication + add_contact_links_to_communication(self, contact_name) + + def deduplicate_timeline_links(self): + if not self.timeline_links: + return + + unique_links = {(link.link_doctype, link.link_name) for link in self.timeline_links} + self.timeline_links = [] + for doctype, name in unique_links: + self.add_link(doctype, name) + + def add_link(self, link_doctype, link_name, autosave=False): + self.append("timeline_links", {"link_doctype": link_doctype, "link_name": link_name}) + + if autosave: + self.save(ignore_permissions=True) + + def get_links(self): + return self.timeline_links + + def remove_link(self, link_doctype, link_name, autosave=False, ignore_permissions=True): + for l in list(self.timeline_links): + if l.link_doctype == link_doctype and l.link_name == link_name: + self.timeline_links.remove(l) + + if autosave: + self.save(ignore_permissions=ignore_permissions) + + +def on_doctype_update(): + """Add indexes in `tabCommunication`""" + xhiveframework.db.add_index("Communication", ["reference_doctype", "reference_name"]) + xhiveframework.db.add_index("Communication", ["status", "communication_type"]) + xhiveframework.db.add_index("Communication", ["message_id(140)"]) + + +def has_permission(doc, ptype, user=None, debug=False): + if ptype == "read": + if doc.reference_doctype == "Communication" and doc.reference_name == doc.name: + return + + if doc.reference_doctype and doc.reference_name: + return xhiveframework.has_permission( + doc.reference_doctype, ptype="read", doc=doc.reference_name, user=user, debug=debug + ) + + +def get_permission_query_conditions_for_communication(user): + if not user: + user = xhiveframework.session.user + + roles = xhiveframework.get_roles(user) + + if "Super Email User" in roles or "System Manager" in roles: + return None + else: + accounts = xhiveframework.get_all( + "User Email", filters={"parent": user}, fields=["email_account"], distinct=True, order_by="idx" + ) + + if not accounts: + return """`tabCommunication`.communication_medium!='Email'""" + + email_accounts = ['"%s"' % account.get("email_account") for account in accounts] + return """`tabCommunication`.email_account in ({email_accounts})""".format( + email_accounts=",".join(email_accounts) + ) + + +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) + contact_name = get_contact_name(email) + + if not contact_name and email and auto_create_contact: + email_parts = email.split("@") + first_name = xhiveframework.unscrub(email_parts[0]) + + try: + contact_name = f"{first_name}-{email_parts[1]}" if first_name == "Contact" else first_name + contact = xhiveframework.get_doc( + {"doctype": "Contact", "first_name": contact_name, "name": contact_name} + ) + contact.add_email(email_id=email, is_primary=True) + contact.insert(ignore_permissions=True) + contact_name = contact.name + except Exception: + contact_name = None + contact.log_error("Unable to add contact") + + if contact_name: + contacts.append(contact_name) + + 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]) + email_addrs.extend(email[1] for email in result) + return email_addrs + + +def add_contact_links_to_communication(communication, contact_name): + contact_links = xhiveframework.get_all( + "Dynamic Link", + filters={"parenttype": "Contact", "parent": contact_name}, + fields=["link_doctype", "link_name"], + ) + + if contact_links: + for contact_link in contact_links: + communication.add_link(contact_link.link_doctype, contact_link.link_name) + + +def parse_email(email_strings): + """ + Parse email to add timeline links. + When automatic email linking is enabled, an email from email_strings can contain + a doctype and docname ie in the format `admin+doctype+docname@example.com` or `admin+doctype=docname@example.com`, + the email is parsed and doctype and docname is extracted. + """ + for email_string in email_strings: + if not email_string: + continue + + for email in email_string.split(","): + email_username = email.split("@", 1)[0] + email_local_parts = email_username.split("+") + docname = doctype = None + if len(email_local_parts) == 3: + doctype = unquote(email_local_parts[1]) + docname = unquote(email_local_parts[2]) + + elif len(email_local_parts) == 2: + document_parts = email_local_parts[1].split("=", 1) + if len(document_parts) != 2: + continue + + doctype = unquote(document_parts[0]) + docname = unquote(document_parts[1]) + + if doctype and docname: + yield doctype, docname + + +def get_email_without_link(email): + """ + returns email address without doctype links + returns admin@example.com for email admin+doctype+docname@example.com + """ + if not xhiveframework.get_all("Email Account", filters={"enable_automatic_linking": 1}): + return email + + try: + _email = email.split("@") + email_id = _email[0].split("+", 1)[0] + email_host = _email[1] + except IndexError: + return email + + return f"{email_id}@{email_host}" + + +def update_parent_document_on_communication(doc): + """Update mins_to_first_communication of parent document based on who is replying.""" + + parent = get_parent_doc(doc) + if not parent: + return + + # update parent mins_to_first_communication only if we create the Email communication + # ignore in case of only Comment is added + if doc.communication_type == "Comment": + return + + status_field = parent.meta.get_field("status") + if status_field: + options = (status_field.options or "").splitlines() + + # if status has a "Replied" option, then update the status for received communication + if ("Replied" in options) and doc.sent_or_received == "Received": + parent.db_set("status", "Open") + parent.run_method("handle_hold_time", "Replied") + apply_assignment_rule(parent) + + update_first_response_time(parent, doc) + set_avg_response_time(parent, doc) + parent.run_method("notify_communication", doc) + parent.notify_update() + + +def update_first_response_time(parent, communication): + if parent.meta.has_field("first_response_time") and not parent.get("first_response_time"): + if ( + is_system_user(communication.sender) + or xhiveframework.get_cached_value("User", xhiveframework.session.user, "user_type") == "System User" + ): + if communication.sent_or_received == "Sent": + first_responded_on = communication.creation + if parent.meta.has_field("first_responded_on"): + parent.db_set("first_responded_on", first_responded_on) + first_response_time = round(time_diff_in_seconds(first_responded_on, parent.creation), 2) + parent.db_set("first_response_time", first_response_time) + + +def set_avg_response_time(parent, communication): + if parent.meta.has_field("avg_response_time") and communication.sent_or_received == "Sent": + # avg response time for all the responses + communications = xhiveframework.get_list( + "Communication", + filters={"reference_doctype": parent.doctype, "reference_name": parent.name}, + fields=["sent_or_received", "name", "creation"], + order_by="creation", + ) + + if len(communications): + response_times = [] + for i in range(len(communications)): + if ( + communications[i].sent_or_received == "Sent" + and communications[i - 1].sent_or_received == "Received" + ): + response_time = round( + time_diff_in_seconds(communications[i].creation, communications[i - 1].creation), 2 + ) + if response_time > 0: + response_times.append(response_time) + if response_times: + avg_response_time = sum(response_times) / len(response_times) + parent.db_set("avg_response_time", avg_response_time) diff --git a/xhiveframework/core/doctype/communication/communication_list.js b/xhiveframework/core/doctype/communication/communication_list.js new file mode 100644 index 0000000..9afa412 --- /dev/null +++ b/xhiveframework/core/doctype/communication/communication_list.js @@ -0,0 +1,30 @@ +xhiveframework.listview_settings["Communication"] = { + add_fields: [ + "sent_or_received", + "recipients", + "subject", + "communication_medium", + "communication_type", + "sender", + "seen", + "reference_doctype", + "reference_name", + "has_attachment", + "communication_date", + ], + + onload: function (list_view) { + let method = "xhiveframework.email.inbox.create_email_flag_queue"; + + list_view.page.add_menu_item(__("Mark as Read"), function () { + list_view.call_for_selected_items(method, { action: "Read" }); + }); + list_view.page.add_menu_item(__("Mark as Unread"), function () { + list_view.call_for_selected_items(method, { action: "Unread" }); + }); + }, + + primary_action: function () { + new xhiveframework.views.CommunicationComposer(); + }, +}; diff --git a/xhiveframework/core/doctype/communication/email.py b/xhiveframework/core/doctype/communication/email.py new file mode 100755 index 0000000..bfae47e --- /dev/null +++ b/xhiveframework/core/doctype/communication/email.py @@ -0,0 +1,300 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +from collections.abc import Iterable +from typing import TYPE_CHECKING + +import xhiveframework +import xhiveframework.email.smtp +from xhiveframework import _ +from xhiveframework.email.email_body import get_message_id +from xhiveframework.utils import ( + cint, + get_datetime, + get_formatted_email, + get_imaginary_pixel_response, + get_string_between, + list_to_str, + split_emails, + validate_email_address, +) + +if TYPE_CHECKING: + from xhiveframework.core.doctype.communication.communication import Communication + + +@xhiveframework.whitelist() +def make( + doctype=None, + name=None, + content=None, + subject=None, + sent_or_received="Sent", + sender=None, + sender_full_name=None, + recipients=None, + communication_medium="Email", + send_email=False, + print_html=None, + print_format=None, + attachments=None, + send_me_a_copy=False, + cc=None, + bcc=None, + read_receipt=None, + print_letterhead=True, + email_template=None, + communication_type=None, + send_after=None, + print_language=None, + now=False, + **kwargs, +) -> dict[str, str]: + """Make a new communication. Checks for email permissions for specified Document. + + :param doctype: Reference DocType. + :param name: Reference Document name. + :param content: Communication body. + :param subject: Communication subject. + :param sent_or_received: Sent or Received (default **Sent**). + :param sender: Communcation sender (default current user). + :param recipients: Communication recipients as list. + :param communication_medium: Medium of communication (default **Email**). + :param send_email: Send via email (default **False**). + :param print_html: HTML Print format to be sent as attachment. + :param print_format: Print Format name of parent document to be sent as attachment. + :param attachments: List of File names or dicts with keys "fname" and "fcontent" + :param send_me_a_copy: Send a copy to the sender (default **False**). + :param email_template: Template which is used to compose mail . + :param send_after: Send after the given datetime. + """ + if kwargs: + from xhiveframework.utils.commands import warn + + warn( + f"Options {kwargs} used in xhiveframework.core.doctype.communication.email.make " + "are deprecated or unsupported", + category=DeprecationWarning, + ) + + if doctype and name and not xhiveframework.has_permission(doctype=doctype, ptype="email", doc=name): + raise xhiveframework.PermissionError(f"You are not allowed to send emails related to: {doctype} {name}") + + return _make( + doctype=doctype, + name=name, + content=content, + subject=subject, + sent_or_received=sent_or_received, + sender=sender, + sender_full_name=sender_full_name, + recipients=recipients, + communication_medium=communication_medium, + send_email=send_email, + print_html=print_html, + print_format=print_format, + attachments=attachments, + send_me_a_copy=cint(send_me_a_copy), + cc=cc, + bcc=bcc, + read_receipt=cint(read_receipt), + print_letterhead=print_letterhead, + email_template=email_template, + communication_type=communication_type, + add_signature=False, + send_after=send_after, + print_language=print_language, + now=now, + ) + + +def _make( + doctype=None, + name=None, + content=None, + subject=None, + sent_or_received="Sent", + sender=None, + sender_full_name=None, + recipients=None, + communication_medium="Email", + send_email=False, + print_html=None, + print_format=None, + attachments=None, + send_me_a_copy=False, + cc=None, + bcc=None, + read_receipt=None, + print_letterhead=True, + email_template=None, + communication_type=None, + add_signature=True, + send_after=None, + print_language=None, + now=False, +) -> dict[str, str]: + """Internal method to make a new communication that ignores Permission checks.""" + + sender = sender or get_formatted_email(xhiveframework.session.user) + recipients = list_to_str(recipients) if isinstance(recipients, list) else recipients + cc = list_to_str(cc) if isinstance(cc, list) else cc + bcc = list_to_str(bcc) if isinstance(bcc, list) else bcc + + comm: "Communication" = xhiveframework.get_doc( + { + "doctype": "Communication", + "subject": subject, + "content": content, + "sender": sender, + "sender_full_name": sender_full_name, + "recipients": recipients, + "cc": cc or None, + "bcc": bcc or None, + "communication_medium": communication_medium, + "sent_or_received": sent_or_received, + "reference_doctype": doctype, + "reference_name": name, + "email_template": email_template, + "message_id": get_string_between("<", get_message_id(), ">"), + "read_receipt": read_receipt, + "has_attachment": 1 if attachments else 0, + "communication_type": communication_type, + "send_after": send_after, + } + ) + comm.flags.skip_add_signature = not add_signature + comm.insert(ignore_permissions=True) + + # if not committed, delayed task doesn't find the communication + if attachments: + if isinstance(attachments, str): + attachments = json.loads(attachments) + add_attachments(comm.name, attachments) + + if cint(send_email): + if not comm.get_outgoing_email_account(): + xhiveframework.throw( + _( + "Unable to send mail because of a missing email account. Please setup default Email Account from Settings > Email Account" + ), + exc=xhiveframework.OutgoingEmailError, + ) + + comm.send_email( + print_html=print_html, + print_format=print_format, + send_me_a_copy=send_me_a_copy, + print_letterhead=print_letterhead, + print_language=print_language, + now=now, + ) + + emails_not_sent_to = comm.exclude_emails_list(include_sender=send_me_a_copy) + + return {"name": comm.name, "emails_not_sent_to": ", ".join(emails_not_sent_to)} + + +def validate_email(doc: "Communication") -> None: + """Validate Email Addresses of Recipients and CC""" + if ( + not (doc.communication_type == "Communication" and doc.communication_medium == "Email") + or doc.flags.in_receive + ): + return + + # validate recipients + for email in split_emails(doc.recipients): + validate_email_address(email, throw=True) + + # validate CC + for email in split_emails(doc.cc): + validate_email_address(email, throw=True) + + for email in split_emails(doc.bcc): + validate_email_address(email, throw=True) + + +def set_incoming_outgoing_accounts(doc): + from xhiveframework.email.doctype.email_account.email_account import EmailAccount + + incoming_email_account = EmailAccount.find_incoming( + match_by_email=doc.sender, match_by_doctype=doc.reference_doctype + ) + doc.incoming_email_account = incoming_email_account.email_id if incoming_email_account else None + + doc.outgoing_email_account = EmailAccount.find_outgoing( + match_by_email=doc.sender, match_by_doctype=doc.reference_doctype + ) + + if doc.sent_or_received == "Sent": + doc.db_set("email_account", doc.outgoing_email_account.name) + + +def add_attachments(name: str, attachments: Iterable[str | dict]) -> None: + """Add attachments to the given Communication + + :param name: Communication name + :param attachments: File names or dicts with keys "fname" and "fcontent" + """ + # loop through attachments + for a in attachments: + if isinstance(a, str): + attach = xhiveframework.db.get_value("File", {"name": a}, ["file_url", "is_private"], as_dict=1) + file_args = { + "file_url": attach.file_url, + "is_private": attach.is_private, + } + elif isinstance(a, dict) and "fcontent" in a and "fname" in a: + # dict returned by xhiveframework.attach_print() + file_args = { + "file_name": a["fname"], + "content": a["fcontent"], + "is_private": 1, + } + else: + continue + + file_args.update( + { + "attached_to_doctype": "Communication", + "attached_to_name": name, + "folder": "Home/Attachments", + } + ) + + _file = xhiveframework.new_doc("File") + _file.update(file_args) + _file.save(ignore_permissions=True) + + +@xhiveframework.whitelist(allow_guest=True, methods=("GET",)) +def mark_email_as_seen(name: str | None = None): + xhiveframework.request.after_response.add(lambda: _mark_email_as_seen(name)) + xhiveframework.response.update(xhiveframework.utils.get_imaginary_pixel_response()) + + +def _mark_email_as_seen(name): + try: + update_communication_as_read(name) + except Exception: + xhiveframework.log_error("Unable to mark as seen", None, "Communication", name) + + xhiveframework.db.commit() # nosemgrep: after_response requires explicit commit + + +def update_communication_as_read(name): + if not name or not isinstance(name, str): + return + + communication = xhiveframework.db.get_value("Communication", name, "read_by_recipient", as_dict=True) + + if not communication or communication.read_by_recipient: + return + + xhiveframework.db.set_value( + "Communication", + name, + {"read_by_recipient": 1, "delivery_status": "Read", "read_by_recipient_on": get_datetime()}, + ) diff --git a/xhiveframework/core/doctype/communication/mixins.py b/xhiveframework/core/doctype/communication/mixins.py new file mode 100644 index 0000000..a256213 --- /dev/null +++ b/xhiveframework/core/doctype/communication/mixins.py @@ -0,0 +1,321 @@ +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.utils import get_parent_doc +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + is_email_notifications_enabled_for_type, +) +from xhiveframework.desk.doctype.todo.todo import ToDo +from xhiveframework.email.doctype.email_account.email_account import EmailAccount +from xhiveframework.utils import get_formatted_email, get_url, parse_addr + + +class CommunicationEmailMixin: + """Mixin class to handle communication mails.""" + + def is_email_communication(self): + return self.communication_type == "Communication" and self.communication_medium == "Email" + + def get_owner(self): + """Get owner of the communication docs parent.""" + parent_doc = get_parent_doc(self) + return parent_doc.owner if parent_doc else None + + def get_all_email_addresses(self, exclude_displayname=False): + """Get all Email addresses mentioned in the doc along with display name.""" + return ( + self.to_list(exclude_displayname=exclude_displayname) + + self.cc_list(exclude_displayname=exclude_displayname) + + self.bcc_list(exclude_displayname=exclude_displayname) + ) + + def get_email_with_displayname(self, email_address): + """Returns email address after adding displayname.""" + display_name, email = parse_addr(email_address) + if display_name and display_name != email: + return email_address + + # emailid to emailid with display name map. + email_map = {parse_addr(email)[1]: email for email in self.get_all_email_addresses()} + return email_map.get(email, email) + + def mail_recipients(self, is_inbound_mail_communcation=False): + """Build to(recipient) list to send an email.""" + # Incase of inbound mail, recipients already received the mail, no need to send again. + if is_inbound_mail_communcation: + return [] + + if hasattr(self, "_final_recipients"): + return self._final_recipients + + to = self.to_list() + self._final_recipients = list(filter(lambda id: id != "Administrator", to)) + return self._final_recipients + + def get_mail_recipients_with_displayname(self, is_inbound_mail_communcation=False): + """Build to(recipient) list to send an email including displayname in email.""" + to_list = self.mail_recipients(is_inbound_mail_communcation=is_inbound_mail_communcation) + return [self.get_email_with_displayname(email) for email in to_list] + + def mail_cc(self, is_inbound_mail_communcation=False, include_sender=False): + """Build cc list to send an email. + + * if email copy is requested by sender, then add sender to CC. + * If this doc is created through inbound mail, then add doc owner to cc list + * remove all the thread_notify disabled users. + * Remove standard users from email list + """ + if hasattr(self, "_final_cc"): + return self._final_cc + + cc = self.cc_list() + + if include_sender: + sender = self.sender_mailid + # if user has selected send_me_a_copy, use their email as sender + if xhiveframework.session.user not in xhiveframework.STANDARD_USERS: + sender = xhiveframework.db.get_value("User", xhiveframework.session.user, "email") + cc.append(sender) + + if is_inbound_mail_communcation: + # inform parent document owner incase communication is created through inbound mail + if doc_owner := self.get_owner(): + cc.append(doc_owner) + cc = set(cc) - {self.sender_mailid} + assignees = set(self.get_assignees()) + # Check and remove If user disabled notifications for incoming emails on assigned document. + for assignee in assignees.copy(): + if not is_email_notifications_enabled_for_type(assignee, "threads_on_assigned_document"): + assignees.remove(assignee) + cc.update(assignees) + + cc = set(cc) - set(self.filter_thread_notification_disbled_users(cc)) + cc = cc - set(self.mail_recipients(is_inbound_mail_communcation=is_inbound_mail_communcation)) + + # # Incase of inbound mail, to and cc already received the mail, no need to send again. + if is_inbound_mail_communcation: + cc = cc - set(self.cc_list() + self.to_list()) + + self._final_cc = [m for m in cc if m and m not in xhiveframework.STANDARD_USERS] + return self._final_cc + + def get_mail_cc_with_displayname(self, is_inbound_mail_communcation=False, include_sender=False): + cc_list = self.mail_cc( + is_inbound_mail_communcation=is_inbound_mail_communcation, include_sender=include_sender + ) + return [self.get_email_with_displayname(email) for email in cc_list if email] + + def mail_bcc(self, is_inbound_mail_communcation=False): + """ + * Thread_notify check + * Email unsubscribe list + * remove standard users. + """ + if hasattr(self, "_final_bcc"): + return self._final_bcc + + bcc = set(self.bcc_list()) + if is_inbound_mail_communcation: + bcc = bcc - {self.sender_mailid} + bcc = bcc - set(self.filter_thread_notification_disbled_users(bcc)) + bcc = bcc - set(self.mail_recipients(is_inbound_mail_communcation=is_inbound_mail_communcation)) + + # Incase of inbound mail, to and cc & bcc already received the mail, no need to send again. + if is_inbound_mail_communcation: + bcc = bcc - set(self.bcc_list() + self.to_list()) + + self._final_bcc = [m for m in bcc if m not in xhiveframework.STANDARD_USERS] + return self._final_bcc + + def get_mail_bcc_with_displayname(self, is_inbound_mail_communcation=False): + bcc_list = self.mail_bcc(is_inbound_mail_communcation=is_inbound_mail_communcation) + return [self.get_email_with_displayname(email) for email in bcc_list if email] + + def mail_sender(self): + email_account = self.get_outgoing_email_account() + if not self.sender_mailid and email_account: + return email_account.email_id + return self.sender_mailid + + def mail_sender_fullname(self): + email_account = self.get_outgoing_email_account() + if not self.sender_full_name: + return (email_account and email_account.name) or _("Notification") + return self.sender_full_name + + def get_mail_sender_with_displayname(self): + return get_formatted_email(self.mail_sender_fullname(), mail=self.mail_sender()) + + def get_content(self, print_format=None): + if print_format and xhiveframework.db.get_single_value("System Settings", "attach_view_link"): + return self.content + self.get_attach_link(print_format) + return self.content + + def get_attach_link(self, print_format): + """Returns public link for the attachment via `templates/emails/print_link.html`.""" + return xhiveframework.get_template("templates/emails/print_link.html").render( + { + "url": get_url(), + "doctype": self.reference_doctype, + "name": self.reference_name, + "print_format": print_format, + "key": get_parent_doc(self).get_document_share_key(), + } + ) + + def get_outgoing_email_account(self): + if not hasattr(self, "_outgoing_email_account"): + if self.email_account: + self._outgoing_email_account = EmailAccount.find(self.email_account) + else: + self._outgoing_email_account = EmailAccount.find_outgoing( + match_by_email=self.sender_mailid, match_by_doctype=self.reference_doctype + ) + + if self.sent_or_received == "Sent" and self._outgoing_email_account: + if xhiveframework.db.exists("Email Account", self._outgoing_email_account.name): + self.db_set("email_account", self._outgoing_email_account.name) + + return self._outgoing_email_account + + def get_incoming_email_account(self): + if not hasattr(self, "_incoming_email_account"): + self._incoming_email_account = EmailAccount.find_incoming( + match_by_email=self.sender_mailid, match_by_doctype=self.reference_doctype + ) + return self._incoming_email_account + + def mail_attachments(self, print_format=None, print_html=None, print_language=None): + final_attachments = [] + + if print_format or print_html: + d = { + "print_format": print_format, + "html": print_html, + "print_format_attachment": 1, + "doctype": self.reference_doctype, + "name": self.reference_name, + "lang": print_language or xhiveframework.local.lang, + } + final_attachments.append(d) + + final_attachments.extend({"fid": a["name"]} for a in self.get_attachments() or []) + return final_attachments + + def get_unsubscribe_message(self): + email_account = self.get_outgoing_email_account() + if email_account and email_account.send_unsubscribe_message: + return _("Leave this conversation") + return "" + + def exclude_emails_list(self, is_inbound_mail_communcation=False, include_sender=False) -> list: + """List of mail id's excluded while sending mail.""" + all_ids = self.get_all_email_addresses(exclude_displayname=True) + + final_ids = ( + self.mail_recipients(is_inbound_mail_communcation=is_inbound_mail_communcation) + + self.mail_bcc(is_inbound_mail_communcation=is_inbound_mail_communcation) + + self.mail_cc( + is_inbound_mail_communcation=is_inbound_mail_communcation, include_sender=include_sender + ) + ) + + return list(set(all_ids) - set(final_ids)) + + def get_assignees(self): + """Get owners of the reference document.""" + filters = { + "status": "Open", + "reference_name": self.reference_name, + "reference_type": self.reference_doctype, + } + + if self.reference_doctype and self.reference_name: + return ToDo.get_owners(filters) + else: + return [] + + @staticmethod + def filter_thread_notification_disbled_users(emails): + """Filter users based on notifications for email threads setting is disabled.""" + if not emails: + return [] + + return xhiveframework.get_all("User", pluck="email", filters={"email": ["in", emails], "thread_notify": 0}) + + @staticmethod + def filter_disabled_users(emails): + """ """ + if not emails: + return [] + + return xhiveframework.get_all("User", pluck="email", filters={"email": ["in", emails], "enabled": 0}) + + def sendmail_input_dict( + self, + print_html=None, + print_format=None, + send_me_a_copy=None, + print_letterhead=None, + is_inbound_mail_communcation=None, + print_language=None, + ) -> dict: + outgoing_email_account = self.get_outgoing_email_account() + if not outgoing_email_account: + return {} + + recipients = self.get_mail_recipients_with_displayname( + is_inbound_mail_communcation=is_inbound_mail_communcation + ) + cc = self.get_mail_cc_with_displayname( + is_inbound_mail_communcation=is_inbound_mail_communcation, include_sender=send_me_a_copy + ) + bcc = self.get_mail_bcc_with_displayname(is_inbound_mail_communcation=is_inbound_mail_communcation) + + if not (recipients or cc): + return {} + + final_attachments = self.mail_attachments( + print_format=print_format, print_html=print_html, print_language=print_language + ) + incoming_email_account = self.get_incoming_email_account() + return { + "recipients": recipients, + "cc": cc, + "bcc": bcc, + "expose_recipients": "header", + "sender": self.get_mail_sender_with_displayname(), + "reply_to": incoming_email_account and incoming_email_account.email_id, + "subject": self.subject, + "content": self.get_content(print_format=print_format), + "reference_doctype": self.reference_doctype, + "reference_name": self.reference_name, + "attachments": final_attachments, + "message_id": self.message_id, + "unsubscribe_message": self.get_unsubscribe_message(), + "delayed": True, + "communication": self.name, + "read_receipt": self.read_receipt, + "is_notification": (self.sent_or_received == "Received" and True) or False, + "print_letterhead": print_letterhead, + "send_after": self.send_after, + } + + def send_email( + self, + print_html=None, + print_format=None, + send_me_a_copy=None, + print_letterhead=None, + is_inbound_mail_communcation=None, + print_language=None, + now=False, + ): + if input_dict := self.sendmail_input_dict( + print_html=print_html, + print_format=print_format, + send_me_a_copy=send_me_a_copy, + print_letterhead=print_letterhead, + is_inbound_mail_communcation=is_inbound_mail_communcation, + print_language=print_language, + ): + xhiveframework.sendmail(now=now, **input_dict) diff --git a/xhiveframework/core/doctype/communication/test_communication.py b/xhiveframework/core/doctype/communication/test_communication.py new file mode 100644 index 0000000..edc98e0 --- /dev/null +++ b/xhiveframework/core/doctype/communication/test_communication.py @@ -0,0 +1,465 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework.core.doctype.communication.communication import Communication, get_emails, parse_email +from xhiveframework.core.doctype.communication.email import add_attachments, make +from xhiveframework.email.doctype.email_queue.email_queue import EmailQueue +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +if TYPE_CHECKING: + from xhiveframework.contacts.doctype.contact.contact import Contact + from xhiveframework.email.doctype.email_account.email_account import EmailAccount + +test_records = xhiveframework.get_test_records("Communication") + + +class TestCommunication(XhiveFrameworkTestCase): + def test_email(self): + valid_email_list = [ + "Full Name ", + '"Full Name with quotes and " ', + "Surname, Name ", + "Purchase@ABC ", + "xyz@abc2.com ", + "Name [something else] ", + ] + + invalid_email_list = [ + "[invalid!email]", + "invalid-email", + "tes2", + "e", + "rrrrrrrr", + "manas", + "[[[sample]]]", + "[invalid!email].com", + ] + + for i, x in enumerate(valid_email_list): + with self.subTest(i=i, x=x): + self.assertTrue(xhiveframework.utils.parse_addr(x)[1]) + + for i, x in enumerate(invalid_email_list): + with self.subTest(i=i, x=x): + self.assertFalse(xhiveframework.utils.parse_addr(x)[0]) + + def test_name(self): + valid_email_list = [ + "Full Name ", + '"Full Name with quotes and " ', + "Surname, Name ", + "Purchase@ABC ", + "xyz@abc2.com ", + "Name [something else] ", + ] + + invalid_email_list = [ + "[invalid!email]", + "invalid-email", + "tes2", + "e", + "rrrrrrrr", + "manas", + "[[[sample]]]", + "[invalid!email].com", + ] + + for x in valid_email_list: + self.assertTrue(xhiveframework.utils.parse_addr(x)[0]) + + for x in invalid_email_list: + self.assertFalse(xhiveframework.utils.parse_addr(x)[0]) + + def test_circular_linking(self): + a = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "This was created to test circular linking: Communication A", + } + ).insert(ignore_permissions=True) + + b = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "This was created to test circular linking: Communication B", + "reference_doctype": "Communication", + "reference_name": a.name, + } + ).insert(ignore_permissions=True) + + c = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "This was created to test circular linking: Communication C", + "reference_doctype": "Communication", + "reference_name": b.name, + } + ).insert(ignore_permissions=True) + + a = xhiveframework.get_doc("Communication", a.name) + a.reference_doctype = "Communication" + a.reference_name = c.name + + self.assertRaises(xhiveframework.CircularLinkingError, a.save) + + def test_deduplication_timeline_links(self): + xhiveframework.delete_doc_if_exists("Note", "deduplication timeline links") + + note = xhiveframework.get_doc( + { + "doctype": "Note", + "title": "deduplication timeline links", + "content": "deduplication timeline links", + } + ).insert(ignore_permissions=True) + + comm = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "Deduplication of Links", + "communication_medium": "Email", + } + ).insert(ignore_permissions=True) + + # adding same link twice + comm.add_link(link_doctype="Note", link_name=note.name, autosave=True) + comm.add_link(link_doctype="Note", link_name=note.name, autosave=True) + + comm = xhiveframework.get_doc("Communication", comm.name) + + self.assertNotEqual(2, len(comm.timeline_links)) + + def test_contacts_attached(self): + contact_sender: "Contact" = xhiveframework.get_doc( + { + "doctype": "Contact", + "first_name": "contact_sender", + } + ) + contact_sender.add_email("comm_sender@example.com") + contact_sender.insert(ignore_permissions=True) + + contact_recipient: "Contact" = xhiveframework.get_doc( + { + "doctype": "Contact", + "first_name": "contact_recipient", + } + ) + contact_recipient.add_email("comm_recipient@example.com") + contact_recipient.insert(ignore_permissions=True) + + contact_cc: "Contact" = xhiveframework.get_doc( + { + "doctype": "Contact", + "first_name": "contact_cc", + } + ) + contact_cc.add_email("comm_cc@example.com") + contact_cc.insert(ignore_permissions=True) + + comm: Communication = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_medium": "Email", + "subject": "Contacts Attached Test", + "sender": "comm_sender@example.com", + "recipients": "comm_recipient@example.com", + "cc": "comm_cc@example.com", + } + ).insert(ignore_permissions=True) + + comm = xhiveframework.get_doc("Communication", comm.name) + contact_links = [x.link_name for x in comm.timeline_links] + + self.assertIn(contact_sender.name, contact_links) + self.assertIn(contact_recipient.name, contact_links) + self.assertIn(contact_cc.name, contact_links) + + def test_get_communication_data(self): + from xhiveframework.desk.form.load import get_communication_data + + xhiveframework.delete_doc_if_exists("Note", "get communication data") + + note = xhiveframework.get_doc( + {"doctype": "Note", "title": "get communication data", "content": "get communication data"} + ).insert(ignore_permissions=True) + + comm_note_1 = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "Test Get Communication Data 1", + "communication_medium": "Email", + } + ).insert(ignore_permissions=True) + + comm_note_1.add_link(link_doctype="Note", link_name=note.name, autosave=True) + + comm_note_2 = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "content": "Test Get Communication Data 2", + "communication_medium": "Email", + } + ).insert(ignore_permissions=True) + + comm_note_2.add_link(link_doctype="Note", link_name=note.name, autosave=True) + + comms = get_communication_data("Note", note.name, as_dict=True) + + data = [comm.name for comm in comms] + self.assertIn(comm_note_1.name, data) + self.assertIn(comm_note_2.name, data) + + def test_parse_email(self): + to = "Jon Doe " + cc = """=?UTF-8?Q?Max_Mu=C3=9F?= , + erp+Customer+that%20company@example.org""" + bcc = "" + + results = list(parse_email([to, cc, bcc])) + self.assertEqual([("Customer", "that company")], results) + + results = list(parse_email([to, bcc])) + self.assertEqual(results, []) + + to = "jane.doe+A+Test@example.org" + cc = "" + bcc = "=?UTF-8?Q?Max_Mu=C3=9F?= " + results = list(parse_email([to, cc, bcc])) + self.assertEqual([("A", "Test"), ("Note", "Very important")], results) + + def test_get_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") + + def test_signature_in_email_content(self): + email_account = create_email_account() + signature = email_account.signature + base_communication = { + "doctype": "Communication", + "communication_medium": "Email", + "subject": "Document Link in Email", + "sender": "comm_sender@example.com", + } + comm_with_signature = xhiveframework.get_doc( + base_communication + | { + "content": f"""

+ Hi, + How are you? +


{signature}

""", + } + ).insert(ignore_permissions=True) + comm_without_signature = xhiveframework.get_doc( + base_communication + | { + "content": """
+ Hi, + How are you? +
""" + } + ).insert(ignore_permissions=True) + + self.assertEqual(comm_with_signature.content, comm_without_signature.content) + self.assertEqual(comm_with_signature.content.count(signature), 1) + self.assertEqual(comm_without_signature.content.count(signature), 1) + + def test_mark_as_spam(self): + xhiveframework.get_doc( + { + "doctype": "Email Rule", + "email_id": "spammer@example.com", + "is_spam": 1, + } + ).insert(ignore_permissions=True) + + spam_comm: Communication = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_medium": "Email", + "subject": "This is spam", + "sender": "spammer@example.com", + "recipients": "comm_recipient@example.com", + "sent_or_received": "Received", + } + ).insert(ignore_permissions=True) + + self.assertEqual(spam_comm.email_status, "Spam") + + normal_comm: Communication = xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_medium": "Email", + "subject": "This is spam", + "sender": "friendlyhuman@example.com", + "recipients": "comm_recipient@example.com", + "sent_or_received": "Received", + } + ).insert(ignore_permissions=True) + self.assertNotEqual(normal_comm.email_status, "Spam") + + +class TestCommunicationEmailMixin(XhiveFrameworkTestCase): + def new_communication(self, recipients=None, cc=None, bcc=None) -> Communication: + recipients = ", ".join(recipients or []) + cc = ", ".join(cc or []) + bcc = ", ".join(bcc or []) + + return xhiveframework.get_doc( + { + "doctype": "Communication", + "communication_type": "Communication", + "communication_medium": "Email", + "content": "Test content", + "recipients": recipients, + "cc": cc, + "bcc": bcc, + "sender": "sender@test.com", + } + ).insert(ignore_permissions=True) + + def new_user(self, email, **user_data): + user_data.setdefault("first_name", "first_name") + user = xhiveframework.new_doc("User") + user.email = email + user.update(user_data) + user.insert(ignore_permissions=True, ignore_if_duplicate=True) + return user + + def test_recipients(self): + to_list = ["to@test.com", "receiver ", "to@test.com"] + comm = self.new_communication(recipients=to_list) + res = comm.get_mail_recipients_with_displayname() + self.assertCountEqual(res, ["to@test.com", "receiver "]) + comm.delete() + + def test_cc(self): + def test(assertion, cc_list=None, set_user_as=None, include_sender=False, thread_notify=False): + if set_user_as: + xhiveframework.set_user(set_user_as) + + user = self.new_user(email="cc+1@test.com", thread_notify=thread_notify) + comm = self.new_communication(recipients=["to@test.com"], cc=cc_list) + res = comm.get_mail_cc_with_displayname(include_sender=include_sender) + + xhiveframework.set_user("Administrator") + user.delete() + comm.delete() + + self.assertEqual(res, assertion) + + # test filter_thread_notification_disbled_users and filter_mail_recipients + test(["cc "], cc_list=["cc+1@test.com", "cc ", "to@test.com"]) + + # test include_sender + test(["sender@test.com"], include_sender=True, thread_notify=True) + test(["cc+1@test.com"], include_sender=True, thread_notify=True, set_user_as="cc+1@test.com") + + def test_bcc(self): + bcc_list = [ + "bcc+1@test.com", + "cc ", + ] + user = self.new_user(email="bcc+2@test.com", enabled=0) + comm = self.new_communication(bcc=bcc_list) + res = comm.get_mail_bcc_with_displayname() + self.assertCountEqual(res, bcc_list) + user.delete() + comm.delete() + + def test_sendmail(self): + to_list = ["to "] + cc_list = ["cc ", "cc "] + + comm = self.new_communication(recipients=to_list, cc=cc_list) + comm.send_email() + doc = EmailQueue.find_one_by_filters(communication=comm.name) + mail_receivers = [each.recipient for each in doc.recipients] + self.assertIsNotNone(doc) + self.assertCountEqual(to_list + cc_list, mail_receivers) + doc.delete() + comm.delete() + + def test_add_attachments_by_filename(self): + to_list = ["to "] + comm = self.new_communication(recipients=to_list) + + file = xhiveframework.new_doc("File") + file.file_name = "test_add_attachments_by_filename.txt" + file.content = "test_add_attachments_by_filename" + file.insert(ignore_permissions=True) + + add_attachments(comm.name, [file.name]) + + attached_file_name, attached_content_hash = xhiveframework.db.get_value( + "File", + {"attached_to_name": comm.name, "attached_to_doctype": comm.doctype}, + ["file_name", "content_hash"], + ) + self.assertEqual(attached_content_hash, file.content_hash) + self.assertEqual(attached_file_name, file.file_name) + + def test_add_attachments_by_file_content(self): + to_list = ["to "] + comm = self.new_communication(recipients=to_list) + file_name = "test_add_attachments_by_file_content.txt" + file_content = "test_add_attachments_by_file_content" + add_attachments(comm.name, [{"fcontent": file_content, "fname": file_name}]) + attached_file_name = xhiveframework.db.get_value( + "File", + {"attached_to_name": comm.name, "attached_to_doctype": comm.doctype}, + ) + attached_file = xhiveframework.get_doc("File", attached_file_name) + self.assertEqual(attached_file.file_name, file_name) + self.assertEqual(attached_file.get_content(), file_content) + + +def create_email_account() -> "EmailAccount": + xhiveframework.delete_doc_if_exists("Email Account", "_Test Comm Account 1") + + xhiveframework.flags.mute_emails = False + xhiveframework.flags.sent_mail = None + + return xhiveframework.get_doc( + { + "is_default": 1, + "is_global": 1, + "doctype": "Email Account", + "domain": "example.com", + "append_to": "ToDo", + "email_account_name": "_Test Comm Account 1", + "enable_outgoing": 1, + "default_outgoing": 1, + "smtp_server": "test.example.com", + "email_id": "test_comm@example.com", + "password": "password", + "add_signature": 1, + "signature": "\nBest Wishes\nTest Signature", + "enable_auto_reply": 1, + "auto_reply_message": "", + "enable_incoming": 1, + "notify_if_unreplied": 1, + "unreplied_for_mins": 20, + "send_notification_to": "test_comm@example.com", + "pop3_server": "pop.test.example.com", + "imap_folder": [{"folder_name": "INBOX", "append_to": "ToDo"}], + "enable_automatic_linking": 1, + } + ).insert(ignore_permissions=True) diff --git a/xhiveframework/core/doctype/communication/test_records.json b/xhiveframework/core/doctype/communication/test_records.json new file mode 100644 index 0000000..a69d3e9 --- /dev/null +++ b/xhiveframework/core/doctype/communication/test_records.json @@ -0,0 +1,10 @@ +[ + { + "doctype": "Communication", + "name": "_Test Communication 1", + "subject": "Test Subject", + "sent_or_received": "Received", + "parenttype": "User", + "parent": "Administrator" + } +] diff --git a/xhiveframework/core/doctype/communication_link/__init__.py b/xhiveframework/core/doctype/communication_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/communication_link/communication_link.json b/xhiveframework/core/doctype/communication_link/communication_link.json new file mode 100644 index 0000000..1dd051b --- /dev/null +++ b/xhiveframework/core/doctype/communication_link/communication_link.json @@ -0,0 +1,47 @@ +{ + "creation": "2019-05-21 09:47:23.043960", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "link_doctype", + "link_name", + "link_title" + ], + "fields": [ + { + "fieldname": "link_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Link DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "link_name", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Link Name", + "options": "link_doctype", + "reqd": 1 + }, + { + "fieldname": "link_title", + "fieldtype": "Read Only", + "in_list_view": 1, + "label": "Link Title", + "read_only": 1 + } + ], + "istable": 1, + "modified": "2019-05-21 09:47:23.043960", + "modified_by": "Administrator", + "module": "Core", + "name": "Communication Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/communication_link/communication_link.py b/xhiveframework/core/doctype/communication_link/communication_link.py new file mode 100644 index 0000000..99d3243 --- /dev/null +++ b/xhiveframework/core/doctype/communication_link/communication_link.py @@ -0,0 +1,28 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class CommunicationLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + link_doctype: DF.Link + link_name: DF.DynamicLink + link_title: DF.ReadOnly | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass + + +def on_doctype_update(): + xhiveframework.db.add_index("Communication Link", ["link_doctype", "link_name"]) diff --git a/xhiveframework/core/doctype/custom_docperm/__init__.py b/xhiveframework/core/doctype/custom_docperm/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/custom_docperm/custom_docperm.js b/xhiveframework/core/doctype/custom_docperm/custom_docperm.js new file mode 100644 index 0000000..d7055e3 --- /dev/null +++ b/xhiveframework/core/doctype/custom_docperm/custom_docperm.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Custom DocPerm", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/custom_docperm/custom_docperm.json b/xhiveframework/core/doctype/custom_docperm/custom_docperm.json new file mode 100644 index 0000000..208b0be --- /dev/null +++ b/xhiveframework/core/doctype/custom_docperm/custom_docperm.json @@ -0,0 +1,241 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "hash", + "creation": "2017-01-11 04:21:35.217943", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "parent", + "role_and_level", + "role", + "if_owner", + "column_break_2", + "permlevel", + "section_break_4", + "select", + "read", + "write", + "create", + "delete", + "column_break_8", + "submit", + "cancel", + "amend", + "additional_permissions", + "report", + "export", + "import", + "column_break_19", + "share", + "print", + "email" + ], + "fields": [ + { + "fieldname": "role_and_level", + "fieldtype": "Section Break", + "label": "Role and Level" + }, + { + "fieldname": "role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Role", + "oldfieldname": "role", + "oldfieldtype": "Link", + "options": "Role", + "print_width": "150px", + "reqd": 1, + "width": "150px" + }, + { + "default": "0", + "description": "Apply this rule if the User is the Owner", + "fieldname": "if_owner", + "fieldtype": "Check", + "label": "If user is the owner" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "permlevel", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Level", + "oldfieldname": "permlevel", + "oldfieldtype": "Int", + "print_width": "40px", + "width": "40px" + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break", + "label": "Permissions" + }, + { + "default": "1", + "fieldname": "read", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Read", + "oldfieldname": "read", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "write", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Write", + "oldfieldname": "write", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "create", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Create", + "oldfieldname": "create", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "delete", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Delete" + }, + { + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "submit", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Submit", + "oldfieldname": "submit", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "cancel", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Cancel", + "oldfieldname": "cancel", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "amend", + "fieldtype": "Check", + "label": "Amend", + "oldfieldname": "amend", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "fieldname": "additional_permissions", + "fieldtype": "Section Break", + "label": "Additional Permissions" + }, + { + "default": "0", + "fieldname": "report", + "fieldtype": "Check", + "label": "Report", + "print_width": "32px", + "width": "32px" + }, + { + "default": "1", + "fieldname": "export", + "fieldtype": "Check", + "label": "Export" + }, + { + "default": "0", + "fieldname": "import", + "fieldtype": "Check", + "label": "Import" + }, + { + "fieldname": "column_break_19", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "share", + "fieldtype": "Check", + "label": "Share" + }, + { + "default": "0", + "fieldname": "print", + "fieldtype": "Check", + "label": "Print" + }, + { + "default": "0", + "fieldname": "email", + "fieldtype": "Check", + "label": "Email" + }, + { + "fieldname": "parent", + "fieldtype": "Data", + "label": "Reference Document Type", + "read_only": 1, + "search_index": 1 + }, + { + "default": "0", + "fieldname": "select", + "fieldtype": "Check", + "label": "Select" + } + ], + "links": [], + "modified": "2023-02-20 13:19:04.889081", + "modified_by": "Administrator", + "module": "Core", + "name": "Custom DocPerm", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "ASC", + "title_field": "parent" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/custom_docperm/custom_docperm.py b/xhiveframework/core/doctype/custom_docperm/custom_docperm.py new file mode 100644 index 0000000..386fa40 --- /dev/null +++ b/xhiveframework/core/doctype/custom_docperm/custom_docperm.py @@ -0,0 +1,37 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class CustomDocPerm(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + amend: DF.Check + cancel: DF.Check + create: DF.Check + delete: DF.Check + email: DF.Check + export: DF.Check + if_owner: DF.Check + parent: DF.Data | None + permlevel: DF.Int + print: DF.Check + read: DF.Check + report: DF.Check + role: DF.Link + select: DF.Check + share: DF.Check + submit: DF.Check + write: DF.Check + + # end: auto-generated types + def on_update(self): + xhiveframework.clear_cache(doctype=self.parent) diff --git a/xhiveframework/core/doctype/custom_docperm/test_custom_docperm.py b/xhiveframework/core/doctype/custom_docperm/test_custom_docperm.py new file mode 100644 index 0000000..16c3974 --- /dev/null +++ b/xhiveframework/core/doctype/custom_docperm/test_custom_docperm.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Custom DocPerm') + + +class TestCustomDocPerm(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/custom_role/__init__.py b/xhiveframework/core/doctype/custom_role/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/custom_role/custom_role.js b/xhiveframework/core/doctype/custom_role/custom_role.js new file mode 100644 index 0000000..2162394 --- /dev/null +++ b/xhiveframework/core/doctype/custom_role/custom_role.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Custom Role", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/custom_role/custom_role.json b/xhiveframework/core/doctype/custom_role/custom_role.json new file mode 100644 index 0000000..7504882 --- /dev/null +++ b/xhiveframework/core/doctype/custom_role/custom_role.json @@ -0,0 +1,76 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "hash", + "creation": "2017-02-13 14:53:36.240122", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "page", + "report", + "permission_rules", + "roles", + "response", + "ref_doctype" + ], + "fields": [ + { + "fieldname": "page", + "fieldtype": "Link", + "label": "Page", + "options": "Page" + }, + { + "fieldname": "report", + "fieldtype": "Link", + "label": "Report", + "options": "Report" + }, + { + "fieldname": "permission_rules", + "fieldtype": "Section Break", + "label": "Permission Rules" + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "label": "Role", + "options": "Has Role" + }, + { + "fieldname": "response", + "fieldtype": "HTML", + "label": "response" + }, + { + "fieldname": "ref_doctype", + "fieldtype": "Data", + "label": "Reference Document Type" + } + ], + "links": [], + "modified": "2022-08-03 12:20:52.985554", + "modified_by": "Administrator", + "module": "Core", + "name": "Custom Role", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/custom_role/custom_role.py b/xhiveframework/core/doctype/custom_role/custom_role.py new file mode 100644 index 0000000..d930dd8 --- /dev/null +++ b/xhiveframework/core/doctype/custom_role/custom_role.py @@ -0,0 +1,36 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class CustomRole(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.types import DF + + page: DF.Link | None + ref_doctype: DF.Data | None + report: DF.Link | None + roles: DF.Table[HasRole] + + # end: auto-generated types + def validate(self): + if self.report and not self.ref_doctype: + self.ref_doctype = xhiveframework.db.get_value("Report", self.report, "ref_doctype") + + +def get_custom_allowed_roles(field, name): + allowed_roles = [] + custom_role = xhiveframework.db.get_value("Custom Role", {field: name}, "name") + if custom_role: + custom_role_doc = xhiveframework.get_doc("Custom Role", custom_role) + allowed_roles = [d.role for d in custom_role_doc.roles] + + return allowed_roles diff --git a/xhiveframework/core/doctype/custom_role/test_custom_role.py b/xhiveframework/core/doctype/custom_role/test_custom_role.py new file mode 100644 index 0000000..a3d2a8c --- /dev/null +++ b/xhiveframework/core/doctype/custom_role/test_custom_role.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Custom Role') + + +class TestCustomRole(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/data_export/__init__.py b/xhiveframework/core/doctype/data_export/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/data_export/data_export.js b/xhiveframework/core/doctype/data_export/data_export.js new file mode 100644 index 0000000..c90d7ef --- /dev/null +++ b/xhiveframework/core/doctype/data_export/data_export.js @@ -0,0 +1,180 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Data Export", { + refresh: (frm) => { + frm.disable_save(); + frm.page.set_primary_action("Export", () => { + can_export(frm) ? export_data(frm) : null; + }); + }, + onload: (frm) => { + frm.set_query("reference_doctype", () => { + return { + filters: { + issingle: 0, + istable: 0, + name: ["in", xhiveframework.boot.user.can_export], + }, + }; + }); + }, + reference_doctype: (frm) => { + const doctype = frm.doc.reference_doctype; + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => set_field_options(frm)); + } else { + reset_filter_and_field(frm); + } + }, + export_without_main_header: (frm) => { + frm.refresh(); + }, +}); + +const can_export = (frm) => { + const doctype = frm.doc.reference_doctype; + const parent_multicheck_options = frm.fields_multicheck[doctype] + ? frm.fields_multicheck[doctype].get_checked_options() + : []; + let is_valid_form = false; + if (!doctype) { + xhiveframework.msgprint(__("Please select the Document Type.")); + } else if (!parent_multicheck_options.length) { + xhiveframework.msgprint(__("Atleast one field of Parent Document Type is mandatory")); + } else { + is_valid_form = true; + } + return is_valid_form; +}; + +const export_data = (frm) => { + let get_template_url = "/api/method/xhiveframework.core.doctype.data_export.exporter.export_data"; + var export_params = () => { + let columns = {}; + Object.keys(frm.fields_multicheck).forEach((dt) => { + const options = frm.fields_multicheck[dt].get_checked_options(); + columns[dt] = options; + }); + return { + doctype: frm.doc.reference_doctype, + select_columns: JSON.stringify(columns), + filters: frm.filter_list.get_filters().map((filter) => filter.slice(1, 4)), + file_type: frm.doc.file_type, + template: !frm.doc.export_without_main_header, + with_data: 1, + export_without_column_meta: frm.doc.export_without_main_header ? true : false, + }; + }; + + open_url_post(get_template_url, export_params()); +}; + +const reset_filter_and_field = (frm) => { + const parent_wrapper = frm.fields_dict.fields_multicheck.$wrapper; + const filter_wrapper = frm.fields_dict.filter_list.$wrapper; + parent_wrapper.empty(); + filter_wrapper.empty(); + frm.filter_list = []; + frm.fields_multicheck = {}; +}; + +const set_field_options = (frm) => { + const parent_wrapper = frm.fields_dict.fields_multicheck.$wrapper; + const filter_wrapper = frm.fields_dict.filter_list.$wrapper; + const doctype = frm.doc.reference_doctype; + const related_doctypes = get_doctypes(doctype); + + parent_wrapper.empty(); + filter_wrapper.empty(); + + frm.filter_list = new xhiveframework.ui.FilterGroup({ + parent: filter_wrapper, + doctype: doctype, + on_change: () => {}, + }); + + // Add 'Select All' and 'Unselect All' button + make_multiselect_buttons(parent_wrapper); + + frm.fields_multicheck = {}; + related_doctypes.forEach((dt) => { + frm.fields_multicheck[dt] = add_doctype_field_multicheck_control(dt, parent_wrapper); + }); + + frm.refresh(); +}; + +const make_multiselect_buttons = (parent_wrapper) => { + const button_container = $(parent_wrapper).append('
').find(".flex"); + + ["Select All", "Unselect All"].map((d) => { + xhiveframework.ui.form.make_control({ + parent: $(button_container), + df: { + label: __(d), + fieldname: xhiveframework.scrub(d), + fieldtype: "Button", + click: () => { + checkbox_toggle(d !== "Select All"); + }, + }, + render_input: true, + }); + }); + + $(button_container) + .find(".xhiveframework-control") + .map((index, button) => { + $(button).css({ "margin-right": "1em" }); + }); + + function checkbox_toggle(checked) { + $(parent_wrapper) + .find('[data-fieldtype="MultiCheck"]') + .map((index, element) => { + $(element).find(`:checkbox`).prop("checked", checked).trigger("click"); + }); + } +}; + +const get_doctypes = (parentdt) => { + return [parentdt].concat(xhiveframework.meta.get_table_fields(parentdt).map((df) => df.options)); +}; + +const add_doctype_field_multicheck_control = (doctype, parent_wrapper) => { + const fields = get_fields(doctype); + + xhiveframework.model.std_fields + .filter((df) => ["owner", "creation"].includes(df.fieldname)) + .forEach((df) => { + fields.push(df); + }); + + const options = fields.map((df) => { + return { + label: __(df.label, null, df.parent), + value: df.fieldname, + danger: df.reqd, + checked: 1, + }; + }); + + const multicheck_control = xhiveframework.ui.form.make_control({ + parent: parent_wrapper, + df: { + label: doctype, + fieldname: doctype + "_fields", + fieldtype: "MultiCheck", + options: options, + columns: 3, + }, + render_input: true, + }); + + multicheck_control.refresh_input(); + return multicheck_control; +}; + +const filter_fields = (df) => xhiveframework.model.is_value_type(df) && !df.hidden; +const get_fields = (dt) => xhiveframework.meta.get_docfields(dt).filter(filter_fields); diff --git a/xhiveframework/core/doctype/data_export/data_export.json b/xhiveframework/core/doctype/data_export/data_export.json new file mode 100644 index 0000000..f63d939 --- /dev/null +++ b/xhiveframework/core/doctype/data_export/data_export.json @@ -0,0 +1,84 @@ +{ + "actions": [], + "creation": "2018-03-07 10:09:49.794764", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "export_without_main_header", + "column_break_2", + "file_type", + "section_break", + "filter_list", + "fields_multicheck" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Select Doctype", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "CSV", + "fieldname": "file_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "File Type", + "options": "Excel\nCSV", + "reqd": 1 + }, + { + "depends_on": "reference_doctype", + "fieldname": "section_break", + "fieldtype": "Section Break" + }, + { + "fieldname": "filter_list", + "fieldtype": "HTML", + "label": "Filter List" + }, + { + "fieldname": "fields_multicheck", + "fieldtype": "HTML", + "label": "Fields Multicheck" + }, + { + "default": "0", + "description": "Export the data without any header notes and column descriptions", + "fieldname": "export_without_main_header", + "fieldtype": "Check", + "label": "Export without main header" + } + ], + "hide_toolbar": 1, + "issingle": 1, + "links": [], + "modified": "2022-09-28 03:51:02.404681", + "modified_by": "Administrator", + "module": "Core", + "name": "Data Export", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/data_export/data_export.py b/xhiveframework/core/doctype/data_export/data_export.py new file mode 100644 index 0000000..04ffa40 --- /dev/null +++ b/xhiveframework/core/doctype/data_export/data_export.py @@ -0,0 +1,20 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class DataExport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + export_without_main_header: DF.Check + file_type: DF.Literal["Excel", "CSV"] + reference_doctype: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/data_export/exporter.py b/xhiveframework/core/doctype/data_export/exporter.py new file mode 100644 index 0000000..8b176ce --- /dev/null +++ b/xhiveframework/core/doctype/data_export/exporter.py @@ -0,0 +1,480 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import csv +import os +import re + +import xhiveframework +import xhiveframework.permissions +from xhiveframework import _ +from xhiveframework.core.doctype.access_log.access_log import make_access_log +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.utils import cint, cstr, format_datetime, format_duration, formatdate, parse_json +from xhiveframework.utils.csvutils import UnicodeWriter + +reflags = {"I": re.I, "L": re.L, "M": re.M, "U": re.U, "S": re.S, "X": re.X, "D": re.DEBUG} + + +def get_data_keys(): + return xhiveframework._dict( + { + "data_separator": _("Start entering data below this line"), + "main_table": _("Table") + ":", + "parent_table": _("Parent Table") + ":", + "columns": _("Column Name") + ":", + "doctype": _("DocType") + ":", + } + ) + + +@xhiveframework.whitelist() +def export_data( + doctype=None, + parent_doctype=None, + all_doctypes=True, + with_data=False, + select_columns=None, + file_type="CSV", + template=False, + filters=None, + export_without_column_meta=False, +): + _doctype = doctype + if isinstance(_doctype, list): + _doctype = _doctype[0] + make_access_log( + doctype=_doctype, + file_type=file_type, + columns=select_columns, + filters=filters, + method=parent_doctype, + ) + + template_bool = template + if isinstance(template, str): + template_bool = template.lower() == "true" + + export_without_column_meta_bool = export_without_column_meta + if isinstance(export_without_column_meta, str): + export_without_column_meta_bool = export_without_column_meta.lower() == "true" + + exporter = DataExporter( + doctype=doctype, + parent_doctype=parent_doctype, + all_doctypes=all_doctypes, + with_data=with_data, + select_columns=select_columns, + file_type=file_type, + template=template_bool, + filters=filters, + export_without_column_meta=export_without_column_meta_bool, + ) + exporter.build_response() + + +class DataExporter: + def __init__( + self, + doctype=None, + parent_doctype=None, + all_doctypes=True, + with_data=False, + select_columns=None, + file_type="CSV", + template=False, + filters=None, + export_without_column_meta=False, + ): + self.doctype = doctype + self.parent_doctype = parent_doctype + self.all_doctypes = all_doctypes + self.with_data = cint(with_data) + self.select_columns = select_columns + self.file_type = file_type + self.template = template + self.filters = filters + self.export_without_column_meta = export_without_column_meta + self.data_keys = get_data_keys() + + self.prepare_args() + + def prepare_args(self): + if self.select_columns: + self.select_columns = parse_json(self.select_columns) + if self.filters: + self.filters = parse_json(self.filters) + + self.docs_to_export = {} + if self.doctype: + if isinstance(self.doctype, str): + self.doctype = [self.doctype] + + if len(self.doctype) > 1: + self.docs_to_export = self.doctype[1] + self.doctype = self.doctype[0] + + if not self.parent_doctype: + self.parent_doctype = self.doctype + + self.column_start_end = {} + + if self.all_doctypes: + self.child_doctypes = [ + dict(doctype=df.options, parentfield=df.fieldname) + for df in xhiveframework.get_meta(self.doctype).get_table_fields() + ] + + def build_response(self): + self.writer = UnicodeWriter() + self.name_field = "parent" if self.parent_doctype != self.doctype else "name" + + if self.template: + self.add_main_header() + + # No need of empty row at the start + if not self.export_without_column_meta: + self.writer.writerow([""]) + + self.tablerow = [self.data_keys.doctype] + self.labelrow = [_("Column Labels:")] + self.fieldrow = [self.data_keys.columns] + self.mandatoryrow = [_("Mandatory:")] + self.typerow = [_("Type:")] + self.inforow = [_("Info:")] + self.columns = [] + + self.build_field_columns(self.doctype) + + if self.all_doctypes: + for d in self.child_doctypes: + self.append_empty_field_column() + if ( + self.select_columns and self.select_columns.get(d["doctype"], None) + ) or not self.select_columns: + # if atleast one column is selected for this doctype + self.build_field_columns(d["doctype"], d["parentfield"]) + + self.add_field_headings() + self.add_data() + if self.with_data and not self.data: + xhiveframework.respond_as_web_page( + _("No Data"), _("There is no data to be exported"), indicator_color="orange" + ) + + if self.file_type == "Excel": + self.build_response_as_excel() + else: + # write out response as a type csv + xhiveframework.response["result"] = cstr(self.writer.getvalue()) + xhiveframework.response["type"] = "csv" + xhiveframework.response["doctype"] = self.doctype + + def add_main_header(self): + self.writer.writerow([_("Data Import Template")]) + self.writer.writerow([self.data_keys.main_table, self.doctype]) + + if self.parent_doctype != self.doctype: + self.writer.writerow([self.data_keys.parent_table, self.parent_doctype]) + else: + self.writer.writerow([""]) + + self.writer.writerow([""]) + self.writer.writerow([_("Notes:")]) + self.writer.writerow([_("Please do not change the template headings.")]) + self.writer.writerow([_("First data column must be blank.")]) + self.writer.writerow([_('If you are uploading new records, leave the "name" (ID) column blank.')]) + self.writer.writerow( + [_('If you are uploading new records, "Naming Series" becomes mandatory, if present.')] + ) + self.writer.writerow( + [ + _( + "Only mandatory fields are necessary for new records. You can delete non-mandatory columns if you wish." + ) + ] + ) + self.writer.writerow([_("For updating, you can update only selective columns.")]) + self.writer.writerow( + [_("You can only upload upto 5000 records in one go. (may be less in some cases)")] + ) + if self.name_field == "parent": + self.writer.writerow([_('"Parent" signifies the parent table in which this row must be added')]) + self.writer.writerow( + [_('If you are updating, please select "Overwrite" else existing rows will not be deleted.')] + ) + + def build_field_columns(self, dt, parentfield=None): + meta = xhiveframework.get_meta(dt) + + # build list of valid docfields + tablecolumns = [] + table_name = "tab" + dt + + for f in xhiveframework.db.get_table_columns_description(table_name): + field = meta.get_field(f.name) + if f.name in ["owner", "creation"]: + std_field = next((x for x in xhiveframework.model.std_fields if x["fieldname"] == f.name), None) + if std_field: + field = xhiveframework._dict( + { + "fieldname": std_field.get("fieldname"), + "label": std_field.get("label"), + "fieldtype": std_field.get("fieldtype"), + "options": std_field.get("options"), + "idx": 0, + "parent": dt, + } + ) + + if field and ( + (self.select_columns and f.name in self.select_columns[dt]) or not self.select_columns + ): + tablecolumns.append(field) + + tablecolumns.sort(key=lambda a: int(a.idx)) + + _column_start_end = xhiveframework._dict(start=0) + + if dt == self.doctype: + if (meta.get("autoname") and meta.get("autoname").lower() == "prompt") or (self.with_data): + self._append_name_column() + + # if importing only child table for new record, add parent field + if meta.get("istable") and not self.with_data: + self.append_field_column( + xhiveframework._dict( + { + "fieldname": "parent", + "parent": "", + "label": "Parent", + "fieldtype": "Data", + "reqd": 1, + "info": _( + "Parent is the name of the document to which the data will get added to." + ), + } + ), + True, + ) + + _column_start_end = xhiveframework._dict(start=0) + else: + _column_start_end = xhiveframework._dict(start=len(self.columns)) + + if self.with_data: + self._append_name_column(dt) + + for docfield in tablecolumns: + self.append_field_column(docfield, True) + + # all non mandatory fields + for docfield in tablecolumns: + self.append_field_column(docfield, False) + + # if there is one column, add a blank column (?) + if len(self.columns) - _column_start_end.start == 1: + self.append_empty_field_column() + + # append DocType name + self.tablerow[_column_start_end.start + 1] = dt + + if parentfield: + self.tablerow[_column_start_end.start + 2] = parentfield + + _column_start_end.end = len(self.columns) + 1 + + self.column_start_end[(dt, parentfield)] = _column_start_end + + def append_field_column(self, docfield, for_mandatory): + if not docfield: + return + if for_mandatory and not docfield.reqd: + return + if not for_mandatory and docfield.reqd: + return + if docfield.fieldname in ("parenttype", "trash_reason"): + return + if docfield.hidden: + return + if ( + self.select_columns + and docfield.fieldname not in self.select_columns.get(docfield.parent, []) + and docfield.fieldname != "name" + ): + return + + self.tablerow.append("") + self.fieldrow.append(docfield.fieldname) + self.labelrow.append(_(docfield.label, context=docfield.parent)) + self.mandatoryrow.append(docfield.reqd and "Yes" or "No") + self.typerow.append(docfield.fieldtype) + self.inforow.append(self.getinforow(docfield)) + self.columns.append(docfield.fieldname) + + def append_empty_field_column(self): + self.tablerow.append("~") + self.fieldrow.append("~") + self.labelrow.append("") + self.mandatoryrow.append("") + self.typerow.append("") + self.inforow.append("") + self.columns.append("") + + @staticmethod + def getinforow(docfield): + """make info comment for options, links etc.""" + if docfield.fieldtype == "Select": + if not docfield.options: + return "" + else: + return _("One of") + ": %s" % ", ".join(filter(None, docfield.options.split("\n"))) + elif docfield.fieldtype == "Link": + return "Valid %s" % docfield.options + elif docfield.fieldtype == "Int": + return "Integer" + elif docfield.fieldtype == "Check": + return "0 or 1" + elif docfield.fieldtype in ["Date", "Datetime"]: + return cstr(xhiveframework.defaults.get_defaults().date_format) + elif hasattr(docfield, "info"): + return docfield.info + else: + return "" + + def add_field_headings(self): + if not self.export_without_column_meta: + self.writer.writerow(self.tablerow) + + # Just include Labels in the first row + self.writer.writerow(self.labelrow) + + if not self.export_without_column_meta: + self.writer.writerow(self.fieldrow) + self.writer.writerow(self.mandatoryrow) + self.writer.writerow(self.typerow) + self.writer.writerow(self.inforow) + + if self.template: + self.writer.writerow([self.data_keys.data_separator]) + + def add_data(self): + from xhiveframework.query_builder import DocType + + if self.template and not self.with_data: + return + + xhiveframework.permissions.can_export(self.parent_doctype, raise_exception=True) + + # sort nested set doctypes by `lft asc` + order_by = None + table_columns = xhiveframework.db.get_table_columns(self.parent_doctype) + if "lft" in table_columns and "rgt" in table_columns: + order_by = f"`tab{self.parent_doctype}`.`lft` asc" + # get permitted data only + self.data = xhiveframework.get_list( + self.doctype, fields=["*"], filters=self.filters, limit_page_length=None, order_by=order_by + ) + + for doc in self.data: + op = self.docs_to_export.get("op") + names = self.docs_to_export.get("name") + + if names and op: + if op == "=" and doc.name not in names: + continue + elif op == "!=" and doc.name in names: + continue + elif names: + try: + sflags = self.docs_to_export.get("flags", "I,U").upper() + flags = 0 + for a in re.split(r"\W+", sflags): + flags = flags | reflags.get(a, 0) + + c = re.compile(names, flags) + m = c.match(doc.name) + if not m: + continue + except Exception: + if doc.name not in names: + continue + # add main table + rows = [] + + self.add_data_row(rows, self.doctype, None, doc, 0) + + if self.all_doctypes: + # add child tables + for c in self.child_doctypes: + if is_virtual_doctype(c["doctype"]): + continue + child_doctype_table = DocType(c["doctype"]) + data_row = ( + xhiveframework.qb.from_(child_doctype_table) + .select("*") + .where(child_doctype_table.parent == doc.name) + .where(child_doctype_table.parentfield == c["parentfield"]) + .orderby(child_doctype_table.idx) + ) + for ci, child in enumerate(data_row.run(as_dict=True)): + self.add_data_row(rows, c["doctype"], c["parentfield"], child, ci) + for row in rows: + self.writer.writerow(row) + + def add_data_row(self, rows, dt, parentfield, doc, rowidx): + d = doc.copy() + meta = xhiveframework.get_meta(dt) + if self.all_doctypes: + d.name = f'"{d.name}"' + + if len(rows) < rowidx + 1: + rows.append([""] * (len(self.columns) + 1)) + row = rows[rowidx] + + _column_start_end = self.column_start_end.get((dt, parentfield)) + + if _column_start_end: + for i, c in enumerate(self.columns[_column_start_end.start : _column_start_end.end]): + df = meta.get_field(c) + fieldtype = df.fieldtype if df else "Data" + value = d.get(c, "") + if value: + if fieldtype == "Date": + value = formatdate(value) + elif fieldtype == "Datetime": + value = format_datetime(value) + elif fieldtype == "Duration": + value = format_duration(value, df.hide_days) + + row[_column_start_end.start + i + 1] = value + + def build_response_as_excel(self): + from xhiveframework.desk.utils import provide_binary_file + from xhiveframework.utils.xlsxutils import make_xlsx + + filename = xhiveframework.generate_hash(length=10) + with open(filename, "wb") as f: + f.write(cstr(self.writer.getvalue()).encode("utf-8")) + f = open(filename) + reader = csv.reader(f) + xlsx_file = make_xlsx(reader, "Data Import Template" if self.template else "Data Export") + + f.close() + os.remove(filename) + + provide_binary_file(self.doctype, "xlsx", xlsx_file.getvalue()) + + def _append_name_column(self, dt=None): + self.append_field_column( + xhiveframework._dict( + { + "fieldname": "name" if dt else self.name_field, + "parent": dt or "", + "label": "ID", + "fieldtype": "Data", + "reqd": 1, + } + ), + True, + ) diff --git a/xhiveframework/core/doctype/data_export/test_data_exporter.py b/xhiveframework/core/doctype/data_export/test_data_exporter.py new file mode 100644 index 0000000..b571476 --- /dev/null +++ b/xhiveframework/core/doctype/data_export/test_data_exporter.py @@ -0,0 +1,113 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.data_export.exporter import DataExporter +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDataExporter(XhiveFrameworkTestCase): + def setUp(self): + self.doctype_name = "Test DocType for Export Tool" + self.doc_name = "Test Data for Export Tool" + self.create_doctype_if_not_exists(doctype_name=self.doctype_name) + self.create_test_data() + + def create_doctype_if_not_exists(self, doctype_name, force=False): + """ + Helper Function for setting up doctypes + """ + if force: + xhiveframework.delete_doc_if_exists("DocType", doctype_name) + xhiveframework.delete_doc_if_exists("DocType", "Child 1 of " + doctype_name) + + if xhiveframework.db.exists("DocType", doctype_name): + return + + # Child Table 1 + table_1_name = "Child 1 of " + doctype_name + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": table_1_name, + "module": "Custom", + "custom": 1, + "istable": 1, + "fields": [ + {"label": "Child Title", "fieldname": "child_title", "reqd": 1, "fieldtype": "Data"}, + {"label": "Child Number", "fieldname": "child_number", "fieldtype": "Int"}, + ], + } + ).insert() + + # Main Table + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": doctype_name, + "module": "Custom", + "custom": 1, + "autoname": "field:title", + "fields": [ + {"label": "Title", "fieldname": "title", "reqd": 1, "fieldtype": "Data"}, + {"label": "Number", "fieldname": "number", "fieldtype": "Int"}, + { + "label": "Table Field 1", + "fieldname": "table_field_1", + "fieldtype": "Table", + "options": table_1_name, + }, + ], + "permissions": [{"role": "System Manager"}], + } + ).insert() + + def create_test_data(self, force=False): + """ + Helper Function creating test data + """ + if force: + xhiveframework.delete_doc(self.doctype_name, self.doc_name) + + if not xhiveframework.db.exists(self.doctype_name, self.doc_name): + self.doc = xhiveframework.get_doc( + doctype=self.doctype_name, + title=self.doc_name, + number="100", + table_field_1=[ + {"child_title": "Child Title 1", "child_number": "50"}, + {"child_title": "Child Title 2", "child_number": "51"}, + ], + ).insert() + else: + self.doc = xhiveframework.get_doc(self.doctype_name, self.doc_name) + + def test_export_content(self): + exp = DataExporter(doctype=self.doctype_name, file_type="CSV") + exp.build_response() + + self.assertEqual(xhiveframework.response["type"], "csv") + self.assertEqual(xhiveframework.response["doctype"], self.doctype_name) + self.assertTrue(xhiveframework.response["result"]) + self.assertRegex(xhiveframework.response["result"], r"Child Title 1.*?,50") + self.assertRegex(xhiveframework.response["result"], r"Child Title 2.*?,51") + + def test_export_type(self): + for type in ["csv", "Excel"]: + with self.subTest(type=type): + exp = DataExporter(doctype=self.doctype_name, file_type=type) + exp.build_response() + + self.assertEqual(xhiveframework.response["doctype"], self.doctype_name) + self.assertTrue(xhiveframework.response["result"]) + + if type == "csv": + self.assertEqual(xhiveframework.response["type"], "csv") + elif type == "Excel": + self.assertEqual(xhiveframework.response["type"], "binary") + self.assertEqual( + xhiveframework.response["filename"], self.doctype_name + ".xlsx" + ) # 'Test DocType for Export Tool.xlsx') + self.assertTrue(xhiveframework.response["filecontent"]) + + def tearDown(self): + pass diff --git a/xhiveframework/core/doctype/data_import/__init__.py b/xhiveframework/core/doctype/data_import/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/data_import/data_import.css b/xhiveframework/core/doctype/data_import/data_import.css new file mode 100644 index 0000000..5206540 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/data_import.css @@ -0,0 +1,3 @@ +.warnings .warning { + margin-bottom: 40px; +} diff --git a/xhiveframework/core/doctype/data_import/data_import.js b/xhiveframework/core/doctype/data_import/data_import.js new file mode 100644 index 0000000..38681a0 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/data_import.js @@ -0,0 +1,536 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Data Import", { + setup(frm) { + xhiveframework.realtime.on("data_import_refresh", ({ data_import }) => { + frm.import_in_progress = false; + if (data_import !== frm.doc.name) return; + xhiveframework.model.clear_doc("Data Import", frm.doc.name); + xhiveframework.model.with_doc("Data Import", frm.doc.name).then(() => { + frm.refresh(); + }); + }); + xhiveframework.realtime.on("data_import_progress", (data) => { + frm.import_in_progress = true; + if (data.data_import !== frm.doc.name) { + return; + } + let percent = Math.floor((data.current * 100) / data.total); + let seconds = Math.floor(data.eta); + let minutes = Math.floor(data.eta / 60); + let eta_message = + // prettier-ignore + seconds < 60 + ? __('About {0} seconds remaining', [seconds]) + : minutes === 1 + ? __('About {0} minute remaining', [minutes]) + : __('About {0} minutes remaining', [minutes]); + + let message; + if (data.success) { + let message_args = [data.current, data.total, eta_message]; + message = + frm.doc.import_type === "Insert New Records" + ? __("Importing {0} of {1}, {2}", message_args) + : __("Updating {0} of {1}, {2}", message_args); + } + if (data.skipping) { + message = __("Skipping {0} of {1}, {2}", [data.current, data.total, eta_message]); + } + frm.dashboard.show_progress(__("Import Progress"), percent, message); + frm.page.set_indicator(__("In Progress"), "orange"); + frm.trigger("update_primary_action"); + + // hide progress when complete + if (data.current === data.total) { + setTimeout(() => { + frm.dashboard.hide(); + frm.refresh(); + }, 2000); + } + }); + + frm.set_query("reference_doctype", () => { + return { + filters: { + name: ["in", xhiveframework.boot.user.can_import], + }, + }; + }); + + frm.get_field("import_file").df.options = { + restrictions: { + allowed_file_types: [".csv", ".xls", ".xlsx"], + }, + }; + + frm.has_import_file = () => { + return frm.doc.import_file || frm.doc.google_sheets_url; + }; + }, + + refresh(frm) { + frm.page.hide_icon_group(); + frm.trigger("update_indicators"); + frm.trigger("import_file"); + frm.trigger("show_import_log"); + frm.trigger("show_import_warnings"); + frm.trigger("toggle_submit_after_import"); + + if (frm.doc.status != "Pending") frm.trigger("show_import_status"); + + frm.trigger("show_report_error_button"); + + if (frm.doc.status === "Partial Success") { + frm.add_custom_button(__("Export Errored Rows"), () => + frm.trigger("export_errored_rows") + ); + } + + if (frm.doc.status.includes("Success")) { + frm.add_custom_button(__("Go to {0} List", [__(frm.doc.reference_doctype)]), () => + xhiveframework.set_route("List", frm.doc.reference_doctype) + ); + } + }, + + onload_post_render(frm) { + frm.trigger("update_primary_action"); + }, + + update_primary_action(frm) { + if (frm.is_dirty()) { + frm.enable_save(); + return; + } + frm.disable_save(); + if (frm.doc.status !== "Success") { + if (!frm.is_new() && frm.has_import_file()) { + let label = frm.doc.status === "Pending" ? __("Start Import") : __("Retry"); + frm.page.set_primary_action(label, () => frm.events.start_import(frm)); + } else { + frm.page.set_primary_action(__("Save"), () => frm.save()); + } + } + }, + + update_indicators(frm) { + const indicator = xhiveframework.get_indicator(frm.doc); + if (indicator) { + frm.page.set_indicator(indicator[0], indicator[1]); + } else { + frm.page.clear_indicator(); + } + }, + + show_import_status(frm) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.data_import.data_import.get_import_status", + args: { + data_import_name: frm.doc.name, + }, + callback: function (r) { + let successful_records = cint(r.message.success); + let failed_records = cint(r.message.failed); + let total_records = cint(r.message.total_records); + + if (!total_records) return; + let action, message; + if (frm.doc.import_type === "Insert New Records") { + action = "imported"; + } else { + action = "updated"; + } + + if (failed_records === 0) { + let message_args = [action, successful_records]; + if (successful_records === 1) { + message = __("Successfully {0} 1 record.", message_args); + } else { + message = __("Successfully {0} {1} records.", message_args); + } + } else { + let message_args = [action, successful_records, total_records]; + if (successful_records === 1) { + message = __( + "Successfully {0} {1} record out of {2}. Click on Export Errored Rows, fix the errors and import again.", + message_args + ); + } else { + message = __( + "Successfully {0} {1} records out of {2}. Click on Export Errored Rows, fix the errors and import again.", + message_args + ); + } + } + + // If the job timed out, display an extra hint + if (r.message.status === "Timed Out") { + message += "
" + __("Import timed out, please re-try."); + } + + frm.dashboard.set_headline(message); + }, + }); + }, + + show_report_error_button(frm) { + if (frm.doc.status === "Error") { + xhiveframework.db + .get_list("Error Log", { + filters: { method: frm.doc.name }, + fields: ["method", "error"], + order_by: "creation desc", + limit: 1, + }) + .then((result) => { + if (result.length > 0) { + frm.add_custom_button("Report Error", () => { + let fake_xhr = { + responseText: JSON.stringify({ + exc: result[0].error, + }), + }; + xhiveframework.request.report_error(fake_xhr, {}); + }); + } + }); + } + }, + + start_import(frm) { + frm.call({ + method: "form_start_import", + args: { data_import: frm.doc.name }, + btn: frm.page.btn_primary, + }).then((r) => { + if (r.message === true) { + frm.disable_save(); + } + }); + }, + + download_template(frm) { + xhiveframework.require("data_import_tools.bundle.js", () => { + frm.data_exporter = new xhiveframework.data_import.DataExporter( + frm.doc.reference_doctype, + frm.doc.import_type + ); + }); + }, + + reference_doctype(frm) { + frm.trigger("toggle_submit_after_import"); + }, + + toggle_submit_after_import(frm) { + frm.toggle_display("submit_after_import", false); + let doctype = frm.doc.reference_doctype; + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => { + let meta = xhiveframework.get_meta(doctype); + frm.toggle_display("submit_after_import", meta.is_submittable); + }); + } + }, + + google_sheets_url(frm) { + if (!frm.is_dirty()) { + frm.trigger("import_file"); + } else { + frm.trigger("update_primary_action"); + } + }, + + refresh_google_sheet(frm) { + frm.trigger("import_file"); + }, + + import_file(frm) { + frm.toggle_display("section_import_preview", frm.has_import_file()); + if (!frm.has_import_file()) { + frm.get_field("import_preview").$wrapper.empty(); + return; + } else { + frm.trigger("update_primary_action"); + } + + // load import preview + frm.get_field("import_preview").$wrapper.empty(); + $('') + .html(__("Loading import file...")) + .appendTo(frm.get_field("import_preview").$wrapper); + + frm.call({ + method: "get_preview_from_template", + args: { + data_import: frm.doc.name, + import_file: frm.doc.import_file, + google_sheets_url: frm.doc.google_sheets_url, + }, + error_handlers: { + TimestampMismatchError() { + // ignore this error + }, + }, + }).then((r) => { + let preview_data = r.message; + frm.events.show_import_preview(frm, preview_data); + frm.events.show_import_warnings(frm, preview_data); + }); + }, + + show_import_preview(frm, preview_data) { + let import_log = preview_data.import_log; + + if (frm.import_preview && frm.import_preview.doctype === frm.doc.reference_doctype) { + frm.import_preview.preview_data = preview_data; + frm.import_preview.import_log = import_log; + frm.import_preview.refresh(); + return; + } + + xhiveframework.require("data_import_tools.bundle.js", () => { + frm.import_preview = new xhiveframework.data_import.ImportPreview({ + wrapper: frm.get_field("import_preview").$wrapper, + doctype: frm.doc.reference_doctype, + preview_data, + import_log, + frm, + events: { + remap_column(changed_map) { + let template_options = JSON.parse(frm.doc.template_options || "{}"); + template_options.column_to_field_map = + template_options.column_to_field_map || {}; + Object.assign(template_options.column_to_field_map, changed_map); + frm.set_value("template_options", JSON.stringify(template_options)); + frm.save().then(() => frm.trigger("import_file")); + }, + }, + }); + }); + }, + + export_errored_rows(frm) { + open_url_post( + "/api/method/xhiveframework.core.doctype.data_import.data_import.download_errored_template", + { + data_import_name: frm.doc.name, + } + ); + }, + + export_import_log(frm) { + open_url_post( + "/api/method/xhiveframework.core.doctype.data_import.data_import.download_import_log", + { + data_import_name: frm.doc.name, + } + ); + }, + + show_import_warnings(frm, preview_data) { + let columns = preview_data.columns; + let warnings = JSON.parse(frm.doc.template_warnings || "[]"); + warnings = warnings.concat(preview_data.warnings || []); + + frm.toggle_display("import_warnings_section", warnings.length > 0); + if (warnings.length === 0) { + frm.get_field("import_warnings").$wrapper.html(""); + return; + } + + // group warnings by row + let warnings_by_row = {}; + let other_warnings = []; + for (let warning of warnings) { + if (warning.row) { + warnings_by_row[warning.row] = warnings_by_row[warning.row] || []; + warnings_by_row[warning.row].push(warning); + } else { + other_warnings.push(warning); + } + } + + let html = ""; + html += Object.keys(warnings_by_row) + .map((row_number) => { + let message = warnings_by_row[row_number] + .map((w) => { + if (w.field) { + let label = + w.field.label + + (w.field.parent !== frm.doc.reference_doctype + ? ` (${w.field.parent})` + : ""); + return `
  • ${label}: ${w.message}
  • `; + } + return `
  • ${w.message}
  • `; + }) + .join(""); + return ` +
    +
    ${__("Row {0}", [row_number])}
    +
      ${message}
    +
    + `; + }) + .join(""); + + html += other_warnings + .map((warning) => { + let header = ""; + if (warning.col) { + let column_number = `${__("Column {0}", [ + warning.col, + ])}`; + let column_header = columns[warning.col].header_title; + header = `${column_number} (${column_header})`; + } + return ` +
    +
    ${header}
    +
    ${warning.message}
    +
    + `; + }) + .join(""); + frm.get_field("import_warnings").$wrapper.html(` +
    +
    ${html}
    +
    + `); + }, + + show_failed_logs(frm) { + frm.trigger("show_import_log"); + }, + + render_import_log(frm) { + xhiveframework.call({ + method: "xhiveframework.client.get_list", + args: { + doctype: "Data Import Log", + filters: { + data_import: frm.doc.name, + }, + fields: ["success", "docname", "messages", "exception", "row_indexes"], + limit_page_length: 5000, + order_by: "log_index", + }, + callback: function (r) { + let logs = r.message; + + if (logs.length === 0) return; + + frm.toggle_display("import_log_section", true); + + let rows = logs + .map((log) => { + let html = ""; + if (log.success) { + if (frm.doc.import_type === "Insert New Records") { + html = __("Successfully imported {0}", [ + `${xhiveframework.utils.get_form_link( + frm.doc.reference_doctype, + log.docname, + true + )}`, + ]); + } else { + html = __("Successfully updated {0}", [ + `${xhiveframework.utils.get_form_link( + frm.doc.reference_doctype, + log.docname, + true + )}`, + ]); + } + } else { + let messages = JSON.parse(log.messages || "[]") + .map((m) => { + let title = m.title ? `${m.title}` : ""; + let message = m.message ? `
    ${m.message}
    ` : ""; + return title + message; + }) + .join(""); + let id = xhiveframework.dom.get_unique_id(); + html = `${messages} + +
    +
    +
    ${log.exception}
    +
    +
    `; + } + let indicator_color = log.success ? "green" : "red"; + let title = log.success ? __("Success") : __("Failure"); + + if (frm.doc.show_failed_logs && log.success) { + return ""; + } + + return ` + ${JSON.parse(log.row_indexes).join(", ")} + +
    ${title}
    + + + ${html} + + `; + }) + .join(""); + + if (!rows && frm.doc.show_failed_logs) { + rows = ` + ${__("No failed logs")} + `; + } + + frm.get_field("import_log_preview").$wrapper.html(` + + + + + + + ${rows} +
    ${__("Row Number")}${__("Status")}${__("Message")}
    + `); + }, + }); + }, + + show_import_log(frm) { + frm.toggle_display("import_log_section", false); + + if (frm.import_in_progress) { + return; + } + + xhiveframework.call({ + method: "xhiveframework.client.get_count", + args: { + doctype: "Data Import Log", + filters: { + data_import: frm.doc.name, + }, + }, + callback: function (r) { + let count = r.message; + if (count < 5000) { + frm.trigger("render_import_log"); + } else { + frm.toggle_display("import_log_section", false); + frm.add_custom_button(__("Export Import Log"), () => + frm.trigger("export_import_log") + ); + } + }, + }); + }, +}); diff --git a/xhiveframework/core/doctype/data_import/data_import.json b/xhiveframework/core/doctype/data_import/data_import.json new file mode 100644 index 0000000..02d65c8 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/data_import.json @@ -0,0 +1,198 @@ +{ + "actions": [], + "autoname": "format:{reference_doctype} Import on {creation}", + "beta": 1, + "creation": "2019-08-04 14:16:08.318714", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "import_type", + "download_template", + "import_file", + "payload_count", + "html_5", + "google_sheets_url", + "refresh_google_sheet", + "column_break_5", + "status", + "submit_after_import", + "mute_emails", + "template_options", + "import_warnings_section", + "template_warnings", + "import_warnings", + "section_import_preview", + "import_preview", + "import_log_section", + "show_failed_logs", + "import_log_preview" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1, + "set_only_once": 1 + }, + { + "fieldname": "import_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Import Type", + "options": "\nInsert New Records\nUpdate Existing Records", + "reqd": 1, + "set_only_once": 1 + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "import_file", + "fieldtype": "Attach", + "in_list_view": 1, + "label": "Import File", + "read_only_depends_on": "eval: ['Success', 'Partial Success'].includes(doc.status)" + }, + { + "fieldname": "import_preview", + "fieldtype": "HTML", + "label": "Import Preview" + }, + { + "fieldname": "section_import_preview", + "fieldtype": "Section Break", + "label": "Preview" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "template_options", + "fieldtype": "Code", + "hidden": 1, + "label": "Template Options", + "options": "JSON", + "read_only": 1 + }, + { + "fieldname": "import_log_section", + "fieldtype": "Section Break", + "label": "Import Log" + }, + { + "fieldname": "import_log_preview", + "fieldtype": "HTML", + "label": "Import Log Preview" + }, + { + "default": "Pending", + "fieldname": "status", + "fieldtype": "Select", + "hidden": 1, + "label": "Status", + "no_copy": 1, + "options": "Pending\nSuccess\nPartial Success\nError\nTimed Out", + "read_only": 1 + }, + { + "fieldname": "template_warnings", + "fieldtype": "Code", + "hidden": 1, + "label": "Template Warnings", + "options": "JSON" + }, + { + "default": "0", + "fieldname": "submit_after_import", + "fieldtype": "Check", + "label": "Submit After Import", + "set_only_once": 1 + }, + { + "fieldname": "import_warnings_section", + "fieldtype": "Section Break", + "label": "Import File Errors and Warnings" + }, + { + "fieldname": "import_warnings", + "fieldtype": "HTML", + "label": "Import Warnings" + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "download_template", + "fieldtype": "Button", + "label": "Download Template" + }, + { + "default": "1", + "fieldname": "mute_emails", + "fieldtype": "Check", + "label": "Don't Send Emails", + "set_only_once": 1 + }, + { + "default": "0", + "fieldname": "show_failed_logs", + "fieldtype": "Check", + "label": "Show Only Failed Logs" + }, + { + "depends_on": "eval:!doc.__islocal && !doc.import_file", + "fieldname": "html_5", + "fieldtype": "HTML", + "options": "
    Or
    " + }, + { + "depends_on": "eval:!doc.__islocal && !doc.import_file\n", + "description": "Must be a publicly accessible Google Sheets URL", + "fieldname": "google_sheets_url", + "fieldtype": "Data", + "label": "Import from Google Sheets", + "read_only_depends_on": "eval: ['Success', 'Partial Success'].includes(doc.status)" + }, + { + "depends_on": "eval:doc.google_sheets_url && !doc.__unsaved && ['Success', 'Partial Success'].includes(doc.status)", + "fieldname": "refresh_google_sheet", + "fieldtype": "Button", + "label": "Refresh Google Sheet" + }, + { + "fieldname": "payload_count", + "fieldtype": "Int", + "hidden": 1, + "label": "Payload Count", + "read_only": 1 + } + ], + "hide_toolbar": 1, + "links": [], + "modified": "2024-01-30 17:08:05.566686", + "modified_by": "Administrator", + "module": "Core", + "name": "Data Import", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/data_import/data_import.py b/xhiveframework/core/doctype/data_import/data_import.py new file mode 100644 index 0000000..0b6de00 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/data_import.py @@ -0,0 +1,308 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import os + +from rq.timeouts import JobTimeoutException + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.doctype.data_import.exporter import Exporter +from xhiveframework.core.doctype.data_import.importer import Importer +from xhiveframework.model import core_doctypes_list +from xhiveframework.model.document import Document +from xhiveframework.modules.import_file import import_file_by_path +from xhiveframework.utils.background_jobs import enqueue, is_job_enqueued +from xhiveframework.utils.csvutils import validate_google_sheets_url + +BLOCKED_DOCTYPES = set(core_doctypes_list) - {"User", "Role"} + + +class DataImport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + google_sheets_url: DF.Data | None + import_file: DF.Attach | None + import_type: DF.Literal["", "Insert New Records", "Update Existing Records"] + mute_emails: DF.Check + payload_count: DF.Int + reference_doctype: DF.Link + show_failed_logs: DF.Check + status: DF.Literal["Pending", "Success", "Partial Success", "Error", "Timed Out"] + submit_after_import: DF.Check + template_options: DF.Code | None + template_warnings: DF.Code | None + # end: auto-generated types + + def validate(self): + doc_before_save = self.get_doc_before_save() + if ( + not (self.import_file or self.google_sheets_url) + or (doc_before_save and doc_before_save.import_file != self.import_file) + or (doc_before_save and doc_before_save.google_sheets_url != self.google_sheets_url) + ): + self.template_options = "" + self.template_warnings = "" + + self.validate_doctype() + self.validate_import_file() + self.validate_google_sheets_url() + self.set_payload_count() + + def validate_doctype(self): + if self.reference_doctype in BLOCKED_DOCTYPES: + xhiveframework.throw(_("Importing {0} is not allowed.").format(self.reference_doctype)) + + def validate_import_file(self): + if self.import_file: + # validate template + self.get_importer() + + def validate_google_sheets_url(self): + if not self.google_sheets_url: + return + validate_google_sheets_url(self.google_sheets_url) + + def set_payload_count(self): + if self.import_file: + i = self.get_importer() + payloads = i.import_file.get_payloads_for_import() + self.payload_count = len(payloads) + + @xhiveframework.whitelist() + def get_preview_from_template(self, import_file=None, google_sheets_url=None): + if import_file: + self.import_file = import_file + + if google_sheets_url: + self.google_sheets_url = google_sheets_url + + if not (self.import_file or self.google_sheets_url): + return + + i = self.get_importer() + return i.get_data_for_import_preview() + + def start_import(self): + from xhiveframework.utils.scheduler import is_scheduler_inactive + + if is_scheduler_inactive() and not xhiveframework.flags.in_test: + xhiveframework.throw(_("Scheduler is inactive. Cannot import data."), title=_("Scheduler Inactive")) + + job_id = f"data_import::{self.name}" + + if not is_job_enqueued(job_id): + enqueue( + start_import, + queue="default", + timeout=10000, + event="data_import", + job_id=job_id, + data_import=self.name, + now=xhiveframework.conf.developer_mode or xhiveframework.flags.in_test, + ) + return True + + return False + + def export_errored_rows(self): + return self.get_importer().export_errored_rows() + + def download_import_log(self): + return self.get_importer().export_import_log() + + def get_importer(self): + return Importer(self.reference_doctype, data_import=self) + + +@xhiveframework.whitelist() +def get_preview_from_template(data_import, import_file=None, google_sheets_url=None): + return xhiveframework.get_doc("Data Import", data_import).get_preview_from_template( + import_file, google_sheets_url + ) + + +@xhiveframework.whitelist() +def form_start_import(data_import): + return xhiveframework.get_doc("Data Import", data_import).start_import() + + +def start_import(data_import): + """This method runs in background job""" + data_import = xhiveframework.get_doc("Data Import", data_import) + try: + i = Importer(data_import.reference_doctype, data_import=data_import) + i.import_data() + except JobTimeoutException: + xhiveframework.db.rollback() + data_import.db_set("status", "Timed Out") + except Exception: + xhiveframework.db.rollback() + data_import.db_set("status", "Error") + data_import.log_error("Data import failed") + finally: + xhiveframework.flags.in_import = False + + xhiveframework.publish_realtime("data_import_refresh", {"data_import": data_import.name}) + + +@xhiveframework.whitelist() +def download_template(doctype, export_fields=None, export_records=None, export_filters=None, file_type="CSV"): + """ + Download template from Exporter + :param doctype: Document Type + :param export_fields=None: Fields to export as dict {'Sales Invoice': ['name', 'customer'], 'Sales Invoice Item': ['item_code']} + :param export_records=None: One of 'all', 'by_filter', 'blank_template' + :param export_filters: Filter dict + :param file_type: File type to export into + """ + + export_fields = xhiveframework.parse_json(export_fields) + export_filters = xhiveframework.parse_json(export_filters) + export_data = export_records != "blank_template" + + e = Exporter( + doctype, + export_fields=export_fields, + export_data=export_data, + export_filters=export_filters, + file_type=file_type, + export_page_length=5 if export_records == "5_records" else None, + ) + e.build_response() + + +@xhiveframework.whitelist() +def download_errored_template(data_import_name): + data_import = xhiveframework.get_doc("Data Import", data_import_name) + data_import.export_errored_rows() + + +@xhiveframework.whitelist() +def download_import_log(data_import_name): + data_import = xhiveframework.get_doc("Data Import", data_import_name) + data_import.download_import_log() + + +@xhiveframework.whitelist() +def get_import_status(data_import_name): + import_status = {} + + data_import = xhiveframework.get_doc("Data Import", data_import_name) + import_status["status"] = data_import.status + + logs = xhiveframework.get_all( + "Data Import Log", + fields=["count(*) as count", "success"], + filters={"data_import": data_import_name}, + group_by="success", + ) + + total_payload_count = xhiveframework.db.get_value("Data Import", data_import_name, "payload_count") + + for log in logs: + if log.get("success"): + import_status["success"] = log.get("count") + else: + import_status["failed"] = log.get("count") + + import_status["total_records"] = total_payload_count + + return import_status + + +def import_file(doctype, file_path, import_type, submit_after_import=False, console=False): + """ + Import documents in from CSV or XLSX using data import. + + :param doctype: DocType to import + :param file_path: Path to .csv, .xls, or .xlsx file to import + :param import_type: One of "Insert" or "Update" + :param submit_after_import: Whether to submit documents after import + :param console: Set to true if this is to be used from command line. Will print errors or progress to stdout. + """ + + data_import = xhiveframework.new_doc("Data Import") + data_import.submit_after_import = submit_after_import + data_import.import_type = ( + "Insert New Records" if import_type.lower() == "insert" else "Update Existing Records" + ) + + i = Importer(doctype=doctype, file_path=file_path, data_import=data_import, console=console) + i.import_data() + + +def import_doc(path, pre_process=None): + if os.path.isdir(path): + files = [os.path.join(path, f) for f in os.listdir(path)] + else: + files = [path] + + for f in files: + if f.endswith(".json"): + xhiveframework.flags.mute_emails = True + import_file_by_path( + f, data_import=True, force=True, pre_process=pre_process, reset_permissions=True + ) + xhiveframework.flags.mute_emails = False + xhiveframework.db.commit() + else: + raise NotImplementedError("Only .json files can be imported") + + +def export_json(doctype, path, filters=None, or_filters=None, name=None, order_by="creation asc"): + def post_process(out): + # Note on Tree DocTypes: + # The tree structure is maintained in the database via the fields "lft" + # and "rgt". They are automatically set and kept up-to-date. Importing + # them would destroy any existing tree structure. For this reason they + # are not exported as well. + del_keys = ("modified_by", "creation", "owner", "idx", "lft", "rgt") + for doc in out: + for key in del_keys: + if key in doc: + del doc[key] + for v in doc.values(): + if isinstance(v, list): + for child in v: + for key in (*del_keys, "docstatus", "doctype", "modified", "name"): + if key in child: + del child[key] + + out = [] + if name: + out.append(xhiveframework.get_doc(doctype, name).as_dict()) + elif xhiveframework.db.get_value("DocType", doctype, "issingle"): + out.append(xhiveframework.get_doc(doctype).as_dict()) + else: + for doc in xhiveframework.get_all( + doctype, + fields=["name"], + filters=filters, + or_filters=or_filters, + limit_page_length=0, + order_by=order_by, + ): + out.append(xhiveframework.get_doc(doctype, doc.name).as_dict()) + post_process(out) + + dirname = os.path.dirname(path) + if not os.path.exists(dirname): + path = os.path.join("..", path) + + with open(path, "w") as outfile: + outfile.write(xhiveframework.as_json(out, ensure_ascii=False)) + + +def export_csv(doctype, path): + from xhiveframework.core.doctype.data_export.exporter import export_data + + with open(path, "wb") as csvfile: + export_data(doctype=doctype, all_doctypes=True, template=True, with_data=True) + csvfile.write(xhiveframework.response.result.encode("utf-8")) diff --git a/xhiveframework/core/doctype/data_import/data_import_list.js b/xhiveframework/core/doctype/data_import/data_import_list.js new file mode 100644 index 0000000..d3deb14 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/data_import_list.js @@ -0,0 +1,45 @@ +let imports_in_progress = []; + +xhiveframework.listview_settings["Data Import"] = { + onload(listview) { + xhiveframework.realtime.on("data_import_progress", (data) => { + if (!imports_in_progress.includes(data.data_import)) { + imports_in_progress.push(data.data_import); + } + }); + xhiveframework.realtime.on("data_import_refresh", (data) => { + imports_in_progress = imports_in_progress.filter((d) => d !== data.data_import); + listview.refresh(); + }); + }, + get_indicator: function (doc) { + var colors = { + Pending: "orange", + "Not Started": "orange", + "Partial Success": "orange", + Success: "green", + "In Progress": "orange", + Error: "red", + "Timed Out": "orange", + }; + let status = doc.status; + + if (imports_in_progress.includes(doc.name)) { + status = "In Progress"; + } + if (status === "Pending") { + status = "Not Started"; + } + + return [__(status), colors[status], "status,=," + doc.status]; + }, + formatters: { + import_type(value) { + return { + "Insert New Records": __("Insert"), + "Update Existing Records": __("Update"), + }[value]; + }, + }, + hide_name_column: true, +}; diff --git a/xhiveframework/core/doctype/data_import/exporter.py b/xhiveframework/core/doctype/data_import/exporter.py new file mode 100644 index 0000000..7d507d9 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/exporter.py @@ -0,0 +1,249 @@ +# Copyright (c) 2019, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import display_fieldtypes, no_value_fields +from xhiveframework.model import table_fields as table_fieldtypes +from xhiveframework.utils import flt, format_duration, groupby_metric +from xhiveframework.utils.csvutils import build_csv_response +from xhiveframework.utils.xlsxutils import build_xlsx_response + + +class Exporter: + def __init__( + self, + doctype, + export_fields=None, + export_data=False, + export_filters=None, + export_page_length=None, + file_type="CSV", + ): + """ + Exports records of a DocType for use with Importer + :param doctype: Document Type to export + :param export_fields=None: One of 'All', 'Mandatory' or {'DocType': ['field1', 'field2'], 'Child DocType': ['childfield1']} + :param export_data=False: Whether to export data as well + :param export_filters=None: The filters (dict or list) which is used to query the records + :param file_type: One of 'Excel' or 'CSV' + """ + self.doctype = doctype + self.meta = xhiveframework.get_meta(doctype) + self.export_fields = export_fields + self.export_filters = export_filters + self.export_page_length = export_page_length + self.file_type = file_type + + # this will contain the csv content + self.csv_array = [] + + # fields that get exported + self.exportable_fields = self.get_all_exportable_fields() + self.fields = self.serialize_exportable_fields() + self.add_header() + + if export_data: + self.data = self.get_data_to_export() + else: + self.data = [] + self.add_data() + + def get_all_exportable_fields(self): + child_table_fields = [df.fieldname for df in self.meta.fields if df.fieldtype in table_fieldtypes] + + meta = xhiveframework.get_meta(self.doctype) + exportable_fields = xhiveframework._dict({}) + + for key, fieldnames in self.export_fields.items(): + if key == self.doctype: + # parent fields + exportable_fields[key] = self.get_exportable_fields(key, fieldnames) + + elif key in child_table_fields: + # child fields + child_df = meta.get_field(key) + child_doctype = child_df.options + exportable_fields[key] = self.get_exportable_fields(child_doctype, fieldnames) + + return exportable_fields + + def serialize_exportable_fields(self): + fields = [] + for key, exportable_fields in self.exportable_fields.items(): + for _df in exportable_fields: + # make a copy of df dict to avoid reference mutation + if isinstance(_df, xhiveframework.core.doctype.docfield.docfield.DocField): + df = _df.as_dict() + else: + df = _df.copy() + + df.is_child_table_field = key != self.doctype + if df.is_child_table_field: + df.child_table_df = self.meta.get_field(key) + fields.append(df) + return fields + + def get_exportable_fields(self, doctype, fieldnames): + meta = xhiveframework.get_meta(doctype) + + def is_exportable(df): + return df and df.fieldtype not in (display_fieldtypes + no_value_fields) + + # add name field + name_field = xhiveframework._dict( + { + "fieldtype": "Data", + "fieldname": "name", + "label": "ID", + "reqd": 1, + "parent": doctype, + } + ) + + fields = [meta.get_field(fieldname) for fieldname in fieldnames] + fields = [df for df in fields if is_exportable(df)] + + if "name" in fieldnames: + fields = [name_field, *fields] + + return fields or [] + + def get_data_to_export(self): + xhiveframework.permissions.can_export(self.doctype, raise_exception=True) + + table_fields = [f for f in self.exportable_fields if f != self.doctype] + data = self.get_data_as_docs() + + for doc in data: + rows = [] + rows = self.add_data_row(self.doctype, None, doc, rows, 0) + + if table_fields: + # add child table data + for f in table_fields: + for i, child_row in enumerate(doc.get(f, [])): + table_df = self.meta.get_field(f) + child_doctype = table_df.options + rows = self.add_data_row(child_doctype, child_row.parentfield, child_row, rows, i) + + yield from rows + + def add_data_row(self, doctype, parentfield, doc, rows, row_idx): + if len(rows) < row_idx + 1: + rows.append([""] * len(self.fields)) + + row = rows[row_idx] + + for i, df in enumerate(self.fields): + if df.parent == doctype: + if df.is_child_table_field and df.child_table_df.fieldname != parentfield: + continue + value = doc.get(df.fieldname, None) + + if df.fieldtype == "Duration": + value = flt(value or 0) + value = format_duration(value, df.hide_days) + + row[i] = value + return rows + + def get_data_as_docs(self): + def format_column_name(df): + return f"`tab{df.parent}`.`{df.fieldname}`" + + filters = self.export_filters + + if self.meta.is_nested_set(): + order_by = f"`tab{self.doctype}`.`lft` ASC" + else: + order_by = f"`tab{self.doctype}`.`creation` DESC" + + parent_fields = [format_column_name(df) for df in self.fields if df.parent == self.doctype] + parent_data = xhiveframework.db.get_list( + self.doctype, + filters=filters, + fields=["name", *parent_fields], + limit_page_length=self.export_page_length, + order_by=order_by, + as_list=0, + ) + parent_names = [p.name for p in parent_data] + + child_data = {} + for key in self.exportable_fields: + if key == self.doctype: + continue + child_table_df = self.meta.get_field(key) + child_table_doctype = child_table_df.options + child_fields = [ + "name", + "idx", + "parent", + "parentfield", + *list({format_column_name(df) for df in self.fields if df.parent == child_table_doctype}), + ] + data = xhiveframework.get_all( + child_table_doctype, + filters={ + "parent": ("in", parent_names), + "parentfield": child_table_df.fieldname, + "parenttype": self.doctype, + }, + fields=child_fields, + order_by="idx asc", + as_list=0, + ) + child_data[key] = data + + # Group children data by parent name + grouped_children_data = self.group_children_data_by_parent(child_data) + for doc in parent_data: + related_children_docs = grouped_children_data.get(str(doc.name), {}) + yield {**doc, **related_children_docs} + + def add_header(self): + header = [] + for df in self.fields: + is_parent = not df.is_child_table_field + if is_parent: + label = _(df.label or df.fieldname) + else: + label = f"{_(df.label or df.fieldname)} ({_(df.child_table_df.label or df.child_table_df.fieldname)})" + + if label in header: + # this label is already in the header, + # which means two fields with the same label + # add the fieldname to avoid clash + if is_parent: + label = f"{df.fieldname}" + else: + label = f"{df.child_table_df.fieldname}.{df.fieldname}" + + header.append(label) + + self.csv_array.append(header) + + def add_data(self): + self.csv_array += self.data + + def get_csv_array(self): + return self.csv_array + + def get_csv_array_for_export(self): + csv_array = self.csv_array + + if not self.data: + # add 2 empty rows + csv_array += [[]] * 2 + + return csv_array + + def build_response(self): + if self.file_type == "CSV": + build_csv_response(self.get_csv_array_for_export(), _(self.doctype)) + elif self.file_type == "Excel": + build_xlsx_response(self.get_csv_array_for_export(), _(self.doctype)) + + def group_children_data_by_parent(self, children_data: dict[str, list]): + return groupby_metric(children_data, key="parent") diff --git a/xhiveframework/core/doctype/data_import/fixtures/sample_import_file.csv b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file.csv new file mode 100644 index 0000000..a432a45 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file.csv @@ -0,0 +1,5 @@ +Title ,Description ,Number ,Duration,another_number ,ID (Table Field 1),Child Title (Table Field 1),Child Description (Table Field 1),Child 2 Title (Table Field 2),Child 2 Date (Table Field 2),Child 2 Number (Table Field 2),Child Title (Table Field 1 Again),Child Date (Table Field 1 Again),Child Number (Table Field 1 Again),table_field_1_again.child_another_number +Test ,test description ,1,3h,2, ,child title ,child description ,child title ,14-08-2019,4,child title again ,22-09-2020,5,7 + , , ,, , ,child title 2,child description 2,title child ,30-10-2019,5,child title again 2,22-09-2021, , +Test 2,test description 2,1,4d 3h,2, ,child mandatory title , ,title child man , , ,child mandatory again , , , +Test 3,test description 3,4,5d 5h 45m,5, ,child title asdf ,child description asdf ,child title asdf adsf ,15-08-2019,6,child title again asdf ,22-09-2022,9,71 \ No newline at end of file diff --git a/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_for_update.csv b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_for_update.csv new file mode 100644 index 0000000..e48208e --- /dev/null +++ b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_for_update.csv @@ -0,0 +1,2 @@ +Title,Description,Number,another_number,ID (Table Field 1),Child Title (Table Field 1),Child Description (Table Field 1),Child 2 Title (Table Field 2),Child 2 Date (Table Field 2),Child 2 Number (Table Field 2),Child Title (Table Field 1 Again),Child Date (Table Field 1 Again),Child Number (Table Field 1 Again),table_field_1_again.child_another_number +Test 26,test description,1,2,"",child title,child description,child title,14-08-2019,4,child title again,22-09-2020,5,7 diff --git a/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_without_mandatory.csv b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_without_mandatory.csv new file mode 100644 index 0000000..c6bff5c --- /dev/null +++ b/xhiveframework/core/doctype/data_import/fixtures/sample_import_file_without_mandatory.csv @@ -0,0 +1,5 @@ +Title ,Description ,Number ,another_number ,ID (Table Field 1) ,Child Title (Table Field 1) ,Child Description (Table Field 1) ,Child 2 Title (Table Field 2) ,Child 2 Date (Table Field 2) ,Child 2 Number (Table Field 2) ,Child Title (Table Field 1 Again) ,Child Date (Table Field 1 Again) ,Child Number (Table Field 1 Again) ,table_field_1_again.child_another_number +Test 5 ,test description ,1 ,2 ,"" , ,child description ,child title ,14-08-2019 ,4 ,child title again ,22-09-2020 ,5 , 7 + , , , , ,child title 2 ,child description 2 ,title child ,30-10-2019 ,5 , ,22-09-2021 , , + ,test description 2 ,1 ,2 , ,child mandatory title , ,title child man , , ,child mandatory again , , , +Test 4 ,test description 3 ,4 ,5 ,"" ,child title asdf ,child description asdf ,child title asdf adsf ,15-08-2019 ,6 ,child title again asdf ,22-09-2022 ,9 , 71 diff --git a/xhiveframework/core/doctype/data_import/importer.py b/xhiveframework/core/doctype/data_import/importer.py new file mode 100644 index 0000000..1751e08 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/importer.py @@ -0,0 +1,1263 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import os +import re +import timeit +from datetime import date, datetime, time + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.doctype.version.version import get_diff +from xhiveframework.model import no_value_fields +from xhiveframework.utils import cint, cstr, duration_to_seconds, flt, update_progress_bar +from xhiveframework.utils.csvutils import get_csv_content_from_google_sheets, read_csv_content +from xhiveframework.utils.xlsxutils import ( + read_xls_file_from_attached_file, + read_xlsx_file_from_attached_file, +) + +INVALID_VALUES = ("", None) +MAX_ROWS_IN_PREVIEW = 10 +INSERT = "Insert New Records" +UPDATE = "Update Existing Records" +DURATION_PATTERN = re.compile(r"^(?:(\d+d)?((^|\s)\d+h)?((^|\s)\d+m)?((^|\s)\d+s)?)$") + + +class Importer: + def __init__(self, doctype, data_import=None, file_path=None, import_type=None, console=False): + self.doctype = doctype + self.console = console + + self.data_import = data_import + if not self.data_import: + self.data_import = xhiveframework.get_doc(doctype="Data Import") + if import_type: + self.data_import.import_type = import_type + + self.template_options = xhiveframework.parse_json(self.data_import.template_options or "{}") + self.import_type = self.data_import.import_type + + self.import_file = ImportFile( + doctype, + file_path or data_import.google_sheets_url or data_import.import_file, + self.template_options, + self.import_type, + console=self.console, + ) + + def get_data_for_import_preview(self): + out = self.import_file.get_data_for_import_preview() + + out.import_log = xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success"], + filters={"data_import": self.data_import.name}, + order_by="log_index", + limit=10, + ) + + return out + + def before_import(self): + # set user lang for translations + xhiveframework.cache.hdel("lang", xhiveframework.session.user) + xhiveframework.set_user_lang(xhiveframework.session.user) + + # set flags + xhiveframework.flags.in_import = True + xhiveframework.flags.mute_emails = self.data_import.mute_emails + + self.data_import.db_set("template_warnings", "") + + def import_data(self): + self.before_import() + + # parse docs from rows + payloads = self.import_file.get_payloads_for_import() + + # dont import if there are non-ignorable warnings + warnings = self.import_file.get_warnings() + warnings = [w for w in warnings if w.get("type") != "info"] + + if warnings: + if self.console: + self.print_grouped_warnings(warnings) + else: + self.data_import.db_set("template_warnings", json.dumps(warnings)) + return + + # setup import log + import_log = ( + xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success", "log_index"], + filters={"data_import": self.data_import.name}, + order_by="log_index", + ) + or [] + ) + + log_index = 0 + + # Do not remove rows in case of retry after an error or pending data import + if self.data_import.status == "Partial Success" and len(import_log) >= self.data_import.payload_count: + # remove previous failures from import log only in case of retry after partial success + import_log = [log for log in import_log if log.get("success")] + + # get successfully imported rows + imported_rows = [] + for log in import_log: + log = xhiveframework._dict(log) + if log.success or len(import_log) < self.data_import.payload_count: + imported_rows += json.loads(log.row_indexes) + + log_index = log.log_index + + # start import + total_payload_count = len(payloads) + batch_size = xhiveframework.conf.data_import_batch_size or 1000 + + for batch_index, batched_payloads in enumerate(xhiveframework.utils.create_batch(payloads, batch_size)): + for i, payload in enumerate(batched_payloads): + doc = payload.doc + row_indexes = [row.row_number for row in payload.rows] + current_index = (i + 1) + (batch_index * batch_size) + + if set(row_indexes).intersection(set(imported_rows)): + print("Skipping imported rows", row_indexes) + if total_payload_count > 5: + xhiveframework.publish_realtime( + "data_import_progress", + { + "current": current_index, + "total": total_payload_count, + "skipping": True, + "data_import": self.data_import.name, + }, + user=xhiveframework.session.user, + ) + continue + + try: + start = timeit.default_timer() + doc = self.process_doc(doc) + processing_time = timeit.default_timer() - start + eta = self.get_eta(current_index, total_payload_count, processing_time) + + if self.console: + update_progress_bar( + f"Importing {self.doctype}: {total_payload_count} records", + current_index - 1, + total_payload_count, + ) + elif total_payload_count > 5: + xhiveframework.publish_realtime( + "data_import_progress", + { + "current": current_index, + "total": total_payload_count, + "docname": doc.name, + "data_import": self.data_import.name, + "success": True, + "row_indexes": row_indexes, + "eta": eta, + }, + user=xhiveframework.session.user, + ) + + create_import_log( + self.data_import.name, + log_index, + {"success": True, "docname": doc.name, "row_indexes": row_indexes}, + ) + + log_index += 1 + + if not self.data_import.status == "Partial Success": + self.data_import.db_set("status", "Partial Success") + + # commit after every successful import + xhiveframework.db.commit() + + except Exception: + messages = xhiveframework.local.message_log + xhiveframework.clear_messages() + + # rollback if exception + xhiveframework.db.rollback() + + create_import_log( + self.data_import.name, + log_index, + { + "success": False, + "exception": xhiveframework.get_traceback(), + "messages": messages, + "row_indexes": row_indexes, + }, + ) + + log_index += 1 + + # Logs are db inserted directly so will have to be fetched again + import_log = ( + xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success", "log_index"], + filters={"data_import": self.data_import.name}, + order_by="log_index", + ) + or [] + ) + + # set status + failures = [log for log in import_log if not log.get("success")] + if len(failures) == total_payload_count: + status = "Pending" + elif len(failures) > 0: + status = "Partial Success" + else: + status = "Success" + + if self.console: + self.print_import_log(import_log) + else: + self.data_import.db_set("status", status) + + self.after_import() + + return import_log + + def after_import(self): + xhiveframework.flags.in_import = False + xhiveframework.flags.mute_emails = False + + def process_doc(self, doc): + if self.import_type == INSERT: + return self.insert_record(doc) + elif self.import_type == UPDATE: + return self.update_record(doc) + + def insert_record(self, doc): + meta = xhiveframework.get_meta(self.doctype) + new_doc = xhiveframework.new_doc(self.doctype) + new_doc.update(doc) + + if not doc.name and (meta.autoname or "").lower() != "prompt": + # name can only be set directly if autoname is prompt + new_doc.set("name", None) + + new_doc.flags.updater_reference = { + "doctype": self.data_import.doctype, + "docname": self.data_import.name, + "label": _("via Data Import"), + } + + new_doc.insert() + if meta.is_submittable and self.data_import.submit_after_import: + new_doc.submit() + return new_doc + + def update_record(self, doc): + id_field = get_id_field(self.doctype) + existing_doc = xhiveframework.get_doc(self.doctype, doc.get(id_field.fieldname)) + + updated_doc = xhiveframework.get_doc(self.doctype, doc.get(id_field.fieldname)) + + updated_doc.update(doc) + + if get_diff(existing_doc, updated_doc): + # update doc if there are changes + updated_doc.flags.updater_reference = { + "doctype": self.data_import.doctype, + "docname": self.data_import.name, + "label": _("via Data Import"), + } + updated_doc.save() + return updated_doc + else: + # throw if no changes + xhiveframework.throw(_("No changes to update")) + + def get_eta(self, current, total, processing_time): + self.last_eta = getattr(self, "last_eta", 0) + remaining = total - current + eta = processing_time * remaining + if not self.last_eta or eta < self.last_eta: + self.last_eta = eta + return self.last_eta + + def export_errored_rows(self): + from xhiveframework.utils.csvutils import build_csv_response + + if not self.data_import: + return + + import_log = ( + xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success"], + filters={"data_import": self.data_import.name}, + order_by="log_index", + ) + or [] + ) + + failures = [log for log in import_log if not log.get("success")] + row_indexes = [] + for f in failures: + row_indexes.extend(json.loads(f.get("row_indexes", []))) + + # de duplicate + row_indexes = list(set(row_indexes)) + row_indexes.sort() + + header_row = [col.header_title for col in self.import_file.columns] + rows = [header_row] + rows += [row.data for row in self.import_file.data if row.row_number in row_indexes] + + build_csv_response(rows, _(self.doctype)) + + def export_import_log(self): + from xhiveframework.utils.csvutils import build_csv_response + + if not self.data_import: + return + + import_log = xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success", "messages", "exception", "docname"], + filters={"data_import": self.data_import.name}, + order_by="log_index", + ) + + header_row = ["Row Numbers", "Status", "Message", "Exception"] + + rows = [header_row] + + for log in import_log: + row_number = json.loads(log.get("row_indexes"))[0] + status = "Success" if log.get("success") else "Failure" + message = ( + "Successfully Imported {}".format(log.get("docname")) + if log.get("success") + else log.get("messages") + ) + exception = xhiveframework.utils.cstr(log.get("exception", "")) + rows += [[row_number, status, message, exception]] + + build_csv_response(rows, self.doctype) + + def print_import_log(self, import_log): + failed_records = [log for log in import_log if not log.success] + successful_records = [log for log in import_log if log.success] + + if successful_records: + print() + print(f"Successfully imported {len(successful_records)} records out of {len(import_log)}") + + if failed_records: + print(f"Failed to import {len(failed_records)} records") + file_name = f"{self.doctype}_import_on_{xhiveframework.utils.now()}.txt" + print("Check {} for errors".format(os.path.join("sites", file_name))) + text = "" + for w in failed_records: + text += "Row Indexes: {}\n".format(str(w.get("row_indexes", []))) + text += "Messages:\n{}\n".format("\n".join(w.get("messages", []))) + text += "Traceback:\n{}\n\n".format(w.get("exception")) + + with open(file_name, "w") as f: + f.write(text) + + def print_grouped_warnings(self, warnings): + warnings_by_row = {} + other_warnings = [] + for w in warnings: + if w.get("row"): + warnings_by_row.setdefault(w.get("row"), []).append(w) + else: + other_warnings.append(w) + + for row_number, warnings in warnings_by_row.items(): + print(f"Row {row_number}") + for w in warnings: + print(w.get("message")) + + for w in other_warnings: + print(w.get("message")) + + +class ImportFile: + def __init__(self, doctype, file, template_options=None, import_type=None, *, console=False): + self.doctype = doctype + self.template_options = template_options or xhiveframework._dict(column_to_field_map=xhiveframework._dict()) + self.column_to_field_map = self.template_options.column_to_field_map + self.import_type = import_type + self.warnings = [] + self.console = console + + self.file_doc = self.file_path = self.google_sheets_url = None + if isinstance(file, str): + if xhiveframework.db.exists("File", {"file_url": file}): + self.file_doc = xhiveframework.get_doc("File", {"file_url": file}) + elif "docs.google.com/spreadsheets" in file: + self.google_sheets_url = file + elif os.path.exists(file): + self.file_path = file + + if not self.file_doc and not self.file_path and not self.google_sheets_url: + xhiveframework.throw(_("Invalid template file for import")) + + self.raw_data = self.get_data_from_template_file() + self.parse_data_from_template() + + def get_data_from_template_file(self): + content = None + extension = None + + if self.file_doc: + parts = self.file_doc.get_extension() + extension = parts[1] + content = self.file_doc.get_content() + extension = extension.lstrip(".") + + elif self.file_path: + content, extension = self.read_file(self.file_path) + + elif self.google_sheets_url: + content = get_csv_content_from_google_sheets(self.google_sheets_url) + extension = "csv" + + if not content: + xhiveframework.throw(_("Invalid or corrupted content for import")) + + if not extension: + extension = "csv" + + if content: + return self.read_content(content, extension) + + def parse_data_from_template(self): + header = None + data = [] + + for i, row in enumerate(self.raw_data): + if all(v in INVALID_VALUES for v in row): + # empty row + continue + + if not header: + header = Header(i, row, self.doctype, self.raw_data[1:], self.column_to_field_map) + else: + row_obj = Row(i, row, self.doctype, header, self.import_type) + data.append(row_obj) + + self.header = header + self.columns = self.header.columns + self.data = data + + if len(data) < 1: + xhiveframework.throw( + _("Import template should contain a Header and atleast one row."), + title=_("Template Error"), + ) + + def get_data_for_import_preview(self): + """Adds a serial number column as the first column""" + + columns = [xhiveframework._dict({"header_title": "Sr. No", "skip_import": True})] + columns += [col.as_dict() for col in self.columns] + for col in columns: + # only pick useful fields in docfields to minimise the payload + if col.df: + col.df = { + "fieldtype": col.df.fieldtype, + "fieldname": col.df.fieldname, + "label": col.df.label, + "options": col.df.options, + "parent": col.df.parent, + "reqd": col.df.reqd, + "default": col.df.default, + "read_only": col.df.read_only, + } + + data = [[row.row_number, *row.as_list()] for row in self.data] + + warnings = self.get_warnings() + + out = xhiveframework._dict() + out.data = data + out.columns = columns + out.warnings = warnings + total_number_of_rows = len(out.data) + if total_number_of_rows > MAX_ROWS_IN_PREVIEW: + out.data = out.data[:MAX_ROWS_IN_PREVIEW] + out.max_rows_exceeded = True + out.max_rows_in_preview = MAX_ROWS_IN_PREVIEW + out.total_number_of_rows = total_number_of_rows + return out + + def get_payloads_for_import(self): + payloads = [] + # make a copy + data = list(self.data) + while data: + doc, rows, data = self.parse_next_row_for_import(data) + payloads.append(xhiveframework._dict(doc=doc, rows=rows)) + return payloads + + def parse_next_row_for_import(self, data): + """ + Parses rows that make up a doc. A doc maybe built from a single row or multiple rows. + Returns the doc, rows, and data without the rows. + """ + doctypes = self.header.doctypes + + # first row is included by default + first_row = data[0] + rows = [first_row] + + # if there are child doctypes, find the subsequent rows + if len(doctypes) > 1: + # subsequent rows that have blank values in parent columns + # are considered as child rows + parent_column_indexes = self.header.get_column_indexes(self.doctype) + + data_without_first_row = data[1:] + for row in data_without_first_row: + row_values = row.get_values(parent_column_indexes) + # if the row is blank, it's a child row doc + if all(v in INVALID_VALUES for v in row_values): + rows.append(row) + continue + # if we encounter a row which has values in parent columns, + # then it is the next doc + break + + parent_doc = None + for row in rows: + for doctype, table_df in doctypes: + if doctype == self.doctype and not parent_doc: + parent_doc = row.parse_doc(doctype) + + if doctype != self.doctype and table_df: + child_doc = row.parse_doc(doctype, parent_doc, table_df) + if child_doc is None: + continue + parent_doc[table_df.fieldname] = parent_doc.get(table_df.fieldname, []) + parent_doc[table_df.fieldname].append(child_doc) + + doc = parent_doc + + return doc, rows, data[len(rows) :] + + def get_warnings(self): + warnings = [] + + # ImportFile warnings + warnings += self.warnings + + # Column warnings + for col in self.header.columns: + warnings += col.warnings + + # Row warnings + for row in self.data: + warnings += row.warnings + + return warnings + + ###### + + def read_file(self, file_path: str): + extn = os.path.splitext(file_path)[1][1:] + + file_content = None + + if self.console: + file_content = xhiveframework.read_file(file_path, True) + return file_content, extn + + file_name = xhiveframework.db.get_value("File", {"file_url": file_path}) + if file_name: + file = xhiveframework.get_doc("File", file_name) + file_content = file.get_content() + + return file_content, extn + + def read_content(self, content, extension): + error_title = _("Template Error") + if extension not in ("csv", "xlsx", "xls"): + xhiveframework.throw(_("Import template should be of type .csv, .xlsx or .xls"), title=error_title) + + if extension == "csv": + data = read_csv_content(content) + elif extension == "xlsx": + data = read_xlsx_file_from_attached_file(fcontent=content) + elif extension == "xls": + data = read_xls_file_from_attached_file(content) + + return data + + +class Row: + def __init__(self, index, row, doctype, header, import_type): + self.index = index + self.row_number = index + 1 + self.doctype = doctype + self.data = row + self.header = header + self.import_type = import_type + self.warnings = [] + + len_row = len(self.data) + len_columns = len(self.header.columns) + if len_row != len_columns: + less_than_columns = len_row < len_columns + message = ( + "Row has less values than columns" + if less_than_columns + else "Row has more values than columns" + ) + self.warnings.append( + { + "row": self.row_number, + "message": message, + } + ) + + def parse_doc(self, doctype, parent_doc=None, table_df=None): + col_indexes = self.header.get_column_indexes(doctype, table_df) + values = self.get_values(col_indexes) + + if all(v in INVALID_VALUES for v in values): + # if all values are invalid, no need to parse it + return None + + columns = self.header.get_columns(col_indexes) + return self._parse_doc(doctype, columns, values, parent_doc, table_df) + + def _parse_doc(self, doctype, columns, values, parent_doc=None, table_df=None): + doc = xhiveframework._dict() + if self.import_type == INSERT: + # new_doc returns a dict with default values set + doc = xhiveframework.new_doc( + doctype, + parent_doc=parent_doc, + parentfield=table_df.fieldname if table_df else None, + as_dict=True, + ) + + # remove standard fields and __islocal + for key in xhiveframework.model.default_fields + xhiveframework.model.child_table_fields + ("__islocal",): + doc.pop(key, None) + + for col, value in zip(columns, values, strict=False): + df = col.df + if value in INVALID_VALUES: + value = None + + if value is not None: + value = self.validate_value(value, col) + + if value is not None: + doc[df.fieldname] = self.parse_value(value, col) + + is_table = xhiveframework.get_meta(doctype).istable + is_update = self.import_type == UPDATE + if is_table and is_update: + # check if the row already exists + # if yes, fetch the original doc so that it is not updated + # if no, create a new doc + id_field = get_id_field(doctype) + id_value = doc.get(id_field.fieldname) + if id_value and xhiveframework.db.exists(doctype, id_value): + existing_doc = xhiveframework.get_doc(doctype, id_value) + existing_doc.update(doc) + doc = existing_doc + else: + # for table rows being inserted in update + # create a new doc with defaults set + new_doc = xhiveframework.new_doc(doctype, as_dict=True) + new_doc.update(doc) + doc = new_doc + + return doc + + def validate_value(self, value, col): + df = col.df + if df.fieldtype == "Select": + select_options = get_select_options(df) + if select_options and cstr(value) not in select_options: + options_string = ", ".join(xhiveframework.bold(d) for d in select_options) + msg = _("Value must be one of {0}").format(options_string) + self.warnings.append( + { + "row": self.row_number, + "field": df_as_json(df), + "message": msg, + } + ) + return + + elif df.fieldtype == "Link": + exists = self.link_exists(value, df) + if not exists: + msg = _("Value {0} missing for {1}").format(xhiveframework.bold(value), xhiveframework.bold(df.options)) + self.warnings.append( + { + "row": self.row_number, + "field": df_as_json(df), + "message": msg, + } + ) + return + elif df.fieldtype in ["Date", "Datetime"]: + value = self.get_date(value, col) + if isinstance(value, str): + # value was not parsed as datetime object + self.warnings.append( + { + "row": self.row_number, + "col": col.column_number, + "field": df_as_json(df), + "message": _("Value {0} must in {1} format").format( + xhiveframework.bold(value), xhiveframework.bold(get_user_format(col.date_format)) + ), + } + ) + return + elif df.fieldtype == "Duration": + if not DURATION_PATTERN.match(value): + self.warnings.append( + { + "row": self.row_number, + "col": col.column_number, + "field": df_as_json(df), + "message": _("Value {0} must be in the valid duration format: d h m s").format( + xhiveframework.bold(value) + ), + } + ) + + return value + + def link_exists(self, value, df): + return bool(xhiveframework.db.exists(df.options, value, cache=True)) + + def parse_value(self, value, col): + df = col.df + if isinstance(value, datetime | date) and df.fieldtype in ["Date", "Datetime"]: + return value + + value = cstr(value) + + # convert boolean values to 0 or 1 + valid_check_values = ["t", "f", "true", "false", "yes", "no", "y", "n"] + if df.fieldtype == "Check" and value.lower().strip() in valid_check_values: + value = value.lower().strip() + value = 1 if value in ["t", "true", "y", "yes"] else 0 + + if df.fieldtype in ["Int", "Check"]: + value = cint(value) + elif df.fieldtype in ["Float", "Percent", "Currency"]: + value = flt(value) + elif df.fieldtype in ["Date", "Datetime"]: + value = self.get_date(value, col) + elif df.fieldtype == "Duration": + value = duration_to_seconds(value) + + return value + + def get_date(self, value, column): + if isinstance(value, datetime | date): + return value + + date_format = column.date_format + if date_format: + try: + return datetime.strptime(value, date_format) + except ValueError: + # ignore date values that dont match the format + # import will break for these values later + pass + return value + + def get_values(self, indexes): + return [self.data[i] for i in indexes] + + def get(self, index): + return self.data[index] + + def as_list(self): + return self.data + + +class Header(Row): + def __init__(self, index, row, doctype, raw_data, column_to_field_map=None): + self.index = index + self.row_number = index + 1 + self.data = row + self.doctype = doctype + column_to_field_map = column_to_field_map or xhiveframework._dict() + + self.seen = [] + self.columns = [] + + for j, header in enumerate(row): + column_values = [get_item_at_index(r, j) for r in raw_data] + map_to_field = column_to_field_map.get(str(j)) + column = Column(j, header, self.doctype, column_values, map_to_field, self.seen) + self.seen.append(header) + self.columns.append(column) + + doctypes = [] + for col in self.columns: + if not col.df: + continue + if col.df.parent == self.doctype: + doctypes.append((col.df.parent, None)) + else: + doctypes.append((col.df.parent, col.df.child_table_df)) + + self.doctypes = sorted(list(set(doctypes)), key=lambda x: -1 if x[0] == self.doctype else 1) + + def get_column_indexes(self, doctype, tablefield=None): + def is_table_field(df): + if tablefield: + return df.child_table_df.fieldname == tablefield.fieldname + return True + + return [ + col.index + for col in self.columns + if not col.skip_import and col.df and col.df.parent == doctype and is_table_field(col.df) + ] + + def get_columns(self, indexes): + return [self.columns[i] for i in indexes] + + +class Column: + def __init__(self, index, header, doctype, column_values, map_to_field=None, seen=None): + if seen is None: + seen = [] + self.index = index + self.column_number = index + 1 + self.doctype = doctype + self.header_title = header + self.column_values = column_values + self.map_to_field = map_to_field + self.seen = seen + + self.date_format = None + self.df = None + self.skip_import = None + self.warnings = [] + + self.meta = xhiveframework.get_meta(doctype) + self.parse() + self.validate_values() + + def parse(self): + header_title = self.header_title + column_number = str(self.column_number) + skip_import = False + + if self.map_to_field and self.map_to_field != "Don't Import": + df = get_df_for_column_header(self.doctype, self.map_to_field) + if df: + self.warnings.append( + { + "message": _("Mapping column {0} to field {1}").format( + xhiveframework.bold(header_title or "Untitled Column"), xhiveframework.bold(df.label) + ), + "type": "info", + } + ) + else: + self.warnings.append( + { + "message": _("Could not map column {0} to field {1}").format( + column_number, self.map_to_field + ), + "type": "info", + } + ) + else: + df = get_df_for_column_header(self.doctype, header_title) + # df = df_by_labels_and_fieldnames.get(header_title) + + if not df: + skip_import = True + else: + skip_import = False + + if header_title in self.seen: + self.warnings.append( + { + "col": column_number, + "message": _("Skipping Duplicate Column {0}").format(xhiveframework.bold(header_title)), + "type": "info", + } + ) + df = None + skip_import = True + elif self.map_to_field == "Don't Import": + skip_import = True + self.warnings.append( + { + "col": column_number, + "message": _("Skipping column {0}").format(xhiveframework.bold(header_title)), + "type": "info", + } + ) + elif header_title and not df: + self.warnings.append( + { + "col": column_number, + "message": _("Cannot match column {0} with any field").format(xhiveframework.bold(header_title)), + "type": "info", + } + ) + elif not header_title and not df: + self.warnings.append( + {"col": column_number, "message": _("Skipping Untitled Column"), "type": "info"} + ) + + self.df = df + self.skip_import = skip_import + + def guess_date_format_for_column(self): + """Guesses date format for a column by parsing all the values in the column, + getting the date format and then returning the one which has the maximum frequency + """ + + def guess_date_format(d): + if isinstance(d, datetime | date | time): + if self.df.fieldtype == "Date": + return "%Y-%m-%d" + if self.df.fieldtype == "Datetime": + return "%Y-%m-%d %H:%M:%S" + if self.df.fieldtype == "Time": + return "%H:%M:%S" + if isinstance(d, str): + return xhiveframework.utils.guess_date_format(d) + + date_formats = [guess_date_format(d) for d in self.column_values] + date_formats = [d for d in date_formats if d] + if not date_formats: + return + + unique_date_formats = set(date_formats) + max_occurred_date_format = max(unique_date_formats, key=date_formats.count) + + if len(unique_date_formats) > 1: + # fmt: off + message = _("The column {0} has {1} different date formats. Automatically setting {2} as the default format as it is the most common. Please change other values in this column to this format.") + # fmt: on + user_date_format = get_user_format(max_occurred_date_format) + self.warnings.append( + { + "col": self.column_number, + "message": message.format( + xhiveframework.bold(self.header_title), + len(unique_date_formats), + xhiveframework.bold(user_date_format), + ), + "type": "info", + } + ) + + return max_occurred_date_format + + def validate_values(self): + if not self.df: + return + + if self.skip_import: + return + + if not any(self.column_values): + return + + if self.df.fieldtype == "Link": + # find all values that dont exist + values = list({cstr(v) for v in self.column_values if v}) + exists = [cstr(d.name) for d in xhiveframework.get_all(self.df.options, filters={"name": ("in", values)})] + not_exists = list(set(values) - set(exists)) + if not_exists: + missing_values = ", ".join(not_exists) + message = _("The following values do not exist for {0}: {1}") + self.warnings.append( + { + "col": self.column_number, + "message": message.format(self.df.options, missing_values), + "type": "warning", + } + ) + elif self.df.fieldtype in ("Date", "Time", "Datetime"): + # guess date/time format + self.date_format = self.guess_date_format_for_column() + if not self.date_format: + if self.df.fieldtype == "Time": + self.date_format = "%H:%M:%S" + date_format = "HH:mm:ss" + else: + self.date_format = "%Y-%m-%d" + date_format = "yyyy-mm-dd" + + message = _( + "{0} format could not be determined from the values in this column. Defaulting to {1}." + ) + self.warnings.append( + { + "col": self.column_number, + "message": message.format(self.df.fieldtype, date_format), + "type": "info", + } + ) + elif self.df.fieldtype == "Select": + options = get_select_options(self.df) + if options: + values = {cstr(v) for v in self.column_values if v} + invalid = values - set(options) + if invalid: + valid_values = ", ".join(xhiveframework.bold(o) for o in options) + invalid_values = ", ".join(xhiveframework.bold(i) for i in invalid) + message = _("The following values are invalid: {0}. Values must be one of {1}") + self.warnings.append( + { + "col": self.column_number, + "message": message.format(invalid_values, valid_values), + } + ) + + def as_dict(self): + d = xhiveframework._dict() + d.index = self.index + d.column_number = self.column_number + d.doctype = self.doctype + d.header_title = self.header_title + d.map_to_field = self.map_to_field + d.date_format = self.date_format + d.df = self.df + if hasattr(self.df, "is_child_table_field"): + d.is_child_table_field = self.df.is_child_table_field + d.child_table_df = self.df.child_table_df + d.skip_import = self.skip_import + d.warnings = self.warnings + return d + + +def build_fields_dict_for_column_matching(parent_doctype): + """ + Build a dict with various keys to match with column headers and value as docfield + The keys can be label or fieldname + { + 'Customer': df1, + 'customer': df1, + 'Due Date': df2, + 'due_date': df2, + 'Item Code (Sales Invoice Item)': df3, + 'Sales Invoice Item:item_code': df3, + } + """ + + def get_standard_fields(doctype): + meta = xhiveframework.get_meta(doctype) + if meta.istable: + standard_fields = [ + {"label": "Parent", "fieldname": "parent"}, + {"label": "Parent Type", "fieldname": "parenttype"}, + {"label": "Parent Field", "fieldname": "parentfield"}, + {"label": "Row Index", "fieldname": "idx"}, + ] + else: + standard_fields = [ + {"label": "Owner", "fieldname": "owner"}, + {"label": "Document Status", "fieldname": "docstatus", "fieldtype": "Int"}, + ] + + out = [] + for df in standard_fields: + df = xhiveframework._dict(df) + df.parent = doctype + out.append(df) + return out + + parent_meta = xhiveframework.get_meta(parent_doctype) + out = {} + + # doctypes and fieldname if it is a child doctype + doctypes = [(parent_doctype, None)] + [(df.options, df) for df in parent_meta.get_table_fields()] + + for doctype, table_df in doctypes: + translated_table_label = _(table_df.label) if table_df else None + + # name field + name_df = xhiveframework._dict( + { + "fieldtype": "Data", + "fieldname": "name", + "label": "ID", + "reqd": 1, # self.import_type == UPDATE, + "parent": doctype, + } + ) + + if doctype == parent_doctype: + name_headers = ( + "name", # fieldname + "ID", # label + _("ID"), # translated label + ) + else: + name_headers = ( + f"{table_df.fieldname}.name", # fieldname + f"ID ({table_df.label})", # label + "{} ({})".format(_("ID"), translated_table_label), # translated label + ) + + name_df.is_child_table_field = True + name_df.child_table_df = table_df + + for header in name_headers: + out[header] = name_df + + fields = get_standard_fields(doctype) + xhiveframework.get_meta(doctype).fields + for df in fields: + fieldtype = df.fieldtype or "Data" + if fieldtype in no_value_fields: + continue + + label = (df.label or "").strip() + translated_label = _(label) + + if parent_doctype == doctype: + # for parent doctypes keys will be + # Label, fieldname, Label (fieldname) + + for header in (label, translated_label): + # if Label is already set, don't set it again + # in case of duplicate column headers + if header not in out: + out[header] = df + + for header in ( + df.fieldname, + f"{label} ({df.fieldname})", + f"{translated_label} ({df.fieldname})", + ): + out[header] = df + + else: + # for child doctypes keys will be + # Label (Table Field Label) + # table_field.fieldname + + # create a new df object to avoid mutation problems + if isinstance(df, dict): + new_df = xhiveframework._dict(df.copy()) + else: + new_df = df.as_dict() + + new_df.is_child_table_field = True + new_df.child_table_df = table_df + + for header in ( + # fieldname + f"{table_df.fieldname}.{df.fieldname}", + # label + f"{label} ({table_df.label})", + # translated label + f"{translated_label} ({translated_table_label})", + ): + out[header] = new_df + + # if autoname is based on field + # add an entry for "ID (Autoname Field)" + autoname_field = get_autoname_field(parent_doctype) + if autoname_field: + for header in ( + f"ID ({autoname_field.label})", # label + "{} ({})".format(_("ID"), _(autoname_field.label)), # translated label + # ID field should also map to the autoname field + "ID", + _("ID"), + "name", + ): + out[header] = autoname_field + + return out + + +def get_df_for_column_header(doctype, header): + def build_fields_dict_for_doctype(): + return build_fields_dict_for_column_matching(doctype) + + df_by_labels_and_fieldname = xhiveframework.cache.hget( + "data_import_column_header_map", doctype, generator=build_fields_dict_for_doctype + ) + return df_by_labels_and_fieldname.get(header) + + +# utilities + + +def get_id_field(doctype): + autoname_field = get_autoname_field(doctype) + if autoname_field: + return autoname_field + return xhiveframework._dict({"label": "ID", "fieldname": "name", "fieldtype": "Data"}) + + +def get_autoname_field(doctype): + meta = xhiveframework.get_meta(doctype) + if meta.autoname and meta.autoname.startswith("field:"): + fieldname = meta.autoname[len("field:") :] + return meta.get_field(fieldname) + + +def get_item_at_index(_list, i, default=None): + try: + a = _list[i] + except IndexError: + a = default + return a + + +def get_user_format(date_format): + return date_format.replace("%Y", "yyyy").replace("%y", "yy").replace("%m", "mm").replace("%d", "dd") + + +def df_as_json(df): + return { + "fieldname": df.fieldname, + "fieldtype": df.fieldtype, + "label": df.label, + "options": df.options, + "parent": df.parent, + "default": df.default, + } + + +def get_select_options(df): + return [d for d in (df.options or "").split("\n") if d] + + +def create_import_log(data_import, log_index, log_details): + xhiveframework.get_doc( + { + "doctype": "Data Import Log", + "log_index": log_index, + "success": log_details.get("success"), + "data_import": data_import, + "row_indexes": json.dumps(log_details.get("row_indexes")), + "docname": log_details.get("docname"), + "messages": json.dumps(log_details.get("messages", "[]")), + "exception": log_details.get("exception"), + } + ).db_insert() diff --git a/xhiveframework/core/doctype/data_import/patches/__init__.py b/xhiveframework/core/doctype/data_import/patches/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/data_import/patches/remove_stale_docfields_from_legacy_version.py b/xhiveframework/core/doctype/data_import/patches/remove_stale_docfields_from_legacy_version.py new file mode 100644 index 0000000..caf3f20 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/patches/remove_stale_docfields_from_legacy_version.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + """Remove stale docfields from legacy version""" + xhiveframework.db.delete("DocField", {"options": "Data Import", "parent": "Data Import Legacy"}) diff --git a/xhiveframework/core/doctype/data_import/test_data_import.py b/xhiveframework/core/doctype/data_import/test_data_import.py new file mode 100644 index 0000000..d6e72fd --- /dev/null +++ b/xhiveframework/core/doctype/data_import/test_data_import.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDataImport(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/data_import/test_exporter.py b/xhiveframework/core/doctype/data_import/test_exporter.py new file mode 100644 index 0000000..64a7b22 --- /dev/null +++ b/xhiveframework/core/doctype/data_import/test_exporter.py @@ -0,0 +1,100 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.data_import.exporter import Exporter +from xhiveframework.core.doctype.data_import.test_importer import create_doctype_if_not_exists +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +doctype_name = "DocType for Export" + + +class TestExporter(XhiveFrameworkTestCase): + def setUp(self): + create_doctype_if_not_exists(doctype_name) + + def test_exports_specified_fields(self): + if not xhiveframework.db.exists(doctype_name, "Test"): + doc = xhiveframework.get_doc( + doctype=doctype_name, + title="Test", + description="Test Description", + table_field_1=[ + {"child_title": "Child Title 1", "child_description": "Child Description 1"}, + {"child_title": "Child Title 2", "child_description": "Child Description 2"}, + ], + table_field_2=[ + {"child_2_title": "Child Title 1", "child_2_description": "Child Description 1"}, + ], + table_field_1_again=[ + { + "child_title": "Child Title 1 Again", + "child_description": "Child Description 1 Again", + }, + ], + ).insert() + else: + doc = xhiveframework.get_doc(doctype_name, "Test") + + e = Exporter( + doctype_name, + export_fields={ + doctype_name: ["title", "description", "number", "another_number"], + "table_field_1": ["name", "child_title", "child_description"], + "table_field_2": ["child_2_date", "child_2_number"], + "table_field_1_again": [ + "child_title", + "child_date", + "child_number", + "child_another_number", + ], + }, + export_data=True, + ) + csv_array = e.get_csv_array() + header_row = csv_array[0] + + self.assertEqual( + header_row, + [ + "Title", + "Description", + "Number", + "another_number", + "ID (Table Field 1)", + "Child Title (Table Field 1)", + "Child Description (Table Field 1)", + "Child 2 Date (Table Field 2)", + "Child 2 Number (Table Field 2)", + "Child Title (Table Field 1 Again)", + "Child Date (Table Field 1 Again)", + "Child Number (Table Field 1 Again)", + "table_field_1_again.child_another_number", + ], + ) + + table_field_1_row_1_name = doc.table_field_1[0].name + table_field_1_row_2_name = doc.table_field_1[1].name + # fmt: off + self.assertEqual( + csv_array[1], + ["Test", "Test Description", 0, 0, table_field_1_row_1_name, "Child Title 1", "Child Description 1", None, 0, "Child Title 1 Again", None, 0, 0] + ) + self.assertEqual( + csv_array[2], + ["", "", "", "", table_field_1_row_2_name, "Child Title 2", "Child Description 2", "", "", "", "", "", ""], + ) + # fmt: on + self.assertEqual(len(csv_array), 3) + + def test_export_csv_response(self): + e = Exporter( + doctype_name, + export_fields={doctype_name: ["title", "description"]}, + export_data=True, + file_type="CSV", + ) + e.build_response() + + self.assertTrue(xhiveframework.response["result"]) + self.assertEqual(xhiveframework.response["doctype"], doctype_name) + self.assertEqual(xhiveframework.response["type"], "csv") diff --git a/xhiveframework/core/doctype/data_import/test_importer.py b/xhiveframework/core/doctype/data_import/test_importer.py new file mode 100644 index 0000000..5751efc --- /dev/null +++ b/xhiveframework/core/doctype/data_import/test_importer.py @@ -0,0 +1,251 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.data_import.importer import Importer +from xhiveframework.tests.test_query_builder import db_type_is, run_only_if +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import format_duration, getdate + +doctype_name = "DocType for Import" + + +class TestImporter(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + create_doctype_if_not_exists( + doctype_name, + ) + + def test_data_import_from_file(self): + import_file = get_import_file("sample_import_file") + data_import = self.get_importer(doctype_name, import_file) + data_import.start_import() + + doc1 = xhiveframework.get_doc(doctype_name, "Test") + doc2 = xhiveframework.get_doc(doctype_name, "Test 2") + doc3 = xhiveframework.get_doc(doctype_name, "Test 3") + + self.assertEqual(doc1.description, "test description") + self.assertEqual(doc1.number, 1) + self.assertEqual(format_duration(doc1.duration), "3h") + + self.assertEqual(doc1.table_field_1[0].child_title, "child title") + self.assertEqual(doc1.table_field_1[0].child_description, "child description") + + self.assertEqual(doc1.table_field_1[1].child_title, "child title 2") + self.assertEqual(doc1.table_field_1[1].child_description, "child description 2") + + self.assertEqual(doc1.table_field_2[1].child_2_title, "title child") + self.assertEqual(doc1.table_field_2[1].child_2_date, getdate("2019-10-30")) + self.assertEqual(doc1.table_field_2[1].child_2_another_number, 5) + + self.assertEqual(doc1.table_field_1_again[0].child_title, "child title again") + self.assertEqual(doc1.table_field_1_again[1].child_title, "child title again 2") + self.assertEqual(doc1.table_field_1_again[1].child_date, getdate("2021-09-22")) + + self.assertEqual(doc2.description, "test description 2") + self.assertEqual(format_duration(doc2.duration), "4d 3h") + + self.assertEqual(doc3.another_number, 5) + self.assertEqual(format_duration(doc3.duration), "5d 5h 45m") + + def test_data_import_preview(self): + import_file = get_import_file("sample_import_file") + data_import = self.get_importer(doctype_name, import_file) + preview = data_import.get_preview_from_template() + + self.assertEqual(len(preview.data), 4) + self.assertEqual(len(preview.columns), 16) + + # ignored on postgres because myisam doesn't exist on pg + @run_only_if(db_type_is.MARIADB) + def test_data_import_without_mandatory_values(self): + import_file = get_import_file("sample_import_file_without_mandatory") + data_import = self.get_importer(doctype_name, import_file) + xhiveframework.clear_messages() + data_import.start_import() + data_import.reload() + + import_log = xhiveframework.get_all( + "Data Import Log", + fields=["row_indexes", "success", "messages", "exception", "docname"], + filters={"data_import": data_import.name}, + order_by="log_index", + ) + + self.assertEqual(xhiveframework.parse_json(import_log[0]["row_indexes"]), [2, 3]) + expected_error = ( + "Error: Child 1 of DocType for Import Row #1: Value missing for: Child Title" + ) + self.assertEqual( + xhiveframework.parse_json(xhiveframework.parse_json(import_log[0]["messages"])[0])["message"], expected_error + ) + expected_error = ( + "Error: Child 1 of DocType for Import Row #2: Value missing for: Child Title" + ) + self.assertEqual( + xhiveframework.parse_json(xhiveframework.parse_json(import_log[0]["messages"])[1])["message"], expected_error + ) + + self.assertEqual(xhiveframework.parse_json(import_log[1]["row_indexes"]), [4]) + self.assertEqual( + xhiveframework.parse_json(xhiveframework.parse_json(import_log[1]["messages"])[0])["message"], + "Title is required", + ) + + def test_data_import_update(self): + existing_doc = xhiveframework.get_doc( + doctype=doctype_name, + title=xhiveframework.generate_hash(length=8), + table_field_1=[{"child_title": "child title to update"}], + ) + existing_doc.save() + xhiveframework.db.commit() + + import_file = get_import_file("sample_import_file_for_update") + data_import = self.get_importer(doctype_name, import_file, update=True) + i = Importer(data_import.reference_doctype, data_import=data_import) + + # update child table id in template date + i.import_file.raw_data[1][4] = existing_doc.table_field_1[0].name + + # uppercase to check if autoname field isn't replaced in mariadb + if xhiveframework.db.db_type == "mariadb": + i.import_file.raw_data[1][0] = existing_doc.name.upper() + else: + i.import_file.raw_data[1][0] = existing_doc.name + + i.import_file.parse_data_from_template() + i.import_data() + + updated_doc = xhiveframework.get_doc(doctype_name, existing_doc.name) + self.assertEqual(existing_doc.title, updated_doc.title) + self.assertEqual(updated_doc.description, "test description") + self.assertEqual(updated_doc.table_field_1[0].child_title, "child title") + self.assertEqual(updated_doc.table_field_1[0].name, existing_doc.table_field_1[0].name) + self.assertEqual(updated_doc.table_field_1[0].child_description, "child description") + self.assertEqual(updated_doc.table_field_1_again[0].child_title, "child title again") + + def get_importer(self, doctype, import_file, update=False): + data_import = xhiveframework.new_doc("Data Import") + data_import.import_type = "Insert New Records" if not update else "Update Existing Records" + data_import.reference_doctype = doctype + data_import.import_file = import_file.file_url + data_import.insert() + # Commit so that the first import failure does not rollback the Data Import insert. + xhiveframework.db.commit() + + return data_import + + +def create_doctype_if_not_exists(doctype_name, force=False): + if force: + xhiveframework.delete_doc_if_exists("DocType", doctype_name) + xhiveframework.delete_doc_if_exists("DocType", "Child 1 of " + doctype_name) + xhiveframework.delete_doc_if_exists("DocType", "Child 2 of " + doctype_name) + + if xhiveframework.db.exists("DocType", doctype_name): + return + + # Child Table 1 + table_1_name = "Child 1 of " + doctype_name + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": table_1_name, + "module": "Custom", + "custom": 1, + "istable": 1, + "fields": [ + {"label": "Child Title", "fieldname": "child_title", "reqd": 1, "fieldtype": "Data"}, + {"label": "Child Description", "fieldname": "child_description", "fieldtype": "Small Text"}, + {"label": "Child Date", "fieldname": "child_date", "fieldtype": "Date"}, + {"label": "Child Number", "fieldname": "child_number", "fieldtype": "Int"}, + {"label": "Child Number", "fieldname": "child_another_number", "fieldtype": "Int"}, + ], + } + ).insert() + + # Child Table 2 + table_2_name = "Child 2 of " + doctype_name + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": table_2_name, + "module": "Custom", + "custom": 1, + "istable": 1, + "fields": [ + {"label": "Child 2 Title", "fieldname": "child_2_title", "reqd": 1, "fieldtype": "Data"}, + { + "label": "Child 2 Description", + "fieldname": "child_2_description", + "fieldtype": "Small Text", + }, + {"label": "Child 2 Date", "fieldname": "child_2_date", "fieldtype": "Date"}, + {"label": "Child 2 Number", "fieldname": "child_2_number", "fieldtype": "Int"}, + {"label": "Child 2 Number", "fieldname": "child_2_another_number", "fieldtype": "Int"}, + ], + } + ).insert() + + # Main Table + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": doctype_name, + "module": "Custom", + "custom": 1, + "autoname": "field:title", + "fields": [ + {"label": "Title", "fieldname": "title", "reqd": 1, "fieldtype": "Data"}, + {"label": "Description", "fieldname": "description", "fieldtype": "Small Text"}, + {"label": "Date", "fieldname": "date", "fieldtype": "Date"}, + {"label": "Duration", "fieldname": "duration", "fieldtype": "Duration"}, + {"label": "Number", "fieldname": "number", "fieldtype": "Int"}, + {"label": "Number", "fieldname": "another_number", "fieldtype": "Int"}, + { + "label": "Table Field 1", + "fieldname": "table_field_1", + "fieldtype": "Table", + "options": table_1_name, + }, + { + "label": "Table Field 2", + "fieldname": "table_field_2", + "fieldtype": "Table", + "options": table_2_name, + }, + { + "label": "Table Field 1 Again", + "fieldname": "table_field_1_again", + "fieldtype": "Table", + "options": table_1_name, + }, + ], + "permissions": [{"role": "System Manager"}], + } + ).insert() + + +def get_import_file(csv_file_name, force=False): + file_name = csv_file_name + ".csv" + _file = xhiveframework.db.exists("File", {"file_name": file_name}) + if force and _file: + xhiveframework.delete_doc_if_exists("File", _file) + + if xhiveframework.db.exists("File", {"file_name": file_name}): + f = xhiveframework.get_doc("File", {"file_name": file_name}) + else: + full_path = get_csv_file_path(file_name) + f = xhiveframework.get_doc( + doctype="File", content=xhiveframework.read_file(full_path), file_name=file_name, is_private=1 + ) + f.save(ignore_permissions=True) + + return f + + +def get_csv_file_path(file_name): + return xhiveframework.get_app_path("xhiveframework", "core", "doctype", "data_import", "fixtures", file_name) diff --git a/xhiveframework/core/doctype/data_import_log/__init__.py b/xhiveframework/core/doctype/data_import_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/data_import_log/data_import_log.js b/xhiveframework/core/doctype/data_import_log/data_import_log.js new file mode 100644 index 0000000..ff4ae63 --- /dev/null +++ b/xhiveframework/core/doctype/data_import_log/data_import_log.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Data Import Log", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/data_import_log/data_import_log.json b/xhiveframework/core/doctype/data_import_log/data_import_log.json new file mode 100644 index 0000000..b1d991f --- /dev/null +++ b/xhiveframework/core/doctype/data_import_log/data_import_log.json @@ -0,0 +1,84 @@ +{ + "actions": [], + "creation": "2021-12-25 16:12:20.205889", + "doctype": "DocType", + "editable_grid": 1, + "engine": "MyISAM", + "field_order": [ + "data_import", + "row_indexes", + "success", + "docname", + "messages", + "exception", + "log_index" + ], + "fields": [ + { + "fieldname": "data_import", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Data Import", + "options": "Data Import" + }, + { + "fieldname": "docname", + "fieldtype": "Data", + "label": "Reference Name" + }, + { + "fieldname": "exception", + "fieldtype": "Text", + "label": "Exception" + }, + { + "fieldname": "row_indexes", + "fieldtype": "Code", + "label": "Row Indexes", + "options": "JSON" + }, + { + "default": "0", + "fieldname": "success", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Success" + }, + { + "fieldname": "log_index", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Log Index" + }, + { + "fieldname": "messages", + "fieldtype": "Code", + "label": "Messages", + "options": "JSON" + } + ], + "in_create": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-12-29 11:19:19.646076", + "modified_by": "Administrator", + "module": "Core", + "name": "Data Import Log", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/data_import_log/data_import_log.py b/xhiveframework/core/doctype/data_import_log/data_import_log.py new file mode 100644 index 0000000..2a1d079 --- /dev/null +++ b/xhiveframework/core/doctype/data_import_log/data_import_log.py @@ -0,0 +1,25 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DataImportLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data_import: DF.Link | None + docname: DF.Data | None + exception: DF.Text | None + log_index: DF.Int + messages: DF.Code | None + row_indexes: DF.Code | None + success: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/data_import_log/test_data_import_log.py b/xhiveframework/core/doctype/data_import_log/test_data_import_log.py new file mode 100644 index 0000000..e7f82d3 --- /dev/null +++ b/xhiveframework/core/doctype/data_import_log/test_data_import_log.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDataImportLog(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/defaultvalue/README.md b/xhiveframework/core/doctype/defaultvalue/README.md new file mode 100644 index 0000000..327fb71 --- /dev/null +++ b/xhiveframework/core/doctype/defaultvalue/README.md @@ -0,0 +1 @@ +Child table for **User** and **Role** where default keys and values are stored. They can also be created from the **User Properties** page. \ No newline at end of file diff --git a/xhiveframework/core/doctype/defaultvalue/__init__.py b/xhiveframework/core/doctype/defaultvalue/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/defaultvalue/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/defaultvalue/defaultvalue.json b/xhiveframework/core/doctype/defaultvalue/defaultvalue.json new file mode 100644 index 0000000..22e2583 --- /dev/null +++ b/xhiveframework/core/doctype/defaultvalue/defaultvalue.json @@ -0,0 +1,47 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-02-22 01:27:32", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "defkey", + "defvalue" + ], + "fields": [ + { + "fieldname": "defkey", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Key", + "oldfieldname": "defkey", + "oldfieldtype": "Data", + "print_width": "200px", + "reqd": 1, + "width": "200px" + }, + { + "fieldname": "defvalue", + "fieldtype": "Text", + "in_list_view": 1, + "label": "Value", + "oldfieldname": "defvalue", + "oldfieldtype": "Text", + "print_width": "200px", + "width": "200px" + } + ], + "idx": 1, + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:54.832785", + "modified_by": "Administrator", + "module": "Core", + "name": "DefaultValue", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/defaultvalue/defaultvalue.py b/xhiveframework/core/doctype/defaultvalue/defaultvalue.py new file mode 100644 index 0000000..1af509a --- /dev/null +++ b/xhiveframework/core/doctype/defaultvalue/defaultvalue.py @@ -0,0 +1,39 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class DefaultValue(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + defkey: DF.Data + defvalue: DF.Text | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass + + +def on_doctype_update(): + """Create indexes for `tabDefaultValue` on `(parent, defkey)`""" + xhiveframework.db.commit() + xhiveframework.db.add_index( + doctype="DefaultValue", + fields=["parent", "defkey"], + index_name="defaultvalue_parent_defkey_index", + ) + + xhiveframework.db.add_index( + doctype="DefaultValue", + fields=["parent", "parenttype"], + index_name="defaultvalue_parent_parenttype_index", + ) diff --git a/xhiveframework/core/doctype/deleted_document/__init__.py b/xhiveframework/core/doctype/deleted_document/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/deleted_document/deleted_document.js b/xhiveframework/core/doctype/deleted_document/deleted_document.js new file mode 100644 index 0000000..cbb420b --- /dev/null +++ b/xhiveframework/core/doctype/deleted_document/deleted_document.js @@ -0,0 +1,22 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Deleted Document", { + refresh: function (frm) { + if (frm.doc.restored) { + frm.add_custom_button(__("Open"), function () { + xhiveframework.set_route("Form", frm.doc.deleted_doctype, frm.doc.new_name); + }); + } else { + frm.add_custom_button(__("Restore"), function () { + xhiveframework.call({ + method: "xhiveframework.core.doctype.deleted_document.deleted_document.restore", + args: { name: frm.doc.name }, + callback: function (r) { + frm.reload_doc(); + }, + }); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/deleted_document/deleted_document.json b/xhiveframework/core/doctype/deleted_document/deleted_document.json new file mode 100644 index 0000000..6b95a52 --- /dev/null +++ b/xhiveframework/core/doctype/deleted_document/deleted_document.json @@ -0,0 +1,81 @@ +{ + "actions": [], + "creation": "2016-12-29 12:59:48.638970", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "deleted_name", + "deleted_doctype", + "column_break_3", + "restored", + "new_name", + "section_break_6", + "data" + ], + "fields": [ + { + "fieldname": "deleted_name", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Deleted Name", + "read_only": 1 + }, + { + "fieldname": "deleted_doctype", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Deleted DocType", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "restored", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Restored", + "read_only": 1 + }, + { + "fieldname": "new_name", + "fieldtype": "Read Only", + "label": "New Name" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break" + }, + { + "fieldname": "data", + "fieldtype": "Code", + "label": "Data", + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2022-06-13 05:50:58.314908", + "modified_by": "Administrator", + "module": "Core", + "name": "Deleted Document", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "export": 1, + "read": 1, + "role": "System Manager" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "deleted_name", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/deleted_document/deleted_document.py b/xhiveframework/core/doctype/deleted_document/deleted_document.py new file mode 100644 index 0000000..0e839ea --- /dev/null +++ b/xhiveframework/core/doctype/deleted_document/deleted_document.py @@ -0,0 +1,92 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.doctype.bulk_update.bulk_update import show_progress +from xhiveframework.model.document import Document +from xhiveframework.model.workflow import get_workflow_name + + +class DeletedDocument(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data: DF.Code | None + deleted_doctype: DF.Data | None + deleted_name: DF.Data | None + new_name: DF.ReadOnly | None + restored: DF.Check + # end: auto-generated types + pass + + @staticmethod + def clear_old_logs(days=180): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Deleted Document") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + +@xhiveframework.whitelist() +def restore(name, alert=True): + deleted = xhiveframework.get_doc("Deleted Document", name) + + if deleted.restored: + xhiveframework.throw(_("Document {0} Already Restored").format(name), exc=xhiveframework.DocumentAlreadyRestored) + + doc = xhiveframework.get_doc(json.loads(deleted.data)) + + try: + doc.insert() + except xhiveframework.DocstatusTransitionError: + xhiveframework.msgprint(_("Cancelled Document restored as Draft")) + doc.docstatus = 0 + active_workflow = get_workflow_name(doc.doctype) + if active_workflow: + workflow_state_fieldname = xhiveframework.get_value("Workflow", active_workflow, "workflow_state_field") + if doc.get(workflow_state_fieldname): + doc.set(workflow_state_fieldname, None) + doc.insert() + + doc.add_comment("Edit", _("restored {0} as {1}").format(deleted.deleted_name, doc.name)) + + deleted.new_name = doc.name + deleted.restored = 1 + deleted.db_update() + + if alert: + xhiveframework.msgprint(_("Document Restored")) + + +@xhiveframework.whitelist() +def bulk_restore(docnames): + docnames = xhiveframework.parse_json(docnames) + message = _("Restoring Deleted Document") + restored, invalid, failed = [], [], [] + + for i, d in enumerate(docnames): + try: + show_progress(docnames, message, i + 1, d) + restore(d, alert=False) + xhiveframework.db.commit() + restored.append(d) + + except xhiveframework.DocumentAlreadyRestored: + xhiveframework.clear_last_message() + invalid.append(d) + + except Exception: + xhiveframework.clear_last_message() + failed.append(d) + xhiveframework.db.rollback() + + return {"restored": restored, "invalid": invalid, "failed": failed} diff --git a/xhiveframework/core/doctype/deleted_document/deleted_document_list.js b/xhiveframework/core/doctype/deleted_document/deleted_document_list.js new file mode 100644 index 0000000..df926d2 --- /dev/null +++ b/xhiveframework/core/doctype/deleted_document/deleted_document_list.js @@ -0,0 +1,50 @@ +xhiveframework.listview_settings["Deleted Document"] = { + onload: function (doclist) { + const action = () => { + const selected_docs = doclist.get_checked_items(); + if (selected_docs.length > 0) { + let docnames = selected_docs.map((doc) => doc.name); + xhiveframework.call({ + method: "xhiveframework.core.doctype.deleted_document.deleted_document.bulk_restore", + args: { docnames }, + callback: function (r) { + if (r.message) { + let body = (docnames) => { + const html = docnames.map((docname) => { + return `
  • ${docname}
  • `; + }); + return "
      " + html.join(""); + }; + + let message = (title, docnames) => { + return docnames.length > 0 ? title + body(docnames) + "
    " : ""; + }; + + const { restored, invalid, failed } = r.message; + const restored_summary = message( + __("Documents restored successfully"), + restored + ); + const invalid_summary = message( + __("Documents that were already restored"), + invalid + ); + const failed_summary = message( + __("Documents that failed to restore"), + failed + ); + const summary = restored_summary + invalid_summary + failed_summary; + + xhiveframework.msgprint(summary, __("Document Restoration Summary"), true); + + if (restored.length > 0) { + doclist.refresh(); + } + } + }, + }); + } + }; + doclist.page.add_actions_menu_item(__("Restore"), action, false); + }, +}; diff --git a/xhiveframework/core/doctype/deleted_document/test_deleted_document.py b/xhiveframework/core/doctype/deleted_document/test_deleted_document.py new file mode 100644 index 0000000..e672035 --- /dev/null +++ b/xhiveframework/core/doctype/deleted_document/test_deleted_document.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Deleted Document') + + +class TestDeletedDocument(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/docfield/README.md b/xhiveframework/core/doctype/docfield/README.md new file mode 100644 index 0000000..8c9caca --- /dev/null +++ b/xhiveframework/core/doctype/docfield/README.md @@ -0,0 +1,3 @@ +Represents a field of a DocType analogous to a table column in the database. DocFields represent the properties both the model and the view and hence may or may not have database columns associated (for example, Section Break does not have any column associated.) + +See Standard Field Types \ No newline at end of file diff --git a/xhiveframework/core/doctype/docfield/__init__.py b/xhiveframework/core/doctype/docfield/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/docfield/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/docfield/docfield.json b/xhiveframework/core/doctype/docfield/docfield.json new file mode 100644 index 0000000..f94a51a --- /dev/null +++ b/xhiveframework/core/doctype/docfield/docfield.json @@ -0,0 +1,580 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-02-22 01:27:33", + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "label_and_type", + "label", + "fieldtype", + "fieldname", + "precision", + "length", + "non_negative", + "hide_days", + "hide_seconds", + "reqd", + "is_virtual", + "search_index", + "column_break_18", + "options", + "sort_options", + "show_dashboard", + "defaults_section", + "default", + "column_break_6", + "fetch_from", + "fetch_if_empty", + "visibility_section", + "hidden", + "bold", + "allow_in_quick_entry", + "translatable", + "print_hide", + "print_hide_if_no_value", + "report_hide", + "column_break_28", + "depends_on", + "collapsible", + "collapsible_depends_on", + "hide_border", + "list__search_settings_section", + "in_list_view", + "in_standard_filter", + "in_preview", + "column_break_35", + "in_filter", + "in_global_search", + "permissions", + "read_only", + "allow_on_submit", + "ignore_user_permissions", + "allow_bulk_edit", + "column_break_13", + "permlevel", + "ignore_xss_filter", + "constraints_section", + "unique", + "no_copy", + "set_only_once", + "remember_last_selected_value", + "column_break_38", + "mandatory_depends_on", + "read_only_depends_on", + "display", + "print_width", + "width", + "max_height", + "columns", + "column_break_22", + "description", + "documentation_url", + "oldfieldname", + "oldfieldtype" + ], + "fields": [ + { + "fieldname": "label_and_type", + "fieldtype": "Section Break" + }, + { + "bold": 1, + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "oldfieldname": "label", + "oldfieldtype": "Data", + "print_width": "163", + "search_index": 1, + "width": "163" + }, + { + "bold": 1, + "default": "Data", + "fieldname": "fieldtype", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Type", + "oldfieldname": "fieldtype", + "oldfieldtype": "Select", + "options": "Autocomplete\nAttach\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\nJSON\nLink\nLong Text\nMarkdown Editor\nPassword\nPercent\nPhone\nRead Only\nRating\nSection Break\nSelect\nSignature\nSmall Text\nTab Break\nTable\nTable MultiSelect\nText\nText Editor\nTime", + "reqd": 1, + "search_index": 1, + "sort_options": 1 + }, + { + "bold": 1, + "fieldname": "fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Name", + "oldfieldname": "fieldname", + "oldfieldtype": "Data", + "search_index": 1 + }, + { + "default": "0", + "depends_on": "eval:!in_list([\"Section Break\", \"Column Break\", \"Button\", \"HTML\"], doc.fieldtype)", + "fieldname": "reqd", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Mandatory", + "oldfieldname": "reqd", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "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", + "print_hide": 1 + }, + { + "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" + }, + { + "default": "0", + "fieldname": "search_index", + "fieldtype": "Check", + "label": "Index", + "oldfieldname": "search_index", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "depends_on": "eval:!doc.is_virtual", + "fieldname": "in_list_view", + "fieldtype": "Check", + "label": "In List View", + "print_width": "70px", + "width": "70px" + }, + { + "default": "0", + "fieldname": "in_standard_filter", + "fieldtype": "Check", + "label": "In List 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", + "depends_on": "eval:!in_list(['Table', 'Table MultiSelect'], doc.fieldtype);", + "fieldname": "in_preview", + "fieldtype": "Check", + "label": "In Preview" + }, + { + "default": "0", + "fieldname": "allow_in_quick_entry", + "fieldtype": "Check", + "label": "Allow in Quick Entry" + }, + { + "default": "0", + "fieldname": "bold", + "fieldtype": "Check", + "label": "Bold" + }, + { + "default": "0", + "depends_on": "eval:['Data', 'Select', 'Text', 'Small Text', 'Text Editor'].includes(doc.fieldtype)", + "fieldname": "translatable", + "fieldtype": "Check", + "label": "Translatable" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype===\"Section Break\"", + "fieldname": "collapsible", + "fieldtype": "Check", + "label": "Collapsible", + "length": 255 + }, + { + "depends_on": "eval:doc.fieldtype==\"Section Break\" && doc.collapsible", + "fieldname": "collapsible_depends_on", + "fieldtype": "Code", + "label": "Collapsible Depends On (JS)", + "max_height": "3rem", + "options": "JS" + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "description": "For Links, enter the DocType as range.\nFor Select, enter list of Options, each on a new line.", + "fieldname": "options", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Options", + "oldfieldname": "options", + "oldfieldtype": "Text" + }, + { + "fieldname": "default", + "fieldtype": "Small Text", + "label": "Default", + "max_height": "3rem", + "oldfieldname": "default", + "oldfieldtype": "Text" + }, + { + "fieldname": "fetch_from", + "fieldtype": "Small Text", + "label": "Fetch From" + }, + { + "default": "0", + "description": "If unchecked, the value will always be re-fetched on save.", + "fieldname": "fetch_if_empty", + "fieldtype": "Check", + "label": "Fetch on Save if Empty" + }, + { + "fieldname": "permissions", + "fieldtype": "Section Break", + "label": "Permissions" + }, + { + "fieldname": "depends_on", + "fieldtype": "Code", + "label": "Display Depends On (JS)", + "length": 255, + "max_height": "3rem", + "oldfieldname": "depends_on", + "oldfieldtype": "Data", + "options": "JS" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden", + "oldfieldname": "hidden", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "read_only", + "fieldtype": "Check", + "label": "Read Only", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "unique", + "fieldtype": "Check", + "label": "Unique" + }, + { + "default": "0", + "fieldname": "set_only_once", + "fieldtype": "Check", + "label": "Set only once" + }, + { + "default": "0", + "depends_on": "eval: doc.fieldtype == \"Table\"", + "fieldname": "allow_bulk_edit", + "fieldtype": "Check", + "label": "Allow Bulk Edit" + }, + { + "fieldname": "column_break_13", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:!in_list(['Section Break', 'Column Break', 'Tab Break'], doc.fieldtype)", + "fieldname": "permlevel", + "fieldtype": "Int", + "label": "Perm Level", + "oldfieldname": "permlevel", + "oldfieldtype": "Int", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "ignore_user_permissions", + "fieldtype": "Check", + "label": "Ignore User Permissions" + }, + { + "default": "0", + "depends_on": "eval: parent.is_submittable", + "fieldname": "allow_on_submit", + "fieldtype": "Check", + "label": "Allow on Submit", + "oldfieldname": "allow_on_submit", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "report_hide", + "fieldtype": "Check", + "label": "Report Hide", + "oldfieldname": "report_hide", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "depends_on": "eval:(doc.fieldtype == 'Link')", + "fieldname": "remember_last_selected_value", + "fieldtype": "Check", + "label": "Remember Last Selected Value" + }, + { + "default": "0", + "description": "Don't 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" + }, + { + "fieldname": "display", + "fieldtype": "Section Break", + "label": "Display" + }, + { + "default": "0", + "fieldname": "in_filter", + "fieldtype": "Check", + "label": "In Filter", + "oldfieldname": "in_filter", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "no_copy", + "fieldtype": "Check", + "label": "No Copy", + "oldfieldname": "no_copy", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "print_hide", + "fieldtype": "Check", + "label": "Print Hide", + "oldfieldname": "print_hide", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "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", + "label": "Print Width", + "length": 10 + }, + { + "fieldname": "width", + "fieldtype": "Data", + "label": "Width", + "length": 10, + "oldfieldname": "width", + "oldfieldtype": "Data", + "print_width": "50px", + "width": "50px" + }, + { + "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": "column_break_22", + "fieldtype": "Column Break" + }, + { + "fieldname": "description", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Description", + "oldfieldname": "description", + "oldfieldtype": "Text", + "print_width": "300px", + "width": "300px" + }, + { + "fieldname": "oldfieldname", + "fieldtype": "Data", + "hidden": 1, + "oldfieldname": "oldfieldname", + "oldfieldtype": "Data" + }, + { + "fieldname": "oldfieldtype", + "fieldtype": "Data", + "hidden": 1, + "oldfieldname": "oldfieldtype", + "oldfieldtype": "Data" + }, + { + "fieldname": "mandatory_depends_on", + "fieldtype": "Code", + "label": "Mandatory Depends On (JS)", + "max_height": "3rem", + "options": "JS" + }, + { + "fieldname": "read_only_depends_on", + "fieldtype": "Code", + "label": "Read Only Depends On (JS)", + "max_height": "3rem", + "options": "JS" + }, + { + "fieldname": "column_break_38", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Duration'", + "fieldname": "hide_days", + "fieldtype": "Check", + "label": "Hide Days" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Duration'", + "fieldname": "hide_seconds", + "fieldtype": "Check", + "label": "Hide Seconds" + }, + { + "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": "column_break_18", + "fieldtype": "Column Break" + }, + { + "fieldname": "defaults_section", + "fieldtype": "Section Break", + "label": "Defaults", + "max_height": "2rem" + }, + { + "fieldname": "visibility_section", + "fieldtype": "Section Break", + "label": "Visibility" + }, + { + "fieldname": "column_break_28", + "fieldtype": "Column Break" + }, + { + "fieldname": "constraints_section", + "fieldtype": "Section Break", + "label": "Constraints" + }, + { + "fieldname": "max_height", + "fieldtype": "Data", + "label": "Max Height", + "length": 10 + }, + { + "fieldname": "list__search_settings_section", + "fieldtype": "Section Break", + "label": "List / Search Settings" + }, + { + "fieldname": "column_break_35", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype===\"Tab Break\"", + "fieldname": "show_dashboard", + "fieldtype": "Check", + "label": "Show Dashboard" + }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Virtual" + }, + { + "depends_on": "eval:!in_list([\"Tab Break\", \"Section Break\", \"Column Break\", \"Button\", \"HTML\"], doc.fieldtype)", + "fieldname": "documentation_url", + "fieldtype": "Data", + "label": "Documentation URL", + "options": "URL" + }, + { + "default": "0", + "depends_on": "eval: doc.fieldtype === 'Select'", + "fieldname": "sort_options", + "fieldtype": "Check", + "label": "Sort Options" + } + ], + "idx": 1, + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2024-02-01 15:55:44.007917", + "modified_by": "Administrator", + "module": "Core", + "name": "DocField", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "ASC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/docfield/docfield.py b/xhiveframework/core/doctype/docfield/docfield.py new file mode 100644 index 0000000..cdbc45d --- /dev/null +++ b/xhiveframework/core/doctype/docfield/docfield.py @@ -0,0 +1,153 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class DocField(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow_bulk_edit: DF.Check + allow_in_quick_entry: DF.Check + allow_on_submit: DF.Check + bold: DF.Check + collapsible: DF.Check + collapsible_depends_on: DF.Code | None + columns: DF.Int + default: DF.SmallText | None + depends_on: DF.Code | None + description: DF.SmallText | None + documentation_url: DF.Data | None + fetch_from: DF.SmallText | None + fetch_if_empty: DF.Check + fieldname: DF.Data | None + fieldtype: DF.Literal[ + "Autocomplete", + "Attach", + "Attach Image", + "Barcode", + "Button", + "Check", + "Code", + "Color", + "Column Break", + "Currency", + "Data", + "Date", + "Datetime", + "Duration", + "Dynamic Link", + "Float", + "Fold", + "Geolocation", + "Heading", + "HTML", + "HTML Editor", + "Icon", + "Image", + "Int", + "JSON", + "Link", + "Long Text", + "Markdown Editor", + "Password", + "Percent", + "Phone", + "Read Only", + "Rating", + "Section Break", + "Select", + "Signature", + "Small Text", + "Tab Break", + "Table", + "Table MultiSelect", + "Text", + "Text Editor", + "Time", + ] + hidden: DF.Check + hide_border: DF.Check + hide_days: DF.Check + hide_seconds: DF.Check + ignore_user_permissions: DF.Check + ignore_xss_filter: DF.Check + in_filter: DF.Check + in_global_search: DF.Check + in_list_view: DF.Check + in_preview: DF.Check + in_standard_filter: DF.Check + is_virtual: DF.Check + label: DF.Data | None + length: DF.Int + mandatory_depends_on: DF.Code | None + max_height: DF.Data | None + no_copy: DF.Check + non_negative: DF.Check + oldfieldname: DF.Data | None + oldfieldtype: DF.Data | None + options: DF.SmallText | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + permlevel: DF.Int + precision: DF.Literal["", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + print_hide: DF.Check + print_hide_if_no_value: DF.Check + print_width: DF.Data | None + read_only: DF.Check + read_only_depends_on: DF.Code | None + remember_last_selected_value: DF.Check + report_hide: DF.Check + reqd: DF.Check + search_index: DF.Check + set_only_once: DF.Check + show_dashboard: DF.Check + sort_options: DF.Check + translatable: DF.Check + unique: DF.Check + width: DF.Data | None + # end: auto-generated types + + def get_link_doctype(self): + """Returns the Link doctype for the docfield (if applicable) + if fieldtype is Link: Returns "options" + if fieldtype is Table MultiSelect: Returns "options" of the Link field in the Child Table + """ + if self.fieldtype == "Link": + return self.options + + if self.fieldtype == "Table MultiSelect": + table_doctype = self.options + + return xhiveframework.db.get_value( + "DocField", + { + "fieldtype": "Link", + "parenttype": "DocType", + "parent": table_doctype, + "in_list_view": 1, + }, + "options", + ) + + def get_select_options(self): + if self.fieldtype == "Select": + options = self.options or "" + return [d for d in options.split("\n") if d] + + def __repr__(self): + unsaved = "unsaved" if not self.name else "" + doctype = self.__class__.__name__ + + docstatus = f" docstatus={self.docstatus}" if self.docstatus else "" + parent = f" parent={self.parent}" if getattr(self, "parent", None) else "" + + return f"<{self.fieldtype}{doctype}: {self.fieldname}{docstatus}{parent}{unsaved}>" diff --git a/xhiveframework/core/doctype/docperm/__init__.py b/xhiveframework/core/doctype/docperm/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/docperm/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/docperm/docperm.json b/xhiveframework/core/doctype/docperm/docperm.json new file mode 100644 index 0000000..3ce49c4 --- /dev/null +++ b/xhiveframework/core/doctype/docperm/docperm.json @@ -0,0 +1,221 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-02-22 01:27:33", + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "role_and_level", + "role", + "if_owner", + "column_break_2", + "permlevel", + "section_break_4", + "select", + "read", + "write", + "create", + "delete", + "column_break_8", + "submit", + "cancel", + "amend", + "additional_permissions", + "report", + "export", + "import", + "column_break_19", + "share", + "print", + "email" + ], + "fields": [ + { + "fieldname": "role_and_level", + "fieldtype": "Section Break", + "label": "Role and Level" + }, + { + "fieldname": "role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Role", + "oldfieldname": "role", + "oldfieldtype": "Link", + "options": "Role", + "print_width": "150px", + "reqd": 1, + "width": "150px" + }, + { + "default": "0", + "description": "Apply this rule if the User is the Owner", + "fieldname": "if_owner", + "fieldtype": "Check", + "label": "If user is the owner" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "permlevel", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Level", + "oldfieldname": "permlevel", + "oldfieldtype": "Int", + "print_width": "40px", + "width": "40px" + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break", + "label": "Permissions" + }, + { + "default": "1", + "fieldname": "read", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Read", + "oldfieldname": "read", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "1", + "fieldname": "write", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Write", + "oldfieldname": "write", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "1", + "fieldname": "create", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Create", + "oldfieldname": "create", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "1", + "fieldname": "delete", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Delete" + }, + { + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "submit", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Submit", + "oldfieldname": "submit", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "cancel", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Cancel", + "oldfieldname": "cancel", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "default": "0", + "fieldname": "amend", + "fieldtype": "Check", + "label": "Amend", + "oldfieldname": "amend", + "oldfieldtype": "Check", + "print_width": "32px", + "width": "32px" + }, + { + "fieldname": "additional_permissions", + "fieldtype": "Section Break", + "label": "Additional Permissions" + }, + { + "default": "1", + "fieldname": "report", + "fieldtype": "Check", + "label": "Report", + "print_width": "32px", + "width": "32px" + }, + { + "default": "1", + "fieldname": "export", + "fieldtype": "Check", + "label": "Export" + }, + { + "default": "0", + "fieldname": "import", + "fieldtype": "Check", + "label": "Import" + }, + { + "fieldname": "column_break_19", + "fieldtype": "Column Break" + }, + { + "default": "1", + "fieldname": "share", + "fieldtype": "Check", + "label": "Share" + }, + { + "default": "1", + "fieldname": "print", + "fieldtype": "Check", + "label": "Print" + }, + { + "default": "1", + "fieldname": "email", + "fieldtype": "Check", + "label": "Email" + }, + { + "default": "0", + "fieldname": "select", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Select" + } + ], + "idx": 1, + "istable": 1, + "links": [], + "modified": "2023-02-20 13:21:45.071310", + "modified_by": "Administrator", + "module": "Core", + "name": "DocPerm", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "ASC" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/docperm/docperm.py b/xhiveframework/core/doctype/docperm/docperm.py new file mode 100644 index 0000000..87f42dd --- /dev/null +++ b/xhiveframework/core/doctype/docperm/docperm.py @@ -0,0 +1,36 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class DocPerm(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + amend: DF.Check + cancel: DF.Check + create: DF.Check + delete: DF.Check + email: DF.Check + export: DF.Check + if_owner: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + permlevel: DF.Int + print: DF.Check + read: DF.Check + report: DF.Check + role: DF.Link + select: DF.Check + share: DF.Check + submit: DF.Check + write: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/docshare/__init__.py b/xhiveframework/core/doctype/docshare/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/docshare/docshare.js b/xhiveframework/core/doctype/docshare/docshare.js new file mode 100644 index 0000000..4735204 --- /dev/null +++ b/xhiveframework/core/doctype/docshare/docshare.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("DocShare", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/docshare/docshare.json b/xhiveframework/core/doctype/docshare/docshare.json new file mode 100644 index 0000000..e3581f5 --- /dev/null +++ b/xhiveframework/core/doctype/docshare/docshare.json @@ -0,0 +1,113 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "hash", + "creation": "2015-02-04 04:33:36.330477", + "description": "Internal record of document shares", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "user", + "share_doctype", + "share_name", + "read", + "write", + "share", + "submit", + "everyone", + "notify_by_email" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "search_index": 1 + }, + { + "fieldname": "share_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "share_name", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Document Name", + "options": "share_doctype", + "reqd": 1, + "search_index": 1 + }, + { + "default": "0", + "fieldname": "read", + "fieldtype": "Check", + "label": "Read" + }, + { + "default": "0", + "fieldname": "write", + "fieldtype": "Check", + "label": "Write" + }, + { + "default": "0", + "fieldname": "share", + "fieldtype": "Check", + "label": "Share" + }, + { + "default": "0", + "fieldname": "everyone", + "fieldtype": "Check", + "label": "Everyone", + "search_index": 1 + }, + { + "default": "1", + "fieldname": "notify_by_email", + "fieldtype": "Check", + "label": "Notify by email", + "print_hide": 1 + }, + { + "default": "0", + "fieldname": "submit", + "fieldtype": "Check", + "label": "Submit" + } + ], + "in_create": 1, + "links": [], + "modified": "2023-06-15 18:02:51.877533", + "modified_by": "Administrator", + "module": "Core", + "name": "DocShare", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "export": 1, + "import": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/docshare/docshare.py b/xhiveframework/core/doctype/docshare/docshare.py new file mode 100644 index 0000000..0b1434a --- /dev/null +++ b/xhiveframework/core/doctype/docshare/docshare.py @@ -0,0 +1,97 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, get_fullname + +exclude_from_linked_with = True + + +class DocShare(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + everyone: DF.Check + notify_by_email: DF.Check + read: DF.Check + share: DF.Check + share_doctype: DF.Link + share_name: DF.DynamicLink + submit: DF.Check + user: DF.Link | None + write: DF.Check + # end: auto-generated types + no_feed_on_delete = True + + def validate(self): + self.validate_user() + self.check_share_permission() + self.check_is_submittable() + self.cascade_permissions_downwards() + self.get_doc().run_method("validate_share", self) + + def cascade_permissions_downwards(self): + if self.share or self.write or self.submit: + self.read = 1 + if self.submit: + self.write = 1 + + def get_doc(self): + if not getattr(self, "_doc", None): + self._doc = xhiveframework.get_doc(self.share_doctype, self.share_name) + return self._doc + + def validate_user(self): + if self.everyone: + self.user = None + elif not self.user: + xhiveframework.throw(_("User is mandatory for Share"), xhiveframework.MandatoryError) + + def check_share_permission(self): + if not self.flags.ignore_share_permission and not xhiveframework.has_permission( + self.share_doctype, "share", self.get_doc() + ): + xhiveframework.throw(_('You need to have "Share" permission'), xhiveframework.PermissionError) + + def check_is_submittable(self): + if self.submit and not cint(xhiveframework.db.get_value("DocType", self.share_doctype, "is_submittable")): + xhiveframework.throw( + _("Cannot share {0} with submit permission as the doctype {1} is not submittable").format( + xhiveframework.bold(self.share_name), xhiveframework.bold(self.share_doctype) + ) + ) + + def after_insert(self): + doc = self.get_doc() + owner = get_fullname(self.owner) + + if self.everyone: + doc.add_comment("Shared", _("{0} shared this document with everyone").format(owner)) + else: + doc.add_comment( + "Shared", _("{0} shared this document with {1}").format(owner, get_fullname(self.user)) + ) + + def on_trash(self): + if not self.flags.ignore_share_permission: + self.check_share_permission() + + self.get_doc().add_comment( + "Unshared", + _("{0} un-shared this document with {1}").format( + get_fullname(self.owner), get_fullname(self.user) + ), + ) + + +def on_doctype_update(): + """Add index in `tabDocShare` for `(user, share_doctype)`""" + xhiveframework.db.add_index("DocShare", ["user", "share_doctype"]) + xhiveframework.db.add_index("DocShare", ["share_doctype", "share_name"]) diff --git a/xhiveframework/core/doctype/docshare/test_docshare.py b/xhiveframework/core/doctype/docshare/test_docshare.py new file mode 100644 index 0000000..9db1678 --- /dev/null +++ b/xhiveframework/core/doctype/docshare/test_docshare.py @@ -0,0 +1,226 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +import xhiveframework.share +from xhiveframework.automation.doctype.auto_repeat.test_auto_repeat import create_submittable_doctype +from xhiveframework.tests.utils import XhiveFrameworkTestCase, change_settings + +test_dependencies = ["User"] + + +class TestDocShare(XhiveFrameworkTestCase): + def setUp(self): + self.user = "test@example.com" + self.event = xhiveframework.get_doc( + { + "doctype": "Event", + "subject": "test share event", + "starts_on": "2015-01-01 10:00:00", + "event_type": "Private", + } + ).insert() + + def tearDown(self): + xhiveframework.set_user("Administrator") + self.event.delete() + + def test_add(self): + # user not shared + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user)) + xhiveframework.share.add("Event", self.event.name, self.user) + self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", self.user)) + + def test_doc_permission(self): + xhiveframework.set_user(self.user) + + self.assertFalse(self.event.has_permission()) + + xhiveframework.set_user("Administrator") + xhiveframework.share.add("Event", self.event.name, self.user) + + xhiveframework.set_user(self.user) + # PERF: All share permission check should happen with maximum 1 query. + with self.assertRowsRead(1): + self.assertTrue(self.event.has_permission()) + + second_event = xhiveframework.get_doc( + { + "doctype": "Event", + "subject": "test share event 2", + "starts_on": "2015-01-01 10:00:00", + "event_type": "Private", + } + ).insert() + xhiveframework.share.add("Event", second_event.name, self.user) + with self.assertRowsRead(1): + self.assertTrue(self.event.has_permission()) + + def test_list_permission(self): + xhiveframework.set_user(self.user) + with self.assertRaises(xhiveframework.PermissionError): + xhiveframework.get_list("Web Page") + + xhiveframework.set_user("Administrator") + doc = xhiveframework.new_doc("Web Page") + doc.update({"title": "test document for docshare permissions"}) + doc.insert() + xhiveframework.share.add("Web Page", doc.name, self.user) + + xhiveframework.set_user(self.user) + self.assertEqual(len(xhiveframework.get_list("Web Page")), 1) + + doc.delete(ignore_permissions=True) + with self.assertRaises(xhiveframework.PermissionError): + xhiveframework.get_list("Web Page") + + def test_share_permission(self): + xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1) + + xhiveframework.set_user(self.user) + self.assertTrue(self.event.has_permission("share")) + + # test cascade + self.assertTrue(self.event.has_permission("read")) + self.assertTrue(self.event.has_permission("write")) + + def test_set_permission(self): + xhiveframework.share.add("Event", self.event.name, self.user) + + xhiveframework.set_user(self.user) + self.assertFalse(self.event.has_permission("share")) + + xhiveframework.set_user("Administrator") + xhiveframework.share.set_permission("Event", self.event.name, self.user, "share") + + xhiveframework.set_user(self.user) + self.assertTrue(self.event.has_permission("share")) + + def test_permission_to_share(self): + xhiveframework.set_user(self.user) + self.assertRaises(xhiveframework.PermissionError, xhiveframework.share.add, "Event", self.event.name, self.user) + + xhiveframework.set_user("Administrator") + xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1) + + # test not raises + xhiveframework.set_user(self.user) + xhiveframework.share.add("Event", self.event.name, "test1@example.com", write=1, share=1) + + def test_remove_share(self): + xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1) + + xhiveframework.set_user(self.user) + self.assertTrue(self.event.has_permission("share")) + + xhiveframework.set_user("Administrator") + xhiveframework.share.remove("Event", self.event.name, self.user) + + xhiveframework.set_user(self.user) + self.assertFalse(self.event.has_permission("share")) + + def test_share_with_everyone(self): + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user)) + + xhiveframework.share.set_permission("Event", self.event.name, None, "read", everyone=1) + self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", self.user)) + self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", "test1@example.com")) + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "Guest")) + + xhiveframework.share.set_permission("Event", self.event.name, None, "read", value=0, everyone=1) + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user)) + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "test1@example.com")) + self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "Guest")) + + def test_share_with_submit_perm(self): + doctype = "Test DocShare with Submit" + create_submittable_doctype(doctype, submit_perms=0) + + submittable_doc = xhiveframework.get_doc(dict(doctype=doctype, test="test docshare with submit")).insert() + + xhiveframework.set_user(self.user) + self.assertFalse(xhiveframework.has_permission(doctype, "submit", user=self.user)) + + xhiveframework.set_user("Administrator") + xhiveframework.share.add(doctype, submittable_doc.name, self.user, submit=1) + + xhiveframework.set_user(self.user) + self.assertTrue(xhiveframework.has_permission(doctype, "submit", doc=submittable_doc.name, user=self.user)) + + # test cascade + self.assertTrue(xhiveframework.has_permission(doctype, "read", doc=submittable_doc.name, user=self.user)) + self.assertTrue(xhiveframework.has_permission(doctype, "write", doc=submittable_doc.name, user=self.user)) + + xhiveframework.share.remove(doctype, submittable_doc.name, self.user) + + def test_share_int_pk(self): + test_doc = xhiveframework.new_doc("Console Log") + + test_doc.insert() + xhiveframework.share.add("Console Log", test_doc.name, self.user) + + xhiveframework.set_user(self.user) + self.assertIn( + str(test_doc.name), [str(name) for name in xhiveframework.get_list("Console Log", pluck="name")] + ) + + test_doc.reload() + self.assertTrue(test_doc.has_permission("read")) + + @change_settings("System Settings", {"disable_document_sharing": 1}) + def test_share_disabled_add(self): + "Test if user loses share access on disabling share globally." + xhiveframework.share.add("Event", self.event.name, self.user, share=1) # Share as admin + xhiveframework.set_user(self.user) + + # User does not have share access although given to them + self.assertFalse(self.event.has_permission("share")) + self.assertRaises( + xhiveframework.PermissionError, xhiveframework.share.add, "Event", self.event.name, "test1@example.com" + ) + + @change_settings("System Settings", {"disable_document_sharing": 1}) + def test_share_disabled_add_with_ignore_permissions(self): + xhiveframework.share.add("Event", self.event.name, self.user, share=1) + xhiveframework.set_user(self.user) + + # User does not have share access although given to them + self.assertFalse(self.event.has_permission("share")) + + # Test if behaviour is consistent for developer overrides + xhiveframework.share.add_docshare( + "Event", self.event.name, "test1@example.com", flags={"ignore_share_permission": True} + ) + + @change_settings("System Settings", {"disable_document_sharing": 1}) + def test_share_disabled_set_permission(self): + xhiveframework.share.add("Event", self.event.name, self.user, share=1) + xhiveframework.set_user(self.user) + + # User does not have share access although given to them + self.assertFalse(self.event.has_permission("share")) + self.assertRaises( + xhiveframework.PermissionError, + xhiveframework.share.set_permission, + "Event", + self.event.name, + "test1@example.com", + "read", + ) + + @change_settings("System Settings", {"disable_document_sharing": 1}) + def test_share_disabled_assign_to(self): + """ + Assigning a document to a user without access must not share the document, + if sharing disabled. + """ + from xhiveframework.desk.form.assign_to import add + + xhiveframework.share.add("Event", self.event.name, self.user, share=1) + xhiveframework.set_user(self.user) + + self.assertRaises( + xhiveframework.ValidationError, + add, + {"doctype": "Event", "name": self.event.name, "assign_to": ["test1@example.com"]}, + ) diff --git a/xhiveframework/core/doctype/docshare/test_records.json b/xhiveframework/core/doctype/docshare/test_records.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/xhiveframework/core/doctype/docshare/test_records.json @@ -0,0 +1 @@ +[] diff --git a/xhiveframework/core/doctype/doctype/README.md b/xhiveframework/core/doctype/doctype/README.md new file mode 100644 index 0000000..3922a7e --- /dev/null +++ b/xhiveframework/core/doctype/doctype/README.md @@ -0,0 +1,15 @@ +DocType is the basic building block of an application and encompasses all the three elements i.e. model, view and controller. It represents a: + +- Table in the database +- Form in the application +- Controller (class) to execute business logic + +#### Single Type + +DocTypes can be of "Single" type where they do not represent a table, and only one instance is maintained. This can be used where the DocType is required only for its view features or to store some configurations in one place. + +#### Child Tables + +DocTypes can be child tables of other DocTypes. In such cases, they must defined `parent`, `parenttype` and `parentfield` properties to uniquely identify its placement. + +In the parent DocType, the position of a child in the field sequence is defined by the `Table` field type. \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype/__init__.py b/xhiveframework/core/doctype/doctype/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/doctype/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/doctype/boilerplate/__init__.py b/xhiveframework/core/doctype/doctype/boilerplate/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/doctype/boilerplate/controller._py b/xhiveframework/core/doctype/doctype/boilerplate/controller._py new file mode 100644 index 0000000..3e6508d --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/controller._py @@ -0,0 +1,9 @@ +# Copyright (c) {year}, {app_publisher} and contributors +# For license information, please see license.txt + +# import xhiveframework +{base_class_import} + + +class {classname}({base_class}): + {custom_controller} diff --git a/xhiveframework/core/doctype/doctype/boilerplate/controller.js b/xhiveframework/core/doctype/doctype/boilerplate/controller.js new file mode 100644 index 0000000..70261cf --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/controller.js @@ -0,0 +1,8 @@ +// Copyright (c) {year}, {app_publisher} and contributors +// For license information, please see license.txt + +// xhiveframework.ui.form.on("{doctype}", {{ +// refresh(frm) {{ + +// }}, +// }}); diff --git a/xhiveframework/core/doctype/doctype/boilerplate/controller_list.html b/xhiveframework/core/doctype/doctype/boilerplate/controller_list.html new file mode 100644 index 0000000..bc56ceb --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/controller_list.html @@ -0,0 +1,34 @@ +
    +
    +
    + {{%= list.get_avatar_and_id(doc) %}} + + + + {{%= doc.text %}} + + + {{% if(doc.check) {{ %}} + + + + {{% }} %}} + + + + {{%= doc.status %}} + +
    +
    + +
    + {{% var completed = doc.completed, title = __("Completed") %}} + {{% include "templates/form_grid/includes/progress.html" %}} +
    +
    diff --git a/xhiveframework/core/doctype/doctype/boilerplate/controller_list.js b/xhiveframework/core/doctype/doctype/boilerplate/controller_list.js new file mode 100644 index 0000000..936ebac --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/controller_list.js @@ -0,0 +1,5 @@ +/* eslint-disable */ +// xhiveframework.listview_settings["{doctype}"] = {{ +// add_fields: ["status"], +// filters: [["status","=", "Open"]], +// }}; diff --git a/xhiveframework/core/doctype/doctype/boilerplate/templates/controller.html b/xhiveframework/core/doctype/doctype/boilerplate/templates/controller.html new file mode 100644 index 0000000..412368d --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/templates/controller.html @@ -0,0 +1,7 @@ +{{% extends "templates/web.html" %}} + +{{% block page_content %}} +

    {{{{ title }}}}

    +{{% endblock %}} + + \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype/boilerplate/templates/controller_row.html b/xhiveframework/core/doctype/doctype/boilerplate/templates/controller_row.html new file mode 100644 index 0000000..66fe744 --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/templates/controller_row.html @@ -0,0 +1,4 @@ + + diff --git a/xhiveframework/core/doctype/doctype/boilerplate/test_controller._py b/xhiveframework/core/doctype/doctype/boilerplate/test_controller._py new file mode 100644 index 0000000..99f1d0f --- /dev/null +++ b/xhiveframework/core/doctype/doctype/boilerplate/test_controller._py @@ -0,0 +1,9 @@ +# Copyright (c) {year}, {app_publisher} and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class Test{classname}(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/doctype/doctype.js b/xhiveframework/core/doctype/doctype/doctype.js new file mode 100644 index 0000000..ded8a1b --- /dev/null +++ b/xhiveframework/core/doctype/doctype/doctype.js @@ -0,0 +1,186 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.ui.form.on("DocType", { + onload: function (frm) { + if (frm.is_new() && !frm.doc?.fields) { + xhiveframework.listview_settings["DocType"].new_doctype_dialog(); + } + }, + + before_save: function (frm) { + let form_builder = xhiveframework.form_builder; + if (form_builder?.store) { + let fields = form_builder.store.update_fields(); + + // if fields is a string, it means there is an error + if (typeof fields === "string") { + xhiveframework.throw(fields); + } + } + }, + + after_save: function (frm) { + if ( + xhiveframework.form_builder && + xhiveframework.form_builder.doctype === frm.doc.name && + xhiveframework.form_builder.store + ) { + xhiveframework.form_builder.store.fetch(); + } + }, + + refresh: function (frm) { + frm.set_query("role", "permissions", function (doc) { + if (doc.custom && xhiveframework.session.user != "Administrator") { + return { + query: "xhiveframework.core.doctype.role.role.role_query", + filters: [["Role", "name", "!=", "All"]], + }; + } + }); + + if (xhiveframework.session.user !== "Administrator" || !xhiveframework.boot.developer_mode) { + if (frm.is_new()) { + frm.set_value("custom", 1); + } + frm.toggle_enable("custom", 0); + frm.toggle_enable("is_virtual", 0); + frm.toggle_enable("beta", 0); + } + + if (!frm.is_new() && !frm.doc.istable) { + if (frm.doc.issingle) { + frm.add_custom_button(__("Go to {0}", [__(frm.doc.name)]), () => { + window.open(`/app/${xhiveframework.router.slug(frm.doc.name)}`); + }); + } else { + frm.add_custom_button(__("Go to {0} List", [__(frm.doc.name)]), () => { + window.open(`/app/${xhiveframework.router.slug(frm.doc.name)}`); + }); + } + } + + const customize_form_link = "Customize Form"; + if (!xhiveframework.boot.developer_mode && !frm.doc.custom) { + // make the document read-only + frm.set_read_only(); + frm.dashboard.clear_comment(); + frm.dashboard.add_comment( + __("DocTypes can not be modified, please use {0} instead", [customize_form_link]), + "blue", + true + ); + } else if (xhiveframework.boot.developer_mode) { + frm.dashboard.clear_comment(); + let msg = __( + "This site is running in developer mode. Any change made here will be updated in code." + ); + msg += "
    "; + msg += __("If you just want to customize for your site, use {0} instead.", [ + customize_form_link, + ]); + frm.dashboard.add_comment(msg, "yellow", true); + } + + if (frm.is_new()) { + frm.events.set_default_permission(frm); + frm.set_value("default_view", "List"); + } else { + frm.toggle_enable("engine", 0); + } + + // set label for "In List View" for child tables + frm.get_docfield("fields", "in_list_view").label = frm.doc.istable + ? __("In Grid View") + : __("In List View"); + + frm.cscript.autoname(frm); + frm.cscript.set_naming_rule_description(frm); + frm.trigger("setup_default_views"); + + render_form_builder(frm); + }, + + istable: (frm) => { + if (frm.doc.istable && frm.is_new()) { + frm.set_value("default_view", null); + } else if (!frm.doc.istable && !frm.is_new()) { + frm.events.set_default_permission(frm); + } + }, + + set_default_permission: (frm) => { + if (!(frm.doc.permissions && frm.doc.permissions.length)) { + frm.add_child("permissions", { role: "System Manager" }); + } + }, + + is_tree: (frm) => { + frm.trigger("setup_default_views"); + }, + + is_calendar_and_gantt: (frm) => { + frm.trigger("setup_default_views"); + }, + + setup_default_views: (frm) => { + xhiveframework.model.set_default_views_for_doctype(frm.doc.name, frm); + }, + + on_tab_change: (frm) => { + let current_tab = frm.get_active_tab().label; + + if (current_tab === "Form") { + frm.footer.wrapper.hide(); + frm.form_wrapper.find(".form-message").hide(); + frm.form_wrapper.addClass("mb-1"); + } else { + frm.footer.wrapper.show(); + frm.form_wrapper.find(".form-message").show(); + frm.form_wrapper.removeClass("mb-1"); + } + }, +}); + +xhiveframework.ui.form.on("DocField", { + form_render(frm, doctype, docname) { + frm.trigger("setup_fetch_from_fields", doctype, docname); + }, + + fieldtype: function (frm) { + frm.trigger("max_attachments"); + }, + + fields_add: (frm) => { + frm.trigger("setup_default_views"); + }, +}); + +function render_form_builder(frm) { + if (xhiveframework.form_builder && xhiveframework.form_builder.doctype === frm.doc.name) { + xhiveframework.form_builder.setup_page_actions(); + xhiveframework.form_builder.store.fetch(); + return; + } + + if (xhiveframework.form_builder) { + xhiveframework.form_builder.wrapper = $(frm.fields_dict["form_builder"].wrapper); + xhiveframework.form_builder.frm = frm; + xhiveframework.form_builder.doctype = frm.doc.name; + xhiveframework.form_builder.customize = false; + xhiveframework.form_builder.init(true); + xhiveframework.form_builder.store.fetch(); + } else { + xhiveframework.require("form_builder.bundle.js").then(() => { + xhiveframework.form_builder = new xhiveframework.ui.FormBuilder({ + wrapper: $(frm.fields_dict["form_builder"].wrapper), + frm: frm, + doctype: frm.doc.name, + customize: false, + }); + }); + } +} + +extend_cscript(cur_frm.cscript, new xhiveframework.model.DocTypeController({ frm: cur_frm })); diff --git a/xhiveframework/core/doctype/doctype/doctype.json b/xhiveframework/core/doctype/doctype/doctype.json new file mode 100644 index 0000000..25d4bd7 --- /dev/null +++ b/xhiveframework/core/doctype/doctype/doctype.json @@ -0,0 +1,790 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2013-02-18 13:36:19", + "description": "DocType is a Table / Form in the application.", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "form_builder_tab", + "form_builder", + "settings_tab", + "sb0", + "module", + "is_submittable", + "istable", + "issingle", + "is_tree", + "is_calendar_and_gantt", + "editable_grid", + "quick_entry", + "cb01", + "track_changes", + "track_seen", + "track_views", + "custom", + "beta", + "is_virtual", + "queue_in_background", + "sb1", + "naming_rule", + "autoname", + "allow_rename", + "column_break_15", + "description", + "documentation", + "form_settings_section", + "image_field", + "timeline_field", + "nsm_parent_field", + "max_attachments", + "column_break_23", + "hide_toolbar", + "allow_copy", + "allow_import", + "allow_events_in_timeline", + "allow_auto_repeat", + "make_attachments_public", + "view_settings", + "title_field", + "show_title_field_in_link", + "translated_doctype", + "search_fields", + "default_print_format", + "sort_field", + "sort_order", + "default_view", + "force_re_route_to_default_view", + "column_break_29", + "document_type", + "icon", + "color", + "show_preview_popup", + "show_name_in_global_search", + "email_settings_sb", + "default_email_template", + "column_break_51", + "email_append_to", + "sender_field", + "sender_name_field", + "subject_field", + "sb2", + "permissions", + "restrict_to_domain", + "read_only", + "in_create", + "actions_section", + "actions", + "links_section", + "links", + "document_states_section", + "states", + "web_view", + "has_web_view", + "allow_guest_to_view", + "index_web_pages_for_search", + "route", + "is_published_field", + "website_search_field", + "advanced", + "engine", + "migration_hash", + "fields_section", + "fields", + "connections_tab" + ], + "fields": [ + { + "fieldname": "sb0", + "fieldtype": "Section Break", + "oldfieldtype": "Section Break" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Module", + "oldfieldname": "module", + "oldfieldtype": "Link", + "options": "Module Def", + "reqd": 1, + "search_index": 1 + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "Once submitted, submittable documents cannot be changed. They can only be Cancelled and Amended.", + "fieldname": "is_submittable", + "fieldtype": "Check", + "label": "Is Submittable" + }, + { + "default": "0", + "description": "Child Tables are shown as a Grid in other DocTypes", + "fieldname": "istable", + "fieldtype": "Check", + "in_standard_filter": 1, + "label": "Is Child Table", + "oldfieldname": "istable", + "oldfieldtype": "Check" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "Single Types have only one record no tables associated. Values are stored in tabSingles", + "fieldname": "issingle", + "fieldtype": "Check", + "in_standard_filter": 1, + "label": "Is Single", + "oldfieldname": "issingle", + "oldfieldtype": "Check", + "set_only_once": 1 + }, + { + "default": "1", + "depends_on": "istable", + "fieldname": "editable_grid", + "fieldtype": "Check", + "label": "Editable Grid" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable && !doc.issingle", + "description": "Open a dialog with mandatory fields to create a new record quickly", + "fieldname": "quick_entry", + "fieldtype": "Check", + "label": "Quick Entry" + }, + { + "fieldname": "cb01", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "If enabled, changes to the document are tracked and shown in timeline", + "fieldname": "track_changes", + "fieldtype": "Check", + "label": "Track Changes" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "If enabled, the document is marked as seen, the first time a user opens it", + "fieldname": "track_seen", + "fieldtype": "Check", + "label": "Track Seen" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "If enabled, document views are tracked, this can happen multiple times", + "fieldname": "track_views", + "fieldtype": "Check", + "label": "Track Views" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "label": "Custom?" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "fieldname": "beta", + "fieldtype": "Check", + "label": "Beta" + }, + { + "fieldname": "fields", + "fieldtype": "Table", + "label": "Fields", + "oldfieldname": "fields", + "oldfieldtype": "Table", + "options": "DocField" + }, + { + "fieldname": "sb1", + "fieldtype": "Section Break", + "label": "Naming" + }, + { + "description": "Naming Options:\n
    1. field:[fieldname] - By Field
    2. autoincrement - Uses Databases' Auto Increment feature
    3. naming_series: - By Naming Series (field called naming_series must be present)
    4. Prompt - Prompt user for a name
    5. [series] - Series by prefix (separated by a dot); for example PRE.#####
    6. \n
    7. format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####} - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.
    ", + "fieldname": "autoname", + "fieldtype": "Data", + "label": "Auto Name", + "oldfieldname": "autoname", + "oldfieldtype": "Data" + }, + { + "fieldname": "column_break_15", + "fieldtype": "Column Break" + }, + { + "fieldname": "description", + "fieldtype": "Small Text", + "label": "Description", + "oldfieldname": "description", + "oldfieldtype": "Text" + }, + { + "collapsible": 1, + "depends_on": "eval:!doc.istable", + "fieldname": "form_settings_section", + "fieldtype": "Section Break", + "label": "Form Settings" + }, + { + "description": "Must be of type \"Attach Image\"", + "fieldname": "image_field", + "fieldtype": "Data", + "label": "Image Field" + }, + { + "depends_on": "eval:!doc.istable", + "description": "Comments and Communications will be associated with this linked document", + "fieldname": "timeline_field", + "fieldtype": "Data", + "label": "Timeline Field" + }, + { + "fieldname": "max_attachments", + "fieldtype": "Int", + "label": "Max Attachments", + "oldfieldname": "max_attachments", + "oldfieldtype": "Int" + }, + { + "fieldname": "column_break_23", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "hide_toolbar", + "fieldtype": "Check", + "label": "Hide Sidebar, Menu, and Comments", + "oldfieldname": "hide_toolbar", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "allow_copy", + "fieldtype": "Check", + "label": "Hide Copy", + "oldfieldname": "allow_copy", + "oldfieldtype": "Check" + }, + { + "default": "1", + "depends_on": "eval:doc.naming_rule !== \"Autoincrement\"", + "fieldname": "allow_rename", + "fieldtype": "Check", + "label": "Allow Rename", + "oldfieldname": "allow_rename", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "allow_import", + "fieldtype": "Check", + "label": "Allow Import (via Data Import Tool)" + }, + { + "default": "0", + "fieldname": "allow_events_in_timeline", + "fieldtype": "Check", + "label": "Allow events in timeline" + }, + { + "default": "0", + "fieldname": "allow_auto_repeat", + "fieldtype": "Check", + "label": "Allow Auto Repeat" + }, + { + "collapsible": 1, + "depends_on": "eval:!doc.istable", + "fieldname": "view_settings", + "fieldtype": "Section Break", + "label": "View Settings" + }, + { + "depends_on": "eval:!doc.istable", + "fieldname": "title_field", + "fieldtype": "Data", + "label": "Title Field", + "mandatory_depends_on": "eval:doc.show_title_field_in_link" + }, + { + "depends_on": "eval:!doc.istable", + "fieldname": "search_fields", + "fieldtype": "Data", + "label": "Search Fields", + "oldfieldname": "search_fields", + "oldfieldtype": "Data" + }, + { + "fieldname": "default_print_format", + "fieldtype": "Data", + "label": "Default Print Format" + }, + { + "default": "modified", + "depends_on": "eval:!doc.istable", + "fieldname": "sort_field", + "fieldtype": "Data", + "label": "Default Sort Field" + }, + { + "default": "DESC", + "depends_on": "eval:!doc.istable", + "fieldname": "sort_order", + "fieldtype": "Select", + "label": "Default Sort Order", + "options": "ASC\nDESC" + }, + { + "fieldname": "column_break_29", + "fieldtype": "Column Break" + }, + { + "fieldname": "document_type", + "fieldtype": "Select", + "label": "Show in Module Section", + "oldfieldname": "document_type", + "oldfieldtype": "Select", + "options": "\nDocument\nSetup\nSystem\nOther" + }, + { + "fieldname": "icon", + "fieldtype": "Data", + "label": "Icon" + }, + { + "fieldname": "color", + "fieldtype": "Data", + "label": "Color" + }, + { + "default": "0", + "fieldname": "show_preview_popup", + "fieldtype": "Check", + "label": "Show Preview Popup" + }, + { + "default": "0", + "fieldname": "show_name_in_global_search", + "fieldtype": "Check", + "label": "Make \"name\" searchable in Global Search" + }, + { + "depends_on": "eval:!doc.istable", + "fieldname": "sb2", + "fieldtype": "Section Break", + "label": "Permission Rules" + }, + { + "fieldname": "permissions", + "fieldtype": "Table", + "label": "Permissions", + "oldfieldname": "permissions", + "oldfieldtype": "Table", + "options": "DocPerm" + }, + { + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict To Domain", + "options": "Domain" + }, + { + "default": "0", + "fieldname": "read_only", + "fieldtype": "Check", + "label": "User Cannot Search", + "oldfieldname": "read_only", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "in_create", + "fieldtype": "Check", + "label": "User Cannot Create", + "oldfieldname": "in_create", + "oldfieldtype": "Check" + }, + { + "depends_on": "eval:doc.custom===0 && !doc.istable", + "fieldname": "web_view", + "fieldtype": "Section Break", + "label": "Web View" + }, + { + "default": "0", + "fieldname": "has_web_view", + "fieldtype": "Check", + "label": "Has Web View" + }, + { + "default": "0", + "depends_on": "has_web_view", + "fieldname": "allow_guest_to_view", + "fieldtype": "Check", + "label": "Allow Guest to View" + }, + { + "depends_on": "eval:!doc.istable", + "fieldname": "route", + "fieldtype": "Data", + "label": "Route" + }, + { + "depends_on": "has_web_view", + "fieldname": "is_published_field", + "fieldtype": "Data", + "label": "Is Published Field" + }, + { + "collapsible": 1, + "fieldname": "advanced", + "fieldtype": "Section Break", + "hidden": 1, + "label": "Advanced" + }, + { + "default": "InnoDB", + "depends_on": "eval:!doc.issingle", + "fieldname": "engine", + "fieldtype": "Select", + "label": "Database Engine", + "options": "InnoDB\nMyISAM" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "Tree structures are implemented using Nested Set", + "fieldname": "is_tree", + "fieldtype": "Check", + "label": "Is Tree" + }, + { + "depends_on": "is_tree", + "fieldname": "nsm_parent_field", + "fieldtype": "Data", + "label": "Parent Field (Tree)" + }, + { + "description": "URL for documentation or help", + "fieldname": "documentation", + "fieldtype": "Data", + "label": "Documentation Link" + }, + { + "collapsible": 1, + "collapsible_depends_on": "actions", + "depends_on": "eval:!doc.istable", + "fieldname": "actions_section", + "fieldtype": "Section Break", + "label": "Actions" + }, + { + "fieldname": "actions", + "fieldtype": "Table", + "label": "Actions", + "options": "DocType Action" + }, + { + "collapsible": 1, + "collapsible_depends_on": "links", + "depends_on": "eval:!doc.istable", + "fieldname": "links_section", + "fieldtype": "Section Break", + "label": "Linked Documents" + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "DocType Link" + }, + { + "depends_on": "email_append_to", + "fieldname": "subject_field", + "fieldtype": "Data", + "label": "Subject Field" + }, + { + "depends_on": "email_append_to", + "fieldname": "sender_field", + "fieldtype": "Data", + "label": "Sender Email Field", + "mandatory_depends_on": "email_append_to" + }, + { + "default": "0", + "fieldname": "email_append_to", + "fieldtype": "Check", + "label": "Allow document creation via Email" + }, + { + "collapsible": 1, + "depends_on": "eval:!doc.istable", + "fieldname": "email_settings_sb", + "fieldtype": "Section Break", + "label": "Email Settings" + }, + { + "default": "1", + "fieldname": "index_web_pages_for_search", + "fieldtype": "Check", + "label": "Index Web Pages for Search" + }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Is Virtual" + }, + { + "fieldname": "default_email_template", + "fieldtype": "Link", + "label": "Default Email Template", + "options": "Email Template" + }, + { + "fieldname": "column_break_51", + "fieldtype": "Column Break" + }, + { + "depends_on": "has_web_view", + "fieldname": "website_search_field", + "fieldtype": "Data", + "label": "Website Search Field" + }, + { + "fieldname": "naming_rule", + "fieldtype": "Select", + "label": "Naming Rule", + "length": 40, + "options": "\nSet by user\nAutoincrement\nBy fieldname\nBy \"Naming Series\" field\nExpression\nExpression (old style)\nRandom\nBy script" + }, + { + "fieldname": "migration_hash", + "fieldtype": "Data", + "hidden": 1 + }, + { + "fieldname": "states", + "fieldtype": "Table", + "label": "States", + "options": "DocType State" + }, + { + "collapsible": 1, + "depends_on": "eval:!doc.istable", + "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" + }, + { + "default": "0", + "fieldname": "translated_doctype", + "fieldtype": "Check", + "label": "Translate Link Fields" + }, + { + "default": "0", + "fieldname": "make_attachments_public", + "fieldtype": "Check", + "label": "Make Attachments Public by Default" + }, + { + "default": "0", + "depends_on": "eval: doc.is_submittable", + "description": "Enabling this will submit documents in background", + "fieldname": "queue_in_background", + "fieldtype": "Check", + "label": "Queue in Background (BETA)" + }, + { + "fieldname": "default_view", + "fieldtype": "Select", + "label": "Default View" + }, + { + "default": "0", + "fieldname": "force_re_route_to_default_view", + "fieldtype": "Check", + "label": "Force Re-route to Default View" + }, + { + "default": "0", + "depends_on": "eval:!doc.istable", + "description": "Enables Calendar and Gantt views.", + "fieldname": "is_calendar_and_gantt", + "fieldtype": "Check", + "label": "Is Calendar and Gantt" + }, + { + "fieldname": "settings_tab", + "fieldtype": "Tab Break", + "label": "Settings" + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "form_builder_tab", + "fieldtype": "Tab Break", + "label": "Form" + }, + { + "fieldname": "form_builder", + "fieldtype": "HTML", + "label": "Form Builder" + }, + { + "collapsible": 1, + "fieldname": "fields_section", + "fieldtype": "Section Break", + "label": "Fields" + }, + { + "fieldname": "connections_tab", + "fieldtype": "Tab Break", + "label": "Connections", + "show_dashboard": 1 + }, + { + "depends_on": "email_append_to", + "fieldname": "sender_name_field", + "fieldtype": "Data", + "label": "Sender Name Field" + } + ], + "icon": "fa fa-bolt", + "idx": 6, + "index_web_pages_for_search": 1, + "links": [ + { + "group": "Views", + "link_doctype": "Report", + "link_fieldname": "ref_doctype" + }, + { + "group": "Workflow", + "link_doctype": "Workflow", + "link_fieldname": "document_type" + }, + { + "group": "Workflow", + "link_doctype": "Notification", + "link_fieldname": "document_type" + }, + { + "group": "Customization", + "link_doctype": "Custom Field", + "link_fieldname": "dt" + }, + { + "group": "Customization", + "link_doctype": "Client Script", + "link_fieldname": "dt" + }, + { + "group": "Customization", + "link_doctype": "Server Script", + "link_fieldname": "reference_doctype" + }, + { + "group": "Workflow", + "link_doctype": "Webhook", + "link_fieldname": "webhook_doctype" + }, + { + "group": "Views", + "link_doctype": "Print Format", + "link_fieldname": "doc_type" + }, + { + "group": "Views", + "link_doctype": "Web Form", + "link_fieldname": "doc_type" + }, + { + "group": "Views", + "link_doctype": "Calendar View", + "link_fieldname": "reference_doctype" + }, + { + "group": "Views", + "link_doctype": "Kanban Board", + "link_fieldname": "reference_doctype" + }, + { + "group": "Workflow", + "link_doctype": "Onboarding Step", + "link_fieldname": "reference_document" + }, + { + "group": "Rules", + "link_doctype": "Auto Repeat", + "link_fieldname": "reference_doctype" + }, + { + "group": "Rules", + "link_doctype": "Assignment Rule", + "link_fieldname": "document_type" + }, + { + "group": "Rules", + "link_doctype": "Energy Point Rule", + "link_fieldname": "reference_doctype" + } + ], + "modified": "2023-12-01 18:37:16.799471", + "modified_by": "Administrator", + "module": "Core", + "name": "DocType", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + } + ], + "route": "doctype", + "search_fields": "module", + "show_name_in_global_search": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1, + "translated_doctype": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype/doctype.py b/xhiveframework/core/doctype/doctype/doctype.py new file mode 100644 index 0000000..3550380 --- /dev/null +++ b/xhiveframework/core/doctype/doctype/doctype.py @@ -0,0 +1,1895 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import copy +import json +import os +import re +import shutil +from typing import TYPE_CHECKING, Union + +import xhiveframework +from xhiveframework import _ +from xhiveframework.cache_manager import clear_controller_cache, clear_user_cache +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_field +from xhiveframework.custom.doctype.property_setter.property_setter import make_property_setter +from xhiveframework.database import savepoint +from xhiveframework.database.schema import validate_column_length, validate_column_name +from xhiveframework.desk.notifications import delete_notification_count_for, get_filters_for +from xhiveframework.desk.utils import validate_route_conflict +from xhiveframework.model import ( + child_table_fields, + data_field_options, + default_fields, + no_value_fields, + table_fields, +) +from xhiveframework.model.base_document import get_controller +from xhiveframework.model.docfield import supports_translation +from xhiveframework.model.document import Document +from xhiveframework.model.meta import Meta +from xhiveframework.modules import get_doc_path, make_boilerplate +from xhiveframework.modules.import_file import get_file_path +from xhiveframework.permissions import ALL_USER_ROLE, AUTOMATIC_ROLES, SYSTEM_USER_ROLE +from xhiveframework.query_builder.functions import Concat +from xhiveframework.utils import cint, flt, is_a_property, random_string +from xhiveframework.website.utils import clear_cache + +if TYPE_CHECKING: + from xhiveframework.custom.doctype.customize_form.customize_form import CustomizeForm + +DEPENDS_ON_PATTERN = re.compile(r'[\w\.:_]+\s*={1}\s*[\w\.@\'"]+') +ILLEGAL_FIELDNAME_PATTERN = re.compile("""['",./%@()<>{}]""") +WHITESPACE_PADDING_PATTERN = re.compile(r"^[ \t\n\r]+|[ \t\n\r]+$", flags=re.ASCII) +START_WITH_LETTERS_PATTERN = re.compile(r"^(?![\W])[^\d_\s][\w -]+$", flags=re.ASCII) +FIELD_PATTERN = re.compile("{(.*?)}", flags=re.UNICODE) + + +class InvalidFieldNameError(xhiveframework.ValidationError): + pass + + +class UniqueFieldnameError(xhiveframework.ValidationError): + pass + + +class IllegalMandatoryError(xhiveframework.ValidationError): + pass + + +class DoctypeLinkError(xhiveframework.ValidationError): + pass + + +class WrongOptionsDoctypeLinkError(xhiveframework.ValidationError): + pass + + +class HiddenAndMandatoryWithoutDefaultError(xhiveframework.ValidationError): + pass + + +class NonUniqueError(xhiveframework.ValidationError): + pass + + +class CannotIndexedError(xhiveframework.ValidationError): + pass + + +class CannotCreateStandardDoctypeError(xhiveframework.ValidationError): + pass + + +form_grid_templates = {"fields": "templates/form_grid/fields.html"} + + +class DocType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.docfield.docfield import DocField + from xhiveframework.core.doctype.docperm.docperm import DocPerm + from xhiveframework.core.doctype.doctype_action.doctype_action import DocTypeAction + from xhiveframework.core.doctype.doctype_link.doctype_link import DocTypeLink + from xhiveframework.core.doctype.doctype_state.doctype_state import DocTypeState + from xhiveframework.types import DF + + actions: DF.Table[DocTypeAction] + allow_auto_repeat: DF.Check + allow_copy: DF.Check + allow_events_in_timeline: DF.Check + allow_guest_to_view: DF.Check + allow_import: DF.Check + allow_rename: DF.Check + autoname: DF.Data | None + beta: DF.Check + color: DF.Data | None + custom: DF.Check + default_email_template: DF.Link | None + default_print_format: DF.Data | None + default_view: DF.Literal[None] + description: DF.SmallText | None + document_type: DF.Literal["", "Document", "Setup", "System", "Other"] + documentation: DF.Data | None + editable_grid: DF.Check + email_append_to: DF.Check + engine: DF.Literal["InnoDB", "MyISAM"] + fields: DF.Table[DocField] + force_re_route_to_default_view: DF.Check + has_web_view: DF.Check + hide_toolbar: DF.Check + icon: DF.Data | None + image_field: DF.Data | None + in_create: DF.Check + index_web_pages_for_search: DF.Check + is_calendar_and_gantt: DF.Check + is_published_field: DF.Data | None + is_submittable: DF.Check + is_tree: DF.Check + is_virtual: DF.Check + issingle: DF.Check + istable: DF.Check + links: DF.Table[DocTypeLink] + make_attachments_public: DF.Check + max_attachments: DF.Int + migration_hash: DF.Data | None + module: DF.Link + naming_rule: DF.Literal[ + "", + "Set by user", + "Autoincrement", + "By fieldname", + 'By "Naming Series" field', + "Expression", + "Expression (old style)", + "Random", + "By script", + ] + nsm_parent_field: DF.Data | None + permissions: DF.Table[DocPerm] + queue_in_background: DF.Check + quick_entry: DF.Check + read_only: DF.Check + restrict_to_domain: DF.Link | None + route: DF.Data | None + search_fields: DF.Data | None + sender_field: DF.Data | None + sender_name_field: DF.Data | None + show_name_in_global_search: DF.Check + show_preview_popup: DF.Check + show_title_field_in_link: DF.Check + sort_field: DF.Data | None + sort_order: DF.Literal["ASC", "DESC"] + states: DF.Table[DocTypeState] + subject_field: DF.Data | None + timeline_field: DF.Data | None + title_field: DF.Data | None + track_changes: DF.Check + track_seen: DF.Check + track_views: DF.Check + translated_doctype: DF.Check + website_search_field: DF.Data | None + # end: auto-generated types + + def validate(self): + """Validate DocType before saving. + + - Check if developer mode is set. + - Validate series + - Check fieldnames (duplication etc) + - Clear permission table for child tables + - Add `amended_from` and `amended_by` if Amendable + - Add custom field `auto_repeat` if Repeatable + - Check if links point to valid fieldnames""" + + self.check_developer_mode() + + self.validate_name() + + self.set_defaults_for_single_and_table() + self.set_defaults_for_autoincremented() + self.scrub_field_names() + self.set_default_in_list_view() + self.set_default_translatable() + validate_series(self) + self.set("can_change_name_type", validate_autoincrement_autoname(self)) + self.validate_document_type() + validate_fields(self) + self.check_indexing_for_dashboard_links() + if not self.istable: + validate_permissions(self) + + self.make_amendable() + self.make_repeatable() + self.validate_nestedset() + self.validate_child_table() + self.validate_website() + self.validate_virtual_doctype_methods() + self.ensure_minimum_max_attachment_limit() + validate_links_table_fieldnames(self) + + if not self.is_new(): + self.before_update = xhiveframework.get_doc("DocType", self.name) + self.setup_fields_to_fetch() + self.validate_field_name_conflicts() + + check_email_append_to(self) + + if self.default_print_format and not self.custom: + xhiveframework.throw(_("Standard DocType cannot have default print format, use Customize Form")) + + def validate_field_name_conflicts(self): + """Check if field names dont conflict with controller properties and methods""" + core_doctypes = [ + "Custom DocPerm", + "DocPerm", + "Custom Field", + "Customize Form Field", + "Web Form Field", + "DocField", + ] + + if self.name in core_doctypes: + return + + try: + controller = get_controller(self.name) + except ImportError: + controller = Document + + available_objects = {x for x in dir(controller) if isinstance(x, str)} + property_set = {x for x in available_objects if is_a_property(getattr(controller, x, None))} + method_set = { + x for x in available_objects if x not in property_set and callable(getattr(controller, x, None)) + } + + for docfield in self.get("fields") or []: + if docfield.fieldtype in no_value_fields: + continue + + conflict_type = None + field = docfield.fieldname + field_label = docfield.label or docfield.fieldname + + if docfield.fieldname in method_set: + conflict_type = "controller method" + if docfield.fieldname in property_set and not docfield.is_virtual: + conflict_type = "class property" + + if conflict_type: + xhiveframework.throw( + _("Fieldname '{0}' conflicting with a {1} of the name {2} in {3}").format( + field_label, conflict_type, field, self.name + ) + ) + + def set_defaults_for_single_and_table(self): + if self.issingle: + self.allow_import = 0 + self.is_submittable = 0 + self.istable = 0 + + elif self.istable: + self.allow_import = 0 + self.permissions = [] + + def set_defaults_for_autoincremented(self): + if self.autoname and self.autoname == "autoincrement": + self.allow_rename = 0 + + def set_default_in_list_view(self): + """Set default in-list-view for first 4 mandatory fields""" + not_allowed_in_list_view = get_fields_not_allowed_in_list_view(self.meta) + + if not [d.fieldname for d in self.fields if d.in_list_view]: + cnt = 0 + for d in self.fields: + if d.reqd and not d.hidden and d.fieldtype not in not_allowed_in_list_view: + d.in_list_view = 1 + cnt += 1 + if cnt == 4: + break + + def set_default_translatable(self): + """Ensure that non-translatable never will be translatable""" + for d in self.fields: + if d.translatable and not supports_translation(d.fieldtype): + d.translatable = 0 + + def check_indexing_for_dashboard_links(self): + """Enable indexing for outgoing links used in dashboard""" + for d in self.fields: + if d.fieldtype == "Link" and not (d.unique or d.search_index): + referred_as_link = xhiveframework.db.exists( + "DocType Link", + {"parent": d.options, "link_doctype": self.name, "link_fieldname": d.fieldname}, + ) + if not referred_as_link: + continue + + xhiveframework.msgprint( + _("{0} should be indexed because it's referred in dashboard connections").format( + _(d.label, context=d.parent) + ), + alert=True, + indicator="orange", + ) + + def check_developer_mode(self): + """Throw exception if not developer mode or via patch""" + if xhiveframework.flags.in_patch or xhiveframework.flags.in_test: + return + + if not xhiveframework.conf.get("developer_mode") and not self.custom: + xhiveframework.throw( + _("Not in Developer Mode! Set in site_config.json or make 'Custom' DocType."), + CannotCreateStandardDoctypeError, + ) + + if self.is_virtual and self.custom: + xhiveframework.throw(_("Not allowed to create custom Virtual DocType."), CannotCreateStandardDoctypeError) + + if xhiveframework.conf.get("developer_mode"): + self.owner = "Administrator" + self.modified_by = "Administrator" + + def setup_fields_to_fetch(self): + """Setup query to update values for newly set fetch values""" + try: + old_meta = xhiveframework.get_meta(xhiveframework.get_doc("DocType", self.name), cached=False) + old_fields_to_fetch = [df.fieldname for df in old_meta.get_fields_to_fetch()] + except xhiveframework.DoesNotExistError: + old_fields_to_fetch = [] + + new_meta = xhiveframework.get_meta(self, cached=False) + + self.flags.update_fields_to_fetch_queries = [] + + new_fields_to_fetch = [df for df in new_meta.get_fields_to_fetch()] + + if set(old_fields_to_fetch) != {df.fieldname for df in new_fields_to_fetch}: + for df in new_fields_to_fetch: + if df.fieldname not in old_fields_to_fetch: + link_fieldname, source_fieldname = df.fetch_from.split(".", 1) + if not source_fieldname: + continue # Invalid expression + link_df = new_meta.get_field(link_fieldname) + + if xhiveframework.db.db_type == "postgres": + update_query = """ + UPDATE `tab{doctype}` + SET `{fieldname}` = source.`{source_fieldname}` + FROM `tab{link_doctype}` as source + WHERE `{link_fieldname}` = source.name + AND ifnull(`{fieldname}`, '')='' + """ + else: + update_query = """ + UPDATE `tab{doctype}` as target + INNER JOIN `tab{link_doctype}` as source + ON `target`.`{link_fieldname}` = `source`.`name` + SET `target`.`{fieldname}` = `source`.`{source_fieldname}` + WHERE ifnull(`target`.`{fieldname}`, '')="" + """ + + self.flags.update_fields_to_fetch_queries.append( + update_query.format( + link_doctype=link_df.options, + source_fieldname=source_fieldname, + doctype=self.name, + fieldname=df.fieldname, + link_fieldname=link_fieldname, + ) + ) + + def update_fields_to_fetch(self): + """Update fetch values based on queries setup""" + if self.flags.update_fields_to_fetch_queries and not self.issingle: + for query in self.flags.update_fields_to_fetch_queries: + xhiveframework.db.sql(query) + + def validate_document_type(self): + if self.document_type == "Transaction": + self.document_type = "Document" + if self.document_type == "Master": + self.document_type = "Setup" + + def validate_website(self): + """Ensure that website generator has field 'route'""" + if self.route: + self.route = self.route.strip("/") + + if self.has_web_view: + # route field must be present + if "route" not in [d.fieldname for d in self.fields]: + xhiveframework.throw(_('Field "route" is mandatory for Web Views'), title="Missing Field") + + # clear website cache + clear_cache() + + def validate_virtual_doctype_methods(self): + if not self.get("is_virtual") or self.is_new(): + return + + from xhiveframework.model.virtual_doctype import validate_controller + + validate_controller(self.name) + + def ensure_minimum_max_attachment_limit(self): + """Ensure that max_attachments is *at least* bigger than number of attach fields.""" + from xhiveframework.model import attachment_fieldtypes + + if not self.max_attachments: + return + + total_attach_fields = len([d for d in self.fields if d.fieldtype in attachment_fieldtypes]) + if total_attach_fields > self.max_attachments: + self.max_attachments = total_attach_fields + field_label = xhiveframework.bold(self.meta.get_field("max_attachments").label) + xhiveframework.msgprint( + _("Number of attachment fields are more than {}, limit updated to {}.").format( + field_label, total_attach_fields + ), + title=_("Insufficient attachment limit"), + alert=True, + ) + + def change_modified_of_parent(self): + """Change the timestamp of parent DocType if the current one is a child to clear caches.""" + if xhiveframework.flags.in_import: + return + parent_list = xhiveframework.get_all( + "DocField", "parent", dict(fieldtype=["in", xhiveframework.model.table_fields], options=self.name) + ) + for p in parent_list: + xhiveframework.db.set_value("DocType", p.parent, {}) + + def scrub_field_names(self): + """Sluggify fieldnames if not set from Label.""" + restricted = ( + "name", + "parent", + "creation", + "owner", + "modified", + "modified_by", + "parentfield", + "parenttype", + "file_list", + "flags", + "docstatus", + ) + for d in self.get("fields"): + if d.fieldtype: + if not getattr(d, "fieldname", None): + if d.label: + d.fieldname = d.label.strip().lower().replace(" ", "_").strip("?") + if d.fieldname in restricted: + d.fieldname = d.fieldname + "1" + if d.fieldtype == "Section Break": + d.fieldname = d.fieldname + "_section" + elif d.fieldtype == "Column Break": + d.fieldname = d.fieldname + "_column" + elif d.fieldtype == "Tab Break": + d.fieldname = d.fieldname + "_tab" + elif d.fieldtype in ("Section Break", "Column Break", "Tab Break"): + d.fieldname = d.fieldtype.lower().replace(" ", "_") + "_" + str(random_string(4)) + else: + xhiveframework.throw( + _("Row #{}: Fieldname is required").format(d.idx), title="Missing Fieldname" + ) + else: + if d.fieldname in restricted: + xhiveframework.throw( + _("Fieldname {0} is restricted").format(d.fieldname), InvalidFieldNameError + ) + d.fieldname = ILLEGAL_FIELDNAME_PATTERN.sub("", d.fieldname) + + # fieldnames should be lowercase + d.fieldname = d.fieldname.lower() + + # unique is automatically an index + if d.unique: + d.search_index = 0 + + def on_update(self): + """Update database schema, make controller templates if `custom` is not set and clear cache.""" + + if self.get("can_change_name_type"): + self.setup_autoincrement_and_sequence() + + try: + xhiveframework.db.updatedb(self.name, Meta(self)) + except Exception as e: + print(f"\n\nThere was an issue while migrating the DocType: {self.name}\n") + raise e + + self.change_modified_of_parent() + make_module_and_roles(self) + + self.update_fields_to_fetch() + + allow_doctype_export = ( + not self.custom + and not xhiveframework.flags.in_import + and (xhiveframework.conf.developer_mode or xhiveframework.flags.allow_doctype_export) + ) + if allow_doctype_export: + self.export_doc() + self.make_controller_template() + self.set_base_class_for_controller() + self.export_types_to_controller() + + # update index + if not self.custom: + self.run_module_method("on_doctype_update") + if self.flags.in_insert: + self.run_module_method("after_doctype_insert") + + self.sync_doctype_layouts() + delete_notification_count_for(doctype=self.name) + + xhiveframework.clear_cache(doctype=self.name) + + # clear user cache so that on the next reload this doctype is included in boot + clear_user_cache(xhiveframework.session.user) + + if not xhiveframework.flags.in_install and hasattr(self, "before_update"): + self.sync_global_search() + + clear_linked_doctype_cache() + + @savepoint(catch=Exception) + def sync_doctype_layouts(self): + """Sync Doctype Layout""" + doctype_layouts = xhiveframework.get_all( + "DocType Layout", filters={"document_type": self.name}, pluck="name", ignore_ddl=True + ) + for layout in doctype_layouts: + layout_doc = xhiveframework.get_doc("DocType Layout", layout) + layout_doc.sync_fields() + layout_doc.save() + + def setup_autoincrement_and_sequence(self): + """Changes name type and makes sequence on change (if required)""" + + name_type = f"varchar({xhiveframework.db.VARCHAR_LEN})" + + if self.autoname == "autoincrement": + name_type = "bigint" + xhiveframework.db.create_sequence(self.name, check_not_exists=True) + + change_name_column_type(self.name, name_type) + + def sync_global_search(self): + """If global search settings are changed, rebuild search properties for this table""" + global_search_fields_before_update = [ + d.fieldname for d in self.before_update.fields if d.in_global_search + ] + if self.before_update.show_name_in_global_search: + global_search_fields_before_update.append("name") + + global_search_fields_after_update = [d.fieldname for d in self.fields if d.in_global_search] + if self.show_name_in_global_search: + global_search_fields_after_update.append("name") + + if set(global_search_fields_before_update) != set(global_search_fields_after_update): + now = (not xhiveframework.request) or xhiveframework.flags.in_test or xhiveframework.flags.in_install + xhiveframework.enqueue("xhiveframework.utils.global_search.rebuild_for_doctype", now=now, doctype=self.name) + + def set_base_class_for_controller(self): + """If DocType.has_web_view has been changed, updates the controller class and import + from `WebsiteGenertor` to `Document` or viceversa""" + + if not self.has_value_changed("has_web_view"): + return + + despaced_name = self.name.replace(" ", "_") + scrubbed_name = xhiveframework.scrub(self.name) + scrubbed_module = xhiveframework.scrub(self.module) + controller_path = xhiveframework.get_module_path( + scrubbed_module, "doctype", scrubbed_name, f"{scrubbed_name}.py" + ) + + document_cls_tag = f"class {despaced_name}(Document)" + document_import_tag = "from xhiveframework.model.document import Document" + website_generator_cls_tag = f"class {despaced_name}(WebsiteGenerator)" + website_generator_import_tag = "from xhiveframework.website.website_generator import WebsiteGenerator" + + with open(controller_path) as f: + code = f.read() + updated_code = code + + is_website_generator_class = all( + [website_generator_cls_tag in code, website_generator_import_tag in code] + ) + + if self.has_web_view and not is_website_generator_class: + updated_code = updated_code.replace(document_import_tag, website_generator_import_tag).replace( + document_cls_tag, website_generator_cls_tag + ) + elif not self.has_web_view and is_website_generator_class: + updated_code = updated_code.replace(website_generator_import_tag, document_import_tag).replace( + website_generator_cls_tag, document_cls_tag + ) + + if updated_code != code: + with open(controller_path, "w") as f: + f.write(updated_code) + + def run_module_method(self, method): + from xhiveframework.modules import load_doctype_module + + module = load_doctype_module(self.name, self.module) + if hasattr(module, method): + getattr(module, method)() + + def before_rename(self, old, new, merge=False): + """Throw exception if merge. DocTypes cannot be merged.""" + if not self.custom and xhiveframework.session.user != "Administrator": + xhiveframework.throw(_("DocType can only be renamed by Administrator")) + + self.check_developer_mode() + self.validate_name(new) + + if merge: + xhiveframework.throw(_("DocType can not be merged")) + + def after_rename(self, old, new, merge=False): + """Change table name using `RENAME TABLE` if table exists. Or update + `doctype` property for Single type.""" + + if self.issingle: + xhiveframework.db.sql("""update tabSingles set doctype=%s where doctype=%s""", (new, old)) + xhiveframework.db.sql( + """update tabSingles set value=%s + where doctype=%s and field='name' and value = %s""", + (new, new, old), + ) + else: + xhiveframework.db.rename_table(old, new) + xhiveframework.db.commit() + + # Do not rename and move files and folders for custom doctype + if not self.custom: + if not xhiveframework.flags.in_patch: + self.rename_files_and_folders(old, new) + + clear_controller_cache(old) + + def after_delete(self): + if not self.custom: + clear_controller_cache(self.name) + + def rename_files_and_folders(self, old, new): + # move files + new_path = get_doc_path(self.module, "doctype", new) + old_path = get_doc_path(self.module, "doctype", old) + shutil.move(old_path, new_path) + + # rename files + for fname in os.listdir(new_path): + if xhiveframework.scrub(old) in fname: + old_file_name = os.path.join(new_path, fname) + new_file_name = os.path.join(new_path, fname.replace(xhiveframework.scrub(old), xhiveframework.scrub(new))) + shutil.move(old_file_name, new_file_name) + + self.rename_inside_controller(new, old, new_path) + xhiveframework.msgprint(_("Renamed files and replaced code in controllers, please check!")) + + def rename_inside_controller(self, new, old, new_path): + for fname in ("{}.js", "{}.py", "{}_list.js", "{}_calendar.js", "test_{}.py", "test_{}.js"): + fname = os.path.join(new_path, fname.format(xhiveframework.scrub(new))) + if os.path.exists(fname): + with open(fname) as f: + code = f.read() + with open(fname, "w") as f: + if fname.endswith(".js"): + file_content = code.replace(old, new) # replace str with full str (js controllers) + + elif fname.endswith(".py"): + file_content = code.replace( + xhiveframework.scrub(old), xhiveframework.scrub(new) + ) # replace str with _ (py imports) + file_content = file_content.replace( + old.replace(" ", ""), new.replace(" ", "") + ) # replace str (py controllers) + + f.write(file_content) + + # updating json file with new name + doctype_json_path = os.path.join(new_path, f"{xhiveframework.scrub(new)}.json") + current_data = xhiveframework.get_file_json(doctype_json_path) + current_data["name"] = new + + with open(doctype_json_path, "w") as f: + json.dump(current_data, f, indent=1) + + def before_reload(self): + """Preserve naming series changes in Property Setter.""" + if not (self.issingle and self.istable): + self.preserve_naming_series_options_in_property_setter() + + def preserve_naming_series_options_in_property_setter(self): + """Preserve naming_series as property setter if it does not exist""" + naming_series = self.get("fields", {"fieldname": "naming_series"}) + + if not naming_series: + return + + # check if atleast 1 record exists + if not ( + xhiveframework.db.table_exists(self.name) + and xhiveframework.get_all(self.name, fields=["name"], limit=1, as_list=True) + ): + return + + existing_property_setter = xhiveframework.db.get_value( + "Property Setter", {"doc_type": self.name, "property": "options", "field_name": "naming_series"} + ) + + if not existing_property_setter: + make_property_setter( + self.name, + "naming_series", + "options", + naming_series[0].options, + "Text", + validate_fields_for_doctype=False, + ) + if naming_series[0].default: + make_property_setter( + self.name, + "naming_series", + "default", + naming_series[0].default, + "Text", + validate_fields_for_doctype=False, + ) + + def before_export(self, docdict): + # remove null and empty fields + def remove_null_fields(o): + to_remove = [] + for attr, value in o.items(): + if isinstance(value, list): + for v in value: + remove_null_fields(v) + elif not value: + to_remove.append(attr) + + for attr in to_remove: + del o[attr] + + remove_null_fields(docdict) + + # retain order of 'fields' table and change order in 'field_order' + docdict["field_order"] = [f.fieldname for f in self.fields] + + if self.custom: + return + + path = get_file_path(self.module, "DocType", self.name) + if os.path.exists(path): + try: + with open(path) as txtfile: + olddoc = json.loads(txtfile.read()) + + old_field_names = [f["fieldname"] for f in olddoc.get("fields", [])] + if old_field_names: + new_field_dicts = [] + remaining_field_names = [f.fieldname for f in self.fields] + + for fieldname in old_field_names: + field_dict = [f for f in docdict["fields"] if f["fieldname"] == fieldname] + if field_dict: + new_field_dicts.append(field_dict[0]) + if fieldname in remaining_field_names: + remaining_field_names.remove(fieldname) + + for fieldname in remaining_field_names: + field_dict = [f for f in docdict["fields"] if f["fieldname"] == fieldname] + new_field_dicts.append(field_dict[0]) + + docdict["fields"] = new_field_dicts + except ValueError: + pass + + @staticmethod + def prepare_for_import(docdict): + # set order of fields from field_order + if docdict.get("field_order"): + new_field_dicts = [] + remaining_field_names = [f["fieldname"] for f in docdict.get("fields", [])] + + for fieldname in docdict.get("field_order"): + field_dict = [f for f in docdict.get("fields", []) if f["fieldname"] == fieldname] + if field_dict: + new_field_dicts.append(field_dict[0]) + if fieldname in remaining_field_names: + remaining_field_names.remove(fieldname) + + for fieldname in remaining_field_names: + field_dict = [f for f in docdict.get("fields", []) if f["fieldname"] == fieldname] + new_field_dicts.append(field_dict[0]) + + docdict["fields"] = new_field_dicts + + if "field_order" in docdict: + del docdict["field_order"] + + def export_doc(self): + """Export to standard folder `[module]/doctype/[name]/[name].json`.""" + from xhiveframework.modules.export_file import export_to_files + + export_to_files(record_list=[["DocType", self.name]], create_init=True) + + def make_controller_template(self): + """Make boilerplate controller template.""" + make_boilerplate("controller._py", self) + + if not self.istable: + make_boilerplate("test_controller._py", self.as_dict()) + make_boilerplate("controller.js", self.as_dict()) + # make_boilerplate("controller_list.js", self.as_dict()) + + if self.has_web_view: + templates_path = xhiveframework.get_module_path( + xhiveframework.scrub(self.module), "doctype", xhiveframework.scrub(self.name), "templates" + ) + if not os.path.exists(templates_path): + os.makedirs(templates_path) + make_boilerplate("templates/controller.html", self.as_dict()) + make_boilerplate("templates/controller_row.html", self.as_dict()) + + def export_types_to_controller(self): + from xhiveframework.modules.utils import get_module_app + from xhiveframework.types.exporter import TypeExporter + + try: + app = get_module_app(self.module) + except xhiveframework.DoesNotExistError: + return + + if any(xhiveframework.get_hooks("export_python_type_annotations", app_name=app)): + TypeExporter(self).export_types() + + def make_amendable(self): + """If is_submittable is set, add amended_from docfields.""" + if self.is_submittable: + docfield_exists = xhiveframework.get_all( + "DocField", filters={"fieldname": "amended_from", "parent": self.name}, pluck="name", limit=1 + ) + if not docfield_exists: + self.append( + "fields", + { + "label": "Amended From", + "fieldtype": "Link", + "fieldname": "amended_from", + "options": self.name, + "read_only": 1, + "print_hide": 1, + "no_copy": 1, + "search_index": 1, + }, + ) + + def make_repeatable(self): + """If allow_auto_repeat is set, add auto_repeat custom field.""" + if self.allow_auto_repeat: + if not xhiveframework.db.exists( + "Custom Field", {"fieldname": "auto_repeat", "dt": self.name} + ) and not xhiveframework.db.exists("DocField", {"fieldname": "auto_repeat", "parent": self.name}): + insert_after = self.fields[len(self.fields) - 1].fieldname + df = dict( + fieldname="auto_repeat", + label="Auto Repeat", + fieldtype="Link", + options="Auto Repeat", + insert_after=insert_after, + read_only=1, + no_copy=1, + print_hide=1, + ) + create_custom_field(self.name, df) + + def validate_nestedset(self): + if not self.get("is_tree"): + return + self.add_nestedset_fields() + + if not self.nsm_parent_field: + field_label = xhiveframework.bold(_("Parent Field (Tree)")) + xhiveframework.throw(_("{0} is a mandatory field").format(field_label), xhiveframework.MandatoryError) + + # check if field is valid + fieldnames = [df.fieldname for df in self.fields] + if self.nsm_parent_field and self.nsm_parent_field not in fieldnames: + xhiveframework.throw(_("Parent Field must be a valid fieldname"), InvalidFieldNameError) + + def add_nestedset_fields(self): + """If is_tree is set, add parent_field, lft, rgt, is_group fields.""" + fieldnames = [df.fieldname for df in self.fields] + if "lft" in fieldnames: + return + + self.append( + "fields", + { + "label": "Left", + "fieldtype": "Int", + "fieldname": "lft", + "read_only": 1, + "hidden": 1, + "no_copy": 1, + }, + ) + + self.append( + "fields", + { + "label": "Right", + "fieldtype": "Int", + "fieldname": "rgt", + "read_only": 1, + "hidden": 1, + "no_copy": 1, + }, + ) + + self.append("fields", {"label": "Is Group", "fieldtype": "Check", "fieldname": "is_group"}) + self.append( + "fields", + {"label": "Old Parent", "fieldtype": "Link", "options": self.name, "fieldname": "old_parent"}, + ) + + parent_field_label = f"Parent {self.name}" + parent_field_name = xhiveframework.scrub(parent_field_label) + self.append( + "fields", + { + "label": parent_field_label, + "fieldtype": "Link", + "options": self.name, + "fieldname": parent_field_name, + "ignore_user_permissions": 1, + }, + ) + self.nsm_parent_field = parent_field_name + + def validate_child_table(self): + if not self.get("istable") or self.is_new() or self.get("is_virtual"): + # if the doctype is not a child table then return + # if the doctype is a new doctype and also a child table then + # don't move forward as it will be handled via schema + return + + self.add_child_table_fields() + + def add_child_table_fields(self): + from xhiveframework.database.schema import add_column + + add_column(self.name, "parent", "Data") + add_column(self.name, "parenttype", "Data") + add_column(self.name, "parentfield", "Data") + + def get_max_idx(self): + """Returns the highest `idx`""" + max_idx = xhiveframework.db.sql("""select max(idx) from `tabDocField` where parent = %s""", self.name) + return max_idx and max_idx[0][0] or 0 + + def validate_name(self, name=None): + if not name: + name = self.name + + # a Doctype name is the tablename created in database + # `tab` the length of tablename is limited to 64 characters + max_length = xhiveframework.db.MAX_COLUMN_LENGTH - 3 + if len(name) > max_length: + # length(tab + ) should be equal to 64 characters hence doctype should be 61 characters + xhiveframework.throw( + _("Doctype name is limited to {0} characters ({1})").format(max_length, name), + xhiveframework.NameError, + ) + + # a DocType name should not start or end with an empty space + if WHITESPACE_PADDING_PATTERN.search(name): + xhiveframework.throw(_("DocType's name should not start or end with whitespace"), xhiveframework.NameError) + + # a DocType's name should not start with a number or underscore + # and should only contain letters, numbers, underscore, and hyphen + if not START_WITH_LETTERS_PATTERN.match(name): + xhiveframework.throw( + _( + "A DocType's name should start with a letter and can only " + "consist of letters, numbers, spaces, underscores and hyphens" + ), + xhiveframework.NameError, + title="Invalid Name", + ) + + validate_route_conflict(self.doctype, self.name) + + +def validate_series(dt, autoname=None, name=None): + """Validate if `autoname` property is correctly set.""" + if not autoname: + autoname = dt.autoname + if not name: + name = dt.name + + if not autoname and dt.get("fields", {"fieldname": "naming_series"}): + dt.autoname = "naming_series:" + elif dt.autoname and dt.autoname.startswith("naming_series:"): + fieldname = dt.autoname.split("naming_series:", 1)[0] or "naming_series" + if not dt.get("fields", {"fieldname": fieldname}): + xhiveframework.throw( + _("Fieldname called {0} must exist to enable autonaming").format(xhiveframework.bold(fieldname)), + title=_("Field Missing"), + ) + + # validate field name if autoname field:fieldname is used + # Create unique index on autoname field automatically. + if autoname and autoname.startswith("field:"): + field = autoname.split(":")[1] + if not field or field not in [df.fieldname for df in dt.fields]: + xhiveframework.throw(_("Invalid fieldname '{0}' in autoname").format(field)) + else: + for df in dt.fields: + if df.fieldname == field: + df.unique = 1 + break + + if ( + autoname + and (not autoname.startswith("field:")) + and (not autoname.startswith("eval:")) + and (autoname.lower() not in ("prompt", "hash")) + and (not autoname.startswith("naming_series:")) + and (not autoname.startswith("format:")) + ): + prefix = autoname.split(".", 1)[0] + doctype = xhiveframework.qb.DocType("DocType") + used_in = ( + xhiveframework.qb.from_(doctype) + .select(doctype.name) + .where(doctype.autoname.like(Concat(prefix, ".%"))) + .where(doctype.name != name) + ).run() + if used_in: + xhiveframework.throw(_("Series {0} already used in {1}").format(prefix, used_in[0][0])) + + +def validate_autoincrement_autoname(dt: Union[DocType, "CustomizeForm"]) -> bool: + """Checks if can doctype can change to/from autoincrement autoname""" + + def get_autoname_before_save(dt: Union[DocType, "CustomizeForm"]) -> str: + if dt.doctype == "Customize Form": + property_value = xhiveframework.db.get_value( + "Property Setter", {"doc_type": dt.doc_type, "property": "autoname"}, "value" + ) + # initially no property setter is set, + # hence getting autoname value from the doctype itself + if not property_value: + return xhiveframework.db.get_value("DocType", dt.doc_type, "autoname") or "" + + return property_value + + return getattr(dt.get_doc_before_save(), "autoname", "") + + if not dt.is_new(): + autoname_before_save = get_autoname_before_save(dt) + is_autoname_autoincrement = dt.autoname == "autoincrement" + + if ( + is_autoname_autoincrement + and autoname_before_save != "autoincrement" + or (not is_autoname_autoincrement and autoname_before_save == "autoincrement") + ): + if dt.doctype == "Customize Form": + xhiveframework.throw(_("Cannot change to/from autoincrement autoname in Customize Form")) + + if xhiveframework.get_meta(dt.name).issingle: + return False + + if not xhiveframework.get_all(dt.name, limit=1): + # allow changing the column type if there is no data + return True + + xhiveframework.throw( + _("Can only change to/from Autoincrement naming rule when there is no data in the doctype") + ) + + return False + + +def change_name_column_type(doctype_name: str, type: str) -> None: + """Changes name column type""" + + args = ( + (doctype_name, "name", type, False, True) + if (xhiveframework.db.db_type == "postgres") + else (doctype_name, "name", type, True) + ) + + xhiveframework.db.change_column_type(*args) + + +def validate_links_table_fieldnames(meta): + """Validate fieldnames in Links table""" + if not meta.links or xhiveframework.flags.in_patch or xhiveframework.flags.in_fixtures or xhiveframework.flags.in_migrate: + return + + fieldnames = tuple(field.fieldname for field in meta.fields) + for index, link in enumerate(meta.links, 1): + _test_connection_query(doctype=link.link_doctype, field=link.link_fieldname, idx=index) + + if not link.is_child_table: + continue + + if not link.parent_doctype: + message = _("Document Links Row #{0}: Parent DocType is mandatory for internal links").format( + index + ) + xhiveframework.throw(message, xhiveframework.ValidationError, _("Parent Missing")) + + if not link.table_fieldname: + message = _("Document Links Row #{0}: Table Fieldname is mandatory for internal links").format( + index + ) + xhiveframework.throw(message, xhiveframework.ValidationError, _("Table Fieldname Missing")) + + if meta.name == link.parent_doctype: + field_exists = link.table_fieldname in fieldnames + else: + field_exists = xhiveframework.get_meta(link.parent_doctype).has_field(link.table_fieldname) + + if not field_exists: + message = _("Document Links Row #{0}: Could not find field {1} in {2} DocType").format( + index, xhiveframework.bold(link.table_fieldname), xhiveframework.bold(meta.name) + ) + xhiveframework.throw(message, xhiveframework.ValidationError, _("Invalid Table Fieldname")) + + +def _test_connection_query(doctype, field, idx): + """Make sure that connection can be queried. + + This function executes query similar to one that would be executed for + finding count on dashboard and hence validates if fieldname/doctype are + correct. + """ + filters = get_filters_for(doctype) or {} + filters[field] = "" + + try: + xhiveframework.get_all(doctype, filters=filters, limit=1, distinct=True, ignore_ifnull=True) + except Exception as e: + xhiveframework.clear_last_message() + msg = _("Document Links Row #{0}: Invalid doctype or fieldname.").format(idx) + msg += "
    " + str(e) + xhiveframework.throw(msg, InvalidFieldNameError) + + +def validate_fields_for_doctype(doctype): + meta = xhiveframework.get_meta(doctype, cached=False) + validate_links_table_fieldnames(meta) + validate_fields(meta) + + +# this is separate because it is also called via custom field +def validate_fields(meta: Meta): + """Validate doctype fields. Checks + 1. There are no illegal characters in fieldnames + 2. If fieldnames are unique. + 3. Validate column length. + 4. Fields that do have database columns are not mandatory. + 5. `Link` and `Table` options are valid. + 6. **Hidden** and **Mandatory** are not set simultaneously. + 7. `Check` type field has default as 0 or 1. + 8. `Dynamic Links` are correctly defined. + 9. Precision is set in numeric fields and is between 1 & 6. + 10. Fold is not at the end (if set). + 11. `search_fields` are valid. + 12. `title_field` and title field pattern are valid. + 13. `unique` check is only valid for Data, Link and Read Only fieldtypes. + 14. `unique` cannot be checked if there exist non-unique values. + + :param meta: `xhiveframework.model.meta.Meta` object to check.""" + + def check_illegal_characters(fieldname): + validate_column_name(fieldname) + + def check_invalid_fieldnames(docname, fieldname): + if fieldname in Document._reserved_keywords: + xhiveframework.throw( + _("{0}: fieldname cannot be set to reserved keyword {1}").format( + xhiveframework.bold(docname), + xhiveframework.bold(fieldname), + ), + title=_("Invalid Fieldname"), + ) + + def check_unique_fieldname(docname, fieldname): + duplicates = list( + filter(None, map(lambda df: df.fieldname == fieldname and str(df.idx) or None, fields)) + ) + if len(duplicates) > 1: + xhiveframework.throw( + _("{0}: Fieldname {1} appears multiple times in rows {2}").format( + docname, fieldname, ", ".join(duplicates) + ), + UniqueFieldnameError, + ) + + def check_fieldname_length(fieldname): + validate_column_length(fieldname) + + def check_illegal_mandatory(docname, d): + if (d.fieldtype in no_value_fields) and d.fieldtype not in table_fields and d.reqd: + xhiveframework.throw( + _("{0}: Field {1} of type {2} cannot be mandatory").format(docname, d.label, d.fieldtype), + IllegalMandatoryError, + ) + + def check_link_table_options(docname, d): + if xhiveframework.flags.in_patch or xhiveframework.flags.in_fixtures: + return + + if d.fieldtype in ("Link", *table_fields): + if not d.options: + xhiveframework.throw( + _("{0}: Options required for Link or Table type field {1} in row {2}").format( + docname, d.label, d.idx + ), + DoctypeLinkError, + ) + if d.options == "[Select]" or d.options == d.parent: + return + if d.options != d.parent: + options = xhiveframework.db.get_value("DocType", d.options, "name") + if not options: + xhiveframework.throw( + _("{0}: Options must be a valid DocType for field {1} in row {2}").format( + docname, d.label, d.idx + ), + WrongOptionsDoctypeLinkError, + ) + elif not (options == d.options): + xhiveframework.throw( + _("{0}: Options {1} must be the same as doctype name {2} for the field {3}").format( + docname, d.options, options, d.label + ), + DoctypeLinkError, + ) + else: + # fix case + d.options = options + + def check_hidden_and_mandatory(docname, d): + if d.hidden and d.reqd and not d.default and not xhiveframework.flags.in_migrate: + xhiveframework.throw( + _("{0}: Field {1} in row {2} cannot be hidden and mandatory without default").format( + docname, d.label, d.idx + ), + HiddenAndMandatoryWithoutDefaultError, + ) + + def check_width(d): + if d.fieldtype == "Currency" and cint(d.width) < 100: + xhiveframework.throw(_("Max width for type Currency is 100px in row {0}").format(d.idx)) + + def check_in_list_view(is_table, d): + if d.in_list_view and (d.fieldtype in not_allowed_in_list_view): + property_label = "In Grid View" if is_table else "In List View" + xhiveframework.throw( + _("'{0}' not allowed for type {1} in row {2}").format(property_label, d.fieldtype, d.idx) + ) + + def check_in_global_search(d): + if d.in_global_search and d.fieldtype in no_value_fields: + xhiveframework.throw( + _("'In Global Search' not allowed for type {0} in row {1}").format(d.fieldtype, d.idx) + ) + + def check_dynamic_link_options(d): + if d.fieldtype == "Dynamic Link": + doctype_pointer = list(filter(lambda df: df.fieldname == d.options, fields)) + if ( + not doctype_pointer + or (doctype_pointer[0].fieldtype not in ("Link", "Select")) + or (doctype_pointer[0].fieldtype == "Link" and doctype_pointer[0].options != "DocType") + ): + xhiveframework.throw( + _( + "Options 'Dynamic Link' type of field must point to another Link Field with options as 'DocType'" + ) + ) + + def check_illegal_default(d): + if d.fieldtype == "Check" and not d.default: + d.default = "0" + if d.fieldtype == "Check" and cint(d.default) not in (0, 1): + xhiveframework.throw( + _("Default for 'Check' type of field {0} must be either '0' or '1'").format( + xhiveframework.bold(d.fieldname) + ) + ) + if d.fieldtype == "Select" and d.default: + if not d.options: + xhiveframework.throw( + _("Options for {0} must be set before setting the default value.").format( + xhiveframework.bold(d.fieldname) + ) + ) + elif d.default not in d.options.split("\n"): + xhiveframework.throw( + _("Default value for {0} must be in the list of options.").format( + xhiveframework.bold(d.fieldname) + ) + ) + + def check_precision(d): + if ( + d.fieldtype in ("Currency", "Float", "Percent") + and d.precision is not None + and not (1 <= cint(d.precision) <= 6) + ): + xhiveframework.throw(_("Precision should be between 1 and 6")) + + def check_unique_and_text(docname, d): + if meta.is_virtual: + return + + if meta.issingle: + d.unique = 0 + d.search_index = 0 + + if getattr(d, "unique", False): + if d.fieldtype not in ("Data", "Link", "Read Only", "Int"): + xhiveframework.throw( + _("{0}: Fieldtype {1} for {2} cannot be unique").format(docname, d.fieldtype, d.label), + NonUniqueError, + ) + + if not d.get("__islocal") and xhiveframework.db.has_column(d.parent, d.fieldname): + has_non_unique_values = xhiveframework.db.sql( + f"""select `{d.fieldname}`, count(*) + from `tab{d.parent}` where ifnull(`{d.fieldname}`, '') != '' + group by `{d.fieldname}` having count(*) > 1 limit 1""" + ) + + if has_non_unique_values and has_non_unique_values[0][0]: + xhiveframework.throw( + _("{0}: Field '{1}' cannot be set as Unique as it has non-unique values").format( + docname, d.label + ), + NonUniqueError, + ) + + if d.search_index and d.fieldtype in ("Text", "Long Text", "Small Text", "Code", "Text Editor"): + xhiveframework.throw( + _("{0}:Fieldtype {1} for {2} cannot be indexed").format(docname, d.fieldtype, d.label), + CannotIndexedError, + ) + + def check_fold(fields): + fold_exists = False + for i, f in enumerate(fields): + if f.fieldtype == "Fold": + if fold_exists: + xhiveframework.throw(_("There can be only one Fold in a form")) + fold_exists = True + if i < len(fields) - 1: + nxt = fields[i + 1] + if nxt.fieldtype != "Section Break": + xhiveframework.throw(_("Fold must come before a Section Break")) + else: + xhiveframework.throw(_("Fold can not be at the end of the form")) + + def check_search_fields(meta, fields): + """Throw exception if `search_fields` don't contain valid fields.""" + if not meta.search_fields: + return + + # No value fields should not be included in search field + search_fields = [field.strip() for field in (meta.search_fields or "").split(",")] + fieldtype_mapper = { + field.fieldname: field.fieldtype + for field in filter(lambda field: field.fieldname in search_fields, fields) + } + + for fieldname in search_fields: + fieldname = fieldname.strip() + if (fieldtype_mapper.get(fieldname) in no_value_fields) or (fieldname not in fieldname_list): + xhiveframework.throw(_("Search field {0} is not valid").format(fieldname)) + + def check_title_field(meta): + """Throw exception if `title_field` isn't a valid fieldname.""" + if not meta.get("title_field"): + return + + if meta.title_field not in fieldname_list: + xhiveframework.throw(_("Title field must be a valid fieldname"), InvalidFieldNameError) + + def _validate_title_field_pattern(pattern): + if not pattern: + return + + for fieldname in FIELD_PATTERN.findall(pattern): + if fieldname.startswith("{"): + # edge case when double curlies are used for escape + continue + + if fieldname not in fieldname_list: + xhiveframework.throw( + _("{{{0}}} is not a valid fieldname pattern. It should be {{field_name}}.").format( + fieldname + ), + InvalidFieldNameError, + ) + + df = meta.get("fields", filters={"fieldname": meta.title_field})[0] + if df: + _validate_title_field_pattern(df.options) + _validate_title_field_pattern(df.default) + + def check_image_field(meta): + '''check image_field exists and is of type "Attach Image"''' + if not meta.image_field: + return + + df = meta.get("fields", {"fieldname": meta.image_field}) + if not df: + xhiveframework.throw(_("Image field must be a valid fieldname"), InvalidFieldNameError) + if df[0].fieldtype != "Attach Image": + xhiveframework.throw(_("Image field must be of type Attach Image"), InvalidFieldNameError) + + def check_is_published_field(meta): + if not meta.is_published_field: + return + + if meta.is_published_field not in fieldname_list: + xhiveframework.throw(_("Is Published Field must be a valid fieldname"), InvalidFieldNameError) + + def check_website_search_field(meta): + if not meta.get("website_search_field"): + return + + if meta.website_search_field not in fieldname_list: + xhiveframework.throw(_("Website Search Field must be a valid fieldname"), InvalidFieldNameError) + + if "title" not in fieldname_list: + xhiveframework.throw( + _('Field "title" is mandatory if "Website Search Field" is set.'), title=_("Missing Field") + ) + + def check_timeline_field(meta): + if not meta.timeline_field: + return + + if meta.timeline_field not in fieldname_list: + xhiveframework.throw(_("Timeline field must be a valid fieldname"), InvalidFieldNameError) + + df = meta.get("fields", {"fieldname": meta.timeline_field})[0] + if df.fieldtype not in ("Link", "Dynamic Link"): + xhiveframework.throw(_("Timeline field must be a Link or Dynamic Link"), InvalidFieldNameError) + + def check_sort_field(meta): + """Validate that sort_field(s) is a valid field""" + if meta.sort_field: + sort_fields = [meta.sort_field] + if "," in meta.sort_field: + sort_fields = [d.split(maxsplit=1)[0] for d in meta.sort_field.split(",")] + + for fieldname in sort_fields: + if fieldname not in (fieldname_list + list(default_fields) + list(child_table_fields)): + xhiveframework.throw( + _("Sort field {0} must be a valid fieldname").format(fieldname), InvalidFieldNameError + ) + + def check_illegal_depends_on_conditions(docfield): + """assignment operation should not be allowed in the depends on condition.""" + depends_on_fields = [ + "depends_on", + "collapsible_depends_on", + "mandatory_depends_on", + "read_only_depends_on", + ] + for field in depends_on_fields: + depends_on = docfield.get(field, None) + if depends_on and ("=" in depends_on) and DEPENDS_ON_PATTERN.match(depends_on): + xhiveframework.throw(_("Invalid {0} condition").format(xhiveframework.unscrub(field)), xhiveframework.ValidationError) + + def check_table_multiselect_option(docfield): + """check if the doctype provided in Option has atleast 1 Link field""" + if not docfield.fieldtype == "Table MultiSelect": + return + + doctype = docfield.options + meta = xhiveframework.get_meta(doctype) + link_field = [df for df in meta.fields if df.fieldtype == "Link"] + + if not link_field: + xhiveframework.throw( + _( + "DocType {0} provided for the field {1} must have atleast one Link field" + ).format(doctype, docfield.fieldname), + xhiveframework.ValidationError, + ) + + def scrub_options_in_select(field): + """Strip options for whitespaces""" + + if field.fieldtype == "Select" and field.options is not None: + options_list = [] + for i, option in enumerate(field.options.split("\n")): + _option = option.strip() + if i == 0 or _option: + options_list.append(_option) + field.options = "\n".join(options_list) + + def scrub_fetch_from(field): + if hasattr(field, "fetch_from") and field.fetch_from: + 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 = xhiveframework.bold(_(docfield.label, context=docfield.parent)) + text_str = ( + _("{0} is an invalid Data field.").format(df_str) + + "
    " * 2 + + _("Only Options allowed for Data field are:") + + "
    " + ) + df_options_str = "
    • " + "
    • ".join(_(x) for x in data_field_options) + "
    " + + xhiveframework.msgprint(text_str + df_options_str, title="Invalid Data Field", alert=True) + + def check_child_table_option(docfield): + if xhiveframework.flags.in_fixtures: + return + if docfield.fieldtype not in ["Table MultiSelect", "Table"]: + return + + doctype = docfield.options + child_doctype_meta = xhiveframework.get_meta(doctype) + + if not child_doctype_meta.istable: + xhiveframework.throw( + _("Option {0} for field {1} is not a child table").format( + xhiveframework.bold(doctype), xhiveframework.bold(docfield.fieldname) + ), + title=_("Invalid Option"), + ) + + if not (meta.is_virtual == child_doctype_meta.is_virtual): + error_msg = " should be virtual." if meta.is_virtual else " cannot be virtual." + xhiveframework.throw( + _("Child Table {0} for field {1}" + error_msg).format( + xhiveframework.bold(doctype), xhiveframework.bold(docfield.fieldname) + ), + title=_("Invalid Option"), + ) + + def check_max_height(docfield): + if getattr(docfield, "max_height", None) and (docfield.max_height[-2:] not in ("px", "em")): + xhiveframework.throw(f"Max for {xhiveframework.bold(docfield.fieldname)} height must be in px, em, rem") + + def check_no_of_ratings(docfield): + if docfield.fieldtype == "Rating": + if docfield.options and (int(docfield.options) > 10 or int(docfield.options) < 3): + xhiveframework.throw(_("Options for Rating field can range from 3 to 10")) + + fields = meta.get("fields") + fieldname_list = [d.fieldname for d in fields] + + not_allowed_in_list_view = get_fields_not_allowed_in_list_view(meta) + + for d in fields: + if not d.permlevel: + d.permlevel = 0 + if d.fieldtype not in table_fields: + d.allow_bulk_edit = 0 + if not d.fieldname: + d.fieldname = d.fieldname.lower().strip("?") + + check_illegal_characters(d.fieldname) + check_invalid_fieldnames(meta.get("name"), d.fieldname) + check_fieldname_length(d.fieldname) + check_hidden_and_mandatory(meta.get("name"), d) + check_unique_and_text(meta.get("name"), d) + check_table_multiselect_option(d) + scrub_options_in_select(d) + scrub_fetch_from(d) + validate_data_field_type(d) + + if not xhiveframework.flags.in_migrate: + check_unique_fieldname(meta.get("name"), d.fieldname) + check_link_table_options(meta.get("name"), d) + check_illegal_mandatory(meta.get("name"), d) + check_dynamic_link_options(d) + check_in_list_view(meta.get("istable"), d) + check_in_global_search(d) + check_illegal_depends_on_conditions(d) + check_illegal_default(d) + check_child_table_option(d) + check_max_height(d) + check_no_of_ratings(d) + + if not xhiveframework.flags.in_migrate: + check_fold(fields) + check_search_fields(meta, fields) + check_title_field(meta) + check_timeline_field(meta) + check_is_published_field(meta) + check_website_search_field(meta) + check_sort_field(meta) + check_image_field(meta) + + +def get_fields_not_allowed_in_list_view(meta) -> list[str]: + not_allowed_in_list_view = list(copy.copy(no_value_fields)) + not_allowed_in_list_view.append("Attach Image") + if meta.istable: + not_allowed_in_list_view.remove("Button") + not_allowed_in_list_view.remove("HTML") + return not_allowed_in_list_view + + +def validate_permissions_for_doctype(doctype, for_remove=False, alert=False): + """Validates if permissions are set correctly.""" + doctype = xhiveframework.get_doc("DocType", doctype) + validate_permissions(doctype, for_remove, alert=alert) + + # save permissions + for perm in doctype.get("permissions"): + perm.db_update() + + clear_permissions_cache(doctype.name) + + +def clear_permissions_cache(doctype): + from xhiveframework.cache_manager import clear_user_cache + + xhiveframework.clear_cache(doctype=doctype) + delete_notification_count_for(doctype) + + clear_user_cache() + + +def validate_permissions(doctype, for_remove=False, alert=False): + permissions = doctype.get("permissions") + # Some DocTypes may not have permissions by default, don't show alert for them + if not permissions and alert: + xhiveframework.msgprint(_("No Permissions Specified"), alert=True, indicator="orange") + issingle = issubmittable = isimportable = False + if doctype: + issingle = cint(doctype.issingle) + issubmittable = cint(doctype.is_submittable) + isimportable = cint(doctype.allow_import) + + def get_txt(d): + return _("For {0} at level {1} in {2} in row {3}").format(d.role, d.permlevel, d.parent, d.idx) + + def check_atleast_one_set(d): + if not d.select and not d.read and not d.write and not d.submit and not d.cancel and not d.create: + xhiveframework.throw(_("{0}: No basic permissions set").format(get_txt(d))) + + def check_double(d): + has_similar = False + similar_because_of = "" + for p in permissions: + if p.role == d.role and p.permlevel == d.permlevel and p != d: + if p.if_owner == d.if_owner: + similar_because_of = _("If Owner") + has_similar = True + break + + if has_similar: + xhiveframework.throw( + _("{0}: Only one rule allowed with the same Role, Level and {1}").format( + get_txt(d), similar_because_of + ) + ) + + def check_level_zero_is_set(d): + if cint(d.permlevel) > 0 and d.role not in (ALL_USER_ROLE, SYSTEM_USER_ROLE): + has_zero_perm = False + for p in permissions: + if p.role == d.role and (p.permlevel or 0) == 0 and p != d: + has_zero_perm = True + break + + if not has_zero_perm: + xhiveframework.throw( + _("{0}: Permission at level 0 must be set before higher levels are set").format( + get_txt(d) + ) + ) + + for invalid in ("create", "submit", "cancel", "amend"): + if d.get(invalid): + d.set(invalid, 0) + + def check_permission_dependency(d): + if d.cancel and not d.submit: + xhiveframework.throw(_("{0}: Cannot set Cancel without Submit").format(get_txt(d))) + + if (d.submit or d.cancel or d.amend) and not d.write: + xhiveframework.throw(_("{0}: Cannot set Submit, Cancel, Amend without Write").format(get_txt(d))) + if d.amend and not d.write: + xhiveframework.throw(_("{0}: Cannot set Amend without Cancel").format(get_txt(d))) + if d.get("import") and not d.create: + xhiveframework.throw(_("{0}: Cannot set Import without Create").format(get_txt(d))) + + def remove_rights_for_single(d): + if not issingle: + return + + if d.report: + xhiveframework.msgprint(_("Report cannot be set for Single types")) + d.report = 0 + d.set("import", 0) + d.set("export", 0) + + def check_if_submittable(d): + if d.submit and not issubmittable: + xhiveframework.throw(_("{0}: Cannot set Assign Submit if not Submittable").format(get_txt(d))) + elif d.amend and not issubmittable: + xhiveframework.throw(_("{0}: Cannot set Assign Amend if not Submittable").format(get_txt(d))) + + def check_if_importable(d): + if d.get("import") and not isimportable: + xhiveframework.throw(_("{0}: Cannot set import as {1} is not importable").format(get_txt(d), doctype)) + + def validate_permission_for_all_role(d): + if xhiveframework.session.user == "Administrator": + return + + if doctype.custom: + if d.role in AUTOMATIC_ROLES: + xhiveframework.throw( + _( + "Row # {0}: Non administrator user can not set the role {1} to the custom doctype" + ).format(d.idx, xhiveframework.bold(_(d.role))), + title=_("Permissions Error"), + ) + + roles = [row.name for row in xhiveframework.get_all("Role", filters={"is_custom": 1})] + + if d.role in roles: + xhiveframework.throw( + _( + "Row # {0}: Non administrator user can not set the role {1} to the custom doctype" + ).format(d.idx, xhiveframework.bold(_(d.role))), + title=_("Permissions Error"), + ) + + for d in permissions: + if not d.permlevel: + d.permlevel = 0 + check_atleast_one_set(d) + if not for_remove: + check_double(d) + check_permission_dependency(d) + check_if_submittable(d) + check_if_importable(d) + check_level_zero_is_set(d) + remove_rights_for_single(d) + validate_permission_for_all_role(d) + + +def make_module_and_roles(doc, perm_fieldname="permissions"): + """Make `Module Def` and `Role` records if already not made. Called while installing.""" + try: + if ( + hasattr(doc, "restrict_to_domain") + and doc.restrict_to_domain + and not xhiveframework.db.exists("Domain", doc.restrict_to_domain) + ): + xhiveframework.get_doc(dict(doctype="Domain", domain=doc.restrict_to_domain)).insert() + + if "tabModule Def" in xhiveframework.db.get_tables() and not xhiveframework.db.exists("Module Def", doc.module): + m = xhiveframework.get_doc({"doctype": "Module Def", "module_name": doc.module}) + if xhiveframework.scrub(doc.module) in xhiveframework.local.module_app: + m.app_name = xhiveframework.local.module_app[xhiveframework.scrub(doc.module)] + else: + m.app_name = "xhiveframework" + m.flags.ignore_mandatory = m.flags.ignore_permissions = True + if xhiveframework.flags.package: + m.package = xhiveframework.flags.package.name + m.custom = 1 + m.insert() + + roles = [p.role for p in doc.get("permissions") or []] + list(AUTOMATIC_ROLES) + + for role in list(set(roles)): + if xhiveframework.db.table_exists("Role", cached=False) and not xhiveframework.db.exists("Role", role): + r = xhiveframework.new_doc("Role") + r.role_name = role + r.desk_access = 1 + r.flags.ignore_mandatory = r.flags.ignore_permissions = True + r.insert() + except xhiveframework.DoesNotExistError: + pass + except xhiveframework.db.ProgrammingError as e: + if xhiveframework.db.is_table_missing(e): + pass + else: + raise + + +def check_fieldname_conflicts(docfield): + """Checks if fieldname conflicts with methods or properties""" + doc = xhiveframework.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 is_a_property(getattr(type(doc), x, None))] + 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 docfield.fieldname in method_list + property_list: + xhiveframework.msgprint(msg, raise_exception=not docfield.is_virtual) + + +def clear_linked_doctype_cache(): + xhiveframework.cache.delete_value("linked_doctypes_without_ignore_user_permissions_enabled") + + +def check_email_append_to(doc): + if not hasattr(doc, "email_append_to") or not doc.email_append_to: + return + + # Subject Field + doc.subject_field = doc.subject_field.strip() if doc.subject_field else None + subject_field = get_field(doc, doc.subject_field) + + if doc.subject_field and not subject_field: + xhiveframework.throw(_("Select a valid Subject field for creating documents from Email")) + + if subject_field and subject_field.fieldtype not in [ + "Data", + "Text", + "Long Text", + "Small Text", + "Text Editor", + ]: + xhiveframework.throw(_("Subject Field type should be Data, Text, Long Text, Small Text, Text Editor")) + + # Sender Field is mandatory + doc.sender_field = doc.sender_field.strip() if doc.sender_field else None + sender_field = get_field(doc, doc.sender_field) + + if doc.sender_field and not sender_field: + xhiveframework.throw(_("Select a valid Sender Field for creating documents from Email")) + + if not sender_field.options == "Email": + xhiveframework.throw(_("Sender Field should have Email in options")) + + +def get_field(doc, fieldname): + if not (doc or fieldname): + return + + for field in doc.fields: + if field.fieldname == fieldname: + return field + + +@xhiveframework.whitelist() +def get_row_size_utilization(doctype: str) -> float: + """Get row size utilization in percentage""" + + xhiveframework.has_permission("DocType", throw=True) + try: + return flt(xhiveframework.db.get_row_size(doctype) / xhiveframework.db.MAX_ROW_SIZE_LIMIT * 100, 2) + except Exception: + return 0.0 diff --git a/xhiveframework/core/doctype/doctype/doctype_list.js b/xhiveframework/core/doctype/doctype/doctype_list.js new file mode 100644 index 0000000..5f0395f --- /dev/null +++ b/xhiveframework/core/doctype/doctype/doctype_list.js @@ -0,0 +1,123 @@ +xhiveframework.listview_settings["DocType"] = { + primary_action: function () { + this.new_doctype_dialog(); + }, + + new_doctype_dialog() { + let non_developer = xhiveframework.session.user !== "Administrator" || !xhiveframework.boot.developer_mode; + let fields = [ + { + label: __("DocType Name"), + fieldname: "name", + fieldtype: "Data", + reqd: 1, + }, + { fieldtype: "Column Break" }, + { + label: __("Module"), + fieldname: "module", + fieldtype: "Link", + options: "Module Def", + reqd: 1, + }, + { fieldtype: "Section Break" }, + { + label: __("Is Submittable"), + fieldname: "is_submittable", + fieldtype: "Check", + description: __( + "Once submitted, submittable documents cannot be changed. They can only be Cancelled and Amended." + ), + depends_on: "eval:!doc.istable && !doc.issingle", + }, + { + label: __("Is Child Table"), + fieldname: "istable", + fieldtype: "Check", + description: __("Child Tables are shown as a Grid in other DocTypes"), + depends_on: "eval:!doc.is_submittable && !doc.issingle", + }, + { + label: __("Editable Grid"), + fieldname: "editable_grid", + fieldtype: "Check", + depends_on: "istable", + default: 1, + }, + { + label: __("Is Single"), + fieldname: "issingle", + fieldtype: "Check", + description: __( + "Single Types have only one record no tables associated. Values are stored in tabSingles" + ), + depends_on: "eval:!doc.istable && !doc.is_submittable", + }, + { + label: "Is Tree", + fieldname: "is_tree", + fieldtype: "Check", + default: "0", + depends_on: "eval:!doc.istable", + description: "Tree structures are implemented using Nested Set", + }, + { + label: __("Custom?"), + fieldname: "custom", + fieldtype: "Check", + default: non_developer, + read_only: non_developer, + }, + ]; + + if (!non_developer) { + fields.push({ + label: "Is Virtual", + fieldname: "is_virtual", + fieldtype: "Check", + default: "0", + }); + } + + let new_d = new xhiveframework.ui.Dialog({ + title: __("Create New DocType"), + fields: fields, + primary_action_label: __("Create & Continue"), + primary_action(values) { + if (!values.istable) values.editable_grid = 0; + xhiveframework.db + .insert({ + doctype: "DocType", + ...values, + permissions: [ + { + create: 1, + delete: 1, + email: 1, + export: 1, + print: 1, + read: 1, + report: 1, + role: "System Manager", + share: 1, + write: 1, + submit: values.is_submittable ? 1 : 0, + }, + ], + fields: [{ fieldtype: "Section Break" }], + }) + .then((doc) => { + xhiveframework.set_route("Form", "DocType", doc.name); + }); + }, + secondary_action_label: __("Cancel"), + secondary_action() { + new_d.hide(); + if (xhiveframework.get_route()[0] === "Form") { + xhiveframework.set_route("List", "DocType"); + } + }, + }); + new_d.show(); + }, +}; diff --git a/xhiveframework/core/doctype/doctype/patches/set_route.py b/xhiveframework/core/doctype/doctype/patches/set_route.py new file mode 100644 index 0000000..bdbb6cc --- /dev/null +++ b/xhiveframework/core/doctype/doctype/patches/set_route.py @@ -0,0 +1,8 @@ +import xhiveframework +from xhiveframework.desk.utils import slug + + +def execute(): + for doctype in xhiveframework.get_all("DocType", ["name", "route"], dict(istable=0)): + if not doctype.route: + xhiveframework.db.set_value("DocType", doctype.name, "route", slug(doctype.name), update_modified=False) diff --git a/xhiveframework/core/doctype/doctype/test_doctype.py b/xhiveframework/core/doctype/doctype/test_doctype.py new file mode 100644 index 0000000..86cee16 --- /dev/null +++ b/xhiveframework/core/doctype/doctype/test_doctype.py @@ -0,0 +1,819 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import os +import random +import string +import unittest +from unittest.mock import patch + +import xhiveframework +from xhiveframework.cache_manager import clear_doctype_cache +from xhiveframework.core.doctype.doctype.doctype import ( + CannotIndexedError, + DoctypeLinkError, + HiddenAndMandatoryWithoutDefaultError, + IllegalMandatoryError, + InvalidFieldNameError, + UniqueFieldnameError, + WrongOptionsDoctypeLinkError, + validate_links_table_fieldnames, +) +from xhiveframework.core.doctype.rq_job.test_rq_job import wait_for_completion +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_fields +from xhiveframework.desk.form.load import getdoc +from xhiveframework.model.delete_doc import delete_controllers +from xhiveframework.model.sync import remove_orphan_doctypes +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocType(XhiveFrameworkTestCase): + def tearDown(self): + xhiveframework.db.rollback() + + def test_validate_name(self): + self.assertRaises(xhiveframework.NameError, new_doctype("_Some DocType").insert) + self.assertRaises(xhiveframework.NameError, new_doctype("8Some DocType").insert) + self.assertRaises(xhiveframework.NameError, new_doctype("Some (DocType)").insert) + self.assertRaises( + xhiveframework.NameError, + new_doctype("Some Doctype with a name whose length is more than 61 characters").insert, + ) + for name in ("Some DocType", "Some_DocType", "Some-DocType"): + if xhiveframework.db.exists("DocType", name): + xhiveframework.delete_doc("DocType", name) + + doc = new_doctype(name).insert() + doc.delete() + + def test_making_sequence_on_change(self): + xhiveframework.delete_doc_if_exists("DocType", self._testMethodName) + dt = new_doctype(self._testMethodName).insert(ignore_permissions=True) + autoname = dt.autoname + + # change autoname + dt.autoname = "autoincrement" + dt.save() + + # check if name type has been changed + self.assertEqual( + xhiveframework.db.sql( + f"""select data_type FROM information_schema.columns + where column_name = 'name' and table_name = 'tab{self._testMethodName}'""" + )[0][0], + "bigint", + ) + + if xhiveframework.db.db_type == "mariadb": + table_name = "information_schema.tables" + conditions = f"table_type = 'sequence' and table_name = '{self._testMethodName}_id_seq'" + else: + table_name = "information_schema.sequences" + conditions = f"sequence_name = '{self._testMethodName}_id_seq'" + + # check if sequence table is created + self.assertTrue( + xhiveframework.db.sql( + f"""select * from {table_name} + where {conditions}""" + ) + ) + + # change the autoname/naming rule back to original + dt.autoname = autoname + dt.save() + + # check if name type has changed + self.assertEqual( + xhiveframework.db.sql( + f"""select data_type FROM information_schema.columns + where column_name = 'name' and table_name = 'tab{self._testMethodName}'""" + )[0][0], + "varchar" if xhiveframework.db.db_type == "mariadb" else "character varying", + ) + + def test_doctype_unique_constraint_dropped(self): + if xhiveframework.db.exists("DocType", "With_Unique"): + xhiveframework.delete_doc("DocType", "With_Unique") + + dt = new_doctype("With_Unique", unique=1) + dt.insert() + + doc1 = xhiveframework.new_doc("With_Unique") + doc2 = xhiveframework.new_doc("With_Unique") + doc1.some_fieldname = "Something" + doc1.name = "one" + doc2.some_fieldname = "Something" + doc2.name = "two" + + doc1.insert() + self.assertRaises(xhiveframework.UniqueValidationError, doc2.insert) + xhiveframework.db.rollback() + + dt.fields[0].unique = 0 + dt.save() + + doc2.insert() + doc1.delete() + doc2.delete() + + def test_validate_search_fields(self): + doc = new_doctype("Test Search Fields") + doc.search_fields = "some_fieldname" + doc.insert() + self.assertEqual(doc.name, "Test Search Fields") + + # check if invalid fieldname is allowed or not + doc.search_fields = "some_fieldname_1" + self.assertRaises(xhiveframework.ValidationError, doc.save) + + # check if no value fields are allowed in search fields + field = doc.append("fields", {}) + field.fieldname = "some_html_field" + field.fieldtype = "HTML" + field.label = "Some HTML Field" + doc.search_fields = "some_fieldname,some_html_field" + self.assertRaises(xhiveframework.ValidationError, doc.save) + + def test_depends_on_fields(self): + doc = new_doctype("Test Depends On", depends_on="eval:doc.__islocal == 0") + doc.insert() + + # check if the assignment operation is allowed in depends_on + field = doc.fields[0] + field.depends_on = "eval:doc.__islocal = 0" + self.assertRaises(xhiveframework.ValidationError, doc.save) + + def test_all_depends_on_fields_conditions(self): + import re + + docfields = xhiveframework.get_all( + "DocField", + or_filters={ + "ifnull(depends_on, '')": ("!=", ""), + "ifnull(collapsible_depends_on, '')": ("!=", ""), + "ifnull(mandatory_depends_on, '')": ("!=", ""), + "ifnull(read_only_depends_on, '')": ("!=", ""), + }, + fields=[ + "parent", + "depends_on", + "collapsible_depends_on", + "mandatory_depends_on", + "read_only_depends_on", + "fieldname", + "fieldtype", + ], + ) + + pattern = r'[\w\.:_]+\s*={1}\s*[\w\.@\'"]+' + for field in docfields: + for depends_on in [ + "depends_on", + "collapsible_depends_on", + "mandatory_depends_on", + "read_only_depends_on", + ]: + condition = field.get(depends_on) + if condition: + self.assertFalse(re.match(pattern, condition)) + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + def test_sync_field_order(self): + import os + + from xhiveframework.modules.import_file import get_file_path + + # create test doctype + test_doctype = xhiveframework.get_doc( + { + "doctype": "DocType", + "module": "Core", + "fields": [ + {"label": "Field 1", "fieldname": "field_1", "fieldtype": "Data"}, + {"label": "Field 2", "fieldname": "field_2", "fieldtype": "Data"}, + {"label": "Field 3", "fieldname": "field_3", "fieldtype": "Data"}, + {"label": "Field 4", "fieldname": "field_4", "fieldtype": "Data"}, + ], + "permissions": [{"role": "System Manager", "read": 1}], + "name": "Test Field Order DocType", + "__islocal": 1, + } + ) + + path = get_file_path(test_doctype.module, test_doctype.doctype, test_doctype.name) + initial_fields_order = ["field_1", "field_2", "field_3", "field_4"] + + xhiveframework.delete_doc_if_exists("DocType", "Test Field Order DocType") + if os.path.isfile(path): + os.remove(path) + + try: + xhiveframework.flags.allow_doctype_export = 1 + test_doctype.save() + + # assert that field_order list is being created with the default order + test_doctype_json = xhiveframework.get_file_json(path) + self.assertTrue(test_doctype_json.get("field_order")) + self.assertEqual(len(test_doctype_json["fields"]), len(test_doctype_json["field_order"])) + self.assertListEqual( + [f["fieldname"] for f in test_doctype_json["fields"]], test_doctype_json["field_order"] + ) + self.assertListEqual([f["fieldname"] for f in test_doctype_json["fields"]], initial_fields_order) + self.assertListEqual(test_doctype_json["field_order"], initial_fields_order) + + # remove field_order to test reload_doc/sync/migrate is backwards compatible without field_order + del test_doctype_json["field_order"] + with open(path, "w+") as txtfile: + txtfile.write(xhiveframework.as_json(test_doctype_json)) + + # assert that field_order is actually removed from the json file + test_doctype_json = xhiveframework.get_file_json(path) + self.assertFalse(test_doctype_json.get("field_order")) + + # make sure that migrate/sync is backwards compatible without field_order + xhiveframework.reload_doctype(test_doctype.name, force=True) + test_doctype.reload() + + # assert that field_order list is being created with the default order again + test_doctype.save() + test_doctype_json = xhiveframework.get_file_json(path) + self.assertTrue(test_doctype_json.get("field_order")) + self.assertEqual(len(test_doctype_json["fields"]), len(test_doctype_json["field_order"])) + self.assertListEqual( + [f["fieldname"] for f in test_doctype_json["fields"]], test_doctype_json["field_order"] + ) + self.assertListEqual([f["fieldname"] for f in test_doctype_json["fields"]], initial_fields_order) + self.assertListEqual(test_doctype_json["field_order"], initial_fields_order) + + # reorder fields: swap row 1 and 3 + test_doctype.fields[0], test_doctype.fields[2] = test_doctype.fields[2], test_doctype.fields[0] + for i, f in enumerate(test_doctype.fields): + f.idx = i + 1 + + # assert that reordering fields only affects `field_order` rather than `fields` attr + test_doctype.save() + test_doctype_json = xhiveframework.get_file_json(path) + self.assertListEqual([f["fieldname"] for f in test_doctype_json["fields"]], initial_fields_order) + self.assertListEqual( + test_doctype_json["field_order"], ["field_3", "field_2", "field_1", "field_4"] + ) + + # reorder `field_order` in the json file: swap row 2 and 4 + test_doctype_json["field_order"][1], test_doctype_json["field_order"][3] = ( + test_doctype_json["field_order"][3], + test_doctype_json["field_order"][1], + ) + with open(path, "w+") as txtfile: + txtfile.write(xhiveframework.as_json(test_doctype_json)) + + # assert that reordering `field_order` from json file is reflected in DocType upon migrate/sync + xhiveframework.reload_doctype(test_doctype.name, force=True) + test_doctype.reload() + self.assertListEqual( + [f.fieldname for f in test_doctype.fields], ["field_3", "field_4", "field_1", "field_2"] + ) + + # insert row in the middle and remove first row (field 3) + test_doctype.append("fields", {"label": "Field 5", "fieldname": "field_5", "fieldtype": "Data"}) + test_doctype.fields[4], test_doctype.fields[3] = test_doctype.fields[3], test_doctype.fields[4] + test_doctype.fields[3], test_doctype.fields[2] = test_doctype.fields[2], test_doctype.fields[3] + test_doctype.remove(test_doctype.fields[0]) + for i, f in enumerate(test_doctype.fields): + f.idx = i + 1 + + test_doctype.save() + test_doctype_json = xhiveframework.get_file_json(path) + self.assertListEqual( + [f["fieldname"] for f in test_doctype_json["fields"]], + ["field_1", "field_2", "field_4", "field_5"], + ) + self.assertListEqual( + test_doctype_json["field_order"], ["field_4", "field_5", "field_1", "field_2"] + ) + except Exception: + raise + finally: + xhiveframework.flags.allow_doctype_export = 0 + + def test_unique_field_name_for_two_fields(self): + doc = new_doctype("Test Unique Field") + field_1 = doc.append("fields", {}) + field_1.fieldname = "some_fieldname_1" + field_1.fieldtype = "Data" + + field_2 = doc.append("fields", {}) + field_2.fieldname = "some_fieldname_1" + field_2.fieldtype = "Data" + + self.assertRaises(UniqueFieldnameError, doc.insert) + + def test_fieldname_is_not_name(self): + doc = new_doctype("Test Name Field") + field_1 = doc.append("fields", {}) + field_1.label = "Name" + field_1.fieldtype = "Data" + doc.insert() + self.assertEqual(doc.fields[1].fieldname, "name1") + doc.fields[1].fieldname = "name" + self.assertRaises(InvalidFieldNameError, doc.save) + + def test_illegal_mandatory_validation(self): + doc = new_doctype("Test Illegal mandatory") + field_1 = doc.append("fields", {}) + field_1.fieldname = "some_fieldname_1" + field_1.fieldtype = "Section Break" + field_1.reqd = 1 + + self.assertRaises(IllegalMandatoryError, doc.insert) + + def test_link_with_wrong_and_no_options(self): + doc = new_doctype("Test link") + field_1 = doc.append("fields", {}) + field_1.fieldname = "some_fieldname_1" + field_1.fieldtype = "Link" + + self.assertRaises(DoctypeLinkError, doc.insert) + + field_1.options = "wrongdoctype" + + self.assertRaises(WrongOptionsDoctypeLinkError, doc.insert) + + def test_hidden_and_mandatory_without_default(self): + doc = new_doctype("Test hidden and mandatory") + field_1 = doc.append("fields", {}) + field_1.fieldname = "some_fieldname_1" + field_1.fieldtype = "Data" + field_1.reqd = 1 + field_1.hidden = 1 + + self.assertRaises(HiddenAndMandatoryWithoutDefaultError, doc.insert) + + def test_field_can_not_be_indexed_validation(self): + doc = new_doctype("Test index") + field_1 = doc.append("fields", {}) + field_1.fieldname = "some_fieldname_1" + field_1.fieldtype = "Long Text" + field_1.search_index = 1 + + self.assertRaises(CannotIndexedError, doc.insert) + + def test_cancel_link_doctype(self): + import json + + from xhiveframework.desk.form.linked_with import cancel_all_linked_docs, get_submitted_linked_docs + + # create doctype + link_doc = new_doctype("Test Linked Doctype") + link_doc.is_submittable = 1 + for data in link_doc.get("permissions"): + data.submit = 1 + data.cancel = 1 + link_doc.insert() + + doc = new_doctype("Test Doctype") + doc.is_submittable = 1 + field_2 = doc.append("fields", {}) + field_2.label = "Test Linked Doctype" + field_2.fieldname = "test_linked_doctype" + field_2.fieldtype = "Link" + field_2.options = "Test Linked Doctype" + for data in link_doc.get("permissions"): + data.submit = 1 + data.cancel = 1 + doc.insert() + + # create doctype data + data_link_doc = xhiveframework.new_doc("Test Linked Doctype") + data_link_doc.some_fieldname = "Data1" + data_link_doc.insert() + data_link_doc.save() + data_link_doc.submit() + + data_doc = xhiveframework.new_doc("Test Doctype") + data_doc.some_fieldname = "Data1" + data_doc.test_linked_doctype = data_link_doc.name + data_doc.insert() + data_doc.save() + data_doc.submit() + + docs = get_submitted_linked_docs(link_doc.name, data_link_doc.name) + dump_docs = json.dumps(docs.get("docs")) + cancel_all_linked_docs(dump_docs) + data_link_doc.cancel() + data_doc.load_from_db() + self.assertEqual(data_link_doc.docstatus, 2) + self.assertEqual(data_doc.docstatus, 2) + + # delete doctype record + data_doc.delete() + data_link_doc.delete() + + # delete doctype + link_doc.delete() + doc.delete() + xhiveframework.db.commit() + + def test_ignore_cancelation_of_linked_doctype_during_cancel(self): + import json + + from xhiveframework.desk.form.linked_with import cancel_all_linked_docs, get_submitted_linked_docs + + # create linked doctype + link_doc = new_doctype("Test Linked Doctype 1") + link_doc.is_submittable = 1 + for data in link_doc.get("permissions"): + data.submit = 1 + data.cancel = 1 + link_doc.insert() + + # create first parent doctype + test_doc_1 = new_doctype("Test Doctype 1") + test_doc_1.is_submittable = 1 + + field_2 = test_doc_1.append("fields", {}) + field_2.label = "Test Linked Doctype 1" + field_2.fieldname = "test_linked_doctype_a" + field_2.fieldtype = "Link" + field_2.options = "Test Linked Doctype 1" + + for data in test_doc_1.get("permissions"): + data.submit = 1 + data.cancel = 1 + test_doc_1.insert() + + # crete second parent doctype + doc = new_doctype("Test Doctype 2") + doc.is_submittable = 1 + + field_2 = doc.append("fields", {}) + field_2.label = "Test Linked Doctype 1" + field_2.fieldname = "test_linked_doctype_a" + field_2.fieldtype = "Link" + field_2.options = "Test Linked Doctype 1" + + for data in link_doc.get("permissions"): + data.submit = 1 + data.cancel = 1 + doc.insert() + + # create doctype data + data_link_doc_1 = xhiveframework.new_doc("Test Linked Doctype 1") + data_link_doc_1.some_fieldname = "Data1" + data_link_doc_1.insert() + data_link_doc_1.save() + data_link_doc_1.submit() + + data_doc_2 = xhiveframework.new_doc("Test Doctype 1") + data_doc_2.some_fieldname = "Data1" + data_doc_2.test_linked_doctype_a = data_link_doc_1.name + data_doc_2.insert() + data_doc_2.save() + data_doc_2.submit() + + data_doc = xhiveframework.new_doc("Test Doctype 2") + data_doc.some_fieldname = "Data1" + data_doc.test_linked_doctype_a = data_link_doc_1.name + data_doc.insert() + data_doc.save() + data_doc.submit() + + docs = get_submitted_linked_docs(link_doc.name, data_link_doc_1.name) + dump_docs = json.dumps(docs.get("docs")) + + cancel_all_linked_docs(dump_docs, ignore_doctypes_on_cancel_all=["Test Doctype 2"]) + + # checking that doc for Test Doctype 2 is not canceled + self.assertRaises(xhiveframework.LinkExistsError, data_link_doc_1.cancel) + + data_doc.load_from_db() + data_doc_2.load_from_db() + self.assertEqual(data_link_doc_1.docstatus, 2) + + # linked doc is canceled + self.assertEqual(data_doc_2.docstatus, 2) + + # ignored doctype 2 during cancel + self.assertEqual(data_doc.docstatus, 1) + + # delete doctype record + data_doc.cancel() + data_doc.delete() + data_doc_2.delete() + data_link_doc_1.delete() + + # delete doctype + link_doc.delete() + doc.delete() + test_doc_1.delete() + xhiveframework.db.commit() + + def test_links_table_fieldname_validation(self): + doc = new_doctype("Test Links Table Validation") + + # check valid data + doc.append("links", {"link_doctype": "User", "link_fieldname": "first_name"}) + validate_links_table_fieldnames(doc) # no error + doc.links = [] # reset links table + + # check invalid doctype + doc.append("links", {"link_doctype": "User2", "link_fieldname": "first_name"}) + self.assertRaises(InvalidFieldNameError, validate_links_table_fieldnames, doc) + doc.links = [] # reset links table + + # check invalid fieldname + doc.append("links", {"link_doctype": "User", "link_fieldname": "a_field_that_does_not_exists"}) + + self.assertRaises(InvalidFieldNameError, validate_links_table_fieldnames, doc) + + def test_create_virtual_doctype(self): + """Test virtual DocType.""" + virtual_doc = new_doctype("Test Virtual Doctype") + virtual_doc.is_virtual = 1 + virtual_doc.insert(ignore_if_duplicate=True) + virtual_doc.reload() + doc = xhiveframework.get_doc("DocType", "Test Virtual Doctype") + + self.assertDictEqual(doc.as_dict(), virtual_doc.as_dict()) + self.assertEqual(doc.is_virtual, 1) + self.assertFalse(xhiveframework.db.table_exists("Test Virtual Doctype")) + + def test_create_virtual_doctype_as_child_table(self): + """Test virtual DocType as Child Table below a normal DocType.""" + xhiveframework.delete_doc_if_exists("DocType", "Test Parent Virtual DocType", force=1) + xhiveframework.delete_doc_if_exists("DocType", "Test Virtual DocType as Child Table", force=1) + + virtual_doc = new_doctype("Test Virtual DocType as Child Table") + virtual_doc.is_virtual = 1 + virtual_doc.istable = 1 + virtual_doc.insert(ignore_permissions=True) + + doc = xhiveframework.get_doc("DocType", "Test Virtual DocType as Child Table") + + self.assertEqual(doc.is_virtual, 1) + self.assertEqual(doc.istable, 1) + self.assertFalse(xhiveframework.db.table_exists("Test Virtual DocType as Child Table")) + + parent_doc = new_doctype("Test Parent Virtual DocType") + parent_doc.append( + "fields", + { + "fieldname": "virtual_child_table", + "fieldtype": "Table", + "options": "Test Virtual DocType as Child Table", + }, + ) + self.assertRaises(xhiveframework.exceptions.ValidationError, parent_doc.insert) + parent_doc.is_virtual = 1 + parent_doc.insert(ignore_permissions=True) + self.assertFalse(xhiveframework.db.table_exists("Test Parent Virtual DocType")) + + def test_default_fieldname(self): + fields = [ + {"label": "title", "fieldname": "title", "fieldtype": "Data", "default": "{some_fieldname}"} + ] + dt = new_doctype("DT with default field", fields=fields) + dt.insert() + + dt.delete() + + def test_autoincremented_doctype_transition(self): + xhiveframework.delete_doc_if_exists("DocType", "testy_autoinc_dt") + dt = new_doctype("testy_autoinc_dt", autoname="autoincrement").insert(ignore_permissions=True) + dt.autoname = "hash" + + dt.save(ignore_permissions=True) + + dt_data = xhiveframework.get_doc({"doctype": dt.name, "some_fieldname": "test data"}).insert( + ignore_permissions=True + ) + + dt.autoname = "autoincrement" + + try: + dt.save(ignore_permissions=True) + except xhiveframework.ValidationError as e: + self.assertEqual( + e.args[0], + "Can only change to/from Autoincrement naming rule when there is no data in the doctype", + ) + else: + self.fail( + """Shouldn't be possible to transition to/from autoincremented doctype + when data is present in doctype""" + ) + finally: + # cleanup + dt_data.delete(ignore_permissions=True) + dt.delete(ignore_permissions=True) + + def test_json_field(self): + """Test json field.""" + import json + + json_doc = new_doctype( + "Test Json Doctype", + fields=[{"label": "json field", "fieldname": "test_json_field", "fieldtype": "JSON"}], + ) + json_doc.insert() + json_doc.save() + doc = xhiveframework.get_doc("DocType", "Test Json Doctype") + for field in doc.fields: + if field.fieldname == "test_json_field": + self.assertEqual(field.fieldtype, "JSON") + break + + doc = xhiveframework.get_doc( + {"doctype": "Test Json Doctype", "test_json_field": json.dumps({"hello": "world"})} + ) + doc.insert() + doc.save() + + test_json = xhiveframework.get_doc("Test Json Doctype", doc.name) + + if isinstance(test_json.test_json_field, str): + test_json.test_json_field = json.loads(test_json.test_json_field) + + self.assertEqual(test_json.test_json_field["hello"], "world") + + def test_no_delete_doc(self): + self.assertRaises(xhiveframework.ValidationError, xhiveframework.delete_doc, "DocType", "Address") + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + @patch.dict(xhiveframework.conf, {"developer_mode": 1}) + def test_export_types(self): + """Export python types.""" + import ast + + from xhiveframework.types.exporter import TypeExporter + + def validate(code): + ast.parse(code) + + doctype = new_doctype(custom=0).insert() + + exporter = TypeExporter(doctype) + code = exporter.controller_path.read_text() + validate(code) + + # regenerate and verify and file is same word to word. + exporter.export_types() + new_code = exporter.controller_path.read_text() + validate(new_code) + + self.assertEqual(code, new_code) + + # Add fields and save + + fieldname = "test_type" + doctype.append("fields", {"fieldname": fieldname, "fieldtype": "Int"}) + doctype.save() + + new_field_code = exporter.controller_path.read_text() + validate(new_field_code) + + self.assertIn(fieldname, new_field_code) + self.assertIn("Int", new_field_code) + + doctype.delete() + xhiveframework.db.commit() + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + @patch.dict(xhiveframework.conf, {"developer_mode": 1}) + def test_custom_field_deletion(self): + """Custom child tables whose doctype doesn't exist should be auto deleted.""" + doctype = new_doctype(custom=0).insert().name + child = new_doctype(custom=0, istable=1).insert().name + + field = "abc" + create_custom_fields({doctype: [{"fieldname": field, "fieldtype": "Table", "options": child}]}) + + xhiveframework.delete_doc("DocType", child) + self.assertFalse(xhiveframework.get_meta(doctype).get_field(field)) + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + @patch.dict(xhiveframework.conf, {"developer_mode": 1}) + def test_delete_doctype_with_customization(self): + from xhiveframework.custom.doctype.property_setter.property_setter import make_property_setter + + custom_field = "customfield" + + doctype = new_doctype(custom=0).insert().name + + # Create property setter and custom field + field = "some_fieldname" + make_property_setter(doctype, field, "default", "DELETETHIS", "Data") + create_custom_fields({doctype: [{"fieldname": custom_field, "fieldtype": "Data"}]}) + + # Create 1 record + original_doc = xhiveframework.get_doc(doctype=doctype, custom_field_name="wat").insert() + self.assertEqual(original_doc.some_fieldname, "DELETETHIS") + + # delete doctype + xhiveframework.delete_doc("DocType", doctype) + clear_doctype_cache(doctype) + + # "restore" doctype by inserting doctype with same schema again + new_doctype(doctype, custom=0).insert() + + # Ensure basically same doctype getting "restored" + restored_doc = xhiveframework.get_last_doc(doctype) + verify_fields = ["doctype", field, custom_field] + for f in verify_fields: + self.assertEqual(original_doc.get(f), restored_doc.get(f)) + + # Check form load of restored doctype + getdoc(doctype, restored_doc.name) + + # ensure meta - property setter + self.assertEqual(xhiveframework.get_meta(doctype).get_field(field).default, "DELETETHIS") + xhiveframework.delete_doc("DocType", doctype) + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + @patch.dict(xhiveframework.conf, {"developer_mode": 1}) + def test_delete_orphaned_doctypes(self): + doctype = new_doctype(custom=0).insert() + xhiveframework.db.commit() + + delete_controllers(doctype.name, doctype.module) + job = xhiveframework.enqueue(remove_orphan_doctypes) + wait_for_completion(job) + + xhiveframework.db.rollback() + self.assertFalse(xhiveframework.db.exists("DocType", doctype.name)) + + def test_not_in_list_view_for_not_allowed_mandatory_field(self): + doctype = new_doctype( + fields=[ + { + "fieldname": "cover_image", + "fieldtype": "Attach Image", + "label": "Cover Image", + "reqd": 1, # mandatory + }, + { + "fieldname": "book_name", + "fieldtype": "Data", + "label": "Book Name", + "reqd": 1, # mandatory + }, + ], + ).insert() + + self.assertFalse(doctype.fields[0].in_list_view) + self.assertTrue(doctype.fields[1].in_list_view) + xhiveframework.delete_doc("DocType", doctype.name) + + +def new_doctype( + name: str | None = None, + unique: bool = False, + depends_on: str = "", + fields: list[dict] | None = None, + custom: bool = True, + default: str | None = None, + **kwargs, +): + if not name: + # Test prefix is required to avoid coverage + name = "Test " + "".join(random.sample(string.ascii_lowercase, 10)) + + doc = xhiveframework.get_doc( + { + "doctype": "DocType", + "module": "Core", + "custom": custom, + "fields": [ + { + "label": "Some Field", + "fieldname": "some_fieldname", + "fieldtype": "Data", + "unique": unique, + "default": default, + "depends_on": depends_on, + } + ], + "permissions": [ + { + "role": "System Manager", + "read": 1, + } + ], + "name": name, + **kwargs, + } + ) + + if fields and len(fields) > 0: + doc.set("fields", fields) + + return doc diff --git a/xhiveframework/core/doctype/doctype_action/__init__.py b/xhiveframework/core/doctype/doctype_action/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/doctype_action/doctype_action.json b/xhiveframework/core/doctype/doctype_action/doctype_action.json new file mode 100644 index 0000000..080755c --- /dev/null +++ b/xhiveframework/core/doctype/doctype_action/doctype_action.json @@ -0,0 +1,74 @@ +{ + "actions": [], + "creation": "2019-09-23 16:28:13.953520", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "label", + "action_type", + "action", + "group", + "hidden", + "custom" + ], + "fields": [ + { + "columns": 2, + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "fieldname": "group", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Group" + }, + { + "columns": 2, + "fieldname": "action_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Action Type", + "options": "Server Action\nRoute", + "reqd": 1 + }, + { + "columns": 4, + "fieldname": "action", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Action / Route", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-09-24 14:19:05.549835", + "modified_by": "Administrator", + "module": "Core", + "name": "DocType Action", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype_action/doctype_action.py b/xhiveframework/core/doctype/doctype_action/doctype_action.py new file mode 100644 index 0000000..58004a1 --- /dev/null +++ b/xhiveframework/core/doctype/doctype_action/doctype_action.py @@ -0,0 +1,27 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DocTypeAction(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action: DF.SmallText + action_type: DF.Literal["Server Action", "Route"] + custom: DF.Check + group: DF.Data | None + hidden: DF.Check + label: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/doctype_link/__init__.py b/xhiveframework/core/doctype/doctype_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/doctype_link/doctype_link.json b/xhiveframework/core/doctype/doctype_link/doctype_link.json new file mode 100644 index 0000000..4baec67 --- /dev/null +++ b/xhiveframework/core/doctype/doctype_link/doctype_link.json @@ -0,0 +1,87 @@ +{ + "actions": [], + "creation": "2019-09-24 11:41:25.291377", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "link_doctype", + "link_fieldname", + "parent_doctype", + "table_fieldname", + "group", + "hidden", + "is_child_table", + "custom" + ], + "fields": [ + { + "fieldname": "link_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Link DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "link_fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Link Fieldname", + "reqd": 1 + }, + { + "fieldname": "group", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Group" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom" + }, + { + "depends_on": "is_child_table", + "fieldname": "parent_doctype", + "fieldtype": "Link", + "label": "Parent DocType", + "mandatory_depends_on": "is_child_table", + "options": "DocType" + }, + { + "default": "0", + "fetch_from": "link_doctype.istable", + "fieldname": "is_child_table", + "fieldtype": "Check", + "label": "Is Child Table", + "read_only": 1 + }, + { + "fieldname": "table_fieldname", + "fieldtype": "Data", + "label": "Table Fieldname" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-07-31 15:23:12.237491", + "modified_by": "Administrator", + "module": "Core", + "name": "DocType Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype_link/doctype_link.py b/xhiveframework/core/doctype/doctype_link/doctype_link.py new file mode 100644 index 0000000..f6c3059 --- /dev/null +++ b/xhiveframework/core/doctype/doctype_link/doctype_link.py @@ -0,0 +1,29 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DocTypeLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + custom: DF.Check + group: DF.Data | None + hidden: DF.Check + is_child_table: DF.Check + link_doctype: DF.Link + link_fieldname: DF.Data + parent: DF.Data + parent_doctype: DF.Link | None + parentfield: DF.Data + parenttype: DF.Data + table_fieldname: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/doctype_state/__init__.py b/xhiveframework/core/doctype/doctype_state/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/doctype_state/doctype_state.json b/xhiveframework/core/doctype/doctype_state/doctype_state.json new file mode 100644 index 0000000..79797b4 --- /dev/null +++ b/xhiveframework/core/doctype/doctype_state/doctype_state.json @@ -0,0 +1,50 @@ +{ + "actions": [], + "creation": "2021-08-23 17:21:28.345841", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "title", + "color", + "custom" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "reqd": 1 + }, + { + "default": "Blue", + "fieldname": "color", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Color", + "options": "Blue\nCyan\nGray\nGreen\nLight Blue\nOrange\nPink\nPurple\nRed\nYellow", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-12-14 14:14:55.716378", + "modified_by": "Administrator", + "module": "Core", + "name": "DocType State", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/doctype_state/doctype_state.py b/xhiveframework/core/doctype/doctype_state/doctype_state.py new file mode 100644 index 0000000..9be11cf --- /dev/null +++ b/xhiveframework/core/doctype/doctype_state/doctype_state.py @@ -0,0 +1,26 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DocTypeState(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + color: DF.Literal[ + "Blue", "Cyan", "Gray", "Green", "Light Blue", "Orange", "Pink", "Purple", "Red", "Yellow" + ] + custom: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + title: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/document_naming_rule/__init__.py b/xhiveframework/core/doctype/document_naming_rule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.js b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.js new file mode 100644 index 0000000..ebf3393 --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.js @@ -0,0 +1,46 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Document Naming Rule", { + refresh: function (frm) { + frm.trigger("document_type"); + frm.last_counter_value = frm.doc.counter; + frm.skip_before_save = false; + }, + before_save: function (frm) { + if (frm.is_new() || frm.skip_before_save || frm.last_counter_value === frm.doc.counter) + return; + + xhiveframework.validated = false; + xhiveframework.warn( + __("Are you sure?"), + __("Updating counter may lead to document name conflicts if not done properly"), + () => { + frm.skip_before_save = true; + frm.save(); + }, + __("Proceed"), + false + ); + }, + document_type: (frm) => { + // update the select field options with fieldnames + if (frm.doc.document_type) { + xhiveframework.model.with_doctype(frm.doc.document_type, () => { + let fieldnames = xhiveframework + .get_meta(frm.doc.document_type) + .fields.filter((d) => { + return xhiveframework.model.no_value_type.indexOf(d.fieldtype) === -1; + }) + .map((d) => { + return { label: `${d.label} (${d.fieldname})`, value: d.fieldname }; + }); + frm.fields_dict.conditions.grid.update_docfield_property( + "field", + "options", + fieldnames + ); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.json b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.json new file mode 100644 index 0000000..1e2247c --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.json @@ -0,0 +1,115 @@ +{ + "actions": [], + "creation": "2020-09-07 12:48:48.334318", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "disabled", + "priority", + "section_break_3", + "conditions", + "naming_section", + "prefix", + "counter", + "column_break_xfqa", + "prefix_digits" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "fieldname": "prefix", + "fieldtype": "Data", + "label": "Prefix", + "mandatory_depends_on": "eval:doc.naming_by===\"Numbered\"", + "reqd": 1 + }, + { + "default": "0", + "description": "Warning: Updating counter may lead to document name conflicts if not done properly", + "fieldname": "counter", + "fieldtype": "Int", + "label": "Counter", + "no_copy": 1 + }, + { + "default": "5", + "description": "Example: 00001", + "fieldname": "prefix_digits", + "fieldtype": "Int", + "label": "Digits", + "mandatory_depends_on": "eval:doc.naming_by===\"Numbered\"", + "reqd": 1 + }, + { + "fieldname": "naming_section", + "fieldtype": "Section Break", + "label": "Naming" + }, + { + "collapsible": 1, + "collapsible_depends_on": "conditions", + "fieldname": "section_break_3", + "fieldtype": "Section Break", + "label": "Rule Conditions" + }, + { + "fieldname": "conditions", + "fieldtype": "Table", + "label": "Conditions", + "options": "Document Naming Rule Condition" + }, + { + "description": "Rules with higher priority number will be applied first.", + "fieldname": "priority", + "fieldtype": "Int", + "label": "Priority" + }, + { + "fieldname": "column_break_xfqa", + "fieldtype": "Column Break" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-04-24 15:14:32.054272", + "modified_by": "Administrator", + "module": "Core", + "name": "Document Naming Rule", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "document_type", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.py b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.py new file mode 100644 index 0000000..43b9db0 --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule/document_naming_rule.py @@ -0,0 +1,69 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.model.naming import parse_naming_series +from xhiveframework.utils.data import evaluate_filters + + +class DocumentNamingRule(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.document_naming_rule_condition.document_naming_rule_condition import ( + DocumentNamingRuleCondition, + ) + from xhiveframework.types import DF + + conditions: DF.Table[DocumentNamingRuleCondition] + counter: DF.Int + disabled: DF.Check + document_type: DF.Link + prefix: DF.Data + prefix_digits: DF.Int + priority: DF.Int + + # end: auto-generated types + def validate(self): + self.validate_fields_in_conditions() + + def clear_doctype_map(self): + xhiveframework.cache_manager.clear_doctype_map(self.doctype, self.document_type) + + def on_update(self): + self.clear_doctype_map() + + def on_trash(self): + self.clear_doctype_map() + + def validate_fields_in_conditions(self): + if self.has_value_changed("document_type"): + docfields = [x.fieldname for x in xhiveframework.get_meta(self.document_type).fields] + for condition in self.conditions: + if condition.field not in docfields: + xhiveframework.throw( + _("{0} is not a field of doctype {1}").format( + xhiveframework.bold(condition.field), xhiveframework.bold(self.document_type) + ) + ) + + def apply(self, doc): + """ + Apply naming rules for the given document. Will set `name` if the rule is matched. + """ + if self.conditions: + if not evaluate_filters( + doc, [(self.document_type, d.field, d.condition, d.value) for d in self.conditions] + ): + return + + counter = xhiveframework.db.get_value(self.doctype, self.name, "counter", for_update=True) or 0 + naming_series = parse_naming_series(self.prefix, doc=doc) + + doc.name = naming_series + ("%0" + str(self.prefix_digits) + "d") % (counter + 1) + xhiveframework.db.set_value(self.doctype, self.name, "counter", counter + 1) diff --git a/xhiveframework/core/doctype/document_naming_rule/test_document_naming_rule.py b/xhiveframework/core/doctype/document_naming_rule/test_document_naming_rule.py new file mode 100644 index 0000000..a3ee08d --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule/test_document_naming_rule.py @@ -0,0 +1,70 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocumentNamingRule(XhiveFrameworkTestCase): + def test_naming_rule_by_series(self): + naming_rule = xhiveframework.get_doc( + dict(doctype="Document Naming Rule", document_type="ToDo", prefix="test-todo-", prefix_digits=5) + ).insert() + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="Is this my name " + xhiveframework.generate_hash()) + ).insert() + + self.assertEqual(todo.name, "test-todo-00001") + + naming_rule.delete() + todo.delete() + + def test_naming_rule_by_condition(self): + naming_rule = xhiveframework.get_doc( + dict( + doctype="Document Naming Rule", + document_type="ToDo", + prefix="test-high-", + prefix_digits=5, + priority=10, + conditions=[dict(field="priority", condition="=", value="High")], + ) + ).insert() + + # another rule + naming_rule_1 = xhiveframework.copy_doc(naming_rule) + naming_rule_1.prefix = "test-medium-" + naming_rule_1.conditions[0].value = "Medium" + naming_rule_1.insert() + + # default rule with low priority - should not get applied for rules + # with higher priority + naming_rule_2 = xhiveframework.copy_doc(naming_rule) + naming_rule_2.prefix = "test-low-" + naming_rule_2.priority = 0 + naming_rule_2.conditions = [] + naming_rule_2.insert() + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", priority="High", description="Is this my name " + xhiveframework.generate_hash()) + ).insert() + + todo_1 = xhiveframework.get_doc( + dict(doctype="ToDo", priority="Medium", description="Is this my name " + xhiveframework.generate_hash()) + ).insert() + + todo_2 = xhiveframework.get_doc( + dict(doctype="ToDo", priority="Low", description="Is this my name " + xhiveframework.generate_hash()) + ).insert() + + try: + self.assertEqual(todo.name, "test-high-00001") + self.assertEqual(todo_1.name, "test-medium-00001") + self.assertEqual(todo_2.name, "test-low-00001") + finally: + naming_rule.delete() + naming_rule_1.delete() + naming_rule_2.delete() + todo.delete() + todo_1.delete() + todo_2.delete() diff --git a/xhiveframework/core/doctype/document_naming_rule_condition/__init__.py b/xhiveframework/core/doctype/document_naming_rule_condition/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.js b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.js new file mode 100644 index 0000000..30d794b --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Document Naming Rule Condition", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.json b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.json new file mode 100644 index 0000000..781566b --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.json @@ -0,0 +1,49 @@ +{ + "actions": [], + "creation": "2020-09-08 10:17:54.366279", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "field", + "condition", + "value" + ], + "fields": [ + { + "fieldname": "field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Field", + "reqd": 1 + }, + { + "fieldname": "condition", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Condition", + "options": "=\n!=\n>\n<\n>=\n<=", + "reqd": 1 + }, + { + "fieldname": "value", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Value", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-09-08 10:19:56.192949", + "modified_by": "Administrator", + "module": "Core", + "name": "Document Naming Rule Condition", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.py b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.py new file mode 100644 index 0000000..57e756f --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule_condition/document_naming_rule_condition.py @@ -0,0 +1,24 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DocumentNamingRuleCondition(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + condition: DF.Literal["=", "!=", ">", "<", ">=", "<="] + field: DF.Literal[None] + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + value: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/document_naming_rule_condition/test_document_naming_rule_condition.py b/xhiveframework/core/doctype/document_naming_rule_condition/test_document_naming_rule_condition.py new file mode 100644 index 0000000..feac9d1 --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_rule_condition/test_document_naming_rule_condition.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocumentNamingRuleCondition(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/document_naming_settings/__init__.py b/xhiveframework/core/doctype/document_naming_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.js b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.js new file mode 100644 index 0000000..c9890ff --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.js @@ -0,0 +1,80 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Document Naming Settings", { + setup: function (frm) { + frm.set_query("document_type", "amend_naming_override", () => { + return { + filters: { + is_submittable: 1, + }, + }; + }); + }, + + refresh: function (frm) { + frm.trigger("setup_transaction_autocomplete"); + frm.disable_save(); + }, + + setup_transaction_autocomplete: function (frm) { + xhiveframework.call({ + method: "get_transactions_and_prefixes", + doc: frm.doc, + callback: function (r) { + frm.fields_dict.transaction_type.set_data(r.message.transactions); + frm.fields_dict.prefix.set_data(r.message.prefixes); + }, + }); + }, + + transaction_type: function (frm) { + frm.set_value("user_must_always_select", 0); + xhiveframework.call({ + method: "get_options", + doc: frm.doc, + callback: function (r) { + frm.set_value("naming_series_options", r.message); + if (r.message && r.message.split("\n")[0] == "") + frm.set_value("user_must_always_select", 1); + }, + }); + }, + + prefix: function (frm) { + xhiveframework.call({ + method: "get_current", + doc: frm.doc, + callback: function (r) { + frm.refresh_field("current_value"); + }, + }); + }, + + update: function (frm) { + xhiveframework.call({ + method: "update_series", + doc: frm.doc, + freeze: true, + freeze_msg: __("Updating naming series options"), + callback: function (r) { + frm.trigger("setup_transaction_autocomplete"); + frm.trigger("transaction_type"); + }, + }); + }, + + try_naming_series(frm) { + xhiveframework.call({ + method: "preview_series", + doc: frm.doc, + callback: function (r) { + if (!r.exc) { + frm.set_value("series_preview", r.message); + } else { + frm.set_value("series_preview", __("Failed to generate preview of series")); + } + }, + }); + }, +}); diff --git a/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.json b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.json new file mode 100644 index 0000000..5a1991c --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.json @@ -0,0 +1,165 @@ +{ + "actions": [], + "creation": "2022-05-30 07:24:07.736646", + "description": "Configure various aspects of how document naming works like naming series, current counter.", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "naming_series_tab", + "setup_series", + "transaction_type", + "naming_series_options", + "user_must_always_select", + "update", + "column_break_9", + "try_naming_series", + "series_preview", + "help_html", + "update_series", + "prefix", + "current_value", + "update_series_start", + "amended_documents_section", + "default_amend_naming", + "amend_naming_override", + "update_amendment_naming" + ], + "fields": [ + { + "collapsible": 1, + "description": "Set Naming Series options on your transactions.", + "fieldname": "setup_series", + "fieldtype": "Section Break", + "label": "Setup Series for transactions" + }, + { + "depends_on": "transaction_type", + "fieldname": "help_html", + "fieldtype": "HTML", + "label": "Help HTML", + "options": "
    \n Edit list of Series in the box. Rules:\n
      \n
    • Each Series Prefix on a new line.
    • \n
    • Allowed special characters are \"/\" and \"-\"
    • \n
    • \n Optionally, set the number of digits in the series using dot (.)\n followed by hashes (#). For example, \".####\" means that the series\n will have four digits. Default is five digits.\n
    • \n
    • \n You can also use variables in the series name by putting them\n between (.) dots\n
      \n Supported Variables:\n
        \n
      • .YYYY. - Year in 4 digits
      • \n
      • .YY. - Year in 2 digits
      • \n
      • .MM. - Month
      • \n
      • .DD. - Day of month
      • \n
      • .WW. - Week of the year
      • \n
      • .FY. - Fiscal Year
      • \n
      • \n .{fieldname}. - fieldname on the document e.g.\n branch\n
      • \n
      \n
    • \n
    \n Examples:\n
      \n
    • INV-
    • \n
    • INV-10-
    • \n
    • INVK-
    • \n
    • INV-.YYYY.-.{branch}.-.MM.-.####
    • \n
    \n
    \n
    \n" + }, + { + "default": "0", + "depends_on": "transaction_type", + "description": "Check this if you want to force the user to select a series before saving. There will be no default if you check this.", + "fieldname": "user_must_always_select", + "fieldtype": "Check", + "label": "User must always select" + }, + { + "depends_on": "transaction_type", + "fieldname": "update", + "fieldtype": "Button", + "label": "Update" + }, + { + "collapsible": 1, + "description": "Change the starting / current sequence number of an existing series.
    \n\nWarning: Incorrectly updating counters can prevent documents from getting created. ", + "fieldname": "update_series", + "fieldtype": "Section Break", + "label": "Update Series Counter" + }, + { + "fieldname": "prefix", + "fieldtype": "Autocomplete", + "label": "Prefix" + }, + { + "description": "This is the number of the last created transaction with this prefix", + "fieldname": "current_value", + "fieldtype": "Int", + "label": "Current Value" + }, + { + "fieldname": "update_series_start", + "fieldtype": "Button", + "label": "Update Series Number", + "options": "update_series_start" + }, + { + "depends_on": "transaction_type", + "fieldname": "naming_series_options", + "fieldtype": "Text", + "label": "Series List for this Transaction" + }, + { + "depends_on": "transaction_type", + "description": "Get a preview of generated names with a series.", + "fieldname": "try_naming_series", + "fieldtype": "Data", + "label": "Try a Naming Series" + }, + { + "fieldname": "transaction_type", + "fieldtype": "Autocomplete", + "label": "Select Transaction" + }, + { + "fieldname": "column_break_9", + "fieldtype": "Column Break" + }, + { + "fieldname": "naming_series_tab", + "fieldtype": "Tab Break", + "label": "Naming Series" + }, + { + "fieldname": "series_preview", + "fieldtype": "Text", + "label": "Preview of generated names", + "read_only": 1 + }, + { + "collapsible": 1, + "description": "Configure how amended documents will be named.
    \n\nDefault behaviour is to follow an amend counter which adds a number to the end of the original name indicating the amended version.
    \n\nDefault Naming will make the amended document to behave same as new documents.", + "fieldname": "amended_documents_section", + "fieldtype": "Section Break", + "label": "Amended Documents" + }, + { + "default": "Amend Counter", + "fieldname": "default_amend_naming", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Default Amendment Naming", + "options": "Amend Counter\nDefault Naming", + "reqd": 1 + }, + { + "fieldname": "amend_naming_override", + "fieldtype": "Table", + "label": "Amendment Naming Override", + "options": "Amended Document Naming Settings" + }, + { + "fieldname": "update_amendment_naming", + "fieldtype": "Button", + "label": "Update Amendment Naming", + "options": "update_amendment_rule" + } + ], + "hide_toolbar": 1, + "icon": "fa fa-sort-by-order", + "issingle": 1, + "links": [], + "modified": "2023-06-20 17:47:52.204139", + "modified_by": "Administrator", + "module": "Core", + "name": "Document Naming Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.py b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.py new file mode 100644 index 0000000..8527fd0 --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_settings/document_naming_settings.py @@ -0,0 +1,256 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.doctype.doctype.doctype import validate_series +from xhiveframework.model.document import Document +from xhiveframework.model.naming import NamingSeries +from xhiveframework.permissions import get_doctypes_with_read + + +class NamingSeriesNotSetError(xhiveframework.ValidationError): + pass + + +class DocumentNamingSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.amended_document_naming_settings.amended_document_naming_settings import ( + AmendedDocumentNamingSettings, + ) + from xhiveframework.types import DF + + amend_naming_override: DF.Table[AmendedDocumentNamingSettings] + current_value: DF.Int + default_amend_naming: DF.Literal["Amend Counter", "Default Naming"] + naming_series_options: DF.Text | None + prefix: DF.Autocomplete | None + series_preview: DF.Text | None + transaction_type: DF.Autocomplete | None + try_naming_series: DF.Data | None + user_must_always_select: DF.Check + + # end: auto-generated types + @xhiveframework.whitelist() + def get_transactions_and_prefixes(self): + transactions = self._get_transactions() + prefixes = self._get_prefixes(transactions) + + return {"transactions": transactions, "prefixes": prefixes} + + def _get_transactions(self) -> list[str]: + readable_doctypes = set(get_doctypes_with_read()) + + standard = xhiveframework.get_all("DocField", {"fieldname": "naming_series"}, "parent", pluck="parent") + custom = xhiveframework.get_all("Custom Field", {"fieldname": "naming_series"}, "dt", pluck="dt") + + return sorted(readable_doctypes.intersection(standard + custom)) + + def _get_prefixes(self, doctypes) -> list[str]: + """Get all prefixes for naming series. + + - For all templates prefix is evaluated considering today's date + - All existing prefix in DB are shared as is. + """ + series_templates = set() + for d in doctypes: + try: + options = xhiveframework.get_meta(d).get_naming_series_options() + series_templates.update(options) + except xhiveframework.DoesNotExistError: + xhiveframework.msgprint(_("Unable to find DocType {0}").format(d)) + continue + + custom_templates = xhiveframework.get_all( + "DocType", + fields=["autoname"], + filters={ + "name": ("not in", doctypes), + "autoname": ("like", "%.#%"), + "module": ("not in", ["Core"]), + }, + ) + if custom_templates: + series_templates.update([d.autoname.rsplit(".", 1)[0] for d in custom_templates]) + + return self._evaluate_and_clean_templates(series_templates) + + def _evaluate_and_clean_templates(self, series_templates: set[str]) -> list[str]: + evalauted_prefix = set() + + series = xhiveframework.qb.DocType("Series") + prefixes_from_db = xhiveframework.qb.from_(series).select(series.name).run(pluck=True) + evalauted_prefix.update(prefixes_from_db) + + for series_template in series_templates: + try: + prefix = NamingSeries(series_template).get_prefix() + if "{" in prefix: + # fieldnames can't be evalauted, rely on data in DB instead + continue + evalauted_prefix.add(prefix) + except Exception: + xhiveframework.clear_last_message() + xhiveframework.log_error(f"Invalid naming series {series_template}") + + return sorted(evalauted_prefix) + + def get_options_list(self, options: str) -> list[str]: + return [op.strip() for op in options.split("\n") if op.strip()] + + @xhiveframework.whitelist() + def update_series(self): + """update series list""" + self.validate_set_series() + self.check_duplicate() + self.set_series_options_in_meta(self.transaction_type, self.naming_series_options) + + xhiveframework.msgprint( + _("Series Updated for {}").format(self.transaction_type), alert=True, indicator="green" + ) + + def validate_set_series(self): + if self.transaction_type and not self.naming_series_options: + xhiveframework.throw(_("Please set the series to be used.")) + + def set_series_options_in_meta(self, doctype: str, options: str) -> None: + options = self.get_options_list(options) + + # validate names + for series in options: + self.validate_series_name(series) + + if options and self.user_must_always_select: + options = ["", *options] + + default = options[0] if options else "" + + option_string = "\n".join(options) + + # Erase default first, it might not be in new options. + self.update_naming_series_property_setter(doctype, "default", "") + self.update_naming_series_property_setter(doctype, "options", option_string) + self.update_naming_series_property_setter(doctype, "default", default) + + self.naming_series_options = option_string + + xhiveframework.clear_cache(doctype=doctype) + + def update_naming_series_property_setter(self, doctype, property, value): + from xhiveframework.custom.doctype.property_setter.property_setter import make_property_setter + + make_property_setter(doctype, "naming_series", property, value, "Text") + + def check_duplicate(self): + def stripped_series(s: str) -> str: + return s.strip().rstrip("#") + + standard = xhiveframework.get_all("DocField", {"fieldname": "naming_series"}, "parent", pluck="parent") + custom = xhiveframework.get_all("Custom Field", {"fieldname": "naming_series"}, "dt", pluck="dt") + + all_doctypes_with_naming_series = set(standard + custom) + all_doctypes_with_naming_series.remove(self.transaction_type) + + existing_series = {} + for doctype in all_doctypes_with_naming_series: + for series in xhiveframework.get_meta(doctype).get_naming_series_options(): + existing_series[stripped_series(series)] = doctype + + dt = xhiveframework.get_doc("DocType", self.transaction_type) + + options = self.get_options_list(self.naming_series_options) + for series in options: + if stripped_series(series) in existing_series: + xhiveframework.throw(_("Series {0} already used in {1}").format(series, existing_series[series])) + validate_series(dt, series) + + def validate_series_name(self, series): + NamingSeries(series).validate() + + @xhiveframework.whitelist() + def get_options(self, doctype=None): + doctype = doctype or self.transaction_type + if not doctype: + return + + if xhiveframework.get_meta(doctype or self.transaction_type).get_field("naming_series"): + return xhiveframework.get_meta(doctype or self.transaction_type).get_field("naming_series").options + + @xhiveframework.whitelist() + def get_current(self): + """get series current""" + if self.prefix is not None: + self.current_value = NamingSeries(self.prefix).get_current_value() + return self.current_value + + @xhiveframework.whitelist() + def update_amendment_rule(self): + self.db_set("default_amend_naming", self.default_amend_naming) + + existing_overrides = xhiveframework.db.get_all( + "Amended Document Naming Settings", + filters={"name": ["not in", [d.name for d in self.amend_naming_override]]}, + pluck="name", + ) + for override in existing_overrides: + xhiveframework.delete_doc("Amended Document Naming Settings", override) + + for row in self.amend_naming_override: + row.save() + + xhiveframework.msgprint(_("Amendment naming rules updated."), indicator="green", alert=True) + + @xhiveframework.whitelist() + def update_series_start(self): + xhiveframework.only_for("System Manager") + + if self.prefix is None: + xhiveframework.throw(_("Please select prefix first")) + + naming_series = NamingSeries(self.prefix) + previous_value = naming_series.get_current_value() + naming_series.update_counter(self.current_value) + + self.create_version_log_for_change(naming_series.get_prefix(), previous_value, self.current_value) + + xhiveframework.msgprint( + _("Series counter for {} updated to {} successfully").format(self.prefix, self.current_value), + alert=True, + indicator="green", + ) + + def create_version_log_for_change(self, series, old, new): + version = xhiveframework.new_doc("Version") + version.ref_doctype = "Series" + version.docname = series or ".#" + version.data = xhiveframework.as_json({"changed": [["current", old, new]]}) + version.flags.ignore_links = True # series is not a "real" doctype + version.flags.ignore_permissions = True + version.insert() + + @xhiveframework.whitelist() + def preview_series(self) -> str: + """Preview what the naming series will generate.""" + + series = self.try_naming_series + if not series: + return "" + try: + doc = self._fetch_last_doc_if_available() + return "\n".join(NamingSeries(series).get_preview(doc=doc)) + except Exception as e: + xhiveframework.clear_last_message() + return _("Failed to generate names from the series") + f"\n{e!s}" + + def _fetch_last_doc_if_available(self): + """Fetch last doc for evaluating naming series with fields.""" + try: + return xhiveframework.get_last_doc(self.transaction_type) + except Exception: + return None diff --git a/xhiveframework/core/doctype/document_naming_settings/test_document_naming_settings.py b/xhiveframework/core/doctype/document_naming_settings/test_document_naming_settings.py new file mode 100644 index 0000000..648101e --- /dev/null +++ b/xhiveframework/core/doctype/document_naming_settings/test_document_naming_settings.py @@ -0,0 +1,117 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# See license.txt + +import xhiveframework +from xhiveframework.core.doctype.doctype.test_doctype import new_doctype +from xhiveframework.core.doctype.document_naming_settings.document_naming_settings import ( + DocumentNamingSettings, +) +from xhiveframework.model.naming import NamingSeries, get_default_naming_series +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import cint + + +class TestNamingSeries(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.ns_doctype = ( + new_doctype( + fields=[ + { + "label": "Series", + "fieldname": "naming_series", + "fieldtype": "Select", + "options": f"\n{xhiveframework.generate_hash()}-.###", + } + ], + autoname="naming_series:", + is_submittable=1, + ) + .insert() + .name + ) + + def setUp(self): + self.dns: DocumentNamingSettings = xhiveframework.get_doc("Document Naming Settings") + + def tearDown(self): + xhiveframework.db.rollback() + + def get_valid_serieses(self): + VALID_SERIES = ["SINV-", "SI-.{field}.", "SI-#.###", ""] + exisiting_series = self.dns.get_transactions_and_prefixes()["prefixes"] + return VALID_SERIES + exisiting_series + + def test_naming_preview(self): + self.dns.transaction_type = self.ns_doctype + + self.dns.try_naming_series = "AXBZ.####" + serieses = self.dns.preview_series().split("\n") + self.assertEqual(["AXBZ0001", "AXBZ0002", "AXBZ0003"], serieses) + + self.dns.try_naming_series = "AXBZ-.{currency}.-" + serieses = self.dns.preview_series().split("\n") + + def test_get_transactions(self): + naming_info = self.dns.get_transactions_and_prefixes() + self.assertIn(self.ns_doctype, naming_info["transactions"]) + + existing_naming_series = xhiveframework.get_meta(self.ns_doctype).get_field("naming_series").options + + for series in existing_naming_series.split("\n"): + self.assertIn(NamingSeries(series).get_prefix(), naming_info["prefixes"]) + + def test_default_naming_series(self): + self.assertIsNone(get_default_naming_series("DocType")) + + def test_updates_naming_options(self): + self.dns.transaction_type = self.ns_doctype + test_series = "KOOHBEW.###" + self.dns.naming_series_options = self.dns.get_options() + "\n" + test_series + self.dns.update_series() + self.assertIn(test_series, xhiveframework.get_meta(self.ns_doctype).get_naming_series_options()) + + def test_update_series_counter(self): + for series in self.get_valid_serieses(): + if not series: + continue + self.dns.prefix = series + current_count = cint(self.dns.get_current()) + new_count = self.dns.current_value = current_count + 1 + self.dns.update_series_start() + + self.assertEqual(self.dns.get_current(), new_count, f"Incorrect update for {series}") + + def test_amended_naming(self): + self.dns.amend_naming_override = [] + self.dns.default_amend_naming = "Amend Counter" + self.dns.update_amendment_rule() + + submittable_doc = xhiveframework.get_doc( + dict(doctype=self.ns_doctype, some_fieldname="test doc with submit") + ).submit() + submittable_doc.cancel() + + amended_doc = xhiveframework.get_doc( + dict( + doctype=self.ns_doctype, + some_fieldname="test doc with submit", + amended_from=submittable_doc.name, + ) + ).insert() + + self.assertIn(submittable_doc.name, amended_doc.name) + amended_doc.delete() + + self.dns.default_amend_naming = "Default Naming" + self.dns.update_amendment_rule() + + new_amended_doc = xhiveframework.get_doc( + dict( + doctype=self.ns_doctype, + some_fieldname="test doc with submit", + amended_from=submittable_doc.name, + ) + ).insert() + self.assertNotIn(submittable_doc.name, new_amended_doc.name) diff --git a/xhiveframework/core/doctype/document_share_key/__init__.py b/xhiveframework/core/doctype/document_share_key/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/document_share_key/document_share_key.js b/xhiveframework/core/doctype/document_share_key/document_share_key.js new file mode 100644 index 0000000..cb64431 --- /dev/null +++ b/xhiveframework/core/doctype/document_share_key/document_share_key.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Document Share Key", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/document_share_key/document_share_key.json b/xhiveframework/core/doctype/document_share_key/document_share_key.json new file mode 100644 index 0000000..b96fe09 --- /dev/null +++ b/xhiveframework/core/doctype/document_share_key/document_share_key.json @@ -0,0 +1,73 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "hash", + "creation": "2022-01-14 13:40:49.487646", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "reference_docname", + "key", + "expires_on" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "reference_docname", + "fieldtype": "Dynamic Link", + "label": "Reference Document Name", + "options": "reference_doctype", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "key", + "fieldtype": "Data", + "label": "Key", + "read_only": 1 + }, + { + "fieldname": "expires_on", + "fieldtype": "Date", + "in_list_view": 1, + "label": "Expires On", + "read_only": 1 + } + ], + "in_create": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2022-01-14 13:57:28.050678", + "modified_by": "Administrator", + "module": "Core", + "name": "Document Share Key", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/document_share_key/document_share_key.py b/xhiveframework/core/doctype/document_share_key/document_share_key.py new file mode 100644 index 0000000..d995881 --- /dev/null +++ b/xhiveframework/core/doctype/document_share_key/document_share_key.py @@ -0,0 +1,34 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +from random import randrange + +import xhiveframework +from xhiveframework.model.document import Document + + +class DocumentShareKey(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + expires_on: DF.Date | None + key: DF.Data | None + reference_docname: DF.DynamicLink | None + reference_doctype: DF.Link | None + + # end: auto-generated types + def before_insert(self): + self.key = xhiveframework.generate_hash(length=randrange(25, 35)) + if not self.expires_on and not self.flags.no_expiry: + self.expires_on = xhiveframework.utils.add_days( + None, days=xhiveframework.get_system_settings("document_share_key_expiry") or 90 + ) + + +def is_expired(expires_on): + return expires_on and expires_on < xhiveframework.utils.getdate() diff --git a/xhiveframework/core/doctype/document_share_key/test_document_share_key.py b/xhiveframework/core/doctype/document_share_key/test_document_share_key.py new file mode 100644 index 0000000..bc23872 --- /dev/null +++ b/xhiveframework/core/doctype/document_share_key/test_document_share_key.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocumentShareKey(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/domain/__init__.py b/xhiveframework/core/doctype/domain/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/domain/domain.js b/xhiveframework/core/doctype/domain/domain.js new file mode 100644 index 0000000..d679c83 --- /dev/null +++ b/xhiveframework/core/doctype/domain/domain.js @@ -0,0 +1,6 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Domain", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/domain/domain.json b/xhiveframework/core/doctype/domain/domain.json new file mode 100644 index 0000000..a6c7397 --- /dev/null +++ b/xhiveframework/core/doctype/domain/domain.json @@ -0,0 +1,54 @@ +{ + "autoname": "field:domain", + "creation": "2017-05-03 15:07:39.752820", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "domain" + ], + "fields": [ + { + "fieldname": "domain", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Domain", + "reqd": 1, + "unique": 1 + } + ], + "modified": "2020-09-18 17:26:09.703215", + "modified_by": "Administrator", + "module": "Core", + "name": "Domain", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "search_fields": "domain", + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "domain" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/domain/domain.py b/xhiveframework/core/doctype/domain/domain.py new file mode 100644 index 0000000..0127dbd --- /dev/null +++ b/xhiveframework/core/doctype/domain/domain.py @@ -0,0 +1,136 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_fields +from xhiveframework.model.document import Document + + +class Domain(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + domain: DF.Data + # end: auto-generated types + """Domain documents are created automatically when DocTypes + with "Restricted" domains are imported during + installation or migration""" + + def setup_domain(self): + """Setup domain icons, permissions, custom fields etc.""" + self.setup_data() + self.setup_roles() + self.setup_properties() + self.set_values() + + if not int(xhiveframework.defaults.get_defaults().setup_complete or 0): + # if setup not complete, setup desktop etc. + self.setup_sidebar_items() + self.set_default_portal_role() + + if self.data.custom_fields: + create_custom_fields(self.data.custom_fields) + + if self.data.on_setup: + # custom on_setup method + xhiveframework.get_attr(self.data.on_setup)() + + def remove_domain(self): + """Unset domain settings""" + self.setup_data() + + if self.data.restricted_roles: + for role_name in self.data.restricted_roles: + if xhiveframework.db.exists("Role", role_name): + role = xhiveframework.get_doc("Role", role_name) + role.disabled = 1 + role.save() + + self.remove_custom_field() + + def remove_custom_field(self): + """Remove custom_fields when disabling domain""" + if self.data.custom_fields: + for doctype in self.data.custom_fields: + custom_fields = self.data.custom_fields[doctype] + + # custom_fields can be a list or dict + if isinstance(custom_fields, dict): + custom_fields = [custom_fields] + + for custom_field_detail in custom_fields: + custom_field_name = xhiveframework.db.get_value( + "Custom Field", dict(dt=doctype, fieldname=custom_field_detail.get("fieldname")) + ) + if custom_field_name: + xhiveframework.delete_doc("Custom Field", custom_field_name) + + def setup_roles(self): + """Enable roles that are restricted to this domain""" + if self.data.restricted_roles: + user = xhiveframework.get_doc("User", xhiveframework.session.user) + for role_name in self.data.restricted_roles: + user.append("roles", {"role": role_name}) + if not xhiveframework.db.get_value("Role", role_name): + xhiveframework.get_doc(dict(doctype="Role", role_name=role_name)).insert() + continue + + role = xhiveframework.get_doc("Role", role_name) + role.disabled = 0 + role.save() + user.save() + + def setup_data(self, domain=None): + """Load domain info via hooks""" + self.data = xhiveframework.get_domain_data(self.name) + + def get_domain_data(self, module): + return xhiveframework.get_attr(xhiveframework.get_hooks("domains")[self.name] + ".data") + + def set_default_portal_role(self): + """Set default portal role based on domain""" + if self.data.get("default_portal_role"): + xhiveframework.db.set_single_value( + "Portal Settings", "default_role", self.data.get("default_portal_role") + ) + + def setup_properties(self): + if self.data.properties: + for args in self.data.properties: + xhiveframework.make_property_setter(args) + + def set_values(self): + """set values based on `data.set_value`""" + if self.data.set_value: + for args in self.data.set_value: + xhiveframework.reload_doctype(args[0]) + doc = xhiveframework.get_doc(args[0], args[1] or args[0]) + doc.set(args[2], args[3]) + doc.save() + + def setup_sidebar_items(self): + """Enable / disable sidebar items""" + if self.data.allow_sidebar_items: + # disable all + xhiveframework.db.sql("update `tabPortal Menu Item` set enabled=0") + + # enable + xhiveframework.db.sql( + """update `tabPortal Menu Item` set enabled=1 + where route in ({})""".format(", ".join(f'"{d}"' for d in self.data.allow_sidebar_items)) + ) + + if self.data.remove_sidebar_items: + # disable all + xhiveframework.db.sql("update `tabPortal Menu Item` set enabled=1") + + # enable + xhiveframework.db.sql( + """update `tabPortal Menu Item` set enabled=0 + where route in ({})""".format(", ".join(f'"{d}"' for d in self.data.remove_sidebar_items)) + ) diff --git a/xhiveframework/core/doctype/domain/test_domain.py b/xhiveframework/core/doctype/domain/test_domain.py new file mode 100644 index 0000000..87ce0fe --- /dev/null +++ b/xhiveframework/core/doctype/domain/test_domain.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDomain(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/domain_settings/__init__.py b/xhiveframework/core/doctype/domain_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/domain_settings/domain_settings.js b/xhiveframework/core/doctype/domain_settings/domain_settings.js new file mode 100644 index 0000000..35ab302 --- /dev/null +++ b/xhiveframework/core/doctype/domain_settings/domain_settings.js @@ -0,0 +1,69 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Domain Settings", { + before_load: function (frm) { + if (!frm.domains_multicheck) { + frm.domains_multicheck = xhiveframework.ui.form.make_control({ + parent: frm.fields_dict.domains_html.$wrapper, + df: { + fieldname: "domains_multicheck", + fieldtype: "MultiCheck", + get_data: () => { + let active_domains = (frm.doc.active_domains || []).map( + (row) => row.domain + ); + return xhiveframework.boot.all_domains.map((domain) => { + return { + label: domain, + value: domain, + checked: active_domains.includes(domain), + }; + }); + }, + on_change: () => { + frm.dirty(); + }, + }, + render_input: true, + }); + frm.domains_multicheck.refresh_input(); + } + }, + + validate: function (frm) { + frm.trigger("set_options_in_table"); + }, + + set_options_in_table: function (frm) { + let selected_options = frm.domains_multicheck.get_value(); + let unselected_options = frm.domains_multicheck.options + .map((option) => option.value) + .filter((value) => { + return !selected_options.includes(value); + }); + + let map = {}, + list = []; + (frm.doc.active_domains || []).map((row) => { + map[row.domain] = row.name; + list.push(row.domain); + }); + + unselected_options.map((option) => { + if (list.includes(option)) { + xhiveframework.model.clear_doc("Has Domain", map[option]); + } + }); + + selected_options.map((option) => { + if (!list.includes(option)) { + xhiveframework.model.clear_doc("Has Domain", map[option]); + let row = xhiveframework.model.add_child(frm.doc, "Has Domain", "active_domains"); + row.domain = option; + } + }); + + refresh_field("active_domains"); + }, +}); diff --git a/xhiveframework/core/doctype/domain_settings/domain_settings.json b/xhiveframework/core/doctype/domain_settings/domain_settings.json new file mode 100644 index 0000000..c363529 --- /dev/null +++ b/xhiveframework/core/doctype/domain_settings/domain_settings.json @@ -0,0 +1,56 @@ +{ + "actions": [], + "creation": "2017-05-03 16:28:11.295095", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "active_domains_sb", + "domains_html", + "active_domains" + ], + "fields": [ + { + "fieldname": "active_domains_sb", + "fieldtype": "Section Break", + "label": "Active Domains" + }, + { + "fieldname": "domains_html", + "fieldtype": "HTML", + "label": "Domains HTML" + }, + { + "fieldname": "active_domains", + "fieldtype": "Table", + "hidden": 1, + "label": "Active Domains", + "options": "Has Domain", + "read_only": 1 + } + ], + "issingle": 1, + "links": [], + "modified": "2022-08-03 12:20:53.256607", + "modified_by": "Administrator", + "module": "Core", + "name": "Domain Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/domain_settings/domain_settings.py b/xhiveframework/core/doctype/domain_settings/domain_settings.py new file mode 100644 index 0000000..b46e48f --- /dev/null +++ b/xhiveframework/core/doctype/domain_settings/domain_settings.py @@ -0,0 +1,102 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class DomainSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_domain.has_domain import HasDomain + from xhiveframework.types import DF + + active_domains: DF.Table[HasDomain] + + # end: auto-generated types + def set_active_domains(self, domains): + active_domains = [d.domain for d in self.active_domains] + added = False + for d in domains: + if d not in active_domains: + self.append("active_domains", dict(domain=d)) + added = True + + if added: + self.save() + + def on_update(self): + for i, d in enumerate(self.active_domains): + # set the flag to update the the desktop icons of all domains + if i >= 1: + xhiveframework.flags.keep_desktop_icons = True + domain = xhiveframework.get_doc("Domain", d.domain) + domain.setup_domain() + + self.restrict_roles_and_modules() + xhiveframework.clear_cache() + + def restrict_roles_and_modules(self): + """Disable all restricted roles and set `restrict_to_domain` property in Module Def""" + active_domains = xhiveframework.get_active_domains() + all_domains = list(xhiveframework.get_hooks("domains") or {}) + + def remove_role(role): + xhiveframework.db.delete("Has Role", {"role": role}) + xhiveframework.set_value("Role", role, "disabled", 1) + + for domain in all_domains: + data = xhiveframework.get_domain_data(domain) + if not xhiveframework.db.get_value("Domain", domain): + xhiveframework.get_doc(dict(doctype="Domain", domain=domain)).insert() + if "modules" in data: + for module in data.get("modules"): + xhiveframework.db.set_value("Module Def", module, "restrict_to_domain", domain) + + if "restricted_roles" in data: + for role in data["restricted_roles"]: + if not xhiveframework.db.get_value("Role", role): + xhiveframework.get_doc(dict(doctype="Role", role_name=role)).insert() + xhiveframework.db.set_value("Role", role, "restrict_to_domain", domain) + + if domain not in active_domains: + remove_role(role) + + if "custom_fields" in data: + if domain not in active_domains: + inactive_domain = xhiveframework.get_doc("Domain", domain) + inactive_domain.setup_data() + inactive_domain.remove_custom_field() + + +def get_active_domains(): + """get the domains set in the Domain Settings as active domain""" + + def _get_active_domains(): + domains = xhiveframework.get_all( + "Has Domain", filters={"parent": "Domain Settings"}, fields=["domain"], distinct=True + ) + + active_domains = [row.get("domain") for row in domains] + active_domains.append("") + return active_domains + + return xhiveframework.cache.get_value("active_domains", _get_active_domains) + + +def get_active_modules(): + """get the active modules from Module Def""" + + def _get_active_modules(): + active_modules = [] + active_domains = get_active_domains() + for m in xhiveframework.get_all("Module Def", fields=["name", "restrict_to_domain"]): + if (not m.restrict_to_domain) or (m.restrict_to_domain in active_domains): + active_modules.append(m.name) + return active_modules + + return xhiveframework.cache.get_value("active_modules", _get_active_modules) diff --git a/xhiveframework/core/doctype/dynamic_link/__init__.py b/xhiveframework/core/doctype/dynamic_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/dynamic_link/dynamic_link.json b/xhiveframework/core/doctype/dynamic_link/dynamic_link.json new file mode 100644 index 0000000..b99f77f --- /dev/null +++ b/xhiveframework/core/doctype/dynamic_link/dynamic_link.json @@ -0,0 +1,47 @@ +{ + "creation": "2017-01-13 04:55:18.835023", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "link_doctype", + "link_name", + "link_title" + ], + "fields": [ + { + "fieldname": "link_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Link Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "link_name", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Link Name", + "options": "link_doctype", + "reqd": 1 + }, + { + "fieldname": "link_title", + "fieldtype": "Read Only", + "in_list_view": 1, + "label": "Link Title", + "read_only": 1 + } + ], + "istable": 1, + "modified": "2019-10-10 22:05:54.736093", + "modified_by": "Administrator", + "module": "Core", + "name": "Dynamic Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/dynamic_link/dynamic_link.py b/xhiveframework/core/doctype/dynamic_link/dynamic_link.py new file mode 100644 index 0000000..1cfabd7 --- /dev/null +++ b/xhiveframework/core/doctype/dynamic_link/dynamic_link.py @@ -0,0 +1,43 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class DynamicLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + link_doctype: DF.Link + link_name: DF.DynamicLink + link_title: DF.ReadOnly | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass + + +def on_doctype_update(): + xhiveframework.db.add_index("Dynamic Link", ["link_doctype", "link_name"]) + + +def deduplicate_dynamic_links(doc): + links, duplicate = [], False + for l in doc.links or []: + t = (l.link_doctype, l.link_name) + if t not in links: + links.append(t) + else: + duplicate = True + + if duplicate: + doc.links = [] + for l in links: + doc.append("links", dict(link_doctype=l[0], link_name=l[1])) diff --git a/xhiveframework/core/doctype/error_log/__init__.py b/xhiveframework/core/doctype/error_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/error_log/error_log.js b/xhiveframework/core/doctype/error_log/error_log.js new file mode 100644 index 0000000..ff74503 --- /dev/null +++ b/xhiveframework/core/doctype/error_log/error_log.js @@ -0,0 +1,17 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Error Log", { + refresh: function (frm) { + frm.disable_save(); + + if (frm.doc.reference_doctype && frm.doc.reference_name) { + frm.add_custom_button(__("Show Related Errors"), function () { + xhiveframework.set_route("List", "Error Log", { + reference_doctype: frm.doc.reference_doctype, + reference_name: frm.doc.reference_name, + }); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/error_log/error_log.json b/xhiveframework/core/doctype/error_log/error_log.json new file mode 100644 index 0000000..813fb5f --- /dev/null +++ b/xhiveframework/core/doctype/error_log/error_log.json @@ -0,0 +1,95 @@ +{ + "actions": [], + "creation": "2013-01-16 13:09:40", + "doctype": "DocType", + "document_type": "System", + "engine": "MyISAM", + "field_order": [ + "seen", + "reference_doctype", + "column_break_3", + "reference_name", + "section_break_5", + "method", + "error", + "trace_id" + ], + "fields": [ + { + "default": "0", + "fieldname": "seen", + "fieldtype": "Check", + "hidden": 1, + "label": "Seen" + }, + { + "fieldname": "method", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "read_only": 1 + }, + { + "fieldname": "error", + "fieldtype": "Code", + "label": "Error", + "read_only": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Reference DocType", + "options": "DocType", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "reference_name", + "fieldtype": "Data", + "label": "Reference Name", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_5", + "fieldtype": "Section Break" + }, + { + "fieldname": "trace_id", + "fieldtype": "Data", + "label": "Trace ID", + "read_only": 1 + } + ], + "icon": "fa fa-warning-sign", + "idx": 1, + "in_create": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Core", + "name": "Error Log", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "method" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/error_log/error_log.py b/xhiveframework/core/doctype/error_log/error_log.py new file mode 100644 index 0000000..5ec6a85 --- /dev/null +++ b/xhiveframework/core/doctype/error_log/error_log.py @@ -0,0 +1,42 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.query_builder import Interval +from xhiveframework.query_builder.functions import Now + + +class ErrorLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + error: DF.Code | None + method: DF.Data | None + reference_doctype: DF.Link | None + reference_name: DF.Data | None + seen: DF.Check + trace_id: DF.Data | None + + # end: auto-generated types + def onload(self): + if not self.seen and not xhiveframework.flags.read_only: + self.db_set("seen", 1, update_modified=0) + xhiveframework.db.commit() + + @staticmethod + def clear_old_logs(days=30): + table = xhiveframework.qb.DocType("Error Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + +@xhiveframework.whitelist() +def clear_error_logs(): + """Flush all Error Logs""" + xhiveframework.only_for("System Manager") + xhiveframework.db.truncate("Error Log") diff --git a/xhiveframework/core/doctype/error_log/error_log_list.js b/xhiveframework/core/doctype/error_log/error_log_list.js new file mode 100644 index 0000000..2bf975f --- /dev/null +++ b/xhiveframework/core/doctype/error_log/error_log_list.js @@ -0,0 +1,25 @@ +xhiveframework.listview_settings["Error Log"] = { + add_fields: ["seen"], + get_indicator: function (doc) { + if (cint(doc.seen)) { + return [__("Seen"), "green", "seen,=,1"]; + } else { + return [__("Not Seen"), "red", "seen,=,0"]; + } + }, + order_by: "seen asc, modified desc", + onload: function (listview) { + listview.page.add_menu_item(__("Clear Error Logs"), function () { + xhiveframework.call({ + method: "xhiveframework.core.doctype.error_log.error_log.clear_error_logs", + callback: function () { + listview.refresh(); + }, + }); + }); + + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(cur_list.doctype); + }); + }, +}; diff --git a/xhiveframework/core/doctype/error_log/test_error_log.py b/xhiveframework/core/doctype/error_log/test_error_log.py new file mode 100644 index 0000000..a68e625 --- /dev/null +++ b/xhiveframework/core/doctype/error_log/test_error_log.py @@ -0,0 +1,72 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from unittest.mock import patch + +from ldap3.core.exceptions import LDAPException, LDAPInappropriateAuthenticationResult + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils.error import _is_ldap_exception, guess_exception_source + +# test_records = xhiveframework.get_test_records('Error Log') + + +class TestErrorLog(XhiveFrameworkTestCase): + def test_error_log(self): + """let's do an error log on error log?""" + doc = xhiveframework.new_doc("Error Log") + error = doc.log_error("This is an error") + self.assertEqual(error.doctype, "Error Log") + + def test_ldap_exceptions(self): + exc = [LDAPException, LDAPInappropriateAuthenticationResult] + + for e in exc: + self.assertTrue(_is_ldap_exception(e())) + + +_RAW_EXC = """ + File "apps/xhiveframework/xhiveframework/model/document.py", line 1284, in runner + add_to_return_value(self, fn(self, *args, **kwargs)) + ^^^^^^^^^^^^^^^^^^^^^^^^^ + File "apps/xhiveframework/xhiveframework/model/document.py", line 933, in fn + return method_object(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "apps/xhiveerp/xhiveerp/selling/doctype/sales_order/sales_order.py", line 58, in onload + raise Exception("what") + Exception: what +""" + +_THROW_EXC = """ + File "apps/xhiveframework/xhiveframework/model/document.py", line 933, in fn + return method_object(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "apps/xhiveerp/xhiveerp/selling/doctype/sales_order/sales_order.py", line 58, in onload + xhiveframework.throw("what") + File "apps/xhiveframework/xhiveframework/__init__.py", line 550, in throw + msgprint( + File "apps/xhiveframework/xhiveframework/__init__.py", line 518, in msgprint + _raise_exception() + File "apps/xhiveframework/xhiveframework/__init__.py", line 467, in _raise_exception + raise raise_exception(msg) + xhiveframework.exceptions.ValidationError: what +""" + +TEST_EXCEPTIONS = ( + ( + "xhiveerp (app)", + _RAW_EXC, + ), + ( + "xhiveerp (app)", + _THROW_EXC, + ), +) + + +class TestExceptionSourceGuessing(XhiveFrameworkTestCase): + @patch.object(xhiveframework, "get_installed_apps", return_value=["xhiveframework", "xhiveerp", "3pa"]) + def test_exc_source_guessing(self, _installed_apps): + for source, exc in TEST_EXCEPTIONS: + result = guess_exception_source(exc) + self.assertEqual(result, source) diff --git a/xhiveframework/core/doctype/file/__init__.py b/xhiveframework/core/doctype/file/__init__.py new file mode 100644 index 0000000..ad28c17 --- /dev/null +++ b/xhiveframework/core/doctype/file/__init__.py @@ -0,0 +1,2 @@ +from .exceptions import * +from .utils import * diff --git a/xhiveframework/core/doctype/file/exceptions.py b/xhiveframework/core/doctype/file/exceptions.py new file mode 100644 index 0000000..f892bb4 --- /dev/null +++ b/xhiveframework/core/doctype/file/exceptions.py @@ -0,0 +1,16 @@ +import xhiveframework + + +class MaxFileSizeReachedError(xhiveframework.ValidationError): + pass + + +class FolderNotEmpty(xhiveframework.ValidationError): + pass + + +class FileTypeNotAllowed(xhiveframework.ValidationError): + pass + + +from xhiveframework.exceptions import * diff --git a/xhiveframework/core/doctype/file/file.js b/xhiveframework/core/doctype/file/file.js new file mode 100644 index 0000000..5fd3868 --- /dev/null +++ b/xhiveframework/core/doctype/file/file.js @@ -0,0 +1,106 @@ +xhiveframework.ui.form.on("File", { + refresh: function (frm) { + if (!frm.doc.is_folder) { + // add download button + frm.add_custom_button(__("Download"), () => frm.trigger("download"), "fa fa-download"); + } + + if (!frm.doc.is_private) { + frm.dashboard.set_headline( + __("This file is public. It can be accessed without authentication."), + "orange" + ); + } + + frm.toggle_display("preview", false); + + // preview different file types + frm.trigger("preview_file"); + + let is_raster_image = /\.(gif|jpg|jpeg|tiff|png)$/i.test(frm.doc.file_url); + let is_optimizable = !frm.doc.is_folder && is_raster_image && frm.doc.file_size > 0; + + // add optimize button + is_optimizable && frm.add_custom_button(__("Optimize"), () => frm.trigger("optimize")); + + // add unzip button + if (frm.doc.file_name && frm.doc.file_name.split(".").splice(-1)[0] === "zip") { + frm.add_custom_button(__("Unzip"), () => frm.trigger("unzip")); + } + if (frm.doc.file_url) { + frm.add_web_link(frm.doc.file_url, __("View file")); + } + }, + + preview_file: function (frm) { + let $preview = ""; + let file_extension = frm.doc.file_type.toLowerCase(); + + if (xhiveframework.utils.is_image_file(frm.doc.file_url)) { + $preview = $(`
    + +
    `); + } else if (xhiveframework.utils.is_video_file(frm.doc.file_url)) { + $preview = $(`
    + +
    `); + } else if (file_extension === "pdf") { + $preview = $(`
    + + + +
    `); + } else if (file_extension === "mp3") { + $preview = $(`
    + +
    `); + } + + if ($preview) { + frm.toggle_display("preview", true); + frm.get_field("preview_html").$wrapper.html($preview); + } + }, + + download: function (frm) { + let file_url = frm.doc.file_url; + if (frm.doc.file_name) { + file_url = file_url.replace(/#/g, "%23"); + } + window.open(file_url); + }, + + optimize: function (frm) { + xhiveframework.show_alert(__("Optimizing image...")); + frm.call("optimize_file").then(() => { + xhiveframework.show_alert(__("Image optimized")); + }); + }, + + unzip: function (frm) { + xhiveframework.call({ + method: "xhiveframework.core.api.file.unzip_file", + args: { + name: frm.doc.name, + }, + callback: function () { + xhiveframework.set_route("List", "File"); + }, + }); + }, +}); diff --git a/xhiveframework/core/doctype/file/file.json b/xhiveframework/core/doctype/file/file.json new file mode 100644 index 0000000..215178d --- /dev/null +++ b/xhiveframework/core/doctype/file/file.json @@ -0,0 +1,224 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2012-12-12 11:19:22", + "default_view": "File", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "file_name", + "is_private", + "column_break_7jmm", + "file_type", + "preview", + "preview_html", + "section_break_5", + "is_home_folder", + "is_attachments_folder", + "file_size", + "column_break_5", + "file_url", + "thumbnail_url", + "folder", + "is_folder", + "section_break_8", + "attached_to_doctype", + "column_break_10", + "attached_to_name", + "attached_to_field", + "old_parent", + "content_hash", + "uploaded_to_dropbox", + "uploaded_to_google_drive" + ], + "fields": [ + { + "fieldname": "file_name", + "fieldtype": "Data", + "in_global_search": 1, + "label": "File Name", + "oldfieldname": "file_name", + "oldfieldtype": "Data", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "eval:!doc.is_folder", + "fieldname": "is_private", + "fieldtype": "Check", + "label": "Is Private" + }, + { + "fieldname": "preview", + "fieldtype": "Section Break", + "label": "Preview" + }, + { + "fieldname": "preview_html", + "fieldtype": "HTML", + "label": "Preview HTML" + }, + { + "fieldname": "section_break_5", + "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "is_home_folder", + "fieldtype": "Check", + "hidden": 1, + "label": "Is Home Folder" + }, + { + "default": "0", + "fieldname": "is_attachments_folder", + "fieldtype": "Check", + "hidden": 1, + "label": "Is Attachments Folder" + }, + { + "fieldname": "file_size", + "fieldtype": "Int", + "in_list_view": 1, + "label": "File Size", + "length": 20, + "options": "File Size", + "read_only": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval:!doc.is_folder", + "fieldname": "file_url", + "fieldtype": "Code", + "label": "File URL", + "read_only": 1 + }, + { + "fieldname": "thumbnail_url", + "fieldtype": "Small Text", + "label": "Thumbnail URL", + "read_only": 1 + }, + { + "fieldname": "folder", + "fieldtype": "Link", + "hidden": 1, + "label": "Folder", + "options": "File", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "is_folder", + "fieldtype": "Check", + "label": "Is Folder", + "read_only": 1 + }, + { + "depends_on": "eval:!doc.is_folder", + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "attached_to_doctype", + "fieldtype": "Link", + "in_standard_filter": 1, + "label": "Attached To DocType", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "fieldname": "attached_to_name", + "fieldtype": "Data", + "label": "Attached To Name", + "read_only": 1 + }, + { + "fieldname": "attached_to_field", + "fieldtype": "Data", + "label": "Attached To Field", + "read_only": 1 + }, + { + "fieldname": "old_parent", + "fieldtype": "Data", + "hidden": 1, + "label": "old_parent" + }, + { + "fieldname": "content_hash", + "fieldtype": "Data", + "label": "Content Hash", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "uploaded_to_dropbox", + "fieldtype": "Check", + "label": "Uploaded To Dropbox", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "uploaded_to_google_drive", + "fieldtype": "Check", + "label": "Uploaded To Google Drive", + "read_only": 1 + }, + { + "fieldname": "column_break_7jmm", + "fieldtype": "Column Break" + }, + { + "fieldname": "file_type", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "File Type", + "read_only": 1 + } + ], + "force_re_route_to_default_view": 1, + "icon": "fa fa-file", + "idx": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Core", + "name": "File", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "read": 1, + "role": "All", + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "file_name", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/file/file.py b/xhiveframework/core/doctype/file/file.py new file mode 100755 index 0000000..c2b77b1 --- /dev/null +++ b/xhiveframework/core/doctype/file/file.py @@ -0,0 +1,835 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import io +import mimetypes +import os +import re +import shutil +import zipfile +from urllib.parse import quote, unquote + +from PIL import Image, ImageFile, ImageOps + +import xhiveframework +from xhiveframework import _ +from xhiveframework.database.schema import SPECIAL_CHAR_PATTERN +from xhiveframework.model.document import Document +from xhiveframework.permissions import get_doctypes_with_read +from xhiveframework.utils import call_hook_method, cint, get_files_path, get_hook_method, get_url +from xhiveframework.utils.file_manager import is_safe_path +from xhiveframework.utils.image import optimize_image, strip_exif_data + +from .exceptions import ( + AttachmentLimitReached, + FileTypeNotAllowed, + FolderNotEmpty, + MaxFileSizeReachedError, +) +from .utils import * + +exclude_from_linked_with = True +ImageFile.LOAD_TRUNCATED_IMAGES = True +URL_PREFIXES = ("http://", "https://") + + +class File(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + attached_to_doctype: DF.Link | None + attached_to_field: DF.Data | None + attached_to_name: DF.Data | None + content_hash: DF.Data | None + file_name: DF.Data | None + file_size: DF.Int + file_type: DF.Data | None + file_url: DF.Code | None + folder: DF.Link | None + is_attachments_folder: DF.Check + is_folder: DF.Check + is_home_folder: DF.Check + is_private: DF.Check + old_parent: DF.Data | None + thumbnail_url: DF.SmallText | None + uploaded_to_dropbox: DF.Check + uploaded_to_google_drive: DF.Check + # end: auto-generated types + no_feed_on_delete = True + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # if content is set, file_url will be generated + # decode comes in the picture if content passed has to be decoded before writing to disk + + self.content = self.get("content") or b"" + self.decode = self.get("decode", False) + + @property + def is_remote_file(self): + if self.file_url: + return self.file_url.startswith(URL_PREFIXES) + return not self.content + + def autoname(self): + """Set name for folder""" + if self.is_folder: + if self.folder: + self.name = self.get_name_based_on_parent_folder() + else: + # home + self.name = self.file_name + else: + self.name = xhiveframework.generate_hash(length=10) + + def before_insert(self): + self.set_folder_name() + self.set_file_name() + self.validate_attachment_limit() + self.set_file_type() + self.validate_file_extension() + + if self.is_folder: + return + + if self.is_remote_file: + self.validate_remote_file() + else: + self.save_file(content=self.get_content()) + self.flags.new_file = True + xhiveframework.db.after_rollback.add(self.on_rollback) + + def after_insert(self): + if not self.is_folder: + self.create_attachment_record() + self.set_is_private() + self.set_file_name() + self.validate_duplicate_entry() + + def validate(self): + if self.is_folder: + return + + # Ensure correct formatting and type + self.file_url = unquote(self.file_url) if self.file_url else "" + + self.validate_attachment_references() + + # when dict is passed to get_doc for creation of new_doc, is_new returns None + # this case is handled inside handle_is_private_changed + if not self.is_new() and self.has_value_changed("is_private"): + self.handle_is_private_changed() + + self.validate_file_path() + self.validate_file_url() + self.validate_file_on_disk() + + self.file_size = xhiveframework.form_dict.file_size or self.file_size + + def validate_attachment_references(self): + if not self.attached_to_doctype: + return + + if not self.attached_to_name or not isinstance(self.attached_to_name, str | int): + xhiveframework.throw(_("Attached To Name must be a string or an integer"), xhiveframework.ValidationError) + + if self.attached_to_field and SPECIAL_CHAR_PATTERN.search(self.attached_to_field): + xhiveframework.throw(_("The fieldname you've specified in Attached To Field is invalid")) + + def after_rename(self, *args, **kwargs): + for successor in self.get_successors(): + setup_folder_path(successor, self.name) + + def on_trash(self): + if self.is_home_folder or self.is_attachments_folder: + xhiveframework.throw(_("Cannot delete Home and Attachments folders")) + self.validate_empty_folder() + self._delete_file_on_disk() + if not self.is_folder: + self.add_comment_in_reference_doc("Attachment Removed", _("Removed {0}").format(self.file_name)) + + def on_rollback(self): + rollback_flags = ("new_file", "original_content", "original_path") + + def pop_rollback_flags(): + for flag in rollback_flags: + self.flags.pop(flag, None) + + # following condition is only executed when an insert has been rolledback + if self.flags.new_file: + self._delete_file_on_disk() + pop_rollback_flags() + return + + # if original_content flag is set, this rollback should revert the file to its original state + if self.flags.original_content: + file_path = self.get_full_path() + + if isinstance(self.flags.original_content, bytes): + mode = "wb+" + elif isinstance(self.flags.original_content, str): + mode = "w+" + + with open(file_path, mode) as f: + f.write(self.flags.original_content) + os.fsync(f.fileno()) + pop_rollback_flags() + + # used in case file path (File.file_url) has been changed + if self.flags.original_path: + target = self.flags.original_path["old"] + source = self.flags.original_path["new"] + shutil.move(source, target) + pop_rollback_flags() + + def get_name_based_on_parent_folder(self) -> str | None: + if self.folder: + return os.path.join(self.folder, self.file_name) + + def get_successors(self): + return xhiveframework.get_all("File", filters={"folder": self.name}, pluck="name") + + def validate_file_path(self): + if self.is_remote_file: + return + + base_path = os.path.realpath(get_files_path(is_private=self.is_private)) + if not os.path.realpath(self.get_full_path()).startswith(base_path): + xhiveframework.throw( + _("The File URL you've entered is incorrect"), + title=_("Invalid File URL"), + ) + + def validate_file_url(self): + if self.is_remote_file or not self.file_url: + return + + if not self.file_url.startswith(("/files/", "/private/files/")): + # Probably an invalid URL since it doesn't start with http either + xhiveframework.throw( + _("URL must start with http:// or https://"), + title=_("Invalid URL"), + ) + + def handle_is_private_changed(self): + if self.is_remote_file: + return + + from pathlib import Path + + old_file_url = self.file_url + file_name = self.file_url.split("/")[-1] + private_file_path = Path(xhiveframework.get_site_path("private", "files", file_name)) + public_file_path = Path(xhiveframework.get_site_path("public", "files", file_name)) + + if cint(self.is_private): + source = public_file_path + target = private_file_path + url_starts_with = "/private/files/" + else: + source = private_file_path + target = public_file_path + url_starts_with = "/files/" + updated_file_url = f"{url_starts_with}{file_name}" + + # if a file document is created by passing dict throught get_doc and __local is not set, + # handle_is_private_changed would be executed; we're checking if updated_file_url is same + # as old_file_url to avoid a FileNotFoundError for this case. + if updated_file_url == old_file_url: + return + + if not source.exists(): + xhiveframework.throw( + _("Cannot find file {} on disk").format(source), + exc=FileNotFoundError, + ) + if target.exists(): + xhiveframework.throw( + _("A file with same name {} already exists").format(target), + exc=FileExistsError, + ) + + # Uses os.rename which is an atomic operation + shutil.move(source, target) + self.flags.original_path = {"old": source, "new": target} + xhiveframework.db.after_rollback.add(self.on_rollback) + + self.file_url = updated_file_url + update_existing_file_docs(self) + + if ( + not self.attached_to_doctype + or not self.attached_to_name + or not self.fetch_attached_to_field(old_file_url) + ): + return + + if xhiveframework.get_meta(self.attached_to_doctype).issingle: + xhiveframework.db.set_single_value( + self.attached_to_doctype, + self.attached_to_field, + self.file_url, + ) + else: + xhiveframework.db.set_value( + self.attached_to_doctype, + self.attached_to_name, + self.attached_to_field, + self.file_url, + ) + + def fetch_attached_to_field(self, old_file_url): + if self.attached_to_field: + return True + + reference_dict = xhiveframework.get_doc(self.attached_to_doctype, self.attached_to_name).as_dict() + + for key, value in reference_dict.items(): + if value == old_file_url: + self.attached_to_field = key + return True + + def validate_attachment_limit(self): + attachment_limit = 0 + if self.attached_to_doctype and self.attached_to_name: + attachment_limit = cint(xhiveframework.get_meta(self.attached_to_doctype).max_attachments) + + if attachment_limit: + current_attachment_count = len( + xhiveframework.get_all( + "File", + filters={ + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_name, + }, + limit=attachment_limit + 1, + ) + ) + + if current_attachment_count >= attachment_limit: + xhiveframework.throw( + _("Maximum Attachment Limit of {0} has been reached for {1} {2}.").format( + xhiveframework.bold(attachment_limit), self.attached_to_doctype, self.attached_to_name + ), + exc=AttachmentLimitReached, + title=_("Attachment Limit Reached"), + ) + + def validate_remote_file(self): + """Validates if file uploaded using URL already exist""" + site_url = get_url() + if self.file_url and "/files/" in self.file_url and self.file_url.startswith(site_url): + self.file_url = self.file_url.split(site_url, 1)[1] + + def set_folder_name(self): + """Make parent folders if not exists based on reference doctype and name""" + if self.folder: + return + + if self.attached_to_doctype: + self.folder = xhiveframework.db.get_value("File", {"is_attachments_folder": 1}) + + elif not self.is_home_folder: + self.folder = "Home" + + def set_file_type(self): + if self.is_folder: + return + + file_type = mimetypes.guess_type(self.file_name)[0] + if not file_type: + return + + file_extension = mimetypes.guess_extension(file_type) + self.file_type = file_extension.lstrip(".").upper() if file_extension else None + + def validate_file_on_disk(self): + """Validates existence file""" + full_path = self.get_full_path() + + if full_path.startswith(URL_PREFIXES): + return True + + if not os.path.exists(full_path): + xhiveframework.throw(_("File {0} does not exist").format(self.file_url), IOError) + + def validate_file_extension(self): + # Only validate uploaded files, not generated by code/integrations. + if not self.file_type or not xhiveframework.request: + return + + allowed_extensions = xhiveframework.get_system_settings("allowed_file_extensions") + if not allowed_extensions: + return + + if self.file_type not in allowed_extensions.splitlines(): + xhiveframework.throw(_("File type of {0} is not allowed").format(self.file_type), exc=FileTypeNotAllowed) + + def validate_duplicate_entry(self): + if not self.flags.ignore_duplicate_entry_error and not self.is_folder: + if not self.content_hash: + self.generate_content_hash() + + # check duplicate name + # check duplicate assignment + filters = { + "content_hash": self.content_hash, + "is_private": self.is_private, + "name": ("!=", self.name), + } + if self.attached_to_doctype and self.attached_to_name: + filters.update( + { + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_name, + } + ) + duplicate_file = xhiveframework.db.get_value("File", filters, ["name", "file_url"], as_dict=1) + + if duplicate_file: + duplicate_file_doc = xhiveframework.get_cached_doc("File", duplicate_file.name) + if duplicate_file_doc.exists_on_disk(): + # just use the url, to avoid uploading a duplicate + self.file_url = duplicate_file.file_url + + def set_file_name(self): + if not self.file_name and not self.file_url: + xhiveframework.throw( + _("Fields `file_name` or `file_url` must be set for File"), exc=xhiveframework.MandatoryError + ) + elif not self.file_name and self.file_url: + self.file_name = self.file_url.split("/")[-1] + else: + self.file_name = re.sub(r"/", "", self.file_name) + + def generate_content_hash(self): + if self.content_hash or not self.file_url or self.is_remote_file: + return + file_name = self.file_url.split("/")[-1] + try: + file_path = get_files_path(file_name, is_private=self.is_private) + with open(file_path, "rb") as f: + self.content_hash = get_content_hash(f.read()) + except OSError: + xhiveframework.throw(_("File {0} does not exist").format(file_path)) + + def make_thumbnail( + self, + set_as_thumbnail: bool = True, + width: int = 300, + height: int = 300, + suffix: str = "small", + crop: bool = False, + ) -> str: + from requests.exceptions import HTTPError, SSLError + + if not self.file_url: + return + + try: + if self.file_url.startswith(("/files", "/private/files")): + image, filename, extn = get_local_image(self.file_url) + else: + image, filename, extn = get_web_image(self.file_url) + except (HTTPError, SSLError, OSError, TypeError): + return + + size = width, height + if crop: + image = ImageOps.fit(image, size, Image.Resampling.LANCZOS) + else: + image.thumbnail(size, Image.Resampling.LANCZOS) + + thumbnail_url = f"{filename}_{suffix}.{extn}" + path = os.path.abspath(xhiveframework.get_site_path("public", thumbnail_url.lstrip("/"))) + + try: + image.save(path) + if set_as_thumbnail: + self.db_set("thumbnail_url", thumbnail_url) + + except OSError: + xhiveframework.msgprint(_("Unable to write file format for {0}").format(path)) + return + + return thumbnail_url + + def validate_empty_folder(self): + """Throw exception if folder is not empty""" + if self.is_folder and xhiveframework.get_all("File", filters={"folder": self.name}, limit=1): + xhiveframework.throw(_("Folder {0} is not empty").format(self.name), FolderNotEmpty) + + def _delete_file_on_disk(self): + """If file not attached to any other record, delete it""" + on_disk_file_not_shared = self.content_hash and not xhiveframework.get_all( + "File", + filters={"content_hash": self.content_hash, "name": ["!=", self.name]}, + limit=1, + ) + if on_disk_file_not_shared: + self.delete_file_data_content() + else: + self.delete_file_data_content(only_thumbnail=True) + + def unzip(self) -> list["File"]: + """Unzip current file and replace it by its children""" + if not self.file_url.endswith(".zip"): + xhiveframework.throw(_("{0} is not a zip file").format(self.file_name)) + + zip_path = self.get_full_path() + + files = [] + with zipfile.ZipFile(zip_path) as z: + for file in z.filelist: + if file.is_dir() or file.filename.startswith("__MACOSX/"): + # skip directories and macos hidden directory + continue + + filename = os.path.basename(file.filename) + if filename.startswith("."): + # skip hidden files + continue + + file_doc = xhiveframework.new_doc("File") + try: + file_doc.content = z.read(file.filename) + except zipfile.BadZipFile: + xhiveframework.throw(_("{0} is a not a valid zip file").format(self.file_name)) + file_doc.file_name = filename + file_doc.folder = self.folder + file_doc.is_private = self.is_private + file_doc.attached_to_doctype = self.attached_to_doctype + file_doc.attached_to_name = self.attached_to_name + file_doc.save() + files.append(file_doc) + + xhiveframework.delete_doc("File", self.name) + return files + + def exists_on_disk(self): + return os.path.exists(self.get_full_path()) + + def get_content(self) -> bytes: + if self.is_folder: + xhiveframework.throw(_("Cannot get file contents of a Folder")) + + if self.get("content"): + self._content = self.content + if self.decode: + self._content = decode_file_content(self._content) + self.decode = False + # self.content = None # TODO: This needs to happen; make it happen somehow + return self._content + + if self.file_url: + self.validate_file_url() + file_path = self.get_full_path() + + # read the file + with open(file_path, mode="rb") as f: + self._content = f.read() + try: + # for plain text files + self._content = self._content.decode() + except UnicodeDecodeError: + # for .png, .jpg, etc + pass + + return self._content + + def get_full_path(self): + """Returns file path from given file name""" + + file_path = self.file_url or self.file_name + + site_url = get_url() + if "/files/" in file_path and file_path.startswith(site_url): + file_path = file_path.split(site_url, 1)[1] + + if "/" not in file_path: + if self.is_private: + file_path = f"/private/files/{file_path}" + else: + file_path = f"/files/{file_path}" + + if file_path.startswith("/private/files/"): + file_path = get_files_path(*file_path.split("/private/files/", 1)[1].split("/"), is_private=1) + + elif file_path.startswith("/files/"): + file_path = get_files_path(*file_path.split("/files/", 1)[1].split("/")) + + elif file_path.startswith(URL_PREFIXES): + pass + + elif not self.file_url: + xhiveframework.throw(_("There is some problem with the file url: {0}").format(file_path)) + + if not is_safe_path(file_path): + xhiveframework.throw(_("Cannot access file path {0}").format(file_path)) + + if os.path.sep in self.file_name: + xhiveframework.throw(_("File name cannot have {0}").format(os.path.sep)) + + return file_path + + def write_file(self): + """write file to disk with a random name (to compare)""" + if self.is_remote_file: + return + + file_path = self.get_full_path() + + if isinstance(self._content, str): + self._content = self._content.encode() + + with open(file_path, "wb+") as f: + f.write(self._content) + os.fsync(f.fileno()) + + xhiveframework.db.after_rollback.add(self.on_rollback) + + return file_path + + def save_file( + self, + content: bytes | str | None = None, + decode=False, + ignore_existing_file_check=False, + overwrite=False, + ): + if self.is_remote_file: + return + + if not self.flags.new_file: + self.flags.original_content = self.get_content() + + if content: + self.content = content + self.decode = decode + self.get_content() + + if not self._content: + return + + file_exists = False + duplicate_file = None + + self.is_private = cint(self.is_private) + self.content_type = mimetypes.guess_type(self.file_name)[0] + + # transform file content based on site settings + if ( + self.content_type + and self.content_type == "image/jpeg" + and xhiveframework.get_system_settings("strip_exif_metadata_from_uploaded_images") + ): + self._content = strip_exif_data(self._content, self.content_type) + + self.file_size = self.check_max_file_size() + self.content_hash = get_content_hash(self._content) + + # check if a file exists with the same content hash and is also in the same folder (public or private) + if not ignore_existing_file_check: + duplicate_file = xhiveframework.get_value( + "File", + {"content_hash": self.content_hash, "is_private": self.is_private}, + ["file_url", "name"], + as_dict=True, + ) + + if duplicate_file: + file_doc: "File" = xhiveframework.get_cached_doc("File", duplicate_file.name) + if file_doc.exists_on_disk(): + self.file_url = duplicate_file.file_url + file_exists = True + + if not file_exists: + if not overwrite: + self.file_name = generate_file_name( + name=self.file_name, + suffix=self.content_hash[-6:], + is_private=self.is_private, + ) + call_hook_method("before_write_file", file_size=self.file_size) + write_file_method = get_hook_method("write_file") + if write_file_method: + return write_file_method(self) + return self.save_file_on_filesystem() + + def save_file_on_filesystem(self): + if self.is_private: + self.file_url = f"/private/files/{self.file_name}" + else: + self.file_url = f"/files/{self.file_name}" + + fpath = self.write_file() + + return {"file_name": os.path.basename(fpath), "file_url": self.file_url} + + def check_max_file_size(self): + from xhiveframework.core.api.file import get_max_file_size + + max_file_size = get_max_file_size() + file_size = len(self._content or b"") + + if file_size > max_file_size: + msg = _("File size exceeded the maximum allowed size of {0} MB").format(max_file_size / 1048576) + if xhiveframework.has_permission("System Settings", "write"): + msg += ".
    " + _("You can increase the limit from System Settings.") + xhiveframework.throw(msg, exc=MaxFileSizeReachedError) + + return file_size + + def delete_file_data_content(self, only_thumbnail=False): + method = get_hook_method("delete_file_data_content") + if method: + method(self, only_thumbnail=only_thumbnail) + else: + self.delete_file_from_filesystem(only_thumbnail=only_thumbnail) + + def delete_file_from_filesystem(self, only_thumbnail=False): + """Delete file, thumbnail from File document""" + if only_thumbnail: + delete_file(self.thumbnail_url) + else: + delete_file(self.file_url) + delete_file(self.thumbnail_url) + + def is_downloadable(self): + return has_permission(self, "read") + + def get_extension(self): + """returns split filename and extension""" + return os.path.splitext(self.file_name) + + def create_attachment_record(self): + icon = ' ' if self.is_private else "" + file_url = quote(xhiveframework.safe_encode(self.file_url), safe="/:") if self.file_url else self.file_name + file_name = self.file_name or self.file_url + + self.add_comment_in_reference_doc( + "Attachment", + _("Added {0}").format(f"{file_name}{icon}"), + ) + + def add_comment_in_reference_doc(self, comment_type, text): + if self.attached_to_doctype and self.attached_to_name: + try: + doc = xhiveframework.get_doc(self.attached_to_doctype, self.attached_to_name) + doc.add_comment(comment_type, text) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_messages() + + def set_is_private(self): + if self.file_url: + self.is_private = cint(self.file_url.startswith("/private")) + + @xhiveframework.whitelist() + def optimize_file(self): + if self.is_folder: + raise TypeError("Folders cannot be optimized") + + content_type = mimetypes.guess_type(self.file_name)[0] + is_local_image = content_type.startswith("image/") and self.file_size > 0 + is_svg = content_type == "image/svg+xml" + + if not is_local_image: + raise NotImplementedError("Only local image files can be optimized") + + if is_svg: + raise TypeError("Optimization of SVG images is not supported") + + original_content = self.get_content() + optimized_content = optimize_image( + content=original_content, + content_type=content_type, + ) + + self.save_file(content=optimized_content, overwrite=True) + self.save() + + @property + def unique_url(self) -> str: + """Unique URL contains file ID in URL to speed up permisison checks.""" + from urllib.parse import urlencode + + if self.is_private: + return self.file_url + "?" + urlencode({"fid": self.name}) + else: + return self.file_url + + @staticmethod + def zip_files(files): + zip_file = io.BytesIO() + zf = zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) + for _file in files: + if isinstance(_file, str): + _file = xhiveframework.get_doc("File", _file) + if not isinstance(_file, File): + continue + if _file.is_folder: + continue + if not has_permission(_file, "read"): + continue + zf.writestr(_file.file_name, _file.get_content()) + zf.close() + return zip_file.getvalue() + + +def on_doctype_update(): + xhiveframework.db.add_index("File", ["attached_to_doctype", "attached_to_name"]) + + +def has_permission(doc, ptype=None, user=None, debug=False): + user = user or xhiveframework.session.user + + if user == "Administrator": + return True + + if ptype == "create": + return xhiveframework.has_permission("File", "create", user=user, debug=debug) + + if not doc.is_private and ptype in ("read", "select"): + return True + + if user != "Guest" and doc.owner == user: + return True + + if doc.attached_to_doctype and doc.attached_to_name: + attached_to_doctype = doc.attached_to_doctype + attached_to_name = doc.attached_to_name + + try: + ref_doc = xhiveframework.get_doc(attached_to_doctype, attached_to_name) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_last_message() + return False + + if ptype in ["write", "create", "delete"]: + return ref_doc.has_permission("write", debug=debug, user=user) + else: + return ref_doc.has_permission("read", debug=debug, user=user) + + return False + + +def get_permission_query_conditions(user: str | None = None) -> str: + user = user or xhiveframework.session.user + if user == "Administrator": + return "" + + readable_doctypes = ", ".join(repr(dt) for dt in get_doctypes_with_read()) + return f""" + (`tabFile`.`is_private` = 0) + OR (`tabFile`.`attached_to_doctype` IS NULL AND `tabFile`.`owner` = {user !r}) + OR (`tabFile`.`attached_to_doctype` IN ({readable_doctypes})) + """ + + +# Note: kept at the end to not cause circular, partial imports & maintain backwards compatibility +from xhiveframework.core.api.file import * diff --git a/xhiveframework/core/doctype/file/test_file.py b/xhiveframework/core/doctype/file/test_file.py new file mode 100644 index 0000000..dee1684 --- /dev/null +++ b/xhiveframework/core/doctype/file/test_file.py @@ -0,0 +1,878 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import base64 +import json +import os +import shutil +import tempfile +from contextlib import contextmanager +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.api.file import ( + create_new_folder, + get_attached_images, + get_files_in_folder, + move_file, + unzip_file, +) +from xhiveframework.core.doctype.file.exceptions import FileTypeNotAllowed +from xhiveframework.core.doctype.file.utils import delete_file, get_extension +from xhiveframework.exceptions import ValidationError +from xhiveframework.tests.utils import XhiveFrameworkTestCase, change_settings +from xhiveframework.utils import get_files_path, set_request + +if TYPE_CHECKING: + from xhiveframework.core.doctype.file.file import File + +test_content1 = "Hello" +test_content2 = "Hello World" + + +def make_test_doc(ignore_permissions=False): + d = xhiveframework.new_doc("ToDo") + d.description = "Test" + d.assigned_by = xhiveframework.session.user + d.save(ignore_permissions) + return d.doctype, d.name + + +@contextmanager +def make_test_image_file(private=False): + file_path = xhiveframework.get_app_path("xhiveframework", "tests/data/sample_image_for_optimization.jpg") + with open(file_path, "rb") as f: + file_content = f.read() + + test_file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "sample_image_for_optimization.jpg", + "content": file_content, + "is_private": private, + } + ).insert() + # remove those flags + _test_file: "File" = xhiveframework.get_doc("File", test_file.name) + + try: + yield _test_file + finally: + _test_file.delete() + + +class TestSimpleFile(XhiveFrameworkTestCase): + def setUp(self): + self.attached_to_doctype, self.attached_to_docname = make_test_doc() + self.test_content = test_content1 + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "test1.txt", + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_docname, + "content": self.test_content, + } + ) + _file.save() + self.saved_file_url = _file.file_url + + def test_save(self): + _file = xhiveframework.get_doc("File", {"file_url": self.saved_file_url}) + content = _file.get_content() + self.assertEqual(content, self.test_content) + + +class TestFSRollbacks(XhiveFrameworkTestCase): + def test_rollback_from_file_system(self): + file_name = content = xhiveframework.generate_hash() + file = xhiveframework.new_doc("File", file_name=file_name, content=content).insert() + self.assertTrue(file.exists_on_disk()) + + xhiveframework.db.rollback() + self.assertFalse(file.exists_on_disk()) + + +class TestExtensionValidations(XhiveFrameworkTestCase): + @change_settings("System Settings", {"allowed_file_extensions": "JPG\nCSV"}) + def test_allowed_extension(self): + set_request(method="POST", path="/") + file_name = content = xhiveframework.generate_hash() + bad_file = xhiveframework.new_doc("File", file_name=f"{file_name}.png", content=content) + self.assertRaises(FileTypeNotAllowed, bad_file.insert) + + bad_file = xhiveframework.new_doc("File", file_name=f"{file_name}.csv", content=content).insert() + xhiveframework.db.rollback() + self.assertFalse(bad_file.exists_on_disk()) + + +class TestBase64File(XhiveFrameworkTestCase): + def setUp(self): + self.attached_to_doctype, self.attached_to_docname = make_test_doc() + self.test_content = base64.b64encode(test_content1.encode("utf-8")) + _file: "File" = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "test_base64.txt", + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_docname, + "content": self.test_content, + "decode": True, + } + ) + _file.save() + self.saved_file_url = _file.file_url + + def test_saved_content(self): + _file = xhiveframework.get_doc("File", {"file_url": self.saved_file_url}) + content = _file.get_content() + self.assertEqual(content, test_content1) + + +class TestSameFileName(XhiveFrameworkTestCase): + def test_saved_content(self): + self.attached_to_doctype, self.attached_to_docname = make_test_doc() + self.test_content1 = test_content1 + self.test_content2 = test_content2 + _file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "testing.txt", + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_docname, + "content": self.test_content1, + } + ) + _file1.save() + _file2 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "testing.txt", + "attached_to_doctype": self.attached_to_doctype, + "attached_to_name": self.attached_to_docname, + "content": self.test_content2, + } + ) + _file2.save() + self.saved_file_url1 = _file1.file_url + self.saved_file_url2 = _file2.file_url + + _file = xhiveframework.get_doc("File", {"file_url": self.saved_file_url1}) + content1 = _file.get_content() + self.assertEqual(content1, self.test_content1) + _file = xhiveframework.get_doc("File", {"file_url": self.saved_file_url2}) + content2 = _file.get_content() + self.assertEqual(content2, self.test_content2) + + def test_saved_content_private(self): + _file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "testing-private.txt", + "content": test_content1, + "is_private": 1, + } + ).insert() + _file2 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "testing-private.txt", + "content": test_content2, + "is_private": 1, + } + ).insert() + + _file = xhiveframework.get_doc("File", {"file_url": _file1.file_url}) + self.assertEqual(_file.get_content(), test_content1) + + _file = xhiveframework.get_doc("File", {"file_url": _file2.file_url}) + self.assertEqual(_file.get_content(), test_content2) + + +class TestSameContent(XhiveFrameworkTestCase): + def setUp(self): + self.attached_to_doctype1, self.attached_to_docname1 = make_test_doc() + self.attached_to_doctype2, self.attached_to_docname2 = make_test_doc() + self.test_content1 = test_content1 + self.test_content2 = test_content1 + self.orig_filename = "hello.txt" + self.dup_filename = "hello2.txt" + _file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": self.orig_filename, + "attached_to_doctype": self.attached_to_doctype1, + "attached_to_name": self.attached_to_docname1, + "content": self.test_content1, + } + ) + _file1.save() + + _file2 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": self.dup_filename, + "attached_to_doctype": self.attached_to_doctype2, + "attached_to_name": self.attached_to_docname2, + "content": self.test_content2, + } + ) + + _file2.save() + + def test_saved_content(self): + self.assertFalse(os.path.exists(get_files_path(self.dup_filename))) + + def test_attachment_limit(self): + doctype, docname = make_test_doc() + from xhiveframework.custom.doctype.property_setter.property_setter import make_property_setter + + limit_property = make_property_setter("ToDo", None, "max_attachments", 1, "int", for_doctype=True) + file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "test-attachment", + "attached_to_doctype": doctype, + "attached_to_name": docname, + "content": "test", + } + ) + + file1.insert() + + file2 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "test-attachment", + "attached_to_doctype": doctype, + "attached_to_name": docname, + "content": "test2", + } + ) + + self.assertRaises(xhiveframework.exceptions.AttachmentLimitReached, file2.insert) + limit_property.delete() + xhiveframework.clear_cache(doctype="ToDo") + + +class TestFile(XhiveFrameworkTestCase): + def setUp(self): + xhiveframework.set_user("Administrator") + self.delete_test_data() + self.upload_file() + + def tearDown(self): + try: + xhiveframework.get_doc("File", {"file_name": "file_copy.txt"}).delete() + except xhiveframework.DoesNotExistError: + pass + + def delete_test_data(self): + test_file_data = xhiveframework.get_all( + "File", + pluck="name", + filters={"is_home_folder": 0, "is_attachments_folder": 0}, + order_by="creation desc", + ) + for f in test_file_data: + xhiveframework.delete_doc("File", f) + + def upload_file(self): + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "file_copy.txt", + "attached_to_name": "", + "attached_to_doctype": "", + "folder": self.get_folder("Test Folder 1", "Home").name, + "content": "Testing file copy example.", + } + ) + _file.save() + self.saved_folder = _file.folder + self.saved_name = _file.name + self.saved_filename = get_files_path(_file.file_name) + + def get_folder(self, folder_name, parent_folder="Home"): + return xhiveframework.get_doc( + {"doctype": "File", "file_name": _(folder_name), "is_folder": 1, "folder": _(parent_folder)} + ).insert() + + def tests_after_upload(self): + self.assertEqual(self.saved_folder, _("Home/Test Folder 1")) + file_folder = xhiveframework.db.get_value("File", self.saved_name, "folder") + self.assertEqual(file_folder, _("Home/Test Folder 1")) + + def test_file_copy(self): + folder = self.get_folder("Test Folder 2", "Home") + + file = xhiveframework.get_doc("File", {"file_name": "file_copy.txt"}) + move_file([{"name": file.name}], folder.name, file.folder) + file = xhiveframework.get_doc("File", {"file_name": "file_copy.txt"}) + + self.assertEqual(_("Home/Test Folder 2"), file.folder) + + def test_folder_depth(self): + result1 = self.get_folder("d1", "Home") + self.assertEqual(result1.name, "Home/d1") + result2 = self.get_folder("d2", "Home/d1") + self.assertEqual(result2.name, "Home/d1/d2") + result3 = self.get_folder("d3", "Home/d1/d2") + self.assertEqual(result3.name, "Home/d1/d2/d3") + result4 = self.get_folder("d4", "Home/d1/d2/d3") + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "folder_copy.txt", + "attached_to_name": "", + "attached_to_doctype": "", + "folder": result4.name, + "content": "Testing folder copy example", + } + ) + _file.save() + + def test_folder_copy(self): + folder = self.get_folder("Test Folder 2", "Home") + folder = self.get_folder("Test Folder 3", "Home/Test Folder 2") + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "folder_copy.txt", + "attached_to_name": "", + "attached_to_doctype": "", + "folder": folder.name, + "content": "Testing folder copy example", + } + ) + _file.save() + + move_file([{"name": folder.name}], "Home/Test Folder 1", folder.folder) + + file = xhiveframework.get_doc("File", {"file_name": "folder_copy.txt"}) + file_copy_txt = xhiveframework.get_value("File", {"file_name": "file_copy.txt"}) + if file_copy_txt: + xhiveframework.get_doc("File", file_copy_txt).delete() + + self.assertEqual(_("Home/Test Folder 1/Test Folder 3"), file.folder) + + def test_default_folder(self): + d = xhiveframework.get_doc({"doctype": "File", "file_name": _("Test_Folder"), "is_folder": 1}) + d.save() + self.assertEqual(d.folder, "Home") + + def test_on_delete(self): + file = xhiveframework.get_doc("File", {"file_name": "file_copy.txt"}) + file.delete() + + self.assertEqual(xhiveframework.db.get_value("File", _("Home/Test Folder 1"), "file_size"), 0) + + folder = self.get_folder("Test Folder 3", "Home/Test Folder 1") + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "folder_copy.txt", + "attached_to_name": "", + "attached_to_doctype": "", + "folder": folder.name, + "content": "Testing folder copy example", + } + ) + _file.save() + + folder = xhiveframework.get_doc("File", "Home/Test Folder 1/Test Folder 3") + self.assertRaises(ValidationError, folder.delete) + + def test_same_file_url_update(self): + attached_to_doctype1, attached_to_docname1 = make_test_doc() + attached_to_doctype2, attached_to_docname2 = make_test_doc() + + file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "file1.txt", + "attached_to_doctype": attached_to_doctype1, + "attached_to_name": attached_to_docname1, + "is_private": 1, + "content": test_content1, + } + ).insert() + + file2 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "file2.txt", + "attached_to_doctype": attached_to_doctype2, + "attached_to_name": attached_to_docname2, + "is_private": 1, + "content": test_content1, + } + ).insert() + + self.assertEqual(file1.is_private, file2.is_private, 1) + self.assertEqual(file1.file_url, file2.file_url) + self.assertTrue(os.path.exists(file1.get_full_path())) + + file1.is_private = 0 + file1.save() + + file2 = xhiveframework.get_doc("File", file2.name) + + self.assertEqual(file1.is_private, file2.is_private, 0) + self.assertEqual(file1.file_url, file2.file_url) + self.assertTrue(os.path.exists(file2.get_full_path())) + + def test_parent_directory_validation_in_file_url(self): + file1 = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "parent_dir.txt", + "is_private": 1, + "content": test_content1, + } + ).insert() + + file1.file_url = "/private/files/../test.txt" + self.assertRaises(ValidationError, file1.save) + + # No validation to see if file exists + file1.reload() + file1.file_url = "/private/files/parent_dir2.txt" + self.assertRaises(OSError, file1.save) + + def test_file_url_validation(self): + test_file: "File" = xhiveframework.new_doc("File") + test_file.update({"file_name": "logo", "file_url": "https://xhiveframework.io/files/xhiveframework.png"}) + + self.assertIsNone(test_file.validate()) + + # bad path + test_file.file_url = "/usr/bin/man" + self.assertRaisesRegex( + ValidationError, f"Cannot access file path {test_file.file_url}", test_file.validate + ) + + test_file.file_url = None + test_file.file_name = "/usr/bin/man" + self.assertRaisesRegex(ValidationError, "There is some problem with the file url", test_file.validate) + + test_file.file_url = None + test_file.file_name = "_file" + self.assertRaisesRegex(IOError, "does not exist", test_file.validate) + + test_file.file_url = None + test_file.file_name = "/private/files/_file" + self.assertRaisesRegex(ValidationError, "File name cannot have", test_file.validate) + + def test_make_thumbnail(self): + # test web image + test_file: "File" = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "logo", + "file_url": xhiveframework.utils.get_url("/_test/assets/image.jpg"), + } + ).insert(ignore_permissions=True) + + test_file.make_thumbnail() + self.assertEqual(test_file.thumbnail_url, "/files/image_small.jpg") + + # test web image without extension + test_file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "logo", + "file_url": xhiveframework.utils.get_url("/_test/assets/image"), + } + ).insert(ignore_permissions=True) + + test_file.make_thumbnail() + self.assertTrue(test_file.thumbnail_url.endswith("_small.jpg")) + + # test local image + test_file.db_set("thumbnail_url", None) + test_file.reload() + test_file.file_url = "/files/image_small.jpg" + test_file.make_thumbnail(suffix="xs", crop=True) + self.assertEqual(test_file.thumbnail_url, "/files/image_small_xs.jpg") + + xhiveframework.clear_messages() + test_file.db_set("thumbnail_url", None) + test_file.reload() + test_file.file_url = xhiveframework.utils.get_url("unknown.jpg") + test_file.make_thumbnail(suffix="xs") + self.assertEqual( + xhiveframework.message_log[0].get("message"), + f"File '{xhiveframework.utils.get_url('unknown.jpg')}' not found", + ) + self.assertEqual(test_file.thumbnail_url, None) + + def test_file_unzip(self): + file_path = xhiveframework.get_app_path("xhiveframework", "www/_test/assets/file.zip") + public_file_path = xhiveframework.get_site_path("public", "files") + try: + import shutil + + shutil.copy(file_path, public_file_path) + except Exception: + pass + + test_file = xhiveframework.get_doc( + { + "doctype": "File", + "file_url": "/files/file.zip", + } + ).insert(ignore_permissions=True) + + self.assertListEqual( + [file.file_name for file in unzip_file(test_file.name)], + ["css_asset.css", "image.jpg", "js_asset.min.js"], + ) + + test_file = xhiveframework.get_doc( + { + "doctype": "File", + "file_url": xhiveframework.utils.get_url("/_test/assets/image.jpg"), + } + ).insert(ignore_permissions=True) + self.assertRaisesRegex(ValidationError, "not a zip file", test_file.unzip) + + def test_create_file_without_file_url(self): + test_file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "logo", + "content": "xhiveframework", + } + ).insert() + assert test_file is not None + + def test_symlinked_files_folder(self): + files_dir = os.path.abspath(get_files_path()) + with convert_to_symlink(files_dir): + file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": "symlinked_folder_test.txt", + "content": "42", + } + ) + file.save() + file.content = "" + file._content = "" + file.save().reload() + self.assertIn("42", file.get_content()) + + +@contextmanager +def convert_to_symlink(directory): + """Moves a directory to temp directory and symlinks original path for testing""" + try: + new_directory = shutil.move(directory, tempfile.mkdtemp()) + os.symlink(new_directory, directory) + yield + finally: + os.unlink(directory) + shutil.move(new_directory, directory) + + +class TestAttachment(XhiveFrameworkTestCase): + test_doctype = "Test For Attachment" + + @classmethod + def setUpClass(cls): + super().setUpClass() + xhiveframework.get_doc( + doctype="DocType", + name=cls.test_doctype, + module="Custom", + custom=1, + fields=[ + {"label": "Title", "fieldname": "title", "fieldtype": "Data"}, + {"label": "Attachment", "fieldname": "attachment", "fieldtype": "Attach"}, + ], + ).insert(ignore_if_duplicate=True) + + @classmethod + def tearDownClass(cls): + xhiveframework.db.rollback() + xhiveframework.delete_doc("DocType", cls.test_doctype) + + def test_file_attachment_on_update(self): + doc = xhiveframework.get_doc(doctype=self.test_doctype, title="test for attachment on update").insert() + + file = xhiveframework.get_doc( + {"doctype": "File", "file_name": "test_attach.txt", "content": "Test Content"} + ).save() + + doc.attachment = file.file_url + doc.save() + + exists = xhiveframework.db.exists( + "File", + { + "file_name": "test_attach.txt", + "file_url": file.file_url, + "attached_to_doctype": self.test_doctype, + "attached_to_name": doc.name, + "attached_to_field": "attachment", + }, + ) + + self.assertTrue(exists) + + +class TestAttachmentsAccess(XhiveFrameworkTestCase): + def setUp(self) -> None: + xhiveframework.db.delete("File", {"is_folder": 0}) + + def test_list_private_attachments(self): + xhiveframework.set_user("test4@example.com") + self.attached_to_doctype, self.attached_to_docname = make_test_doc() + + xhiveframework.new_doc( + "File", + file_name="test_user_attachment.txt", + attached_to_doctype=self.attached_to_doctype, + attached_to_name=self.attached_to_docname, + content="Testing User", + is_private=1, + ).insert() + + xhiveframework.new_doc( + "File", + file_name="test_user_standalone.txt", + content="User Home", + is_private=1, + ).insert() + + xhiveframework.set_user("test@example.com") + + xhiveframework.new_doc( + "File", + file_name="test_sm_attachment.txt", + attached_to_doctype=self.attached_to_doctype, + attached_to_name=self.attached_to_docname, + content="Testing System Manager", + is_private=1, + ).insert() + + xhiveframework.new_doc( + "File", + file_name="test_sm_standalone.txt", + content="System Manager Home", + is_private=1, + ).insert() + + system_manager_files = [file.file_name for file in get_files_in_folder("Home")["files"]] + system_manager_attachments_files = [ + file.file_name for file in get_files_in_folder("Home/Attachments")["files"] + ] + + xhiveframework.set_user("test4@example.com") + user_files = [file.file_name for file in get_files_in_folder("Home")["files"]] + user_attachments_files = [file.file_name for file in get_files_in_folder("Home/Attachments")["files"]] + + self.assertIn("test_sm_standalone.txt", system_manager_files) + self.assertNotIn("test_sm_standalone.txt", user_files) + + self.assertIn("test_user_standalone.txt", user_files) + self.assertNotIn("test_user_standalone.txt", system_manager_files) + + self.assertIn("test_sm_attachment.txt", system_manager_attachments_files) + self.assertIn("test_sm_attachment.txt", user_attachments_files) + self.assertIn("test_user_attachment.txt", system_manager_attachments_files) + self.assertIn("test_user_attachment.txt", user_attachments_files) + + def test_list_public_single_file(self): + """Ensure that users are able to list public standalone files.""" + xhiveframework.set_user("test@example.com") + xhiveframework.new_doc( + "File", + file_name="test_public_single.txt", + content="Public single File", + is_private=0, + ).insert() + + xhiveframework.set_user("test4@example.com") + files = [file.file_name for file in get_files_in_folder("Home")["files"]] + self.assertIn("test_public_single.txt", files) + + def test_list_public_attachment(self): + """Ensure that users are able to list public attachments.""" + xhiveframework.set_user("test@example.com") + self.attached_to_doctype, self.attached_to_docname = make_test_doc() + xhiveframework.new_doc( + "File", + file_name="test_public_attachment.txt", + attached_to_doctype=self.attached_to_doctype, + attached_to_name=self.attached_to_docname, + content="Public Attachment", + is_private=0, + ).insert() + + xhiveframework.set_user("test4@example.com") + files = [file.file_name for file in get_files_in_folder("Home/Attachments")["files"]] + self.assertIn("test_public_attachment.txt", files) + + def tearDown(self) -> None: + xhiveframework.set_user("Administrator") + xhiveframework.db.rollback() + + +class TestFileUtils(XhiveFrameworkTestCase): + def test_extract_images_from_doc(self): + # with filename in data URI + todo = xhiveframework.get_doc( + { + "doctype": "ToDo", + "description": 'Test ', + } + ).insert() + self.assertTrue(xhiveframework.db.exists("File", {"attached_to_name": todo.name})) + self.assertIn('', todo.description) + self.assertListEqual(get_attached_images("ToDo", [todo.name])[todo.name], ["/files/pix.png"]) + + # without filename in data URI + todo = xhiveframework.get_doc( + { + "doctype": "ToDo", + "description": 'Test ', + } + ).insert() + filename = xhiveframework.db.exists("File", {"attached_to_name": todo.name}) + self.assertIn(f' None: + xhiveframework.db.delete("File", {"is_folder": 0}) + xhiveframework.get_doc( + doctype="DocType", + name="Test For Attachment", + module="Custom", + custom=1, + fields=[ + {"label": "Title", "fieldname": "title", "fieldtype": "Data"}, + {"label": "Attachment", "fieldname": "attachment", "fieldtype": "Attach"}, + ], + ).insert(ignore_if_duplicate=True) + + def tearDown(self) -> None: + xhiveframework.set_user("Administrator") + xhiveframework.db.rollback() + xhiveframework.delete_doc("DocType", "Test For Attachment") + + def test_attach_unattached_guest_file(self): + """Ensure that unattached files are attached on doc update.""" + f = xhiveframework.new_doc( + "File", + file_name="test_private_guest_attachment.txt", + content="Guest Home", + is_private=1, + ).insert(ignore_permissions=True) + + d = xhiveframework.new_doc("Test For Attachment") + d.title = "Test for attachment on update" + d.attachment = f.file_url + d.assigned_by = xhiveframework.session.user + d.save() + + self.assertTrue( + xhiveframework.db.exists( + "File", + { + "file_name": "test_private_guest_attachment.txt", + "file_url": f.file_url, + "attached_to_doctype": "Test For Attachment", + "attached_to_name": d.name, + "attached_to_field": "attachment", + }, + ) + ) + + def test_list_private_guest_single_file(self): + """Ensure that guests are not able to read private standalone guest files.""" + xhiveframework.set_user("Guest") + + file = xhiveframework.new_doc( + "File", + file_name="test_private_guest_single_txt", + content="Private single File", + is_private=1, + ).insert(ignore_permissions=True) + + self.assertFalse(file.is_downloadable()) + + def test_list_private_guest_attachment(self): + """Ensure that guests are not able to read private guest attachments.""" + xhiveframework.set_user("Guest") + + self.attached_to_doctype, self.attached_to_docname = make_test_doc(ignore_permissions=True) + + file = xhiveframework.new_doc( + "File", + file_name="test_private_guest_attachment.txt", + attached_to_doctype=self.attached_to_doctype, + attached_to_name=self.attached_to_docname, + content="Private Attachment", + is_private=1, + ).insert(ignore_permissions=True) + + self.assertFalse(file.is_downloadable()) diff --git a/xhiveframework/core/doctype/file/utils.py b/xhiveframework/core/doctype/file/utils.py new file mode 100644 index 0000000..7068ef9 --- /dev/null +++ b/xhiveframework/core/doctype/file/utils.py @@ -0,0 +1,429 @@ +import hashlib +import mimetypes +import os +import re +from io import BytesIO +from typing import TYPE_CHECKING, Optional +from urllib.parse import unquote + +import filetype + +import xhiveframework +from xhiveframework import _, safe_decode +from xhiveframework.utils import cint, cstr, encode, get_files_path, random_string, strip +from xhiveframework.utils.file_manager import safe_b64decode +from xhiveframework.utils.image import optimize_image + +if TYPE_CHECKING: + from PIL.ImageFile import ImageFile + from requests.models import Response + + from xhiveframework.model.document import Document + + from .file import File + + +def make_home_folder() -> None: + home = xhiveframework.get_doc( + {"doctype": "File", "is_folder": 1, "is_home_folder": 1, "file_name": _("Home")} + ).insert(ignore_if_duplicate=True) + + xhiveframework.get_doc( + { + "doctype": "File", + "folder": home.name, + "is_folder": 1, + "is_attachments_folder": 1, + "file_name": _("Attachments"), + } + ).insert(ignore_if_duplicate=True) + + +def setup_folder_path(filename: str, new_parent: str) -> None: + file: "File" = xhiveframework.get_doc("File", filename) + file.folder = new_parent + file.save() + + if file.is_folder: + from xhiveframework.model.rename_doc import rename_doc + + rename_doc("File", file.name, file.get_name_based_on_parent_folder(), ignore_permissions=True) + + +def get_extension( + filename, + extn: str | None = None, + content: bytes | None = None, + response: Optional["Response"] = None, +) -> str: + mimetype = None + + if response: + content_type = response.headers.get("Content-Type") + + if content_type: + _extn = mimetypes.guess_extension(content_type) + if _extn: + return _extn[1:] + + if extn: + # remove '?' char and parameters from extn if present + if "?" in extn: + extn = extn.split("?", 1)[0] + + mimetype = mimetypes.guess_type(filename + "." + extn)[0] + + if mimetype is None and extn is None and content: + # detect file extension by using filetype matchers + _type_info = filetype.match(content) + if _type_info: + extn = _type_info.extension + + return extn + + +def get_local_image(file_url: str) -> tuple["ImageFile", str, str]: + from PIL import Image + + if file_url.startswith("/private"): + file_url_path = (file_url.lstrip("/"),) + else: + file_url_path = ("public", file_url.lstrip("/")) + + file_path = xhiveframework.get_site_path(*file_url_path) + + try: + image = Image.open(file_path) + except OSError: + xhiveframework.throw(_("Unable to read file format for {0}").format(file_url)) + + content = None + + try: + filename, extn = file_url.rsplit(".", 1) + except ValueError: + # no extn + with open(file_path) as f: + content = f.read() + + filename = file_url + extn = None + + extn = get_extension(filename, extn, content) + + return image, filename, extn + + +def get_web_image(file_url: str) -> tuple["ImageFile", str, str]: + import requests + import requests.exceptions + from PIL import Image + + file_url = xhiveframework.utils.get_url(file_url) + r = requests.get(file_url, stream=True) + try: + r.raise_for_status() + except requests.exceptions.HTTPError as e: + if "404" in e.args[0]: + xhiveframework.msgprint(_("File '{0}' not found").format(file_url)) + else: + xhiveframework.msgprint(_("Unable to read file format for {0}").format(file_url)) + raise + + try: + image = Image.open(BytesIO(r.content)) + except Exception as e: + xhiveframework.msgprint(_("Image link '{0}' is not valid").format(file_url), raise_exception=e) + + try: + filename, extn = file_url.rsplit("/", 1)[1].rsplit(".", 1) + except ValueError: + # the case when the file url doesn't have filename or extension + # but is fetched due to a query string. example: https://encrypted-tbn3.gstatic.com/images?q=something + filename = get_random_filename() + extn = None + + extn = get_extension(filename, extn, r.content) + filename = "/files/" + strip(unquote(filename)) + + return image, filename, extn + + +def delete_file(path: str) -> None: + """Delete file from `public folder`""" + if path: + if ".." in path.split("/"): + xhiveframework.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": + path = xhiveframework.utils.get_site_path("public", "files", parts[-1]) + + else: + path = xhiveframework.utils.get_site_path("private", "files", parts[-1]) + + path = encode(path) + if os.path.exists(path): + os.remove(path) + + +def remove_file_by_url(file_url: str, doctype: str | None = None, name: str | None = None) -> "Document": + if doctype and name: + fid = xhiveframework.db.get_value( + "File", {"file_url": file_url, "attached_to_doctype": doctype, "attached_to_name": name} + ) + else: + fid = xhiveframework.db.get_value("File", {"file_url": file_url}) + + if fid: + from xhiveframework.utils.file_manager import remove_file + + return remove_file(fid=fid) + + +def get_content_hash(content: bytes | str) -> str: + if isinstance(content, str): + content = content.encode() + return hashlib.md5(content, usedforsecurity=False).hexdigest() # nosec + + +def generate_file_name(name: str, suffix: str | None = None, is_private: bool = False) -> str: + """Generate conflict-free file name. Suffix will be ignored if name available. If the + provided suffix doesn't result in an available path, a random suffix will be picked. + """ + + def path_exists(name, is_private): + return os.path.exists(encode(get_files_path(name, is_private=is_private))) + + if not path_exists(name, is_private): + return name + + candidate_path = get_file_name(name, suffix) + + if path_exists(candidate_path, is_private): + return generate_file_name(name, is_private=is_private) + return candidate_path + + +def get_file_name(fname: str, optional_suffix: str | None = None) -> str: + # convert to unicode + fname = cstr(fname) + partial, extn = os.path.splitext(fname) + suffix = optional_suffix or xhiveframework.generate_hash(length=6) + + return f"{partial}{suffix}{extn}" + + +def extract_images_from_doc(doc: "Document", fieldname: str): + content = doc.get(fieldname) + content = extract_images_from_html(doc, content) + if xhiveframework.flags.has_dataurl: + doc.set(fieldname, content) + + +def extract_images_from_html(doc: "Document", content: str, is_private: bool = False): + xhiveframework.flags.has_dataurl = False + + def _save_file(match): + data = match.group(1).split("data:")[1] + headers, content = data.split(",") + mtype = headers.split(";", 1)[0] + + if isinstance(content, str): + content = content.encode("utf-8") + if b"," in content: + content = content.split(b",")[1] + content = safe_b64decode(content) + + content = optimize_image(content, mtype) + + if "filename=" in headers: + filename = headers.split("filename=")[-1] + filename = safe_decode(filename).split(";", 1)[0] + + else: + filename = get_random_filename(content_type=mtype) + + if doc.meta.istable: + doctype = doc.parenttype + name = doc.parent + else: + doctype = doc.doctype + name = doc.name + + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": filename, + "attached_to_doctype": doctype, + "attached_to_name": name, + "content": content, + "decode": False, + "is_private": is_private, + } + ) + _file.save(ignore_permissions=True) + file_url = _file.unique_url + xhiveframework.flags.has_dataurl = True + + return f']*src\s*=\s*["\'](?=data:)(.*?)["\']', _save_file, content) + + return content + + +def get_random_filename(content_type: str | None = None) -> str: + extn = None + if content_type: + extn = mimetypes.guess_extension(content_type) + + return random_string(7) + (extn or "") + + +def update_existing_file_docs(doc: "File") -> None: + # Update is private and file url of all file docs that point to the same file + file_doctype = xhiveframework.qb.DocType("File") + ( + xhiveframework.qb.update(file_doctype) + .set(file_doctype.file_url, doc.file_url) + .set(file_doctype.is_private, doc.is_private) + .where(file_doctype.content_hash == doc.content_hash) + .where(file_doctype.name != doc.name) + ).run() + + +def attach_files_to_document(doc: "Document", event) -> None: + """Runs on on_update hook of all documents. + Goes through every file linked with the Attach and Attach Image field and attaches + the file to the document if not already attached. If no file is found, a new file + is created. + """ + + attach_fields = doc.meta.get("fields", {"fieldtype": ["in", ["Attach", "Attach Image"]]}) + + for df in attach_fields: + # this method runs in on_update hook of all documents + # we dont want the update to fail if file cannot be attached for some reason + value = doc.get(df.fieldname) + if not (value or "").startswith(("/files", "/private/files")): + continue + + if xhiveframework.db.exists( + "File", + { + "file_url": value, + "attached_to_name": doc.name, + "attached_to_doctype": doc.doctype, + "attached_to_field": df.fieldname, + }, + ): + continue + + unattached_file = xhiveframework.db.exists( + "File", + { + "file_url": value, + "attached_to_name": None, + "attached_to_doctype": None, + "attached_to_field": None, + }, + ) + + if unattached_file: + xhiveframework.db.set_value( + "File", + unattached_file, + field={ + "attached_to_name": doc.name, + "attached_to_doctype": doc.doctype, + "attached_to_field": df.fieldname, + "is_private": cint(value.startswith("/private")), + }, + ) + continue + + file: "File" = xhiveframework.get_doc( + doctype="File", + file_url=value, + attached_to_name=doc.name, + attached_to_doctype=doc.doctype, + attached_to_field=df.fieldname, + folder="Home/Attachments", + ) + try: + file.insert(ignore_permissions=True) + except Exception: + doc.log_error("Error Attaching File") + + +def relink_files(doc, fieldname, temp_doc_name): + if not temp_doc_name: + return + from xhiveframework.utils.data import add_to_date, now_datetime + + """ + Relink files attached to incorrect document name to the new document name + by check if file with temp name exists that was created in last 60 minutes + """ + mislinked_file = xhiveframework.db.exists( + "File", + { + "file_url": doc.get(fieldname), + "attached_to_name": temp_doc_name, + "attached_to_doctype": doc.doctype, + "attached_to_field": fieldname, + "creation": ( + "between", + [add_to_date(date=now_datetime(), minutes=-60), now_datetime()], + ), + }, + ) + """If file exists, attach it to the new docname""" + if mislinked_file: + xhiveframework.db.set_value( + "File", + mislinked_file, + field={ + "attached_to_name": doc.name, + }, + ) + return + + +def relink_mismatched_files(doc: "Document") -> None: + if not doc.get("__temporary_name", None): + return + attach_fields = doc.meta.get("fields", {"fieldtype": ["in", ["Attach", "Attach Image"]]}) + for df in attach_fields: + if doc.get(df.fieldname): + relink_files(doc, df.fieldname, doc.__temporary_name) + # delete temporary name after relinking is done + doc.delete_key("__temporary_name") + + +def decode_file_content(content: bytes) -> bytes: + if isinstance(content, str): + content = content.encode("utf-8") + if b"," in content: + content = content.split(b",")[1] + return safe_b64decode(content) + + +def find_file_by_url(path: str, name: str | None = None) -> Optional["File"]: + filters = {"file_url": str(path)} + if name: + filters["name"] = str(name) + + files = xhiveframework.get_all("File", filters=filters, fields="*") + + # this file might be attached to multiple documents + # if the file is accessible from any one of those documents + # then it should be downloadable + for file_data in files: + file: "File" = xhiveframework.get_doc(doctype="File", **file_data) + if file.is_downloadable(): + return file diff --git a/xhiveframework/core/doctype/has_domain/__init__.py b/xhiveframework/core/doctype/has_domain/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/has_domain/has_domain.json b/xhiveframework/core/doctype/has_domain/has_domain.json new file mode 100644 index 0000000..c34626b --- /dev/null +++ b/xhiveframework/core/doctype/has_domain/has_domain.json @@ -0,0 +1,32 @@ +{ + "actions": [], + "creation": "2017-05-03 15:20:22.326623", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "domain" + ], + "fields": [ + { + "fieldname": "domain", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Domain", + "options": "Domain" + } + ], + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:53.381248", + "modified_by": "Administrator", + "module": "Core", + "name": "Has Domain", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/has_domain/has_domain.py b/xhiveframework/core/doctype/has_domain/has_domain.py new file mode 100644 index 0000000..12f0530 --- /dev/null +++ b/xhiveframework/core/doctype/has_domain/has_domain.py @@ -0,0 +1,21 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class HasDomain(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + domain: DF.Link | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/has_role/__init__.py b/xhiveframework/core/doctype/has_role/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/has_role/has_role.json b/xhiveframework/core/doctype/has_role/has_role.json new file mode 100644 index 0000000..689e804 --- /dev/null +++ b/xhiveframework/core/doctype/has_role/has_role.json @@ -0,0 +1,34 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-02-22 01:27:34", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "role" + ], + "fields": [ + { + "fieldname": "role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Role", + "oldfieldname": "role", + "oldfieldtype": "Link", + "options": "Role" + } + ], + "idx": 1, + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:54.382064", + "modified_by": "Administrator", + "module": "Core", + "name": "Has Role", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/has_role/has_role.py b/xhiveframework/core/doctype/has_role/has_role.py new file mode 100644 index 0000000..2cae768 --- /dev/null +++ b/xhiveframework/core/doctype/has_role/has_role.py @@ -0,0 +1,25 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class HasRole(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + role: DF.Link | None + + # end: auto-generated types + def before_insert(self): + if xhiveframework.db.exists("Has Role", {"parent": self.parent, "role": self.role}): + xhiveframework.throw(xhiveframework._("User '{0}' already has the role '{1}'").format(self.parent, self.role)) diff --git a/xhiveframework/core/doctype/installed_application/__init__.py b/xhiveframework/core/doctype/installed_application/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/installed_application/installed_application.json b/xhiveframework/core/doctype/installed_application/installed_application.json new file mode 100644 index 0000000..1f32c55 --- /dev/null +++ b/xhiveframework/core/doctype/installed_application/installed_application.json @@ -0,0 +1,49 @@ +{ + "actions": [], + "creation": "2020-05-11 17:44:54.674657", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "app_name", + "app_version", + "git_branch" + ], + "fields": [ + { + "fieldname": "git_branch", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Git Branch", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "app_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Application Name", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "app_version", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Application Version", + "read_only": 1, + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2020-05-12 10:09:49.148087", + "modified_by": "Administrator", + "module": "Core", + "name": "Installed Application", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/installed_application/installed_application.py b/xhiveframework/core/doctype/installed_application/installed_application.py new file mode 100644 index 0000000..86c63cf --- /dev/null +++ b/xhiveframework/core/doctype/installed_application/installed_application.py @@ -0,0 +1,24 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class InstalledApplication(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + app_name: DF.Data + app_version: DF.Data + git_branch: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/installed_applications/__init__.py b/xhiveframework/core/doctype/installed_applications/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/installed_applications/installed_applications.js b/xhiveframework/core/doctype/installed_applications/installed_applications.js new file mode 100644 index 0000000..18160eb --- /dev/null +++ b/xhiveframework/core/doctype/installed_applications/installed_applications.js @@ -0,0 +1,67 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Installed Applications", { + refresh: function (frm) { + frm.add_custom_button(__("Update Hooks Resolution Order"), () => { + frm.trigger("show_update_order_dialog"); + }); + }, + + show_update_order_dialog() { + const dialog = new xhiveframework.ui.Dialog({ + title: __("Update Hooks Resolution Order"), + fields: [ + { + fieldname: "apps", + fieldtype: "Table", + label: __("Installed Apps"), + cannot_add_rows: true, + cannot_delete_rows: true, + in_place_edit: true, + data: [], + fields: [ + { + fieldtype: "Data", + fieldname: "app_name", + label: __("App Name"), + in_list_view: 1, + read_only: 1, + }, + ], + }, + ], + primary_action: function () { + const new_order = this.get_values()["apps"].map((row) => row.app_name); + xhiveframework.call({ + method: "xhiveframework.core.doctype.installed_applications.installed_applications.update_installed_apps_order", + freeze: true, + args: { + new_order: new_order, + }, + }); + this.hide(); + }, + primary_action_label: __("Update Order"), + }); + + xhiveframework + .xcall( + "xhiveframework.core.doctype.installed_applications.installed_applications.get_installed_app_order" + ) + .then((data) => { + data.forEach((app) => { + dialog.fields_dict.apps.df.data.push({ + app_name: app, + }); + }); + + dialog.fields_dict.apps.grid.refresh(); + // hack: change checkboxes to drag handles. + let grid = $(dialog.fields_dict.apps.grid.parent); + grid.find(".grid-row-check:first").remove() && + grid.find(".grid-row-check").replaceWith(xhiveframework.utils.icon("menu")); + dialog.show(); + }); + }, +}); diff --git a/xhiveframework/core/doctype/installed_applications/installed_applications.json b/xhiveframework/core/doctype/installed_applications/installed_applications.json new file mode 100644 index 0000000..f2345e6 --- /dev/null +++ b/xhiveframework/core/doctype/installed_applications/installed_applications.json @@ -0,0 +1,42 @@ +{ + "actions": [], + "creation": "2020-05-11 17:45:41.587750", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "installed_applications" + ], + "fields": [ + { + "fieldname": "installed_applications", + "fieldtype": "Table", + "label": "Installed Applications", + "options": "Installed Application", + "read_only": 1 + } + ], + "issingle": 1, + "links": [], + "modified": "2020-05-12 10:09:14.310622", + "modified_by": "Administrator", + "module": "Core", + "name": "Installed Applications", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/installed_applications/installed_applications.py b/xhiveframework/core/doctype/installed_applications/installed_applications.py new file mode 100644 index 0000000..c6594a1 --- /dev/null +++ b/xhiveframework/core/doctype/installed_applications/installed_applications.py @@ -0,0 +1,87 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class InvalidAppOrder(xhiveframework.ValidationError): + pass + + +class InstalledApplications(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.installed_application.installed_application import InstalledApplication + from xhiveframework.types import DF + + installed_applications: DF.Table[InstalledApplication] + + # end: auto-generated types + def update_versions(self): + self.delete_key("installed_applications") + for app in xhiveframework.utils.get_installed_apps_info(): + self.append( + "installed_applications", + { + "app_name": app.get("app_name"), + "app_version": app.get("version") or "UNVERSIONED", + "git_branch": app.get("branch") or "UNVERSIONED", + }, + ) + self.save() + + +@xhiveframework.whitelist() +def update_installed_apps_order(new_order: list[str] | str): + """Change the ordering of `installed_apps` global + + This list is used to resolve hooks and by default it's order of installation on site. + + Sometimes it might not be the ordering you want, so thie function is provided to override it. + """ + xhiveframework.only_for("System Manager") + + if isinstance(new_order, str): + new_order = json.loads(new_order) + + xhiveframework.local.request_cache and xhiveframework.local.request_cache.clear() + existing_order = xhiveframework.get_installed_apps(_ensure_on_bench=True) + + if set(existing_order) != set(new_order) or not isinstance(new_order, list): + xhiveframework.throw( + _("You are only allowed to update order, do not remove or add apps."), exc=InvalidAppOrder + ) + + # Ensure xhiveframework is always first regardless of user's preference. + if "xhiveframework" in new_order: + new_order.remove("xhiveframework") + new_order.insert(0, "xhiveframework") + + xhiveframework.db.set_global("installed_apps", json.dumps(new_order)) + + _create_version_log_for_change(existing_order, new_order) + + +def _create_version_log_for_change(old, new): + version = xhiveframework.new_doc("Version") + version.ref_doctype = "DefaultValue" + version.docname = "installed_apps" + version.data = xhiveframework.as_json({"changed": [["current", json.dumps(old), json.dumps(new)]]}) + version.flags.ignore_links = True # This is a fake doctype + version.flags.ignore_permissions = True + version.insert() + + +@xhiveframework.whitelist() +def get_installed_app_order() -> list[str]: + xhiveframework.only_for("System Manager") + + return xhiveframework.get_installed_apps(_ensure_on_bench=True) diff --git a/xhiveframework/core/doctype/installed_applications/test_installed_applications.py b/xhiveframework/core/doctype/installed_applications/test_installed_applications.py new file mode 100644 index 0000000..90a8853 --- /dev/null +++ b/xhiveframework/core/doctype/installed_applications/test_installed_applications.py @@ -0,0 +1,16 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.core.doctype.installed_applications.installed_applications import ( + InvalidAppOrder, + update_installed_apps_order, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestInstalledApplications(XhiveFrameworkTestCase): + def test_order_change(self): + update_installed_apps_order(["xhiveframework"]) + self.assertRaises(InvalidAppOrder, update_installed_apps_order, []) + self.assertRaises(InvalidAppOrder, update_installed_apps_order, ["xhiveframework", "deepmind"]) diff --git a/xhiveframework/core/doctype/language/__init__.py b/xhiveframework/core/doctype/language/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/language/language.js b/xhiveframework/core/doctype/language/language.js new file mode 100644 index 0000000..3c50630 --- /dev/null +++ b/xhiveframework/core/doctype/language/language.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Language", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/language/language.json b/xhiveframework/core/doctype/language/language.json new file mode 100644 index 0000000..c9110bb --- /dev/null +++ b/xhiveframework/core/doctype/language/language.json @@ -0,0 +1,80 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:language_code", + "creation": "2014-08-22 16:12:17.249590", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "enabled", + "language_code", + "language_name", + "flag", + "based_on" + ], + "fields": [ + { + "fieldname": "language_code", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Language Code", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "language_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Language Name", + "reqd": 1 + }, + { + "fieldname": "flag", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Flag" + }, + { + "fieldname": "based_on", + "fieldtype": "Link", + "label": "Based On", + "options": "Language" + }, + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + } + ], + "icon": "fa fa-globe", + "in_create": 1, + "links": [], + "modified": "2023-04-13 13:48:38.127995", + "modified_by": "Administrator", + "module": "Core", + "name": "Language", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "read": 1, + "role": "System Manager", + "write": 1 + }, + { + "role": "All", + "read": 1 + } + ], + "search_fields": "language_name", + "show_title_field_in_link": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "language_name", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/language/language.py b/xhiveframework/core/doctype/language/language.py new file mode 100644 index 0000000..2c49ac1 --- /dev/null +++ b/xhiveframework/core/doctype/language/language.py @@ -0,0 +1,84 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json +import re + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class Language(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + based_on: DF.Link | None + enabled: DF.Check + flag: DF.Data | None + language_code: DF.Data + language_name: DF.Data + + # end: auto-generated types + def validate(self): + validate_with_regex(self.language_code, "Language Code") + + def before_rename(self, old, new, merge=False): + validate_with_regex(new, "Name") + + def on_update(self): + xhiveframework.cache.delete_value("languages_with_name") + xhiveframework.cache.delete_value("languages") + + +def validate_with_regex(name, label): + pattern = re.compile("^[a-zA-Z]+[-_]*[a-zA-Z]+$") + if not pattern.match(name): + xhiveframework.throw( + _( + """{0} must begin and end with a letter and can only contain letters, + hyphen or underscore.""" + ).format(label) + ) + + +def export_languages_json(): + """Export list of all languages""" + languages = xhiveframework.get_all("Language", fields=["name", "language_name"]) + languages = [{"name": d.language_name, "code": d.name} for d in languages] + + languages.sort(key=lambda a: a["code"]) + + with open(xhiveframework.get_app_path("xhiveframework", "geo", "languages.json"), "w") as f: + f.write(xhiveframework.as_json(languages)) + + +def sync_languages(): + """Sync xhiveframework/geo/languages.json with Language""" + with open(xhiveframework.get_app_path("xhiveframework", "geo", "languages.json")) as f: + data = json.loads(f.read()) + + for l in data: + if not xhiveframework.db.exists("Language", l["code"]): + xhiveframework.get_doc( + { + "doctype": "Language", + "language_code": l["code"], + "language_name": l["name"], + "enabled": 1, + } + ).insert() + + +def update_language_names(): + """Update xhiveframework/geo/languages.json names (for use via patch)""" + with open(xhiveframework.get_app_path("xhiveframework", "geo", "languages.json")) as f: + data = json.loads(f.read()) + + for l in data: + xhiveframework.db.set_value("Language", l["code"], "language_name", l["name"]) diff --git a/xhiveframework/core/doctype/language/test_language.py b/xhiveframework/core/doctype/language/test_language.py new file mode 100644 index 0000000..c2d00c1 --- /dev/null +++ b/xhiveframework/core/doctype/language/test_language.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Language') + + +class TestLanguage(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/log_setting_user/__init__.py b/xhiveframework/core/doctype/log_setting_user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/log_setting_user/log_setting_user.js b/xhiveframework/core/doctype/log_setting_user/log_setting_user.js new file mode 100644 index 0000000..19231af --- /dev/null +++ b/xhiveframework/core/doctype/log_setting_user/log_setting_user.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Log Setting User", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/log_setting_user/log_setting_user.json b/xhiveframework/core/doctype/log_setting_user/log_setting_user.json new file mode 100644 index 0000000..7f4b0ef --- /dev/null +++ b/xhiveframework/core/doctype/log_setting_user/log_setting_user.json @@ -0,0 +1,34 @@ +{ + "actions": [], + "autoname": "field:user", + "creation": "2020-10-08 13:09:36.034430", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "reqd": 1, + "unique": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-10-08 17:22:04.690348", + "modified_by": "Administrator", + "module": "Core", + "name": "Log Setting User", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/log_setting_user/log_setting_user.py b/xhiveframework/core/doctype/log_setting_user/log_setting_user.py new file mode 100644 index 0000000..78c0d81 --- /dev/null +++ b/xhiveframework/core/doctype/log_setting_user/log_setting_user.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class LogSettingUser(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + user: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/log_setting_user/test_log_setting_user.py b/xhiveframework/core/doctype/log_setting_user/test_log_setting_user.py new file mode 100644 index 0000000..609e809 --- /dev/null +++ b/xhiveframework/core/doctype/log_setting_user/test_log_setting_user.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestLogSettingUser(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/log_settings/__init__.py b/xhiveframework/core/doctype/log_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/log_settings/log_settings.js b/xhiveframework/core/doctype/log_settings/log_settings.js new file mode 100644 index 0000000..97d8a00 --- /dev/null +++ b/xhiveframework/core/doctype/log_settings/log_settings.js @@ -0,0 +1,14 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Log Settings", { + refresh: (frm) => { + frm.set_query("ref_doctype", "logs_to_clear", () => { + const added_doctypes = frm.doc.logs_to_clear.map((r) => r.ref_doctype); + return { + query: "xhiveframework.core.doctype.log_settings.log_settings.get_log_doctypes", + filters: [["name", "not in", added_doctypes]], + }; + }); + }, +}); diff --git a/xhiveframework/core/doctype/log_settings/log_settings.json b/xhiveframework/core/doctype/log_settings/log_settings.json new file mode 100644 index 0000000..5a9dd15 --- /dev/null +++ b/xhiveframework/core/doctype/log_settings/log_settings.json @@ -0,0 +1,43 @@ +{ + "actions": [], + "creation": "2020-10-08 12:12:21.694424", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "logs_to_clear" + ], + "fields": [ + { + "fieldname": "logs_to_clear", + "fieldtype": "Table", + "label": "Logs to Clear", + "options": "Logs To Clear" + } + ], + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2022-06-11 02:17:30.803721", + "modified_by": "Administrator", + "module": "Core", + "name": "Log Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/log_settings/log_settings.py b/xhiveframework/core/doctype/log_settings/log_settings.py new file mode 100644 index 0000000..b608fbf --- /dev/null +++ b/xhiveframework/core/doctype/log_settings/log_settings.py @@ -0,0 +1,192 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from typing import Protocol, runtime_checkable + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.base_document import get_controller +from xhiveframework.model.document import Document +from xhiveframework.utils import cint +from xhiveframework.utils.caching import site_cache + + +@runtime_checkable +class LogType(Protocol): + """Interface requirement for doctypes that can be cleared using log settings.""" + + @staticmethod + def clear_old_logs(days: int) -> None: + ... + + +@site_cache +def _supports_log_clearing(doctype: str) -> bool: + try: + controller = get_controller(doctype) + return issubclass(controller, LogType) + except Exception: + return False + + +class LogSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.logs_to_clear.logs_to_clear import LogsToClear + from xhiveframework.types import DF + + logs_to_clear: DF.Table[LogsToClear] + + # end: auto-generated types + def validate(self): + self.remove_unsupported_doctypes() + self._deduplicate_entries() + self.add_default_logtypes() + + def remove_unsupported_doctypes(self): + for entry in list(self.logs_to_clear): + if _supports_log_clearing(entry.ref_doctype): + continue + + msg = _("{} does not support automated log clearing.").format(xhiveframework.bold(entry.ref_doctype)) + if xhiveframework.conf.developer_mode: + msg += "
    " + _("Implement `clear_old_logs` method to enable auto error clearing.") + xhiveframework.msgprint(msg, title=_("DocType not supported by Log Settings.")) + self.remove(entry) + + def _deduplicate_entries(self): + seen = set() + for entry in list(self.logs_to_clear): + if entry.ref_doctype in seen: + self.remove(entry) + seen.add(entry.ref_doctype) + + def add_default_logtypes(self): + existing_logtypes = {d.ref_doctype for d in self.logs_to_clear} + added_logtypes = set() + default_logtypes_retention = xhiveframework.get_hooks("default_log_clearing_doctypes", {}) + + for logtype, retentions in default_logtypes_retention.items(): + if logtype not in existing_logtypes and _supports_log_clearing(logtype): + if not xhiveframework.db.exists("DocType", logtype): + continue + + self.append("logs_to_clear", {"ref_doctype": logtype, "days": cint(retentions[-1])}) + added_logtypes.add(logtype) + + if added_logtypes: + xhiveframework.msgprint(_("Added default log doctypes: {}").format(",".join(added_logtypes)), alert=True) + + def clear_logs(self): + """ + Log settings can clear any log type that's registered to it and provides a method to delete old logs. + + Check `LogDoctype` above for interface that doctypes need to implement. + """ + + for entry in self.logs_to_clear: + controller: LogType = get_controller(entry.ref_doctype) + func = controller.clear_old_logs + + # Only pass what the method can handle, this is considering any + # future addition that might happen to the required interface. + kwargs = xhiveframework.get_newargs(func, {"days": entry.days}) + func(**kwargs) + xhiveframework.db.commit() + + def register_doctype(self, doctype: str, days=30): + existing_logtypes = {d.ref_doctype for d in self.logs_to_clear} + + if doctype not in existing_logtypes and _supports_log_clearing(doctype): + self.append("logs_to_clear", {"ref_doctype": doctype, "days": cint(days)}) + else: + for entry in self.logs_to_clear: + if entry.ref_doctype == doctype: + entry.days = days + break + + +def run_log_clean_up(): + doc = xhiveframework.get_doc("Log Settings") + doc.remove_unsupported_doctypes() + doc.add_default_logtypes() + doc.save() + doc.clear_logs() + + +@xhiveframework.whitelist() +def has_unseen_error_log(): + if xhiveframework.get_all("Error Log", filters={"seen": 0}, limit=1): + return { + "show_alert": True, + "message": _("You have unseen {0}").format( + ' Error Logs ' + ), + } + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_log_doctypes(doctype, txt, searchfield, start, page_len, filters): + filters = filters or {} + + filters.extend( + [ + ["istable", "=", 0], + ["issingle", "=", 0], + ["name", "like", f"%%{txt}%%"], + ] + ) + doctypes = xhiveframework.get_list("DocType", filters=filters, pluck="name") + + supported_doctypes = [(d,) for d in doctypes if _supports_log_clearing(d)] + + return supported_doctypes[start:page_len] + + +LOG_DOCTYPES = [ + "Scheduled Job Log", + "Activity Log", + "Route History", + "Email Queue", + "Email Queue Recipient", + "Error Log", +] + + +def clear_log_table(doctype, days=90): + """If any logtype table grows too large then clearing it with DELETE query + is not feasible in reasonable time. This command copies recent data to new + table and replaces current table with new smaller table. + + ref: https://mariadb.com/kb/en/big-deletes/#deleting-more-than-half-a-table + """ + from xhiveframework.utils import get_table_name + + if doctype not in LOG_DOCTYPES: + raise xhiveframework.ValidationError(f"Unsupported logging DocType: {doctype}") + + original = get_table_name(doctype) + temporary = f"{original} temp_table" + backup = f"{original} backup_table" + + try: + xhiveframework.db.sql_ddl(f"CREATE TABLE `{temporary}` LIKE `{original}`") + + # Copy all recent data to new table + xhiveframework.db.sql( + f"""INSERT INTO `{temporary}` + SELECT * FROM `{original}` + WHERE `{original}`.`modified` > NOW() - INTERVAL '{days}' DAY""" + ) + xhiveframework.db.sql_ddl(f"RENAME TABLE `{original}` TO `{backup}`, `{temporary}` TO `{original}`") + except Exception: + xhiveframework.db.rollback() + xhiveframework.db.sql_ddl(f"DROP TABLE IF EXISTS `{temporary}`") + raise + else: + xhiveframework.db.sql_ddl(f"DROP TABLE `{backup}`") diff --git a/xhiveframework/core/doctype/log_settings/test_log_settings.py b/xhiveframework/core/doctype/log_settings/test_log_settings.py new file mode 100644 index 0000000..5ff0008 --- /dev/null +++ b/xhiveframework/core/doctype/log_settings/test_log_settings.py @@ -0,0 +1,104 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +from datetime import datetime + +import xhiveframework +from xhiveframework.core.doctype.log_settings.log_settings import _supports_log_clearing, run_log_clean_up +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import add_to_date, now_datetime + + +class TestLogSettings(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + xhiveframework.db.set_single_value( + "Log Settings", + { + "clear_error_log_after": 1, + "clear_activity_log_after": 1, + "clear_email_queue_after": 1, + }, + ) + + def setUp(self) -> None: + if self._testMethodName == "test_delete_logs": + self.datetime = xhiveframework._dict() + self.datetime.current = now_datetime() + self.datetime.past = add_to_date(self.datetime.current, days=-4) + setup_test_logs(self.datetime.past) + + def tearDown(self) -> None: + if self._testMethodName == "test_delete_logs": + del self.datetime + + def test_delete_logs(self): + # make sure test data is present + activity_log_count = xhiveframework.db.count("Activity Log", {"creation": ("<=", self.datetime.past)}) + error_log_count = xhiveframework.db.count("Error Log", {"creation": ("<=", self.datetime.past)}) + email_queue_count = xhiveframework.db.count("Email Queue", {"creation": ("<=", self.datetime.past)}) + + self.assertNotEqual(activity_log_count, 0) + self.assertNotEqual(error_log_count, 0) + self.assertNotEqual(email_queue_count, 0) + + # run clean up job + run_log_clean_up() + + # test if logs are deleted + activity_log_count = xhiveframework.db.count("Activity Log", {"creation": ("<", self.datetime.past)}) + error_log_count = xhiveframework.db.count("Error Log", {"creation": ("<", self.datetime.past)}) + email_queue_count = xhiveframework.db.count("Email Queue", {"creation": ("<", self.datetime.past)}) + + self.assertEqual(activity_log_count, 0) + self.assertEqual(error_log_count, 0) + self.assertEqual(email_queue_count, 0) + + def test_logtype_identification(self): + supported_types = [ + "Error Log", + "Activity Log", + "Email Queue", + "Route History", + "Scheduled Job Log", + ] + + for lt in supported_types: + self.assertTrue(_supports_log_clearing(lt), f"{lt} should be recognized as log type") + + unsupported_types = ["DocType", "User", "Non Existing dt"] + for dt in unsupported_types: + self.assertFalse(_supports_log_clearing(dt), f"{dt} shouldn't be recognized as log type") + + +def setup_test_logs(past: datetime) -> None: + activity_log = xhiveframework.get_doc( + { + "doctype": "Activity Log", + "subject": "Test subject", + "full_name": "test user2", + } + ).insert(ignore_permissions=True) + activity_log.db_set("creation", past) + + error_log = xhiveframework.get_doc( + { + "doctype": "Error Log", + "method": "test_method", + "error": "traceback", + } + ).insert(ignore_permissions=True) + error_log.db_set("creation", past) + + doc1 = xhiveframework.get_doc( + { + "doctype": "Email Queue", + "sender": "test1@example.com", + "message": "This is a test email1", + "priority": 1, + "expose_recipients": "test@receiver.com", + } + ).insert(ignore_permissions=True) + doc1.db_set("creation", past) diff --git a/xhiveframework/core/doctype/logs_to_clear/__init__.py b/xhiveframework/core/doctype/logs_to_clear/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.json b/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.json new file mode 100644 index 0000000..df19ccd --- /dev/null +++ b/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.json @@ -0,0 +1,43 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2022-06-11 02:02:39.472511", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ref_doctype", + "days" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Log DocType", + "options": "DocType", + "reqd": 1 + }, + { + "default": "30", + "fieldname": "days", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Clear Logs After (days)", + "non_negative": 1, + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2022-06-13 02:51:36.857786", + "modified_by": "Administrator", + "module": "Core", + "name": "Logs To Clear", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} diff --git a/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.py b/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.py new file mode 100644 index 0000000..c73dc7d --- /dev/null +++ b/xhiveframework/core/doctype/logs_to_clear/logs_to_clear.py @@ -0,0 +1,23 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class LogsToClear(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + days: DF.Int + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + ref_doctype: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/module_def/README.md b/xhiveframework/core/doctype/module_def/README.md new file mode 100644 index 0000000..ff89390 --- /dev/null +++ b/xhiveframework/core/doctype/module_def/README.md @@ -0,0 +1 @@ +Module of the application (e.g. Core, Accounts etc.) \ No newline at end of file diff --git a/xhiveframework/core/doctype/module_def/__init__.py b/xhiveframework/core/doctype/module_def/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/module_def/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/module_def/module_def.js b/xhiveframework/core/doctype/module_def/module_def.js new file mode 100644 index 0000000..f07a2de --- /dev/null +++ b/xhiveframework/core/doctype/module_def/module_def.js @@ -0,0 +1,20 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Module Def", { + refresh: function (frm) { + xhiveframework.xcall("xhiveframework.core.doctype.module_def.module_def.get_installed_apps").then((r) => { + frm.set_df_property("app_name", "options", JSON.parse(r)); + if (!frm.doc.app_name) { + frm.set_value("app_name", "xhiveframework"); + } + }); + + if (!xhiveframework.boot.developer_mode) { + frm.set_df_property("custom", "read_only", 1); + if (frm.is_new()) { + frm.set_value("custom", 1); + } + } + }, +}); diff --git a/xhiveframework/core/doctype/module_def/module_def.json b/xhiveframework/core/doctype/module_def/module_def.json new file mode 100644 index 0000000..e9a03bc --- /dev/null +++ b/xhiveframework/core/doctype/module_def/module_def.json @@ -0,0 +1,166 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:module_name", + "creation": "2013-01-10 16:34:03", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "module_name", + "custom", + "package", + "app_name", + "restrict_to_domain", + "connections_tab" + ], + "fields": [ + { + "fieldname": "module_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Module Name", + "oldfieldname": "module_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "depends_on": "eval:!doc.custom", + "fieldname": "app_name", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "App Name", + "reqd": 1 + }, + { + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict To Domain", + "options": "Domain" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "label": "Custom" + }, + { + "depends_on": "custom", + "fieldname": "package", + "fieldtype": "Link", + "label": "Package", + "options": "Package" + }, + { + "fieldname": "connections_tab", + "fieldtype": "Tab Break", + "label": "Connections", + "show_dashboard": 1 + } + ], + "icon": "fa fa-sitemap", + "idx": 1, + "links": [ + { + "group": "DocType", + "link_doctype": "DocType", + "link_fieldname": "module" + }, + { + "group": "DocType", + "link_doctype": "Client Script", + "link_fieldname": "module" + }, + { + "group": "DocType", + "link_doctype": "Server Script", + "link_fieldname": "module" + }, + { + "group": "Website", + "link_doctype": "Web Page", + "link_fieldname": "module" + }, + { + "group": "Website", + "link_doctype": "Web Template", + "link_fieldname": "module" + }, + { + "group": "Website", + "link_doctype": "Website Theme", + "link_fieldname": "module" + }, + { + "group": "Website", + "link_doctype": "Web Form", + "link_fieldname": "module" + }, + { + "group": "Customization", + "link_doctype": "Workspace", + "link_fieldname": "module" + }, + { + "group": "Customization", + "link_doctype": "Custom Field", + "link_fieldname": "module" + }, + { + "group": "Customization", + "link_doctype": "Property Setter", + "link_fieldname": "module" + }, + { + "group": "Customization", + "link_doctype": "Print Format", + "link_fieldname": "module" + }, + { + "group": "Customization", + "link_doctype": "Notification", + "link_fieldname": "module" + } + ], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Core", + "name": "Module Def", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "select": 1, + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "select": 1, + "write": 1 + }, + { + "read": 1, + "report": 1, + "role": "All", + "select": 1 + } + ], + "show_name_in_global_search": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/module_def/module_def.py b/xhiveframework/core/doctype/module_def/module_def.py new file mode 100644 index 0000000..be06005 --- /dev/null +++ b/xhiveframework/core/doctype/module_def/module_def.py @@ -0,0 +1,89 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import os +from pathlib import Path + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.modules.export_file import delete_folder + + +class ModuleDef(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + app_name: DF.Literal[None] + custom: DF.Check + module_name: DF.Data + package: DF.Link | None + restrict_to_domain: DF.Link | None + + # end: auto-generated types + def on_update(self): + """If in `developer_mode`, create folder for module and + add in `modules.txt` of app if missing.""" + xhiveframework.clear_cache() + if not self.custom and xhiveframework.conf.get("developer_mode"): + self.create_modules_folder() + self.add_to_modules_txt() + + def create_modules_folder(self): + """Creates a folder `[app]/[module]` and adds `__init__.py`""" + module_path = xhiveframework.get_app_path(self.app_name, self.name) + if not os.path.exists(module_path): + os.mkdir(module_path) + with open(os.path.join(module_path, "__init__.py"), "w") as f: + f.write("") + + def add_to_modules_txt(self): + """Adds to `[app]/modules.txt`""" + modules = None + if not xhiveframework.local.module_app.get(xhiveframework.scrub(self.name)): + with open(xhiveframework.get_app_path(self.app_name, "modules.txt")) as f: + content = f.read() + if self.name not in content.splitlines(): + modules = list(filter(None, content.splitlines())) + modules.append(self.name) + + if modules: + with open(xhiveframework.get_app_path(self.app_name, "modules.txt"), "w") as f: + f.write("\n".join(modules)) + + xhiveframework.clear_cache() + xhiveframework.setup_module_map() + + def on_trash(self): + """Delete module name from modules.txt""" + + if not xhiveframework.conf.get("developer_mode") or xhiveframework.flags.in_uninstall or self.custom: + return + + if xhiveframework.local.module_app.get(xhiveframework.scrub(self.name)): + xhiveframework.db.after_commit.add(self.delete_module_from_file) + + def delete_module_from_file(self): + delete_folder(self.module_name, "Module Def", self.name) + modules = [] + + modules_txt = Path(xhiveframework.get_app_path(self.app_name, "modules.txt")) + modules = [m for m in modules_txt.read_text().splitlines() if m] + + if self.name in modules: + modules.remove(self.name) + + if modules: + modules_txt.write_text("\n".join(modules)) + xhiveframework.clear_cache() + xhiveframework.setup_module_map() + + +@xhiveframework.whitelist() +def get_installed_apps(): + return json.dumps(xhiveframework.get_installed_apps()) diff --git a/xhiveframework/core/doctype/module_def/module_def_list.js b/xhiveframework/core/doctype/module_def/module_def_list.js new file mode 100644 index 0000000..b2067fb --- /dev/null +++ b/xhiveframework/core/doctype/module_def/module_def_list.js @@ -0,0 +1,16 @@ +xhiveframework.listview_settings["Module Def"] = { + onload: function (list_view) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.module_def.module_def.get_installed_apps", + callback: (r) => { + const field = list_view.page.fields_dict.app_name; + if (!field) return; + + const options = JSON.parse(r.message); + options.unshift(""); // Add empty option + field.df.options = options; + field.set_options(); + }, + }); + }, +}; diff --git a/xhiveframework/core/doctype/module_def/test_module_def.py b/xhiveframework/core/doctype/module_def/test_module_def.py new file mode 100644 index 0000000..819d38a --- /dev/null +++ b/xhiveframework/core/doctype/module_def/test_module_def.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Module Def') + + +class TestModuleDef(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/module_profile/__init__.py b/xhiveframework/core/doctype/module_profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/module_profile/module_profile.js b/xhiveframework/core/doctype/module_profile/module_profile.js new file mode 100644 index 0000000..15d0755 --- /dev/null +++ b/xhiveframework/core/doctype/module_profile/module_profile.js @@ -0,0 +1,23 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Module Profile", { + refresh: function (frm) { + if (has_common(xhiveframework.user_roles, ["Administrator", "System Manager"])) { + if (!frm.module_editor && frm.doc.__onload && frm.doc.__onload.all_modules) { + const module_area = $(frm.fields_dict.module_html.wrapper); + frm.module_editor = new xhiveframework.ModuleEditor(frm, module_area); + } + } + + if (frm.module_editor) { + frm.module_editor.show(); + } + }, + + validate: function (frm) { + if (frm.module_editor) { + frm.module_editor.set_modules_in_table(); + } + }, +}); diff --git a/xhiveframework/core/doctype/module_profile/module_profile.json b/xhiveframework/core/doctype/module_profile/module_profile.json new file mode 100644 index 0000000..32bc757 --- /dev/null +++ b/xhiveframework/core/doctype/module_profile/module_profile.json @@ -0,0 +1,66 @@ +{ + "actions": [], + "autoname": "field:module_profile_name", + "creation": "2020-12-22 22:00:30.614475", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "module_profile_name", + "module_html", + "block_modules" + ], + "fields": [ + { + "fieldname": "module_profile_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Module Profile Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "module_html", + "fieldtype": "HTML", + "label": "Module HTML" + }, + { + "fieldname": "block_modules", + "fieldtype": "Table", + "hidden": 1, + "label": "Block Modules", + "options": "Block Module", + "read_only": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [ + { + "link_doctype": "User", + "link_fieldname": "module_profile" + } + ], + "modified": "2021-12-03 15:47:21.296443", + "modified_by": "Administrator", + "module": "Core", + "name": "Module Profile", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/module_profile/module_profile.py b/xhiveframework/core/doctype/module_profile/module_profile.py new file mode 100644 index 0000000..1262193 --- /dev/null +++ b/xhiveframework/core/doctype/module_profile/module_profile.py @@ -0,0 +1,24 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class ModuleProfile(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.block_module.block_module import BlockModule + from xhiveframework.types import DF + + block_modules: DF.Table[BlockModule] + module_profile_name: DF.Data + + # end: auto-generated types + def onload(self): + from xhiveframework.config import get_modules_from_all_apps + + self.set_onload("all_modules", sorted(m.get("module_name") for m in get_modules_from_all_apps())) diff --git a/xhiveframework/core/doctype/module_profile/test_module_profile.py b/xhiveframework/core/doctype/module_profile/test_module_profile.py new file mode 100644 index 0000000..a3c3232 --- /dev/null +++ b/xhiveframework/core/doctype/module_profile/test_module_profile.py @@ -0,0 +1,29 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestModuleProfile(XhiveFrameworkTestCase): + def test_make_new_module_profile(self): + if not xhiveframework.db.get_value("Module Profile", "_Test Module Profile"): + xhiveframework.get_doc( + { + "doctype": "Module Profile", + "module_profile_name": "_Test Module Profile", + "block_modules": [{"module": "Accounts"}], + } + ).insert() + + # add to user and check + if not xhiveframework.db.get_value("User", "test-for-module_profile@example.com"): + new_user = xhiveframework.get_doc( + {"doctype": "User", "email": "test-for-module_profile@example.com", "first_name": "Test User"} + ).insert() + else: + new_user = xhiveframework.get_doc("User", "test-for-module_profile@example.com") + + new_user.module_profile = "_Test Module Profile" + new_user.save() + + self.assertEqual(new_user.block_modules[0].module, "Accounts") diff --git a/xhiveframework/core/doctype/navbar_item/__init__.py b/xhiveframework/core/doctype/navbar_item/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/navbar_item/navbar_item.js b/xhiveframework/core/doctype/navbar_item/navbar_item.js new file mode 100644 index 0000000..c266612 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_item/navbar_item.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Navbar Item", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/navbar_item/navbar_item.json b/xhiveframework/core/doctype/navbar_item/navbar_item.json new file mode 100644 index 0000000..541d785 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_item/navbar_item.json @@ -0,0 +1,89 @@ +{ + "actions": [], + "creation": "2020-08-01 23:38:41.783206", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "item_label", + "item_type", + "route", + "action", + "hidden", + "is_standard" + ], + "fields": [ + { + "columns": 2, + "fieldname": "item_label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Item Label", + "mandatory_depends_on": "eval:doc.item_type == 'Route' || doc.item_type == 'Action'", + "show_days": 1, + "show_seconds": 1 + }, + { + "columns": 2, + "fieldname": "item_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Item Type", + "options": "Route\nAction\nSeparator", + "read_only_depends_on": "eval:doc.is_standard", + "show_days": 1, + "show_seconds": 1 + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Hidden", + "show_days": 1, + "show_seconds": 1 + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "read_only": 1, + "show_days": 1, + "show_seconds": 1 + }, + { + "columns": 4, + "depends_on": "eval:doc.item_type == 'Route'", + "fieldname": "route", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Route", + "mandatory_depends_on": "eval:doc.item_type == 'Route'", + "read_only_depends_on": "eval:doc.is_standard", + "show_days": 1, + "show_seconds": 1 + }, + { + "depends_on": "eval:doc.item_type == 'Action'", + "fieldname": "action", + "fieldtype": "Data", + "label": "Action", + "mandatory_depends_on": "eval:doc.item_type == 'Action'", + "read_only_depends_on": "eval:doc.is_standard", + "show_days": 1, + "show_seconds": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2020-11-02 10:57:37.709262", + "modified_by": "Administrator", + "module": "Core", + "name": "Navbar Item", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/navbar_item/navbar_item.py b/xhiveframework/core/doctype/navbar_item/navbar_item.py new file mode 100644 index 0000000..bc7d128 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_item/navbar_item.py @@ -0,0 +1,27 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class NavbarItem(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action: DF.Data | None + hidden: DF.Check + is_standard: DF.Check + item_label: DF.Data | None + item_type: DF.Literal["Route", "Action", "Separator"] + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + route: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/navbar_item/test_navbar_item.py b/xhiveframework/core/doctype/navbar_item/test_navbar_item.py new file mode 100644 index 0000000..6123891 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_item/test_navbar_item.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNavbarItem(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/navbar_settings/__init__.py b/xhiveframework/core/doctype/navbar_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/navbar_settings/navbar_settings.js b/xhiveframework/core/doctype/navbar_settings/navbar_settings.js new file mode 100644 index 0000000..631eaa1 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_settings/navbar_settings.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Navbar Settings", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/navbar_settings/navbar_settings.json b/xhiveframework/core/doctype/navbar_settings/navbar_settings.json new file mode 100644 index 0000000..8fc0c83 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_settings/navbar_settings.json @@ -0,0 +1,91 @@ +{ + "actions": [], + "creation": "2020-08-01 23:41:12.577160", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "logo_section", + "app_logo", + "column_break_3", + "logo_width", + "section_break_2", + "settings_dropdown", + "help_dropdown" + ], + "fields": [ + { + "fieldname": "app_logo", + "fieldtype": "Attach Image", + "label": "Application Logo", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "settings_dropdown", + "fieldtype": "Table", + "label": "Settings Dropdown", + "options": "Navbar Item", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "help_dropdown", + "fieldtype": "Table", + "label": "Help Dropdown", + "options": "Navbar Item", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "section_break_2", + "fieldtype": "Section Break", + "label": "Dropdowns", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "logo_section", + "fieldtype": "Section Break", + "label": "Application Logo", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break", + "show_days": 1, + "show_seconds": 1 + }, + { + "fieldname": "logo_width", + "fieldtype": "Int", + "label": "Logo Width", + "show_days": 1, + "show_seconds": 1 + } + ], + "issingle": 1, + "links": [], + "modified": "2020-08-06 18:11:29.955835", + "modified_by": "Administrator", + "module": "Core", + "name": "Navbar Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/navbar_settings/navbar_settings.py b/xhiveframework/core/doctype/navbar_settings/navbar_settings.py new file mode 100644 index 0000000..b2bdcf1 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_settings/navbar_settings.py @@ -0,0 +1,55 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class NavbarSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.navbar_item.navbar_item import NavbarItem + from xhiveframework.types import DF + + app_logo: DF.AttachImage | None + help_dropdown: DF.Table[NavbarItem] + logo_width: DF.Int + settings_dropdown: DF.Table[NavbarItem] + + # end: auto-generated types + def validate(self): + self.validate_standard_navbar_items() + + def validate_standard_navbar_items(self): + doc_before_save = self.get_doc_before_save() + + if not doc_before_save: + return + + before_save_items = [ + item + for item in doc_before_save.help_dropdown + doc_before_save.settings_dropdown + if item.is_standard + ] + + after_save_items = [item for item in self.help_dropdown + self.settings_dropdown if item.is_standard] + + if not xhiveframework.flags.in_patch and (len(before_save_items) > len(after_save_items)): + xhiveframework.throw(_("Please hide the standard navbar items instead of deleting them")) + + +def get_app_logo(): + app_logo = xhiveframework.db.get_single_value("Navbar Settings", "app_logo", cache=True) + if not app_logo: + app_logo = xhiveframework.get_hooks("app_logo_url")[-1] + + return app_logo + + +def get_navbar_settings(): + return xhiveframework.get_single("Navbar Settings") diff --git a/xhiveframework/core/doctype/navbar_settings/test_navbar_settings.py b/xhiveframework/core/doctype/navbar_settings/test_navbar_settings.py new file mode 100644 index 0000000..b7c0803 --- /dev/null +++ b/xhiveframework/core/doctype/navbar_settings/test_navbar_settings.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNavbarSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/package/__init__.py b/xhiveframework/core/doctype/package/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/package/licenses/GNU Affero General Public License.md b/xhiveframework/core/doctype/package/licenses/GNU Affero General Public License.md new file mode 100644 index 0000000..c7f159a --- /dev/null +++ b/xhiveframework/core/doctype/package/licenses/GNU Affero General Public License.md @@ -0,0 +1,614 @@ +### GNU AFFERO GENERAL PUBLIC LICENSE + +Version 3, 19 November 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +### Preamble + +The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains +free software for all its users. + +When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + +Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + +A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + +The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + +An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing +under this license. + +The precise terms and conditions for copying, distribution and +modification follow. + +### TERMS AND CONDITIONS + +#### 0. Definitions. + +"This License" refers to version 3 of the GNU Affero General Public +License. + +"Copyright" also means copyright-like laws that apply to other kinds +of works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of +an exact copy. The resulting work is called a "modified version" of +the earlier work or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based +on the Program. + +To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user +through a computer network, with no transfer of a copy, is not +conveying. + +An interactive user interface displays "Appropriate Legal Notices" to +the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + +#### 1. Source Code. + +The "source code" for a work means the preferred form of the work for +making modifications to it. "Object code" means any non-source form of +a work. + +A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + +The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can +regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same +work. + +#### 2. Basic Permissions. + +All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, +without conditions so long as your license otherwise remains in force. +You may convey covered works to others for the sole purpose of having +them make modifications exclusively for you, or provide you with +facilities for running those works, provided that you comply with the +terms of this License in conveying all material for which you do not +control copyright. Those thus making or running the covered works for +you must do so exclusively on your behalf, under your direction and +control, on terms that prohibit them from making any copies of your +copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes +it unnecessary. + +#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + +No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such +circumvention is effected by exercising rights under this License with +respect to the covered work, and you disclaim any intention to limit +operation or modification of the work as a means of enforcing, against +the work's users, your or third parties' legal rights to forbid +circumvention of technological measures. + +#### 4. Conveying Verbatim Copies. + +You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + +#### 5. Conveying Modified Source Versions. + +You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these +conditions: + +- a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. +- b) The work must carry prominent notices stating that it is + released under this License and any conditions added under + section 7. This requirement modifies the requirement in section 4 + to "keep intact all notices". +- c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. +- d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + +A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + +#### 6. Conveying Non-Source Forms. + +You may convey a covered work in object code form under the terms of +sections 4 and 5, provided that you also convey the machine-readable +Corresponding Source under the terms of this License, in one of these +ways: + +- a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. +- b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the Corresponding + Source from a network server at no charge. +- c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. +- d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. +- e) Convey the object code using peer-to-peer transmission, + provided you inform other peers where the object code and + Corresponding Source of the work are being offered to the general + public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, +family, or household purposes, or (2) anything designed or sold for +incorporation into a dwelling. In determining whether a product is a +consumer product, doubtful cases shall be resolved in favor of +coverage. For a particular product received by a particular user, +"normally used" refers to a typical or common use of that class of +product, regardless of the status of the particular user or of the way +in which the particular user actually uses, or expects or is expected +to use, the product. A product is a consumer product regardless of +whether the product has substantial commercial, industrial or +non-consumer uses, unless such uses represent the only significant +mode of use of the product. + +"Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to +install and execute modified versions of a covered work in that User +Product from a modified version of its Corresponding Source. The +information must suffice to ensure that the continued functioning of +the modified object code is in no case prevented or interfered with +solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or +updates for a work that has been modified or installed by the +recipient, or for the User Product in which it has been modified or +installed. Access to a network may be denied when the modification +itself materially and adversely affects the operation of the network +or violates the rules and protocols for communication across the +network. + +Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + +#### 7. Additional Terms. + +"Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders +of that material) supplement the terms of this License with terms: + +- a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or +- b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or +- c) Prohibiting misrepresentation of the origin of that material, + or requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or +- d) Limiting the use for publicity purposes of names of licensors + or authors of the material; or +- e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or +- f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions + of it) with contractual assumptions of liability to the recipient, + for any liability that these contractual assumptions directly + impose on those licensors and authors. + +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; the +above requirements apply either way. + +#### 8. Termination. + +You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + +However, if you cease all violation of this License, then your license +from a particular copyright holder is reinstated (a) provisionally, +unless and until the copyright holder explicitly and finally +terminates your license, and (b) permanently, if the copyright holder +fails to notify you of the violation by some reasonable means prior to +60 days after the cessation. + +Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + +Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + +#### 9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run +a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + +#### 10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + +#### 11. Patents. + +A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned +or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + +If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + +A patent license is "discriminatory" if it does not include within the +scope of its coverage, prohibits the exercise of, or is conditioned on +the non-exercise of one or more of the rights that are specifically +granted under this License. You may not convey a covered work if you +are a party to an arrangement with a third party that is in the +business of distributing software, under which you make payment to the +third party based on the extent of your activity of conveying the +work, and under which the third party grants, to any of the parties +who would receive the covered work from you, a discriminatory patent +license (a) in connection with copies of the covered work conveyed by +you (or copies made from those copies), or (b) primarily for and in +connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + +#### 12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under +this License and any other pertinent obligations, then as a +consequence you may not convey it at all. For example, if you agree to +terms that obligate you to collect a royalty for further conveying +from those to whom you convey the Program, the only way you could +satisfy both those terms and this License would be to refrain entirely +from conveying the Program. + +#### 13. Remote Network Interaction; Use with the GNU General Public License. + +Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your +version supports such interaction) an opportunity to receive the +Corresponding Source of your version by providing access to the +Corresponding Source from a network server at no charge, through some +standard or customary means of facilitating copying of software. This +Corresponding Source shall include the Corresponding Source for any +work covered by version 3 of the GNU General Public License that is +incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + +#### 14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions +of the GNU Affero General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever +published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions +of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + +Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + +#### 15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT +WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND +PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE +DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR +CORRECTION. + +#### 16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR +CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES +ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT +NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR +LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM +TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER +PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +#### 17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. diff --git a/xhiveframework/core/doctype/package/licenses/GNU General Public License.md b/xhiveframework/core/doctype/package/licenses/GNU General Public License.md new file mode 100644 index 0000000..c4580f2 --- /dev/null +++ b/xhiveframework/core/doctype/package/licenses/GNU General Public License.md @@ -0,0 +1,617 @@ +### GNU GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +### Preamble + +The GNU General Public License is a free, copyleft license for +software and other kinds of works. + +The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom +to share and change all versions of a program--to make sure it remains +free software for all its users. We, the Free Software Foundation, use +the GNU General Public License for most of our software; it applies +also to any other work released this way by its authors. You can apply +it to your programs, too. + +When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + +To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you +have certain responsibilities if you distribute copies of the +software, or if you modify it: responsibilities to respect the freedom +of others. + +For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + +Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + +For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + +Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the +manufacturer can do so. This is fundamentally incompatible with the +aim of protecting users' freedom to change the software. The +systematic pattern of such abuse occurs in the area of products for +individuals to use, which is precisely where it is most unacceptable. +Therefore, we have designed this version of the GPL to prohibit the +practice for those products. If such problems arise substantially in +other domains, we stand ready to extend this provision to those +domains in future versions of the GPL, as needed to protect the +freedom of users. + +Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish +to avoid the special danger that patents applied to a free program +could make it effectively proprietary. To prevent this, the GPL +assures that patents cannot be used to render the program non-free. + +The precise terms and conditions for copying, distribution and +modification follow. + +### TERMS AND CONDITIONS + +#### 0. Definitions. + +"This License" refers to version 3 of the GNU General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds +of works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of +an exact copy. The resulting work is called a "modified version" of +the earlier work or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based +on the Program. + +To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user +through a computer network, with no transfer of a copy, is not +conveying. + +An interactive user interface displays "Appropriate Legal Notices" to +the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + +#### 1. Source Code. + +The "source code" for a work means the preferred form of the work for +making modifications to it. "Object code" means any non-source form of +a work. + +A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + +The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can +regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same +work. + +#### 2. Basic Permissions. + +All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, +without conditions so long as your license otherwise remains in force. +You may convey covered works to others for the sole purpose of having +them make modifications exclusively for you, or provide you with +facilities for running those works, provided that you comply with the +terms of this License in conveying all material for which you do not +control copyright. Those thus making or running the covered works for +you must do so exclusively on your behalf, under your direction and +control, on terms that prohibit them from making any copies of your +copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes +it unnecessary. + +#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + +No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such +circumvention is effected by exercising rights under this License with +respect to the covered work, and you disclaim any intention to limit +operation or modification of the work as a means of enforcing, against +the work's users, your or third parties' legal rights to forbid +circumvention of technological measures. + +#### 4. Conveying Verbatim Copies. + +You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + +#### 5. Conveying Modified Source Versions. + +You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these +conditions: + +- a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. +- b) The work must carry prominent notices stating that it is + released under this License and any conditions added under + section 7. This requirement modifies the requirement in section 4 + to "keep intact all notices". +- c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. +- d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + +A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + +#### 6. Conveying Non-Source Forms. + +You may convey a covered work in object code form under the terms of +sections 4 and 5, provided that you also convey the machine-readable +Corresponding Source under the terms of this License, in one of these +ways: + +- a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. +- b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the Corresponding + Source from a network server at no charge. +- c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. +- d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. +- e) Convey the object code using peer-to-peer transmission, + provided you inform other peers where the object code and + Corresponding Source of the work are being offered to the general + public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, +family, or household purposes, or (2) anything designed or sold for +incorporation into a dwelling. In determining whether a product is a +consumer product, doubtful cases shall be resolved in favor of +coverage. For a particular product received by a particular user, +"normally used" refers to a typical or common use of that class of +product, regardless of the status of the particular user or of the way +in which the particular user actually uses, or expects or is expected +to use, the product. A product is a consumer product regardless of +whether the product has substantial commercial, industrial or +non-consumer uses, unless such uses represent the only significant +mode of use of the product. + +"Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to +install and execute modified versions of a covered work in that User +Product from a modified version of its Corresponding Source. The +information must suffice to ensure that the continued functioning of +the modified object code is in no case prevented or interfered with +solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or +updates for a work that has been modified or installed by the +recipient, or for the User Product in which it has been modified or +installed. Access to a network may be denied when the modification +itself materially and adversely affects the operation of the network +or violates the rules and protocols for communication across the +network. + +Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + +#### 7. Additional Terms. + +"Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders +of that material) supplement the terms of this License with terms: + +- a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or +- b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or +- c) Prohibiting misrepresentation of the origin of that material, + or requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or +- d) Limiting the use for publicity purposes of names of licensors + or authors of the material; or +- e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or +- f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions + of it) with contractual assumptions of liability to the recipient, + for any liability that these contractual assumptions directly + impose on those licensors and authors. + +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; the +above requirements apply either way. + +#### 8. Termination. + +You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + +However, if you cease all violation of this License, then your license +from a particular copyright holder is reinstated (a) provisionally, +unless and until the copyright holder explicitly and finally +terminates your license, and (b) permanently, if the copyright holder +fails to notify you of the violation by some reasonable means prior to +60 days after the cessation. + +Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + +Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + +#### 9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run +a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + +#### 10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + +#### 11. Patents. + +A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned +or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + +If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + +A patent license is "discriminatory" if it does not include within the +scope of its coverage, prohibits the exercise of, or is conditioned on +the non-exercise of one or more of the rights that are specifically +granted under this License. You may not convey a covered work if you +are a party to an arrangement with a third party that is in the +business of distributing software, under which you make payment to the +third party based on the extent of your activity of conveying the +work, and under which the third party grants, to any of the parties +who would receive the covered work from you, a discriminatory patent +license (a) in connection with copies of the covered work conveyed by +you (or copies made from those copies), or (b) primarily for and in +connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + +#### 12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under +this License and any other pertinent obligations, then as a +consequence you may not convey it at all. For example, if you agree to +terms that obligate you to collect a royalty for further conveying +from those to whom you convey the Program, the only way you could +satisfy both those terms and this License would be to refrain entirely +from conveying the Program. + +#### 13. Use with the GNU Affero General Public License. + +Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + +#### 14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions +of the GNU General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in +detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU General Public +License "or any later version" applies to it, you have the option of +following the terms and conditions either of that numbered version or +of any later version published by the Free Software Foundation. If the +Program does not specify a version number of the GNU General Public +License, you may choose any version ever published by the Free +Software Foundation. + +If the Program specifies that a proxy can decide which future versions +of the GNU General Public License can be used, that proxy's public +statement of acceptance of a version permanently authorizes you to +choose that version for the Program. + +Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + +#### 15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT +WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND +PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE +DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR +CORRECTION. + +#### 16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR +CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES +ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT +NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR +LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM +TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER +PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +#### 17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. diff --git a/xhiveframework/core/doctype/package/licenses/MIT License.md b/xhiveframework/core/doctype/package/licenses/MIT License.md new file mode 100644 index 0000000..c038ee7 --- /dev/null +++ b/xhiveframework/core/doctype/package/licenses/MIT License.md @@ -0,0 +1,17 @@ +### MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies +or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/xhiveframework/core/doctype/package/package.js b/xhiveframework/core/doctype/package/package.js new file mode 100644 index 0000000..36ec020 --- /dev/null +++ b/xhiveframework/core/doctype/package/package.js @@ -0,0 +1,20 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Package", { + validate: function (frm) { + if (!frm.doc.package_name) { + frm.set_value("package_name", frm.doc.name.toLowerCase().replace(" ", "-")); + } + }, + + license_type: function (frm) { + xhiveframework + .call("xhiveframework.core.doctype.package.package.get_license_text", { + license_type: frm.doc.license_type, + }) + .then((r) => { + frm.set_value("license", r.message); + }); + }, +}); diff --git a/xhiveframework/core/doctype/package/package.json b/xhiveframework/core/doctype/package/package.json new file mode 100644 index 0000000..285e17a --- /dev/null +++ b/xhiveframework/core/doctype/package/package.json @@ -0,0 +1,76 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2021-09-04 11:54:35.155687", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "package_name", + "readme", + "license_type", + "license" + ], + "fields": [ + { + "fieldname": "readme", + "fieldtype": "Markdown Editor", + "label": "Readme" + }, + { + "fieldname": "package_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Package Name", + "reqd": 1 + }, + { + "fieldname": "license_type", + "fieldtype": "Select", + "label": "License Type", + "options": "\nMIT License\nGNU General Public License\nGNU Affero General Public License" + }, + { + "fieldname": "license", + "fieldtype": "Markdown Editor", + "label": "License" + } + ], + "index_web_pages_for_search": 1, + "links": [ + { + "group": "Modules", + "link_doctype": "Module Def", + "link_fieldname": "package" + }, + { + "group": "Release", + "link_doctype": "Package Release", + "link_fieldname": "package" + } + ], + "modified": "2021-09-05 13:15:01.130982", + "modified_by": "Administrator", + "module": "Core", + "name": "Package", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/package/package.py b/xhiveframework/core/doctype/package/package.py new file mode 100644 index 0000000..7b7c849 --- /dev/null +++ b/xhiveframework/core/doctype/package/package.py @@ -0,0 +1,42 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import os + +import xhiveframework +from xhiveframework.model.document import Document + +LICENSES = ( + "GNU Affero General Public License", + "GNU General Public License", + "MIT License", +) + + +class Package(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + license: DF.MarkdownEditor | None + license_type: DF.Literal[ + "", "MIT License", "GNU General Public License", "GNU Affero General Public License" + ] + package_name: DF.Data + readme: DF.MarkdownEditor | None + + # end: auto-generated types + def validate(self): + if not self.package_name: + self.package_name = self.name.lower().replace(" ", "-") + + +@xhiveframework.whitelist() +def get_license_text(license_type: str) -> str | None: + if license_type in LICENSES: + with open(os.path.join(os.path.dirname(__file__), "licenses", license_type + ".md")) as textfile: + return textfile.read() diff --git a/xhiveframework/core/doctype/package/test_package.py b/xhiveframework/core/doctype/package/test_package.py new file mode 100644 index 0000000..0df8eb6 --- /dev/null +++ b/xhiveframework/core/doctype/package/test_package.py @@ -0,0 +1,112 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +import json +import os + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPackage(XhiveFrameworkTestCase): + def test_package_release(self): + make_test_package() + make_test_module() + make_test_doctype() + make_test_server_script() + make_test_web_page() + + # make release + xhiveframework.get_doc(dict(doctype="Package Release", package="Test Package", publish=1)).insert() + + self.assertTrue(os.path.exists(xhiveframework.get_site_path("packages", "test-package"))) + self.assertTrue( + os.path.exists(xhiveframework.get_site_path("packages", "test-package", "test_module_for_package")) + ) + self.assertTrue( + os.path.exists( + xhiveframework.get_site_path( + "packages", + "test-package", + "test_module_for_package", + "doctype", + "test_doctype_for_package", + ) + ) + ) + with open( + xhiveframework.get_site_path( + "packages", + "test-package", + "test_module_for_package", + "doctype", + "test_doctype_for_package", + "test_doctype_for_package.json", + ) + ) as f: + doctype = json.loads(f.read()) + self.assertEqual(doctype["doctype"], "DocType") + self.assertEqual(doctype["name"], "Test DocType for Package") + self.assertEqual(doctype["fields"][0]["fieldname"], "test_field") + + +def make_test_package(): + if not xhiveframework.db.exists("Package", "Test Package"): + xhiveframework.get_doc( + dict(doctype="Package", name="Test Package", package_name="test-package", readme="# Test Package") + ).insert() + + +def make_test_module(): + if not xhiveframework.db.exists("Module Def", "Test Module for Package"): + xhiveframework.get_doc( + dict( + doctype="Module Def", + module_name="Test Module for Package", + custom=1, + app_name="xhiveframework", + package="Test Package", + ) + ).insert() + + +def make_test_doctype(): + if not xhiveframework.db.exists("DocType", "Test DocType for Package"): + xhiveframework.get_doc( + dict( + doctype="DocType", + name="Test DocType for Package", + custom=1, + module="Test Module for Package", + autoname="Prompt", + fields=[dict(fieldname="test_field", fieldtype="Data", label="Test Field")], + ) + ).insert() + + +def make_test_server_script(): + if not xhiveframework.db.exists("Server Script", "Test Script for Package"): + xhiveframework.get_doc( + dict( + doctype="Server Script", + name="Test Script for Package", + module="Test Module for Package", + script_type="DocType Event", + reference_doctype="Test DocType for Package", + doctype_event="Before Save", + script='xhiveframework.msgprint("Test")', + ) + ).insert() + + +def make_test_web_page(): + if not xhiveframework.db.exists("Web Page", "test-web-page-for-package"): + xhiveframework.get_doc( + dict( + doctype="Web Page", + module="Test Module for Package", + main_section="Some content", + published=1, + title="Test Web Page for Package", + ) + ).insert() diff --git a/xhiveframework/core/doctype/package_import/__init__.py b/xhiveframework/core/doctype/package_import/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/package_import/package_import.js b/xhiveframework/core/doctype/package_import/package_import.js new file mode 100644 index 0000000..11be35d --- /dev/null +++ b/xhiveframework/core/doctype/package_import/package_import.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Package Import", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/package_import/package_import.json b/xhiveframework/core/doctype/package_import/package_import.json new file mode 100644 index 0000000..f3c6168 --- /dev/null +++ b/xhiveframework/core/doctype/package_import/package_import.json @@ -0,0 +1,65 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "format:Package Import at {creation}", + "creation": "2021-09-05 16:36:46.680094", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "attach_package", + "activate", + "force", + "log" + ], + "fields": [ + { + "fieldname": "attach_package", + "fieldtype": "Attach", + "label": "Attach Package" + }, + { + "default": "0", + "fieldname": "activate", + "fieldtype": "Check", + "label": "Activate" + }, + { + "fieldname": "log", + "fieldtype": "Code", + "label": "Log", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "force", + "fieldtype": "Check", + "label": "Force" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-09-05 21:30:04.796090", + "modified_by": "Administrator", + "module": "Core", + "name": "Package Import", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/package_import/package_import.py b/xhiveframework/core/doctype/package_import/package_import.py new file mode 100644 index 0000000..24d4be4 --- /dev/null +++ b/xhiveframework/core/doctype/package_import/package_import.py @@ -0,0 +1,80 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import json +import os +import subprocess + +import xhiveframework +from xhiveframework.desk.form.load import get_attachments +from xhiveframework.model.document import Document +from xhiveframework.model.sync import get_doc_files +from xhiveframework.modules.import_file import import_doc, import_file_by_path +from xhiveframework.utils import get_files_path + + +class PackageImport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + activate: DF.Check + attach_package: DF.Attach | None + force: DF.Check + log: DF.Code | None + + # end: auto-generated types + def validate(self): + if self.activate: + self.import_package() + + def import_package(self): + attachment = get_attachments(self.doctype, self.name) + + if not attachment: + xhiveframework.throw(xhiveframework._("Please attach the package")) + + attachment = attachment[0] + + # get package_name from file (package_name-0.0.0.tar.gz) + package_name = attachment.file_name.split(".", 1)[0].rsplit("-", 1)[0] + if not os.path.exists(xhiveframework.get_site_path("packages")): + os.makedirs(xhiveframework.get_site_path("packages")) + + # extract + subprocess.check_output( + [ + "tar", + "xzf", + get_files_path(attachment.file_name, is_private=attachment.is_private), + "-C", + xhiveframework.get_site_path("packages"), + ] + ) + + package_path = xhiveframework.get_site_path("packages", package_name) + + # import Package + with open(os.path.join(package_path, package_name + ".json")) as packagefile: + doc_dict = json.loads(packagefile.read()) + + xhiveframework.flags.package = import_doc(doc_dict) + + # collect modules + files = [] + log = [] + for module in os.listdir(package_path): + module_path = os.path.join(package_path, module) + if os.path.isdir(module_path): + files = get_doc_files(files, module_path) + + # import files + for file in files: + import_file_by_path(file, force=self.force, ignore_version=True) + log.append(f"Imported {file}") + + self.log = "\n".join(log) diff --git a/xhiveframework/core/doctype/package_import/test_package_import.py b/xhiveframework/core/doctype/package_import/test_package_import.py new file mode 100644 index 0000000..28b26f3 --- /dev/null +++ b/xhiveframework/core/doctype/package_import/test_package_import.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPackageImport(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/package_release/__init__.py b/xhiveframework/core/doctype/package_release/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/package_release/package_release.js b/xhiveframework/core/doctype/package_release/package_release.js new file mode 100644 index 0000000..2ea4cc0 --- /dev/null +++ b/xhiveframework/core/doctype/package_release/package_release.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Package Release", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/package_release/package_release.json b/xhiveframework/core/doctype/package_release/package_release.json new file mode 100644 index 0000000..b651d69 --- /dev/null +++ b/xhiveframework/core/doctype/package_release/package_release.json @@ -0,0 +1,95 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2021-09-05 12:59:01.932327", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "package", + "publish", + "path", + "column_break_3", + "major", + "minor", + "patch", + "section_break_7", + "release_notes" + ], + "fields": [ + { + "fieldname": "package", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Package", + "options": "Package", + "reqd": 1 + }, + { + "fieldname": "major", + "fieldtype": "Int", + "label": "Major" + }, + { + "fieldname": "minor", + "fieldtype": "Int", + "label": "Minor" + }, + { + "fieldname": "patch", + "fieldtype": "Int", + "label": "Patch", + "no_copy": 1 + }, + { + "fieldname": "path", + "fieldtype": "Small Text", + "label": "Path", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_7", + "fieldtype": "Section Break" + }, + { + "fieldname": "release_notes", + "fieldtype": "Markdown Editor", + "label": "Release Notes" + }, + { + "default": "0", + "fieldname": "publish", + "fieldtype": "Check", + "label": "Publish" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-09-05 16:04:32.860988", + "modified_by": "Administrator", + "module": "Core", + "name": "Package Release", + "naming_rule": "By script", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/package_release/package_release.py b/xhiveframework/core/doctype/package_release/package_release.py new file mode 100644 index 0000000..a956541 --- /dev/null +++ b/xhiveframework/core/doctype/package_release/package_release.py @@ -0,0 +1,128 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import os +import subprocess + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.modules.export_file import export_doc +from xhiveframework.query_builder.functions import Max + + +class PackageRelease(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + major: DF.Int + minor: DF.Int + package: DF.Link + patch: DF.Int + path: DF.SmallText | None + publish: DF.Check + release_notes: DF.MarkdownEditor | None + + # end: auto-generated types + def set_version(self): + # set the next patch release by default + doctype = xhiveframework.qb.DocType("Package Release") + if not self.major: + self.major = ( + xhiveframework.qb.from_(doctype) + .where(doctype.package == self.package) + .select(Max(doctype.minor)) + .run()[0][0] + or 0 + ) + + if not self.minor: + self.minor = ( + xhiveframework.qb.from_(doctype) + .where(doctype.package == self.package) + .select(Max("minor")) + .run()[0][0] + or 0 + ) + if not self.patch: + value = ( + xhiveframework.qb.from_(doctype) + .where(doctype.package == self.package) + .select(Max("patch")) + .run()[0][0] + or 0 + ) + self.patch = value + 1 + + def autoname(self): + self.set_version() + self.name = "{}-{}.{}.{}".format( + xhiveframework.db.get_value("Package", self.package, "package_name"), self.major, self.minor, self.patch + ) + + def validate(self): + if self.publish: + self.export_files() + + def export_files(self): + """Export all the documents in this package to site/packages folder""" + package = xhiveframework.get_doc("Package", self.package) + + self.export_modules() + self.export_package_files(package) + self.make_tarfile(package) + + def export_modules(self): + for m in xhiveframework.get_all("Module Def", dict(package=self.package)): + module = xhiveframework.get_doc("Module Def", m.name) + for l in module.meta.links: + if l.link_doctype == "Module Def": + continue + # all documents of the type in the module + for d in xhiveframework.get_all(l.link_doctype, dict(module=m.name)): + export_doc(xhiveframework.get_doc(l.link_doctype, d.name)) + + def export_package_files(self, package): + # write readme + with open(xhiveframework.get_site_path("packages", package.package_name, "README.md"), "w") as readme: + readme.write(package.readme) + + # write license + if package.license: + with open(xhiveframework.get_site_path("packages", package.package_name, "LICENSE.md"), "w") as license: + license.write(package.license) + + # write package.json as `xhiveframework_package.json` + with open( + xhiveframework.get_site_path("packages", package.package_name, package.package_name + ".json"), "w" + ) as packagefile: + packagefile.write(xhiveframework.as_json(package.as_dict(no_nulls=True))) + + def make_tarfile(self, package): + # make tarfile + filename = f"{self.name}.tar.gz" + subprocess.check_output( + ["tar", "czf", filename, package.package_name], cwd=xhiveframework.get_site_path("packages") + ) + + # move file + subprocess.check_output( + ["mv", xhiveframework.get_site_path("packages", filename), xhiveframework.get_site_path("public", "files")] + ) + + # make attachment + file = xhiveframework.get_doc( + dict( + doctype="File", + file_url="/" + os.path.join("files", filename), + attached_to_doctype=self.doctype, + attached_to_name=self.name, + ) + ) + + file.flags.ignore_duplicate_entry_error = True + file.insert() diff --git a/xhiveframework/core/doctype/package_release/test_package_release.py b/xhiveframework/core/doctype/package_release/test_package_release.py new file mode 100644 index 0000000..3668ceb --- /dev/null +++ b/xhiveframework/core/doctype/package_release/test_package_release.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPackageRelease(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/page/README.md b/xhiveframework/core/doctype/page/README.md new file mode 100644 index 0000000..0a06292 --- /dev/null +++ b/xhiveframework/core/doctype/page/README.md @@ -0,0 +1 @@ +A page (view) in the application. A page is made to define custom user interfaces and contains server side and client side code. Standard events are attached to the page and called when the page is loaded, shown etc. \ No newline at end of file diff --git a/xhiveframework/core/doctype/page/__init__.py b/xhiveframework/core/doctype/page/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/page/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/page/page.js b/xhiveframework/core/doctype/page/page.js new file mode 100644 index 0000000..cad514a --- /dev/null +++ b/xhiveframework/core/doctype/page/page.js @@ -0,0 +1,16 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Page", { + refresh: function (frm) { + if (!xhiveframework.boot.developer_mode && xhiveframework.session.user != "Administrator") { + // make the document read-only + frm.set_read_only(); + } + if (!frm.is_new() && !frm.doc.istable) { + frm.add_custom_button(__("Go to {0} Page", [frm.doc.title || frm.doc.name]), () => { + xhiveframework.set_route(frm.doc.name); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/page/page.json b/xhiveframework/core/doctype/page/page.json new file mode 100644 index 0000000..2105bc7 --- /dev/null +++ b/xhiveframework/core/doctype/page/page.json @@ -0,0 +1,133 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:page_name", + "creation": "2012-12-20 17:16:49", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "system_page", + "page_html", + "page_name", + "title", + "icon", + "column_break0", + "module", + "restrict_to_domain", + "standard", + "section_break0", + "roles" + ], + "fields": [ + { + "default": "0", + "fieldname": "system_page", + "fieldtype": "Check", + "label": "System Page" + }, + { + "fieldname": "page_html", + "fieldtype": "Section Break", + "label": "Page HTML", + "oldfieldtype": "Section Break" + }, + { + "fieldname": "page_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Page Name", + "oldfieldname": "page_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "no_copy": 1 + }, + { + "fieldname": "icon", + "fieldtype": "Data", + "in_list_view": 1, + "label": "icon" + }, + { + "fieldname": "column_break0", + "fieldtype": "Column Break" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "in_standard_filter": 1, + "label": "Module", + "oldfieldname": "module", + "oldfieldtype": "Select", + "options": "Module Def", + "reqd": 1 + }, + { + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict To Domain", + "options": "Domain" + }, + { + "fieldname": "standard", + "fieldtype": "Select", + "label": "Standard", + "oldfieldname": "standard", + "oldfieldtype": "Select", + "options": "Yes\nNo", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "section_break0", + "fieldtype": "Section Break" + }, + { + "depends_on": "eval:doc.standard == 'Yes'", + "fieldname": "roles", + "fieldtype": "Table", + "label": "Roles", + "oldfieldname": "roles", + "oldfieldtype": "Table", + "options": "Has Role" + } + ], + "icon": "fa fa-file", + "idx": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Core", + "name": "Page", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "Administrator", + "share": 1, + "write": 1 + }, + { + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/page/page.py b/xhiveframework/core/doctype/page/page.py new file mode 100644 index 0000000..f83e18b --- /dev/null +++ b/xhiveframework/core/doctype/page/page.py @@ -0,0 +1,190 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import os + +import xhiveframework +from xhiveframework import _, conf, safe_decode +from xhiveframework.build import html_to_js_template +from xhiveframework.core.doctype.custom_role.custom_role import get_custom_allowed_roles +from xhiveframework.desk.form.meta import get_code_files_via_hooks, get_js +from xhiveframework.desk.utils import validate_route_conflict +from xhiveframework.model.document import Document +from xhiveframework.model.utils import render_include + + +class Page(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.types import DF + + icon: DF.Data | None + module: DF.Link + page_name: DF.Data + restrict_to_domain: DF.Link | None + roles: DF.Table[HasRole] + standard: DF.Literal["Yes", "No"] + system_page: DF.Check + title: DF.Data | None + + # end: auto-generated types + def autoname(self): + """ + Creates a url friendly name for this page. + Will restrict the name to 30 characters, if there exists a similar name, + it will add name-1, name-2 etc. + """ + from xhiveframework.utils import cint + + if (self.name and self.name.startswith("New Page")) or not self.name: + self.name = self.page_name.lower().replace('"', "").replace("'", "").replace(" ", "-")[:20] + if xhiveframework.db.exists("Page", self.name): + cnt = xhiveframework.db.sql( + """select name from tabPage + where name like "%s-%%" order by name desc limit 1""" + % self.name + ) + if cnt: + cnt = cint(cnt[0][0].split("-")[-1]) + 1 + else: + cnt = 1 + self.name += "-" + str(cnt) + + def validate(self): + validate_route_conflict(self.doctype, self.name) + + if self.is_new() and not getattr(conf, "developer_mode", 0): + xhiveframework.throw(_("Not in Developer Mode")) + + # setting ignore_permissions via update_setup_wizard_access (setup_wizard.py) + if xhiveframework.session.user != "Administrator" and not self.flags.ignore_permissions: + xhiveframework.throw(_("Only Administrator can edit")) + + # export + def on_update(self): + """ + Writes the .json for this page and if write_content is checked, + it will write out a .html file + """ + if self.flags.do_not_update_json: + return + + from xhiveframework.core.doctype.doctype.doctype import make_module_and_roles + + make_module_and_roles(self, "roles") + + from xhiveframework.modules.utils import export_module_json + + path = export_module_json(self, self.standard == "Yes", self.module) + + if path: + # js + if not os.path.exists(path + ".js"): + with open(path + ".js", "w") as f: + f.write( + f"""xhiveframework.pages['{self.name}'].on_page_load = function(wrapper) {{ + var page = xhiveframework.ui.make_app_page({{ + parent: wrapper, + title: '{self.title}', + single_column: true + }}); +}}""" + ) + + def as_dict(self, no_nulls=False): + d = super().as_dict(no_nulls=no_nulls) + for key in ("script", "style", "content"): + d[key] = self.get(key) + return d + + def on_trash(self): + delete_custom_role("page", self.name) + + def is_permitted(self): + """Returns true if Has Role is not set or the user is allowed.""" + from xhiveframework.utils import has_common + + allowed = [d.role for d in xhiveframework.get_all("Has Role", fields=["role"], filters={"parent": self.name})] + + custom_roles = get_custom_allowed_roles("page", self.name) + allowed.extend(custom_roles) + + if not allowed: + return True + + roles = xhiveframework.get_roles() + + if has_common(roles, allowed): + return True + + def load_assets(self): + import os + + from xhiveframework.modules import get_module_path, scrub + + self.script = "" + + page_name = scrub(self.name) + + path = os.path.join(get_module_path(self.module), "page", page_name) + + # script + fpath = os.path.join(path, page_name + ".js") + if os.path.exists(fpath): + with open(fpath) as f: + self.script = render_include(f.read()) + self.script += f"\n\n//# sourceURL={page_name}.js" + + # css + fpath = os.path.join(path, page_name + ".css") + if os.path.exists(fpath): + with open(fpath) as f: + self.style = safe_decode(f.read()) + + # html as js template + for fname in os.listdir(path): + if fname.endswith(".html"): + with open(os.path.join(path, fname)) as f: + template = f.read() + if "" in template: + context = xhiveframework._dict({}) + try: + out = xhiveframework.get_attr( + "{app}.{module}.page.{page}.{page}.get_context".format( + app=xhiveframework.local.module_app[scrub(self.module)], + module=scrub(self.module), + page=page_name, + ) + )(context) + + if out: + context = out + except (AttributeError, ImportError): + pass + + template = xhiveframework.render_template(template, context) + self.script = html_to_js_template(fname, template) + self.script + + # flag for not caching this page + self._dynamic_page = True + + if xhiveframework.lang != "en": + from xhiveframework.translate import get_lang_js + + self.script += get_lang_js("page", self.name) + + for path in get_code_files_via_hooks("page_js", self.name): + js = get_js(path) + if js: + self.script += "\n\n" + js + + +def delete_custom_role(field, docname): + name = xhiveframework.db.get_value("Custom Role", {field: docname}, "name") + if name: + xhiveframework.delete_doc("Custom Role", name) diff --git a/xhiveframework/core/doctype/page/patches/drop_unused_pages.py b/xhiveframework/core/doctype/page/patches/drop_unused_pages.py new file mode 100644 index 0000000..b084194 --- /dev/null +++ b/xhiveframework/core/doctype/page/patches/drop_unused_pages.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + for name in ("desktop", "space"): + xhiveframework.delete_doc("Page", name) diff --git a/xhiveframework/core/doctype/page/test_page.py b/xhiveframework/core/doctype/page/test_page.py new file mode 100644 index 0000000..277acb3 --- /dev/null +++ b/xhiveframework/core/doctype/page/test_page.py @@ -0,0 +1,14 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Page") + + +class TestPage(XhiveFrameworkTestCase): + def test_naming(self): + self.assertRaises( + xhiveframework.NameError, + xhiveframework.get_doc(dict(doctype="Page", page_name="DocType", module="Core")).insert, + ) diff --git a/xhiveframework/core/doctype/page/test_records.json b/xhiveframework/core/doctype/page/test_records.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/xhiveframework/core/doctype/page/test_records.json @@ -0,0 +1 @@ +[] diff --git a/xhiveframework/core/doctype/patch_log/README.md b/xhiveframework/core/doctype/patch_log/README.md new file mode 100644 index 0000000..080490e --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/README.md @@ -0,0 +1 @@ +Log of patch executed. Used by `patch_handler` to check if a particular patch is executed or not. \ No newline at end of file diff --git a/xhiveframework/core/doctype/patch_log/__init__.py b/xhiveframework/core/doctype/patch_log/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/patch_log/patch_log.js b/xhiveframework/core/doctype/patch_log/patch_log.js new file mode 100644 index 0000000..cbff884 --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/patch_log.js @@ -0,0 +1,8 @@ +// Copyright (c) 2016, XhiveFramework Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Patch Log", { + refresh: function (frm) { + frm.disable_save(); + }, +}); diff --git a/xhiveframework/core/doctype/patch_log/patch_log.json b/xhiveframework/core/doctype/patch_log/patch_log.json new file mode 100644 index 0000000..6be3ce0 --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/patch_log.json @@ -0,0 +1,69 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-01-17 11:36:45", + "description": "List of patches executed", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "patch", + "skipped", + "traceback" + ], + "fields": [ + { + "fieldname": "patch", + "fieldtype": "Code", + "label": "Patch", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "skipped", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Skipped", + "read_only": 1 + }, + { + "depends_on": "eval:doc.skipped == 1", + "fieldname": "traceback", + "fieldtype": "Code", + "label": "Traceback", + "read_only": 1 + } + ], + "icon": "fa fa-cog", + "idx": 1, + "links": [], + "modified": "2023-06-07 00:00:01.369265", + "modified_by": "Administrator", + "module": "Core", + "name": "Patch Log", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator" + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager" + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "patch", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/patch_log/patch_log.py b/xhiveframework/core/doctype/patch_log/patch_log.py new file mode 100644 index 0000000..10c25ff --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/patch_log.py @@ -0,0 +1,27 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class PatchLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + patch: DF.Code | None + skipped: DF.Check + traceback: DF.Code | None + # end: auto-generated types + pass + + +def before_migrate(): + xhiveframework.reload_doc("core", "doctype", "patch_log") diff --git a/xhiveframework/core/doctype/patch_log/test_patch_log.py b/xhiveframework/core/doctype/patch_log/test_patch_log.py new file mode 100644 index 0000000..c2f2636 --- /dev/null +++ b/xhiveframework/core/doctype/patch_log/test_patch_log.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Patch Log') + + +class TestPatchLog(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/permission_inspector/__init__.py b/xhiveframework/core/doctype/permission_inspector/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/permission_inspector/permission_inspector.js b/xhiveframework/core/doctype/permission_inspector/permission_inspector.js new file mode 100644 index 0000000..18c02b3 --- /dev/null +++ b/xhiveframework/core/doctype/permission_inspector/permission_inspector.js @@ -0,0 +1,24 @@ +// Copyright (c) 2024, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +const call_debug = (frm) => { + frm.trigger("debug"); +}; + +xhiveframework.ui.form.on("Permission Inspector", { + refresh(frm) { + frm.disable_save(); + }, + docname: call_debug, + ref_doctype(frm) { + frm.doc.docname = ""; // Usually doctype change invalidates docname + call_debug(frm); + }, + user: call_debug, + permission_type: call_debug, + debug(frm) { + if (frm.doc.ref_doctype && frm.doc.user) { + frm.call("debug"); + } + }, +}); diff --git a/xhiveframework/core/doctype/permission_inspector/permission_inspector.json b/xhiveframework/core/doctype/permission_inspector/permission_inspector.json new file mode 100644 index 0000000..466f7e6 --- /dev/null +++ b/xhiveframework/core/doctype/permission_inspector/permission_inspector.json @@ -0,0 +1,90 @@ +{ + "actions": [], + "allow_rename": 1, + "beta": 1, + "creation": "2024-01-03 17:43:27.257317", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "ref_doctype", + "column_break_mcqo", + "docname", + "column_break_xbrd", + "user", + "column_break_nvaa", + "permission_type", + "section_break_hkjp", + "output" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "docname", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Document", + "options": "ref_doctype" + }, + { + "fieldname": "column_break_mcqo", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_xbrd", + "fieldtype": "Column Break" + }, + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "reqd": 1 + }, + { + "fieldname": "section_break_hkjp", + "fieldtype": "Section Break" + }, + { + "fieldname": "output", + "fieldtype": "Code", + "label": "Output", + "read_only": 1 + }, + { + "fieldname": "column_break_nvaa", + "fieldtype": "Column Break" + }, + { + "fieldname": "permission_type", + "fieldtype": "Select", + "label": "Permission Type", + "options": "read\nwrite\ncreate\ndelete\nsubmit\ncancel\nselect\namend\nprint\nemail\nreport\nimport\nexport\nshare" + } + ], + "index_web_pages_for_search": 1, + "is_virtual": 1, + "issingle": 1, + "links": [], + "modified": "2024-01-10 14:17:49.722593", + "modified_by": "Administrator", + "module": "Core", + "name": "Permission Inspector", + "owner": "Administrator", + "permissions": [ + { + "read": 1, + "role": "System Manager", + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/permission_inspector/permission_inspector.py b/xhiveframework/core/doctype/permission_inspector/permission_inspector.py new file mode 100644 index 0000000..0cc7546 --- /dev/null +++ b/xhiveframework/core/doctype/permission_inspector/permission_inspector.py @@ -0,0 +1,75 @@ +# Copyright (c) 2024, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.permissions import _pop_debug_log, has_permission + + +class PermissionInspector(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + docname: DF.DynamicLink | None + output: DF.Code | None + permission_type: DF.Literal[ + "read", + "write", + "create", + "delete", + "submit", + "cancel", + "select", + "amend", + "print", + "email", + "report", + "import", + "export", + "share", + ] + ref_doctype: DF.Link + user: DF.Link + # end: auto-generated types + + @xhiveframework.whitelist() + def debug(self): + if not (self.ref_doctype and self.user): + return + + result = has_permission( + self.ref_doctype, ptype=self.permission_type, doc=self.docname, user=self.user, debug=True + ) + + self.output = "\n==============================\n".join(_pop_debug_log()) + self.output += "\n\n" + f"Ouput of has_permission: {result}" + + # None of these apply, overriden for sanity. + def load_from_db(self): + super(Document, self).__init__({"modified": None, "permission_type": "read"}) + + def db_insert(self, *args, **kwargs): + ... + + def db_update(self): + ... + + @staticmethod + def get_list(args): + ... + + @staticmethod + def get_count(args): + ... + + @staticmethod + def get_stats(args): + ... + + def delete(self): + ... diff --git a/xhiveframework/core/doctype/permission_inspector/test_permission_inspector.py b/xhiveframework/core/doctype/permission_inspector/test_permission_inspector.py new file mode 100644 index 0000000..4db77c8 --- /dev/null +++ b/xhiveframework/core/doctype/permission_inspector/test_permission_inspector.py @@ -0,0 +1,9 @@ +# Copyright (c) 2024, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPermissionInspector(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/prepared_report/__init__.py b/xhiveframework/core/doctype/prepared_report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/prepared_report/prepared_report.js b/xhiveframework/core/doctype/prepared_report/prepared_report.js new file mode 100644 index 0000000..8858151 --- /dev/null +++ b/xhiveframework/core/doctype/prepared_report/prepared_report.js @@ -0,0 +1,55 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Prepared Report", { + render_filter_values: function (frm, filters) { + var wrapper = $(frm.fields_dict["filter_values"].wrapper).empty(); + + let filter_table = $(` + + + + + + + +
    ${__("Filter")}${__("Value")}
    `); + + Object.keys(filters).forEach((key) => { + const filter_row = $(` + ${xhiveframework.model.unscrub(key)} + ${filters[key]} + `); + filter_table.find("tbody").append(filter_row); + }); + + wrapper.append(filter_table); + }, + + refresh: function (frm) { + frm.disable_save(); + + const filters = JSON.parse(frm.doc.filters); + if (!$.isEmptyObject(filters)) { + frm.toggle_display(["filter_values"], 1); + frm.events.render_filter_values(frm, filters); + } + + // always keep report_name hidden - we do this as we can't set mandatory and hidden + // property on a docfield at the same time + frm.toggle_display(["report_name"], 0); + + if (frm.doc.status == "Completed") { + frm.page.set_primary_action(__("Show Report"), () => { + xhiveframework.route_options = filters; + xhiveframework.set_route( + "query-report", + frm.doc.report_name, + xhiveframework.utils.make_query_string({ + prepared_report_name: frm.doc.name, + }) + ); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/prepared_report/prepared_report.json b/xhiveframework/core/doctype/prepared_report/prepared_report.json new file mode 100644 index 0000000..ec86237 --- /dev/null +++ b/xhiveframework/core/doctype/prepared_report/prepared_report.json @@ -0,0 +1,154 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2018-06-25 18:39:11.152960", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "status", + "report_name", + "queued_by", + "job_id", + "column_break_4", + "queued_at", + "report_end_time", + "section_break_7", + "error_message", + "filters_sb", + "filters", + "filter_values" + ], + "fields": [ + { + "fieldname": "report_name", + "fieldtype": "Data", + "label": "Report Name", + "read_only": 1, + "reqd": 1, + "search_index": 1 + }, + { + "default": "Queued", + "fieldname": "status", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Error\nQueued\nCompleted\nStarted", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "report_end_time", + "fieldtype": "Datetime", + "label": "Finished At", + "read_only": 1 + }, + { + "depends_on": "eval:doc.status == 'Error'", + "fieldname": "section_break_7", + "fieldtype": "Section Break" + }, + { + "fieldname": "error_message", + "fieldtype": "Text", + "label": "Error Message", + "no_copy": 1, + "print_hide": 1, + "read_only": 1 + }, + { + "fieldname": "filters_sb", + "fieldtype": "Section Break", + "label": "Filters" + }, + { + "fieldname": "filters", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Filters", + "read_only": 1 + }, + { + "fieldname": "filter_values", + "fieldtype": "HTML", + "hidden": 1, + "label": "Filter Values" + }, + { + "fieldname": "job_id", + "fieldtype": "Data", + "label": "Job ID", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "queued_by", + "fieldtype": "Data", + "is_virtual": 1, + "label": "Queued By", + "read_only": 1 + }, + { + "fieldname": "queued_at", + "fieldtype": "Datetime", + "is_virtual": 1, + "label": "Queued At", + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2024-03-07 12:01:58.209879", + "modified_by": "Administrator", + "module": "Core", + "name": "Prepared Report", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Prepared Report User", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [ + { + "color": "Blue", + "title": "Queued" + }, + { + "color": "Red", + "title": "Error" + }, + { + "color": "Green", + "title": "Completed" + } + ], + "title_field": "report_name" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/prepared_report/prepared_report.py b/xhiveframework/core/doctype/prepared_report/prepared_report.py new file mode 100644 index 0000000..046738f --- /dev/null +++ b/xhiveframework/core/doctype/prepared_report/prepared_report.py @@ -0,0 +1,284 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +import gzip +import json +from contextlib import suppress +from typing import Any + +from rq import get_current_job + +import xhiveframework +from xhiveframework.desk.form.load import get_attachments +from xhiveframework.desk.query_report import generate_report_result +from xhiveframework.model.document import Document +from xhiveframework.monitor import add_data_to_monitor +from xhiveframework.utils import add_to_date, now +from xhiveframework.utils.background_jobs import enqueue + +# If prepared report runs for longer than this time it's automatically considered as failed +FAILURE_THRESHOLD = 60 * 60 +REPORT_TIMEOUT = 25 * 60 + + +class PreparedReport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + error_message: DF.Text | None + filters: DF.SmallText | None + job_id: DF.Data | None + queued_at: DF.Datetime | None + queued_by: DF.Data | None + report_end_time: DF.Datetime | None + report_name: DF.Data + status: DF.Literal["Error", "Queued", "Completed", "Started"] + + # end: auto-generated types + @property + def queued_by(self): + return self.owner + + @property + def queued_at(self): + return self.creation + + @staticmethod + def clear_old_logs(days=30): + prepared_reports_to_delete = xhiveframework.get_all( + "Prepared Report", + filters={"modified": ["<", xhiveframework.utils.add_days(xhiveframework.utils.now(), -days)]}, + ) + + for batch in xhiveframework.utils.create_batch(prepared_reports_to_delete, 100): + enqueue(method=delete_prepared_reports, reports=batch) + + def before_insert(self): + self.status = "Queued" + + def on_trash(self): + """Remove pending job from queue, if already running then kill the job.""" + if self.status not in ("Started", "Queued"): + return + + with suppress(Exception): + job = xhiveframework.get_doc("RQ Job", self.job_id) + job.stop_job() if self.status == "Started" else job.delete() + + def after_insert(self): + enqueue( + generate_report, + queue="long", + prepared_report=self.name, + timeout=REPORT_TIMEOUT, + enqueue_after_commit=True, + ) + + def get_prepared_data(self, with_file_name=False): + if attachments := get_attachments(self.doctype, self.name): + attachment = attachments[0] + attached_file = xhiveframework.get_doc("File", attachment.name) + + if with_file_name: + return (gzip.decompress(attached_file.get_content()), attachment.file_name) + return gzip.decompress(attached_file.get_content()) + + +def generate_report(prepared_report): + update_job_id(prepared_report) + + instance: PreparedReport = xhiveframework.get_doc("Prepared Report", prepared_report) + report = xhiveframework.get_doc("Report", instance.report_name) + + add_data_to_monitor(report=instance.report_name) + + try: + report.custom_columns = [] + + if report.report_type == "Custom Report": + custom_report_doc = report + reference_report = custom_report_doc.reference_report + report = xhiveframework.get_doc("Report", reference_report) + if custom_report_doc.json: + data = json.loads(custom_report_doc.json) + if data: + report.custom_columns = data["columns"] + + result = generate_report_result(report=report, filters=instance.filters, user=instance.owner) + create_json_gz_file(result, instance.doctype, instance.name, instance.report_name) + + instance.status = "Completed" + except Exception: + instance.status = "Error" + instance.error_message = xhiveframework.get_traceback(with_context=True) + + instance.report_end_time = xhiveframework.utils.now() + instance.save(ignore_permissions=True) + + xhiveframework.publish_realtime( + "report_generated", + {"report_name": instance.report_name, "name": instance.name}, + user=xhiveframework.session.user, + ) + + +def update_job_id(prepared_report): + job = get_current_job() + + xhiveframework.db.set_value( + "Prepared Report", + prepared_report, + { + "job_id": job and job.id, + "status": "Started", + }, + ) + + xhiveframework.db.commit() + + +@xhiveframework.whitelist() +def make_prepared_report(report_name, filters=None): + """run reports in background""" + prepared_report = xhiveframework.get_doc( + { + "doctype": "Prepared Report", + "report_name": report_name, + "filters": process_filters_for_prepared_report(filters), + } + ).insert(ignore_permissions=True) + + return {"name": prepared_report.name} + + +def process_filters_for_prepared_report(filters: dict[str, Any] | str) -> str: + if isinstance(filters, str): + filters = json.loads(filters) + + # This looks like an insanity but, without this it'd be very hard to find Prepared Reports matching given condition + # We're ensuring that spacing is consistent. e.g. JS seems to put no spaces after ":", Python on the other hand does. + # We are also ensuring that order of keys is same so generated JSON string will be identical too. + # PS: xhiveframework.as_json sorts keys + return xhiveframework.as_json(filters, indent=None, separators=(",", ":")) + + +@xhiveframework.whitelist() +def get_reports_in_queued_state(report_name, filters): + return xhiveframework.get_all( + "Prepared Report", + filters={ + "report_name": report_name, + "filters": process_filters_for_prepared_report(filters), + "status": ("in", ("Queued", "Started")), + "owner": xhiveframework.session.user, + }, + ) + + +def get_completed_prepared_report(filters, user, report_name): + return xhiveframework.db.get_value( + "Prepared Report", + filters={ + "status": "Completed", + "filters": process_filters_for_prepared_report(filters), + "owner": user, + "report_name": report_name, + }, + ) + + +def expire_stalled_report(): + xhiveframework.db.set_value( + "Prepared Report", + { + "status": "Started", + "modified": ("<", add_to_date(now(), seconds=-FAILURE_THRESHOLD, as_datetime=True)), + }, + { + "status": "Failed", + "error_message": xhiveframework._("Report timed out."), + }, + update_modified=False, + ) + + +@xhiveframework.whitelist() +def delete_prepared_reports(reports): + reports = xhiveframework.parse_json(reports) + for report in reports: + prepared_report = xhiveframework.get_doc("Prepared Report", report["name"]) + if prepared_report.has_permission(): + prepared_report.delete(ignore_permissions=True, delete_permanently=True) + + +def create_json_gz_file(data, dt, dn, report_name): + # Storing data in CSV file causes information loss + # Reports like P&L Statement were completely unsuable because of this + json_filename = "{}_{}.json.gz".format( + xhiveframework.scrub(report_name), xhiveframework.utils.data.format_datetime(xhiveframework.utils.now(), "Y-m-d-H-M") + ) + encoded_content = xhiveframework.safe_encode(xhiveframework.as_json(data, indent=None, separators=(",", ":"))) + compressed_content = gzip.compress(encoded_content) + + # Call save() file function to upload and attach the file + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": json_filename, + "attached_to_doctype": dt, + "attached_to_name": dn, + "content": compressed_content, + "is_private": 1, + } + ) + _file.save(ignore_permissions=True) + + +@xhiveframework.whitelist() +def download_attachment(dn): + pr = xhiveframework.get_doc("Prepared Report", dn) + if not pr.has_permission("read"): + xhiveframework.throw(xhiveframework._("Cannot Download Report due to insufficient permissions")) + + data, file_name = pr.get_prepared_data(with_file_name=True) + xhiveframework.local.response.filename = file_name[:-3] + xhiveframework.local.response.filecontent = data + xhiveframework.local.response.type = "binary" + + +def get_permission_query_condition(user): + if not user: + user = xhiveframework.session.user + if user == "Administrator": + return None + + from xhiveframework.utils.user import UserPermissions + + user = UserPermissions(user) + + if "System Manager" in user.roles: + return None + + reports = [xhiveframework.db.escape(report) for report in user.get_all_reports().keys()] + + return """`tabPrepared Report`.report_name in ({reports})""".format(reports=",".join(reports)) + + +def has_permission(doc, user): + if not user: + user = xhiveframework.session.user + if user == "Administrator": + return True + + from xhiveframework.utils.user import UserPermissions + + user = UserPermissions(user) + + if "System Manager" in user.roles: + return True + + return doc.report_name in user.get_all_reports().keys() diff --git a/xhiveframework/core/doctype/prepared_report/prepared_report_list.js b/xhiveframework/core/doctype/prepared_report/prepared_report_list.js new file mode 100644 index 0000000..725f14d --- /dev/null +++ b/xhiveframework/core/doctype/prepared_report/prepared_report_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Prepared Report"] = { + onload: function (list_view) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(list_view.doctype); + }); + }, +}; diff --git a/xhiveframework/core/doctype/prepared_report/test_prepared_report.py b/xhiveframework/core/doctype/prepared_report/test_prepared_report.py new file mode 100644 index 0000000..9216716 --- /dev/null +++ b/xhiveframework/core/doctype/prepared_report/test_prepared_report.py @@ -0,0 +1,92 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import json +import time +from contextlib import contextmanager + +import xhiveframework +from xhiveframework.desk.query_report import generate_report_result, get_report_doc +from xhiveframework.query_builder.utils import db_type_is +from xhiveframework.tests.test_query_builder import run_only_if +from xhiveframework.tests.utils import XhiveFrameworkTestCase, timeout + + +class TestPreparedReport(XhiveFrameworkTestCase): + @classmethod + def tearDownClass(cls): + for r in xhiveframework.get_all("Prepared Report", pluck="name"): + xhiveframework.delete_doc("Prepared Report", r, force=True, delete_permanently=True) + + xhiveframework.db.commit() + + @timeout(seconds=20) + def wait_for_status(self, report, status): + xhiveframework.db.commit() # Flush changes first + while True: + xhiveframework.db.rollback() # read new data + report.reload() + if report.status == status: + break + time.sleep(0.5) + + def create_prepared_report(self, report=None, commit=True): + doc = xhiveframework.get_doc( + { + "doctype": "Prepared Report", + "report_name": report or "Database Storage Usage By Tables", + } + ).insert() + + if commit: + xhiveframework.db.commit() + + return doc + + def test_queueing(self): + doc = self.create_prepared_report() + self.assertEqual("Queued", doc.status) + self.assertTrue(doc.queued_at) + + self.wait_for_status(doc, "Completed") + + doc = xhiveframework.get_last_doc("Prepared Report") + self.assertTrue(doc.job_id) + self.assertTrue(doc.report_end_time) + + def test_prepared_data(self): + doc = self.create_prepared_report() + self.wait_for_status(doc, "Completed") + + prepared_data = json.loads(doc.get_prepared_data().decode("utf-8")) + generated_data = generate_report_result(get_report_doc("Database Storage Usage By Tables")) + self.assertEqual(len(prepared_data["columns"]), len(generated_data["columns"])) + self.assertEqual(len(prepared_data["result"]), len(generated_data["result"])) + self.assertEqual(len(prepared_data), len(generated_data)) + + @run_only_if(db_type_is.MARIADB) + def test_start_status_and_kill_jobs(self): + with test_report(report_type="Query Report", query="select sleep(10)") as report: + doc = self.create_prepared_report(report.name) + self.wait_for_status(doc, "Started") + job_id = doc.job_id + + doc.delete() + time.sleep(1) + job = xhiveframework.get_doc("RQ Job", job_id) + self.assertEqual(job.status, "stopped") + + +@contextmanager +def test_report(**args): + try: + report = xhiveframework.new_doc("Report") + report.update(args) + if not report.report_name: + report.report_name = xhiveframework.generate_hash() + if not report.ref_doctype: + report.ref_doctype = "ToDo" + report.insert() + xhiveframework.db.commit() + yield report + finally: + report.delete() diff --git a/xhiveframework/core/doctype/recorder/__init__.py b/xhiveframework/core/doctype/recorder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/recorder/recorder.js b/xhiveframework/core/doctype/recorder/recorder.js new file mode 100644 index 0000000..01a32f2 --- /dev/null +++ b/xhiveframework/core/doctype/recorder/recorder.js @@ -0,0 +1,84 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Recorder", { + onload: function (frm) { + frm.fields_dict.sql_queries.grid.only_sortable(); + }, + refresh: function (frm) { + frm.disable_save(); + frm._sort_order = {}; + frm.trigger("setup_sort"); + }, + + setup_sort: function (frm) { + const sortable_fields = ["index", "duration", "exact_copies", "normalized_copies"]; + sortable_fields.forEach((field) => { + const field_header = $(`.col[data-fieldname='${field}']`)[0]; + $(field_header).click(() => { + let sort_order = frm._sort_order[field] || -1; + let grid = frm.fields_dict.sql_queries.grid; + grid.data.sort((a, b) => sort_order * (a[field] - b[field])); + frm._sort_order[field] = -1 * sort_order; // reverse for next click + grid.refresh(); + frm.trigger("setup_sort"); // grid creates new elements again, resetup listeners. + }); + }); + }, +}); + +xhiveframework.ui.form.on("Recorder Query", "form_render", function (frm, cdt, cdn) { + let row = xhiveframework.get_doc(cdt, cdn); + let stack = JSON.parse(row.stack); + render_html_field(stack, "stack_html", __("Stack Trace")); + + let explain_result = JSON.parse(row.explain_result); + render_html_field(explain_result, "sql_explain_html", __("SQL Explain")); + + function render_html_field(parsed_json, fieldname, label) { + let html = + "
    "; + if (parsed_json.length == 0) { + html += ""; + } else { + html = create_html_table(parsed_json, html); + } + + let field_wrapper = + frm.fields_dict[row.parentfield].grid.grid_rows_by_docname[cdn].grid_form.fields_dict[ + fieldname + ].wrapper; + $(html).appendTo(field_wrapper); + } + + function create_html_table(table_content, html) { + html += ` + + `; + return html; + } +}); diff --git a/xhiveframework/core/doctype/recorder/recorder.json b/xhiveframework/core/doctype/recorder/recorder.json new file mode 100644 index 0000000..72291db --- /dev/null +++ b/xhiveframework/core/doctype/recorder/recorder.json @@ -0,0 +1,149 @@ +{ + "actions": [], + "creation": "2023-08-01 12:06:49.630877", + "default_view": "List", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "path", + "number_of_queries", + "time_in_queries", + "method", + "column_break_qo53", + "cmd", + "time", + "duration", + "event_type", + "section_break_1skt", + "request_headers", + "section_break_sgro", + "form_dict", + "section_break_9jhm", + "sql_queries", + "section_break_optn", + "profile" + ], + "fields": [ + { + "fieldname": "path", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Path" + }, + { + "depends_on": "eval:doc.event_type==\"HTTP Request\"", + "fieldname": "cmd", + "fieldtype": "Data", + "in_standard_filter": 1, + "label": "CMD" + }, + { + "fieldname": "duration", + "fieldtype": "Float", + "in_list_view": 1, + "label": "Duration" + }, + { + "fieldname": "time", + "fieldtype": "Datetime", + "in_list_view": 1, + "label": "Time" + }, + { + "fieldname": "number_of_queries", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Number of Queries" + }, + { + "fieldname": "time_in_queries", + "fieldtype": "Float", + "label": "Time in Queries" + }, + { + "fieldname": "column_break_qo53", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_1skt", + "fieldtype": "Section Break" + }, + { + "depends_on": "eval:doc.event_type==\"HTTP Request\"", + "fieldname": "request_headers", + "fieldtype": "Code", + "label": "Request Headers" + }, + { + "fieldname": "section_break_sgro", + "fieldtype": "Section Break" + }, + { + "depends_on": "eval:doc.event_type==\"HTTP Request\"", + "fieldname": "form_dict", + "fieldtype": "Code", + "label": "Form Dict" + }, + { + "depends_on": "eval:doc.event_type==\"HTTP Request\"", + "fieldname": "method", + "fieldtype": "Select", + "in_standard_filter": 1, + "label": "Method", + "options": "GET\nPOST\nPUT\nDELETE\nPATCH\nHEAD\nOPTIONS" + }, + { + "fieldname": "sql_queries", + "fieldtype": "Table", + "label": "SQL Queries", + "options": "Recorder Query" + }, + { + "fieldname": "section_break_9jhm", + "fieldtype": "Section Break" + }, + { + "fieldname": "event_type", + "fieldtype": "Data", + "hidden": 1, + "label": "Event Type" + }, + { + "fieldname": "section_break_optn", + "fieldtype": "Section Break" + }, + { + "fieldname": "profile", + "fieldtype": "Code", + "label": "cProfile Output", + "read_only": 1 + } + ], + "hide_toolbar": 1, + "in_create": 1, + "index_web_pages_for_search": 1, + "is_virtual": 1, + "links": [], + "modified": "2024-02-01 22:13:26.505174", + "modified_by": "Administrator", + "module": "Core", + "name": "Recorder", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1 + } + ], + "sort_field": "duration", + "sort_order": "DESC", + "states": [], + "title_field": "path" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/recorder/recorder.py b/xhiveframework/core/doctype/recorder/recorder.py new file mode 100644 index 0000000..7a11f28 --- /dev/null +++ b/xhiveframework/core/doctype/recorder/recorder.py @@ -0,0 +1,104 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.recorder import get as get_recorder_data +from xhiveframework.utils import cint, evaluate_filters, make_filter_dict + + +class Recorder(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.recorder_query.recorder_query import RecorderQuery + from xhiveframework.types import DF + + cmd: DF.Data | None + duration: DF.Float + event_type: DF.Data | None + form_dict: DF.Code | None + method: DF.Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"] + number_of_queries: DF.Int + path: DF.Data | None + profile: DF.Code | None + request_headers: DF.Code | None + sql_queries: DF.Table[RecorderQuery] + time: DF.Datetime | None + time_in_queries: DF.Float + # end: auto-generated types + + def load_from_db(self): + request_data = get_recorder_data(self.name) + if not request_data: + raise xhiveframework.DoesNotExistError + request = serialize_request(request_data) + super(Document, self).__init__(request) + + @staticmethod + def get_list(args): + start = cint(args.get("start")) or 0 + page_length = cint(args.get("page_length")) or 20 + requests = Recorder.get_filtered_requests(args)[start : start + page_length] + + if order_by_statment := args.get("order_by"): + if "." in order_by_statment: + order_by_statment = order_by_statment.split(".")[1] + + if " " in order_by_statment: + sort_key, sort_order = order_by_statment.split(" ", 1) + else: + sort_key = order_by_statment + sort_order = "desc" + + sort_key = sort_key.replace("`", "") + return sorted(requests, key=lambda r: r.get(sort_key) or 0, reverse=bool(sort_order == "desc")) + + return sorted(requests, key=lambda r: r.duration, reverse=1) + + @staticmethod + def get_count(args): + return len(Recorder.get_filtered_requests(args)) + + @staticmethod + def get_filtered_requests(args): + filters = args.get("filters") + requests = [serialize_request(request) for request in get_recorder_data()] + return [req for req in requests if evaluate_filters(req, filters)] + + @staticmethod + def get_stats(args): + pass + + @staticmethod + def delete(self): + pass + + def db_insert(self, *args, **kwargs): + pass + + def db_update(self): + pass + + +def serialize_request(request): + request = xhiveframework._dict(request) + if request.get("calls"): + for i in request.calls: + i["stack"] = xhiveframework.as_json(i["stack"]) + i["explain_result"] = xhiveframework.as_json(i["explain_result"]) + request.update( + name=request.get("uuid"), + number_of_queries=request.get("queries"), + time_in_queries=request.get("time_queries"), + request_headers=xhiveframework.as_json(request.get("headers"), indent=4), + form_dict=xhiveframework.as_json(request.get("form_dict"), indent=4), + sql_queries=request.get("calls"), + modified=request.get("time"), + creation=request.get("time"), + ) + + return request diff --git a/xhiveframework/core/doctype/recorder/recorder_list.js b/xhiveframework/core/doctype/recorder/recorder_list.js new file mode 100644 index 0000000..132af0c --- /dev/null +++ b/xhiveframework/core/doctype/recorder/recorder_list.js @@ -0,0 +1,212 @@ +xhiveframework.listview_settings["Recorder"] = { + hide_name_column: true, + + onload(listview) { + listview.page.sidebar.remove(); + if (!has_common(xhiveframework.user_roles, ["Administrator", "System Manager"])) return; + + if (listview.list_view_settings) { + listview.list_view_settings.disable_comment_count = true; + } + + listview.page.add_button(__("Clear"), () => { + xhiveframework.xcall("xhiveframework.recorder.delete").then(listview.refresh); + }); + + listview.page.add_menu_item(__("Import"), () => { + new xhiveframework.ui.FileUploader({ + folder: this.current_folder, + on_success: (file) => { + if (cur_list.data.length > 0) { + // don't replace existing capture + return; + } + xhiveframework.call({ + method: "xhiveframework.recorder.import_data", + args: { + file: file.file_url, + }, + callback: function () { + listview.refresh(); + }, + }); + }, + }); + }); + + listview.page.add_menu_item(__("Export"), () => { + xhiveframework.call({ + method: "xhiveframework.recorder.export_data", + callback: function (r) { + const data = r.message; + const filename = `${data[0]["uuid"]}..${data[data.length - 1]["uuid"]}.json`; + + const el = document.createElement("a"); + el.setAttribute( + "href", + "data:application/json," + encodeURIComponent(JSON.stringify(data)) + ); + el.setAttribute("download", filename); + el.click(); + }, + }); + }); + + setInterval(() => { + if (listview.list_view_settings.disable_auto_refresh) { + return; + } + if (!listview.enabled) return; + + const route = xhiveframework.get_route() || []; + if (route[0] != "List" || "Recorder" != route[1]) { + return; + } + + listview.refresh(); + }, 5000); + }, + + refresh(listview) { + this.fetch_recorder_status(listview).then(() => this.refresh_controls(listview)); + }, + + refresh_controls(listview) { + this.setup_recorder_controls(listview); + this.update_indicators(listview); + }, + + fetch_recorder_status(listview) { + return xhiveframework.xcall("xhiveframework.recorder.status").then((status) => { + listview.enabled = Boolean(status); + }); + }, + + setup_recorder_controls(listview) { + let me = this; + listview.page.set_primary_action(listview.enabled ? __("Stop") : __("Start"), () => { + if (listview.enabled) { + me.stop_recorder(listview); + } else { + me.start_recorder(listview); + } + }); + }, + + stop_recorder(listview) { + let me = this; + xhiveframework.xcall("xhiveframework.recorder.stop", {}).then(() => { + listview.refresh(); + listview.enabled = false; + me.refresh_controls(listview); + }); + }, + + start_recorder(listview) { + let me = this; + xhiveframework.prompt( + [ + { + fieldtype: "Section Break", + fieldname: "req_job_section", + }, + { + fieldtype: "Column Break", + fieldname: "web_request_columns", + label: "Web Requests", + }, + { + fieldname: "record_requests", + fieldtype: "Check", + label: "Record Web Requests", + default: 1, + }, + { + fieldname: "request_filter", + fieldtype: "Data", + label: "Request path filter", + default: "/", + depends_on: "record_requests", + description: `This will be used for filtering paths which will be recorded. + You can use this to avoid slowing down other traffic. + e.g. /api/method/xhiveerp. Leave it empty to record every request.`, + }, + { + fieldtype: "Column Break", + fieldname: "background_col", + label: "Background Jobs", + }, + + { + fieldname: "record_jobs", + fieldtype: "Check", + label: "Record Background Jobs", + default: 1, + }, + { + fieldname: "jobs_filter", + fieldtype: "Data", + label: "Background Jobs filter", + default: "", + depends_on: "record_jobs", + description: `This will be used for filtering jobs which will be recorded. + You can use this to avoid slowing down other jobs. e.g. email_queue.pull. + Leave it empty to record every job.`, + }, + { + fieldtype: "Section Break", + fieldname: "sql_section", + label: "SQL", + }, + { + fieldname: "record_sql", + fieldtype: "Check", + label: "Record SQL queries", + default: 1, + }, + { + fieldname: "explain", + fieldtype: "Check", + label: "Generate EXPLAIN for SQL queries", + default: 1, + }, + { + fieldname: "capture_stack", + fieldtype: "Check", + label: "Capture callstack of SQL queries", + default: 1, + }, + { + fieldtype: "Section Break", + fieldname: "python_section", + label: "Python", + }, + { + fieldname: "profile", + fieldtype: "Check", + label: "Run cProfile", + default: 0, + description: + "Warning: cProfile adds a lot of overhead. For best results, disable stack capturing when using cProfile.", + }, + ], + (values) => { + xhiveframework.xcall("xhiveframework.recorder.start", values).then(() => { + listview.refresh(); + listview.enabled = true; + me.refresh_controls(listview); + }); + }, + __("Configure Recorder"), + __("Start Recording") + ); + }, + + update_indicators(listview) { + if (listview.enabled) { + listview.page.set_indicator(__("Active"), "green"); + } else { + listview.page.set_indicator(__("Inactive"), "red"); + } + }, +}; diff --git a/xhiveframework/core/doctype/recorder/test_recorder.py b/xhiveframework/core/doctype/recorder/test_recorder.py new file mode 100644 index 0000000..e7b08de --- /dev/null +++ b/xhiveframework/core/doctype/recorder/test_recorder.py @@ -0,0 +1,77 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +import re + +import xhiveframework +import xhiveframework.recorder +from xhiveframework.core.doctype.recorder.recorder import serialize_request +from xhiveframework.recorder import get as get_recorder_data +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import set_request + + +class TestRecorder(XhiveFrameworkTestCase): + def setUp(self): + self.start_recoder() + + def tearDown(self) -> None: + xhiveframework.recorder.stop() + + def start_recoder(self): + xhiveframework.recorder.stop() + xhiveframework.recorder.delete() + set_request(path="/api/method/ping") + xhiveframework.recorder.start() + xhiveframework.recorder.record() + + def stop_recorder(self): + xhiveframework.recorder.dump() + + def test_recorder_list(self): + xhiveframework.get_all("User") # trigger one query + self.stop_recorder() + requests = xhiveframework.get_all("Recorder") + self.assertGreaterEqual(len(requests), 1) + request = xhiveframework.get_doc("Recorder", requests[0].name) + self.assertGreaterEqual(len(request.sql_queries), 1) + queries = [sql_query.query for sql_query in request.sql_queries] + match_flag = 0 + for query in queries: + if bool(re.match("^[select.*from `tabUser`]", query, flags=re.IGNORECASE)): + match_flag = 1 + break + self.assertEqual(match_flag, 1) + + def test_recorder_list_filters(self): + user = xhiveframework.qb.DocType("User") + xhiveframework.qb.from_(user).select("name").run() + self.stop_recorder() + + set_request(path="/api/method/abc") + xhiveframework.recorder.start() + xhiveframework.recorder.record() + xhiveframework.get_all("User") + self.stop_recorder() + + requests = xhiveframework.get_list( + "Recorder", filters={"path": ("like", "/api/method/ping"), "number_of_queries": 1} + ) + self.assertGreaterEqual(len(requests), 1) + requests = xhiveframework.get_list("Recorder", filters={"path": ("like", "/api/method/test")}) + self.assertEqual(len(requests), 0) + + requests = xhiveframework.get_list("Recorder", filters={"method": "GET"}) + self.assertGreaterEqual(len(requests), 1) + requests = xhiveframework.get_list("Recorder", filters={"method": "POST"}) + self.assertEqual(len(requests), 0) + + requests = xhiveframework.get_list("Recorder", order_by="path desc") + self.assertEqual(requests[0].path, "/api/method/ping") + + def test_recorder_serialization(self): + xhiveframework.get_all("User") # trigger one query + self.stop_recorder() + requests = xhiveframework.get_all("Recorder") + request_doc = get_recorder_data(requests[0].name) + self.assertIsInstance(serialize_request(request_doc), dict) diff --git a/xhiveframework/core/doctype/recorder_query/__init__.py b/xhiveframework/core/doctype/recorder_query/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/recorder_query/recorder_query.js b/xhiveframework/core/doctype/recorder_query/recorder_query.js new file mode 100644 index 0000000..fd70277 --- /dev/null +++ b/xhiveframework/core/doctype/recorder_query/recorder_query.js @@ -0,0 +1,8 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +// xhiveframework.ui.form.on("Recorder Query", { +// refresh(frm) { + +// }, +// }); diff --git a/xhiveframework/core/doctype/recorder_query/recorder_query.json b/xhiveframework/core/doctype/recorder_query/recorder_query.json new file mode 100644 index 0000000..4a529ad --- /dev/null +++ b/xhiveframework/core/doctype/recorder_query/recorder_query.json @@ -0,0 +1,106 @@ +{ + "actions": [], + "creation": "2023-08-01 17:04:12.173774", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "index", + "query", + "duration", + "column_break_qmju", + "exact_copies", + "normalized_query", + "normalized_copies", + "section_break_dygy", + "stack_html", + "stack", + "section_break_kvkb", + "sql_explain_html", + "explain_result" + ], + "fields": [ + { + "fieldname": "query", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Query", + "length": 2 + }, + { + "fieldname": "normalized_query", + "fieldtype": "Data", + "label": "Normalized Query" + }, + { + "fieldname": "duration", + "fieldtype": "Float", + "in_list_view": 1, + "label": "Duration" + }, + { + "fieldname": "exact_copies", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Exact Copies" + }, + { + "fieldname": "normalized_copies", + "fieldtype": "Int", + "label": "Normalized Copies" + }, + { + "fieldname": "column_break_qmju", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_dygy", + "fieldtype": "Section Break" + }, + { + "fieldname": "stack", + "fieldtype": "Text", + "hidden": 1, + "print_hide": 1 + }, + { + "fieldname": "stack_html", + "fieldtype": "HTML", + "label": "Stack Trace" + }, + { + "fieldname": "section_break_kvkb", + "fieldtype": "Section Break" + }, + { + "fieldname": "explain_result", + "fieldtype": "Text", + "hidden": 1, + "print_hide": 1 + }, + { + "fieldname": "sql_explain_html", + "fieldtype": "HTML", + "label": "SQL Explain" + }, + { + "fieldname": "index", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Index" + } + ], + "index_web_pages_for_search": 1, + "is_virtual": 1, + "istable": 1, + "links": [], + "modified": "2023-08-07 13:12:23.496002", + "modified_by": "Administrator", + "module": "Core", + "name": "Recorder Query", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/recorder_query/recorder_query.py b/xhiveframework/core/doctype/recorder_query/recorder_query.py new file mode 100644 index 0000000..4523d73 --- /dev/null +++ b/xhiveframework/core/doctype/recorder_query/recorder_query.py @@ -0,0 +1,53 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class RecorderQuery(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + duration: DF.Float + exact_copies: DF.Int + explain_result: DF.Text | None + index: DF.Int + normalized_copies: DF.Int + normalized_query: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + query: DF.Data + stack: DF.Text | None + # end: auto-generated types + pass + + def db_insert(self, *args, **kwargs): + pass + + def load_from_db(self): + pass + + def db_update(self): + pass + + @staticmethod + def get_list(args): + pass + + @staticmethod + def get_count(args): + pass + + @staticmethod + def get_stats(args): + pass + + def delete(self): + pass diff --git a/xhiveframework/core/doctype/recorder_query/test_recorder_query.py b/xhiveframework/core/doctype/recorder_query/test_recorder_query.py new file mode 100644 index 0000000..f5a8fd9 --- /dev/null +++ b/xhiveframework/core/doctype/recorder_query/test_recorder_query.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestRecorderQuery(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/report/README.md b/xhiveframework/core/doctype/report/README.md new file mode 100644 index 0000000..99702e7 --- /dev/null +++ b/xhiveframework/core/doctype/report/README.md @@ -0,0 +1 @@ +Standard / Custom report. Reports can be of types Query Report or Script Report. \ No newline at end of file diff --git a/xhiveframework/core/doctype/report/__init__.py b/xhiveframework/core/doctype/report/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/report/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/report/boilerplate/controller.js b/xhiveframework/core/doctype/report/boilerplate/controller.js new file mode 100644 index 0000000..af24c8d --- /dev/null +++ b/xhiveframework/core/doctype/report/boilerplate/controller.js @@ -0,0 +1,8 @@ +// Copyright (c) {year}, {app_publisher} and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["{name}"] = {{ + "filters": [ + + ] +}}; diff --git a/xhiveframework/core/doctype/report/boilerplate/controller.py b/xhiveframework/core/doctype/report/boilerplate/controller.py new file mode 100644 index 0000000..4c087c1 --- /dev/null +++ b/xhiveframework/core/doctype/report/boilerplate/controller.py @@ -0,0 +1,9 @@ +# Copyright (c) {year}, {app_publisher} and contributors +# For license information, please see license.txt + +# import xhiveframework + + +def execute(filters=None): + columns, data = [], [] + return columns, data diff --git a/xhiveframework/core/doctype/report/report.js b/xhiveframework/core/doctype/report/report.js new file mode 100644 index 0000000..6cecdf6 --- /dev/null +++ b/xhiveframework/core/doctype/report/report.js @@ -0,0 +1,68 @@ +xhiveframework.ui.form.on("Report", { + refresh: function (frm) { + if (frm.doc.is_standard === "Yes" && !xhiveframework.boot.developer_mode) { + // make the document read-only + frm.disable_form(); + } else { + frm.enable_save(); + } + + let doc = frm.doc; + if (!doc.__islocal) { + frm.add_custom_button( + __("Show Report"), + function () { + switch (doc.report_type) { + case "Report Builder": + xhiveframework.set_route("List", doc.ref_doctype, "Report", doc.name); + break; + case "Query Report": + xhiveframework.set_route("query-report", doc.name); + break; + case "Script Report": + xhiveframework.set_route("query-report", doc.name); + break; + case "Custom Report": + xhiveframework.set_route("query-report", doc.name); + break; + } + }, + "fa fa-table" + ); + } + + if (doc.is_standard === "Yes" && frm.perm[0].write) { + frm.add_custom_button( + doc.disabled ? __("Enable Report") : __("Disable Report"), + function () { + frm.call("toggle_disable", { + disable: doc.disabled ? 0 : 1, + }).then(() => { + frm.reload_doc(); + }); + }, + doc.disabled ? "fa fa-check" : "fa fa-off" + ); + } + + frm.set_query("ref_doctype", () => { + return { + filters: { + istable: 0, + }, + }; + }); + }, + + ref_doctype: function (frm) { + if (frm.doc.ref_doctype) { + frm.trigger("set_doctype_roles"); + } + }, + + set_doctype_roles: function (frm) { + return frm.call("set_doctype_roles").then(() => { + frm.refresh_field("roles"); + }); + }, +}); diff --git a/xhiveframework/core/doctype/report/report.json b/xhiveframework/core/doctype/report/report.json new file mode 100644 index 0000000..0c64c88 --- /dev/null +++ b/xhiveframework/core/doctype/report/report.json @@ -0,0 +1,242 @@ +{ + "actions": [], + "autoname": "field:report_name", + "creation": "2013-03-09 15:45:57", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "report_name", + "ref_doctype", + "reference_report", + "is_standard", + "module", + "column_break_4", + "report_type", + "letter_head", + "add_total_row", + "disabled", + "prepared_report", + "filters_section", + "filters", + "columns_section", + "columns", + "section_break_6", + "query", + "report_script", + "client_code_section", + "javascript", + "json", + "permission_rules", + "roles" + ], + "fields": [ + { + "fieldname": "report_name", + "fieldtype": "Data", + "label": "Report Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Ref DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "reference_report", + "fieldtype": "Data", + "label": "Reference Report" + }, + { + "fieldname": "is_standard", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Is Standard", + "options": "No\nYes", + "reqd": 1 + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "options": "Module Def" + }, + { + "default": "0", + "fieldname": "add_total_row", + "fieldtype": "Check", + "label": "Add Total Row" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "report_type", + "fieldtype": "Select", + "label": "Report Type", + "options": "Report Builder\nQuery Report\nScript Report\nCustom Report", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "depends_on": "eval: doc.is_standard == \"No\"", + "fieldname": "letter_head", + "fieldtype": "Link", + "label": "Letter Head", + "options": "Letter Head" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break", + "label": "Query / Script" + }, + { + "depends_on": "eval:doc.report_type==\"Query Report\"", + "fieldname": "query", + "fieldtype": "Code", + "label": "Query" + }, + { + "depends_on": "eval:doc.report_type==\"Script Report\" && doc.is_standard===\"No\"", + "description": "JavaScript Format: xhiveframework.query_reports['REPORTNAME'] = {}", + "fieldname": "javascript", + "fieldtype": "Code", + "label": "Javascript" + }, + { + "depends_on": "eval:doc.report_type==\"Report Builder\" || \"Custom Report\"", + "fieldname": "json", + "fieldtype": "Code", + "label": "JSON", + "read_only": 1 + }, + { + "fieldname": "permission_rules", + "fieldtype": "Section Break" + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "label": "Roles", + "options": "Has Role" + }, + { + "default": "0", + "fieldname": "prepared_report", + "fieldtype": "Check", + "label": "Prepared Report" + }, + { + "depends_on": "eval:(doc.report_type===\"Script Report\" \n|| doc.report_type==\"Query Report\") \n&& doc.is_standard===\"No\"", + "description": "Filters will be accessible via filters.

    Send output as result = [result], or for old style data = [columns], [result]", + "fieldname": "report_script", + "fieldtype": "Code", + "label": "Script" + }, + { + "collapsible": 1, + "collapsible_depends_on": "filters", + "depends_on": "eval:doc.report_type != \"Custom Report\"", + "fieldname": "filters_section", + "fieldtype": "Section Break", + "label": "Filters" + }, + { + "depends_on": "eval:doc.report_type != \"Custom Report\"", + "fieldname": "filters", + "fieldtype": "Table", + "label": "Filters", + "options": "Report Filter" + }, + { + "collapsible": 1, + "collapsible_depends_on": "columns", + "depends_on": "eval:doc.report_type != \"Custom Report\"", + "fieldname": "columns_section", + "fieldtype": "Section Break", + "label": "Columns" + }, + { + "depends_on": "eval:doc.report_type != \"Custom Report\"", + "fieldname": "columns", + "fieldtype": "Table", + "label": "Columns", + "options": "Report Column" + }, + { + "collapsible": 1, + "collapsible_depends_on": "javascript", + "fieldname": "client_code_section", + "fieldtype": "Section Break", + "label": "Client Code" + } + ], + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-04-07 18:18:11.782178", + "modified_by": "Administrator", + "module": "Core", + "name": "Report", + "naming_rule": "By fieldname", + "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 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Report Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User" + } + ], + "show_name_in_global_search": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/report/report.py b/xhiveframework/core/doctype/report/report.py new file mode 100644 index 0000000..074e592 --- /dev/null +++ b/xhiveframework/core/doctype/report/report.py @@ -0,0 +1,418 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import datetime +import json + +import xhiveframework +import xhiveframework.desk.query_report +from xhiveframework import _, scrub +from xhiveframework.core.doctype.custom_role.custom_role import get_custom_allowed_roles +from xhiveframework.core.doctype.page.page import delete_custom_role +from xhiveframework.desk.reportview import append_totals_row +from xhiveframework.model.document import Document +from xhiveframework.modules import make_boilerplate +from xhiveframework.modules.export_file import export_to_files +from xhiveframework.utils import cint, cstr +from xhiveframework.utils.safe_exec import check_safe_sql_query, safe_exec + + +class Report(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.core.doctype.report_column.report_column import ReportColumn + from xhiveframework.core.doctype.report_filter.report_filter import ReportFilter + from xhiveframework.types import DF + + add_total_row: DF.Check + columns: DF.Table[ReportColumn] + disabled: DF.Check + filters: DF.Table[ReportFilter] + is_standard: DF.Literal["No", "Yes"] + javascript: DF.Code | None + json: DF.Code | None + letter_head: DF.Link | None + module: DF.Link | None + prepared_report: DF.Check + query: DF.Code | None + ref_doctype: DF.Link + reference_report: DF.Data | None + report_name: DF.Data + report_script: DF.Code | None + report_type: DF.Literal["Report Builder", "Query Report", "Script Report", "Custom Report"] + roles: DF.Table[HasRole] + + # end: auto-generated types + def validate(self): + """only administrator can save standard report""" + if not self.module: + self.module = xhiveframework.db.get_value("DocType", self.ref_doctype, "module") + + if not self.is_standard: + self.is_standard = "No" + if ( + xhiveframework.session.user == "Administrator" + and getattr(xhiveframework.local.conf, "developer_mode", 0) == 1 + ): + self.is_standard = "Yes" + + if self.is_standard == "No": + # allow only script manager to edit scripts + if self.report_type != "Report Builder": + xhiveframework.only_for("Script Manager", True) + + if xhiveframework.db.get_value("Report", self.name, "is_standard") == "Yes": + xhiveframework.throw(_("Cannot edit a standard report. Please duplicate and create a new report")) + + if self.is_standard == "Yes" and xhiveframework.session.user != "Administrator": + xhiveframework.throw(_("Only Administrator can save a standard report. Please rename and save.")) + + if self.report_type == "Report Builder": + self.update_report_json() + + def before_insert(self): + self.set_doctype_roles() + + def on_update(self): + self.export_doc() + + def before_export(self, doc): + doc.letterhead = None + doc.prepared_report = 0 + + def on_trash(self): + if ( + self.is_standard == "Yes" + and not cint(getattr(xhiveframework.local.conf, "developer_mode", 0)) + and not (xhiveframework.flags.in_migrate or xhiveframework.flags.in_patch) + ): + xhiveframework.throw(_("You are not allowed to delete Standard Report")) + delete_custom_role("report", self.name) + + def get_columns(self): + return [d.as_dict(no_default_fields=True, no_child_table_fields=True) for d in self.columns] + + @xhiveframework.whitelist() + def set_doctype_roles(self): + if not self.get("roles") and self.is_standard == "No": + meta = xhiveframework.get_meta(self.ref_doctype) + if not meta.istable: + roles = [{"role": d.role} for d in meta.permissions if d.permlevel == 0] + self.set("roles", roles) + + def is_permitted(self): + """Returns true if Has Role is not set or the user is allowed.""" + from xhiveframework.utils import has_common + + allowed = [d.role for d in xhiveframework.get_all("Has Role", fields=["role"], filters={"parent": self.name})] + + custom_roles = get_custom_allowed_roles("report", self.name) + + if custom_roles: + allowed = custom_roles + + if not allowed: + return True + + if has_common(xhiveframework.get_roles(), allowed): + return True + + def update_report_json(self): + if not self.json: + self.json = "{}" + + def export_doc(self): + if xhiveframework.flags.in_import: + return + + if self.is_standard == "Yes" and (xhiveframework.local.conf.get("developer_mode") or 0) == 1: + export_to_files(record_list=[["Report", self.name]], record_module=self.module, create_init=True) + + self.create_report_py() + + def create_report_py(self): + if self.report_type == "Script Report": + make_boilerplate("controller.py", self, {"name": self.name}) + make_boilerplate("controller.js", self, {"name": self.name}) + + def execute_query_report(self, filters): + if not self.query: + xhiveframework.throw(_("Must specify a Query to run"), title=_("Report Document Error")) + + check_safe_sql_query(self.query) + + result = [list(t) for t in xhiveframework.db.sql(self.query, filters)] + columns = self.get_columns() or [cstr(c[0]) for c in xhiveframework.db.get_description()] + + return [columns, result] + + def execute_script_report(self, filters): + # save the timestamp to automatically set to prepared + threshold = 15 + res = [] + + start_time = datetime.datetime.now() + + # The JOB + if self.is_standard == "Yes": + res = self.execute_module(filters) + else: + res = self.execute_script(filters) + + # automatically set as prepared + execution_time = (datetime.datetime.now() - start_time).total_seconds() + if execution_time > threshold and not self.prepared_report: + xhiveframework.enqueue(enable_prepared_report, report=self.name) + + xhiveframework.cache.hset("report_execution_time", self.name, execution_time) + + return res + + def execute_module(self, filters): + # report in python module + module = self.module or xhiveframework.db.get_value("DocType", self.ref_doctype, "module") + method_name = get_report_module_dotted_path(module, self.name) + ".execute" + return xhiveframework.get_attr(method_name)(xhiveframework._dict(filters)) + + def execute_script(self, filters): + # server script + loc = {"filters": xhiveframework._dict(filters), "data": None, "result": None} + safe_exec(self.report_script, None, loc, script_filename=f"Report {self.name}") + if loc["data"]: + return loc["data"] + else: + return self.get_columns(), loc["result"] + + def get_data( + self, + filters=None, + limit=None, + user=None, + as_dict=False, + ignore_prepared_report=False, + are_default_filters=True, + ): + if self.report_type in ("Query Report", "Script Report", "Custom Report"): + columns, result = self.run_query_report( + filters, user, ignore_prepared_report, are_default_filters + ) + else: + columns, result = self.run_standard_report(filters, limit, user) + + if as_dict: + result = self.build_data_dict(result, columns) + + return columns, result + + def run_query_report( + self, filters=None, user=None, ignore_prepared_report=False, are_default_filters=True + ): + columns, result = [], [] + data = xhiveframework.desk.query_report.run( + self.name, + filters=filters, + user=user, + ignore_prepared_report=ignore_prepared_report, + are_default_filters=are_default_filters, + ) + + for d in data.get("columns"): + if isinstance(d, dict): + col = xhiveframework._dict(d) + if not col.fieldname: + col.fieldname = col.label + columns.append(col) + else: + fieldtype, options = "Data", None + parts = d.split(":") + if len(parts) > 1: + if parts[1]: + fieldtype, options = parts[1], None + if fieldtype and "/" in fieldtype: + fieldtype, options = fieldtype.split("/") + + columns.append( + xhiveframework._dict(label=parts[0], fieldtype=fieldtype, fieldname=parts[0], options=options) + ) + + result += data.get("result") + + return columns, result + + def run_standard_report(self, filters, limit, user): + params = json.loads(self.json) + columns = self.get_standard_report_columns(params) + result = [] + order_by, group_by, group_by_args = self.get_standard_report_order_by(params) + + _result = xhiveframework.get_list( + self.ref_doctype, + fields=[ + get_group_by_field(group_by_args, c[1]) + if c[0] == "_aggregate_column" and group_by_args + else Report._format([c[1], c[0]]) + for c in columns + ], + filters=self.get_standard_report_filters(params, filters), + order_by=order_by, + group_by=group_by, + as_list=True, + limit=limit, + user=user, + ) + + columns = self.build_standard_report_columns(columns, group_by_args) + + result = result + [list(d) for d in _result] + + if params.get("add_totals_row"): + result = append_totals_row(result) + + return columns, result + + @staticmethod + def _format(parts): + # sort by is saved as DocType.fieldname, covert it to sql + return "`tab{}`.`{}`".format(*parts) + + def get_standard_report_columns(self, params): + if params.get("fields"): + columns = params.get("fields") + elif params.get("columns"): + columns = params.get("columns") + elif params.get("fields"): + columns = params.get("fields") + else: + columns = [["name", self.ref_doctype]] + columns.extend( + [df.fieldname, self.ref_doctype] + for df in xhiveframework.get_meta(self.ref_doctype).fields + if df.in_list_view + ) + return columns + + def get_standard_report_filters(self, params, filters): + _filters = params.get("filters") or [] + + if filters: + for key, value in filters.items(): + condition, _value = "=", value + if isinstance(value, list | tuple): + condition, _value = value + _filters.append([key, condition, _value]) + + return _filters + + def get_standard_report_order_by(self, params): + group_by_args = None + if params.get("sort_by"): + order_by = Report._format(params.get("sort_by").split(".")) + " " + params.get("sort_order") + + elif params.get("order_by"): + order_by = params.get("order_by") + else: + order_by = Report._format([self.ref_doctype, "modified"]) + " desc" + + if params.get("sort_by_next"): + order_by += ( + ", " + + Report._format(params.get("sort_by_next").split(".")) + + " " + + params.get("sort_order_next") + ) + + group_by = None + if params.get("group_by"): + group_by_args = xhiveframework._dict(params["group_by"]) + group_by = group_by_args["group_by"] + order_by = "_aggregate_column desc" + + return order_by, group_by, group_by_args + + def build_standard_report_columns(self, columns, group_by_args): + _columns = [] + + for fieldname, doctype in columns: + meta = xhiveframework.get_meta(doctype) + + if meta.get_field(fieldname): + field = meta.get_field(fieldname) + else: + if fieldname == "_aggregate_column": + label = get_group_by_column_label(group_by_args, meta) + else: + label = meta.get_label(fieldname) + + field = xhiveframework._dict(fieldname=fieldname, label=label) + + # since name is the primary key for a document, it will always be a Link datatype + if fieldname == "name": + field.fieldtype = "Link" + field.options = doctype + + _columns.append(field) + return _columns + + def build_data_dict(self, result, columns): + data = [] + for row in result: + if isinstance(row, list | tuple): + _row = xhiveframework._dict() + for i, val in enumerate(row): + _row[columns[i].get("fieldname")] = val + elif isinstance(row, dict): + # no need to convert from dict to dict + _row = xhiveframework._dict(row) + data.append(_row) + + return data + + @xhiveframework.whitelist() + def toggle_disable(self, disable: bool): + if not self.has_permission("write"): + xhiveframework.throw(_("You are not allowed to edit the report.")) + + self.db_set("disabled", cint(disable)) + + +def is_prepared_report_enabled(report): + return cint(xhiveframework.db.get_value("Report", report, "prepared_report")) or 0 + + +def get_report_module_dotted_path(module, report_name): + return ( + xhiveframework.local.module_app[scrub(module)] + + "." + + scrub(module) + + ".report." + + scrub(report_name) + + "." + + scrub(report_name) + ) + + +def get_group_by_field(args, doctype): + if args["aggregate_function"] == "count": + group_by_field = "count(*) as _aggregate_column" + else: + group_by_field = f"{args.aggregate_function}({args.aggregate_on}) as _aggregate_column" + + return group_by_field + + +def get_group_by_column_label(args, meta): + if args["aggregate_function"] == "count": + label = "Count" + else: + sql_fn_map = {"avg": "Average", "sum": "Sum"} + aggregate_on_label = meta.get_label(args.aggregate_on) + label = _("{0} of {1}").format(_(sql_fn_map[args.aggregate_function]), _(aggregate_on_label)) + return label + + +def enable_prepared_report(report: str): + xhiveframework.db.set_value("Report", report, "prepared_report", 1) diff --git a/xhiveframework/core/doctype/report/test_records.json b/xhiveframework/core/doctype/report/test_records.json new file mode 100644 index 0000000..cee1a20 --- /dev/null +++ b/xhiveframework/core/doctype/report/test_records.json @@ -0,0 +1,10 @@ +[ + { + "doctype": "Report", + "name": "_Test Report 1", + "report_name": "_Test Report 1", + "report_type": "Query Report", + "is_standard": "No", + "ref_doctype": "Event" + } +] diff --git a/xhiveframework/core/doctype/report/test_report.py b/xhiveframework/core/doctype/report/test_report.py new file mode 100644 index 0000000..826ef64 --- /dev/null +++ b/xhiveframework/core/doctype/report/test_report.py @@ -0,0 +1,433 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import os +import textwrap + +import xhiveframework +from xhiveframework.core.doctype.user_permission.test_user_permission import create_user +from xhiveframework.custom.doctype.customize_form.customize_form import reset_customization +from xhiveframework.desk.query_report import add_total_row, run, save_report +from xhiveframework.desk.reportview import delete_report +from xhiveframework.desk.reportview import save_report as _save_report +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Report") +test_dependencies = ["User"] + + +class TestReport(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls) -> None: + cls.enable_safe_exec() + return super().setUpClass() + + def test_report_builder(self): + if xhiveframework.db.exists("Report", "User Activity Report"): + xhiveframework.delete_doc("Report", "User Activity Report") + + with open(os.path.join(os.path.dirname(__file__), "user_activity_report.json")) as f: + xhiveframework.get_doc(json.loads(f.read())).insert() + + report = xhiveframework.get_doc("Report", "User Activity Report") + columns, data = report.get_data() + self.assertEqual(columns[0].get("label"), "ID") + self.assertEqual(columns[1].get("label"), "User Type") + self.assertTrue("Administrator" in [d[0] for d in data]) + + def test_query_report(self): + report = xhiveframework.get_doc("Report", "Permitted Documents For User") + columns, data = report.get_data(filters={"user": "Administrator", "doctype": "DocType"}) + self.assertEqual(columns[0].get("label"), "Name") + 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 = xhiveframework.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") + xhiveframework.set_user("test_report_owner@example.com") + self.assertRaises(xhiveframework.PermissionError, delete_report, report.name) + + # Check for Report Type + xhiveframework.set_user("Administrator") + report.db_set("report_type", "Custom Report") + self.assertRaisesRegex( + xhiveframework.ValidationError, + "Only reports of type Report Builder can be deleted", + delete_report, + report.name, + ) + + # Check if creating and deleting works with proper validations + xhiveframework.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 = xhiveframework.get_doc("Report", report_name) + delete_report(doc.name) + + finally: + xhiveframework.set_user("Administrator") + xhiveframework.db.rollback() + + def test_custom_report(self): + reset_customization("User") + custom_report_name = save_report( + "Permitted Documents For User", + "Permitted Documents For User Custom", + 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", + } + ] + ), + json.dumps({"user": "Administrator", "doctype": "User"}), + ) + custom_report = xhiveframework.get_doc("Report", custom_report_name) + columns, result = custom_report.run_query_report(user=xhiveframework.session.user) + + self.assertListEqual(["email"], [column.get("fieldname") for column in columns]) + admin_dict = xhiveframework.core.utils.find(result, lambda d: d["name"] == "Administrator") + self.assertDictEqual( + { + "name": "Administrator", + "user_type": "System User", + "email": "admin@example.com", + }, + admin_dict, + ) + + def test_report_with_custom_column(self): + reset_customization("User") + response = run( + "Permitted Documents For User", + filters={"user": "Administrator", "doctype": "User"}, + custom_columns=[ + { + "fieldname": "email", + "fieldtype": "Data", + "label": "Email", + "insert_after_index": 0, + "link_field": "name", + "doctype": "User", + "options": "Email", + "width": 100, + "id": "email", + "name": "Email", + } + ], + ) + result = response.get("result") + columns = response.get("columns") + self.assertListEqual( + ["name", "email", "user_type"], + [column.get("fieldname") for column in columns], + ) + admin_dict = xhiveframework.core.utils.find(result, lambda d: d["name"] == "Administrator") + self.assertDictEqual( + { + "name": "Administrator", + "user_type": "System User", + "email": "admin@example.com", + }, + admin_dict, + ) + + def test_report_permissions(self): + xhiveframework.set_user("test@example.com") + xhiveframework.db.delete("Has Role", {"parent": xhiveframework.session.user, "role": "Test Has Role"}) + xhiveframework.db.commit() + if not xhiveframework.db.exists("Role", "Test Has Role"): + xhiveframework.get_doc({"doctype": "Role", "role_name": "Test Has Role"}).insert(ignore_permissions=True) + + if not xhiveframework.db.exists("Report", "Test Report"): + report = xhiveframework.get_doc( + { + "doctype": "Report", + "ref_doctype": "User", + "report_name": "Test Report", + "report_type": "Query Report", + "is_standard": "No", + "roles": [{"role": "Test Has Role"}], + } + ).insert(ignore_permissions=True) + else: + report = xhiveframework.get_doc("Report", "Test Report") + + self.assertNotEqual(report.is_permitted(), True) + xhiveframework.set_user("Administrator") + + def test_report_custom_permissions(self): + xhiveframework.set_user("test@example.com") + xhiveframework.db.delete("Custom Role", {"report": "Test Custom Role Report"}) + xhiveframework.db.commit() # nosemgrep + if not xhiveframework.db.exists("Report", "Test Custom Role Report"): + report = xhiveframework.get_doc( + { + "doctype": "Report", + "ref_doctype": "User", + "report_name": "Test Custom Role Report", + "report_type": "Query Report", + "is_standard": "No", + "roles": [{"role": "_Test Role"}, {"role": "System Manager"}], + } + ).insert(ignore_permissions=True) + else: + report = xhiveframework.get_doc("Report", "Test Custom Role Report") + + self.assertEqual(report.is_permitted(), True) + + xhiveframework.get_doc( + { + "doctype": "Custom Role", + "report": "Test Custom Role Report", + "roles": [{"role": "_Test Role 2"}], + "ref_doctype": "User", + } + ).insert(ignore_permissions=True) + + self.assertNotEqual(report.is_permitted(), True) + xhiveframework.set_user("Administrator") + + # test for the `_format` method if report data doesn't have sort_by parameter + def test_format_method(self): + if xhiveframework.db.exists("Report", "User Activity Report Without Sort"): + xhiveframework.delete_doc("Report", "User Activity Report Without Sort") + with open(os.path.join(os.path.dirname(__file__), "user_activity_report_without_sort.json")) as f: + xhiveframework.get_doc(json.loads(f.read())).insert() + + report = xhiveframework.get_doc("Report", "User Activity Report Without Sort") + columns, data = report.get_data() + + self.assertEqual(columns[0].get("label"), "ID") + self.assertEqual(columns[1].get("label"), "User Type") + self.assertTrue("Administrator" in [d[0] for d in data]) + xhiveframework.delete_doc("Report", "User Activity Report Without Sort") + + def test_non_standard_script_report(self): + report_name = "Test Non Standard Script Report" + if not xhiveframework.db.exists("Report", report_name): + report = xhiveframework.get_doc( + { + "doctype": "Report", + "ref_doctype": "User", + "report_name": report_name, + "report_type": "Script Report", + "is_standard": "No", + } + ).insert(ignore_permissions=True) + else: + report = xhiveframework.get_doc("Report", report_name) + + report.report_script = """ +totals = {} +for user in xhiveframework.get_all('User', fields = ['name', 'user_type', 'creation']): + if not user.user_type in totals: + totals[user.user_type] = 0 + totals[user.user_type] = totals[user.user_type] + 1 + +data = [ + [ + {'fieldname': 'type', 'label': 'Type'}, + {'fieldname': 'value', 'label': 'Value'} + ], + [ + {"type":key, "value": value} for key, value in totals.items() + ] +] +""" + report.save() + data = report.get_data() + + # check columns + self.assertEqual(data[0][0]["label"], "Type") + + # check values + self.assertTrue("System User" in [d.get("type") for d in data[1]]) + + def test_script_report_with_columns(self): + report_name = "Test Script Report With Columns" + + if xhiveframework.db.exists("Report", report_name): + xhiveframework.delete_doc("Report", report_name) + + report = xhiveframework.get_doc( + { + "doctype": "Report", + "ref_doctype": "User", + "report_name": report_name, + "report_type": "Script Report", + "is_standard": "No", + "columns": [ + dict(fieldname="type", label="Type", fieldtype="Data"), + dict(fieldname="value", label="Value", fieldtype="Int"), + ], + } + ).insert(ignore_permissions=True) + + report.report_script = """ +totals = {} +for user in xhiveframework.get_all('User', fields = ['name', 'user_type', 'creation']): + if not user.user_type in totals: + totals[user.user_type] = 0 + totals[user.user_type] = totals[user.user_type] + 1 + +result = [ + {"type":key, "value": value} for key, value in totals.items() + ] +""" + + report.save() + data = report.get_data() + + # check columns + self.assertEqual(data[0][0]["label"], "Type") + + # check values + self.assertTrue("System User" in [d.get("type") for d in data[1]]) + + def test_toggle_disabled(self): + """Make sure that authorization is respected.""" + # Assuming that there will be reports in the system. + reports = xhiveframework.get_all(doctype="Report", limit=1) + report_name = reports[0]["name"] + doc = xhiveframework.get_doc("Report", report_name) + status = doc.disabled + + # User has write permission on reports and should pass through + xhiveframework.set_user("test@example.com") + doc.toggle_disable(not status) + doc.reload() + self.assertNotEqual(status, doc.disabled) + + # User has no write permission on reports, permission error is expected. + xhiveframework.set_user("test1@example.com") + doc = xhiveframework.get_doc("Report", report_name) + with self.assertRaises(xhiveframework.exceptions.ValidationError): + doc.toggle_disable(1) + + # Set user back to administrator + xhiveframework.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, + is_tree=report_settings["tree"], + parent_field=report_settings["parent_field"], + ) + self.assertEqual(result[-1][0], "Total") + self.assertEqual(result[-1][1], 200) + self.assertEqual(result[-1][2], 150.50) + + def test_cte_in_query_report(self): + cte_query = textwrap.dedent( + """ + with enabled_users as ( + select name + from `tabUser` + where enabled = 1 + ) + select * from enabled_users; + """ + ) + + report = xhiveframework.get_doc( + { + "doctype": "Report", + "ref_doctype": "User", + "report_name": "Enabled Users List", + "report_type": "Query Report", + "is_standard": "No", + "query": cte_query, + } + ).insert() + + if xhiveframework.db.db_type == "mariadb": + col, rows = report.execute_query_report(filters={}) + self.assertEqual(col[0], "name") + self.assertGreaterEqual(len(rows), 1) + elif xhiveframework.db.db_type == "postgres": + self.assertRaises(xhiveframework.PermissionError, report.execute_query_report, filters={}) diff --git a/xhiveframework/core/doctype/report/user_activity_report.json b/xhiveframework/core/doctype/report/user_activity_report.json new file mode 100644 index 0000000..5339bbe --- /dev/null +++ b/xhiveframework/core/doctype/report/user_activity_report.json @@ -0,0 +1,17 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "is_standard": "No", + "javascript": null, + "json": "{\"filters\":[],\"columns\":[[\"name\",\"User\"],[\"user_type\",\"User\"],[\"first_name\",\"User\"],[\"last_name\",\"User\"],[\"last_active\",\"User\"],[\"role\",\"Has Role\"]],\"sort_by\":\"User.modified\",\"sort_order\":\"desc\",\"sort_by_next\":null,\"sort_order_next\":\"desc\"}", + "modified": "2016-09-01 02:59:07.728890", + "module": "Core", + "name": "User Activity Report", + "query": null, + "ref_doctype": "User", + "report_name": "User Activity Report", + "report_type": "Report Builder" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/report/user_activity_report_without_sort.json b/xhiveframework/core/doctype/report/user_activity_report_without_sort.json new file mode 100644 index 0000000..bb520a2 --- /dev/null +++ b/xhiveframework/core/doctype/report/user_activity_report_without_sort.json @@ -0,0 +1,17 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "is_standard": "No", + "javascript": null, + "json": "{\"filters\":[],\"columns\":[[\"name\",\"User\"],[\"user_type\",\"User\"],[\"first_name\",\"User\"],[\"last_name\",\"User\"],[\"last_active\",\"User\"],[\"role\",\"Has Role\"]],\"sort_order\":\"desc\",\"sort_by_next\":null,\"sort_order_next\":\"desc\"}", + "modified": "2018-12-17 18:27:07.728890", + "module": "Core", + "name": "User Activity Report Without Sort", + "query": null, + "ref_doctype": "User", + "report_name": "User Activity Report Without Sort", + "report_type": "Report Builder" + } \ No newline at end of file diff --git a/xhiveframework/core/doctype/report_column/__init__.py b/xhiveframework/core/doctype/report_column/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/report_column/report_column.json b/xhiveframework/core/doctype/report_column/report_column.json new file mode 100644 index 0000000..2e6a22d --- /dev/null +++ b/xhiveframework/core/doctype/report_column/report_column.json @@ -0,0 +1,61 @@ +{ + "actions": [], + "creation": "2020-01-14 11:28:37.583656", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "fieldname", + "label", + "fieldtype", + "options", + "width" + ], + "fields": [ + { + "fieldname": "fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Fieldname", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "fieldname": "fieldtype", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Fieldtype", + "options": "Check\nCurrency\nData\nDate\nDatetime\nDuration\nDynamic Link\nFloat\nFold\nInt\nLink\nSelect\nTime", + "reqd": 1 + }, + { + "fieldname": "options", + "fieldtype": "Data", + "label": "Options" + }, + { + "fieldname": "width", + "fieldtype": "Int", + "label": "Width" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-09-03 10:52:03.895817", + "modified_by": "Administrator", + "module": "Core", + "name": "Report Column", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/report_column/report_column.py b/xhiveframework/core/doctype/report_column/report_column.py new file mode 100644 index 0000000..79d78ad --- /dev/null +++ b/xhiveframework/core/doctype/report_column/report_column.py @@ -0,0 +1,40 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class ReportColumn(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + fieldname: DF.Data + fieldtype: DF.Literal[ + "Check", + "Currency", + "Data", + "Date", + "Datetime", + "Duration", + "Dynamic Link", + "Float", + "Fold", + "Int", + "Link", + "Select", + "Time", + ] + label: DF.Data + options: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + width: DF.Int + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/report_filter/__init__.py b/xhiveframework/core/doctype/report_filter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/report_filter/report_filter.json b/xhiveframework/core/doctype/report_filter/report_filter.json new file mode 100644 index 0000000..f0800a0 --- /dev/null +++ b/xhiveframework/core/doctype/report_filter/report_filter.json @@ -0,0 +1,90 @@ +{ + "actions": [], + "creation": "2020-01-14 11:38:58.016498", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "label", + "fieldtype", + "fieldname", + "mandatory", + "wildcard_filter", + "column_break_urrx", + "options", + "default" + ], + "fields": [ + { + "columns": 2, + "fieldname": "fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Fieldname", + "reqd": 1 + }, + { + "columns": 2, + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "columns": 2, + "fieldname": "fieldtype", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Fieldtype", + "options": "Check\nCurrency\nData\nDate\nDatetime\nDynamic Link\nFloat\nFold\nInt\nLink\nSelect\nTime", + "reqd": 1 + }, + { + "columns": 1, + "default": "0", + "fieldname": "mandatory", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Mandatory" + }, + { + "columns": 2, + "fieldname": "options", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Options" + }, + { + "default": "0", + "description": "Will add \"%\" before and after the query", + "fieldname": "wildcard_filter", + "fieldtype": "Check", + "label": "Wildcard Filter" + }, + { + "fieldname": "default", + "fieldtype": "Small Text", + "label": "Default" + }, + { + "columns": 2, + "fieldname": "column_break_urrx", + "fieldtype": "Column Break" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-06-15 11:37:35.961036", + "modified_by": "Administrator", + "module": "Core", + "name": "Report Filter", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/report_filter/report_filter.py b/xhiveframework/core/doctype/report_filter/report_filter.py new file mode 100644 index 0000000..3053607 --- /dev/null +++ b/xhiveframework/core/doctype/report_filter/report_filter.py @@ -0,0 +1,41 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class ReportFilter(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + default: DF.SmallText | None + fieldname: DF.Data + fieldtype: DF.Literal[ + "Check", + "Currency", + "Data", + "Date", + "Datetime", + "Dynamic Link", + "Float", + "Fold", + "Int", + "Link", + "Select", + "Time", + ] + label: DF.Data + mandatory: DF.Check + options: DF.SmallText | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + wildcard_filter: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/role/README.md b/xhiveframework/core/doctype/role/README.md new file mode 100644 index 0000000..fe8643e --- /dev/null +++ b/xhiveframework/core/doctype/role/README.md @@ -0,0 +1 @@ +Roles are be assigned to users and permissions on DocTypes are defined on Roles. Standard roles are "Administrator" (developer), "Guest" and "System Manager" (system, user, permission administrator) \ No newline at end of file diff --git a/xhiveframework/core/doctype/role/__init__.py b/xhiveframework/core/doctype/role/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/doctype/role/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/doctype/role/patches/v13_set_default_desk_properties.py b/xhiveframework/core/doctype/role/patches/v13_set_default_desk_properties.py new file mode 100644 index 0000000..66f8340 --- /dev/null +++ b/xhiveframework/core/doctype/role/patches/v13_set_default_desk_properties.py @@ -0,0 +1,11 @@ +import xhiveframework + +from ..role import desk_properties + + +def execute(): + for role in xhiveframework.get_all("Role", ["name", "desk_access"]): + role_doc = xhiveframework.get_doc("Role", role.name) + for key in desk_properties: + role_doc.set(key, role_doc.desk_access) + role_doc.save() diff --git a/xhiveframework/core/doctype/role/role.js b/xhiveframework/core/doctype/role/role.js new file mode 100644 index 0000000..5436428 --- /dev/null +++ b/xhiveframework/core/doctype/role/role.js @@ -0,0 +1,29 @@ +// Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See LICENSE + +xhiveframework.ui.form.on("Role", { + refresh: function (frm) { + if (frm.doc.name === "All") { + frm.dashboard.add_comment( + __("Role 'All' will be given to all system + website users."), + "yellow" + ); + } else if (frm.doc.name === "Desk User") { + frm.dashboard.add_comment( + __("Role 'Desk User' will be given to all system users."), + "yellow" + ); + } + + frm.set_df_property("is_custom", "read_only", xhiveframework.session.user !== "Administrator"); + + frm.add_custom_button("Role Permissions Manager", function () { + xhiveframework.route_options = { role: frm.doc.name }; + xhiveframework.set_route("permission-manager"); + }); + frm.add_custom_button("Show Users", function () { + xhiveframework.route_options = { role: frm.doc.name }; + xhiveframework.set_route("List", "User", "Report"); + }); + }, +}); diff --git a/xhiveframework/core/doctype/role/role.json b/xhiveframework/core/doctype/role/role.json new file mode 100644 index 0000000..c963bad --- /dev/null +++ b/xhiveframework/core/doctype/role/role.json @@ -0,0 +1,176 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:role_name", + "creation": "2013-01-08 15:50:01", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "role_name", + "home_page", + "restrict_to_domain", + "column_break_4", + "disabled", + "is_custom", + "desk_access", + "two_factor_auth", + "navigation_settings_section", + "search_bar", + "notifications", + "list_settings_section", + "list_sidebar", + "bulk_actions", + "view_switcher", + "form_settings_section", + "form_sidebar", + "timeline", + "dashboard" + ], + "fields": [ + { + "fieldname": "role_name", + "fieldtype": "Data", + "label": "Role Name", + "oldfieldname": "role_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "description": "If disabled, this role will be removed from all users.", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "default": "1", + "fieldname": "desk_access", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Desk Access" + }, + { + "default": "0", + "fieldname": "two_factor_auth", + "fieldtype": "Check", + "label": "Two Factor Authentication" + }, + { + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict To Domain", + "options": "Domain" + }, + { + "description": "Route: Example \"/app\"", + "fieldname": "home_page", + "fieldtype": "Data", + "label": "Home Page" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "navigation_settings_section", + "fieldtype": "Section Break", + "label": "Navigation Settings" + }, + { + "default": "1", + "fieldname": "search_bar", + "fieldtype": "Check", + "label": "Search Bar" + }, + { + "fieldname": "list_settings_section", + "fieldtype": "Section Break", + "label": "List Settings" + }, + { + "default": "1", + "fieldname": "list_sidebar", + "fieldtype": "Check", + "label": "Sidebar" + }, + { + "default": "1", + "fieldname": "bulk_actions", + "fieldtype": "Check", + "label": "Bulk Actions" + }, + { + "fieldname": "form_settings_section", + "fieldtype": "Section Break", + "label": "Form Settings" + }, + { + "default": "1", + "fieldname": "form_sidebar", + "fieldtype": "Check", + "label": "Sidebar" + }, + { + "default": "1", + "fieldname": "timeline", + "fieldtype": "Check", + "label": "Timeline" + }, + { + "default": "1", + "fieldname": "dashboard", + "fieldtype": "Check", + "label": "Dashboard" + }, + { + "default": "1", + "fieldname": "view_switcher", + "fieldtype": "Check", + "label": "View Switcher" + }, + { + "default": "1", + "fieldname": "notifications", + "fieldtype": "Check", + "label": "Notifications" + }, + { + "default": "0", + "fieldname": "is_custom", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Custom" + } + ], + "icon": "fa fa-bookmark", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-03-13 20:59:37.875253", + "modified_by": "Administrator", + "module": "Core", + "name": "Role", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1, + "translated_doctype": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/role/role.py b/xhiveframework/core/doctype/role/role.py new file mode 100644 index 0000000..794fb4f --- /dev/null +++ b/xhiveframework/core/doctype/role/role.py @@ -0,0 +1,153 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.website.path_resolver import validate_path + +desk_properties = ( + "search_bar", + "notifications", + "list_sidebar", + "bulk_actions", + "view_switcher", + "form_sidebar", + "timeline", + "dashboard", +) +from xhiveframework.website.router import clear_routing_cache + +STANDARD_ROLES = ("Administrator", "System Manager", "Script Manager", "All", "Guest") + + +class Role(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + bulk_actions: DF.Check + dashboard: DF.Check + desk_access: DF.Check + disabled: DF.Check + form_sidebar: DF.Check + home_page: DF.Data | None + is_custom: DF.Check + list_sidebar: DF.Check + notifications: DF.Check + restrict_to_domain: DF.Link | None + role_name: DF.Data + search_bar: DF.Check + timeline: DF.Check + two_factor_auth: DF.Check + view_switcher: DF.Check + + # end: auto-generated types + def before_rename(self, old, new, merge=False): + if old in STANDARD_ROLES: + xhiveframework.throw(xhiveframework._("Standard roles cannot be renamed")) + + def after_insert(self): + xhiveframework.cache.hdel("roles", "Administrator") + + def validate(self): + if self.disabled: + self.disable_role() + else: + self.set_desk_properties() + self.validate_homepage() + + def disable_role(self): + if self.name in STANDARD_ROLES: + xhiveframework.throw(xhiveframework._("Standard roles cannot be disabled")) + else: + self.remove_roles() + + def validate_homepage(self): + if xhiveframework.request and self.home_page: + validate_path(self.home_page) + + if self.has_value_changed("home_page"): + clear_routing_cache() + + def set_desk_properties(self): + # set if desk_access is not allowed, unset all desk properties + if self.name == "Guest": + self.desk_access = 0 + + if not self.desk_access: + for key in desk_properties: + self.set(key, 0) + + def remove_roles(self): + xhiveframework.db.delete("Has Role", {"role": self.name}) + xhiveframework.clear_cache() + + def on_update(self): + """update system user desk access if this has changed in this update""" + if xhiveframework.flags.in_install: + return + if self.has_value_changed("desk_access"): + self.update_user_type_on_change() + + def update_user_type_on_change(self): + """When desk access changes, all the users that have this role need to be re-evaluated""" + + users_with_role = get_users(self.name) + + # perf: Do not re-evaluate users who already have same desk access that this role permits. + role_user_type = "System User" if self.desk_access else "Website User" + users_with_same_user_type = xhiveframework.get_all("User", {"user_type": role_user_type}, pluck="name") + + for user_name in set(users_with_role) - set(users_with_same_user_type): + user = xhiveframework.get_doc("User", user_name) + user_type = user.user_type + user.set_system_user() + if user_type != user.user_type: + user.save() + + +def get_info_based_on_role(role, field="email", ignore_permissions=False): + """Get information of all users that have been assigned this role""" + users = xhiveframework.get_list( + "Has Role", + filters={"role": role, "parenttype": "User"}, + parent_doctype="User", + fields=["parent as user_name"], + ignore_permissions=ignore_permissions, + ) + + return get_user_info(users, field) + + +def get_user_info(users, field="email"): + """Fetch details about users for the specified field""" + info_list = [] + for user in users: + user_info, enabled = xhiveframework.db.get_value("User", user.get("user_name"), [field, "enabled"]) + if enabled and user_info not in ["admin@example.com", "guest@example.com"]: + info_list.append(user_info) + return info_list + + +def get_users(role): + return [ + d.parent + for d in xhiveframework.get_all("Has Role", filters={"role": role, "parenttype": "User"}, fields=["parent"]) + ] + + +# searches for active employees +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def role_query(doctype, txt, searchfield, start, page_len, filters): + report_filters = [["Role", "name", "like", f"%{txt}%"], ["Role", "is_custom", "=", 0]] + if filters and isinstance(filters, list): + report_filters.extend(filters) + + return xhiveframework.get_all( + "Role", limit_start=start, limit_page_length=page_len, filters=report_filters, as_list=1 + ) diff --git a/xhiveframework/core/doctype/role/test_records.json b/xhiveframework/core/doctype/role/test_records.json new file mode 100644 index 0000000..49442e6 --- /dev/null +++ b/xhiveframework/core/doctype/role/test_records.json @@ -0,0 +1,22 @@ +[ + { + "doctype": "Role", + "role_name": "_Test Role", + "desk_access": 1 + }, + { + "doctype": "Role", + "role_name": "_Test Role 2", + "desk_access": 1 + }, + { + "doctype": "Role", + "role_name": "_Test Role 3", + "desk_access": 1 + }, + { + "doctype": "Role", + "role_name": "_Test Role 4", + "desk_access": 0 + } +] \ No newline at end of file diff --git a/xhiveframework/core/doctype/role/test_role.py b/xhiveframework/core/doctype/role/test_role.py new file mode 100644 index 0000000..611fcc8 --- /dev/null +++ b/xhiveframework/core/doctype/role/test_role.py @@ -0,0 +1,53 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.core.doctype.role.role import get_info_based_on_role +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Role") + + +class TestUser(XhiveFrameworkTestCase): + def test_disable_role(self): + xhiveframework.get_doc("User", "test@example.com").add_roles("_Test Role 3") + + role = xhiveframework.get_doc("Role", "_Test Role 3") + role.disabled = 1 + role.save() + + self.assertTrue("_Test Role 3" not in xhiveframework.get_roles("test@example.com")) + + role = xhiveframework.get_doc("Role", "_Test Role 3") + role.disabled = 0 + role.save() + + xhiveframework.get_doc("User", "test@example.com").add_roles("_Test Role 3") + self.assertTrue("_Test Role 3" in xhiveframework.get_roles("test@example.com")) + + def test_change_desk_access(self): + """if we change desk acecss from role, remove from user""" + xhiveframework.delete_doc_if_exists("User", "test-user-for-desk-access@example.com") + xhiveframework.delete_doc_if_exists("Role", "desk-access-test") + user = xhiveframework.get_doc( + dict(doctype="User", email="test-user-for-desk-access@example.com", first_name="test") + ).insert() + role = xhiveframework.get_doc(dict(doctype="Role", role_name="desk-access-test", desk_access=0)).insert() + user.add_roles(role.name) + user.save() + self.assertTrue(user.user_type == "Website User") + role.desk_access = 1 + role.save() + user.reload() + self.assertTrue(user.user_type == "System User") + role.desk_access = 0 + role.save() + user.reload() + self.assertTrue(user.user_type == "Website User") + + def test_get_users_by_role(self): + role = "System Manager" + sys_managers = get_info_based_on_role(role, field="name") + + for user in sys_managers: + self.assertIn(role, xhiveframework.get_roles(user)) diff --git a/xhiveframework/core/doctype/role_permission_for_page_and_report/__init__.py b/xhiveframework/core/doctype/role_permission_for_page_and_report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.js b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.js new file mode 100644 index 0000000..895ae34 --- /dev/null +++ b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.js @@ -0,0 +1,127 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Role Permission for Page and Report", { + setup: function (frm) { + frm.trigger("set_queries"); + }, + + refresh: function (frm) { + frm.disable_save(); + frm.role_area.hide(); + frm.events.setup_buttons(frm); + }, + + setup_buttons: function (frm) { + frm.clear_custom_buttons(); + frm.page.clear_actions(); + if (frm.doc.set_role_for && frm.doc[xhiveframework.model.scrub(frm.doc.set_role_for)]) { + frm.add_custom_button(__("Reset to defaults"), function () { + frm.trigger("reset_roles"); + }); + + frm.page.set_primary_action(__("Update"), () => { + frm.trigger("update_report_page_data"); + }); + } + }, + + onload: function (frm) { + if (!frm.roles_editor) { + frm.role_area = $(frm.fields_dict.roles_html.wrapper); + frm.roles_editor = new xhiveframework.RoleEditor(frm.role_area, frm); + } + }, + + set_queries: function (frm) { + frm.set_query("page", function () { + return { + filters: { + system_page: 0, + }, + }; + }); + }, + + set_role_for: function (frm) { + frm.trigger("clear_fields"); + frm.toggle_display("roles_html", false); + }, + + clear_fields: function (frm) { + var field = frm.doc.set_role_for == "Report" ? "page" : "report"; + frm.set_value(field, ""); + }, + + page: function (frm) { + frm.events.setup_buttons(frm); + if (frm.doc.page) { + frm.trigger("set_report_page_data"); + } else { + frm.trigger("set_role_for"); + } + }, + + report: function (frm) { + frm.events.setup_buttons(frm); + if (frm.doc.report) { + frm.trigger("set_report_page_data"); + } else { + frm.trigger("set_role_for"); + } + }, + + set_report_page_data: function (frm) { + frm.toggle_display("roles_html", true); + frm.role_area.show(); + + return frm.call({ + method: "set_report_page_data", + doc: frm.doc, + callback: function (r) { + refresh_field("roles"); + frm.roles_editor.show(); + }, + }); + }, + + update_report_page_data: function (frm) { + frm.trigger("validate_mandatory_fields"); + if (frm.roles_editor) { + frm.roles_editor.set_roles_in_table(); + } + + return frm.call({ + method: "update_report_page_data", + doc: frm.doc, + callback: function (r) { + refresh_field("roles"); + frm.roles_editor.show(); + xhiveframework.msgprint(__("Successfully Updated")); + }, + }); + }, + + reset_roles: function (frm) { + frm.trigger("validate_mandatory_fields"); + return frm.call({ + method: "reset_roles", + doc: frm.doc, + callback: function (r) { + refresh_field("roles"); + frm.roles_editor.show(); + xhiveframework.msgprint(__("Successfully Updated")); + }, + }); + }, + + validate_mandatory_fields: function (frm) { + if (!frm.doc.set_role_for) { + xhiveframework.throw(__("Mandatory field: set role for")); + } + + if (frm.doc.set_role_for && !frm.doc[frm.doc.set_role_for.toLocaleLowerCase()]) { + xhiveframework.throw(__("Mandatory field: {0}", [frm.doc.set_role_for])); + } + }, +}); diff --git a/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.json b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.json new file mode 100644 index 0000000..52ecc5d --- /dev/null +++ b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.json @@ -0,0 +1,95 @@ +{ + "actions": [], + "allow_copy": 1, + "creation": "2017-02-13 17:33:25.157332", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "set_role_for", + "page", + "report", + "column_break_4", + "enable_prepared_report", + "roles_permission", + "roles_html", + "roles" + ], + "fields": [ + { + "fieldname": "set_role_for", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Set Role For", + "options": "\nPage\nReport", + "reqd": 1 + }, + { + "depends_on": "eval:doc.set_role_for == 'Page'", + "fieldname": "page", + "fieldtype": "Link", + "label": "Page", + "options": "Page" + }, + { + "depends_on": "eval:doc.set_role_for == 'Report'", + "fieldname": "report", + "fieldtype": "Link", + "label": "Report", + "options": "Report" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "roles_permission", + "fieldtype": "Section Break", + "label": "Allow Roles" + }, + { + "fieldname": "roles_html", + "fieldtype": "HTML", + "label": "Roles Html" + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "hidden": 1, + "label": "Roles", + "options": "Has Role", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "report", + "fieldname": "enable_prepared_report", + "fieldtype": "Check", + "label": "Enable Prepared Report" + } + ], + "hide_toolbar": 1, + "issingle": 1, + "links": [], + "modified": "2022-11-23 12:39:05.750386", + "modified_by": "Administrator", + "module": "Core", + "name": "Role Permission for Page and Report", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.py b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.py new file mode 100644 index 0000000..840dbe6 --- /dev/null +++ b/xhiveframework/core/doctype/role_permission_for_page_and_report/role_permission_for_page_and_report.py @@ -0,0 +1,107 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.core.doctype.report.report import is_prepared_report_enabled +from xhiveframework.model.document import Document +from xhiveframework.permissions import ALL_USER_ROLE +from xhiveframework.utils import cint + + +class RolePermissionforPageandReport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.types import DF + + enable_prepared_report: DF.Check + page: DF.Link | None + report: DF.Link | None + roles: DF.Table[HasRole] + set_role_for: DF.Literal["", "Page", "Report"] + + # end: auto-generated types + @xhiveframework.whitelist() + def set_report_page_data(self): + self.set_custom_roles() + self.check_prepared_report_disabled() + + def set_custom_roles(self): + args = self.get_args() + self.set("roles", []) + + name = xhiveframework.db.get_value("Custom Role", args, "name") + if name: + doc = xhiveframework.get_doc("Custom Role", name) + roles = doc.roles + else: + roles = self.get_standard_roles() + + self.set("roles", roles) + + def check_prepared_report_disabled(self): + if self.report: + self.enable_prepared_report = is_prepared_report_enabled(self.report) + + def get_standard_roles(self): + doctype = self.set_role_for + docname = self.page if self.set_role_for == "Page" else self.report + doc = xhiveframework.get_doc(doctype, docname) + return doc.roles + + @xhiveframework.whitelist() + def reset_roles(self): + roles = self.get_standard_roles() + self.set("roles", roles) + self.update_custom_roles() + self.update_disable_prepared_report() + + @xhiveframework.whitelist() + def update_report_page_data(self): + self.update_custom_roles() + self.update_disable_prepared_report() + + def update_custom_roles(self): + args = self.get_args() + name = xhiveframework.db.get_value("Custom Role", args, "name") + + args.update({"doctype": "Custom Role", "roles": self.get_roles()}) + + if self.report: + args.update({"ref_doctype": xhiveframework.db.get_value("Report", self.report, "ref_doctype")}) + + if name: + custom_role = xhiveframework.get_doc("Custom Role", name) + custom_role.set("roles", self.get_roles()) + custom_role.save() + else: + xhiveframework.get_doc(args).insert() + + def update_disable_prepared_report(self): + if self.report: + # intentionally written update query in xhiveframework.db.sql instead of xhiveframework.db.set_value + xhiveframework.db.sql( + """update `tabReport` set prepared_report = %s + where name = %s""", + (self.enable_prepared_report, self.report), + ) + + def get_args(self, row=None): + name = self.page if self.set_role_for == "Page" else self.report + check_for_field = self.set_role_for.replace(" ", "_").lower() + + return {check_for_field: name} + + def get_roles(self): + return [ + {"role": data.role, "parenttype": "Custom Role"} + for data in self.roles + if data.role != ALL_USER_ROLE + ] + + def update_status(self): + return xhiveframework.render_template diff --git a/xhiveframework/core/doctype/role_permission_for_page_and_report/test_role_permission_for_page_and_report.py b/xhiveframework/core/doctype/role_permission_for_page_and_report/test_role_permission_for_page_and_report.py new file mode 100644 index 0000000..c3ec850 --- /dev/null +++ b/xhiveframework/core/doctype/role_permission_for_page_and_report/test_role_permission_for_page_and_report.py @@ -0,0 +1,9 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestRolePermissionforPageandReport(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/role_profile/__init__.py b/xhiveframework/core/doctype/role_profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/role_profile/role_profile.js b/xhiveframework/core/doctype/role_profile/role_profile.js new file mode 100644 index 0000000..0c21908 --- /dev/null +++ b/xhiveframework/core/doctype/role_profile/role_profile.js @@ -0,0 +1,20 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Role Profile", { + refresh: function (frm) { + if (has_common(xhiveframework.user_roles, ["Administrator", "System Manager"])) { + if (!frm.roles_editor) { + const role_area = $(frm.fields_dict.roles_html.wrapper); + frm.roles_editor = new xhiveframework.RoleEditor(role_area, frm); + } + frm.roles_editor.show(); + } + }, + + validate: function (frm) { + if (frm.roles_editor) { + frm.roles_editor.set_roles_in_table(); + } + }, +}); diff --git a/xhiveframework/core/doctype/role_profile/role_profile.json b/xhiveframework/core/doctype/role_profile/role_profile.json new file mode 100644 index 0000000..7cd60a1 --- /dev/null +++ b/xhiveframework/core/doctype/role_profile/role_profile.json @@ -0,0 +1,80 @@ +{ + "actions": [], + "autoname": "role_profile", + "creation": "2017-08-31 04:16:38.764465", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "role_profile", + "roles_html", + "roles" + ], + "fields": [ + { + "fieldname": "role_profile", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Role Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "roles_html", + "fieldtype": "HTML", + "label": "Roles HTML", + "read_only": 1 + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "hidden": 1, + "label": "Roles Assigned", + "options": "Has Role", + "permlevel": 1, + "print_hide": 1, + "read_only": 1 + } + ], + "links": [ + { + "link_doctype": "User", + "link_fieldname": "role_profile_name" + } + ], + "modified": "2021-12-03 15:45:45.270963", + "modified_by": "Administrator", + "module": "Core", + "name": "Role Profile", + "naming_rule": "Expression (old style)", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "permlevel": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "role_profile", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/role_profile/role_profile.py b/xhiveframework/core/doctype/role_profile/role_profile.py new file mode 100644 index 0000000..a6f4629 --- /dev/null +++ b/xhiveframework/core/doctype/role_profile/role_profile.py @@ -0,0 +1,57 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from collections import defaultdict + +import xhiveframework +from xhiveframework.model.document import Document + + +class RoleProfile(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.types import DF + + role_profile: DF.Data + roles: DF.Table[HasRole] + + # end: auto-generated types + def autoname(self): + """set name as Role Profile name""" + self.name = self.role_profile + + def on_update(self): + self.queue_action( + "update_all_users", + now=xhiveframework.flags.in_test or xhiveframework.flags.in_install, + enqueue_after_commit=True, + ) + + def update_all_users(self): + """Changes in role_profile reflected across all its user""" + has_role = xhiveframework.qb.DocType("Has Role") + user = xhiveframework.qb.DocType("User") + + all_current_roles = ( + xhiveframework.qb.from_(user) + .join(has_role) + .on(user.name == has_role.parent) + .where(user.role_profile_name == self.name) + .select(user.name, has_role.role) + ).run() + + user_roles = defaultdict(set) + for user, role in all_current_roles: + user_roles[user].add(role) + + role_profile_roles = {role.role for role in self.roles} + for user, roles in user_roles.items(): + if roles != role_profile_roles: + user = xhiveframework.get_doc("User", user) + user.roles = [] + user.add_roles(*role_profile_roles) diff --git a/xhiveframework/core/doctype/role_profile/test_role_profile.py b/xhiveframework/core/doctype/role_profile/test_role_profile.py new file mode 100644 index 0000000..8cfb85d --- /dev/null +++ b/xhiveframework/core/doctype/role_profile/test_role_profile.py @@ -0,0 +1,46 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["Role"] + + +class TestRoleProfile(XhiveFrameworkTestCase): + def test_make_new_role_profile(self): + xhiveframework.delete_doc_if_exists("Role Profile", "Test 1", force=1) + new_role_profile = xhiveframework.get_doc(dict(doctype="Role Profile", role_profile="Test 1")).insert() + + self.assertEqual(new_role_profile.role_profile, "Test 1") + + # add role + new_role_profile.append("roles", {"role": "_Test Role 2"}) + new_role_profile.save() + self.assertEqual(new_role_profile.roles[0].role, "_Test Role 2") + + # user with a role profile + random_user = xhiveframework.mock("email") + random_user_name = xhiveframework.mock("name") + + random_user = xhiveframework.get_doc( + { + "doctype": "User", + "email": random_user, + "enabled": 1, + "first_name": random_user_name, + "new_password": "Eastern_43A1W", + "role_profile_name": "Test 1", + } + ).insert(ignore_permissions=True, ignore_if_duplicate=True) + self.assertListEqual( + [role.role for role in random_user.roles], [role.role for role in new_role_profile.roles] + ) + + # clear roles + new_role_profile.roles = [] + new_role_profile.save() + self.assertEqual(new_role_profile.roles, []) + + # user roles with the role profile should also be updated + random_user.reload() + self.assertListEqual(random_user.roles, []) diff --git a/xhiveframework/core/doctype/rq_job/__init__.py b/xhiveframework/core/doctype/rq_job/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/rq_job/rq_job.js b/xhiveframework/core/doctype/rq_job/rq_job.js new file mode 100644 index 0000000..0e05964 --- /dev/null +++ b/xhiveframework/core/doctype/rq_job/rq_job.js @@ -0,0 +1,32 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("RQ Job", { + refresh: function (frm) { + // Nothing in this form is supposed to be editable. + frm.disable_form(); + frm.dashboard.set_headline_alert( + __("This is a virtual doctype and data is cleared periodically.") + ); + + if (["started", "queued"].includes(frm.doc.status)) { + frm.add_custom_button(__("Force Stop job"), () => { + xhiveframework.confirm( + __( + "This will terminate the job immediately and might be dangerous, are you sure? " + ), + () => { + xhiveframework + .xcall("xhiveframework.core.doctype.rq_job.rq_job.stop_job", { + job_id: frm.doc.name, + }) + .then((r) => { + xhiveframework.show_alert(__("Job Stopped Successfully")); + frm.reload_doc(); + }); + } + ); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/rq_job/rq_job.json b/xhiveframework/core/doctype/rq_job/rq_job.json new file mode 100644 index 0000000..6a39467 --- /dev/null +++ b/xhiveframework/core/doctype/rq_job/rq_job.json @@ -0,0 +1,163 @@ +{ + "actions": [], + "allow_copy": 1, + "autoname": "field:job_id", + "creation": "2023-03-22 20:05:22.962044", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "job_info_section", + "job_id", + "job_name", + "queue", + "timeout", + "column_break_5", + "arguments", + "job_status_section", + "status", + "time_taken", + "column_break_11", + "started_at", + "ended_at", + "exception_section", + "exc_info" + ], + "fields": [ + { + "fieldname": "queue", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Queue", + "options": "default\nshort\nlong" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "queued\nstarted\nfinished\nfailed\ndeferred\nscheduled\ncanceled" + }, + { + "fieldname": "job_id", + "fieldtype": "Data", + "label": "Job ID", + "unique": 1 + }, + { + "fieldname": "exc_info", + "fieldtype": "Code", + "label": "Exception" + }, + { + "fieldname": "job_name", + "fieldtype": "Data", + "label": "Job Name" + }, + { + "fieldname": "arguments", + "fieldtype": "Code", + "label": "Arguments" + }, + { + "fieldname": "timeout", + "fieldtype": "Duration", + "label": "Timeout" + }, + { + "fieldname": "time_taken", + "fieldtype": "Duration", + "label": "Time Taken" + }, + { + "fieldname": "started_at", + "fieldtype": "Datetime", + "label": "Started At" + }, + { + "fieldname": "ended_at", + "fieldtype": "Datetime", + "label": "Ended At" + }, + { + "fieldname": "job_info_section", + "fieldtype": "Section Break", + "label": "Job Info" + }, + { + "fieldname": "job_status_section", + "fieldtype": "Section Break", + "label": "Job Status" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_11", + "fieldtype": "Column Break" + }, + { + "fieldname": "exception_section", + "fieldtype": "Section Break" + } + ], + "hide_toolbar": 1, + "in_create": 1, + "is_virtual": 1, + "links": [], + "modified": "2024-01-13 10:38:40.230972", + "modified_by": "Administrator", + "module": "Core", + "name": "RQ Job", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + }, + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [ + { + "color": "Yellow", + "title": "queued" + }, + { + "color": "Blue", + "title": "started" + }, + { + "color": "Red", + "title": "failed" + }, + { + "color": "Green", + "title": "finished" + }, + { + "color": "Orange", + "title": "cancelled" + } + ], + "title_field": "job_name" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/rq_job/rq_job.py b/xhiveframework/core/doctype/rq_job/rq_job.py new file mode 100644 index 0000000..8c4f410 --- /dev/null +++ b/xhiveframework/core/doctype/rq_job/rq_job.py @@ -0,0 +1,230 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import functools +import re + +from rq.command import send_stop_job_command +from rq.exceptions import InvalidJobOperation, NoSuchJobError +from rq.job import Job +from rq.queue import Queue + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import ( + cint, + compare, + convert_utc_to_system_timezone, + create_batch, + make_filter_dict, +) +from xhiveframework.utils.background_jobs import get_queues, get_redis_conn + +QUEUES = ["default", "long", "short"] +JOB_STATUSES = ["queued", "started", "failed", "finished", "deferred", "scheduled", "canceled"] + + +def check_permissions(method): + @functools.wraps(method) + def wrapper(*args, **kwargs): + xhiveframework.only_for("System Manager") + job = args[0].job + if not for_current_site(job): + raise xhiveframework.PermissionError + + return method(*args, **kwargs) + + return wrapper + + +class RQJob(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + arguments: DF.Code | None + ended_at: DF.Datetime | None + exc_info: DF.Code | None + job_id: DF.Data | None + job_name: DF.Data | None + queue: DF.Literal["default", "short", "long"] + started_at: DF.Datetime | None + status: DF.Literal["queued", "started", "finished", "failed", "deferred", "scheduled", "canceled"] + time_taken: DF.Duration | None + timeout: DF.Duration | None + # end: auto-generated types + + def load_from_db(self): + try: + job = Job.fetch(self.name, connection=get_redis_conn()) + except NoSuchJobError: + raise xhiveframework.DoesNotExistError + + if not for_current_site(job): + raise xhiveframework.PermissionError + + super(Document, self).__init__(serialize_job(job)) + self._job_obj = job + + @property + def job(self): + return self._job_obj + + @staticmethod + def get_list(args): + start = cint(args.get("start")) or 0 + page_length = cint(args.get("page_length")) or 20 + + order_desc = "desc" in args.get("order_by", "") + + matched_job_ids = RQJob.get_matching_job_ids(args)[start : start + page_length] + + conn = get_redis_conn() + jobs = [serialize_job(job) for job in Job.fetch_many(job_ids=matched_job_ids, connection=conn) if job] + + return sorted(jobs, key=lambda j: j.modified, reverse=order_desc) + + @staticmethod + def get_matching_job_ids(args) -> list[str]: + filters = make_filter_dict(args.get("filters")) + + queues = _eval_filters(filters.get("queue"), QUEUES) + statuses = _eval_filters(filters.get("status"), JOB_STATUSES) + + matched_job_ids = [] + for queue in get_queues(): + if not queue.name.endswith(tuple(queues)): + continue + for status in statuses: + matched_job_ids.extend(fetch_job_ids(queue, status)) + + return filter_current_site_jobs(matched_job_ids) + + @check_permissions + def delete(self): + self.job.delete() + + @check_permissions + def stop_job(self): + try: + send_stop_job_command(connection=get_redis_conn(), job_id=self.job_id) + except InvalidJobOperation: + xhiveframework.msgprint(_("Job is not running."), title=_("Invalid Operation")) + + @staticmethod + def get_count(args) -> int: + return len(RQJob.get_matching_job_ids(args)) + + # None of these methods apply to virtual job doctype, overriden for sanity. + @staticmethod + def get_stats(args): + return {} + + def db_insert(self, *args, **kwargs): + pass + + def db_update(self, *args, **kwargs): + pass + + +def serialize_job(job: Job) -> xhiveframework._dict: + modified = job.last_heartbeat or job.ended_at or job.started_at or job.created_at + job_kwargs = job.kwargs.get("kwargs", {}) + job_name = job_kwargs.get("job_type") or str(job.kwargs.get("job_name")) + if job_name == "xhiveframework.utils.background_jobs.run_doc_method": + doctype = job_kwargs.get("doctype") + doc_method = job_kwargs.get("doc_method") + if doctype and doc_method: + job_name = f"{doctype}.{doc_method}" + + # function objects have this repr: '' + # This regex just removes unnecessary things around it. + if matches := re.match(r".*) at 0x.*>", job_name): + job_name = matches.group("func_name") + + return xhiveframework._dict( + name=job.id, + job_id=job.id, + queue=job.origin.rsplit(":", 1)[1], + job_name=job_name, + status=job.get_status(), + started_at=convert_utc_to_system_timezone(job.started_at) if job.started_at else "", + ended_at=convert_utc_to_system_timezone(job.ended_at) if job.ended_at else "", + time_taken=(job.ended_at - job.started_at).total_seconds() if job.ended_at else "", + exc_info=job.exc_info, + arguments=xhiveframework.as_json(job.kwargs), + timeout=job.timeout, + creation=convert_utc_to_system_timezone(job.created_at), + modified=convert_utc_to_system_timezone(modified), + _comment_count=0, + owner=job.kwargs.get("user"), + modified_by=job.kwargs.get("user"), + ) + + +def for_current_site(job: Job) -> bool: + return job.kwargs.get("site") == xhiveframework.local.site + + +def filter_current_site_jobs(job_ids: list[str]) -> list[str]: + site = xhiveframework.local.site + + return [j for j in job_ids if j.startswith(site)] + + +def _eval_filters(filter, values: list[str]) -> list[str]: + if filter: + operator, operand = filter + return [val for val in values if compare(val, operator, operand)] + return values + + +def fetch_job_ids(queue: Queue, status: str) -> list[str]: + registry_map = { + "queued": queue, # self + "started": queue.started_job_registry, + "finished": queue.finished_job_registry, + "failed": queue.failed_job_registry, + "deferred": queue.deferred_job_registry, + "scheduled": queue.scheduled_job_registry, + "canceled": queue.canceled_job_registry, + } + + registry = registry_map.get(status) + if registry is not None: + job_ids = registry.get_job_ids() + return [j for j in job_ids if j] + + return [] + + +@xhiveframework.whitelist() +def remove_failed_jobs(): + xhiveframework.only_for("System Manager") + for queue in get_queues(): + fail_registry = queue.failed_job_registry + failed_jobs = filter_current_site_jobs(fail_registry.get_job_ids()) + + # Delete in batches to avoid loading too many things in memory + conn = get_redis_conn() + for job_ids in create_batch(failed_jobs, 100): + for job in Job.fetch_many(job_ids=job_ids, connection=conn): + job and fail_registry.remove(job, delete_job=True) + + +def get_all_queued_jobs(): + jobs = [] + for q in get_queues(): + jobs.extend(q.get_jobs()) + + return [job for job in jobs if for_current_site(job)] + + +@xhiveframework.whitelist() +def stop_job(job_id): + xhiveframework.get_doc("RQ Job", job_id).stop_job() diff --git a/xhiveframework/core/doctype/rq_job/rq_job_list.js b/xhiveframework/core/doctype/rq_job/rq_job_list.js new file mode 100644 index 0000000..ea1dae2 --- /dev/null +++ b/xhiveframework/core/doctype/rq_job/rq_job_list.js @@ -0,0 +1,57 @@ +xhiveframework.listview_settings["RQ Job"] = { + hide_name_column: true, + + onload(listview) { + if (!has_common(xhiveframework.user_roles, ["Administrator", "System Manager"])) return; + + listview.page.add_inner_button( + __("Remove Failed Jobs"), + () => { + xhiveframework.confirm(__("Are you sure you want to remove all failed jobs?"), () => { + xhiveframework.xcall("xhiveframework.core.doctype.rq_job.rq_job.remove_failed_jobs"); + }); + }, + __("Actions") + ); + + xhiveframework.xcall("xhiveframework.utils.scheduler.get_scheduler_status").then(({ status }) => { + if (status === "active") { + listview.page.set_indicator(__("Scheduler: Active"), "green"); + } else { + listview.page.set_indicator(__("Scheduler: Inactive"), "red"); + listview.page.add_inner_button( + __("Enable Scheduler"), + () => { + xhiveframework.confirm(__("Are you sure you want to re-enable scheduler?"), () => { + xhiveframework + .xcall("xhiveframework.utils.scheduler.activate_scheduler") + .then(() => { + xhiveframework.show_alert(__("Enabled Scheduler")); + }) + .catch((e) => { + xhiveframework.show_alert({ + message: __("Failed to enable scheduler: {0}", e), + indicator: "error", + }); + }); + }); + }, + __("Actions") + ); + } + }); + + setInterval(() => { + if (listview.list_view_settings.disable_auto_refresh) { + return; + } + + const route = xhiveframework.get_route() || []; + if (route[0] != "List" || "RQ Job" != route[1]) { + return; + } + + listview.refresh(); + }, 15000); + }, +}; diff --git a/xhiveframework/core/doctype/rq_job/test_rq_job.py b/xhiveframework/core/doctype/rq_job/test_rq_job.py new file mode 100644 index 0000000..68a3bb4 --- /dev/null +++ b/xhiveframework/core/doctype/rq_job/test_rq_job.py @@ -0,0 +1,183 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors + +# See license.txt + +import time + +from rq import exceptions as rq_exc +from rq.job import Job + +import xhiveframework +from xhiveframework.core.doctype.rq_job.rq_job import RQJob, remove_failed_jobs, stop_job +from xhiveframework.installer import update_site_config +from xhiveframework.tests.utils import XhiveFrameworkTestCase, timeout +from xhiveframework.utils import cstr, execute_in_shell +from xhiveframework.utils.background_jobs import get_job_status, is_job_enqueued + + +@timeout(seconds=60) +def wait_for_completion(job: Job): + while True: + if not (job.is_queued or job.is_started): + break + time.sleep(0.2) + + +class TestRQJob(XhiveFrameworkTestCase): + BG_JOB = "xhiveframework.core.doctype.rq_job.test_rq_job.test_func" + + def check_status(self, job: Job, status, wait=True): + if wait: + wait_for_completion(job) + self.assertEqual(xhiveframework.get_doc("RQ Job", job.id).status, status) + + def test_serialization(self): + job = xhiveframework.enqueue(method=self.BG_JOB, queue="short") + rq_job = xhiveframework.get_doc("RQ Job", job.id) + + self.assertEqual(job, rq_job.job) + self.assertDocumentEqual( + { + "name": job.id, + "queue": "short", + "job_name": self.BG_JOB, + "exc_info": None, + }, + rq_job, + ) + self.check_status(job, "finished") + + def test_configurable_ttl(self): + xhiveframework.conf.rq_job_failure_ttl = 600 + job = xhiveframework.enqueue(method=self.BG_JOB, queue="short") + + self.assertEqual(job.failure_ttl, 600) + + def test_func_obj_serialization(self): + job = xhiveframework.enqueue(method=test_func, queue="short") + rq_job = xhiveframework.get_doc("RQ Job", job.id) + self.assertEqual(rq_job.job_name, "test_func") + + @timeout + def test_get_list_filtering(self): + # Check failed job clearning and filtering + remove_failed_jobs() + jobs = RQJob.get_list({"filters": [["RQ Job", "status", "=", "failed"]]}) + self.assertEqual(jobs, []) + + # Fail a job + job = xhiveframework.enqueue(method=self.BG_JOB, queue="short", fail=True) + self.check_status(job, "failed") + jobs = RQJob.get_list({"filters": [["RQ Job", "status", "=", "failed"]]}) + self.assertEqual(len(jobs), 1) + self.assertTrue(jobs[0].exc_info) + + # Assert that non-failed job still exists + non_failed_jobs = RQJob.get_list({"filters": [["RQ Job", "status", "!=", "failed"]]}) + self.assertGreaterEqual(len(non_failed_jobs), 1) + + # Create a slow job and check if it's stuck in "Started" + job = xhiveframework.enqueue(method=self.BG_JOB, queue="short", sleep=10) + time.sleep(3) + self.check_status(job, "started", wait=False) + stop_job(job_id=job.id) + self.check_status(job, "stopped") + + def test_delete_doc(self): + job = xhiveframework.enqueue(method=self.BG_JOB, queue="short") + xhiveframework.get_doc("RQ Job", job.id).delete() + + with self.assertRaises(rq_exc.NoSuchJobError): + job.refresh() + + @timeout + def test_multi_queue_burst_consumption(self): + for _ in range(3): + for q in ["default", "short"]: + xhiveframework.enqueue(self.BG_JOB, sleep=1, queue=q) + + _, stderr = execute_in_shell("bench worker --queue short,default --burst", check_exit_code=True) + self.assertIn("quitting", cstr(stderr)) + + @timeout + def test_multi_queue_burst_consumption_worker_pool(self): + for _ in range(3): + for q in ["default", "short"]: + xhiveframework.enqueue(self.BG_JOB, sleep=1, queue=q) + + _, stderr = execute_in_shell( + "bench worker-pool --queue short,default --burst --num-workers=4", check_exit_code=True + ) + self.assertIn("quitting", cstr(stderr)) + + def test_job_id_manual_dedup(self): + job_id = "test_dedup" + job = xhiveframework.enqueue(self.BG_JOB, sleep=5, job_id=job_id) + self.assertTrue(is_job_enqueued(job_id)) + self.check_status(job, "finished") + self.assertFalse(is_job_enqueued(job_id)) + + def test_auto_job_dedup(self): + job_id = "test_dedup" + job1 = xhiveframework.enqueue(self.BG_JOB, sleep=2, job_id=job_id, deduplicate=True) + job2 = xhiveframework.enqueue(self.BG_JOB, sleep=5, job_id=job_id, deduplicate=True) + self.assertIsNone(job2) + self.check_status(job1, "finished") # wait + + # Failed jobs last longer, subsequent job should still pass with same ID. + job3 = xhiveframework.enqueue(self.BG_JOB, fail=True, job_id=job_id, deduplicate=True) + self.check_status(job3, "failed") + job4 = xhiveframework.enqueue(self.BG_JOB, sleep=1, job_id=job_id, deduplicate=True) + self.check_status(job4, "finished") + + @timeout + def test_enqueue_after_commit(self): + job_id = xhiveframework.generate_hash() + + xhiveframework.enqueue(self.BG_JOB, enqueue_after_commit=True, job_id=job_id) + self.assertIsNone(get_job_status(job_id)) + + xhiveframework.db.commit() + self.assertIsNotNone(get_job_status(job_id)) + + job_id = xhiveframework.generate_hash() + xhiveframework.enqueue(self.BG_JOB, enqueue_after_commit=True, job_id=job_id) + self.assertIsNone(get_job_status(job_id)) + + xhiveframework.db.rollback() + self.assertIsNone(get_job_status(job_id)) + + xhiveframework.db.commit() + self.assertIsNone(get_job_status(job_id)) + + def test_memory_usage(self): + if xhiveframework.db.db_type != "mariadb": + return + job = xhiveframework.enqueue("xhiveframework.utils.data._get_rss_memory_usage") + self.check_status(job, "finished") + + rss = job.latest_result().return_value + msg = """Memory usage of simple background job increased. Potential root cause can be a newly added python module import. Check and move them to approriate file/function to avoid loading the module by default.""" + + # If this starts failing analyze memory usage using memray or some equivalent tool to find + # offending imports/function calls. + # Refer this PR: https://lab.membtech.com/xhiveframework/xhiveframework15/pull/21467 + LAST_MEASURED_USAGE = 41 + self.assertLessEqual(rss, LAST_MEASURED_USAGE * 1.05, msg) + + def test_clear_failed_jobs(self): + limit = 10 + update_site_config("rq_failed_jobs_limit", limit) + + jobs = [xhiveframework.enqueue(method=self.BG_JOB, queue="short", fail=True) for _ in range(limit * 2)] + self.check_status(jobs[-1], "failed") + self.assertLessEqual(RQJob.get_count({"filters": [["RQ Job", "status", "=", "failed"]]}), limit * 1.1) + + +def test_func(fail=False, sleep=0): + if fail: + 42 / 0 + if sleep: + time.sleep(sleep) + + return True diff --git a/xhiveframework/core/doctype/rq_worker/__init__.py b/xhiveframework/core/doctype/rq_worker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/rq_worker/rq_worker.js b/xhiveframework/core/doctype/rq_worker/rq_worker.js new file mode 100644 index 0000000..e3d79a4 --- /dev/null +++ b/xhiveframework/core/doctype/rq_worker/rq_worker.js @@ -0,0 +1,9 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("RQ Worker", { + refresh: function (frm) { + // Nothing in this form is supposed to be editable. + frm.disable_form(); + }, +}); diff --git a/xhiveframework/core/doctype/rq_worker/rq_worker.json b/xhiveframework/core/doctype/rq_worker/rq_worker.json new file mode 100644 index 0000000..cc301be --- /dev/null +++ b/xhiveframework/core/doctype/rq_worker/rq_worker.json @@ -0,0 +1,146 @@ +{ + "actions": [], + "allow_copy": 1, + "creation": "2022-09-10 14:54:57.342170", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "worker_information_section", + "queue", + "queue_type", + "column_break_4", + "worker_name", + "statistics_section", + "status", + "pid", + "current_job_id", + "successful_job_count", + "failed_job_count", + "column_break_12", + "birth_date", + "last_heartbeat", + "total_working_time", + "utilization_percent" + ], + "fields": [ + { + "fieldname": "worker_name", + "fieldtype": "Data", + "label": "Worker Name", + "unique": 1 + }, + { + "fieldname": "status", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Status" + }, + { + "fieldname": "current_job_id", + "fieldtype": "Link", + "label": "Current Job ID", + "options": "RQ Job" + }, + { + "fieldname": "pid", + "fieldtype": "Data", + "label": "PID" + }, + { + "fieldname": "last_heartbeat", + "fieldtype": "Datetime", + "label": "Last Heartbeat" + }, + { + "fieldname": "birth_date", + "fieldtype": "Datetime", + "label": "Start Time" + }, + { + "fieldname": "successful_job_count", + "fieldtype": "Int", + "label": "Successful Job Count" + }, + { + "fieldname": "failed_job_count", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Failed Job Count" + }, + { + "fieldname": "total_working_time", + "fieldtype": "Duration", + "label": "Total Working Time" + }, + { + "fieldname": "queue", + "fieldtype": "Data", + "label": "Queue(s)" + }, + { + "fieldname": "queue_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Queue Type(s)", + "options": "default\nlong\nshort" + }, + { + "fieldname": "worker_information_section", + "fieldtype": "Section Break", + "label": "Worker Information" + }, + { + "fieldname": "statistics_section", + "fieldtype": "Section Break", + "label": "Statistics" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, + { + "fieldname": "utilization_percent", + "fieldtype": "Percent", + "in_list_view": 1, + "label": "Utilization %" + } + ], + "hide_toolbar": 1, + "in_create": 1, + "is_virtual": 1, + "links": [], + "modified": "2024-02-29 19:31:08.502527", + "modified_by": "Administrator", + "module": "Core", + "name": "RQ Worker", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [ + { + "color": "Blue", + "title": "idle" + }, + { + "color": "Yellow", + "title": "busy" + } + ], + "title_field": "pid" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/rq_worker/rq_worker.py b/xhiveframework/core/doctype/rq_worker/rq_worker.py new file mode 100644 index 0000000..f386cb9 --- /dev/null +++ b/xhiveframework/core/doctype/rq_worker/rq_worker.py @@ -0,0 +1,109 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import datetime +from contextlib import suppress + +from rq import Worker + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, convert_utc_to_system_timezone +from xhiveframework.utils.background_jobs import get_workers + + +class RQWorker(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + birth_date: DF.Datetime | None + current_job_id: DF.Link | None + failed_job_count: DF.Int + last_heartbeat: DF.Datetime | None + pid: DF.Data | None + queue: DF.Data | None + queue_type: DF.Literal["default", "long", "short"] + status: DF.Data | None + successful_job_count: DF.Int + total_working_time: DF.Duration | None + utilization_percent: DF.Percent + worker_name: DF.Data | None + # end: auto-generated types + + def load_from_db(self): + all_workers = get_workers() + workers = [w for w in all_workers if w.name == self.name] + if not workers: + raise xhiveframework.DoesNotExistError + d = serialize_worker(workers[0]) + + super(Document, self).__init__(d) + + @staticmethod + def get_list(args): + start = cint(args.get("start")) or 0 + page_length = cint(args.get("page_length")) or 20 + + workers = get_workers() + + valid_workers = [w for w in workers if w.pid][start : start + page_length] + return [serialize_worker(worker) for worker in valid_workers] + + @staticmethod + def get_count(args) -> int: + return len(get_workers()) + + # None of these methods apply to virtual workers, overriden for sanity. + @staticmethod + def get_stats(args): + return {} + + def db_insert(self, *args, **kwargs): + pass + + def db_update(self, *args, **kwargs): + pass + + def delete(self): + pass + + +def serialize_worker(worker: Worker) -> xhiveframework._dict: + queue_names = worker.queue_names() + + queue = ", ".join(queue_names) + queue_types = ",".join(q.rsplit(":", 1)[1] for q in queue_names) + + current_job = worker.get_current_job_id() + if current_job and not current_job.startswith(xhiveframework.local.site): + current_job = None + + return xhiveframework._dict( + name=worker.name, + queue=queue, + queue_type=queue_types, + worker_name=worker.name, + status=worker.get_state(), + pid=worker.pid, + current_job_id=current_job, + last_heartbeat=convert_utc_to_system_timezone(worker.last_heartbeat), + birth_date=convert_utc_to_system_timezone(worker.birth_date), + successful_job_count=worker.successful_job_count, + failed_job_count=worker.failed_job_count, + total_working_time=worker.total_working_time, + _comment_count=0, + modified=convert_utc_to_system_timezone(worker.last_heartbeat), + creation=convert_utc_to_system_timezone(worker.birth_date), + utilization_percent=compute_utilization(worker), + ) + + +def compute_utilization(worker: Worker) -> float: + with suppress(Exception): + total_time = (datetime.datetime.utcnow() - worker.birth_date).total_seconds() + return worker.total_working_time / total_time * 100 diff --git a/xhiveframework/core/doctype/rq_worker/rq_worker_list.js b/xhiveframework/core/doctype/rq_worker/rq_worker_list.js new file mode 100644 index 0000000..4547e0d --- /dev/null +++ b/xhiveframework/core/doctype/rq_worker/rq_worker_list.js @@ -0,0 +1,9 @@ +xhiveframework.listview_settings["RQ Worker"] = { + refresh(listview) { + listview.$no_result.html(` +
    + ${__("No RQ Workers connected. Try restarting the bench.")} +
    + `); + }, +}; diff --git a/xhiveframework/core/doctype/rq_worker/test_rq_worker.py b/xhiveframework/core/doctype/rq_worker/test_rq_worker.py new file mode 100644 index 0000000..67a92c2 --- /dev/null +++ b/xhiveframework/core/doctype/rq_worker/test_rq_worker.py @@ -0,0 +1,17 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# See license.txt + +import xhiveframework +from xhiveframework.core.doctype.rq_worker.rq_worker import RQWorker +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestRQWorker(XhiveFrameworkTestCase): + def test_get_worker_list(self): + workers = RQWorker.get_list({}) + self.assertGreaterEqual(len(workers), 1) + self.assertTrue(any("short" in w.queue_type for w in workers)) + + def test_worker_serialization(self): + workers = RQWorker.get_list({}) + xhiveframework.get_doc("RQ Worker", workers[0].name) diff --git a/xhiveframework/core/doctype/scheduled_job_log/__init__.py b/xhiveframework/core/doctype/scheduled_job_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.js b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.js new file mode 100644 index 0000000..7971d2e --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Scheduled Job Log", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.json b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.json new file mode 100644 index 0000000..451c410 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.json @@ -0,0 +1,65 @@ +{ + "actions": [], + "creation": "2019-09-23 14:36:36.935869", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "status", + "scheduled_job_type", + "details" + ], + "fields": [ + { + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Scheduled\nComplete\nFailed", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "details", + "fieldtype": "Code", + "label": "Details", + "read_only": 1 + }, + { + "fieldname": "scheduled_job_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Scheduled Job", + "options": "Scheduled Job Type", + "read_only": 1, + "reqd": 1 + } + ], + "links": [], + "modified": "2022-06-13 05:41:21.090972", + "modified_by": "Administrator", + "module": "Core", + "name": "Scheduled Job Log", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "scheduled_job_type" +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.py b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.py new file mode 100644 index 0000000..59b3548 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log.py @@ -0,0 +1,27 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.query_builder import Interval +from xhiveframework.query_builder.functions import Now + + +class ScheduledJobLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + details: DF.Code | None + scheduled_job_type: DF.Link + status: DF.Literal["Scheduled", "Complete", "Failed"] + + # end: auto-generated types + @staticmethod + def clear_old_logs(days=90): + table = xhiveframework.qb.DocType("Scheduled Job Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) diff --git a/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log_list.js b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log_list.js new file mode 100644 index 0000000..d8c66f3 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_log/scheduled_job_log_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Scheduled Job Log"] = { + onload: function (listview) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(cur_list.doctype); + }); + }, +}; diff --git a/xhiveframework/core/doctype/scheduled_job_log/test_scheduled_job_log.py b/xhiveframework/core/doctype/scheduled_job_log/test_scheduled_job_log.py new file mode 100644 index 0000000..11ff251 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_log/test_scheduled_job_log.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestScheduledJobLog(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/scheduled_job_type/__init__.py b/xhiveframework/core/doctype/scheduled_job_type/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.js b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.js new file mode 100644 index 0000000..e242705 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Scheduled Job Type", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.json b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.json new file mode 100644 index 0000000..ac25625 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.json @@ -0,0 +1,129 @@ +{ + "actions": [ + { + "action": "xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type.execute_event", + "action_type": "Server Action", + "label": "Execute" + } + ], + "creation": "2019-09-23 14:34:09.205368", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "stopped", + "method", + "server_script", + "frequency", + "cron_format", + "create_log", + "status_section", + "last_execution", + "column_break_9", + "next_execution" + ], + "fields": [ + { + "fieldname": "method", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Method", + "read_only": 1, + "reqd": 1 + }, + { + "default": "0", + "fieldname": "stopped", + "fieldtype": "Check", + "label": "Stopped" + }, + { + "default": "0", + "depends_on": "eval:doc.frequency==='All'", + "fieldname": "create_log", + "fieldtype": "Check", + "label": "Create Log" + }, + { + "fieldname": "last_execution", + "fieldtype": "Datetime", + "label": "Last Execution", + "read_only": 1 + }, + { + "allow_in_quick_entry": 1, + "depends_on": "eval:doc.frequency==='Cron'", + "description": "
    *  *  *  *  *\n\u252c  \u252c  \u252c  \u252c  \u252c\n\u2502  \u2502  \u2502  \u2502  \u2502\n\u2502  \u2502  \u2502  \u2502  \u2514 day of week (0 - 6) (0 is Sunday)\n\u2502  \u2502  \u2502  \u2514\u2500\u2500\u2500\u2500\u2500 month (1 - 12)\n\u2502  \u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 day of month (1 - 31)\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 hour (0 - 23)\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 minute (0 - 59)\n\n---\n\n* - Any value\n/ - Step values\n
    \n", + "fieldname": "cron_format", + "fieldtype": "Data", + "label": "Cron Format", + "read_only": 1 + }, + { + "fieldname": "frequency", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Frequency", + "options": "All\nHourly\nHourly Long\nDaily\nDaily Long\nWeekly\nWeekly Long\nMonthly\nMonthly Long\nCron\nYearly\nAnnual", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "server_script", + "fieldtype": "Link", + "label": "Server Script", + "options": "Server Script", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "next_execution", + "fieldtype": "Datetime", + "is_virtual": 1, + "label": "Next Execution", + "read_only": 1 + }, + { + "fieldname": "status_section", + "fieldtype": "Section Break", + "label": "Status" + }, + { + "fieldname": "column_break_9", + "fieldtype": "Column Break" + } + ], + "in_create": 1, + "links": [ + { + "link_doctype": "Scheduled Job Log", + "link_fieldname": "scheduled_job_type" + } + ], + "modified": "2023-10-14 11:26:05.005930", + "modified_by": "Administrator", + "module": "Core", + "name": "Scheduled Job Type", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "method", + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.py b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.py new file mode 100644 index 0000000..5c600c8 --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_type/scheduled_job_type.py @@ -0,0 +1,261 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json +from datetime import datetime + +import click +from croniter import CroniterBadCronError, croniter + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import get_datetime, now_datetime +from xhiveframework.utils.background_jobs import enqueue, is_job_enqueued + + +class ScheduledJobType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + create_log: DF.Check + cron_format: DF.Data | None + frequency: DF.Literal[ + "All", + "Hourly", + "Hourly Long", + "Daily", + "Daily Long", + "Weekly", + "Weekly Long", + "Monthly", + "Monthly Long", + "Cron", + "Yearly", + "Annual", + ] + last_execution: DF.Datetime | None + method: DF.Data + next_execution: DF.Datetime | None + server_script: DF.Link | None + stopped: DF.Check + + # end: auto-generated types + def autoname(self): + self.name = ".".join(self.method.split(".")[-2:]) + + def validate(self): + if self.frequency != "All": + # force logging for all events other than continuous ones (ALL) + self.create_log = 1 + + if self.frequency == "Cron": + if not self.cron_format: + xhiveframework.throw(_("Cron format is required for job types with Cron frequency.")) + try: + croniter(self.cron_format) + except CroniterBadCronError: + xhiveframework.throw( + _("{0} is not a valid Cron expression.").format(f"{self.cron_format}"), + title=_("Bad Cron Expression"), + ) + + def enqueue(self, force=False) -> bool: + # enqueue event if last execution is done + if self.is_event_due() or force: + if not self.is_job_in_queue(): + enqueue( + "xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type.run_scheduled_job", + queue=self.get_queue_name(), + job_type=self.method, + job_id=self.rq_job_id, + ) + return True + else: + xhiveframework.logger("scheduler").error( + f"Skipped queueing {self.method} because it was found in queue for {xhiveframework.local.site}" + ) + + return False + + def is_event_due(self, current_time=None): + """Return true if event is due based on time lapsed since last execution""" + # if the next scheduled event is before NOW, then its due! + return self.get_next_execution() <= (current_time or now_datetime()) + + def is_job_in_queue(self) -> bool: + return is_job_enqueued(self.rq_job_id) + + @property + def rq_job_id(self): + """Unique ID created to deduplicate jobs with single RQ call.""" + return f"scheduled_job::{self.method}" + + @property + def next_execution(self): + return self.get_next_execution() + + def get_next_execution(self): + CRON_MAP = { + "Yearly": "0 0 1 1 *", + "Annual": "0 0 1 1 *", + "Monthly": "0 0 1 * *", + "Monthly Long": "0 0 1 * *", + "Weekly": "0 0 * * 0", + "Weekly Long": "0 0 * * 0", + "Daily": "0 0 * * *", + "Daily Long": "0 0 * * *", + "Hourly": "0 * * * *", + "Hourly Long": "0 * * * *", + "All": f"*/{(xhiveframework.get_conf().scheduler_interval or 240) // 60} * * * *", + } + + if not self.cron_format: + self.cron_format = CRON_MAP[self.frequency] + + # If this is a cold start then last_execution will not be set. + # Creation is set as fallback because if very old fallback is set job might trigger + # immediately, even when it's meant to be daily. + # A dynamic fallback like current time might miss the scheduler interval and job will never start. + last_execution = get_datetime(self.last_execution or self.creation) + return croniter(self.cron_format, last_execution).get_next(datetime) + + def execute(self): + self.scheduler_log = None + try: + self.log_status("Start") + if self.server_script: + script_name = xhiveframework.db.get_value("Server Script", self.server_script) + if script_name: + xhiveframework.get_doc("Server Script", script_name).execute_scheduled_method() + else: + xhiveframework.get_attr(self.method)() + xhiveframework.db.commit() + self.log_status("Complete") + except Exception: + xhiveframework.db.rollback() + self.log_status("Failed") + + def log_status(self, status): + # log file + xhiveframework.logger("scheduler").info(f"Scheduled Job {status}: {self.method} for {xhiveframework.local.site}") + self.update_scheduler_log(status) + + def update_scheduler_log(self, status): + if not self.create_log: + # self.get_next_execution will work properly iff self.last_execution is properly set + if self.frequency == "All" and status == "Start": + self.db_set("last_execution", now_datetime(), update_modified=False) + xhiveframework.db.commit() + return + if not self.scheduler_log: + self.scheduler_log = xhiveframework.get_doc( + dict(doctype="Scheduled Job Log", scheduled_job_type=self.name) + ).insert(ignore_permissions=True) + self.scheduler_log.db_set("status", status) + if status == "Failed": + self.scheduler_log.db_set("details", xhiveframework.get_traceback(with_context=True)) + if status == "Start": + self.db_set("last_execution", now_datetime(), update_modified=False) + xhiveframework.db.commit() + + def get_queue_name(self): + return "long" if ("Long" in self.frequency) else "default" + + def on_trash(self): + xhiveframework.db.delete("Scheduled Job Log", {"scheduled_job_type": self.name}) + + +@xhiveframework.whitelist() +def execute_event(doc: str): + xhiveframework.only_for("System Manager") + doc = json.loads(doc) + xhiveframework.get_doc("Scheduled Job Type", doc.get("name")).enqueue(force=True) + return doc + + +def run_scheduled_job(job_type: str): + """This is a wrapper function that runs a hooks.scheduler_events method""" + try: + xhiveframework.get_doc("Scheduled Job Type", dict(method=job_type)).execute() + except Exception: + print(xhiveframework.get_traceback()) + + +def sync_jobs(hooks: dict | None = None): + xhiveframework.reload_doc("core", "doctype", "scheduled_job_type") + scheduler_events = hooks or xhiveframework.get_hooks("scheduler_events") + all_events = insert_events(scheduler_events) + clear_events(all_events) + + +def insert_events(scheduler_events: dict) -> list: + cron_jobs, event_jobs = [], [] + for event_type in scheduler_events: + events = scheduler_events.get(event_type) + if isinstance(events, dict): + cron_jobs += insert_cron_jobs(events) + else: + # hourly, daily etc + event_jobs += insert_event_jobs(events, event_type) + return cron_jobs + event_jobs + + +def insert_cron_jobs(events: dict) -> list: + cron_jobs = [] + for cron_format in events: + for event in events.get(cron_format): + cron_jobs.append(event) + insert_single_event("Cron", event, cron_format) + return cron_jobs + + +def insert_event_jobs(events: list, event_type: str) -> list: + event_jobs = [] + for event in events: + event_jobs.append(event) + frequency = event_type.replace("_", " ").title() + insert_single_event(frequency, event) + return event_jobs + + +def insert_single_event(frequency: str, event: str, cron_format: str | None = None): + cron_expr = {"cron_format": cron_format} if cron_format else {} + + try: + xhiveframework.get_attr(event) + except Exception as e: + click.secho(f"{event} is not a valid method: {e}", fg="yellow") + + doc = xhiveframework.get_doc( + { + "doctype": "Scheduled Job Type", + "method": event, + "cron_format": cron_format, + "frequency": frequency, + } + ) + + if not xhiveframework.db.exists("Scheduled Job Type", {"method": event, "frequency": frequency, **cron_expr}): + savepoint = "scheduled_job_type_creation" + try: + xhiveframework.db.savepoint(savepoint) + doc.insert() + except xhiveframework.DuplicateEntryError: + xhiveframework.db.rollback(save_point=savepoint) + doc.delete() + doc.insert() + + +def clear_events(all_events: list): + for event in xhiveframework.get_all("Scheduled Job Type", fields=["name", "method", "server_script"]): + is_server_script = event.server_script + is_defined_in_hooks = event.method in all_events + + if not (is_defined_in_hooks or is_server_script): + xhiveframework.delete_doc("Scheduled Job Type", event.name) diff --git a/xhiveframework/core/doctype/scheduled_job_type/test_scheduled_job_type.py b/xhiveframework/core/doctype/scheduled_job_type/test_scheduled_job_type.py new file mode 100644 index 0000000..53ced4b --- /dev/null +++ b/xhiveframework/core/doctype/scheduled_job_type/test_scheduled_job_type.py @@ -0,0 +1,90 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from datetime import timedelta + +import xhiveframework +from xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import get_datetime +from xhiveframework.utils.data import add_to_date, now_datetime + + +class TestScheduledJobType(XhiveFrameworkTestCase): + def setUp(self): + xhiveframework.db.rollback() + xhiveframework.db.truncate("Scheduled Job Type") + sync_jobs() + xhiveframework.db.commit() + + def test_sync_jobs(self): + all_job = xhiveframework.get_doc("Scheduled Job Type", dict(method="xhiveframework.email.queue.flush")) + self.assertEqual(all_job.frequency, "All") + + daily_job = xhiveframework.get_doc( + "Scheduled Job Type", dict(method="xhiveframework.desk.notifications.clear_notifications") + ) + self.assertEqual(daily_job.frequency, "Daily") + + # check if cron jobs are synced + cron_job = xhiveframework.get_doc("Scheduled Job Type", dict(method="xhiveframework.oauth.delete_oauth2_data")) + self.assertEqual(cron_job.frequency, "Cron") + self.assertEqual(cron_job.cron_format, "0/15 * * * *") + + # check if jobs are synced after change in hooks + updated_scheduler_events = {"hourly": ["xhiveframework.email.queue.flush"]} + sync_jobs(updated_scheduler_events) + updated_scheduled_job = xhiveframework.get_doc("Scheduled Job Type", {"method": "xhiveframework.email.queue.flush"}) + self.assertEqual(updated_scheduled_job.frequency, "Hourly") + + def test_daily_job(self): + job = xhiveframework.get_doc( + "Scheduled Job Type", dict(method="xhiveframework.desk.notifications.clear_notifications") + ) + job.db_set("last_execution", "2019-01-01 00:00:00") + self.assertTrue(job.is_event_due(get_datetime("2019-01-02 00:00:06"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:00:06"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-01 23:59:59"))) + + def test_weekly_job(self): + job = xhiveframework.get_doc( + "Scheduled Job Type", + dict(method="xhiveframework.social.doctype.energy_point_log.energy_point_log.send_weekly_summary"), + ) + job.db_set("last_execution", "2019-01-01 00:00:00") + self.assertTrue(job.is_event_due(get_datetime("2019-01-06 00:00:01"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-02 00:00:06"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-05 23:59:59"))) + + def test_monthly_job(self): + job = xhiveframework.get_doc( + "Scheduled Job Type", + dict(method="xhiveframework.email.doctype.auto_email_report.auto_email_report.send_monthly"), + ) + job.db_set("last_execution", "2019-01-01 00:00:00") + self.assertTrue(job.is_event_due(get_datetime("2019-02-01 00:00:01"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-15 00:00:06"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-31 23:59:59"))) + + def test_cron_job(self): + # runs every 15 mins + job = xhiveframework.get_doc("Scheduled Job Type", dict(method="xhiveframework.oauth.delete_oauth2_data")) + job.db_set("last_execution", "2019-01-01 00:00:00") + self.assertEqual(job.next_execution, get_datetime("2019-01-01 00:15:00")) + self.assertTrue(job.is_event_due(get_datetime("2019-01-01 00:15:01"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:05:06"))) + self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:14:59"))) + + def test_cold_start(self): + now = now_datetime() + just_before_12_am = now.replace(hour=11, minute=59, second=30) + just_after_12_am = now.replace(hour=0, minute=0, second=30) + timedelta(days=1) + + job = xhiveframework.new_doc("Scheduled Job Type") + job.frequency = "Daily" + job.set_user_and_timestamp() + + with self.freeze_time(just_before_12_am): + self.assertFalse(job.is_event_due()) + + with self.freeze_time(just_after_12_am): + self.assertTrue(job.is_event_due()) diff --git a/xhiveframework/core/doctype/server_script/__init__.py b/xhiveframework/core/doctype/server_script/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/server_script/server_script.js b/xhiveframework/core/doctype/server_script/server_script.js new file mode 100644 index 0000000..b2c925a --- /dev/null +++ b/xhiveframework/core/doctype/server_script/server_script.js @@ -0,0 +1,97 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Server Script", { + setup: function (frm) { + frm.trigger("setup_help"); + }, + refresh: function (frm) { + if (frm.doc.script_type != "Scheduler Event") { + frm.dashboard.hide(); + } + + if (!frm.is_new()) { + frm.add_custom_button(__("Compare Versions"), () => { + new xhiveframework.ui.DiffView("Server Script", "script", frm.doc.name); + }); + } + + frm.call("get_autocompletion_items") + .then((r) => r.message) + .then((items) => { + frm.set_df_property("script", "autocompletions", items); + }); + + frm.trigger("check_safe_exec"); + }, + + check_safe_exec(frm) { + xhiveframework.xcall("xhiveframework.core.doctype.server_script.server_script.enabled").then((enabled) => { + if (enabled === false) { + let docs_link = + "https://xhiveframework.com/docs/user/en/desk/scripting/server-script"; + let docs = `${__("Official Documentation")}`; + + frm.dashboard.clear_comment(); + let msg = __("Server Scripts feature is not available on this site.") + " "; + msg += __("To enable server scripts, read the {0}.", [docs]); + frm.dashboard.add_comment(msg, "yellow", true); + } + }); + }, + + setup_help(frm) { + frm.get_field("help_html").html(` +

    DocType Event

    +

    Add logic for standard doctype events like Before Insert, After Submit, etc.

    +
    +	
    +# set property
    +if "test" in doc.description:
    +	doc.status = 'Closed'
    +
    +
    +# validate
    +if "validate" in doc.description:
    +	raise xhiveframework.ValidationError
    +
    +# auto create another document
    +if doc.allocated_to:
    +	xhiveframework.get_doc(dict(
    +		doctype = 'ToDo'
    +		owner = doc.allocated_to,
    +		description = doc.subject
    +	)).insert()
    +
    +
    + +
    + +

    API Call

    +

    Respond to /api/method/<method-name> calls, just like whitelisted methods

    +
    
    +# respond to API
    +
    +if xhiveframework.form_dict.message == "ping":
    +	xhiveframework.response['message'] = "pong"
    +else:
    +	xhiveframework.response['message'] = "ok"
    +
    + +
    + +

    Permission Query

    +

    Add conditions to the where clause of list queries.

    +
    
    +# generate dynamic conditions and set it in the conditions variable
    +tenant_id = xhiveframework.db.get_value(...)
    +conditions = f'tenant_id = {tenant_id}'
    +
    +# resulting select query
    +select name from \`tabPerson\`
    +where tenant_id = 2
    +order by creation desc
    +
    +`); + }, +}); diff --git a/xhiveframework/core/doctype/server_script/server_script.json b/xhiveframework/core/doctype/server_script/server_script.json new file mode 100644 index 0000000..9f072c1 --- /dev/null +++ b/xhiveframework/core/doctype/server_script/server_script.json @@ -0,0 +1,178 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2019-09-30 11:56:57.943241", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "script_type", + "reference_doctype", + "event_frequency", + "cron_format", + "doctype_event", + "api_method", + "allow_guest", + "column_break_3", + "module", + "disabled", + "section_break_8", + "script", + "rate_limiting_section", + "enable_rate_limit", + "rate_limit_count", + "rate_limit_seconds", + "help_section", + "help_html" + ], + "fields": [ + { + "fieldname": "script_type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Script Type", + "options": "DocType Event\nScheduler Event\nPermission Query\nAPI", + "reqd": 1 + }, + { + "fieldname": "script", + "fieldtype": "Code", + "label": "Script", + "options": "Python", + "reqd": 1 + }, + { + "depends_on": "eval:['DocType Event', 'Permission Query'].includes(doc.script_type)", + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Reference Document Type", + "options": "DocType", + "search_index": 1 + }, + { + "depends_on": "eval:doc.script_type==='DocType Event'", + "fieldname": "doctype_event", + "fieldtype": "Select", + "label": "DocType Event", + "options": "Before Insert\nBefore Validate\nBefore Save\nAfter Insert\nAfter Save\nBefore Rename\nAfter Rename\nBefore Submit\nAfter Submit\nBefore Cancel\nAfter Cancel\nBefore Delete\nAfter Delete\nBefore Save (Submitted Document)\nAfter Save (Submitted Document)\nOn Payment Authorization" + }, + { + "depends_on": "eval:doc.script_type==='API'", + "fieldname": "api_method", + "fieldtype": "Data", + "label": "API Method" + }, + { + "default": "0", + "depends_on": "eval:doc.script_type==='API'", + "fieldname": "allow_guest", + "fieldtype": "Check", + "label": "Allow Guest" + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "help_section", + "fieldtype": "Section Break", + "label": "Help" + }, + { + "fieldname": "help_html", + "fieldtype": "HTML" + }, + { + "depends_on": "eval:doc.script_type == \"Scheduler Event\"", + "fieldname": "event_frequency", + "fieldtype": "Select", + "label": "Event Frequency", + "mandatory_depends_on": "eval:doc.script_type == \"Scheduler Event\"", + "options": "All\nHourly\nDaily\nWeekly\nMonthly\nYearly\nHourly Long\nDaily Long\nWeekly Long\nMonthly Long\nCron" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module (for export)", + "options": "Module Def", + "search_index": 1 + }, + { + "depends_on": "eval:doc.script_type==='API'", + "fieldname": "rate_limiting_section", + "fieldtype": "Section Break", + "label": "Rate Limiting" + }, + { + "default": "0", + "fieldname": "enable_rate_limit", + "fieldtype": "Check", + "label": "Enable Rate Limit" + }, + { + "default": "5", + "depends_on": "enable_rate_limit", + "fieldname": "rate_limit_count", + "fieldtype": "Int", + "label": "Request Limit" + }, + { + "default": "86400", + "depends_on": "enable_rate_limit", + "fieldname": "rate_limit_seconds", + "fieldtype": "Int", + "label": "Time Window (Seconds)" + }, + { + "depends_on": "eval:doc.event_frequency==='Cron'", + "description": "
    *  *  *  *  *\n\u252c  \u252c  \u252c  \u252c  \u252c\n\u2502  \u2502  \u2502  \u2502  \u2502\n\u2502  \u2502  \u2502  \u2502  \u2514 day of week (0 - 6) (0 is Sunday)\n\u2502  \u2502  \u2502  \u2514\u2500\u2500\u2500\u2500\u2500 month (1 - 12)\n\u2502  \u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 day of month (1 - 31)\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 hour (0 - 23)\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 minute (0 - 59)\n\n---\n\n* - Any value\n/ - Step values\n
    \n", + "fieldname": "cron_format", + "fieldtype": "Data", + "label": "Cron Format" + } + ], + "index_web_pages_for_search": 1, + "links": [ + { + "link_doctype": "Scheduled Job Type", + "link_fieldname": "server_script" + } + ], + "modified": "2024-02-27 11:44:46.397495", + "modified_by": "Administrator", + "module": "Core", + "name": "Server Script", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Script Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/server_script/server_script.py b/xhiveframework/core/doctype/server_script/server_script.py new file mode 100644 index 0000000..92be90a --- /dev/null +++ b/xhiveframework/core/doctype/server_script/server_script.py @@ -0,0 +1,304 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from functools import partial +from types import FunctionType, MethodType, ModuleType + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.rate_limiter import rate_limit +from xhiveframework.utils.safe_exec import ( + XhiveFrameworkTransformer, + NamespaceDict, + get_safe_globals, + is_safe_exec_enabled, + safe_exec, +) + + +class ServerScript(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow_guest: DF.Check + api_method: DF.Data | None + cron_format: DF.Data | None + disabled: DF.Check + doctype_event: DF.Literal[ + "Before Insert", + "Before Validate", + "Before Save", + "After Insert", + "After Save", + "Before Rename", + "After Rename", + "Before Submit", + "After Submit", + "Before Cancel", + "After Cancel", + "Before Delete", + "After Delete", + "Before Save (Submitted Document)", + "After Save (Submitted Document)", + "On Payment Authorization", + ] + enable_rate_limit: DF.Check + event_frequency: DF.Literal[ + "All", + "Hourly", + "Daily", + "Weekly", + "Monthly", + "Yearly", + "Hourly Long", + "Daily Long", + "Weekly Long", + "Monthly Long", + "Cron", + ] + module: DF.Link | None + rate_limit_count: DF.Int + rate_limit_seconds: DF.Int + reference_doctype: DF.Link | None + script: DF.Code + script_type: DF.Literal["DocType Event", "Scheduler Event", "Permission Query", "API"] + + # end: auto-generated types + def validate(self): + xhiveframework.only_for("Script Manager", True) + self.sync_scheduled_jobs() + self.clear_scheduled_events() + self.check_if_compilable_in_restricted_context() + + def on_update(self): + self.sync_scheduler_events() + + def clear_cache(self): + xhiveframework.cache.delete_value("server_script_map") + return super().clear_cache() + + def on_trash(self): + xhiveframework.cache.delete_value("server_script_map") + if self.script_type == "Scheduler Event": + for job in self.scheduled_jobs: + xhiveframework.delete_doc("Scheduled Job Type", job.name) + + def get_code_fields(self): + return {"script": "py"} + + @property + def scheduled_jobs(self) -> list[dict[str, str]]: + return xhiveframework.get_all( + "Scheduled Job Type", + filters={"server_script": self.name}, + fields=["name", "stopped"], + ) + + def sync_scheduled_jobs(self): + """Sync Scheduled Job Type statuses if Server Script's disabled status is changed""" + if self.script_type != "Scheduler Event" or not self.has_value_changed("disabled"): + return + + for scheduled_job in self.scheduled_jobs: + if bool(scheduled_job.stopped) != bool(self.disabled): + job = xhiveframework.get_doc("Scheduled Job Type", scheduled_job.name) + job.stopped = self.disabled + job.save() + + def sync_scheduler_events(self): + """Create or update Scheduled Job Type documents for Scheduler Event Server Scripts""" + if not self.disabled and self.event_frequency and self.script_type == "Scheduler Event": + cron_format = self.cron_format if self.event_frequency == "Cron" else None + setup_scheduler_events( + script_name=self.name, frequency=self.event_frequency, cron_format=cron_format + ) + + def clear_scheduled_events(self): + """Deletes existing scheduled jobs by Server Script if self.event_frequency or self.cron_format has changed""" + if ( + self.script_type == "Scheduler Event" + and (self.has_value_changed("event_frequency") or self.has_value_changed("cron_format")) + ) or (self.has_value_changed("script_type") and self.script_type != "Scheduler Event"): + for scheduled_job in self.scheduled_jobs: + xhiveframework.delete_doc("Scheduled Job Type", scheduled_job.name, delete_permanently=1) + + def check_if_compilable_in_restricted_context(self): + """Check compilation errors and send them back as warnings.""" + from RestrictedPython import compile_restricted + + try: + compile_restricted(self.script, policy=XhiveFrameworkTransformer) + except Exception as e: + xhiveframework.msgprint(str(e), title=_("Compilation warning")) + + def execute_method(self) -> dict: + """Specific to API endpoint Server Scripts + + Raises: + xhiveframework.DoesNotExistError: If self.script_type is not API + xhiveframework.PermissionError: If self.allow_guest is unset for API accessed by Guest user + + Returns: + dict: Evaluates self.script with xhiveframework.utils.safe_exec.safe_exec and returns the flags set in it's safe globals + """ + + if self.enable_rate_limit: + # Wrap in rate limiter, required for specifying custom limits for each script + # Note that rate limiter works on `cmd` which is script name + limit = self.rate_limit_count or 5 + seconds = self.rate_limit_seconds or 24 * 60 * 60 + + _fn = partial(execute_api_server_script, script=self) + return rate_limit(limit=limit, seconds=seconds)(_fn)() + else: + return execute_api_server_script(self) + + def execute_doc(self, doc: Document): + """Specific to Document Event triggered Server Scripts + + Args: + doc (Document): Executes script with for a certain document's events + """ + safe_exec( + self.script, + _locals={"doc": doc}, + restrict_commit_rollback=True, + script_filename=self.name, + ) + + def execute_scheduled_method(self): + """Specific to Scheduled Jobs via Server Scripts + + Raises: + xhiveframework.DoesNotExistError: If script type is not a scheduler event + """ + if self.script_type != "Scheduler Event": + raise xhiveframework.DoesNotExistError + + safe_exec(self.script, script_filename=self.name) + + def get_permission_query_conditions(self, user: str) -> list[str]: + """Specific to Permission Query Server Scripts + + Args: + user (str): Takes user email to execute script and return list of conditions + + Returns: + list: Returns list of conditions defined by rules in self.script + """ + locals = {"user": user, "conditions": ""} + safe_exec(self.script, None, locals, script_filename=self.name) + if locals["conditions"]: + return locals["conditions"] + + @xhiveframework.whitelist() + def get_autocompletion_items(self): + """Generates a list of a autocompletion strings from the context dict + that is used while executing a Server Script. + + Returns: + list: Returns list of autocompletion items. + For e.g., ["xhiveframework.utils.cint", "xhiveframework.get_all", ...] + """ + + def get_keys(obj): + out = [] + for key in obj: + if key.startswith("_"): + continue + value = obj[key] + if isinstance(value, NamespaceDict | dict) and value: + if key == "form_dict": + out.append(["form_dict", 7]) + continue + for subkey, score in get_keys(value): + fullkey = f"{key}.{subkey}" + out.append([fullkey, score]) + else: + if isinstance(value, type) and issubclass(value, Exception): + score = 0 + elif isinstance(value, ModuleType): + score = 10 + elif isinstance(value, FunctionType | MethodType): + score = 9 + elif isinstance(value, type): + score = 8 + elif isinstance(value, dict): + score = 7 + else: + score = 6 + out.append([key, score]) + return out + + items = xhiveframework.cache.get_value("server_script_autocompletion_items") + if not items: + items = get_keys(get_safe_globals()) + items = [{"value": d[0], "score": d[1]} for d in items] + xhiveframework.cache.set_value("server_script_autocompletion_items", items) + return items + + +def setup_scheduler_events(script_name: str, frequency: str, cron_format: str | None = None): + """Creates or Updates Scheduled Job Type documents based on the specified script name and frequency + + Args: + script_name (str): Name of the Server Script document + frequency (str): Event label compatible with the XhiveFramework scheduler + """ + method = xhiveframework.scrub(f"{script_name}-{frequency}") + scheduled_script = xhiveframework.db.get_value("Scheduled Job Type", {"method": method}) + + if not scheduled_script: + xhiveframework.get_doc( + { + "doctype": "Scheduled Job Type", + "method": method, + "frequency": frequency, + "server_script": script_name, + "cron_format": cron_format, + } + ).insert() + + xhiveframework.msgprint(_("Enabled scheduled execution for script {0}").format(script_name)) + + else: + doc = xhiveframework.get_doc("Scheduled Job Type", scheduled_script) + + if doc.frequency == frequency: + return + + doc.frequency = frequency + doc.cron_format = cron_format + doc.save() + + xhiveframework.msgprint(_("Scheduled execution for script {0} has updated").format(script_name)) + + +def execute_api_server_script(script=None, *args, **kwargs): + # These are only added for compatibility with rate limiter. + del args + del kwargs + + if script.script_type != "API": + raise xhiveframework.DoesNotExistError + + # validate if guest is allowed + if xhiveframework.session.user == "Guest" and not script.allow_guest: + raise xhiveframework.PermissionError + + # output can be stored in flags + _globals, _locals = safe_exec(script.script, script_filename=script.name) + + return _globals.xhiveframework.flags + + +@xhiveframework.whitelist() +def enabled() -> bool | None: + if xhiveframework.has_permission("Server Script"): + return is_safe_exec_enabled() diff --git a/xhiveframework/core/doctype/server_script/server_script_list.js b/xhiveframework/core/doctype/server_script/server_script_list.js new file mode 100644 index 0000000..62433fe --- /dev/null +++ b/xhiveframework/core/doctype/server_script/server_script_list.js @@ -0,0 +1,38 @@ +xhiveframework.listview_settings["Server Script"] = { + onload: function (listview) { + add_github_star_cta(listview); + }, +}; + +function add_github_star_cta(listview) { + try { + const key = "show_github_star_banner"; + if (localStorage.getItem(key) == "false") { + return; + } + + if (listview.github_star_banner) { + listview.github_star_banner.remove(); + } + + const message = "Loving Xhive Framework?"; + const link = "https://lab.membtech.com/xhiveframework/xhiveframework15"; + const cta = "Star us on GitHub"; + + listview.github_star_banner = $(` +
    +
    + ${message}
    ${cta} → +
    +
    + + + +
    +
    + `).appendTo(listview.page.sidebar); + } catch (error) { + console.error(error); + } +} diff --git a/xhiveframework/core/doctype/server_script/server_script_utils.py b/xhiveframework/core/doctype/server_script/server_script_utils.py new file mode 100644 index 0000000..b0f4685 --- /dev/null +++ b/xhiveframework/core/doctype/server_script/server_script_utils.py @@ -0,0 +1,80 @@ +import xhiveframework + +# this is a separate file since it is imported in xhiveframework.model.document +# to avoid circular imports + +EVENT_MAP = { + "before_insert": "Before Insert", + "after_insert": "After Insert", + "before_validate": "Before Validate", + "validate": "Before Save", + "on_update": "After Save", + "before_rename": "Before Rename", + "after_rename": "After Rename", + "before_submit": "Before Submit", + "on_submit": "After Submit", + "before_cancel": "Before Cancel", + "on_cancel": "After Cancel", + "on_trash": "Before Delete", + "after_delete": "After Delete", + "before_update_after_submit": "Before Save (Submitted Document)", + "on_update_after_submit": "After Save (Submitted Document)", + "on_payment_authorized": "On Payment Authorization", +} + + +def run_server_script_for_doc_event(doc, event): + # run document event method + if event not in EVENT_MAP: + return + + if xhiveframework.flags.in_install: + return + + if xhiveframework.flags.in_migrate: + return + + scripts = get_server_script_map().get(doc.doctype, {}).get(EVENT_MAP[event], None) + if scripts: + # run all scripts for this doctype + event + for script_name in scripts: + xhiveframework.get_doc("Server Script", script_name).execute_doc(doc) + + +def get_server_script_map(): + # fetch cached server script methods + # { + # '[doctype]': { + # 'Before Insert': ['[server script 1]', '[server script 2]'] + # }, + # '_api': { + # '[path]': '[server script]' + # }, + # 'permission_query': { + # 'DocType': '[server script]' + # } + # } + if xhiveframework.flags.in_patch and not xhiveframework.db.table_exists("Server Script"): + return {} + + script_map = xhiveframework.cache.get_value("server_script_map") + if script_map is None: + script_map = {"permission_query": {}} + enabled_server_scripts = xhiveframework.get_all( + "Server Script", + fields=("name", "reference_doctype", "doctype_event", "api_method", "script_type"), + filters={"disabled": 0}, + ) + for script in enabled_server_scripts: + if script.script_type == "DocType Event": + script_map.setdefault(script.reference_doctype, {}).setdefault( + script.doctype_event, [] + ).append(script.name) + elif script.script_type == "Permission Query": + script_map["permission_query"][script.reference_doctype] = script.name + else: + script_map.setdefault("_api", {})[script.api_method] = script.name + + xhiveframework.cache.set_value("server_script_map", script_map) + + return script_map diff --git a/xhiveframework/core/doctype/server_script/test_server_script.py b/xhiveframework/core/doctype/server_script/test_server_script.py new file mode 100644 index 0000000..a02f024 --- /dev/null +++ b/xhiveframework/core/doctype/server_script/test_server_script.py @@ -0,0 +1,342 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import requests + +import xhiveframework +from xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs +from xhiveframework.xhiveframeworkclient import XhiveFrameworkClient, XhiveFrameworkException +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import get_site_url + +scripts = [ + dict( + name="test_todo", + script_type="DocType Event", + doctype_event="Before Insert", + reference_doctype="ToDo", + script=""" +if "test" in doc.description: + doc.status = 'Closed' +""", + ), + dict( + name="test_todo_validate", + script_type="DocType Event", + doctype_event="Before Insert", + reference_doctype="ToDo", + script=""" +if "validate" in doc.description: + raise xhiveframework.ValidationError +""", + ), + dict( + name="test_api", + script_type="API", + api_method="test_server_script", + allow_guest=1, + script=""" +xhiveframework.response['message'] = 'hello' +""", + ), + dict( + name="test_return_value", + script_type="API", + api_method="test_return_value", + allow_guest=1, + script=""" +xhiveframework.flags = 'hello' +""", + ), + dict( + name="test_permission_query", + script_type="Permission Query", + reference_doctype="ToDo", + script=""" +conditions = '1 = 1' +""", + ), + dict( + name="test_invalid_namespace_method", + script_type="DocType Event", + doctype_event="Before Insert", + reference_doctype="Note", + script=""" +xhiveframework.method_that_doesnt_exist("do some magic") +""", + ), + dict( + name="test_todo_commit", + script_type="DocType Event", + doctype_event="Before Save", + reference_doctype="ToDo", + disabled=1, + script=""" +xhiveframework.db.commit() +""", + ), + dict( + name="test_add_index", + script_type="DocType Event", + doctype_event="Before Save", + reference_doctype="ToDo", + disabled=1, + script=""" +xhiveframework.db.add_index("Todo", ["color", "date"]) +""", + ), + dict( + name="test_before_rename", + script_type="DocType Event", + doctype_event="After Rename", + reference_doctype="Role", + script=""" +doc.desk_access =0 +doc.save() +""", + ), + dict( + name="test_after_rename", + script_type="DocType Event", + doctype_event="After Rename", + reference_doctype="Role", + script=""" +doc.disabled =1 +doc.save() +""", + ), +] + + +class TestServerScript(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + xhiveframework.db.truncate("Server Script") + xhiveframework.get_doc("User", "Administrator").add_roles("Script Manager") + for script in scripts: + script_doc = xhiveframework.get_doc(doctype="Server Script") + script_doc.update(script) + script_doc.insert() + cls.enable_safe_exec() + xhiveframework.db.commit() + return super().setUpClass() + + @classmethod + def tearDownClass(cls): + xhiveframework.db.commit() + xhiveframework.db.truncate("Server Script") + xhiveframework.cache.delete_value("server_script_map") + + def setUp(self): + xhiveframework.cache.delete_value("server_script_map") + + def test_doctype_event(self): + todo = xhiveframework.get_doc(dict(doctype="ToDo", description="hello")).insert() + self.assertEqual(todo.status, "Open") + + todo = xhiveframework.get_doc(dict(doctype="ToDo", description="test todo")).insert() + self.assertEqual(todo.status, "Closed") + + self.assertRaises( + xhiveframework.ValidationError, xhiveframework.get_doc(dict(doctype="ToDo", description="validate me")).insert + ) + + role = xhiveframework.get_doc(doctype="Role", role_name="_Test Role 9").insert(ignore_if_duplicate=True) + role.rename("_Test Role 10") + role.reload() + self.assertEqual(role.disabled, 1) + self.assertEqual(role.desk_access, 0) + + def test_api(self): + response = requests.post(get_site_url(xhiveframework.local.site) + "/api/method/test_server_script") + self.assertEqual(response.status_code, 200) + self.assertEqual("hello", response.json()["message"]) + + def test_api_return(self): + self.assertEqual(xhiveframework.get_doc("Server Script", "test_return_value").execute_method(), "hello") + + def test_permission_query(self): + if xhiveframework.conf.db_type == "mariadb": + self.assertTrue("where (1 = 1)" in xhiveframework.db.get_list("ToDo", run=False)) + else: + self.assertTrue("where (1 = '1')" in xhiveframework.db.get_list("ToDo", run=False)) + self.assertTrue(isinstance(xhiveframework.db.get_list("ToDo"), list)) + + def test_attribute_error(self): + """Raise AttributeError if method not found in Namespace""" + note = xhiveframework.get_doc({"doctype": "Note", "title": "Test Note: Server Script"}) + self.assertRaises(AttributeError, note.insert) + + def test_syntax_validation(self): + server_script = scripts[0] + server_script["script"] = "js || code.?" + + with self.assertRaises(xhiveframework.ValidationError) as se: + xhiveframework.get_doc(doctype="Server Script", **server_script).insert() + + self.assertTrue( + "invalid python code" in str(se.exception).lower(), msg="Python code validation not working" + ) + + def test_commit_in_doctype_event(self): + server_script = xhiveframework.get_doc("Server Script", "test_todo_commit") + server_script.disabled = 0 + server_script.save() + + self.assertRaises(AttributeError, xhiveframework.get_doc(dict(doctype="ToDo", description="test me")).insert) + + server_script.disabled = 1 + server_script.save() + + def test_add_index_in_doctype_event(self): + server_script = xhiveframework.get_doc("Server Script", "test_add_index") + server_script.disabled = 0 + server_script.save() + + self.assertRaises(AttributeError, xhiveframework.get_doc(dict(doctype="ToDo", description="test me")).insert) + + server_script.disabled = 1 + server_script.save() + + def test_restricted_qb(self): + todo = xhiveframework.get_doc(doctype="ToDo", description="QbScriptTestNote") + todo.insert() + + script = xhiveframework.get_doc( + doctype="Server Script", + name="test_qb_restrictions", + script_type="API", + api_method="test_qb_restrictions", + allow_guest=1, + # whitelisted update + script=f""" +xhiveframework.db.set_value("ToDo", "{todo.name}", "description", "safe") +""", + ) + script.insert() + script.execute_method() + + todo.reload() + self.assertEqual(todo.description, "safe") + + # unsafe update + script.script = f""" +todo = xhiveframework.qb.DocType("ToDo") +xhiveframework.qb.update(todo).set(todo.description, "unsafe").where(todo.name == "{todo.name}").run() +""" + script.save() + self.assertRaises(xhiveframework.PermissionError, script.execute_method) + todo.reload() + self.assertEqual(todo.description, "safe") + + # safe select + script.script = f""" +todo = xhiveframework.qb.DocType("ToDo") +xhiveframework.qb.from_(todo).select(todo.name).where(todo.name == "{todo.name}").run() +""" + script.save() + script.execute_method() + + def test_scripts_all_the_way_down(self): + # why not + script = xhiveframework.get_doc( + doctype="Server Script", + name="test_nested_scripts_1", + script_type="API", + api_method="test_nested_scripts_1", + script="""log("nothing")""", + ) + script.insert() + script.execute_method() + + script = xhiveframework.get_doc( + doctype="Server Script", + name="test_nested_scripts_2", + script_type="API", + api_method="test_nested_scripts_2", + script="""xhiveframework.call("test_nested_scripts_1")""", + ) + script.insert() + script.execute_method() + + def test_server_script_rate_limiting(self): + script1 = xhiveframework.get_doc( + doctype="Server Script", + name="rate_limited_server_script", + script_type="API", + enable_rate_limit=1, + allow_guest=1, + rate_limit_count=5, + api_method="rate_limited_endpoint", + script="""xhiveframework.flags = {"test": True}""", + ) + + script1.insert() + + script2 = xhiveframework.get_doc( + doctype="Server Script", + name="rate_limited_server_script2", + script_type="API", + enable_rate_limit=1, + allow_guest=1, + rate_limit_count=5, + api_method="rate_limited_endpoint2", + script="""xhiveframework.flags = {"test": False}""", + ) + + script2.insert() + + xhiveframework.db.commit() + + site = xhiveframework.utils.get_site_url(xhiveframework.local.site) + client = XhiveFrameworkClient(site) + + # Exhaust rate limit + for _ in range(5): + client.get_api(script1.api_method) + + self.assertRaises(XhiveFrameworkException, client.get_api, script1.api_method) + + # Exhaust rate limit + for _ in range(5): + client.get_api(script2.api_method) + + self.assertRaises(XhiveFrameworkException, client.get_api, script2.api_method) + + script1.delete() + script2.delete() + xhiveframework.db.commit() + + def test_server_script_scheduled(self): + scheduled_script = xhiveframework.get_doc( + doctype="Server Script", + name="scheduled_script_wo_cron", + script_type="Scheduler Event", + script="""xhiveframework.flags = {"test": True}""", + event_frequency="Hourly", + ).insert() + + cron_script = xhiveframework.get_doc( + doctype="Server Script", + name="scheduled_script_w_cron", + script_type="Scheduler Event", + script="""xhiveframework.flags = {"test": True}""", + event_frequency="Cron", + cron_format="0 0 1 1 *", # 1st january + ).insert() + + # Ensure that jobs remain in DB after migrate + sync_jobs() + self.assertTrue(xhiveframework.db.exists("Scheduled Job Type", {"server_script": scheduled_script.name})) + + cron_job_name = xhiveframework.db.get_value("Scheduled Job Type", {"server_script": cron_script.name}) + self.assertTrue(cron_job_name) + + cron_job = xhiveframework.get_doc("Scheduled Job Type", cron_job_name) + self.assertEqual(cron_job.next_execution.day, 1) + self.assertEqual(cron_job.next_execution.month, 1) + + cron_script.cron_format = "0 0 2 1 *" # 2nd january + cron_script.save() + cron_job.reload() + self.assertEqual(cron_job.next_execution.day, 2) diff --git a/xhiveframework/core/doctype/session_default/__init__.py b/xhiveframework/core/doctype/session_default/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/session_default/session_default.json b/xhiveframework/core/doctype/session_default/session_default.json new file mode 100644 index 0000000..d382e12 --- /dev/null +++ b/xhiveframework/core/doctype/session_default/session_default.json @@ -0,0 +1,29 @@ +{ + "creation": "2019-07-17 16:21:33.546379", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ref_doctype" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType" + } + ], + "istable": 1, + "modified": "2019-07-21 13:22:25.752553", + "modified_by": "Administrator", + "module": "Core", + "name": "Session Default", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/session_default/session_default.py b/xhiveframework/core/doctype/session_default/session_default.py new file mode 100644 index 0000000..3fd07ce --- /dev/null +++ b/xhiveframework/core/doctype/session_default/session_default.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class SessionDefault(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + ref_doctype: DF.Link | None + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/session_default_settings/__init__.py b/xhiveframework/core/doctype/session_default_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/session_default_settings/session_default_settings.js b/xhiveframework/core/doctype/session_default_settings/session_default_settings.js new file mode 100644 index 0000000..9af9d8e --- /dev/null +++ b/xhiveframework/core/doctype/session_default_settings/session_default_settings.js @@ -0,0 +1,15 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.ui.form.on("Session Default Settings", { + refresh: function (frm) { + frm.set_query("ref_doctype", "session_defaults", function () { + return { + filters: { + issingle: 0, + istable: 0, + }, + }; + }); + }, +}); diff --git a/xhiveframework/core/doctype/session_default_settings/session_default_settings.json b/xhiveframework/core/doctype/session_default_settings/session_default_settings.json new file mode 100644 index 0000000..3eeb3f4 --- /dev/null +++ b/xhiveframework/core/doctype/session_default_settings/session_default_settings.json @@ -0,0 +1,39 @@ +{ + "creation": "2019-07-17 16:22:31.300991", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "session_defaults" + ], + "fields": [ + { + "fieldname": "session_defaults", + "fieldtype": "Table", + "label": "Session Defaults", + "options": "Session Default" + } + ], + "issingle": 1, + "modified": "2019-07-19 16:04:33.971089", + "modified_by": "Administrator", + "module": "Core", + "name": "Session Default Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/session_default_settings/session_default_settings.py b/xhiveframework/core/doctype/session_default_settings/session_default_settings.py new file mode 100644 index 0000000..bf091c7 --- /dev/null +++ b/xhiveframework/core/doctype/session_default_settings/session_default_settings.py @@ -0,0 +1,59 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class SessionDefaultSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.session_default.session_default import SessionDefault + from xhiveframework.types import DF + + session_defaults: DF.Table[SessionDefault] + # end: auto-generated types + pass + + +@xhiveframework.whitelist() +def get_session_default_values(): + settings = xhiveframework.get_single("Session Default Settings") + fields = [] + for default_values in settings.session_defaults: + reference_doctype = xhiveframework.scrub(default_values.ref_doctype) + fields.append( + { + "fieldname": reference_doctype, + "fieldtype": "Link", + "options": default_values.ref_doctype, + "label": _("Default {0}").format(_(default_values.ref_doctype)), + "default": xhiveframework.defaults.get_user_default(reference_doctype), + } + ) + return json.dumps(fields) + + +@xhiveframework.whitelist() +def set_session_default_values(default_values): + default_values = xhiveframework.parse_json(default_values) + for entry in default_values: + try: + xhiveframework.defaults.set_user_default(entry, default_values.get(entry)) + except Exception: + return + return "success" + + +# called on hook 'on_logout' to clear defaults for the session +def clear_session_defaults(): + settings = xhiveframework.get_single("Session Default Settings").session_defaults + for entry in settings: + xhiveframework.defaults.clear_user_default(xhiveframework.scrub(entry.ref_doctype)) diff --git a/xhiveframework/core/doctype/session_default_settings/test_session_default_settings.py b/xhiveframework/core/doctype/session_default_settings/test_session_default_settings.py new file mode 100644 index 0000000..0b2f483 --- /dev/null +++ b/xhiveframework/core/doctype/session_default_settings/test_session_default_settings.py @@ -0,0 +1,31 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.session_default_settings.session_default_settings import ( + clear_session_defaults, + set_session_default_values, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSessionDefaultSettings(XhiveFrameworkTestCase): + def test_set_session_default_settings(self): + xhiveframework.set_user("Administrator") + settings = xhiveframework.get_single("Session Default Settings") + settings.session_defaults = [] + settings.append("session_defaults", {"ref_doctype": "Role"}) + settings.save() + + set_session_default_values({"role": "Website Manager"}) + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test session defaults set", assigned_by="Administrator") + ).insert() + self.assertEqual(todo.role, "Website Manager") + + def test_clear_session_defaults(self): + clear_session_defaults() + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test session defaults cleared", assigned_by="Administrator") + ).insert() + self.assertNotEqual(todo.role, "Website Manager") diff --git a/xhiveframework/core/doctype/sms_parameter/README.md b/xhiveframework/core/doctype/sms_parameter/README.md new file mode 100644 index 0000000..5935a39 --- /dev/null +++ b/xhiveframework/core/doctype/sms_parameter/README.md @@ -0,0 +1 @@ +SMS query parameter for SMS Settings. \ No newline at end of file diff --git a/xhiveframework/core/doctype/sms_parameter/__init__.py b/xhiveframework/core/doctype/sms_parameter/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/sms_parameter/sms_parameter.json b/xhiveframework/core/doctype/sms_parameter/sms_parameter.json new file mode 100755 index 0000000..98972f9 --- /dev/null +++ b/xhiveframework/core/doctype/sms_parameter/sms_parameter.json @@ -0,0 +1,51 @@ +{ + "actions": [], + "creation": "2013-02-22 01:27:58", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "parameter", + "value", + "header" + ], + "fields": [ + { + "fieldname": "parameter", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Parameter", + "print_width": "150px", + "reqd": 1, + "width": "150px" + }, + { + "fieldname": "value", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Value", + "print_width": "150px", + "reqd": 1, + "width": "150px" + }, + { + "default": "0", + "fieldname": "header", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Header" + } + ], + "idx": 1, + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:53.129765", + "modified_by": "Administrator", + "module": "Core", + "name": "SMS Parameter", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/sms_parameter/sms_parameter.py b/xhiveframework/core/doctype/sms_parameter/sms_parameter.py new file mode 100644 index 0000000..c1ce7f8 --- /dev/null +++ b/xhiveframework/core/doctype/sms_parameter/sms_parameter.py @@ -0,0 +1,23 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class SMSParameter(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + header: DF.Check + parameter: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + value: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/sms_settings/README.md b/xhiveframework/core/doctype/sms_settings/README.md new file mode 100644 index 0000000..4fb4980 --- /dev/null +++ b/xhiveframework/core/doctype/sms_settings/README.md @@ -0,0 +1 @@ +Settings for automatically sending SMS from the system. \ No newline at end of file diff --git a/xhiveframework/core/doctype/sms_settings/__init__.py b/xhiveframework/core/doctype/sms_settings/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/sms_settings/sms_settings.js b/xhiveframework/core/doctype/sms_settings/sms_settings.js new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/sms_settings/sms_settings.json b/xhiveframework/core/doctype/sms_settings/sms_settings.json new file mode 100755 index 0000000..14ff324 --- /dev/null +++ b/xhiveframework/core/doctype/sms_settings/sms_settings.json @@ -0,0 +1,80 @@ +{ + "actions": [], + "allow_copy": 1, + "creation": "2013-01-10 16:34:24", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "sms_gateway_url", + "message_parameter", + "receiver_parameter", + "static_parameters_section", + "parameters", + "use_post" + ], + "fields": [ + { + "description": "Eg. smsgateway.com/api/send_sms.cgi", + "fieldname": "sms_gateway_url", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "SMS Gateway URL", + "reqd": 1 + }, + { + "description": "Enter url parameter for message", + "fieldname": "message_parameter", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Message Parameter", + "reqd": 1 + }, + { + "description": "Enter url parameter for receiver nos", + "fieldname": "receiver_parameter", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Receiver Parameter", + "reqd": 1 + }, + { + "fieldname": "static_parameters_section", + "fieldtype": "Column Break", + "width": "50%" + }, + { + "description": "Enter static url parameters here (Eg. sender=XhiveERP, username=XhiveERP, password=1234 etc.)", + "fieldname": "parameters", + "fieldtype": "Table", + "label": "Static Parameters", + "options": "SMS Parameter" + }, + { + "default": "0", + "fieldname": "use_post", + "fieldtype": "Check", + "label": "Use POST" + } + ], + "icon": "fa fa-cog", + "idx": 1, + "issingle": 1, + "links": [], + "modified": "2021-09-21 19:45:26.809793", + "modified_by": "Administrator", + "module": "Core", + "name": "SMS Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/sms_settings/sms_settings.py b/xhiveframework/core/doctype/sms_settings/sms_settings.py new file mode 100644 index 0000000..320e3c5 --- /dev/null +++ b/xhiveframework/core/doctype/sms_settings/sms_settings.py @@ -0,0 +1,157 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _, msgprint, throw +from xhiveframework.model.document import Document +from xhiveframework.utils import nowdate + + +class SMSSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.sms_parameter.sms_parameter import SMSParameter + from xhiveframework.types import DF + + message_parameter: DF.Data + parameters: DF.Table[SMSParameter] + receiver_parameter: DF.Data + sms_gateway_url: DF.SmallText + use_post: DF.Check + # end: auto-generated types + pass + + +def validate_receiver_nos(receiver_list): + validated_receiver_list = [] + for d in receiver_list: + if not d: + continue + + # remove invalid character + for x in [" ", "-", "(", ")"]: + d = d.replace(x, "") + + validated_receiver_list.append(d) + + if not validated_receiver_list: + throw(_("Please enter valid mobile nos")) + + return validated_receiver_list + + +@xhiveframework.whitelist() +def get_contact_number(contact_name, ref_doctype, ref_name): + "returns mobile number of the contact" + number = xhiveframework.db.sql( + """select mobile_no, phone from tabContact + where name=%s + and exists( + select name from `tabDynamic Link` where link_doctype=%s and link_name=%s + ) + """, + (contact_name, ref_doctype, ref_name), + ) + + return number and (number[0][0] or number[0][1]) or "" + + +@xhiveframework.whitelist() +def send_sms(receiver_list, msg, sender_name="", success_msg=True): + import json + + if isinstance(receiver_list, str): + receiver_list = json.loads(receiver_list) + if not isinstance(receiver_list, list): + receiver_list = [receiver_list] + + receiver_list = validate_receiver_nos(receiver_list) + + arg = { + "receiver_list": receiver_list, + "message": xhiveframework.safe_decode(msg).encode("utf-8"), + "success_msg": success_msg, + } + + if xhiveframework.db.get_single_value("SMS Settings", "sms_gateway_url"): + send_via_gateway(arg) + else: + msgprint(_("Please Update SMS Settings")) + + +def send_via_gateway(arg): + ss = xhiveframework.get_doc("SMS Settings", "SMS Settings") + headers = get_headers(ss) + use_json = headers.get("Content-Type") == "application/json" + + message = xhiveframework.safe_decode(arg.get("message")) + args = {ss.message_parameter: message} + for d in ss.get("parameters"): + if not d.header: + args[d.parameter] = d.value + + success_list = [] + for d in arg.get("receiver_list"): + args[ss.receiver_parameter] = d + status = send_request(ss.sms_gateway_url, args, headers, ss.use_post, use_json) + + if 200 <= status < 300: + success_list.append(d) + + if len(success_list) > 0: + args.update(arg) + create_sms_log(args, success_list) + if arg.get("success_msg"): + xhiveframework.msgprint(_("SMS sent to following numbers: {0}").format("\n" + "\n".join(success_list))) + + +def get_headers(sms_settings=None): + if not sms_settings: + sms_settings = xhiveframework.get_doc("SMS Settings", "SMS Settings") + + headers = {"Accept": "text/plain, text/html, */*"} + for d in sms_settings.get("parameters"): + if d.header == 1: + headers.update({d.parameter: d.value}) + + return headers + + +def send_request(gateway_url, params, headers=None, use_post=False, use_json=False): + import requests + + if not headers: + headers = get_headers() + kwargs = {"headers": headers} + + if use_json: + kwargs["json"] = params + elif use_post: + kwargs["data"] = params + else: + kwargs["params"] = params + + if use_post: + response = requests.post(gateway_url, **kwargs) + else: + response = requests.get(gateway_url, **kwargs) + response.raise_for_status() + return response.status_code + + +# Create SMS Log +# ========================================================= +def create_sms_log(args, sent_to): + sl = xhiveframework.new_doc("SMS Log") + sl.sent_on = nowdate() + sl.message = args["message"].decode("utf-8") + sl.no_of_requested_sms = len(args["receiver_list"]) + sl.requested_numbers = "\n".join(args["receiver_list"]) + sl.no_of_sent_sms = len(sent_to) + sl.sent_to = "\n".join(sent_to) + sl.flags.ignore_permissions = True + sl.save() diff --git a/xhiveframework/core/doctype/sms_settings/test_sms_settings.py b/xhiveframework/core/doctype/sms_settings/test_sms_settings.py new file mode 100644 index 0000000..c611589 --- /dev/null +++ b/xhiveframework/core/doctype/sms_settings/test_sms_settings.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSMSSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/submission_queue/__init__.py b/xhiveframework/core/doctype/submission_queue/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/submission_queue/submission_queue.js b/xhiveframework/core/doctype/submission_queue/submission_queue.js new file mode 100644 index 0000000..8314b6b --- /dev/null +++ b/xhiveframework/core/doctype/submission_queue/submission_queue.js @@ -0,0 +1,20 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Submission Queue", { + refresh: function (frm) { + if (frm.doc.status === "Queued" && xhiveframework.boot.user.roles.includes("System Manager")) { + frm.add_custom_button(__("Unlock Reference Document"), () => { + xhiveframework.confirm( + ` + Are you sure you want to go ahead with this action? + Doing this could unlock other submissions of this document which are in queue (if present) + and could lead to non-ideal conditions.`, + () => { + frm.call("unlock_doc"); + } + ); + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/submission_queue/submission_queue.json b/xhiveframework/core/doctype/submission_queue/submission_queue.json new file mode 100644 index 0000000..4e4abce --- /dev/null +++ b/xhiveframework/core/doctype/submission_queue/submission_queue.json @@ -0,0 +1,130 @@ +{ + "actions": [], + "autoname": "hash", + "beta": 1, + "creation": "2022-10-04 00:41:00.028163", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "status", + "created_at", + "enqueued_by", + "job_id", + "column_break_5", + "ended_at", + "ref_doctype", + "ref_docname", + "section_break_8", + "exception" + ], + "fields": [ + { + "fieldname": "job_id", + "fieldtype": "Link", + "label": "Job Id", + "options": "RQ Job", + "read_only": 1 + }, + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference DocType", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "ref_docname", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Reference Docname", + "options": "ref_doctype", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "status", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "label": "Status", + "options": "Queued\nFinished\nFailed", + "read_only": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "enqueued_by", + "fieldtype": "Data", + "is_virtual": 1, + "label": "Enqueued By", + "read_only": 1 + }, + { + "fieldname": "ended_at", + "fieldtype": "Datetime", + "label": "Ended At", + "read_only": 1 + }, + { + "fieldname": "created_at", + "fieldtype": "Datetime", + "is_virtual": 1, + "label": "Created At", + "read_only": 1 + }, + { + "fieldname": "exception", + "fieldtype": "Long Text", + "label": "Exception", + "read_only": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-29 12:27:54.754054", + "modified_by": "Administrator", + "module": "Core", + "name": "Submission Queue", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + }, + { + "if_owner": 1, + "read": 1, + "role": "Desk User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [ + { + "color": "Blue", + "title": "Queued" + }, + { + "color": "Red", + "title": "Failed" + }, + { + "color": "Green", + "title": "Finished" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/submission_queue/submission_queue.py b/xhiveframework/core/doctype/submission_queue/submission_queue.py new file mode 100644 index 0000000..6c888c9 --- /dev/null +++ b/xhiveframework/core/doctype/submission_queue/submission_queue.py @@ -0,0 +1,208 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +from urllib.parse import quote + +from rq import get_current_job + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.doctype.notification_log.notification_log import enqueue_create_notification +from xhiveframework.model.document import Document +from xhiveframework.monitor import add_data_to_monitor +from xhiveframework.utils import now, time_diff_in_seconds +from xhiveframework.utils.data import cint + + +class SubmissionQueue(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + created_at: DF.Datetime | None + ended_at: DF.Datetime | None + enqueued_by: DF.Data | None + exception: DF.LongText | None + job_id: DF.Link | None + ref_docname: DF.DynamicLink | None + ref_doctype: DF.Link | None + status: DF.Literal["Queued", "Finished", "Failed"] + + # end: auto-generated types + @property + def created_at(self): + return self.creation + + @property + def enqueued_by(self): + return self.owner + + @property + def queued_doc(self): + return getattr(self, "to_be_queued_doc", xhiveframework.get_doc(self.ref_doctype, self.ref_docname)) + + @staticmethod + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Submission Queue") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + def insert(self, to_be_queued_doc: Document, action: str): + self.status = "Queued" + self.to_be_queued_doc = to_be_queued_doc + self.action_for_queuing = action + super().insert(ignore_permissions=True) + + def lock(self): + self.queued_doc.lock() + + def unlock(self): + self.queued_doc.unlock() + + def update_job_id(self, job_id): + xhiveframework.db.set_value( + self.doctype, + self.name, + {"job_id": job_id}, + update_modified=False, + ) + xhiveframework.db.commit() + + def after_insert(self): + self.queue_action( + "background_submission", + to_be_queued_doc=self.queued_doc, + action_for_queuing=self.action_for_queuing, + timeout=600, + enqueue_after_commit=True, + ) + + def background_submission(self, to_be_queued_doc: Document, action_for_queuing: str): + # Set the job id for that submission doctype + self.update_job_id(get_current_job().id) + + _action = action_for_queuing.lower() + if _action == "update": + _action = "submit" + + try: + getattr(to_be_queued_doc, _action)() + add_data_to_monitor( + doctype=to_be_queued_doc.doctype, + docname=to_be_queued_doc.name, + action=_action, + execution_time=time_diff_in_seconds(now(), self.created_at), + enqueued_by=self.enqueued_by, + ) + values = {"status": "Finished"} + except Exception: + values = {"status": "Failed", "exception": xhiveframework.get_traceback(with_context=True)} + xhiveframework.db.rollback() + + values["ended_at"] = now() + xhiveframework.db.set_value(self.doctype, self.name, values, update_modified=False) + self.notify(values["status"], action_for_queuing) + + def notify(self, submission_status: str, action: str): + if submission_status == "Failed": + doctype = self.doctype + docname = self.name + message = _("Action {0} failed on {1} {2}. View it {3}") + else: + doctype = self.ref_doctype + docname = self.ref_docname + message = _("Action {0} completed successfully on {1} {2}. View it {3}") + + message_replacements = ( + xhiveframework.bold(action), + xhiveframework.bold(str(self.ref_doctype)), + xhiveframework.bold(str(self.ref_docname)), + ) + + time_diff = time_diff_in_seconds(now(), self.created_at) + if cint(time_diff) <= 60: + xhiveframework.publish_realtime( + "msgprint", + { + "message": message.format( + *message_replacements, + f"here", + ), + "alert": True, + "indicator": "red" if submission_status == "Failed" else "green", + }, + user=self.enqueued_by, + ) + else: + notification_doc = { + "type": "Alert", + "document_type": doctype, + "document_name": docname, + "subject": message.format(*message_replacements, "here"), + } + + notify_to = xhiveframework.db.get_value("User", self.enqueued_by, fieldname="email") + enqueue_create_notification([notify_to], notification_doc) + + @xhiveframework.whitelist() + def unlock_doc(self): + # NOTE: this can lead to some weird unlocking/locking behaviours. + # for example: hitting unlock on a submission could lead to unlocking of another submission + # of the same reference document. + + if self.status != "Queued": + return + + self.queued_doc.unlock() + xhiveframework.msgprint(_("Document Unlocked")) + + +def queue_submission(doc: Document, action: str, alert: bool = True): + queue = xhiveframework.new_doc("Submission Queue") + queue.ref_doctype = doc.doctype + queue.ref_docname = doc.name + queue.insert(doc, action) + + if alert: + xhiveframework.msgprint( + _("Queued for Submission. You can track the progress over {0}.").format( + f"here" + ), + indicator="green", + alert=True, + ) + + +@xhiveframework.whitelist() +def get_latest_submissions(doctype, docname): + # NOTE: not used creation as orderby intentianlly as we have used update_modified=False everywhere + # hence assuming modified will be equal to creation for submission queue documents + + latest_submission = xhiveframework.db.get_value( + "Submission Queue", + filters={"ref_doctype": doctype, "ref_docname": docname}, + fieldname=["name", "exception", "status"], + ) + + out = None + if latest_submission: + out = { + "latest_submission": latest_submission[0], + "exc": format_tb(latest_submission[1]), + "status": latest_submission[2], + } + + return out + + +def format_tb(traceback: str | None = None): + if not traceback: + return + + return traceback.strip().split("\n")[-1] diff --git a/xhiveframework/core/doctype/submission_queue/test_submission_queue.py b/xhiveframework/core/doctype/submission_queue/test_submission_queue.py new file mode 100644 index 0000000..8616903 --- /dev/null +++ b/xhiveframework/core/doctype/submission_queue/test_submission_queue.py @@ -0,0 +1,51 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# See license.txt + +import time +import typing + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase, timeout +from xhiveframework.utils.background_jobs import get_queue + +if typing.TYPE_CHECKING: + from rq.job import Job + + +class TestSubmissionQueue(XhiveFrameworkTestCase): + queue = get_queue(qtype="default") + + @timeout(seconds=20) + def check_status(self, job: "Job", status, wait=True): + if wait: + while True: + if job.is_queued or job.is_started: + time.sleep(0.2) + else: + break + self.assertEqual(xhiveframework.get_doc("RQ Job", job.id).status, status) + + def test_queue_operation(self): + from xhiveframework.core.doctype.doctype.test_doctype import new_doctype + from xhiveframework.core.doctype.submission_queue.submission_queue import queue_submission + + if not xhiveframework.db.table_exists("Test Submission Queue", cached=False): + doc = new_doctype("Test Submission Queue", is_submittable=True, queue_in_background=True) + doc.insert() + + d = xhiveframework.new_doc("Test Submission Queue") + d.update({"some_fieldname": "Random"}) + d.insert() + + xhiveframework.db.commit() + queue_submission(d, "submit") + xhiveframework.db.commit() + + # Waiting for execution + time.sleep(4) + submission_queue = xhiveframework.get_last_doc("Submission Queue") + + # Test queueing / starting + job = self.queue.fetch_job(submission_queue.job_id) + # Test completion + self.check_status(job, status="finished") diff --git a/xhiveframework/core/doctype/success_action/__init__.py b/xhiveframework/core/doctype/success_action/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/success_action/success_action.js b/xhiveframework/core/doctype/success_action/success_action.js new file mode 100644 index 0000000..b350623 --- /dev/null +++ b/xhiveframework/core/doctype/success_action/success_action.js @@ -0,0 +1,60 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Success Action", { + on_load: (frm) => { + if (!frm.action_multicheck) { + frm.trigger("set_next_action_multicheck"); + } + }, + refresh: (frm) => { + if (!frm.action_multicheck) { + frm.trigger("set_next_action_multicheck"); + } + }, + validate: (frm) => { + const checked_actions = frm.action_multicheck.get_checked_options(); + if (checked_actions.length < 2) { + xhiveframework.msgprint(__("Select atleast 2 actions")); + } else { + return true; + } + }, + before_save: (frm) => { + const checked_actions = frm.action_multicheck.get_checked_options(); + frm.doc.next_actions = checked_actions.join("\n"); + }, + after_save: (frm) => { + xhiveframework.boot.success_action.push(frm.doc); + //TODO: update success action cache on record update and delete + }, + set_next_action_multicheck: (frm) => { + const next_actions_wrapper = frm.fields_dict.next_actions_html.$wrapper; + const checked_actions = frm.doc.next_actions ? frm.doc.next_actions.split("\n") : []; + const action_multicheck_options = get_default_next_actions().map((action) => { + return { + label: action.label, + value: action.value, + checked: checked_actions.length ? checked_actions.includes(action.value) : 1, + }; + }); + frm.action_multicheck = xhiveframework.ui.form.make_control({ + parent: next_actions_wrapper, + df: { + label: "Next Actions", + fieldname: "next_actions_multicheck", + fieldtype: "MultiCheck", + options: action_multicheck_options, + }, + }); + }, +}); + +const get_default_next_actions = () => { + return [ + { label: __("New"), value: "new" }, + { label: __("Print"), value: "print" }, + { label: __("Email"), value: "email" }, + { label: __("View All"), value: "list" }, + ]; +}; diff --git a/xhiveframework/core/doctype/success_action/success_action.json b/xhiveframework/core/doctype/success_action/success_action.json new file mode 100644 index 0000000..749fa67 --- /dev/null +++ b/xhiveframework/core/doctype/success_action/success_action.json @@ -0,0 +1,84 @@ +{ + "actions": [], + "autoname": "field:ref_doctype", + "creation": "2018-04-15 18:07:35.316870", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ref_doctype", + "first_success_message", + "message", + "next_actions_html", + "next_actions", + "action_timeout" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType", + "reqd": 1, + "unique": 1 + }, + { + "default": "Congratulations on first creations", + "fieldname": "first_success_message", + "fieldtype": "Data", + "in_list_view": 1, + "label": "First Success Message", + "reqd": 1 + }, + { + "default": "Successfully created", + "fieldname": "message", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Message", + "reqd": 1 + }, + { + "fieldname": "next_actions_html", + "fieldtype": "HTML", + "label": "Next Actions HTML" + }, + { + "fieldname": "next_actions", + "fieldtype": "Data", + "hidden": 1 + }, + { + "default": "7", + "fieldname": "action_timeout", + "fieldtype": "Int", + "label": "Action Timeout (Seconds)" + } + ], + "hide_toolbar": 1, + "links": [], + "modified": "2022-08-03 12:20:54.532708", + "modified_by": "Administrator", + "module": "Core", + "name": "Success Action", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/success_action/success_action.py b/xhiveframework/core/doctype/success_action/success_action.py new file mode 100644 index 0000000..b6e846f --- /dev/null +++ b/xhiveframework/core/doctype/success_action/success_action.py @@ -0,0 +1,22 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class SuccessAction(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action_timeout: DF.Int + first_success_message: DF.Data + message: DF.Data + next_actions: DF.Data | None + ref_doctype: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/system_settings/__init__.py b/xhiveframework/core/doctype/system_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/system_settings/system_settings.js b/xhiveframework/core/doctype/system_settings/system_settings.js new file mode 100644 index 0000000..a4b6ee6 --- /dev/null +++ b/xhiveframework/core/doctype/system_settings/system_settings.js @@ -0,0 +1,89 @@ +xhiveframework.ui.form.on("System Settings", { + refresh: function (frm) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.system_settings.system_settings.load", + callback: function (data) { + xhiveframework.all_timezones = data.message.timezones; + frm.set_df_property("time_zone", "options", xhiveframework.all_timezones); + + $.each(data.message.defaults, function (key, val) { + frm.set_value(key, val, null, true); + xhiveframework.sys_defaults[key] = val; + }); + if (frm.re_setup_moment) { + xhiveframework.app.setup_moment(); + delete frm.re_setup_moment; + } + }, + }); + + frm.trigger("set_rounding_method_options"); + }, + enable_password_policy: function (frm) { + if (frm.doc.enable_password_policy == 0) { + frm.set_value("minimum_password_score", ""); + } else { + frm.set_value("minimum_password_score", "2"); + } + }, + enable_two_factor_auth: function (frm) { + if (frm.doc.enable_two_factor_auth == 0) { + frm.set_value("bypass_2fa_for_retricted_ip_users", 0); + frm.set_value("bypass_restrict_ip_check_if_2fa_enabled", 0); + } + }, + after_save: function (frm) { + /** + * Checks whether the effective value has changed. + * + * @param {Array.} - Tuple with new fallback, previous fallback and + * optionally an override value. + * @returns {boolean} - Whether the resulting value has effectively changed + */ + const has_effectively_changed = ([new_fallback, prev_fallback, override = undefined]) => + !override && prev_fallback !== new_fallback; + + const attr_tuples = [ + [frm.doc.language, xhiveframework.boot.sysdefaults.language, xhiveframework.boot.user.language], + [frm.doc.rounding_method, xhiveframework.boot.sysdefaults.rounding_method], // no user override. + ]; + + if (attr_tuples.some(has_effectively_changed)) { + xhiveframework.msgprint(__("Refreshing...")); + window.location.reload(); + } + }, + first_day_of_the_week(frm) { + frm.re_setup_moment = true; + }, + + rounding_method: function (frm) { + if (frm.doc.rounding_method == xhiveframework.boot.sysdefaults.rounding_method) return; + let msg = __( + "Changing rounding method on site with data can result in unexpected behaviour." + ); + msg += "
    "; + msg += __("Do you still want to proceed?"); + + xhiveframework.confirm( + msg, + () => {}, + () => { + frm.set_value("rounding_method", xhiveframework.boot.sysdefaults.rounding_method); + } + ); + }, + + set_rounding_method_options: function (frm) { + if (frm.doc.rounding_method != "Banker's Rounding (legacy)") { + let field = frm.fields_dict.rounding_method; + + field.df.options = field.df.options + .split("\n") + .filter((o) => o != "Banker's Rounding (legacy)") + .join("\n"); + + field.refresh(); + } + }, +}); diff --git a/xhiveframework/core/doctype/system_settings/system_settings.json b/xhiveframework/core/doctype/system_settings/system_settings.json new file mode 100644 index 0000000..ae12672 --- /dev/null +++ b/xhiveframework/core/doctype/system_settings/system_settings.json @@ -0,0 +1,683 @@ +{ + "actions": [], + "creation": "2022-01-06 03:18:16.326761", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "localization", + "app_name", + "country", + "language", + "column_break_3", + "time_zone", + "enable_onboarding", + "setup_complete", + "disable_document_sharing", + "date_and_number_format", + "date_format", + "time_format", + "number_format", + "first_day_of_the_week", + "column_break_7", + "float_precision", + "currency_precision", + "rounding_method", + "permissions", + "apply_strict_user_permissions", + "column_break_21", + "allow_older_web_view_links", + "security_tab", + "security", + "session_expiry", + "document_share_key_expiry", + "column_break_txqh", + "deny_multiple_sessions", + "disable_user_pass_login", + "login_methods_section", + "allow_login_using_mobile_number", + "allow_login_using_user_name", + "column_break_uhqk", + "login_with_email_link", + "login_with_email_link_expiry", + "brute_force_security", + "allow_consecutive_login_attempts", + "column_break_34", + "allow_login_after_fail", + "two_factor_authentication", + "enable_two_factor_auth", + "bypass_2fa_for_retricted_ip_users", + "bypass_restrict_ip_check_if_2fa_enabled", + "two_factor_method", + "lifespan_qrcode_image", + "otp_issuer_name", + "password_tab", + "password_settings", + "logout_on_password_reset", + "force_user_to_reset_password", + "reset_password_link_expiry_duration", + "password_reset_limit", + "column_break_31", + "enable_password_policy", + "minimum_password_score", + "email_tab", + "email", + "email_footer_address", + "email_retry_limit", + "column_break_18", + "disable_standard_email_footer", + "hide_footer_in_auto_email_reports", + "attach_view_link", + "store_attached_pdf_document", + "welcome_email_template", + "reset_password_template", + "files_tab", + "files_section", + "max_file_size", + "allow_guests_to_upload_files", + "force_web_capture_mode_for_uploads", + "strip_exif_metadata_from_uploaded_images", + "column_break_uqma", + "allowed_file_extensions", + "updates_tab", + "system_updates_section", + "disable_system_update_notification", + "disable_change_log_notification", + "backups_tab", + "sec_backup_limit", + "backup_limit", + "encrypt_backup", + "advanced_tab", + "prepared_report_section", + "max_auto_email_report_per_user", + "background_workers", + "enable_scheduler", + "dormant_days", + "telemetry_section", + "allow_error_traceback", + "enable_telemetry", + "search_section", + "link_field_results_limit" + ], + "fields": [ + { + "fieldname": "localization", + "fieldtype": "Section Break" + }, + { + "fieldname": "country", + "fieldtype": "Link", + "label": "Country", + "options": "Country" + }, + { + "fieldname": "language", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Language", + "options": "Language", + "reqd": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "time_zone", + "fieldtype": "Select", + "label": "Time Zone", + "read_only": 1, + "reqd": 1 + }, + { + "default": "0", + "fieldname": "setup_complete", + "fieldtype": "Check", + "hidden": 1, + "label": "Setup Complete", + "read_only": 1 + }, + { + "fieldname": "date_and_number_format", + "fieldtype": "Section Break", + "label": "Date and Number Format" + }, + { + "fieldname": "date_format", + "fieldtype": "Select", + "label": "Date Format", + "options": "yyyy-mm-dd\ndd-mm-yyyy\ndd/mm/yyyy\ndd.mm.yyyy\nmm/dd/yyyy\nmm-dd-yyyy", + "reqd": 1 + }, + { + "default": "HH:mm:ss", + "fieldname": "time_format", + "fieldtype": "Select", + "label": "Time Format", + "options": "HH:mm:ss\nHH:mm", + "reqd": 1 + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "fieldname": "number_format", + "fieldtype": "Select", + "label": "Number Format", + "options": "#,###.##\n#.###,##\n# ###.##\n# ###,##\n#'###.##\n#, ###.##\n#,##,###.##\n#,###.###\n#.###\n#,###", + "reqd": 1 + }, + { + "fieldname": "float_precision", + "fieldtype": "Select", + "label": "Float Precision", + "options": "\n2\n3\n4\n5\n6\n7\n8\n9" + }, + { + "description": "If not set, the currency precision will depend on number format", + "fieldname": "currency_precision", + "fieldtype": "Select", + "label": "Currency Precision", + "options": "\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" + }, + { + "fieldname": "sec_backup_limit", + "fieldtype": "Section Break" + }, + { + "default": "3", + "description": "Older backups will be automatically deleted", + "fieldname": "backup_limit", + "fieldtype": "Int", + "label": "Number of Backups" + }, + { + "fieldname": "background_workers", + "fieldtype": "Section Break", + "label": "Background Workers" + }, + { + "default": "0", + "description": "Run scheduled jobs only if checked", + "fieldname": "enable_scheduler", + "fieldtype": "Check", + "hidden": 1, + "label": "Enable Scheduled Jobs" + }, + { + "fieldname": "permissions", + "fieldtype": "Section Break", + "label": "Permissions" + }, + { + "default": "0", + "description": "If Apply Strict User Permission is checked and User Permission is defined for a DocType for a User, then all the documents where value of the link is blank, will not be shown to that User", + "fieldname": "apply_strict_user_permissions", + "fieldtype": "Check", + "label": "Apply Strict User Permissions" + }, + { + "fieldname": "security", + "fieldtype": "Section Break" + }, + { + "default": "170:00", + "description": "Example: Setting this to 24:00 will log out a user if they are not active for 24:00 hours.", + "fieldname": "session_expiry", + "fieldtype": "Data", + "label": "Session Expiry (idle timeout)" + }, + { + "default": "0", + "description": "Note: Multiple sessions will be allowed in case of mobile device", + "fieldname": "deny_multiple_sessions", + "fieldtype": "Check", + "label": "Allow only one session per user" + }, + { + "default": "0", + "description": "User can login using Email id or Mobile number", + "fieldname": "allow_login_using_mobile_number", + "fieldtype": "Check", + "label": "Allow Login using Mobile Number" + }, + { + "default": "0", + "description": "User can login using Email id or User Name", + "fieldname": "allow_login_using_user_name", + "fieldtype": "Check", + "label": "Allow Login using User Name" + }, + { + "default": "1", + "fieldname": "allow_error_traceback", + "fieldtype": "Check", + "label": "Show Full Error and Allow Reporting of Issues to the Developer" + }, + { + "fieldname": "password_settings", + "fieldtype": "Section Break", + "label": "Password" + }, + { + "description": "In Days", + "fieldname": "force_user_to_reset_password", + "fieldtype": "Int", + "label": "Force User to Reset Password" + }, + { + "fieldname": "column_break_31", + "fieldtype": "Column Break" + }, + { + "default": "1", + "description": "If enabled, the password strength will be enforced based on the Minimum Password Score value. A value of 2 being medium strong and 4 being very strong.", + "fieldname": "enable_password_policy", + "fieldtype": "Check", + "label": "Enable Password Policy" + }, + { + "default": "2", + "depends_on": "eval:doc.enable_password_policy==1", + "fieldname": "minimum_password_score", + "fieldtype": "Select", + "label": "Minimum Password Score", + "options": "2\n3\n4" + }, + { + "fieldname": "brute_force_security", + "fieldtype": "Section Break", + "label": "Brute Force Security" + }, + { + "default": "10", + "fieldname": "allow_consecutive_login_attempts", + "fieldtype": "Int", + "label": "Allow Consecutive Login Attempts " + }, + { + "fieldname": "column_break_34", + "fieldtype": "Column Break" + }, + { + "default": "60", + "description": "In seconds", + "fieldname": "allow_login_after_fail", + "fieldtype": "Int", + "label": "Allow Login After Fail" + }, + { + "fieldname": "two_factor_authentication", + "fieldtype": "Section Break", + "label": "Two Factor Authentication" + }, + { + "default": "0", + "fieldname": "enable_two_factor_auth", + "fieldtype": "Check", + "label": "Enable Two Factor Auth" + }, + { + "default": "0", + "depends_on": "enable_two_factor_auth", + "description": "If enabled, users who login from Restricted IP Address, won't be prompted for Two Factor Auth", + "fieldname": "bypass_2fa_for_retricted_ip_users", + "fieldtype": "Check", + "label": "Bypass Two Factor Auth for users who login from restricted IP Address" + }, + { + "default": "0", + "depends_on": "enable_two_factor_auth", + "description": "If enabled, all users can login from any IP Address using Two Factor Auth. This can also be set only for specific user(s) in User Page", + "fieldname": "bypass_restrict_ip_check_if_2fa_enabled", + "fieldtype": "Check", + "label": "Bypass restricted IP Address check If Two Factor Auth Enabled" + }, + { + "default": "OTP App", + "depends_on": "enable_two_factor_auth", + "description": "Choose authentication method to be used by all users", + "fieldname": "two_factor_method", + "fieldtype": "Select", + "label": "Two Factor Authentication method", + "options": "OTP App\nSMS\nEmail" + }, + { + "depends_on": "eval:doc.enable_two_factor_auth && doc.two_factor_method == \"OTP App\"", + "description": "Time in seconds to retain QR code image on server. Min:240", + "fieldname": "lifespan_qrcode_image", + "fieldtype": "Int", + "label": "Expiry time of QR Code Image Page" + }, + { + "default": "Xhive Framework", + "depends_on": "enable_two_factor_auth", + "fieldname": "otp_issuer_name", + "fieldtype": "Data", + "label": "OTP Issuer Name" + }, + { + "fieldname": "email", + "fieldtype": "Section Break" + }, + { + "description": "Your organization name and address for the email footer.", + "fieldname": "email_footer_address", + "fieldtype": "Small Text", + "label": "Email Footer Address" + }, + { + "fieldname": "column_break_18", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "disable_standard_email_footer", + "fieldtype": "Check", + "label": "Disable Standard Email Footer" + }, + { + "default": "0", + "fieldname": "hide_footer_in_auto_email_reports", + "fieldtype": "Check", + "label": "Hide footer in auto email reports" + }, + { + "fieldname": "column_break_21", + "fieldtype": "Column Break" + }, + { + "default": "0", + "description": "When enabled this will allow guests to upload files to your application, You can enable this if you wish to collect files from user without having them to log in, for example in job applications web form.", + "fieldname": "allow_guests_to_upload_files", + "fieldtype": "Check", + "label": "Allow Guests to Upload Files" + }, + { + "default": "4", + "description": "Will run scheduled jobs only once a day for inactive sites. Default 4 days if set to 0.", + "fieldname": "dormant_days", + "fieldtype": "Int", + "label": "Run Jobs only Daily if Inactive For (Days)" + }, + { + "default": "3", + "description": "Hourly rate limit for generating password reset links", + "fieldname": "password_reset_limit", + "fieldtype": "Int", + "label": "Password Reset Link Generation Limit" + }, + { + "default": "1", + "fieldname": "logout_on_password_reset", + "fieldtype": "Check", + "label": "Logout All Sessions on Password Reset" + }, + { + "default": "0", + "fieldname": "enable_onboarding", + "fieldtype": "Check", + "label": "Enable Onboarding" + }, + { + "default": "1", + "fieldname": "attach_view_link", + "fieldtype": "Check", + "label": "Include Web View Link in Email" + }, + { + "fieldname": "prepared_report_section", + "fieldtype": "Section Break", + "label": "Reports" + }, + { + "default": "XhiveFramework", + "description": "The application name will be used in the Login page.", + "fieldname": "app_name", + "fieldtype": "Data", + "hidden": 1, + "label": "Application Name" + }, + { + "default": "1", + "fieldname": "strip_exif_metadata_from_uploaded_images", + "fieldtype": "Check", + "label": "Strip EXIF tags from uploaded images" + }, + { + "default": "0", + "fieldname": "encrypt_backup", + "fieldtype": "Check", + "label": "Encrypt Backups" + }, + { + "fieldname": "system_updates_section", + "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "disable_system_update_notification", + "fieldtype": "Check", + "label": "Disable System Update Notification" + }, + { + "default": "Sunday", + "fieldname": "first_day_of_the_week", + "fieldtype": "Select", + "label": "First Day of the Week", + "options": "Sunday\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday" + }, + { + "default": "30", + "description": "Number of days after which the document Web View link shared on email will be expired", + "fieldname": "document_share_key_expiry", + "fieldtype": "Int", + "label": "Document Share Key Expiry (in Days)" + }, + { + "default": "0", + "fieldname": "allow_older_web_view_links", + "fieldtype": "Check", + "label": "Allow Older Web View Links (Insecure)" + }, + { + "default": "20", + "fieldname": "max_auto_email_report_per_user", + "fieldtype": "Int", + "label": "Max auto email report per user" + }, + { + "default": "0", + "fieldname": "disable_change_log_notification", + "fieldtype": "Check", + "label": "Disable Change Log Notification" + }, + { + "default": "1200", + "fieldname": "reset_password_link_expiry_duration", + "fieldtype": "Duration", + "label": "Reset Password Link Expiry Duration", + "non_negative": 1 + }, + { + "default": "3", + "fieldname": "email_retry_limit", + "fieldtype": "Int", + "label": "Email Retry Limit" + }, + { + "default": "0", + "description": "Make sure to configure a Social Login Key before disabling to prevent lockout", + "fieldname": "disable_user_pass_login", + "fieldtype": "Check", + "label": "Disable Username/Password Login" + }, + { + "default": "1", + "description": "Allow users to log in without a password, using a login link sent to their email", + "fieldname": "login_with_email_link", + "fieldtype": "Check", + "label": "Login with email link" + }, + { + "default": "10", + "depends_on": "login_with_email_link", + "fieldname": "login_with_email_link_expiry", + "fieldtype": "Int", + "label": "Login with email link expiry (in minutes)" + }, + { + "default": "Banker's Rounding (legacy)", + "fieldname": "rounding_method", + "fieldtype": "Select", + "label": "Rounding Method", + "options": "Banker's Rounding (legacy)\nBanker's Rounding\nCommercial Rounding" + }, + { + "default": "0", + "fieldname": "disable_document_sharing", + "fieldtype": "Check", + "label": "Disable Document Sharing" + }, + { + "fieldname": "telemetry_section", + "fieldtype": "Section Break", + "label": "Telemetry" + }, + { + "default": "1", + "fieldname": "enable_telemetry", + "fieldtype": "Check", + "label": "Allow Sending Usage Data for Improving Applications" + }, + { + "fieldname": "welcome_email_template", + "fieldtype": "Link", + "label": "Welcome Email Template", + "options": "Email Template" + }, + { + "fieldname": "reset_password_template", + "fieldtype": "Link", + "label": "Reset Password Template", + "options": "Email Template" + }, + { + "default": "0", + "description": "When uploading files, force the use of the web-based image capture. If this is unchecked, the default behavior is to use the mobile native camera when use from a mobile is detected.", + "fieldname": "force_web_capture_mode_for_uploads", + "fieldtype": "Check", + "label": "Force Web Capture Mode for Uploads" + }, + { + "fieldname": "files_section", + "fieldtype": "Section Break" + }, + { + "fieldname": "max_file_size", + "fieldtype": "Int", + "label": "Max File Size (MB)", + "non_negative": 1 + }, + { + "fieldname": "column_break_uqma", + "fieldtype": "Column Break" + }, + { + "description": "Provide a list of allowed file extensions for file uploads. Each line should contain one allowed file type. If unset, all file extensions are allowed. Example:
    CSV
    JPG
    PNG", + "fieldname": "allowed_file_extensions", + "fieldtype": "Small Text", + "label": "Allowed File Extensions" + }, + { + "fieldname": "security_tab", + "fieldtype": "Tab Break", + "label": "Login" + }, + { + "fieldname": "email_tab", + "fieldtype": "Tab Break", + "label": "Email" + }, + { + "fieldname": "files_tab", + "fieldtype": "Tab Break", + "label": "Files" + }, + { + "fieldname": "updates_tab", + "fieldtype": "Tab Break", + "label": "Updates" + }, + { + "fieldname": "backups_tab", + "fieldtype": "Tab Break", + "label": "Backups" + }, + { + "fieldname": "advanced_tab", + "fieldtype": "Tab Break", + "label": "Advanced" + }, + { + "fieldname": "password_tab", + "fieldtype": "Tab Break", + "label": "Password" + }, + { + "fieldname": "column_break_txqh", + "fieldtype": "Column Break" + }, + { + "fieldname": "login_methods_section", + "fieldtype": "Section Break", + "label": "Login Methods" + }, + { + "fieldname": "column_break_uhqk", + "fieldtype": "Column Break" + }, + { + "fieldname": "search_section", + "fieldtype": "Section Break", + "label": "Search" + }, + { + "default": "10", + "fieldname": "link_field_results_limit", + "fieldtype": "Int", + "label": "Link Field Results Limit", + "non_negative": 1 + }, + { + "default": "1", + "description": "When sending document using email, store the PDF on Communication. Warning: This can increase your storage usage.", + "fieldname": "store_attached_pdf_document", + "fieldtype": "Check", + "label": "Store Attached PDF Document" + } + ], + "icon": "fa fa-cog", + "issingle": 1, + "links": [], + "modified": "2024-03-14 15:18:01.465057", + "modified_by": "Administrator", + "module": "Core", + "name": "System Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/system_settings/system_settings.py b/xhiveframework/core/doctype/system_settings/system_settings.py new file mode 100644 index 0000000..6fd45fa --- /dev/null +++ b/xhiveframework/core/doctype/system_settings/system_settings.py @@ -0,0 +1,217 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import no_value_fields +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, today + + +class SystemSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow_consecutive_login_attempts: DF.Int + allow_error_traceback: DF.Check + allow_guests_to_upload_files: DF.Check + allow_login_after_fail: DF.Int + allow_login_using_mobile_number: DF.Check + allow_login_using_user_name: DF.Check + allow_older_web_view_links: DF.Check + allowed_file_extensions: DF.SmallText | None + app_name: DF.Data | None + apply_strict_user_permissions: DF.Check + attach_view_link: DF.Check + backup_limit: DF.Int + bypass_2fa_for_retricted_ip_users: DF.Check + bypass_restrict_ip_check_if_2fa_enabled: DF.Check + country: DF.Link | None + currency_precision: DF.Literal["", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + date_format: DF.Literal[ + "yyyy-mm-dd", "dd-mm-yyyy", "dd/mm/yyyy", "dd.mm.yyyy", "mm/dd/yyyy", "mm-dd-yyyy" + ] + deny_multiple_sessions: DF.Check + disable_change_log_notification: DF.Check + disable_document_sharing: DF.Check + disable_standard_email_footer: DF.Check + disable_system_update_notification: DF.Check + disable_user_pass_login: DF.Check + document_share_key_expiry: DF.Int + dormant_days: DF.Int + email_footer_address: DF.SmallText | None + email_retry_limit: DF.Int + enable_onboarding: DF.Check + enable_password_policy: DF.Check + enable_scheduler: DF.Check + enable_telemetry: DF.Check + enable_two_factor_auth: DF.Check + encrypt_backup: DF.Check + first_day_of_the_week: DF.Literal[ + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" + ] + float_precision: DF.Literal["", "2", "3", "4", "5", "6", "7", "8", "9"] + force_user_to_reset_password: DF.Int + force_web_capture_mode_for_uploads: DF.Check + hide_footer_in_auto_email_reports: DF.Check + language: DF.Link + lifespan_qrcode_image: DF.Int + link_field_results_limit: DF.Int + login_with_email_link: DF.Check + login_with_email_link_expiry: DF.Int + logout_on_password_reset: DF.Check + max_auto_email_report_per_user: DF.Int + max_file_size: DF.Int + minimum_password_score: DF.Literal["2", "3", "4"] + number_format: DF.Literal[ + "#,###.##", + "#.###,##", + "# ###.##", + "# ###,##", + "#'###.##", + "#, ###.##", + "#,##,###.##", + "#,###.###", + "#.###", + "#,###", + ] + otp_issuer_name: DF.Data | None + password_reset_limit: DF.Int + reset_password_link_expiry_duration: DF.Duration | None + reset_password_template: DF.Link | None + rounding_method: DF.Literal["Banker's Rounding (legacy)", "Banker's Rounding", "Commercial Rounding"] + session_expiry: DF.Data | None + setup_complete: DF.Check + store_attached_pdf_document: DF.Check + strip_exif_metadata_from_uploaded_images: DF.Check + time_format: DF.Literal["HH:mm:ss", "HH:mm"] + time_zone: DF.Literal[None] + two_factor_method: DF.Literal["OTP App", "SMS", "Email"] + welcome_email_template: DF.Link | None + # end: auto-generated types + + def validate(self): + from xhiveframework.twofactor import toggle_two_factor_auth + + enable_password_policy = cint(self.enable_password_policy) and True or False + minimum_password_score = cint(getattr(self, "minimum_password_score", 0)) or 0 + if enable_password_policy and minimum_password_score <= 0: + xhiveframework.throw(_("Please select Minimum Password Score")) + elif not enable_password_policy: + self.minimum_password_score = "" + + if self.session_expiry: + parts = self.session_expiry.split(":") + if len(parts) != 2 or not (cint(parts[0]) or cint(parts[1])): + xhiveframework.throw(_("Session Expiry must be in format {0}").format("hh:mm")) + + if self.enable_two_factor_auth: + if self.two_factor_method == "SMS": + if not xhiveframework.db.get_single_value("SMS Settings", "sms_gateway_url"): + xhiveframework.throw( + _("Please setup SMS before setting it as an authentication method, via SMS Settings") + ) + toggle_two_factor_auth(True, roles=["All"]) + else: + self.bypass_2fa_for_retricted_ip_users = 0 + self.bypass_restrict_ip_check_if_2fa_enabled = 0 + + xhiveframework.flags.update_last_reset_password_date = False + if self.force_user_to_reset_password and not cint( + xhiveframework.db.get_single_value("System Settings", "force_user_to_reset_password") + ): + xhiveframework.flags.update_last_reset_password_date = True + + self.validate_user_pass_login() + self.validate_backup_limit() + self.validate_file_extensions() + + if not self.link_field_results_limit: + self.link_field_results_limit = 10 + + if self.link_field_results_limit > 50: + self.link_field_results_limit = 50 + label = _(self.meta.get_label("link_field_results_limit")) + xhiveframework.msgprint( + _("{0} can not be more than {1}").format(label, 50), alert=True, indicator="yellow" + ) + + def validate_user_pass_login(self): + if not self.disable_user_pass_login: + return + + social_login_enabled = xhiveframework.db.exists("Social Login Key", {"enable_social_login": 1}) + ldap_enabled = xhiveframework.db.get_single_value("LDAP Settings", "enabled") + login_with_email_link_enabled = xhiveframework.db.get_single_value("System Settings", "login_with_email_link") + + if not (social_login_enabled or ldap_enabled or login_with_email_link_enabled): + xhiveframework.throw( + _( + "Please enable atleast one Social Login Key or LDAP or Login With Email Link before disabling username/password based login." + ) + ) + + def validate_backup_limit(self): + if not self.backup_limit or self.backup_limit < 1: + xhiveframework.msgprint(_("Number of backups must be greater than zero."), alert=True) + self.backup_limit = 1 + + def validate_file_extensions(self): + if not self.allowed_file_extensions: + return + + self.allowed_file_extensions = "\n".join( + ext.strip().upper() for ext in self.allowed_file_extensions.strip().splitlines() + ) + + def on_update(self): + self.set_defaults() + + xhiveframework.cache.delete_value("system_settings") + xhiveframework.cache.delete_value("time_zone") + + if xhiveframework.flags.update_last_reset_password_date: + update_last_reset_password_date() + + def set_defaults(self): + from xhiveframework.translate import set_default_language + + for df in self.meta.get("fields"): + if df.fieldtype not in no_value_fields and self.has_value_changed(df.fieldname): + xhiveframework.db.set_default(df.fieldname, self.get(df.fieldname)) + + if self.language: + set_default_language(self.language) + + +def update_last_reset_password_date(): + xhiveframework.db.sql( + """ UPDATE `tabUser` + SET + last_password_reset_date = %s + WHERE + last_password_reset_date is null""", + today(), + ) + + +@xhiveframework.whitelist() +def load(): + from xhiveframework.utils.momentjs import get_all_timezones + + if "System Manager" not in xhiveframework.get_roles(): + xhiveframework.throw(_("Not permitted"), xhiveframework.PermissionError) + + all_defaults = xhiveframework.db.get_defaults() + defaults = {} + + for df in xhiveframework.get_meta("System Settings").get("fields"): + if df.fieldtype in ("Select", "Data"): + defaults[df.fieldname] = all_defaults.get(df.fieldname) + + return {"timezones": get_all_timezones(), "defaults": defaults} diff --git a/xhiveframework/core/doctype/system_settings/test_system_settings.py b/xhiveframework/core/doctype/system_settings/test_system_settings.py new file mode 100644 index 0000000..e8aa14e --- /dev/null +++ b/xhiveframework/core/doctype/system_settings/test_system_settings.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSystemSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/transaction_log/__init__.py b/xhiveframework/core/doctype/transaction_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/transaction_log/readme.md b/xhiveframework/core/doctype/transaction_log/readme.md new file mode 100644 index 0000000..09162ef --- /dev/null +++ b/xhiveframework/core/doctype/transaction_log/readme.md @@ -0,0 +1,16 @@ +# Transaction Log Changelog + +## v1.0.0 +Initial version + +The line hash summarizes: +- The index of the row +- The timestamp +- The document raw data + +The chain hash summarizes: +- The previous line hash +- The current line hash + +## v1.0.1 +Modification of the timestamp fieldtype from "Time" to "Datetime" \ No newline at end of file diff --git a/xhiveframework/core/doctype/transaction_log/test_transaction_log.py b/xhiveframework/core/doctype/transaction_log/test_transaction_log.py new file mode 100644 index 0000000..41334a6 --- /dev/null +++ b/xhiveframework/core/doctype/transaction_log/test_transaction_log.py @@ -0,0 +1,46 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import hashlib + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = [] + + +class TestTransactionLog(XhiveFrameworkTestCase): + def test_validate_chaining(self): + xhiveframework.get_doc( + { + "doctype": "Transaction Log", + "reference_doctype": "Test Doctype", + "document_name": "Test Document 1", + "data": "first_data", + } + ).insert(ignore_permissions=True) + + second_log = xhiveframework.get_doc( + { + "doctype": "Transaction Log", + "reference_doctype": "Test Doctype", + "document_name": "Test Document 2", + "data": "second_data", + } + ).insert(ignore_permissions=True) + + third_log = xhiveframework.get_doc( + { + "doctype": "Transaction Log", + "reference_doctype": "Test Doctype", + "document_name": "Test Document 3", + "data": "third_data", + } + ).insert(ignore_permissions=True) + + sha = hashlib.sha256() + sha.update( + xhiveframework.safe_encode(str(third_log.transaction_hash)) + + xhiveframework.safe_encode(str(second_log.chaining_hash)) + ) + + self.assertEqual(sha.hexdigest(), third_log.chaining_hash) diff --git a/xhiveframework/core/doctype/transaction_log/transaction_log.js b/xhiveframework/core/doctype/transaction_log/transaction_log.js new file mode 100644 index 0000000..98a3634 --- /dev/null +++ b/xhiveframework/core/doctype/transaction_log/transaction_log.js @@ -0,0 +1,4 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Transaction Log", {}); diff --git a/xhiveframework/core/doctype/transaction_log/transaction_log.json b/xhiveframework/core/doctype/transaction_log/transaction_log.json new file mode 100644 index 0000000..2135976 --- /dev/null +++ b/xhiveframework/core/doctype/transaction_log/transaction_log.json @@ -0,0 +1,124 @@ +{ + "actions": [], + "creation": "2018-02-06 11:48:51.270524", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "row_index", + "section_break_2", + "reference_doctype", + "document_name", + "column_break_5", + "timestamp", + "checksum_version", + "section_break_8", + "previous_hash", + "transaction_hash", + "chaining_hash", + "data", + "amended_from" + ], + "fields": [ + { + "fieldname": "row_index", + "fieldtype": "Data", + "label": "Row Index", + "read_only": 1 + }, + { + "fieldname": "section_break_2", + "fieldtype": "Section Break" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Data", + "label": "Reference Document Type", + "read_only": 1 + }, + { + "fieldname": "document_name", + "fieldtype": "Data", + "label": "Document Name", + "read_only": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "timestamp", + "fieldtype": "Datetime", + "label": "Timestamp", + "read_only": 1 + }, + { + "fieldname": "checksum_version", + "fieldtype": "Data", + "label": "Checksum Version", + "read_only": 1 + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "previous_hash", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Previous Hash", + "read_only": 1 + }, + { + "fieldname": "transaction_hash", + "fieldtype": "Small Text", + "label": "Transaction Hash", + "read_only": 1 + }, + { + "fieldname": "chaining_hash", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Chaining Hash", + "read_only": 1 + }, + { + "fieldname": "data", + "fieldtype": "Long Text", + "hidden": 1, + "label": "Data", + "read_only": 1 + }, + { + "fieldname": "amended_from", + "fieldtype": "Link", + "label": "Amended From", + "no_copy": 1, + "options": "Transaction Log", + "print_hide": 1, + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2022-08-03 12:20:54.684305", + "modified_by": "Administrator", + "module": "Core", + "name": "Transaction Log", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/transaction_log/transaction_log.py b/xhiveframework/core/doctype/transaction_log/transaction_log.py new file mode 100644 index 0000000..ccb34a6 --- /dev/null +++ b/xhiveframework/core/doctype/transaction_log/transaction_log.py @@ -0,0 +1,86 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import hashlib + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.query_builder import DocType +from xhiveframework.utils import cint, now_datetime + + +class TransactionLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + amended_from: DF.Link | None + chaining_hash: DF.SmallText | None + checksum_version: DF.Data | None + data: DF.LongText | None + document_name: DF.Data | None + previous_hash: DF.SmallText | None + reference_doctype: DF.Data | None + row_index: DF.Data | None + timestamp: DF.Datetime | None + transaction_hash: DF.SmallText | None + + # end: auto-generated types + def before_insert(self): + index = get_current_index() + self.row_index = index + self.timestamp = now_datetime() + if index != 1: + prev_hash = xhiveframework.get_all( + "Transaction Log", filters={"row_index": str(index - 1)}, pluck="chaining_hash", limit=1 + ) + if prev_hash: + self.previous_hash = prev_hash[0] + else: + self.previous_hash = "Indexing broken" + else: + self.previous_hash = self.hash_line() + self.transaction_hash = self.hash_line() + self.chaining_hash = self.hash_chain() + self.checksum_version = "v1.0.1" + + def hash_line(self): + sha = hashlib.sha256() + sha.update( + xhiveframework.safe_encode(str(self.row_index)) + + xhiveframework.safe_encode(str(self.timestamp)) + + xhiveframework.safe_encode(str(self.data)) + ) + return sha.hexdigest() + + def hash_chain(self): + sha = hashlib.sha256() + sha.update( + xhiveframework.safe_encode(str(self.transaction_hash)) + xhiveframework.safe_encode(str(self.previous_hash)) + ) + return sha.hexdigest() + + +def get_current_index(): + series = DocType("Series") + current = ( + xhiveframework.qb.from_(series).where(series.name == "TRANSACTLOG").for_update().select("current") + ).run() + + if current and current[0][0] is not None: + current = current[0][0] + + xhiveframework.db.sql( + """UPDATE `tabSeries` + SET `current` = `current` + 1 + where `name` = 'TRANSACTLOG'""" + ) + current = cint(current) + 1 + else: + xhiveframework.db.sql("INSERT INTO `tabSeries` (name, current) VALUES ('TRANSACTLOG', 1)") + current = 1 + return current diff --git a/xhiveframework/core/doctype/translation/__init__.py b/xhiveframework/core/doctype/translation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/translation/test_translation.py b/xhiveframework/core/doctype/translation/test_translation.py new file mode 100644 index 0000000..e9a8694 --- /dev/null +++ b/xhiveframework/core/doctype/translation/test_translation.py @@ -0,0 +1,110 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework import _ +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.translate import clear_cache + + +class TestTranslation(XhiveFrameworkTestCase): + def setUp(self): + xhiveframework.db.delete("Translation") + + def tearDown(self): + xhiveframework.local.lang = "en" + clear_cache() + + def test_doctype(self): + translation_data = get_translation_data() + for key, val in translation_data.items(): + xhiveframework.local.lang = key + + translation = create_translation(key, val) + self.assertEqual(_(val[0]), val[1]) + + xhiveframework.delete_doc("Translation", translation.name) + self.assertEqual(_(val[0]), val[0]) + + def test_parent_language(self): + data = [ + ["es", ["Test Data", "datos de prueba"]], + ["es", ["Test Spanish", "prueba de español"]], + ["es-MX", ["Test Data", "pruebas de datos"]], + ] + + for key, val in data: + create_translation(key, val) + + xhiveframework.local.lang = "es" + + self.assertTrue(_(data[0][0]), data[0][1]) + + self.assertTrue(_(data[1][0]), data[1][1]) + + xhiveframework.local.lang = "es-MX" + + # different translation for es-MX + self.assertTrue(_(data[2][0]), data[2][1]) + + # from spanish (general) + self.assertTrue(_(data[1][0]), data[1][1]) + + def test_multi_language_translations(self): + source = "User" + self.assertNotEqual(_(source, lang="de"), _(source, lang="es")) + + def test_html_content_data_translation(self): + source = """ + MacBook Air lasts up to an incredible 12 hours between charges. So from your morning coffee to + your evening commute, you can work unplugged. When it’s time to kick back and relax, + you can get up to 12 hours of iTunes movie playback. And with up to 30 days of standby time, + you can go away for weeks and pick up where you left off.Whatever the task, + fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics 6000 are up to it.
    + """ + + target = """ + MacBook Air dura hasta 12 horas increíbles entre cargas. Por lo tanto, + desde el café de la mañana hasta el viaje nocturno, puede trabajar desconectado. + Cuando es hora de descansar y relajarse, puede obtener hasta 12 horas de reproducción de películas de iTunes. + Y con hasta 30 días de tiempo de espera, puede irse por semanas y continuar donde lo dejó. Sea cual sea la tarea, + los procesadores Intel Core i5 e i7 de quinta generación con Intel HD Graphics 6000 son capaces de hacerlo. + """ + + create_translation("es", [source, target]) + + source = """ + MacBook Air lasts up to an incredible 12 hours between charges. So from your morning coffee to + your evening commute, you can work unplugged. When it’s time to kick back and relax, + you can get up to 12 hours of iTunes movie playback. And with up to 30 days of standby time, + you can go away for weeks and pick up where you left off.Whatever the task, + fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics 6000 are up to it.
    + """ + + self.assertTrue(_(source), target) + + +def get_translation_data(): + html_source_data = """ + Test Data""" + html_translated_data = """ + testituloksia """ + + return { + "hr": ["Test data", "Testdaten"], + "ms": ["Test Data", "ujian Data"], + "et": ["Test Data", "testandmed"], + "es": ["Test Data", "datos de prueba"], + "en": ["Quotation", "Tax Invoice"], + "fi": [html_source_data, html_translated_data], + } + + +def create_translation(key, val): + translation = xhiveframework.new_doc("Translation") + translation.language = key + translation.source_text = val[0] + translation.translated_text = val[1] + translation.save() + return translation diff --git a/xhiveframework/core/doctype/translation/translation.js b/xhiveframework/core/doctype/translation/translation.js new file mode 100644 index 0000000..51c9c4f --- /dev/null +++ b/xhiveframework/core/doctype/translation/translation.js @@ -0,0 +1,8 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Translation", { + refresh: function () { + // + }, +}); diff --git a/xhiveframework/core/doctype/translation/translation.json b/xhiveframework/core/doctype/translation/translation.json new file mode 100644 index 0000000..68b83ed --- /dev/null +++ b/xhiveframework/core/doctype/translation/translation.json @@ -0,0 +1,111 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "hash", + "creation": "2016-02-17 12:21:16.175465", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "contributed", + "language", + "section_break_4", + "source_text", + "context", + "column_break_6", + "translated_text", + "section_break_6", + "contribution_status", + "contribution_docname" + ], + "fields": [ + { + "fieldname": "language", + "fieldtype": "Link", + "label": "Language", + "options": "Language", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break" + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break" + }, + { + "fieldname": "context", + "fieldtype": "Data", + "label": "Context" + }, + { + "default": "0", + "fieldname": "contributed", + "fieldtype": "Check", + "hidden": 1, + "label": "Contributed", + "read_only": 1 + }, + { + "depends_on": "doc.contributed", + "fieldname": "contribution_status", + "fieldtype": "Select", + "label": "Contribution Status", + "options": "\nPending\nVerified\nRejected", + "read_only": 1 + }, + { + "fieldname": "contribution_docname", + "fieldtype": "Data", + "hidden": 1, + "label": "Contribution Document Name", + "read_only": 1 + }, + { + "description": "If your data is in HTML, please copy paste the exact HTML code with the tags.", + "fieldname": "source_text", + "fieldtype": "Code", + "label": "Source Text", + "reqd": 1 + }, + { + "fieldname": "translated_text", + "fieldtype": "Code", + "in_list_view": 1, + "label": "Translated Text", + "reqd": 1 + } + ], + "links": [], + "modified": "2022-07-04 06:53:54.997004", + "modified_by": "Administrator", + "module": "Core", + "name": "Translation", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "source_text", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/translation/translation.py b/xhiveframework/core/doctype/translation/translation.py new file mode 100644 index 0000000..af9a2d0 --- /dev/null +++ b/xhiveframework/core/doctype/translation/translation.py @@ -0,0 +1,46 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.translate import MERGED_TRANSLATION_KEY, USER_TRANSLATION_KEY, get_translator_url +from xhiveframework.utils import is_html, strip_html_tags + + +class Translation(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + context: DF.Data | None + contributed: DF.Check + contribution_docname: DF.Data | None + contribution_status: DF.Literal["", "Pending", "Verified", "Rejected"] + language: DF.Link + source_text: DF.Code + translated_text: DF.Code + + # end: auto-generated types + def validate(self): + if is_html(self.source_text): + self.remove_html_from_source() + + def remove_html_from_source(self): + self.source_text = strip_html_tags(self.source_text).strip() + + def on_update(self): + clear_user_translation_cache(self.language) + + def on_trash(self): + clear_user_translation_cache(self.language) + + +def clear_user_translation_cache(lang): + xhiveframework.cache.hdel(USER_TRANSLATION_KEY, lang) + xhiveframework.cache.hdel(MERGED_TRANSLATION_KEY, lang) diff --git a/xhiveframework/core/doctype/user/__init__.py b/xhiveframework/core/doctype/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user/test_records.json b/xhiveframework/core/doctype/user/test_records.json new file mode 100644 index 0000000..9d1bf0e --- /dev/null +++ b/xhiveframework/core/doctype/user/test_records.json @@ -0,0 +1,95 @@ +[ + { + "doctype": "User", + "email": "test@example.com", + "enabled": 1, + "first_name": "_Test", + "new_password": "Eastern_43A1W", + "roles": [ + { + "doctype": "Has Role", + "parentfield": "roles", + "role": "_Test Role" + }, + { + "doctype": "Has Role", + "parentfield": "roles", + "role": "System Manager" + } + ] + }, + { + "doctype": "User", + "email": "test1@example.com", + "first_name": "_Test1", + "new_password": "Eastern_43A1W" + }, + { + "doctype": "User", + "email": "test2@example.com", + "first_name": "_Test2", + "new_password": "Eastern_43A1W", + "enabled": 1 + }, + { + "doctype": "User", + "email": "test3@example.com", + "first_name": "_Test3", + "new_password": "Eastern_43A1W", + "enabled": 1 + }, + { + "doctype": "User", + "email": "test4@example.com", + "first_name": "_Test4", + "new_password": "Eastern_43A1W", + "enabled": 1 + }, + { + "doctype": "User", + "email": "test'5@example.com", + "first_name": "_Test'5", + "new_password": "Eastern_43A1W", + "enabled": 1 + }, + { + "doctype": "User", + "email": "testperm@example.com", + "first_name": "_Test Perm", + "new_password": "Eastern_43A1W", + "enabled": 1 + }, + { + "doctype": "User", + "email": "testdelete@example.com", + "enabled": 1, + "first_name": "_Test", + "new_password": "Eastern_43A1W", + "roles": [ + { + "doctype": "Has Role", + "parentfield": "roles", + "role": "_Test Role 2" + }, + { + "doctype": "Has Role", + "parentfield": "roles", + "role": "System Manager" + } + ] + }, + { + "doctype": "User", + "email": "testpassword@example.com", + "enabled": 1, + "first_name": "_Test", + "new_password": "Eastern_43A1W", + "roles": [ + { + "doctype": "Has Role", + "parentfield": "roles", + "role": "System Manager" + } + ] + } +] diff --git a/xhiveframework/core/doctype/user/test_user.py b/xhiveframework/core/doctype/user/test_user.py new file mode 100644 index 0000000..9039907 --- /dev/null +++ b/xhiveframework/core/doctype/user/test_user.py @@ -0,0 +1,493 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import json +import time +from contextlib import contextmanager +from unittest.mock import patch +from urllib.parse import parse_qs, urlparse + +from werkzeug.http import parse_cookie + +import xhiveframework +import xhiveframework.exceptions +from xhiveframework.core.doctype.user.user import ( + handle_password_test_fail, + reset_password, + sign_up, + test_password_strength, + update_password, + verify_password, +) +from xhiveframework.desk.notifications import extract_mentions +from xhiveframework.xhiveframeworkclient import XhiveFrameworkClient +from xhiveframework.model.delete_doc import delete_doc +from xhiveframework.tests.test_api import XhiveFrameworkAPITestCase +from xhiveframework.tests.utils import XhiveFrameworkTestCase, change_settings +from xhiveframework.utils import get_url + +user_module = xhiveframework.core.doctype.user.user +test_records = xhiveframework.get_test_records("User") + + +class TestUser(XhiveFrameworkTestCase): + def tearDown(self): + # disable password strength test + xhiveframework.db.set_single_value("System Settings", "enable_password_policy", 0) + xhiveframework.db.set_single_value("System Settings", "minimum_password_score", "") + xhiveframework.db.set_single_value("System Settings", "password_reset_limit", 3) + xhiveframework.set_user("Administrator") + + @staticmethod + def reset_password(user) -> str: + link = user.reset_password() + return parse_qs(urlparse(link).query)["key"][0] + + def test_user_type(self): + user_id = xhiveframework.generate_hash() + "@example.com" + new_user = xhiveframework.get_doc(doctype="User", email=user_id, first_name="Tester").insert() + self.assertEqual(new_user.user_type, "Website User") + + # social login userid for xhiveframework + self.assertTrue(new_user.social_logins[0].userid) + self.assertEqual(new_user.social_logins[0].provider, "xhiveframework") + + # role with desk access + new_user.add_roles("_Test Role 2") + new_user.save() + self.assertEqual(new_user.user_type, "System User") + + # clear role + new_user.roles = [] + new_user.save() + self.assertEqual(new_user.user_type, "Website User") + + # role without desk access + new_user.add_roles("_Test Role 4") + new_user.save() + self.assertEqual(new_user.user_type, "Website User") + + delete_contact(new_user.name) + xhiveframework.delete_doc("User", new_user.name) + + def test_delete(self): + xhiveframework.get_doc("User", "test@example.com").add_roles("_Test Role 2") + self.assertRaises(xhiveframework.LinkExistsError, delete_doc, "Role", "_Test Role 2") + xhiveframework.db.delete("Has Role", {"role": "_Test Role 2"}) + delete_doc("Role", "_Test Role 2") + + if xhiveframework.db.exists("User", "_test@example.com"): + delete_contact("_test@example.com") + delete_doc("User", "_test@example.com") + + user = xhiveframework.copy_doc(test_records[1]) + user.email = "_test@example.com" + user.insert() + + xhiveframework.get_doc({"doctype": "ToDo", "description": "_Test"}).insert() + + delete_contact("_test@example.com") + delete_doc("User", "_test@example.com") + + self.assertTrue( + not xhiveframework.db.sql("""select * from `tabToDo` where allocated_to=%s""", ("_test@example.com",)) + ) + + from xhiveframework.core.doctype.role.test_role import test_records as role_records + + xhiveframework.copy_doc(role_records[1]).insert() + + def test_get_value(self): + self.assertEqual(xhiveframework.db.get_value("User", "test@example.com"), "test@example.com") + self.assertEqual(xhiveframework.db.get_value("User", {"email": "test@example.com"}), "test@example.com") + self.assertEqual( + xhiveframework.db.get_value("User", {"email": "test@example.com"}, "email"), "test@example.com" + ) + self.assertEqual( + xhiveframework.db.get_value("User", {"email": "test@example.com"}, ["first_name", "email"]), + ("_Test", "test@example.com"), + ) + self.assertEqual( + xhiveframework.db.get_value( + "User", {"email": "test@example.com", "first_name": "_Test"}, ["first_name", "email"] + ), + ("_Test", "test@example.com"), + ) + + test_user = xhiveframework.db.sql("select * from tabUser where name='test@example.com'", as_dict=True)[0] + self.assertEqual( + xhiveframework.db.get_value("User", {"email": "test@example.com"}, "*", as_dict=True), test_user + ) + + self.assertEqual(xhiveframework.db.get_value("User", "xxxtest@example.com"), None) + + xhiveframework.db.set_single_value("Website Settings", "_test", "_test_val") + self.assertEqual(xhiveframework.db.get_value("Website Settings", None, "_test"), "_test_val") + self.assertEqual(xhiveframework.db.get_value("Website Settings", "Website Settings", "_test"), "_test_val") + + def test_high_permlevel_validations(self): + user = xhiveframework.get_meta("User") + self.assertTrue("roles" in [d.fieldname for d in user.get_high_permlevel_fields()]) + + me = xhiveframework.get_doc("User", "testperm@example.com") + me.remove_roles("System Manager") + + xhiveframework.set_user("testperm@example.com") + + me = xhiveframework.get_doc("User", "testperm@example.com") + me.add_roles("System Manager") + + # system manager is not added (it is reset) + self.assertFalse("System Manager" in [d.role for d in me.roles]) + + # ignore permlevel using flags + me.flags.ignore_permlevel_for_fields = ["roles"] + me.add_roles("System Manager") + + # system manager now added due to flags + self.assertTrue("System Manager" in [d.role for d in me.get("roles")]) + + # reset flags + me.flags.ignore_permlevel_for_fields = None + + # change user + xhiveframework.set_user("Administrator") + + me = xhiveframework.get_doc("User", "testperm@example.com") + me.add_roles("System Manager") + + # system manager now added by Administrator + self.assertTrue("System Manager" in [d.role for d in me.get("roles")]) + + def test_delete_user(self): + new_user = xhiveframework.get_doc( + dict(doctype="User", email="test-for-delete@example.com", first_name="Tester Delete User") + ).insert(ignore_if_duplicate=True) + self.assertEqual(new_user.user_type, "Website User") + + # role with desk access + new_user.add_roles("_Test Role 2") + new_user.save() + self.assertEqual(new_user.user_type, "System User") + + comm = xhiveframework.get_doc( + { + "doctype": "Communication", + "subject": "To check user able to delete even if linked with communication", + "content": "To check user able to delete even if linked with communication", + "sent_or_received": "Sent", + "user": new_user.name, + } + ) + comm.insert(ignore_permissions=True) + + delete_contact(new_user.name) + xhiveframework.delete_doc("User", new_user.name) + self.assertFalse(xhiveframework.db.exists("User", new_user.name)) + + def test_password_strength(self): + # Test Password without Password Strength Policy + xhiveframework.db.set_single_value("System Settings", "enable_password_policy", 0) + + # password policy is disabled, test_password_strength should be ignored + result = test_password_strength("test_password") + self.assertFalse(result.get("feedback", None)) + + # Test Password with Password Strenth Policy Set + xhiveframework.db.set_single_value("System Settings", "enable_password_policy", 1) + xhiveframework.db.set_single_value("System Settings", "minimum_password_score", 2) + + # Score 1; should now fail + result = test_password_strength("bee2ve") + self.assertEqual(result["feedback"]["password_policy_validation_passed"], False) + self.assertRaises(xhiveframework.exceptions.ValidationError, handle_password_test_fail, result["feedback"]) + self.assertRaises( + xhiveframework.exceptions.ValidationError, handle_password_test_fail, result + ) # test backwards compatibility + + # Score 4; should pass + result = test_password_strength("Eastern_43A1W") + self.assertEqual(result["feedback"]["password_policy_validation_passed"], True) + + # test password strength while saving user with new password + user = xhiveframework.get_doc("User", "test@example.com") + xhiveframework.flags.in_test = False + user.new_password = "password" + self.assertRaises(xhiveframework.exceptions.ValidationError, user.save) + user.reload() + user.new_password = "Eastern_43A1W" + user.save() + xhiveframework.flags.in_test = True + + def test_comment_mentions(self): + comment = """ + + @Test + + """ + self.assertEqual(extract_mentions(comment)[0], "test.comment@example.com") + + comment = """ +
    + Testing comment, + + @Test + + please check +
    + """ + self.assertEqual(extract_mentions(comment)[0], "test.comment@example.com") + comment = """ +
    + Testing comment for + + @Test + + and + + @Test + + please check +
    + """ + self.assertEqual(extract_mentions(comment)[0], "test_user@example.com") + self.assertEqual(extract_mentions(comment)[1], "test.again@example1.com") + + xhiveframework.delete_doc("User Group", "Team") + doc = xhiveframework.get_doc( + { + "doctype": "User Group", + "name": "Team", + "user_group_members": [{"user": "test@example.com"}, {"user": "test1@example.com"}], + } + ) + + doc.insert() + + comment = """ +
    + Testing comment for + + @Team + and + + @Unknown Team + + please check +
    + """ + self.assertListEqual(extract_mentions(comment), ["test@example.com", "test1@example.com"]) + + @change_settings("System Settings", commit=True, password_reset_limit=1) + def test_rate_limiting_for_reset_password(self): + url = get_url() + data = {"cmd": "xhiveframework.core.doctype.user.user.reset_password", "user": "test@test.com"} + + # Clear rate limit tracker to start fresh + key = f"rl:{data['cmd']}:{data['user']}" + xhiveframework.cache.delete(key) + + c = XhiveFrameworkClient(url) + res1 = c.session.post(url, data=data, verify=c.verify, headers=c.headers) + res2 = c.session.post(url, data=data, verify=c.verify, headers=c.headers) + self.assertEqual(res1.status_code, 404) + self.assertEqual(res2.status_code, 417) + + def test_user_rename(self): + old_name = "test_user_rename@example.com" + new_name = "test_user_rename_new@example.com" + user = xhiveframework.get_doc( + { + "doctype": "User", + "email": old_name, + "enabled": 1, + "first_name": "_Test", + "new_password": "Eastern_43A1W", + "roles": [{"doctype": "Has Role", "parentfield": "roles", "role": "System Manager"}], + } + ).insert(ignore_permissions=True, ignore_if_duplicate=True) + + xhiveframework.rename_doc("User", user.name, new_name) + self.assertTrue(xhiveframework.db.exists("Notification Settings", new_name)) + + xhiveframework.delete_doc("User", new_name) + + def test_signup(self): + import xhiveframework.website.utils + + random_user = xhiveframework.mock("email") + random_user_name = xhiveframework.mock("name") + # disabled signup + with patch.object(user_module, "is_signup_disabled", return_value=True): + self.assertRaisesRegex( + xhiveframework.exceptions.ValidationError, + "Sign Up is disabled", + sign_up, + random_user, + random_user_name, + "/signup", + ) + + self.assertTupleEqual( + sign_up(random_user, random_user_name, "/welcome"), + (1, "Please check your email for verification"), + ) + self.assertEqual(xhiveframework.cache.hget("redirect_after_login", random_user), "/welcome") + + # re-register + self.assertTupleEqual(sign_up(random_user, random_user_name, "/welcome"), (0, "Already Registered")) + + # disabled user + user = xhiveframework.get_doc("User", random_user) + user.enabled = 0 + user.save() + + self.assertTupleEqual( + sign_up(random_user, random_user_name, "/welcome"), (0, "Registered but disabled") + ) + + # throttle user creation + with patch.object(user_module.xhiveframework.db, "get_creation_count", return_value=301): + self.assertRaisesRegex( + xhiveframework.exceptions.ValidationError, + "Throttled", + sign_up, + xhiveframework.mock("email"), + random_user_name, + "/signup", + ) + + @change_settings("System Settings", password_reset_limit=6) + def test_reset_password(self): + from xhiveframework.auth import CookieManager, LoginManager + from xhiveframework.utils import set_request + + old_password = "Eastern_43A1W" + new_password = "easy_password" + + set_request(path="/random") + xhiveframework.local.cookie_manager = CookieManager() + xhiveframework.local.login_manager = LoginManager() + # used by rate limiter when calling reset_password + xhiveframework.local.request_ip = "127.0.0.69" + + xhiveframework.set_user("testpassword@example.com") + test_user = xhiveframework.get_doc("User", "testpassword@example.com") + key = self.reset_password(test_user) + self.assertEqual(update_password(new_password, key=key), "/app") + self.assertEqual( + update_password(new_password, key="wrong_key"), + "The reset password link has either been used before or is invalid", + ) + + # password verification should fail with old password + self.assertRaises(xhiveframework.exceptions.AuthenticationError, verify_password, old_password) + verify_password(new_password) + + # reset password + update_password(old_password, old_password=new_password) + self.assertRaises(TypeError, update_password, "test", 1, ["like", "%"]) + + password_strength_response = { + "feedback": {"password_policy_validation_passed": False, "suggestions": ["Fix password"]} + } + + # password strength failure test + with patch.object(user_module, "test_password_strength", return_value=password_strength_response): + self.assertRaisesRegex( + xhiveframework.exceptions.ValidationError, + "Fix password", + update_password, + new_password, + 0, + test_user.reset_password_key, + ) + + # test redirect URL for website users + xhiveframework.set_user("test2@example.com") + self.assertEqual(update_password(new_password, old_password=old_password), "/") + # reset password + update_password(old_password, old_password=new_password) + + # test API endpoint + with patch.object(user_module.xhiveframework, "sendmail") as sendmail: + xhiveframework.clear_messages() + test_user = xhiveframework.get_doc("User", "test2@example.com") + self.assertEqual(reset_password(user="test2@example.com"), None) + test_user.reload() + link = sendmail.call_args_list[0].kwargs["args"]["link"] + key = parse_qs(urlparse(link).query)["key"][0] + self.assertEqual(update_password(new_password, key=key), "/") + update_password(old_password, old_password=new_password) + self.assertEqual( + xhiveframework.message_log[0].get("message"), + "Password reset instructions have been sent to your email", + ) + + sendmail.assert_called_once() + self.assertEqual(sendmail.call_args[1]["recipients"], "test2@example.com") + + self.assertEqual(reset_password(user="test2@example.com"), None) + self.assertEqual(reset_password(user="Administrator"), "not allowed") + self.assertEqual(reset_password(user="random"), "not found") + + def test_user_onload_modules(self): + from xhiveframework.config import get_modules_from_all_apps + from xhiveframework.desk.form.load import getdoc + + xhiveframework.response.docs = [] + getdoc("User", "Administrator") + doc = xhiveframework.response.docs[0] + self.assertListEqual( + sorted(doc.get("__onload").get("all_modules", [])), + sorted(m.get("module_name") for m in get_modules_from_all_apps()), + ) + + @change_settings("System Settings", reset_password_link_expiry_duration=1) + def test_reset_password_link_expiry(self): + new_password = "new_password" + xhiveframework.set_user("testpassword@example.com") + test_user = xhiveframework.get_doc("User", "testpassword@example.com") + key = self.reset_password(test_user) + time.sleep(1) + + self.assertEqual( + update_password(new_password, key=key), + "The reset password link has been expired", + ) + + +class TestImpersonation(XhiveFrameworkAPITestCase): + def test_impersonation(self): + with test_user(roles=["System Manager"]) as user: + self.post( + self.method_path("xhiveframework.core.doctype.user.user.impersonate"), + {"user": user.name, "reason": "test", "sid": self.sid}, + ) + resp = self.get(self.method_path("xhiveframework.auth.get_logged_user")) + self.assertEqual(resp.json["message"], user.name) + + +@contextmanager +def test_user(*, first_name: str | None = None, email: str | None = None, roles: list[str], **kwargs): + try: + first_name = first_name or xhiveframework.generate_hash() + email = email or (first_name + "@example.com") + user = xhiveframework.new_doc( + "User", + send_welcome_email=0, + email=email, + first_name=first_name, + **kwargs, + ) + user.append_roles(*roles) + user.insert() + yield user + finally: + user.delete(force=True, ignore_permissions=True) + xhiveframework.db.commit() + + +def delete_contact(user): + xhiveframework.db.delete("Contact", {"email_id": user}) + xhiveframework.db.delete("Contact Email", {"email_id": user}) diff --git a/xhiveframework/core/doctype/user/user.js b/xhiveframework/core/doctype/user/user.js new file mode 100644 index 0000000..68fc0f2 --- /dev/null +++ b/xhiveframework/core/doctype/user/user.js @@ -0,0 +1,413 @@ +xhiveframework.ui.form.on("User", { + before_load: function (frm) { + let update_tz_options = function () { + frm.fields_dict.time_zone.set_data(xhiveframework.all_timezones); + }; + + if (!xhiveframework.all_timezones) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.get_timezones", + callback: function (r) { + xhiveframework.all_timezones = r.message.timezones; + update_tz_options(); + }, + }); + } else { + update_tz_options(); + } + }, + + time_zone: function (frm) { + if (frm.doc.time_zone && frm.doc.time_zone.startsWith("Etc")) { + frm.set_df_property( + "time_zone", + "description", + __("Note: Etc timezones have their signs reversed.") + ); + } + }, + + role_profile_name: function (frm) { + if (frm.doc.role_profile_name) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.get_role_profile", + args: { + role_profile: frm.doc.role_profile_name, + }, + callback: function (data) { + frm.set_value("roles", []); + $.each(data.message || [], function (i, v) { + var d = frm.add_child("roles"); + d.role = v.role; + }); + frm.roles_editor.show(); + }, + }); + } + }, + + module_profile: function (frm) { + if (frm.doc.module_profile) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.get_module_profile", + args: { + module_profile: frm.doc.module_profile, + }, + callback: function (data) { + frm.set_value("block_modules", []); + $.each(data.message || [], function (i, v) { + let d = frm.add_child("block_modules"); + d.module = v.module; + }); + frm.module_editor && frm.module_editor.show(); + }, + }); + } + }, + + onload: function (frm) { + frm.can_edit_roles = has_access_to_edit_user(); + + if (frm.is_new() && frm.roles_editor) { + frm.roles_editor.reset(); + } + + if ( + frm.can_edit_roles && + !frm.is_new() && + ["System User", "Website User"].includes(frm.doc.user_type) + ) { + if (!frm.roles_editor) { + const role_area = $('
    ').appendTo( + frm.fields_dict.roles_html.wrapper + ); + + frm.roles_editor = new xhiveframework.RoleEditor( + role_area, + frm, + frm.doc.role_profile_name ? 1 : 0 + ); + + if (frm.doc.user_type == "System User") { + var module_area = $("
    ").appendTo(frm.fields_dict.modules_html.wrapper); + frm.module_editor = new xhiveframework.ModuleEditor(frm, module_area); + } + } else { + frm.roles_editor.show(); + } + } + }, + refresh: function (frm) { + let doc = frm.doc; + + if (frm.is_new()) { + frm.set_value("time_zone", xhiveframework.sys_defaults.time_zone); + } + + if ( + ["System User", "Website User"].includes(frm.doc.user_type) && + !frm.is_new() && + !frm.roles_editor && + frm.can_edit_roles + ) { + frm.reload_doc(); + return; + } + + frm.toggle_display(["sb1", "sb3", "modules_access"], false); + frm.trigger("setup_impersonation"); + + if (!frm.is_new()) { + if (has_access_to_edit_user()) { + frm.add_custom_button( + __("Set User Permissions"), + function () { + xhiveframework.route_options = { + user: doc.name, + }; + xhiveframework.set_route("List", "User Permission"); + }, + __("Permissions") + ); + + frm.add_custom_button( + __("View Permitted Documents"), + () => + xhiveframework.set_route("query-report", "Permitted Documents For User", { + user: frm.doc.name, + }), + __("Permissions") + ); + + frm.toggle_display(["sb1", "sb3", "modules_access"], true); + } + + frm.add_custom_button( + __("Reset Password"), + function () { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.reset_password", + args: { + user: frm.doc.name, + }, + }); + }, + __("Password") + ); + + if (xhiveframework.user.has_role("System Manager")) { + xhiveframework.db.get_single_value("LDAP Settings", "enabled").then((value) => { + if (value === 1 && frm.doc.name != "Administrator") { + frm.add_custom_button( + __("Reset LDAP Password"), + function () { + const d = new xhiveframework.ui.Dialog({ + title: __("Reset LDAP Password"), + fields: [ + { + label: __("New Password"), + fieldtype: "Password", + fieldname: "new_password", + reqd: 1, + }, + { + label: __("Confirm New Password"), + fieldtype: "Password", + fieldname: "confirm_password", + reqd: 1, + }, + { + label: __("Logout All Sessions"), + fieldtype: "Check", + fieldname: "logout_sessions", + }, + ], + primary_action: (values) => { + d.hide(); + if (values.new_password !== values.confirm_password) { + xhiveframework.throw(__("Passwords do not match!")); + } + xhiveframework.call( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.reset_password", + { + user: frm.doc.email, + password: values.new_password, + logout: values.logout_sessions, + } + ); + }, + }); + d.show(); + }, + __("Password") + ); + } + }); + } + + if ( + cint(xhiveframework.boot.sysdefaults.enable_two_factor_auth) && + (xhiveframework.session.user == doc.name || xhiveframework.user.has_role("System Manager")) + ) { + frm.add_custom_button( + __("Reset OTP Secret"), + function () { + xhiveframework.call({ + method: "xhiveframework.twofactor.reset_otp_secret", + args: { + user: frm.doc.name, + }, + }); + }, + __("Password") + ); + } + + frm.trigger("enabled"); + + if (frm.roles_editor && frm.can_edit_roles) { + frm.roles_editor.disable = frm.doc.role_profile_name ? 1 : 0; + frm.roles_editor.show(); + } + + frm.module_editor && frm.module_editor.show(); + + if (xhiveframework.session.user == doc.name) { + // update display settings + if (doc.user_image) { + xhiveframework.boot.user_info[xhiveframework.session.user].image = xhiveframework.utils.get_file_link( + doc.user_image + ); + } + } + } + if (frm.doc.user_emails && xhiveframework.model.can_create("Email Account")) { + var found = 0; + for (var i = 0; i < frm.doc.user_emails.length; i++) { + if (frm.doc.email == frm.doc.user_emails[i].email_id) { + found = 1; + } + } + if (!found) { + frm.add_custom_button(__("Create User Email"), function () { + frm.events.create_user_email(frm); + }); + } + } + + if (xhiveframework.route_flags.unsaved === 1) { + delete xhiveframework.route_flags.unsaved; + for (let i = 0; i < frm.doc.user_emails.length; i++) { + frm.doc.user_emails[i].idx = frm.doc.user_emails[i].idx + 1; + } + frm.dirty(); + } + frm.trigger("time_zone"); + }, + validate: function (frm) { + if (frm.roles_editor) { + frm.roles_editor.set_roles_in_table(); + } + }, + enabled: function (frm) { + var doc = frm.doc; + if (!frm.is_new() && has_access_to_edit_user()) { + frm.toggle_display(["sb1", "sb3", "modules_access"], doc.enabled); + frm.set_df_property("enabled", "read_only", 0); + } + + if (xhiveframework.session.user !== "Administrator") { + frm.toggle_enable("email", frm.is_new()); + } + }, + create_user_email: function (frm) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.has_email_account", + args: { + email: frm.doc.email, + }, + callback: function (r) { + if (!Array.isArray(r.message) || !r.message.length) { + xhiveframework.route_options = { + email_id: frm.doc.email, + awaiting_password: 1, + enable_incoming: 1, + }; + xhiveframework.model.with_doctype("Email Account", function (doc) { + doc = xhiveframework.model.get_new_doc("Email Account"); + xhiveframework.route_flags.linked_user = frm.doc.name; + xhiveframework.route_flags.delete_user_from_locals = true; + xhiveframework.set_route("Form", "Email Account", doc.name); + }); + } else { + xhiveframework.route_flags.create_user_account = frm.doc.name; + xhiveframework.set_route("Form", "Email Account", r.message[0]["name"]); + } + }, + }); + }, + generate_keys: function (frm) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.generate_keys", + args: { + user: frm.doc.name, + }, + callback: function (r) { + if (r.message) { + xhiveframework.msgprint(__("Save API Secret: {0}", [r.message.api_secret])); + frm.reload_doc(); + } + }, + }); + }, + after_save: function (frm) { + /** + * Checks whether the effective value has changed. + * + * @param {Array.} - Tuple with new override, previous override, + * and optionally fallback. + * @returns {boolean} - Whether the resulting value has effectively changed + */ + const has_effectively_changed = ([new_override, prev_override, fallback = undefined]) => { + const prev_effective = prev_override || fallback; + const new_effective = new_override || fallback; + return new_override !== undefined && prev_effective !== new_effective; + }; + + const doc = frm.doc; + const boot = xhiveframework.boot; + const attr_tuples = [ + [doc.language, boot.user.language, boot.sysdefaults.language], + [doc.time_zone, boot.time_zone.user, boot.time_zone.system], + [doc.desk_theme, boot.user.desk_theme], // No system default. + ]; + + if (doc.name === xhiveframework.session.user && attr_tuples.some(has_effectively_changed)) { + xhiveframework.msgprint(__("Refreshing...")); + window.location.reload(); + } + }, + setup_impersonation: function (frm) { + if (xhiveframework.session.user === "Administrator" && frm.doc.name != "Administrator") { + frm.add_custom_button(__("Impersonate"), () => { + if (frm.doc.restrict_ip) { + xhiveframework.msgprint({ + message: + "There's IP restriction for this user, you can not impersonate as this user.", + title: "IP restriction is enabled", + }); + return; + } + xhiveframework.prompt( + [ + { + fieldname: "reason", + fieldtype: "Small Text", + label: "Reason for impersonating", + description: __("Note: This will be shared with user."), + reqd: 1, + }, + ], + (values) => { + xhiveframework + .xcall("xhiveframework.core.doctype.user.user.impersonate", { + user: frm.doc.name, + reason: values.reason, + }) + .then(() => window.location.reload()); + }, + __("Impersonate as {0}", [frm.doc.name]), + __("Confirm") + ); + }); + } + }, +}); + +xhiveframework.ui.form.on("User Email", { + email_account(frm, cdt, cdn) { + let child_row = locals[cdt][cdn]; + xhiveframework.model.get_value( + "Email Account", + child_row.email_account, + "auth_method", + (value) => { + child_row.used_oauth = value.auth_method === "OAuth"; + frm.refresh_field("user_emails", cdn, "used_oauth"); + } + ); + }, +}); + +function has_access_to_edit_user() { + return has_common(xhiveframework.user_roles, get_roles_for_editing_user()); +} + +function get_roles_for_editing_user() { + return ( + xhiveframework + .get_meta("User") + .permissions.filter((perm) => perm.permlevel >= 1 && perm.write) + .map((perm) => perm.role) || ["System Manager"] + ); +} diff --git a/xhiveframework/core/doctype/user/user.json b/xhiveframework/core/doctype/user/user.json new file mode 100644 index 0000000..2db45c9 --- /dev/null +++ b/xhiveframework/core/doctype/user/user.json @@ -0,0 +1,806 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "creation": "2022-01-10 17:29:51.672911", + "description": "Represents a User in the system.", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "user_details_tab", + "enabled", + "section_break_3", + "email", + "first_name", + "middle_name", + "last_name", + "column_break0", + "full_name", + "username", + "column_break_11", + "language", + "time_zone", + "send_welcome_email", + "unsubscribed", + "user_image", + "roles_permissions_tab", + "sb1", + "role_profile_name", + "roles_html", + "roles", + "sb_allow_modules", + "module_profile", + "modules_html", + "block_modules", + "home_settings", + "short_bio", + "gender", + "birth_date", + "interest", + "column_break_26", + "phone", + "location", + "bio", + "column_break_22", + "mobile_no", + "settings_tab", + "desk_settings_section", + "mute_sounds", + "desk_theme", + "banner_image", + "change_password", + "new_password", + "logout_all_sessions", + "reset_password_key", + "last_reset_password_key_generated_on", + "last_password_reset_date", + "redirect_url", + "document_follow_notifications_section", + "document_follow_notify", + "document_follow_frequency", + "column_break_75", + "follow_created_documents", + "follow_commented_documents", + "follow_liked_documents", + "follow_assigned_documents", + "follow_shared_documents", + "email_settings", + "email_signature", + "thread_notify", + "send_me_a_copy", + "allowed_in_mentions", + "user_emails", + "sb2", + "defaults", + "sb3", + "simultaneous_sessions", + "restrict_ip", + "last_ip", + "column_break1", + "login_after", + "user_type", + "last_active", + "section_break_63", + "login_before", + "bypass_restrict_ip_check_if_2fa_enabled", + "last_login", + "last_known_versions", + "third_party_authentication", + "social_logins", + "api_access", + "api_key", + "generate_keys", + "column_break_65", + "api_secret", + "onboarding_status", + "connections_tab" + ], + "fields": [ + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled", + "oldfieldname": "enabled", + "oldfieldtype": "Check", + "read_only": 1 + }, + { + "depends_on": "enabled", + "fieldname": "section_break_3", + "fieldtype": "Section Break", + "label": "Basic Info" + }, + { + "fieldname": "email", + "fieldtype": "Data", + "label": "Email", + "no_copy": 1, + "oldfieldname": "email", + "oldfieldtype": "Data", + "options": "Email", + "reqd": 1 + }, + { + "fieldname": "first_name", + "fieldtype": "Data", + "label": "First Name", + "oldfieldname": "first_name", + "oldfieldtype": "Data", + "reqd": 1 + }, + { + "fieldname": "middle_name", + "fieldtype": "Data", + "label": "Middle Name", + "oldfieldname": "middle_name", + "oldfieldtype": "Data" + }, + { + "bold": 1, + "fieldname": "last_name", + "fieldtype": "Data", + "label": "Last Name", + "oldfieldname": "last_name", + "oldfieldtype": "Data" + }, + { + "fieldname": "full_name", + "fieldtype": "Data", + "in_global_search": 1, + "in_standard_filter": 1, + "label": "Full Name", + "read_only": 1 + }, + { + "bold": 1, + "default": "1", + "depends_on": "eval:doc.__islocal", + "fieldname": "send_welcome_email", + "fieldtype": "Check", + "label": "Send Welcome Email" + }, + { + "default": "0", + "fieldname": "unsubscribed", + "fieldtype": "Check", + "hidden": 1, + "label": "Unsubscribed", + "no_copy": 1 + }, + { + "fieldname": "column_break0", + "fieldtype": "Column Break", + "oldfieldtype": "Column Break", + "print_width": "50%", + "width": "50%" + }, + { + "fieldname": "username", + "fieldtype": "Data", + "in_global_search": 1, + "in_standard_filter": 1, + "label": "Username", + "unique": 1 + }, + { + "fieldname": "language", + "fieldtype": "Link", + "label": "Language", + "options": "Language" + }, + { + "fieldname": "time_zone", + "fieldtype": "Autocomplete", + "label": "Time Zone" + }, + { + "description": "Get your globally recognized avatar from Gravatar.com", + "fieldname": "user_image", + "fieldtype": "Attach Image", + "hidden": 1, + "label": "User Image", + "no_copy": 1, + "print_hide": 1 + }, + { + "depends_on": "eval:in_list(['System User', 'Website User'], doc.user_type) && doc.enabled == 1", + "fieldname": "sb1", + "fieldtype": "Section Break", + "label": "Roles", + "permlevel": 1, + "read_only": 1 + }, + { + "allow_in_quick_entry": 1, + "fieldname": "role_profile_name", + "fieldtype": "Link", + "label": "Role Profile", + "options": "Role Profile", + "permlevel": 1 + }, + { + "fieldname": "roles_html", + "fieldtype": "HTML", + "label": "Roles HTML", + "read_only": 1 + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "hidden": 1, + "label": "Roles Assigned", + "options": "Has Role", + "permlevel": 1, + "print_hide": 1, + "read_only": 1 + }, + { + "collapsible": 1, + "depends_on": "enabled", + "fieldname": "short_bio", + "fieldtype": "Tab Break", + "label": "More Information" + }, + { + "fieldname": "gender", + "fieldtype": "Link", + "label": "Gender", + "oldfieldname": "gender", + "oldfieldtype": "Select", + "options": "Gender" + }, + { + "fieldname": "phone", + "fieldtype": "Data", + "label": "Phone", + "options": "Phone" + }, + { + "fieldname": "mobile_no", + "fieldtype": "Data", + "label": "Mobile No", + "options": "Phone", + "unique": 1 + }, + { + "fieldname": "birth_date", + "fieldtype": "Date", + "label": "Birth Date", + "no_copy": 1, + "oldfieldname": "birth_date", + "oldfieldtype": "Date" + }, + { + "fieldname": "location", + "fieldtype": "Data", + "label": "Location", + "no_copy": 1 + }, + { + "fieldname": "banner_image", + "fieldtype": "Attach Image", + "label": "Banner Image" + }, + { + "fieldname": "column_break_22", + "fieldtype": "Column Break" + }, + { + "fieldname": "interest", + "fieldtype": "Small Text", + "label": "Interests" + }, + { + "fieldname": "bio", + "fieldtype": "Small Text", + "label": "Bio", + "no_copy": 1 + }, + { + "default": "0", + "fieldname": "mute_sounds", + "fieldtype": "Check", + "label": "Mute Sounds" + }, + { + "collapsible": 1, + "depends_on": "eval:doc.enabled && (!doc.__islocal || !cint(doc.send_welcome_email))", + "fieldname": "change_password", + "fieldtype": "Section Break", + "label": "Change Password" + }, + { + "fieldname": "new_password", + "fieldtype": "Password", + "label": "Set New Password", + "no_copy": 1 + }, + { + "default": "1", + "fieldname": "logout_all_sessions", + "fieldtype": "Check", + "label": "Logout From All Devices After Changing Password" + }, + { + "fieldname": "reset_password_key", + "fieldtype": "Data", + "hidden": 1, + "label": "Reset Password Key", + "no_copy": 1, + "permlevel": 1, + "print_hide": 1, + "read_only": 1 + }, + { + "fieldname": "last_password_reset_date", + "fieldtype": "Date", + "hidden": 1, + "label": "Last Password Reset Date", + "no_copy": 1, + "print_hide": 1, + "read_only": 1 + }, + { + "fieldname": "redirect_url", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Redirect URL" + }, + { + "collapsible": 1, + "fieldname": "document_follow_notifications_section", + "fieldtype": "Section Break", + "label": "Document Follow" + }, + { + "default": "0", + "fieldname": "document_follow_notify", + "fieldtype": "Check", + "label": "Send Notifications For Documents Followed By Me" + }, + { + "default": "Daily", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "document_follow_frequency", + "fieldtype": "Select", + "label": "Frequency", + "options": "Hourly\nDaily\nWeekly" + }, + { + "collapsible": 1, + "depends_on": "enabled", + "fieldname": "email_settings", + "fieldtype": "Section Break", + "label": "Email" + }, + { + "default": "1", + "fieldname": "thread_notify", + "fieldtype": "Check", + "label": "Send Notifications For Email Threads" + }, + { + "default": "0", + "fieldname": "send_me_a_copy", + "fieldtype": "Check", + "label": "Send Me A Copy of Outgoing Emails" + }, + { + "default": "1", + "fieldname": "allowed_in_mentions", + "fieldtype": "Check", + "label": "Allowed In Mentions" + }, + { + "fieldname": "email_signature", + "fieldtype": "Small Text", + "label": "Email Signature", + "no_copy": 1 + }, + { + "fieldname": "user_emails", + "fieldtype": "Table", + "label": "User Emails", + "options": "User Email", + "permlevel": 1 + }, + { + "depends_on": "eval:in_list(['System User'], doc.user_type)", + "fieldname": "sb_allow_modules", + "fieldtype": "Section Break", + "label": "Allow Modules", + "permlevel": 1 + }, + { + "fieldname": "modules_html", + "fieldtype": "HTML", + "label": "Modules HTML", + "permlevel": 1 + }, + { + "fieldname": "block_modules", + "fieldtype": "Table", + "hidden": 1, + "label": "Block Modules", + "options": "Block Module", + "permlevel": 1 + }, + { + "fieldname": "home_settings", + "fieldtype": "Code", + "hidden": 1, + "label": "Home Settings" + }, + { + "description": "These values will be automatically updated in transactions and also will be useful to restrict permissions for this user on transactions containing these values.", + "fieldname": "sb2", + "fieldtype": "Section Break", + "hidden": 1, + "label": "Defaults", + "oldfieldtype": "Column Break", + "permlevel": 1, + "print_width": "50%", + "read_only": 1, + "width": "50%" + }, + { + "description": "Enter default value fields (keys) and values. If you add multiple values for a field, the first one will be picked. These defaults are also used to set \"match\" permission rules. To see list of fields, go to \"Customize Form\".", + "fieldname": "defaults", + "fieldtype": "Table", + "hidden": 1, + "label": "User Defaults", + "no_copy": 1, + "options": "DefaultValue" + }, + { + "collapsible": 1, + "depends_on": "enabled", + "fieldname": "sb3", + "fieldtype": "Section Break", + "label": "Security Settings", + "oldfieldtype": "Section Break", + "read_only": 1 + }, + { + "default": "2", + "fieldname": "simultaneous_sessions", + "fieldtype": "Int", + "label": "Simultaneous Sessions" + }, + { + "bold": 1, + "default": "System User", + "description": "If the user has any role checked, then the user becomes a \"System User\". \"System User\" has access to the desktop", + "fieldname": "user_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "User Type", + "oldfieldname": "user_type", + "oldfieldtype": "Select", + "options": "User Type", + "permlevel": 1 + }, + { + "description": "Allow user to login only after this hour (0-24)", + "fieldname": "login_after", + "fieldtype": "Int", + "label": "Login After", + "permlevel": 1 + }, + { + "description": "Allow user to login only before this hour (0-24)", + "fieldname": "login_before", + "fieldtype": "Int", + "label": "Login Before", + "permlevel": 1 + }, + { + "description": "Restrict user from this IP address only. Multiple IP addresses can be added by separating with commas. Also accepts partial IP addresses like (111.111.111)", + "fieldname": "restrict_ip", + "fieldtype": "Small Text", + "label": "Restrict IP", + "permlevel": 1 + }, + { + "default": "0", + "depends_on": "eval:doc.restrict_ip && doc.restrict_ip.length", + "description": "If enabled, user can login from any IP Address using Two Factor Auth, this can also be set for all users in System Settings", + "fieldname": "bypass_restrict_ip_check_if_2fa_enabled", + "fieldtype": "Check", + "label": "Bypass Restricted IP Address Check If Two Factor Auth Enabled" + }, + { + "fieldname": "column_break1", + "fieldtype": "Column Break", + "oldfieldtype": "Column Break", + "print_width": "50%", + "width": "50%" + }, + { + "fieldname": "last_login", + "fieldtype": "Read Only", + "label": "Last Login", + "no_copy": 1, + "oldfieldname": "last_login", + "oldfieldtype": "Read Only", + "read_only": 1 + }, + { + "fieldname": "last_ip", + "fieldtype": "Read Only", + "label": "Last IP", + "no_copy": 1, + "oldfieldname": "last_ip", + "oldfieldtype": "Read Only", + "read_only": 1 + }, + { + "fieldname": "last_active", + "fieldtype": "Datetime", + "label": "Last Active", + "no_copy": 1, + "read_only": 1 + }, + { + "description": "Stores the JSON of last known versions of various installed apps. It is used to show release notes.", + "fieldname": "last_known_versions", + "fieldtype": "Text", + "hidden": 1, + "label": "Last Known Versions", + "read_only": 1 + }, + { + "collapsible": 1, + "depends_on": "enabled", + "fieldname": "third_party_authentication", + "fieldtype": "Section Break", + "label": "Third Party Authentication", + "permlevel": 1 + }, + { + "fieldname": "social_logins", + "fieldtype": "Table", + "label": "Social Logins", + "options": "User Social Login" + }, + { + "collapsible": 1, + "fieldname": "api_access", + "fieldtype": "Section Break", + "label": "API Access" + }, + { + "description": "API Key cannot be regenerated", + "fieldname": "api_key", + "fieldtype": "Data", + "label": "API Key", + "permlevel": 1, + "read_only": 1, + "unique": 1 + }, + { + "fieldname": "generate_keys", + "fieldtype": "Button", + "label": "Generate Keys", + "permlevel": 1 + }, + { + "fieldname": "column_break_65", + "fieldtype": "Column Break" + }, + { + "fieldname": "api_secret", + "fieldtype": "Password", + "label": "API Secret", + "permlevel": 1, + "read_only": 1 + }, + { + "fieldname": "column_break_11", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_26", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_63", + "fieldtype": "Column Break" + }, + { + "fieldname": "desk_theme", + "fieldtype": "Select", + "label": "Desk Theme", + "options": "Light\nDark\nAutomatic" + }, + { + "fieldname": "module_profile", + "fieldtype": "Link", + "label": "Module Profile", + "options": "Module Profile" + }, + { + "description": "Stores the datetime when the last reset password key was generated.", + "fieldname": "last_reset_password_key_generated_on", + "fieldtype": "Datetime", + "hidden": 1, + "label": "Last Reset Password Key Generated On", + "permlevel": 1, + "read_only": 1 + }, + { + "fieldname": "column_break_75", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "follow_created_documents", + "fieldtype": "Check", + "label": "Auto follow documents that you create" + }, + { + "default": "0", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "follow_commented_documents", + "fieldtype": "Check", + "label": "Auto follow documents that you comment on" + }, + { + "default": "0", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "follow_liked_documents", + "fieldtype": "Check", + "label": "Auto follow documents that you Like" + }, + { + "default": "0", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "follow_shared_documents", + "fieldtype": "Check", + "label": "Auto follow documents that are shared with you" + }, + { + "default": "0", + "depends_on": "eval:(doc.document_follow_notify== 1)", + "fieldname": "follow_assigned_documents", + "fieldtype": "Check", + "label": "Auto follow documents that are assigned to you" + }, + { + "fieldname": "user_details_tab", + "fieldtype": "Tab Break", + "label": "User Details" + }, + { + "fieldname": "roles_permissions_tab", + "fieldtype": "Tab Break", + "label": "Roles & Permissions" + }, + { + "fieldname": "settings_tab", + "fieldtype": "Tab Break", + "label": "Settings" + }, + { + "fieldname": "connections_tab", + "fieldtype": "Tab Break", + "label": "Connections", + "show_dashboard": 1 + }, + { + "collapsible": 1, + "fieldname": "desk_settings_section", + "fieldtype": "Section Break", + "label": "Desk Settings" + }, + { + "default": "{}", + "fieldname": "onboarding_status", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Onboarding Status" + } + ], + "icon": "fa fa-user", + "idx": 413, + "image_field": "user_image", + "links": [ + { + "group": "Profile", + "link_doctype": "Contact", + "link_fieldname": "user" + }, + { + "group": "Profile", + "link_doctype": "Blogger", + "link_fieldname": "user" + }, + { + "group": "Logs", + "link_doctype": "Access Log", + "link_fieldname": "user" + }, + { + "group": "Logs", + "link_doctype": "Activity Log", + "link_fieldname": "user" + }, + { + "group": "Logs", + "link_doctype": "Energy Point Log", + "link_fieldname": "user" + }, + { + "group": "Logs", + "link_doctype": "Route History", + "link_fieldname": "user" + }, + { + "group": "Settings", + "link_doctype": "User Permission", + "link_fieldname": "user" + }, + { + "group": "Settings", + "link_doctype": "Document Follow", + "link_fieldname": "user" + }, + { + "group": "Activity", + "link_doctype": "Communication", + "link_fieldname": "user" + }, + { + "group": "Activity", + "link_doctype": "ToDo", + "link_fieldname": "allocated_to" + }, + { + "group": "Integrations", + "link_doctype": "Token Cache", + "link_fieldname": "user" + } + ], + "modified": "2024-02-11 13:16:29.574666", + "modified_by": "Administrator", + "module": "Core", + "name": "User", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "permlevel": 1, + "read": 1, + "role": "System Manager", + "write": 1 + }, + { + "role": "All", + "select": 1 + } + ], + "quick_entry": 1, + "route": "user", + "search_fields": "full_name", + "show_name_in_global_search": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "full_name", + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/user/user.py b/xhiveframework/core/doctype/user/user.py new file mode 100644 index 0000000..468d968 --- /dev/null +++ b/xhiveframework/core/doctype/user/user.py @@ -0,0 +1,1352 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from collections.abc import Iterable +from datetime import timedelta + +import xhiveframework +import xhiveframework.defaults +import xhiveframework.permissions +import xhiveframework.share +from xhiveframework import STANDARD_USERS, _, msgprint, throw +from xhiveframework.auth import MAX_PASSWORD_SIZE +from xhiveframework.core.doctype.user_type.user_type import user_linked_with_permission_on_doctype +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + create_notification_settings, + toggle_notifications, +) +from xhiveframework.desk.notifications import clear_notifications +from xhiveframework.model.delete_doc import check_if_doc_is_linked +from xhiveframework.model.document import Document +from xhiveframework.query_builder import DocType +from xhiveframework.rate_limiter import rate_limit +from xhiveframework.utils import ( + cint, + escape_html, + flt, + format_datetime, + get_formatted_email, + get_system_timezone, + has_gravatar, + now_datetime, + today, +) +from xhiveframework.utils.data import sha256_hash +from xhiveframework.utils.deprecations import deprecated +from xhiveframework.utils.password import check_password, get_password_reset_limit +from xhiveframework.utils.password import update_password as _update_password +from xhiveframework.utils.user import get_system_managers +from xhiveframework.website.utils import is_signup_disabled + + +class User(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.block_module.block_module import BlockModule + from xhiveframework.core.doctype.defaultvalue.defaultvalue import DefaultValue + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.core.doctype.user_email.user_email import UserEmail + from xhiveframework.core.doctype.user_social_login.user_social_login import UserSocialLogin + from xhiveframework.types import DF + + allowed_in_mentions: DF.Check + api_key: DF.Data | None + api_secret: DF.Password | None + banner_image: DF.AttachImage | None + bio: DF.SmallText | None + birth_date: DF.Date | None + block_modules: DF.Table[BlockModule] + bypass_restrict_ip_check_if_2fa_enabled: DF.Check + defaults: DF.Table[DefaultValue] + desk_theme: DF.Literal["Light", "Dark", "Automatic"] + document_follow_frequency: DF.Literal["Hourly", "Daily", "Weekly"] + document_follow_notify: DF.Check + email: DF.Data + email_signature: DF.SmallText | None + enabled: DF.Check + first_name: DF.Data + follow_assigned_documents: DF.Check + follow_commented_documents: DF.Check + follow_created_documents: DF.Check + follow_liked_documents: DF.Check + follow_shared_documents: DF.Check + full_name: DF.Data | None + gender: DF.Link | None + home_settings: DF.Code | None + interest: DF.SmallText | None + language: DF.Link | None + last_active: DF.Datetime | None + last_ip: DF.ReadOnly | None + last_known_versions: DF.Text | None + last_login: DF.ReadOnly | None + last_name: DF.Data | None + last_password_reset_date: DF.Date | None + last_reset_password_key_generated_on: DF.Datetime | None + location: DF.Data | None + login_after: DF.Int + login_before: DF.Int + logout_all_sessions: DF.Check + middle_name: DF.Data | None + mobile_no: DF.Data | None + module_profile: DF.Link | None + mute_sounds: DF.Check + new_password: DF.Password | None + onboarding_status: DF.SmallText | None + phone: DF.Data | None + redirect_url: DF.SmallText | None + reset_password_key: DF.Data | None + restrict_ip: DF.SmallText | None + role_profile_name: DF.Link | None + roles: DF.Table[HasRole] + send_me_a_copy: DF.Check + send_welcome_email: DF.Check + simultaneous_sessions: DF.Int + social_logins: DF.Table[UserSocialLogin] + thread_notify: DF.Check + time_zone: DF.Autocomplete | None + unsubscribed: DF.Check + user_emails: DF.Table[UserEmail] + user_image: DF.AttachImage | None + user_type: DF.Link | None + username: DF.Data | None + # end: auto-generated types + __new_password = None + + def __setup__(self): + # because it is handled separately + self.flags.ignore_save_passwords = ["new_password"] + + def autoname(self): + """set name as Email Address""" + if self.get("is_admin") or self.get("is_guest"): + self.name = self.first_name + else: + self.email = self.email.strip().lower() + self.name = self.email + + def onload(self): + from xhiveframework.config import get_modules_from_all_apps + + self.set_onload("all_modules", sorted(m.get("module_name") for m in get_modules_from_all_apps())) + + def before_insert(self): + self.flags.in_insert = True + throttle_user_creation() + + def after_insert(self): + create_notification_settings(self.name) + xhiveframework.cache.delete_key("users_for_mentions") + xhiveframework.cache.delete_key("enabled_users") + + def validate(self): + # clear new password + self.__new_password = self.new_password + self.new_password = "" + + if not xhiveframework.flags.in_test: + self.password_strength_test() + + if self.name not in STANDARD_USERS: + self.validate_email_type(self.email) + self.validate_email_type(self.name) + self.add_system_manager_role() + self.populate_role_profile_roles() + self.check_roles_added() + self.set_system_user() + self.set_full_name() + self.check_enable_disable() + self.ensure_unique_roles() + self.remove_all_roles_for_guest() + self.validate_username() + self.remove_disabled_roles() + self.validate_user_email_inbox() + ask_pass_update() + self.validate_allowed_modules() + self.validate_user_image() + self.set_time_zone() + + if self.language == "Loading...": + self.language = None + + if (self.name not in ["Administrator", "Guest"]) and (not self.get_social_login_userid("xhiveframework")): + self.set_social_login_userid("xhiveframework", xhiveframework.generate_hash(length=39)) + + def populate_role_profile_roles(self): + if self.role_profile_name: + role_profile = xhiveframework.get_doc("Role Profile", self.role_profile_name) + self.set("roles", []) + self.append_roles(*[role.role for role in role_profile.roles]) + + @deprecated + def validate_roles(self): + self.populate_role_profile_roles() + + def validate_allowed_modules(self): + if self.module_profile: + module_profile = xhiveframework.get_doc("Module Profile", self.module_profile) + self.set("block_modules", []) + for d in module_profile.get("block_modules"): + self.append("block_modules", {"module": d.module}) + + def validate_user_image(self): + if self.user_image and len(self.user_image) > 2000: + xhiveframework.throw(_("Not a valid User Image.")) + + def on_update(self): + # clear new password + self.share_with_self() + clear_notifications(user=self.name) + xhiveframework.clear_cache(user=self.name) + now = xhiveframework.flags.in_test or xhiveframework.flags.in_install + self.send_password_notification(self.__new_password) + xhiveframework.enqueue( + "xhiveframework.core.doctype.user.user.create_contact", + user=self, + ignore_mandatory=True, + now=now, + enqueue_after_commit=True, + ) + + if self.name not in STANDARD_USERS and not self.user_image: + xhiveframework.enqueue( + "xhiveframework.core.doctype.user.user.update_gravatar", + name=self.name, + now=now, + enqueue_after_commit=True, + ) + + # Set user selected timezone + if self.time_zone: + xhiveframework.defaults.set_default("time_zone", self.time_zone, self.name) + + if self.has_value_changed("enabled"): + xhiveframework.cache.delete_key("users_for_mentions") + xhiveframework.cache.delete_key("enabled_users") + elif self.has_value_changed("allow_in_mentions") or self.has_value_changed("user_type"): + xhiveframework.cache.delete_key("users_for_mentions") + + def has_website_permission(self, ptype, user, verbose=False): + """Returns true if current user is the session user""" + return self.name == xhiveframework.session.user + + def set_full_name(self): + self.full_name = " ".join(filter(None, [self.first_name, self.last_name])) + + def check_enable_disable(self): + # do not allow disabling administrator/guest + if not cint(self.enabled) and self.name in STANDARD_USERS: + xhiveframework.throw(_("User {0} cannot be disabled").format(self.name)) + + if not cint(self.enabled): + self.a_system_manager_should_exist() + + # clear sessions if disabled + if not cint(self.enabled) and getattr(xhiveframework.local, "login_manager", None): + xhiveframework.local.login_manager.logout(user=self.name) + + # toggle notifications based on the user's status + toggle_notifications(self.name, enable=cint(self.enabled)) + + def add_system_manager_role(self): + if self.is_system_manager_disabled(): + return + + # if adding system manager, do nothing + if not cint(self.enabled) or ( + "System Manager" in [user_role.role for user_role in self.get("roles")] + ): + return + + if ( + self.name not in STANDARD_USERS + and self.user_type == "System User" + and not self.get_other_system_managers() + and cint(xhiveframework.db.get_single_value("System Settings", "setup_complete")) + ): + msgprint(_("Adding System Manager to this User as there must be atleast one System Manager")) + self.append("roles", {"doctype": "Has Role", "role": "System Manager"}) + + if self.name == "Administrator": + # Administrator should always have System Manager Role + self.extend( + "roles", + [ + {"doctype": "Has Role", "role": "System Manager"}, + {"doctype": "Has Role", "role": "Administrator"}, + ], + ) + + def is_system_manager_disabled(self): + return xhiveframework.db.get_value("Role", {"name": "System Manager"}, ["disabled"]) + + def email_new_password(self, new_password=None): + if new_password and not self.flags.in_insert: + _update_password(user=self.name, pwd=new_password, logout_all_sessions=self.logout_all_sessions) + + def set_system_user(self): + """For the standard users like admin and guest, the user type is fixed.""" + user_type_mapper = {"Administrator": "System User", "Guest": "Website User"} + + if self.user_type and not xhiveframework.get_cached_value("User Type", self.user_type, "is_standard"): + if user_type_mapper.get(self.name): + self.user_type = user_type_mapper.get(self.name) + else: + self.set_roles_and_modules_based_on_user_type() + else: + """Set as System User if any of the given roles has desk_access""" + self.user_type = "System User" if self.has_desk_access() else "Website User" + + def set_roles_and_modules_based_on_user_type(self): + user_type_doc = xhiveframework.get_cached_doc("User Type", self.user_type) + if user_type_doc.role: + self.roles = [] + + # Check whether User has linked with the 'Apply User Permission On' doctype or not + if user_linked_with_permission_on_doctype(user_type_doc, self.name): + self.append("roles", {"role": user_type_doc.role}) + + xhiveframework.msgprint( + _("Role has been set as per the user type {0}").format(self.user_type), alert=True + ) + + user_type_doc.update_modules_in_user(self) + + def has_desk_access(self): + """Return true if any of the set roles has desk access""" + if not self.roles: + return False + + role_table = DocType("Role") + return xhiveframework.db.count( + role_table, + ((role_table.desk_access == 1) & (role_table.name.isin([d.role for d in self.roles]))), + ) + + def share_with_self(self): + if self.name in STANDARD_USERS: + return + + xhiveframework.share.add_docshare( + self.doctype, self.name, self.name, write=1, share=1, flags={"ignore_share_permission": True} + ) + + def validate_share(self, docshare): + pass + # if docshare.user == self.name: + # if self.user_type=="System User": + # if docshare.share != 1: + # xhiveframework.throw(_("Sorry! User should have complete access to their own record.")) + # else: + # xhiveframework.throw(_("Sorry! Sharing with Website User is prohibited.")) + + def send_password_notification(self, new_password): + try: + if self.flags.in_insert: + if self.name not in STANDARD_USERS: + if new_password: + # new password given, no email required + _update_password( + user=self.name, pwd=new_password, logout_all_sessions=self.logout_all_sessions + ) + + if not self.flags.no_welcome_mail and cint(self.send_welcome_email): + self.send_welcome_mail_to_user() + self.flags.email_sent = 1 + if xhiveframework.session.user != "Guest": + msgprint(_("Welcome email sent")) + return + else: + self.email_new_password(new_password) + + except xhiveframework.OutgoingEmailError: + xhiveframework.clear_last_message() + xhiveframework.msgprint( + _("Please setup default outgoing Email Account from Settings > Email Account"), alert=True + ) + # email server not set, don't send email + self.log_error("Unable to send new password notification") + + @Document.hook + def validate_reset_password(self): + pass + + def reset_password(self, send_email=False, password_expired=False): + from xhiveframework.utils import get_url + + key = xhiveframework.generate_hash() + hashed_key = sha256_hash(key) + self.db_set("reset_password_key", hashed_key) + self.db_set("last_reset_password_key_generated_on", now_datetime()) + + url = "/update-password?key=" + key + if password_expired: + url = "/update-password?key=" + key + "&password_expired=true" + + link = get_url(url) + if send_email: + self.password_reset_mail(link) + + return link + + def get_other_system_managers(self): + user_doctype = DocType("User").as_("user") + user_role_doctype = DocType("Has Role").as_("user_role") + return ( + xhiveframework.qb.from_(user_doctype) + .from_(user_role_doctype) + .select(user_doctype.name) + .where(user_role_doctype.role == "System Manager") + .where(user_doctype.enabled == 1) + .where(user_role_doctype.parent == user_doctype.name) + .where(user_role_doctype.parent.notin(["Administrator", self.name])) + .limit(1) + ).run() + + def get_fullname(self): + """get first_name space last_name""" + return (self.first_name or "") + (self.first_name and " " or "") + (self.last_name or "") + + def password_reset_mail(self, link): + reset_password_template = xhiveframework.db.get_system_setting("reset_password_template") + + self.send_login_mail( + _("Password Reset"), + "password_reset", + {"link": link}, + now=True, + custom_template=reset_password_template, + ) + + def send_welcome_mail_to_user(self): + from xhiveframework.utils import get_url + + link = self.reset_password() + subject = None + method = xhiveframework.get_hooks("welcome_email") + if method: + subject = xhiveframework.get_attr(method[-1])() + if not subject: + site_name = xhiveframework.db.get_default("site_name") or xhiveframework.get_conf().get("site_name") + if site_name: + subject = _("Welcome to {0}").format(site_name) + else: + subject = _("Complete Registration") + + welcome_email_template = xhiveframework.db.get_system_setting("welcome_email_template") + + self.send_login_mail( + subject, + "new_user", + dict( + link=link, + site_url=get_url(), + ), + custom_template=welcome_email_template, + ) + + def send_login_mail(self, subject, template, add_args, now=None, custom_template=None): + """send mail with login details""" + from xhiveframework.utils import get_url + from xhiveframework.utils.user import get_user_fullname + + created_by = get_user_fullname(xhiveframework.session["user"]) + if created_by == "Guest": + created_by = "Administrator" + + args = { + "first_name": self.first_name or self.last_name or "user", + "user": self.name, + "title": subject, + "login_url": get_url(), + "created_by": created_by, + } + + args.update(add_args) + + sender = ( + xhiveframework.session.user not in STANDARD_USERS and get_formatted_email(xhiveframework.session.user) or None + ) + + if custom_template: + from xhiveframework.email.doctype.email_template.email_template import get_email_template + + email_template = get_email_template(custom_template, args) + subject = email_template.get("subject") + content = email_template.get("message") + + xhiveframework.sendmail( + recipients=self.email, + sender=sender, + subject=subject, + template=template if not custom_template else None, + content=content if custom_template else None, + args=args, + header=[subject, "green"], + delayed=(not now) if now is not None else self.flags.delay_emails, + retry=3, + ) + + def a_system_manager_should_exist(self): + if self.is_system_manager_disabled(): + return + + if not self.get_other_system_managers(): + throw(_("There should remain at least one System Manager")) + + def on_trash(self): + xhiveframework.clear_cache(user=self.name) + if self.name in STANDARD_USERS: + throw(_("User {0} cannot be deleted").format(self.name)) + + self.a_system_manager_should_exist() + + # disable the user and log him/her out + self.enabled = 0 + if getattr(xhiveframework.local, "login_manager", None): + xhiveframework.local.login_manager.logout(user=self.name) + + # delete todos + xhiveframework.db.delete("ToDo", {"allocated_to": self.name}) + todo_table = DocType("ToDo") + ( + xhiveframework.qb.update(todo_table) + .set(todo_table.assigned_by, None) + .where(todo_table.assigned_by == self.name) + ).run() + + # delete events + xhiveframework.db.delete("Event", {"owner": self.name, "event_type": "Private"}) + + # delete shares + xhiveframework.db.delete("DocShare", {"user": self.name}) + # delete messages + table = DocType("Communication") + xhiveframework.db.delete( + table, + filters=( + (table.communication_type.isin(["Chat", "Notification"])) + & (table.reference_doctype == "User") + & ((table.reference_name == self.name) | table.owner == self.name) + ), + run=False, + ) + # unlink contact + table = DocType("Contact") + xhiveframework.qb.update(table).where(table.user == self.name).set(table.user, None).run() + + # delete notification settings + xhiveframework.delete_doc("Notification Settings", self.name, ignore_permissions=True) + + if self.get("allow_in_mentions"): + xhiveframework.cache.delete_key("users_for_mentions") + + xhiveframework.cache.delete_key("enabled_users") + + # delete user permissions + xhiveframework.db.delete("User Permission", {"user": self.name}) + + # Delete OAuth data + xhiveframework.db.delete("OAuth Authorization Code", {"user": self.name}) + xhiveframework.db.delete("Token Cache", {"user": self.name}) + + # Delete EPS data + xhiveframework.db.delete("Energy Point Log", {"user": self.name}) + + # Ask user to disable instead if document is still linked + try: + check_if_doc_is_linked(self) + except xhiveframework.LinkExistsError: + xhiveframework.throw(_("You can disable the user instead of deleting it."), xhiveframework.LinkExistsError) + + def before_rename(self, old_name, new_name, merge=False): + xhiveframework.clear_cache(user=old_name) + self.validate_rename(old_name, new_name) + + def validate_rename(self, old_name, new_name): + # do not allow renaming administrator and guest + if old_name in STANDARD_USERS: + throw(_("User {0} cannot be renamed").format(self.name)) + + self.validate_email_type(new_name) + + def validate_email_type(self, email): + from xhiveframework.utils import validate_email_address + + validate_email_address(email.strip(), True) + + def after_rename(self, old_name, new_name, merge=False): + tables = xhiveframework.db.get_tables() + for tab in tables: + desc = xhiveframework.db.get_table_columns_description(tab) + has_fields = [d.get("name") for d in desc if d.get("name") in ["owner", "modified_by"]] + for field in has_fields: + xhiveframework.db.sql( + """UPDATE `{}` + SET `{}` = {} + WHERE `{}` = {}""".format(tab, field, "%s", field, "%s"), + (new_name, old_name), + ) + + if xhiveframework.db.exists("Notification Settings", old_name): + xhiveframework.rename_doc("Notification Settings", old_name, new_name, force=True, show_alert=False) + + # set email + xhiveframework.db.set_value("User", new_name, "email", new_name) + + def append_roles(self, *roles): + """Add roles to user""" + current_roles = [d.role for d in self.get("roles")] + for role in roles: + if role in current_roles: + continue + self.append("roles", {"role": role}) + + def add_roles(self, *roles): + """Add roles to user and save""" + self.append_roles(*roles) + self.save() + + def remove_roles(self, *roles): + existing_roles = {d.role: d for d in self.get("roles")} + for role in roles: + if role in existing_roles: + self.get("roles").remove(existing_roles[role]) + + self.save() + + def remove_all_roles_for_guest(self): + if self.name == "Guest": + self.set("roles", list({d for d in self.get("roles") if d.role == "Guest"})) + + def remove_disabled_roles(self): + disabled_roles = [d.name for d in xhiveframework.get_all("Role", filters={"disabled": 1})] + for role in list(self.get("roles")): + if role.role in disabled_roles: + self.get("roles").remove(role) + + def ensure_unique_roles(self): + exists = [] + for d in self.get("roles"): + if (not d.role) or (d.role in exists): + self.get("roles").remove(d) + else: + exists.append(d.role) + + def validate_username(self): + if not self.username and self.is_new() and self.first_name: + self.username = xhiveframework.scrub(self.first_name) + + if not self.username: + return + + # strip space and @ + self.username = self.username.strip(" @") + + if self.username_exists(): + if self.user_type == "System User": + xhiveframework.msgprint(_("Username {0} already exists").format(self.username)) + self.suggest_username() + + self.username = "" + + def password_strength_test(self): + """test password strength""" + if self.flags.ignore_password_policy: + return + + if self.__new_password: + user_data = (self.first_name, self.middle_name, self.last_name, self.email, self.birth_date) + result = test_password_strength(self.__new_password, user_data=user_data) + feedback = result.get("feedback", None) + + if feedback and not feedback.get("password_policy_validation_passed", False): + handle_password_test_fail(feedback) + + def suggest_username(self): + def _check_suggestion(suggestion): + if self.username != suggestion and not self.username_exists(suggestion): + return suggestion + + return None + + # @firstname + username = _check_suggestion(xhiveframework.scrub(self.first_name)) + + if not username: + # @firstname_last_name + username = _check_suggestion(xhiveframework.scrub("{} {}".format(self.first_name, self.last_name or ""))) + + if username: + xhiveframework.msgprint(_("Suggested Username: {0}").format(username)) + + return username + + def username_exists(self, username=None): + return xhiveframework.db.get_value("User", {"username": username or self.username, "name": ("!=", self.name)}) + + def get_blocked_modules(self): + """Returns list of modules blocked for that user""" + return [d.module for d in self.block_modules] if self.block_modules else [] + + def validate_user_email_inbox(self): + """check if same email account added in User Emails twice""" + + email_accounts = [user_email.email_account for user_email in self.user_emails] + if len(email_accounts) != len(set(email_accounts)): + xhiveframework.throw(_("Email Account added multiple times")) + + def get_social_login_userid(self, provider: str): + try: + for p in self.social_logins: + if p.provider == provider: + return p.userid + except Exception: + return None + + def set_social_login_userid(self, provider, userid, username=None): + social_logins = {"provider": provider, "userid": userid} + + if username: + social_logins["username"] = username + + self.append("social_logins", social_logins) + + def get_restricted_ip_list(self): + return get_restricted_ip_list(self) + + @classmethod + def find_by_credentials(cls, user_name: str, password: str, validate_password: bool = True): + """Find the user by credentials. + + This is a login utility that needs to check login related system settings while finding the user. + 1. Find user by email ID by default + 2. If allow_login_using_mobile_number is set, you can use mobile number while finding the user. + 3. If allow_login_using_user_name is set, you can use username while finding the user. + """ + + login_with_mobile = cint( + xhiveframework.db.get_single_value("System Settings", "allow_login_using_mobile_number") + ) + login_with_username = cint( + xhiveframework.db.get_single_value("System Settings", "allow_login_using_user_name") + ) + + or_filters = [{"name": user_name}] + if login_with_mobile: + or_filters.append({"mobile_no": user_name}) + if login_with_username: + or_filters.append({"username": user_name}) + + users = xhiveframework.get_all("User", fields=["name", "enabled"], or_filters=or_filters, limit=1) + if not users: + return + + user = users[0] + user["is_authenticated"] = True + if validate_password: + try: + check_password(user["name"], password, delete_tracker_cache=False) + except xhiveframework.AuthenticationError: + user["is_authenticated"] = False + + return user + + def set_time_zone(self): + if not self.time_zone: + self.time_zone = get_system_timezone() + + def check_roles_added(self): + if self.user_type != "System User" or self.roles or not self.is_new(): + return + + xhiveframework.msgprint( + _("Newly created user {0} has no roles enabled.").format(xhiveframework.bold(self.name)), + title=_("No Roles Specified"), + indicator="orange", + primary_action={ + "label": _("Add Roles"), + "client_action": "xhiveframework.set_route", + "args": ["Form", self.doctype, self.name], + }, + ) + + +@xhiveframework.whitelist() +def get_timezones(): + import pytz + + return {"timezones": pytz.all_timezones} + + +@xhiveframework.whitelist() +def get_all_roles(): + """return all roles""" + active_domains = xhiveframework.get_active_domains() + + roles = xhiveframework.get_all( + "Role", + filters={ + "name": ("not in", xhiveframework.permissions.AUTOMATIC_ROLES), + "disabled": 0, + }, + or_filters={"ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains)}, + order_by="name", + ) + + return sorted([role.get("name") for role in roles]) + + +@xhiveframework.whitelist() +def get_roles(arg=None): + """get roles for a user""" + return xhiveframework.get_roles(xhiveframework.form_dict["uid"]) + + +@xhiveframework.whitelist() +def get_perm_info(role): + """get permission info""" + from xhiveframework.permissions import get_all_perms + + return get_all_perms(role) + + +@xhiveframework.whitelist(allow_guest=True, methods=["POST"]) +def update_password( + new_password: str, logout_all_sessions: int = 0, key: str | None = None, old_password: str | None = None +): + """Update password for the current user. + + Args: + new_password (str): New password. + logout_all_sessions (int, optional): If set to 1, all other sessions will be logged out. Defaults to 0. + key (str, optional): Password reset key. Defaults to None. + old_password (str, optional): Old password. Defaults to None. + """ + + if len(new_password) > MAX_PASSWORD_SIZE: + xhiveframework.throw(_("Password size exceeded the maximum allowed size.")) + + result = test_password_strength(new_password) + feedback = result.get("feedback", None) + + if feedback and not feedback.get("password_policy_validation_passed", False): + handle_password_test_fail(feedback) + + res = _get_user_for_update_password(key, old_password) + if res.get("message"): + xhiveframework.local.response.http_status_code = 410 + return res["message"] + else: + user = res["user"] + + logout_all_sessions = cint(logout_all_sessions) or xhiveframework.db.get_single_value( + "System Settings", "logout_on_password_reset" + ) + _update_password(user, new_password, logout_all_sessions=cint(logout_all_sessions)) + + user_doc, redirect_url = reset_user_data(user) + + # get redirect url from cache + redirect_to = xhiveframework.cache.hget("redirect_after_login", user) + if redirect_to: + redirect_url = redirect_to + xhiveframework.cache.hdel("redirect_after_login", user) + + xhiveframework.local.login_manager.login_as(user) + + xhiveframework.db.set_value("User", user, "last_password_reset_date", today()) + xhiveframework.db.set_value("User", user, "reset_password_key", "") + + if user_doc.user_type == "System User": + return "/app" + else: + return redirect_url or "/" + + +@xhiveframework.whitelist(allow_guest=True) +def test_password_strength(new_password: str, key=None, old_password=None, user_data: tuple | None = None): + from xhiveframework.utils.deprecations import deprecation_warning + from xhiveframework.utils.password_strength import test_password_strength as _test_password_strength + + if key is not None or old_password is not None: + deprecation_warning( + "Arguments `key` and `old_password` are deprecated in function `test_password_strength`." + ) + + enable_password_policy = xhiveframework.get_system_settings("enable_password_policy") or 0 + + if not enable_password_policy: + return {} + + if not user_data: + user_data = xhiveframework.db.get_value( + "User", xhiveframework.session.user, ["first_name", "middle_name", "last_name", "email", "birth_date"] + ) + + if new_password: + result = _test_password_strength(new_password, user_inputs=user_data) + password_policy_validation_passed = False + minimum_password_score = cint(xhiveframework.get_system_settings("minimum_password_score")) or 0 + + # score should be greater than 0 and minimum_password_score + if result.get("score") and result.get("score") >= minimum_password_score: + password_policy_validation_passed = True + + result["feedback"]["password_policy_validation_passed"] = password_policy_validation_passed + result.pop("password", None) + return result + + +@xhiveframework.whitelist() +def has_email_account(email: str): + return xhiveframework.get_list("Email Account", filters={"email_id": email}) + + +@xhiveframework.whitelist(allow_guest=False) +def get_email_awaiting(user): + return xhiveframework.get_all( + "User Email", + fields=["email_account", "email_id"], + filters={"awaiting_password": 1, "parent": user, "used_oauth": 0}, + ) + + +def ask_pass_update(): + # update the sys defaults as to awaiting users + from xhiveframework.utils import set_default + + password_list = xhiveframework.get_all( + "User Email", filters={"awaiting_password": 1, "used_oauth": 0}, pluck="parent", distinct=True + ) + set_default("email_user_password", ",".join(password_list)) + + +def _get_user_for_update_password(key, old_password): + # verify old password + result = xhiveframework._dict() + if key: + hashed_key = sha256_hash(key) + user = xhiveframework.db.get_value( + "User", {"reset_password_key": hashed_key}, ["name", "last_reset_password_key_generated_on"] + ) + result.user, last_reset_password_key_generated_on = user or (None, None) + if result.user: + reset_password_link_expiry = cint( + xhiveframework.db.get_single_value("System Settings", "reset_password_link_expiry_duration") + ) + if ( + reset_password_link_expiry + and now_datetime() + > last_reset_password_key_generated_on + timedelta(seconds=reset_password_link_expiry) + ): + result.message = _("The reset password link has been expired") + else: + result.message = _("The reset password link has either been used before or is invalid") + elif old_password: + # verify old password + xhiveframework.local.login_manager.check_password(xhiveframework.session.user, old_password) + user = xhiveframework.session.user + result.user = user + return result + + +def reset_user_data(user): + user_doc = xhiveframework.get_doc("User", user) + redirect_url = user_doc.redirect_url + user_doc.reset_password_key = "" + user_doc.redirect_url = "" + user_doc.save(ignore_permissions=True) + + return user_doc, redirect_url + + +@xhiveframework.whitelist(methods=["POST"]) +def verify_password(password): + xhiveframework.local.login_manager.check_password(xhiveframework.session.user, password) + + +@xhiveframework.whitelist(allow_guest=True) +def sign_up(email: str, full_name: str, redirect_to: str) -> tuple[int, str]: + if is_signup_disabled(): + xhiveframework.throw(_("Sign Up is disabled"), title=_("Not Allowed")) + + user = xhiveframework.db.get("User", {"email": email}) + if user: + if user.enabled: + return 0, _("Already Registered") + else: + return 0, _("Registered but disabled") + else: + if xhiveframework.db.get_creation_count("User", 60) > 300: + xhiveframework.respond_as_web_page( + _("Temporarily Disabled"), + _( + "Too many users signed up recently, so the registration is disabled. Please try back in an hour" + ), + http_status_code=429, + ) + + from xhiveframework.utils import random_string + + user = xhiveframework.get_doc( + { + "doctype": "User", + "email": email, + "first_name": escape_html(full_name), + "enabled": 1, + "new_password": random_string(10), + "user_type": "Website User", + } + ) + user.flags.ignore_permissions = True + user.flags.ignore_password_policy = True + user.insert() + + # set default signup role as per Portal Settings + default_role = xhiveframework.db.get_single_value("Portal Settings", "default_role") + if default_role: + user.add_roles(default_role) + + if redirect_to: + xhiveframework.cache.hset("redirect_after_login", user.name, redirect_to) + + if user.flags.email_sent: + return 1, _("Please check your email for verification") + else: + return 2, _("Please ask your administrator to verify your sign-up") + + +@xhiveframework.whitelist(allow_guest=True, methods=["POST"]) +@rate_limit(limit=get_password_reset_limit, seconds=60 * 60) +def reset_password(user: str) -> str: + try: + user: User = xhiveframework.get_doc("User", user) + if user.name == "Administrator": + return "not allowed" + if not user.enabled: + return "disabled" + + user.validate_reset_password() + user.reset_password(send_email=True) + + return xhiveframework.msgprint( + msg=_("Password reset instructions have been sent to your email"), + title=_("Password Email Sent"), + ) + except xhiveframework.DoesNotExistError: + xhiveframework.local.response["http_status_code"] = 404 + xhiveframework.clear_messages() + return "not found" + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def user_query(doctype, txt, searchfield, start, page_len, filters): + from xhiveframework.desk.reportview import get_filters_cond, get_match_cond + + doctype = "User" + conditions = [] + + user_type_condition = "and user_type != 'Website User'" + if filters and filters.get("ignore_user_type") and xhiveframework.session.data.user_type == "System User": + user_type_condition = "" + filters and filters.pop("ignore_user_type", None) + + txt = f"%{txt}%" + return xhiveframework.db.sql( + """SELECT `name`, CONCAT_WS(' ', first_name, middle_name, last_name) + FROM `tabUser` + WHERE `enabled`=1 + {user_type_condition} + AND `docstatus` < 2 + AND `name` NOT IN ({standard_users}) + AND ({key} LIKE %(txt)s + OR CONCAT_WS(' ', first_name, middle_name, last_name) LIKE %(txt)s) + {fcond} {mcond} + ORDER BY + CASE WHEN `name` LIKE %(txt)s THEN 0 ELSE 1 END, + CASE WHEN concat_ws(' ', first_name, middle_name, last_name) LIKE %(txt)s + THEN 0 ELSE 1 END, + NAME asc + LIMIT %(page_len)s OFFSET %(start)s + """.format( + user_type_condition=user_type_condition, + standard_users=", ".join(xhiveframework.db.escape(u) for u in STANDARD_USERS), + key=searchfield, + fcond=get_filters_cond(doctype, filters, conditions), + mcond=get_match_cond(doctype), + ), + dict(start=start, page_len=page_len, txt=txt), + ) + + +def get_total_users(): + """Returns total no. of system users""" + return flt( + xhiveframework.db.sql( + """SELECT SUM(`simultaneous_sessions`) + FROM `tabUser` + WHERE `enabled` = 1 + AND `user_type` = 'System User' + AND `name` NOT IN ({})""".format(", ".join(["%s"] * len(STANDARD_USERS))), + STANDARD_USERS, + )[0][0] + ) + + +def get_system_users(exclude_users: Iterable[str] | str | None = None, limit: int | None = None): + _excluded_users = list(STANDARD_USERS) + if isinstance(exclude_users, str): + _excluded_users.append(exclude_users) + elif isinstance(exclude_users, Iterable): + _excluded_users.extend(exclude_users) + + return xhiveframework.get_all( + "User", + filters={ + "enabled": 1, + "user_type": ("!=", "Website User"), + "name": ("not in", _excluded_users), + }, + pluck="name", + limit=limit, + ) + + +def get_active_users(): + """Returns No. of system users who logged in, in the last 3 days""" + return xhiveframework.db.sql( + """select count(*) from `tabUser` + where enabled = 1 and user_type != 'Website User' + and name not in ({}) + and hour(timediff(now(), last_active)) < 72""".format(", ".join(["%s"] * len(STANDARD_USERS))), + STANDARD_USERS, + )[0][0] + + +def get_website_users(): + """Returns total no. of website users""" + return xhiveframework.db.count("User", filters={"enabled": True, "user_type": "Website User"}) + + +def get_active_website_users(): + """Returns No. of website users who logged in, in the last 3 days""" + return xhiveframework.db.sql( + """select count(*) from `tabUser` + where enabled = 1 and user_type = 'Website User' + and hour(timediff(now(), last_active)) < 72""" + )[0][0] + + +def get_permission_query_conditions(user): + if user == "Administrator": + return "" + else: + return """(`tabUser`.name not in ({standard_users}))""".format( + standard_users=", ".join(xhiveframework.db.escape(user) for user in STANDARD_USERS) + ) + + +def has_permission(doc, user): + if (user != "Administrator") and (doc.name in STANDARD_USERS): + # dont allow non Administrator user to view / edit Administrator user + return False + + +def notify_admin_access_to_system_manager(login_manager=None): + if ( + login_manager + and login_manager.user == "Administrator" + and xhiveframework.local.conf.notify_admin_access_to_system_manager + ): + site = '{0}'.format(xhiveframework.local.request.host_url) + date_and_time = "{}".format(format_datetime(now_datetime(), format_string="medium")) + ip_address = xhiveframework.local.request_ip + + access_message = _("Administrator accessed {0} on {1} via IP Address {2}.").format( + site, date_and_time, ip_address + ) + + xhiveframework.sendmail( + recipients=get_system_managers(), + subject=_("Administrator Logged In"), + template="administrator_logged_in", + args={"access_message": access_message}, + header=["Access Notification", "orange"], + ) + + +def handle_password_test_fail(feedback: dict): + # Backward compatibility + if "feedback" in feedback: + feedback = feedback["feedback"] + + suggestions = feedback.get("suggestions", []) + warning = feedback.get("warning", "") + + xhiveframework.throw(msg=" ".join([warning, *suggestions]), title=_("Invalid Password")) + + +def update_gravatar(name): + gravatar = has_gravatar(name) + if gravatar: + xhiveframework.db.set_value("User", name, "user_image", gravatar) + + +def throttle_user_creation(): + if xhiveframework.flags.in_import: + return + + if xhiveframework.db.get_creation_count("User", 60) > xhiveframework.local.conf.get("throttle_user_limit", 60): + xhiveframework.throw(_("Throttled")) + + +@xhiveframework.whitelist() +def get_role_profile(role_profile: str): + return xhiveframework.get_doc("Role Profile", {"role_profile": role_profile}).roles + + +@xhiveframework.whitelist() +def get_module_profile(module_profile: str): + module_profile = xhiveframework.get_doc("Module Profile", {"module_profile_name": module_profile}) + return module_profile.get("block_modules") + + +def create_contact(user, ignore_links=False, ignore_mandatory=False): + from xhiveframework.contacts.doctype.contact.contact import get_contact_name + + if user.name in ["Administrator", "Guest"]: + return + + contact_name = get_contact_name(user.email) + if not contact_name: + try: + contact = xhiveframework.get_doc( + { + "doctype": "Contact", + "first_name": user.first_name, + "last_name": user.last_name, + "user": user.name, + "gender": user.gender, + } + ) + + if user.email: + contact.add_email(user.email, is_primary=True) + + if user.phone: + contact.add_phone(user.phone, is_primary_phone=True) + + if user.mobile_no: + contact.add_phone(user.mobile_no, is_primary_mobile_no=True) + + contact.insert( + ignore_permissions=True, ignore_links=ignore_links, ignore_mandatory=ignore_mandatory + ) + except xhiveframework.DuplicateEntryError: + pass + else: + try: + contact = xhiveframework.get_doc("Contact", contact_name) + contact.first_name = user.first_name + contact.last_name = user.last_name + contact.gender = user.gender + + # Add mobile number if phone does not exists in contact + if user.phone and not any(new_contact.phone == user.phone for new_contact in contact.phone_nos): + # Set primary phone if there is no primary phone number + contact.add_phone( + user.phone, + is_primary_phone=not any( + new_contact.is_primary_phone == 1 for new_contact in contact.phone_nos + ), + ) + + # Add mobile number if mobile does not exists in contact + if user.mobile_no and not any( + new_contact.phone == user.mobile_no for new_contact in contact.phone_nos + ): + # Set primary mobile if there is no primary mobile number + contact.add_phone( + user.mobile_no, + is_primary_mobile_no=not any( + new_contact.is_primary_mobile_no == 1 for new_contact in contact.phone_nos + ), + ) + + contact.save(ignore_permissions=True) + except xhiveframework.TimestampMismatchError: + raise xhiveframework.RetryBackgroundJobError + + +def get_restricted_ip_list(user): + if not user.restrict_ip: + return + + return [i.strip() for i in user.restrict_ip.split(",")] + + +@xhiveframework.whitelist(methods=["POST"]) +def generate_keys(user: str): + """ + generate api key and api secret + + :param user: str + """ + xhiveframework.only_for("System Manager") + user_details: User = xhiveframework.get_doc("User", user) + api_secret = xhiveframework.generate_hash(length=15) + # if api key is not set generate api key + if not user_details.api_key: + api_key = xhiveframework.generate_hash(length=15) + user_details.api_key = api_key + user_details.api_secret = api_secret + user_details.save() + + return {"api_secret": api_secret} + + +@xhiveframework.whitelist() +def switch_theme(theme): + if theme in ["Dark", "Light", "Automatic"]: + xhiveframework.db.set_value("User", xhiveframework.session.user, "desk_theme", theme) + + +def get_enabled_users(): + def _get_enabled_users(): + enabled_users = xhiveframework.get_all("User", filters={"enabled": "1"}, pluck="name") + return enabled_users + + return xhiveframework.cache.get_value("enabled_users", _get_enabled_users) + + +@xhiveframework.whitelist(methods=["POST"]) +def impersonate(user: str, reason: str): + # Note: For now we only allow admins, we MIGHT allow system manager in future. + # All the impersonation code doesn't assume anything about user. + xhiveframework.only_for("Administrator") + + impersonator = xhiveframework.session.user + xhiveframework.get_doc( + { + "doctype": "Activity Log", + "user": user, + "status": "Success", + "subject": _("User {0} impersonated as {1}").format(impersonator, user), + "operation": "Impersonate", + } + ).insert(ignore_permissions=True, ignore_links=True) + + notification = xhiveframework.new_doc( + "Notification Log", + for_user=user, + from_user=xhiveframework.session.user, + subject=_("{0} just impersonated as you. They gave this reason: {1}").format(impersonator, reason), + ) + notification.set("type", "Alert") + notification.insert(ignore_permissions=True) + xhiveframework.local.login_manager.impersonate(user) diff --git a/xhiveframework/core/doctype/user/user_list.js b/xhiveframework/core/doctype/user/user_list.js new file mode 100644 index 0000000..59a3a64 --- /dev/null +++ b/xhiveframework/core/doctype/user/user_list.js @@ -0,0 +1,19 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.listview_settings["User"] = { + add_fields: ["enabled", "user_type", "user_image"], + filters: [["enabled", "=", 1]], + prepare_data: function (data) { + data["user_for_avatar"] = data["name"]; + }, + get_indicator: function (doc) { + if (doc.enabled) { + return [__("Active"), "green", "enabled,=,1"]; + } else { + return [__("Disabled"), "grey", "enabled,=,0"]; + } + }, +}; + +xhiveframework.help.youtube_id["User"] = "w"; diff --git a/xhiveframework/core/doctype/user_document_type/__init__.py b/xhiveframework/core/doctype/user_document_type/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_document_type/user_document_type.json b/xhiveframework/core/doctype/user_document_type/user_document_type.json new file mode 100644 index 0000000..69983a2 --- /dev/null +++ b/xhiveframework/core/doctype/user_document_type/user_document_type.json @@ -0,0 +1,109 @@ +{ + "actions": [], + "creation": "2021-01-13 01:51:40.158521", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "column_break_2", + "is_custom", + "permissions_section", + "read", + "write", + "create", + "column_break_8", + "submit", + "cancel", + "amend", + "delete" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "permissions_section", + "fieldtype": "Section Break", + "label": "Role Permissions" + }, + { + "default": "1", + "fieldname": "read", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Read" + }, + { + "default": "0", + "fieldname": "write", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Write" + }, + { + "default": "0", + "fieldname": "create", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Create" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fetch_from": "document_type.custom", + "fieldname": "is_custom", + "fieldtype": "Check", + "label": "Is Custom", + "read_only": 1 + }, + { + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "submit", + "fieldtype": "Check", + "label": "Submit" + }, + { + "default": "0", + "fieldname": "cancel", + "fieldtype": "Check", + "label": "Cancel" + }, + { + "default": "0", + "fieldname": "amend", + "fieldtype": "Check", + "label": "Amend" + }, + { + "default": "0", + "fieldname": "delete", + "fieldtype": "Check", + "label": "Delete" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-03-16 00:32:24.414313", + "modified_by": "Administrator", + "module": "Core", + "name": "User Document Type", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_document_type/user_document_type.py b/xhiveframework/core/doctype/user_document_type/user_document_type.py new file mode 100644 index 0000000..746991d --- /dev/null +++ b/xhiveframework/core/doctype/user_document_type/user_document_type.py @@ -0,0 +1,30 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class UserDocumentType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + amend: DF.Check + cancel: DF.Check + create: DF.Check + delete: DF.Check + document_type: DF.Link + is_custom: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + read: DF.Check + submit: DF.Check + write: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/user_email/__init__.py b/xhiveframework/core/doctype/user_email/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_email/user_email.json b/xhiveframework/core/doctype/user_email/user_email.json new file mode 100644 index 0000000..6e3f813 --- /dev/null +++ b/xhiveframework/core/doctype/user_email/user_email.json @@ -0,0 +1,73 @@ +{ + "actions": [], + "creation": "2016-03-30 10:04:25.828742", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "email_account", + "email_id", + "column_break_3", + "awaiting_password", + "used_oauth", + "enable_outgoing" + ], + "fields": [ + { + "fieldname": "email_account", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Email Account", + "options": "Email Account", + "reqd": 1 + }, + { + "fetch_from": "email_account.email_id", + "fieldname": "email_id", + "fieldtype": "Data", + "label": "Email ID", + "options": "Email", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fetch_from": "email_account.awaiting_password", + "fieldname": "awaiting_password", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Awaiting Password", + "read_only": 1 + }, + { + "default": "0", + "fetch_from": "email_account.enable_outgoing", + "fieldname": "enable_outgoing", + "fieldtype": "Check", + "label": "Enable Outgoing", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "used_oauth", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Used OAuth", + "read_only": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2022-06-03 14:25:46.944733", + "modified_by": "Administrator", + "module": "Core", + "name": "User Email", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_email/user_email.py b/xhiveframework/core/doctype/user_email/user_email.py new file mode 100644 index 0000000..8bfe1da --- /dev/null +++ b/xhiveframework/core/doctype/user_email/user_email.py @@ -0,0 +1,25 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class UserEmail(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + awaiting_password: DF.Check + email_account: DF.Link + email_id: DF.Data | None + enable_outgoing: DF.Check + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + used_oauth: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/user_group/__init__.py b/xhiveframework/core/doctype/user_group/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_group/test_user_group.py b/xhiveframework/core/doctype/user_group/test_user_group.py new file mode 100644 index 0000000..a7fe566 --- /dev/null +++ b/xhiveframework/core/doctype/user_group/test_user_group.py @@ -0,0 +1,8 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestUserGroup(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/user_group/user_group.js b/xhiveframework/core/doctype/user_group/user_group.js new file mode 100644 index 0000000..d27ce65 --- /dev/null +++ b/xhiveframework/core/doctype/user_group/user_group.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("User Group", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/user_group/user_group.json b/xhiveframework/core/doctype/user_group/user_group.json new file mode 100644 index 0000000..a67ceea --- /dev/null +++ b/xhiveframework/core/doctype/user_group/user_group.json @@ -0,0 +1,50 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2021-04-12 15:17:24.751710", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user_group_members" + ], + "fields": [ + { + "fieldname": "user_group_members", + "fieldtype": "Table MultiSelect", + "label": "User Group Members", + "options": "User Group Member", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 22:25:16.941457", + "modified_by": "Administrator", + "module": "Core", + "name": "User Group", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Desk User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_group/user_group.py b/xhiveframework/core/doctype/user_group/user_group.py new file mode 100644 index 0000000..ce696d0 --- /dev/null +++ b/xhiveframework/core/doctype/user_group/user_group.py @@ -0,0 +1,27 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework + +# import xhiveframework +from xhiveframework.model.document import Document + + +class UserGroup(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.user_group_member.user_group_member import UserGroupMember + from xhiveframework.types import DF + + user_group_members: DF.TableMultiSelect[UserGroupMember] + + # end: auto-generated types + def after_insert(self): + xhiveframework.cache.delete_key("user_groups") + + def on_trash(self): + xhiveframework.cache.delete_key("user_groups") diff --git a/xhiveframework/core/doctype/user_group_member/__init__.py b/xhiveframework/core/doctype/user_group_member/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_group_member/test_user_group_member.py b/xhiveframework/core/doctype/user_group_member/test_user_group_member.py new file mode 100644 index 0000000..4ec829f --- /dev/null +++ b/xhiveframework/core/doctype/user_group_member/test_user_group_member.py @@ -0,0 +1,8 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestUserGroupMember(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/core/doctype/user_group_member/user_group_member.js b/xhiveframework/core/doctype/user_group_member/user_group_member.js new file mode 100644 index 0000000..566f883 --- /dev/null +++ b/xhiveframework/core/doctype/user_group_member/user_group_member.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("User Group Member", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/core/doctype/user_group_member/user_group_member.json b/xhiveframework/core/doctype/user_group_member/user_group_member.json new file mode 100644 index 0000000..d2ff149 --- /dev/null +++ b/xhiveframework/core/doctype/user_group_member/user_group_member.json @@ -0,0 +1,32 @@ +{ + "actions": [], + "creation": "2021-04-12 15:16:29.279107", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-04-12 15:17:18.773046", + "modified_by": "Administrator", + "module": "Core", + "name": "User Group Member", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_group_member/user_group_member.py b/xhiveframework/core/doctype/user_group_member/user_group_member.py new file mode 100644 index 0000000..7ae61da --- /dev/null +++ b/xhiveframework/core/doctype/user_group_member/user_group_member.py @@ -0,0 +1,22 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class UserGroupMember(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + user: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/user_permission/__init__.py b/xhiveframework/core/doctype/user_permission/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_permission/test_user_permission.py b/xhiveframework/core/doctype/user_permission/test_user_permission.py new file mode 100644 index 0000000..38a05c0 --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/test_user_permission.py @@ -0,0 +1,332 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See LICENSE +import xhiveframework +from xhiveframework.core.doctype.doctype.test_doctype import new_doctype +from xhiveframework.core.doctype.user_permission.user_permission import ( + add_user_permissions, + remove_applicable, +) +from xhiveframework.permissions import add_permission, has_user_permission +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.website.doctype.blog_post.test_blog_post import make_test_blog + + +class TestUserPermission(XhiveFrameworkTestCase): + def setUp(self): + test_users = ( + "test_bulk_creation_update@example.com", + "test_user_perm1@example.com", + "nested_doc_user@example.com", + ) + xhiveframework.db.delete("User Permission", {"user": ("in", test_users)}) + xhiveframework.delete_doc_if_exists("DocType", "Person") + xhiveframework.db.sql_ddl("DROP TABLE IF EXISTS `tabPerson`") + xhiveframework.delete_doc_if_exists("DocType", "Doc A") + xhiveframework.db.sql_ddl("DROP TABLE IF EXISTS `tabDoc A`") + + def test_default_user_permission_validation(self): + user = create_user("test_default_permission@example.com") + param = get_params(user, "User", user.name, is_default=1) + add_user_permissions(param) + # create a duplicate entry with default + perm_user = create_user("test_user_perm@example.com") + param = get_params(user, "User", perm_user.name, is_default=1) + self.assertRaises(xhiveframework.ValidationError, add_user_permissions, param) + + def test_default_user_permission_corectness(self): + user = create_user("test_default_corectness_permission_1@example.com") + param = get_params(user, "User", user.name, is_default=1, hide_descendants=1) + add_user_permissions(param) + # create a duplicate entry with default + perm_user = create_user("test_default_corectness2@example.com") + test_blog = make_test_blog() + param = get_params(perm_user, "Blog Post", test_blog.name, is_default=1, hide_descendants=1) + add_user_permissions(param) + xhiveframework.db.delete("User Permission", filters={"for_value": test_blog.name}) + xhiveframework.delete_doc("Blog Post", test_blog.name) + + def test_default_user_permission(self): + xhiveframework.set_user("Administrator") + user = create_user("test_user_perm1@example.com", "Website Manager") + for category in ["general", "public"]: + if not xhiveframework.db.exists("Blog Category", category): + xhiveframework.get_doc({"doctype": "Blog Category", "title": category}).insert() + + param = get_params(user, "Blog Category", "general", is_default=1) + add_user_permissions(param) + + param = get_params(user, "Blog Category", "public") + add_user_permissions(param) + + xhiveframework.set_user("test_user_perm1@example.com") + doc = xhiveframework.new_doc("Blog Post") + + self.assertEqual(doc.blog_category, "general") + xhiveframework.set_user("Administrator") + + def test_apply_to_all(self): + """Create User permission for User having access to all applicable Doctypes""" + user = create_user("test_bulk_creation_update@example.com") + param = get_params(user, "User", user.name) + is_created = add_user_permissions(param) + self.assertEqual(is_created, 1) + + def test_for_apply_to_all_on_update_from_apply_all(self): + user = create_user("test_bulk_creation_update@example.com") + param = get_params(user, "User", user.name) + + # Initially create User Permission document with apply_to_all checked + is_created = add_user_permissions(param) + + self.assertEqual(is_created, 1) + is_created = add_user_permissions(param) + + # User Permission should not be changed + self.assertEqual(is_created, 0) + + def test_for_applicable_on_update_from_apply_to_all(self): + """Update User Permission from all to some applicable Doctypes""" + user = create_user("test_bulk_creation_update@example.com") + param = get_params(user, "User", user.name, applicable=["Comment", "Contact"]) + + # Initially create User Permission document with apply_to_all checked + is_created = add_user_permissions(get_params(user, "User", user.name)) + + self.assertEqual(is_created, 1) + + is_created = add_user_permissions(param) + xhiveframework.db.commit() + + removed_apply_to_all = xhiveframework.db.exists("User Permission", get_exists_param(user)) + is_created_applicable_first = xhiveframework.db.exists( + "User Permission", get_exists_param(user, applicable="Comment") + ) + is_created_applicable_second = xhiveframework.db.exists( + "User Permission", get_exists_param(user, applicable="Contact") + ) + + # Check that apply_to_all is removed + self.assertIsNone(removed_apply_to_all) + + # Check that User Permissions for applicable is created + self.assertIsNotNone(is_created_applicable_first) + self.assertIsNotNone(is_created_applicable_second) + self.assertEqual(is_created, 1) + + def test_for_apply_to_all_on_update_from_applicable(self): + """Update User Permission from some to all applicable Doctypes""" + user = create_user("test_bulk_creation_update@example.com") + param = get_params(user, "User", user.name) + + # create User permissions that with applicable + is_created = add_user_permissions( + get_params(user, "User", user.name, applicable=["Comment", "Contact"]) + ) + + self.assertEqual(is_created, 1) + + is_created = add_user_permissions(param) + is_created_apply_to_all = xhiveframework.db.exists("User Permission", get_exists_param(user)) + removed_applicable_first = xhiveframework.db.exists( + "User Permission", get_exists_param(user, applicable="Comment") + ) + removed_applicable_second = xhiveframework.db.exists( + "User Permission", get_exists_param(user, applicable="Contact") + ) + + # To check that a User permission with apply_to_all exists + self.assertIsNotNone(is_created_apply_to_all) + + # Check that all User Permission with applicable is removed + self.assertIsNone(removed_applicable_first) + self.assertIsNone(removed_applicable_second) + self.assertEqual(is_created, 1) + + def test_user_perm_for_nested_doctype(self): + """Test if descendants' visibility is controlled for a nested DocType.""" + from xhiveframework.core.doctype.doctype.test_doctype import new_doctype + + user = create_user("nested_doc_user@example.com", "Blogger") + if not xhiveframework.db.exists("DocType", "Person"): + doc = new_doctype( + "Person", + fields=[{"label": "Person Name", "fieldname": "person_name", "fieldtype": "Data"}], + unique=0, + ) + doc.is_tree = 1 + doc.insert() + + parent_record = xhiveframework.get_doc({"doctype": "Person", "person_name": "Parent", "is_group": 1}).insert() + + child_record = xhiveframework.get_doc( + { + "doctype": "Person", + "person_name": "Child", + "is_group": 0, + "parent_person": parent_record.name, + } + ).insert() + + add_user_permissions(get_params(user, "Person", parent_record.name)) + + # check if adding perm on a group record, makes child record visible + self.assertTrue(has_user_permission(xhiveframework.get_doc("Person", parent_record.name), user.name)) + self.assertTrue(has_user_permission(xhiveframework.get_doc("Person", child_record.name), user.name)) + + # give access of Parent DocType to Blogger role + add_permission("Person", "Blogger") + xhiveframework.set_user(user.name) + visible_names = xhiveframework.get_list( + doctype="Person", + pluck="person_name", + ) + + user_permission = xhiveframework.get_doc( + "User Permission", {"allow": "Person", "for_value": parent_record.name} + ) + user_permission.hide_descendants = 1 + user_permission.save(ignore_permissions=True) + + # check if adding perm on a group record with hide_descendants enabled, + # hides child records + self.assertTrue(has_user_permission(xhiveframework.get_doc("Person", parent_record.name), user.name)) + self.assertFalse(has_user_permission(xhiveframework.get_doc("Person", child_record.name), user.name)) + + visible_names_after_hide_descendants = xhiveframework.get_list( + "Person", + pluck="person_name", + ) + self.assertEqual(visible_names, ["Child", "Parent"]) + self.assertEqual(visible_names_after_hide_descendants, ["Parent"]) + xhiveframework.set_user("Administrator") + + def test_user_perm_on_new_doc_with_field_default(self): + """Test User Perm impact on xhiveframework.new_doc. with *field* default value""" + xhiveframework.set_user("Administrator") + user = create_user("new_doc_test@example.com", "Blogger") + + # make a doctype "Doc A" with 'doctype' link field and default value ToDo + if not xhiveframework.db.exists("DocType", "Doc A"): + doc = new_doctype( + "Doc A", + fields=[ + { + "label": "DocType", + "fieldname": "doc", + "fieldtype": "Link", + "options": "DocType", + "default": "ToDo", + } + ], + unique=0, + ) + doc.insert() + + # make User Perm on DocType 'ToDo' in Assignment Rule (unrelated doctype) + add_user_permissions(get_params(user, "DocType", "ToDo", applicable=["Assignment Rule"])) + xhiveframework.set_user("new_doc_test@example.com") + + new_doc = xhiveframework.new_doc("Doc A") + + # User perm is created on ToDo but for doctype Assignment Rule only + # it should not have impact on Doc A + self.assertEqual(new_doc.doc, "ToDo") + + xhiveframework.set_user("Administrator") + remove_applicable(["Assignment Rule"], "new_doc_test@example.com", "DocType", "ToDo") + + def test_user_perm_on_new_doc_with_user_default(self): + """Test User Perm impact on xhiveframework.new_doc. with *user* default value""" + from xhiveframework.core.doctype.session_default_settings.session_default_settings import ( + clear_session_defaults, + set_session_default_values, + ) + + xhiveframework.set_user("Administrator") + user = create_user("user_default_test@example.com", "Blogger") + + # make a doctype "Doc A" with 'doctype' link field + if not xhiveframework.db.exists("DocType", "Doc A"): + doc = new_doctype( + "Doc A", + fields=[ + { + "label": "DocType", + "fieldname": "doc", + "fieldtype": "Link", + "options": "DocType", + } + ], + unique=0, + ) + doc.insert() + + # create a 'DocType' session default field + if not xhiveframework.db.exists("Session Default", {"ref_doctype": "DocType"}): + settings = xhiveframework.get_single("Session Default Settings") + settings.append("session_defaults", {"ref_doctype": "DocType"}) + settings.save() + + # make User Perm on DocType 'ToDo' in Assignment Rule (unrelated doctype) + add_user_permissions(get_params(user, "DocType", "ToDo", applicable=["Assignment Rule"])) + + # User default Doctype value is ToDo via Session Defaults + xhiveframework.set_user("user_default_test@example.com") + set_session_default_values({"doc": "ToDo"}) + + new_doc = xhiveframework.new_doc("Doc A") + + # User perm is created on ToDo but for doctype Assignment Rule only + # it should not have impact on Doc A + self.assertEqual(new_doc.doc, "ToDo") + + xhiveframework.set_user("Administrator") + clear_session_defaults() + remove_applicable(["Assignment Rule"], "user_default_test@example.com", "DocType", "ToDo") + + +def create_user(email, *roles): + """create user with role system manager""" + if xhiveframework.db.exists("User", email): + return xhiveframework.get_doc("User", email) + + user = xhiveframework.new_doc("User") + user.email = email + user.first_name = email.split("@", 1)[0] + + if not roles: + roles = ("System Manager",) + + user.add_roles(*roles) + return user + + +def get_params(user, doctype, docname, is_default=0, hide_descendants=0, applicable=None): + """Return param to insert""" + param = { + "user": user.name, + "doctype": doctype, + "docname": docname, + "is_default": is_default, + "apply_to_all_doctypes": 1, + "applicable_doctypes": [], + "hide_descendants": hide_descendants, + } + if applicable: + param.update({"apply_to_all_doctypes": 0}) + param.update({"applicable_doctypes": applicable}) + return param + + +def get_exists_param(user, applicable=None): + """param to check existing Document""" + param = { + "user": user.name, + "allow": "User", + "for_value": user.name, + } + if applicable: + param.update({"applicable_for": applicable}) + else: + param.update({"apply_to_all_doctypes": 1}) + return param diff --git a/xhiveframework/core/doctype/user_permission/user_permission.js b/xhiveframework/core/doctype/user_permission/user_permission.js new file mode 100644 index 0000000..9a438de --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/user_permission.js @@ -0,0 +1,58 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("User Permission", { + setup: (frm) => { + frm.set_query("allow", () => { + return { + filters: { + issingle: 0, + istable: 0, + }, + }; + }); + + frm.set_query("applicable_for", () => { + return { + query: "xhiveframework.core.doctype.user_permission.user_permission.get_applicable_for_doctype_list", + doctype: frm.doc.allow, + }; + }); + }, + + refresh: (frm) => { + frm.add_custom_button(__("View Permitted Documents"), () => + xhiveframework.set_route("query-report", "Permitted Documents For User", { + user: frm.doc.user, + }) + ); + frm.trigger("set_applicable_for_constraint"); + frm.trigger("toggle_hide_descendants"); + }, + + allow: (frm) => { + if (frm.doc.allow) { + if (frm.doc.for_value) { + frm.set_value("for_value", null); + } + frm.trigger("toggle_hide_descendants"); + } + }, + + apply_to_all_doctypes: (frm) => { + frm.trigger("set_applicable_for_constraint"); + }, + + set_applicable_for_constraint: (frm) => { + frm.toggle_reqd("applicable_for", !frm.doc.apply_to_all_doctypes); + + if (frm.doc.apply_to_all_doctypes && frm.doc.applicable_for) { + frm.set_value("applicable_for", null, null, true); + } + }, + + toggle_hide_descendants: (frm) => { + let show = xhiveframework.boot.nested_set_doctypes.includes(frm.doc.allow); + frm.toggle_display("hide_descendants", show); + }, +}); diff --git a/xhiveframework/core/doctype/user_permission/user_permission.json b/xhiveframework/core/doctype/user_permission/user_permission.json new file mode 100644 index 0000000..60b6779 --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/user_permission.json @@ -0,0 +1,117 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2017-07-17 14:25:27.881871", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user", + "allow", + "for_value", + "column_break_3", + "is_default", + "advanced_control_section", + "apply_to_all_doctypes", + "applicable_for", + "column_break_9", + "hide_descendants" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "User", + "options": "User", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "allow", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Allow", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "for_value", + "fieldtype": "Dynamic Link", + "ignore_user_permissions": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "For Value", + "options": "allow", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "is_default", + "fieldtype": "Check", + "label": "Is Default" + }, + { + "fieldname": "advanced_control_section", + "fieldtype": "Section Break", + "label": "Advanced Control" + }, + { + "default": "1", + "fieldname": "apply_to_all_doctypes", + "fieldtype": "Check", + "label": "Apply To All Document Types" + }, + { + "depends_on": "eval:!doc.apply_to_all_doctypes", + "fieldname": "applicable_for", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Applicable For", + "options": "DocType" + }, + { + "fieldname": "column_break_9", + "fieldtype": "Column Break" + }, + { + "default": "0", + "description": "Hide descendant records of For Value.", + "fieldname": "hide_descendants", + "fieldtype": "Check", + "hidden": 1, + "label": "Hide Descendants" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + } + ], + "links": [], + "modified": "2022-01-03 11:25:03.726150", + "modified_by": "Administrator", + "module": "Core", + "name": "User Permission", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "user", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_permission/user_permission.py b/xhiveframework/core/doctype/user_permission/user_permission.py new file mode 100644 index 0000000..bdfde7b --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/user_permission.py @@ -0,0 +1,339 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.utils import find +from xhiveframework.desk.form.linked_with import get_linked_doctypes +from xhiveframework.model.document import Document +from xhiveframework.utils import cstr + + +class UserPermission(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow: DF.Link + applicable_for: DF.Link | None + apply_to_all_doctypes: DF.Check + for_value: DF.DynamicLink + hide_descendants: DF.Check + is_default: DF.Check + user: DF.Link + + # end: auto-generated types + def validate(self): + self.validate_user_permission() + self.validate_default_permission() + + def on_update(self): + xhiveframework.cache.hdel("user_permissions", self.user) + xhiveframework.publish_realtime("update_user_permissions", user=self.user, after_commit=True) + + def on_trash(self): + xhiveframework.cache.hdel("user_permissions", self.user) + xhiveframework.publish_realtime("update_user_permissions", user=self.user, after_commit=True) + + def validate_user_permission(self): + """checks for duplicate user permission records""" + + duplicate_exists = xhiveframework.get_all( + self.doctype, + filters={ + "allow": self.allow, + "for_value": self.for_value, + "user": self.user, + "applicable_for": cstr(self.applicable_for), + "apply_to_all_doctypes": self.apply_to_all_doctypes, + "name": ["!=", self.name], + }, + limit=1, + ) + if duplicate_exists: + xhiveframework.throw(_("User permission already exists"), xhiveframework.DuplicateEntryError) + + def validate_default_permission(self): + """validate user permission overlap for default value of a particular doctype""" + overlap_exists = [] + if self.is_default: + overlap_exists = xhiveframework.get_all( + self.doctype, + filters={"allow": self.allow, "user": self.user, "is_default": 1, "name": ["!=", self.name]}, + or_filters={ + "applicable_for": cstr(self.applicable_for), + "apply_to_all_doctypes": 1, + }, + limit=1, + ) + if overlap_exists: + ref_link = xhiveframework.get_desk_link(self.doctype, overlap_exists[0].name) + xhiveframework.throw(_("{0} has already assigned default value for {1}.").format(ref_link, self.allow)) + + +def send_user_permissions(bootinfo): + bootinfo.user["user_permissions"] = get_user_permissions() + + +@xhiveframework.whitelist() +def get_user_permissions(user=None): + """Get all users permissions for the user as a dict of doctype""" + # if this is called from client-side, + # user can access only his/her user permissions + if xhiveframework.request and xhiveframework.local.form_dict.cmd == "get_user_permissions": + user = xhiveframework.session.user + + if not user: + user = xhiveframework.session.user + + if not user or user in ("Administrator", "Guest"): + return {} + + cached_user_permissions = xhiveframework.cache.hget("user_permissions", user) + + if cached_user_permissions is not None: + return cached_user_permissions + + out = {} + + def add_doc_to_perm(perm, doc_name, is_default): + # group rules for each type + # for example if allow is "Customer", then build all allowed customers + # in a list + if not out.get(perm.allow): + out[perm.allow] = [] + + out[perm.allow].append( + xhiveframework._dict( + {"doc": doc_name, "applicable_for": perm.get("applicable_for"), "is_default": is_default} + ) + ) + + try: + for perm in xhiveframework.get_all( + "User Permission", + fields=["allow", "for_value", "applicable_for", "is_default", "hide_descendants"], + filters=dict(user=user), + ): + meta = xhiveframework.get_meta(perm.allow) + add_doc_to_perm(perm, perm.for_value, perm.is_default) + + if meta.is_nested_set() and not perm.hide_descendants: + decendants = xhiveframework.db.get_descendants(perm.allow, perm.for_value) + for doc in decendants: + add_doc_to_perm(perm, doc, False) + + out = xhiveframework._dict(out) + xhiveframework.cache.hset("user_permissions", user, out) + except xhiveframework.db.SQLError as e: + if xhiveframework.db.is_table_missing(e): + # called from patch + pass + + return out + + +def user_permission_exists(user, allow, for_value, applicable_for=None): + """Checks if similar user permission already exists""" + user_permissions = get_user_permissions(user).get(allow, []) + if not user_permissions: + return None + return find( + user_permissions, + lambda perm: perm["doc"] == for_value and perm.get("applicable_for") == applicable_for, + ) + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_applicable_for_doctype_list(doctype, txt, searchfield, start, page_len, filters): + linked_doctypes_map = get_linked_doctypes(doctype, True) + + linked_doctypes = [] + for linked_doctype, linked_doctype_values in linked_doctypes_map.items(): + linked_doctypes.append(linked_doctype) + child_doctype = linked_doctype_values.get("child_doctype") + if child_doctype: + linked_doctypes.append(child_doctype) + + linked_doctypes += [doctype] + + if txt: + linked_doctypes = [d for d in linked_doctypes if txt.lower() in d.lower()] + + linked_doctypes.sort() + + return [[doctype] for doctype in linked_doctypes[start:page_len]] + + +def get_permitted_documents(doctype): + """Returns permitted documents from the given doctype for the session user""" + # sort permissions in a way to make the first permission in the list to be default + user_perm_list = sorted( + get_user_permissions().get(doctype, []), key=lambda x: x.get("is_default"), reverse=True + ) + + return [d.get("doc") for d in user_perm_list if d.get("doc")] + + +@xhiveframework.whitelist() +def check_applicable_doc_perm(user, doctype, docname): + xhiveframework.only_for("System Manager") + applicable = [] + doc_exists = xhiveframework.get_all( + "User Permission", + fields=["name"], + filters={ + "user": user, + "allow": doctype, + "for_value": docname, + "apply_to_all_doctypes": 1, + }, + limit=1, + ) + if doc_exists: + applicable = get_linked_doctypes(doctype).keys() + else: + data = xhiveframework.get_all( + "User Permission", + fields=["applicable_for"], + filters={ + "user": user, + "allow": doctype, + "for_value": docname, + }, + ) + for permission in data: + applicable.append(permission.applicable_for) + return applicable + + +@xhiveframework.whitelist() +def clear_user_permissions(user, for_doctype): + xhiveframework.only_for("System Manager") + total = xhiveframework.db.count("User Permission", {"user": user, "allow": for_doctype}) + + if total: + xhiveframework.db.delete( + "User Permission", + { + "allow": for_doctype, + "user": user, + }, + ) + xhiveframework.clear_cache() + + return total + + +@xhiveframework.whitelist() +def add_user_permissions(data): + """Add and update the user permissions""" + xhiveframework.only_for("System Manager") + if isinstance(data, str): + data = json.loads(data) + data = xhiveframework._dict(data) + + # get all doctypes on whom this permission is applied + perm_applied_docs = check_applicable_doc_perm(data.user, data.doctype, data.docname) + exists = xhiveframework.db.exists( + "User Permission", + { + "user": data.user, + "allow": data.doctype, + "for_value": data.docname, + "apply_to_all_doctypes": 1, + }, + ) + if data.apply_to_all_doctypes == 1 and not exists: + remove_applicable(perm_applied_docs, data.user, data.doctype, data.docname) + insert_user_perm( + data.user, data.doctype, data.docname, data.is_default, data.hide_descendants, apply_to_all=1 + ) + return 1 + elif len(data.applicable_doctypes) > 0 and data.apply_to_all_doctypes != 1: + remove_apply_to_all(data.user, data.doctype, data.docname) + update_applicable(perm_applied_docs, data.applicable_doctypes, data.user, data.doctype, data.docname) + for applicable in data.applicable_doctypes: + if applicable not in perm_applied_docs: + insert_user_perm( + data.user, + data.doctype, + data.docname, + data.is_default, + data.hide_descendants, + applicable=applicable, + ) + elif exists: + insert_user_perm( + data.user, + data.doctype, + data.docname, + data.is_default, + data.hide_descendants, + applicable=applicable, + ) + return 1 + return 0 + + +def insert_user_perm( + user, doctype, docname, is_default=0, hide_descendants=0, apply_to_all=None, applicable=None +): + user_perm = xhiveframework.new_doc("User Permission") + user_perm.user = user + user_perm.allow = doctype + user_perm.for_value = docname + user_perm.is_default = is_default + user_perm.hide_descendants = hide_descendants + if applicable: + user_perm.applicable_for = applicable + user_perm.apply_to_all_doctypes = 0 + else: + user_perm.apply_to_all_doctypes = 1 + user_perm.insert() + + +def remove_applicable(perm_applied_docs, user, doctype, docname): + for applicable_for in perm_applied_docs: + xhiveframework.db.delete( + "User Permission", + { + "applicable_for": applicable_for, + "for_value": docname, + "allow": doctype, + "user": user, + }, + ) + + +def remove_apply_to_all(user, doctype, docname): + xhiveframework.db.delete( + "User Permission", + { + "apply_to_all_doctypes": 1, + "for_value": docname, + "allow": doctype, + "user": user, + }, + ) + + +def update_applicable(already_applied, to_apply, user, doctype, docname): + for applied in already_applied: + if applied not in to_apply: + xhiveframework.db.delete( + "User Permission", + { + "applicable_for": applied, + "for_value": docname, + "allow": doctype, + "user": user, + }, + ) diff --git a/xhiveframework/core/doctype/user_permission/user_permission_help.html b/xhiveframework/core/doctype/user_permission/user_permission_help.html new file mode 100644 index 0000000..d4b48fe --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/user_permission_help.html @@ -0,0 +1,8 @@ +
    +
    {%= __("Records for following doctypes will be filtered") %}
    +
      + {% Object.keys(linked_doctypes).forEach(key => { %} +
    • = 10 ? "col-md-4" : "" }}>{%= __(key) %}
    • + {% }) %} +
    +
    \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_permission/user_permission_list.js b/xhiveframework/core/doctype/user_permission/user_permission_list.js new file mode 100644 index 0000000..f057c10 --- /dev/null +++ b/xhiveframework/core/doctype/user_permission/user_permission_list.js @@ -0,0 +1,293 @@ +xhiveframework.listview_settings["User Permission"] = { + onload: function (list_view) { + var me = this; + list_view.page.add_inner_button(__("Add / Update"), function () { + let dialog = new xhiveframework.ui.Dialog({ + title: __("Add User Permissions"), + fields: [ + { + fieldname: "user", + label: __("For User"), + fieldtype: "Link", + options: "User", + reqd: 1, + onchange: function () { + dialog.fields_dict.doctype.set_input(undefined); + dialog.fields_dict.docname.set_input(undefined); + dialog.set_df_property("docname", "hidden", 1); + dialog.set_df_property("is_default", "hidden", 1); + dialog.set_df_property("apply_to_all_doctypes", "hidden", 1); + dialog.set_df_property("applicable_doctypes", "hidden", 1); + dialog.set_df_property("hide_descendants", "hidden", 1); + }, + }, + { + fieldname: "doctype", + label: __("Document Type"), + fieldtype: "Link", + options: "DocType", + reqd: 1, + onchange: function () { + me.on_doctype_change(dialog); + }, + }, + { + fieldname: "docname", + label: __("Document Name"), + fieldtype: "Dynamic Link", + options: "doctype", + hidden: 1, + onchange: function () { + let field = dialog.fields_dict["docname"]; + if (field.value != field.last_value) { + if ( + dialog.fields_dict.doctype.value && + dialog.fields_dict.docname.value && + dialog.fields_dict.user.value + ) { + me.get_applicable_doctype(dialog).then((applicable) => { + me.get_multi_select_options(dialog, applicable).then( + (options) => { + me.applicable_options = options; + me.on_docname_change(dialog, options, applicable); + if (options.length > 5) { + dialog.fields_dict.applicable_doctypes.setup_select_all(); + } + } + ); + }); + } + } + }, + }, + { + fieldtype: "Section Break", + hide_border: 1, + }, + { + fieldname: "is_default", + label: __("Is Default"), + fieldtype: "Check", + hidden: 1, + }, + { + fieldname: "apply_to_all_doctypes", + label: __("Apply to all Documents Types"), + fieldtype: "Check", + hidden: 1, + onchange: function () { + if ( + dialog.fields_dict.doctype.value && + dialog.fields_dict.docname.value && + dialog.fields_dict.user.value + ) { + me.on_apply_to_all_doctypes_change(dialog, me.applicable_options); + if (me.applicable_options.length > 5) { + dialog.fields_dict.applicable_doctypes.setup_select_all(); + } + } + }, + }, + { + fieldtype: "Column Break", + }, + { + fieldname: "hide_descendants", + label: __("Hide Descendants"), + fieldtype: "Check", + hidden: 1, + }, + { + fieldtype: "Section Break", + hide_border: 1, + }, + { + label: __("Applicable Document Types"), + fieldname: "applicable_doctypes", + fieldtype: "MultiCheck", + options: [], + columns: 2, + hidden: 1, + }, + ], + primary_action: (data) => { + data = me.validate(dialog, data); + xhiveframework.call({ + async: false, + method: "xhiveframework.core.doctype.user_permission.user_permission.add_user_permissions", + args: { + data: data, + }, + callback: function (r) { + if (r.message === 1) { + xhiveframework.show_alert({ + message: __("User Permissions created sucessfully"), + indicator: "blue", + }); + } else { + xhiveframework.show_alert({ + message: __("Nothing to update"), + indicator: "red", + }); + } + }, + }); + dialog.hide(); + list_view.refresh(); + }, + primary_action_label: __("Submit"), + }); + dialog.show(); + }); + list_view.page.add_inner_button(__("Bulk Delete"), function () { + const dialog = new xhiveframework.ui.Dialog({ + title: __("Clear User Permissions"), + fields: [ + { + fieldname: "user", + label: __("For User"), + fieldtype: "Link", + options: "User", + reqd: 1, + }, + { + fieldname: "for_doctype", + label: __("For Document Type"), + fieldtype: "Link", + options: "DocType", + reqd: 1, + }, + ], + primary_action: (data) => { + // mandatory not filled + if (!data) return; + + xhiveframework.confirm(__("Are you sure?"), () => { + xhiveframework + .xcall( + "xhiveframework.core.doctype.user_permission.user_permission.clear_user_permissions", + data + ) + .then((data) => { + dialog.hide(); + let message = ""; + if (data === 0) { + message = __("No records deleted"); + } else if (data === 1) { + message = __("{0} record deleted", [data]); + } else { + message = __("{0} records deleted", [data]); + } + xhiveframework.show_alert({ + message, + indicator: "info", + }); + list_view.refresh(); + }); + }); + }, + primary_action_label: __("Delete"), + }); + + dialog.show(); + }); + }, + + validate: function (dialog, data) { + if (dialog.fields_dict.applicable_doctypes.get_unchecked_options().length == 0) { + data.apply_to_all_doctypes = 1; + data.applicable_doctypes = []; + return data; + } + if (data.apply_to_all_doctypes == 0 && !("applicable_doctypes" in data)) { + xhiveframework.throw(__("Please select applicable Doctypes")); + } + return data; + }, + + get_applicable_doctype: function (dialog) { + return new Promise((resolve) => { + xhiveframework + .call({ + method: "xhiveframework.core.doctype.user_permission.user_permission.check_applicable_doc_perm", + async: false, + args: { + user: dialog.fields_dict.user.value, + doctype: dialog.fields_dict.doctype.value, + docname: dialog.fields_dict.docname.value, + }, + }) + .then((r) => { + resolve(r.message); + }); + }); + }, + + get_multi_select_options: function (dialog, applicable) { + return new Promise((resolve) => { + xhiveframework + .call({ + method: "xhiveframework.desk.form.linked_with.get_linked_doctypes", + async: false, + args: { + user: dialog.fields_dict.user.value, + doctype: dialog.fields_dict.doctype.value, + docname: dialog.fields_dict.docname.value, + }, + }) + .then((r) => { + var options = []; + for (var d in r.message) { + var checked = $.inArray(d, applicable) != -1 ? 1 : 0; + options.push({ label: d, value: d, checked: checked }); + } + resolve(options); + }); + }); + }, + + on_doctype_change: function (dialog) { + dialog.set_df_property("docname", "hidden", 0); + dialog.set_df_property("docname", "reqd", 1); + dialog.set_df_property("is_default", "hidden", 0); + dialog.set_df_property("apply_to_all_doctypes", "hidden", 0); + dialog.set_value("apply_to_all_doctypes", "checked", 1); + let show = xhiveframework.boot.nested_set_doctypes.includes(dialog.get_value("doctype")); + dialog.set_df_property("hide_descendants", "hidden", !show); + dialog.refresh(); + }, + + on_docname_change: function (dialog, options, applicable) { + if (applicable.length != 0) { + dialog.set_primary_action("Update"); + dialog.set_title("Update User Permissions"); + dialog.set_df_property("applicable_doctypes", "options", options); + if ( + dialog.fields_dict.applicable_doctypes.get_checked_options().length == + options.length + ) { + dialog.set_df_property("applicable_doctypes", "hidden", 1); + } else { + dialog.set_df_property("applicable_doctypes", "hidden", 0); + dialog.set_df_property("apply_to_all_doctypes", "checked", 0); + } + } else { + dialog.set_primary_action("Submit"); + dialog.set_title("Add User Permissions"); + dialog.set_df_property("applicable_doctypes", "options", options); + dialog.set_df_property("applicable_doctypes", "hidden", 1); + } + dialog.refresh(); + }, + + on_apply_to_all_doctypes_change: function (dialog, options) { + if (dialog.fields_dict.apply_to_all_doctypes.get_value() == 0) { + dialog.set_df_property("applicable_doctypes", "hidden", 0); + dialog.set_df_property("applicable_doctypes", "options", options); + } else { + dialog.set_df_property("applicable_doctypes", "options", options); + dialog.set_df_property("applicable_doctypes", "hidden", 1); + } + dialog.refresh_sections(); + }, +}; diff --git a/xhiveframework/core/doctype/user_select_document_type/__init__.py b/xhiveframework/core/doctype/user_select_document_type/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.json b/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.json new file mode 100644 index 0000000..86e1942 --- /dev/null +++ b/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.json @@ -0,0 +1,33 @@ +{ + "actions": [], + "creation": "2021-01-17 18:28:14.208576", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-01-17 18:45:44.993190", + "modified_by": "Administrator", + "module": "Core", + "name": "User Select Document Type", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.py b/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.py new file mode 100644 index 0000000..4648d77 --- /dev/null +++ b/xhiveframework/core/doctype/user_select_document_type/user_select_document_type.py @@ -0,0 +1,22 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class UserSelectDocumentType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document_type: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/user_social_login/__init__.py b/xhiveframework/core/doctype/user_social_login/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_social_login/user_social_login.json b/xhiveframework/core/doctype/user_social_login/user_social_login.json new file mode 100644 index 0000000..6b4b182 --- /dev/null +++ b/xhiveframework/core/doctype/user_social_login/user_social_login.json @@ -0,0 +1,58 @@ +{ + "actions": [], + "creation": "2017-12-02 13:01:20.507112", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "provider", + "section_break_0", + "username", + "column_break_0", + "userid" + ], + "fields": [ + { + "fieldname": "provider", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Provider", + "read_only": 1 + }, + { + "fieldname": "section_break_0", + "fieldtype": "Section Break" + }, + { + "fieldname": "username", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Username", + "read_only": 1 + }, + { + "fieldname": "column_break_0", + "fieldtype": "Column Break" + }, + { + "fieldname": "userid", + "fieldtype": "Data", + "in_list_view": 1, + "label": "User ID", + "read_only": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:53.800689", + "modified_by": "Administrator", + "module": "Core", + "name": "User Social Login", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_social_login/user_social_login.py b/xhiveframework/core/doctype/user_social_login/user_social_login.py new file mode 100644 index 0000000..abcfa20 --- /dev/null +++ b/xhiveframework/core/doctype/user_social_login/user_social_login.py @@ -0,0 +1,23 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class UserSocialLogin(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + provider: DF.Data | None + userid: DF.Data | None + username: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/user_type/__init__.py b/xhiveframework/core/doctype/user_type/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_type/test_user_type.py b/xhiveframework/core/doctype/user_type/test_user_type.py new file mode 100644 index 0000000..4ef9744 --- /dev/null +++ b/xhiveframework/core/doctype/user_type/test_user_type.py @@ -0,0 +1,64 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.installer import update_site_config +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestUserType(XhiveFrameworkTestCase): + def setUp(self): + create_role() + + def test_add_select_perm_doctypes(self): + user_type = create_user_type("Test User Type") + + # select perms added for all link fields + doc = xhiveframework.get_meta("Contact") + link_fields = doc.get_link_fields() + select_doctypes = xhiveframework.get_all( + "User Select Document Type", {"parent": user_type.name}, pluck="document_type" + ) + + for entry in link_fields: + self.assertTrue(entry.options in select_doctypes) + + # select perms added for all child table link fields + link_fields = [] + for child_table in doc.get_table_fields(): + child_doc = xhiveframework.get_meta(child_table.options) + link_fields.extend(child_doc.get_link_fields()) + + for entry in link_fields: + self.assertTrue(entry.options in select_doctypes) + + def tearDown(self): + xhiveframework.db.rollback() + + +def create_user_type(user_type): + if xhiveframework.db.exists("User Type", user_type): + xhiveframework.delete_doc("User Type", user_type) + + user_type_limit = {xhiveframework.scrub(user_type): 1} + update_site_config("user_type_doctype_limit", user_type_limit) + + doc = xhiveframework.get_doc( + { + "doctype": "User Type", + "name": user_type, + "role": "_Test User Type", + "user_id_field": "user", + "apply_user_permission_on": "User", + } + ) + + doc.append("user_doctypes", {"document_type": "Contact", "read": 1, "write": 1}) + + return doc.insert() + + +def create_role(): + if not xhiveframework.db.exists("Role", "_Test User Type"): + xhiveframework.get_doc( + {"doctype": "Role", "role_name": "_Test User Type", "desk_access": 1, "is_custom": 1} + ).insert() diff --git a/xhiveframework/core/doctype/user_type/user_type.js b/xhiveframework/core/doctype/user_type/user_type.js new file mode 100644 index 0000000..a4f6f4e --- /dev/null +++ b/xhiveframework/core/doctype/user_type/user_type.js @@ -0,0 +1,71 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("User Type", { + refresh: function (frm) { + if (frm.is_new() && !xhiveframework.boot.developer_mode) frm.set_value("is_standard", 1); + + frm.set_query("document_type", "user_doctypes", function () { + return { + filters: { + istable: 0, + }, + }; + }); + + frm.set_query("document_type", "select_doctypes", function () { + return { + filters: { + istable: 0, + }, + }; + }); + + frm.set_query("document_type", "custom_select_doctypes", function () { + return { + filters: { + istable: 0, + }, + }; + }); + + frm.set_query("role", function () { + return { + filters: { + is_custom: 1, + disabled: 0, + desk_access: 1, + }, + }; + }); + + frm.set_query("apply_user_permission_on", function () { + return { + query: "xhiveframework.core.doctype.user_type.user_type.get_user_linked_doctypes", + }; + }); + }, + + onload: function (frm) { + frm.trigger("get_user_id_fields"); + }, + + apply_user_permission_on: function (frm) { + frm.set_value("user_id_field", ""); + frm.trigger("get_user_id_fields"); + }, + + get_user_id_fields: function (frm) { + if (frm.doc.apply_user_permission_on) { + xhiveframework.call({ + method: "xhiveframework.core.doctype.user_type.user_type.get_user_id", + args: { + parent: frm.doc.apply_user_permission_on, + }, + callback: function (r) { + set_field_options("user_id_field", [""].concat(r.message)); + }, + }); + } + }, +}); diff --git a/xhiveframework/core/doctype/user_type/user_type.json b/xhiveframework/core/doctype/user_type/user_type.json new file mode 100644 index 0000000..b0bdb21 --- /dev/null +++ b/xhiveframework/core/doctype/user_type/user_type.json @@ -0,0 +1,145 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2021-01-13 01:48:02.378548", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "is_standard", + "section_break_2", + "role", + "column_break_4", + "apply_user_permission_on", + "user_id_field", + "section_break_6", + "user_doctypes", + "custom_select_doctypes", + "select_doctypes", + "allowed_modules_section", + "user_type_modules" + ], + "fields": [ + { + "default": "0", + "depends_on": "eval: xhiveframework.boot.developer_mode", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "read_only_depends_on": "eval: !xhiveframework.boot.developer_mode" + }, + { + "depends_on": "eval: !doc.is_standard", + "fieldname": "section_break_2", + "fieldtype": "Section Break", + "label": "Document Types and Permissions" + }, + { + "depends_on": "eval: !doc.is_standard", + "fieldname": "user_doctypes", + "fieldtype": "Table", + "label": "Document Types", + "mandatory_depends_on": "eval: !doc.is_standard", + "options": "User Document Type" + }, + { + "depends_on": "eval: !doc.is_standard", + "fieldname": "role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Role", + "mandatory_depends_on": "eval: !doc.is_standard", + "options": "Role" + }, + { + "fieldname": "select_doctypes", + "fieldtype": "Table", + "hidden": 1, + "label": "Document Types (Select Permissions Only)", + "options": "User Select Document Type", + "read_only": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: !doc.is_standard", + "description": "Can only list down the document types which has been linked to the User document type.", + "fieldname": "apply_user_permission_on", + "fieldtype": "Link", + "label": "Apply User Permission On", + "mandatory_depends_on": "eval: !doc.is_standard", + "options": "DocType" + }, + { + "depends_on": "eval: !doc.is_standard", + "fieldname": "section_break_6", + "fieldtype": "Section Break", + "hide_border": 1 + }, + { + "depends_on": "apply_user_permission_on", + "fieldname": "user_id_field", + "fieldtype": "Select", + "label": "User Id Field", + "mandatory_depends_on": "eval: !doc.is_standard" + }, + { + "depends_on": "eval: !doc.is_standard", + "fieldname": "allowed_modules_section", + "fieldtype": "Section Break", + "label": "Allowed Modules" + }, + { + "fieldname": "user_type_modules", + "fieldtype": "Table", + "label": "User Type Module", + "no_copy": 1, + "options": "User Type Module", + "print_hide": 1, + "read_only": 1 + }, + { + "fieldname": "custom_select_doctypes", + "fieldtype": "Table", + "label": "Custom Document Types (Select Permission)", + "options": "User Select Document Type" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2022-06-09 14:00:36.820306", + "modified_by": "Administrator", + "module": "Core", + "name": "User Type", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/core/doctype/user_type/user_type.py b/xhiveframework/core/doctype/user_type/user_type.py new file mode 100644 index 0000000..c3f4054 --- /dev/null +++ b/xhiveframework/core/doctype/user_type/user_type.py @@ -0,0 +1,346 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.config import get_modules_from_app +from xhiveframework.model.document import Document +from xhiveframework.permissions import add_permission, add_user_permission +from xhiveframework.utils import get_link_to_form + + +class UserType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.user_document_type.user_document_type import UserDocumentType + from xhiveframework.core.doctype.user_select_document_type.user_select_document_type import ( + UserSelectDocumentType, + ) + from xhiveframework.core.doctype.user_type_module.user_type_module import UserTypeModule + from xhiveframework.types import DF + + apply_user_permission_on: DF.Link | None + custom_select_doctypes: DF.Table[UserSelectDocumentType] + is_standard: DF.Check + role: DF.Link | None + select_doctypes: DF.Table[UserSelectDocumentType] + user_doctypes: DF.Table[UserDocumentType] + user_id_field: DF.Literal[None] + user_type_modules: DF.Table[UserTypeModule] + + # end: auto-generated types + def validate(self): + self.set_modules() + self.add_select_perm_doctypes() + + def clear_cache(self): + super().clear_cache() + + if not self.is_standard: + xhiveframework.cache.delete_value("non_standard_user_types") + + def on_update(self): + if self.is_standard: + return + + self.validate_document_type_limit() + self.validate_role() + self.add_role_permissions_for_user_doctypes() + self.add_role_permissions_for_select_doctypes() + self.add_role_permissions_for_file() + self.update_users() + self.remove_permission_for_deleted_doctypes() + + def on_trash(self): + if self.is_standard: + xhiveframework.throw(_("Standard user type {0} can not be deleted.").format(xhiveframework.bold(self.name))) + + def set_modules(self): + if not self.user_doctypes: + return + + modules = xhiveframework.get_all( + "DocType", + filters={"name": ("in", [d.document_type for d in self.user_doctypes])}, + distinct=True, + pluck="module", + ) + + self.set("user_type_modules", []) + for module in modules: + self.append("user_type_modules", {"module": module}) + + def validate_document_type_limit(self): + limit = xhiveframework.conf.get("user_type_doctype_limit", {}).get(xhiveframework.scrub(self.name)) + + if not limit and xhiveframework.session.user != "Administrator": + xhiveframework.throw( + _("User does not have permission to create the new {0}").format(xhiveframework.bold(_("User Type"))), + title=_("Permission Error"), + ) + + if not limit: + xhiveframework.throw( + _("The limit has not set for the user type {0} in the site config file.").format( + xhiveframework.bold(self.name) + ), + title=_("Set Limit"), + ) + + if self.user_doctypes and len(self.user_doctypes) > limit: + xhiveframework.throw( + _("The total number of user document types limit has been crossed."), + title=_("User Document Types Limit Exceeded"), + ) + + custom_doctypes = [row.document_type for row in self.user_doctypes if row.is_custom] + if custom_doctypes and len(custom_doctypes) > 3: + xhiveframework.throw( + _("You can only set the 3 custom doctypes in the Document Types table."), + title=_("Custom Document Types Limit Exceeded"), + ) + + def validate_role(self): + if not self.role: + xhiveframework.throw(_("The field {0} is mandatory").format(xhiveframework.bold(_("Role")))) + + if not xhiveframework.db.get_value("Role", self.role, "is_custom"): + xhiveframework.throw( + _("The role {0} should be a custom role.").format( + xhiveframework.bold(get_link_to_form("Role", self.role)) + ) + ) + + def update_users(self): + for row in xhiveframework.get_all("User", filters={"user_type": self.name}): + user = xhiveframework.get_cached_doc("User", row.name) + self.update_roles_in_user(user) + self.update_modules_in_user(user) + user.update_children() + + def update_roles_in_user(self, user): + user.set("roles", []) + user.append("roles", {"role": self.role}) + + def update_modules_in_user(self, user): + block_modules = xhiveframework.get_all( + "Module Def", + fields=["name as module"], + filters={"name": ["not in", [d.module for d in self.user_type_modules]]}, + ) + + if block_modules: + user.set("block_modules", block_modules) + + def add_role_permissions_for_user_doctypes(self): + perms = ["read", "write", "create", "submit", "cancel", "amend", "delete"] + for row in self.user_doctypes: + docperm = add_role_permissions(row.document_type, self.role) + + values = {perm: row.get(perm) or 0 for perm in perms} + for perm in ["print", "email", "share"]: + values[perm] = 1 + + xhiveframework.db.set_value("Custom DocPerm", docperm, values) + + def add_select_perm_doctypes(self): + if xhiveframework.flags.ignore_select_perm: + return + + self.select_doctypes = [] + + select_doctypes = [] + user_doctypes = [row.document_type for row in self.user_doctypes] + + for doctype in user_doctypes: + doc = xhiveframework.get_meta(doctype) + self.prepare_select_perm_doctypes(doc, user_doctypes, select_doctypes) + + for child_table in doc.get_table_fields(): + child_doc = xhiveframework.get_meta(child_table.options) + if child_doc: + self.prepare_select_perm_doctypes(child_doc, user_doctypes, select_doctypes) + + if select_doctypes: + select_doctypes = set(select_doctypes) + for select_doctype in select_doctypes: + self.append("select_doctypes", {"document_type": select_doctype}) + + def prepare_select_perm_doctypes(self, doc, user_doctypes, select_doctypes): + for field in doc.get_link_fields(): + if field.options not in user_doctypes: + select_doctypes.append(field.options) + + def add_role_permissions_for_select_doctypes(self): + for doctype in ["select_doctypes", "custom_select_doctypes"]: + for row in self.get(doctype): + docperm = add_role_permissions(row.document_type, self.role) + xhiveframework.db.set_value( + "Custom DocPerm", docperm, {"select": 1, "read": 0, "create": 0, "write": 0} + ) + + def add_role_permissions_for_file(self): + docperm = add_role_permissions("File", self.role) + xhiveframework.db.set_value("Custom DocPerm", docperm, {"read": 1, "create": 1, "write": 1}) + + def remove_permission_for_deleted_doctypes(self): + doctypes = [d.document_type for d in self.user_doctypes] + + # Do not remove the doc permission for the file doctype + doctypes.append("File") + + for doctype in ["select_doctypes", "custom_select_doctypes"]: + doctypes.extend(dt.document_type for dt in self.get(doctype)) + for perm in xhiveframework.get_all( + "Custom DocPerm", filters={"role": self.role, "parent": ["not in", doctypes]} + ): + xhiveframework.delete_doc("Custom DocPerm", perm.name) + + +def add_role_permissions(doctype, role): + name = xhiveframework.get_value("Custom DocPerm", dict(parent=doctype, role=role, permlevel=0)) + + if not name: + name = add_permission(doctype, role, 0) + + return name + + +def get_non_standard_user_types(): + user_types = xhiveframework.get_all( + "User Type", + fields=["apply_user_permission_on", "name", "user_id_field"], + filters={"is_standard": 0}, + ) + + return {d.name: [d.apply_user_permission_on, d.user_id_field] for d in user_types} + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_user_linked_doctypes(doctype, txt, searchfield, start, page_len, filters): + modules = [d.get("module_name") for d in get_modules_from_app("xhiveframework")] + + filters = [ + ["DocField", "options", "=", "User"], + ["DocType", "is_submittable", "=", 0], + ["DocType", "issingle", "=", 0], + ["DocType", "module", "not in", modules], + ["DocType", "read_only", "=", 0], + ["DocType", "name", "like", f"%{txt}%"], + ] + + doctypes = xhiveframework.get_all( + "DocType", + fields=["`tabDocType`.`name`"], + filters=filters, + order_by="`tabDocType`.`idx` desc", + limit_start=start, + limit_page_length=page_len, + as_list=1, + ) + + custom_dt_filters = [ + ["Custom Field", "dt", "like", f"%{txt}%"], + ["Custom Field", "options", "=", "User"], + ["Custom Field", "fieldtype", "=", "Link"], + ] + + custom_doctypes = xhiveframework.get_all( + "Custom Field", fields=["dt as name"], filters=custom_dt_filters, as_list=1 + ) + + return doctypes + custom_doctypes + + +@xhiveframework.whitelist() +def get_user_id(parent): + data = ( + xhiveframework.get_all( + "DocField", + fields=["label", "fieldname as value"], + filters={"options": "User", "fieldtype": "Link", "parent": parent}, + ) + or [] + ) + + data.extend( + xhiveframework.get_all( + "Custom Field", + fields=["label", "fieldname as value"], + filters={"options": "User", "fieldtype": "Link", "dt": parent}, + ) + ) + + return data + + +def user_linked_with_permission_on_doctype(doc, user): + if not doc.apply_user_permission_on: + return True + + if not doc.user_id_field: + xhiveframework.throw(_("User Id Field is mandatory in the user type {0}").format(xhiveframework.bold(doc.name))) + + if xhiveframework.db.get_value(doc.apply_user_permission_on, {doc.user_id_field: user}, "name"): + return True + else: + label = xhiveframework.get_meta(doc.apply_user_permission_on).get_field(doc.user_id_field).label + + xhiveframework.msgprint( + _( + "To set the role {0} in the user {1}, kindly set the {2} field as {3} in one of the {4} record." + ).format( + xhiveframework.bold(doc.role), + xhiveframework.bold(user), + xhiveframework.bold(label), + xhiveframework.bold(user), + xhiveframework.bold(doc.apply_user_permission_on), + ) + ) + + return False + + +def apply_permissions_for_non_standard_user_type(doc, method=None): + """Create user permission for the non standard user type""" + if not xhiveframework.db.table_exists("User Type") or xhiveframework.flags.in_migrate: + return + + user_types = xhiveframework.cache.get_value( + "non_standard_user_types", + get_non_standard_user_types, + ) + + if not user_types: + return + + for user_type, data in user_types.items(): + if not doc.get(data[1]) or doc.doctype != data[0]: + continue + + if xhiveframework.get_cached_value("User", doc.get(data[1]), "user_type") != user_type: + return + + if doc.get(data[1]) and ( + not doc._doc_before_save + or doc.get(data[1]) != doc._doc_before_save.get(data[1]) + or not xhiveframework.db.get_value( + "User Permission", {"user": doc.get(data[1]), "allow": data[0], "for_value": doc.name}, "name" + ) + ): + perm_data = xhiveframework.db.get_value( + "User Permission", {"allow": doc.doctype, "for_value": doc.name}, ["name", "user"] + ) + + if not perm_data: + user_doc = xhiveframework.get_cached_doc("User", doc.get(data[1])) + user_doc.set_roles_and_modules_based_on_user_type() + user_doc.update_children() + add_user_permission(doc.doctype, doc.name, doc.get(data[1])) + else: + xhiveframework.db.set_value("User Permission", perm_data[0], "user", doc.get(data[1])) diff --git a/xhiveframework/core/doctype/user_type/user_type_dashboard.py b/xhiveframework/core/doctype/user_type/user_type_dashboard.py new file mode 100644 index 0000000..8cc610f --- /dev/null +++ b/xhiveframework/core/doctype/user_type/user_type_dashboard.py @@ -0,0 +1,5 @@ +from xhiveframework import _ + + +def get_data(): + return {"fieldname": "user_type", "transactions": [{"label": _("Reference"), "items": ["User"]}]} diff --git a/xhiveframework/core/doctype/user_type/user_type_list.js b/xhiveframework/core/doctype/user_type/user_type_list.js new file mode 100644 index 0000000..a6b22b1 --- /dev/null +++ b/xhiveframework/core/doctype/user_type/user_type_list.js @@ -0,0 +1,10 @@ +xhiveframework.listview_settings["User Type"] = { + add_fields: ["is_standard"], + get_indicator: function (doc) { + if (doc.is_standard) { + return [__("Standard"), "green", "is_standard,=,1"]; + } else { + return [__("Custom"), "blue", "is_standard,=,0"]; + } + }, +}; diff --git a/xhiveframework/core/doctype/user_type_module/__init__.py b/xhiveframework/core/doctype/user_type_module/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/user_type_module/user_type_module.json b/xhiveframework/core/doctype/user_type_module/user_type_module.json new file mode 100644 index 0000000..0f9cbef --- /dev/null +++ b/xhiveframework/core/doctype/user_type_module/user_type_module.json @@ -0,0 +1,33 @@ +{ + "actions": [], + "creation": "2021-01-24 03:05:24.634719", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "module" + ], + "fields": [ + { + "fieldname": "module", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Module", + "options": "Module Def", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-01-24 03:07:43.602927", + "modified_by": "Administrator", + "module": "Core", + "name": "User Type Module", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/user_type_module/user_type_module.py b/xhiveframework/core/doctype/user_type_module/user_type_module.py new file mode 100644 index 0000000..6e5f16f --- /dev/null +++ b/xhiveframework/core/doctype/user_type_module/user_type_module.py @@ -0,0 +1,22 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class UserTypeModule(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + module: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/core/doctype/version/__init__.py b/xhiveframework/core/doctype/version/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/version/test_records.json b/xhiveframework/core/doctype/version/test_records.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/xhiveframework/core/doctype/version/test_records.json @@ -0,0 +1 @@ +[] diff --git a/xhiveframework/core/doctype/version/test_version.py b/xhiveframework/core/doctype/version/test_version.py new file mode 100644 index 0000000..d8b3edd --- /dev/null +++ b/xhiveframework/core/doctype/version/test_version.py @@ -0,0 +1,58 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import copy + +import xhiveframework +from xhiveframework.core.doctype.version.version import get_diff +from xhiveframework.test_runner import make_test_objects +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestVersion(XhiveFrameworkTestCase): + def test_get_diff(self): + xhiveframework.set_user("Administrator") + test_records = make_test_objects("Event", reset=True) + old_doc = xhiveframework.get_doc("Event", test_records[0]) + new_doc = copy.deepcopy(old_doc) + + old_doc.color = None + new_doc.color = "#fafafa" + + diff = get_diff(old_doc, new_doc)["changed"] + + self.assertEqual(get_fieldnames(diff)[0], "color") + self.assertTrue(get_old_values(diff)[0] is None) + self.assertEqual(get_new_values(diff)[0], "#fafafa") + + new_doc.starts_on = "2017-07-20" + + diff = get_diff(old_doc, new_doc)["changed"] + + self.assertEqual(get_fieldnames(diff)[1], "starts_on") + self.assertEqual(get_old_values(diff)[1], "01-01-2014 00:00:00") + self.assertEqual(get_new_values(diff)[1], "07-20-2017 00:00:00") + + def test_no_version_on_new_doc(self): + from xhiveframework.desk.form.load import get_versions + + t = xhiveframework.get_doc(doctype="ToDo", description="something") + t.save(ignore_version=False) + + self.assertFalse(get_versions(t)) + + t = xhiveframework.get_doc(t.doctype, t.name) + t.description = "changed" + t.save(ignore_version=False) + self.assertTrue(get_versions(t)) + + +def get_fieldnames(change_array): + return [d[0] for d in change_array] + + +def get_old_values(change_array): + return [d[1] for d in change_array] + + +def get_new_values(change_array): + return [d[2] for d in change_array] diff --git a/xhiveframework/core/doctype/version/version.js b/xhiveframework/core/doctype/version/version.js new file mode 100644 index 0000000..8eed509 --- /dev/null +++ b/xhiveframework/core/doctype/version/version.js @@ -0,0 +1,12 @@ +xhiveframework.ui.form.on("Version", "refresh", function (frm) { + $( + xhiveframework.render_template("version_view", { doc: frm.doc, data: JSON.parse(frm.doc.data) }) + ).appendTo(frm.fields_dict.table_html.$wrapper.empty()); + + frm.add_custom_button(__("Show all Versions"), function () { + xhiveframework.set_route("List", "Version", { + ref_doctype: frm.doc.ref_doctype, + docname: frm.doc.docname, + }); + }); +}); diff --git a/xhiveframework/core/doctype/version/version.json b/xhiveframework/core/doctype/version/version.json new file mode 100644 index 0000000..570b536 --- /dev/null +++ b/xhiveframework/core/doctype/version/version.json @@ -0,0 +1,81 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2014-02-20 17:22:37", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "ref_doctype", + "column_break_3", + "docname", + "data", + "section_break_4", + "table_html" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "docname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Document Name", + "reqd": 1 + }, + { + "fieldname": "data", + "fieldtype": "Code", + "hidden": 1, + "label": "Data" + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break" + }, + { + "fieldname": "table_html", + "fieldtype": "HTML", + "label": "Table HTML" + } + ], + "icon": "fa fa-copy", + "idx": 1, + "in_create": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Core", + "name": "Version", + "owner": "Administrator", + "permissions": [ + { + "export": 1, + "read": 1, + "report": 1, + "role": "System Manager" + }, + { + "delete": 1, + "read": 1, + "role": "Administrator" + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "docname", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/version/version.py b/xhiveframework/core/doctype/version/version.py new file mode 100644 index 0000000..cfa4a3c --- /dev/null +++ b/xhiveframework/core/doctype/version/version.py @@ -0,0 +1,169 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.model import no_value_fields, table_fields +from xhiveframework.model.document import Document + + +class Version(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data: DF.Code | None + docname: DF.Data + ref_doctype: DF.Link + + # end: auto-generated types + def update_version_info(self, old: Document | None, new: Document) -> bool: + """Update changed info and return true if change contains useful data.""" + if not old: + # Check if doc has some information about creation source like data import + return self.for_insert(new) + else: + return self.set_diff(old, new) + + @staticmethod + def set_impersonator(data): + if not xhiveframework.session: + return + if impersonator := xhiveframework.session.data.get("impersonated_by"): + data["impersonated_by"] = impersonator + + def set_diff(self, old: Document, new: Document) -> bool: + """Set the data property with the diff of the docs if present""" + diff = get_diff(old, new) + if diff: + self.set_impersonator(diff) + self.ref_doctype = new.doctype + self.docname = new.name + self.data = xhiveframework.as_json(diff, indent=None, separators=(",", ":")) + return True + else: + return False + + def for_insert(self, doc: Document) -> bool: + updater_reference = doc.flags.updater_reference + if not updater_reference: + return False + + data = { + "creation": doc.creation, + "updater_reference": updater_reference, + "created_by": doc.owner, + } + self.set_impersonator(data) + self.ref_doctype = doc.doctype + self.docname = doc.name + self.data = xhiveframework.as_json(data, indent=None, separators=(",", ":")) + return True + + def get_data(self): + return json.loads(self.data) + + +def get_diff(old, new, for_child=False, compare_cancelled=False): + """Get diff between 2 document objects + + If there is a change, then returns a dict like: + + { + "changed" : [[fieldname1, old, new], [fieldname2, old, new]], + "added" : [[table_fieldname1, {dict}], ], + "removed" : [[table_fieldname1, {dict}], ], + "row_changed": [[table_fieldname1, row_name1, row_index, + [[child_fieldname1, old, new], + [child_fieldname2, old, new]], ] + ], + + }""" + if not new: + return None + + blacklisted_fields = ["Markdown Editor", "Text Editor", "Code", "HTML Editor"] + + # capture data import if set + data_import = new.flags.via_data_import + updater_reference = new.flags.updater_reference + + out = xhiveframework._dict( + changed=[], + added=[], + removed=[], + row_changed=[], + data_import=data_import, + updater_reference=updater_reference, + ) + + if not for_child: + amended_from = new.get("amended_from") + old_row_name_field = "_amended_from" if (amended_from and amended_from == old.name) else "name" + + for df in new.meta.fields: + if df.fieldtype in no_value_fields and df.fieldtype not in table_fields: + continue + + old_value, new_value = old.get(df.fieldname), new.get(df.fieldname) + + if not for_child and df.fieldtype in table_fields: + old_rows_by_name = {} + for d in old_value: + old_rows_by_name[d.name] = d + + found_rows = set() + + # check rows for additions, changes + for i, d in enumerate(new_value): + old_row_name = getattr(d, old_row_name_field, None) + if compare_cancelled: + if amended_from: + if len(old_value) > i: + old_row_name = old_value[i].name + + if old_row_name and old_row_name in old_rows_by_name: + found_rows.add(old_row_name) + + diff = get_diff(old_rows_by_name[old_row_name], d, for_child=True) + if diff and diff.changed: + out.row_changed.append((df.fieldname, i, d.name, diff.changed)) + else: + out.added.append([df.fieldname, d.as_dict()]) + + # check for deletions + for d in old_value: + if d.name not in found_rows: + out.removed.append([df.fieldname, d.as_dict()]) + + elif old_value != new_value: + if df.fieldtype not in blacklisted_fields: + old_value = old.get_formatted(df.fieldname) if old_value else old_value + new_value = new.get_formatted(df.fieldname) if new_value else new_value + + if old_value != new_value: + out.changed.append((df.fieldname, old_value, new_value)) + + # name & docstatus + if not for_child: + for key in ("name", "docstatus"): + old_value = getattr(old, key) + new_value = getattr(new, key) + + if old_value != new_value: + out.changed.append([key, old_value, new_value]) + + if any((out.changed, out.added, out.removed, out.row_changed)): + return out + + else: + return None + + +def on_doctype_update(): + xhiveframework.db.add_index("Version", ["ref_doctype", "docname"]) diff --git a/xhiveframework/core/doctype/version/version_view.html b/xhiveframework/core/doctype/version/version_view.html new file mode 100644 index 0000000..81d3e5c --- /dev/null +++ b/xhiveframework/core/doctype/version/version_view.html @@ -0,0 +1,95 @@ +
    +{% if data.comment %} +

    {{ __("Comment") + " (" + data.comment_type }})

    +

    {{ data.comment }}

    +{% endif %} + +{% if data.changed && data.changed.length %} +

    {{ __("Values Changed") }}

    + + + + + + + + + + {% for item in data.changed %} + + + + + + {% endfor %} + +
    {{ __("Property") }}{{ __("Original Value") }}{{ __("New Value") }}
    {{ xhiveframework.meta.get_label(doc.ref_doctype, item[0]) }}{{ xhiveframework.utils.escape_html(item[1]) }}{{ xhiveframework.utils.escape_html(item[2]) }}
    +{% endif %} + +{% var _keys = ["added", "removed"]; %} +{% for key in _keys %} + {% if data[key] && data[key].length %} + {% var title = key==="added" ? __("Rows Added") : __("Rows Removed"); %} +

    {{ title }}

    + + + + + + + + + {% var values = data[key]; %} + {% for item in values %} + + + + + {% endfor %} + +
    {{ __("Property") }}{{ title }}
    {{ xhiveframework.meta.get_label(doc.ref_doctype, item[0]) }} + {% var item_keys = Object.keys(item[1]).sort(); %} + + + {% for row_key in item_keys %} + + + + + {% endfor %} + +
    {{ row_key }}{{ xhiveframework.utils.escape_html(item[1][row_key]) }}
    +
    + + {% endif %} +{% endfor %} + +{% if data.row_changed && data.row_changed.length %} +

    {{ __("Row Values Changed") }}

    + + + + + + + + + + + + {% var values = data.row_changed; %} + {% for table_info in values %} + {% var _changed = table_info[3]; %} + {% for item in _changed %} + + + + + + + + {% endfor %} + {% endfor %} + +{% endif %} + diff --git a/xhiveframework/core/doctype/view_log/__init__.py b/xhiveframework/core/doctype/view_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/doctype/view_log/test_view_log.py b/xhiveframework/core/doctype/view_log/test_view_log.py new file mode 100644 index 0000000..513f13b --- /dev/null +++ b/xhiveframework/core/doctype/view_log/test_view_log.py @@ -0,0 +1,34 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestViewLog(XhiveFrameworkTestCase): + def tearDown(self): + xhiveframework.set_user("Administrator") + + def test_if_user_is_added(self): + ev = xhiveframework.get_doc( + { + "doctype": "Event", + "subject": "test event for view logs", + "starts_on": "2018-06-04 14:11:00", + "event_type": "Public", + } + ).insert() + + xhiveframework.set_user("test@example.com") + + from xhiveframework.desk.form.load import getdoc + + # load the form + getdoc("Event", ev.name) + a = xhiveframework.get_value( + doctype="View Log", + filters={"reference_doctype": "Event", "reference_name": ev.name}, + fieldname=["viewed_by"], + ) + + self.assertEqual("test@example.com", a) + self.assertNotEqual("test1@example.com", a) diff --git a/xhiveframework/core/doctype/view_log/view_log.js b/xhiveframework/core/doctype/view_log/view_log.js new file mode 100644 index 0000000..f023205 --- /dev/null +++ b/xhiveframework/core/doctype/view_log/view_log.js @@ -0,0 +1,6 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("View Log", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/core/doctype/view_log/view_log.json b/xhiveframework/core/doctype/view_log/view_log.json new file mode 100644 index 0000000..a350ae8 --- /dev/null +++ b/xhiveframework/core/doctype/view_log/view_log.json @@ -0,0 +1,56 @@ +{ + "actions": [], + "creation": "2018-05-27 02:20:11.193944", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "viewed_by", + "reference_doctype", + "reference_name" + ], + "fields": [ + { + "fieldname": "viewed_by", + "fieldtype": "Data", + "label": "Viewed By", + "set_only_once": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType", + "set_only_once": 1 + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "label": "Reference name", + "options": "reference_doctype", + "search_index": 1, + "set_only_once": 1 + } + ], + "links": [], + "modified": "2022-09-07 05:16:14.587628", + "modified_by": "Administrator", + "module": "Core", + "name": "View Log", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/core/doctype/view_log/view_log.py b/xhiveframework/core/doctype/view_log/view_log.py new file mode 100644 index 0000000..d1b9311 --- /dev/null +++ b/xhiveframework/core/doctype/view_log/view_log.py @@ -0,0 +1,28 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class ViewLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + reference_doctype: DF.Link | None + reference_name: DF.DynamicLink | None + viewed_by: DF.Data | None + + # end: auto-generated types + @staticmethod + def clear_old_logs(days=180): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("View Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) diff --git a/xhiveframework/core/form_tour/doctype/doctype.json b/xhiveframework/core/form_tour/doctype/doctype.json new file mode 100644 index 0000000..391d3ec --- /dev/null +++ b/xhiveframework/core/form_tour/doctype/doctype.json @@ -0,0 +1,56 @@ +{ + "creation": "2021-11-23 12:38:52.807353", + "docstatus": 0, + "doctype": "Form Tour", + "first_document": 0, + "idx": 0, + "include_name_field": 1, + "is_standard": 1, + "modified": "2021-11-25 17:03:01.646360", + "modified_by": "Administrator", + "module": "Core", + "name": "Doctype", + "owner": "Administrator", + "reference_doctype": "DocType", + "save_on_complete": 1, + "steps": [ + { + "description": "Select a Module to which this DocType would belong", + "field": "", + "fieldname": "module", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Module", + "parent_field": "", + "position": "Right", + "title": "Module" + }, + { + "description": "Check this to make the DocType as Custom", + "field": "", + "fieldname": "custom", + "fieldtype": "Check", + "has_next_condition": 1, + "is_table_field": 0, + "label": "Custom?", + "next_step_condition": "eval: doc.custom", + "parent_field": "", + "position": "Left", + "title": "Custom " + }, + { + "description": "A Field (or a docfield) defines a property of a DocType. You can define the column name, label, datatype and more for DocFields. For instance, a ToDo doctype has fields description, status and priority. These ultimately become columns in the database table tabToDo.", + "field": "", + "fieldname": "fields", + "fieldtype": "Table", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Fields", + "parent_field": "", + "position": "Top", + "title": "Fields" + } + ], + "title": "Doctype" +} \ No newline at end of file diff --git a/xhiveframework/core/notifications.py b/xhiveframework/core/notifications.py new file mode 100644 index 0000000..b7bee35 --- /dev/null +++ b/xhiveframework/core/notifications.py @@ -0,0 +1,44 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def get_notification_config(): + return { + "for_doctype": { + "Error Log": {"seen": 0}, + "Communication": {"status": "Open", "communication_type": "Communication"}, + "ToDo": "xhiveframework.core.notifications.get_things_todo", + "Event": "xhiveframework.core.notifications.get_todays_events", + "Workflow Action": {"status": "Open"}, + }, + } + + +def get_things_todo(as_list=False): + """Returns a count of incomplete todos""" + data = xhiveframework.get_list( + "ToDo", + fields=["name", "description"] if as_list else "count(*)", + filters=[["ToDo", "status", "=", "Open"]], + or_filters=[ + ["ToDo", "allocated_to", "=", xhiveframework.session.user], + ["ToDo", "assigned_by", "=", xhiveframework.session.user], + ], + as_list=True, + ) + + if as_list: + return data + return data[0][0] + + +def get_todays_events(as_list: bool = False): + """Returns a count of todays events in calendar""" + from xhiveframework.desk.doctype.event.event import get_events + from xhiveframework.utils import nowdate + + today = nowdate() + events = get_events(today, today) + return events if as_list else len(events) diff --git a/xhiveframework/core/page/__init__.py b/xhiveframework/core/page/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/page/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/page/dashboard_view/__init__.py b/xhiveframework/core/page/dashboard_view/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/page/dashboard_view/dashboard_view.js b/xhiveframework/core/page/dashboard_view/dashboard_view.js new file mode 100644 index 0000000..00ce70a --- /dev/null +++ b/xhiveframework/core/page/dashboard_view/dashboard_view.js @@ -0,0 +1,196 @@ +// Copyright (c) 2019, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.provide("xhiveframework.dashboards"); +xhiveframework.provide("xhiveframework.dashboards.chart_sources"); + +xhiveframework.pages["dashboard-view"].on_page_load = function (wrapper) { + xhiveframework.ui.make_app_page({ + parent: wrapper, + title: __("Dashboard"), + single_column: true, + }); + + xhiveframework.dashboard = new Dashboard(wrapper); + $(wrapper).bind("show", function () { + xhiveframework.dashboard.show(); + }); +}; + +class Dashboard { + constructor(wrapper) { + this.wrapper = $(wrapper); + $(`
    +
    +
    `).appendTo(this.wrapper.find(".page-content").empty()); + this.container = this.wrapper.find(".dashboard-graph"); + this.page = wrapper.page; + } + + show() { + this.route = xhiveframework.get_route(); + this.set_breadcrumbs(); + if (this.route.length > 1) { + // from route + this.show_dashboard(this.route.slice(-1)[0]); + } else { + // last opened + if (xhiveframework.last_dashboard) { + xhiveframework.set_re_route("dashboard-view", xhiveframework.last_dashboard); + } else { + // default dashboard + xhiveframework.db.get_list("Dashboard", { filters: { is_default: 1 } }).then((data) => { + if (data && data.length) { + xhiveframework.set_re_route("dashboard-view", data[0].name); + } else { + // no default, get the latest one + xhiveframework.db.get_list("Dashboard", { limit: 1 }).then((data) => { + if (data && data.length) { + xhiveframework.set_re_route("dashboard-view", data[0].name); + } else { + // create a new dashboard! + xhiveframework.new_doc("Dashboard"); + } + }); + } + }); + } + } + } + + show_dashboard(current_dashboard_name) { + if (this.dashboard_name !== current_dashboard_name) { + this.dashboard_name = current_dashboard_name; + let title = this.dashboard_name; + if (!this.dashboard_name.toLowerCase().includes(__("dashboard"))) { + // ensure dashboard title has "dashboard" + title = __("{0} Dashboard", [__(title)]); + } + this.page.set_title(__(title)); + this.set_dropdown(); + this.container.empty(); + this.refresh(); + } + this.charts = {}; + xhiveframework.last_dashboard = current_dashboard_name; + } + + set_breadcrumbs() { + xhiveframework.breadcrumbs.add("Desk", "Dashboard"); + } + + refresh() { + xhiveframework.run_serially([() => this.render_cards(), () => this.render_charts()]); + } + + render_charts() { + return this.get_permitted_items( + "xhiveframework.desk.doctype.dashboard.dashboard.get_permitted_charts" + ).then((charts) => { + if (!charts.length) { + xhiveframework.msgprint( + __("No Permitted Charts on this Dashboard"), + __("No Permitted Charts") + ); + } + + xhiveframework.dashboard_utils.get_dashboard_settings().then((settings) => { + let chart_config = settings.chart_config ? JSON.parse(settings.chart_config) : {}; + this.charts = charts.map((chart) => { + return { + chart_name: chart.chart, + label: chart.chart, + chart_settings: chart_config[chart.chart] || {}, + ...chart, + }; + }); + + this.chart_group = new xhiveframework.widget.WidgetGroup({ + title: null, + container: this.container, + type: "chart", + columns: 2, + options: { + allow_sorting: false, + allow_create: false, + allow_delete: false, + allow_hiding: false, + allow_edit: false, + }, + widgets: this.charts, + }); + }); + }); + } + + render_cards() { + return this.get_permitted_items( + "xhiveframework.desk.doctype.dashboard.dashboard.get_permitted_cards" + ).then((cards) => { + if (!cards.length) { + return; + } + + this.number_cards = cards.map((card) => { + return { + name: card.card, + }; + }); + + this.number_card_group = new xhiveframework.widget.WidgetGroup({ + container: this.container, + type: "number_card", + columns: 3, + options: { + allow_sorting: false, + allow_create: false, + allow_delete: false, + allow_hiding: false, + allow_edit: false, + }, + widgets: this.number_cards, + }); + }); + } + + get_permitted_items(method) { + return xhiveframework + .xcall(method, { + dashboard_name: this.dashboard_name, + }) + .then((items) => { + return items; + }); + } + + set_dropdown() { + this.page.clear_menu(); + + this.page.add_menu_item(__("Edit"), () => { + xhiveframework.set_route("Form", "Dashboard", xhiveframework.dashboard.dashboard_name); + }); + + this.page.add_menu_item(__("New"), () => { + xhiveframework.new_doc("Dashboard"); + }); + + this.page.add_menu_item(__("Refresh All"), () => { + this.chart_group && this.chart_group.widgets_list.forEach((chart) => chart.refresh()); + this.number_card_group && + this.number_card_group.widgets_list.forEach((card) => card.render_card()); + }); + + xhiveframework.db.get_list("Dashboard").then((dashboards) => { + dashboards.map((dashboard) => { + let name = dashboard.name; + if (name != this.dashboard_name) { + this.page.add_menu_item( + name, + () => xhiveframework.set_route("dashboard-view", name), + 1 + ); + } + }); + }); + } +} diff --git a/xhiveframework/core/page/dashboard_view/dashboard_view.json b/xhiveframework/core/page/dashboard_view/dashboard_view.json new file mode 100644 index 0000000..4ece98a --- /dev/null +++ b/xhiveframework/core/page/dashboard_view/dashboard_view.json @@ -0,0 +1,19 @@ +{ + "content": null, + "creation": "2019-01-08 19:19:48.073410", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2020-12-16 12:29:08.610352", + "modified_by": "Administrator", + "module": "Core", + "name": "dashboard-view", + "owner": "Administrator", + "page_name": "dashboard-view", + "roles": [], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0, + "title": "Dashboard" +} \ No newline at end of file diff --git a/xhiveframework/core/page/permission_manager/README.md b/xhiveframework/core/page/permission_manager/README.md new file mode 100644 index 0000000..c62ccb3 --- /dev/null +++ b/xhiveframework/core/page/permission_manager/README.md @@ -0,0 +1 @@ +Interface for easy browsing and setting of user permissions (DocPerm) on DocTypes. \ No newline at end of file diff --git a/xhiveframework/core/page/permission_manager/__init__.py b/xhiveframework/core/page/permission_manager/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/page/permission_manager/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/page/permission_manager/permission_manager.css b/xhiveframework/core/page/permission_manager/permission_manager.css new file mode 100644 index 0000000..fec486a --- /dev/null +++ b/xhiveframework/core/page/permission_manager/permission_manager.css @@ -0,0 +1,51 @@ +.table { + margin-bottom: 0px; + margin-top: 0px; + border-radius: var(--border-radius-md); +} + +thead { + border: none; + background-color: var(--control-bg); + border-radius: var(--border-radius-md); +} + +thead > tr { + border-radius: var(--border-radius-md); +} + +thead > tr > th:first-child { + border-radius: var(--border-radius-md) 0 0 var(--border-radius-md); +} +thead > tr > th:last-child { + border-radius: 0 var(--border-radius-md) var(--border-radius-md) 0; +} + +/* Space between thead and tbody */ +/* tbody:before { + content: "@"; + display: block; + line-height: var(--margin-md); + text-indent: -99999px; +} */ + +td[data-fieldname="permissions"] > .row > .col-md-4 { + margin-bottom: var(--margin-sm); +} + +tbody > tr { + border-top: 1px solid var(--border-color); +} + +tbody > tr:first-child { + border-top: none; +} + +button.btn-remove-perm { + box-shadow: none; + padding: var(--padding-xs) var(--padding-xs); +} + +button.btn-remove-perm > svg > use { + stroke: var(--white); +} diff --git a/xhiveframework/core/page/permission_manager/permission_manager.js b/xhiveframework/core/page/permission_manager/permission_manager.js new file mode 100644 index 0000000..35bec88 --- /dev/null +++ b/xhiveframework/core/page/permission_manager/permission_manager.js @@ -0,0 +1,522 @@ +xhiveframework.pages["permission-manager"].on_page_load = (wrapper) => { + let page = xhiveframework.ui.make_app_page({ + parent: wrapper, + title: __("Role Permissions Manager"), + card_layout: true, + single_column: true, + }); + + xhiveframework.breadcrumbs.add("Setup"); + + $("
    ").appendTo( + page.main + ); + $(xhiveframework.render_template("permission_manager_help", {})).appendTo(page.main); + wrapper.permission_engine = new xhiveframework.PermissionEngine(wrapper); +}; + +xhiveframework.pages["permission-manager"].refresh = function (wrapper) { + wrapper.permission_engine.set_from_route(); +}; + +xhiveframework.PermissionEngine = class PermissionEngine { + constructor(wrapper) { + this.wrapper = wrapper; + this.page = wrapper.page; + this.body = $(this.wrapper).find(".perm-engine"); + this.make(); + this.refresh(); + this.add_check_events(); + } + + make() { + this.make_reset_button(); + xhiveframework + .call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "get_roles_and_doctypes", + }) + .then((res) => { + this.options = res.message; + this.setup_page(); + }); + } + + setup_page() { + this.doctype_select = this.wrapper.page.add_field({ + fieldname: "doctype_select", + label: __("Document Type"), + fieldtype: "Link", + options: "DocType", + change: function () { + xhiveframework.set_route("permission-manager", this.get_value()); + }, + }); + + this.role_select = this.wrapper.page.add_field({ + fieldname: "role_select", + label: __("Roles"), + fieldtype: "Link", + options: "Role", + change: () => this.refresh(), + }); + + this.page.add_inner_button(__("Set User Permissions"), () => { + return xhiveframework.set_route("List", "User Permission"); + }); + this.set_from_route(); + } + + set_from_route() { + if (!this.doctype_select) { + // selects not yet loaded, call again after a bit + setTimeout(() => { + this.set_from_route(); + }, 500); + return; + } + if (xhiveframework.get_route()[1]) { + this.doctype_select.set_value(xhiveframework.get_route()[1]); + } else if (xhiveframework.route_options) { + if (xhiveframework.route_options.doctype) { + this.doctype_select.set_value(xhiveframework.route_options.doctype); + } + if (xhiveframework.route_options.role) { + this.role_select.set_value(xhiveframework.route_options.role); + } + xhiveframework.route_options = null; + } + this.refresh(); + } + + get_standard_permissions(callback) { + let doctype = this.get_doctype(); + if (doctype) { + return xhiveframework.call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "get_standard_permissions", + args: { doctype: doctype }, + callback: callback, + }); + } + return false; + } + + reset_std_permissions(data) { + let doctype = this.get_doctype(); + let d = xhiveframework.confirm(__("Reset Permissions for {0}?", [doctype]), () => { + return xhiveframework + .call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "reset", + args: { doctype }, + }) + .then(() => { + this.refresh(); + }); + }); + + // show standard permissions + let $d = $(d.wrapper) + .find(".xhiveframework-confirm-message") + .append("
    Standard Permissions:

    "); + let $wrapper = $("

    ").appendTo($d); + data.message.forEach((d) => { + let rights = this.rights + .filter((r) => d[r]) + .map((r) => { + return __(toTitle(xhiveframework.unscrub(r))); + }); + + d.rights = rights.join(", "); + + $wrapper.append(`
    \ +
    ${d.role}, Level ${d.permlevel || 0}
    \ +
    ${d.rights}
    \ +

    `); + }); + } + + get_doctype() { + return this.doctype_select.get_value(); + } + + get_role() { + return this.role_select.get_value(); + } + + set_empty_message(message) { + this.body.html(` +
    +

    + ${message} +

    +
    `); + } + + refresh() { + this.page.clear_secondary_action(); + this.page.clear_primary_action(); + + if (!this.doctype_select) { + return this.set_empty_message(__("Loading")); + } + + let doctype = this.get_doctype(); + let role = this.get_role(); + + if (!doctype && !role) { + return this.set_empty_message(__("Select Document Type or Role to start.")); + } + + // get permissions + xhiveframework + .call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "get_permissions", + args: { doctype, role }, + }) + .then((r) => { + this.render(r.message); + }); + } + + render(perm_list) { + this.body.empty(); + this.perm_list = perm_list || []; + if (!this.perm_list.length) { + this.set_empty_message(__("No Permissions set for this criteria.")); + } else { + this.show_permission_table(this.perm_list); + } + this.show_add_rule(); + this.get_doctype() && this.make_reset_button(); + } + + show_permission_table(perm_list) { + this.table = $( + "
    \ +
    {{ __("Table Field") }}{{ __("Row #") }}{{ __("Property") }}{{ __("Original Value") }}{{ __("New Value") }}
    {{ xhiveframework.meta.get_label(doc.ref_doctype, table_info[0]) }}{{ table_info[1] }}{{ item[0] }}{{ xhiveframework.utils.escape_html(item[1]) }}{{ xhiveframework.utils.escape_html(item[2]) }}
    \ + \ + \ +
    \ +
    " + ).appendTo(this.body); + + const table_columns = [ + [__("Document Type"), 150], + [__("Role"), 170], + [__("Level"), 40], + [__("Permissions"), 350], + ["", 40], + ]; + + table_columns.forEach((col) => { + $("") + .html(col[0]) + .css("width", col[1] + "px") + .appendTo(this.table.find("thead tr")); + }); + + perm_list.forEach((d) => { + if (d.parent === "DocType") { + return; + } + + if (!d.permlevel) d.permlevel = 0; + + let row = $("").appendTo(this.table.find("tbody")); + this.add_cell(row, d, "parent"); + let role_cell = this.add_cell(row, d, "role"); + + this.set_show_users(role_cell, d.role); + + if (d.permlevel === 0) { + // this.setup_user_permissions(d, role_cell); + this.setup_if_owner(d, role_cell); + } + + let cell = this.add_cell(row, d, "permlevel"); + + if (d.permlevel == 0) { + cell.css("font-weight", "bold"); + } + + let perm_cell = this.add_cell(row, d, "permissions"); + let perm_container = $("
    ").appendTo(perm_cell); + + this.rights.forEach((r) => { + if (!d.is_submittable && ["submit", "cancel", "amend"].includes(r)) return; + if (d.in_create && ["create", "delete"].includes(r)) return; + this.add_check(perm_container, d, r); + + if (d.if_owner && r == "report") { + perm_container.find("div[data-fieldname='report']").toggle(false); + } + }); + + // buttons + this.add_delete_button(row, d); + }); + } + + add_cell(row, d, fieldname) { + return $("") + .appendTo(row) + .attr("data-fieldname", fieldname) + .addClass("pt-4") + .html(__(d[fieldname])); + } + + add_check(cell, d, fieldname, label, description = "") { + if (!label) label = toTitle(fieldname.replace(/_/g, " ")); + if (d.permlevel > 0 && ["read", "write"].indexOf(fieldname) == -1) { + return; + } + + let checkbox = $( + `
    +
    + +

    ${__(description)}

    +
    +
    ` + ) + .appendTo(cell) + .attr("data-fieldname", fieldname); + + checkbox + .find("input") + .prop("checked", d[fieldname] ? true : false) + .attr("data-ptype", fieldname) + .attr("data-role", d.role) + .attr("data-permlevel", d.permlevel) + .attr("data-if_owner", d.if_owner) + .attr("data-doctype", d.parent); + + checkbox.find("label").css("text-transform", "capitalize"); + + return checkbox; + } + + setup_if_owner(d, role_cell) { + this.add_check(role_cell, d, "if_owner", "Only if Creator") + .removeClass("col-md-4") + .css({ "margin-top": "15px" }); + } + + get rights() { + return [ + "select", + "read", + "write", + "create", + "delete", + "submit", + "cancel", + "amend", + "print", + "email", + "report", + "import", + "export", + "share", + ]; + } + + set_show_users(cell, role) { + cell.html("" + __(role) + "") + .find("a") + .attr("data-role", role) + .click(function () { + let role = $(this).attr("data-role"); + xhiveframework.call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "get_users_with_role", + args: { + role: role, + }, + callback: function (r) { + r.message = $.map(r.message, function (p) { + return $.format('{1}', [p, p]); + }); + xhiveframework.msgprint( + __("Users with role {0}:", [__(role)]) + + "
    " + + r.message.join("
    ") + ); + }, + }); + return false; + }); + } + + add_delete_button(row, d) { + $( + `` + ) + .appendTo($(``).appendTo(row)) + .attr("data-doctype", d.parent) + .attr("data-role", d.role) + .attr("data-permlevel", d.permlevel) + .on("click", () => { + return xhiveframework.call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "remove", + args: { + doctype: d.parent, + role: d.role, + permlevel: d.permlevel, + if_owner: d.if_owner, + }, + callback: (r) => { + if (r.exc) { + xhiveframework.msgprint(__("Did not remove")); + } else { + this.refresh(); + } + }, + }); + }); + } + + add_check_events() { + let me = this; + this.body.on("click", ".show-user-permissions", () => { + xhiveframework.route_options = { allow: this.get_doctype() || "" }; + xhiveframework.set_route("List", "User Permission"); + }); + + this.body.on("click", "input[type='checkbox']", function () { + xhiveframework.dom.freeze(); + let chk = $(this); + let args = { + role: chk.attr("data-role"), + permlevel: chk.attr("data-permlevel"), + doctype: chk.attr("data-doctype"), + ptype: chk.attr("data-ptype"), + value: chk.prop("checked") ? 1 : 0, + if_owner: chk.attr("data-if_owner"), + }; + return xhiveframework.call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "update", + args: args, + callback: (r) => { + xhiveframework.dom.unfreeze(); + if (r.exc) { + // exception: reverse + chk.prop("checked", !chk.prop("checked")); + } else { + me.get_perm(args.role)[args.ptype] = args.value; + + if (args.ptype == "if_owner") { + let report_checkbox = chk + .closest("div.row") + .find("div[data-fieldname='report']"); + report_checkbox.toggle(!args.value); + } + } + }, + }); + }); + } + + show_add_rule() { + this.page.set_primary_action( + __("Add A New Rule"), + () => { + let d = new xhiveframework.ui.Dialog({ + title: __("Add New Permission Rule"), + fields: [ + { + fieldtype: "Select", + label: __("Document Type"), + options: this.options.doctypes, + reqd: 1, + fieldname: "parent", + }, + { + fieldtype: "Select", + label: __("Role"), + options: this.options.roles, + reqd: 1, + fieldname: "role", + }, + { + fieldtype: "Select", + label: __("Permission Level"), + options: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + reqd: 1, + fieldname: "permlevel", + description: __( + "Level 0 is for document level permissions, higher levels for field level permissions." + ), + }, + ], + }); + if (this.get_doctype()) { + d.set_value("parent", this.get_doctype()); + d.get_input("parent").prop("disabled", true); + } + if (this.get_role()) { + d.set_value("role", this.get_role()); + d.get_input("role").prop("disabled", true); + } + d.set_value("permlevel", "0"); + d.set_primary_action(__("Add"), () => { + let args = d.get_values(); + if (!args) { + return; + } + xhiveframework.call({ + module: "xhiveframework.core", + page: "permission_manager", + method: "add", + args: args, + callback: (r) => { + if (r.exc) { + xhiveframework.msgprint(__("Did not add")); + } else { + this.refresh(); + } + }, + }); + d.hide(); + }); + d.show(); + }, + "small-add" + ); + } + + make_reset_button() { + this.page.set_secondary_action(__("Restore Original Permissions"), () => { + this.get_standard_permissions((data) => { + this.reset_std_permissions(data); + }); + }); + } + + get_perm(role) { + return $.map(this.perm_list, function (d) { + if (d.role == role) return d; + })[0]; + } + + get_link_fields(doctype) { + return xhiveframework.get_children("DocType", doctype, "fields", { + fieldtype: "Link", + options: ["not in", ["User", "[Select]"]], + }); + } +}; diff --git a/xhiveframework/core/page/permission_manager/permission_manager.json b/xhiveframework/core/page/permission_manager/permission_manager.json new file mode 100644 index 0000000..0af33ca --- /dev/null +++ b/xhiveframework/core/page/permission_manager/permission_manager.json @@ -0,0 +1,20 @@ +{ + "creation": "2013-01-01 11:00:01.000000", + "docstatus": 0, + "doctype": "Page", + "icon": "fa fa-lock", + "idx": 1, + "modified": "2013-07-11 14:43:43.000000", + "modified_by": "Administrator", + "module": "Core", + "name": "permission-manager", + "owner": "Administrator", + "page_name": "permission-manager", + "roles": [ + { + "role": "System Manager" + } + ], + "standard": "Yes", + "title": "Role Permissions Manager" +} \ No newline at end of file diff --git a/xhiveframework/core/page/permission_manager/permission_manager.py b/xhiveframework/core/page/permission_manager/permission_manager.py new file mode 100644 index 0000000..deccda0 --- /dev/null +++ b/xhiveframework/core/page/permission_manager/permission_manager.py @@ -0,0 +1,183 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + + +import xhiveframework +import xhiveframework.defaults +from xhiveframework import _ +from xhiveframework.core.doctype.doctype.doctype import ( + clear_permissions_cache, + validate_permissions_for_doctype, +) +from xhiveframework.exceptions import DoesNotExistError +from xhiveframework.modules.import_file import get_file_path, read_doc_from_file +from xhiveframework.permissions import ( + AUTOMATIC_ROLES, + add_permission, + get_all_perms, + get_linked_doctypes, + reset_perms, + setup_custom_perms, + update_permission_property, +) +from xhiveframework.utils.user import get_users_with_role as _get_user_with_role + +not_allowed_in_permission_manager = ["DocType", "Patch Log", "Module Def", "Transaction Log"] + + +@xhiveframework.whitelist() +def get_roles_and_doctypes(): + xhiveframework.only_for("System Manager") + + active_domains = xhiveframework.get_active_domains() + + doctypes = xhiveframework.get_all( + "DocType", + filters={ + "istable": 0, + "name": ("not in", ",".join(not_allowed_in_permission_manager)), + }, + or_filters={"ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains)}, + fields=["name"], + ) + + restricted_roles = ["Administrator"] + if xhiveframework.session.user != "Administrator": + custom_user_type_roles = xhiveframework.get_all("User Type", filters={"is_standard": 0}, fields=["role"]) + restricted_roles.extend(row.role for row in custom_user_type_roles) + restricted_roles.extend(AUTOMATIC_ROLES) + + roles = xhiveframework.get_all( + "Role", + filters={ + "name": ("not in", restricted_roles), + "disabled": 0, + }, + or_filters={"ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains)}, + fields=["name"], + ) + + doctypes_list = [{"label": _(d.get("name")), "value": d.get("name")} for d in doctypes] + roles_list = [{"label": _(d.get("name")), "value": d.get("name")} for d in roles] + + return { + "doctypes": sorted(doctypes_list, key=lambda d: d["label"].casefold()), + "roles": sorted(roles_list, key=lambda d: d["label"].casefold()), + } + + +@xhiveframework.whitelist() +def get_permissions(doctype: str | None = None, role: str | None = None): + xhiveframework.only_for("System Manager") + + if role: + out = get_all_perms(role) + if doctype: + out = [p for p in out if p.parent == doctype] + + else: + filters = {"parent": doctype} + if xhiveframework.session.user != "Administrator": + custom_roles = xhiveframework.get_all("Role", filters={"is_custom": 1}, pluck="name") + filters["role"] = ["not in", custom_roles] + + out = xhiveframework.get_all("Custom DocPerm", fields="*", filters=filters, order_by="permlevel") + if not out: + out = xhiveframework.get_all("DocPerm", fields="*", filters=filters, order_by="permlevel") + + linked_doctypes = {} + for d in out: + if d.parent not in linked_doctypes: + try: + linked_doctypes[d.parent] = get_linked_doctypes(d.parent) + except DoesNotExistError: + # exclude & continue if linked doctype is not found + xhiveframework.clear_last_message() + continue + d.linked_doctypes = linked_doctypes[d.parent] + if meta := xhiveframework.get_meta(d.parent): + d.is_submittable = meta.is_submittable + d.in_create = meta.in_create + + return out + + +@xhiveframework.whitelist() +def add(parent, role, permlevel): + xhiveframework.only_for("System Manager") + add_permission(parent, role, permlevel) + + +@xhiveframework.whitelist() +def update(doctype, role, permlevel, ptype, value=None, if_owner=0): + """Update role permission params + + Args: + doctype (str): Name of the DocType to update params for + role (str): Role to be updated for, eg "Website Manager". + permlevel (int): perm level the provided rule applies to + ptype (str): permission type, example "read", "delete", etc. + value (None, optional): value for ptype, None indicates False + + Returns: + str: Refresh flag is permission is updated successfully + """ + + def clear_cache(): + xhiveframework.clear_cache(doctype=doctype) + + xhiveframework.only_for("System Manager") + + if ptype == "report" and value == "1" and if_owner == "1": + xhiveframework.throw(_("Cannot set 'Report' permission if 'Only If Creator' permission is set")) + + out = update_permission_property(doctype, role, permlevel, ptype, value, if_owner=if_owner) + + if ptype == "if_owner" and value == "1": + update_permission_property(doctype, role, permlevel, "report", "0", if_owner=value) + + xhiveframework.db.after_commit.add(clear_cache) + + return "refresh" if out else None + + +@xhiveframework.whitelist() +def remove(doctype, role, permlevel, if_owner=0): + xhiveframework.only_for("System Manager") + setup_custom_perms(doctype) + + xhiveframework.db.delete( + "Custom DocPerm", + {"parent": doctype, "role": role, "permlevel": permlevel, "if_owner": if_owner}, + ) + + if not xhiveframework.get_all("Custom DocPerm", {"parent": doctype}): + xhiveframework.throw(_("There must be atleast one permission rule."), title=_("Cannot Remove")) + + validate_permissions_for_doctype(doctype, for_remove=True, alert=True) + + +@xhiveframework.whitelist() +def reset(doctype): + xhiveframework.only_for("System Manager") + reset_perms(doctype) + clear_permissions_cache(doctype) + + +@xhiveframework.whitelist() +def get_users_with_role(role): + xhiveframework.only_for("System Manager") + return _get_user_with_role(role) + + +@xhiveframework.whitelist() +def get_standard_permissions(doctype): + xhiveframework.only_for("System Manager") + meta = xhiveframework.get_meta(doctype) + if meta.custom: + doc = xhiveframework.get_doc("DocType", doctype) + return [p.as_dict() for p in doc.permissions] + else: + # also used to setup permissions via patch + path = get_file_path(meta.module, "DocType", doctype) + return read_doc_from_file(path).get("permissions") diff --git a/xhiveframework/core/page/permission_manager/permission_manager_help.html b/xhiveframework/core/page/permission_manager/permission_manager_help.html new file mode 100644 index 0000000..15ea59f --- /dev/null +++ b/xhiveframework/core/page/permission_manager/permission_manager_help.html @@ -0,0 +1,41 @@ +
    +
    +

    {%= __("Quick Help for Setting Permissions") %}:

    +
      +
    1. {%= __("Permissions are set on Roles and Document Types (called DocTypes) by setting rights like Read, Write, Create, Delete, Submit, Cancel, Amend, Report, Import, Export, Print, Email and Set User Permissions.") %}
    2. +
    3. {%= __("Permissions get applied on Users based on what Roles they are assigned.") %}
    4. +
    5. {%= __("Roles can be set for users from their User page.") %} + {%= __("Setup > User") %}
    6. +
    7. {%= __("The system provides many pre-defined roles. You can add new roles to set finer permissions.") %} {%= __("Add a New Role") %}
    8. +
    9. {%= __("Permissions are automatically applied to Standard Reports and searches.") %}
    10. +
    11. {%= __("As a best practice, do not assign the same set of permission rule to different Roles. Instead, set multiple Roles to the same User.") %}
    12. +
    +
    +

    {%= __("Meaning of Submit, Cancel, Amend") %}:

    +
      +
    1. {%= __("Certain documents, like an Invoice, should not be changed once final. The final state for such documents is called Submitted. You can restrict which roles can Submit.") %}
    2. +
    3. {%= __("You can change Submitted documents by cancelling them and then, amending them.") %}
    4. +
    5. {%= __("When you Amend a document after Cancel and save it, it will get a new number that is a version of the old number.") %}
    6. +
    7. {%= __("For example if you cancel and amend INV004 it will become a new document INV004-1. This helps you to keep track of each amendment.") %}
    8. +
    +
    +

    {%= __("Permission Levels") %}:

    +
      +
    1. {%= __("Permissions at level 0 are Document Level permissions, i.e. they are primary for access to the document.") %}
    2. +
    3. {%= __("If a Role does not have access at Level 0, then higher levels are meaningless.") %}
    4. +
    5. {%= __("Permissions at higher levels are Field Level permissions. All Fields have a Permission Level set against them and the rules defined at that permissions apply to the field. This is useful in case you want to hide or make certain field read-only for certain Roles.") %}
    6. +
    7. {%= __("You can use Customize Form to set levels on fields.") %} {%= __("Setup > Customize Form") %}
    8. +
    +
    +

    {%= __("User Permissions") %}:

    +
      +
    1. {%= __("User Permissions are used to limit users to specific records.") %} + {%= __("Setup > User Permissions") %}
    2. +
    3. {%= __("Select Document Types to set which User Permissions are used to limit access.") %}
    4. +
    5. {%= __("Once you have set this, the users will only be able access documents (eg. Blog Post) where the link exists (eg. Blogger).") %}
    6. +
    7. {%= __("Apart from System Manager, roles with Set User Permissions right can set permissions for other users for that Document Type.") %}
    8. +
    +

    {%= __("If these instructions where not helpful, please add in your suggestions on GitHub Issues.") %} + {%= __("Submit an Issue") %} +

    +
    diff --git a/xhiveframework/core/report/__init__.py b/xhiveframework/core/report/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/core/report/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/core/report/database_storage_usage_by_tables/__init__.py b/xhiveframework/core/report/database_storage_usage_by_tables/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.js b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.js new file mode 100644 index 0000000..10ea12f --- /dev/null +++ b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.js @@ -0,0 +1,6 @@ +// Copyright (c) 2022, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["Database Storage Usage By Tables"] = { + filters: [], +}; diff --git a/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.json b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.json new file mode 100644 index 0000000..773cb77 --- /dev/null +++ b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.json @@ -0,0 +1,27 @@ +{ + "add_total_row": 1, + "columns": [], + "creation": "2022-10-19 02:25:24.326791", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "filters": [], + "idx": 0, + "is_standard": "Yes", + "modified": "2022-10-19 02:59:00.365307", + "modified_by": "Administrator", + "module": "Core", + "name": "Database Storage Usage By Tables", + "owner": "Administrator", + "prepared_report": 0, + "query": "", + "ref_doctype": "Error Log", + "report_name": "Database Storage Usage By Tables", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + } + ] +} diff --git a/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.py b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.py new file mode 100644 index 0000000..d00b21c --- /dev/null +++ b/xhiveframework/core/report/database_storage_usage_by_tables/database_storage_usage_by_tables.py @@ -0,0 +1,40 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework + +COLUMNS = [ + {"label": "Table", "fieldname": "table", "fieldtype": "Data", "width": 200}, + {"label": "Size (MB)", "fieldname": "size", "fieldtype": "Float"}, + {"label": "Data (MB)", "fieldname": "data_size", "fieldtype": "Float"}, + {"label": "Index (MB)", "fieldname": "index_size", "fieldtype": "Float"}, +] + + +def execute(filters=None): + xhiveframework.only_for("System Manager") + + data = xhiveframework.db.multisql( + { + "mariadb": """ + SELECT table_name AS `table`, + round(((data_length + index_length) / 1024 / 1024), 2) `size`, + round((data_length / 1024 / 1024), 2) as data_size, + round((index_length / 1024 / 1024), 2) as index_size + FROM information_schema.TABLES + ORDER BY (data_length + index_length) DESC; + """, + "postgres": """ + SELECT + table_name as "table", + round(pg_total_relation_size(quote_ident(table_name)) / 1024 / 1024, 2) as "size", + round(pg_relation_size(quote_ident(table_name)) / 1024 / 1024, 2) as "data_size", + round(pg_indexes_size(quote_ident(table_name)) / 1024 / 1024, 2) as "index_size" + FROM information_schema.tables + WHERE table_schema = 'public' + ORDER BY 2 DESC; + """, + }, + as_dict=1, + ) + return COLUMNS, data diff --git a/xhiveframework/core/report/database_storage_usage_by_tables/test_database_storage_usage_by_tables.py b/xhiveframework/core/report/database_storage_usage_by_tables/test_database_storage_usage_by_tables.py new file mode 100644 index 0000000..88d6bbd --- /dev/null +++ b/xhiveframework/core/report/database_storage_usage_by_tables/test_database_storage_usage_by_tables.py @@ -0,0 +1,15 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + + +from xhiveframework.core.report.database_storage_usage_by_tables.database_storage_usage_by_tables import ( + execute, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDBUsageReport(XhiveFrameworkTestCase): + def test_basic_query(self): + _, data = execute() + tables = [d.table for d in data] + self.assertFalse({"tabUser", "tabDocField"}.difference(tables)) diff --git a/xhiveframework/core/report/document_share_report/__init__.py b/xhiveframework/core/report/document_share_report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/report/document_share_report/document_share_report.json b/xhiveframework/core/report/document_share_report/document_share_report.json new file mode 100644 index 0000000..db2f2b6 --- /dev/null +++ b/xhiveframework/core/report/document_share_report/document_share_report.json @@ -0,0 +1,24 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "creation": "2015-02-05 06:01:35.060098", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 2, + "is_standard": "Yes", + "json": "{\"add_total_row\": 0, \"sort_by\": \"DocShare.modified\", \"sort_order\": \"desc\", \"sort_by_next\": null, \"filters\": [], \"sort_order_next\": \"desc\", \"columns\": [[\"name\", \"DocShare\"], [\"user\", \"DocShare\"], [\"share_doctype\", \"DocShare\"], [\"share_name\", \"DocShare\"], [\"read\", \"DocShare\"], [\"write\", \"DocShare\"], [\"share\", \"DocShare\"]]}", + "modified": "2017-02-24 20:01:16.232286", + "modified_by": "Administrator", + "module": "Core", + "name": "Document Share Report", + "owner": "Administrator", + "ref_doctype": "DocShare", + "report_name": "Document Share Report", + "report_type": "Report Builder", + "roles": [ + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/core/report/permitted_documents_for_user/__init__.py b/xhiveframework/core/report/permitted_documents_for_user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.js b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.js new file mode 100644 index 0000000..1cad077 --- /dev/null +++ b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.js @@ -0,0 +1,34 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.query_reports["Permitted Documents For User"] = { + filters: [ + { + fieldname: "user", + label: __("User"), + fieldtype: "Link", + options: "User", + reqd: 1, + }, + { + fieldname: "doctype", + label: __("DocType"), + fieldtype: "Link", + options: "DocType", + reqd: 1, + get_query: function () { + return { + query: "xhiveframework.core.report.permitted_documents_for_user.permitted_documents_for_user.query_doctypes", + filters: { + user: xhiveframework.query_report.get_filter_value("user"), + }, + }; + }, + }, + { + fieldname: "show_permissions", + label: __("Show Permissions"), + fieldtype: "Check", + }, + ], +}; diff --git a/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.json b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.json new file mode 100644 index 0000000..bf384ea --- /dev/null +++ b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.json @@ -0,0 +1,23 @@ +{ + "add_total_row": 0, + "creation": "2014-06-03 05:20:35.218263", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 3, + "is_standard": "Yes", + "modified": "2018-06-29 15:46:42.805039", + "modified_by": "Administrator", + "module": "Core", + "name": "Permitted Documents For User", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "User", + "report_name": "Permitted Documents For User", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.py b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.py new file mode 100644 index 0000000..5365717 --- /dev/null +++ b/xhiveframework/core/report/permitted_documents_for_user/permitted_documents_for_user.py @@ -0,0 +1,60 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +import xhiveframework.utils.user +from xhiveframework.model import data_fieldtypes +from xhiveframework.permissions import rights + + +def execute(filters=None): + xhiveframework.only_for("System Manager") + + user, doctype, show_permissions = ( + filters.get("user"), + filters.get("doctype"), + filters.get("show_permissions"), + ) + + columns, fields = get_columns_and_fields(doctype) + data = xhiveframework.get_list(doctype, fields=fields, as_list=True, user=user) + + if show_permissions: + columns = columns + [xhiveframework.unscrub(right) + ":Check:80" for right in rights] + data = list(data) + for i, doc in enumerate(data): + permission = xhiveframework.permissions.get_doc_permissions(xhiveframework.get_doc(doctype, doc[0]), user) + data[i] = doc + tuple(permission.get(right) for right in rights) + + return columns, data + + +def get_columns_and_fields(doctype): + columns = [f"Name:Link/{doctype}:200"] + fields = ["name"] + for df in xhiveframework.get_meta(doctype).fields: + if df.in_list_view and df.fieldtype in data_fieldtypes: + fields.append(f"`{df.fieldname}`") + fieldtype = f"Link/{df.options}" if df.fieldtype == "Link" else df.fieldtype + columns.append(f"{df.label}:{fieldtype}:{df.width or 100}") + + return columns, fields + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def query_doctypes(doctype, txt, searchfield, start, page_len, filters): + user = filters.get("user") + user_perms = xhiveframework.utils.user.UserPermissions(user) + user_perms.build_permissions() + can_read = user_perms.can_read # Does not include child tables + include_single_doctypes = filters.get("include_single_doctypes") + + single_doctypes = [d[0] for d in xhiveframework.db.get_values("DocType", {"issingle": 1})] + + return [ + [dt] + for dt in can_read + if txt.lower().replace("%", "") in xhiveframework._(dt).lower() + and (include_single_doctypes or dt not in single_doctypes) + ] diff --git a/xhiveframework/core/report/transaction_log_report/__init__.py b/xhiveframework/core/report/transaction_log_report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/report/transaction_log_report/transaction_log_report.js b/xhiveframework/core/report/transaction_log_report/transaction_log_report.js new file mode 100644 index 0000000..064b95d --- /dev/null +++ b/xhiveframework/core/report/transaction_log_report/transaction_log_report.js @@ -0,0 +1,10 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["Transaction Log Report"] = { + onload: function (query_report) { + query_report.add_make_chart_button = function () { + // + }; + }, +}; diff --git a/xhiveframework/core/report/transaction_log_report/transaction_log_report.json b/xhiveframework/core/report/transaction_log_report/transaction_log_report.json new file mode 100644 index 0000000..6d6fb78 --- /dev/null +++ b/xhiveframework/core/report/transaction_log_report/transaction_log_report.json @@ -0,0 +1,26 @@ +{ + "add_total_row": 0, + "creation": "2018-03-15 18:37:48.783779", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "modified": "2018-12-27 18:10:29.785415", + "modified_by": "Administrator", + "module": "Core", + "name": "Transaction Log Report", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Transaction Log", + "report_name": "Transaction Log Report", + "report_type": "Script Report", + "roles": [ + { + "role": "Administrator" + }, + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/core/report/transaction_log_report/transaction_log_report.py b/xhiveframework/core/report/transaction_log_report/transaction_log_report.py new file mode 100644 index 0000000..d55af29 --- /dev/null +++ b/xhiveframework/core/report/transaction_log_report/transaction_log_report.py @@ -0,0 +1,117 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import hashlib + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils import format_datetime + + +def execute(filters=None): + columns, data = get_columns(filters), get_data(filters) + + return columns, data + + +def get_data(filters=None): + result = [] + logs = xhiveframework.get_all("Transaction Log", fields=["*"], order_by="creation desc") + + for l in logs: + row_index = int(l.row_index) + if row_index > 1: + previous_hash = xhiveframework.get_all( + "Transaction Log", + fields=["chaining_hash"], + filters={"row_index": row_index - 1}, + ) + if not previous_hash: + integrity = False + else: + integrity = check_data_integrity( + l.chaining_hash, l.transaction_hash, l.previous_hash, previous_hash[0][0] + ) + + result.append( + [ + _(str(integrity)), + _(l.reference_doctype), + l.document_name, + l.owner, + l.modified_by, + format_datetime(l.timestamp, "YYYYMMDDHHmmss"), + ] + ) + else: + result.append( + [ + _("First Transaction"), + _(l.reference_doctype), + l.document_name, + l.owner, + l.modified_by, + format_datetime(l.timestamp, "YYYYMMDDHHmmss"), + ] + ) + + return result + + +def check_data_integrity(chaining_hash, transaction_hash, registered_previous_hash, previous_hash): + if registered_previous_hash != previous_hash: + return False + + calculated_chaining_hash = calculate_chain(transaction_hash, previous_hash) + + if calculated_chaining_hash != chaining_hash: + return False + else: + return True + + +def calculate_chain(transaction_hash, previous_hash): + sha = hashlib.sha256() + sha.update(str(transaction_hash) + str(previous_hash)) + return sha.hexdigest() + + +def get_columns(filters=None): + return [ + { + "label": _("Chain Integrity"), + "fieldname": "chain_integrity", + "fieldtype": "Data", + "width": 150, + }, + { + "label": _("Reference Doctype"), + "fieldname": "reference_doctype", + "fieldtype": "Data", + "width": 150, + }, + { + "label": _("Reference Name"), + "fieldname": "reference_name", + "fieldtype": "Data", + "width": 150, + }, + { + "label": _("Owner"), + "fieldname": "owner", + "fieldtype": "Data", + "width": 100, + }, + { + "label": _("Modified By"), + "fieldname": "modified_by", + "fieldtype": "Data", + "width": 100, + }, + { + "label": _("Timestamp"), + "fieldname": "timestamp", + "fieldtype": "Data", + "width": 100, + }, + ] diff --git a/xhiveframework/core/utils.py b/xhiveframework/core/utils.py new file mode 100644 index 0000000..1024bb7 --- /dev/null +++ b/xhiveframework/core/utils.py @@ -0,0 +1,91 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from markdownify import markdownify as md + +import xhiveframework + + +def get_parent_doc(doc): + """Return document of `reference_doctype`, `reference_doctype`.""" + if not getattr(doc, "parent_doc", None): + if doc.reference_doctype and doc.reference_name: + doc.parent_doc = xhiveframework.get_doc(doc.reference_doctype, doc.reference_name) + else: + doc.parent_doc = None + return doc.parent_doc + + +def set_timeline_doc(doc): + """Set timeline_doctype and timeline_name""" + parent_doc = get_parent_doc(doc) + if (doc.timeline_doctype and doc.timeline_name) or not parent_doc: + return + + timeline_field = parent_doc.meta.timeline_field + if not timeline_field: + return + + doctype = parent_doc.meta.get_link_doctype(timeline_field) + name = parent_doc.get(timeline_field) + + if doctype and name: + doc.timeline_doctype = doctype + doc.timeline_name = name + + else: + return + + +def find(list_of_dict, match_function): + """Returns a dict in a list of dicts on matching the conditions + provided in match function + + Usage: + list_of_dict = [{'name': 'Suraj'}, {'name': 'Aditya'}] + + required_dict = find(list_of_dict, lambda d: d['name'] == 'Aditya') + """ + + for entry in list_of_dict: + if match_function(entry): + return entry + return None + + +def find_all(list_of_dict, match_function): + """Returns all matching dicts in a list of dicts. + Uses matching function to filter out the dicts + + Usage: + colored_shapes = [ + {'color': 'red', 'shape': 'square'}, + {'color': 'red', 'shape': 'circle'}, + {'color': 'blue', 'shape': 'triangle'} + ] + + red_shapes = find_all(colored_shapes, lambda d: d['color'] == 'red') + """ + return [entry for entry in list_of_dict if match_function(entry)] + + +def ljust_list(_list, length, fill_word=None): + """ + Similar to ljust but for list. + + Usage: + $ ljust_list([1, 2, 3], 5) + > [1, 2, 3, None, None] + """ + # make a copy to avoid mutation of passed list + _list = list(_list) + fill_length = length - len(_list) + if fill_length > 0: + _list.extend([fill_word] * fill_length) + + return _list + + +def html2text(html, strip_links=False, wrap=True): + strip = ["a"] if strip_links else None + return md(html, heading_style="ATX", strip=strip, wrap=wrap) diff --git a/xhiveframework/core/web_form/__init__.py b/xhiveframework/core/web_form/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/web_form/edit_profile/__init__.py b/xhiveframework/core/web_form/edit_profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/core/web_form/edit_profile/edit_profile.js b/xhiveframework/core/web_form/edit_profile/edit_profile.js new file mode 100644 index 0000000..64d0606 --- /dev/null +++ b/xhiveframework/core/web_form/edit_profile/edit_profile.js @@ -0,0 +1,3 @@ +xhiveframework.ready(function () { + // bind events here +}); diff --git a/xhiveframework/core/web_form/edit_profile/edit_profile.json b/xhiveframework/core/web_form/edit_profile/edit_profile.json new file mode 100644 index 0000000..b631908 --- /dev/null +++ b/xhiveframework/core/web_form/edit_profile/edit_profile.json @@ -0,0 +1,156 @@ +{ + "allow_comments": 0, + "allow_delete": 0, + "allow_edit": 1, + "allow_incomplete": 0, + "allow_multiple": 0, + "allow_print": 0, + "apply_document_permissions": 0, + "breadcrumbs": "[{\"title\": _(\"My Account\"), \"route\": \"me\"}]", + "client_script": "xhiveframework.web_form.after_load = () => {\n if (window.location.pathname.endsWith(\"/new\") && xhiveframework.session.user) {\n let current_path = window.location.href;\n window.location.href = current_path.replace(\"/new\", \"/\" + xhiveframework.session.user);\n }\n}", + "creation": "2016-09-19 05:16:59.242754", + "doc_type": "User", + "docstatus": 0, + "doctype": "Web Form", + "idx": 0, + "introduction_text": "", + "is_standard": 1, + "list_columns": [], + "login_required": 1, + "max_attachment_size": 0, + "modified": "2023-01-18 10:26:26.766414", + "modified_by": "Administrator", + "module": "Core", + "name": "edit-profile", + "owner": "Administrator", + "published": 1, + "route": "update-profile", + "show_attachments": 0, + "show_list": 0, + "show_sidebar": 0, + "success_message": "Profile updated successfully.", + "success_url": "/me", + "title": "Update Profile", + "web_form_fields": [ + { + "allow_read_on_all_link_options": 0, + "fieldname": "first_name", + "fieldtype": "Data", + "hidden": 0, + "label": "First Name", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 1, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "middle_name", + "fieldtype": "Data", + "hidden": 0, + "label": "Middle Name (Optional)", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "last_name", + "fieldtype": "Data", + "hidden": 0, + "label": "Last Name", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "", + "fieldtype": "Column Break", + "hidden": 0, + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "description": "", + "fieldname": "user_image", + "fieldtype": "Attach Image", + "hidden": 0, + "label": "Profile Picture", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldtype": "Section Break", + "hidden": 0, + "label": "More Information", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "phone", + "fieldtype": "Data", + "hidden": 0, + "label": "Phone", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "mobile_no", + "fieldtype": "Data", + "hidden": 0, + "label": "Mobile Number", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "", + "fieldtype": "Column Break", + "hidden": 0, + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "description": "", + "fieldname": "language", + "fieldtype": "Link", + "hidden": 0, + "label": "Language", + "max_length": 0, + "max_value": 0, + "options": "Language", + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + } + ] +} diff --git a/xhiveframework/core/web_form/edit_profile/edit_profile.py b/xhiveframework/core/web_form/edit_profile/edit_profile.py new file mode 100644 index 0000000..02e3e93 --- /dev/null +++ b/xhiveframework/core/web_form/edit_profile/edit_profile.py @@ -0,0 +1,3 @@ +def get_context(context): + # do your magic here + pass diff --git a/xhiveframework/core/workspace/build/build.json b/xhiveframework/core/workspace/build/build.json new file mode 100644 index 0000000..8154259 --- /dev/null +++ b/xhiveframework/core/workspace/build/build.json @@ -0,0 +1,388 @@ +{ + "charts": [], + "content": "[{\"id\":\"5nnLaQeoFa\",\"type\":\"header\",\"data\":{\"text\":\"Get started
    \",\"col\":12}},{\"id\":\"HXRmktXYHy\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"DocType\",\"col\":3}},{\"id\":\"pYALX3MwBW\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Customize Form\",\"col\":3}},{\"id\":\"XC78DuYB65\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Report\",\"col\":3}},{\"id\":\"XPm50Ppq3J\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Client Script\",\"col\":3}},{\"id\":\"yoU6nWiT83\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Server Script\",\"col\":3}},{\"id\":\"5UgFESBY0N\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Print Format Builder\",\"col\":3}},{\"id\":\"0gE0s-S70E\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"System Settings\",\"col\":3}},{\"id\":\"62hseENHbd\",\"type\":\"spacer\",\"data\":{\"col\":12}},{\"id\":\"tOCrOgLW1G\",\"type\":\"header\",\"data\":{\"text\":\"Components to build your app\",\"col\":12}},{\"id\":\"cJ6CVsa8qW\",\"type\":\"card\",\"data\":{\"card_name\":\"Models\",\"col\":4}},{\"id\":\"MmEJpjEdGR\",\"type\":\"card\",\"data\":{\"card_name\":\"Views\",\"col\":4}},{\"id\":\"2ZdtgxQZqq\",\"type\":\"card\",\"data\":{\"card_name\":\"Customization\",\"col\":4}},{\"id\":\"NPFolijIcb\",\"type\":\"card\",\"data\":{\"card_name\":\"Scripting\",\"col\":4}},{\"id\":\"BIHjudL0T_\",\"type\":\"card\",\"data\":{\"card_name\":\"Modules\",\"col\":4}},{\"id\":\"iK3JQ9RXJE\",\"type\":\"card\",\"data\":{\"card_name\":\"Packages\",\"col\":4}},{\"id\":\"TiO9FCUUeC\",\"type\":\"card\",\"data\":{\"card_name\":\"System Logs\",\"col\":4}}]", + "creation": "2021-01-02 10:51:16.579957", + "custom_blocks": [], + "docstatus": 0, + "doctype": "Workspace", + "for_user": "", + "hide_custom": 0, + "icon": "tool", + "idx": 1, + "is_hidden": 0, + "label": "Build", + "links": [ + { + "description": "Customize properties, naming, fields and more for standard doctypes", + "hidden": 0, + "is_query_report": 0, + "label": "Customization", + "link_count": 4, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Customize Form", + "link_count": 0, + "link_to": "Customize Form", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Custom Field", + "link_count": 0, + "link_to": "Custom Field", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Custom Translation", + "link_count": 0, + "link_to": "Translation", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Navbar Settings", + "link_count": 0, + "link_to": "Navbar Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "description": "Group your custom doctypes under modules", + "hidden": 0, + "is_query_report": 0, + "label": "Modules", + "link_count": 2, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Module Def", + "link_count": 0, + "link_to": "Module Def", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Module Onboarding", + "link_count": 0, + "link_to": "Module Onboarding", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "description": "Monitor logs for errors, background jobs, communications, and user activity", + "hidden": 0, + "is_query_report": 0, + "label": "System Logs", + "link_count": 5, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Background Jobs", + "link_count": 0, + "link_to": "RQ Job", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Scheduled Jobs Logs", + "link_count": 0, + "link_to": "Scheduled Job Log", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Error Logs", + "link_count": 0, + "link_to": "Error Log", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Communication Logs", + "link_count": 0, + "link_to": "Communication", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Activity Log", + "link_count": 0, + "link_to": "Activity Log", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "description": "Packages are lightweight apps (collection of Module Defs) that can be created, imported, or released right from the UI", + "hidden": 0, + "is_query_report": 0, + "label": "Packages", + "link_count": 2, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Package", + "link_count": 0, + "link_to": "Package", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Package Import", + "link_count": 0, + "link_to": "Package Import", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "description": "Automate processes and extend standard functionality using scripts and background jobs", + "hidden": 0, + "is_query_report": 0, + "label": "Scripting", + "link_count": 3, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Server Script", + "link_count": 0, + "link_to": "Server Script", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Client Script", + "link_count": 0, + "link_to": "Client Script", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Scheduled Job Type", + "link_count": 0, + "link_to": "Scheduled Job Type", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "description": "Build your own reports, print formats, and dashboards. Create personalized workspaces for easier navigation", + "hidden": 0, + "is_query_report": 0, + "label": "Views", + "link_count": 5, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Report", + "link_count": 0, + "link_to": "Report", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Print Format", + "link_count": 0, + "link_to": "Print Format", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Workspace", + "link_count": 0, + "link_to": "Workspace", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Dashboard", + "link_count": 0, + "link_to": "Dashboard", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Dashboard Chart", + "link_count": 0, + "link_to": "Dashboard Chart", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "description": "Create new forms and views with doctypes. Set up multi-level workflows for approval", + "hidden": 0, + "is_query_report": 0, + "label": "Models", + "link_count": 2, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "DocType", + "link_count": 0, + "link_to": "DocType", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Workflow", + "link_count": 0, + "link_to": "Workflow", + "link_type": "DocType", + "onboard": 0, + "only_for": "", + "type": "Link" + } + ], + "modified": "2024-01-24 12:27:44.769958", + "modified_by": "Administrator", + "module": "Core", + "name": "Build", + "number_cards": [], + "owner": "Administrator", + "parent_page": "", + "public": 1, + "quick_lists": [], + "restrict_to_domain": "", + "roles": [], + "sequence_id": 27.0, + "shortcuts": [ + { + "color": "Grey", + "doc_view": "List", + "label": "Print Format Builder", + "link_to": "print-format-builder", + "type": "Page" + }, + { + "color": "Grey", + "doc_view": "List", + "label": "System Settings", + "link_to": "System Settings", + "type": "DocType", + "url": "https://xhiveframework.school/courses/xhiveframework-framework-course?utm_source=in_app" + }, + { + "color": "Grey", + "doc_view": "List", + "label": "Client Script", + "link_to": "Client Script", + "type": "DocType" + }, + { + "doc_view": "", + "label": "DocType", + "link_to": "DocType", + "type": "DocType" + }, + { + "doc_view": "", + "label": "Customize Form", + "link_to": "Customize Form", + "type": "DocType" + }, + { + "color": "Grey", + "doc_view": "List", + "label": "Server Script", + "link_to": "Server Script", + "type": "DocType" + }, + { + "doc_view": "", + "label": "Report", + "link_to": "Report", + "type": "DocType" + } + ], + "title": "Build" +} diff --git a/xhiveframework/core/workspace/users/users.json b/xhiveframework/core/workspace/users/users.json new file mode 100644 index 0000000..3899ad5 --- /dev/null +++ b/xhiveframework/core/workspace/users/users.json @@ -0,0 +1,202 @@ +{ + "charts": [], + "content": "[{\"id\":\"YpGCeLfign\",\"type\":\"header\",\"data\":{\"text\":\"Your Shortcuts\",\"col\":12}},{\"id\":\"b7abeqw4NZ\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"User\",\"col\":3}},{\"id\":\"eghSJPhZRC\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Role\",\"col\":3}},{\"id\":\"uAzl_lT_C0\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Permission Manager\",\"col\":3}},{\"id\":\"EpBz2lplSt\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"User Profile\",\"col\":3}},{\"id\":\"vHWhzaFoAH\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"User Type\",\"col\":3}},{\"id\":\"oFB4l28FMU\",\"type\":\"spacer\",\"data\":{\"col\":12}},{\"id\":\"yJNNylguxk\",\"type\":\"header\",\"data\":{\"text\":\"Reports & Masters\",\"col\":12}},{\"id\":\"NMpIkExl3i\",\"type\":\"card\",\"data\":{\"card_name\":\"Users\",\"col\":4}},{\"id\":\"VepG3durKm\",\"type\":\"card\",\"data\":{\"card_name\":\"Logs\",\"col\":4}},{\"id\":\"S9FeWt7xXE\",\"type\":\"card\",\"data\":{\"card_name\":\"Permissions\",\"col\":4}}]", + "creation": "2020-03-02 15:12:16.754449", + "custom_blocks": [], + "docstatus": 0, + "doctype": "Workspace", + "for_user": "", + "hide_custom": 0, + "icon": "users", + "idx": 0, + "is_hidden": 0, + "label": "Users", + "links": [ + { + "hidden": 0, + "is_query_report": 0, + "label": "Logs", + "link_count": 0, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Activity Log", + "link_count": 0, + "link_to": "Activity Log", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Access Log", + "link_count": 0, + "link_to": "Access Log", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Permissions", + "link_count": 0, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Role Permissions Manager", + "link_count": 0, + "link_to": "permission-manager", + "link_type": "Page", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "User Permissions", + "link_count": 0, + "link_to": "User Permission", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Role Permission for Page and Report", + "link_count": 0, + "link_to": "Role Permission for Page and Report", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "User", + "hidden": 0, + "is_query_report": 1, + "label": "Permitted Documents For User", + "link_count": 0, + "link_to": "Permitted Documents For User", + "link_type": "Report", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "DocShare", + "hidden": 0, + "is_query_report": 0, + "label": "Document Share Report", + "link_count": 0, + "link_to": "Document Share Report", + "link_type": "Report", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Users", + "link_count": 4, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "User", + "link_count": 0, + "link_to": "User", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Role", + "link_count": 0, + "link_to": "Role", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Role Profile", + "link_count": 0, + "link_to": "Role Profile", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Module Profile", + "link_count": 0, + "link_to": "Module Profile", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + } + ], + "modified": "2024-01-02 15:39:13.811700", + "modified_by": "Administrator", + "module": "Core", + "name": "Users", + "number_cards": [], + "owner": "Administrator", + "parent_page": "", + "public": 1, + "quick_lists": [], + "restrict_to_domain": "", + "roles": [], + "sequence_id": 13.0, + "shortcuts": [ + { + "label": "User", + "link_to": "User", + "type": "DocType" + }, + { + "label": "Role", + "link_to": "Role", + "type": "DocType" + }, + { + "label": "Permission Manager", + "link_to": "permission-manager", + "type": "Page" + }, + { + "label": "User Profile", + "link_to": "user-profile", + "type": "Page" + }, + { + "doc_view": "", + "label": "User Type", + "link_to": "User Type", + "type": "DocType" + } + ], + "title": "Users" +} \ No newline at end of file diff --git a/xhiveframework/core/workspace/welcome_workspace/welcome_workspace.json b/xhiveframework/core/workspace/welcome_workspace/welcome_workspace.json new file mode 100644 index 0000000..1b45434 --- /dev/null +++ b/xhiveframework/core/workspace/welcome_workspace/welcome_workspace.json @@ -0,0 +1,28 @@ +{ + "charts": [], + "content": "[{\"id\":\"2eyXSHwMTE\",\"type\":\"header\",\"data\":{\"text\":\"Hi,\",\"col\":12}},{\"id\":\"ZusKvFOXgu\",\"type\":\"paragraph\",\"data\":{\"text\":\"I guess you don't have access to any workspace yet, but you can create one just for yourself. Click on the Create Workspace button to create one.
    \",\"col\":12}}]", + "creation": "2023-07-28 17:14:28.608321", + "custom_blocks": [], + "docstatus": 0, + "doctype": "Workspace", + "for_user": "", + "hide_custom": 0, + "icon": "image-view", + "idx": 1, + "is_hidden": 0, + "label": "Welcome Workspace", + "links": [], + "modified": "2023-07-28 20:15:32.222029", + "modified_by": "Administrator", + "module": "Core", + "name": "Welcome Workspace", + "number_cards": [], + "owner": "Administrator", + "parent_page": "", + "public": 1, + "quick_lists": [], + "roles": [], + "sequence_id": 22.0, + "shortcuts": [], + "title": "Welcome Workspace" +} \ No newline at end of file diff --git a/xhiveframework/coverage.py b/xhiveframework/coverage.py new file mode 100644 index 0000000..1605a5a --- /dev/null +++ b/xhiveframework/coverage.py @@ -0,0 +1,77 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE +""" + xhiveframework.coverage + ~~~~~~~~~~~~~~~~ + + Coverage settings for xhiveframework +""" + +STANDARD_INCLUSIONS = ["*.py"] + +STANDARD_EXCLUSIONS = [ + "*.js", + "*.xml", + "*.pyc", + "*.css", + "*.less", + "*.scss", + "*.vue", + "*.html", + "*/test_*", + "*/node_modules/*", + "*/doctype/*/*_dashboard.py", + "*/patches/*", +] + +# tested via commands' test suite +TESTED_VIA_CLI = [ + "*/xhiveframework/installer.py", + "*/xhiveframework/build.py", + "*/xhiveframework/database/__init__.py", + "*/xhiveframework/database/db_manager.py", + "*/xhiveframework/database/**/setup_db.py", +] + +XHIVEFRAMEWORK_EXCLUSIONS = [ + "*/tests/*", + "*/commands/*", + "*/xhiveframework/change_log/*", + "*/xhiveframework/exceptions*", + "*/xhiveframework/coverage.py", + "*xhiveframework/setup.py", + "*/doctype/*/*_dashboard.py", + "*/patches/*", + *TESTED_VIA_CLI, +] + + +class CodeCoverage: + def __init__(self, with_coverage, app): + self.with_coverage = with_coverage + self.app = app or "xhiveframework" + + def __enter__(self): + if self.with_coverage: + import os + + from coverage import Coverage + + from xhiveframework.utils import get_bench_path + + # Generate coverage report only for app that is being tested + source_path = os.path.join(get_bench_path(), "apps", self.app) + omit = STANDARD_EXCLUSIONS[:] + + if self.app == "xhiveframework": + omit.extend(XHIVEFRAMEWORK_EXCLUSIONS) + + self.coverage = Coverage(source=[source_path], omit=omit, include=STANDARD_INCLUSIONS) + self.coverage.start() + + def __exit__(self, exc_type, exc_value, traceback): + if self.with_coverage: + self.coverage.stop() + self.coverage.save() + self.coverage.xml_report() + print("Saved Coverage") diff --git a/xhiveframework/custom/__init__.py b/xhiveframework/custom/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/doctype/__init__.py b/xhiveframework/custom/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/doctype/client_script/README.md b/xhiveframework/custom/doctype/client_script/README.md new file mode 100644 index 0000000..df0eded --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/README.md @@ -0,0 +1,11 @@ +Client or server script appended to standard DocType code. + +For client: + +- Code is appended to the js code. +- Best practice is to declare additional methods beginning with prefix `custom_` e.g. `custom_validate` to standard events. + +For server: + +- Script is appended to module code before DocType is `execute`d. +- All class methods must be written with a tab \ No newline at end of file diff --git a/xhiveframework/custom/doctype/client_script/__init__.py b/xhiveframework/custom/doctype/client_script/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/custom/doctype/client_script/client_script.js b/xhiveframework/custom/doctype/client_script/client_script.js new file mode 100644 index 0000000..33d129d --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/client_script.js @@ -0,0 +1,158 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Client Script", { + setup(frm) { + frm.get_field("sample").html(SAMPLE_HTML); + }, + refresh(frm) { + if (frm.doc.dt && frm.doc.script) { + frm.add_custom_button(__("Go to {0}", [frm.doc.dt]), () => + xhiveframework.set_route("List", frm.doc.dt, "List") + ); + } + + if (frm.doc.view == "Form") { + frm.add_custom_button(__("Add script for Child Table"), () => { + xhiveframework.model.with_doctype(frm.doc.dt, () => { + const child_tables = xhiveframework.meta + .get_docfields(frm.doc.dt, null, { + fieldtype: "Table", + }) + .map((df) => df.options); + + const d = new xhiveframework.ui.Dialog({ + title: __("Select Child Table"), + fields: [ + { + label: __("Select Child Table"), + fieldtype: "Link", + fieldname: "cdt", + options: "DocType", + get_query: () => { + return { + filters: { + istable: 1, + name: ["in", child_tables], + }, + }; + }, + }, + ], + primary_action: ({ cdt }) => { + cdt = d.get_field("cdt").value; + frm.events.add_script_for_doctype(frm, cdt); + d.hide(); + }, + }); + + d.show(); + }); + }); + + if (!frm.is_new()) { + frm.add_custom_button(__("Compare Versions"), () => { + new xhiveframework.ui.DiffView("Client Script", "script", frm.doc.name); + }); + } + } + + frm.set_query("dt", { + filters: { + istable: 0, + }, + }); + }, + + dt(frm) { + frm.toggle_display("view", !xhiveframework.boot.single_types.includes(frm.doc.dt)); + + if (!frm.doc.script) { + frm.events.add_script_for_doctype(frm, frm.doc.dt); + } + + if (frm.doc.script && !frm.doc.script.includes(frm.doc.dt)) { + frm.doc.script = ""; + frm.events.add_script_for_doctype(frm, frm.doc.dt); + } + }, + + view(frm) { + let has_form_boilerplate = frm.doc.script.includes("xhiveframework.ui.form.on"); + if (frm.doc.view === "List" && has_form_boilerplate) { + frm.set_value("script", ""); + } + if (frm.doc.view === "Form" && !has_form_boilerplate) { + frm.trigger("dt"); + } + }, + + add_script_for_doctype(frm, doctype) { + if (!doctype) return; + let boilerplate = ` +xhiveframework.ui.form.on('${doctype}', { + refresh(frm) { + // your code here + } +}) + `.trim(); + let script = frm.doc.script || ""; + if (script) { + script += "\n\n"; + } + frm.set_value("script", script + boilerplate); + }, +}); + +const SAMPLE_HTML = `

    Client Script Help

    +

    Client Scripts are executed only on the client-side (i.e. in Forms). Here are some examples to get you started

    +
    
    +
    +// fetch local_tax_no on selection of customer
    +// cur_frm.add_fetch(link_field,  source_fieldname,  target_fieldname);
    +cur_frm.add_fetch("customer",  "local_tax_no',  'local_tax_no');
    +
    +// additional validation on dates
    +xhiveframework.ui.form.on('Task',  'validate',  function(frm) {
    +    if (frm.doc.from_date < get_today()) {
    +        msgprint('You can not select past date in From Date');
    +        validated = false;
    +    }
    +});
    +
    +// make a field read-only after saving
    +xhiveframework.ui.form.on('Task',  {
    +    refresh: function(frm) {
    +        // use the __islocal value of doc,  to check if the doc is saved or not
    +        frm.set_df_property('myfield',  'read_only',  frm.doc.__islocal ? 0 : 1);
    +    }
    +});
    +
    +// additional permission check
    +xhiveframework.ui.form.on('Task',  {
    +    validate: function(frm) {
    +        if(user=='user1@example.com' && frm.doc.purpose!='Material Receipt') {
    +            msgprint('You are only allowed Material Receipt');
    +            validated = false;
    +        }
    +    }
    +});
    +
    +// calculate sales incentive
    +xhiveframework.ui.form.on('Sales Invoice',  {
    +    validate: function(frm) {
    +        // calculate incentives for each person on the deal
    +        total_incentive = 0
    +        $.each(frm.doc.sales_team,  function(i,  d) {
    +            // calculate incentive
    +            var incentive_percent = 2;
    +            if(frm.doc.base_grand_total > 400) incentive_percent = 4;
    +            // actual incentive
    +            d.incentives = flt(frm.doc.base_grand_total) * incentive_percent / 100;
    +            total_incentive += flt(d.incentives)
    +        });
    +        frm.doc.total_incentive = total_incentive;
    +    }
    +})
    +
    +
    `; diff --git a/xhiveframework/custom/doctype/client_script/client_script.json b/xhiveframework/custom/doctype/client_script/client_script.json new file mode 100644 index 0000000..dddf0c0 --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/client_script.json @@ -0,0 +1,114 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "Prompt", + "creation": "2013-01-10 16:34:01", + "description": "Adds a custom client script to a DocType", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "dt", + "view", + "column_break_3", + "module", + "enabled", + "section_break_6", + "script", + "sample" + ], + "fields": [ + { + "fieldname": "dt", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "DocType", + "oldfieldname": "dt", + "oldfieldtype": "Link", + "options": "DocType", + "reqd": 1, + "set_only_once": 1 + }, + { + "fieldname": "script", + "fieldtype": "Code", + "label": "Script", + "oldfieldname": "script", + "oldfieldtype": "Code", + "options": "JS" + }, + { + "fieldname": "sample", + "fieldtype": "HTML", + "label": "Sample" + }, + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "default": "Form", + "fieldname": "view", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Apply To", + "options": "List\nForm", + "set_only_once": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module (for export)", + "options": "Module Def" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break" + } + ], + "icon": "fa fa-glass", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Custom", + "name": "Client Script", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/custom/doctype/client_script/client_script.py b/xhiveframework/custom/doctype/client_script/client_script.py new file mode 100644 index 0000000..d9c1534 --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/client_script.py @@ -0,0 +1,27 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.model.document import Document + + +class ClientScript(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + dt: DF.Link + enabled: DF.Check + module: DF.Link | None + script: DF.Code | None + view: DF.Literal["List", "Form"] + + # end: auto-generated types + def on_update(self): + xhiveframework.clear_cache(doctype=self.dt) + + def on_trash(self): + xhiveframework.clear_cache(doctype=self.dt) diff --git a/xhiveframework/custom/doctype/client_script/test_client_script.py b/xhiveframework/custom/doctype/client_script/test_client_script.py new file mode 100644 index 0000000..11de604 --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/test_client_script.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Client Script') + + +class TestClientScript(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/custom/doctype/client_script/ui_test_client_script.js b/xhiveframework/custom/doctype/client_script/ui_test_client_script.js new file mode 100644 index 0000000..0d202d6 --- /dev/null +++ b/xhiveframework/custom/doctype/client_script/ui_test_client_script.js @@ -0,0 +1,98 @@ +context("Client Script", () => { + before(() => { + cy.login(); + cy.visit("/app"); + }); + + it("should run form script in doctype form", () => { + cy.insert_doc( + "Client Script", + { + name: "Todo form script", + dt: "ToDo", + view: "Form", + enabled: 1, + script: `console.log('todo form script')`, + }, + true + ); + cy.visit("/app/todo/new", { + onBeforeLoad(win) { + cy.spy(win.console, "log").as("consoleLog"); + }, + }); + cy.get("@consoleLog").should("be.calledWith", "todo form script"); + }); + + it("should run list script in doctype list view", () => { + cy.insert_doc( + "Client Script", + { + name: "Todo list script", + dt: "ToDo", + view: "List", + enabled: 1, + script: `console.log('todo list script')`, + }, + true + ); + cy.visit("/app/todo", { + onBeforeLoad(win) { + cy.spy(win.console, "log").as("consoleLog"); + }, + }); + cy.get("@consoleLog").should("be.calledWith", "todo list script"); + }); + + it("should not run disabled scripts", () => { + cy.insert_doc( + "Client Script", + { + name: "Todo disabled list", + dt: "ToDo", + view: "List", + enabled: 0, + script: `console.log('todo disabled script')`, + }, + true + ); + cy.visit("/app/todo", { + onBeforeLoad(win) { + cy.spy(win.console, "log").as("consoleLog"); + }, + }); + cy.get("@consoleLog").should("not.be.calledWith", "todo disabled script"); + }); + + it("should run multiple scripts", () => { + cy.insert_doc( + "Client Script", + { + name: "Todo form script 1", + dt: "ToDo", + view: "Form", + enabled: 1, + script: `console.log('todo form script 1')`, + }, + true + ); + cy.insert_doc( + "Client Script", + { + name: "Todo form script 2", + dt: "ToDo", + view: "Form", + enabled: 1, + script: `console.log('todo form script 2')`, + }, + true + ); + cy.visit("/app/todo/new", { + onBeforeLoad(win) { + cy.spy(win.console, "log").as("consoleLog"); + }, + }); + cy.get("@consoleLog").should("be.calledWith", "todo form script 1"); + cy.get("@consoleLog").should("be.calledWith", "todo form script 2"); + }); +}); diff --git a/xhiveframework/custom/doctype/custom_field/README.md b/xhiveframework/custom/doctype/custom_field/README.md new file mode 100644 index 0000000..5d6bf14 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/README.md @@ -0,0 +1 @@ +Custom Field added by the user (not part of DocType but part of table). Custom fields are automatically added to the DocType when they are loaded as metadata. \ No newline at end of file diff --git a/xhiveframework/custom/doctype/custom_field/__init__.py b/xhiveframework/custom/doctype/custom_field/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/custom/doctype/custom_field/custom_field.js b/xhiveframework/custom/doctype/custom_field/custom_field.js new file mode 100644 index 0000000..c00ede7 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/custom_field.js @@ -0,0 +1,152 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +// Refresh +// -------- + +xhiveframework.ui.form.on("Custom Field", { + setup: function (frm) { + frm.set_query("dt", function (doc) { + var filters = [ + ["DocType", "issingle", "=", 0], + ["DocType", "custom", "=", 0], + ["DocType", "name", "not in", xhiveframework.model.core_doctypes_list], + ["DocType", "restrict_to_domain", "in", xhiveframework.boot.active_domains], + ]; + if (xhiveframework.session.user !== "Administrator") { + filters.push(["DocType", "module", "not in", ["Core", "Custom"]]); + } + return { + filters: filters, + }; + }); + }, + refresh: function (frm) { + frm.toggle_enable("dt", frm.doc.__islocal); + frm.trigger("dt"); + frm.toggle_reqd("label", !frm.doc.fieldname); + frm.trigger("add_rename_field"); + + if (frm.doc.is_system_generated) { + frm.dashboard.add_comment( + __( + "Warning: This field is system generated and may be overwritten by a future update. Modify it using {0} instead.", + [ + xhiveframework.utils.get_form_link( + "Customize Form", + "Customize Form", + true, + __("Customize Form"), + { + doc_type: frm.doc.dt, + } + ), + ] + ), + "yellow", + true + ); + } + }, + dt: function (frm) { + if (!frm.doc.dt) { + set_field_options("insert_after", ""); + return; + } + var insert_after = frm.doc.insert_after || null; + return xhiveframework.call({ + method: "xhiveframework.custom.doctype.custom_field.custom_field.get_fields_label", + args: { doctype: frm.doc.dt, fieldname: frm.doc.fieldname }, + callback: function (r) { + if (r) { + if (r._server_messages && r._server_messages.length) { + frm.set_value("dt", ""); + } else { + set_field_options("insert_after", r.message); + var fieldnames = $.map(r.message, function (v) { + return v.value; + }); + + if (insert_after == null || !fieldnames.includes(insert_after)) { + insert_after = fieldnames[-1]; + } + + frm.set_value("insert_after", insert_after); + } + } + }, + }); + }, + label: function (frm) { + if (frm.doc.label && xhiveframework.utils.has_special_chars(frm.doc.label)) { + frm.fields_dict["label_help"].disp_area.innerHTML = + '' + __("Special Characters are not allowed") + ""; + frm.set_value("label", ""); + } else { + frm.fields_dict["label_help"].disp_area.innerHTML = ""; + } + }, + fieldtype: function (frm) { + if (frm.doc.fieldtype == "Link") { + frm.fields_dict["options_help"].disp_area.innerHTML = __( + "Name of the Document Type (DocType) you want this field to be linked to. e.g. Customer" + ); + } else if (frm.doc.fieldtype == "Select") { + frm.fields_dict["options_help"].disp_area.innerHTML = + __("Options for select. Each option on a new line.") + + " " + + __("e.g.:") + + "
    " + + __("Option 1") + + "
    " + + __("Option 2") + + "
    " + + __("Option 3") + + "
    "; + } else if (frm.doc.fieldtype == "Dynamic Link") { + frm.fields_dict["options_help"].disp_area.innerHTML = __( + "Fieldname which will be the DocType for this link field." + ); + } else { + frm.fields_dict["options_help"].disp_area.innerHTML = ""; + } + }, + add_rename_field(frm) { + if (!frm.is_new()) { + frm.add_custom_button(__("Rename Fieldname"), () => { + xhiveframework.prompt( + { + fieldtype: "Data", + label: __("Fieldname"), + fieldname: "fieldname", + reqd: 1, + default: frm.doc.fieldname, + }, + function (data) { + xhiveframework + .xcall( + "xhiveframework.custom.doctype.custom_field.custom_field.rename_fieldname", + { + custom_field: frm.doc.name, + fieldname: data.fieldname, + } + ) + .then(() => frm.reload()); + }, + __("Rename Fieldname"), + __("Rename") + ); + }); + } + }, +}); + +xhiveframework.utils.has_special_chars = function (t) { + var iChars = "!@#$%^&*()+=-[]\\';,./{}|\":<>?"; + for (var i = 0; i < t.length; i++) { + if (iChars.indexOf(t.charAt(i)) != -1) { + return true; + } + } + return false; +}; diff --git a/xhiveframework/custom/doctype/custom_field/custom_field.json b/xhiveframework/custom/doctype/custom_field/custom_field.json new file mode 100644 index 0000000..32c4552 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/custom_field.json @@ -0,0 +1,494 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2024-02-21 18:14:42.281748", + "description": "Adds a custom field to a DocType", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "is_system_generated", + "dt", + "module", + "label", + "label_help", + "fieldname", + "insert_after", + "length", + "column_break_6", + "fieldtype", + "precision", + "hide_seconds", + "hide_days", + "options", + "sort_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", + "show_dashboard", + "description", + "permlevel", + "width", + "columns" + ], + "fields": [ + { + "bold": 1, + "fieldname": "dt", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "DocType", + "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, + "in_standard_filter": 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": "Autocomplete\nAttach\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\nJSON\nLink\nLong Text\nMarkdown Editor\nPassword\nPercent\nPhone\nRead Only\nRating\nSection Break\nSelect\nSignature\nSmall Text\nTab Break\nTable\nTable MultiSelect\nText\nText Editor\nTime", + "reqd": 1, + "sort_options": 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 unchecked, the value will always be re-fetched on save.", + "fieldname": "fetch_if_empty", + "fieldtype": "Check", + "label": "Fetch on Save 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": "0", + "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" + }, + { + "default": "0", + "fieldname": "is_system_generated", + "fieldtype": "Check", + "label": "Is System Generated", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "eval: doc.fieldtype === 'Select'", + "fieldname": "sort_options", + "fieldtype": "Check", + "label": "Sort Options" + }, + { + "default": "0", + "fieldname": "show_dashboard", + "fieldtype": "Check", + "label": "Show Dashboard" + } + ], + "icon": "fa fa-glass", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-03-07 17:34:47.167183", + "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": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/custom/doctype/custom_field/custom_field.py b/xhiveframework/custom/doctype/custom_field/custom_field.py new file mode 100644 index 0000000..d143d10 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/custom_field.py @@ -0,0 +1,391 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import core_doctypes_list +from xhiveframework.model.docfield import supports_translation +from xhiveframework.model.document import Document +from xhiveframework.query_builder.functions import IfNull +from xhiveframework.utils import cstr, random_string + + +class CustomField(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow_in_quick_entry: DF.Check + allow_on_submit: DF.Check + bold: DF.Check + collapsible: DF.Check + collapsible_depends_on: DF.Code | None + columns: DF.Int + default: DF.Text | None + depends_on: DF.Code | None + description: DF.Text | None + dt: DF.Link + fetch_from: DF.SmallText | None + fetch_if_empty: DF.Check + fieldname: DF.Data | None + fieldtype: DF.Literal[ + "Autocomplete", + "Attach", + "Attach Image", + "Barcode", + "Button", + "Check", + "Code", + "Color", + "Column Break", + "Currency", + "Data", + "Date", + "Datetime", + "Duration", + "Dynamic Link", + "Float", + "Fold", + "Geolocation", + "Heading", + "HTML", + "HTML Editor", + "Icon", + "Image", + "Int", + "JSON", + "Link", + "Long Text", + "Markdown Editor", + "Password", + "Percent", + "Phone", + "Read Only", + "Rating", + "Section Break", + "Select", + "Signature", + "Small Text", + "Tab Break", + "Table", + "Table MultiSelect", + "Text", + "Text Editor", + "Time", + ] + hidden: DF.Check + hide_border: DF.Check + hide_days: DF.Check + hide_seconds: DF.Check + ignore_user_permissions: DF.Check + ignore_xss_filter: DF.Check + in_global_search: DF.Check + in_list_view: DF.Check + in_preview: DF.Check + in_standard_filter: DF.Check + insert_after: DF.Literal[None] + is_system_generated: DF.Check + is_virtual: DF.Check + label: DF.Data | None + length: DF.Int + mandatory_depends_on: DF.Code | None + module: DF.Link | None + no_copy: DF.Check + non_negative: DF.Check + options: DF.SmallText | None + permlevel: DF.Int + precision: DF.Literal["", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + print_hide: DF.Check + print_hide_if_no_value: DF.Check + print_width: DF.Data | None + read_only: DF.Check + read_only_depends_on: DF.Code | None + report_hide: DF.Check + reqd: DF.Check + search_index: DF.Check + show_dashboard: DF.Check + sort_options: DF.Check + translatable: DF.Check + unique: DF.Check + width: DF.Data | None + + # end: auto-generated types + def autoname(self): + self.set_fieldname() + self.name = self.dt + "-" + self.fieldname + + def set_fieldname(self): + restricted = ( + "name", + "parent", + "creation", + "modified", + "modified_by", + "parentfield", + "parenttype", + "file_list", + "flags", + "docstatus", + ) + if not self.fieldname: + label = self.label + if not label: + if self.fieldtype in ["Section Break", "Column Break", "Tab Break"]: + label = self.fieldtype + "_" + str(random_string(5)) + else: + xhiveframework.throw(_("Label is mandatory")) + + # remove special characters from fieldname + self.fieldname = "".join( + [c for c in cstr(label).replace(" ", "_") if c.isdigit() or c.isalpha() or c == "_"] + ) + self.fieldname = f"custom_{self.fieldname}" + + # fieldnames should be lowercase + self.fieldname = self.fieldname.lower() + + if self.fieldname in restricted: + self.fieldname = self.fieldname + "1" + + def before_insert(self): + self.set_fieldname() + + def validate(self): + # these imports have been added to avoid cyclical import, should fix in future + from xhiveframework.core.doctype.doctype.doctype import check_fieldname_conflicts + from xhiveframework.custom.doctype.customize_form.customize_form import CustomizeForm + + # don't always get meta to improve performance + # setting idx is just an improvement, not a requirement + if self.is_new() or self.insert_after == "append": + meta = xhiveframework.get_meta(self.dt, cached=False) + fieldnames = [df.fieldname for df in meta.get("fields")] + + if self.is_new() and self.fieldname in fieldnames: + xhiveframework.throw( + _("A field with the name {0} already exists in {1}").format( + xhiveframework.bold(self.fieldname), self.dt + ) + ) + + if self.insert_after == "append": + self.insert_after = fieldnames[-1] + + if self.insert_after and self.insert_after in fieldnames: + self.idx = fieldnames.index(self.insert_after) + 1 + + if ( + not self.is_virtual + and (doc_before_save := self.get_doc_before_save()) + and (old_fieldtype := doc_before_save.fieldtype) != self.fieldtype + and not CustomizeForm.allow_fieldtype_change(old_fieldtype, self.fieldtype) + ): + xhiveframework.throw( + _("Fieldtype cannot be changed from {0} to {1}").format(old_fieldtype, self.fieldtype) + ) + + if not self.fieldname: + xhiveframework.throw(_("Fieldname not set for Custom Field")) + + if self.get("translatable", 0) and not supports_translation(self.fieldtype): + self.translatable = 0 + + check_fieldname_conflicts(self) + + def on_update(self): + # validate field + if not self.flags.ignore_validate: + from xhiveframework.core.doctype.doctype.doctype import validate_fields_for_doctype + + validate_fields_for_doctype(self.dt) + + # clear cache and update the schema + if not xhiveframework.flags.in_create_custom_fields: + xhiveframework.clear_cache(doctype=self.dt) + xhiveframework.db.updatedb(self.dt) + + def on_trash(self): + # check if Admin owned field + if self.owner == "Administrator" and xhiveframework.session.user != "Administrator": + xhiveframework.throw( + _( + "Custom Field {0} is created by the Administrator and can only be deleted through the Administrator account." + ).format(xhiveframework.bold(self.label)) + ) + + # delete property setter entries + xhiveframework.db.delete("Property Setter", {"doc_type": self.dt, "field_name": self.fieldname}) + + # update doctype layouts + doctype_layouts = xhiveframework.get_all("DocType Layout", filters={"document_type": self.dt}, pluck="name") + + for layout in doctype_layouts: + layout_doc = xhiveframework.get_doc("DocType Layout", layout) + for field in layout_doc.fields: + if field.fieldname == self.fieldname: + layout_doc.remove(field) + layout_doc.save() + break + + xhiveframework.clear_cache(doctype=self.dt) + + def validate_insert_after(self, meta): + if not meta.get_field(self.insert_after): + xhiveframework.throw( + _( + "Insert After field '{0}' mentioned in Custom Field '{1}', with label '{2}', does not exist" + ).format(self.insert_after, self.name, self.label), + xhiveframework.DoesNotExistError, + ) + + if self.fieldname == self.insert_after: + xhiveframework.throw(_("Insert After cannot be set as {0}").format(meta.get_label(self.insert_after))) + + +@xhiveframework.whitelist() +def get_fields_label(doctype=None): + meta = xhiveframework.get_meta(doctype) + + if doctype in core_doctypes_list: + return xhiveframework.msgprint(_("Custom Fields cannot be added to core DocTypes.")) + + if meta.custom: + return xhiveframework.msgprint(_("Custom Fields can only be added to a standard DocType.")) + + return [ + {"value": df.fieldname or "", "label": _(df.label, context=df.parent) if df.label else ""} + for df in xhiveframework.get_meta(doctype).get("fields") + ] + + +def create_custom_field_if_values_exist(doctype, df): + df = xhiveframework._dict(df) + if df.fieldname in xhiveframework.db.get_table_columns(doctype) and xhiveframework.db.count( + dt=doctype, filters=IfNull(df.fieldname, "") != "" + ): + create_custom_field(doctype, df) + + +def create_custom_field(doctype, df, ignore_validate=False, is_system_generated=True): + df = xhiveframework._dict(df) + if not df.fieldname and df.label: + df.fieldname = xhiveframework.scrub(df.label) + if not xhiveframework.db.get_value("Custom Field", {"dt": doctype, "fieldname": df.fieldname}): + custom_field = xhiveframework.get_doc( + { + "doctype": "Custom Field", + "dt": doctype, + "permlevel": 0, + "fieldtype": "Data", + "hidden": 0, + "is_system_generated": is_system_generated, + } + ) + custom_field.update(df) + custom_field.flags.ignore_validate = ignore_validate + custom_field.insert() + return custom_field + + +def create_custom_fields(custom_fields: dict, ignore_validate=False, update=True): + """Add / update multiple custom fields + + :param custom_fields: example `{'Sales Invoice': [dict(fieldname='test')]}`""" + + try: + xhiveframework.flags.in_create_custom_fields = True + doctypes_to_update = set() + + if xhiveframework.flags.in_setup_wizard: + ignore_validate = True + + for doctypes, fields in custom_fields.items(): + if isinstance(fields, dict): + # only one field + fields = [fields] + + if isinstance(doctypes, str): + # only one doctype + doctypes = (doctypes,) + + for doctype in doctypes: + doctypes_to_update.add(doctype) + + for df in fields: + field = xhiveframework.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]}) + if not field: + try: + df = df.copy() + df["owner"] = "Administrator" + create_custom_field(doctype, df, ignore_validate=ignore_validate) + + except xhiveframework.exceptions.DuplicateEntryError: + pass + + elif update: + custom_field = xhiveframework.get_doc("Custom Field", field) + custom_field.flags.ignore_validate = ignore_validate + custom_field.update(df) + custom_field.save() + + for doctype in doctypes_to_update: + xhiveframework.clear_cache(doctype=doctype) + xhiveframework.db.updatedb(doctype) + + finally: + xhiveframework.flags.in_create_custom_fields = False + + +@xhiveframework.whitelist() +def rename_fieldname(custom_field: str, fieldname: str): + xhiveframework.only_for("System Manager") + + field: CustomField = xhiveframework.get_doc("Custom Field", custom_field) + parent_doctype = field.dt + old_fieldname = field.fieldname + field.fieldname = fieldname + field.set_fieldname() + new_fieldname = field.fieldname + + if field.is_system_generated: + xhiveframework.throw(_("System Generated Fields can not be renamed")) + if xhiveframework.db.has_column(parent_doctype, fieldname): + xhiveframework.throw(_("Can not rename as column {0} is already present on DocType.").format(fieldname)) + if old_fieldname == new_fieldname: + xhiveframework.msgprint(_("Old and new fieldnames are same."), alert=True) + return + + if xhiveframework.db.has_column(field.dt, old_fieldname): + xhiveframework.db.rename_column(parent_doctype, old_fieldname, new_fieldname) + + # Update in DB after alter column is successful, alter column will implicitly commit, so it's + # best to commit change on field too to avoid any possible mismatch between two. + field.db_set("fieldname", field.fieldname, notify=True) + _update_fieldname_references(field, old_fieldname, new_fieldname) + + xhiveframework.msgprint(_("Custom field renamed to {0} successfully.").format(fieldname), alert=True) + xhiveframework.db.commit() + xhiveframework.clear_cache() + + +def _update_fieldname_references(field: CustomField, old_fieldname: str, new_fieldname: str) -> None: + # Passwords are stored in auth table, so column name needs to be updated there. + if field.fieldtype == "Password": + Auth = xhiveframework.qb.Table("__Auth") + xhiveframework.qb.update(Auth).set(Auth.fieldname, new_fieldname).where( + (Auth.doctype == field.dt) & (Auth.fieldname == old_fieldname) + ).run() + + # Update ordering reference. + xhiveframework.db.set_value( + "Custom Field", + {"insert_after": old_fieldname, "dt": field.dt}, + "insert_after", + new_fieldname, + ) diff --git a/xhiveframework/custom/doctype/custom_field/test_custom_field.py b/xhiveframework/custom/doctype/custom_field/test_custom_field.py new file mode 100644 index 0000000..67f84e9 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/test_custom_field.py @@ -0,0 +1,107 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.custom.doctype.custom_field.custom_field import ( + create_custom_field, + create_custom_fields, + rename_fieldname, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Custom Field") + + +class TestCustomField(XhiveFrameworkTestCase): + def test_create_custom_fields(self): + create_custom_fields( + { + "Address": [ + { + "fieldname": "_test_custom_field_1", + "label": "_Test Custom Field 1", + "fieldtype": "Data", + "insert_after": "phone", + }, + ], + ("Address", "Contact"): [ + { + "fieldname": "_test_custom_field_2", + "label": "_Test Custom Field 2", + "fieldtype": "Data", + "insert_after": "phone", + }, + ], + } + ) + + xhiveframework.db.commit() + + self.assertTrue(xhiveframework.db.exists("Custom Field", "Address-_test_custom_field_1")) + self.assertTrue(xhiveframework.db.exists("Custom Field", "Address-_test_custom_field_2")) + self.assertTrue(xhiveframework.db.exists("Custom Field", "Contact-_test_custom_field_2")) + + def test_custom_field_sorting(self): + try: + custom_fields = { + "ToDo": [ + {"fieldname": "a_test_field", "insert_after": "b_test_field"}, + {"fieldname": "b_test_field", "insert_after": "status"}, + {"fieldname": "c_test_field", "insert_after": "unknown_custom_field"}, + {"fieldname": "d_test_field", "insert_after": "status"}, + ] + } + + create_custom_fields(custom_fields, ignore_validate=True) + + fields = xhiveframework.get_meta("ToDo", cached=False).fields + + for i, field in enumerate(fields): + if field.fieldname == "b_test_field": + self.assertEqual(fields[i - 1].fieldname, "status") + + if field.fieldname == "d_test_field": + self.assertEqual(fields[i - 1].fieldname, "a_test_field") + + self.assertEqual(fields[-1].fieldname, "c_test_field") + + finally: + xhiveframework.db.delete( + "Custom Field", + { + "dt": "ToDo", + "fieldname": ( + "in", + ( + "a_test_field", + "b_test_field", + "c_test_field", + "d_test_field", + ), + ), + }, + ) + + # undo changes commited by DDL + # nosemgrep + xhiveframework.db.commit() + + def test_custom_field_renaming(self): + def gen_fieldname(): + return "test_" + xhiveframework.generate_hash() + + field = create_custom_field("ToDo", {"label": gen_fieldname()}, is_system_generated=False) + old = field.fieldname + new = gen_fieldname() + data = xhiveframework.generate_hash() + doc = xhiveframework.get_doc({"doctype": "ToDo", old: data, "description": "Something"}).insert() + + rename_fieldname(field.name, new) + field.reload() + self.assertEqual(field.fieldname, new) + + doc = xhiveframework.get_doc("ToDo", doc.name) # doc.reload doesn't clear old fields. + self.assertEqual(doc.get(new), data) + self.assertFalse(doc.get(old)) + + field.delete() diff --git a/xhiveframework/custom/doctype/custom_field/test_records.json b/xhiveframework/custom/doctype/custom_field/test_records.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/xhiveframework/custom/doctype/custom_field/test_records.json @@ -0,0 +1 @@ +[] diff --git a/xhiveframework/custom/doctype/customize_form/README.md b/xhiveframework/custom/doctype/customize_form/README.md new file mode 100644 index 0000000..280b66d --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/README.md @@ -0,0 +1 @@ +Create a DocType like fields table and allow user to set field properties for which **Property Setter** will be created when updated. \ No newline at end of file diff --git a/xhiveframework/custom/doctype/customize_form/__init__.py b/xhiveframework/custom/doctype/customize_form/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/custom/doctype/customize_form/customize_form.js b/xhiveframework/custom/doctype/customize_form/customize_form.js new file mode 100644 index 0000000..8875f58 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/customize_form.js @@ -0,0 +1,445 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.provide("xhiveframework.customize_form"); + +xhiveframework.ui.form.on("Customize Form", { + setup: function (frm) { + // save the last setting if refreshing + window.addEventListener("beforeunload", () => { + if (frm.doc.doc_type && frm.doc.doc_type != "undefined") { + localStorage["customize_doctype"] = frm.doc.doc_type; + } + }); + }, + + onload: function (frm) { + frm.set_query("doc_type", function () { + return { + filters: [ + ["DocType", "issingle", "=", 0], + ["DocType", "custom", "=", 0], + ["DocType", "name", "not in", xhiveframework.model.core_doctypes_list], + ["DocType", "restrict_to_domain", "in", xhiveframework.boot.active_domains], + ], + }; + }); + + frm.set_query("default_print_format", function () { + return { + filters: { + print_format_type: ["!=", "JS"], + doc_type: ["=", frm.doc.doc_type], + }, + }; + }); + + $(frm.wrapper).on("grid-row-render", function (e, grid_row) { + if (grid_row.doc && grid_row.doc.fieldtype == "Section Break") { + $(grid_row.row).css({ "font-weight": "bold" }); + } + + grid_row.row.removeClass("highlight"); + + if ( + grid_row.doc.is_custom_field && + !grid_row.row.hasClass("highlight") && + !grid_row.doc.is_system_generated + ) { + grid_row.row.addClass("highlight"); + } + }); + }, + + doc_type: function (frm) { + if (frm.doc.doc_type) { + return frm.call({ + method: "fetch_to_customize", + doc: frm.doc, + freeze: true, + callback: function (r) { + if (r) { + if (r._server_messages && r._server_messages.length) { + frm.set_value("doc_type", ""); + } else { + frm.refresh(); + frm.trigger("add_customize_child_table_button"); + frm.trigger("setup_default_views"); + } + } + localStorage["customize_doctype"] = frm.doc.doc_type; + }, + }); + } else { + frm.refresh(); + } + }, + + is_calendar_and_gantt: function (frm) { + frm.trigger("setup_default_views"); + }, + + add_customize_child_table_button: function (frm) { + frm.doc.fields.forEach(function (f) { + if (!["Table", "Table MultiSelect"].includes(f.fieldtype)) return; + + frm.add_custom_button( + __(f.options), + () => frm.set_value("doc_type", f.options), + __("Customize Child Table") + ); + }); + }, + + refresh: function (frm) { + frm.disable_save(true); + frm.page.clear_icons(); + + if (frm.doc.doc_type) { + xhiveframework.model.with_doctype(frm.doc.doc_type).then(() => { + frm.page.set_title(__("Customize Form - {0}", [__(frm.doc.doc_type)])); + xhiveframework.customize_form.set_primary_action(frm); + + frm.add_custom_button( + __("Go to {0} List", [__(frm.doc.doc_type)]), + function () { + xhiveframework.set_route("List", frm.doc.doc_type); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Set Permissions"), + function () { + xhiveframework.set_route("permission-manager", frm.doc.doc_type); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Reload"), + function () { + frm.script_manager.trigger("doc_type"); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Reset Layout"), + () => { + frm.trigger("reset_layout"); + }, + __("Actions") + ); + + frm.add_custom_button( + __("Reset All Customizations"), + function () { + xhiveframework.customize_form.confirm(__("Remove all customizations?"), frm); + }, + __("Actions") + ); + + const is_autoname_autoincrement = frm.doc.autoname === "autoincrement"; + frm.set_df_property("naming_rule", "hidden", is_autoname_autoincrement); + frm.set_df_property("autoname", "read_only", is_autoname_autoincrement); + frm.toggle_display( + ["queue_in_background"], + xhiveframework.get_meta(frm.doc.doc_type).is_submittable || 0 + ); + + render_form_builder(frm); + frm.get_field("form_builder").tab.set_active(); + }); + } + + frm.events.setup_export(frm); + frm.events.setup_sort_order(frm); + frm.events.set_default_doc_type(frm); + }, + + set_default_doc_type(frm) { + let doc_type; + if (xhiveframework.route_options && xhiveframework.route_options.doc_type) { + doc_type = xhiveframework.route_options.doc_type; + xhiveframework.route_options = null; + localStorage.removeItem("customize_doctype"); + } + if (!doc_type) { + doc_type = localStorage.getItem("customize_doctype"); + } + if (doc_type) { + setTimeout(() => frm.set_value("doc_type", doc_type, false, true), 1000); + } + }, + + reset_layout(frm) { + xhiveframework.confirm( + __("Layout will be reset to standard layout, are you sure you want to do this?"), + () => { + return frm.call({ + doc: frm.doc, + method: "reset_layout", + callback: function (r) { + if (!r.exc) { + xhiveframework.show_alert({ + message: __("Layout Reset"), + indicator: "green", + }); + xhiveframework.customize_form.clear_locals_and_refresh(frm); + } + }, + }); + } + ); + }, + + setup_export(frm) { + if (xhiveframework.boot.developer_mode) { + frm.add_custom_button( + __("Export Customizations"), + function () { + xhiveframework.prompt( + [ + { + fieldtype: "Link", + fieldname: "module", + options: "Module Def", + label: __("Module to Export"), + reqd: 1, + }, + { + fieldtype: "Check", + fieldname: "sync_on_migrate", + label: __("Sync on Migrate"), + default: 1, + }, + { + fieldtype: "Check", + fieldname: "with_permissions", + label: __("Export Custom Permissions"), + default: 1, + }, + ], + function (data) { + xhiveframework.call({ + method: "xhiveframework.modules.utils.export_customizations", + args: { + doctype: frm.doc.doc_type, + module: data.module, + sync_on_migrate: data.sync_on_migrate, + with_permissions: data.with_permissions, + }, + }); + }, + __("Select Module") + ); + }, + __("Actions") + ); + } + }, + + setup_sort_order(frm) { + // sort order select + if (frm.doc.doc_type) { + var fields = $.map(frm.doc.fields, function (df) { + return xhiveframework.model.is_value_type(df.fieldtype) ? df.fieldname : null; + }); + fields = ["", "name", "modified"].concat(fields); + frm.set_df_property("sort_field", "options", fields); + } + }, + + setup_default_views(frm) { + xhiveframework.model.set_default_views_for_doctype(frm.doc.doc_type, frm); + }, +}); + +// can't delete standard fields +xhiveframework.ui.form.on("Customize Form Field", { + before_fields_remove: function (frm, doctype, name) { + const row = xhiveframework.get_doc(doctype, name); + + if (row.is_system_generated) { + xhiveframework.throw( + __( + "Cannot delete system generated field {0}. You can hide it instead.", + [__(row.label) || row.fieldname] + ) + ); + } + + if (!(row.is_custom_field || row.__islocal)) { + xhiveframework.throw( + __("Cannot delete standard field {0}. You can hide it instead.", [ + __(row.label) || row.fieldname, + ]) + ); + } + }, + fields_add: function (frm, cdt, cdn) { + var f = xhiveframework.model.get_doc(cdt, cdn); + f.is_system_generated = false; + f.is_custom_field = true; + frm.trigger("setup_default_views"); + }, + + form_render(frm, doctype, docname) { + frm.trigger("setup_fetch_from_fields", doctype, docname); + }, +}); + +// can't delete standard links +xhiveframework.ui.form.on("DocType Link", { + before_links_remove: function (frm, doctype, name) { + let row = xhiveframework.get_doc(doctype, name); + if (!(row.custom || row.__islocal)) { + xhiveframework.msgprint(__("Cannot delete standard link. You can hide it if you want")); + throw "cannot delete standard link"; + } + }, + links_add: function (frm, cdt, cdn) { + let f = xhiveframework.model.get_doc(cdt, cdn); + f.custom = 1; + }, +}); + +// can't delete standard actions +xhiveframework.ui.form.on("DocType Action", { + before_actions_remove: function (frm, doctype, name) { + let row = xhiveframework.get_doc(doctype, name); + if (!(row.custom || row.__islocal)) { + xhiveframework.msgprint(__("Cannot delete standard action. You can hide it if you want")); + throw "cannot delete standard action"; + } + }, + actions_add: function (frm, cdt, cdn) { + let f = xhiveframework.model.get_doc(cdt, cdn); + f.custom = 1; + }, +}); + +// can't delete standard states +xhiveframework.ui.form.on("DocType State", { + before_states_remove: function (frm, doctype, name) { + let row = xhiveframework.get_doc(doctype, name); + if (!(row.custom || row.__islocal)) { + xhiveframework.msgprint(__("Cannot delete standard document state.")); + throw "cannot delete standard document state"; + } + }, + states_add: function (frm, cdt, cdn) { + let f = xhiveframework.model.get_doc(cdt, cdn); + f.custom = 1; + }, +}); + +xhiveframework.customize_form.save_customization = function (frm) { + if (frm.doc.doc_type) { + return frm.call({ + doc: frm.doc, + freeze: true, + freeze_message: __("Saving Customization..."), + btn: frm.page.btn_primary, + method: "save_customization", + callback: function (r) { + if (!r.exc) { + xhiveframework.customize_form.clear_locals_and_refresh(frm); + frm.script_manager.trigger("doc_type"); + } + }, + }); + } +}; + +xhiveframework.customize_form.update_fields_from_form_builder = function (frm) { + let form_builder = xhiveframework.form_builder; + if (form_builder?.store) { + let fields = form_builder.store.update_fields(); + + // if fields is a string, it means there is an error + if (typeof fields === "string") { + xhiveframework.throw(fields); + } + frm.refresh_fields(); + } +}; + +xhiveframework.customize_form.set_primary_action = function (frm) { + frm.page.set_primary_action(__("Update"), () => { + this.update_fields_from_form_builder(frm); + this.save_customization(frm); + }); +}; + +xhiveframework.customize_form.confirm = function (msg, frm) { + if (!frm.doc.doc_type) return; + + var d = new xhiveframework.ui.Dialog({ + title: "Reset To Defaults", + fields: [ + { + fieldtype: "HTML", + options: __("All customizations will be removed. Please confirm."), + }, + ], + primary_action: function () { + return frm.call({ + doc: frm.doc, + method: "reset_to_defaults", + callback: function (r) { + if (r.exc) { + xhiveframework.msgprint(r.exc); + } else { + d.hide(); + xhiveframework.show_alert({ + message: __("Customizations Reset"), + indicator: "green", + }); + xhiveframework.customize_form.clear_locals_and_refresh(frm); + } + }, + }); + }, + }); + + xhiveframework.customize_form.confirm.dialog = d; + d.show(); +}; + +xhiveframework.customize_form.clear_locals_and_refresh = function (frm) { + delete frm.doc.__unsaved; + // clear doctype from locals + xhiveframework.model.clear_doc("DocType", frm.doc.doc_type); + delete xhiveframework.meta.docfield_copy[frm.doc.doc_type]; + frm.refresh(); +}; + +function render_form_builder(frm) { + if (xhiveframework.form_builder && xhiveframework.form_builder.doctype === frm.doc.doc_type) { + xhiveframework.form_builder.setup_page_actions(); + xhiveframework.form_builder.store.fetch(); + return; + } + + if (xhiveframework.form_builder) { + xhiveframework.form_builder.wrapper = $(frm.fields_dict["form_builder"].wrapper); + xhiveframework.form_builder.frm = frm; + xhiveframework.form_builder.doctype = frm.doc.doc_type; + xhiveframework.form_builder.customize = true; + xhiveframework.form_builder.init(true); + xhiveframework.form_builder.store.fetch(); + } else { + xhiveframework.require("form_builder.bundle.js").then(() => { + xhiveframework.form_builder = new xhiveframework.ui.FormBuilder({ + wrapper: $(frm.fields_dict["form_builder"].wrapper), + frm: frm, + doctype: frm.doc.doc_type, + customize: true, + }); + }); + } +} + +extend_cscript(cur_frm.cscript, new xhiveframework.model.DocTypeController({ frm: cur_frm })); diff --git a/xhiveframework/custom/doctype/customize_form/customize_form.json b/xhiveframework/custom/doctype/customize_form/customize_form.json new file mode 100644 index 0000000..b3bb3ee --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/customize_form.json @@ -0,0 +1,426 @@ +{ + "actions": [], + "autoname": "DL.####", + "creation": "2013-01-29 17:55:08", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "details_tab", + "doc_type", + "properties", + "label", + "search_fields", + "column_break_5", + "istable", + "is_calendar_and_gantt", + "editable_grid", + "quick_entry", + "track_changes", + "track_views", + "allow_auto_repeat", + "allow_import", + "queue_in_background", + "naming_section", + "naming_rule", + "autoname", + "form_settings_section", + "image_field", + "max_attachments", + "column_break_21", + "allow_copy", + "make_attachments_public", + "view_settings_section", + "title_field", + "show_title_field_in_link", + "translated_doctype", + "default_print_format", + "default_view", + "force_re_route_to_default_view", + "column_break_29", + "show_preview_popup", + "email_settings_section", + "default_email_template", + "column_break_26", + "email_append_to", + "sender_field", + "sender_name_field", + "subject_field", + "section_break_8", + "sort_field", + "column_break_10", + "sort_order", + "document_actions_section", + "actions", + "document_links_section", + "links", + "document_states_section", + "states", + "fields_section_break", + "fields", + "form_tab", + "form_builder" + ], + "fields": [ + { + "fieldname": "naming_rule", + "fieldtype": "Select", + "label": "Naming Rule", + "length": 40, + "options": "\nSet by user\nBy fieldname\nBy \"Naming Series\" field\nExpression\nExpression (old style)\nRandom\nBy script" + }, + { + "fieldname": "doc_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Enter Form Type", + "options": "DocType" + }, + { + "depends_on": "doc_type", + "fieldname": "properties", + "fieldtype": "Section Break" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "label": "Change Label (via Custom Translation)" + }, + { + "fieldname": "default_print_format", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Default Print Format", + "options": "Print Format" + }, + { + "fieldname": "max_attachments", + "fieldtype": "Int", + "label": "Max Attachments" + }, + { + "default": "0", + "fieldname": "allow_copy", + "fieldtype": "Check", + "label": "Hide Copy" + }, + { + "default": "0", + "fieldname": "istable", + "fieldtype": "Check", + "label": "Is Table", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "istable", + "fieldname": "editable_grid", + "fieldtype": "Check", + "label": "Editable Grid" + }, + { + "default": "1", + "fieldname": "quick_entry", + "fieldtype": "Check", + "label": "Quick Entry" + }, + { + "default": "0", + "fieldname": "track_changes", + "fieldtype": "Check", + "label": "Track Changes" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "description": "Use this fieldname to generate title", + "fieldname": "title_field", + "fieldtype": "Data", + "label": "Title Field" + }, + { + "description": "Must be of type \"Attach Image\"", + "fieldname": "image_field", + "fieldtype": "Data", + "label": "Image Field" + }, + { + "description": "Fields separated by comma (,) will be included in the \"Search By\" list of Search dialog box", + "fieldname": "search_fields", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Search Fields" + }, + { + "collapsible": 1, + "depends_on": "doc_type", + "fieldname": "section_break_8", + "fieldtype": "Section Break", + "label": "List Settings" + }, + { + "fieldname": "sort_field", + "fieldtype": "Select", + "label": "Sort Field" + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "fieldname": "sort_order", + "fieldtype": "Select", + "label": "Sort Order", + "options": "ASC\nDESC" + }, + { + "collapsible": 1, + "depends_on": "doc_type", + "fieldname": "fields_section_break", + "fieldtype": "Section Break", + "label": "Fields" + }, + { + "allow_bulk_edit": 1, + "fieldname": "fields", + "fieldtype": "Table", + "label": "Fields", + "options": "Customize Form Field", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "track_views", + "fieldtype": "Check", + "label": "Track Views" + }, + { + "default": "0", + "fieldname": "allow_auto_repeat", + "fieldtype": "Check", + "label": "Allow Auto Repeat" + }, + { + "default": "0", + "fieldname": "allow_import", + "fieldtype": "Check", + "label": "Allow Import (via Data Import Tool)" + }, + { + "depends_on": "email_append_to", + "fieldname": "subject_field", + "fieldtype": "Data", + "label": "Subject Field" + }, + { + "depends_on": "email_append_to", + "fieldname": "sender_field", + "fieldtype": "Data", + "label": "Sender Email Field", + "mandatory_depends_on": "email_append_to" + }, + { + "default": "0", + "fieldname": "email_append_to", + "fieldtype": "Check", + "label": "Allow document creation via Email" + }, + { + "default": "0", + "fieldname": "show_preview_popup", + "fieldtype": "Check", + "label": "Show Preview Popup" + }, + { + "collapsible": 1, + "depends_on": "doc_type", + "fieldname": "view_settings_section", + "fieldtype": "Section Break", + "label": "View Settings" + }, + { + "fieldname": "column_break_29", + "fieldtype": "Column Break" + }, + { + "collapsible": 1, + "collapsible_depends_on": "email_append_to", + "depends_on": "doc_type", + "fieldname": "email_settings_section", + "fieldtype": "Section Break", + "label": "Email Settings" + }, + { + "collapsible": 1, + "collapsible_depends_on": "links", + "depends_on": "doc_type", + "fieldname": "document_links_section", + "fieldtype": "Section Break", + "label": "Document Links" + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "DocType Link" + }, + { + "collapsible": 1, + "collapsible_depends_on": "actions", + "depends_on": "doc_type", + "fieldname": "document_actions_section", + "fieldtype": "Section Break", + "label": "Document Actions" + }, + { + "fieldname": "actions", + "fieldtype": "Table", + "label": "Actions", + "options": "DocType Action" + }, + { + "fieldname": "default_email_template", + "fieldtype": "Link", + "label": "Default Email Template", + "options": "Email Template" + }, + { + "fieldname": "column_break_26", + "fieldtype": "Column Break" + }, + { + "collapsible": 1, + "depends_on": "doc_type", + "fieldname": "naming_section", + "fieldtype": "Section Break", + "label": "Naming" + }, + { + "description": "Naming Options:\n
    1. field:[fieldname] - By Field
    2. naming_series: - By Naming Series (field called naming_series must be present)
    3. Prompt - Prompt user for a name
    4. [series] - Series by prefix (separated by a dot); for example PRE.#####
    5. \n
    6. format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####} - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.
    ", + "fieldname": "autoname", + "fieldtype": "Data", + "label": "Auto Name" + }, + { + "collapsible": 1, + "collapsible_depends_on": "states", + "depends_on": "doc_type", + "fieldname": "document_states_section", + "fieldtype": "Section Break", + "label": "Document States" + }, + { + "fieldname": "states", + "fieldtype": "Table", + "label": "States", + "options": "DocType State" + }, + { + "default": "0", + "fieldname": "show_title_field_in_link", + "fieldtype": "Check", + "label": "Show Title in Link Fields" + }, + { + "default": "0", + "fieldname": "translated_doctype", + "fieldtype": "Check", + "label": "Translate Link Fields" + }, + { + "collapsible": 1, + "fieldname": "form_settings_section", + "fieldtype": "Section Break", + "label": "Form Settings" + }, + { + "fieldname": "column_break_21", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "make_attachments_public", + "fieldtype": "Check", + "label": "Make Attachments Public by Default" + }, + { + "default": "0", + "description": "Enabling this will submit documents in background", + "fieldname": "queue_in_background", + "fieldtype": "Check", + "label": "Queue in Background (BETA)" + }, + { + "fieldname": "default_view", + "fieldtype": "Select", + "label": "Default View" + }, + { + "default": "0", + "depends_on": "default_view", + "fieldname": "force_re_route_to_default_view", + "fieldtype": "Check", + "label": "Force Re-route to Default View" + }, + { + "default": "0", + "description": "Enables Calendar and Gantt views.", + "fieldname": "is_calendar_and_gantt", + "fieldtype": "Check", + "label": "Is Calendar and Gantt" + }, + { + "fieldname": "form_builder", + "fieldtype": "HTML", + "label": "Form Builder" + }, + { + "fieldname": "form_tab", + "fieldtype": "Tab Break", + "label": "Form" + }, + { + "fieldname": "details_tab", + "fieldtype": "Tab Break", + "label": "Details" + }, + { + "depends_on": "email_append_to", + "fieldname": "sender_name_field", + "fieldtype": "Data", + "label": "Sender Name Field" + } + ], + "hide_toolbar": 1, + "icon": "fa fa-glass", + "idx": 1, + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2023-12-01 18:18:23.086134", + "modified_by": "Administrator", + "module": "Custom", + "name": "Customize Form", + "naming_rule": "Expression (old style)", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "search_fields": "doc_type", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/custom/doctype/customize_form/customize_form.py b/xhiveframework/custom/doctype/customize_form/customize_form.py new file mode 100644 index 0000000..3e48965 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/customize_form.py @@ -0,0 +1,794 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE + +""" + Customize Form is a Single DocType used to mask the Property Setter + Thus providing a better UI from user perspective +""" +import json + +import xhiveframework +import xhiveframework.translate +from xhiveframework import _ +from xhiveframework.core.doctype.doctype.doctype import ( + check_email_append_to, + validate_autoincrement_autoname, + validate_fields_for_doctype, + validate_series, +) +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_field +from xhiveframework.custom.doctype.property_setter.property_setter import delete_property_setter +from xhiveframework.model import core_doctypes_list, no_value_fields +from xhiveframework.model.docfield import supports_translation +from xhiveframework.model.document import Document +from xhiveframework.utils import cint + + +class CustomizeForm(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.doctype_action.doctype_action import DocTypeAction + from xhiveframework.core.doctype.doctype_link.doctype_link import DocTypeLink + from xhiveframework.core.doctype.doctype_state.doctype_state import DocTypeState + from xhiveframework.custom.doctype.customize_form_field.customize_form_field import CustomizeFormField + from xhiveframework.types import DF + + actions: DF.Table[DocTypeAction] + allow_auto_repeat: DF.Check + allow_copy: DF.Check + allow_import: DF.Check + autoname: DF.Data | None + default_email_template: DF.Link | None + default_print_format: DF.Link | None + default_view: DF.Literal[None] + doc_type: DF.Link | None + editable_grid: DF.Check + email_append_to: DF.Check + fields: DF.Table[CustomizeFormField] + force_re_route_to_default_view: DF.Check + image_field: DF.Data | None + is_calendar_and_gantt: DF.Check + istable: DF.Check + label: DF.Data | None + links: DF.Table[DocTypeLink] + make_attachments_public: DF.Check + max_attachments: DF.Int + naming_rule: DF.Literal[ + "", + "Set by user", + "By fieldname", + 'By "Naming Series" field', + "Expression", + "Expression (old style)", + "Random", + "By script", + ] + queue_in_background: DF.Check + quick_entry: DF.Check + search_fields: DF.Data | None + sender_field: DF.Data | None + sender_name_field: DF.Data | None + show_preview_popup: DF.Check + show_title_field_in_link: DF.Check + sort_field: DF.Literal[None] + sort_order: DF.Literal["ASC", "DESC"] + states: DF.Table[DocTypeState] + subject_field: DF.Data | None + title_field: DF.Data | None + track_changes: DF.Check + track_views: DF.Check + translated_doctype: DF.Check + # end: auto-generated types + + def on_update(self): + xhiveframework.db.delete("Singles", {"doctype": "Customize Form"}) + xhiveframework.db.delete("Customize Form Field") + + @xhiveframework.whitelist() + def fetch_to_customize(self): + self.clear_existing_doc() + if not self.doc_type: + return + + meta = xhiveframework.get_meta(self.doc_type, cached=False) + + self.validate_doctype(meta) + + # load the meta properties on the customize (self) object + self.load_properties(meta) + + # load custom translation + translation = self.get_name_translation() + self.label = translation.translated_text if translation else "" + + self.create_auto_repeat_custom_field_if_required(meta) + + # NOTE doc (self) is sent to clientside by run_method + + def validate_doctype(self, meta): + """ + Check if the doctype is allowed to be customized. + """ + if self.doc_type in core_doctypes_list: + xhiveframework.throw(_("Core DocTypes cannot be customized.")) + + if meta.issingle: + xhiveframework.throw(_("Single DocTypes cannot be customized.")) + + if meta.custom: + xhiveframework.throw(_("Only standard DocTypes are allowed to be customized from Customize Form.")) + + def load_properties(self, meta): + """ + Load the customize object (this) with the metadata properties + """ + # doctype properties + for prop in doctype_properties: + self.set(prop, meta.get(prop)) + + for d in meta.get("fields"): + new_d = { + "fieldname": d.fieldname, + "is_custom_field": d.get("is_custom_field"), + "is_system_generated": d.get("is_system_generated"), + "name": d.name, + } + for prop in docfield_properties: + new_d[prop] = d.get(prop) + self.append("fields", new_d) + + for fieldname in ("links", "actions", "states"): + for d in meta.get(fieldname): + self.append(fieldname, d) + + def create_auto_repeat_custom_field_if_required(self, meta): + """ + Create auto repeat custom field if it's not already present + """ + if self.allow_auto_repeat: + all_fields = [df.fieldname for df in meta.fields] + + if "auto_repeat" in all_fields: + return + + insert_after = self.fields[len(self.fields) - 1].fieldname + create_custom_field( + self.doc_type, + dict( + fieldname="auto_repeat", + label="Auto Repeat", + fieldtype="Link", + options="Auto Repeat", + insert_after=insert_after, + read_only=1, + no_copy=1, + print_hide=1, + ), + ) + + def get_name_translation(self): + """Get translation object if exists of current doctype name in the default language""" + return xhiveframework.get_value( + "Translation", + {"source_text": self.doc_type, "language": xhiveframework.local.lang or "en"}, + ["name", "translated_text"], + as_dict=True, + ) + + def set_name_translation(self): + """Create, update custom translation for this doctype""" + current = self.get_name_translation() + if not self.label: + if current: + # clear translation + xhiveframework.delete_doc("Translation", current.name) + return + + if not current: + xhiveframework.get_doc( + { + "doctype": "Translation", + "source_text": self.doc_type, + "translated_text": self.label, + "language_code": xhiveframework.local.lang or "en", + } + ).insert() + return + + if self.label != current.translated_text: + xhiveframework.db.set_value("Translation", current.name, "translated_text", self.label) + xhiveframework.translate.clear_cache() + + def clear_existing_doc(self): + doc_type = self.doc_type + + for fieldname in self.meta.get_valid_columns(): + self.set(fieldname, None) + + for df in self.meta.get_table_fields(): + self.set(df.fieldname, []) + + self.doc_type = doc_type + self.name = "Customize Form" + + @xhiveframework.whitelist() + def save_customization(self): + if not self.doc_type: + return + + validate_series(self, self.autoname, self.doc_type) + validate_autoincrement_autoname(self) + self.flags.update_db = False + self.flags.rebuild_doctype_for_global_search = False + self.set_property_setters() + self.update_custom_fields() + self.set_name_translation() + validate_fields_for_doctype(self.doc_type) + check_email_append_to(self) + + if self.flags.update_db: + try: + xhiveframework.db.updatedb(self.doc_type) + except Exception as e: + if xhiveframework.db.is_db_table_size_limit(e): + xhiveframework.throw( + _("You have hit the row size limit on database table: {0}").format( + "" + "Maximum Number of Fields in a Form" + ), + title=_("Database Table Row Size Limit"), + ) + raise + + if not hasattr(self, "hide_success") or not self.hide_success: + xhiveframework.msgprint(_("{0} updated").format(_(self.doc_type)), alert=True) + xhiveframework.clear_cache(doctype=self.doc_type) + self.fetch_to_customize() + + if self.flags.rebuild_doctype_for_global_search: + xhiveframework.enqueue( + "xhiveframework.utils.global_search.rebuild_for_doctype", + doctype=self.doc_type, + enqueue_after_commit=True, + ) + + def set_property_setters(self): + meta = xhiveframework.get_meta(self.doc_type) + + # doctype + self.set_property_setters_for_doctype(meta) + + # docfield + for df in self.get("fields"): + meta_df = meta.get("fields", {"fieldname": df.fieldname}) + if not meta_df or not is_standard_or_system_generated_field(meta_df[0]): + continue + + self.set_property_setters_for_docfield(meta, df, meta_df) + + # action and links + self.set_property_setters_for_actions_and_links(meta) + + def set_property_setter_for_field_order(self, meta): + new_order = [df.fieldname for df in self.fields] + existing_order = getattr(meta, "field_order", None) + default_order = [ + fieldname for fieldname, df in meta._fields.items() if not getattr(df, "is_custom_field", False) + ] + + if new_order == default_order: + if existing_order: + delete_property_setter(self.doc_type, "field_order") + + return + + if existing_order and new_order == json.loads(existing_order): + return + + xhiveframework.make_property_setter( + { + "doctype": self.doc_type, + "doctype_or_field": "DocType", + "property": "field_order", + "value": json.dumps(new_order), + }, + is_system_generated=False, + ) + + def set_property_setters_for_doctype(self, meta): + for prop, prop_type in doctype_properties.items(): + if self.get(prop) != meta.get(prop): + self.make_property_setter(prop, self.get(prop), prop_type) + + self.set_property_setter_for_field_order(meta) + + def set_property_setters_for_docfield(self, meta, df, meta_df): + for prop, prop_type in docfield_properties.items(): + if prop != "idx" and (df.get(prop) or "") != (meta_df[0].get(prop) or ""): + if not self.allow_property_change(prop, meta_df, df): + continue + + self.make_property_setter(prop, df.get(prop), prop_type, fieldname=df.fieldname) + + def allow_property_change(self, prop, meta_df, df): + if prop == "fieldtype": + self.validate_fieldtype_change(df, meta_df[0].get(prop), df.get(prop)) + + elif prop == "length": + old_value_length = cint(meta_df[0].get(prop)) + new_value_length = cint(df.get(prop)) + + if new_value_length and (old_value_length > new_value_length): + self.check_length_for_fieldtypes.append({"df": df, "old_value": meta_df[0].get(prop)}) + self.validate_fieldtype_length() + else: + self.flags.update_db = True + + elif prop == "allow_on_submit" and df.get(prop): + if not xhiveframework.db.get_value( + "DocField", {"parent": self.doc_type, "fieldname": df.fieldname}, "allow_on_submit" + ): + xhiveframework.msgprint( + _("Row {0}: Not allowed to enable Allow on Submit for standard fields").format(df.idx) + ) + return False + + elif prop == "reqd" and ( + ( + xhiveframework.db.get_value("DocField", {"parent": self.doc_type, "fieldname": df.fieldname}, "reqd") + == 1 + ) + and (df.get(prop) == 0) + ): + xhiveframework.msgprint(_("Row {0}: Not allowed to disable Mandatory for standard fields").format(df.idx)) + return False + + elif ( + prop == "in_list_view" + and df.get(prop) + and df.fieldtype != "Attach Image" + and df.fieldtype in no_value_fields + ): + xhiveframework.msgprint( + _("'In List View' not allowed for type {0} in row {1}").format(df.fieldtype, df.idx) + ) + return False + + elif ( + prop == "precision" + and cint(df.get("precision")) > 6 + and cint(df.get("precision")) > cint(meta_df[0].get("precision")) + ): + self.flags.update_db = True + + elif prop == "unique": + self.flags.update_db = True + + elif ( + prop == "read_only" + and cint(df.get("read_only")) == 0 + and xhiveframework.db.get_value( + "DocField", {"parent": self.doc_type, "fieldname": df.fieldname}, "read_only" + ) + == 1 + ): + # if docfield has read_only checked and user is trying to make it editable, don't allow it + xhiveframework.msgprint(_("You cannot unset 'Read Only' for field {0}").format(df.label)) + return False + + elif prop == "options" and df.get("fieldtype") not in ALLOWED_OPTIONS_CHANGE: + xhiveframework.msgprint(_("You can't set 'Options' for field {0}").format(df.label)) + return False + + elif prop == "translatable" and not supports_translation(df.get("fieldtype")): + xhiveframework.msgprint(_("You can't set 'Translatable' for field {0}").format(df.label)) + return False + + elif prop == "in_global_search" and df.in_global_search != meta_df[0].get("in_global_search"): + self.flags.rebuild_doctype_for_global_search = True + + return True + + def set_property_setters_for_actions_and_links(self, meta): + """ + Apply property setters or create custom records for DocType Action and DocType Link + """ + for doctype, fieldname, field_map in ( + ("DocType Link", "links", doctype_link_properties), + ("DocType Action", "actions", doctype_action_properties), + ("DocType State", "states", doctype_state_properties), + ): + has_custom = False + items = [] + for i, d in enumerate(self.get(fieldname) or []): + d.idx = i + if xhiveframework.db.exists(doctype, d.name) and not d.custom: + # check property and apply property setter + original = xhiveframework.get_doc(doctype, d.name) + for prop, prop_type in field_map.items(): + if d.get(prop) != original.get(prop): + self.make_property_setter( + prop, d.get(prop), prop_type, apply_on=doctype, row_name=d.name + ) + items.append(d.name) + else: + # custom - just insert/update + d.parent = self.doc_type + d.custom = 1 + d.save(ignore_permissions=True) + has_custom = True + items.append(d.name) + + self.update_order_property_setter(has_custom, fieldname) + self.clear_removed_items(doctype, items) + + def update_order_property_setter(self, has_custom, fieldname): + """ + We need to maintain the order of the link/actions if the user has shuffled them. + So we create a new property (ex `links_order`) to keep a list of items. + """ + property_name = f"{fieldname}_order" + if has_custom: + # save the order of the actions and links + self.make_property_setter( + property_name, json.dumps([d.name for d in self.get(fieldname)]), "Small Text" + ) + else: + xhiveframework.db.delete("Property Setter", dict(property=property_name, doc_type=self.doc_type)) + + def clear_removed_items(self, doctype, items): + """ + Clear rows that do not appear in `items`. These have been removed by the user. + """ + if items: + xhiveframework.db.delete(doctype, dict(parent=self.doc_type, custom=1, name=("not in", items))) + else: + xhiveframework.db.delete(doctype, dict(parent=self.doc_type, custom=1)) + + def update_custom_fields(self): + for i, df in enumerate(self.get("fields")): + if is_standard_or_system_generated_field(df): + continue + + if not xhiveframework.db.exists("Custom Field", {"dt": self.doc_type, "fieldname": df.fieldname}): + self.add_custom_field(df, i) + self.flags.update_db = True + else: + self.update_in_custom_field(df, i) + + self.delete_custom_fields() + + def add_custom_field(self, df, i): + d = xhiveframework.new_doc("Custom Field") + + d.dt = self.doc_type + + for prop in docfield_properties: + d.set(prop, df.get(prop)) + + if i != 0: + d.insert_after = self.fields[i - 1].fieldname + d.idx = i + + d.insert() + df.fieldname = d.fieldname + + if df.get("in_global_search"): + self.flags.rebuild_doctype_for_global_search = True + + def update_in_custom_field(self, df, i): + meta = xhiveframework.get_meta(self.doc_type) + meta_df = meta.get("fields", {"fieldname": df.fieldname}) + if not meta_df or is_standard_or_system_generated_field(meta_df[0]): + # not a custom field + return + + custom_field = xhiveframework.get_doc("Custom Field", meta_df[0].name) + changed = False + for prop in docfield_properties: + if df.get(prop) != custom_field.get(prop): + if prop == "fieldtype": + self.validate_fieldtype_change(df, meta_df[0].get(prop), df.get(prop)) + if prop == "in_global_search": + self.flags.rebuild_doctype_for_global_search = True + + custom_field.set(prop, df.get(prop)) + changed = True + + # check and update `insert_after` property + if i != 0: + insert_after = self.fields[i - 1].fieldname + if custom_field.insert_after != insert_after: + custom_field.insert_after = insert_after + custom_field.idx = i + changed = True + + if changed: + custom_field.db_update() + self.flags.update_db = True + # custom_field.save() + + def delete_custom_fields(self): + meta = xhiveframework.get_meta(self.doc_type) + fields_to_remove = {df.fieldname for df in meta.get("fields")} - { + df.fieldname for df in self.get("fields") + } + for fieldname in fields_to_remove: + df = meta.get("fields", {"fieldname": fieldname})[0] + if not is_standard_or_system_generated_field(df): + xhiveframework.delete_doc("Custom Field", df.name) + + def make_property_setter(self, prop, value, property_type, fieldname=None, apply_on=None, row_name=None): + delete_property_setter(self.doc_type, prop, fieldname, row_name) + + property_value = self.get_existing_property_value(prop, fieldname) + + if property_value == value: + return + + if not apply_on: + apply_on = "DocField" if fieldname else "DocType" + + # create a new property setter + xhiveframework.make_property_setter( + { + "doctype": self.doc_type, + "doctype_or_field": apply_on, + "fieldname": fieldname, + "row_name": row_name, + "property": prop, + "value": value, + "property_type": property_type, + }, + is_system_generated=False, + ) + + def get_existing_property_value(self, property_name, fieldname=None): + # check if there is any need to make property setter! + if fieldname: + property_value = xhiveframework.db.get_value( + "DocField", {"parent": self.doc_type, "fieldname": fieldname}, property_name + ) + else: + if xhiveframework.db.has_column("DocType", property_name): + property_value = xhiveframework.db.get_value("DocType", self.doc_type, property_name) + else: + property_value = None + + 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(xhiveframework.db.type_map.get(old_value)[1]) + new_value_length = cint(xhiveframework.db.type_map.get(new_value)[1]) + + # Ignore fieldtype check validation if new field type has unspecified maxlength + # Changes like DATA to TEXT, where new_value_lenth equals 0 will not be validated + if new_value_length and (old_value_length > new_value_length): + self.check_length_for_fieldtypes.append({"df": df, "old_value": old_value}) + self.validate_fieldtype_length() + else: + self.flags.update_db = True + + else: + xhiveframework.throw( + _("Fieldtype cannot be changed from {0} to {1} in row {2}").format( + old_value, new_value, df.idx + ) + ) + + def validate_fieldtype_length(self): + for field in self.check_length_for_fieldtypes: + df = field.get("df") + max_length = cint(xhiveframework.db.type_map.get(df.fieldtype)[1]) + fieldname = df.fieldname + docs = xhiveframework.db.sql( + f""" + SELECT name, {fieldname}, LENGTH({fieldname}) AS len + FROM `tab{self.doc_type}` + WHERE LENGTH({fieldname}) > {max_length} + """, + as_dict=True, + ) + label = df.label + links_str = ", ".join(xhiveframework.utils.get_link_to_form(self.doc_type, doc.name) for doc in docs) + + if docs: + xhiveframework.throw( + _( + "Value for field {0} is too long in {1}. Length should be lesser than {2} characters" + ).format(xhiveframework.bold(label), links_str, xhiveframework.bold(max_length)), + title=_("Data Too Long"), + is_minimizable=len(docs) > 1, + ) + + self.flags.update_db = True + + @xhiveframework.whitelist() + def reset_to_defaults(self): + if not self.doc_type: + return + + reset_customization(self.doc_type) + self.fetch_to_customize() + + @xhiveframework.whitelist() + def reset_layout(self): + if not self.doc_type: + return + + property_setters = xhiveframework.get_all( + "Property Setter", + filters={"doc_type": self.doc_type, "property": ("in", ("field_order", "insert_after"))}, + pluck="name", + ) + + if not property_setters: + return + + xhiveframework.db.delete("Property Setter", {"name": ("in", property_setters)}) + xhiveframework.clear_cache(doctype=self.doc_type) + self.fetch_to_customize() + + @classmethod + def allow_fieldtype_change(self, old_type: str, new_type: str) -> bool: + """allow type change, if both old_type and new_type are in same field group. + field groups are defined in ALLOWED_FIELDTYPE_CHANGE variables. + """ + + def in_field_group(group): + return (old_type in group) and (new_type in group) + + return any(map(in_field_group, ALLOWED_FIELDTYPE_CHANGE)) + + +def reset_customization(doctype): + setters = xhiveframework.get_all( + "Property Setter", + filters={ + "doc_type": doctype, + "field_name": ["!=", "naming_series"], + "property": ["!=", "options"], + "is_system_generated": False, + }, + pluck="name", + ) + + for setter in setters: + xhiveframework.delete_doc("Property Setter", setter) + + custom_fields = xhiveframework.get_all( + "Custom Field", filters={"dt": doctype, "is_system_generated": False}, pluck="name" + ) + + for field in custom_fields: + xhiveframework.delete_doc("Custom Field", field) + + xhiveframework.clear_cache(doctype=doctype) + + +def is_standard_or_system_generated_field(df): + return not df.get("is_custom_field") or df.get("is_system_generated") + + +doctype_properties = { + "search_fields": "Data", + "title_field": "Data", + "image_field": "Data", + "sort_field": "Data", + "sort_order": "Data", + "default_print_format": "Data", + "allow_copy": "Check", + "istable": "Check", + "quick_entry": "Check", + "queue_in_background": "Check", + "editable_grid": "Check", + "max_attachments": "Int", + "make_attachments_public": "Check", + "track_changes": "Check", + "track_views": "Check", + "allow_auto_repeat": "Check", + "allow_import": "Check", + "show_preview_popup": "Check", + "default_email_template": "Data", + "email_append_to": "Check", + "subject_field": "Data", + "sender_field": "Data", + "naming_rule": "Data", + "autoname": "Data", + "show_title_field_in_link": "Check", + "is_calendar_and_gantt": "Check", + "default_view": "Select", + "force_re_route_to_default_view": "Check", + "translated_doctype": "Check", +} + +docfield_properties = { + "idx": "Int", + "label": "Data", + "fieldtype": "Select", + "options": "Text", + "sort_options": "Check", + "fetch_from": "Small Text", + "fetch_if_empty": "Check", + "show_dashboard": "Check", + "permlevel": "Int", + "width": "Data", + "print_width": "Data", + "non_negative": "Check", + "reqd": "Check", + "unique": "Check", + "ignore_user_permissions": "Check", + "in_list_view": "Check", + "in_standard_filter": "Check", + "in_global_search": "Check", + "in_preview": "Check", + "bold": "Check", + "no_copy": "Check", + "ignore_xss_filter": "Check", + "hidden": "Check", + "collapsible": "Check", + "collapsible_depends_on": "Data", + "print_hide": "Check", + "print_hide_if_no_value": "Check", + "report_hide": "Check", + "allow_on_submit": "Check", + "translatable": "Check", + "mandatory_depends_on": "Data", + "read_only_depends_on": "Data", + "depends_on": "Data", + "description": "Text", + "default": "Text", + "precision": "Select", + "read_only": "Check", + "length": "Int", + "columns": "Int", + "remember_last_selected_value": "Check", + "allow_bulk_edit": "Check", + "auto_repeat": "Link", + "allow_in_quick_entry": "Check", + "hide_border": "Check", + "hide_days": "Check", + "hide_seconds": "Check", + "is_virtual": "Check", +} + +doctype_link_properties = { + "link_doctype": "Link", + "link_fieldname": "Data", + "group": "Data", + "hidden": "Check", +} + +doctype_action_properties = { + "label": "Link", + "action_type": "Select", + "action": "Small Text", + "group": "Data", + "hidden": "Check", +} + +doctype_state_properties = {"title": "Data", "color": "Select"} + + +ALLOWED_FIELDTYPE_CHANGE = ( + ("Currency", "Float", "Percent"), + ("Small Text", "Data"), + ("Text", "Data"), + ("Text", "Text Editor", "Code", "Signature", "HTML Editor"), + ("Data", "Select"), + ("Text", "Small Text", "Long Text"), + ("Text", "Data", "Barcode"), + ("Code", "Geolocation"), + ("Table", "Table MultiSelect"), +) + +ALLOWED_OPTIONS_CHANGE = ("Read Only", "HTML", "Data") diff --git a/xhiveframework/custom/doctype/customize_form/test_customize_form.py b/xhiveframework/custom/doctype/customize_form/test_customize_form.py new file mode 100644 index 0000000..29c18d8 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form/test_customize_form.py @@ -0,0 +1,427 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.core.doctype.doctype.doctype import InvalidFieldNameError +from xhiveframework.core.doctype.doctype.test_doctype import new_doctype +from xhiveframework.test_runner import make_test_records_for_doctype +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["Custom Field", "Property Setter"] + + +class TestCustomizeForm(XhiveFrameworkTestCase): + def insert_custom_field(self): + xhiveframework.delete_doc_if_exists("Custom Field", "Event-custom_test_field") + self.field = xhiveframework.get_doc( + { + "doctype": "Custom Field", + "fieldname": "custom_test_field", + "dt": "Event", + "label": "Test Custom Field", + "description": "A Custom Field for Testing", + "fieldtype": "Select", + "in_list_view": 1, + "options": "\nCustom 1\nCustom 2\nCustom 3", + "default": "Custom 3", + "insert_after": xhiveframework.get_meta("Event").fields[-1].fieldname, + } + ).insert() + + def setUp(self): + self.insert_custom_field() + xhiveframework.db.delete("Property Setter", dict(doc_type="Event")) + xhiveframework.db.commit() + xhiveframework.clear_cache(doctype="Event") + + def tearDown(self): + xhiveframework.delete_doc("Custom Field", self.field.name) + xhiveframework.db.commit() + xhiveframework.clear_cache(doctype="Event") + + def get_customize_form(self, doctype=None): + d = xhiveframework.get_doc("Customize Form") + if doctype: + d.doc_type = doctype + d.run_method("fetch_to_customize") + return d + + def test_fetch_to_customize(self): + d = self.get_customize_form() + self.assertEqual(d.doc_type, None) + self.assertEqual(len(d.get("fields")), 0) + + d = self.get_customize_form("Event") + self.assertEqual(d.doc_type, "Event") + self.assertEqual(len(d.get("fields")), 38) + + d = self.get_customize_form("Event") + self.assertEqual(d.doc_type, "Event") + + self.assertEqual(len(d.get("fields")), len(xhiveframework.get_doc("DocType", d.doc_type).fields) + 1) + self.assertEqual(d.get("fields")[-1].fieldname, self.field.fieldname) + self.assertEqual(d.get("fields", {"fieldname": "event_type"})[0].in_list_view, 1) + + return d + + def test_save_customization_property(self): + d = self.get_customize_form("Event") + self.assertEqual( + xhiveframework.db.get_value("Property Setter", {"doc_type": "Event", "property": "allow_copy"}, "value"), + None, + ) + + d.allow_copy = 1 + d.run_method("save_customization") + self.assertEqual( + xhiveframework.db.get_value("Property Setter", {"doc_type": "Event", "property": "allow_copy"}, "value"), + "1", + ) + + d.allow_copy = 0 + d.run_method("save_customization") + self.assertEqual( + xhiveframework.db.get_value("Property Setter", {"doc_type": "Event", "property": "allow_copy"}, "value"), + None, + ) + + def test_save_customization_field_property(self): + d = self.get_customize_form("Event") + self.assertEqual( + xhiveframework.db.get_value( + "Property Setter", + {"doc_type": "Event", "property": "reqd", "field_name": "repeat_this_event"}, + "value", + ), + None, + ) + + repeat_this_event_field = d.get("fields", {"fieldname": "repeat_this_event"})[0] + repeat_this_event_field.reqd = 1 + d.run_method("save_customization") + self.assertEqual( + xhiveframework.db.get_value( + "Property Setter", + {"doc_type": "Event", "property": "reqd", "field_name": "repeat_this_event"}, + "value", + ), + "1", + ) + + repeat_this_event_field = d.get("fields", {"fieldname": "repeat_this_event"})[0] + repeat_this_event_field.reqd = 0 + d.run_method("save_customization") + self.assertEqual( + xhiveframework.db.get_value( + "Property Setter", + {"doc_type": "Event", "property": "reqd", "field_name": "repeat_this_event"}, + "value", + ), + None, + ) + + def test_save_customization_custom_field_property(self): + d = self.get_customize_form("Event") + self.assertEqual(xhiveframework.db.get_value("Custom Field", self.field.name, "reqd"), 0) + + custom_field = d.get("fields", {"fieldname": self.field.fieldname})[0] + custom_field.reqd = 1 + custom_field.no_copy = 1 + d.run_method("save_customization") + self.assertEqual(xhiveframework.db.get_value("Custom Field", self.field.name, "reqd"), 1) + self.assertEqual(xhiveframework.db.get_value("Custom Field", self.field.name, "no_copy"), 1) + + custom_field = d.get("fields", {"is_custom_field": True})[0] + custom_field.reqd = 0 + custom_field.no_copy = 0 + d.run_method("save_customization") + self.assertEqual(xhiveframework.db.get_value("Custom Field", self.field.name, "reqd"), 0) + self.assertEqual(xhiveframework.db.get_value("Custom Field", self.field.name, "no_copy"), 0) + + def test_save_customization_new_field(self): + d = self.get_customize_form("Event") + last_fieldname = d.fields[-1].fieldname + d.append( + "fields", + { + "label": "Test Add Custom Field Via Customize Form", + "fieldtype": "Data", + "is_custom_field": 1, + }, + ) + d.run_method("save_customization") + + custom_field_name = "Event-custom_test_add_custom_field_via_customize_form" + self.assertEqual( + xhiveframework.db.get_value("Custom Field", custom_field_name, "fieldtype"), + "Data", + ) + + self.assertEqual( + xhiveframework.db.get_value("Custom Field", custom_field_name, "insert_after"), + last_fieldname, + ) + + xhiveframework.delete_doc("Custom Field", custom_field_name) + self.assertEqual(xhiveframework.db.get_value("Custom Field", custom_field_name), None) + + def test_save_customization_remove_field(self): + d = self.get_customize_form("Event") + custom_field = d.get("fields", {"fieldname": self.field.fieldname})[0] + d.get("fields").remove(custom_field) + d.run_method("save_customization") + + self.assertEqual(xhiveframework.db.get_value("Custom Field", custom_field.name), None) + + xhiveframework.local.test_objects["Custom Field"] = [] + make_test_records_for_doctype("Custom Field") + + def test_reset_to_defaults(self): + d = xhiveframework.get_doc("Customize Form") + d.doc_type = "Event" + d.run_method("reset_to_defaults") + + self.assertEqual(d.get("fields", {"fieldname": "repeat_this_event"})[0].in_list_view, 0) + + xhiveframework.local.test_objects["Property Setter"] = [] + make_test_records_for_doctype("Property Setter") + + def test_set_allow_on_submit(self): + d = self.get_customize_form("Event") + d.get("fields", {"fieldname": "subject"})[0].allow_on_submit = 1 + d.get("fields", {"fieldname": "custom_test_field"})[0].allow_on_submit = 1 + d.run_method("save_customization") + + d = self.get_customize_form("Event") + + # don't allow for standard fields + self.assertEqual(d.get("fields", {"fieldname": "subject"})[0].allow_on_submit or 0, 0) + + # allow for custom field + self.assertEqual(d.get("fields", {"fieldname": "custom_test_field"})[0].allow_on_submit, 1) + + def test_title_field_pattern(self): + d = self.get_customize_form("Web Form") + + df = d.get("fields", {"fieldname": "title"})[0] + + # invalid fieldname + df.default = """{doc_type} - {introduction_test}""" + self.assertRaises(InvalidFieldNameError, d.run_method, "save_customization") + + # space in formatter + df.default = """{doc_type} - {introduction text}""" + self.assertRaises(InvalidFieldNameError, d.run_method, "save_customization") + + # valid fieldname + df.default = """{doc_type} - {introduction_text}""" + d.run_method("save_customization") + + # valid fieldname with escaped curlies + df.default = """{{ {doc_type} }} - {introduction_text}""" + d.run_method("save_customization") + + # undo + df.default = None + d.run_method("save_customization") + + def test_core_doctype_customization(self): + self.assertRaises(xhiveframework.ValidationError, self.get_customize_form, "User") + + def test_save_customization_length_field_property(self): + # Using Notification Log doctype as it doesn't have any other custom fields + d = self.get_customize_form("Notification Log") + + new_document_length = 255 + document_name = d.get("fields", {"fieldname": "document_name"})[0] + document_name.length = new_document_length + d.run_method("save_customization") + + self.assertEqual( + xhiveframework.db.get_value( + "Property Setter", + {"doc_type": "Notification Log", "property": "length", "field_name": "document_name"}, + "value", + ), + str(new_document_length), + ) + + length = xhiveframework.db.sql( + """SELECT character_maximum_length + FROM information_schema.columns + WHERE table_name = 'tabNotification Log' + AND column_name = 'document_name'""" + )[0][0] + + self.assertEqual(length, new_document_length) + + def test_custom_link(self): + try: + # create a dummy doctype linked to Event + testdt_name = "Test Link for Event" + testdt = new_doctype( + testdt_name, fields=[dict(fieldtype="Link", fieldname="event", options="Event")] + ).insert() + + testdt_name1 = "Test Link for Event 1" + testdt1 = new_doctype( + testdt_name1, fields=[dict(fieldtype="Link", fieldname="event", options="Event")] + ).insert() + + # add a custom link + d = self.get_customize_form("Event") + + d.append("links", dict(link_doctype=testdt_name, link_fieldname="event", group="Tests")) + d.append("links", dict(link_doctype=testdt_name1, link_fieldname="event", group="Tests")) + + d.run_method("save_customization") + + xhiveframework.clear_cache() + event = xhiveframework.get_meta("Event") + + # check links exist + self.assertTrue([d.name for d in event.links if d.link_doctype == testdt_name]) + self.assertTrue([d.name for d in event.links if d.link_doctype == testdt_name1]) + + # check order + order = json.loads(event.links_order) + self.assertListEqual(order, [d.name for d in event.links]) + + # remove the link + d = self.get_customize_form("Event") + d.links = [] + d.run_method("save_customization") + + xhiveframework.clear_cache() + event = xhiveframework.get_meta("Event") + self.assertFalse([d.name for d in (event.links or []) if d.link_doctype == testdt_name]) + finally: + testdt.delete() + testdt1.delete() + + def test_custom_internal_links(self): + # add a custom internal link + xhiveframework.clear_cache() + d = self.get_customize_form("User Group") + + d.append( + "links", + dict( + link_doctype="User Group Member", + parent_doctype="User Group", + link_fieldname="user", + table_fieldname="user_group_members", + group="Tests", + custom=1, + ), + ) + + d.run_method("save_customization") + + xhiveframework.clear_cache() + user_group = xhiveframework.get_meta("User Group") + + # check links exist + self.assertTrue([d.name for d in user_group.links if d.link_doctype == "User Group Member"]) + self.assertTrue([d.name for d in user_group.links if d.parent_doctype == "User Group"]) + + # remove the link + d = self.get_customize_form("User Group") + d.links = [] + d.run_method("save_customization") + + xhiveframework.clear_cache() + user_group = xhiveframework.get_meta("Event") + self.assertFalse([d.name for d in (user_group.links or []) if d.link_doctype == "User Group Member"]) + + def test_custom_action(self): + test_route = "/app/List/DocType" + + # create a dummy action (route) + d = self.get_customize_form("Event") + d.append("actions", dict(label="Test Action", action_type="Route", action=test_route)) + d.run_method("save_customization") + + xhiveframework.clear_cache() + event = xhiveframework.get_meta("Event") + + # check if added to meta + action = [d for d in event.actions if d.label == "Test Action"] + self.assertEqual(len(action), 1) + self.assertEqual(action[0].action, test_route) + + # clear the action + d = self.get_customize_form("Event") + d.actions = [] + d.run_method("save_customization") + + xhiveframework.clear_cache() + event = xhiveframework.get_meta("Event") + + action = [d for d in event.actions if d.label == "Test Action"] + self.assertEqual(len(action), 0) + + def test_custom_label(self): + d = self.get_customize_form("Event") + + # add label + d.label = "Test Rename" + d.run_method("save_customization") + self.assertEqual(d.label, "Test Rename") + + # change label + d.label = "Test Rename 2" + d.run_method("save_customization") + self.assertEqual(d.label, "Test Rename 2") + + # saving again to make sure existing label persists + d.run_method("save_customization") + self.assertEqual(d.label, "Test Rename 2") + + # clear label + d.label = "" + d.run_method("save_customization") + self.assertEqual(d.label, "") + + def test_change_to_autoincrement_autoname(self): + d = self.get_customize_form("Event") + d.autoname = "autoincrement" + + with self.assertRaises(xhiveframework.ValidationError): + d.run_method("save_customization") + + def test_system_generated_fields(self): + doctype = "Event" + custom_field_name = "custom_test_field" + + custom_field = xhiveframework.get_doc("Custom Field", {"dt": doctype, "fieldname": custom_field_name}) + custom_field.is_system_generated = 1 + custom_field.save() + + d = self.get_customize_form(doctype) + custom_field = d.getone("fields", {"fieldname": custom_field_name}) + custom_field.description = "Test Description" + d.run_method("save_customization") + + property_setter_filters = { + "doc_type": doctype, + "field_name": custom_field_name, + "property": "description", + } + self.assertEqual( + xhiveframework.db.get_value("Property Setter", property_setter_filters, "value"), "Test Description" + ) + + def test_custom_field_order(self): + # shuffle fields + customize_form = self.get_customize_form(doctype="ToDo") + customize_form.fields.insert(0, customize_form.fields.pop()) + customize_form.save_customization() + + field_order_property = json.loads( + xhiveframework.db.get_value("Property Setter", {"doc_type": "ToDo", "property": "field_order"}, "value") + ) + + self.assertEqual(field_order_property, [df.fieldname for df in xhiveframework.get_meta("ToDo").fields]) diff --git a/xhiveframework/custom/doctype/customize_form_field/__init__.py b/xhiveframework/custom/doctype/customize_form_field/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form_field/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/custom/doctype/customize_form_field/customize_form_field.json b/xhiveframework/custom/doctype/customize_form_field/customize_form_field.json new file mode 100644 index 0000000..fd55963 --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form_field/customize_form_field.json @@ -0,0 +1,491 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-02-22 01:27:32", + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "is_system_generated", + "label_and_type", + "label", + "fieldtype", + "fieldname", + "non_negative", + "reqd", + "unique", + "is_virtual", + "in_list_view", + "in_standard_filter", + "in_global_search", + "in_preview", + "bold", + "no_copy", + "allow_in_quick_entry", + "translatable", + "column_break_7", + "default", + "precision", + "length", + "options", + "sort_options", + "fetch_from", + "fetch_if_empty", + "show_dashboard", + "permissions", + "depends_on", + "permlevel", + "hidden", + "read_only", + "collapsible", + "allow_bulk_edit", + "collapsible_depends_on", + "column_break_14", + "ignore_user_permissions", + "allow_on_submit", + "report_hide", + "remember_last_selected_value", + "hide_border", + "ignore_xss_filter", + "property_depends_on_section", + "mandatory_depends_on", + "column_break_33", + "read_only_depends_on", + "display", + "in_filter", + "hide_seconds", + "hide_days", + "column_break_21", + "description", + "print_hide", + "print_hide_if_no_value", + "print_width", + "columns", + "width", + "is_custom_field" + ], + "fields": [ + { + "fieldname": "label_and_type", + "fieldtype": "Section Break", + "label": "Label and Type" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "oldfieldname": "label", + "oldfieldtype": "Data", + "search_index": 1 + }, + { + "default": "Data", + "fieldname": "fieldtype", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Type", + "oldfieldname": "fieldtype", + "oldfieldtype": "Select", + "options": "Autocomplete\nAttach\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\nPhone\nRating\nRead Only\nSection Break\nSelect\nSignature\nSmall Text\nTab Break\nTable\nTable MultiSelect\nText\nText Editor\nTime", + "reqd": 1, + "search_index": 1, + "sort_options": 1 + }, + { + "fieldname": "fieldname", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Name", + "oldfieldname": "fieldname", + "oldfieldtype": "Data", + "read_only": 1, + "search_index": 1 + }, + { + "default": "0", + "depends_on": "eval:!in_list([\"Section Break\", \"Column Break\", \"Button\", \"HTML\"], doc.fieldtype)", + "fieldname": "reqd", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Mandatory", + "oldfieldname": "reqd", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "unique", + "fieldtype": "Check", + "label": "Unique" + }, + { + "default": "0", + "fieldname": "is_virtual", + "fieldtype": "Check", + "label": "Is Virtual" + }, + { + "default": "0", + "depends_on": "eval:!doc.is_virtual", + "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": "1", + "depends_on": "eval:['Data', 'Select', 'Text', 'Small Text', 'Text Editor'].includes(doc.fieldtype)", + "fieldname": "translatable", + "fieldtype": "Check", + "label": "Translatable" + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "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" + }, + { + "depends_on": "eval:in_list(['Data', 'Link', 'Dynamic Link', 'Password', 'Select', 'Read Only', 'Attach', 'Attach Image'], doc.fieldtype)", + "fieldname": "length", + "fieldtype": "Int", + "label": "Length" + }, + { + "description": "For Links, enter the DocType as range.\nFor Select, enter list of Options, each on a new line.", + "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 unchecked, the value will always be re-fetched on save.", + "fieldname": "fetch_if_empty", + "fieldtype": "Check", + "label": "Fetch on Save if Empty" + }, + { + "fieldname": "permissions", + "fieldtype": "Section Break", + "label": "Permissions" + }, + { + "description": "This field will appear only if the fieldname defined here has value OR the rules are true (examples):\nmyfield\neval:doc.myfield=='My Value'\neval:doc.age>18", + "fieldname": "depends_on", + "fieldtype": "Code", + "label": "Depends On", + "oldfieldname": "depends_on", + "oldfieldtype": "Data", + "options": "JS" + }, + { + "default": "0", + "depends_on": "eval:!in_list(['Section Break', 'Column Break', 'Tab Break'], doc.fieldtype)", + "fieldname": "permlevel", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Perm Level", + "oldfieldname": "permlevel", + "oldfieldtype": "Int" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden", + "oldfieldname": "hidden", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "read_only", + "fieldtype": "Check", + "label": "Read Only" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype==\"Section Break\"", + "fieldname": "collapsible", + "fieldtype": "Check", + "label": "Collapsible" + }, + { + "default": "0", + "depends_on": "eval: doc.fieldtype == \"Table\"", + "fieldname": "allow_bulk_edit", + "fieldtype": "Check", + "label": "Allow Bulk Edit" + }, + { + "depends_on": "eval:doc.fieldtype==\"Section Break\"", + "fieldname": "collapsible_depends_on", + "fieldtype": "Code", + "label": "Collapsible Depends On", + "options": "JS" + }, + { + "fieldname": "column_break_14", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "ignore_user_permissions", + "fieldtype": "Check", + "label": "Ignore User Permissions" + }, + { + "default": "0", + "fieldname": "allow_on_submit", + "fieldtype": "Check", + "label": "Allow on Submit", + "oldfieldname": "allow_on_submit", + "oldfieldtype": "Check" + }, + { + "default": "0", + "fieldname": "report_hide", + "fieldtype": "Check", + "label": "Report Hide", + "oldfieldname": "report_hide", + "oldfieldtype": "Check" + }, + { + "default": "0", + "depends_on": "eval:(doc.fieldtype == 'Link')", + "fieldname": "remember_last_selected_value", + "fieldtype": "Check", + "label": "Remember Last Selected Value" + }, + { + "fieldname": "display", + "fieldtype": "Section Break", + "label": "Display" + }, + { + "fieldname": "default", + "fieldtype": "Small Text", + "label": "Default", + "oldfieldname": "default", + "oldfieldtype": "Text" + }, + { + "default": "0", + "fieldname": "in_filter", + "fieldtype": "Check", + "label": "In Filter", + "oldfieldname": "in_filter", + "oldfieldtype": "Check", + "print_width": "50px", + "width": "50px" + }, + { + "fieldname": "column_break_21", + "fieldtype": "Column Break" + }, + { + "fieldname": "description", + "fieldtype": "Text", + "label": "Description", + "oldfieldname": "description", + "oldfieldtype": "Text", + "print_width": "300px", + "width": "300px" + }, + { + "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" + }, + { + "description": "Print Width of the field, if the field is a column in a table", + "fieldname": "print_width", + "fieldtype": "Data", + "label": "Print Width", + "print_width": "50px", + "width": "50px" + }, + { + "depends_on": "eval:parent.istable", + "description": "Number of columns for a field in a Grid (Total Columns in a grid should be less than 11)", + "fieldname": "columns", + "fieldtype": "Int", + "label": "Columns" + }, + { + "fieldname": "width", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Width", + "oldfieldname": "width", + "oldfieldtype": "Data", + "print_width": "50px", + "width": "50px" + }, + { + "default": "0", + "fieldname": "is_custom_field", + "fieldtype": "Check", + "hidden": 1, + "label": "Is Custom Field", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "allow_in_quick_entry", + "fieldtype": "Check", + "label": "Allow in Quick Entry" + }, + { + "fieldname": "property_depends_on_section", + "fieldtype": "Section Break", + "label": "Property Depends On" + }, + { + "fieldname": "mandatory_depends_on", + "fieldtype": "Code", + "label": "Mandatory Depends On", + "options": "JS" + }, + { + "fieldname": "column_break_33", + "fieldtype": "Column Break" + }, + { + "fieldname": "read_only_depends_on", + "fieldtype": "Code", + "label": "Read Only Depends On", + "options": "JS" + }, + { + "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" + }, + { + "default": "0", + "depends_on": "eval:doc.fieldtype=='Tab Break'", + "fieldname": "show_dashboard", + "fieldtype": "Check", + "label": "Show Dashboard" + }, + { + "default": "0", + "fieldname": "no_copy", + "fieldtype": "Check", + "label": "No Copy" + }, + { + "default": "0", + "fieldname": "is_system_generated", + "fieldtype": "Check", + "hidden": 1, + "label": "Is System Generated", + "read_only": 1 + }, + { + "default": "0", + "description": "Don't 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": "0", + "depends_on": "eval: doc.fieldtype === 'Select'", + "fieldname": "sort_options", + "fieldtype": "Check", + "label": "Sort Options" + } + ], + "idx": 1, + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2024-02-01 15:56:39.171633", + "modified_by": "Administrator", + "module": "Custom", + "name": "Customize Form Field", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/custom/doctype/customize_form_field/customize_form_field.py b/xhiveframework/custom/doctype/customize_form_field/customize_form_field.py new file mode 100644 index 0000000..16f074f --- /dev/null +++ b/xhiveframework/custom/doctype/customize_form_field/customize_form_field.py @@ -0,0 +1,113 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class CustomizeFormField(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allow_bulk_edit: DF.Check + allow_in_quick_entry: DF.Check + allow_on_submit: DF.Check + bold: DF.Check + collapsible: DF.Check + collapsible_depends_on: DF.Code | None + columns: DF.Int + default: DF.SmallText | None + depends_on: DF.Code | None + description: DF.Text | None + fetch_from: DF.SmallText | None + fetch_if_empty: DF.Check + fieldname: DF.Data | None + fieldtype: DF.Literal[ + "Autocomplete", + "Attach", + "Attach Image", + "Barcode", + "Button", + "Check", + "Code", + "Color", + "Column Break", + "Currency", + "Data", + "Date", + "Datetime", + "Duration", + "Dynamic Link", + "Float", + "Fold", + "Geolocation", + "Heading", + "HTML", + "HTML Editor", + "Icon", + "Image", + "Int", + "Link", + "Long Text", + "Markdown Editor", + "Password", + "Percent", + "Phone", + "Rating", + "Read Only", + "Section Break", + "Select", + "Signature", + "Small Text", + "Tab Break", + "Table", + "Table MultiSelect", + "Text", + "Text Editor", + "Time", + ] + hidden: DF.Check + hide_border: DF.Check + hide_days: DF.Check + hide_seconds: DF.Check + ignore_user_permissions: DF.Check + ignore_xss_filter: DF.Check + in_filter: DF.Check + in_global_search: DF.Check + in_list_view: DF.Check + in_preview: DF.Check + in_standard_filter: DF.Check + is_custom_field: DF.Check + is_system_generated: DF.Check + is_virtual: DF.Check + label: DF.Data | None + length: DF.Int + mandatory_depends_on: DF.Code | None + no_copy: DF.Check + non_negative: DF.Check + options: DF.SmallText | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + permlevel: DF.Int + precision: DF.Literal["", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + print_hide: DF.Check + print_hide_if_no_value: DF.Check + print_width: DF.Data | None + read_only: DF.Check + read_only_depends_on: DF.Code | None + remember_last_selected_value: DF.Check + report_hide: DF.Check + reqd: DF.Check + show_dashboard: DF.Check + sort_options: DF.Check + translatable: DF.Check + unique: DF.Check + width: DF.Data | None + # end: auto-generated types + + pass diff --git a/xhiveframework/custom/doctype/doctype_layout/__init__.py b/xhiveframework/custom/doctype/doctype_layout/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/doctype/doctype_layout/doctype_layout.js b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.js new file mode 100644 index 0000000..236e73d --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.js @@ -0,0 +1,105 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("DocType Layout", { + onload_post_render(frm) { + // disallow users from manually adding/deleting rows; this doctype should only + // be used for managing layout, and docfields and custom fields should be used + // to manage other field metadata (hidden, etc.) + frm.set_df_property("fields", "cannot_add_rows", true); + frm.set_df_property("fields", "cannot_delete_rows", true); + + $(frm.wrapper).on("grid-move-row", (e, frm) => { + // refresh the layout after moving a row + frm.dirty(); + }); + }, + + refresh(frm) { + frm.events.add_buttons(frm); + }, + + async document_type(frm) { + if (frm.doc.document_type) { + // refreshing the doctype fields resets the new name input field; + // once the fields are set, reset the name to the original input + if (frm.is_new()) { + const document_name = frm.doc.__newname || frm.doc.name; + } + + frm.set_value("fields", []); + await frm.events.sync_fields(frm, false); + + if (frm.is_new()) { + frm.doc.__newname = document_name; // eslint-disable-line + frm.refresh_field("__newname"); + } + } + }, + + add_buttons(frm) { + if (!frm.is_new()) { + frm.add_custom_button(__("Go to {0} List", [frm.doc.name]), () => { + window.open(`/app/${xhiveframework.router.slug(frm.doc.name)}`); + }); + + frm.add_custom_button(__("Sync {0} Fields", [frm.doc.name]), async () => { + await frm.events.sync_fields(frm, true); + }); + } + }, + + async sync_fields(frm, notify) { + xhiveframework.dom.freeze("Fetching fields..."); + const response = await frm.call({ doc: frm.doc, method: "sync_fields" }); + frm.refresh_field("fields"); + xhiveframework.dom.unfreeze(); + + if (!response.message) { + xhiveframework.msgprint(__("No changes to sync")); + return; + } + + frm.dirty(); + if (notify) { + const addedFields = response.message.added; + const removedFields = response.message.removed; + + const getChangedMessage = (fields) => { + let changes = ""; + for (const field of fields) { + if (field.label) { + changes += `
  • Row #${field.idx}: ${field.fieldname.bold()} (${ + field.label + })
  • `; + } else { + changes += `
  • Row #${field.idx}: ${field.fieldname.bold()}
  • `; + } + } + return changes; + }; + + let message = ""; + + if (addedFields.length) { + message += `The following fields have been added:

      ${getChangedMessage( + addedFields + )}
    `; + } + + if (removedFields.length) { + message += `The following fields have been removed:

      ${getChangedMessage( + removedFields + )}
    `; + } + + if (message) { + xhiveframework.msgprint({ + message: __(message), + indicator: "green", + title: __("Synced Fields"), + }); + } + } + }, +}); diff --git a/xhiveframework/custom/doctype/doctype_layout/doctype_layout.json b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.json new file mode 100644 index 0000000..5237e76 --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.json @@ -0,0 +1,75 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "prompt", + "creation": "2020-11-16 17:05:35.306846", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "route", + "fields", + "client_script" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1, + "set_only_once": 1 + }, + { + "fieldname": "fields", + "fieldtype": "Table", + "label": "Fields", + "options": "DocType Layout Field", + "reqd": 1 + }, + { + "fieldname": "client_script", + "fieldtype": "Code", + "label": "Client Script" + }, + { + "fieldname": "route", + "fieldtype": "Data", + "label": "Route", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 22:24:08.103972", + "modified_by": "Administrator", + "module": "Custom", + "name": "DocType Layout", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Desk User" + } + ], + "route": "doctype-layout", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/custom/doctype/doctype_layout/doctype_layout.py b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.py new file mode 100644 index 0000000..9de73ed --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout/doctype_layout.py @@ -0,0 +1,92 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework.desk.utils import slug +from xhiveframework.model.document import Document + +if TYPE_CHECKING: + from xhiveframework.core.doctype.docfield.docfield import DocField + + +class DocTypeLayout(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.custom.doctype.doctype_layout_field.doctype_layout_field import DocTypeLayoutField + from xhiveframework.types import DF + + client_script: DF.Code | None + document_type: DF.Link + fields: DF.Table[DocTypeLayoutField] + route: DF.Data + + # end: auto-generated types + def validate(self): + if not self.route: + self.route = slug(self.name) + + @xhiveframework.whitelist() + def sync_fields(self): + doctype_fields = xhiveframework.get_meta(self.document_type, cached=False).fields + + if self.is_new(): + added_fields = [field.fieldname for field in doctype_fields] + removed_fields = [] + else: + doctype_fieldnames = {field.fieldname for field in doctype_fields} + layout_fieldnames = {field.fieldname for field in self.fields} + added_fields = list(doctype_fieldnames - layout_fieldnames) + removed_fields = list(layout_fieldnames - doctype_fieldnames) + + if not (added_fields or removed_fields): + return + + added = self.add_fields(added_fields, doctype_fields) + removed = self.remove_fields(removed_fields) + + for index, field in enumerate(self.fields): + field.idx = index + 1 + + return {"added": added, "removed": removed} + + def add_fields(self, added_fields: list[str], doctype_fields: list["DocField"]) -> list[dict]: + added = [] + for field in added_fields: + field_details = next((f for f in doctype_fields if f.fieldname == field), None) + if not field_details: + continue + + # remove 'doctype' data from the DocField to allow adding it to the layout + row = self.append("fields", field_details.as_dict(no_default_fields=True)) + row_data = row.as_dict() + + if field_details.get("insert_after"): + insert_after = next( + (f for f in self.fields if f.fieldname == field_details.insert_after), + None, + ) + + # initialize new row to just after the insert_after field + if insert_after: + self.fields.insert(insert_after.idx, row) + self.fields.pop() + + row_data = {"idx": insert_after.idx + 1, "fieldname": row.fieldname, "label": row.label} + + added.append(row_data) + return added + + def remove_fields(self, removed_fields: list[str]) -> list[dict]: + removed = [] + for field in removed_fields: + field_details = next((f for f in self.fields if f.fieldname == field), None) + if field_details: + self.remove(field_details) + removed.append(field_details.as_dict()) + return removed diff --git a/xhiveframework/custom/doctype/doctype_layout/patches/convert_web_forms_to_doctype_layout.py b/xhiveframework/custom/doctype/doctype_layout/patches/convert_web_forms_to_doctype_layout.py new file mode 100644 index 0000000..3345a0a --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout/patches/convert_web_forms_to_doctype_layout.py @@ -0,0 +1,20 @@ +import xhiveframework + + +def execute(): + for web_form_name in xhiveframework.get_all("Web Form", pluck="name"): + web_form = xhiveframework.get_doc("Web Form", web_form_name) + doctype_layout = xhiveframework.get_doc( + dict( + doctype="DocType Layout", + document_type=web_form.doc_type, + name=web_form.title, + route=web_form.route, + fields=[ + dict(fieldname=d.fieldname, label=d.label) + for d in web_form.web_form_fields + if d.fieldname + ], + ) + ).insert() + print(doctype_layout.name) diff --git a/xhiveframework/custom/doctype/doctype_layout/test_doctype_layout.py b/xhiveframework/custom/doctype/doctype_layout/test_doctype_layout.py new file mode 100644 index 0000000..1c5579e --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout/test_doctype_layout.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocTypeLayout(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/custom/doctype/doctype_layout_field/__init__.py b/xhiveframework/custom/doctype/doctype_layout_field/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.json b/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.json new file mode 100644 index 0000000..006c01a --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.json @@ -0,0 +1,38 @@ +{ + "actions": [], + "creation": "2020-11-16 16:03:43.771801", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "label", + "fieldname" + ], + "fields": [ + { + "fieldname": "fieldname", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Fieldname", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-05-19 16:27:40.585865", + "modified_by": "Administrator", + "module": "Custom", + "name": "DocType Layout Field", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} diff --git a/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.py b/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.py new file mode 100644 index 0000000..d69bf5d --- /dev/null +++ b/xhiveframework/custom/doctype/doctype_layout_field/doctype_layout_field.py @@ -0,0 +1,23 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DocTypeLayoutField(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + fieldname: DF.Literal[None] + label: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/custom/doctype/property_setter/README.md b/xhiveframework/custom/doctype/property_setter/README.md new file mode 100644 index 0000000..65ece5e --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/README.md @@ -0,0 +1 @@ +Overrides standard DocType, DocField properties. The standard application is configured with properties for forms and fields (like, whether they are hidden or not). These can be overridden by a System Manager who can configure DocTypes based on custom requirements. \ No newline at end of file diff --git a/xhiveframework/custom/doctype/property_setter/__init__.py b/xhiveframework/custom/doctype/property_setter/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/custom/doctype/property_setter/patches/__init__.py b/xhiveframework/custom/doctype/property_setter/patches/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/doctype/property_setter/patches/remove_invalid_fetch_from_expressions.py b/xhiveframework/custom/doctype/property_setter/patches/remove_invalid_fetch_from_expressions.py new file mode 100644 index 0000000..41eae99 --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/patches/remove_invalid_fetch_from_expressions.py @@ -0,0 +1,28 @@ +from contextlib import suppress + +import xhiveframework + + +def execute(): + """Remove invalid fetch from expressions""" + with suppress(Exception): + property_setters = xhiveframework.get_all( + "Property Setter", {"doctype_or_field": "DocField", "property": "fetch_from"}, ["name", "value"] + ) + for ps in property_setters: + if not is_valid_expression(ps.value): + xhiveframework.db.delete("Property Setter", {"name": ps.name}) + + custom_fields = xhiveframework.get_all("Custom Field", {"fetch_from": ("is", "set")}, ["name", "fetch_from"]) + for cf in custom_fields: + if not is_valid_expression(cf.fetch_from): + xhiveframework.db.set_value("Custom Field", cf.name, "fetch_from", "") + + +def is_valid_expression(expr) -> bool: + if not expr or "." not in expr: + return False + source_field, target_field = expr.split(".", maxsplit=1) + if not source_field or not target_field: + return False + return True diff --git a/xhiveframework/custom/doctype/property_setter/property_setter.js b/xhiveframework/custom/doctype/property_setter/property_setter.js new file mode 100644 index 0000000..08d6197 --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/property_setter.js @@ -0,0 +1,10 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.ui.form.on("Property Setter", { + validate: function (frm) { + if (frm.doc.property_type == "Check" && !["0", "1"].includes(frm.doc.value)) { + xhiveframework.throw(__("Value for a check field can be either 0 or 1")); + } + }, +}); diff --git a/xhiveframework/custom/doctype/property_setter/property_setter.json b/xhiveframework/custom/doctype/property_setter/property_setter.json new file mode 100644 index 0000000..039826b --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/property_setter.json @@ -0,0 +1,154 @@ +{ + "actions": [], + "creation": "2013-01-10 16:34:04", + "description": "Property Setter overrides a standard DocType or Field property", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "is_system_generated", + "help", + "sb0", + "doctype_or_field", + "doc_type", + "field_name", + "row_name", + "column_break0", + "module", + "section_break_9", + "property", + "property_type", + "value", + "default_value" + ], + "fields": [ + { + "fieldname": "help", + "fieldtype": "HTML", + "label": "Help", + "options": "
    Please don't update it as it can mess up your form. Use the Customize Form View and Custom Fields to set properties!
    " + }, + { + "fieldname": "sb0", + "fieldtype": "Section Break" + }, + { + "fieldname": "doctype_or_field", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Applied On", + "options": "\nDocField\nDocType\nDocType Link\nDocType Action\nDocType State", + "read_only_depends_on": "eval:!doc.__islocal", + "reqd": 1 + }, + { + "description": "New value to be set", + "fieldname": "value", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Set Value" + }, + { + "fieldname": "column_break0", + "fieldtype": "Column Break" + }, + { + "fieldname": "doc_type", + "fieldtype": "Link", + "in_standard_filter": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1, + "search_index": 1 + }, + { + "depends_on": "eval:doc.doctype_or_field=='DocField'", + "description": "ID (name) of the entity whose property is to be set", + "fieldname": "field_name", + "fieldtype": "Data", + "in_standard_filter": 1, + "label": "Field Name", + "search_index": 1 + }, + { + "fieldname": "property", + "fieldtype": "Data", + "in_standard_filter": 1, + "label": "Property", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "property_type", + "fieldtype": "Data", + "label": "Property Type" + }, + { + "fieldname": "default_value", + "fieldtype": "Data", + "label": "Default Value" + }, + { + "description": "For DocType Link / DocType Action", + "fieldname": "row_name", + "fieldtype": "Data", + "label": "Row Name" + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module (for export)", + "options": "Module Def" + }, + { + "fieldname": "section_break_9", + "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "is_system_generated", + "fieldtype": "Check", + "label": "Is System Generated", + "read_only": 1 + } + ], + "icon": "fa fa-glass", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2022-02-28 22:24:12.377693", + "modified_by": "Administrator", + "module": "Custom", + "name": "Property Setter", + "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": "doc_type,property", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/custom/doctype/property_setter/property_setter.py b/xhiveframework/custom/doctype/property_setter/property_setter.py new file mode 100644 index 0000000..3ed499a --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/property_setter.py @@ -0,0 +1,98 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + +not_allowed_fieldtype_change = ["naming_series"] + + +class PropertySetter(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + default_value: DF.Data | None + doc_type: DF.Link + doctype_or_field: DF.Literal[ + "", "DocField", "DocType", "DocType Link", "DocType Action", "DocType State" + ] + field_name: DF.Data | None + is_system_generated: DF.Check + module: DF.Link | None + property: DF.Data + property_type: DF.Data | None + row_name: DF.Data | None + value: DF.SmallText | None + + # end: auto-generated types + def autoname(self): + self.name = "{doctype}-{field}-{property}".format( + doctype=self.doc_type, field=self.field_name or self.row_name or "main", property=self.property + ) + + 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) + xhiveframework.clear_cache(doctype=self.doc_type) + + def on_trash(self): + xhiveframework.clear_cache(doctype=self.doc_type) + + def validate_fieldtype_change(self): + if self.property == "fieldtype" and self.field_name in not_allowed_fieldtype_change: + xhiveframework.throw(_("Field type cannot be changed for {0}").format(self.field_name)) + + def on_update(self): + if xhiveframework.flags.in_patch: + self.flags.validate_fields_for_doctype = False + + if not self.flags.ignore_validate and self.flags.validate_fields_for_doctype: + from xhiveframework.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 + property_setter = xhiveframework.get_doc( + { + "doctype": "Property Setter", + "doctype_or_field": for_doctype and "DocType" or "DocField", + "doc_type": doctype, + "field_name": fieldname, + "property": property, + "value": value, + "property_type": property_type, + } + ) + property_setter.flags.ignore_permissions = True + property_setter.flags.validate_fields_for_doctype = validate_fields_for_doctype + 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) + if field_name: + filters["field_name"] = field_name + if row_name: + filters["row_name"] = row_name + + xhiveframework.db.delete("Property Setter", filters) diff --git a/xhiveframework/custom/doctype/property_setter/test_property_setter.py b/xhiveframework/custom/doctype/property_setter/test_property_setter.py new file mode 100644 index 0000000..705db3a --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/test_property_setter.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Property Setter') + + +class TestPropertySetter(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/custom/doctype/property_setter/test_records.json b/xhiveframework/custom/doctype/property_setter/test_records.json new file mode 100644 index 0000000..3c084b4 --- /dev/null +++ b/xhiveframework/custom/doctype/property_setter/test_records.json @@ -0,0 +1,10 @@ +[ + { + "doc_type": "User", + "doctype_or_field": "DocField", + "field_name": "location", + "property": "in_list_view", + "property_type": "Check", + "value": "1" + } +] \ No newline at end of file diff --git a/xhiveframework/custom/fixtures/temp_doctype.json b/xhiveframework/custom/fixtures/temp_doctype.json new file mode 100644 index 0000000..20b3d9c --- /dev/null +++ b/xhiveframework/custom/fixtures/temp_doctype.json @@ -0,0 +1,166 @@ +{ + "docstatus": 0, + "doctype": "DocType", + "name": "new-doctype-2", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "is_submittable": 0, + "istable": 0, + "issingle": 0, + "is_tree": 0, + "editable_grid": 1, + "quick_entry": 1, + "track_changes": 1, + "track_seen": 0, + "track_views": 0, + "custom": 1, + "beta": 0, + "is_virtual": 0, + "naming_rule": "", + "allow_rename": 1, + "hide_toolbar": 0, + "allow_copy": 0, + "allow_import": 0, + "allow_events_in_timeline": 0, + "allow_auto_repeat": 0, + "sort_field": "modified", + "sort_order": "DESC", + "document_type": "", + "show_preview_popup": 0, + "show_name_in_global_search": 0, + "email_append_to": 0, + "read_only": 0, + "in_create": 0, + "has_web_view": 0, + "allow_guest_to_view": 0, + "index_web_pages_for_search": 1, + "engine": "InnoDB", + "permissions": [ + { + "docstatus": 0, + "doctype": "DocPerm", + "name": "new-docperm-2", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "if_owner": 0, + "permlevel": 0, + "select": 0, + "read": 1, + "write": 1, + "create": 1, + "delete": 1, + "submit": 0, + "cancel": 0, + "amend": 0, + "report": 1, + "export": 1, + "import": 0, + "share": 1, + "print": 1, + "email": 1, + "parent": "new-doctype-2", + "parentfield": "permissions", + "parenttype": "DocType", + "idx": 1, + "role": "System Manager" + } + ], + "__newname": "temp_doctype", + "module": "Custom", + "fields": [ + { + "docstatus": 0, + "doctype": "DocField", + "name": "new-docfield-1", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "fieldtype": "Data", + "precision": "", + "non_negative": 0, + "hide_days": 0, + "hide_seconds": 0, + "reqd": 1, + "search_index": 0, + "fetch_if_empty": 0, + "hidden": 0, + "bold": 0, + "allow_in_quick_entry": 0, + "translatable": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "report_hide": 0, + "collapsible": 0, + "hide_border": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "in_preview": 0, + "in_filter": 0, + "in_global_search": 0, + "read_only": 0, + "allow_on_submit": 0, + "ignore_user_permissions": 0, + "allow_bulk_edit": 0, + "permlevel": 0, + "ignore_xss_filter": 0, + "unique": 0, + "no_copy": 0, + "set_only_once": 0, + "remember_last_selected_value": 0, + "parent": "new-doctype-2", + "parentfield": "fields", + "parenttype": "DocType", + "idx": 1, + "__unedited": false, + "label": "member_name" + }, + { + "docstatus": 0, + "doctype": "DocField", + "name": "new-docfield-2", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "fieldtype": "Data", + "precision": "", + "non_negative": 0, + "hide_days": 0, + "hide_seconds": 0, + "reqd": 0, + "search_index": 0, + "fetch_if_empty": 0, + "hidden": 0, + "bold": 0, + "allow_in_quick_entry": 0, + "translatable": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "report_hide": 0, + "collapsible": 0, + "hide_border": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "in_preview": 0, + "in_filter": 0, + "in_global_search": 0, + "read_only": 0, + "allow_on_submit": 0, + "ignore_user_permissions": 0, + "allow_bulk_edit": 0, + "permlevel": 0, + "ignore_xss_filter": 0, + "unique": 0, + "no_copy": 0, + "set_only_once": 0, + "remember_last_selected_value": 0, + "parent": "new-doctype-2", + "parentfield": "fields", + "parenttype": "DocType", + "idx": 2, + "__unedited": false, + "label": "email" + } + ] +} diff --git a/xhiveframework/custom/fixtures/temp_singles.json b/xhiveframework/custom/fixtures/temp_singles.json new file mode 100644 index 0000000..723f47d --- /dev/null +++ b/xhiveframework/custom/fixtures/temp_singles.json @@ -0,0 +1,166 @@ +{ + "docstatus": 0, + "doctype": "DocType", + "name": "new-doctype-1", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "is_submittable": 0, + "istable": 0, + "issingle": 1, + "is_tree": 0, + "editable_grid": 1, + "quick_entry": 0, + "track_changes": 1, + "track_seen": 0, + "track_views": 0, + "custom": 1, + "beta": 0, + "is_virtual": 0, + "naming_rule": "", + "allow_rename": 1, + "hide_toolbar": 0, + "allow_copy": 0, + "allow_import": 0, + "allow_events_in_timeline": 0, + "allow_auto_repeat": 0, + "sort_field": "modified", + "sort_order": "DESC", + "document_type": "", + "show_preview_popup": 0, + "show_name_in_global_search": 0, + "email_append_to": 0, + "read_only": 0, + "in_create": 0, + "has_web_view": 0, + "allow_guest_to_view": 0, + "index_web_pages_for_search": 1, + "engine": "InnoDB", + "permissions": [ + { + "docstatus": 0, + "doctype": "DocPerm", + "name": "new-docperm-1", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "if_owner": 0, + "permlevel": 0, + "select": 0, + "read": 1, + "write": 1, + "create": 1, + "delete": 1, + "submit": 0, + "cancel": 0, + "amend": 0, + "report": 1, + "export": 1, + "import": 0, + "share": 1, + "print": 1, + "email": 1, + "parent": "new-doctype-1", + "parentfield": "permissions", + "parenttype": "DocType", + "idx": 1, + "role": "System Manager" + } + ], + "__newname": "temp_singles", + "module": "Custom", + "fields": [ + { + "docstatus": 0, + "doctype": "DocField", + "name": "new-docfield-1", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "fieldtype": "Data", + "precision": "", + "non_negative": 0, + "hide_days": 0, + "hide_seconds": 0, + "reqd": 0, + "search_index": 0, + "fetch_if_empty": 0, + "hidden": 0, + "bold": 0, + "allow_in_quick_entry": 0, + "translatable": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "report_hide": 0, + "collapsible": 0, + "hide_border": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "in_preview": 0, + "in_filter": 0, + "in_global_search": 0, + "read_only": 0, + "allow_on_submit": 0, + "ignore_user_permissions": 0, + "allow_bulk_edit": 0, + "permlevel": 0, + "ignore_xss_filter": 0, + "unique": 0, + "no_copy": 0, + "set_only_once": 0, + "remember_last_selected_value": 0, + "parent": "new-doctype-1", + "parentfield": "fields", + "parenttype": "DocType", + "idx": 1, + "__unedited": false, + "label": "member_name" + }, + { + "docstatus": 0, + "doctype": "DocField", + "name": "new-docfield-2", + "__islocal": 1, + "__unsaved": 1, + "owner": "Administrator", + "fieldtype": "Data", + "precision": "", + "non_negative": 0, + "hide_days": 0, + "hide_seconds": 0, + "reqd": 0, + "search_index": 0, + "fetch_if_empty": 0, + "hidden": 0, + "bold": 0, + "allow_in_quick_entry": 0, + "translatable": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "report_hide": 0, + "collapsible": 0, + "hide_border": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "in_preview": 0, + "in_filter": 0, + "in_global_search": 0, + "read_only": 0, + "allow_on_submit": 0, + "ignore_user_permissions": 0, + "allow_bulk_edit": 0, + "permlevel": 0, + "ignore_xss_filter": 0, + "unique": 0, + "no_copy": 0, + "set_only_once": 0, + "remember_last_selected_value": 0, + "parent": "new-doctype-1", + "parentfield": "fields", + "parenttype": "DocType", + "idx": 2, + "__unedited": false, + "label": "email" + } + ] +} diff --git a/xhiveframework/custom/form_tour/custom_field/custom_field.json b/xhiveframework/custom/form_tour/custom_field/custom_field.json new file mode 100644 index 0000000..3279449 --- /dev/null +++ b/xhiveframework/custom/form_tour/custom_field/custom_field.json @@ -0,0 +1,79 @@ +{ + "creation": "2021-11-23 12:22:32.922700", + "docstatus": 0, + "doctype": "Form Tour", + "first_document": 0, + "idx": 0, + "include_name_field": 0, + "is_standard": 1, + "modified": "2021-11-24 19:15:34.244244", + "modified_by": "Administrator", + "module": "Custom", + "name": "Custom Field", + "owner": "Administrator", + "reference_doctype": "Custom Field", + "save_on_complete": 1, + "steps": [ + { + "description": "Select a Document for which you want the Custom Field", + "field": "", + "fieldname": "dt", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Document", + "parent_field": "", + "position": "Right", + "title": "Document" + }, + { + "description": "Enter a Label for this field", + "field": "", + "fieldname": "label", + "fieldtype": "Data", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Label", + "parent_field": "", + "position": "Right", + "title": "Label" + }, + { + "description": "Select the label after which you want to insert new field.", + "field": "", + "fieldname": "insert_after", + "fieldtype": "Select", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Insert After", + "parent_field": "", + "position": "Right", + "title": "Insert After" + }, + { + "description": "Select an appropriate Field Type that suits your requirements", + "field": "", + "fieldname": "fieldtype", + "fieldtype": "Select", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Field Type", + "parent_field": "", + "position": "Left", + "title": "Field Type" + }, + { + "description": "Check this to make it a mandatory field", + "field": "", + "fieldname": "reqd", + "fieldtype": "Check", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Is Mandatory Field", + "parent_field": "", + "position": "Left", + "title": "Is Mandatory Field" + } + ], + "title": "Custom Field" +} \ No newline at end of file diff --git a/xhiveframework/custom/module_onboarding/customization/customization.json b/xhiveframework/custom/module_onboarding/customization/customization.json new file mode 100644 index 0000000..74719f0 --- /dev/null +++ b/xhiveframework/custom/module_onboarding/customization/customization.json @@ -0,0 +1,44 @@ +{ + "allow_roles": [ + { + "role": "All" + } + ], + "creation": "2021-11-23 12:21:11.384229", + "docstatus": 0, + "doctype": "Module Onboarding", + "documentation_url": "https://docs.xhiveerp.com/docs/v13/user/manual/en/customize-xhiveerp", + "idx": 0, + "is_complete": 0, + "modified": "2021-11-24 17:04:31.523715", + "modified_by": "Administrator", + "module": "Custom", + "name": "Customization", + "owner": "Administrator", + "steps": [ + { + "step": "Custom Field" + }, + { + "step": "Custom Doctype" + }, + { + "step": "Naming Series" + }, + { + "step": "Workflows" + }, + { + "step": "Role Permissions" + }, + { + "step": "Print Format" + }, + { + "step": "Report Builder" + } + ], + "subtitle": "Custom Field, Custom Doctype, Naming Series, Role Permission, Workflow, Print Formats, Reports", + "success_message": "Customization onboarding is all done!", + "title": "Customization" +} diff --git a/xhiveframework/custom/onboarding_step/custom_doctype/custom_doctype.json b/xhiveframework/custom/onboarding_step/custom_doctype/custom_doctype.json new file mode 100644 index 0000000..2a84d8b --- /dev/null +++ b/xhiveframework/custom/onboarding_step/custom_doctype/custom_doctype.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Learn more about creating new DocTypes", + "creation": "2021-11-23 12:30:04.407568", + "description": "A DocType (Document Type) is used to insert forms in XhiveERP. Forms such as Customer, Orders, and Invoices are Doctypes in the backend. You can also create new DocTypes to create new forms in XhiveERP as per your business needs.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-23 12:30:04.407568", + "modified_by": "Administrator", + "name": "Custom Doctype", + "owner": "Administrator", + "reference_document": "DocType", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Custom Document Types", + "validate_action": 1 +} diff --git a/xhiveframework/custom/onboarding_step/custom_field/custom_field.json b/xhiveframework/custom/onboarding_step/custom_field/custom_field.json new file mode 100644 index 0000000..f4e1e09 --- /dev/null +++ b/xhiveframework/custom/onboarding_step/custom_field/custom_field.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Learn how to add Custom Fields", + "creation": "2021-11-23 12:21:09.479808", + "description": "Every form in XhiveERP has a standard set of fields. If you need to capture some information, but there is no standard Field available for it, you can insert Custom Field for it.\n\nOnce custom fields are added, you can use them for reports and analytics charts as well.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-23 12:21:09.479808", + "modified_by": "Administrator", + "name": "Custom Field", + "owner": "Administrator", + "reference_document": "Custom Field", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Create Custom Fields", + "validate_action": 1 +} diff --git a/xhiveframework/custom/onboarding_step/naming_series/naming_series.json b/xhiveframework/custom/onboarding_step/naming_series/naming_series.json new file mode 100644 index 0000000..8dc4c28 --- /dev/null +++ b/xhiveframework/custom/onboarding_step/naming_series/naming_series.json @@ -0,0 +1,20 @@ +{ + "action": "Watch Video", + "creation": "2021-11-23 13:57:45.091427", + "description": "Each document created in XhiveERP can have a unique ID generated for it, using a prefix defined for it. Though each document has some prefix pre-configured, you can further customize it using tools like Naming Series Tool and Document Naming Rule.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-24 15:04:14.662684", + "modified_by": "Administrator", + "name": "Naming Series", + "owner": "Administrator", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Setup Naming Series", + "validate_action": 1, + "video_url": "https://youtu.be/IGyISSfI1qU" +} diff --git a/xhiveframework/custom/onboarding_step/print_format/print_format.json b/xhiveframework/custom/onboarding_step/print_format/print_format.json new file mode 100644 index 0000000..681ef85 --- /dev/null +++ b/xhiveframework/custom/onboarding_step/print_format/print_format.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Learn about Standard and Custom Print Formats", + "creation": "2021-11-23 15:04:12.728513", + "description": "Print Formats allow you can define looks for documents when printed or converted to PDF. You can also create a custom Print Format using drag-and-drop tools.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-23 15:04:12.728513", + "modified_by": "Administrator", + "name": "Print Format", + "owner": "Administrator", + "reference_document": "Print Format", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Customize Print Formats", + "validate_action": 1 +} \ No newline at end of file diff --git a/xhiveframework/custom/onboarding_step/report_builder/report_builder.json b/xhiveframework/custom/onboarding_step/report_builder/report_builder.json new file mode 100644 index 0000000..d98d3bf --- /dev/null +++ b/xhiveframework/custom/onboarding_step/report_builder/report_builder.json @@ -0,0 +1,22 @@ +{ + "action": "Watch Video", + "action_label": "Learn more about Report Builders", + "creation": "2021-11-24 17:04:18.762838", + "description": "In each module, you will find a host of single-click reports, ranging from financial statements to sales and purchase analytics and stock tracking reports. If a required new report is not available out-of-the-box, you can create custom reports in XhiveERP by pulling values from the same multiple XhiveERP tables.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-24 17:04:18.762838", + "modified_by": "Administrator", + "name": "Report Builder", + "owner": "Administrator", + "reference_document": "Report", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Generate Custom Reports", + "validate_action": 1, + "video_url": "https://youtu.be/TxJGUNarcQs" +} diff --git a/xhiveframework/custom/onboarding_step/role_permissions/role_permissions.json b/xhiveframework/custom/onboarding_step/role_permissions/role_permissions.json new file mode 100644 index 0000000..2180690 --- /dev/null +++ b/xhiveframework/custom/onboarding_step/role_permissions/role_permissions.json @@ -0,0 +1,20 @@ +{ + "action": "Watch Video", + "creation": "2021-11-23 14:00:27.208500", + "description": "In XhiveERP, you can add your Employees as Users, and give them restricted access. Tools like Role Permission and User Permission allow you to define rules which give restricted access to the user to masters and transactions.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-24 15:04:14.615232", + "modified_by": "Administrator", + "name": "Role Permissions", + "owner": "Administrator", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Setup Limited Access for a User", + "validate_action": 1, + "video_url": "https://youtu.be/g3mk45o1zAg" +} diff --git a/xhiveframework/custom/onboarding_step/workflows/workflows.json b/xhiveframework/custom/onboarding_step/workflows/workflows.json new file mode 100644 index 0000000..9ee3ca0 --- /dev/null +++ b/xhiveframework/custom/onboarding_step/workflows/workflows.json @@ -0,0 +1,20 @@ +{ + "action": "Watch Video", + "creation": "2021-11-23 13:58:58.530044", + "description": "Workflows allow you to define custom rules for the approval process of a particular document in XhiveERP. You can also set complex Workflow Rules and set approval conditions.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-11-24 15:04:14.632144", + "modified_by": "Administrator", + "name": "Workflows", + "owner": "Administrator", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Setup Approval Workflows", + "validate_action": 1, + "video_url": "https://youtu.be/yObJUg9FxFs" +} diff --git a/xhiveframework/custom/report/__init__.py b/xhiveframework/custom/report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/report/audit_system_hooks/__init__.py b/xhiveframework/custom/report/audit_system_hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.js b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.js new file mode 100644 index 0000000..5433c99 --- /dev/null +++ b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.js @@ -0,0 +1,6 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["Audit System Hooks"] = { + filters: [], +}; diff --git a/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.json b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.json new file mode 100644 index 0000000..b13a43a --- /dev/null +++ b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.json @@ -0,0 +1,27 @@ +{ + "add_total_row": 0, + "columns": [], + "creation": "2023-01-25 15:02:21.896117", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "filters": [], + "idx": 0, + "is_standard": "Yes", + "letter_head": "", + "modified": "2023-01-31 14:53:37.778576", + "modified_by": "Administrator", + "module": "Custom", + "name": "Audit System Hooks", + "owner": "Administrator", + "prepared_report": 0, + "query": "", + "ref_doctype": "Property Setter", + "report_name": "Audit System Hooks", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.py b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.py new file mode 100644 index 0000000..5f78040 --- /dev/null +++ b/xhiveframework/custom/report/audit_system_hooks/audit_system_hooks.py @@ -0,0 +1,68 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework + + +def execute(filters=None): + return get_columns(), get_data() + + +def get_columns(): + values_field_type = "Data" # TODO: better text wrapping in reportview + columns = [ + {"label": "Hook name", "fieldname": "hook_name", "fieldtype": "Data", "width": 200}, + {"label": "Hook key (optional)", "fieldname": "hook_key", "fieldtype": "Data", "width": 200}, + {"label": "Hook Values (resolved)", "fieldname": "hook_values", "fieldtype": values_field_type}, + ] + + # Each app is shown in order as a column + installed_apps = xhiveframework.get_installed_apps(_ensure_on_bench=True) + columns += [{"label": app, "fieldname": app, "fieldtype": values_field_type} for app in installed_apps] + + return columns + + +def get_data(): + hooks = xhiveframework.get_hooks() + installed_apps = xhiveframework.get_installed_apps(_ensure_on_bench=True) + + def fmt_hook_values(v): + """Improve readability by discarding falsy values and removing containers when only 1 + value is in container""" + if not v: + return "" + + v = delist(v) + + if isinstance(v, dict | list): + try: + return xhiveframework.as_json(v) + except Exception: + pass + + return str(v) + + data = [] + for hook, values in hooks.items(): + if isinstance(values, dict): + for k, v in values.items(): + row = {"hook_name": hook, "hook_key": fmt_hook_values(k), "hook_values": fmt_hook_values(v)} + for app in installed_apps: + if app_hooks := delist(xhiveframework.get_hooks(hook, app_name=app)): + row[app] = fmt_hook_values(app_hooks.get(k)) + data.append(row) + else: + row = {"hook_name": hook, "hook_values": fmt_hook_values(values)} + for app in installed_apps: + row[app] = fmt_hook_values(xhiveframework.get_hooks(hook, app_name=app)) + + data.append(row) + + return data + + +def delist(val): + if isinstance(val, list) and len(val) == 1: + return val[0] + return val diff --git a/xhiveframework/custom/report/audit_system_hooks/test_audit_system_hooks.py b/xhiveframework/custom/report/audit_system_hooks/test_audit_system_hooks.py new file mode 100644 index 0000000..a822692 --- /dev/null +++ b/xhiveframework/custom/report/audit_system_hooks/test_audit_system_hooks.py @@ -0,0 +1,17 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + + +from xhiveframework.custom.report.audit_system_hooks.audit_system_hooks import execute +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestAuditSystemHooksReport(XhiveFrameworkTestCase): + def test_basic_query(self): + _, data = execute() + for row in data: + if row.get("hook_name") == "app_name": + self.assertEqual(row.get("hook_values"), "xhiveframework") + break + else: + self.fail("Failed to generate hooks report") diff --git a/xhiveframework/data/google_fonts.json b/xhiveframework/data/google_fonts.json new file mode 100644 index 0000000..232e509 --- /dev/null +++ b/xhiveframework/data/google_fonts.json @@ -0,0 +1,56 @@ +[ + "Alegreya Sans", + "Alegreya", + "Andada Pro", + "Anton", + "Archivo Narrow", + "Archivo", + "BioRhyme", + "Cardo", + "Chivo", + "Cormorant", + "Crimson Text", + "DM Sans", + "Eczar", + "Encode Sans", + "Epilogue ", + "Fira Sans", + "Hahmlet", + "IBM Plex Sans", + "Inconsolata", + "Inknut Antiqua", + "Inter", + "JetBrains Mono", + "Karla", + "Lato", + "Libre Baskerville", + "Libre Franklin", + "Lora", + "Manrope", + "Merriweather", + "Montserrat", + "Neuton", + "Nunito", + "Old Standard TT", + "Open Sans", + "Oswald", + "Oxygen", + "Playfair Display", + "Poppins", + "Proza Libre", + "PT Sans", + "PT Serif", + "Raleway", + "Roboto Slab", + "Roboto", + "Rubik", + "Sora", + "Source Sans Pro", + "Source Serif Pro", + "Space Grotesk", + "Space Mono", + "Spectral", + "Syne", + "Work Sans" +] + diff --git a/xhiveframework/database/__init__.py b/xhiveframework/database/__init__.py new file mode 100644 index 0000000..0c08a1f --- /dev/null +++ b/xhiveframework/database/__init__.py @@ -0,0 +1,108 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# Database Module +# -------------------- +from shutil import which + +from xhiveframework.database.database import savepoint + + +def setup_database(force, source_sql=None, verbose=None, no_mariadb_socket=False): + import xhiveframework + + if xhiveframework.conf.db_type == "postgres": + import xhiveframework.database.postgres.setup_db + + return xhiveframework.database.postgres.setup_db.setup_database(force, source_sql, verbose) + else: + import xhiveframework.database.mariadb.setup_db + + return xhiveframework.database.mariadb.setup_db.setup_database( + force, source_sql, verbose, no_mariadb_socket=no_mariadb_socket + ) + + +def drop_user_and_database(db_name, root_login=None, root_password=None): + import xhiveframework + + if xhiveframework.conf.db_type == "postgres": + import xhiveframework.database.postgres.setup_db + + return xhiveframework.database.postgres.setup_db.drop_user_and_database(db_name, root_login, root_password) + else: + import xhiveframework.database.mariadb.setup_db + + return xhiveframework.database.mariadb.setup_db.drop_user_and_database(db_name, root_login, root_password) + + +def get_db(host=None, user=None, password=None, port=None): + import xhiveframework + + if xhiveframework.conf.db_type == "postgres": + import xhiveframework.database.postgres.database + + return xhiveframework.database.postgres.database.PostgresDatabase(host, user, password, port=port) + else: + import xhiveframework.database.mariadb.database + + return xhiveframework.database.mariadb.database.MariaDBDatabase(host, user, password, port=port) + + +def get_command(host=None, port=None, user=None, password=None, db_name=None, extra=None, dump=False): + import xhiveframework + + if xhiveframework.conf.db_type == "postgres": + if dump: + bin, bin_name = which("pg_dump"), "pg_dump" + else: + bin, bin_name = which("psql"), "psql" + + if password: + conn_string = f"postgresql://{user}:{password}@{host}:{port}/{db_name}" + else: + conn_string = f"postgresql://{user}@{host}:{port}/{db_name}" + + command = [conn_string] + + if extra: + command.extend(extra) + + else: + if dump: + bin, bin_name = which("mariadb-dump") or which("mysqldump"), "mariadb-dump" + else: + bin, bin_name = which("mariadb") or which("mysql"), "mariadb" + + command = [ + f"--user={user}", + f"--host={host}", + f"--port={port}", + ] + + if password: + command.append(f"--password={password}") + + if dump: + command.extend( + [ + "--single-transaction", + "--quick", + "--lock-tables=false", + ] + ) + else: + command.extend( + [ + "--pager=less -SFX", + "--safe-updates", + "--no-auto-rehash", + ] + ) + + command.append(db_name) + + if extra: + command.extend(extra) + + return bin, command, bin_name diff --git a/xhiveframework/database/database.py b/xhiveframework/database/database.py new file mode 100644 index 0000000..711144f --- /dev/null +++ b/xhiveframework/database/database.py @@ -0,0 +1,1431 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import datetime +import itertools +import json +import random +import re +import string +import traceback +from collections.abc import Iterable, Sequence +from contextlib import contextmanager, suppress +from time import time +from typing import TYPE_CHECKING, Any, Union + +from pypika.dialects import MySQLQueryBuilder, PostgreSQLQueryBuilder +from pypika.terms import Criterion, NullValue + +import xhiveframework +import xhiveframework.defaults +from xhiveframework import _ +from xhiveframework.database.utils import ( + DefaultOrderBy, + EmptyQueryValues, + FallBackDateTimeStr, + LazyMogrify, + Query, + QueryValues, + is_query_type, +) +from xhiveframework.exceptions import DoesNotExistError, ImplicitCommitError +from xhiveframework.monitor import get_trace_id +from xhiveframework.query_builder.functions import Count +from xhiveframework.utils import CallbackManager, cint, get_datetime, get_table_name, getdate, now, sbool +from xhiveframework.utils import cast as cast_fieldtype +from xhiveframework.utils.deprecations import deprecation_warning + +if TYPE_CHECKING: + from psycopg2 import connection as PostgresConnection + from psycopg2 import cursor as PostgresCursor + from pymysql.connections import Connection as MariadbConnection + from pymysql.cursors import Cursor as MariadbCursor + + +IFNULL_PATTERN = re.compile(r"ifnull\(", flags=re.IGNORECASE) +INDEX_PATTERN = re.compile(r"\s*\([^)]+\)\s*") +SINGLE_WORD_PATTERN = re.compile(r'([`"]?)(tab([A-Z]\w+))\1') +MULTI_WORD_PATTERN = re.compile(r'([`"])(tab([A-Z]\w+)( [A-Z]\w+)+)\1') + +SQL_ITERATOR_BATCH_SIZE = 100 + + +class Database: + """ + Open a database connection with the given parmeters, if use_default is True, use the + login details from `conf.py`. This is called by the request handler and is accessible using + the `db` global variable. the `sql` method is also global to run queries + """ + + VARCHAR_LEN = 140 + MAX_COLUMN_LENGTH = 64 + + OPTIONAL_COLUMNS = ("_user_tags", "_comments", "_assign", "_liked_by") + DEFAULT_SHORTCUTS = ("_Login", "__user", "_Full Name", "Today", "__today", "now", "Now") + STANDARD_VARCHAR_COLUMNS = ("name", "owner", "modified_by") + DEFAULT_COLUMNS = ("name", "creation", "modified", "modified_by", "owner", "docstatus", "idx") + CHILD_TABLE_COLUMNS = ("parent", "parenttype", "parentfield") + MAX_WRITES_PER_TRANSACTION = 200_000 + + class InvalidColumnName(xhiveframework.ValidationError): + pass + + def __init__( + self, + host=None, + user=None, + password=None, + ac_name=None, + use_default=0, + port=None, + ): + self.setup_type_map() + self.host = host or xhiveframework.conf.db_host + self.port = port or xhiveframework.conf.db_port + self.user = user or xhiveframework.conf.db_name + self.db_name = xhiveframework.conf.db_name + self._conn = None + + if ac_name: + self.user = ac_name or xhiveframework.conf.db_name + + if use_default: + self.user = xhiveframework.conf.db_name + + self.transaction_writes = 0 + self.auto_commit_on_many_writes = 0 + + self.password = password or xhiveframework.conf.db_password + self.value_cache = {} + self.logger = xhiveframework.logger("database") + self.logger.setLevel("WARNING") + + self.before_commit = CallbackManager() + self.after_commit = CallbackManager() + self.before_rollback = CallbackManager() + self.after_rollback = CallbackManager() + + # self.db_type: str + # self.last_query (lazy) attribute of last sql query executed + + def setup_type_map(self): + pass + + def connect(self): + """Connects to a database as set in `site_config.json`.""" + self.cur_db_name = self.user + self._conn: "MariadbConnection" | "PostgresConnection" = self.get_connection() + self._cursor: "MariadbCursor" | "PostgresCursor" = self._conn.cursor() + + try: + if execution_timeout := get_query_execution_timeout(): + self.set_execution_timeout(execution_timeout) + except Exception as e: + self.logger.warning(f"Couldn't set execution timeout {e}") + + def set_execution_timeout(self, seconds: int): + """Set session speicifc timeout on exeuction of statements. + If any statement takes more time it will be killed along with entire transaction.""" + raise NotImplementedError + + def use(self, db_name): + """`USE` db_name.""" + self._conn.select_db(db_name) + + def get_connection(self): + """Returns a Database connection object that conforms with https://peps.python.org/pep-0249/#connection-objects""" + raise NotImplementedError + + def get_database_size(self): + raise NotImplementedError + + def _transform_query(self, query: Query, values: QueryValues) -> tuple: + return query, values + + def _transform_result(self, result: list[tuple]) -> list[tuple]: + return result + + def _clean_up(self): + pass + + def sql( + self, + query: Query, + values: QueryValues = EmptyQueryValues, + *, + as_dict=0, + as_list=0, + debug=0, + ignore_ddl=0, + auto_commit=0, + update=None, + explain=False, + run=True, + pluck=False, + as_iterator=False, + ): + """Execute a SQL query and fetch all rows. + + :param query: SQL query. + :param values: Tuple / List / Dict of values to be escaped and substituted in the query. + :param as_dict: Return as a dictionary. + :param as_list: Always return as a list. + :param debug: Print query and `EXPLAIN` in debug log. + :param ignore_ddl: Catch exception if table, column missing. + :param auto_commit: Commit after executing the query. + :param update: Update this dict to all rows (if returned `as_dict`). + :param run: Return query without executing it if False. + :param pluck: Get the plucked field only. + :param explain: Print `EXPLAIN` in error log. + :param as_iterator: Returns iterator over results instead of fetching all results at once. + This should be used with unbuffered cursor as default cursors used by pymysql and postgres + buffer the results internally. See `Database.unbuffered_cursor`. + Examples: + + # return customer names as dicts + xhiveframework.db.sql("select name from tabCustomer", as_dict=True) + + # return names beginning with a + xhiveframework.db.sql("select name from tabCustomer where name like %s", "a%") + + # values as dict + xhiveframework.db.sql("select name from tabCustomer where name like %(name)s and owner=%(owner)s", + {"name": "a%", "owner":"test@example.com"}) + + """ + if isinstance(query, MySQLQueryBuilder | PostgreSQLQueryBuilder): + xhiveframework.log("Use run method to execute SQL queries generated by Query Engine") + + debug = debug or getattr(self, "debug", False) + query = str(query) + if not run: + return query + + # remove whitespace / indentation from start and end of query + query = query.strip() + + # replaces ifnull in query with coalesce + query = IFNULL_PATTERN.sub("coalesce(", query) + + if not self._conn: + self.connect() + + # in transaction validations + self.check_transaction_status(query) + self.clear_db_table_cache(query) + + if auto_commit: + self.commit() + + if debug: + time_start = time() + + if values == EmptyQueryValues: + values = None + elif not isinstance(values, tuple | dict | list): + values = (values,) + + query, values = self._transform_query(query, values) + + if trace_id := get_trace_id(): + query += f" /* XHIVEFRAMEWORK_TRACE_ID: {trace_id} */" + + try: + self._cursor.execute(query, values) + except Exception as e: + if self.is_syntax_error(e): + xhiveframework.log(f"Syntax error in query:\n{query} {values or ''}") + + elif self.is_deadlocked(e): + raise xhiveframework.QueryDeadlockError(e) from e + + elif self.is_timedout(e): + raise xhiveframework.QueryTimeoutError(e) from e + + elif self.is_read_only_mode_error(e): + xhiveframework.throw( + _( + "Site is running in read only mode for maintenance or site update, this action can not be performed right now. Please try again later." + ), + title=_("In Read Only Mode"), + exc=xhiveframework.InReadOnlyMode, + ) + + # TODO: added temporarily + elif self.db_type == "postgres": + traceback.print_stack() + xhiveframework.log(f"Error in query:\n{e}") + raise + + elif isinstance(e, self.ProgrammingError): + if xhiveframework.conf.developer_mode: + traceback.print_stack() + xhiveframework.log(f"Error in query:\n{query, values}") + raise + + if not ( + ignore_ddl + and (self.is_missing_column(e) or self.is_table_missing(e) or self.cant_drop_field_or_key(e)) + ): + raise + + if debug: + time_end = time() + xhiveframework.log(f"Execution time: {time_end - time_start:.2f} sec") + + self.log_query(query, values, debug, explain) + + if auto_commit: + self.commit() + + if not self._cursor.description: + return () + + if as_iterator: + return self._return_as_iterator(pluck=pluck, as_dict=as_dict, as_list=as_list, update=update) + + last_result = self._transform_result(self._cursor.fetchall()) + if pluck: + last_result = [r[0] for r in last_result] + self._clean_up() + return last_result + + # scrub output if required + if as_dict: + last_result = self.fetch_as_dict(last_result) + if update: + for r in last_result: + r.update(update) + + elif as_list: + last_result = self.convert_to_lists(last_result) + + self._clean_up() + return last_result + + def _return_as_iterator(self, *, pluck, as_dict, as_list, update): + while result := self._transform_result(self._cursor.fetchmany(SQL_ITERATOR_BATCH_SIZE)): + if pluck: + for row in result: + yield row[0] + + elif as_dict: + keys = [column[0] for column in self._cursor.description] + for row in result: + row = xhiveframework._dict(zip(keys, row, strict=False)) + if update: + row.update(update) + yield row + + elif as_list: + for row in result: + yield list(row) + else: + xhiveframework.throw(_("`as_iterator` only works with `as_list=True` or `as_dict=True`")) + + self._clean_up() + + def _log_query( + self, + mogrified_query: str, + debug: bool = False, + explain: bool = False, + unmogrified_query: str = "", + ) -> None: + """Takes the query and logs it to various interfaces according to the settings.""" + _query = None + + if xhiveframework.conf.allow_tests and xhiveframework.cache.get_value("flag_print_sql"): + _query = _query or str(mogrified_query) + print(_query) + + if debug: + _query = _query or str(mogrified_query) + if explain and is_query_type(_query, "select"): + self.explain_query(_query) + xhiveframework.log(_query) + + if xhiveframework.conf.logging == 2: + _query = _query or str(mogrified_query) + xhiveframework.log(f"#### query\n{_query}\n####") + + if unmogrified_query and is_query_type( + unmogrified_query, ("alter", "drop", "create", "truncate", "rename") + ): + _query = _query or str(mogrified_query) + self.logger.warning("DDL Query made to DB:\n" + _query) + + if xhiveframework.flags.in_migrate: + _query = _query or str(mogrified_query) + self.log_touched_tables(_query) + + def log_query( + self, query: str, values: QueryValues = None, debug: bool = False, explain: bool = False + ) -> str: + # TODO: Use mogrify until MariaDB Connector/C 1.1 is released and we can fetch something + # like cursor._transformed_statement from the cursor object. We can also avoid setting + # mogrified_query if we don't need to log it. + mogrified_query = self.lazy_mogrify(query, values) + self._log_query(mogrified_query, debug, explain, unmogrified_query=query) + return mogrified_query + + def mogrify(self, query: Query, values: QueryValues): + """build the query string with values""" + if not values: + return query + + try: + return self._cursor.mogrify(query, values) + except AttributeError: + if isinstance(values, dict): + return query % { + k: xhiveframework.db.escape(v) if isinstance(v, str) else v for k, v in values.items() + } + elif isinstance(values, list | tuple): + return query % tuple(xhiveframework.db.escape(v) if isinstance(v, str) else v for v in values) + return query, values + + def lazy_mogrify(self, query: Query, values: QueryValues) -> LazyMogrify: + """Wrap the object with str to generate mogrified query.""" + return LazyMogrify(query, values) + + def explain_query(self, query, values=None): + """Print `EXPLAIN` in error log.""" + xhiveframework.log("--- query explain ---") + try: + self._cursor.execute(f"EXPLAIN {query}", values) + except Exception as e: + xhiveframework.log(f"error in query explain: {e}") + else: + xhiveframework.log(json.dumps(self.fetch_as_dict(), indent=1)) + xhiveframework.log("--- query explain end ---") + + def sql_list(self, query, values=(), debug=False, **kwargs): + """Return data as list of single elements (first column). + + Example: + + # doctypes = ["DocType", "DocField", "User", ...] + doctypes = xhiveframework.db.sql_list("select name from DocType") + """ + return self.sql(query, values, **kwargs, debug=debug, pluck=True) + + def sql_ddl(self, query, debug=False): + """Commit and execute a query. DDL (Data Definition Language) queries that alter schema + autocommit in MariaDB.""" + self.commit() + self.sql(query, debug=debug) + + def check_transaction_status(self, query): + """Raises exception if more than 200,000 `INSERT`, `UPDATE` queries are + executed in one transaction. This is to ensure that writes are always flushed otherwise this + could cause the system to hang.""" + self.check_implicit_commit(query) + + if query and is_query_type(query, ("commit", "rollback")): + self.transaction_writes = 0 + + if query[:6].lower() in ("update", "insert", "delete"): + self.transaction_writes += 1 + if self.transaction_writes > self.MAX_WRITES_PER_TRANSACTION: + if self.auto_commit_on_many_writes: + self.commit() + else: + msg = "

    " + _("Too many changes to database in single action.") + "
    " + msg += _("The changes have been reverted.") + "
    " + raise xhiveframework.TooManyWritesError(msg) + + def check_implicit_commit(self, query): + if ( + self.transaction_writes + and query + and is_query_type(query, ("start", "alter", "drop", "create", "begin", "truncate")) + ): + raise ImplicitCommitError("This statement can cause implicit commit") + + def fetch_as_dict(self, result) -> list[xhiveframework._dict]: + """Internal. Convert results to dict.""" + if result: + keys = [column[0] for column in self._cursor.description] + + return [xhiveframework._dict(zip(keys, row, strict=False)) for row in result] + + @staticmethod + def clear_db_table_cache(query): + if query and is_query_type(query, ("drop", "create")): + xhiveframework.cache.delete_key("db_tables") + + def get_description(self): + """Returns result metadata.""" + return self._cursor.description + + @staticmethod + def convert_to_lists(res): + """Convert tuple output to lists (internal).""" + return [[value for value in row] for row in res] + + def get(self, doctype, filters=None, as_dict=True, cache=False): + """Returns `get_value` with fieldname='*'""" + return self.get_value(doctype, filters, "*", as_dict=as_dict, cache=cache) + + def get_value( + self, + doctype, + filters=None, + fieldname="name", + ignore=None, + as_dict=False, + debug=False, + order_by=DefaultOrderBy, + cache=False, + for_update=False, + *, + run=True, + pluck=False, + distinct=False, + skip_locked=False, + wait=True, + ): + """Returns a document property or list of properties. + + :param doctype: DocType name. + :param filters: Filters like `{"x":"y"}` or name of the document. `None` if Single DocType. + :param fieldname: Column name. + :param ignore: Don't raise exception if table, column is missing. + :param as_dict: Return values as dict. + :param debug: Print query in error log. + :param order_by: Column to order by + :param cache: Use cached results fetched during current job/request + :param pluck: pluck first column instead of returning as nested list or dict. + :param for_update: All the affected/read rows will be locked. + :param skip_locked: Skip selecting currently locked rows. + :param wait: Wait for aquiring lock + + Example: + + # return first customer starting with a + xhiveframework.db.get_value("Customer", {"name": ("like a%")}) + + # return last login of **User** `test@example.com` + xhiveframework.db.get_value("User", "test@example.com", "last_login") + + last_login, last_ip = xhiveframework.db.get_value("User", "test@example.com", + ["last_login", "last_ip"]) + + # returns default date_format + xhiveframework.db.get_value("System Settings", None, "date_format") + """ + + result = self.get_values( + doctype, + filters, + fieldname, + ignore, + as_dict, + debug, + order_by, + cache=cache, + for_update=for_update, + run=run, + pluck=pluck, + distinct=distinct, + limit=1, + skip_locked=skip_locked, + wait=wait, + ) + + if not run: + return result + + if not result: + return None + + row = result[0] + + if len(row) > 1 or as_dict: + return row + # single field is requested, send it without wrapping in containers + return row[0] + + def get_values( + self, + doctype, + filters=None, + fieldname="name", + ignore=None, + as_dict=False, + debug=False, + order_by=DefaultOrderBy, + update=None, + cache=False, + for_update=False, + *, + run=True, + pluck=False, + distinct=False, + limit=None, + skip_locked=False, + wait=True, + ): + """Returns multiple document properties. + + :param doctype: DocType name. + :param filters: Filters like `{"x":"y"}` or name of the document. + :param fieldname: Column name. + :param ignore: Don't raise exception if table, column is missing. + :param as_dict: Return values as dict. + :param debug: Print query in error log. + :param order_by: Column to order by, + :param distinct: Get Distinct results. + + Example: + + # return first customer starting with a + customers = xhiveframework.db.get_values("Customer", {"name": ("like a%")}) + + # return last login of **User** `test@example.com` + user = xhiveframework.db.get_values("User", "test@example.com", "*")[0] + """ + out = None + if cache and isinstance(filters, str) and (doctype, filters, fieldname) in self.value_cache: + return self.value_cache[(doctype, filters, fieldname)] + + if distinct: + order_by = None + + if isinstance(filters, list): + out = self._get_value_for_many_names( + doctype=doctype, + names=filters, + field=fieldname, + order_by=order_by, + debug=debug, + run=run, + pluck=pluck, + distinct=distinct, + limit=limit, + as_dict=as_dict, + skip_locked=skip_locked, + wait=True, + for_update=for_update, + ) + + else: + fields = fieldname + if fieldname != "*": + if isinstance(fieldname, str): + fields = [fieldname] + + if (filters is not None) and (filters != doctype or doctype == "DocType"): + try: + if order_by: + order_by = "modified" if order_by == DefaultOrderBy else order_by + out = self._get_values_from_table( + fields=fields, + filters=filters, + doctype=doctype, + as_dict=as_dict, + debug=debug, + order_by=order_by, + update=update, + run=run, + pluck=pluck, + distinct=distinct, + limit=limit, + for_update=for_update, + skip_locked=skip_locked, + wait=wait, + ) + except Exception as e: + if ignore and ( + xhiveframework.db.is_missing_column(e) + or xhiveframework.db.is_table_missing(e) + or str(e).startswith("Invalid DocType") + ): + out = None + elif (not ignore) and xhiveframework.db.is_table_missing(e): + # table not found, look in singles + out = self.get_values_from_single( + fields, filters, doctype, as_dict, debug, update, run=run, distinct=distinct + ) + + else: + raise + else: + out = self.get_values_from_single( + fields, filters, doctype, as_dict, debug, update, run=run, pluck=pluck, distinct=distinct + ) + + if cache and isinstance(filters, str): + self.value_cache[(doctype, filters, fieldname)] = out + + return out + + def get_values_from_single( + self, + fields, + filters, + doctype, + as_dict=False, + debug=False, + update=None, + *, + run=True, + pluck=False, + distinct=False, + ): + """Get values from `tabSingles` (Single DocTypes) (internal). + + :param fields: List of fields, + :param filters: Filters (dict). + :param doctype: DocType name. + """ + if fields == "*" or isinstance(filters, dict): + # check if single doc matches with filters + values = self.get_singles_dict(doctype) + if isinstance(filters, dict): + for key, value in filters.items(): + if values.get(key) != value: + return [] + + if as_dict: + return [values] if values else [] + + if isinstance(fields, list): + return [list(map(values.get, fields))] + + else: + r = xhiveframework.qb.get_query( + "Singles", + filters={"field": ("in", tuple(fields)), "doctype": doctype}, + fields=["field", "value"], + distinct=distinct, + ).run(pluck=pluck, debug=debug, as_dict=False) + + if not run: + return r + + if not r: + return [] + + r = xhiveframework._dict(r) + if update: + r.update(update) + + if not as_dict: + return [[r.get(field) for field in fields]] + + return [r] + + def get_singles_dict(self, doctype, debug=False, *, for_update=False, cast=False): + """Get Single DocType as dict. + + :param doctype: DocType of the single object whose value is requested + :param debug: Execute query in debug mode - print to STDOUT + :param for_update: Take `FOR UPDATE` lock on the records + :param cast: Cast values to Python data types based on field type + + Example: + + # Get coulmn and value of the single doctype Accounts Settings + account_settings = xhiveframework.db.get_singles_dict("Accounts Settings") + """ + queried_result = xhiveframework.qb.get_query( + "Singles", + filters={"doctype": doctype}, + fields=["field", "value"], + for_update=for_update, + ).run(debug=debug) + + if not cast: + return xhiveframework._dict(queried_result) + + try: + meta = xhiveframework.get_meta(doctype) + except DoesNotExistError: + return xhiveframework._dict(queried_result) + + return_value = xhiveframework._dict() + + for fieldname, value in queried_result: + if df := meta.get_field(fieldname): + casted_value = cast_fieldtype(df.fieldtype, value) + else: + casted_value = value + return_value[fieldname] = casted_value + + return return_value + + @staticmethod + def get_all(*args, **kwargs): + return xhiveframework.get_all(*args, **kwargs) + + @staticmethod + def get_list(*args, **kwargs): + return xhiveframework.get_list(*args, **kwargs) + + @staticmethod + def _get_update_dict( + fieldname: str | dict, value: Any, *, modified: str, modified_by: str, update_modified: bool + ) -> dict[str, Any]: + """Create update dict that represents column-values to be updated.""" + update_dict = fieldname if isinstance(fieldname, dict) else {fieldname: value} + + if update_modified: + modified = modified or now() + modified_by = modified_by or xhiveframework.session.user + update_dict.update({"modified": modified, "modified_by": modified_by}) + + return update_dict + + def set_single_value( + self, + doctype: str, + fieldname: str | dict, + value: str | int | None = None, + *, + modified=None, + modified_by=None, + update_modified=True, + debug=False, + ): + """Set field value of Single DocType. + + :param doctype: DocType of the single object + :param fieldname: `fieldname` of the property + :param value: `value` of the property + + Example: + + # Update the `deny_multiple_sessions` field in System Settings DocType. + xhiveframework.db.set_single_value("System Settings", "deny_multiple_sessions", True) + """ + + to_update = self._get_update_dict( + fieldname, value, modified=modified, modified_by=modified_by, update_modified=update_modified + ) + + xhiveframework.db.delete( + "Singles", filters={"field": ("in", tuple(to_update)), "doctype": doctype}, debug=debug + ) + + singles_data = ((doctype, key, sbool(value)) for key, value in to_update.items()) + xhiveframework.qb.into("Singles").columns("doctype", "field", "value").insert(*singles_data).run(debug=debug) + xhiveframework.clear_document_cache(doctype, doctype) + + if doctype in self.value_cache: + del self.value_cache[doctype] + + def get_single_value(self, doctype, fieldname, cache=True): + """Get property of Single DocType. Cache locally by default + + :param doctype: DocType of the single object whose value is requested + :param fieldname: `fieldname` of the property whose value is requested + + Example: + + # Get the default value of the company from the Global Defaults doctype. + company = xhiveframework.db.get_single_value('Global Defaults', 'default_company') + """ + + if doctype not in self.value_cache: + self.value_cache[doctype] = {} + + if cache and fieldname in self.value_cache[doctype]: + return self.value_cache[doctype][fieldname] + + val = xhiveframework.qb.get_query( + table="Singles", + filters={"doctype": doctype, "field": fieldname}, + fields="value", + ).run() + val = val[0][0] if val else None + + df = xhiveframework.get_meta(doctype).get_field(fieldname) + + if not df: + xhiveframework.throw( + _("Field {0} does not exist on {1}").format( + xhiveframework.bold(fieldname), xhiveframework.bold(doctype), self.InvalidColumnName + ) + ) + + val = cast_fieldtype(df.fieldtype, val) + + self.value_cache[doctype][fieldname] = val + + return val + + def get_singles_value(self, *args, **kwargs): + """Alias for get_single_value""" + return self.get_single_value(*args, **kwargs) + + def _get_values_from_table( + self, + fields, + filters, + doctype, + as_dict, + *, + debug=False, + order_by=None, + update=None, + for_update=False, + skip_locked=False, + wait=True, + run=True, + pluck=False, + distinct=False, + limit=None, + ): + query = xhiveframework.qb.get_query( + table=doctype, + filters=filters, + order_by=order_by, + for_update=for_update, + skip_locked=skip_locked, + wait=wait, + fields=fields, + distinct=distinct, + limit=limit, + validate_filters=True, + ) + if isinstance(fields, str) and fields == "*": + as_dict = True + + return query.run(as_dict=as_dict, debug=debug, update=update, run=run, pluck=pluck) + + def _get_value_for_many_names( + self, + doctype, + names, + field, + order_by, + *, + debug=False, + run=True, + pluck=False, + distinct=False, + limit=None, + as_dict=False, + for_update=False, + skip_locked=False, + wait=True, + ): + if names := list(filter(None, names)): + return xhiveframework.qb.get_query( + doctype, + fields=field, + filters=names, + order_by=order_by, + distinct=distinct, + limit=limit, + validate_filters=True, + for_update=for_update, + skip_locked=skip_locked, + wait=wait, + ).run(debug=debug, run=run, as_dict=as_dict, pluck=pluck) + return {} + + def set_value( + self, + dt, + dn, + field, + val=None, + modified=None, + modified_by=None, + update_modified=True, + debug=False, + ): + """Set a single value in the database, do not call the ORM triggers + but update the modified timestamp (unless specified not to). + + **Warning:** this function will not call Document events and should be avoided in normal cases. + + :param dt: DocType name. + :param dn: Document name for updating single record or filters for updating many records. + :param field: Property / field name or dictionary of values to be updated + :param value: Value to be updated. + :param modified: Use this as the `modified` timestamp. + :param modified_by: Set this user as `modified_by`. + :param update_modified: default True. Set as false, if you don't want to update the timestamp. + :param debug: Print the query in the developer / js console. + """ + from xhiveframework.model.utils import is_single_doctype + + if dn is None or dt == dn: + if not is_single_doctype(dt): + return + deprecation_warning( + "Calling db.set_value on single doctype is deprecated. This behaviour will be removed in future. Use db.set_single_value instead." + ) + self.set_single_value( + doctype=dt, + fieldname=field, + value=val, + debug=debug, + update_modified=update_modified, + modified=modified, + modified_by=modified_by, + ) + return + + to_update = self._get_update_dict( + field, val, modified=modified, modified_by=modified_by, update_modified=update_modified + ) + + query = xhiveframework.qb.get_query( + table=dt, + filters=dn, + update=True, + validate_filters=True, + ) + + if isinstance(dn, str): + xhiveframework.clear_document_cache(dt, dn) + else: + # No way to guess which documents are modified, clear all of them + xhiveframework.clear_document_cache(dt) + + for column, value in to_update.items(): + query = query.set(column, value) + + query.run(debug=debug) + + if dt in self.value_cache: + del self.value_cache[dt] + + def set_global(self, key, val, user="__global"): + """Save a global key value. Global values will be automatically set if they match fieldname.""" + self.set_default(key, val, user) + + def get_global(self, key, user="__global"): + """Returns a global key value.""" + return self.get_default(key, user) + + def get_default(self, key, parent="__default"): + """Returns default value as a list if multiple or single""" + d = self.get_defaults(key, parent) + return isinstance(d, list) and d[0] or d + + @staticmethod + def set_default(key, val, parent="__default", parenttype=None): + """Sets a global / user default value.""" + xhiveframework.defaults.set_default(key, val, parent, parenttype) + + @staticmethod + def add_default(key, val, parent="__default", parenttype=None): + """Append a default value for a key, there can be multiple default values for a particular key.""" + xhiveframework.defaults.add_default(key, val, parent, parenttype) + + @staticmethod + def get_defaults(key=None, parent="__default"): + """Get all defaults""" + defaults = xhiveframework.defaults.get_defaults_for(parent) + if not key: + return defaults + + if key in defaults: + return defaults[key] + + return defaults.get(xhiveframework.scrub(key)) + + def begin(self, *, read_only=False): + read_only = read_only or xhiveframework.flags.read_only + mode = "READ ONLY" if read_only else "" + self.sql(f"START TRANSACTION {mode}") + + def commit(self): + """Commit current transaction. Calls SQL `COMMIT`.""" + self.before_rollback.reset() + self.after_rollback.reset() + + self.before_commit.run() + + self.sql("commit") + self.begin() # explicitly start a new transaction + + self.after_commit.run() + + def rollback(self, *, save_point=None): + """`ROLLBACK` current transaction. Optionally rollback to a known save_point.""" + if save_point: + self.sql(f"rollback to savepoint {save_point}") + else: + self.before_commit.reset() + self.after_commit.reset() + + self.before_rollback.run() + + self.sql("rollback") + self.begin() + + self.after_rollback.run() + + def savepoint(self, save_point): + """Savepoints work as a nested transaction. + + Changes can be undone to a save point by doing xhiveframework.db.rollback(save_point) + + Note: rollback watchers can not work with save points. + so only changes to database are undone when rolling back to a savepoint. + Avoid using savepoints when writing to filesystem.""" + self.sql(f"savepoint {save_point}") + + def release_savepoint(self, save_point): + self.sql(f"release savepoint {save_point}") + + def field_exists(self, dt, fn): + """Return true of field exists.""" + return self.exists("DocField", {"fieldname": fn, "parent": dt}) + + def table_exists(self, doctype, cached=True): + """Returns True if table for given doctype exists.""" + return f"tab{doctype}" in self.get_tables(cached=cached) + + def has_table(self, doctype): + return self.table_exists(doctype) + + def get_tables(self, cached=True): + raise NotImplementedError + + def a_row_exists(self, doctype): + """Returns True if atleast one row exists.""" + return xhiveframework.get_all(doctype, limit=1, order_by=None, as_list=True) + + def exists(self, dt, dn=None, cache=False): + """Return the document name of a matching document, or None. + + Note: `cache` only works if `dt` and `dn` are of type `str`. + + ## Examples + + Pass doctype and docname (only in this case we can cache the result) + + ``` + exists("User", "jane@example.org", cache=True) + ``` + + Pass a dict of filters including the `"doctype"` key: + + ``` + exists({"doctype": "User", "full_name": "Jane Doe"}) + ``` + + Pass the doctype and a dict of filters: + + ``` + exists("User", {"full_name": "Jane Doe"}) + ``` + """ + if dt != "DocType" and dt == dn: + # single always exists (!) + return dn + + if isinstance(dt, dict): + dt = dt.copy() # don't modify the original dict + dt, dn = dt.pop("doctype"), dt + + return self.get_value(dt, dn, ignore=True, cache=cache, order_by=None) + + def count(self, dt, filters=None, debug=False, cache=False, distinct: bool = True): + """Returns `COUNT(*)` for given DocType and filters.""" + if cache and not filters: + cache_count = xhiveframework.cache.get_value(f"doctype:count:{dt}") + if cache_count is not None: + return cache_count + count = xhiveframework.qb.get_query( + table=dt, + filters=filters, + fields=Count("*"), + distinct=distinct, + validate_filters=True, + ).run(debug=debug)[0][0] + if not filters and cache: + xhiveframework.cache.set_value(f"doctype:count:{dt}", count, expires_in_sec=86400) + return count + + @staticmethod + def format_date(date): + return getdate(date).strftime("%Y-%m-%d") + + @staticmethod + def format_datetime(datetime): # noqa: F811 + if not datetime: + return FallBackDateTimeStr + + return get_datetime(datetime).strftime("%Y-%m-%d %H:%M:%S.%f") + + def get_creation_count(self, doctype, minutes): + """Get count of records created in the last x minutes""" + from dateutil.relativedelta import relativedelta + + from xhiveframework.utils import now_datetime + + Table = xhiveframework.qb.DocType(doctype) + + return ( + xhiveframework.qb.from_(Table) + .select(Count(Table.name)) + .where(Table.creation >= now_datetime() - relativedelta(minutes=minutes)) + .run()[0][0] + ) + + def get_db_table_columns(self, table) -> list[str]: + """Returns list of column names from given table.""" + columns = xhiveframework.cache.hget("table_columns", table) + if columns is None: + information_schema = xhiveframework.qb.Schema("information_schema") + + columns = ( + xhiveframework.qb.from_(information_schema.columns) + .select(information_schema.columns.column_name) + .where(information_schema.columns.table_name == table) + .run(pluck=True) + ) + + if columns: + xhiveframework.cache.hset("table_columns", table, columns) + + return columns + + def get_table_columns(self, doctype): + """Returns list of column names from given doctype.""" + columns = self.get_db_table_columns("tab" + doctype) + if not columns: + raise self.TableMissingError("DocType", doctype) + return columns + + def has_column(self, doctype, column): + """Returns True if column exists in database.""" + return column in self.get_table_columns(doctype) + + def has_index(self, table_name, index_name): + raise NotImplementedError + + def add_index(self, doctype, fields, index_name=None): + raise NotImplementedError + + def add_unique(self, doctype, fields, constraint_name=None): + raise NotImplementedError + + @staticmethod + def get_index_name(fields): + index_name = "_".join(fields) + "_index" + # remove index length if present e.g. (10) from index name + return INDEX_PATTERN.sub(r"", index_name) + + def get_system_setting(self, key): + return xhiveframework.get_system_settings(key) + + def close(self): + """Close database connection.""" + if self._conn: + self._conn.close() + self._cursor = None + self._conn = None + + @staticmethod + def escape(s, percent=True): + """Excape quotes and percent in given string.""" + # implemented in specific class + raise NotImplementedError + + @staticmethod + def is_column_missing(e): + return xhiveframework.db.is_missing_column(e) + + def get_descendants(self, doctype, name): + """Return descendants of the group node in tree""" + from xhiveframework.utils.nestedset import get_descendants_of + + try: + return get_descendants_of(doctype, name, ignore_permissions=True) + except Exception: + # Can only happen if document doesn't exists - kept for backward compatibility + return [] + + def is_missing_table_or_column(self, e): + return self.is_missing_column(e) or self.is_table_missing(e) + + def multisql(self, sql_dict, values=(), **kwargs): + current_dialect = self.db_type or "mariadb" + query = sql_dict.get(current_dialect) + return self.sql(query, values, **kwargs) + + def delete(self, doctype: str, filters: dict | list | None = None, debug=False, **kwargs): + """Delete rows from a table in site which match the passed filters. This + does trigger DocType hooks. Simply runs a DELETE query in the database. + + Doctype name can be passed directly, it will be pre-pended with `tab`. + """ + filters = filters or kwargs.get("conditions") + query = xhiveframework.qb.get_query( + table=doctype, + filters=filters, + delete=True, + validate_filters=True, + ) + if "debug" not in kwargs: + kwargs["debug"] = debug + return query.run(**kwargs) + + def truncate(self, doctype: str): + """Truncate a table in the database. This runs a DDL command `TRUNCATE TABLE`. + This cannot be rolled back. + + Doctype name can be passed directly, it will be pre-pended with `tab`. + """ + return self.sql_ddl(f"truncate `{get_table_name(doctype)}`") + + def get_last_created(self, doctype): + last_record = self.get_all(doctype, ("creation"), limit=1, order_by="creation desc") + if last_record: + return get_datetime(last_record[0].creation) + else: + return None + + def log_touched_tables(self, query): + if is_query_type(query, ("insert", "delete", "update", "alter", "drop", "rename")): + # single_word_regex is designed to match following patterns + # `tabXxx`, tabXxx and "tabXxx" + + # multi_word_regex is designed to match following patterns + # `tabXxx Xxx` and "tabXxx Xxx" + + # ([`"]?) Captures " or ` at the beginning of the table name (if provided) + # \1 matches the first captured group (quote character) at the end of the table name + # multi word table name must have surrounding quotes. + + # (tab([A-Z]\w+)( [A-Z]\w+)*) Captures table names that start with "tab" + # and are continued with multiple words that start with a captital letter + # e.g. 'tabXxx' or 'tabXxx Xxx' or 'tabXxx Xxx Xxx' and so on + + tables = [] + for regex in (SINGLE_WORD_PATTERN, MULTI_WORD_PATTERN): + tables += [groups[1] for groups in regex.findall(query)] + + if xhiveframework.flags.touched_tables is None: + xhiveframework.flags.touched_tables = set() + xhiveframework.flags.touched_tables.update(tables) + + def bulk_insert( + self, + doctype: str, + fields: list[str], + values: Iterable[Sequence[Any]], + ignore_duplicates=False, + *, + chunk_size=10_000, + ): + """ + Insert multiple records at a time + + :param doctype: Doctype name + :param fields: list of fields + :params values: iterable of values + """ + table = xhiveframework.qb.DocType(doctype) + + query = xhiveframework.qb.into(table).columns(fields) + + if ignore_duplicates: + # Pypika does not have same api for ignoring duplicates + if xhiveframework.conf.db_type == "mariadb": + query = query.ignore() + elif xhiveframework.conf.db_type == "postgres": + query = query.on_conflict().do_nothing() + + value_iterator = iter(values) + while value_chunk := tuple(itertools.islice(value_iterator, chunk_size)): + query.insert(*value_chunk).run() + + def create_sequence(self, *args, **kwargs): + from xhiveframework.database.sequence import create_sequence + + return create_sequence(*args, **kwargs) + + def set_next_sequence_val(self, *args, **kwargs): + from xhiveframework.database.sequence import set_next_val + + set_next_val(*args, **kwargs) + + def get_next_sequence_val(self, *args, **kwargs): + from xhiveframework.database.sequence import get_next_val + + return get_next_val(*args, **kwargs) + + def get_row_size(self, doctype: str) -> int: + """Get estimated max row size of any table in bytes.""" + raise NotImplementedError + + def rename_column(self, doctype: str, old_column_name: str, new_column_name: str): + raise NotImplementedError + + @contextmanager + def unbuffered_cursor(self): + """Context manager to temporarily use unbuffered cursor. + + Using this with `as_iterator=True` provides O(1) memory usage while reading large result sets. + + NOTE: You MUST do entire result set processing in the context, otherwise underlying cursor + will be switched and you'll not get complete results. + + Usage: + with xhiveframework.db.unbuffered_cursor(): + for row in xhiveframework.db.sql("query with huge result", as_iterator=True): + continue # Do some processing. + """ + raise NotImplementedError + + +@contextmanager +def savepoint(catch: type | tuple[type, ...] = Exception): + """Wrapper for wrapping blocks of DB operations in a savepoint. + + as contextmanager: + + for doc in docs: + with savepoint(catch=DuplicateError): + doc.insert() + + as decorator (wraps FULL function call): + + @savepoint(catch=DuplicateError) + def process_doc(doc): + doc.insert() + """ + try: + savepoint = "".join(random.sample(string.ascii_lowercase, 10)) + xhiveframework.db.savepoint(savepoint) + yield # control back to calling function + except catch: + xhiveframework.db.rollback(save_point=savepoint) + else: + xhiveframework.db.release_savepoint(savepoint) + + +def get_query_execution_timeout() -> int: + """Get execution timeout based on current timeout in different contexts. + + HTTP requests: HTTP timeout or a default (300) + Background jobs: Job timeout + Console/Commands: No timeout = 0. + + Note: Timeout adds 1.5x as "safety factor" + """ + from rq import get_current_job + + if not xhiveframework.conf.get("enable_db_statement_timeout"): + return 0 + + # Zero means no timeout, which is the default value in db. + timeout = 0 + with suppress(Exception): + if getattr(xhiveframework.local, "request", None): + timeout = xhiveframework.conf.http_timeout or 300 + elif job := get_current_job(): + timeout = job.timeout + + return int(cint(timeout) * 1.5) diff --git a/xhiveframework/database/db_manager.py b/xhiveframework/database/db_manager.py new file mode 100644 index 0000000..47a7db4 --- /dev/null +++ b/xhiveframework/database/db_manager.py @@ -0,0 +1,88 @@ +import xhiveframework +from xhiveframework import _ + + +class DbManager: + def __init__(self, db): + """ + Pass root_conn here for access to all databases. + """ + if db: + self.db = db + + def get_current_host(self): + return self.db.sql("select user()")[0][0].split("@")[1] + + def create_user(self, user, password, host=None): + host = host or self.get_current_host() + password_predicate = f" IDENTIFIED BY '{password}'" if password else "" + self.db.sql(f"CREATE USER '{user}'@'{host}'{password_predicate}") + + def delete_user(self, target, host=None): + host = host or self.get_current_host() + self.db.sql(f"DROP USER IF EXISTS '{target}'@'{host}'") + + def create_database(self, target): + if target in self.get_database_list(): + self.drop_database(target) + self.db.sql(f"CREATE DATABASE `{target}`") + + def drop_database(self, target): + self.db.sql_ddl(f"DROP DATABASE IF EXISTS `{target}`") + + def grant_all_privileges(self, target, user, host=None): + host = host or self.get_current_host() + permissions = ( + ( + "SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, " + "CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, " + "CREATE ROUTINE, ALTER ROUTINE, EXECUTE, LOCK TABLES" + ) + if xhiveframework.conf.rds_db + else "ALL PRIVILEGES" + ) + self.db.sql(f"GRANT {permissions} ON `{target}`.* TO '{user}'@'{host}'") + + def flush_privileges(self): + self.db.sql("FLUSH PRIVILEGES") + + def get_database_list(self): + return self.db.sql("SHOW DATABASES", pluck=True) + + @staticmethod + def restore_database(verbose, target, source, user, password): + import shlex + from shutil import which + + from xhiveframework.database import get_command + from xhiveframework.utils import execute_in_shell + + command = ["set -o pipefail;"] + + if source.endswith(".gz"): + if gzip := which("gzip"): + command.extend([gzip, "-cd", source, "|"]) + source = [] + else: + raise Exception("`gzip` not installed") + + else: + source = ["<", source] + + bin, args, bin_name = get_command( + host=xhiveframework.conf.db_host, + port=xhiveframework.conf.db_port, + user=user, + password=password, + db_name=target, + ) + if not bin: + xhiveframework.throw( + _("{} not found in PATH! This is required to restore the database.").format(bin_name), + exc=xhiveframework.ExecutableNotFound, + ) + command.append(bin) + command.append(shlex.join(args)) + command.extend(source) + execute_in_shell(" ".join(command), check_exit_code=True, verbose=verbose) + xhiveframework.cache.delete_keys("") # Delete all keys associated with this site. diff --git a/xhiveframework/database/mariadb/__init__.py b/xhiveframework/database/mariadb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/database/mariadb/database.py b/xhiveframework/database/mariadb/database.py new file mode 100644 index 0000000..58c8b55 --- /dev/null +++ b/xhiveframework/database/mariadb/database.py @@ -0,0 +1,529 @@ +import re +from contextlib import contextmanager + +import pymysql +from pymysql.constants import ER, FIELD_TYPE +from pymysql.converters import conversions, escape_string + +import xhiveframework +from xhiveframework.database.database import Database +from xhiveframework.database.mariadb.schema import MariaDBTable +from xhiveframework.utils import UnicodeWithAttrs, cstr, get_datetime, get_table_name + +_PARAM_COMP = re.compile(r"%\([\w]*\)s") + + +class MariaDBExceptionUtil: + ProgrammingError = pymysql.ProgrammingError + TableMissingError = pymysql.ProgrammingError + OperationalError = pymysql.OperationalError + InternalError = pymysql.InternalError + SQLError = pymysql.ProgrammingError + DataError = pymysql.DataError + + # match ER_SEQUENCE_RUN_OUT - https://mariadb.com/kb/en/mariadb-error-codes/ + SequenceGeneratorLimitExceeded = pymysql.OperationalError + SequenceGeneratorLimitExceeded.errno = 4084 + + @staticmethod + def is_deadlocked(e: pymysql.Error) -> bool: + return e.args[0] == ER.LOCK_DEADLOCK + + @staticmethod + def is_timedout(e: pymysql.Error) -> bool: + return e.args[0] == ER.LOCK_WAIT_TIMEOUT + + @staticmethod + def is_read_only_mode_error(e: pymysql.Error) -> bool: + return e.args[0] == 1792 + + @staticmethod + def is_table_missing(e: pymysql.Error) -> bool: + return e.args[0] == ER.NO_SUCH_TABLE + + @staticmethod + def is_missing_table(e: pymysql.Error) -> bool: + return MariaDBDatabase.is_table_missing(e) + + @staticmethod + def is_missing_column(e: pymysql.Error) -> bool: + return e.args[0] == ER.BAD_FIELD_ERROR + + @staticmethod + def is_duplicate_fieldname(e: pymysql.Error) -> bool: + return e.args[0] == ER.DUP_FIELDNAME + + @staticmethod + def is_duplicate_entry(e: pymysql.Error) -> bool: + return e.args[0] == ER.DUP_ENTRY + + @staticmethod + def is_access_denied(e: pymysql.Error) -> bool: + return e.args[0] == ER.ACCESS_DENIED_ERROR + + @staticmethod + def cant_drop_field_or_key(e: pymysql.Error) -> bool: + return e.args[0] == ER.CANT_DROP_FIELD_OR_KEY + + @staticmethod + def is_syntax_error(e: pymysql.Error) -> bool: + return e.args[0] == ER.PARSE_ERROR + + @staticmethod + def is_statement_timeout(e: pymysql.Error) -> bool: + return e.args[0] == 1969 + + @staticmethod + def is_data_too_long(e: pymysql.Error) -> bool: + return e.args[0] == ER.DATA_TOO_LONG + + @staticmethod + def is_db_table_size_limit(e: pymysql.Error) -> bool: + return e.args[0] == ER.TOO_BIG_ROWSIZE + + @staticmethod + def is_primary_key_violation(e: pymysql.Error) -> bool: + return ( + MariaDBDatabase.is_duplicate_entry(e) + and "PRIMARY" in cstr(e.args[1]) + and isinstance(e, pymysql.IntegrityError) + ) + + @staticmethod + def is_unique_key_violation(e: pymysql.Error) -> bool: + return ( + MariaDBDatabase.is_duplicate_entry(e) + and "Duplicate" in cstr(e.args[1]) + and isinstance(e, pymysql.IntegrityError) + ) + + +class MariaDBConnectionUtil: + def get_connection(self): + conn = self._get_connection() + conn.auto_reconnect = True + return conn + + def _get_connection(self): + """Return MariaDB connection object.""" + return self.create_connection() + + def create_connection(self): + return pymysql.connect(**self.get_connection_settings()) + + def set_execution_timeout(self, seconds: int): + self.sql("set session max_statement_time = %s", int(seconds)) + + def get_connection_settings(self) -> dict: + conn_settings = { + "host": self.host, + "user": self.user, + "password": self.password, + "conv": self.CONVERSION_MAP, + "charset": "utf8mb4", + "use_unicode": True, + } + + if self.user not in (xhiveframework.flags.root_login, "root"): + conn_settings["database"] = self.user + + if self.port: + conn_settings["port"] = int(self.port) + + if xhiveframework.conf.local_infile: + conn_settings["local_infile"] = xhiveframework.conf.local_infile + + if xhiveframework.conf.db_ssl_ca and xhiveframework.conf.db_ssl_cert and xhiveframework.conf.db_ssl_key: + conn_settings["ssl"] = { + "ca": xhiveframework.conf.db_ssl_ca, + "cert": xhiveframework.conf.db_ssl_cert, + "key": xhiveframework.conf.db_ssl_key, + } + return conn_settings + + +class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database): + REGEX_CHARACTER = "regexp" + CONVERSION_MAP = conversions | { + FIELD_TYPE.NEWDECIMAL: float, + FIELD_TYPE.DATETIME: get_datetime, + UnicodeWithAttrs: escape_string, + } + default_port = "3306" + MAX_ROW_SIZE_LIMIT = 65_535 # bytes + + def setup_type_map(self): + self.db_type = "mariadb" + self.type_map = { + "Currency": ("decimal", "21,9"), + "Int": ("int", "11"), + "Long Int": ("bigint", "20"), + "Float": ("decimal", "21,9"), + "Percent": ("decimal", "21,9"), + "Check": ("int", "1"), + "Small Text": ("text", ""), + "Long Text": ("longtext", ""), + "Code": ("longtext", ""), + "Text Editor": ("longtext", ""), + "Markdown Editor": ("longtext", ""), + "HTML Editor": ("longtext", ""), + "Date": ("date", ""), + "Datetime": ("datetime", "6"), + "Time": ("time", "6"), + "Text": ("text", ""), + "Data": ("varchar", self.VARCHAR_LEN), + "Link": ("varchar", self.VARCHAR_LEN), + "Dynamic Link": ("varchar", self.VARCHAR_LEN), + "Password": ("text", ""), + "Select": ("varchar", self.VARCHAR_LEN), + "Rating": ("decimal", "3,2"), + "Read Only": ("varchar", self.VARCHAR_LEN), + "Attach": ("text", ""), + "Attach Image": ("text", ""), + "Signature": ("longtext", ""), + "Color": ("varchar", self.VARCHAR_LEN), + "Barcode": ("longtext", ""), + "Geolocation": ("longtext", ""), + "Duration": ("decimal", "21,9"), + "Icon": ("varchar", self.VARCHAR_LEN), + "Phone": ("varchar", self.VARCHAR_LEN), + "Autocomplete": ("varchar", self.VARCHAR_LEN), + "JSON": ("json", ""), + } + + def get_database_size(self): + """'Returns database size in MB""" + db_size = self.sql( + """ + SELECT `table_schema` as `database_name`, + SUM(`data_length` + `index_length`) / 1024 / 1024 AS `database_size` + FROM information_schema.tables WHERE `table_schema` = %s GROUP BY `table_schema` + """, + self.db_name, + as_dict=True, + ) + + return db_size[0].get("database_size") + + def log_query(self, query, values, debug, explain): + self.last_query = self._cursor._executed + self._log_query(self.last_query, debug, explain, query) + return self.last_query + + def _clean_up(self): + # PERF: Erase internal references of pymysql to trigger GC as soon as + # results are consumed. + self._cursor._result = None + self._cursor._rows = None + self._cursor.connection._result = None + + @staticmethod + def escape(s, percent=True): + """Excape quotes and percent in given string.""" + # Update: We've scrapped PyMySQL in favour of MariaDB's official Python client + # Also, given we're promoting use of the PyPika builder via xhiveframework.qb, the use + # of this method should be limited. + + # pymysql expects unicode argument to escape_string with Python 3 + s = xhiveframework.as_unicode(escape_string(xhiveframework.as_unicode(s)), "utf-8").replace("`", "\\`") + + # NOTE separating % escape, because % escape should only be done when using LIKE operator + # or when you use python format string to generate query that already has a %s + # for example: sql("select name from `tabUser` where name=%s and {0}".format(conditions), something) + # defaulting it to True, as this is the most frequent use case + # ideally we shouldn't have to use ESCAPE and strive to pass values via the values argument of sql + if percent: + s = s.replace("%", "%%") + + return "'" + s + "'" + + # column type + @staticmethod + def is_type_number(code): + return code == pymysql.NUMBER + + @staticmethod + def is_type_datetime(code): + return code == pymysql.DATETIME + + def rename_table(self, old_name: str, new_name: str) -> list | tuple: + old_name = get_table_name(old_name) + new_name = get_table_name(new_name) + return self.sql(f"RENAME TABLE `{old_name}` TO `{new_name}`") + + def describe(self, doctype: str) -> list | tuple: + table_name = get_table_name(doctype) + return self.sql(f"DESC `{table_name}`") + + def change_column_type( + self, doctype: str, column: str, type: str, nullable: bool = False + ) -> list | tuple: + table_name = get_table_name(doctype) + null_constraint = "NOT NULL" if not nullable else "" + return self.sql_ddl(f"ALTER TABLE `{table_name}` MODIFY `{column}` {type} {null_constraint}") + + def rename_column(self, doctype: str, old_column_name, new_column_name): + current_data_type = self.get_column_type(doctype, old_column_name) + + table_name = get_table_name(doctype) + + xhiveframework.db.sql_ddl( + f"""ALTER TABLE `{table_name}` + CHANGE COLUMN `{old_column_name}` + `{new_column_name}` + {current_data_type}""" + # ^ Mariadb requires passing current data type again even if there's no change + # This requirement is gone from v10.5 + ) + + def create_auth_table(self): + self.sql_ddl( + """create table if not exists `__Auth` ( + `doctype` VARCHAR(140) NOT NULL, + `name` VARCHAR(255) NOT NULL, + `fieldname` VARCHAR(140) NOT NULL, + `password` TEXT NOT NULL, + `encrypted` INT(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`doctype`, `name`, `fieldname`) + ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci""" + ) + + def create_global_search_table(self): + if "__global_search" not in self.get_tables(): + self.sql( + f"""create table __global_search( + doctype varchar(100), + name varchar({self.VARCHAR_LEN}), + title varchar({self.VARCHAR_LEN}), + content text, + fulltext(content), + route varchar({self.VARCHAR_LEN}), + published int(1) not null default 0, + unique `doctype_name` (doctype, name)) + COLLATE=utf8mb4_unicode_ci + ENGINE=MyISAM + CHARACTER SET=utf8mb4""" + ) + + def create_user_settings_table(self): + self.sql_ddl( + """create table if not exists __UserSettings ( + `user` VARCHAR(180) NOT NULL, + `doctype` VARCHAR(180) NOT NULL, + `data` TEXT, + UNIQUE(user, doctype) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8""" + ) + + @staticmethod + def get_on_duplicate_update(key=None): + return "ON DUPLICATE key UPDATE " + + def get_table_columns_description(self, table_name): + """Returns list of column and its description""" + return self.sql( + f"""select + column_name as 'name', + column_type as 'type', + column_default as 'default', + COALESCE( + (select 1 + from information_schema.statistics + where table_name="{table_name}" + and column_name=columns.column_name + and NON_UNIQUE=1 + and Seq_in_index = 1 + limit 1 + ), 0) as 'index', + column_key = 'UNI' as 'unique' + from information_schema.columns as columns + where table_name = '{table_name}' """, + as_dict=1, + ) + + def get_column_type(self, doctype, column): + """Returns column type from database.""" + information_schema = xhiveframework.qb.Schema("information_schema") + table = get_table_name(doctype) + + return ( + xhiveframework.qb.from_(information_schema.columns) + .select(information_schema.columns.column_type) + .where( + (information_schema.columns.table_name == table) + & (information_schema.columns.column_name == column) + ) + .run(pluck=True)[0] + ) + + def has_index(self, table_name, index_name): + return self.sql( + f"""SHOW INDEX FROM `{table_name}` + WHERE Key_name='{index_name}'""" + ) + + def get_column_index(self, table_name: str, fieldname: str, unique: bool = False) -> xhiveframework._dict | None: + """Check if column exists for a specific fields in specified order. + + This differs from db.has_index because it doesn't rely on index name but columns inside an + index. + """ + + indexes = self.sql( + f"""SHOW INDEX FROM `{table_name}` + WHERE Column_name = "{fieldname}" + AND Seq_in_index = 1 + AND Non_unique={int(not unique)} + AND Index_type != 'FULLTEXT' + """, + as_dict=True, + ) + + # Same index can be part of clustered index which contains more fields + # We don't want those. + for index in indexes: + clustered_index = self.sql( + f"""SHOW INDEX FROM `{table_name}` + WHERE Key_name = "{index.Key_name}" + AND Seq_in_index = 2 + """, + as_dict=True, + ) + if not clustered_index: + return index + + def add_index(self, doctype: str, fields: list, index_name: str | None = None): + """Creates an index with given fields if not already created. + Index name will be `fieldname1_fieldname2_index`""" + index_name = index_name or self.get_index_name(fields) + table_name = get_table_name(doctype) + if not self.has_index(table_name, index_name): + self.commit() + self.sql( + """ALTER TABLE `{}` + ADD INDEX `{}`({})""".format(table_name, index_name, ", ".join(fields)) + ) + + def add_unique(self, doctype, fields, constraint_name=None): + if isinstance(fields, str): + fields = [fields] + if not constraint_name: + constraint_name = "unique_" + "_".join(fields) + + if not self.sql( + """select CONSTRAINT_NAME from information_schema.TABLE_CONSTRAINTS + where table_name=%s and constraint_type='UNIQUE' and CONSTRAINT_NAME=%s""", + ("tab" + doctype, constraint_name), + ): + self.commit() + self.sql( + """alter table `tab{}` + add unique `{}`({})""".format(doctype, constraint_name, ", ".join(fields)) + ) + + def updatedb(self, doctype, meta=None): + """ + Syncs a `DocType` to the table + * creates if required + * updates columns + * updates indices + """ + res = self.sql("select issingle from `tabDocType` where name=%s", (doctype,)) + if not res: + raise Exception(f"Wrong doctype {doctype} in updatedb") + + if not res[0][0]: + db_table = MariaDBTable(doctype, meta) + db_table.validate() + + db_table.sync() + self.commit() + + def get_database_list(self): + return self.sql("SHOW DATABASES", pluck=True) + + def get_tables(self, cached=True): + """Returns list of tables""" + to_query = not cached + + if cached: + tables = xhiveframework.cache.get_value("db_tables") + to_query = not tables + + if to_query: + information_schema = xhiveframework.qb.Schema("information_schema") + + tables = ( + xhiveframework.qb.from_(information_schema.tables) + .select(information_schema.tables.table_name) + .where(information_schema.tables.table_schema != "information_schema") + .run(pluck=True) + ) + xhiveframework.cache.set_value("db_tables", tables) + + return tables + + def get_row_size(self, doctype: str) -> int: + """Get estimated max row size of any table in bytes.""" + + # Query reused from this answer: https://dba.stackexchange.com/a/313889/274503 + # Modification: get values for particular table instead of full summary. + # Reference: https://mariadb.com/kb/en/data-type-storage-requirements/ + + est_row_size = xhiveframework.db.sql( + """ + SELECT SUM(col_sizes.col_size) AS EST_MAX_ROW_SIZE + FROM ( + SELECT + cols.COLUMN_NAME, + CASE cols.DATA_TYPE + WHEN 'tinyint' THEN 1 + WHEN 'smallint' THEN 2 + WHEN 'mediumint' THEN 3 + WHEN 'int' THEN 4 + WHEN 'bigint' THEN 8 + WHEN 'float' THEN IF(cols.NUMERIC_PRECISION > 24, 8, 4) + WHEN 'double' THEN 8 + WHEN 'decimal' THEN ((cols.NUMERIC_PRECISION - cols.NUMERIC_SCALE) DIV 9)*4 + (cols.NUMERIC_SCALE DIV 9)*4 + CEIL(MOD(cols.NUMERIC_PRECISION - cols.NUMERIC_SCALE,9)/2) + CEIL(MOD(cols.NUMERIC_SCALE,9)/2) + WHEN 'bit' THEN (cols.NUMERIC_PRECISION + 7) DIV 8 + WHEN 'year' THEN 1 + WHEN 'date' THEN 3 + WHEN 'time' THEN 3 + CEIL(cols.DATETIME_PRECISION /2) + WHEN 'datetime' THEN 5 + CEIL(cols.DATETIME_PRECISION /2) + WHEN 'timestamp' THEN 4 + CEIL(cols.DATETIME_PRECISION /2) + WHEN 'char' THEN cols.CHARACTER_OCTET_LENGTH + WHEN 'binary' THEN cols.CHARACTER_OCTET_LENGTH + WHEN 'varchar' THEN IF(cols.CHARACTER_OCTET_LENGTH > 255, 2, 1) + cols.CHARACTER_OCTET_LENGTH + WHEN 'varbinary' THEN IF(cols.CHARACTER_OCTET_LENGTH > 255, 2, 1) + cols.CHARACTER_OCTET_LENGTH + WHEN 'tinyblob' THEN 9 + WHEN 'tinytext' THEN 9 + WHEN 'blob' THEN 10 + WHEN 'text' THEN 10 + WHEN 'mediumblob' THEN 11 + WHEN 'mediumtext' THEN 11 + WHEN 'longblob' THEN 12 + WHEN 'longtext' THEN 12 + WHEN 'enum' THEN 2 + WHEN 'set' THEN 8 + ELSE 0 + END AS col_size + FROM INFORMATION_SCHEMA.COLUMNS cols + WHERE cols.TABLE_NAME = %s + ) AS col_sizes;""", + (get_table_name(doctype),), + ) + + if est_row_size: + return int(est_row_size[0][0]) + + @contextmanager + def unbuffered_cursor(self): + from pymysql.cursors import SSCursor + + try: + original_cursor = self._cursor + new_cursor = self._cursor = self._conn.cursor(SSCursor) + yield + finally: + self._cursor = original_cursor + new_cursor.close() diff --git a/xhiveframework/database/mariadb/framework_mariadb.sql b/xhiveframework/database/mariadb/framework_mariadb.sql new file mode 100644 index 0000000..91ef696 --- /dev/null +++ b/xhiveframework/database/mariadb/framework_mariadb.sql @@ -0,0 +1,338 @@ +-- Core Elements to install WNFramework +-- To be called from install.py + + +-- +-- Table structure for table `tabDocField` +-- + +DROP TABLE IF EXISTS `tabDocField`; +CREATE TABLE `tabDocField` ( + `name` varchar(255) NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(255) DEFAULT NULL, + `owner` varchar(255) DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(255) DEFAULT NULL, + `parentfield` varchar(255) DEFAULT NULL, + `parenttype` varchar(255) DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `fieldname` varchar(255) DEFAULT NULL, + `label` varchar(255) DEFAULT NULL, + `oldfieldname` varchar(255) DEFAULT NULL, + `fieldtype` varchar(255) DEFAULT NULL, + `oldfieldtype` varchar(255) DEFAULT NULL, + `options` text, + `search_index` int(1) NOT NULL DEFAULT 0, + `show_dashboard` int(1) NOT NULL DEFAULT 0, + `hidden` int(1) NOT NULL DEFAULT 0, + `set_only_once` int(1) NOT NULL DEFAULT 0, + `allow_in_quick_entry` int(1) NOT NULL DEFAULT 0, + `print_hide` int(1) NOT NULL DEFAULT 0, + `report_hide` int(1) NOT NULL DEFAULT 0, + `reqd` int(1) NOT NULL DEFAULT 0, + `bold` int(1) NOT NULL DEFAULT 0, + `in_global_search` int(1) NOT NULL DEFAULT 0, + `collapsible` int(1) NOT NULL DEFAULT 0, + `unique` int(1) NOT NULL DEFAULT 0, + `no_copy` int(1) NOT NULL DEFAULT 0, + `allow_on_submit` int(1) NOT NULL DEFAULT 0, + `show_preview_popup` int(1) NOT NULL DEFAULT 0, + `trigger` varchar(255) DEFAULT NULL, + `collapsible_depends_on` text, + `mandatory_depends_on` text, + `read_only_depends_on` text, + `depends_on` text, + `permlevel` int(11) NOT NULL DEFAULT 0, + `ignore_user_permissions` int(1) NOT NULL DEFAULT 0, + `width` varchar(255) DEFAULT NULL, + `print_width` varchar(255) DEFAULT NULL, + `columns` int(11) NOT NULL DEFAULT 0, + `default` text, + `description` text, + `in_list_view` int(1) NOT NULL DEFAULT 0, + `fetch_if_empty` int(1) NOT NULL DEFAULT 0, + `in_filter` int(1) NOT NULL DEFAULT 0, + `remember_last_selected_value` int(1) NOT NULL DEFAULT 0, + `ignore_xss_filter` int(1) NOT NULL DEFAULT 0, + `print_hide_if_no_value` int(1) NOT NULL DEFAULT 0, + `allow_bulk_edit` int(1) NOT NULL DEFAULT 0, + `in_standard_filter` int(1) NOT NULL DEFAULT 0, + `in_preview` int(1) NOT NULL DEFAULT 0, + `read_only` int(1) NOT NULL DEFAULT 0, + `precision` varchar(255) DEFAULT NULL, + `max_height` varchar(10) DEFAULT NULL, + `length` int(11) NOT NULL DEFAULT 0, + `translatable` int(1) NOT NULL DEFAULT 0, + `hide_border` int(1) NOT NULL DEFAULT 0, + `hide_days` int(1) NOT NULL DEFAULT 0, + `hide_seconds` int(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`name`), + KEY `parent` (`parent`), + KEY `label` (`label`), + KEY `fieldtype` (`fieldtype`), + KEY `fieldname` (`fieldname`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + + +-- +-- Table structure for table `tabDocPerm` +-- + +DROP TABLE IF EXISTS `tabDocPerm`; +CREATE TABLE `tabDocPerm` ( + `name` varchar(255) NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(255) DEFAULT NULL, + `owner` varchar(255) DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(255) DEFAULT NULL, + `parentfield` varchar(255) DEFAULT NULL, + `parenttype` varchar(255) DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `permlevel` int(11) DEFAULT '0', + `role` varchar(255) DEFAULT NULL, + `match` varchar(255) DEFAULT NULL, + `read` int(1) NOT NULL DEFAULT 1, + `write` int(1) NOT NULL DEFAULT 1, + `create` int(1) NOT NULL DEFAULT 1, + `submit` int(1) NOT NULL DEFAULT 0, + `cancel` int(1) NOT NULL DEFAULT 0, + `delete` int(1) NOT NULL DEFAULT 1, + `amend` int(1) NOT NULL DEFAULT 0, + `report` int(1) NOT NULL DEFAULT 1, + `export` int(1) NOT NULL DEFAULT 1, + `import` int(1) NOT NULL DEFAULT 0, + `share` int(1) NOT NULL DEFAULT 1, + `print` int(1) NOT NULL DEFAULT 1, + `email` int(1) NOT NULL DEFAULT 1, + PRIMARY KEY (`name`), + KEY `parent` (`parent`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `tabDocType Action` +-- + +DROP TABLE IF EXISTS `tabDocType Action`; +CREATE TABLE `tabDocType Action` ( + `name` varchar(140) COLLATE utf8mb4_unicode_ci NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `owner` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `parentfield` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `parenttype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `label` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `group` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `action_type` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `action` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, + PRIMARY KEY (`name`), + KEY `parent` (`parent`), + KEY `modified` (`modified`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC; + +-- +-- Table structure for table `tabDocType Link` +-- + +DROP TABLE IF EXISTS `tabDocType Link`; +CREATE TABLE `tabDocType Link` ( + `name` varchar(140) COLLATE utf8mb4_unicode_ci NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `owner` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `parentfield` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `parenttype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `group` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `link_doctype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `link_fieldname` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + PRIMARY KEY (`name`), + KEY `parent` (`parent`), + KEY `modified` (`modified`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC; + +-- +-- Table structure for table `tabDocType` +-- + +DROP TABLE IF EXISTS `tabDocType`; +CREATE TABLE `tabDocType` ( + `name` varchar(255) NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(255) DEFAULT NULL, + `owner` varchar(255) DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `idx` int(8) NOT NULL DEFAULT 0, + `search_fields` varchar(255) DEFAULT NULL, + `issingle` int(1) NOT NULL DEFAULT 0, + `is_virtual` int(1) NOT NULL DEFAULT 0, + `is_tree` int(1) NOT NULL DEFAULT 0, + `istable` int(1) NOT NULL DEFAULT 0, + `editable_grid` int(1) NOT NULL DEFAULT 1, + `track_changes` int(1) NOT NULL DEFAULT 0, + `module` varchar(255) DEFAULT NULL, + `restrict_to_domain` varchar(255) DEFAULT NULL, + `app` varchar(255) DEFAULT NULL, + `autoname` varchar(255) DEFAULT NULL, + `naming_rule` varchar(40) DEFAULT NULL, + `title_field` varchar(255) DEFAULT NULL, + `image_field` varchar(255) DEFAULT NULL, + `timeline_field` varchar(255) DEFAULT NULL, + `sort_field` varchar(255) DEFAULT NULL, + `sort_order` varchar(255) DEFAULT NULL, + `description` text, + `colour` varchar(255) DEFAULT NULL, + `read_only` int(1) NOT NULL DEFAULT 0, + `in_create` int(1) NOT NULL DEFAULT 0, + `menu_index` int(11) DEFAULT NULL, + `parent_node` varchar(255) DEFAULT NULL, + `smallicon` varchar(255) DEFAULT NULL, + `allow_copy` int(1) NOT NULL DEFAULT 0, + `allow_rename` int(1) NOT NULL DEFAULT 0, + `allow_import` int(1) NOT NULL DEFAULT 0, + `hide_toolbar` int(1) NOT NULL DEFAULT 0, + `track_seen` int(1) NOT NULL DEFAULT 0, + `max_attachments` int(11) NOT NULL DEFAULT 0, + `print_outline` varchar(255) DEFAULT NULL, + `document_type` varchar(255) DEFAULT NULL, + `icon` varchar(255) DEFAULT NULL, + `color` varchar(255) DEFAULT NULL, + `tag_fields` varchar(255) DEFAULT NULL, + `subject` varchar(255) DEFAULT NULL, + `_last_update` varchar(32) DEFAULT NULL, + `engine` varchar(20) DEFAULT 'InnoDB', + `default_print_format` varchar(255) DEFAULT NULL, + `is_submittable` int(1) NOT NULL DEFAULT 0, + `show_name_in_global_search` int(1) NOT NULL DEFAULT 0, + `_user_tags` varchar(255) DEFAULT NULL, + `custom` int(1) NOT NULL DEFAULT 0, + `beta` int(1) NOT NULL DEFAULT 0, + `has_web_view` int(1) NOT NULL DEFAULT 0, + `allow_guest_to_view` int(1) NOT NULL DEFAULT 0, + `route` varchar(255) DEFAULT NULL, + `is_published_field` varchar(255) DEFAULT NULL, + `website_search_field` varchar(255) DEFAULT NULL, + `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, + `translated_doctype` int(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`name`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `tabSeries` +-- + +DROP TABLE IF EXISTS `tabSeries`; +CREATE TABLE `tabSeries` ( + `name` varchar(100), + `current` int(10) NOT NULL DEFAULT 0, + PRIMARY KEY(`name`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + + +-- +-- Table structure for table `tabSessions` +-- + +DROP TABLE IF EXISTS `tabSessions`; +CREATE TABLE `tabSessions` ( + `user` varchar(255) DEFAULT NULL, + `sid` varchar(255) DEFAULT NULL, + `sessiondata` longtext, + `ipaddress` varchar(16) DEFAULT NULL, + `lastupdate` datetime(6) DEFAULT NULL, + `status` varchar(20) DEFAULT NULL, + KEY `sid` (`sid`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + + +-- +-- Table structure for table `tabSingles` +-- + +DROP TABLE IF EXISTS `tabSingles`; +CREATE TABLE `tabSingles` ( + `doctype` varchar(255) DEFAULT NULL, + `field` varchar(255) DEFAULT NULL, + `value` longtext, + KEY `singles_doctype_field_index` (`doctype`, `field`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `__Auth` +-- + +DROP TABLE IF EXISTS `__Auth`; +CREATE TABLE `__Auth` ( + `doctype` VARCHAR(140) NOT NULL, + `name` VARCHAR(255) NOT NULL, + `fieldname` VARCHAR(140) NOT NULL, + `password` TEXT NOT NULL, + `encrypted` INT(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`doctype`, `name`, `fieldname`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `tabFile` +-- + +DROP TABLE IF EXISTS `tabFile`; +CREATE TABLE `tabFile` ( + `name` varchar(255) NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(255) DEFAULT NULL, + `owner` varchar(255) DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(255) DEFAULT NULL, + `parentfield` varchar(255) DEFAULT NULL, + `parenttype` varchar(255) DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `file_name` varchar(255) DEFAULT NULL, + `file_url` varchar(255) DEFAULT NULL, + `module` varchar(255) DEFAULT NULL, + `attached_to_name` varchar(255) DEFAULT NULL, + `file_size` int(11) NOT NULL DEFAULT 0, + `attached_to_doctype` varchar(255) DEFAULT NULL, + PRIMARY KEY (`name`), + KEY `parent` (`parent`), + KEY `attached_to_name` (`attached_to_name`), + KEY `attached_to_doctype` (`attached_to_doctype`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Table structure for table `tabDefaultValue` +-- + +DROP TABLE IF EXISTS `tabDefaultValue`; +CREATE TABLE `tabDefaultValue` ( + `name` varchar(255) NOT NULL, + `creation` datetime(6) DEFAULT NULL, + `modified` datetime(6) DEFAULT NULL, + `modified_by` varchar(255) DEFAULT NULL, + `owner` varchar(255) DEFAULT NULL, + `docstatus` int(1) NOT NULL DEFAULT 0, + `parent` varchar(255) DEFAULT NULL, + `parentfield` varchar(255) DEFAULT NULL, + `parenttype` varchar(255) DEFAULT NULL, + `idx` int(8) NOT NULL DEFAULT 0, + `defvalue` text, + `defkey` varchar(255) DEFAULT NULL, + PRIMARY KEY (`name`), + KEY `parent` (`parent`), + KEY `defaultvalue_parent_defkey_index` (`parent`,`defkey`) +) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/xhiveframework/database/mariadb/schema.py b/xhiveframework/database/mariadb/schema.py new file mode 100644 index 0000000..c9af2bc --- /dev/null +++ b/xhiveframework/database/mariadb/schema.py @@ -0,0 +1,119 @@ +from pymysql.constants.ER import DUP_ENTRY + +import xhiveframework +from xhiveframework import _ +from xhiveframework.database.schema import DBTable + + +class MariaDBTable(DBTable): + def create(self): + additional_definitions = [] + engine = self.meta.get("engine") or "InnoDB" + varchar_len = xhiveframework.db.VARCHAR_LEN + name_column = f"name varchar({varchar_len}) primary key" + + # columns + column_defs = self.get_column_definitions() + if column_defs: + additional_definitions += column_defs + + # index + index_defs = self.get_index_definitions() + if index_defs: + additional_definitions += index_defs + + # child table columns + if self.meta.get("istable") or 0: + additional_definitions += [ + f"parent varchar({varchar_len})", + f"parentfield varchar({varchar_len})", + f"parenttype varchar({varchar_len})", + "index parent(parent)", + ] + else: + # parent types + additional_definitions.append("index modified(modified)") + + # creating sequence(s) + if not self.meta.issingle and self.meta.autoname == "autoincrement": + xhiveframework.db.create_sequence(self.doctype, check_not_exists=True) + + # NOTE: not used nextval func as default as the ability to restore + # database with sequences has bugs in mariadb and gives a scary error. + # issue link: https://jira.mariadb.org/browse/MDEV-20070 + name_column = "name bigint primary key" + + additional_definitions = ",\n".join(additional_definitions) + + # create table + query = f"""create table `{self.table_name}` ( + {name_column}, + creation datetime(6), + modified datetime(6), + modified_by varchar({varchar_len}), + owner varchar({varchar_len}), + docstatus int(1) not null default '0', + idx int(8) not null default '0', + {additional_definitions}) + ENGINE={engine} + ROW_FORMAT=DYNAMIC + CHARACTER SET=utf8mb4 + COLLATE=utf8mb4_unicode_ci""" + + xhiveframework.db.sql_ddl(query) + + def alter(self): + for col in self.columns.values(): + col.build_for_alter_table(self.current_columns.get(col.fieldname.lower())) + + add_column_query = [f"ADD COLUMN `{col.fieldname}` {col.get_definition()}" for col in self.add_column] + columns_to_modify = set(self.change_type + self.set_default) + modify_column_query = [ + f"MODIFY `{col.fieldname}` {col.get_definition(for_modification=True)}" + for col in columns_to_modify + ] + modify_column_query.extend( + [f"ADD UNIQUE INDEX IF NOT EXISTS {col.fieldname} (`{col.fieldname}`)" for col in self.add_unique] + ) + add_index_query = [ + f"ADD INDEX `{col.fieldname}_index`(`{col.fieldname}`)" + for col in self.add_index + if not xhiveframework.db.get_column_index(self.table_name, col.fieldname, unique=False) + ] + drop_index_query = [] + + for col in {*self.drop_index, *self.drop_unique}: + if col.fieldname == "name": + continue + + current_column = self.current_columns.get(col.fieldname.lower()) + unique_constraint_changed = current_column.unique != col.unique + if unique_constraint_changed and not col.unique: + if unique_index := xhiveframework.db.get_column_index(self.table_name, col.fieldname, unique=True): + drop_index_query.append(f"DROP INDEX `{unique_index.Key_name}`") + + index_constraint_changed = current_column.index != col.set_index + if index_constraint_changed and not col.set_index: + if index_record := xhiveframework.db.get_column_index(self.table_name, col.fieldname, unique=False): + drop_index_query.append(f"DROP INDEX `{index_record.Key_name}`") + + try: + for query_parts in [add_column_query, modify_column_query, add_index_query, drop_index_query]: + if query_parts: + query_body = ", ".join(query_parts) + query = f"ALTER TABLE `{self.table_name}` {query_body}" + xhiveframework.db.sql_ddl(query) + + except Exception as e: + if query := locals().get("query"): # this weirdness is to avoid potentially unbounded vars + print(f"Failed to alter schema using query: {query}") + + if e.args[0] == DUP_ENTRY: + fieldname = str(e).split("'")[-2] + xhiveframework.throw( + _( + "{0} field cannot be set as unique in {1}, as there are non-unique existing values" + ).format(fieldname, self.table_name) + ) + + raise diff --git a/xhiveframework/database/mariadb/setup_db.py b/xhiveframework/database/mariadb/setup_db.py new file mode 100644 index 0000000..0d0a6b0 --- /dev/null +++ b/xhiveframework/database/mariadb/setup_db.py @@ -0,0 +1,173 @@ +import os + +import click + +import xhiveframework +from xhiveframework.database.db_manager import DbManager + +REQUIRED_MARIADB_CONFIG = { + "character_set_server": "utf8mb4", + "collation_server": "utf8mb4_unicode_ci", +} + + +def get_mariadb_variables(): + return xhiveframework._dict(xhiveframework.db.sql("show variables")) + + +def get_mariadb_version(version_string: str = ""): + # MariaDB classifies their versions as Major (1st and 2nd number), and Minor (3rd number) + # Example: Version 10.3.13 is Major Version = 10.3, Minor Version = 13 + version_string = version_string or get_mariadb_variables().get("version") + version = version_string.split("-", 1)[0] + return version.rsplit(".", 1) + + +def setup_database(force, source_sql, verbose, no_mariadb_socket=False): + xhiveframework.local.session = xhiveframework._dict({"user": "Administrator"}) + + db_name = xhiveframework.local.conf.db_name + root_conn = get_root_connection(xhiveframework.flags.root_login, xhiveframework.flags.root_password) + dbman = DbManager(root_conn) + dbman_kwargs = {} + if no_mariadb_socket: + dbman_kwargs["host"] = "%" + + if force or (db_name not in dbman.get_database_list()): + dbman.delete_user(db_name, **dbman_kwargs) + dbman.drop_database(db_name) + else: + raise Exception(f"Database {db_name} already exists") + + dbman.create_user(db_name, xhiveframework.conf.db_password, **dbman_kwargs) + if verbose: + print("Created user %s" % db_name) + + dbman.create_database(db_name) + if verbose: + print("Created database %s" % db_name) + + dbman.grant_all_privileges(db_name, db_name, **dbman_kwargs) + dbman.flush_privileges() + if verbose: + print(f"Granted privileges to user {db_name} and database {db_name}") + + # close root connection + root_conn.close() + + bootstrap_database(db_name, verbose, source_sql) + + +def drop_user_and_database(db_name, root_login, root_password): + xhiveframework.local.db = get_root_connection(root_login, root_password) + dbman = DbManager(xhiveframework.local.db) + dbman.drop_database(db_name) + dbman.delete_user(db_name, host="%") + dbman.delete_user(db_name) + + +def bootstrap_database(db_name, verbose, source_sql=None): + import sys + + xhiveframework.connect(db_name=db_name) + if not check_database_settings(): + print("Database settings do not match expected values; stopping database setup.") + sys.exit(1) + + import_db_from_sql(source_sql, verbose) + + xhiveframework.connect(db_name=db_name) + if "tabDefaultValue" not in xhiveframework.db.get_tables(cached=False): + from click import secho + + secho( + "Table 'tabDefaultValue' missing in the restored site. " + "Database not installed correctly, this can due to lack of " + "permission, or that the database name exists. Check your mysql" + " root password, validity of the backup file or use --force to" + " reinstall", + fg="red", + ) + sys.exit(1) + + +def import_db_from_sql(source_sql=None, verbose=False): + if verbose: + print("Starting database import...") + db_name = xhiveframework.conf.db_name + if not source_sql: + source_sql = os.path.join(os.path.dirname(__file__), "framework_mariadb.sql") + DbManager(xhiveframework.local.db).restore_database( + verbose, db_name, source_sql, db_name, xhiveframework.conf.db_password + ) + if verbose: + print("Imported from database %s" % source_sql) + + +def check_database_settings(): + check_compatible_versions() + + # Check each expected value vs. actuals: + mariadb_variables = get_mariadb_variables() + result = True + for key, expected_value in REQUIRED_MARIADB_CONFIG.items(): + if mariadb_variables.get(key) != expected_value: + print(f"For key {key}. Expected value {expected_value}, found value {mariadb_variables.get(key)}") + result = False + + if not result: + print( + ( + "{sep2}Creation of your site - {site} failed because MariaDB is not properly {sep}" + "configured.{sep2}" + "Please verify the above settings in MariaDB's my.cnf. Restart MariaDB.{sep}" + "And then run `bench new-site {site}` again.{sep2}" + ).format(site=xhiveframework.local.site, sep2="\n\n", sep="\n") + ) + + return result + + +def check_compatible_versions(): + try: + version = get_mariadb_version() + version_tuple = tuple(int(v) for v in version[0].split(".")) + + if version_tuple < (10, 6): + click.secho( + f"Warning: MariaDB version {version} is less than 10.6 which is not supported by XhiveFramework", + fg="yellow", + ) + elif version_tuple >= (10, 9): + click.secho( + f"Warning: MariaDB version {version} is more than 10.8 which is not yet tested with Xhive Framework.", + fg="yellow", + ) + except Exception: + click.secho( + "MariaDB version compatibility checks failed, make sure you're running a supported version.", + fg="yellow", + ) + + +def get_root_connection(root_login, root_password): + import getpass + + if not xhiveframework.local.flags.root_connection: + if not root_login: + root_login = "root" + + if not root_password: + root_password = xhiveframework.conf.get("root_password") or None + + if not root_password: + root_password = getpass.getpass("MySQL root password: ") + + xhiveframework.local.flags.root_connection = xhiveframework.database.get_db( + host=xhiveframework.conf.db_host, + port=xhiveframework.conf.db_port, + user=root_login, + password=root_password, + ) + + return xhiveframework.local.flags.root_connection diff --git a/xhiveframework/database/operator_map.py b/xhiveframework/database/operator_map.py new file mode 100644 index 0000000..3eb2994 --- /dev/null +++ b/xhiveframework/database/operator_map.py @@ -0,0 +1,138 @@ +# Copyright (c) 2023, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +import operator +from collections.abc import Callable + +import xhiveframework +from xhiveframework.database.utils import NestedSetHierarchy +from xhiveframework.model.db_query import get_timespan_date_range +from xhiveframework.query_builder import Field + + +def like(key: Field, value: str) -> xhiveframework.qb: + """Wrapper method for `LIKE` + + Args: + key (str): field + value (str): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `LIKE` + """ + return key.like(value) + + +def func_in(key: Field, value: list | tuple) -> xhiveframework.qb: + """Wrapper method for `IN` + + Args: + key (str): field + value (Union[int, str]): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `IN` + """ + if isinstance(value, str): + value = value.split(",") + return key.isin(value) + + +def not_like(key: Field, value: str) -> xhiveframework.qb: + """Wrapper method for `NOT LIKE` + + Args: + key (str): field + value (str): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `NOT LIKE` + """ + return key.not_like(value) + + +def func_not_in(key: Field, value: list | tuple | str): + """Wrapper method for `NOT IN` + + Args: + key (str): field + value (Union[int, str]): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `NOT IN` + """ + if isinstance(value, str): + value = value.split(",") + return key.notin(value) + + +def func_regex(key: Field, value: str) -> xhiveframework.qb: + """Wrapper method for `REGEX` + + Args: + key (str): field + value (str): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `REGEX` + """ + return key.regex(value) + + +def func_between(key: Field, value: list | tuple) -> xhiveframework.qb: + """Wrapper method for `BETWEEN` + + Args: + key (str): field + value (Union[int, str]): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `BETWEEN` + """ + return key[slice(*value)] + + +def func_is(key, value): + "Wrapper for IS" + return key.isnotnull() if value.lower() == "set" else key.isnull() + + +def func_timespan(key: Field, value: str) -> xhiveframework.qb: + """Wrapper method for `TIMESPAN` + + Args: + key (str): field + value (str): criterion + + Returns: + xhiveframework.qb: `xhiveframework.qb object with `TIMESPAN` + """ + + return func_between(key, get_timespan_date_range(value)) + + +# default operators +OPERATOR_MAP: dict[str, Callable] = { + "+": operator.add, + "=": operator.eq, + "-": operator.sub, + "!=": operator.ne, + "<": operator.lt, + ">": operator.gt, + "<=": operator.le, + "=<": operator.le, + ">=": operator.ge, + "=>": operator.ge, + "/": operator.truediv, + "*": operator.mul, + "in": func_in, + "not in": func_not_in, + "like": like, + "not like": not_like, + "regex": func_regex, + "between": func_between, + "is": func_is, + "timespan": func_timespan, + "nested_set": NestedSetHierarchy, + # TODO: Add support for custom operators (WIP) - via filters_config hooks +} diff --git a/xhiveframework/database/postgres/__init__.py b/xhiveframework/database/postgres/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/database/postgres/database.py b/xhiveframework/database/postgres/database.py new file mode 100644 index 0000000..0bb9c67 --- /dev/null +++ b/xhiveframework/database/postgres/database.py @@ -0,0 +1,471 @@ +import re + +import psycopg2 +import psycopg2.extensions +from psycopg2.errorcodes import ( + CLASS_INTEGRITY_CONSTRAINT_VIOLATION, + DEADLOCK_DETECTED, + DUPLICATE_COLUMN, + INSUFFICIENT_PRIVILEGE, + STRING_DATA_RIGHT_TRUNCATION, + UNDEFINED_COLUMN, + UNDEFINED_TABLE, + UNIQUE_VIOLATION, +) +from psycopg2.errors import ( + LockNotAvailable, + ReadOnlySqlTransaction, + SequenceGeneratorLimitExceeded, + SyntaxError, +) +from psycopg2.extensions import ISOLATION_LEVEL_REPEATABLE_READ + +import xhiveframework +from xhiveframework.database.database import Database +from xhiveframework.database.postgres.schema import PostgresTable +from xhiveframework.database.utils import EmptyQueryValues, LazyDecode +from xhiveframework.utils import cstr, get_table_name + +# cast decimals as floats +DEC2FLOAT = psycopg2.extensions.new_type( + psycopg2.extensions.DECIMAL.values, + "DEC2FLOAT", + lambda value, curs: float(value) if value is not None else None, +) + +psycopg2.extensions.register_type(DEC2FLOAT) + +LOCATE_SUB_PATTERN = re.compile(r"locate\(([^,]+),([^)]+)(\)?)\)", flags=re.IGNORECASE) +LOCATE_QUERY_PATTERN = re.compile(r"locate\(", flags=re.IGNORECASE) +PG_TRANSFORM_PATTERN = re.compile(r"([=><]+)\s*([+-]?\d+)(\.0)?(?![a-zA-Z\.\d])") +FROM_TAB_PATTERN = re.compile(r"from tab([\w-]*)", flags=re.IGNORECASE) + + +class PostgresExceptionUtil: + ProgrammingError = psycopg2.ProgrammingError + TableMissingError = psycopg2.ProgrammingError + OperationalError = psycopg2.OperationalError + InternalError = psycopg2.InternalError + SQLError = psycopg2.ProgrammingError + DataError = psycopg2.DataError + InterfaceError = psycopg2.InterfaceError + SequenceGeneratorLimitExceeded = SequenceGeneratorLimitExceeded + + @staticmethod + def is_deadlocked(e): + return getattr(e, "pgcode", None) == DEADLOCK_DETECTED + + @staticmethod + def is_timedout(e): + # http://initd.org/psycopg/docs/extensions.html?highlight=datatype#psycopg2.extensions.QueryCanceledError + return isinstance(e, (psycopg2.extensions.QueryCanceledError | LockNotAvailable)) + + @staticmethod + def is_read_only_mode_error(e) -> bool: + return isinstance(e, ReadOnlySqlTransaction) + + @staticmethod + def is_syntax_error(e): + return isinstance(e, SyntaxError) + + @staticmethod + def is_table_missing(e): + return getattr(e, "pgcode", None) == UNDEFINED_TABLE + + @staticmethod + def is_missing_table(e): + return PostgresDatabase.is_table_missing(e) + + @staticmethod + def is_missing_column(e): + return getattr(e, "pgcode", None) == UNDEFINED_COLUMN + + @staticmethod + def is_access_denied(e): + return getattr(e, "pgcode", None) == INSUFFICIENT_PRIVILEGE + + @staticmethod + def cant_drop_field_or_key(e): + return getattr(e, "pgcode", None) == CLASS_INTEGRITY_CONSTRAINT_VIOLATION + + @staticmethod + def is_duplicate_entry(e): + return getattr(e, "pgcode", None) == UNIQUE_VIOLATION + + @staticmethod + def is_primary_key_violation(e): + return getattr(e, "pgcode", None) == UNIQUE_VIOLATION and "_pkey" in cstr(e.args[0]) + + @staticmethod + def is_unique_key_violation(e): + return getattr(e, "pgcode", None) == UNIQUE_VIOLATION and "_key" in cstr(e.args[0]) + + @staticmethod + def is_duplicate_fieldname(e): + return getattr(e, "pgcode", None) == DUPLICATE_COLUMN + + @staticmethod + def is_statement_timeout(e): + return PostgresDatabase.is_timedout(e) or isinstance(e, xhiveframework.QueryTimeoutError) + + @staticmethod + def is_data_too_long(e): + return getattr(e, "pgcode", None) == STRING_DATA_RIGHT_TRUNCATION + + @staticmethod + def is_db_table_size_limit(e) -> bool: + return False + + +class PostgresDatabase(PostgresExceptionUtil, Database): + REGEX_CHARACTER = "~" + default_port = "5432" + + def setup_type_map(self): + self.db_type = "postgres" + self.type_map = { + "Currency": ("decimal", "21,9"), + "Int": ("bigint", None), + "Long Int": ("bigint", None), + "Float": ("decimal", "21,9"), + "Percent": ("decimal", "21,9"), + "Check": ("smallint", None), + "Small Text": ("text", ""), + "Long Text": ("text", ""), + "Code": ("text", ""), + "Text Editor": ("text", ""), + "Markdown Editor": ("text", ""), + "HTML Editor": ("text", ""), + "Date": ("date", ""), + "Datetime": ("timestamp", None), + "Time": ("time", "6"), + "Text": ("text", ""), + "Data": ("varchar", self.VARCHAR_LEN), + "Link": ("varchar", self.VARCHAR_LEN), + "Dynamic Link": ("varchar", self.VARCHAR_LEN), + "Password": ("text", ""), + "Select": ("varchar", self.VARCHAR_LEN), + "Rating": ("decimal", "3,2"), + "Read Only": ("varchar", self.VARCHAR_LEN), + "Attach": ("text", ""), + "Attach Image": ("text", ""), + "Signature": ("text", ""), + "Color": ("varchar", self.VARCHAR_LEN), + "Barcode": ("text", ""), + "Geolocation": ("text", ""), + "Duration": ("decimal", "21,9"), + "Icon": ("varchar", self.VARCHAR_LEN), + "Phone": ("varchar", self.VARCHAR_LEN), + "Autocomplete": ("varchar", self.VARCHAR_LEN), + "JSON": ("json", ""), + } + + @property + def last_query(self): + return LazyDecode(self._cursor.query) + + def get_connection(self): + conn = psycopg2.connect( + "host='{}' dbname='{}' user='{}' password='{}' port={}".format( + self.host, self.user, self.user, self.password, self.port + ) + ) + conn.set_isolation_level(ISOLATION_LEVEL_REPEATABLE_READ) + + return conn + + def set_execution_timeout(self, seconds: int): + # Postgres expects milliseconds as input + self.sql("set local statement_timeout = %s", int(seconds) * 1000) + + def escape(self, s, percent=True): + """Escape quotes and percent in given string.""" + if isinstance(s, bytes): + s = s.decode("utf-8") + + # MariaDB's driver treats None as an empty string + # So Postgres should do the same + + if s is None: + s = "" + + if percent: + s = s.replace("%", "%%") + + s = s.encode("utf-8") + + return str(psycopg2.extensions.QuotedString(s)) + + def get_database_size(self): + """'Returns database size in MB""" + db_size = self.sql( + "SELECT (pg_database_size(%s) / 1024 / 1024) as database_size", self.db_name, as_dict=True + ) + return db_size[0].get("database_size") + + # pylint: disable=W0221 + def sql(self, query, values=EmptyQueryValues, *args, **kwargs): + return super().sql(modify_query(query), modify_values(values), *args, **kwargs) + + def lazy_mogrify(self, *args, **kwargs) -> str: + return self.last_query + + def get_tables(self, cached=True): + return [ + d[0] + for d in self.sql( + """select table_name + from information_schema.tables + where table_catalog='{}' + and table_type = 'BASE TABLE' + and table_schema='{}'""".format(xhiveframework.conf.db_name, xhiveframework.conf.get("db_schema", "public")) + ) + ] + + def format_date(self, date): + if not date: + return "0001-01-01" + + if not isinstance(date, str): + date = date.strftime("%Y-%m-%d") + + return date + + # column type + @staticmethod + def is_type_number(code): + return code == psycopg2.NUMBER + + @staticmethod + def is_type_datetime(code): + return code == psycopg2.DATETIME + + def rename_table(self, old_name: str, new_name: str) -> list | tuple: + old_name = get_table_name(old_name) + new_name = get_table_name(new_name) + return self.sql(f"ALTER TABLE `{old_name}` RENAME TO `{new_name}`") + + def describe(self, doctype: str) -> list | tuple: + table_name = get_table_name(doctype) + return self.sql( + f"SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = '{table_name}'" + ) + + def change_column_type( + self, doctype: str, column: str, type: str, nullable: bool = False, use_cast: bool = False + ) -> list | tuple: + table_name = get_table_name(doctype) + null_constraint = "SET NOT NULL" if not nullable else "DROP NOT NULL" + using_cast = f'using "{column}"::{type}' if use_cast else "" + + # postgres allows ddl in transactions but since we've currently made + # things same as mariadb (raising exception on ddl commands if the transaction has any writes), + # hence using sql_ddl here for committing and then moving forward. + return self.sql_ddl( + f"""ALTER TABLE "{table_name}" + ALTER COLUMN "{column}" TYPE {type} {using_cast}, + ALTER COLUMN "{column}" {null_constraint}""" + ) + + def rename_column(self, doctype: str, old_column_name: str, new_column_name: str): + table_name = get_table_name(doctype) + xhiveframework.db.sql_ddl( + f"ALTER TABLE `{table_name}` RENAME COLUMN `{old_column_name}` TO `{new_column_name}`" + ) + + def create_auth_table(self): + self.sql_ddl( + """create table if not exists "__Auth" ( + "doctype" VARCHAR(140) NOT NULL, + "name" VARCHAR(255) NOT NULL, + "fieldname" VARCHAR(140) NOT NULL, + "password" TEXT NOT NULL, + "encrypted" INT NOT NULL DEFAULT 0, + PRIMARY KEY ("doctype", "name", "fieldname") + )""" + ) + + def create_global_search_table(self): + if "__global_search" not in self.get_tables(): + self.sql( + f"""create table "__global_search"( + doctype varchar(100), + name varchar({self.VARCHAR_LEN}), + title varchar({self.VARCHAR_LEN}), + content text, + route varchar({self.VARCHAR_LEN}), + published int not null default 0, + unique (doctype, name))""" + ) + + def create_user_settings_table(self): + self.sql_ddl( + """create table if not exists "__UserSettings" ( + "user" VARCHAR(180) NOT NULL, + "doctype" VARCHAR(180) NOT NULL, + "data" TEXT, + UNIQUE ("user", "doctype") + )""" + ) + + def updatedb(self, doctype, meta=None): + """ + Syncs a `DocType` to the table + * creates if required + * updates columns + * updates indices + """ + res = self.sql(f"select issingle from `tabDocType` where name='{doctype}'") + if not res: + raise Exception(f"Wrong doctype {doctype} in updatedb") + + if not res[0][0]: + db_table = PostgresTable(doctype, meta) + db_table.validate() + + db_table.sync() + self.begin() + + @staticmethod + def get_on_duplicate_update(key="name"): + if isinstance(key, list): + key = '", "'.join(key) + return f'ON CONFLICT ("{key}") DO UPDATE SET ' + + def check_implicit_commit(self, query): + pass # postgres can run DDL in transactions without implicit commits + + def has_index(self, table_name, index_name): + return self.sql( + f"""SELECT 1 FROM pg_indexes WHERE tablename='{table_name}' + and indexname='{index_name}' limit 1""" + ) + + def add_index(self, doctype: str, fields: list, index_name: str | None = None): + """Creates an index with given fields if not already created. + Index name will be `fieldname1_fieldname2_index`""" + table_name = get_table_name(doctype) + index_name = index_name or self.get_index_name(fields) + fields_str = '", "'.join(re.sub(r"\(.*\)", "", field) for field in fields) + + self.sql_ddl(f'CREATE INDEX IF NOT EXISTS "{index_name}" ON `{table_name}` ("{fields_str}")') + + def add_unique(self, doctype, fields, constraint_name=None): + if isinstance(fields, str): + fields = [fields] + if not constraint_name: + constraint_name = "unique_" + "_".join(fields) + + if not self.sql( + """ + SELECT CONSTRAINT_NAME + FROM information_schema.TABLE_CONSTRAINTS + WHERE table_name=%s + AND constraint_type='UNIQUE' + AND CONSTRAINT_NAME=%s""", + ("tab" + doctype, constraint_name), + ): + self.commit() + self.sql( + """ALTER TABLE `tab{}` + ADD CONSTRAINT {} UNIQUE ({})""".format(doctype, constraint_name, ", ".join(fields)) + ) + + def get_table_columns_description(self, table_name): + """Returns list of column and its description""" + # pylint: disable=W1401 + return self.sql( + f""" + SELECT a.column_name AS name, + CASE LOWER(a.data_type) + WHEN 'character varying' THEN CONCAT('varchar(', a.character_maximum_length ,')') + WHEN 'timestamp without time zone' THEN 'timestamp' + ELSE a.data_type + END AS type, + BOOL_OR(b.index) AS index, + SPLIT_PART(COALESCE(a.column_default, NULL), '::', 1) AS default, + BOOL_OR(b.unique) AS unique + FROM information_schema.columns a + LEFT JOIN + (SELECT indexdef, tablename, + indexdef LIKE '%UNIQUE INDEX%' AS unique, + indexdef NOT LIKE '%UNIQUE INDEX%' AS index + FROM pg_indexes + WHERE tablename='{table_name}') b + ON SUBSTRING(b.indexdef, '(.*)') LIKE CONCAT('%', a.column_name, '%') + WHERE a.table_name = '{table_name}' + GROUP BY a.column_name, a.data_type, a.column_default, a.character_maximum_length; + """, + as_dict=1, + ) + + def get_column_type(self, doctype, column): + """Returns column type from database.""" + information_schema = xhiveframework.qb.Schema("information_schema") + table = get_table_name(doctype) + + return ( + xhiveframework.qb.from_(information_schema.columns) + .select(information_schema.columns.data_type) + .where( + (information_schema.columns.table_name == table) + & (information_schema.columns.column_name == column) + ) + .run(pluck=True)[0] + ) + + def get_database_list(self): + return self.sql("SELECT datname FROM pg_database", pluck=True) + + +def modify_query(query): + """ "Modifies query according to the requirements of postgres""" + # replace ` with " for definitions + query = str(query).replace("`", '"') + query = replace_locate_with_strpos(query) + # select from requires "" + query = FROM_TAB_PATTERN.sub(r'from "tab\1"', query) + + # only find int (with/without signs), ignore decimals (with/without signs), ignore hashes (which start with numbers), + # drop .0 from decimals and add quotes around them + # + # >>> query = "c='abcd' , a >= 45, b = -45.0, c = 40, d=4500.0, e=3500.53, f=40psdfsd, g=9092094312, h=12.00023" + # >>> re.sub(r"([=><]+)\s*([+-]?\d+)(\.0)?(?![a-zA-Z\.\d])", r"\1 '\2'", query) + # "c='abcd' , a >= '45', b = '-45', c = '40', d= '4500', e=3500.53, f=40psdfsd, g= '9092094312', h=12.00023 + + return PG_TRANSFORM_PATTERN.sub(r"\1 '\2'", query) + + +def modify_values(values): + def modify_value(value): + if isinstance(value, list | tuple): + value = tuple(modify_values(value)) + + elif isinstance(value, int): + value = str(value) + + return value + + if not values or values == EmptyQueryValues: + return values + + if isinstance(values, dict): + for k, v in values.items(): + values[k] = modify_value(v) + elif isinstance(values, tuple | list): + new_values = [] + for val in values: + new_values.append(modify_value(val)) + + values = new_values + else: + values = modify_value(values) + + return values + + +def replace_locate_with_strpos(query): + # strpos is the locate equivalent in postgres + if LOCATE_QUERY_PATTERN.search(query): + query = LOCATE_SUB_PATTERN.sub(r"strpos(\2\3, \1)", query) + return query diff --git a/xhiveframework/database/postgres/framework_postgres.sql b/xhiveframework/database/postgres/framework_postgres.sql new file mode 100644 index 0000000..e27cbff --- /dev/null +++ b/xhiveframework/database/postgres/framework_postgres.sql @@ -0,0 +1,345 @@ +-- Core Elements to install WNFramework +-- To be called from install.py + + +-- +-- Table structure for table "tabDocField" +-- + +DROP TABLE IF EXISTS "tabDocField"; +CREATE TABLE "tabDocField" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "fieldname" varchar(255) DEFAULT NULL, + "label" varchar(255) DEFAULT NULL, + "oldfieldname" varchar(255) DEFAULT NULL, + "fieldtype" varchar(255) DEFAULT NULL, + "oldfieldtype" varchar(255) DEFAULT NULL, + "options" text, + "search_index" smallint NOT NULL DEFAULT 0, + "hidden" smallint NOT NULL DEFAULT 0, + "set_only_once" smallint NOT NULL DEFAULT 0, + "show_dashboard" smallint NOT NULL DEFAULT 0, + "allow_in_quick_entry" smallint NOT NULL DEFAULT 0, + "print_hide" smallint NOT NULL DEFAULT 0, + "report_hide" smallint NOT NULL DEFAULT 0, + "reqd" smallint NOT NULL DEFAULT 0, + "bold" smallint NOT NULL DEFAULT 0, + "in_global_search" smallint NOT NULL DEFAULT 0, + "collapsible" smallint NOT NULL DEFAULT 0, + "unique" smallint NOT NULL DEFAULT 0, + "no_copy" smallint NOT NULL DEFAULT 0, + "allow_on_submit" smallint NOT NULL DEFAULT 0, + "show_preview_popup" smallint NOT NULL DEFAULT 0, + "trigger" varchar(255) DEFAULT NULL, + "collapsible_depends_on" text, + "mandatory_depends_on" text, + "read_only_depends_on" text, + "depends_on" text, + "permlevel" bigint NOT NULL DEFAULT 0, + "ignore_user_permissions" smallint NOT NULL DEFAULT 0, + "width" varchar(255) DEFAULT NULL, + "print_width" varchar(255) DEFAULT NULL, + "columns" bigint NOT NULL DEFAULT 0, + "default" text, + "description" text, + "in_list_view" smallint NOT NULL DEFAULT 0, + "fetch_if_empty" smallint NOT NULL DEFAULT 0, + "in_filter" smallint NOT NULL DEFAULT 0, + "remember_last_selected_value" smallint NOT NULL DEFAULT 0, + "ignore_xss_filter" smallint NOT NULL DEFAULT 0, + "print_hide_if_no_value" smallint NOT NULL DEFAULT 0, + "allow_bulk_edit" smallint NOT NULL DEFAULT 0, + "in_standard_filter" smallint NOT NULL DEFAULT 0, + "in_preview" smallint NOT NULL DEFAULT 0, + "read_only" smallint NOT NULL DEFAULT 0, + "precision" varchar(255) DEFAULT NULL, + "max_height" varchar(10) DEFAULT NULL, + "length" bigint NOT NULL DEFAULT 0, + "translatable" smallint NOT NULL DEFAULT 0, + "hide_border" smallint NOT NULL DEFAULT 0, + "hide_days" smallint NOT NULL DEFAULT 0, + "hide_seconds" smallint NOT NULL DEFAULT 0, + PRIMARY KEY ("name") +) ; + +create index on "tabDocField" ("parent"); +create index on "tabDocField" ("label"); +create index on "tabDocField" ("fieldtype"); +create index on "tabDocField" ("fieldname"); + +-- +-- Table structure for table "tabDocPerm" +-- + +DROP TABLE IF EXISTS "tabDocPerm"; +CREATE TABLE "tabDocPerm" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "permlevel" bigint DEFAULT '0', + "role" varchar(255) DEFAULT NULL, + "match" varchar(255) DEFAULT NULL, + "read" smallint NOT NULL DEFAULT 1, + "write" smallint NOT NULL DEFAULT 1, + "create" smallint NOT NULL DEFAULT 1, + "submit" smallint NOT NULL DEFAULT 0, + "cancel" smallint NOT NULL DEFAULT 0, + "delete" smallint NOT NULL DEFAULT 1, + "amend" smallint NOT NULL DEFAULT 0, + "report" smallint NOT NULL DEFAULT 1, + "export" smallint NOT NULL DEFAULT 1, + "import" smallint NOT NULL DEFAULT 0, + "share" smallint NOT NULL DEFAULT 1, + "print" smallint NOT NULL DEFAULT 1, + "email" smallint NOT NULL DEFAULT 1, + PRIMARY KEY ("name") +) ; + +create index on "tabDocPerm" ("parent"); + +-- +-- Table structure for table "tabDocType Action" +-- + +DROP TABLE IF EXISTS "tabDocType Action"; +CREATE TABLE "tabDocType Action" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "label" varchar(140) NOT NULL, + "group" text DEFAULT NULL, + "action_type" varchar(140) NOT NULL, + "action" varchar(140) NOT NULL, + PRIMARY KEY ("name") +) ; + +create index on "tabDocType Action" ("parent"); + +-- +-- Table structure for table "tabDocType Link" +-- + +DROP TABLE IF EXISTS "tabDocType Link"; +CREATE TABLE "tabDocType Link" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "label" varchar(140) DEFAULT NULL, + "group" varchar(140) DEFAULT NULL, + "link_doctype" varchar(140) NOT NULL, + "link_fieldname" varchar(140) NOT NULL, + PRIMARY KEY ("name") +) ; + +create index on "tabDocType Link" ("parent"); + + +-- +-- Table structure for table "tabDocType" +-- + +DROP TABLE IF EXISTS "tabDocType"; +CREATE TABLE "tabDocType" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "idx" bigint NOT NULL DEFAULT 0, + "search_fields" varchar(255) DEFAULT NULL, + "issingle" smallint NOT NULL DEFAULT 0, + "is_virtual" smallint NOT NULL DEFAULT 0, + "is_tree" smallint NOT NULL DEFAULT 0, + "istable" smallint NOT NULL DEFAULT 0, + "editable_grid" smallint NOT NULL DEFAULT 1, + "track_changes" smallint NOT NULL DEFAULT 0, + "module" varchar(255) DEFAULT NULL, + "restrict_to_domain" varchar(255) DEFAULT NULL, + "app" varchar(255) DEFAULT NULL, + "autoname" varchar(255) DEFAULT NULL, + "naming_rule" varchar(40) DEFAULT NULL, + "title_field" varchar(255) DEFAULT NULL, + "image_field" varchar(255) DEFAULT NULL, + "timeline_field" varchar(255) DEFAULT NULL, + "sort_field" varchar(255) DEFAULT NULL, + "sort_order" varchar(255) DEFAULT NULL, + "description" text, + "colour" varchar(255) DEFAULT NULL, + "read_only" smallint NOT NULL DEFAULT 0, + "in_create" smallint NOT NULL DEFAULT 0, + "menu_index" bigint DEFAULT NULL, + "parent_node" varchar(255) DEFAULT NULL, + "smallicon" varchar(255) DEFAULT NULL, + "allow_copy" smallint NOT NULL DEFAULT 0, + "allow_rename" smallint NOT NULL DEFAULT 0, + "allow_import" smallint NOT NULL DEFAULT 0, + "hide_toolbar" smallint NOT NULL DEFAULT 0, + "track_seen" smallint NOT NULL DEFAULT 0, + "max_attachments" bigint NOT NULL DEFAULT 0, + "print_outline" varchar(255) DEFAULT NULL, + "document_type" varchar(255) DEFAULT NULL, + "icon" varchar(255) DEFAULT NULL, + "color" varchar(255) DEFAULT NULL, + "tag_fields" varchar(255) DEFAULT NULL, + "subject" varchar(255) DEFAULT NULL, + "_last_update" varchar(32) DEFAULT NULL, + "engine" varchar(20) DEFAULT 'InnoDB', + "default_print_format" varchar(255) DEFAULT NULL, + "is_submittable" smallint NOT NULL DEFAULT 0, + "show_name_in_global_search" smallint NOT NULL DEFAULT 0, + "_user_tags" varchar(255) DEFAULT NULL, + "custom" smallint NOT NULL DEFAULT 0, + "beta" smallint NOT NULL DEFAULT 0, + "has_web_view" smallint NOT NULL DEFAULT 0, + "allow_guest_to_view" smallint NOT NULL DEFAULT 0, + "route" varchar(255) DEFAULT NULL, + "is_published_field" varchar(255) DEFAULT NULL, + "website_search_field" varchar(255) DEFAULT NULL, + "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, + "translated_doctype" smallint NOT NULL DEFAULT 0, + PRIMARY KEY ("name") +) ; + +-- +-- Table structure for table "tabSeries" +-- + +DROP TABLE IF EXISTS "tabSeries"; +CREATE TABLE "tabSeries" ( + "name" varchar(100), + "current" bigint NOT NULL DEFAULT 0, + PRIMARY KEY ("name") +) ; + +-- +-- Table structure for table "tabSessions" +-- + +DROP TABLE IF EXISTS "tabSessions"; +CREATE TABLE "tabSessions" ( + "user" varchar(255) DEFAULT NULL, + "sid" varchar(255) DEFAULT NULL, + "sessiondata" text, + "ipaddress" varchar(16) DEFAULT NULL, + "lastupdate" timestamp(6) DEFAULT NULL, + "status" varchar(20) DEFAULT NULL +); + +create index on "tabSessions" ("sid"); + +-- +-- Table structure for table "tabSingles" +-- + +DROP TABLE IF EXISTS "tabSingles"; +CREATE TABLE "tabSingles" ( + "doctype" varchar(255) DEFAULT NULL, + "field" varchar(255) DEFAULT NULL, + "value" text +); + +create index on "tabSingles" ("doctype", "field"); + +-- +-- Table structure for table "__Auth" +-- + +DROP TABLE IF EXISTS "__Auth"; +CREATE TABLE "__Auth" ( + "doctype" VARCHAR(140) NOT NULL, + "name" VARCHAR(255) NOT NULL, + "fieldname" VARCHAR(140) NOT NULL, + "password" TEXT NOT NULL, + "encrypted" int NOT NULL DEFAULT 0, + PRIMARY KEY ("doctype", "name", "fieldname") +); + +create index on "__Auth" ("doctype", "name", "fieldname"); + +-- +-- Table structure for table "tabFile" +-- + +DROP TABLE IF EXISTS "tabFile"; +CREATE TABLE "tabFile" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "file_name" varchar(255) DEFAULT NULL, + "file_url" varchar(255) DEFAULT NULL, + "module" varchar(255) DEFAULT NULL, + "attached_to_name" varchar(255) DEFAULT NULL, + "file_size" bigint NOT NULL DEFAULT 0, + "attached_to_doctype" varchar(255) DEFAULT NULL, + PRIMARY KEY ("name") +); + +create index on "tabFile" ("parent"); +create index on "tabFile" ("attached_to_name"); +create index on "tabFile" ("attached_to_doctype"); + +-- +-- Table structure for table "tabDefaultValue" +-- + +DROP TABLE IF EXISTS "tabDefaultValue"; +CREATE TABLE "tabDefaultValue" ( + "name" varchar(255) NOT NULL, + "creation" timestamp(6) DEFAULT NULL, + "modified" timestamp(6) DEFAULT NULL, + "modified_by" varchar(255) DEFAULT NULL, + "owner" varchar(255) DEFAULT NULL, + "docstatus" smallint NOT NULL DEFAULT 0, + "parent" varchar(255) DEFAULT NULL, + "parentfield" varchar(255) DEFAULT NULL, + "parenttype" varchar(255) DEFAULT NULL, + "idx" bigint NOT NULL DEFAULT 0, + "defvalue" text, + "defkey" varchar(255) DEFAULT NULL, + PRIMARY KEY ("name") +); + +create index on "tabDefaultValue" ("parent"); +create index on "tabDefaultValue" ("parent", "defkey"); diff --git a/xhiveframework/database/postgres/schema.py b/xhiveframework/database/postgres/schema.py new file mode 100644 index 0000000..cf49ddf --- /dev/null +++ b/xhiveframework/database/postgres/schema.py @@ -0,0 +1,159 @@ +import xhiveframework +from xhiveframework import _ +from xhiveframework.database.schema import DBTable, get_definition +from xhiveframework.utils import cint, flt + + +class PostgresTable(DBTable): + def create(self): + varchar_len = xhiveframework.db.VARCHAR_LEN + name_column = f"name varchar({varchar_len}) primary key" + + additional_definitions = "" + # columns + column_defs = self.get_column_definitions() + if column_defs: + additional_definitions += ",\n".join(column_defs) + + # child table columns + if self.meta.get("istable") or 0: + if column_defs: + additional_definitions += ",\n" + + additional_definitions += ",\n".join( + ( + f"parent varchar({varchar_len})", + f"parentfield varchar({varchar_len})", + f"parenttype varchar({varchar_len})", + ) + ) + + # creating sequence(s) + if not self.meta.issingle and self.meta.autoname == "autoincrement": + xhiveframework.db.create_sequence(self.doctype, check_not_exists=True) + name_column = "name bigint primary key" + + # TODO: set docstatus length + # create table + xhiveframework.db.sql( + f"""create table `{self.table_name}` ( + {name_column}, + creation timestamp(6), + modified timestamp(6), + modified_by varchar({varchar_len}), + owner varchar({varchar_len}), + docstatus smallint not null default '0', + idx bigint not null default '0', + {additional_definitions} + )""" + ) + + self.create_indexes() + xhiveframework.db.commit() + + def create_indexes(self): + create_index_query = "" + for col in self.columns.values(): + if ( + col.set_index + and col.fieldtype in xhiveframework.db.type_map + and xhiveframework.db.type_map.get(col.fieldtype)[0] not in ("text", "longtext") + ): + create_index_query += ( + f'CREATE INDEX IF NOT EXISTS "{col.fieldname}" ON `{self.table_name}`(`{col.fieldname}`);' + ) + if create_index_query: + # nosemgrep + xhiveframework.db.sql(create_index_query) + + def alter(self): + for col in self.columns.values(): + col.build_for_alter_table(self.current_columns.get(col.fieldname.lower())) + + query = [f"ADD COLUMN `{col.fieldname}` {col.get_definition()}" for col in self.add_column] + + for col in self.change_type: + using_clause = "" + if col.fieldtype in ("Datetime"): + # The USING option of SET DATA TYPE can actually specify any expression + # involving the old values of the row + # read more https://www.postgresql.org/docs/9.1/sql-altertable.html + using_clause = f"USING {col.fieldname}::timestamp without time zone" + elif col.fieldtype == "Check": + using_clause = f"USING {col.fieldname}::smallint" + + query.append( + "ALTER COLUMN `{}` TYPE {} {}".format( + col.fieldname, + get_definition(col.fieldtype, precision=col.precision, length=col.length), + using_clause, + ) + ) + + for col in self.set_default: + if col.fieldname == "name": + continue + + if col.fieldtype in ("Check", "Int"): + col_default = cint(col.default) + + elif col.fieldtype in ("Currency", "Float", "Percent"): + col_default = flt(col.default) + + elif not col.default: + col_default = "NULL" + + else: + col_default = f"{xhiveframework.db.escape(col.default)}" + + query.append(f"ALTER COLUMN `{col.fieldname}` SET DEFAULT {col_default}") + + create_contraint_query = "" + for col in self.add_index: + # if index key not exists + create_contraint_query += ( + f'CREATE INDEX IF NOT EXISTS "{col.fieldname}" ON `{self.table_name}`(`{col.fieldname}`);' + ) + + for col in self.add_unique: + # if index key not exists + create_contraint_query += 'CREATE UNIQUE INDEX IF NOT EXISTS "unique_{index_name}" ON `{table_name}`(`{field}`);'.format( + index_name=col.fieldname, table_name=self.table_name, field=col.fieldname + ) + + drop_contraint_query = "" + for col in self.drop_index: + # primary key + if col.fieldname != "name": + # if index key exists + drop_contraint_query += f'DROP INDEX IF EXISTS "{col.fieldname}" ;' + + for col in self.drop_unique: + # primary key + if col.fieldname != "name": + # if index key exists + drop_contraint_query += f'DROP INDEX IF EXISTS "unique_{col.fieldname}" ;' + try: + if query: + final_alter_query = "ALTER TABLE `{}` {}".format(self.table_name, ", ".join(query)) + # nosemgrep + xhiveframework.db.sql(final_alter_query) + if create_contraint_query: + # nosemgrep + xhiveframework.db.sql(create_contraint_query) + if drop_contraint_query: + # nosemgrep + xhiveframework.db.sql(drop_contraint_query) + except Exception as e: + # sanitize + if xhiveframework.db.is_duplicate_fieldname(e): + xhiveframework.throw(str(e)) + elif xhiveframework.db.is_duplicate_entry(e): + fieldname = str(e).split("'")[-2] + xhiveframework.throw( + _( + "{0} field cannot be set as unique in {1}, as there are non-unique existing values" + ).format(fieldname, self.table_name) + ) + else: + raise e diff --git a/xhiveframework/database/postgres/setup_db.py b/xhiveframework/database/postgres/setup_db.py new file mode 100644 index 0000000..6a72af1 --- /dev/null +++ b/xhiveframework/database/postgres/setup_db.py @@ -0,0 +1,91 @@ +import os + +import xhiveframework +from xhiveframework.database.db_manager import DbManager + + +def setup_database(force, source_sql=None, verbose=False): + root_conn = get_root_connection(xhiveframework.flags.root_login, xhiveframework.flags.root_password) + root_conn.commit() + root_conn.sql("end") + root_conn.sql(f"DROP DATABASE IF EXISTS `{xhiveframework.conf.db_name}`") + root_conn.sql(f"DROP USER IF EXISTS {xhiveframework.conf.db_name}") + root_conn.sql(f"CREATE DATABASE `{xhiveframework.conf.db_name}`") + root_conn.sql(f"CREATE user {xhiveframework.conf.db_name} password '{xhiveframework.conf.db_password}'") + root_conn.sql(f"GRANT ALL PRIVILEGES ON DATABASE `{xhiveframework.conf.db_name}` TO {xhiveframework.conf.db_name}") + root_conn.close() + + bootstrap_database(xhiveframework.conf.db_name, verbose, source_sql=source_sql) + xhiveframework.connect() + + +def bootstrap_database(db_name, verbose, source_sql=None): + xhiveframework.connect(db_name=db_name) + import_db_from_sql(source_sql, verbose) + xhiveframework.connect(db_name=db_name) + + if "tabDefaultValue" not in xhiveframework.db.get_tables(): + import sys + + from click import secho + + secho( + "Table 'tabDefaultValue' missing in the restored site. " + "This may be due to incorrect permissions or the result of a restore from a bad backup file. " + "Database not installed correctly.", + fg="red", + ) + sys.exit(1) + + +def import_db_from_sql(source_sql=None, verbose=False): + if verbose: + print("Starting database import...") + db_name = xhiveframework.conf.db_name + if not source_sql: + source_sql = os.path.join(os.path.dirname(__file__), "framework_postgres.sql") + DbManager(xhiveframework.local.db).restore_database( + verbose, db_name, source_sql, db_name, xhiveframework.conf.db_password + ) + if verbose: + print("Imported from database %s" % source_sql) + + +def get_root_connection(root_login=None, root_password=None): + if not xhiveframework.local.flags.root_connection: + if not root_login: + root_login = xhiveframework.conf.get("root_login") or None + + if not root_login: + root_login = input("Enter postgres super user: ") + + if not root_password: + root_password = xhiveframework.conf.get("root_password") or None + + if not root_password: + from getpass import getpass + + root_password = getpass("Postgres super user password: ") + + xhiveframework.local.flags.root_connection = xhiveframework.database.get_db( + host=xhiveframework.conf.db_host, + port=xhiveframework.conf.db_port, + user=root_login, + password=root_password, + ) + + return xhiveframework.local.flags.root_connection + + +def drop_user_and_database(db_name, root_login, root_password): + root_conn = get_root_connection( + xhiveframework.flags.root_login or root_login, xhiveframework.flags.root_password or root_password + ) + root_conn.commit() + root_conn.sql( + "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = %s", + (db_name,), + ) + root_conn.sql("end") + root_conn.sql(f"DROP DATABASE IF EXISTS {db_name}") + root_conn.sql(f"DROP USER IF EXISTS {db_name}") diff --git a/xhiveframework/database/query.py b/xhiveframework/database/query.py new file mode 100644 index 0000000..5859fed --- /dev/null +++ b/xhiveframework/database/query.py @@ -0,0 +1,557 @@ +import re +from ast import literal_eval +from types import BuiltinFunctionType +from typing import TYPE_CHECKING + +import sqlparse +from pypika.queries import QueryBuilder, Table + +import xhiveframework +from xhiveframework import _ +from xhiveframework.database.operator_map import OPERATOR_MAP +from xhiveframework.database.schema import SPECIAL_CHAR_PATTERN +from xhiveframework.database.utils import DefaultOrderBy, get_doctype_name +from xhiveframework.query_builder import Criterion, Field, Order, functions +from xhiveframework.query_builder.functions import Function, SqlFunctions +from xhiveframework.query_builder.utils import PseudoColumnMapper +from xhiveframework.utils.data import MARIADB_SPECIFIC_COMMENT + +if TYPE_CHECKING: + from xhiveframework.query_builder import DocType + +TAB_PATTERN = re.compile("^tab") +WORDS_PATTERN = re.compile(r"\w+") +BRACKETS_PATTERN = re.compile(r"\(.*?\)|$") +SQL_FUNCTIONS = [sql_function.value for sql_function in SqlFunctions] +COMMA_PATTERN = re.compile(r",\s*(?![^()]*\))") + +# less restrictive version of xhiveframework.core.doctype.doctype.doctype.START_WITH_LETTERS_PATTERN +# to allow table names like __Auth +TABLE_NAME_PATTERN = re.compile(r"^[\w -]*$", flags=re.ASCII) + + +class Engine: + def get_query( + self, + table: str | Table, + fields: list | tuple | None = None, + filters: dict[str, str | int] | str | int | list[list | str | int] | None = None, + order_by: str | None = None, + group_by: str | None = None, + limit: int | None = None, + offset: int | None = None, + distinct: bool = False, + for_update: bool = False, + update: bool = False, + into: bool = False, + delete: bool = False, + *, + validate_filters: bool = False, + skip_locked: bool = False, + wait: bool = True, + ) -> QueryBuilder: + self.is_mariadb = xhiveframework.db.db_type == "mariadb" + self.is_postgres = xhiveframework.db.db_type == "postgres" + self.validate_filters = validate_filters + + if isinstance(table, Table): + self.table = table + self.doctype = get_doctype_name(table.get_sql()) + else: + self.doctype = table + self.validate_doctype() + self.table = xhiveframework.qb.DocType(table) + + if update: + self.query = xhiveframework.qb.update(self.table) + elif into: + self.query = xhiveframework.qb.into(self.table) + elif delete: + self.query = xhiveframework.qb.from_(self.table).delete() + else: + self.query = xhiveframework.qb.from_(self.table) + self.apply_fields(fields) + + self.apply_filters(filters) + self.apply_order_by(order_by) + + if limit: + self.query = self.query.limit(limit) + + if offset: + self.query = self.query.offset(offset) + + if distinct: + self.query = self.query.distinct() + + if for_update: + self.query = self.query.for_update(skip_locked=skip_locked, nowait=not wait) + + if group_by: + self.query = self.query.groupby(group_by) + + return self.query + + def validate_doctype(self): + if not TABLE_NAME_PATTERN.match(self.doctype): + xhiveframework.throw(_("Invalid DocType: {0}").format(self.doctype)) + + def apply_fields(self, fields): + # add fields + self.fields = self.parse_fields(fields) + if not self.fields: + self.fields = [self.table.name] + + self.query._child_queries = [] + for field in self.fields: + if isinstance(field, DynamicTableField): + self.query = field.apply_select(self.query) + elif isinstance(field, ChildQuery): + self.query._child_queries.append(field) + else: + self.query = self.query.select(field) + + def apply_filters( + self, + filters: dict[str, str | int] | str | int | list[list | str | int] | None = None, + ): + if filters is None: + return + + if isinstance(filters, str | int): + filters = {"name": str(filters)} + + if isinstance(filters, Criterion): + self.query = self.query.where(filters) + + elif isinstance(filters, dict): + self.apply_dict_filters(filters) + + elif isinstance(filters, list | tuple): + if all(isinstance(d, str | int) for d in filters) and len(filters) > 0: + self.apply_dict_filters({"name": ("in", filters)}) + else: + for filter in filters: + if isinstance(filter, str | int | Criterion | dict): + self.apply_filters(filter) + elif isinstance(filter, list | tuple): + self.apply_list_filters(filter) + + def apply_list_filters(self, filter: list): + if len(filter) == 2: + field, value = filter + self._apply_filter(field, value) + elif len(filter) == 3: + field, operator, value = filter + self._apply_filter(field, value, operator) + elif len(filter) == 4: + doctype, field, operator, value = filter + self._apply_filter(field, value, operator, doctype) + + def apply_dict_filters(self, filters: dict[str, str | int | list]): + for field, value in filters.items(): + operator = "=" + if isinstance(value, list | tuple): + operator, value = value + + self._apply_filter(field, value, operator) + + def _apply_filter( + self, field: str, value: str | int | list | None, operator: str = "=", doctype: str | None = None + ): + _field = field + _value = value + _operator = operator + + if not isinstance(_field, str): + pass + elif not self.validate_filters and (dynamic_field := DynamicTableField.parse(field, self.doctype)): + # apply implicit join if link field's field is referenced + self.query = dynamic_field.apply_join(self.query) + _field = dynamic_field.field + elif self.validate_filters and SPECIAL_CHAR_PATTERN.search(_field): + xhiveframework.throw(_("Invalid filter: {0}").format(_field)) + elif not doctype or doctype == self.doctype: + _field = self.table[field] + elif doctype: + _field = xhiveframework.qb.DocType(doctype)[field] + + # apply implicit join if child table is referenced + if doctype and doctype != self.doctype: + meta = xhiveframework.get_meta(doctype) + table = xhiveframework.qb.DocType(doctype) + if meta.istable and not self.query.is_joined(table): + self.query = self.query.left_join(table).on( + (table.parent == self.table.name) & (table.parenttype == self.doctype) + ) + + if isinstance(_value, bool): + _value = int(_value) + + elif not _value and isinstance(_value, list | tuple): + _value = ("",) + + # Nested set + if _operator in OPERATOR_MAP["nested_set"]: + hierarchy = _operator + docname = _value + + _df = xhiveframework.get_meta(self.doctype).get_field(field) + ref_doctype = _df.options if _df else self.doctype + + nodes = get_nested_set_hierarchy_result(ref_doctype, docname, hierarchy) + operator_fn = ( + OPERATOR_MAP["not in"] + if hierarchy in ("not ancestors of", "not descendants of") + else OPERATOR_MAP["in"] + ) + if nodes: + self.query = self.query.where(operator_fn(_field, nodes)) + else: + self.query = self.query.where(operator_fn(_field, ("",))) + return + + operator_fn = OPERATOR_MAP[_operator.casefold()] + if _value is None and isinstance(_field, Field): + self.query = self.query.where(_field.isnull()) + else: + self.query = self.query.where(operator_fn(_field, _value)) + + def get_function_object(self, field: str) -> "Function": + """Expects field to look like 'SUM(*)' or 'name' or something similar. Returns PyPika Function object""" + func = field.split("(", maxsplit=1)[0].capitalize() + args_start, args_end = len(func) + 1, field.index(")") + args = field[args_start:args_end].split(",") + + _, alias = field.split(" as ") if " as " in field else (None, None) + + to_cast = "*" not in args + _args = [] + + for arg in args: + initial_fields = literal_eval_(arg.strip()) + if to_cast: + has_primitive_operator = False + for _operator in OPERATOR_MAP.keys(): + if _operator in initial_fields: + operator_mapping = OPERATOR_MAP[_operator] + # Only perform this if operator is of primitive type. + if isinstance(operator_mapping, BuiltinFunctionType): + has_primitive_operator = True + field = operator_mapping( + *map( + lambda field: Field(field.strip()) + if "`" not in field + else PseudoColumnMapper(field.strip()), + arg.split(_operator), + ), + ) + + field = ( + ( + Field(initial_fields) + if "`" not in initial_fields + else PseudoColumnMapper(initial_fields) + ) + if not has_primitive_operator + else field + ) + else: + field = initial_fields + + _args.append(field) + + if alias and "`" in alias: + alias = alias.replace("`", "") + try: + if func.casefold() == "now": + return getattr(functions, func)() + return getattr(functions, func)(*_args, alias=alias or None) + except AttributeError: + # Fall back for functions not present in `SqlFunctions`` + return Function(func, *_args, alias=alias or None) + + def sanitize_fields(self, fields: str | list | tuple): + def _sanitize_field(field: str): + if not isinstance(field, str): + return field + stripped_field = sqlparse.format(field, strip_comments=True, keyword_case="lower") + if self.is_mariadb: + return MARIADB_SPECIFIC_COMMENT.sub("", stripped_field) + return stripped_field + + if isinstance(fields, list | tuple): + return [_sanitize_field(field) for field in fields] + elif isinstance(fields, str): + return _sanitize_field(fields) + + return fields + + def parse_string_field(self, field: str): + if field == "*": + return self.table.star + alias = None + if " as " in field: + field, alias = field.split(" as ") + if "`" in field: + if alias: + return PseudoColumnMapper(f"{field} {alias}") + return PseudoColumnMapper(field) + if alias: + return self.table[field].as_(alias) + return self.table[field] + + def parse_fields(self, fields: str | list | tuple | None) -> list: + if not fields: + return [] + fields = self.sanitize_fields(fields) + if isinstance(fields, list | tuple | set) and None in fields and Field not in fields: + return [] + + if not isinstance(fields, list | tuple): + fields = [fields] + + def parse_field(field: str): + if has_function(field): + return self.get_function_object(field) + elif parsed := DynamicTableField.parse(field, self.doctype): + return parsed + else: + return self.parse_string_field(field) + + _fields = [] + for field in fields: + if isinstance(field, Criterion): + _fields.append(field) + elif isinstance(field, dict): + for child_field, fields in field.items(): + _fields.append(ChildQuery(child_field, fields, self.doctype)) + elif isinstance(field, str): + if "," in field: + field = field.casefold() if "`" not in field else field + field_list = COMMA_PATTERN.split(field) + for field in field_list: + if _field := field.strip(): + _fields.append(parse_field(_field)) + else: + _fields.append(parse_field(field)) + + return _fields + + def apply_order_by(self, order_by: str | None): + if not order_by or order_by == DefaultOrderBy: + return + for declaration in order_by.split(","): + if _order_by := declaration.strip(): + parts = _order_by.split(" ") + order_field, order_direction = parts[0], parts[1] if len(parts) > 1 else "desc" + order_direction = Order.asc if order_direction.lower() == "asc" else Order.desc + self.query = self.query.orderby(order_field, order=order_direction) + + +class Permission: + @classmethod + def check_permissions(cls, query, **kwargs): + if not isinstance(query, str): + query = query.get_sql() + + doctype = cls.get_tables_from_query(query) + if isinstance(doctype, str): + doctype = [doctype] + + for dt in doctype: + dt = TAB_PATTERN.sub("", dt) + if not xhiveframework.has_permission( + dt, + "select", + user=kwargs.get("user"), + parent_doctype=kwargs.get("parent_doctype"), + ) and not xhiveframework.has_permission( + dt, + "read", + user=kwargs.get("user"), + parent_doctype=kwargs.get("parent_doctype"), + ): + xhiveframework.throw(_("Insufficient Permission for {0}").format(xhiveframework.bold(dt))) + + @staticmethod + def get_tables_from_query(query: str): + return [table for table in WORDS_PATTERN.findall(query) if table.startswith("tab")] + + +class DynamicTableField: + def __init__( + self, + doctype: str, + fieldname: str, + parent_doctype: str, + alias: str | None = None, + ) -> None: + self.doctype = doctype + self.fieldname = fieldname + self.alias = alias + self.parent_doctype = parent_doctype + + def __str__(self) -> str: + table_name = f"`tab{self.doctype}`" + fieldname = f"`{self.fieldname}`" + if xhiveframework.db.db_type == "postgres": + table_name = table_name.replace("`", '"') + fieldname = fieldname.replace("`", '"') + alias = f"AS {self.alias}" if self.alias else "" + return f"{table_name}.{fieldname} {alias}".strip() + + @staticmethod + def parse(field: str, doctype: str): + if "." in field: + alias = None + if " as " in field: + field, alias = field.split(" as ") + if field.startswith("`tab") or field.startswith('"tab'): + _, child_doctype, child_field = re.search(r'([`"])tab(.+?)\1.\1(.+)\1', field).groups() + if child_doctype == doctype: + return + return ChildTableField(child_doctype, child_field, doctype, alias=alias) + else: + linked_fieldname, fieldname = field.split(".") + linked_field = xhiveframework.get_meta(doctype).get_field(linked_fieldname) + linked_doctype = linked_field.options + if linked_field.fieldtype == "Link": + return LinkTableField(linked_doctype, fieldname, doctype, linked_fieldname, alias=alias) + elif linked_field.fieldtype in xhiveframework.model.table_fields: + return ChildTableField(linked_doctype, fieldname, doctype, alias=alias) + + def apply_select(self, query: QueryBuilder) -> QueryBuilder: + raise NotImplementedError + + +class ChildTableField(DynamicTableField): + def __init__( + self, + doctype: str, + fieldname: str, + parent_doctype: str, + alias: str | None = None, + ) -> None: + self.doctype = doctype + self.fieldname = fieldname + self.alias = alias + self.parent_doctype = parent_doctype + self.table = xhiveframework.qb.DocType(self.doctype) + self.field = self.table[self.fieldname] + + def apply_select(self, query: QueryBuilder) -> QueryBuilder: + table = xhiveframework.qb.DocType(self.doctype) + query = self.apply_join(query) + return query.select(getattr(table, self.fieldname).as_(self.alias or None)) + + def apply_join(self, query: QueryBuilder) -> QueryBuilder: + table = xhiveframework.qb.DocType(self.doctype) + main_table = xhiveframework.qb.DocType(self.parent_doctype) + if not query.is_joined(table): + query = query.left_join(table).on( + (table.parent == main_table.name) & (table.parenttype == self.parent_doctype) + ) + return query + + +class LinkTableField(DynamicTableField): + def __init__( + self, + doctype: str, + fieldname: str, + parent_doctype: str, + link_fieldname: str, + alias: str | None = None, + ) -> None: + super().__init__(doctype, fieldname, parent_doctype, alias=alias) + self.link_fieldname = link_fieldname + self.table = xhiveframework.qb.DocType(self.doctype) + self.field = self.table[self.fieldname] + + def apply_select(self, query: QueryBuilder) -> QueryBuilder: + table = xhiveframework.qb.DocType(self.doctype) + query = self.apply_join(query) + return query.select(getattr(table, self.fieldname).as_(self.alias or None)) + + def apply_join(self, query: QueryBuilder) -> QueryBuilder: + table = xhiveframework.qb.DocType(self.doctype) + main_table = xhiveframework.qb.DocType(self.parent_doctype) + if not query.is_joined(table): + query = query.left_join(table).on(table.name == getattr(main_table, self.link_fieldname)) + return query + + +class ChildQuery: + def __init__( + self, + fieldname: str, + fields: list, + parent_doctype: str, + ) -> None: + field = xhiveframework.get_meta(parent_doctype).get_field(fieldname) + if field.fieldtype not in xhiveframework.model.table_fields: + return + self.fieldname = fieldname + self.fields = fields + self.parent_doctype = parent_doctype + self.doctype = field.options + + def get_query(self, parent_names=None) -> QueryBuilder: + filters = { + "parenttype": self.parent_doctype, + "parentfield": self.fieldname, + "parent": ["in", parent_names], + } + return xhiveframework.qb.get_query( + self.doctype, + fields=[*self.fields, "parent", "parentfield"], + filters=filters, + order_by="idx asc", + ) + + +def literal_eval_(literal): + try: + return literal_eval(literal) + except (ValueError, SyntaxError): + return literal + + +def has_function(field): + _field = field.casefold() if (isinstance(field, str) and "`" not in field) else field + if not issubclass(type(_field), Criterion): + if any([f"{func}(" in _field for func in SQL_FUNCTIONS]): + return True + + +def get_nested_set_hierarchy_result(doctype: str, name: str, hierarchy: str) -> list[str]: + """Get matching nodes based on operator.""" + table = xhiveframework.qb.DocType(doctype) + try: + lft, rgt = xhiveframework.qb.from_(table).select("lft", "rgt").where(table.name == name).run()[0] + except IndexError: + lft, rgt = None, None + + if hierarchy in ("descendants of", "not descendants of", "descendants of (inclusive)"): + result = ( + xhiveframework.qb.from_(table) + .select(table.name) + .where(table.lft > lft) + .where(table.rgt < rgt) + .orderby(table.lft, order=Order.asc) + .run(pluck=True) + ) + if hierarchy == "descendants of (inclusive)": + result += [name] + else: + # Get ancestor elements of a DocType with a tree structure + result = ( + xhiveframework.qb.from_(table) + .select(table.name) + .where(table.lft < lft) + .where(table.rgt > rgt) + .orderby(table.lft, order=Order.desc) + .run(pluck=True) + ) + return result diff --git a/xhiveframework/database/schema.py b/xhiveframework/database/schema.py new file mode 100644 index 0000000..7c7a17b --- /dev/null +++ b/xhiveframework/database/schema.py @@ -0,0 +1,366 @@ +import re + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils import cint, cstr, flt + +SPECIAL_CHAR_PATTERN = re.compile(r"[\W]", flags=re.UNICODE) +VARCHAR_CAST_PATTERN = re.compile(r"varchar\(([\d]+)\)") + + +class InvalidColumnName(xhiveframework.ValidationError): + pass + + +class DBTable: + def __init__(self, doctype, meta=None): + self.doctype = doctype + self.table_name = f"tab{doctype}" + self.meta = meta or xhiveframework.get_meta(doctype, False) + self.columns: dict[str, DbColumn] = {} + self.current_columns = {} + + # lists for change + self.add_column: list[DbColumn] = [] + self.change_type: list[DbColumn] = [] + self.change_name: list[DbColumn] = [] + self.add_unique: list[DbColumn] = [] + self.add_index: list[DbColumn] = [] + self.drop_unique: list[DbColumn] = [] + self.drop_index: list[DbColumn] = [] + self.set_default: list[DbColumn] = [] + + # load + self.get_columns_from_docfields() + + def sync(self): + if self.meta.get("is_virtual"): + # no schema to sync for virtual doctypes + return + if self.is_new(): + self.create() + else: + xhiveframework.cache.hdel("table_columns", self.table_name) + self.alter() + + def create(self): + pass + + def get_column_definitions(self): + column_list = [*xhiveframework.db.DEFAULT_COLUMNS] + ret = [] + for k in list(self.columns): + if k not in column_list: + d = self.columns[k].get_definition() + if d: + ret.append("`" + k + "` " + d) + column_list.append(k) + return ret + + def get_index_definitions(self): + return [ + "index `" + key + "`(`" + key + "`)" + for key, col in self.columns.items() + if ( + col.set_index + and not col.unique + and col.fieldtype in xhiveframework.db.type_map + and xhiveframework.db.type_map.get(col.fieldtype)[0] not in ("text", "longtext") + ) + ] + + def get_columns_from_docfields(self): + """ + get columns from docfields and custom fields + """ + fields = self.meta.get_fieldnames_with_value(with_field_meta=True) + + # optional fields like _comments + if not self.meta.get("istable"): + for fieldname in xhiveframework.db.OPTIONAL_COLUMNS: + fields.append({"fieldname": fieldname, "fieldtype": "Text"}) + + # add _seen column if track_seen + if self.meta.get("track_seen"): + fields.append({"fieldname": "_seen", "fieldtype": "Text"}) + + for field in fields: + if field.get("is_virtual"): + continue + + self.columns[field.get("fieldname")] = DbColumn( + self, + field.get("fieldname"), + field.get("fieldtype"), + field.get("length"), + field.get("default"), + field.get("search_index"), + field.get("options"), + field.get("unique"), + field.get("precision"), + ) + + def validate(self): + """Check if change in varchar length isn't truncating the columns""" + if self.is_new(): + return + + self.setup_table_columns() + + columns = [ + xhiveframework._dict({"fieldname": f, "fieldtype": "Data"}) for f in xhiveframework.db.STANDARD_VARCHAR_COLUMNS + ] + if self.meta.get("istable"): + columns += [ + xhiveframework._dict({"fieldname": f, "fieldtype": "Data"}) for f in xhiveframework.db.CHILD_TABLE_COLUMNS + ] + columns += self.columns.values() + + for col in columns: + if len(col.fieldname) >= 64: + xhiveframework.throw( + _("Fieldname is limited to 64 characters ({0})").format(xhiveframework.bold(col.fieldname)) + ) + + if "varchar" in xhiveframework.db.type_map.get(col.fieldtype, ()): + # validate length range + new_length = cint(col.length) or cint(xhiveframework.db.VARCHAR_LEN) + if not (1 <= new_length <= 1000): + xhiveframework.throw(_("Length of {0} should be between 1 and 1000").format(col.fieldname)) + + current_col = self.current_columns.get(col.fieldname, {}) + if not current_col: + continue + current_type = self.current_columns[col.fieldname]["type"] + current_length = VARCHAR_CAST_PATTERN.findall(current_type) + if not current_length: + # case when the field is no longer a varchar + continue + current_length = current_length[0] + if cint(current_length) != cint(new_length): + try: + # check for truncation + max_length = xhiveframework.db.sql( + f"""SELECT MAX(CHAR_LENGTH(`{col.fieldname}`)) FROM `tab{self.doctype}`""" + ) + + except xhiveframework.db.InternalError as e: + if xhiveframework.db.is_missing_column(e): + # Unknown column 'column_name' in 'field list' + continue + raise + + if max_length and max_length[0][0] and max_length[0][0] > new_length: + if col.fieldname in self.columns: + self.columns[col.fieldname].length = current_length + info_message = _( + "Reverting length to {0} for '{1}' in '{2}'. Setting the length as {3} will cause truncation of data." + ).format(current_length, col.fieldname, self.doctype, new_length) + xhiveframework.msgprint(info_message) + + def is_new(self): + return self.table_name not in xhiveframework.db.get_tables() + + def setup_table_columns(self): + # TODO: figure out a way to get key data + for c in xhiveframework.db.get_table_columns_description(self.table_name): + self.current_columns[c.name.lower()] = c + + def alter(self): + pass + + +class DbColumn: + def __init__(self, table, fieldname, fieldtype, length, default, set_index, options, unique, precision): + self.table = table + self.fieldname = fieldname + self.fieldtype = fieldtype + self.length = length + self.set_index = set_index + self.default = default + self.options = options + self.unique = unique + self.precision = precision + + def get_definition(self, for_modification=False): + column_def = get_definition(self.fieldtype, precision=self.precision, length=self.length) + + if not column_def: + return column_def + + if self.fieldtype in ("Check", "Int"): + default_value = cint(self.default) or 0 + column_def += f" not null default {default_value}" + + elif self.fieldtype in ("Currency", "Float", "Percent"): + default_value = flt(self.default) or 0 + column_def += f" not null default {default_value}" + + elif ( + self.default + and (self.default not in xhiveframework.db.DEFAULT_SHORTCUTS) + and not cstr(self.default).startswith(":") + ): + column_def += f" default {xhiveframework.db.escape(self.default)}" + + if self.unique and not for_modification and (column_def not in ("text", "longtext")): + column_def += " unique" + + return column_def + + def build_for_alter_table(self, current_def): + column_type = get_definition(self.fieldtype, self.precision, self.length) + + # no columns + if not column_type: + return + + # to add? + if not current_def: + self.fieldname = validate_column_name(self.fieldname) + self.table.add_column.append(self) + + if column_type not in ("text", "longtext"): + if self.unique: + self.table.add_unique.append(self) + if self.set_index: + self.table.add_index.append(self) + return + + # type + if current_def["type"] != column_type: + self.table.change_type.append(self) + + # unique + if (self.unique and not current_def["unique"]) and column_type not in ("text", "longtext"): + self.table.add_unique.append(self) + elif (current_def["unique"] and not self.unique) and column_type not in ("text", "longtext"): + self.table.drop_unique.append(self) + + # default + if ( + self.default_changed(current_def) + and (self.default not in xhiveframework.db.DEFAULT_SHORTCUTS) + and not cstr(self.default).startswith(":") + ): + self.table.set_default.append(self) + + # index should be applied or dropped irrespective of type change + if (current_def["index"] and not self.set_index) and column_type not in ("text", "longtext"): + self.table.drop_index.append(self) + + elif (not current_def["index"] and self.set_index) and column_type not in ("text", "longtext"): + self.table.add_index.append(self) + + def default_changed(self, current_def): + if "decimal" in current_def["type"]: + return self.default_changed_for_decimal(current_def) + else: + cur_default = current_def["default"] + new_default = self.default + if cur_default == "NULL" or cur_default is None: + cur_default = None + else: + # Strip quotes from default value + # eg. database returns default value as "'System Manager'" + cur_default = cur_default.lstrip("'").rstrip("'") + + fieldtype = self.fieldtype + if fieldtype in ["Int", "Check"]: + cur_default = cint(cur_default) + new_default = cint(new_default) + elif fieldtype in ["Currency", "Float", "Percent"]: + cur_default = flt(cur_default) + new_default = flt(new_default) + return cur_default != new_default + + def default_changed_for_decimal(self, current_def): + try: + if current_def["default"] in ("", None) and self.default in ("", None): + # both none, empty + return False + + elif current_def["default"] in ("", None): + try: + # check if new default value is valid + float(self.default) + return True + except ValueError: + return False + + elif self.default in ("", None): + # new default value is empty + return True + + else: + # NOTE float() raise ValueError when "" or None is passed + return float(current_def["default"]) != float(self.default) + except TypeError: + return True + + +def validate_column_name(n): + if special_characters := SPECIAL_CHAR_PATTERN.findall(n): + special_characters = ", ".join(f'"{c}"' for c in special_characters) + xhiveframework.throw( + _("Fieldname {0} cannot have special characters like {1}").format( + xhiveframework.bold(cstr(n)), special_characters + ), + xhiveframework.db.InvalidColumnName, + ) + return n + + +def validate_column_length(fieldname): + if len(fieldname) > xhiveframework.db.MAX_COLUMN_LENGTH: + xhiveframework.throw(_("Fieldname is limited to 64 characters ({0})").format(fieldname)) + + +def get_definition(fieldtype, precision=None, length=None): + d = xhiveframework.db.type_map.get(fieldtype) + + if not d: + return + + if fieldtype == "Int" and length and length > 11: + # convert int to long int if the length of the int is greater than 11 + d = xhiveframework.db.type_map.get("Long Int") + + coltype = d[0] + size = d[1] if d[1] else None + + if size: + # This check needs to exist for backward compatibility. + # Till V13, default size used for float, currency and percent are (18, 6). + if fieldtype in ["Float", "Currency", "Percent"] and cint(precision) > 6: + size = "21,9" + + if length: + if coltype == "varchar": + size = length + elif coltype == "int" and length < 11: + # allow setting custom length for int if length provided is less than 11 + # NOTE: this will only be applicable for mariadb as xhiveframework implements int + # in postgres as bigint (as seen in type_map) + size = length + + if size is not None: + coltype = f"{coltype}({size})" + + return coltype + + +def add_column(doctype, column_name, fieldtype, precision=None, length=None, default=None, not_null=False): + xhiveframework.db.commit() + query = "alter table `tab{}` add column if not exists {} {}".format( + doctype, + column_name, + get_definition(fieldtype, precision, length), + ) + + if not_null: + query += " not null" + if default: + query += f" default '{default}'" + + xhiveframework.db.sql(query) diff --git a/xhiveframework/database/sequence.py b/xhiveframework/database/sequence.py new file mode 100644 index 0000000..3c83b6d --- /dev/null +++ b/xhiveframework/database/sequence.py @@ -0,0 +1,99 @@ +from xhiveframework import db, scrub + +# NOTE: +# FOR MARIADB - using no cache - as during backup, if the sequence was used in anyform, +# it drops the cache and uses the next non cached value in setval query and +# puts that in the backup file, which will start the counter +# from that value when inserting any new record in the doctype. +# By default the cache is 1000 which will mess up the sequence when +# using the system after a restore. +# +# Another case could be if the cached values expire then also there is a chance of +# the cache being skipped. +# +# FOR POSTGRES - The sequence cache for postgres is per connection. +# Since we're opening and closing connections for every request this results in skipping the cache +# to the next non-cached value hence not using cache in postgres. +# ref: https://stackoverflow.com/questions/21356375/postgres-9-0-4-sequence-skipping-numbers +SEQUENCE_CACHE = 0 + + +def create_sequence( + doctype_name: str, + *, + slug: str = "_id_seq", + temporary: bool = False, + check_not_exists: bool = False, + cycle: bool = False, + cache: int = SEQUENCE_CACHE, + start_value: int = 0, + increment_by: int = 0, + min_value: int = 0, + max_value: int = 0, +) -> str: + query = "create sequence" if not temporary else "create temporary sequence" + sequence_name = scrub(doctype_name + slug) + + if check_not_exists: + query += " if not exists" + + query += f" {sequence_name}" + + if increment_by: + # default is 1 + query += f" increment by {increment_by}" + + if min_value: + # default is 1 + query += f" minvalue {min_value}" + + if max_value: + query += f" maxvalue {max_value}" + + if start_value: + # default is 1 + query += f" start {start_value}" + + # in postgres, the default is cache 1 / no cache + if cache: + query += f" cache {cache}" + elif db.db_type == "mariadb": + query += " nocache" + + if not cycle: + # in postgres, default is no cycle + if db.db_type == "mariadb": + query += " nocycle" + else: + query += " cycle" + + db.sql_ddl(query) + + return sequence_name + + +def get_next_val(doctype_name: str, slug: str = "_id_seq") -> int: + sequence_name = scrub(f"{doctype_name}{slug}") + + if db.db_type == "postgres": + sequence_name = f"'\"{sequence_name}\"'" + elif db.db_type == "mariadb": + sequence_name = f"`{sequence_name}`" + + try: + return db.sql(f"SELECT nextval({sequence_name})")[0][0] + except IndexError: + raise db.SequenceGeneratorLimitExceeded + + +def set_next_val( + doctype_name: str, next_val: int, *, slug: str = "_id_seq", is_val_used: bool = False +) -> None: + is_val_used = "false" if not is_val_used else "true" + + db.multisql( + { + "postgres": f"SELECT SETVAL('\"{scrub(doctype_name + slug)}\"', {next_val}, {is_val_used})", + "mariadb": f"SELECT SETVAL(`{scrub(doctype_name + slug)}`, {next_val}, {is_val_used})", + } + ) diff --git a/xhiveframework/database/utils.py b/xhiveframework/database/utils.py new file mode 100644 index 0000000..6301e6d --- /dev/null +++ b/xhiveframework/database/utils.py @@ -0,0 +1,78 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import typing +from functools import cached_property +from types import NoneType + +import xhiveframework +from xhiveframework.query_builder.builder import MariaDB, Postgres +from xhiveframework.query_builder.functions import Function + +if typing.TYPE_CHECKING: + from xhiveframework.query_builder import DocType + +Query = str | MariaDB | Postgres +QueryValues = tuple | list | dict | NoneType + +EmptyQueryValues = object() +FallBackDateTimeStr = "0001-01-01 00:00:00.000000" +DefaultOrderBy = "KEEP_DEFAULT_ORDERING" +NestedSetHierarchy = ( + "ancestors of", + "descendants of", + "not ancestors of", + "not descendants of", + "descendants of (inclusive)", +) + + +def is_query_type(query: str, query_type: str | tuple[str]) -> bool: + return query.lstrip().split(maxsplit=1)[0].lower().startswith(query_type) + + +def is_pypika_function_object(field: str) -> bool: + return getattr(field, "__module__", None) == "pypika.functions" or isinstance(field, Function) + + +def get_doctype_name(table_name: str) -> str: + if table_name.startswith(("tab", "`tab", '"tab')): + table_name = table_name.replace("tab", "", 1) + table_name = table_name.replace("`", "") + return table_name.replace('"', "") + + +class LazyString: + def _setup(self) -> None: + raise NotImplementedError + + @cached_property + def value(self) -> str: + return self._setup() + + def __str__(self) -> str: + return self.value + + def __repr__(self) -> str: + return f"'{self.value}'" + + +class LazyDecode(LazyString): + __slots__ = () + + def __init__(self, value: str) -> None: + self._value = value + + def _setup(self) -> None: + return self._value.decode() + + +class LazyMogrify(LazyString): + __slots__ = () + + def __init__(self, query, values) -> None: + self.query = query + self.values = values + + def _setup(self) -> str: + return xhiveframework.db.mogrify(self.query, self.values) diff --git a/xhiveframework/defaults.py b/xhiveframework/defaults.py new file mode 100644 index 0000000..cd03ee4 --- /dev/null +++ b/xhiveframework/defaults.py @@ -0,0 +1,264 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.cache_manager import clear_defaults_cache, common_default_keys +from xhiveframework.query_builder import DocType + +# Note: DefaultValue records are identified by parent (e.g. __default, __global) + + +def set_user_default(key, value, user=None, parenttype=None): + set_default(key, value, user or xhiveframework.session.user, parenttype) + + +def add_user_default(key, value, user=None, parenttype=None): + add_default(key, value, user or xhiveframework.session.user, parenttype) + + +def get_user_default(key, user=None): + user_defaults = get_defaults(user or xhiveframework.session.user) + d = user_defaults.get(key, None) + + if is_a_user_permission_key(key): + if d and isinstance(d, list | tuple) and len(d) == 1: + # Use User Permission value when only when it has a single value + d = d[0] + else: + d = user_defaults.get(xhiveframework.scrub(key), None) + user_permission_default = get_user_permission_default(key, user_defaults) + if not d: + # If no default value is found, use the User Permission value + d = user_permission_default + + value = isinstance(d, list | tuple) and d[0] or d + if not_in_user_permission(key, value, user): + return + + return value + + +def get_user_permission_default(key, defaults): + permissions = get_user_permissions() + user_default = "" + if permissions.get(key): + # global default in user permission + for item in permissions.get(key): + doc = item.get("doc") + if defaults.get(key) == doc: + user_default = doc + + for item in permissions.get(key): + if item.get("is_default"): + user_default = item.get("doc") + break + + return user_default + + +def get_user_default_as_list(key, user=None): + user_defaults = get_defaults(user or xhiveframework.session.user) + d = user_defaults.get(key, None) + + if is_a_user_permission_key(key): + if d and isinstance(d, list | tuple) and len(d) == 1: + # Use User Permission value when only when it has a single value + d = [d[0]] + + else: + d = user_defaults.get(xhiveframework.scrub(key), None) + + d = list(filter(None, (not isinstance(d, list | tuple)) and [d] or d)) + + # filter default values if not found in user permission + return [value for value in d if not not_in_user_permission(key, value)] + + +def is_a_user_permission_key(key): + return ":" not in key and key != xhiveframework.scrub(key) + + +def not_in_user_permission(key, value, user=None): + # returns true or false based on if value exist in user permission + user = user or xhiveframework.session.user + user_permission = get_user_permissions(user).get(xhiveframework.unscrub(key)) or [] + + for perm in user_permission: + # doc found in user permission + if perm.get("doc") == value: + return False + + # return true only if user_permission exists + return True if user_permission else False + + +def get_user_permissions(user=None): + from xhiveframework.core.doctype.user_permission.user_permission import ( + get_user_permissions as _get_user_permissions, + ) + + """Return xhiveframework.core.doctype.user_permissions.user_permissions._get_user_permissions (kept for backward compatibility)""" + return _get_user_permissions(user) + + +def get_defaults(user=None): + global_defaults = get_defaults_for() + + if not user: + user = xhiveframework.session.user if xhiveframework.session else "Guest" + + if not user: + return global_defaults + + defaults = global_defaults.copy() + defaults.update(get_defaults_for(user)) + defaults.update(user=user, owner=user) + + return defaults + + +def clear_user_default(key, user=None): + clear_default(key, parent=user or xhiveframework.session.user) + + +# Global + + +def set_global_default(key, value): + set_default(key, value, "__default") + + +def add_global_default(key, value): + add_default(key, value, "__default") + + +def get_global_default(key): + d = get_defaults().get(key, None) + + value = isinstance(d, list | tuple) and d[0] or d + if not_in_user_permission(key, value): + return + + return value + + +# Common + + +def set_default(key, value, parent, parenttype="__default"): + """Override or add a default value. + Adds default value in table `tabDefaultValue`. + + :param key: Default key. + :param value: Default value. + :param parent: Usually, **User** to whom the default belongs. + :param parenttype: [optional] default is `__default`.""" + table = DocType("DefaultValue") + key_exists = ( + xhiveframework.qb.from_(table) + .where((table.defkey == key) & (table.parent == parent)) + .select(table.defkey) + .for_update() + .run() + ) + if key_exists: + xhiveframework.db.delete("DefaultValue", {"defkey": key, "parent": parent}) + if value is not None: + add_default(key, value, parent) + else: + _clear_cache(parent) + + +def add_default(key, value, parent, parenttype=None): + d = xhiveframework.get_doc( + { + "doctype": "DefaultValue", + "parent": parent, + "parenttype": parenttype or "__default", + "parentfield": "system_defaults", + "defkey": key, + "defvalue": value, + } + ) + d.insert(ignore_permissions=True) + _clear_cache(parent) + + +def clear_default(key=None, value=None, parent=None, name=None, parenttype=None): + """Clear a default value by any of the given parameters and delete caches. + + :param key: Default key. + :param value: Default value. + :param parent: User name, or `__global`, `__default`. + :param name: Default ID. + :param parenttype: Clear defaults table for a particular type e.g. **User**. + """ + filters = {} + + if name: + filters.update({"name": name}) + + else: + if key: + filters.update({"defkey": key}) + + if value: + filters.update({"defvalue": value}) + + if parent: + filters.update({"parent": parent}) + + if parenttype: + filters.update({"parenttype": parenttype}) + + if parent: + clear_defaults_cache(parent) + else: + clear_defaults_cache("__default") + clear_defaults_cache("__global") + + if not filters: + raise Exception("[clear_default] No key specified.") + + xhiveframework.db.delete("DefaultValue", filters) + + _clear_cache(parent) + + +def get_defaults_for(parent="__default"): + """get all defaults""" + defaults = xhiveframework.cache.hget("defaults", parent) + + if defaults is None: + # sort descending because first default must get precedence + table = DocType("DefaultValue") + res = ( + xhiveframework.qb.from_(table) + .where(table.parent == parent) + .select(table.defkey, table.defvalue) + .orderby("creation") + .run(as_dict=True) + ) + + defaults = xhiveframework._dict() + for d in res: + if d.defkey in defaults: + # listify + if not isinstance(defaults[d.defkey], list) and defaults[d.defkey] != d.defvalue: + defaults[d.defkey] = [defaults[d.defkey]] + + if d.defvalue not in defaults[d.defkey]: + defaults[d.defkey].append(d.defvalue) + + elif d.defvalue is not None: + defaults[d.defkey] = d.defvalue + + xhiveframework.cache.hset("defaults", parent, defaults) + + return defaults + + +def _clear_cache(parent): + if xhiveframework.flags.in_install: + return + xhiveframework.clear_cache(user=parent if parent not in common_default_keys else None) diff --git a/xhiveframework/deferred_insert.py b/xhiveframework/deferred_insert.py new file mode 100644 index 0000000..73eac4d --- /dev/null +++ b/xhiveframework/deferred_insert.py @@ -0,0 +1,59 @@ +import json +from typing import TYPE_CHECKING, Union + +import redis + +import xhiveframework +from xhiveframework.utils import cstr + +if TYPE_CHECKING: + from xhiveframework.model.document import Document + +queue_prefix = "insert_queue_for_" + + +def deferred_insert(doctype: str, records: list[Union[dict, "Document"]] | str): + if isinstance(records, dict | list): + _records = json.dumps(records) + else: + _records = records + + try: + xhiveframework.cache.rpush(f"{queue_prefix}{doctype}", _records) + except redis.exceptions.ConnectionError: + for record in records: + insert_record(record, doctype) + + +def save_to_db(): + queue_keys = xhiveframework.cache.get_keys(queue_prefix) + for key in queue_keys: + record_count = 0 + queue_key = get_key_name(key) + doctype = get_doctype_name(key) + while xhiveframework.cache.llen(queue_key) > 0 and record_count <= 500: + records = xhiveframework.cache.lpop(queue_key) + records = json.loads(records.decode("utf-8")) + if isinstance(records, dict): + record_count += 1 + insert_record(records, doctype) + continue + for record in records: + record_count += 1 + insert_record(record, doctype) + + +def insert_record(record: Union[dict, "Document"], doctype: str): + try: + record.update({"doctype": doctype}) + xhiveframework.get_doc(record).insert() + except Exception as e: + xhiveframework.logger().error(f"Error while inserting deferred {doctype} record: {e}") + + +def get_key_name(key: str) -> str: + return cstr(key).split("|")[1] + + +def get_doctype_name(key: str) -> str: + return cstr(key).split(queue_prefix)[1] diff --git a/xhiveframework/desk/__init__.py b/xhiveframework/desk/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/desk/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/desk/calendar.py b/xhiveframework/desk/calendar.py new file mode 100644 index 0000000..994a10f --- /dev/null +++ b/xhiveframework/desk/calendar.py @@ -0,0 +1,57 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ + + +@xhiveframework.whitelist() +def update_event(args, field_map): + """Updates Event (called via calendar) based on passed `field_map`""" + args = xhiveframework._dict(json.loads(args)) + field_map = xhiveframework._dict(json.loads(field_map)) + w = xhiveframework.get_doc(args.doctype, args.name) + w.set(field_map.start, args[field_map.start]) + w.set(field_map.end, args.get(field_map.end)) + w.save() + + +def get_event_conditions(doctype, filters=None): + """Returns SQL conditions with user permissions and filters for event queries""" + from xhiveframework.desk.reportview import get_filters_cond + + if not xhiveframework.has_permission(doctype): + xhiveframework.throw(_("Not Permitted"), xhiveframework.PermissionError) + + return get_filters_cond(doctype, filters, [], with_match_conditions=True) + + +@xhiveframework.whitelist() +def get_events(doctype, start, end, field_map, filters=None, fields=None): + field_map = xhiveframework._dict(json.loads(field_map)) + fields = xhiveframework.parse_json(fields) + + doc_meta = xhiveframework.get_meta(doctype) + for d in doc_meta.fields: + if d.fieldtype == "Color": + field_map.update({"color": d.fieldname}) + + filters = json.loads(filters) if filters else [] + + if not fields: + fields = [field_map.start, field_map.end, field_map.title, "name"] + + if field_map.color: + fields.append(field_map.color) + + start_date = "ifnull(%s, '0001-01-01 00:00:00')" % field_map.start + end_date = "ifnull(%s, '2199-12-31 00:00:00')" % field_map.end + + filters += [ + [doctype, start_date, "<=", end], + [doctype, end_date, ">=", start], + ] + fields = list({field for field in fields if field}) + return xhiveframework.get_list(doctype, fields=fields, filters=filters) diff --git a/xhiveframework/desk/desk_page.py b/xhiveframework/desk/desk_page.py new file mode 100644 index 0000000..0de2ad2 --- /dev/null +++ b/xhiveframework/desk/desk_page.py @@ -0,0 +1,33 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +@xhiveframework.whitelist() +def get(name): + """ + Return the :term:`doclist` of the `Page` specified by `name` + """ + page = xhiveframework.get_doc("Page", name) + if page.is_permitted(): + page.load_assets() + docs = xhiveframework._dict(page.as_dict()) + if getattr(page, "_dynamic_page", None): + docs["_dynamic_page"] = 1 + + return docs + else: + xhiveframework.response["403"] = 1 + raise xhiveframework.PermissionError("No read permission for Page %s" % (page.title or name)) + + +@xhiveframework.whitelist(allow_guest=True) +def getpage(): + """ + Load the page from `xhiveframework.form` and send it via `xhiveframework.response` + """ + page = xhiveframework.form_dict.get("name") + doc = get(page) + + xhiveframework.response.docs.append(doc) diff --git a/xhiveframework/desk/desktop.py b/xhiveframework/desk/desktop.py new file mode 100644 index 0000000..7e2c8cd --- /dev/null +++ b/xhiveframework/desk/desktop.py @@ -0,0 +1,668 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +# Author - Shivam Mishra + +from functools import wraps +from json import dumps, loads + +import xhiveframework +from xhiveframework import DoesNotExistError, ValidationError, _, _dict +from xhiveframework.boot import get_allowed_pages, get_allowed_reports +from xhiveframework.cache_manager import ( + build_domain_restriced_doctype_cache, + build_domain_restriced_page_cache, + build_table_count_cache, +) +from xhiveframework.core.doctype.custom_role.custom_role import get_custom_allowed_roles + + +def handle_not_exist(fn): + @wraps(fn) + def wrapper(*args, **kwargs): + try: + return fn(*args, **kwargs) + except DoesNotExistError: + xhiveframework.clear_last_message() + return [] + + return wrapper + + +class Workspace: + def __init__(self, page, minimal=False): + self.page_name = page.get("name") + self.page_title = page.get("title") + self.public_page = page.get("public") + self.workspace_manager = "Workspace Manager" in xhiveframework.get_roles() + + self.user = xhiveframework.get_user() + self.allowed_modules = self.get_cached("user_allowed_modules", self.get_allowed_modules) + + self.doc = xhiveframework.get_cached_doc("Workspace", self.page_name) + if ( + self.doc + and self.doc.module + and self.doc.module not in self.allowed_modules + and not self.workspace_manager + ): + raise xhiveframework.PermissionError + + self.can_read = self.get_cached("user_perm_can_read", self.get_can_read_items) + + self.allowed_pages = get_allowed_pages(cache=True) + self.allowed_reports = get_allowed_reports(cache=True) + + if not minimal: + if self.doc.content: + self.onboarding_list = [ + x["data"]["onboarding_name"] for x in loads(self.doc.content) if x["type"] == "onboarding" + ] + self.onboardings = [] + + self.table_counts = get_table_with_counts() + self.restricted_doctypes = ( + xhiveframework.cache.get_value("domain_restricted_doctypes") or build_domain_restriced_doctype_cache() + ) + self.restricted_pages = ( + xhiveframework.cache.get_value("domain_restricted_pages") or build_domain_restriced_page_cache() + ) + + def is_permitted(self): + """Returns true if Has Role is not set or the user is allowed.""" + from xhiveframework.utils import has_common + + allowed = [d.role for d in self.doc.roles] + + custom_roles = get_custom_allowed_roles("page", self.doc.name) + allowed.extend(custom_roles) + + if not allowed: + return True + + roles = xhiveframework.get_roles() + + if has_common(roles, allowed): + return True + + def get_cached(self, cache_key, fallback_fn): + value = xhiveframework.cache.get_value(cache_key, user=xhiveframework.session.user) + if value: + return value + + value = fallback_fn() + + # Expire every six hour + xhiveframework.cache.set_value(cache_key, value, xhiveframework.session.user, 21600) + return value + + def get_can_read_items(self): + if not self.user.can_read: + self.user.build_permissions() + + return self.user.can_read + + def get_allowed_modules(self): + if not self.user.allow_modules: + self.user.build_permissions() + + return self.user.allow_modules + + def get_onboarding_doc(self, onboarding): + # Check if onboarding is enabled + if not xhiveframework.get_system_settings("enable_onboarding"): + return None + + if not self.onboarding_list: + return None + + if xhiveframework.db.get_value("Module Onboarding", onboarding, "is_complete"): + return None + + doc = xhiveframework.get_doc("Module Onboarding", onboarding) + + # Check if user is allowed + allowed_roles = set(doc.get_allowed_roles()) + user_roles = set(xhiveframework.get_roles()) + if not allowed_roles & user_roles: + return None + + # Check if already complete + if doc.check_completion(): + return None + + return doc + + def is_item_allowed(self, name, item_type): + if xhiveframework.session.user == "Administrator": + return True + + item_type = item_type.lower() + + if item_type == "doctype": + return name in self.can_read or [] and name in self.restricted_doctypes or [] + if item_type == "page": + return name in self.allowed_pages and name in self.restricted_pages + if item_type == "report": + return name in self.allowed_reports + if item_type == "help": + return True + if item_type == "dashboard": + return True + if item_type == "url": + return True + + return False + + def build_workspace(self): + self.cards = {"items": self.get_links()} + self.charts = {"items": self.get_charts()} + self.shortcuts = {"items": self.get_shortcuts()} + self.onboardings = {"items": self.get_onboardings()} + self.quick_lists = {"items": self.get_quick_lists()} + self.number_cards = {"items": self.get_number_cards()} + self.custom_blocks = {"items": self.get_custom_blocks()} + + def _doctype_contains_a_record(self, name): + exists = self.table_counts.get(name, False) + + if not exists and xhiveframework.db.exists(name): + if not xhiveframework.db.get_value("DocType", name, "issingle"): + exists = bool(xhiveframework.get_all(name, limit=1)) + else: + exists = True + self.table_counts[name] = exists + + return exists + + def _prepare_item(self, item): + if item.dependencies: + dependencies = [dep.strip() for dep in item.dependencies.split(",")] + + incomplete_dependencies = [d for d in dependencies if not self._doctype_contains_a_record(d)] + + if len(incomplete_dependencies): + item.incomplete_dependencies = incomplete_dependencies + else: + item.incomplete_dependencies = "" + + if item.onboard: + # Mark Spotlights for initial + if item.get("type") == "doctype": + name = item.get("name") + count = self._doctype_contains_a_record(name) + + item["count"] = count + + if item.get("link_type") == "DocType": + item["description"] = xhiveframework.get_meta(item.link_to).description + + # Translate label + item["label"] = _(item.label) if item.label else _(item.name) + + return item + + def is_custom_block_permitted(self, custom_block_name): + from xhiveframework.utils import has_common + + allowed = [ + d.role for d in xhiveframework.get_all("Has Role", fields=["role"], filters={"parent": custom_block_name}) + ] + + if not allowed: + return True + + roles = xhiveframework.get_roles() + + if has_common(roles, allowed): + return True + + return False + + @handle_not_exist + def get_links(self): + cards = self.doc.get_link_groups() + + if not self.doc.hide_custom: + cards = cards + get_custom_reports_and_doctypes(self.doc.module) + + default_country = xhiveframework.db.get_default("country") + + new_data = [] + for card in cards: + new_items = [] + card = _dict(card) + + links = card.get("links", []) + + for item in links: + item = _dict(item) + + # Condition: based on country + if item.country and item.country != default_country: + continue + + # Check if user is allowed to view + if self.is_item_allowed(item.link_to, item.link_type): + prepared_item = self._prepare_item(item) + new_items.append(prepared_item) + + if new_items: + if isinstance(card, _dict): + new_card = card.copy() + else: + new_card = card.as_dict().copy() + new_card["links"] = new_items + new_card["label"] = _(new_card["label"]) + new_data.append(new_card) + + return new_data + + @handle_not_exist + def get_charts(self): + all_charts = [] + if xhiveframework.has_permission("Dashboard Chart", throw=False): + charts = self.doc.charts + + for chart in charts: + if xhiveframework.has_permission("Dashboard Chart", doc=chart.chart_name): + # Translate label + chart.label = _(chart.label) if chart.label else _(chart.chart_name) + all_charts.append(chart) + + return all_charts + + @handle_not_exist + def get_shortcuts(self): + def _in_active_domains(item): + if not item.restrict_to_domain: + return True + else: + return item.restrict_to_domain in xhiveframework.get_active_domains() + + items = [] + shortcuts = self.doc.shortcuts + + for item in shortcuts: + new_item = item.as_dict().copy() + if self.is_item_allowed(item.link_to, item.type) and _in_active_domains(item): + if item.type == "Report": + report = self.allowed_reports.get(item.link_to, {}) + if report.get("report_type") in ["Query Report", "Script Report", "Custom Report"]: + new_item["is_query_report"] = 1 + else: + new_item["ref_doctype"] = report.get("ref_doctype") + + # Translate label + new_item["label"] = _(item.label) if item.label else _(item.link_to) + + items.append(new_item) + + return items + + @handle_not_exist + def get_quick_lists(self): + items = [] + quick_lists = self.doc.quick_lists + + for item in quick_lists: + if self.is_item_allowed(item.document_type, "doctype"): + new_item = item.as_dict().copy() + + # Translate label + new_item["label"] = _(item.label) if item.label else _(item.document_type) + + items.append(new_item) + + return items + + @handle_not_exist + def get_onboardings(self): + if self.onboarding_list: + for onboarding in self.onboarding_list: + onboarding_doc = self.get_onboarding_doc(onboarding) + if onboarding_doc: + item = { + "label": _(onboarding), + "title": _(onboarding_doc.title), + "subtitle": _(onboarding_doc.subtitle), + "success": _(onboarding_doc.success_message), + "docs_url": onboarding_doc.documentation_url, + "items": self.get_onboarding_steps(onboarding_doc), + } + self.onboardings.append(item) + return self.onboardings + + @handle_not_exist + def get_onboarding_steps(self, onboarding_doc): + steps = [] + for doc in onboarding_doc.get_steps(): + step = doc.as_dict().copy() + step.label = _(doc.title) + if step.action == "Create Entry": + step.is_submittable = xhiveframework.db.get_value( + "DocType", step.reference_document, "is_submittable", cache=True + ) + steps.append(step) + + return steps + + @handle_not_exist + def get_number_cards(self): + all_number_cards = [] + if xhiveframework.has_permission("Number Card", throw=False): + number_cards = self.doc.number_cards + for number_card in number_cards: + if xhiveframework.has_permission("Number Card", doc=number_card.number_card_name): + # Translate label + number_card.label = ( + _(number_card.label) if number_card.label else _(number_card.number_card_name) + ) + all_number_cards.append(number_card) + + return all_number_cards + + @handle_not_exist + def get_custom_blocks(self): + all_custom_blocks = [] + if xhiveframework.has_permission("Custom HTML Block", throw=False): + custom_blocks = self.doc.custom_blocks + + for custom_block in custom_blocks: + if xhiveframework.has_permission("Custom HTML Block", doc=custom_block.custom_block_name): + if not self.is_custom_block_permitted(custom_block.custom_block_name): + continue + + # Translate label + custom_block.label = ( + _(custom_block.label) if custom_block.label else _(custom_block.custom_block_name) + ) + all_custom_blocks.append(custom_block) + + return all_custom_blocks + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_desktop_page(page): + """Applies permissions, customizations and returns the configruration for a page + on desk. + + Args: + page (json): page data + + Returns: + dict: dictionary of cards, charts and shortcuts to be displayed on website + """ + try: + workspace = Workspace(loads(page)) + workspace.build_workspace() + return { + "charts": workspace.charts, + "shortcuts": workspace.shortcuts, + "cards": workspace.cards, + "onboardings": workspace.onboardings, + "quick_lists": workspace.quick_lists, + "number_cards": workspace.number_cards, + "custom_blocks": workspace.custom_blocks, + } + except DoesNotExistError: + xhiveframework.log_error("Workspace Missing") + return {} + + +@xhiveframework.whitelist() +def get_workspace_sidebar_items(): + """Get list of sidebar items for desk""" + has_access = "Workspace Manager" in xhiveframework.get_roles() + + # don't get domain restricted pages + blocked_modules = xhiveframework.get_doc("User", xhiveframework.session.user).get_blocked_modules() + blocked_modules.append("Dummy Module") + + # adding None to allowed_domains to include pages without domain restriction + allowed_domains = [None, *xhiveframework.get_active_domains()] + + filters = { + "restrict_to_domain": ["in", allowed_domains], + "module": ["not in", blocked_modules], + } + + if has_access: + filters = [] + + # pages sorted based on sequence id + order_by = "sequence_id asc" + fields = [ + "name", + "title", + "for_user", + "parent_page", + "content", + "public", + "module", + "icon", + "indicator_color", + "is_hidden", + ] + all_pages = xhiveframework.get_all( + "Workspace", fields=fields, filters=filters, order_by=order_by, ignore_permissions=True + ) + pages = [] + private_pages = [] + + # Filter Page based on Permission + for page in all_pages: + try: + workspace = Workspace(page, True) + if has_access or workspace.is_permitted(): + if page.public and (has_access or not page.is_hidden) and page.title != "Welcome Workspace": + pages.append(page) + elif page.for_user == xhiveframework.session.user: + private_pages.append(page) + page["label"] = _(page.get("name")) + except xhiveframework.PermissionError: + pass + if private_pages: + pages.extend(private_pages) + + if len(pages) == 0: + pages = [xhiveframework.get_doc("Workspace", "Welcome Workspace").as_dict()] + pages[0]["label"] = _("Welcome Workspace") + + return {"pages": pages, "has_access": has_access} + + +def get_table_with_counts(): + counts = xhiveframework.cache.get_value("information_schema:counts") + if not counts: + counts = build_table_count_cache() + + return counts + + +def get_custom_reports_and_doctypes(module): + return [ + _dict({"label": _("Custom Documents"), "links": get_custom_doctype_list(module)}), + _dict({"label": _("Custom Reports"), "links": get_custom_report_list(module)}), + ] + + +def get_custom_doctype_list(module): + doctypes = xhiveframework.get_all( + "DocType", + fields=["name"], + filters={"custom": 1, "istable": 0, "module": module}, + order_by="name", + ) + + return [ + { + "type": "Link", + "link_type": "doctype", + "link_to": d.name, + "label": _(d.name), + } + for d in doctypes + ] + + +def get_custom_report_list(module): + """Returns list on new style reports for modules.""" + reports = xhiveframework.get_all( + "Report", + fields=["name", "ref_doctype", "report_type"], + filters={"is_standard": "No", "disabled": 0, "module": module}, + order_by="name", + ) + + return [ + { + "type": "Link", + "link_type": "report", + "doctype": r.ref_doctype, + "dependencies": r.ref_doctype, + "is_query_report": 1 + if r.report_type in ("Query Report", "Script Report", "Custom Report") + else 0, + "label": _(r.name), + "link_to": r.name, + } + for r in reports + ] + + +def save_new_widget(doc, page, blocks, new_widgets): + if loads(new_widgets): + widgets = _dict(loads(new_widgets)) + + if widgets.chart: + doc.charts.extend(new_widget(widgets.chart, "Workspace Chart", "charts")) + if widgets.shortcut: + doc.shortcuts.extend(new_widget(widgets.shortcut, "Workspace Shortcut", "shortcuts")) + if widgets.quick_list: + doc.quick_lists.extend(new_widget(widgets.quick_list, "Workspace Quick List", "quick_lists")) + if widgets.custom_block: + doc.custom_blocks.extend( + new_widget(widgets.custom_block, "Workspace Custom Block", "custom_blocks") + ) + if widgets.number_card: + doc.number_cards.extend(new_widget(widgets.number_card, "Workspace Number Card", "number_cards")) + if widgets.card: + doc.build_links_table_from_card(widgets.card) + + # remove duplicate and unwanted widgets + clean_up(doc, blocks) + + try: + doc.save(ignore_permissions=True) + except (ValidationError, TypeError) as e: + # Create a json string to log + json_config = widgets and dumps(widgets, sort_keys=True, indent=4) + + # Error log body + log = f""" + page: {page} + config: {json_config} + exception: {e} + """ + doc.log_error("Could not save customization", log) + return False + + return True + + +def clean_up(original_page, blocks): + page_widgets = {} + + for wid in ["shortcut", "card", "chart", "quick_list", "number_card", "custom_block"]: + # get list of widget's name from blocks + page_widgets[wid] = [x["data"][wid + "_name"] for x in loads(blocks) if x["type"] == wid] + + # shortcut, chart, quick_list, number_card & custom_block cleanup + for wid in ["shortcut", "chart", "quick_list", "number_card", "custom_block"]: + updated_widgets = [] + original_page.get(wid + "s").reverse() + + for w in original_page.get(wid + "s"): + if w.label in page_widgets[wid] and w.label not in [x.label for x in updated_widgets]: + updated_widgets.append(w) + original_page.set(wid + "s", updated_widgets) + + # card cleanup + for i, v in enumerate(original_page.links): + if v.type == "Card Break" and v.label not in page_widgets["card"]: + del original_page.links[i : i + v.link_count + 1] + + +def new_widget(config, doctype, parentfield): + if not config: + return [] + prepare_widget_list = [] + for idx, widget in enumerate(config): + # Some cleanup + widget.pop("name", None) + + # New Doc + doc = xhiveframework.new_doc(doctype) + doc.update(widget) + + # Manually Set IDX + doc.idx = idx + 1 + + # Set Parent Field + doc.parentfield = parentfield + + prepare_widget_list.append(doc) + return prepare_widget_list + + +def prepare_widget(config, doctype, parentfield): + """Create widget child table entries with parent details + + Args: + config (dict): Dictionary containing widget config + doctype (string): Doctype name of the child table + parentfield (string): Parent field for the child table + + Returns: + TYPE: List of Document objects + """ + if not config: + return [] + order = config.get("order") + widgets = config.get("widgets") + prepare_widget_list = [] + for idx, name in enumerate(order): + wid_config = widgets[name].copy() + # Some cleanup + wid_config.pop("name", None) + + # New Doc + doc = xhiveframework.new_doc(doctype) + doc.update(wid_config) + + # Manually Set IDX + doc.idx = idx + 1 + + # Set Parent Field + doc.parentfield = parentfield + + prepare_widget_list.append(doc) + return prepare_widget_list + + +@xhiveframework.whitelist() +def update_onboarding_step(name, field, value): + """Update status of onboaridng step + + Args: + name (string): Name of the doc + field (string): field to be updated + value: Value to be updated + + """ + from xhiveframework.utils.telemetry import capture + + xhiveframework.db.set_value("Onboarding Step", name, field, value) + + capture(xhiveframework.scrub(name), app="xhiveframework_onboarding", properties={field: value}) diff --git a/xhiveframework/desk/doctype/__init__.py b/xhiveframework/desk/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/bulk_update/__init__.py b/xhiveframework/desk/doctype/bulk_update/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/bulk_update/bulk_update.js b/xhiveframework/desk/doctype/bulk_update/bulk_update.js new file mode 100644 index 0000000..eab9201 --- /dev/null +++ b/xhiveframework/desk/doctype/bulk_update/bulk_update.js @@ -0,0 +1,58 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Bulk Update", { + refresh: function (frm) { + frm.set_query("document_type", function () { + return { + filters: [ + ["DocType", "issingle", "=", 0], + ["DocType", "name", "not in", xhiveframework.model.core_doctypes_list], + ], + }; + }); + + frm.page.set_primary_action(__("Update"), function () { + if (!frm.doc.update_value) { + xhiveframework.throw(__('Field "value" is mandatory. Please specify value to be updated')); + } else { + frm.call("bulk_update").then((r) => { + let failed = r.message; + if (!failed) failed = []; + + if (failed.length && !r._server_messages) { + xhiveframework.throw( + __("Cannot update {0}", [ + failed.map((f) => (f.bold ? f.bold() : f)).join(", "), + ]) + ); + } else { + xhiveframework.msgprint({ + title: __("Success"), + message: __("Updated Successfully"), + indicator: "green", + }); + } + + xhiveframework.hide_progress(); + frm.save(); + }); + } + }); + }, + + document_type: function (frm) { + // set field options + if (!frm.doc.document_type) return; + + xhiveframework.model.with_doctype(frm.doc.document_type, function () { + var options = $.map(xhiveframework.get_meta(frm.doc.document_type).fields, function (d) { + if (d.fieldname && xhiveframework.model.no_value_type.indexOf(d.fieldtype) === -1) { + return d.fieldname; + } + return null; + }); + frm.set_df_property("field", "options", options); + }); + }, +}); diff --git a/xhiveframework/desk/doctype/bulk_update/bulk_update.json b/xhiveframework/desk/doctype/bulk_update/bulk_update.json new file mode 100644 index 0000000..9345851 --- /dev/null +++ b/xhiveframework/desk/doctype/bulk_update/bulk_update.json @@ -0,0 +1,77 @@ +{ + "actions": [], + "creation": "2016-07-15 05:51:29.224123", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "field", + "update_value", + "condition", + "limit" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Field", + "reqd": 1 + }, + { + "fieldname": "update_value", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Update Value", + "reqd": 1 + }, + { + "bold": 1, + "description": "SQL Conditions. Example: status=\"Open\"", + "fieldname": "condition", + "fieldtype": "Small Text", + "label": "Condition" + }, + { + "bold": 1, + "default": "500", + "description": "Max 500 records at a time", + "fieldname": "limit", + "fieldtype": "Int", + "label": "Limit" + } + ], + "issingle": 1, + "links": [], + "modified": "2022-08-03 12:20:50.742376", + "modified_by": "Administrator", + "module": "Desk", + "name": "Bulk Update", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/bulk_update/bulk_update.py b/xhiveframework/desk/doctype/bulk_update/bulk_update.py new file mode 100644 index 0000000..1d6fc8d --- /dev/null +++ b/xhiveframework/desk/doctype/bulk_update/bulk_update.py @@ -0,0 +1,118 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.doctype.submission_queue.submission_queue import queue_submission +from xhiveframework.model.document import Document +from xhiveframework.utils import cint +from xhiveframework.utils.deprecations import deprecated +from xhiveframework.utils.scheduler import is_scheduler_inactive + + +class BulkUpdate(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + condition: DF.SmallText | None + document_type: DF.Link + field: DF.Literal[None] + limit: DF.Int + update_value: DF.SmallText + + # end: auto-generated types + + @xhiveframework.whitelist() + def bulk_update(self): + self.check_permission("write") + limit = self.limit if self.limit and cint(self.limit) < 500 else 500 + + condition = "" + if self.condition: + if ";" in self.condition: + xhiveframework.throw(_("; not allowed in condition")) + + condition = f" where {self.condition}" + + docnames = xhiveframework.db.sql_list( + f"""select name from `tab{self.document_type}`{condition} limit {limit} offset 0""" + ) + return submit_cancel_or_update_docs( + self.document_type, docnames, "update", {self.field: self.update_value} + ) + + +@xhiveframework.whitelist() +def submit_cancel_or_update_docs(doctype, docnames, action="submit", data=None, task_id=None): + if isinstance(docnames, str): + docnames = xhiveframework.parse_json(docnames) + + if len(docnames) < 20: + return _bulk_action(doctype, docnames, action, data, task_id) + elif len(docnames) <= 500: + xhiveframework.msgprint(_("Bulk operation is enqueued in background."), alert=True) + xhiveframework.enqueue( + _bulk_action, + doctype=doctype, + docnames=docnames, + action=action, + data=data, + task_id=task_id, + queue="short", + timeout=1000, + ) + else: + xhiveframework.throw(_("Bulk operations only support up to 500 documents."), title=_("Too Many Documents")) + + +def _bulk_action(doctype, docnames, action, data, task_id=None): + if data: + data = xhiveframework.parse_json(data) + + failed = [] + num_documents = len(docnames) + + for idx, docname in enumerate(docnames, 1): + doc = xhiveframework.get_doc(doctype, docname) + try: + message = "" + if action == "submit" and doc.docstatus.is_draft(): + if doc.meta.queue_in_background and not is_scheduler_inactive(): + queue_submission(doc, action) + message = _("Queuing {0} for Submission").format(doctype) + else: + doc.submit() + message = _("Submitting {0}").format(doctype) + elif action == "cancel" and doc.docstatus.is_submitted(): + doc.cancel() + message = _("Cancelling {0}").format(doctype) + elif action == "update" and not doc.docstatus.is_cancelled(): + doc.update(data) + doc.save() + message = _("Updating {0}").format(doctype) + else: + failed.append(docname) + xhiveframework.db.commit() + xhiveframework.publish_progress( + percent=idx / num_documents * 100, + title=message, + description=docname, + task_id=task_id, + ) + + except Exception: + failed.append(docname) + xhiveframework.db.rollback() + + return failed + + +@deprecated +def show_progress(docnames, message, i, description): + n = len(docnames) + xhiveframework.publish_progress(float(i) * 100 / n, title=message, description=description) diff --git a/xhiveframework/desk/doctype/bulk_update/test_bulk_update.py b/xhiveframework/desk/doctype/bulk_update/test_bulk_update.py new file mode 100644 index 0000000..5ef08c2 --- /dev/null +++ b/xhiveframework/desk/doctype/bulk_update/test_bulk_update.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See LICENSE + +import time + +import xhiveframework +from xhiveframework.core.doctype.doctype.test_doctype import new_doctype +from xhiveframework.desk.doctype.bulk_update.bulk_update import submit_cancel_or_update_docs +from xhiveframework.tests.utils import XhiveFrameworkTestCase, timeout + + +class TestBulkUpdate(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls) -> None: + super().setUpClass() + cls.doctype = new_doctype(is_submittable=1, custom=1).insert().name + xhiveframework.db.commit() + for _ in range(50): + xhiveframework.new_doc(cls.doctype, some_fieldname=xhiveframework.mock("name")).insert() + + @timeout() + def wait_for_assertion(self, assertion): + """Wait till an assertion becomes True""" + while True: + if assertion(): + break + time.sleep(0.2) + + def test_bulk_submit_in_background(self): + unsubmitted = xhiveframework.get_all(self.doctype, {"docstatus": 0}, limit=5, pluck="name") + failed = submit_cancel_or_update_docs(self.doctype, unsubmitted, action="submit") + self.assertEqual(failed, []) + + def check_docstatus(docs, status): + xhiveframework.db.rollback() + matching_docs = xhiveframework.get_all( + self.doctype, {"docstatus": status, "name": ("in", docs)}, pluck="name" + ) + return set(matching_docs) == set(docs) + + unsubmitted = xhiveframework.get_all(self.doctype, {"docstatus": 0}, limit=20, pluck="name") + submit_cancel_or_update_docs(self.doctype, unsubmitted, action="submit") + + self.wait_for_assertion(lambda: check_docstatus(unsubmitted, 1)) + + submitted = xhiveframework.get_all(self.doctype, {"docstatus": 1}, limit=20, pluck="name") + submit_cancel_or_update_docs(self.doctype, submitted, action="cancel") + self.wait_for_assertion(lambda: check_docstatus(submitted, 2)) diff --git a/xhiveframework/desk/doctype/calendar_view/__init__.py b/xhiveframework/desk/doctype/calendar_view/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/calendar_view/calendar_view.js b/xhiveframework/desk/doctype/calendar_view/calendar_view.js new file mode 100644 index 0000000..f3b7c22 --- /dev/null +++ b/xhiveframework/desk/doctype/calendar_view/calendar_view.js @@ -0,0 +1,36 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Calendar View", { + onload: function (frm) { + frm.trigger("reference_doctype"); + }, + refresh: function (frm) { + if (!frm.is_new()) { + frm.add_custom_button(__("Show Calendar"), () => + xhiveframework.set_route("List", frm.doc.reference_doctype, "Calendar", frm.doc.name) + ); + } + }, + reference_doctype: function (frm) { + const { reference_doctype } = frm.doc; + if (!reference_doctype) return; + + xhiveframework.model.with_doctype(reference_doctype, () => { + const meta = xhiveframework.get_meta(reference_doctype); + + const subject_options = meta.fields + .filter((df) => !xhiveframework.model.no_value_type.includes(df.fieldtype)) + .map((df) => df.fieldname); + + const date_options = meta.fields + .filter((df) => ["Date", "Datetime"].includes(df.fieldtype)) + .map((df) => df.fieldname); + + frm.set_df_property("subject_field", "options", subject_options); + frm.set_df_property("start_date_field", "options", date_options); + frm.set_df_property("end_date_field", "options", date_options); + frm.refresh(); + }); + }, +}); diff --git a/xhiveframework/desk/doctype/calendar_view/calendar_view.json b/xhiveframework/desk/doctype/calendar_view/calendar_view.json new file mode 100644 index 0000000..8149446 --- /dev/null +++ b/xhiveframework/desk/doctype/calendar_view/calendar_view.json @@ -0,0 +1,83 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2017-10-23 13:02:10.295824", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "subject_field", + "start_date_field", + "end_date_field", + "column_break_5", + "all_day" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "subject_field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Subject Field", + "reqd": 1 + }, + { + "fieldname": "start_date_field", + "fieldtype": "Select", + "label": "Start Date Field", + "reqd": 1 + }, + { + "fieldname": "end_date_field", + "fieldtype": "Select", + "label": "End Date Field", + "reqd": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "all_day", + "fieldtype": "Check", + "label": "All Day" + } + ], + "links": [], + "modified": "2023-08-28 22:29:39.662726", + "modified_by": "Administrator", + "module": "Desk", + "name": "Calendar View", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Desk User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/calendar_view/calendar_view.py b/xhiveframework/desk/doctype/calendar_view/calendar_view.py new file mode 100644 index 0000000..c61236b --- /dev/null +++ b/xhiveframework/desk/doctype/calendar_view/calendar_view.py @@ -0,0 +1,22 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class CalendarView(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + all_day: DF.Check + end_date_field: DF.Literal[None] + reference_doctype: DF.Link + start_date_field: DF.Literal[None] + subject_field: DF.Literal[None] + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/calendar_view/calendar_view_list.js b/xhiveframework/desk/doctype/calendar_view/calendar_view_list.js new file mode 100644 index 0000000..b5b54d5 --- /dev/null +++ b/xhiveframework/desk/doctype/calendar_view/calendar_view_list.js @@ -0,0 +1,16 @@ +xhiveframework.listview_settings["Calendar View"] = { + button: { + show(doc) { + return doc.name; + }, + get_label() { + return xhiveframework.utils.icon("calendar", "sm"); + }, + get_description(doc) { + return __("View {0}", [`${doc.name}`]); + }, + action(doc) { + xhiveframework.set_route("List", doc.reference_doctype, "Calendar", doc.name); + }, + }, +}; diff --git a/xhiveframework/desk/doctype/console_log/__init__.py b/xhiveframework/desk/doctype/console_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/console_log/console_log.js b/xhiveframework/desk/doctype/console_log/console_log.js new file mode 100644 index 0000000..cd09d81 --- /dev/null +++ b/xhiveframework/desk/doctype/console_log/console_log.js @@ -0,0 +1,12 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Console Log", { + refresh: function (frm) { + frm.add_custom_button(__("Re-Run in Console"), () => { + window.localStorage.setItem("system_console_code", frm.doc.script); + window.localStorage.setItem("system_console_type", frm.doc.type); + xhiveframework.set_route("Form", "System Console"); + }); + }, +}); diff --git a/xhiveframework/desk/doctype/console_log/console_log.json b/xhiveframework/desk/doctype/console_log/console_log.json new file mode 100644 index 0000000..a2955bf --- /dev/null +++ b/xhiveframework/desk/doctype/console_log/console_log.json @@ -0,0 +1,62 @@ +{ + "actions": [], + "autoname": "format:Log on {timestamp}", + "creation": "2020-08-18 19:56:12.336427", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "script", + "type", + "committed" + ], + "fields": [ + { + "fieldname": "script", + "fieldtype": "Code", + "in_list_view": 1, + "label": "Script", + "read_only": 1 + }, + { + "fieldname": "type", + "fieldtype": "Data", + "hidden": 1, + "label": "Type", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "committed", + "fieldtype": "Check", + "label": "Committed", + "read_only": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-03-12 20:35:43.921009", + "modified_by": "Administrator", + "module": "Desk", + "name": "Console Log", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/console_log/console_log.py b/xhiveframework/desk/doctype/console_log/console_log.py new file mode 100644 index 0000000..b715941 --- /dev/null +++ b/xhiveframework/desk/doctype/console_log/console_log.py @@ -0,0 +1,24 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class ConsoleLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + committed: DF.Check + script: DF.Code | None + type: DF.Data | None + # end: auto-generated types + + def after_delete(self): + # because on_trash can be bypassed + xhiveframework.throw(xhiveframework._("Console Logs can not be deleted")) diff --git a/xhiveframework/desk/doctype/console_log/test_console_log.py b/xhiveframework/desk/doctype/console_log/test_console_log.py new file mode 100644 index 0000000..90d1542 --- /dev/null +++ b/xhiveframework/desk/doctype/console_log/test_console_log.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestConsoleLog(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/custom_html_block/__init__.py b/xhiveframework/desk/doctype/custom_html_block/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/custom_html_block/custom_html_block.js b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.js new file mode 100644 index 0000000..3c15eed --- /dev/null +++ b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.js @@ -0,0 +1,23 @@ +// Copyright (c) 2023, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Custom HTML Block", { + refresh(frm) { + if ( + !has_common(xhiveframework.user_roles, [ + "Administrator", + "System Manager", + "Workspace Manager", + ]) + ) { + frm.set_value("private", true); + } else { + frm.set_df_property("private", "read_only", false); + } + + let wrapper = frm.fields_dict["preview"].wrapper; + wrapper.classList.add("mb-3"); + + xhiveframework.create_shadow_element(wrapper, frm.doc.html, frm.doc.style, frm.doc.script); + }, +}); diff --git a/xhiveframework/desk/doctype/custom_html_block/custom_html_block.json b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.json new file mode 100644 index 0000000..f65ba51 --- /dev/null +++ b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.json @@ -0,0 +1,154 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "prompt", + "creation": "2023-05-17 13:58:37.311045", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "private", + "preview_section", + "preview", + "html_section", + "html", + "javascript_section", + "js_message", + "script", + "css_section", + "style", + "roles_section", + "roles" + ], + "fields": [ + { + "collapsible": 1, + "collapsible_depends_on": "eval:true;", + "fieldname": "html_section", + "fieldtype": "Section Break", + "label": "HTML" + }, + { + "fieldname": "html", + "fieldtype": "Code", + "options": "HTML" + }, + { + "fieldname": "preview_section", + "fieldtype": "Section Break", + "label": "Preview" + }, + { + "fieldname": "preview", + "fieldtype": "HTML" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:true;", + "fieldname": "javascript_section", + "fieldtype": "Section Break", + "label": "Javascript" + }, + { + "fieldname": "script", + "fieldtype": "Code", + "options": "JS" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:true;", + "fieldname": "css_section", + "fieldtype": "Section Break", + "label": "CSS" + }, + { + "fieldname": "style", + "fieldtype": "Code", + "options": "CSS" + }, + { + "fieldname": "js_message", + "fieldtype": "HTML", + "label": "JS Message", + "options": "

    To interact with above HTML you will have to use `root_element` as a parent selector.

    For example:

    // here root_element is provided by default\nlet some_class_element = root_element.querySelector('.some-class');\nsome_class_element.textContent = \"New content\";\n
    " + }, + { + "fieldname": "roles_section", + "fieldtype": "Section Break", + "label": "Roles" + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "label": "Roles", + "options": "Has Role" + }, + { + "default": "0", + "depends_on": "eval: doc.private || doc.__unsaved", + "fieldname": "private", + "fieldtype": "Check", + "label": "Private", + "read_only": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 20:25:00.740795", + "modified_by": "Administrator", + "module": "Desk", + "name": "Custom HTML Block", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "if_owner": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Workspace Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/custom_html_block/custom_html_block.py b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.py new file mode 100644 index 0000000..ce12ac9 --- /dev/null +++ b/xhiveframework/desk/doctype/custom_html_block/custom_html_block.py @@ -0,0 +1,40 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.query_builder.utils import DocType + + +class CustomHTMLBlock(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.types import DF + + html: DF.Code | None + private: DF.Check + roles: DF.Table[HasRole] + script: DF.Code | None + style: DF.Code | None + # end: auto-generated types + pass + + +@xhiveframework.whitelist() +def get_custom_blocks_for_user(doctype, txt, searchfield, start, page_len, filters): + # return logged in users private blocks and all public blocks + customHTMLBlock = DocType("Custom HTML Block") + + condition_query = xhiveframework.qb.from_(customHTMLBlock) + + return ( + condition_query.select(customHTMLBlock.name).where( + (customHTMLBlock.private == 0) + | ((customHTMLBlock.owner == xhiveframework.session.user) & (customHTMLBlock.private == 1)) + ) + ).run() diff --git a/xhiveframework/desk/doctype/custom_html_block/test_custom_html_block.py b/xhiveframework/desk/doctype/custom_html_block/test_custom_html_block.py new file mode 100644 index 0000000..599c064 --- /dev/null +++ b/xhiveframework/desk/doctype/custom_html_block/test_custom_html_block.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestCustomHTMLBlock(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/dashboard/__init__.py b/xhiveframework/desk/doctype/dashboard/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard/dashboard.js b/xhiveframework/desk/doctype/dashboard/dashboard.js new file mode 100644 index 0000000..69dace4 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard/dashboard.js @@ -0,0 +1,30 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Dashboard", { + refresh: function (frm) { + frm.add_custom_button(__("Show Dashboard"), () => + xhiveframework.set_route("dashboard-view", frm.doc.name) + ); + + if (!xhiveframework.boot.developer_mode && frm.doc.is_standard) { + frm.disable_form(); + } + + frm.set_query("chart", "charts", function () { + return { + filters: { + is_public: 1, + }, + }; + }); + + frm.set_query("card", "cards", function () { + return { + filters: { + is_public: 1, + }, + }; + }); + }, +}); diff --git a/xhiveframework/desk/doctype/dashboard/dashboard.json b/xhiveframework/desk/doctype/dashboard/dashboard.json new file mode 100644 index 0000000..342f2a4 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard/dashboard.json @@ -0,0 +1,116 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:dashboard_name", + "creation": "2019-01-10 12:54:40.938705", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "dashboard_name", + "is_default", + "is_standard", + "module", + "charts", + "chart_options", + "cards" + ], + "fields": [ + { + "fieldname": "dashboard_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Dashboard Name", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "fieldname": "is_default", + "fieldtype": "Check", + "label": "Is Default" + }, + { + "fieldname": "charts", + "fieldtype": "Table", + "label": "Charts", + "options": "Dashboard Chart Link", + "reqd": 1 + }, + { + "description": "Set Default Options for all charts on this Dashboard (Ex: \"colors\": [\"#d1d8dd\", \"#ff5858\"])", + "fieldname": "chart_options", + "fieldtype": "Code", + "label": "Chart Options", + "options": "JSON" + }, + { + "fieldname": "cards", + "fieldtype": "Table", + "label": "Cards", + "options": "Number Card Link" + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "read_only_depends_on": "eval: !xhiveframework.boot.developer_mode" + }, + { + "depends_on": "eval: doc.is_standard", + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "mandatory_depends_on": "eval: doc.is_standard", + "options": "Module Def" + } + ], + "links": [], + "modified": "2023-08-28 22:35:02.993039", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Dashboard Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "dashboard_name", + "track_changes": 1 +} diff --git a/xhiveframework/desk/doctype/dashboard/dashboard.py b/xhiveframework/desk/doctype/dashboard/dashboard.py new file mode 100644 index 0000000..da19411 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard/dashboard.py @@ -0,0 +1,137 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.config import get_modules_from_all_apps_for_user +from xhiveframework.model.document import Document +from xhiveframework.modules.export_file import export_to_files +from xhiveframework.query_builder import DocType + + +class Dashboard(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.dashboard_chart_link.dashboard_chart_link import DashboardChartLink + from xhiveframework.desk.doctype.number_card_link.number_card_link import NumberCardLink + from xhiveframework.types import DF + + cards: DF.Table[NumberCardLink] + chart_options: DF.Code | None + charts: DF.Table[DashboardChartLink] + dashboard_name: DF.Data + is_default: DF.Check + is_standard: DF.Check + module: DF.Link | None + + # end: auto-generated types + def on_update(self): + if self.is_default: + # make all other dashboards non-default + DashBoard = DocType("Dashboard") + + xhiveframework.qb.update(DashBoard).set(DashBoard.is_default, 0).where(DashBoard.name != self.name).run() + + if xhiveframework.conf.developer_mode and self.is_standard: + export_to_files( + record_list=[["Dashboard", self.name, f"{self.module} Dashboard"]], record_module=self.module + ) + + def validate(self): + if not xhiveframework.conf.developer_mode and self.is_standard: + xhiveframework.throw(_("Cannot edit Standard Dashboards")) + + if self.is_standard: + non_standard_docs_map = { + "Dashboard Chart": get_non_standard_charts_in_dashboard(self), + "Number Card": get_non_standard_cards_in_dashboard(self), + } + + if non_standard_docs_map["Dashboard Chart"] or non_standard_docs_map["Number Card"]: + message = get_non_standard_warning_message(non_standard_docs_map) + xhiveframework.throw(message, title=_("Standard Not Set"), is_minimizable=True) + + self.validate_custom_options() + + def validate_custom_options(self): + if self.chart_options: + try: + json.loads(self.chart_options) + except ValueError as error: + xhiveframework.throw(_("Invalid json added in the custom options: {0}").format(error)) + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + return + + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return None + + allowed_modules = [ + xhiveframework.db.escape(module.get("module_name")) for module in get_modules_from_all_apps_for_user() + ] + return "`tabDashboard`.`module` in ({allowed_modules}) or `tabDashboard`.`module` is NULL".format( + allowed_modules=",".join(allowed_modules) + ) + + +@xhiveframework.whitelist() +def get_permitted_charts(dashboard_name): + permitted_charts = [] + dashboard = xhiveframework.get_doc("Dashboard", dashboard_name) + for chart in dashboard.charts: + if xhiveframework.has_permission("Dashboard Chart", doc=chart.chart): + chart_dict = xhiveframework._dict() + chart_dict.update(chart.as_dict()) + + if dashboard.get("chart_options"): + chart_dict.custom_options = dashboard.get("chart_options") + permitted_charts.append(chart_dict) + + return permitted_charts + + +@xhiveframework.whitelist() +def get_permitted_cards(dashboard_name): + dashboard = xhiveframework.get_doc("Dashboard", dashboard_name) + return [card for card in dashboard.cards if xhiveframework.has_permission("Number Card", doc=card.card)] + + +def get_non_standard_charts_in_dashboard(dashboard): + non_standard_charts = [doc.name for doc in xhiveframework.get_list("Dashboard Chart", {"is_standard": 0})] + return [chart_link.chart for chart_link in dashboard.charts if chart_link.chart in non_standard_charts] + + +def get_non_standard_cards_in_dashboard(dashboard): + non_standard_cards = [doc.name for doc in xhiveframework.get_list("Number Card", {"is_standard": 0})] + return [card_link.card for card_link in dashboard.cards if card_link.card in non_standard_cards] + + +def get_non_standard_warning_message(non_standard_docs_map): + message = _("""Please set the following documents in this Dashboard as standard first.""") + + def get_html(docs, doctype): + html = f"

    {xhiveframework.bold(doctype)}

    " + for doc in docs: + html += f'' + html += "
    " + return html + + html = message + "
    " + + for doctype in non_standard_docs_map: + if non_standard_docs_map[doctype]: + html += get_html(non_standard_docs_map[doctype], doctype) + + return html diff --git a/xhiveframework/desk/doctype/dashboard/dashboard_list.js b/xhiveframework/desk/doctype/dashboard/dashboard_list.js new file mode 100644 index 0000000..614414a --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard/dashboard_list.js @@ -0,0 +1,16 @@ +xhiveframework.listview_settings["Dashboard"] = { + button: { + show(doc) { + return doc.name; + }, + get_label() { + return xhiveframework.utils.icon("dashboard-list", "sm"); + }, + get_description(doc) { + return __("View {0}", [`${doc.name}`]); + }, + action(doc) { + xhiveframework.set_route("dashboard-view", doc.name); + }, + }, +}; diff --git a/xhiveframework/desk/doctype/dashboard/test_dashboard.py b/xhiveframework/desk/doctype/dashboard/test_dashboard.py new file mode 100644 index 0000000..85fb807 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard/test_dashboard.py @@ -0,0 +1,7 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDashboard(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/dashboard_chart/__init__.py b/xhiveframework/desk/doctype/dashboard_chart/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.js b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.js new file mode 100644 index 0000000..4b7056c --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.js @@ -0,0 +1,557 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.provide("xhiveframework.dashboards.chart_sources"); + +xhiveframework.ui.form.on("Dashboard Chart", { + setup: function (frm) { + // fetch timeseries from source + frm.add_fetch("source", "timeseries", "timeseries"); + }, + + before_save: function (frm) { + let dynamic_filters = JSON.parse(frm.doc.dynamic_filters_json || "null"); + let static_filters = JSON.parse(frm.doc.filters_json || "null"); + static_filters = xhiveframework.dashboard_utils.remove_common_static_filter_values( + static_filters, + dynamic_filters + ); + + frm.set_value("filters_json", JSON.stringify(static_filters)); + frm.trigger("show_filters"); + }, + + refresh: function (frm) { + frm.chart_filters = null; + frm.is_disabled = !xhiveframework.boot.developer_mode && frm.doc.is_standard; + + if (frm.is_disabled) { + !frm.doc.custom_options && frm.set_df_property("chart_options_section", "hidden", 1); + frm.disable_form(); + } + + if (!frm.is_new()) { + frm.add_custom_button("Add Chart to Dashboard", () => { + const dialog = xhiveframework.dashboard_utils.get_add_to_dashboard_dialog( + frm.doc.name, + "Dashboard Chart", + "xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.add_chart_to_dashboard" + ); + + if (!frm.doc.chart_name) { + xhiveframework.msgprint(__("Please create chart first")); + } else { + dialog.show(); + } + }); + } + + frm.set_df_property("filters_section", "hidden", 1); + frm.set_df_property("dynamic_filters_section", "hidden", 1); + + frm.trigger("set_parent_document_type"); + frm.trigger("set_time_series"); + frm.set_query("document_type", function () { + return { + filters: { + issingle: false, + }, + }; + }); + frm.trigger("update_options"); + frm.trigger("set_heatmap_year_options"); + if (frm.doc.report_name) { + frm.trigger("set_chart_report_filters"); + } + }, + + is_standard: function (frm) { + if (xhiveframework.boot.developer_mode && frm.doc.is_standard) { + frm.trigger("render_dynamic_filters_table"); + } else { + frm.set_df_property("dynamic_filters_section", "hidden", 1); + } + }, + + source: function (frm) { + frm.trigger("show_filters"); + }, + + set_heatmap_year_options: function (frm) { + if (frm.doc.type == "Heatmap") { + xhiveframework.db.get_doc("System Settings").then((doc) => { + const creation_date = doc.creation; + frm.set_df_property( + "heatmap_year", + "options", + xhiveframework.dashboard_utils.get_years_since_creation(creation_date) + ); + }); + } + }, + + chart_type: function (frm) { + frm.trigger("set_time_series"); + if (frm.doc.chart_type == "Report") { + frm.set_query("report_name", () => { + return { + filters: { + report_type: ["!=", "Report Builder"], + }, + }; + }); + } else { + frm.set_value("document_type", ""); + } + }, + + set_time_series: function (frm) { + // set timeseries based on chart type + if (["Count", "Average", "Sum"].includes(frm.doc.chart_type)) { + frm.set_value("timeseries", 1); + } else if (frm.doc.chart_type == "Custom") { + return; + } else { + frm.set_value("timeseries", 0); + } + }, + + document_type: function (frm) { + // update `based_on` options based on date / datetime fields + frm.set_value("source", ""); + frm.set_value("based_on", ""); + frm.set_value("value_based_on", ""); + frm.set_value("parent_document_type", ""); + frm.set_value("filters_json", "[]"); + frm.set_value("dynamic_filters_json", "[]"); + frm.trigger("update_options"); + frm.trigger("set_parent_document_type"); + }, + + report_name: function (frm) { + frm.set_value("x_field", ""); + frm.set_value("y_axis", []); + frm.set_df_property("x_field", "options", []); + frm.set_value("filters_json", "{}"); + frm.set_value("dynamic_filters_json", "{}"); + frm.set_value("use_report_chart", 0); + frm.trigger("set_chart_report_filters"); + }, + + set_chart_report_filters: function (frm) { + let report_name = frm.doc.report_name; + + if (report_name) { + if (frm.doc.filters_json.length > 2) { + frm.trigger("show_filters"); + frm.trigger("set_chart_field_options"); + } else { + xhiveframework.report_utils.get_report_filters(report_name).then((filters) => { + if (filters) { + frm.chart_filters = filters; + let filter_values = xhiveframework.report_utils.get_filter_values(filters); + frm.set_value("filters_json", JSON.stringify(filter_values)); + } + frm.trigger("show_filters"); + frm.trigger("set_chart_field_options"); + }); + } + } + }, + + use_report_chart: function (frm) { + !frm.doc.use_report_chart && frm.trigger("set_chart_field_options"); + }, + + set_chart_field_options: function (frm) { + let filters = frm.doc.filters_json.length > 2 ? JSON.parse(frm.doc.filters_json) : null; + if (frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2) { + filters = xhiveframework.dashboard_utils.get_all_filters(frm.doc); + } + xhiveframework + .xcall("xhiveframework.desk.query_report.run", { + report_name: frm.doc.report_name, + filters: filters, + ignore_prepared_report: 1, + }) + .then((data) => { + frm.report_data = data; + let report_has_chart = Boolean(data.chart); + + frm.set_df_property("use_report_chart", "hidden", !report_has_chart); + + if (!frm.doc.use_report_chart) { + if (data.result.length) { + frm.field_options = xhiveframework.report_utils.get_field_options_from_report( + data.columns, + data + ); + frm.set_df_property( + "x_field", + "options", + frm.field_options.non_numeric_fields + ); + if (!frm.field_options.numeric_fields.length) { + xhiveframework.msgprint( + __("Report has no numeric fields, please change the Report Name") + ); + } else { + let y_field_df = xhiveframework.meta.get_docfield( + "Dashboard Chart Field", + "y_field", + frm.doc.name + ); + y_field_df.options = frm.field_options.numeric_fields; + } + } else { + xhiveframework.msgprint( + __( + "Report has no data, please modify the filters or change the Report Name" + ) + ); + } + } else { + frm.set_value("use_report_chart", 1); + frm.set_df_property("use_report_chart", "hidden", false); + } + }); + }, + + timespan: function (frm) { + const time_interval_options = { + "Select Date Range": ["Quarterly", "Monthly", "Weekly", "Daily"], + "All Time": ["Yearly", "Monthly"], + "Last Year": ["Quarterly", "Monthly", "Weekly", "Daily"], + "Last Quarter": ["Monthly", "Weekly", "Daily"], + "Last Month": ["Weekly", "Daily"], + "Last Week": ["Daily"], + }; + if (frm.doc.timespan) { + frm.set_df_property( + "time_interval", + "options", + time_interval_options[frm.doc.timespan] + ); + } + }, + + update_options: function (frm) { + let doctype = frm.doc.document_type; + let date_fields = [ + { label: __("Created On"), value: "creation" }, + { label: __("Last Modified On"), value: "modified" }, + ]; + let value_fields = []; + let group_by_fields = [{ label: "Created By", value: "owner" }]; + let aggregate_function_fields = []; + let update_form = function () { + // update select options + frm.set_df_property("based_on", "options", date_fields); + frm.set_df_property("value_based_on", "options", value_fields); + frm.set_df_property("group_by_based_on", "options", group_by_fields); + frm.set_df_property( + "aggregate_function_based_on", + "options", + aggregate_function_fields + ); + frm.trigger("show_filters"); + }; + + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => { + // get all date and datetime fields + xhiveframework.get_meta(doctype).fields.map((df) => { + if (["Date", "Datetime"].includes(df.fieldtype)) { + date_fields.push({ label: df.label, value: df.fieldname }); + } + if ( + ["Int", "Float", "Currency", "Percent", "Duration"].includes(df.fieldtype) + ) { + value_fields.push({ label: df.label, value: df.fieldname }); + aggregate_function_fields.push({ label: df.label, value: df.fieldname }); + } + if (["Link", "Select"].includes(df.fieldtype)) { + group_by_fields.push({ label: df.label, value: df.fieldname }); + } + }); + update_form(); + }); + } else { + // update select options + update_form(); + } + }, + + show_filters: function (frm) { + frm.chart_filters = []; + xhiveframework.dashboard_utils.get_filters_for_chart_type(frm.doc).then((filters) => { + if (filters) { + frm.chart_filters = filters; + } + frm.trigger("render_filters_table"); + + if (xhiveframework.boot.developer_mode && frm.doc.is_standard) { + frm.trigger("render_dynamic_filters_table"); + } + }); + }, + + render_filters_table: function (frm) { + frm.set_df_property("filters_section", "hidden", 0); + let is_document_type = frm.doc.chart_type !== "Report" && frm.doc.chart_type !== "Custom"; + let is_dynamic_filter = (f) => ["Date", "DateRange"].includes(f.fieldtype) && f.default; + + let wrapper = $(frm.get_field("filters_json").wrapper).empty(); + let table = $(` + + + + + + + + +
    ${__("Filter")}${__("Condition")}${__("Value")}
    `).appendTo(wrapper); + $(`

    ${__("Click table to edit")}

    `).appendTo(wrapper); + + let filters = JSON.parse(frm.doc.filters_json || "[]"); + var filters_set = false; + + // Set dynamic filters for reports + if (frm.doc.chart_type == "Report") { + let set_filters = false; + frm.chart_filters.forEach((f) => { + if (is_dynamic_filter(f)) { + filters[f.fieldname] = f.default; + set_filters = true; + } + }); + set_filters && frm.set_value("filters_json", JSON.stringify(filters)); + } + + let fields = []; + if (is_document_type) { + fields = [ + { + fieldtype: "HTML", + fieldname: "filter_area", + }, + ]; + + if (filters.length > 0) { + filters.forEach((filter) => { + const filter_row = $(` + ${filter[1]} + ${filter[2] || ""} + ${filter[3]} + `); + + table.find("tbody").append(filter_row); + filters_set = true; + }); + } + } else if (frm.chart_filters.length) { + fields = frm.chart_filters.filter((f) => f.fieldname); + + fields.map((f) => { + if (filters[f.fieldname]) { + let condition = "="; + const filter_row = $(` + ${f.label} + ${condition} + ${filters[f.fieldname] || ""} + `); + + table.find("tbody").append(filter_row); + filters_set = true; + } + }); + } + + if (!filters_set) { + const filter_row = $(` + ${__("Click to Set Filters")}`); + table.find("tbody").append(filter_row); + } + + table.on("click", () => { + frm.is_disabled && xhiveframework.throw(__("Cannot edit filters for standard charts")); + + let dialog = new xhiveframework.ui.Dialog({ + title: __("Set Filters"), + fields: fields.filter((f) => !is_dynamic_filter(f)), + primary_action: function () { + let values = this.get_values(); + if (values) { + this.hide(); + if (is_document_type) { + let filters = frm.filter_group.get_filters(); + frm.set_value("filters_json", JSON.stringify(filters)); + } else { + frm.set_value("filters_json", JSON.stringify(values)); + } + + frm.trigger("show_filters"); + if (frm.doc.chart_type == "Report") { + frm.trigger("set_chart_report_filters"); + } + } + }, + primary_action_label: "Set", + }); + xhiveframework.dashboards.filters_dialog = dialog; + + if (is_document_type) { + frm.filter_group = new xhiveframework.ui.FilterGroup({ + parent: dialog.get_field("filter_area").$wrapper, + doctype: frm.doc.document_type, + parent_doctype: frm.doc.parent_document_type, + on_change: () => {}, + }); + + frm.filter_group.add_filters_to_filter_group(filters); + } + + dialog.show(); + + if (frm.doc.chart_type == "Report") { + //Set query report object so that it can be used while fetching filter values in the report + xhiveframework.query_report = new xhiveframework.views.QueryReport({ + filters: dialog.fields_list, + }); + xhiveframework.query_reports[frm.doc.report_name] && + xhiveframework.query_reports[frm.doc.report_name].onload && + xhiveframework.query_reports[frm.doc.report_name].onload(xhiveframework.query_report); + } + + dialog.set_values(filters); + }); + }, + + render_dynamic_filters_table(frm) { + frm.set_df_property("dynamic_filters_section", "hidden", 0); + + let is_document_type = frm.doc.chart_type !== "Report" && frm.doc.chart_type !== "Custom"; + + let wrapper = $(frm.get_field("dynamic_filters_json").wrapper).empty(); + + frm.dynamic_filter_table = + $(` + + + + + + + + +
    ${__("Filter")}${__("Condition")}${__("Value")}
    `).appendTo(wrapper); + + frm.dynamic_filters = + frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2 + ? JSON.parse(frm.doc.dynamic_filters_json) + : null; + + frm.trigger("set_dynamic_filters_in_table"); + + let filters = JSON.parse(frm.doc.filters_json || "[]"); + + let fields = xhiveframework.dashboard_utils.get_fields_for_dynamic_filter_dialog( + is_document_type, + filters, + frm.dynamic_filters + ); + + frm.dynamic_filter_table.on("click", () => { + let dialog = new xhiveframework.ui.Dialog({ + title: __("Set Dynamic Filters"), + fields: fields, + primary_action: () => { + let values = dialog.get_values(); + dialog.hide(); + let dynamic_filters = []; + for (let key of Object.keys(values)) { + if (is_document_type) { + let [doctype, fieldname] = key.split(":"); + dynamic_filters.push([doctype, fieldname, "=", values[key]]); + } + } + + if (is_document_type) { + frm.set_value("dynamic_filters_json", JSON.stringify(dynamic_filters)); + } else { + frm.set_value("dynamic_filters_json", JSON.stringify(values)); + } + frm.trigger("set_dynamic_filters_in_table"); + }, + primary_action_label: "Set", + }); + + dialog.show(); + dialog.set_values(frm.dynamic_filters); + }); + }, + + set_dynamic_filters_in_table: function (frm) { + frm.dynamic_filters = + frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2 + ? JSON.parse(frm.doc.dynamic_filters_json) + : null; + + if (!frm.dynamic_filters) { + const filter_row = $(` + ${__("Click to Set Dynamic Filters")}`); + frm.dynamic_filter_table.find("tbody").html(filter_row); + } else { + let filter_rows = ""; + if ($.isArray(frm.dynamic_filters)) { + frm.dynamic_filters.forEach((filter) => { + filter_rows += ` + ${filter[1]} + ${filter[2] || ""} + ${filter[3]} + `; + }); + } else { + let condition = "="; + for (let [key, val] of Object.entries(frm.dynamic_filters)) { + filter_rows += ` + ${key} + ${condition} + ${val || ""} + `; + } + } + + frm.dynamic_filter_table.find("tbody").html(filter_rows); + } + }, + + set_parent_document_type: async function (frm) { + let document_type = frm.doc.document_type; + let doc_is_table = + document_type && + (await xhiveframework.db.get_value("DocType", document_type, "istable")).message.istable; + + frm.set_df_property("parent_document_type", "hidden", !doc_is_table); + + if (document_type && doc_is_table) { + let parents = await xhiveframework.xcall( + "xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.get_parent_doctypes", + { child_type: document_type } + ); + + frm.set_query("parent_document_type", function () { + return { + filters: { + name: ["in", parents], + }, + }; + }); + + if (parents.length === 1) { + frm.set_value("parent_document_type", parents[0]); + } + } + }, +}); diff --git a/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.json b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.json new file mode 100644 index 0000000..fcbe0f4 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.json @@ -0,0 +1,336 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:chart_name", + "creation": "2019-01-10 12:28:06.282875", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "is_standard", + "module", + "chart_name", + "chart_type", + "report_name", + "use_report_chart", + "x_field", + "y_axis", + "source", + "document_type", + "parent_document_type", + "based_on", + "value_based_on", + "group_by_type", + "group_by_based_on", + "aggregate_function_based_on", + "number_of_groups", + "column_break_6", + "is_public", + "heatmap_year", + "timespan", + "from_date", + "to_date", + "time_interval", + "timeseries", + "type", + "filters_section", + "filters_json", + "dynamic_filters_section", + "dynamic_filters_json", + "chart_options_section", + "custom_options", + "column_break_2", + "color", + "section_break_10", + "last_synced_on", + "roles" + ], + "fields": [ + { + "fieldname": "chart_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Chart Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "chart_type", + "fieldtype": "Select", + "label": "Chart Type", + "options": "Count\nSum\nAverage\nGroup By\nCustom\nReport", + "set_only_once": 1 + }, + { + "depends_on": "eval:doc.chart_type === 'Custom'", + "fieldname": "source", + "fieldtype": "Link", + "label": "Chart Source", + "options": "Dashboard Chart Source" + }, + { + "depends_on": "eval: doc.chart_type !== 'Custom' && doc.chart_type !== 'Report'", + "fieldname": "document_type", + "fieldtype": "Link", + "label": "Document Type", + "options": "DocType", + "set_only_once": 1 + }, + { + "depends_on": "eval: doc.timeseries && ['Count', 'Sum', 'Average'].includes(doc.chart_type)", + "fieldname": "based_on", + "fieldtype": "Select", + "label": "Time Series Based On" + }, + { + "depends_on": "eval: ['Sum', 'Average'].includes(doc.chart_type)\n", + "fieldname": "value_based_on", + "fieldtype": "Select", + "label": "Value Based On" + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: doc.timeseries && doc.type !== 'Heatmap'", + "fieldname": "timespan", + "fieldtype": "Select", + "label": "Timespan", + "options": "Last Year\nLast Quarter\nLast Month\nLast Week\nSelect Date Range" + }, + { + "depends_on": "eval: doc.timeseries && doc.type !== 'Heatmap'", + "fieldname": "time_interval", + "fieldtype": "Select", + "label": "Time Interval", + "options": "Yearly\nQuarterly\nMonthly\nWeekly\nDaily" + }, + { + "default": "0", + "depends_on": "eval: !['Group By', 'Report'].includes(doc.chart_type)\n", + "fieldname": "timeseries", + "fieldtype": "Check", + "label": "Time Series" + }, + { + "fieldname": "filters_section", + "fieldtype": "Section Break", + "label": "Filters" + }, + { + "fieldname": "filters_json", + "fieldtype": "Code", + "label": "Filters JSON", + "options": "JSON", + "reqd": 1 + }, + { + "fieldname": "chart_options_section", + "fieldtype": "Section Break", + "label": "Chart Options" + }, + { + "default": "Line", + "fieldname": "type", + "fieldtype": "Select", + "label": "Type", + "options": "Line\nBar\nPercentage\nPie\nDonut\nHeatmap" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: doc.chart_type !== 'Report' && doc.type !== 'Heatmap'", + "fieldname": "color", + "fieldtype": "Color", + "label": "Color" + }, + { + "fieldname": "section_break_10", + "fieldtype": "Section Break" + }, + { + "fieldname": "last_synced_on", + "fieldtype": "Datetime", + "label": "Last Synced On", + "read_only": 1 + }, + { + "depends_on": "eval:doc.chart_type === 'Group By'", + "fieldname": "group_by_based_on", + "fieldtype": "Select", + "label": "Group By Based On" + }, + { + "default": "Count", + "depends_on": "eval:doc.chart_type === 'Group By'", + "fieldname": "group_by_type", + "fieldtype": "Select", + "label": "Group By Type", + "options": "Count\nSum\nAverage" + }, + { + "depends_on": "eval: ['Sum', 'Average'].includes(doc.group_by_type)", + "fieldname": "aggregate_function_based_on", + "fieldtype": "Select", + "label": "Aggregate Function Based On" + }, + { + "depends_on": "eval:doc.chart_type === 'Group By'", + "fieldname": "number_of_groups", + "fieldtype": "Int", + "label": "Number of Groups" + }, + { + "depends_on": "eval:doc.timespan === 'Select Date Range'", + "fieldname": "from_date", + "fieldtype": "Date", + "label": "From Date" + }, + { + "depends_on": "eval:doc.timespan === 'Select Date Range'", + "fieldname": "to_date", + "fieldtype": "Date", + "label": "To Date" + }, + { + "depends_on": "eval:doc.chart_type == 'Report' && doc.report_name && !doc.use_report_chart", + "fieldname": "x_field", + "fieldtype": "Select", + "label": "X Field", + "mandatory_depends_on": "eval: doc.report_name && !doc.use_report_chart" + }, + { + "depends_on": "eval:doc.chart_type === 'Report'", + "fieldname": "report_name", + "fieldtype": "Link", + "label": "Report Name", + "mandatory_depends_on": "eval:doc.chart_type === 'Report'", + "options": "Report", + "set_only_once": 1 + }, + { + "depends_on": "eval:doc.chart_type == 'Report' && doc.report_name && !doc.use_report_chart", + "fieldname": "y_axis", + "fieldtype": "Table", + "label": "Y Axis", + "mandatory_depends_on": "eval:doc.report_name && !doc.use_report_chart", + "options": "Dashboard Chart Field" + }, + { + "description": "Ex: \"colors\": [\"#d1d8dd\", \"#ff5858\"]", + "fieldname": "custom_options", + "fieldtype": "Code", + "label": "Custom Options" + }, + { + "default": "0", + "description": "This chart will be available to all Users if this is set", + "fieldname": "is_public", + "fieldtype": "Check", + "label": "Is Public" + }, + { + "depends_on": "eval: doc.type == 'Heatmap'", + "fieldname": "heatmap_year", + "fieldtype": "Select", + "label": "Year" + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "read_only_depends_on": "eval: !xhiveframework.boot.developer_mode" + }, + { + "depends_on": "eval: doc.is_standard", + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "mandatory_depends_on": "eval: doc.is_standard", + "options": "Module Def" + }, + { + "fieldname": "dynamic_filters_json", + "fieldtype": "Code", + "label": "Dynamic Filters JSON", + "options": "JSON" + }, + { + "fieldname": "dynamic_filters_section", + "fieldtype": "Section Break", + "label": "Dynamic Filters" + }, + { + "default": "0", + "depends_on": "eval: doc.report_name", + "fieldname": "use_report_chart", + "fieldtype": "Check", + "label": "Use Report Chart" + }, + { + "depends_on": "eval: doc.chart_type !== 'Custom' && doc.chart_type !== 'Report'", + "description": "The document type selected is a child table, so the parent document type is required.", + "fieldname": "parent_document_type", + "fieldtype": "Link", + "label": "Parent Document Type", + "options": "DocType" + }, + { + "description": "If set, only user with these roles can access this chart. If not set, DocType or Report permissions will be used.", + "fieldname": "roles", + "fieldtype": "Table", + "label": "Roles", + "options": "Has Role" + } + ], + "links": [], + "modified": "2023-09-18 13:41:05.263676", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard Chart", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Dashboard Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.py b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.py new file mode 100644 index 0000000..f6451fc --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart/dashboard_chart.py @@ -0,0 +1,436 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import datetime +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.boot import get_allowed_report_names +from xhiveframework.config import get_modules_from_all_apps_for_user +from xhiveframework.model.document import Document +from xhiveframework.model.naming import append_number_if_name_exists +from xhiveframework.modules.export_file import export_to_files +from xhiveframework.utils import cint, get_datetime, getdate, has_common, now_datetime, nowdate +from xhiveframework.utils.dashboard import cache_source +from xhiveframework.utils.data import format_date +from xhiveframework.utils.dateutils import ( + get_dates_from_timegrain, + get_from_date_from_timespan, + get_period, + get_period_beginning, +) + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + return + + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return None + + doctype_condition = False + report_condition = False + module_condition = False + + allowed_doctypes = [xhiveframework.db.escape(doctype) for doctype in xhiveframework.permissions.get_doctypes_with_read()] + allowed_reports = [xhiveframework.db.escape(report) for report in get_allowed_report_names()] + allowed_modules = [ + xhiveframework.db.escape(module.get("module_name")) for module in get_modules_from_all_apps_for_user() + ] + + if allowed_doctypes: + doctype_condition = "`tabDashboard Chart`.`document_type` in ({allowed_doctypes})".format( + allowed_doctypes=",".join(allowed_doctypes) + ) + if allowed_reports: + report_condition = "`tabDashboard Chart`.`report_name` in ({allowed_reports})".format( + allowed_reports=",".join(allowed_reports) + ) + if allowed_modules: + module_condition = """`tabDashboard Chart`.`module` in ({allowed_modules}) + or `tabDashboard Chart`.`module` is NULL""".format(allowed_modules=",".join(allowed_modules)) + + return f""" + ((`tabDashboard Chart`.`chart_type` in ('Count', 'Sum', 'Average') + and {doctype_condition}) + or + (`tabDashboard Chart`.`chart_type` = 'Report' + and {report_condition})) + and + ({module_condition}) + """ + + +def has_permission(doc, ptype, user): + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return True + + if doc.roles: + allowed = [d.role for d in doc.roles] + if has_common(roles, allowed): + return True + elif doc.chart_type == "Report": + if doc.report_name in get_allowed_report_names(): + return True + else: + allowed_doctypes = xhiveframework.permissions.get_doctypes_with_read() + if doc.document_type in allowed_doctypes: + return True + + return False + + +@xhiveframework.whitelist() +@cache_source +def get( + chart_name=None, + chart=None, + no_cache=None, + filters=None, + from_date=None, + to_date=None, + timespan=None, + time_interval=None, + heatmap_year=None, + refresh=None, +): + if chart_name: + chart: DashboardChart = xhiveframework.get_doc("Dashboard Chart", chart_name) + else: + chart = xhiveframework._dict(xhiveframework.parse_json(chart)) + + heatmap_year = heatmap_year or chart.heatmap_year + timespan = timespan or chart.timespan + + if timespan == "Select Date Range": + if from_date and len(from_date): + from_date = get_datetime(from_date) + else: + from_date = chart.from_date + + if to_date and len(to_date): + to_date = get_datetime(to_date) + else: + to_date = get_datetime(chart.to_date) + + timegrain = time_interval or chart.time_interval + filters = xhiveframework.parse_json(filters) or xhiveframework.parse_json(chart.filters_json) + if not filters: + filters = [] + + # don't include cancelled documents + filters.append([chart.document_type, "docstatus", "<", 2, False]) + + if chart.chart_type == "Group By": + chart_config = get_group_by_chart_config(chart, filters) + else: + if chart.type == "Heatmap": + chart_config = get_heatmap_chart_config(chart, filters, heatmap_year) + else: + chart_config = get_chart_config(chart, filters, timespan, timegrain, from_date, to_date) + + return chart_config + + +@xhiveframework.whitelist() +def create_dashboard_chart(args): + args = xhiveframework.parse_json(args) + doc = xhiveframework.new_doc("Dashboard Chart") + + doc.update(args) + + if args.get("custom_options"): + doc.custom_options = json.dumps(args.get("custom_options")) + + if xhiveframework.db.exists("Dashboard Chart", args.chart_name): + args.chart_name = append_number_if_name_exists("Dashboard Chart", args.chart_name) + doc.chart_name = args.chart_name + doc.insert(ignore_permissions=True) + return doc + + +@xhiveframework.whitelist() +def create_report_chart(args): + doc = create_dashboard_chart(args) + args = xhiveframework.parse_json(args) + args.chart_name = doc.chart_name + if args.dashboard: + add_chart_to_dashboard(json.dumps(args)) + + +@xhiveframework.whitelist() +def add_chart_to_dashboard(args): + args = xhiveframework.parse_json(args) + + dashboard = xhiveframework.get_doc("Dashboard", args.dashboard) + dashboard_link = xhiveframework.new_doc("Dashboard Chart Link") + dashboard_link.chart = args.chart_name or args.name + + if args.set_standard and dashboard.is_standard: + chart = xhiveframework.get_doc("Dashboard Chart", dashboard_link.chart) + chart.is_standard = 1 + chart.module = dashboard.module + chart.save() + + dashboard.append("charts", dashboard_link) + dashboard.save() + xhiveframework.db.commit() + + +def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): + if not from_date: + from_date = get_from_date_from_timespan(to_date, timespan) + from_date = get_period_beginning(from_date, timegrain) + if not to_date: + to_date = now_datetime() + + doctype = chart.document_type + datefield = chart.based_on + value_field = chart.value_based_on or "1" + from_date = from_date.strftime("%Y-%m-%d") + to_date = to_date + + filters.append([doctype, datefield, ">=", from_date, False]) + filters.append([doctype, datefield, "<=", to_date, False]) + + data = xhiveframework.get_list( + doctype, + fields=[datefield, f"SUM({value_field})", "COUNT(*)"], + filters=filters, + group_by=datefield, + order_by=datefield, + as_list=True, + parent_doctype=chart.parent_document_type, + ) + + result = get_result(data, timegrain, from_date, to_date, chart.chart_type) + + return { + "labels": [ + format_date(get_period(r[0], timegrain), parse_day_first=True) + if timegrain in ("Daily", "Weekly") + else get_period(r[0], timegrain) + for r in result + ], + "datasets": [{"name": chart.name, "values": [r[1] for r in result]}], + } + + +def get_heatmap_chart_config(chart, filters, heatmap_year): + aggregate_function = get_aggregate_function(chart.chart_type) + value_field = chart.value_based_on or "1" + doctype = chart.document_type + datefield = chart.based_on + year = cint(heatmap_year) if heatmap_year else getdate(nowdate()).year + year_start_date = datetime.date(year, 1, 1).strftime("%Y-%m-%d") + next_year_start_date = datetime.date(year + 1, 1, 1).strftime("%Y-%m-%d") + + filters.append([doctype, datefield, ">", f"{year_start_date}", False]) + filters.append([doctype, datefield, "<", f"{next_year_start_date}", False]) + + if xhiveframework.db.db_type == "mariadb": + timestamp_field = f"unix_timestamp({datefield})" + else: + timestamp_field = f"extract(epoch from timestamp {datefield})" + + data = dict( + xhiveframework.get_all( + doctype, + fields=[ + timestamp_field, + f"{aggregate_function}({value_field})", + ], + filters=filters, + group_by=f"date({datefield})", + as_list=1, + order_by=f"{datefield} asc", + ignore_ifnull=True, + ) + ) + + return { + "labels": [], + "dataPoints": data, + } + + +def get_group_by_chart_config(chart, filters): + aggregate_function = get_aggregate_function(chart.group_by_type) + value_field = chart.aggregate_function_based_on or "1" + group_by_field = chart.group_by_based_on + doctype = chart.document_type + + data = xhiveframework.get_list( + doctype, + fields=[ + f"{group_by_field} as name", + f"{aggregate_function}({value_field}) as count", + ], + filters=filters, + parent_doctype=chart.parent_document_type, + group_by=group_by_field, + order_by="count desc", + ignore_ifnull=True, + ) + + if data: + return { + "labels": [item["name"] if item["name"] else "Not Specified" for item in data], + "datasets": [{"name": chart.name, "values": [item["count"] for item in data]}], + } + else: + return None + + +def get_aggregate_function(chart_type): + return { + "Sum": "SUM", + "Count": "COUNT", + "Average": "AVG", + }[chart_type] + + +def get_result(data, timegrain, from_date, to_date, chart_type): + dates = get_dates_from_timegrain(from_date, to_date, timegrain) + result = [[date, 0] for date in dates] + data_index = 0 + if data: + for d in result: + count = 0 + while data_index < len(data) and getdate(data[data_index][0]) <= d[0]: + d[1] += data[data_index][1] + count += data[data_index][2] + data_index += 1 + if chart_type == "Average" and not count == 0: + d[1] = d[1] / count + if chart_type == "Count": + d[1] = count + + return result + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_charts_for_user(doctype, txt, searchfield, start, page_len, filters): + or_filters = {"owner": xhiveframework.session.user, "is_public": 1} + return xhiveframework.db.get_list( + "Dashboard Chart", fields=["name"], filters=filters, or_filters=or_filters, as_list=1 + ) + + +class DashboardChart(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.desk.doctype.dashboard_chart_field.dashboard_chart_field import DashboardChartField + from xhiveframework.types import DF + + aggregate_function_based_on: DF.Literal[None] + based_on: DF.Literal[None] + chart_name: DF.Data + chart_type: DF.Literal["Count", "Sum", "Average", "Group By", "Custom", "Report"] + color: DF.Color | None + custom_options: DF.Code | None + document_type: DF.Link | None + dynamic_filters_json: DF.Code | None + filters_json: DF.Code + from_date: DF.Date | None + group_by_based_on: DF.Literal[None] + group_by_type: DF.Literal["Count", "Sum", "Average"] + heatmap_year: DF.Literal[None] + is_public: DF.Check + is_standard: DF.Check + last_synced_on: DF.Datetime | None + module: DF.Link | None + number_of_groups: DF.Int + parent_document_type: DF.Link | None + report_name: DF.Link | None + roles: DF.Table[HasRole] + source: DF.Link | None + time_interval: DF.Literal["Yearly", "Quarterly", "Monthly", "Weekly", "Daily"] + timeseries: DF.Check + timespan: DF.Literal["Last Year", "Last Quarter", "Last Month", "Last Week", "Select Date Range"] + to_date: DF.Date | None + type: DF.Literal["Line", "Bar", "Percentage", "Pie", "Donut", "Heatmap"] + use_report_chart: DF.Check + value_based_on: DF.Literal[None] + x_field: DF.Literal[None] + y_axis: DF.Table[DashboardChartField] + + # end: auto-generated types + def on_update(self): + xhiveframework.cache.delete_key(f"chart-data:{self.name}") + if xhiveframework.conf.developer_mode and self.is_standard: + export_to_files(record_list=[["Dashboard Chart", self.name]], record_module=self.module) + + def validate(self): + if not xhiveframework.conf.developer_mode and self.is_standard: + xhiveframework.throw(_("Cannot edit Standard charts")) + if self.chart_type != "Custom" and self.chart_type != "Report": + self.check_required_field() + self.check_document_type() + + self.validate_custom_options() + + def check_required_field(self): + if not self.document_type: + xhiveframework.throw(_("Document type is required to create a dashboard chart")) + + if ( + self.document_type + and xhiveframework.get_meta(self.document_type).istable + and not self.parent_document_type + ): + xhiveframework.throw(_("Parent document type is required to create a dashboard chart")) + + if self.chart_type == "Group By": + if not self.group_by_based_on: + xhiveframework.throw(_("Group By field is required to create a dashboard chart")) + if self.group_by_type in ["Sum", "Average"] and not self.aggregate_function_based_on: + xhiveframework.throw(_("Aggregate Function field is required to create a dashboard chart")) + else: + if not self.based_on: + xhiveframework.throw(_("Time series based on is required to create a dashboard chart")) + + def check_document_type(self): + if xhiveframework.get_meta(self.document_type).issingle: + xhiveframework.throw(_("You cannot create a dashboard chart from single DocTypes")) + + def validate_custom_options(self): + if self.custom_options: + try: + json.loads(self.custom_options) + except ValueError as error: + xhiveframework.throw(_("Invalid json added in the custom options: {0}").format(error)) + + +@xhiveframework.whitelist() +def get_parent_doctypes(child_type: str) -> list[str]: + """Get all parent doctypes that have the child doctype.""" + assert isinstance(child_type, str) + + standard = xhiveframework.get_all( + "DocField", + fields="parent", + filters={"fieldtype": "Table", "options": child_type}, + pluck="parent", + ) + + custom = xhiveframework.get_all( + "Custom Field", + fields="dt", + filters={"fieldtype": "Table", "options": child_type}, + pluck="dt", + ) + + return standard + custom diff --git a/xhiveframework/desk/doctype/dashboard_chart/test_dashboard_chart.py b/xhiveframework/desk/doctype/dashboard_chart/test_dashboard_chart.py new file mode 100644 index 0000000..2f35bbf --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart/test_dashboard_chart.py @@ -0,0 +1,282 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +from datetime import datetime +from unittest.mock import patch + +from dateutil.relativedelta import relativedelta + +import xhiveframework +from xhiveframework.desk.doctype.dashboard_chart.dashboard_chart import get +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import formatdate, get_last_day, getdate +from xhiveframework.utils.dateutils import get_period, get_period_ending + + +class TestDashboardChart(XhiveFrameworkTestCase): + def test_period_ending(self): + self.assertEqual(get_period_ending("2019-04-10", "Daily"), getdate("2019-04-10")) + + # week starts on monday + with patch.object(xhiveframework.utils.data, "get_first_day_of_the_week", return_value="Monday"): + self.assertEqual(get_period_ending("2019-04-10", "Weekly"), getdate("2019-04-14")) + + self.assertEqual(get_period_ending("2019-04-10", "Monthly"), getdate("2019-04-30")) + self.assertEqual(get_period_ending("2019-04-30", "Monthly"), getdate("2019-04-30")) + self.assertEqual(get_period_ending("2019-03-31", "Monthly"), getdate("2019-03-31")) + + self.assertEqual(get_period_ending("2019-04-10", "Quarterly"), getdate("2019-06-30")) + self.assertEqual(get_period_ending("2019-06-30", "Quarterly"), getdate("2019-06-30")) + self.assertEqual(get_period_ending("2019-10-01", "Quarterly"), getdate("2019-12-31")) + + def test_dashboard_chart(self): + if xhiveframework.db.exists("Dashboard Chart", "Test Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Dashboard Chart") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Dashboard Chart", + chart_type="Count", + document_type="DocType", + based_on="creation", + timespan="Last Year", + time_interval="Monthly", + filters_json="{}", + timeseries=1, + ) + ).insert() + + cur_date = datetime.now() - relativedelta(years=1) + + result = get(chart_name="Test Dashboard Chart", refresh=1) + + for idx in range(13): + month = get_last_day(cur_date) + month = formatdate(month.strftime("%Y-%m-%d")) + self.assertEqual(result.get("labels")[idx], get_period(month)) + cur_date += relativedelta(months=1) + + def test_empty_dashboard_chart(self): + if xhiveframework.db.exists("Dashboard Chart", "Test Empty Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Empty Dashboard Chart") + + xhiveframework.db.delete("Error Log") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Empty Dashboard Chart", + chart_type="Count", + document_type="Error Log", + based_on="creation", + timespan="Last Year", + time_interval="Monthly", + filters_json="[]", + timeseries=1, + ) + ).insert() + + cur_date = datetime.now() - relativedelta(years=1) + + result = get(chart_name="Test Empty Dashboard Chart", refresh=1) + + for idx in range(13): + month = get_last_day(cur_date) + month = formatdate(month.strftime("%Y-%m-%d")) + self.assertEqual(result.get("labels")[idx], get_period(month)) + cur_date += relativedelta(months=1) + + def test_chart_wih_one_value(self): + if xhiveframework.db.exists("Dashboard Chart", "Test Empty Dashboard Chart 2"): + xhiveframework.delete_doc("Dashboard Chart", "Test Empty Dashboard Chart 2") + + xhiveframework.db.delete("Error Log") + + # create one data point + xhiveframework.get_doc(dict(doctype="Error Log", creation="2018-06-01 00:00:00")).insert() + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Empty Dashboard Chart 2", + chart_type="Count", + document_type="Error Log", + based_on="creation", + timespan="Last Year", + time_interval="Monthly", + filters_json="[]", + timeseries=1, + ) + ).insert() + + cur_date = datetime.now() - relativedelta(years=1) + + result = get(chart_name="Test Empty Dashboard Chart 2", refresh=1) + + for idx in range(13): + month = get_last_day(cur_date) + month = formatdate(month.strftime("%Y-%m-%d")) + self.assertEqual(result.get("labels")[idx], get_period(month)) + cur_date += relativedelta(months=1) + + # only 1 data point with value + self.assertEqual(result.get("datasets")[0].get("values")[2], 0) + + def test_group_by_chart_type(self): + if xhiveframework.db.exists("Dashboard Chart", "Test Group By Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Group By Dashboard Chart") + + xhiveframework.get_doc({"doctype": "ToDo", "description": "test"}).insert() + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Group By Dashboard Chart", + chart_type="Group By", + document_type="ToDo", + group_by_based_on="status", + filters_json="[]", + ) + ).insert() + + result = get(chart_name="Test Group By Dashboard Chart", refresh=1) + todo_status_count = xhiveframework.db.count("ToDo", {"status": result.get("labels")[0]}) + + self.assertEqual(result.get("datasets")[0].get("values")[0], todo_status_count) + + def test_daily_dashboard_chart(self): + insert_test_records() + + if xhiveframework.db.exists("Dashboard Chart", "Test Daily Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Daily Dashboard Chart") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Daily Dashboard Chart", + chart_type="Sum", + document_type="Communication", + based_on="communication_date", + value_based_on="rating", + timespan="Select Date Range", + time_interval="Daily", + from_date=datetime(2019, 1, 6), + to_date=datetime(2019, 1, 11), + filters_json="[]", + timeseries=1, + ) + ).insert() + + result = get(chart_name="Test Daily Dashboard Chart", refresh=1) + + self.assertEqual(result.get("datasets")[0].get("values"), [200.0, 400.0, 300.0, 0.0, 100.0, 0.0]) + self.assertEqual( + result.get("labels"), + ["01-06-2019", "01-07-2019", "01-08-2019", "01-09-2019", "01-10-2019", "01-11-2019"], + ) + + def test_weekly_dashboard_chart(self): + insert_test_records() + + if xhiveframework.db.exists("Dashboard Chart", "Test Weekly Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Weekly Dashboard Chart") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Weekly Dashboard Chart", + chart_type="Sum", + document_type="Communication", + based_on="communication_date", + value_based_on="rating", + timespan="Select Date Range", + time_interval="Weekly", + from_date=datetime(2018, 12, 30), + to_date=datetime(2019, 1, 15), + filters_json="[]", + timeseries=1, + ) + ).insert() + + with patch.object(xhiveframework.utils.data, "get_first_day_of_the_week", return_value="Monday"): + result = get(chart_name="Test Weekly Dashboard Chart", refresh=1) + + self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 300.0, 800.0, 0.0]) + self.assertEqual(result.get("labels"), ["12-30-2018", "01-06-2019", "01-13-2019", "01-20-2019"]) + + def test_avg_dashboard_chart(self): + insert_test_records() + + if xhiveframework.db.exists("Dashboard Chart", "Test Average Dashboard Chart"): + xhiveframework.delete_doc("Dashboard Chart", "Test Average Dashboard Chart") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Average Dashboard Chart", + chart_type="Average", + document_type="Communication", + based_on="communication_date", + value_based_on="rating", + timespan="Select Date Range", + time_interval="Weekly", + from_date=datetime(2018, 12, 30), + to_date=datetime(2019, 1, 15), + filters_json="[]", + timeseries=1, + ) + ).insert() + + with patch.object(xhiveframework.utils.data, "get_first_day_of_the_week", return_value="Monday"): + result = get(chart_name="Test Average Dashboard Chart", refresh=1) + self.assertEqual(result.get("labels"), ["12-30-2018", "01-06-2019", "01-13-2019", "01-20-2019"]) + self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 150.0, 266.6666666666667, 0.0]) + + def test_user_date_label_dashboard_chart(self): + xhiveframework.delete_doc_if_exists("Dashboard Chart", "Test Dashboard Chart Date Label") + + xhiveframework.get_doc( + dict( + doctype="Dashboard Chart", + chart_name="Test Dashboard Chart Date Label", + chart_type="Count", + document_type="DocType", + based_on="creation", + timespan="Select Date Range", + time_interval="Weekly", + from_date=datetime(2018, 12, 30), + to_date=datetime(2019, 1, 15), + filters_json="[]", + timeseries=1, + ) + ).insert() + + with patch.object(xhiveframework.utils.data, "get_user_date_format", return_value="dd.mm.yyyy"): + result = get(chart_name="Test Dashboard Chart Date Label") + self.assertEqual(sorted(result.get("labels")), sorted(["05.01.2019", "12.01.2019", "19.01.2019"])) + + with patch.object(xhiveframework.utils.data, "get_user_date_format", return_value="mm-dd-yyyy"): + result = get(chart_name="Test Dashboard Chart Date Label") + self.assertEqual(sorted(result.get("labels")), sorted(["01-19-2019", "01-05-2019", "01-12-2019"])) + + +def insert_test_records(): + create_new_communication("Communication 1", datetime(2018, 12, 30), 50) + create_new_communication("Communication 2", datetime(2019, 1, 4), 100) + create_new_communication("Communication 3", datetime(2019, 1, 6), 200) + create_new_communication("Communication 4", datetime(2019, 1, 7), 400) + create_new_communication("Communication 5", datetime(2019, 1, 8), 300) + create_new_communication("Communication 6", datetime(2019, 1, 10), 100) + + +def create_new_communication(subject, date, rating): + communication = { + "doctype": "Communication", + "subject": subject, + "rating": rating, + "communication_date": date, + } + comm = xhiveframework.get_doc(communication) + if not xhiveframework.db.exists("Communication", {"subject": comm.subject}): + comm.insert() diff --git a/xhiveframework/desk/doctype/dashboard_chart_field/__init__.py b/xhiveframework/desk/doctype/dashboard_chart_field/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.json b/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.json new file mode 100644 index 0000000..6347be4 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.json @@ -0,0 +1,37 @@ +{ + "actions": [], + "creation": "2020-02-28 11:40:27.017380", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "y_field", + "color" + ], + "fields": [ + { + "fieldname": "y_field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Y Field" + }, + { + "fieldname": "color", + "fieldtype": "Color", + "in_list_view": 1, + "label": "Color" + } + ], + "istable": 1, + "links": [], + "modified": "2020-02-28 11:48:24.731946", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard Chart Field", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.py b/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.py new file mode 100644 index 0000000..a981537 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_field/dashboard_chart_field.py @@ -0,0 +1,23 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DashboardChartField(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + color: DF.Color | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + y_field: DF.Literal[None] + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/dashboard_chart_link/__init__.py b/xhiveframework/desk/doctype/dashboard_chart_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.json b/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.json new file mode 100644 index 0000000..51b5ed3 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.json @@ -0,0 +1,41 @@ +{ + "actions": [], + "creation": "2019-03-12 15:00:57.052684", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "chart", + "width" + ], + "fields": [ + { + "columns": 8, + "fieldname": "chart", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Chart", + "options": "Dashboard Chart" + }, + { + "default": "Half", + "fieldname": "width", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Width", + "options": "Half\nFull" + } + ], + "istable": 1, + "links": [], + "modified": "2020-03-13 19:23:05.561687", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard Chart Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "ASC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.py b/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.py new file mode 100644 index 0000000..6fd5deb --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_link/dashboard_chart_link.py @@ -0,0 +1,23 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DashboardChartLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + chart: DF.Link | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + width: DF.Literal["Half", "Full"] + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/dashboard_chart_source/__init__.py b/xhiveframework/desk/doctype/dashboard_chart_source/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.js b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.js new file mode 100644 index 0000000..060b300 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.js @@ -0,0 +1,4 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Dashboard Chart Source", {}); diff --git a/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.json b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.json new file mode 100644 index 0000000..fbe0ae9 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.json @@ -0,0 +1,69 @@ +{ + "actions": [], + "autoname": "field:source_name", + "creation": "2019-02-06 07:55:29.579840", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "source_name", + "module", + "timeseries" + ], + "fields": [ + { + "fieldname": "source_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Source Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "module", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Module", + "options": "Module Def", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "timeseries", + "fieldtype": "Check", + "label": "Timeseries" + } + ], + "links": [], + "modified": "2020-06-26 18:00:37.421491", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard Chart Source", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.py b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.py new file mode 100644 index 0000000..476520b --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_source/dashboard_chart_source.py @@ -0,0 +1,38 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import os + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.modules import get_module_path, scrub +from xhiveframework.modules.export_file import export_to_files + + +@xhiveframework.whitelist() +def get_config(name): + doc = xhiveframework.get_doc("Dashboard Chart Source", name) + with open( + os.path.join( + get_module_path(doc.module), "dashboard_chart_source", scrub(doc.name), scrub(doc.name) + ".js" + ), + ) as f: + return f.read() + + +class DashboardChartSource(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + module: DF.Link + source_name: DF.Data + timeseries: DF.Check + + # end: auto-generated types + def on_update(self): + export_to_files(record_list=[[self.doctype, self.name]], record_module=self.module, create_init=True) diff --git a/xhiveframework/desk/doctype/dashboard_chart_source/test_dashboard_chart_source.py b/xhiveframework/desk/doctype/dashboard_chart_source/test_dashboard_chart_source.py new file mode 100644 index 0000000..c7ad25a --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_chart_source/test_dashboard_chart_source.py @@ -0,0 +1,7 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDashboardChartSource(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/dashboard_settings/__init__.py b/xhiveframework/desk/doctype/dashboard_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.js b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.js new file mode 100644 index 0000000..1f444f0 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Dashboard Settings", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.json b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.json new file mode 100644 index 0000000..504cd4b --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.json @@ -0,0 +1,53 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2020-03-31 19:41:45.785014", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user", + "chart_config" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "chart_config", + "fieldtype": "Code", + "label": "Chart Configuration", + "options": "JSON", + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2023-08-28 22:23:42.722543", + "modified_by": "Administrator", + "module": "Desk", + "name": "Dashboard Settings", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.py b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.py new file mode 100644 index 0000000..af32bc0 --- /dev/null +++ b/xhiveframework/desk/doctype/dashboard_settings/dashboard_settings.py @@ -0,0 +1,58 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework + +# import xhiveframework +from xhiveframework.model.document import Document + + +class DashboardSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + chart_config: DF.Code | None + user: DF.Link | None + # end: auto-generated types + pass + + +@xhiveframework.whitelist() +def create_dashboard_settings(user): + if not xhiveframework.db.exists("Dashboard Settings", user): + doc = xhiveframework.new_doc("Dashboard Settings") + doc.name = user + doc.insert(ignore_permissions=True) + xhiveframework.db.commit() + return doc + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + return f"""(`tabDashboard Settings`.name = {xhiveframework.db.escape(user)})""" + + +@xhiveframework.whitelist() +def save_chart_config(reset, config, chart_name): + reset = xhiveframework.parse_json(reset) + doc = xhiveframework.get_doc("Dashboard Settings", xhiveframework.session.user) + chart_config = xhiveframework.parse_json(doc.chart_config) or {} + + if reset: + chart_config[chart_name] = {} + else: + config = xhiveframework.parse_json(config) + if chart_name not in chart_config: + chart_config[chart_name] = {} + chart_config[chart_name].update(config) + + xhiveframework.db.set_value("Dashboard Settings", xhiveframework.session.user, "chart_config", json.dumps(chart_config)) diff --git a/xhiveframework/desk/doctype/desktop_icon/__init__.py b/xhiveframework/desk/doctype/desktop_icon/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/desktop_icon/desktop_icon.js b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.js new file mode 100644 index 0000000..1484a0e --- /dev/null +++ b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Desktop Icon", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/desk/doctype/desktop_icon/desktop_icon.json b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.json new file mode 100644 index 0000000..ef88346 --- /dev/null +++ b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.json @@ -0,0 +1,175 @@ +{ + "actions": [], + "creation": "2016-02-22 03:47:45.387068", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "module_name", + "label", + "standard", + "custom", + "column_break_3", + "app", + "description", + "category", + "hidden", + "blocked", + "force_show", + "section_break_7", + "type", + "_doctype", + "_report", + "link", + "column_break_10", + "color", + "icon", + "reverse", + "idx" + ], + "fields": [ + { + "fieldname": "module_name", + "fieldtype": "Data", + "label": "Module Name" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "label": "Label" + }, + { + "default": "0", + "fieldname": "standard", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Standard" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "label": "Custom", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "app", + "fieldtype": "Data", + "label": "App", + "read_only": 1 + }, + { + "fieldname": "description", + "fieldtype": "Small Text", + "label": "Description" + }, + { + "fieldname": "category", + "fieldtype": "Data", + "label": "Category" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "default": "0", + "fieldname": "blocked", + "fieldtype": "Check", + "label": "Blocked" + }, + { + "default": "0", + "fieldname": "force_show", + "fieldtype": "Check", + "label": "Force Show", + "read_only": 1 + }, + { + "fieldname": "section_break_7", + "fieldtype": "Section Break" + }, + { + "fieldname": "type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Type", + "options": "module\nlist\nlink\npage\nquery-report" + }, + { + "fieldname": "_doctype", + "fieldtype": "Link", + "label": "_doctype", + "options": "DocType" + }, + { + "fieldname": "_report", + "fieldtype": "Link", + "label": "_report", + "options": "Report" + }, + { + "fieldname": "link", + "fieldtype": "Small Text", + "label": "Link" + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "fieldname": "color", + "fieldtype": "Data", + "label": "Color" + }, + { + "fieldname": "icon", + "fieldtype": "Data", + "label": "Icon" + }, + { + "default": "0", + "fieldname": "reverse", + "fieldtype": "Check", + "label": "Reverse Icon Color" + }, + { + "fieldname": "idx", + "fieldtype": "Int", + "label": "Idx" + } + ], + "in_create": 1, + "links": [], + "modified": "2022-08-03 12:20:50.577580", + "modified_by": "Administrator", + "module": "Desk", + "name": "Desktop Icon", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "module_name", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/desktop_icon/desktop_icon.py b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.py new file mode 100644 index 0000000..5aca1b7 --- /dev/null +++ b/xhiveframework/desk/doctype/desktop_icon/desktop_icon.py @@ -0,0 +1,572 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import json +import random + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils.user import UserPermissions + + +class DesktopIcon(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + _doctype: DF.Link | None + _report: DF.Link | None + app: DF.Data | None + blocked: DF.Check + category: DF.Data | None + color: DF.Data | None + custom: DF.Check + description: DF.SmallText | None + force_show: DF.Check + hidden: DF.Check + icon: DF.Data | None + idx: DF.Int + label: DF.Data | None + link: DF.SmallText | None + module_name: DF.Data | None + reverse: DF.Check + standard: DF.Check + type: DF.Literal["module", "list", "link", "page", "query-report"] + + # end: auto-generated types + def validate(self): + if not self.label: + self.label = self.module_name + + def on_trash(self): + clear_desktop_icons_cache() + + +def after_doctype_insert(): + xhiveframework.db.add_unique("Desktop Icon", ("module_name", "owner", "standard")) + + +def get_desktop_icons(user=None): + """Return desktop icons for user""" + if not user: + user = xhiveframework.session.user + + user_icons = xhiveframework.cache.hget("desktop_icons", user) + + if not user_icons: + fields = [ + "module_name", + "hidden", + "label", + "link", + "type", + "icon", + "color", + "description", + "category", + "_doctype", + "_report", + "idx", + "force_show", + "reverse", + "custom", + "standard", + "blocked", + ] + + active_domains = xhiveframework.get_active_domains() + + blocked_doctypes = xhiveframework.get_all( + "DocType", + filters={"ifnull(restrict_to_domain, '')": ("not in", ",".join(active_domains))}, + fields=["name"], + ) + + blocked_doctypes = [d.get("name") for d in blocked_doctypes] + + standard_icons = xhiveframework.get_all("Desktop Icon", fields=fields, filters={"standard": 1}) + + standard_map = {} + for icon in standard_icons: + if icon._doctype in blocked_doctypes: + icon.blocked = 1 + standard_map[icon.module_name] = icon + + user_icons = xhiveframework.get_all("Desktop Icon", fields=fields, filters={"standard": 0, "owner": user}) + + # update hidden property + for icon in user_icons: + standard_icon = standard_map.get(icon.module_name, None) + + if icon._doctype in blocked_doctypes: + icon.blocked = 1 + + # override properties from standard icon + if standard_icon: + for key in ("route", "label", "color", "icon", "link"): + if standard_icon.get(key): + icon[key] = standard_icon.get(key) + + if standard_icon.blocked: + icon.hidden = 1 + + # flag for modules_select dialog + icon.hidden_in_standard = 1 + + elif standard_icon.force_show: + icon.hidden = 0 + + # add missing standard icons (added via new install apps?) + user_icon_names = [icon.module_name for icon in user_icons] + for standard_icon in standard_icons: + if standard_icon.module_name not in user_icon_names: + # if blocked, hidden too! + if standard_icon.blocked: + standard_icon.hidden = 1 + standard_icon.hidden_in_standard = 1 + + user_icons.append(standard_icon) + + user_blocked_modules = xhiveframework.get_doc("User", user).get_blocked_modules() + for icon in user_icons: + if icon.module_name in user_blocked_modules: + icon.hidden = 1 + + # sort by idx + user_icons.sort(key=lambda a: a.idx) + + # translate + for d in user_icons: + if d.label: + d.label = _(d.label, context=d.parent) + + xhiveframework.cache.hset("desktop_icons", user, user_icons) + + return user_icons + + +@xhiveframework.whitelist() +def add_user_icon(_doctype, _report=None, label=None, link=None, type="link", standard=0): + """Add a new user desktop icon to the desktop""" + + if not label: + label = _doctype or _report + if not link: + link = f"List/{_doctype}" + + # find if a standard icon exists + icon_name = xhiveframework.db.exists( + "Desktop Icon", {"standard": standard, "link": link, "owner": xhiveframework.session.user} + ) + + if icon_name: + if xhiveframework.db.get_value("Desktop Icon", icon_name, "hidden"): + # if it is hidden, unhide it + xhiveframework.db.set_value("Desktop Icon", icon_name, "hidden", 0) + clear_desktop_icons_cache() + + else: + idx = ( + xhiveframework.db.sql("select max(idx) from `tabDesktop Icon` where owner=%s", xhiveframework.session.user)[0][0] + or xhiveframework.db.sql("select count(*) from `tabDesktop Icon` where standard=1")[0][0] + ) + + if not xhiveframework.db.get_value("Report", _report): + _report = None + userdefined_icon = xhiveframework.db.get_value( + "DocType", _doctype, ["icon", "color", "module"], as_dict=True + ) + else: + userdefined_icon = xhiveframework.db.get_value( + "Report", _report, ["icon", "color", "module"], as_dict=True + ) + + module_icon = xhiveframework.get_value( + "Desktop Icon", + {"standard": 1, "module_name": userdefined_icon.module}, + ["name", "icon", "color", "reverse"], + as_dict=True, + ) + + if not module_icon: + module_icon = xhiveframework._dict() + opts = random.choice(palette) + module_icon.color = opts[0] + module_icon.reverse = 0 if (len(opts) > 1) else 1 + + try: + new_icon = xhiveframework.get_doc( + { + "doctype": "Desktop Icon", + "label": label, + "module_name": label, + "link": link, + "type": type, + "_doctype": _doctype, + "_report": _report, + "icon": userdefined_icon.icon or module_icon.icon, + "color": userdefined_icon.color or module_icon.color, + "reverse": module_icon.reverse, + "idx": idx + 1, + "custom": 1, + "standard": standard, + } + ).insert(ignore_permissions=True) + clear_desktop_icons_cache() + + icon_name = new_icon.name + + except xhiveframework.UniqueValidationError: + xhiveframework.throw(_("Desktop Icon already exists")) + except Exception as e: + raise e + + return icon_name + + +@xhiveframework.whitelist() +def set_order(new_order, user=None): + """set new order by duplicating user icons (if user is set) or set global order""" + if isinstance(new_order, str): + new_order = json.loads(new_order) + for i, module_name in enumerate(new_order): + if module_name not in ("Explore",): + if user: + icon = get_user_copy(module_name, user) + else: + name = xhiveframework.db.get_value("Desktop Icon", {"standard": 1, "module_name": module_name}) + if name: + icon = xhiveframework.get_doc("Desktop Icon", name) + else: + # standard icon missing, create one for DocType + name = add_user_icon(module_name, standard=1) + icon = xhiveframework.get_doc("Desktop Icon", name) + + icon.db_set("idx", i) + + clear_desktop_icons_cache() + + +def set_desktop_icons(visible_list, ignore_duplicate=True): + """Resets all lists and makes only the given one standard, + if the desktop icon does not exist and the name is a DocType, then will create + an icon for the doctype""" + + # clear all custom only if setup is not complete + if not int(xhiveframework.defaults.get_defaults().setup_complete or 0): + xhiveframework.db.delete("Desktop Icon", {"standard": 0}) + + # set standard as blocked and hidden if setting first active domain + if not xhiveframework.flags.keep_desktop_icons: + xhiveframework.db.sql("update `tabDesktop Icon` set blocked=0, hidden=1 where standard=1") + + # set as visible if present, or add icon + for module_name in list(visible_list): + name = xhiveframework.db.get_value("Desktop Icon", {"module_name": module_name}) + if name: + xhiveframework.db.set_value("Desktop Icon", name, "hidden", 0) + else: + if xhiveframework.db.exists("DocType", module_name): + try: + add_user_icon(module_name, standard=1) + except xhiveframework.UniqueValidationError as e: + if not ignore_duplicate: + raise e + else: + visible_list.remove(module_name) + xhiveframework.clear_last_message() + + # set the order + set_order(visible_list) + + clear_desktop_icons_cache() + + +def set_hidden_list(hidden_list, user=None): + """Sets property `hidden`=1 in **Desktop Icon** for given user. + If user is None then it will set global values. + It will also set the rest of the icons as shown (`hidden` = 0)""" + if isinstance(hidden_list, str): + hidden_list = json.loads(hidden_list) + + # set as hidden + for module_name in hidden_list: + set_hidden(module_name, user, 1) + + # set as seen + for module_name in list(set(get_all_icons()) - set(hidden_list)): + set_hidden(module_name, user, 0) + + if user: + clear_desktop_icons_cache() + else: + xhiveframework.clear_cache() + + +def set_hidden(module_name, user=None, hidden=1): + """Set module hidden property for given user. If user is not specified, + hide/unhide it globally""" + if user: + icon = get_user_copy(module_name, user) + + if hidden and icon.custom: + xhiveframework.delete_doc(icon.doctype, icon.name, ignore_permissions=True) + return + + # hidden by user + icon.db_set("hidden", hidden) + else: + icon = xhiveframework.get_doc("Desktop Icon", {"standard": 1, "module_name": module_name}) + + # blocked is globally hidden + icon.db_set("blocked", hidden) + + +def get_all_icons(): + return [ + d.module_name for d in xhiveframework.get_all("Desktop Icon", filters={"standard": 1}, fields=["module_name"]) + ] + + +def clear_desktop_icons_cache(user=None): + xhiveframework.cache.hdel("desktop_icons", user or xhiveframework.session.user) + xhiveframework.cache.hdel("bootinfo", user or xhiveframework.session.user) + + +def get_user_copy(module_name, user=None): + """Return user copy (Desktop Icon) of the given module_name. If user copy does not exist, create one. + + :param module_name: Name of the module + :param user: User for which the copy is required (optional) + """ + if not user: + user = xhiveframework.session.user + + desktop_icon_name = xhiveframework.db.get_value( + "Desktop Icon", {"module_name": module_name, "owner": user, "standard": 0} + ) + + if desktop_icon_name: + return xhiveframework.get_doc("Desktop Icon", desktop_icon_name) + else: + return make_user_copy(module_name, user) + + +def make_user_copy(module_name, user): + """Insert and return the user copy of a standard Desktop Icon""" + standard_name = xhiveframework.db.get_value("Desktop Icon", {"module_name": module_name, "standard": 1}) + + if not standard_name: + xhiveframework.throw(_("{0} not found").format(module_name), xhiveframework.DoesNotExistError) + + original = xhiveframework.get_doc("Desktop Icon", standard_name) + + desktop_icon = xhiveframework.get_doc( + {"doctype": "Desktop Icon", "standard": 0, "owner": user, "module_name": module_name} + ) + + for key in ( + "app", + "label", + "route", + "type", + "_doctype", + "idx", + "reverse", + "force_show", + "link", + "icon", + "color", + ): + if original.get(key): + desktop_icon.set(key, original.get(key)) + + desktop_icon.insert(ignore_permissions=True) + + return desktop_icon + + +def sync_desktop_icons(): + """Sync desktop icons from all apps""" + for app in xhiveframework.get_installed_apps(): + sync_from_app(app) + + +def sync_from_app(app): + """Sync desktop icons from app. To be called during install""" + try: + modules = xhiveframework.get_attr(app + ".config.desktop.get_data")() or {} + except ImportError: + return [] + + if isinstance(modules, dict): + modules_list = [] + for m, desktop_icon in modules.items(): + desktop_icon["module_name"] = m + modules_list.append(desktop_icon) + else: + modules_list = modules + + for i, m in enumerate(modules_list): + desktop_icon_name = xhiveframework.db.get_value( + "Desktop Icon", {"module_name": m["module_name"], "app": app, "standard": 1} + ) + if desktop_icon_name: + desktop_icon = xhiveframework.get_doc("Desktop Icon", desktop_icon_name) + else: + # new icon + desktop_icon = xhiveframework.get_doc( + {"doctype": "Desktop Icon", "idx": i, "standard": 1, "app": app, "owner": "Administrator"} + ) + + if "doctype" in m: + m["_doctype"] = m.pop("doctype") + + desktop_icon.update(m) + try: + desktop_icon.save() + except xhiveframework.exceptions.UniqueValidationError: + pass + + return modules_list + + +@xhiveframework.whitelist() +def update_icons(hidden_list, user=None): + """update modules""" + if not user: + xhiveframework.only_for("System Manager") + + set_hidden_list(hidden_list, user) + xhiveframework.msgprint(xhiveframework._("Updated"), indicator="green", title=_("Success"), alert=True) + + +def get_context(context): + context.icons = get_user_icons(xhiveframework.session.user) + context.user = xhiveframework.session.user + + if "System Manager" in xhiveframework.get_roles(): + context.users = xhiveframework.get_all( + "User", + filters={"user_type": "System User", "enabled": 1}, + fields=["name", "first_name", "last_name"], + ) + + +@xhiveframework.whitelist() +def get_module_icons(user=None): + if user != xhiveframework.session.user: + xhiveframework.only_for("System Manager") + + if not user: + icons = xhiveframework.get_all("Desktop Icon", fields="*", filters={"standard": 1}, order_by="idx") + else: + xhiveframework.cache.hdel("desktop_icons", user) + icons = get_user_icons(user) + + for icon in icons: + icon.value = xhiveframework.db.escape(_(icon.label or icon.module_name)) + + return {"icons": icons, "user": user} + + +def get_user_icons(user): + """Get user icons for module setup page""" + user_perms = UserPermissions(user) + user_perms.build_permissions() + + from xhiveframework.boot import get_allowed_pages + + allowed_pages = get_allowed_pages() + + icons = [] + for icon in get_desktop_icons(user): + add = True + if icon.hidden_in_standard: + add = False + + if not icon.custom: + if icon.module_name == ["Help", "Settings"]: + pass + + elif icon.type == "page" and icon.link not in allowed_pages: + add = False + + elif icon.type == "module" and icon.module_name not in user_perms.allow_modules: + add = False + + if add: + icons.append(icon) + + return icons + + +palette = ( + ("#FFC4C4",), + ("#FFE8CD",), + ("#FFD2C2",), + ("#FF8989",), + ("#FFD19C",), + ("#FFA685",), + ("#FF4D4D", 1), + ("#FFB868",), + ("#FF7846", 1), + ("#A83333", 1), + ("#A87945", 1), + ("#A84F2E", 1), + ("#D2D2FF",), + ("#F8D4F8",), + ("#DAC7FF",), + ("#A3A3FF",), + ("#F3AAF0",), + ("#B592FF",), + ("#7575FF", 1), + ("#EC7DEA", 1), + ("#8E58FF", 1), + ("#4D4DA8", 1), + ("#934F92", 1), + ("#5E3AA8", 1), + ("#EBF8CC",), + ("#FFD7D7",), + ("#D2F8ED",), + ("#D9F399",), + ("#FFB1B1",), + ("#A4F3DD",), + ("#C5EC63",), + ("#FF8989", 1), + ("#77ECCA",), + ("#7B933D", 1), + ("#A85B5B", 1), + ("#49937E", 1), + ("#FFFACD",), + ("#D2F1FF",), + ("#CEF6D1",), + ("#FFF69C",), + ("#A6E4FF",), + ("#9DECA2",), + ("#FFF168",), + ("#78D6FF",), + ("#6BE273",), + ("#A89F45", 1), + ("#4F8EA8", 1), + ("#428B46", 1), +) + + +@xhiveframework.whitelist() +def hide(name, user=None): + if not user: + user = xhiveframework.session.user + + try: + set_hidden(name, user, hidden=1) + clear_desktop_icons_cache() + except Exception: + return False + + return True diff --git a/xhiveframework/desk/doctype/event/README.md b/xhiveframework/desk/doctype/event/README.md new file mode 100644 index 0000000..571c78c --- /dev/null +++ b/xhiveframework/desk/doctype/event/README.md @@ -0,0 +1 @@ +Calendar Event \ No newline at end of file diff --git a/xhiveframework/desk/doctype/event/__init__.py b/xhiveframework/desk/doctype/event/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/desk/doctype/event/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/desk/doctype/event/event.js b/xhiveframework/desk/doctype/event/event.js new file mode 100644 index 0000000..ede962b --- /dev/null +++ b/xhiveframework/desk/doctype/event/event.js @@ -0,0 +1,117 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt +xhiveframework.provide("xhiveframework.desk"); + +xhiveframework.ui.form.on("Event", { + onload: function (frm) { + frm.set_query("reference_doctype", "event_participants", function () { + return { + filters: { + issingle: 0, + }, + }; + }); + frm.set_query("google_calendar", function () { + return { + filters: { + owner: xhiveframework.session.user, + }, + }; + }); + }, + refresh: function (frm) { + if (frm.doc.event_participants) { + frm.doc.event_participants.forEach((value) => { + frm.add_custom_button( + __(value.reference_docname), + function () { + xhiveframework.set_route("Form", value.reference_doctype, value.reference_docname); + }, + __("Participants") + ); + }); + } + + frm.page.set_inner_btn_group_as_primary(__("Add Participants")); + + frm.add_custom_button( + __("Add Contacts"), + function () { + new xhiveframework.desk.eventParticipants(frm, "Contact"); + }, + __("Add Participants") + ); + + const [ends_on_date] = frm.doc.ends_on + ? frm.doc.ends_on.split(" ") + : frm.doc.starts_on?.split(" ") || []; + + if ( + ends_on_date && + frm.doc.google_meet_link && + xhiveframework.datetime.now_date() <= ends_on_date + ) { + frm.dashboard.set_headline( + __("Join video conference with {0}", [ + `Google Meet`, + ]) + ); + } + }, + repeat_on: function (frm) { + if (frm.doc.repeat_on === "Every Day") { + ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"].map( + function (v) { + frm.set_value(v, 1); + } + ); + } + }, +}); + +xhiveframework.ui.form.on("Event Participants", { + event_participants_remove: function (frm, cdt, cdn) { + if (cdt && !cdn.includes("New Event Participants")) { + xhiveframework.call({ + type: "POST", + method: "xhiveframework.desk.doctype.event.event.delete_communication", + args: { + event: frm.doc, + reference_doctype: cdt, + reference_docname: cdn, + }, + freeze: true, + callback: function (r) { + if (r.exc) { + xhiveframework.show_alert({ + message: __("{0}", [r.exc]), + indicator: "orange", + }); + } + }, + }); + } + }, +}); + +xhiveframework.desk.eventParticipants = class eventParticipants { + constructor(frm, doctype) { + this.frm = frm; + this.doctype = doctype; + this.make(); + } + + make() { + let me = this; + + let table = me.frm.get_field("event_participants").grid; + new xhiveframework.ui.form.LinkSelector({ + doctype: me.doctype, + dynamic_link_field: "reference_doctype", + dynamic_link_reference: me.doctype, + fieldname: "reference_docname", + target: table, + txt: "", + }); + } +}; diff --git a/xhiveframework/desk/doctype/event/event.json b/xhiveframework/desk/doctype/event/event.json new file mode 100644 index 0000000..3a4f192 --- /dev/null +++ b/xhiveframework/desk/doctype/event/event.json @@ -0,0 +1,339 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "EV.#####", + "creation": "2013-06-10 13:17:47", + "doctype": "DocType", + "document_type": "Document", + "email_append_to": 1, + "engine": "InnoDB", + "field_order": [ + "details", + "subject", + "event_category", + "event_type", + "color", + "send_reminder", + "repeat_this_event", + "column_break_4", + "starts_on", + "ends_on", + "status", + "sender", + "all_day", + "sync_with_google_calendar", + "add_video_conferencing", + "sb_00", + "google_calendar", + "google_calendar_id", + "cb_00", + "google_calendar_event_id", + "google_meet_link", + "pulled_from_google_calendar", + "section_break_13", + "repeat_on", + "repeat_till", + "column_break_16", + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + "sunday", + "section_break_8", + "description", + "participants", + "event_participants" + ], + "fields": [ + { + "fieldname": "details", + "fieldtype": "Section Break", + "label": "Details", + "oldfieldtype": "Section Break" + }, + { + "fieldname": "subject", + "fieldtype": "Small Text", + "in_global_search": 1, + "in_list_view": 1, + "label": "Subject", + "reqd": 1 + }, + { + "fieldname": "event_category", + "fieldtype": "Select", + "label": "Event Category", + "options": "Event\nMeeting\nCall\nSent/Received Email\nOther" + }, + { + "fieldname": "event_type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Event Type", + "oldfieldname": "event_type", + "oldfieldtype": "Select", + "options": "Private\nPublic", + "reqd": 1, + "search_index": 1 + }, + { + "default": "1", + "fieldname": "send_reminder", + "fieldtype": "Check", + "label": "Send an email reminder in the morning" + }, + { + "default": "0", + "fieldname": "repeat_this_event", + "fieldtype": "Check", + "label": "Repeat this Event" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "starts_on", + "fieldtype": "Datetime", + "label": "Starts on", + "reqd": 1 + }, + { + "fieldname": "ends_on", + "fieldtype": "Datetime", + "label": "Ends on" + }, + { + "default": "0", + "fieldname": "all_day", + "fieldtype": "Check", + "label": "All Day" + }, + { + "depends_on": "repeat_this_event", + "fieldname": "section_break_13", + "fieldtype": "Section Break" + }, + { + "depends_on": "repeat_this_event", + "fieldname": "repeat_on", + "fieldtype": "Select", + "in_global_search": 1, + "label": "Repeat On", + "options": "\nDaily\nWeekly\nMonthly\nYearly" + }, + { + "depends_on": "repeat_this_event", + "description": "Leave blank to repeat always", + "fieldname": "repeat_till", + "fieldtype": "Date", + "label": "Repeat Till" + }, + { + "fieldname": "column_break_16", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "monday", + "fieldtype": "Check", + "label": "Monday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "tuesday", + "fieldtype": "Check", + "label": "Tuesday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "wednesday", + "fieldtype": "Check", + "label": "Wednesday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "thursday", + "fieldtype": "Check", + "label": "Thursday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "friday", + "fieldtype": "Check", + "label": "Friday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "saturday", + "fieldtype": "Check", + "label": "Saturday" + }, + { + "default": "0", + "depends_on": "eval:doc.repeat_this_event && doc.repeat_on===\"Weekly\"", + "fieldname": "sunday", + "fieldtype": "Check", + "label": "Sunday" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "color", + "fieldtype": "Color", + "label": "Color" + }, + { + "fieldname": "description", + "fieldtype": "Text Editor", + "in_global_search": 1, + "label": "Description", + "oldfieldname": "description", + "oldfieldtype": "Text", + "print_width": "300px", + "width": "300px" + }, + { + "fieldname": "participants", + "fieldtype": "Section Break", + "label": "Participants", + "oldfieldtype": "Section Break" + }, + { + "fieldname": "event_participants", + "fieldtype": "Table", + "label": "Event Participants", + "options": "Event Participants" + }, + { + "default": "Open", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Open\nCompleted\nClosed\nCancelled" + }, + { + "collapsible": 1, + "depends_on": "eval:doc.sync_with_google_calendar || doc.pulled_from_google_calendar", + "fieldname": "sb_00", + "fieldtype": "Section Break", + "label": "Google Calendar" + }, + { + "fetch_from": "google_calendar.google_calendar_id", + "fieldname": "google_calendar_id", + "fieldtype": "Data", + "label": "Google Calendar ID", + "read_only": 1 + }, + { + "fieldname": "cb_00", + "fieldtype": "Column Break" + }, + { + "fieldname": "google_calendar_event_id", + "fieldtype": "Data", + "label": "Google Calendar Event ID", + "no_copy": 1, + "read_only": 1 + }, + { + "default": "0", + "fieldname": "sync_with_google_calendar", + "fieldtype": "Check", + "label": "Sync with Google Calendar" + }, + { + "fieldname": "google_calendar", + "fieldtype": "Link", + "label": "Google Calendar", + "options": "Google Calendar" + }, + { + "default": "0", + "fieldname": "pulled_from_google_calendar", + "fieldtype": "Check", + "label": "Pulled from Google Calendar", + "read_only": 1 + }, + { + "fieldname": "sender", + "fieldtype": "Data", + "label": "Sender", + "options": "Email", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "eval:doc.sync_with_google_calendar", + "description": "via Google Meet", + "fieldname": "add_video_conferencing", + "fieldtype": "Check", + "label": "Add Video Conferencing" + }, + { + "fieldname": "google_meet_link", + "fieldtype": "Data", + "label": "Google Meet Link", + "no_copy": 1, + "read_only": 1 + } + ], + "icon": "fa fa-calendar", + "idx": 1, + "links": [], + "modified": "2023-06-23 10:33:15.685368", + "modified_by": "Administrator", + "module": "Desk", + "name": "Event", + "naming_rule": "Expression (old style)", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sender_field": "sender", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "subject_field": "subject", + "title_field": "subject", + "track_changes": 1, + "track_seen": 1, + "track_views": 1 +} diff --git a/xhiveframework/desk/doctype/event/event.py b/xhiveframework/desk/doctype/event/event.py new file mode 100644 index 0000000..c29ac14 --- /dev/null +++ b/xhiveframework/desk/doctype/event/event.py @@ -0,0 +1,482 @@ +# Copyright (c) 2018, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.contacts.doctype.contact.contact import get_default_contact +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + is_email_notifications_enabled_for_type, +) +from xhiveframework.desk.reportview import get_filters_cond +from xhiveframework.model.document import Document +from xhiveframework.utils import ( + add_days, + add_months, + cint, + cstr, + date_diff, + format_datetime, + get_datetime_str, + getdate, + now_datetime, + nowdate, +) +from xhiveframework.utils.user import get_enabled_system_users + +weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] +communication_mapping = { + "": "Event", + "Event": "Event", + "Meeting": "Meeting", + "Call": "Phone", + "Sent/Received Email": "Email", + "Other": "Other", +} + + +class Event(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.event_participants.event_participants import EventParticipants + from xhiveframework.types import DF + + add_video_conferencing: DF.Check + all_day: DF.Check + color: DF.Color | None + description: DF.TextEditor | None + ends_on: DF.Datetime | None + event_category: DF.Literal["Event", "Meeting", "Call", "Sent/Received Email", "Other"] + event_participants: DF.Table[EventParticipants] + event_type: DF.Literal["Private", "Public"] + friday: DF.Check + google_calendar: DF.Link | None + google_calendar_event_id: DF.Data | None + google_calendar_id: DF.Data | None + google_meet_link: DF.Data | None + monday: DF.Check + pulled_from_google_calendar: DF.Check + repeat_on: DF.Literal["", "Daily", "Weekly", "Monthly", "Yearly"] + repeat_this_event: DF.Check + repeat_till: DF.Date | None + saturday: DF.Check + send_reminder: DF.Check + sender: DF.Data | None + starts_on: DF.Datetime + status: DF.Literal["Open", "Completed", "Closed", "Cancelled"] + subject: DF.SmallText + sunday: DF.Check + sync_with_google_calendar: DF.Check + thursday: DF.Check + tuesday: DF.Check + wednesday: DF.Check + + # end: auto-generated types + def validate(self): + if not self.starts_on: + self.starts_on = now_datetime() + + # if start == end this scenario doesn't make sense i.e. it starts and ends at the same second! + self.ends_on = None if self.starts_on == self.ends_on else self.ends_on + + if self.starts_on and self.ends_on: + self.validate_from_to_dates("starts_on", "ends_on") + + if self.repeat_on == "Daily" and self.ends_on and getdate(self.starts_on) != getdate(self.ends_on): + xhiveframework.throw(_("Daily Events should finish on the Same Day.")) + + if self.sync_with_google_calendar and not self.google_calendar: + xhiveframework.throw(_("Select Google Calendar to which event should be synced.")) + + if not self.sync_with_google_calendar: + self.add_video_conferencing = 0 + + def before_save(self): + self.set_participants_email() + + def on_update(self): + self.sync_communication() + + def on_trash(self): + communications = xhiveframework.get_all( + "Communication", dict(reference_doctype=self.doctype, reference_name=self.name) + ) + if communications: + for communication in communications: + xhiveframework.delete_doc_if_exists("Communication", communication.name) + + def sync_communication(self): + if self.event_participants: + for participant in self.event_participants: + filters = [ + ["Communication", "reference_doctype", "=", self.doctype], + ["Communication", "reference_name", "=", self.name], + ["Communication Link", "link_doctype", "=", participant.reference_doctype], + ["Communication Link", "link_name", "=", participant.reference_docname], + ] + if comms := xhiveframework.get_all("Communication", filters=filters, fields=["name"], distinct=True): + for comm in comms: + communication = xhiveframework.get_doc("Communication", comm.name) + self.update_communication(participant, communication) + else: + meta = xhiveframework.get_meta(participant.reference_doctype) + if hasattr(meta, "allow_events_in_timeline") and meta.allow_events_in_timeline == 1: + self.create_communication(participant) + + def create_communication(self, participant): + communication = xhiveframework.new_doc("Communication") + self.update_communication(participant, communication) + self.communication = communication.name + + def update_communication(self, participant, communication): + communication.communication_medium = "Event" + communication.subject = self.subject + communication.content = self.description if self.description else self.subject + communication.communication_date = self.starts_on + communication.sender = self.owner + communication.sender_full_name = xhiveframework.utils.get_fullname(self.owner) + communication.reference_doctype = self.doctype + communication.reference_name = self.name + communication.communication_medium = ( + communication_mapping.get(self.event_category) if self.event_category else "" + ) + communication.status = "Linked" + communication.add_link(participant.reference_doctype, participant.reference_docname) + communication.save(ignore_permissions=True) + + def add_participant(self, doctype, docname): + """Add a single participant to event participants + + Args: + doctype (string): Reference Doctype + docname (string): Reference Docname + """ + self.append( + "event_participants", + { + "reference_doctype": doctype, + "reference_docname": docname, + }, + ) + + def add_participants(self, participants): + """Add participant entry + + Args: + participants ([Array]): Array of a dict with doctype and docname + """ + for participant in participants: + self.add_participant(participant["doctype"], participant["docname"]) + + def set_participants_email(self): + for participant in self.event_participants: + if participant.email: + continue + + if participant.reference_doctype != "Contact": + participant_contact = get_default_contact( + participant.reference_doctype, participant.reference_docname + ) + else: + participant_contact = participant.reference_docname + + participant.email = ( + xhiveframework.get_value("Contact", participant_contact, "email_id") if participant_contact else None + ) + + +@xhiveframework.whitelist() +def delete_communication(event, reference_doctype, reference_docname): + deleted_participant = xhiveframework.get_doc(reference_doctype, reference_docname) + if isinstance(event, str): + event = json.loads(event) + + filters = [ + ["Communication", "reference_doctype", "=", event.get("doctype")], + ["Communication", "reference_name", "=", event.get("name")], + ["Communication Link", "link_doctype", "=", deleted_participant.reference_doctype], + ["Communication Link", "link_name", "=", deleted_participant.reference_docname], + ] + + comms = xhiveframework.get_list("Communication", filters=filters, fields=["name"]) + + if comms: + deletion = [] + for comm in comms: + delete = xhiveframework.get_doc("Communication", comm.name).delete() + deletion.append(delete) + + return deletion + + return {} + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + return f"""(`tabEvent`.`event_type`='Public' or `tabEvent`.`owner`={xhiveframework.db.escape(user)})""" + + +def has_permission(doc, user): + if doc.event_type == "Public" or doc.owner == user: + return True + + return False + + +def send_event_digest(): + today = nowdate() + + # select only those users that have event reminder email notifications enabled + users = [ + user + for user in get_enabled_system_users() + if is_email_notifications_enabled_for_type(user.name, "Event Reminders") + ] + + for user in users: + events = get_events(today, today, user.name, for_reminder=True) + if events: + xhiveframework.set_user_lang(user.name, user.language) + + for e in events: + e.starts_on = format_datetime(e.starts_on, "hh:mm a") + if e.all_day: + e.starts_on = "All Day" + + xhiveframework.sendmail( + recipients=user.email, + subject=xhiveframework._("Upcoming Events for Today"), + template="upcoming_events", + args={ + "events": events, + }, + header=[xhiveframework._("Events in Today's Calendar"), "blue"], + ) + + +@xhiveframework.whitelist() +def get_events(start, end, user=None, for_reminder=False, filters=None) -> list[xhiveframework._dict]: + if not user: + user = xhiveframework.session.user + + if isinstance(filters, str): + filters = json.loads(filters) + + filter_condition = get_filters_cond("Event", filters, []) + + tables = ["`tabEvent`"] + if "`tabEvent Participants`" in filter_condition: + tables.append("`tabEvent Participants`") + + events = xhiveframework.db.sql( + """ + SELECT `tabEvent`.name, + `tabEvent`.subject, + `tabEvent`.description, + `tabEvent`.color, + `tabEvent`.starts_on, + `tabEvent`.ends_on, + `tabEvent`.owner, + `tabEvent`.all_day, + `tabEvent`.event_type, + `tabEvent`.repeat_this_event, + `tabEvent`.repeat_on, + `tabEvent`.repeat_till, + `tabEvent`.monday, + `tabEvent`.tuesday, + `tabEvent`.wednesday, + `tabEvent`.thursday, + `tabEvent`.friday, + `tabEvent`.saturday, + `tabEvent`.sunday + FROM {tables} + WHERE ( + ( + (date(`tabEvent`.starts_on) BETWEEN date(%(start)s) AND date(%(end)s)) + OR (date(`tabEvent`.ends_on) BETWEEN date(%(start)s) AND date(%(end)s)) + OR ( + date(`tabEvent`.starts_on) <= date(%(start)s) + AND date(`tabEvent`.ends_on) >= date(%(end)s) + ) + ) + OR ( + date(`tabEvent`.starts_on) <= date(%(start)s) + AND `tabEvent`.repeat_this_event=1 + AND coalesce(`tabEvent`.repeat_till, '3000-01-01') > date(%(start)s) + ) + ) + {reminder_condition} + {filter_condition} + AND ( + `tabEvent`.event_type='Public' + OR `tabEvent`.owner=%(user)s + OR EXISTS( + SELECT `tabDocShare`.name + FROM `tabDocShare` + WHERE `tabDocShare`.share_doctype='Event' + AND `tabDocShare`.share_name=`tabEvent`.name + AND `tabDocShare`.user=%(user)s + ) + ) + AND `tabEvent`.status='Open' + ORDER BY `tabEvent`.starts_on""".format( + tables=", ".join(tables), + filter_condition=filter_condition, + reminder_condition="AND coalesce(`tabEvent`.send_reminder, 0)=1" if for_reminder else "", + ), + { + "start": start, + "end": end, + "user": user, + }, + as_dict=1, + ) + + # process recurring events + start = start.split(" ", 1)[0] + end = end.split(" ", 1)[0] + add_events = [] + remove_events = [] + + def add_event(e, date): + new_event = e.copy() + + enddate = ( + add_days(date, int(date_diff(e.ends_on.split(" ", 1)[0], e.starts_on.split(" ", 1)[0]))) + if (e.starts_on and e.ends_on) + else date + ) + + new_event.starts_on = date + " " + e.starts_on.split(" ")[1] + new_event.ends_on = new_event.ends_on = enddate + " " + e.ends_on.split(" ")[1] if e.ends_on else None + + add_events.append(new_event) + + for e in events: + if e.repeat_this_event: + e.starts_on = get_datetime_str(e.starts_on) + e.ends_on = get_datetime_str(e.ends_on) if e.ends_on else None + + event_start, time_str = get_datetime_str(e.starts_on).split(" ") + + repeat = "3000-01-01" if cstr(e.repeat_till) == "" else e.repeat_till + + if e.repeat_on == "Yearly": + start_year = cint(start.split("-", 1)[0]) + end_year = cint(end.split("-", 1)[0]) + + # creates a string with date (27) and month (07) eg: 07-27 + event_start = "-".join(event_start.split("-")[1:]) + + # repeat for all years in period + for year in range(start_year, end_year + 1): + date = str(year) + "-" + event_start + if ( + getdate(date) >= getdate(start) + and getdate(date) <= getdate(end) + and getdate(date) <= getdate(repeat) + ): + add_event(e, date) + + remove_events.append(e) + + if e.repeat_on == "Monthly": + # creates a string with date (27) and month (07) and year (2019) eg: 2019-07-27 + year, month = start.split("-", maxsplit=2)[:2] + date = f"{year}-{month}-" + event_start.split("-", maxsplit=3)[2] + + # last day of month issue, start from prev month! + try: + getdate(date) + except Exception: + date = date.split("-") + date = date[0] + "-" + str(cint(date[1]) - 1) + "-" + date[2] + + start_from = date + for i in range(int(date_diff(end, start) / 30) + 3): + if ( + getdate(date) >= getdate(start) + and getdate(date) <= getdate(end) + and getdate(date) <= getdate(repeat) + and getdate(date) >= getdate(event_start) + ): + add_event(e, date) + + date = add_months(start_from, i + 1) + remove_events.append(e) + + if e.repeat_on == "Weekly": + for cnt in range(date_diff(end, start) + 1): + date = add_days(start, cnt) + if ( + getdate(date) >= getdate(start) + and getdate(date) <= getdate(end) + and getdate(date) <= getdate(repeat) + and getdate(date) >= getdate(event_start) + and e[weekdays[getdate(date).weekday()]] + ): + add_event(e, date) + + remove_events.append(e) + + if e.repeat_on == "Daily": + for cnt in range(date_diff(end, start) + 1): + date = add_days(start, cnt) + if ( + getdate(date) >= getdate(event_start) + and getdate(date) <= getdate(end) + and getdate(date) <= getdate(repeat) + ): + add_event(e, date) + + remove_events.append(e) + + for e in remove_events: + events.remove(e) + + events = events + add_events + + for e in events: + # remove weekday properties (to reduce message size) + for w in weekdays: + del e[w] + + return events + + +def delete_events(ref_type, ref_name, delete_event=False): + participations = xhiveframework.get_all( + "Event Participants", + filters={"reference_doctype": ref_type, "reference_docname": ref_name, "parenttype": "Event"}, + fields=["parent", "name"], + ) + + if participations: + for participation in participations: + if delete_event: + xhiveframework.delete_doc("Event", participation.parent, for_reload=True) + else: + total_participants = xhiveframework.get_all( + "Event Participants", filters={"parenttype": "Event", "parent": participation.parent} + ) + + if len(total_participants) <= 1: + xhiveframework.db.delete("Event", {"name": participation.parent}) + xhiveframework.db.delete("Event Participants", {"name": participation.name}) + + +# Close events if ends_on or repeat_till is less than now_datetime +def set_status_of_events(): + events = xhiveframework.get_list("Event", filters={"status": "Open"}, fields=["name", "ends_on", "repeat_till"]) + for event in events: + if (event.ends_on and getdate(event.ends_on) < getdate(nowdate())) or ( + event.repeat_till and getdate(event.repeat_till) < getdate(nowdate()) + ): + xhiveframework.db.set_value("Event", event.name, "status", "Closed") diff --git a/xhiveframework/desk/doctype/event/event_calendar.js b/xhiveframework/desk/doctype/event/event_calendar.js new file mode 100644 index 0000000..ceaba7a --- /dev/null +++ b/xhiveframework/desk/doctype/event/event_calendar.js @@ -0,0 +1,16 @@ +xhiveframework.views.calendar["Event"] = { + field_map: { + start: "starts_on", + end: "ends_on", + id: "name", + allDay: "all_day", + title: "subject", + status: "event_type", + color: "color", + }, + style_map: { + Public: "success", + Private: "info", + }, + get_events_method: "xhiveframework.desk.doctype.event.event.get_events", +}; diff --git a/xhiveframework/desk/doctype/event/event_list.js b/xhiveframework/desk/doctype/event/event_list.js new file mode 100644 index 0000000..f60115a --- /dev/null +++ b/xhiveframework/desk/doctype/event/event_list.js @@ -0,0 +1,8 @@ +xhiveframework.listview_settings["Event"] = { + add_fields: ["starts_on", "ends_on"], + onload: function () { + xhiveframework.route_options = { + status: "Open", + }; + }, +}; diff --git a/xhiveframework/desk/doctype/event/test_event.py b/xhiveframework/desk/doctype/event/test_event.py new file mode 100644 index 0000000..556f251 --- /dev/null +++ b/xhiveframework/desk/doctype/event/test_event.py @@ -0,0 +1,138 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +"""Use blog post test to test user permissions logic""" + +import json + +import xhiveframework +import xhiveframework.defaults +from xhiveframework.desk.doctype.event.event import get_events +from xhiveframework.test_runner import make_test_objects +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Event") + + +class TestEvent(XhiveFrameworkTestCase): + def setUp(self): + xhiveframework.db.delete("Event") + make_test_objects("Event", reset=True) + + self.test_records = xhiveframework.get_test_records("Event") + self.test_user = "test1@example.com" + + def tearDown(self): + xhiveframework.set_user("Administrator") + + def test_allowed_public(self): + xhiveframework.set_user(self.test_user) + doc = xhiveframework.get_doc("Event", xhiveframework.db.get_value("Event", {"subject": "_Test Event 1"})) + self.assertTrue(xhiveframework.has_permission("Event", doc=doc)) + + def test_not_allowed_private(self): + xhiveframework.set_user(self.test_user) + doc = xhiveframework.get_doc("Event", xhiveframework.db.get_value("Event", {"subject": "_Test Event 2"})) + self.assertFalse(xhiveframework.has_permission("Event", doc=doc)) + + def test_allowed_private_if_in_event_user(self): + name = xhiveframework.db.get_value("Event", {"subject": "_Test Event 3"}) + xhiveframework.share.add("Event", name, self.test_user, "read") + xhiveframework.set_user(self.test_user) + doc = xhiveframework.get_doc("Event", name) + self.assertTrue(xhiveframework.has_permission("Event", doc=doc)) + xhiveframework.set_user("Administrator") + xhiveframework.share.remove("Event", name, self.test_user) + + def test_event_list(self): + xhiveframework.set_user(self.test_user) + res = xhiveframework.get_list( + "Event", filters=[["Event", "subject", "like", "_Test Event%"]], fields=["name", "subject"] + ) + self.assertEqual(len(res), 1) + subjects = [r.subject for r in res] + self.assertTrue("_Test Event 1" in subjects) + self.assertFalse("_Test Event 3" in subjects) + self.assertFalse("_Test Event 2" in subjects) + + def test_revert_logic(self): + ev = xhiveframework.get_doc(self.test_records[0]).insert() + name = ev.name + + xhiveframework.delete_doc("Event", ev.name) + + # insert again + ev = xhiveframework.get_doc(self.test_records[0]).insert() + + # the name should be same! + self.assertEqual(ev.name, name) + + def test_assign(self): + from xhiveframework.desk.form.assign_to import add + + ev = xhiveframework.get_doc(self.test_records[0]).insert() + + add( + { + "assign_to": ["test@example.com"], + "doctype": "Event", + "name": ev.name, + "description": "Test Assignment", + } + ) + + ev = xhiveframework.get_doc("Event", ev.name) + + self.assertEqual(ev._assign, json.dumps(["test@example.com"])) + + # add another one + add( + { + "assign_to": [self.test_user], + "doctype": "Event", + "name": ev.name, + "description": "Test Assignment", + } + ) + + ev = xhiveframework.get_doc("Event", ev.name) + + self.assertEqual(set(json.loads(ev._assign)), {"test@example.com", self.test_user}) + + # Remove an assignment + todo = xhiveframework.get_doc( + "ToDo", + {"reference_type": ev.doctype, "reference_name": ev.name, "allocated_to": self.test_user}, + ) + todo.status = "Cancelled" + todo.save() + + ev = xhiveframework.get_doc("Event", ev.name) + self.assertEqual(ev._assign, json.dumps(["test@example.com"])) + + # cleanup + ev.delete() + + def test_recurring(self): + ev = xhiveframework.get_doc( + { + "doctype": "Event", + "subject": "_Test Event", + "starts_on": "2014-02-01", + "event_type": "Public", + "repeat_this_event": 1, + "repeat_on": "Yearly", + } + ) + ev.insert() + + ev_list = get_events("2014-02-01", "2014-02-01", "Administrator", for_reminder=True) + self.assertTrue(bool(list(filter(lambda e: e.name == ev.name, ev_list)))) + + ev_list1 = get_events("2015-01-20", "2015-01-20", "Administrator", for_reminder=True) + self.assertFalse(bool(list(filter(lambda e: e.name == ev.name, ev_list1)))) + + ev_list2 = get_events("2014-02-20", "2014-02-20", "Administrator", for_reminder=True) + self.assertFalse(bool(list(filter(lambda e: e.name == ev.name, ev_list2)))) + + ev_list3 = get_events("2015-02-01", "2015-02-01", "Administrator", for_reminder=True) + self.assertTrue(bool(list(filter(lambda e: e.name == ev.name, ev_list3)))) diff --git a/xhiveframework/desk/doctype/event/test_records.json b/xhiveframework/desk/doctype/event/test_records.json new file mode 100644 index 0000000..41d5803 --- /dev/null +++ b/xhiveframework/desk/doctype/event/test_records.json @@ -0,0 +1,23 @@ +[ + { + "doctype": "Event", + "subject":"_Test Event 1", + "starts_on": "2014-01-01", + "event_type": "Public", + "creation": "2014-01-01" + }, + { + "doctype": "Event", + "subject":"_Test Event 2", + "starts_on": "2014-01-01", + "event_type": "Private", + "creation": "2014-01-01" + }, + { + "doctype": "Event", + "subject": "_Test Event 3", + "starts_on": "2014-02-01", + "event_type": "Private", + "creation": "2014-02-01" + } +] diff --git a/xhiveframework/desk/doctype/event_participants/__init__.py b/xhiveframework/desk/doctype/event_participants/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/event_participants/event_participants.json b/xhiveframework/desk/doctype/event_participants/event_participants.json new file mode 100644 index 0000000..bbb0a24 --- /dev/null +++ b/xhiveframework/desk/doctype/event_participants/event_participants.json @@ -0,0 +1,49 @@ +{ + "actions": [], + "creation": "2018-09-21 15:44:58.836156", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "reference_docname", + "email" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "reference_docname", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Reference Name", + "options": "reference_doctype", + "reqd": 1 + }, + { + "fieldname": "email", + "fieldtype": "Data", + "label": "Email", + "options": "Email" + } + ], + "istable": 1, + "links": [], + "modified": "2022-10-18 17:49:33.549459", + "modified_by": "Administrator", + "module": "Desk", + "name": "Event Participants", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/event_participants/event_participants.py b/xhiveframework/desk/doctype/event_participants/event_participants.py new file mode 100644 index 0000000..ffc5eb0 --- /dev/null +++ b/xhiveframework/desk/doctype/event_participants/event_participants.py @@ -0,0 +1,22 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +from xhiveframework.model.document import Document + + +class EventParticipants(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + reference_docname: DF.DynamicLink + reference_doctype: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/form_tour/__init__.py b/xhiveframework/desk/doctype/form_tour/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/form_tour/form_tour.js b/xhiveframework/desk/doctype/form_tour/form_tour.js new file mode 100644 index 0000000..57ff3c9 --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour/form_tour.js @@ -0,0 +1,255 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Form Tour", { + refresh(frm) { + if (frm.doc.is_standard && !xhiveframework.boot.developer_mode) { + frm.trigger("disable_form"); + } + frm.set_query("reference_doctype", () => { + return { filters: { istable: 0 } }; + }); + frm.trigger("reference_doctype"); + frm.set_query("report_name", () => { + if (frm.doc.reference_doctype) { + return { + filters: { + ref_doctype: frm.doc.reference_doctype, + }, + }; + } + return {}; + }); + !frm.is_new() && add_custom_button(frm); + }, + async report_name(frm) { + if (!frm.doc.ui_tour || !frm.doc.report_name) return; + let { message } = await xhiveframework.db.get_value("Report", frm.doc.report_name, "ref_doctype"); + frm.set_value("reference_doctype", message?.ref_doctype || ""); + }, + async before_save(frm) { + if ( + frm.doc.select_view == "List" && + frm.doc.list_name == "Dashboard" && + frm.doc.dashboard_name && + frm.doc.reference_doctype + ) { + xhiveframework.throw( + __("Referance Doctype and Dashboard Name both can't be used at the same time.") + ); + } + frm.doc.ui_tour && (frm.doc.page_route = JSON.stringify(await get_path(frm))); + }, + disable_form: function (frm) { + frm.set_read_only(); + frm.fields + .filter((field) => field.has_input) + .forEach((field) => { + frm.set_df_property(field.df.fieldname, "read_only", "1"); + }); + frm.disable_save(); + }, + + reference_doctype(frm) { + if (!frm.doc.reference_doctype) return; + + frm.set_fields_as_options("fieldname", frm.doc.reference_doctype, (df) => !df.hidden).then( + (options) => { + frm.fields_dict.steps.grid.update_docfield_property( + "fieldname", + "options", + [""].concat(options) + ); + } + ); + + frm.set_fields_as_options( + "parent_fieldname", + frm.doc.reference_doctype, + (df) => df.fieldtype == "Table" && !df.hidden + ).then((options) => { + frm.fields_dict.steps.grid.update_docfield_property( + "parent_fieldname", + "options", + [""].concat(options) + ); + }); + if (!frm.doc.ui_tour) { + // remove report name if reference doctype is changed and report name is not valid. + xhiveframework.db + .get_list( + "Report", + { + filters: { + ref_doctype: frm.doc.reference_doctype, + }, + }, + { fields: ["name"] } + ) + .then((reports) => { + if (reports.findIndex((r) => r.name == frm.doc.report_name) == -1) { + frm.set_value("report_name", ""); + frm.refresh_field("report_name"); + } + }); + } + }, +}); + +let add_custom_button = (frm) => { + if (frm.doc.ui_tour) { + frm.add_custom_button(__("Reset"), function () { + xhiveframework.confirm( + __("This will reset this tour and show it to all users. Are you sure?"), + function () { + xhiveframework.call({ + method: "xhiveframework.desk.doctype.form_tour.form_tour.reset_tour", + args: { + tour_name: frm.doc.name, + }, + }); + delete xhiveframework.boot.user.onboarding_status[frm.doc.name]; + } + ); + }); + } else { + frm.add_custom_button(__("Show Tour"), async () => { + const issingle = await check_if_single(frm.doc.reference_doctype); + let route_changed = null; + + if (issingle) { + route_changed = xhiveframework.set_route("Form", frm.doc.reference_doctype); + } else if (frm.doc.first_document) { + const name = await get_first_document(frm.doc.reference_doctype); + route_changed = xhiveframework.set_route("Form", frm.doc.reference_doctype, name); + } else { + route_changed = xhiveframework.set_route("Form", frm.doc.reference_doctype, "new"); + } + route_changed.then(() => { + const tour_name = frm.doc.name; + cur_frm.tour.init({ tour_name }).then(() => cur_frm.tour.start()); + }); + }); + } +}; + +xhiveframework.ui.form.on("Form Tour Step", { + form_render(frm, cdt, cdn) { + if (locals[cdt][cdn].is_table_field) { + frm.trigger("parent_fieldname", cdt, cdn); + } + }, + parent_fieldname(frm, cdt, cdn) { + const child_row = locals[cdt][cdn]; + + const parent_fieldname_df = xhiveframework + .get_meta(frm.doc.reference_doctype) + .fields.find((df) => df.fieldname == child_row.parent_fieldname); + + frm.set_fields_as_options( + "fieldname", + parent_fieldname_df.options, + (df) => !df.hidden + ).then((options) => { + frm.fields_dict.steps.grid.update_docfield_property( + "fieldname", + "options", + [""].concat(options) + ); + if (child_row.fieldname) { + xhiveframework.model.set_value(cdt, cdn, "fieldname", child_row.fieldname); + } + }); + }, +}); + +async function check_if_single(doctype) { + const { message } = await xhiveframework.db.get_value("DocType", doctype, "issingle"); + return message.issingle || 0; +} +async function check_if_private_workspace(name) { + const { message } = await xhiveframework.db.get_value("Workspace", name, "public"); + return !message.public || 0; +} + +async function get_first_document(doctype) { + let docname; + + await xhiveframework.db.get_list(doctype, { order_by: "creation" }).then((res) => { + if (Array.isArray(res) && res.length) docname = res[0].name; + }); + + return docname || "new"; +} + +async function get_path(frm) { + let route = [frm.doc.view_name]; + switch (route[0]) { + case "Workspaces": + frm.doc.list_name = ""; + frm.doc.new_document_form = 0; + frm.doc.report_name = ""; + frm.doc.page_name = ""; + frm.doc.dashboard_name = ""; + frm.doc.reference_doctype = ""; + if (!frm.doc.workspace_name) { + route.push("*"); + return route; + } + if (await check_if_private_workspace(frm.doc.workspace_name)) { + route.push("private"); + } + route.push(frm.doc.workspace_name); + return route; + case "List": + frm.doc.workspace_name = ""; + frm.doc.new_document_form = 0; + frm.doc.list_name != "Report" && (frm.doc.report_name = ""); + frm.doc.list_name != "Dashboard" && (frm.doc.dashboard_name = ""); + frm.doc.page_name = ""; + if (frm.doc.list_name == "File") return ["List", "File"]; + if (!frm.doc.reference_doctype) { + if (frm.doc.list_name == "Dashboard") + return ["dashboard-view", frm.doc.dashboard_name || "*"]; + route.push("*"); + } else { + route.push(frm.doc.reference_doctype); + } + route.push(frm.doc.list_name); + return route; + case "Form": + frm.doc.workspace_name = ""; + frm.doc.list_name = ""; + frm.doc.report_name = ""; + frm.doc.page_name = ""; + frm.doc.dashboard_name = ""; + if (!frm.doc.reference_doctype) { + route.push("*"); + frm.doc.new_document_form && route.push("new-*"); + return route; + } + route.push(frm.doc.reference_doctype); + if (await check_if_single(frm.doc.reference_doctype)) { + route.push(frm.doc.reference_doctype); + } else if (frm.doc.new_document_form) { + route.push("new-" + xhiveframework.router.slug(frm.doc.reference_doctype)); + } + return route; + case "Tree": + frm.doc.workspace_name = ""; + frm.doc.list_name = ""; + frm.doc.new_document_form = 0; + frm.doc.report_name = ""; + frm.doc.page_name = ""; + frm.doc.dashboard_name = ""; + return route; + case "Page": + frm.doc.workspace_name = ""; + frm.doc.list_name = ""; + frm.doc.new_document_form = 0; + frm.doc.report_name = ""; + frm.doc.dashboard_name = ""; + frm.doc.reference_doctype = ""; + return [frm.doc.page_name]; + } +} diff --git a/xhiveframework/desk/doctype/form_tour/form_tour.json b/xhiveframework/desk/doctype/form_tour/form_tour.json new file mode 100644 index 0000000..6c04999 --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour/form_tour.json @@ -0,0 +1,209 @@ +{ + "actions": [], + "autoname": "field:title", + "creation": "2021-05-21 23:02:52.242721", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "title", + "view_name", + "workspace_name", + "list_name", + "report_name", + "dashboard_name", + "new_document_form", + "page_name", + "reference_doctype", + "module", + "column_break_6", + "ui_tour", + "track_steps", + "is_standard", + "save_on_complete", + "first_document", + "include_name_field", + "page_route", + "section_break_3", + "steps" + ], + "fields": [ + { + "depends_on": "eval:(!doc.ui_tour || doc.ui_tour && [\"Workspaces\", \"Page\", \"Tree\"].indexOf(doc.view_name) == -1);", + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document", + "mandatory_depends_on": "eval:(!doc.ui_tour)", + "options": "DocType" + }, + { + "depends_on": "eval:(doc.ui_tour || doc.reference_doctype)", + "fieldname": "steps", + "fieldtype": "Table", + "label": "Steps", + "options": "Form Tour Step", + "reqd": 1 + }, + { + "fieldname": "section_break_3", + "fieldtype": "Section Break" + }, + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Title", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "save_on_complete", + "fieldtype": "Check", + "label": "Save on Completion" + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard" + }, + { + "depends_on": "eval: doc.ui_tour && doc.is_standard", + "fetch_from": "reference_doctype.module", + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "options": "Module Def", + "read_only": 1 + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "first_document", + "fieldtype": "Check", + "label": "Show First Document Tour" + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour && !doc.first_document)", + "fieldname": "include_name_field", + "fieldtype": "Check", + "label": "Include Name Field" + }, + { + "default": "0", + "fieldname": "ui_tour", + "fieldtype": "Check", + "label": "UI Tour", + "set_only_once": 1 + }, + { + "fieldname": "page_route", + "fieldtype": "Small Text", + "hidden": 1, + "label": "Page Route" + }, + { + "depends_on": "eval:(doc.ui_tour && doc.view_name == \"List\" && doc.list_name == \"Dashboard\")", + "fetch_from": ".", + "fieldname": "dashboard_name", + "fieldtype": "Link", + "label": "Select Dashboard", + "options": "Dashboard" + }, + { + "depends_on": "ui_tour", + "fieldname": "view_name", + "fieldtype": "Select", + "label": "View", + "mandatory_depends_on": "ui_tour", + "options": "Workspaces\nList\nForm\nTree\nPage" + }, + { + "depends_on": "eval:(doc.ui_tour && doc.view_name == \"Workspaces\")", + "fetch_from": ".", + "fieldname": "workspace_name", + "fieldtype": "Link", + "label": "Select Workspace", + "options": "Workspace" + }, + { + "depends_on": "eval:(doc.ui_tour && doc.view_name == \"Page\")", + "fetch_from": ".", + "fieldname": "page_name", + "fieldtype": "Link", + "label": "Select Page", + "mandatory_depends_on": "eval:(doc.ui_tour && doc.view_name == \"Page\")", + "options": "Page" + }, + { + "default": "List", + "depends_on": "eval:(doc.ui_tour && doc.view_name == \"List\")", + "fetch_from": ".", + "fieldname": "list_name", + "fieldtype": "Select", + "label": "Select List View", + "mandatory_depends_on": "eval:(doc.ui_tour && doc.view_name == \"List\")", + "options": "List\nReport\nDashboard\nKanban\nGantt\nCalendar\nFile\nImage\nInbox\nMap" + }, + { + "depends_on": "eval:(doc.ui_tour && doc.view_name == \"List\" && doc.list_name == \"Report\")", + "fetch_from": ".", + "fieldname": "report_name", + "fieldtype": "Link", + "label": "Select Report", + "options": "Report" + }, + { + "default": "0", + "depends_on": "ui_tour", + "description": "The next tour will start from where the user left off.", + "fieldname": "track_steps", + "fieldtype": "Check", + "label": "Track Steps" + }, + { + "default": "0", + "depends_on": "eval: (doc.ui_tour && doc.view_name == \"Form\")", + "fieldname": "new_document_form", + "fieldtype": "Check", + "label": "New Document Form" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 20:24:42.594360", + "modified_by": "Administrator", + "module": "Desk", + "name": "Form Tour", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Desk User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/form_tour/form_tour.py b/xhiveframework/desk/doctype/form_tour/form_tour.py new file mode 100644 index 0000000..4e8024c --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour/form_tour.py @@ -0,0 +1,113 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.modules.export_file import export_to_files + + +class FormTour(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.form_tour_step.form_tour_step import FormTourStep + from xhiveframework.types import DF + + dashboard_name: DF.Link | None + first_document: DF.Check + include_name_field: DF.Check + is_standard: DF.Check + list_name: DF.Literal[ + "List", "Report", "Dashboard", "Kanban", "Gantt", "Calendar", "File", "Image", "Inbox", "Map" + ] + module: DF.Link | None + new_document_form: DF.Check + page_name: DF.Link | None + page_route: DF.SmallText | None + reference_doctype: DF.Link | None + report_name: DF.Link | None + save_on_complete: DF.Check + steps: DF.Table[FormTourStep] + title: DF.Data + track_steps: DF.Check + ui_tour: DF.Check + view_name: DF.Literal["Workspaces", "List", "Form", "Tree", "Page"] + workspace_name: DF.Link | None + + # end: auto-generated types + def before_save(self): + if self.is_standard and not self.module: + if self.workspace_name: + self.module = xhiveframework.db.get_value("Workspace", self.workspace_name, "module") + elif self.dashboard_name: + dashboard_doctype = xhiveframework.db.get_value("Dashboard", self.dashboard_name, "module") + self.module = xhiveframework.db.get_value("DocType", dashboard_doctype, "module") + else: + self.module = "Desk" + if not self.ui_tour: + meta = xhiveframework.get_meta(self.reference_doctype) + for step in self.steps: + if step.is_table_field and step.parent_fieldname: + parent_field_df = meta.get_field(step.parent_fieldname) + step.child_doctype = parent_field_df.options + field_df = xhiveframework.get_meta(step.child_doctype).get_field(step.fieldname) + step.label = field_df.label + step.fieldtype = field_df.fieldtype + else: + field_df = meta.get_field(step.fieldname) + step.label = field_df.label + step.fieldtype = field_df.fieldtype + + def on_update(self): + xhiveframework.cache.delete_key("bootinfo") + + if xhiveframework.conf.developer_mode and self.is_standard: + export_to_files([["Form Tour", self.name]], self.module) + + def on_trash(self): + xhiveframework.cache.delete_key("bootinfo") + + +@xhiveframework.whitelist() +def reset_tour(tour_name): + for user in xhiveframework.get_all("User", pluck="name"): + onboarding_status = xhiveframework.parse_json(xhiveframework.db.get_value("User", user, "onboarding_status")) + onboarding_status.pop(tour_name, None) + xhiveframework.db.set_value( + "User", user, "onboarding_status", xhiveframework.as_json(onboarding_status), update_modified=False + ) + xhiveframework.cache.hdel("bootinfo", user) + + xhiveframework.msgprint(_("Successfully reset onboarding status for all users."), alert=True) + + +@xhiveframework.whitelist() +def update_user_status(value, step): + from xhiveframework.utils.telemetry import capture + + step = xhiveframework.parse_json(step) + tour = xhiveframework.parse_json(value) + + capture( + xhiveframework.scrub(f"{step.parent}_{step.title}"), + app="xhiveframework_ui_tours", + properties={"is_completed": tour.is_completed}, + ) + xhiveframework.db.set_value("User", xhiveframework.session.user, "onboarding_status", value, update_modified=False) + + xhiveframework.cache.hdel("bootinfo", xhiveframework.session.user) + + +def get_onboarding_ui_tours(): + if not xhiveframework.get_system_settings("enable_onboarding"): + return [] + + ui_tours = xhiveframework.get_all("Form Tour", filters={"ui_tour": 1}, fields=["page_route", "name"]) + + return [[tour.name, json.loads(tour.page_route)] for tour in ui_tours] diff --git a/xhiveframework/desk/doctype/form_tour/patches/__init__.py b/xhiveframework/desk/doctype/form_tour/patches/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/form_tour/patches/introduce_ui_tours.py b/xhiveframework/desk/doctype/form_tour/patches/introduce_ui_tours.py new file mode 100644 index 0000000..4ba07b6 --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour/patches/introduce_ui_tours.py @@ -0,0 +1,13 @@ +import json + +import xhiveframework + + +def execute(): + """Handle introduction of UI tours""" + completed = {} + for tour in xhiveframework.get_all("Form Tour", {"ui_tour": 1}, pluck="name"): + completed[tour] = {"is_complete": True} + + User = xhiveframework.qb.DocType("User") + xhiveframework.qb.update(User).set("onboarding_status", json.dumps(completed)).run() diff --git a/xhiveframework/desk/doctype/form_tour/test_form_tour.py b/xhiveframework/desk/doctype/form_tour/test_form_tour.py new file mode 100644 index 0000000..e594ef8 --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour/test_form_tour.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestFormTour(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/form_tour_step/__init__.py b/xhiveframework/desk/doctype/form_tour_step/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/form_tour_step/form_tour_step.json b/xhiveframework/desk/doctype/form_tour_step/form_tour_step.json new file mode 100644 index 0000000..26209cc --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour_step/form_tour_step.json @@ -0,0 +1,230 @@ +{ + "actions": [], + "creation": "2021-05-21 23:05:45.342114", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ui_tour", + "is_table_field", + "section_break_2", + "title", + "parent_fieldname", + "fieldname", + "element_selector", + "parent_element_selector", + "description", + "ondemand_description", + "column_break_2", + "position", + "hide_buttons", + "popover_element", + "modal_trigger", + "offset_x", + "offset_y", + "next_on_click", + "label", + "fieldtype", + "has_next_condition", + "next_step_condition", + "next_form_tour", + "section_break_13", + "child_doctype" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "reqd": 1 + }, + { + "columns": 4, + "fieldname": "description", + "fieldtype": "HTML Editor", + "ignore_xss_filter": 1, + "in_list_view": 1, + "label": "Description", + "reqd": 1 + }, + { + "depends_on": "eval: (!doc.ui_tour && (!doc.is_table_field || (doc.is_table_field && doc.parent_fieldname)))", + "fieldname": "fieldname", + "fieldtype": "Select", + "label": "Fieldname", + "mandatory_depends_on": "eval: (!doc.ui_tour)" + }, + { + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "read_only": 1 + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "Bottom", + "fieldname": "position", + "fieldtype": "Select", + "label": "Position", + "options": "Left\nLeft Center\nLeft Bottom\nTop\nTop Center\nTop Right\nRight\nRight Center\nRight Bottom\nBottom\nBottom Center\nBottom Right\nMid Center" + }, + { + "depends_on": "has_next_condition", + "fieldname": "next_step_condition", + "fieldtype": "Code", + "label": "Next Step Condition", + "oldfieldname": "condition", + "options": "JS" + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "has_next_condition", + "fieldtype": "Check", + "label": "Has Next Condition" + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "fieldtype", + "fieldtype": "Data", + "label": "Fieldtype", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "eval:(!doc.ui_tour)", + "fieldname": "is_table_field", + "fieldtype": "Check", + "label": "Is Table Field" + }, + { + "fieldname": "section_break_2", + "fieldtype": "Section Break" + }, + { + "fieldname": "section_break_13", + "fieldtype": "Section Break", + "hidden": 1, + "label": "Hidden Fields" + }, + { + "fieldname": "child_doctype", + "fieldtype": "Data", + "hidden": 1, + "label": "Child Doctype", + "read_only": 1 + }, + { + "depends_on": "eval: (!doc.ui_tour || doc.is_table_field)", + "fieldname": "parent_fieldname", + "fieldtype": "Select", + "label": "Parent Field", + "mandatory_depends_on": "is_table_field" + }, + { + "default": "0", + "fetch_from": "next_form_tour.ui_tour", + "fieldname": "ui_tour", + "fieldtype": "Check", + "in_list_view": 1, + "label": "UI Tour" + }, + { + "depends_on": "eval:(doc.ui_tour)", + "description": "CSS selector for the element you want to highlight.", + "fieldname": "element_selector", + "fieldtype": "Data", + "label": "Element Selector", + "mandatory_depends_on": "eval:(doc.ui_tour)" + }, + { + "depends_on": "eval:(doc.ui_tour)", + "description": "Mozilla doesn't support :has() so you can pass parent selector here as workaround", + "fieldname": "parent_element_selector", + "fieldtype": "Data", + "label": "Parent Element Selector" + }, + { + "depends_on": "eval:(doc.ui_tour)", + "fieldname": "next_form_tour", + "fieldtype": "Link", + "label": "Next Form Tour", + "options": "Form Tour" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "description": "Hide Previous, Next and Close button on highlight dialog.", + "fieldname": "hide_buttons", + "fieldtype": "Check", + "label": "Hide Buttons" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "description": "Move to next step when clicked inside highlighted area.", + "fieldname": "next_on_click", + "fieldtype": "Check", + "label": "Next on Click" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "description": "when clicked on element it will focus popover if present.", + "fieldname": "popover_element", + "fieldtype": "Check", + "label": "Popover Element" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "fieldname": "offset_x", + "fieldtype": "Int", + "label": "Offset X" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "fieldname": "offset_y", + "fieldtype": "Int", + "label": "Offset Y" + }, + { + "default": "0", + "depends_on": "eval:(doc.ui_tour)", + "description": "Enable if on click\nopens modal.", + "fieldname": "modal_trigger", + "fieldtype": "Check", + "label": "Modal Trigger" + }, + { + "columns": 4, + "depends_on": "eval: (doc.popover_element || doc.modal_trigger)", + "fieldname": "ondemand_description", + "fieldtype": "HTML Editor", + "ignore_xss_filter": 1, + "in_list_view": 1, + "label": "Popover or Modal Description" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-05-23 13:09:15.923043", + "modified_by": "Administrator", + "module": "Desk", + "name": "Form Tour Step", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/form_tour_step/form_tour_step.py b/xhiveframework/desk/doctype/form_tour_step/form_tour_step.py new file mode 100644 index 0000000..fc2768e --- /dev/null +++ b/xhiveframework/desk/doctype/form_tour_step/form_tour_step.py @@ -0,0 +1,57 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class FormTourStep(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + child_doctype: DF.Data | None + description: DF.HTMLEditor + element_selector: DF.Data | None + fieldname: DF.Literal[None] + fieldtype: DF.Data | None + has_next_condition: DF.Check + hide_buttons: DF.Check + is_table_field: DF.Check + label: DF.Data | None + modal_trigger: DF.Check + next_form_tour: DF.Link | None + next_on_click: DF.Check + next_step_condition: DF.Code | None + offset_x: DF.Int + offset_y: DF.Int + ondemand_description: DF.HTMLEditor | None + parent: DF.Data + parent_element_selector: DF.Data | None + parent_fieldname: DF.Literal[None] + parentfield: DF.Data + parenttype: DF.Data + popover_element: DF.Check + position: DF.Literal[ + "Left", + "Left Center", + "Left Bottom", + "Top", + "Top Center", + "Top Right", + "Right", + "Right Center", + "Right Bottom", + "Bottom", + "Bottom Center", + "Bottom Right", + "Mid Center", + ] + title: DF.Data + ui_tour: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/global_search_doctype/__init__.py b/xhiveframework/desk/doctype/global_search_doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.json b/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.json new file mode 100644 index 0000000..648e8f1 --- /dev/null +++ b/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.json @@ -0,0 +1,29 @@ +{ + "creation": "2019-09-13 21:33:55.551941", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "options": "DocType" + } + ], + "istable": 1, + "modified": "2019-09-18 17:59:44.354052", + "modified_by": "Administrator", + "module": "Desk", + "name": "Global Search DocType", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.py b/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.py new file mode 100644 index 0000000..4591f27 --- /dev/null +++ b/xhiveframework/desk/doctype/global_search_doctype/global_search_doctype.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class GlobalSearchDocType(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document_type: DF.Link | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/global_search_settings/__init__.py b/xhiveframework/desk/doctype/global_search_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/global_search_settings/global_search_settings.js b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.js new file mode 100644 index 0000000..789f32f --- /dev/null +++ b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.js @@ -0,0 +1,32 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Global Search Settings", { + refresh: function (frm) { + xhiveframework.realtime.on("global_search_settings", (data) => { + if (data.progress) { + frm.dashboard.show_progress( + "Setting up Global Search", + (data.progress / data.total) * 100, + data.msg + ); + if (data.progress === data.total) { + frm.dashboard.hide_progress("Setting up Global Search"); + } + } + }); + + frm.add_custom_button(__("Reset"), function () { + xhiveframework.call({ + method: "xhiveframework.desk.doctype.global_search_settings.global_search_settings.reset_global_search_settings_doctypes", + callback: function () { + xhiveframework.show_alert({ + message: __("Global Search Document Types Reset."), + indicator: "green", + }); + frm.refresh(); + }, + }); + }); + }, +}); diff --git a/xhiveframework/desk/doctype/global_search_settings/global_search_settings.json b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.json new file mode 100644 index 0000000..6fa25f7 --- /dev/null +++ b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.json @@ -0,0 +1,39 @@ +{ + "creation": "2019-09-03 16:08:21.333698", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "allowed_in_global_search" + ], + "fields": [ + { + "fieldname": "allowed_in_global_search", + "fieldtype": "Table", + "label": "Search Priorities", + "options": "Global Search DocType" + } + ], + "issingle": 1, + "modified": "2019-10-10 22:05:02.692689", + "modified_by": "Administrator", + "module": "Desk", + "name": "Global Search Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/global_search_settings/global_search_settings.py b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.py new file mode 100644 index 0000000..0f6939f --- /dev/null +++ b/xhiveframework/desk/doctype/global_search_settings/global_search_settings.py @@ -0,0 +1,103 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class GlobalSearchSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.global_search_doctype.global_search_doctype import GlobalSearchDocType + from xhiveframework.types import DF + + allowed_in_global_search: DF.Table[GlobalSearchDocType] + + # end: auto-generated types + def validate(self): + dts, core_dts, repeated_dts = [], [], [] + + for dt in self.allowed_in_global_search: + if dt.document_type in dts: + repeated_dts.append(dt.document_type) + + if xhiveframework.get_meta(dt.document_type).module == "Core": + core_dts.append(dt.document_type) + + dts.append(dt.document_type) + + if core_dts: + core_dts = ", ".join(xhiveframework.bold(dt) for dt in core_dts) + xhiveframework.throw(_("Core Modules {0} cannot be searched in Global Search.").format(core_dts)) + + if repeated_dts: + repeated_dts = ", ".join([xhiveframework.bold(dt) for dt in repeated_dts]) + xhiveframework.throw(_("Document Type {0} has been repeated.").format(repeated_dts)) + + # reset cache + xhiveframework.cache.hdel("global_search", "search_priorities") + + +def get_doctypes_for_global_search(): + def get_from_db(): + doctypes = xhiveframework.get_all("Global Search DocType", fields=["document_type"], order_by="idx ASC") + return [d.document_type for d in doctypes] or [] + + return xhiveframework.cache.hget("global_search", "search_priorities", get_from_db) + + +@xhiveframework.whitelist() +def reset_global_search_settings_doctypes(): + update_global_search_doctypes() + + +def update_global_search_doctypes(): + global_search_doctypes = [] + show_message(1, _("Fetching default Global Search documents.")) + + installed_apps = [app for app in xhiveframework.get_installed_apps() if app] + active_domains = [domain for domain in xhiveframework.get_active_domains() if domain] + active_domains.append("Default") + + for app in installed_apps: + search_doctypes = xhiveframework.get_hooks(hook="global_search_doctypes", app_name=app) + if not search_doctypes: + continue + + for domain in active_domains: + if search_doctypes.get(domain): + global_search_doctypes.extend(search_doctypes.get(domain)) + + doctype_list = {dt.name for dt in xhiveframework.get_all("DocType")} + allowed_in_global_search = [] + + for dt in global_search_doctypes: + if dt.get("index") is not None: + allowed_in_global_search.insert(dt.get("index"), dt.get("doctype")) + continue + + allowed_in_global_search.append(dt.get("doctype")) + + show_message(2, _("Setting up Global Search documents.")) + global_search_settings = xhiveframework.get_single("Global Search Settings") + global_search_settings.allowed_in_global_search = [] + for dt in allowed_in_global_search: + if dt not in doctype_list: + continue + + global_search_settings.append("allowed_in_global_search", {"document_type": dt}) + global_search_settings.save(ignore_permissions=True) + show_message(3, "Global Search Documents have been reset.") + + +def show_message(progress, msg): + xhiveframework.publish_realtime( + "global_search_settings", + {"progress": progress, "total": 3, "msg": msg}, + user=xhiveframework.session.user, + ) diff --git a/xhiveframework/desk/doctype/kanban_board/__init__.py b/xhiveframework/desk/doctype/kanban_board/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/kanban_board/kanban_board.js b/xhiveframework/desk/doctype/kanban_board/kanban_board.js new file mode 100644 index 0000000..1dc75f0 --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board/kanban_board.js @@ -0,0 +1,45 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Kanban Board", { + onload: function (frm) { + frm.trigger("reference_doctype"); + }, + refresh: function (frm) { + if (frm.is_new()) return; + frm.add_custom_button("Show Board", function () { + xhiveframework.set_route("List", frm.doc.reference_doctype, "Kanban", frm.doc.name); + }); + }, + reference_doctype: function (frm) { + // set field options + if (!frm.doc.reference_doctype) return; + + xhiveframework.model.with_doctype(frm.doc.reference_doctype, function () { + var options = $.map(xhiveframework.get_meta(frm.doc.reference_doctype).fields, function (d) { + if ( + d.fieldname && + d.fieldtype === "Select" && + xhiveframework.model.no_value_type.indexOf(d.fieldtype) === -1 + ) { + return d.fieldname; + } + return null; + }); + frm.set_df_property("field_name", "options", options); + frm.get_field("field_name").refresh(); + }); + }, + field_name: function (frm) { + var field = xhiveframework.meta.get_field(frm.doc.reference_doctype, frm.doc.field_name); + frm.doc.columns = []; + field.options && + field.options.split("\n").forEach(function (o) { + o = o.trim(); + if (!o) return; + var d = frm.add_child("columns"); + d.column_name = o; + }); + frm.refresh(); + }, +}); diff --git a/xhiveframework/desk/doctype/kanban_board/kanban_board.json b/xhiveframework/desk/doctype/kanban_board/kanban_board.json new file mode 100644 index 0000000..296aa01 --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board/kanban_board.json @@ -0,0 +1,124 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:kanban_board_name", + "creation": "2016-10-19 12:26:04.809812", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "kanban_board_name", + "reference_doctype", + "field_name", + "column_break_4", + "private", + "show_labels", + "section_break_3", + "columns", + "filters", + "fields" + ], + "fields": [ + { + "fieldname": "kanban_board_name", + "fieldtype": "Data", + "label": "Kanban Board Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Document Type", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "field_name", + "fieldtype": "Select", + "label": "Field Name", + "reqd": 1 + }, + { + "fieldname": "section_break_3", + "fieldtype": "Section Break" + }, + { + "fieldname": "columns", + "fieldtype": "Table", + "label": "Columns", + "options": "Kanban Board Column" + }, + { + "fieldname": "filters", + "fieldtype": "Code", + "label": "Filters", + "options": "JSON", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "private", + "fieldtype": "Check", + "label": "Private", + "read_only": 1 + }, + { + "fieldname": "fields", + "fieldtype": "Code", + "label": "Fields", + "options": "JSON", + "read_only": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "show_labels", + "fieldtype": "Check", + "label": "Show Labels", + "read_only": 1 + } + ], + "links": [], + "modified": "2023-08-28 22:29:29.569670", + "modified_by": "Administrator", + "module": "Desk", + "name": "Kanban Board", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "read": 1, + "role": "Desk User" + }, + { + "create": 1, + "delete": 1, + "if_owner": 1, + "read": 1, + "role": "Desk User", + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/kanban_board/kanban_board.py b/xhiveframework/desk/doctype/kanban_board/kanban_board.py new file mode 100644 index 0000000..df38f22 --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board/kanban_board.py @@ -0,0 +1,290 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class KanbanBoard(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.kanban_board_column.kanban_board_column import KanbanBoardColumn + from xhiveframework.types import DF + + columns: DF.Table[KanbanBoardColumn] + field_name: DF.Literal[None] + fields: DF.Code | None + filters: DF.Code | None + kanban_board_name: DF.Data + private: DF.Check + reference_doctype: DF.Link + show_labels: DF.Check + + # end: auto-generated types + def validate(self): + self.validate_column_name() + + def on_change(self): + xhiveframework.clear_cache(doctype=self.reference_doctype) + xhiveframework.cache.delete_keys("_user_settings") + + def before_insert(self): + for column in self.columns: + column.order = get_order_for_column(self, column.column_name) + + def validate_column_name(self): + for column in self.columns: + if not column.column_name: + xhiveframework.msgprint(_("Column Name cannot be empty"), raise_exception=True) + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + return "" + + return f"""(`tabKanban Board`.private=0 or `tabKanban Board`.owner={xhiveframework.db.escape(user)})""" + + +def has_permission(doc, ptype, user): + if doc.private == 0 or user == "Administrator": + return True + + if user == doc.owner: + return True + + return False + + +@xhiveframework.whitelist() +def get_kanban_boards(doctype): + """Get Kanban Boards for doctype to show in List View""" + return xhiveframework.get_list( + "Kanban Board", + fields=["name", "filters", "reference_doctype", "private"], + filters={"reference_doctype": doctype}, + ) + + +@xhiveframework.whitelist() +def add_column(board_name, column_title): + """Adds new column to Kanban Board""" + doc = xhiveframework.get_doc("Kanban Board", board_name) + for col in doc.columns: + if column_title == col.column_name: + xhiveframework.throw(_("Column {0} already exist.").format(column_title)) + + doc.append("columns", dict(column_name=column_title)) + doc.save() + return doc.columns + + +@xhiveframework.whitelist() +def archive_restore_column(board_name, column_title, status): + """Set column's status to status""" + doc = xhiveframework.get_doc("Kanban Board", board_name) + for col in doc.columns: + if column_title == col.column_name: + col.status = status + + doc.save() + return doc.columns + + +@xhiveframework.whitelist() +def update_order(board_name, order): + """Save the order of cards in columns""" + board = xhiveframework.get_doc("Kanban Board", board_name) + doctype = board.reference_doctype + updated_cards = [] + + if not xhiveframework.has_permission(doctype, "write"): + # Return board data from db + return board, updated_cards + + fieldname = board.field_name + order_dict = json.loads(order) + + for col_name, cards in order_dict.items(): + for card in cards: + column = xhiveframework.get_value(doctype, {"name": card}, fieldname) + if column != col_name: + xhiveframework.set_value(doctype, card, fieldname, col_name) + updated_cards.append(dict(name=card, column=col_name)) + + for column in board.columns: + if column.column_name == col_name: + column.order = json.dumps(cards) + + return board.save(ignore_permissions=True), updated_cards + + +@xhiveframework.whitelist() +def update_order_for_single_card(board_name, docname, from_colname, to_colname, old_index, new_index): + """Save the order of cards in columns""" + board = xhiveframework.get_doc("Kanban Board", board_name) + doctype = board.reference_doctype + + xhiveframework.has_permission(doctype, "write", throw=True) + + fieldname = board.field_name + old_index = xhiveframework.parse_json(old_index) + new_index = xhiveframework.parse_json(new_index) + + # save current order and index of columns to be updated + from_col_order, from_col_idx = get_kanban_column_order_and_index(board, from_colname) + to_col_order, to_col_idx = get_kanban_column_order_and_index(board, to_colname) + + if from_colname == to_colname: + from_col_order = to_col_order + + to_col_order.insert(new_index, from_col_order.pop(old_index)) + + # save updated order + board.columns[from_col_idx].order = xhiveframework.as_json(from_col_order) + board.columns[to_col_idx].order = xhiveframework.as_json(to_col_order) + board.save(ignore_permissions=True) + + # update changed value in doc + xhiveframework.set_value(doctype, docname, fieldname, to_colname) + + return board + + +def get_kanban_column_order_and_index(board, colname): + for i, col in enumerate(board.columns): + if col.column_name == colname: + col_order = xhiveframework.parse_json(col.order) + col_idx = i + + return col_order, col_idx + + +@xhiveframework.whitelist() +def add_card(board_name, docname, colname): + board = xhiveframework.get_doc("Kanban Board", board_name) + + xhiveframework.has_permission(board.reference_doctype, "write", throw=True) + + col_order, col_idx = get_kanban_column_order_and_index(board, colname) + col_order.insert(0, docname) + + board.columns[col_idx].order = xhiveframework.as_json(col_order) + + return board.save(ignore_permissions=True) + + +@xhiveframework.whitelist() +def quick_kanban_board(doctype, board_name, field_name, project=None): + """Create new KanbanBoard quickly with default options""" + + doc = xhiveframework.new_doc("Kanban Board") + meta = xhiveframework.get_meta(doctype) + + doc.kanban_board_name = board_name + doc.reference_doctype = doctype + doc.field_name = field_name + + if project: + doc.filters = f'[["Task","project","=","{project}"]]' + + options = "" + for field in meta.fields: + if field.fieldname == field_name: + options = field.options + + columns = [] + if options: + columns = options.split("\n") + + for column in columns: + if not column: + continue + doc.append("columns", dict(column_name=column)) + + if doctype in ["Note", "ToDo"]: + doc.private = 1 + + doc.save() + return doc + + +def get_order_for_column(board, colname): + filters = [[board.reference_doctype, board.field_name, "=", colname]] + if board.filters: + filters.append(xhiveframework.parse_json(board.filters)[0]) + + return xhiveframework.as_json(xhiveframework.get_list(board.reference_doctype, filters=filters, pluck="name")) + + +@xhiveframework.whitelist() +def update_column_order(board_name, order): + """Set the order of columns in Kanban Board""" + board = xhiveframework.get_doc("Kanban Board", board_name) + order = json.loads(order) + old_columns = board.columns + new_columns = [] + + for col in order: + for column in list(old_columns): + if col == column.column_name: + new_columns.append(column) + old_columns.remove(column) + + new_columns.extend(old_columns) + + board.columns = [] + for col in new_columns: + board.append( + "columns", + dict( + column_name=col.column_name, + status=col.status, + order=col.order, + indicator=col.indicator, + ), + ) + + board.save() + return board + + +@xhiveframework.whitelist() +def set_indicator(board_name, column_name, indicator): + """Set the indicator color of column""" + board = xhiveframework.get_doc("Kanban Board", board_name) + + for column in board.columns: + if column.column_name == column_name: + column.indicator = indicator + + board.save() + return board + + +@xhiveframework.whitelist() +def save_settings(board_name: str, settings: str) -> Document: + settings = json.loads(settings) + doc = xhiveframework.get_doc("Kanban Board", board_name) + + fields = settings["fields"] + if not isinstance(fields, str): + fields = json.dumps(fields) + + doc.fields = fields + doc.show_labels = settings["show_labels"] + doc.save() + + resp = doc.as_dict() + resp["fields"] = xhiveframework.parse_json(resp["fields"]) + + return resp diff --git a/xhiveframework/desk/doctype/kanban_board/test_kanban_board.py b/xhiveframework/desk/doctype/kanban_board/test_kanban_board.py new file mode 100644 index 0000000..bfa974d --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board/test_kanban_board.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Kanban Board') + + +class TestKanbanBoard(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/kanban_board_column/__init__.py b/xhiveframework/desk/doctype/kanban_board_column/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.json b/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.json new file mode 100644 index 0000000..c0acde5 --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.json @@ -0,0 +1,55 @@ +{ + "actions": [], + "creation": "2016-10-19 12:26:42.569185", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "column_name", + "status", + "indicator", + "order" + ], + "fields": [ + { + "fieldname": "column_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Column Name" + }, + { + "default": "Active", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Status", + "options": "Active\nArchived" + }, + { + "default": "Gray", + "fieldname": "indicator", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Indicator", + "options": "Blue\nCyan\nGray\nGreen\nLight Blue\nOrange\nPink\nPurple\nRed\nRed\nYellow" + }, + { + "fieldname": "order", + "fieldtype": "Code", + "label": "Order" + } + ], + "istable": 1, + "links": [], + "modified": "2021-12-14 13:13:38.804259", + "modified_by": "Administrator", + "module": "Desk", + "name": "Kanban Board Column", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.py b/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.py new file mode 100644 index 0000000..0492ce9 --- /dev/null +++ b/xhiveframework/desk/doctype/kanban_board_column/kanban_board_column.py @@ -0,0 +1,36 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class KanbanBoardColumn(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + column_name: DF.Data | None + indicator: DF.Literal[ + "Blue", + "Cyan", + "Gray", + "Green", + "Light Blue", + "Orange", + "Pink", + "Purple", + "Red", + "Red", + "Yellow", + ] + order: DF.Code | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + status: DF.Literal["Active", "Archived"] + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/list_filter/__init__.py b/xhiveframework/desk/doctype/list_filter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/list_filter/list_filter.json b/xhiveframework/desk/doctype/list_filter/list_filter.json new file mode 100644 index 0000000..c897aef --- /dev/null +++ b/xhiveframework/desk/doctype/list_filter/list_filter.json @@ -0,0 +1,62 @@ +{ + "actions": [], + "creation": "2018-02-22 15:10:24.401801", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "filter_name", + "reference_doctype", + "for_user", + "filters" + ], + "fields": [ + { + "fieldname": "filter_name", + "fieldtype": "Data", + "label": "Filter Name" + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType" + }, + { + "fieldname": "for_user", + "fieldtype": "Link", + "label": "For User", + "options": "User" + }, + { + "fieldname": "filters", + "fieldtype": "Long Text", + "label": "Filters" + } + ], + "in_create": 1, + "links": [], + "modified": "2023-08-28 22:32:51.465521", + "modified_by": "Administrator", + "module": "Desk", + "name": "List Filter", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/list_filter/list_filter.py b/xhiveframework/desk/doctype/list_filter/list_filter.py new file mode 100644 index 0000000..f3e89db --- /dev/null +++ b/xhiveframework/desk/doctype/list_filter/list_filter.py @@ -0,0 +1,21 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class ListFilter(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + filter_name: DF.Data | None + filters: DF.LongText | None + for_user: DF.Link | None + reference_doctype: DF.Link | None + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/list_view_settings/__init__.py b/xhiveframework/desk/doctype/list_view_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/list_view_settings/list_view_settings.js b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.js new file mode 100644 index 0000000..377e937 --- /dev/null +++ b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("List View Settings", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/desk/doctype/list_view_settings/list_view_settings.json b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.json new file mode 100644 index 0000000..69ea379 --- /dev/null +++ b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.json @@ -0,0 +1,85 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2019-10-23 15:00:48.392374", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "disable_count", + "disable_comment_count", + "disable_sidebar_stats", + "disable_auto_refresh", + "total_fields", + "fields_html", + "fields" + ], + "fields": [ + { + "default": "0", + "fieldname": "disable_count", + "fieldtype": "Check", + "label": "Disable Count" + }, + { + "default": "0", + "fieldname": "disable_sidebar_stats", + "fieldtype": "Check", + "label": "Disable Sidebar Stats" + }, + { + "default": "0", + "fieldname": "disable_auto_refresh", + "fieldtype": "Check", + "label": "Disable Auto Refresh" + }, + { + "fieldname": "total_fields", + "fieldtype": "Select", + "label": "Maximum Number of Fields", + "options": "\n4\n5\n6\n7\n8\n9\n10" + }, + { + "fieldname": "fields_html", + "fieldtype": "HTML", + "label": "Fields" + }, + { + "fieldname": "fields", + "fieldtype": "Code", + "hidden": 1, + "label": "Fields", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "disable_comment_count", + "fieldtype": "Check", + "label": "Disable Comment Count" + } + ], + "links": [], + "modified": "2023-02-14 14:46:43.764229", + "modified_by": "Administrator", + "module": "Desk", + "name": "List View Settings", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/list_view_settings/list_view_settings.py b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.py new file mode 100644 index 0000000..02eab60 --- /dev/null +++ b/xhiveframework/desk/doctype/list_view_settings/list_view_settings.py @@ -0,0 +1,99 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class ListViewSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + disable_auto_refresh: DF.Check + disable_comment_count: DF.Check + disable_count: DF.Check + disable_sidebar_stats: DF.Check + fields: DF.Code | None + total_fields: DF.Literal["", "4", "5", "6", "7", "8", "9", "10"] + # end: auto-generated types + pass + + +@xhiveframework.whitelist() +def save_listview_settings(doctype, listview_settings, removed_listview_fields): + listview_settings = xhiveframework.parse_json(listview_settings) + removed_listview_fields = xhiveframework.parse_json(removed_listview_fields) + + if xhiveframework.get_all("List View Settings", filters={"name": doctype}): + doc = xhiveframework.get_doc("List View Settings", doctype) + doc.update(listview_settings) + doc.save() + else: + doc = xhiveframework.new_doc("List View Settings") + doc.name = doctype + doc.update(listview_settings) + doc.insert() + + set_listview_fields(doctype, listview_settings.get("fields"), removed_listview_fields) + + return {"meta": xhiveframework.get_meta(doctype, False), "listview_settings": doc} + + +def set_listview_fields(doctype, listview_fields, removed_listview_fields): + meta = xhiveframework.get_meta(doctype) + + listview_fields = [f.get("fieldname") for f in xhiveframework.parse_json(listview_fields) if f.get("fieldname")] + + for field in removed_listview_fields: + set_in_list_view_property(doctype, meta.get_field(field), "0") + + for field in listview_fields: + set_in_list_view_property(doctype, meta.get_field(field), "1") + + +def set_in_list_view_property(doctype, field, value): + if not field or field.fieldname == "status_field": + return + + property_setter = xhiveframework.db.get_value( + "Property Setter", + {"doc_type": doctype, "field_name": field.fieldname, "property": "in_list_view"}, + ) + if property_setter: + doc = xhiveframework.get_doc("Property Setter", property_setter) + doc.value = value + doc.save() + else: + xhiveframework.make_property_setter( + { + "doctype": doctype, + "doctype_or_field": "DocField", + "fieldname": field.fieldname, + "property": "in_list_view", + "value": value, + "property_type": "Check", + }, + ignore_validate=True, + ) + + +@xhiveframework.whitelist() +def get_default_listview_fields(doctype): + meta = xhiveframework.get_meta(doctype) + path = xhiveframework.get_module_path( + xhiveframework.scrub(meta.module), "doctype", xhiveframework.scrub(meta.name), xhiveframework.scrub(meta.name) + ".json" + ) + doctype_json = xhiveframework.get_file_json(path) + + fields = [f.get("fieldname") for f in doctype_json.get("fields") if f.get("in_list_view")] + + if meta.title_field: + if meta.title_field.strip() not in fields: + fields.append(meta.title_field.strip()) + + return fields diff --git a/xhiveframework/desk/doctype/list_view_settings/test_list_view_settings.py b/xhiveframework/desk/doctype/list_view_settings/test_list_view_settings.py new file mode 100644 index 0000000..456d4ec --- /dev/null +++ b/xhiveframework/desk/doctype/list_view_settings/test_list_view_settings.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestListViewSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/module_onboarding/__init__.py b/xhiveframework/desk/doctype/module_onboarding/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/module_onboarding/module_onboarding.js b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.js new file mode 100644 index 0000000..ac807b7 --- /dev/null +++ b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.js @@ -0,0 +1,31 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Module Onboarding", { + refresh: function (frm) { + xhiveframework.boot.developer_mode && + frm.set_intro( + __( + "Saving this will export this document as well as the steps linked here as json." + ), + true + ); + if (!xhiveframework.boot.developer_mode) { + frm.trigger("disable_form"); + } + + frm.add_custom_button(__("Reset"), () => { + frm.call("reset_progress"); + }); + }, + + disable_form: function (frm) { + frm.set_read_only(); + frm.fields + .filter((field) => field.has_input) + .forEach((field) => { + frm.set_df_property(field.df.fieldname, "read_only", "1"); + }); + frm.disable_save(); + }, +}); diff --git a/xhiveframework/desk/doctype/module_onboarding/module_onboarding.json b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.json new file mode 100644 index 0000000..1eca835 --- /dev/null +++ b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.json @@ -0,0 +1,118 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2020-04-24 13:58:14.948024", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "title", + "subtitle", + "module", + "allow_roles", + "column_break_4", + "success_message", + "documentation_url", + "is_complete", + "section_break_6", + "steps" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "reqd": 1 + }, + { + "fieldname": "subtitle", + "fieldtype": "Data", + "label": "Subtitle", + "reqd": 1 + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "options": "Module Def", + "reqd": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break" + }, + { + "fieldname": "success_message", + "fieldtype": "Data", + "label": "Success Message", + "reqd": 1 + }, + { + "fieldname": "documentation_url", + "fieldtype": "Data", + "label": "Documentation URL", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "is_complete", + "fieldtype": "Check", + "label": "Is Complete", + "read_only": 1 + }, + { + "fieldname": "steps", + "fieldtype": "Table", + "label": "Steps", + "options": "Onboarding Step Map", + "reqd": 1 + }, + { + "description": "System managers are allowed by default", + "fieldname": "allow_roles", + "fieldtype": "Table MultiSelect", + "label": "Allow Roles", + "options": "Onboarding Permission", + "reqd": 1 + } + ], + "links": [], + "modified": "2023-08-28 22:24:02.233272", + "modified_by": "Administrator", + "module": "Desk", + "name": "Module Onboarding", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/module_onboarding/module_onboarding.py b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.py new file mode 100644 index 0000000..bb61cc1 --- /dev/null +++ b/xhiveframework/desk/doctype/module_onboarding/module_onboarding.py @@ -0,0 +1,84 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.modules.export_file import export_to_files + + +class ModuleOnboarding(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.onboarding_permission.onboarding_permission import OnboardingPermission + from xhiveframework.desk.doctype.onboarding_step_map.onboarding_step_map import OnboardingStepMap + from xhiveframework.types import DF + + allow_roles: DF.TableMultiSelect[OnboardingPermission] + documentation_url: DF.Data + is_complete: DF.Check + module: DF.Link + steps: DF.Table[OnboardingStepMap] + subtitle: DF.Data + success_message: DF.Data + title: DF.Data + + # end: auto-generated types + def on_update(self): + if xhiveframework.conf.developer_mode: + export_to_files(record_list=[["Module Onboarding", self.name]], record_module=self.module) + + for step in self.steps: + export_to_files(record_list=[["Onboarding Step", step.step]], record_module=self.module) + + def get_steps(self): + return [xhiveframework.get_doc("Onboarding Step", step.step) for step in self.steps] + + def get_allowed_roles(self): + all_roles = [role.role for role in self.allow_roles] + if "System Manager" not in all_roles: + all_roles.append("System Manager") + + return all_roles + + def check_completion(self): + if self.is_complete: + return True + + steps = self.get_steps() + is_complete = [bool(step.is_complete or step.is_skipped) for step in steps] + if all(is_complete): + self.is_complete = True + self.save(ignore_permissions=True) + return True + + return False + + @xhiveframework.whitelist() + def reset_progress(self): + self.db_set("is_complete", 0) + + for step in self.get_steps(): + step.db_set("is_complete", 0) + step.db_set("is_skipped", 0) + + xhiveframework.msgprint(_("Module onboarding progress reset"), alert=True) + + def before_export(self, doc): + doc.is_complete = 0 + + def reset_onboarding(self): + xhiveframework.only_for("Administrator") + + self.is_complete = 0 + steps = self.get_steps() + for step in steps: + step.is_complete = 0 + step.is_skipped = 0 + step.save() + + self.save() diff --git a/xhiveframework/desk/doctype/module_onboarding/test_module_onboarding.py b/xhiveframework/desk/doctype/module_onboarding/test_module_onboarding.py new file mode 100644 index 0000000..484e596 --- /dev/null +++ b/xhiveframework/desk/doctype/module_onboarding/test_module_onboarding.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestModuleOnboarding(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/note/README.md b/xhiveframework/desk/doctype/note/README.md new file mode 100644 index 0000000..95d7b33 --- /dev/null +++ b/xhiveframework/desk/doctype/note/README.md @@ -0,0 +1 @@ +Shared Note. (Page with standard information, links, attachments). \ No newline at end of file diff --git a/xhiveframework/desk/doctype/note/__init__.py b/xhiveframework/desk/doctype/note/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/note/note.js b/xhiveframework/desk/doctype/note/note.js new file mode 100644 index 0000000..a431df1 --- /dev/null +++ b/xhiveframework/desk/doctype/note/note.js @@ -0,0 +1,54 @@ +xhiveframework.ui.form.on("Note", { + refresh: function (frm) { + if (!frm.is_new()) { + frm.is_note_editable = false; + frm.events.set_editable(frm); + } + }, + set_editable: function (frm) { + if (frm.has_perm("write")) { + const read_label = __("Read mode"); + const edit_label = __("Edit mode"); + frm.remove_custom_button(frm.is_note_editable ? edit_label : read_label); + frm.add_custom_button(frm.is_note_editable ? read_label : edit_label, function () { + frm.is_note_editable = !frm.is_note_editable; + frm.events.set_editable(frm); + }); + } + // toggle "read_only" for content and "hidden" of all other fields + + // content read_only + frm.set_df_property("content", "read_only", frm.is_note_editable ? 0 : 1); + + // hide all other fields + for (const field of frm.meta.fields) { + if (field.fieldname !== "content") { + frm.set_df_property( + field.fieldname, + "hidden", + frm.is_note_editable && !field.hidden && frm.get_perm(field.permlevel, "write") + ? 0 + : 1 + ); + } + } + + // no label, description for content either + frm.get_field("content").toggle_label(frm.is_note_editable); + frm.get_field("content").toggle_description(frm.is_note_editable); + }, +}); + +xhiveframework.tour["Note"] = [ + { + fieldname: "title", + title: "Title of the Note", + description: "This is the name by which the note will be saved, you can change this later", + }, + { + fieldname: "public", + title: "Sets the Note to Public", + description: + "You can change the visibility of the note with this, setting it to public will allow other users to view it.", + }, +]; diff --git a/xhiveframework/desk/doctype/note/note.json b/xhiveframework/desk/doctype/note/note.json new file mode 100644 index 0000000..4d60393 --- /dev/null +++ b/xhiveframework/desk/doctype/note/note.json @@ -0,0 +1,148 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2013-05-24 13:41:00", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "title", + "public", + "notify_on_login", + "notify_on_every_login", + "expire_notification_on", + "content", + "seen_by_section", + "seen_by" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "reqd": 1 + }, + { + "bold": 1, + "default": "0", + "fieldname": "public", + "fieldtype": "Check", + "label": "Public", + "permlevel": 1, + "print_hide": 1 + }, + { + "bold": 1, + "default": "0", + "depends_on": "public", + "fieldname": "notify_on_login", + "fieldtype": "Check", + "label": "Notify users with a popup when they log in", + "permlevel": 1 + }, + { + "bold": 1, + "default": "0", + "depends_on": "notify_on_login", + "description": "If enabled, users will be notified every time they login. If not enabled, users will only be notified once.", + "fieldname": "notify_on_every_login", + "fieldtype": "Check", + "label": "Notify Users On Every Login", + "permlevel": 1 + }, + { + "depends_on": "eval:doc.notify_on_login && doc.public", + "fieldname": "expire_notification_on", + "fieldtype": "Date", + "label": "Expire Notification On", + "permlevel": 1, + "search_index": 1 + }, + { + "bold": 1, + "description": "Help: To link to another record in the system, use \"/app/note/[Note Name]\" as the Link URL. (don't use \"http://\")", + "fieldname": "content", + "fieldtype": "Text Editor", + "in_global_search": 1, + "label": "Content" + }, + { + "collapsible": 1, + "depends_on": "notify_on_login", + "fieldname": "seen_by_section", + "fieldtype": "Section Break", + "label": "Seen By", + "permlevel": 1 + }, + { + "fieldname": "seen_by", + "fieldtype": "Table", + "label": "Seen By Table", + "options": "Note Seen By", + "permlevel": 1 + } + ], + "icon": "fa fa-file-text", + "idx": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Desk", + "name": "Note", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "permlevel": 1, + "read": 1, + "role": "System Manager", + "write": 1 + }, + { + "permlevel": 2, + "read": 1, + "role": "System Manager", + "write": 1 + }, + { + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User" + }, + { + "create": 1, + "delete": 1, + "email": 1, + "if_owner": 1, + "role": "Desk User", + "share": 1, + "write": 1 + }, + { + "permlevel": 1, + "read": 1, + "role": "Desk User" + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "title", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/note/note.py b/xhiveframework/desk/doctype/note/note.py new file mode 100644 index 0000000..b04ccc9 --- /dev/null +++ b/xhiveframework/desk/doctype/note/note.py @@ -0,0 +1,64 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class Note(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.note_seen_by.note_seen_by import NoteSeenBy + from xhiveframework.types import DF + + content: DF.TextEditor | None + expire_notification_on: DF.Date | None + notify_on_every_login: DF.Check + notify_on_login: DF.Check + public: DF.Check + seen_by: DF.Table[NoteSeenBy] + title: DF.Data + + # end: auto-generated types + def validate(self): + if self.notify_on_login and not self.expire_notification_on: + # expire this notification in a week (default) + self.expire_notification_on = xhiveframework.utils.add_days(self.creation, 7) + + if not self.public and self.notify_on_login: + self.notify_on_login = 0 + + if not self.content: + self.content = "" + + def before_print(self, settings=None): + self.print_heading = self.name + self.sub_heading = "" + + def mark_seen_by(self, user: str) -> None: + if user in [d.user for d in self.seen_by]: + return + + self.append("seen_by", {"user": user}) + + +@xhiveframework.whitelist() +def mark_as_seen(note: str): + note: Note = xhiveframework.get_doc("Note", note) + note.mark_seen_by(xhiveframework.session.user) + note.save(ignore_permissions=True, ignore_version=True) + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + return f"(`tabNote`.owner = {xhiveframework.db.escape(user)} or `tabNote`.public = 1)" + + +def has_permission(doc, user): + return doc.public or doc.owner == user diff --git a/xhiveframework/desk/doctype/note/note_list.js b/xhiveframework/desk/doctype/note/note_list.js new file mode 100644 index 0000000..15b7171 --- /dev/null +++ b/xhiveframework/desk/doctype/note/note_list.js @@ -0,0 +1,11 @@ +xhiveframework.listview_settings["Note"] = { + hide_name_column: true, + add_fields: ["public"], + get_indicator: function (doc) { + if (doc.public) { + return [__("Public"), "green", "public,=,Yes"]; + } else { + return [__("Private"), "gray", "public,=,No"]; + } + }, +}; diff --git a/xhiveframework/desk/doctype/note/test_note.py b/xhiveframework/desk/doctype/note/test_note.py new file mode 100644 index 0000000..c4cd83b --- /dev/null +++ b/xhiveframework/desk/doctype/note/test_note.py @@ -0,0 +1,75 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Note") + + +class TestNote(XhiveFrameworkTestCase): + def insert_note(self): + xhiveframework.db.delete("Version") + xhiveframework.db.delete("Note") + xhiveframework.db.delete("Note Seen By") + + return xhiveframework.get_doc(dict(doctype="Note", title="test note", content="test note content")).insert() + + def test_version(self): + note = self.insert_note() + note.title = "test note 1" + note.content = "1" + note.save(ignore_version=False) + + version = xhiveframework.get_doc("Version", dict(docname=note.name)) + data = version.get_data() + + self.assertTrue(("title", "test note", "test note 1"), data["changed"]) + self.assertTrue(("content", "test note content", "1"), data["changed"]) + + def test_rows(self): + note = self.insert_note() + + # test add + note.append("seen_by", {"user": "Administrator"}) + note.save(ignore_version=False) + + version = xhiveframework.get_doc("Version", dict(docname=note.name)) + data = version.get_data() + + self.assertEqual(len(data.get("added")), 1) + self.assertEqual(len(data.get("removed")), 0) + self.assertEqual(len(data.get("changed")), 0) + + for row in data.get("added"): + self.assertEqual(row[0], "seen_by") + self.assertEqual(row[1]["user"], "Administrator") + + # test row change + note.seen_by[0].user = "Guest" + note.save(ignore_version=False) + + version = xhiveframework.get_doc("Version", dict(docname=note.name)) + data = version.get_data() + + self.assertEqual(len(data.get("row_changed")), 1) + for row in data.get("row_changed"): + self.assertEqual(row[0], "seen_by") + self.assertEqual(row[1], 0) + self.assertEqual(row[2], note.seen_by[0].name) + self.assertEqual(row[3], [["user", "Administrator", "Guest"]]) + + # test remove + note.seen_by = [] + note.save(ignore_version=False) + + version = xhiveframework.get_doc("Version", dict(docname=note.name)) + data = version.get_data() + + self.assertEqual(len(data.get("removed")), 1) + for row in data.get("removed"): + self.assertEqual(row[0], "seen_by") + self.assertEqual(row[1]["user"], "Guest") + + # self.assertTrue(('title', 'test note', 'test note 1'), data['changed']) + # self.assertTrue(('content', 'test note content', '1'), data['changed']) diff --git a/xhiveframework/desk/doctype/note/test_records.json b/xhiveframework/desk/doctype/note/test_records.json new file mode 100644 index 0000000..f3d7cff --- /dev/null +++ b/xhiveframework/desk/doctype/note/test_records.json @@ -0,0 +1,7 @@ +[ + { + "doctype": "Note", + "name": "_Test Note 1", + "title": "Test Note Title" + } +] diff --git a/xhiveframework/desk/doctype/note_seen_by/__init__.py b/xhiveframework/desk/doctype/note_seen_by/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/note_seen_by/note_seen_by.json b/xhiveframework/desk/doctype/note_seen_by/note_seen_by.json new file mode 100644 index 0000000..905a043 --- /dev/null +++ b/xhiveframework/desk/doctype/note_seen_by/note_seen_by.json @@ -0,0 +1,32 @@ +{ + "actions": [], + "creation": "2016-08-29 05:29:16.726172", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "permlevel": 2 + } + ], + "istable": 1, + "links": [], + "modified": "2023-04-24 16:14:53.684098", + "modified_by": "Administrator", + "module": "Desk", + "name": "Note Seen By", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/note_seen_by/note_seen_by.py b/xhiveframework/desk/doctype/note_seen_by/note_seen_by.py new file mode 100644 index 0000000..96c0274 --- /dev/null +++ b/xhiveframework/desk/doctype/note_seen_by/note_seen_by.py @@ -0,0 +1,21 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class NoteSeenBy(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + user: DF.Link | None + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/notification_log/__init__.py b/xhiveframework/desk/doctype/notification_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/notification_log/notification_log.js b/xhiveframework/desk/doctype/notification_log/notification_log.js new file mode 100644 index 0000000..20678c4 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_log/notification_log.js @@ -0,0 +1,49 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Notification Log", { + refresh: function (frm) { + if (frm.doc.attached_file) { + frm.trigger("set_attachment"); + } else { + frm.get_field("attachment_link").$wrapper.empty(); + } + }, + + open_reference_document: function (frm) { + if (frm.doc?.link) { + xhiveframework.set_route(frm.doc.link); + return; + } + const dt = frm.doc.document_type; + const dn = frm.doc.document_name; + xhiveframework.set_route("Form", dt, dn); + }, + + set_attachment: function (frm) { + const attachment = JSON.parse(frm.doc.attached_file); + + const $wrapper = frm.get_field("attachment_link").$wrapper; + $wrapper.html(` + + `); + + $wrapper.find(".attached-file-link").click(() => { + const w = window.open( + xhiveframework.urllib.get_full_url(`/api/method/xhiveframework.utils.print_format.download_pdf? + doctype=${encodeURIComponent(attachment.doctype)} + &name=${encodeURIComponent(attachment.name)} + &format=${encodeURIComponent(attachment.print_format)} + &lang=${encodeURIComponent(attachment.lang)}`) + ); + if (!w) { + xhiveframework.msgprint(__("Please enable pop-ups")); + } + }); + }, +}); diff --git a/xhiveframework/desk/doctype/notification_log/notification_log.json b/xhiveframework/desk/doctype/notification_log/notification_log.json new file mode 100644 index 0000000..9fbe732 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_log/notification_log.json @@ -0,0 +1,127 @@ +{ + "actions": [], + "creation": "2019-08-26 13:37:34.165254", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "subject", + "for_user", + "type", + "email_content", + "document_type", + "read", + "document_name", + "attached_file", + "attachment_link", + "open_reference_document", + "from_user", + "link" + ], + "fields": [ + { + "fieldname": "subject", + "fieldtype": "Text", + "in_list_view": 1, + "label": "Subject" + }, + { + "fieldname": "for_user", + "fieldtype": "Link", + "hidden": 1, + "label": "For User", + "options": "User", + "search_index": 1 + }, + { + "fieldname": "type", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Type", + "options": "Mention\nEnergy Point\nAssignment\nShare\nAlert" + }, + { + "fieldname": "email_content", + "fieldtype": "Text Editor", + "label": "Message" + }, + { + "fieldname": "document_type", + "fieldtype": "Link", + "hidden": 1, + "label": "Document Type", + "options": "DocType" + }, + { + "fieldname": "document_name", + "fieldtype": "Data", + "hidden": 1, + "label": "Document Link", + "search_index": 1 + }, + { + "fieldname": "from_user", + "fieldtype": "Link", + "hidden": 1, + "label": "From User", + "options": "User" + }, + { + "default": "0", + "fieldname": "read", + "fieldtype": "Check", + "hidden": 1, + "ignore_user_permissions": 1, + "label": "Read" + }, + { + "fieldname": "open_reference_document", + "fieldtype": "Button", + "label": "Open Reference Document" + }, + { + "fieldname": "attached_file", + "fieldtype": "Code", + "hidden": 1, + "label": "Attached File", + "options": "JSON" + }, + { + "fieldname": "attachment_link", + "fieldtype": "HTML", + "label": "Attachment Link" + }, + { + "fieldname": "link", + "fieldtype": "Data", + "hidden": 1, + "label": "Link" + } + ], + "hide_toolbar": 1, + "in_create": 1, + "links": [], + "modified": "2023-11-18 22:40:12.145940", + "modified_by": "Administrator", + "module": "Desk", + "name": "Notification Log", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "subject", + "track_seen": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/notification_log/notification_log.py b/xhiveframework/desk/doctype/notification_log/notification_log.py new file mode 100644 index 0000000..c246f48 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_log/notification_log.py @@ -0,0 +1,210 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + is_email_notifications_enabled_for_type, + is_notifications_enabled, +) +from xhiveframework.model.document import Document + + +class NotificationLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + attached_file: DF.Code | None + document_name: DF.Data | None + document_type: DF.Link | None + email_content: DF.TextEditor | None + for_user: DF.Link | None + from_user: DF.Link | None + link: DF.Data | None + read: DF.Check + subject: DF.Text | None + type: DF.Literal["Mention", "Energy Point", "Assignment", "Share", "Alert"] + + # end: auto-generated types + def after_insert(self): + xhiveframework.publish_realtime("notification", after_commit=True, user=self.for_user) + set_notifications_as_unseen(self.for_user) + if is_email_notifications_enabled_for_type(self.for_user, self.type): + try: + send_notification_email(self) + except xhiveframework.OutgoingEmailError: + self.log_error(_("Failed to send notification email")) + + @staticmethod + def clear_old_logs(days=180): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Notification Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + +def get_permission_query_conditions(for_user): + if not for_user: + for_user = xhiveframework.session.user + + if for_user == "Administrator": + return + + return f"""(`tabNotification Log`.for_user = {xhiveframework.db.escape(for_user)})""" + + +def get_title(doctype, docname, title_field=None): + if not title_field: + title_field = xhiveframework.get_meta(doctype).get_title_field() + return docname if title_field == "name" else xhiveframework.db.get_value(doctype, docname, title_field) + + +def get_title_html(title): + return f'{title}' + + +def enqueue_create_notification(users: list[str] | str, doc: dict): + """Send notification to users. + + users: list of user emails or string of users with comma separated emails + doc: contents of `Notification` doc + """ + + # During installation of new site, enqueue_create_notification tries to connect to Redis. + # This breaks new site creation if Redis server is not running. + # We do not need any notifications in fresh installation + if xhiveframework.flags.in_install: + return + + doc = xhiveframework._dict(doc) + + if isinstance(users, str): + users = [user.strip() for user in users.split(",") if user.strip()] + users = list(set(users)) + + xhiveframework.enqueue( + "xhiveframework.desk.doctype.notification_log.notification_log.make_notification_logs", + doc=doc, + users=users, + now=xhiveframework.flags.in_test, + ) + + +def make_notification_logs(doc, users): + for user in _get_user_ids(users): + notification = xhiveframework.new_doc("Notification Log") + notification.update(doc) + notification.for_user = user + if ( + notification.for_user != notification.from_user + or doc.type == "Energy Point" + or doc.type == "Alert" + ): + notification.insert(ignore_permissions=True) + + +def _get_user_ids(user_emails): + user_names = xhiveframework.db.get_values( + "User", {"enabled": 1, "email": ("in", user_emails)}, "name", pluck=True + ) + return [user for user in user_names if is_notifications_enabled(user)] + + +def send_notification_email(doc: NotificationLog): + if doc.type == "Energy Point" and doc.email_content is None: + return + + from xhiveframework.utils import get_url_to_form, strip_html + + user = xhiveframework.db.get_value("User", doc.for_user, fieldname=["email", "language"], as_dict=True) + if not user: + return + + header = get_email_header(doc, user.language) + email_subject = strip_html(doc.subject) + args = { + "body_content": doc.subject, + "description": doc.email_content, + } + if doc.link: + args["doc_link"] = doc.link + else: + args["document_type"] = doc.document_type + args["document_name"] = doc.document_name + args["doc_link"] = get_url_to_form(doc.document_type, doc.document_name) + + xhiveframework.sendmail( + recipients=user.email, + subject=email_subject, + template="new_notification", + args=args, + header=[header, "orange"], + now=xhiveframework.flags.in_test, + ) + + +def get_email_header(doc, language: str | None = None): + docname = doc.document_name + header_map = { + "Default": _("New Notification", lang=language), + "Mention": _("New Mention on {0}", lang=language).format(docname), + "Assignment": _("Assignment Update on {0}", lang=language).format(docname), + "Share": _("New Document Shared {0}", lang=language).format(docname), + "Energy Point": _("Energy Point Update on {0}", lang=language).format(docname), + } + + return header_map[doc.type or "Default"] + + +@xhiveframework.whitelist() +def get_notification_logs(limit=20): + notification_logs = xhiveframework.db.get_list( + "Notification Log", fields=["*"], limit=limit, order_by="modified desc" + ) + + users = [log.from_user for log in notification_logs] + users = [*set(users)] # remove duplicates + user_info = xhiveframework._dict() + + for user in users: + xhiveframework.utils.add_user_info(user, user_info) + + return {"notification_logs": notification_logs, "user_info": user_info} + + +@xhiveframework.whitelist() +def mark_all_as_read(): + unread_docs_list = xhiveframework.get_all( + "Notification Log", filters={"read": 0, "for_user": xhiveframework.session.user} + ) + unread_docnames = [doc.name for doc in unread_docs_list] + if unread_docnames: + filters = {"name": ["in", unread_docnames]} + xhiveframework.db.set_value("Notification Log", filters, "read", 1, update_modified=False) + + +@xhiveframework.whitelist() +def mark_as_read(docname: str): + if xhiveframework.flags.read_only: + return + + if docname: + xhiveframework.db.set_value("Notification Log", str(docname), "read", 1, update_modified=False) + + +@xhiveframework.whitelist() +def trigger_indicator_hide(): + xhiveframework.publish_realtime("indicator_hide", user=xhiveframework.session.user) + + +def set_notifications_as_unseen(user): + try: + xhiveframework.db.set_value("Notification Settings", user, "seen", 0, update_modified=False) + except xhiveframework.DoesNotExistError: + return diff --git a/xhiveframework/desk/doctype/notification_log/notification_log_list.js b/xhiveframework/desk/doctype/notification_log/notification_log_list.js new file mode 100644 index 0000000..d9488be --- /dev/null +++ b/xhiveframework/desk/doctype/notification_log/notification_log_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Notification Log"] = { + onload: function (listview) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(cur_list.doctype); + }); + }, +}; diff --git a/xhiveframework/desk/doctype/notification_log/test_notification_log.py b/xhiveframework/desk/doctype/notification_log/test_notification_log.py new file mode 100644 index 0000000..c1a5d40 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_log/test_notification_log.py @@ -0,0 +1,51 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.user.user import get_system_users +from xhiveframework.desk.form.assign_to import add as assign_task +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNotificationLog(XhiveFrameworkTestCase): + def test_assignment(self): + todo = get_todo() + user = get_user() + + assign_task( + {"assign_to": [user], "doctype": "ToDo", "name": todo.name, "description": todo.description} + ) + log_type = xhiveframework.db.get_value( + "Notification Log", {"document_type": "ToDo", "document_name": todo.name}, "type" + ) + self.assertEqual(log_type, "Assignment") + + def test_share(self): + todo = get_todo() + user = get_user() + + xhiveframework.share.add("ToDo", todo.name, user, notify=1) + log_type = xhiveframework.db.get_value( + "Notification Log", {"document_type": "ToDo", "document_name": todo.name}, "type" + ) + self.assertEqual(log_type, "Share") + + email = get_last_email_queue() + content = f"Subject: {xhiveframework.utils.get_fullname(xhiveframework.session.user)} shared a document ToDo" + self.assertTrue(content in email.message) + + +def get_last_email_queue(): + res = xhiveframework.get_all("Email Queue", fields=["message"], order_by="creation desc", limit=1) + return res[0] + + +def get_todo(): + if not xhiveframework.get_all("ToDo"): + return xhiveframework.get_doc({"doctype": "ToDo", "description": "Test for Notification"}).insert() + + res = xhiveframework.get_all("ToDo", limit=1) + return xhiveframework.get_cached_doc("ToDo", res[0].name) + + +def get_user(): + return get_system_users(limit=1)[0] diff --git a/xhiveframework/desk/doctype/notification_settings/__init__.py b/xhiveframework/desk/doctype/notification_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/notification_settings/notification_settings.js b/xhiveframework/desk/doctype/notification_settings/notification_settings.js new file mode 100644 index 0000000..6871ae5 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_settings/notification_settings.js @@ -0,0 +1,22 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Notification Settings", { + onload: (frm) => { + frm.set_query("subscribed_documents", () => { + return { + filters: { + istable: 0, + }, + }; + }); + }, + + refresh: (frm) => { + if (xhiveframework.user.has_role("System Manager")) { + frm.add_custom_button(__("Go to Notification Settings List"), () => { + xhiveframework.set_route("List", "Notification Settings"); + }); + } + }, +}); diff --git a/xhiveframework/desk/doctype/notification_settings/notification_settings.json b/xhiveframework/desk/doctype/notification_settings/notification_settings.json new file mode 100644 index 0000000..b4ea0fd --- /dev/null +++ b/xhiveframework/desk/doctype/notification_settings/notification_settings.json @@ -0,0 +1,146 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2019-09-11 22:15:44.851526", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enabled", + "subscribed_documents", + "column_break_3", + "enable_email_notifications", + "enable_email_mention", + "enable_email_assignment", + "enable_email_threads_on_assigned_document", + "enable_email_energy_point", + "enable_email_share", + "enable_email_event_reminders", + "user", + "seen", + "system_notifications_section", + "energy_points_system_notifications" + ], + "fields": [ + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "fieldname": "subscribed_documents", + "fieldtype": "Table MultiSelect", + "label": "Open Documents", + "options": "Notification Subscribed Document" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Section Break", + "label": "Email Settings" + }, + { + "default": "1", + "fieldname": "enable_email_notifications", + "fieldtype": "Check", + "label": "Enable Email Notifications" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "fieldname": "enable_email_mention", + "fieldtype": "Check", + "label": "Mentions" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "fieldname": "enable_email_assignment", + "fieldtype": "Check", + "label": "Assignments" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "fieldname": "enable_email_energy_point", + "fieldtype": "Check", + "label": "Energy Points" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "fieldname": "enable_email_share", + "fieldtype": "Check", + "label": "Document Share" + }, + { + "default": "__user", + "fieldname": "user", + "fieldtype": "Link", + "hidden": 1, + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "seen", + "fieldtype": "Check", + "hidden": 1, + "label": "Seen" + }, + { + "fieldname": "system_notifications_section", + "fieldtype": "Section Break", + "label": "System Notifications" + }, + { + "default": "1", + "fieldname": "energy_points_system_notifications", + "fieldtype": "Check", + "label": "Energy Points" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "fieldname": "enable_email_event_reminders", + "fieldtype": "Check", + "label": "Event Reminders" + }, + { + "default": "1", + "depends_on": "enable_email_notifications", + "description": "Get notified when an email is received on any of the documents assigned to you.", + "fieldname": "enable_email_threads_on_assigned_document", + "fieldtype": "Check", + "label": "Email Threads on Assigned Document" + } + ], + "in_create": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-12-01 12:46:15.490640", + "modified_by": "Administrator", + "module": "Desk", + "name": "Notification Settings", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/notification_settings/notification_settings.py b/xhiveframework/desk/doctype/notification_settings/notification_settings.py new file mode 100644 index 0000000..f1f03a7 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_settings/notification_settings.py @@ -0,0 +1,138 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class NotificationSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.desk.doctype.notification_subscribed_document.notification_subscribed_document import ( + NotificationSubscribedDocument, + ) + from xhiveframework.types import DF + + enable_email_assignment: DF.Check + enable_email_energy_point: DF.Check + enable_email_event_reminders: DF.Check + enable_email_mention: DF.Check + enable_email_notifications: DF.Check + enable_email_share: DF.Check + enable_email_threads_on_assigned_document: DF.Check + enabled: DF.Check + energy_points_system_notifications: DF.Check + seen: DF.Check + subscribed_documents: DF.TableMultiSelect[NotificationSubscribedDocument] + user: DF.Link | None + + # end: auto-generated types + def on_update(self): + from xhiveframework.desk.notifications import clear_notification_config + + clear_notification_config(xhiveframework.session.user) + + +def is_notifications_enabled(user): + enabled = xhiveframework.db.get_value("Notification Settings", user, "enabled") + if enabled is None: + return True + return enabled + + +def is_email_notifications_enabled(user): + enabled = xhiveframework.db.get_value("Notification Settings", user, "enable_email_notifications") + if enabled is None: + return True + return enabled + + +def is_email_notifications_enabled_for_type(user, notification_type): + if not is_email_notifications_enabled(user): + return False + + if notification_type == "Alert": + return False + + fieldname = "enable_email_" + xhiveframework.scrub(notification_type) + enabled = xhiveframework.db.get_value("Notification Settings", user, fieldname) + if enabled is None: + return True + return enabled + + +def create_notification_settings(user): + if not xhiveframework.db.exists("Notification Settings", user): + _doc = xhiveframework.new_doc("Notification Settings") + _doc.name = user + _doc.insert(ignore_permissions=True) + + +def toggle_notifications(user: str, enable: bool = False): + try: + settings = xhiveframework.get_doc("Notification Settings", user) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_last_message() + return + + if settings.enabled != enable: + settings.enabled = enable + settings.save() + + +@xhiveframework.whitelist() +def get_subscribed_documents(): + if not xhiveframework.session.user: + return [] + + try: + if xhiveframework.db.exists("Notification Settings", xhiveframework.session.user): + doc = xhiveframework.get_doc("Notification Settings", xhiveframework.session.user) + return [item.document for item in doc.subscribed_documents] + # Notification Settings is fetched even before sync doctype is called + # but it will throw an ImportError, we can ignore it in migrate + except ImportError: + pass + + return [] + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + return + + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return """(`tabNotification Settings`.name != 'Administrator')""" + + return f"""(`tabNotification Settings`.name = {xhiveframework.db.escape(user)})""" + + +def has_permission(doc, ptype="read", user=None): + # - Administrator can access everything. + # - System managers can access everything except admin. + # - Everyone else can only access their document. + user = user or xhiveframework.session.user + + if user == "Administrator": + return True + + if "System Manager" in xhiveframework.get_roles(user): + return doc.name != "Administrator" + + return doc.name == user + + +@xhiveframework.whitelist() +def set_seen_value(value, user): + if xhiveframework.flags.read_only: + return + + xhiveframework.db.set_value("Notification Settings", user, "seen", value, update_modified=False) diff --git a/xhiveframework/desk/doctype/notification_settings/test_notification_settings.py b/xhiveframework/desk/doctype/notification_settings/test_notification_settings.py new file mode 100644 index 0000000..d6fe61f --- /dev/null +++ b/xhiveframework/desk/doctype/notification_settings/test_notification_settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNotificationSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/notification_subscribed_document/__init__.py b/xhiveframework/desk/doctype/notification_subscribed_document/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.json b/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.json new file mode 100644 index 0000000..b3f4046 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.json @@ -0,0 +1,30 @@ +{ + "creation": "2019-10-09 15:04:39.504787", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document" + ], + "fields": [ + { + "fieldname": "document", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document", + "options": "DocType", + "reqd": 1 + } + ], + "istable": 1, + "modified": "2019-10-09 16:02:00.049237", + "modified_by": "Administrator", + "module": "Desk", + "name": "Notification Subscribed Document", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.py b/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.py new file mode 100644 index 0000000..e31d351 --- /dev/null +++ b/xhiveframework/desk/doctype/notification_subscribed_document/notification_subscribed_document.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class NotificationSubscribedDocument(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/number_card/__init__.py b/xhiveframework/desk/doctype/number_card/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/number_card/number_card.js b/xhiveframework/desk/doctype/number_card/number_card.js new file mode 100644 index 0000000..38ea177 --- /dev/null +++ b/xhiveframework/desk/doctype/number_card/number_card.js @@ -0,0 +1,451 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Number Card", { + refresh: function (frm) { + if (!xhiveframework.boot.developer_mode && frm.doc.is_standard) { + frm.disable_save(); + } else { + frm.enable_save(); + } + + frm.set_df_property("filters_section", "hidden", 1); + frm.set_df_property("dynamic_filters_section", "hidden", 1); + frm.trigger("set_options"); + + if (!frm.doc.type) { + frm.set_value("type", "Document Type"); + } + + if (frm.doc.type == "Report" && frm.doc.report_name) { + frm.trigger("set_report_filters"); + } + + if (frm.doc.type == "Custom") { + frm.filters = eval(frm.doc.filters_config); + frm.trigger("render_filters_table"); + } + frm.trigger("set_parent_document_type"); + + if (!frm.is_new()) { + frm.trigger("create_add_to_dashboard_button"); + } + }, + + create_add_to_dashboard_button: function (frm) { + frm.add_custom_button("Add Card to Dashboard", () => { + const dialog = xhiveframework.dashboard_utils.get_add_to_dashboard_dialog( + frm.doc.name, + "Number Card", + "xhiveframework.desk.doctype.number_card.number_card.add_card_to_dashboard" + ); + + if (!frm.doc.name) { + xhiveframework.msgprint(__("Please create Card first")); + } else { + dialog.show(); + } + }); + }, + + before_save: function (frm) { + let dynamic_filters = JSON.parse(frm.doc.dynamic_filters_json || "null"); + let static_filters = JSON.parse(frm.doc.filters_json || "null"); + static_filters = xhiveframework.dashboard_utils.remove_common_static_filter_values( + static_filters, + dynamic_filters + ); + + frm.set_value("filters_json", JSON.stringify(static_filters)); + frm.trigger("render_filters_table"); + frm.trigger("render_dynamic_filters_table"); + }, + + is_standard: function (frm) { + frm.trigger("render_dynamic_filters_table"); + frm.set_df_property("dynamic_filters_section", "hidden", 1); + }, + + type: function (frm) { + if (frm.doc.type == "Report") { + frm.set_query("report_name", () => { + return { + filters: { + report_type: ["!=", "Report Builder"], + }, + }; + }); + } + }, + + report_name: function (frm) { + frm.filters = []; + frm.set_value("filters_json", "{}"); + frm.set_value("dynamic_filters_json", "{}"); + frm.set_df_property("report_field", "options", []); + frm.trigger("set_report_filters"); + }, + + filters_config: function (frm) { + frm.filters = eval(frm.doc.filters_config); + const filter_values = xhiveframework.report_utils.get_filter_values(frm.filters); + frm.set_value("filters_json", JSON.stringify(filter_values)); + frm.trigger("render_filters_table"); + }, + + document_type: function (frm) { + frm.set_query("document_type", function () { + return { + filters: { + issingle: false, + }, + }; + }); + frm.set_value("filters_json", "[]"); + frm.set_value("dynamic_filters_json", "[]"); + frm.set_value("aggregate_function_based_on", ""); + frm.set_value("parent_document_type", ""); + frm.trigger("set_options"); + frm.trigger("set_parent_document_type"); + }, + + set_options: function (frm) { + if (frm.doc.type !== "Document Type") { + return; + } + + let aggregate_based_on_fields = []; + const doctype = frm.doc.document_type; + + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => { + xhiveframework.get_meta(doctype).fields.map((df) => { + if (xhiveframework.model.numeric_fieldtypes.includes(df.fieldtype)) { + if (df.fieldtype == "Currency") { + if (!df.options || df.options !== "Company:company:default_currency") { + return; + } + } + aggregate_based_on_fields.push({ label: df.label, value: df.fieldname }); + } + }); + + frm.set_df_property( + "aggregate_function_based_on", + "options", + aggregate_based_on_fields + ); + }); + frm.trigger("render_filters_table"); + frm.trigger("render_dynamic_filters_table"); + } + }, + + set_report_filters: function (frm) { + const report_name = frm.doc.report_name; + if (report_name) { + xhiveframework.report_utils.get_report_filters(report_name).then((filters) => { + if (filters) { + frm.filters = filters; + const filter_values = xhiveframework.report_utils.get_filter_values(filters); + if (frm.doc.filters_json.length <= 2) { + frm.set_value("filters_json", JSON.stringify(filter_values)); + } + } + frm.trigger("render_filters_table"); + frm.trigger("set_report_field_options"); + frm.trigger("render_dynamic_filters_table"); + }); + } + }, + + set_report_field_options: function (frm) { + let filters = frm.doc.filters_json.length > 2 ? JSON.parse(frm.doc.filters_json) : null; + if (frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2) { + filters = xhiveframework.dashboard_utils.get_all_filters(frm.doc); + } + xhiveframework + .xcall("xhiveframework.desk.query_report.run", { + report_name: frm.doc.report_name, + filters: filters, + ignore_prepared_report: 1, + }) + .then((data) => { + if (data.result.length) { + frm.field_options = xhiveframework.report_utils.get_field_options_from_report( + data.columns, + data + ); + frm.set_df_property( + "report_field", + "options", + frm.field_options.numeric_fields + ); + if (!frm.field_options.numeric_fields.length) { + xhiveframework.msgprint( + __("Report has no numeric fields, please change the Report Name") + ); + } + } else { + xhiveframework.msgprint( + __( + "Report has no data, please modify the filters or change the Report Name" + ) + ); + } + }); + }, + + render_filters_table: function (frm) { + frm.set_df_property("filters_section", "hidden", 0); + let is_document_type = frm.doc.type == "Document Type"; + let is_dynamic_filter = (f) => ["Date", "DateRange"].includes(f.fieldtype) && f.default; + + let wrapper = $(frm.get_field("filters_json").wrapper).empty(); + let table = $(` + + + + + + + + +
    ${__("Filter")}${__("Condition")}${__("Value")}
    `).appendTo(wrapper); + $(`

    ${__("Click table to edit")}

    `).appendTo(wrapper); + + let filters = JSON.parse(frm.doc.filters_json || "[]"); + let filters_set = false; + + // Set dynamic filters for reports + if (frm.doc.type == "Report") { + let set_filters = false; + frm.filters.forEach((f) => { + if (is_dynamic_filter(f)) { + filters[f.fieldname] = f.default; + set_filters = true; + } + }); + set_filters && frm.set_value("filters_json", JSON.stringify(filters)); + } + + let fields = []; + if (is_document_type) { + fields = [ + { + fieldtype: "HTML", + fieldname: "filter_area", + }, + ]; + + if (filters.length) { + filters.forEach((filter) => { + const filter_row = $(` + ${filter[1]} + ${filter[2] || ""} + ${filter[3]} + `); + + table.find("tbody").append(filter_row); + }); + filters_set = true; + } + } else if (frm.filters.length) { + fields = frm.filters.filter((f) => f.fieldname); + fields.map((f) => { + if (filters[f.fieldname]) { + let condition = "="; + const filter_row = $(` + ${f.label} + ${condition} + ${filters[f.fieldname] || ""} + `); + table.find("tbody").append(filter_row); + if (!filters_set) filters_set = true; + } + }); + } + + if (!filters_set) { + const filter_row = $(` + ${__("Click to Set Filters")}`); + table.find("tbody").append(filter_row); + } + + table.on("click", () => { + let dialog = new xhiveframework.ui.Dialog({ + title: __("Set Filters"), + fields: fields.filter((f) => !is_dynamic_filter(f)), + primary_action: function () { + let values = this.get_values(); + if (values) { + this.hide(); + if (is_document_type) { + let filters = frm.filter_group.get_filters(); + frm.set_value("filters_json", JSON.stringify(filters)); + } else { + frm.set_value("filters_json", JSON.stringify(values)); + } + frm.trigger("render_filters_table"); + } + }, + primary_action_label: "Set", + }); + + if (is_document_type) { + frm.filter_group = new xhiveframework.ui.FilterGroup({ + parent: dialog.get_field("filter_area").$wrapper, + doctype: frm.doc.document_type, + parent_doctype: frm.doc.parent_document_type, + on_change: () => {}, + }); + filters && frm.filter_group.add_filters_to_filter_group(filters); + } + + dialog.show(); + + if (frm.doc.type == "Report") { + //Set query report object so that it can be used while fetching filter values in the report + xhiveframework.query_report = new xhiveframework.views.QueryReport({ + filters: dialog.fields_list, + }); + xhiveframework.query_reports[frm.doc.report_name] && + xhiveframework.query_reports[frm.doc.report_name].onload && + xhiveframework.query_reports[frm.doc.report_name].onload(xhiveframework.query_report); + } + + dialog.set_values(filters); + }); + }, + + render_dynamic_filters_table(frm) { + if (!xhiveframework.boot.developer_mode || !frm.doc.is_standard || frm.doc.type == "Custom") { + return; + } + + frm.set_df_property("dynamic_filters_section", "hidden", 0); + + let is_document_type = frm.doc.type == "Document Type"; + + let wrapper = $(frm.get_field("dynamic_filters_json").wrapper).empty(); + + frm.dynamic_filter_table = + $(` + + + + + + + + +
    ${__("Filter")}${__("Condition")}${__("Value")}
    `).appendTo(wrapper); + + frm.dynamic_filters = + frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2 + ? JSON.parse(frm.doc.dynamic_filters_json) + : null; + + frm.trigger("set_dynamic_filters_in_table"); + + let filters = JSON.parse(frm.doc.filters_json || "[]"); + + let fields = xhiveframework.dashboard_utils.get_fields_for_dynamic_filter_dialog( + is_document_type, + filters, + frm.dynamic_filters + ); + + frm.dynamic_filter_table.on("click", () => { + let dialog = new xhiveframework.ui.Dialog({ + title: __("Set Dynamic Filters"), + fields: fields, + primary_action: () => { + let values = dialog.get_values(); + dialog.hide(); + let dynamic_filters = []; + for (let key of Object.keys(values)) { + if (is_document_type) { + let [doctype, fieldname] = key.split(":"); + dynamic_filters.push([doctype, fieldname, "=", values[key]]); + } + } + + if (is_document_type) { + frm.set_value("dynamic_filters_json", JSON.stringify(dynamic_filters)); + } else { + frm.set_value("dynamic_filters_json", JSON.stringify(values)); + } + frm.trigger("set_dynamic_filters_in_table"); + }, + primary_action_label: "Set", + }); + + dialog.show(); + dialog.set_values(frm.dynamic_filters); + }); + }, + + set_dynamic_filters_in_table: function (frm) { + frm.dynamic_filters = + frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2 + ? JSON.parse(frm.doc.dynamic_filters_json) + : null; + + if (!frm.dynamic_filters) { + const filter_row = $(` + ${__("Click to Set Dynamic Filters")}`); + frm.dynamic_filter_table.find("tbody").html(filter_row); + } else { + let filter_rows = ""; + if ($.isArray(frm.dynamic_filters)) { + frm.dynamic_filters.forEach((filter) => { + filter_rows += ` + ${filter[1]} + ${filter[2] || ""} + ${filter[3]} + `; + }); + } else { + let condition = "="; + for (let [key, val] of Object.entries(frm.dynamic_filters)) { + filter_rows += ` + ${key} + ${condition} + ${val || ""} + `; + } + } + + frm.dynamic_filter_table.find("tbody").html(filter_rows); + } + }, + + set_parent_document_type: async function (frm) { + let document_type = frm.doc.document_type; + let doc_is_table = + document_type && + (await xhiveframework.db.get_value("DocType", document_type, "istable")).message.istable; + + frm.set_df_property("parent_document_type", "hidden", !doc_is_table); + + if (document_type && doc_is_table) { + let parents = await xhiveframework.xcall( + "xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.get_parent_doctypes", + { child_type: document_type } + ); + + frm.set_query("parent_document_type", function () { + return { + filters: { + name: ["in", parents], + }, + }; + }); + + if (parents.length === 1) { + frm.set_value("parent_document_type", parents[0]); + } + } + }, +}); diff --git a/xhiveframework/desk/doctype/number_card/number_card.json b/xhiveframework/desk/doctype/number_card/number_card.json new file mode 100644 index 0000000..cdc357e --- /dev/null +++ b/xhiveframework/desk/doctype/number_card/number_card.json @@ -0,0 +1,252 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2020-04-15 18:06:39.444683", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "is_standard", + "module", + "label", + "type", + "report_name", + "method", + "function", + "aggregate_function_based_on", + "column_break_2", + "document_type", + "parent_document_type", + "report_field", + "report_function", + "is_public", + "custom_configuration_section", + "filters_config", + "stats_section", + "show_percentage_stats", + "stats_time_interval", + "filters_section", + "filters_json", + "dynamic_filters_section", + "dynamic_filters_json", + "section_break_16", + "color" + ], + "fields": [ + { + "depends_on": "eval: doc.type == 'Document Type'", + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Document Type", + "mandatory_depends_on": "eval: doc.type == 'Document Type'", + "options": "DocType" + }, + { + "depends_on": "eval: doc.type == 'Document Type'", + "fieldname": "function", + "fieldtype": "Select", + "label": "Function", + "mandatory_depends_on": "eval: doc.type == 'Document Type'", + "options": "Count\nSum\nAverage\nMinimum\nMaximum" + }, + { + "depends_on": "eval: doc.type === 'Document Type' && doc.function !== 'Count'", + "fieldname": "aggregate_function_based_on", + "fieldtype": "Select", + "label": "Aggregate Function Based On", + "mandatory_depends_on": "eval: doc.function !== 'Count'" + }, + { + "fieldname": "filters_json", + "fieldtype": "Code", + "label": "Filters JSON", + "options": "JSON" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "fieldname": "color", + "fieldtype": "Color", + "label": "Color" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "fieldname": "filters_section", + "fieldtype": "Section Break", + "label": "Filters Section" + }, + { + "default": "0", + "description": "This card will be available to all Users if this is set", + "fieldname": "is_public", + "fieldtype": "Check", + "label": "Is Public" + }, + { + "default": "1", + "fieldname": "show_percentage_stats", + "fieldtype": "Check", + "label": "Show Percentage Stats" + }, + { + "default": "Daily", + "depends_on": "eval: doc.show_percentage_stats", + "description": "Show percentage difference according to this time interval", + "fieldname": "stats_time_interval", + "fieldtype": "Select", + "label": "Stats Time Interval", + "options": "Daily\nWeekly\nMonthly\nYearly" + }, + { + "depends_on": "eval: doc.type == 'Document Type'", + "fieldname": "stats_section", + "fieldtype": "Section Break", + "label": "Stats" + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "no_copy": 1, + "read_only_depends_on": "eval: !xhiveframework.boot.developer_mode" + }, + { + "depends_on": "eval: doc.is_standard", + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "mandatory_depends_on": "eval: doc.is_standard", + "options": "Module Def" + }, + { + "fieldname": "dynamic_filters_json", + "fieldtype": "Code", + "label": "Dynamic Filters JSON", + "options": "JSON" + }, + { + "fieldname": "section_break_16", + "fieldtype": "Section Break" + }, + { + "fieldname": "dynamic_filters_section", + "fieldtype": "Section Break", + "label": "Dynamic Filters Section" + }, + { + "fieldname": "type", + "fieldtype": "Select", + "label": "Type", + "options": "Document Type\nReport\nCustom" + }, + { + "depends_on": "eval: doc.type == 'Report'", + "fieldname": "report_name", + "fieldtype": "Link", + "label": "Report Name", + "mandatory_depends_on": "eval: doc.type == 'Report'", + "options": "Report" + }, + { + "depends_on": "eval: doc.type == 'Report'", + "fieldname": "report_field", + "fieldtype": "Select", + "label": "Field", + "mandatory_depends_on": "eval: doc.type == 'Report'" + }, + { + "depends_on": "eval: doc.type == 'Custom'", + "description": "Set the path to a whitelisted function that will return the data for the number card in the format:\n\n
    \n{\n\t\"value\": value,\n\t\"fieldtype\": \"Currency\",\n\t\"route_options\": {\"from_date\": \"2023-05-23\"},\n\t\"route\": [\"query-report\", \"Permitted Documents For User\"]\n}
    ", + "fieldname": "method", + "fieldtype": "Data", + "label": "Method", + "mandatory_depends_on": "eval: doc.type == 'Custom'" + }, + { + "depends_on": "eval: doc.type == 'Custom'", + "fieldname": "custom_configuration_section", + "fieldtype": "Section Break", + "label": "Custom Configuration" + }, + { + "description": "Set the filters here. For example:\n
    \n[{\n\tfieldname: \"company\",\n\tlabel: __(\"Company\"),\n\tfieldtype: \"Link\",\n\toptions: \"Company\",\n\tdefault: xhiveframework.defaults.get_user_default(\"Company\"),\n\treqd: 1\n},\n{\n\tfieldname: \"account\",\n\tlabel: __(\"Account\"),\n\tfieldtype: \"Link\",\n\toptions: \"Account\",\n\treqd: 1\n}]\n
    ", + "fieldname": "filters_config", + "fieldtype": "Code", + "label": "Filters Configuration", + "options": "JSON" + }, + { + "depends_on": "eval: doc.type == 'Report'", + "fieldname": "report_function", + "fieldtype": "Select", + "label": "Function", + "mandatory_depends_on": "eval: doc.type == 'Report'", + "options": "Sum\nAverage\nMinimum\nMaximum" + }, + { + "depends_on": "eval: doc.type === 'Document Type'", + "description": "The document type selected is a child table, so the parent document type is required.", + "fieldname": "parent_document_type", + "fieldtype": "Link", + "label": "Parent Document Type", + "options": "DocType" + } + ], + "links": [], + "modified": "2023-11-09 13:44:00.280846", + "modified_by": "Administrator", + "module": "Desk", + "name": "Number Card", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Dashboard Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "search_fields": "label, document_type", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "label", + "track_changes": 1 +} diff --git a/xhiveframework/desk/doctype/number_card/number_card.py b/xhiveframework/desk/doctype/number_card/number_card.py new file mode 100644 index 0000000..dd1ecc4 --- /dev/null +++ b/xhiveframework/desk/doctype/number_card/number_card.py @@ -0,0 +1,260 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.boot import get_allowed_report_names +from xhiveframework.config import get_modules_from_all_apps_for_user +from xhiveframework.model.document import Document +from xhiveframework.model.naming import append_number_if_name_exists +from xhiveframework.modules.export_file import export_to_files +from xhiveframework.query_builder import Criterion +from xhiveframework.query_builder.utils import DocType +from xhiveframework.utils import cint, flt + + +class NumberCard(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + aggregate_function_based_on: DF.Literal[None] + color: DF.Color | None + document_type: DF.Link | None + dynamic_filters_json: DF.Code | None + filters_config: DF.Code | None + filters_json: DF.Code | None + function: DF.Literal["Count", "Sum", "Average", "Minimum", "Maximum"] + is_public: DF.Check + is_standard: DF.Check + label: DF.Data + method: DF.Data | None + module: DF.Link | None + parent_document_type: DF.Link | None + report_field: DF.Literal[None] + report_function: DF.Literal["Sum", "Average", "Minimum", "Maximum"] + report_name: DF.Link | None + show_percentage_stats: DF.Check + stats_time_interval: DF.Literal["Daily", "Weekly", "Monthly", "Yearly"] + type: DF.Literal["Document Type", "Report", "Custom"] + + # end: auto-generated types + def autoname(self): + if not self.name: + self.name = self.label + + if xhiveframework.db.exists("Number Card", self.name): + self.name = append_number_if_name_exists("Number Card", self.name) + + def validate(self): + if self.type == "Document Type": + if not (self.document_type and self.function): + xhiveframework.throw(_("Document Type and Function are required to create a number card")) + + if self.function != "Count" and not self.aggregate_function_based_on: + xhiveframework.throw(_("Aggregate Field is required to create a number card")) + + if xhiveframework.get_meta(self.document_type).istable and not self.parent_document_type: + xhiveframework.throw(_("Parent Document Type is required to create a number card")) + + elif self.type == "Report": + if not (self.report_name and self.report_field and self.function): + xhiveframework.throw(_("Report Name, Report Field and Fucntion are required to create a number card")) + + elif self.type == "Custom": + if not self.method: + xhiveframework.throw(_("Method is required to create a number card")) + + def on_update(self): + if xhiveframework.conf.developer_mode and self.is_standard: + export_to_files(record_list=[["Number Card", self.name]], record_module=self.module) + + +def get_permission_query_conditions(user=None): + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + return + + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return None + + doctype_condition = False + module_condition = False + + allowed_doctypes = [xhiveframework.db.escape(doctype) for doctype in xhiveframework.permissions.get_doctypes_with_read()] + allowed_modules = [ + xhiveframework.db.escape(module.get("module_name")) for module in get_modules_from_all_apps_for_user() + ] + + if allowed_doctypes: + doctype_condition = "`tabNumber Card`.`document_type` in ({allowed_doctypes})".format( + allowed_doctypes=",".join(allowed_doctypes) + ) + if allowed_modules: + module_condition = """`tabNumber Card`.`module` in ({allowed_modules}) + or `tabNumber Card`.`module` is NULL""".format(allowed_modules=",".join(allowed_modules)) + + return f""" + {doctype_condition} + and + {module_condition} + """ + + +def has_permission(doc, ptype, user): + roles = xhiveframework.get_roles(user) + if "System Manager" in roles: + return True + + if doc.type == "Report": + if doc.report_name in get_allowed_report_names(): + return True + else: + allowed_doctypes = tuple(xhiveframework.permissions.get_doctypes_with_read()) + if doc.document_type in allowed_doctypes: + return True + + return False + + +@xhiveframework.whitelist() +def get_result(doc, filters, to_date=None): + doc = xhiveframework.parse_json(doc) + fields = [] + sql_function_map = { + "Count": "count", + "Sum": "sum", + "Average": "avg", + "Minimum": "min", + "Maximum": "max", + } + + function = sql_function_map[doc.function] + + if function == "count": + fields = [f"{function}(*) as result"] + else: + fields = [f"{function}({doc.aggregate_function_based_on}) as result"] + + if not filters: + filters = [] + elif isinstance(filters, str): + filters = xhiveframework.parse_json(filters) + + if to_date: + filters.append([doc.document_type, "creation", "<", to_date]) + + res = xhiveframework.get_list( + doc.document_type, fields=fields, filters=filters, parent_doctype=doc.parent_document_type + ) + number = res[0]["result"] if res else 0 + + return flt(number) + + +@xhiveframework.whitelist() +def get_percentage_difference(doc, filters, result): + doc = xhiveframework.parse_json(doc) + result = xhiveframework.parse_json(result) + + doc = xhiveframework.get_doc("Number Card", doc.name) + + if not doc.get("show_percentage_stats"): + return + + previous_result = calculate_previous_result(doc, filters) + if previous_result == 0: + return None + else: + if result == previous_result: + return 0 + else: + return ((result / previous_result) - 1) * 100.0 + + +def calculate_previous_result(doc, filters): + from xhiveframework.utils import add_to_date + + current_date = xhiveframework.utils.now() + if doc.stats_time_interval == "Daily": + previous_date = add_to_date(current_date, days=-1) + elif doc.stats_time_interval == "Weekly": + previous_date = add_to_date(current_date, weeks=-1) + elif doc.stats_time_interval == "Monthly": + previous_date = add_to_date(current_date, months=-1) + else: + previous_date = add_to_date(current_date, years=-1) + + return get_result(doc, filters, previous_date) + + +@xhiveframework.whitelist() +def create_number_card(args): + args = xhiveframework.parse_json(args) + doc = xhiveframework.new_doc("Number Card") + + doc.update(args) + doc.insert(ignore_permissions=True) + return doc + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_cards_for_user(doctype, txt, searchfield, start, page_len, filters): + meta = xhiveframework.get_meta(doctype) + searchfields = meta.get_search_fields() + search_conditions = [] + + if not xhiveframework.db.exists("DocType", doctype): + return + + numberCard = DocType("Number Card") + + if txt: + search_conditions = [numberCard[field].like(f"%{txt}%") for field in searchfields] + + condition_query = xhiveframework.qb.get_query( + doctype, + filters=filters, + validate_filters=True, + ) + + return ( + condition_query.select(numberCard.name, numberCard.label, numberCard.document_type) + .where((numberCard.owner == xhiveframework.session.user) | (numberCard.is_public == 1)) + .where(Criterion.any(search_conditions)) + ).run() + + +@xhiveframework.whitelist() +def create_report_number_card(args): + card = create_number_card(args) + args = xhiveframework.parse_json(args) + args.name = card.name + if args.dashboard: + add_card_to_dashboard(xhiveframework.as_json(args)) + + +@xhiveframework.whitelist() +def add_card_to_dashboard(args): + args = xhiveframework.parse_json(args) + + dashboard = xhiveframework.get_doc("Dashboard", args.dashboard) + dashboard_link = xhiveframework.new_doc("Number Card Link") + dashboard_link.card = args.name + + if args.set_standard and dashboard.is_standard: + card = xhiveframework.get_doc("Number Card", dashboard_link.card) + card.is_standard = 1 + card.module = dashboard.module + card.save() + + dashboard.append("cards", dashboard_link) + dashboard.save() diff --git a/xhiveframework/desk/doctype/number_card/test_number_card.py b/xhiveframework/desk/doctype/number_card/test_number_card.py new file mode 100644 index 0000000..48024e3 --- /dev/null +++ b/xhiveframework/desk/doctype/number_card/test_number_card.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNumberCard(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/number_card_link/__init__.py b/xhiveframework/desk/doctype/number_card_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/number_card_link/number_card_link.json b/xhiveframework/desk/doctype/number_card_link/number_card_link.json new file mode 100644 index 0000000..ac035b3 --- /dev/null +++ b/xhiveframework/desk/doctype/number_card_link/number_card_link.json @@ -0,0 +1,31 @@ +{ + "actions": [], + "creation": "2020-04-19 17:43:50.858343", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "card" + ], + "fields": [ + { + "fieldname": "card", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Card", + "options": "Number Card" + } + ], + "istable": 1, + "links": [], + "modified": "2020-04-19 17:45:11.878472", + "modified_by": "Administrator", + "module": "Desk", + "name": "Number Card Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/number_card_link/number_card_link.py b/xhiveframework/desk/doctype/number_card_link/number_card_link.py new file mode 100644 index 0000000..2913df6 --- /dev/null +++ b/xhiveframework/desk/doctype/number_card_link/number_card_link.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class NumberCardLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + card: DF.Link | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/onboarding_permission/__init__.py b/xhiveframework/desk/doctype/onboarding_permission/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.js b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.js new file mode 100644 index 0000000..2ab83cf --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.js @@ -0,0 +1,7 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Onboarding Permission", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.json b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.json new file mode 100644 index 0000000..f2a9dc3 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.json @@ -0,0 +1,32 @@ +{ + "actions": [], + "creation": "2020-04-30 18:27:48.255489", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "role" + ], + "fields": [ + { + "fieldname": "role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Role", + "options": "Role", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2020-04-30 18:28:40.423802", + "modified_by": "Administrator", + "module": "Desk", + "name": "Onboarding Permission", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.py b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.py new file mode 100644 index 0000000..d625a57 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_permission/onboarding_permission.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class OnboardingPermission(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + role: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/onboarding_permission/test_onboarding_permission.py b/xhiveframework/desk/doctype/onboarding_permission/test_onboarding_permission.py new file mode 100644 index 0000000..0119d06 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_permission/test_onboarding_permission.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestOnboardingPermission(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/onboarding_step/__init__.py b/xhiveframework/desk/doctype/onboarding_step/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/onboarding_step/onboarding_step.js b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.js new file mode 100644 index 0000000..18300f9 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.js @@ -0,0 +1,86 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Onboarding Step", { + setup: function (frm) { + frm.set_query("form_tour", function () { + return { + filters: { + reference_doctype: frm.doc.reference_document, + }, + }; + }); + }, + + refresh: function (frm) { + xhiveframework.boot.developer_mode && + frm.set_intro( + __( + "To export this step as JSON, link it in a Onboarding document and save the document." + ), + true + ); + if (frm.doc.reference_document && frm.doc.action == "Update Settings") { + setup_fields(frm); + } + + if (!xhiveframework.boot.developer_mode) { + frm.trigger("disable_form"); + } + }, + + reference_document: function (frm) { + if (frm.doc.reference_document && frm.doc.action == "Update Settings") { + setup_fields(frm); + } + }, + + action: function (frm) { + if (frm.doc.action == "Show Form Tour") { + frm.fields_dict.reference_document + .set_description(`You need to add the steps in the contoller JS file. For example: note.js +
    
    +xhiveframework.tour['Note'] = [
    +	{
    +		fieldname: "title",
    +		title: "Title of the Note",
    +		description: "...",
    +	}
    +];
    +
    + `); + } else { + frm.fields_dict.reference_document.set_description(null); + } + }, + + disable_form: function (frm) { + frm.set_read_only(); + frm.fields + .filter((field) => field.has_input) + .forEach((field) => { + frm.set_df_property(field.df.fieldname, "read_only", "1"); + }); + frm.disable_save(); + }, +}); + +function setup_fields(frm) { + if (frm.doc.reference_document && frm.doc.action == "Update Settings") { + xhiveframework.model.with_doctype(frm.doc.reference_document, () => { + let fields = xhiveframework + .get_meta(frm.doc.reference_document) + .fields.filter((df) => { + return ["Data", "Check", "Int", "Link", "Select"].includes(df.fieldtype); + }) + .map((df) => { + return { + label: `${__(df.label, null, df.parent)} (${df.fieldname})`, + value: df.fieldname, + }; + }); + + frm.set_df_property("field", "options", fields); + }); + } +} diff --git a/xhiveframework/desk/doctype/onboarding_step/onboarding_step.json b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.json new file mode 100644 index 0000000..f81550b --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.json @@ -0,0 +1,255 @@ +{ + "actions": [], + "autoname": "prompt", + "creation": "2020-04-14 15:50:25.782387", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "title", + "column_break_2", + "is_complete", + "is_skipped", + "description_section", + "description", + "intro_video_url", + "section_break_5", + "action", + "action_label", + "column_break_7", + "reference_document", + "show_full_form", + "show_form_tour", + "form_tour", + "is_single", + "reference_report", + "report_reference_doctype", + "report_type", + "report_description", + "path", + "callback_title", + "callback_message", + "validate_action", + "field", + "value_to_validate", + "video_url" + ], + "fields": [ + { + "default": "0", + "fieldname": "is_complete", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Complete" + }, + { + "fieldname": "title", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Title", + "reqd": 1 + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_5", + "fieldtype": "Section Break" + }, + { + "fieldname": "action", + "fieldtype": "Select", + "label": "Action", + "options": "Create Entry\nUpdate Settings\nShow Form Tour\nView Report\nGo to Page\nWatch Video", + "reqd": 1 + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval:doc.action == \"Create Entry\" || doc.action == \"Update Settings\" || doc.action == \"Create Entry\" || doc.action == \"Show Form Tour\"", + "fieldname": "reference_document", + "fieldtype": "Link", + "label": "Reference Document", + "mandatory_depends_on": "eval:doc.action == \"Create Entry\" || doc.action == \"Update Settings\" || doc.action == \"Create Entry\" || doc.action == \"Show Form Tour\"", + "options": "DocType" + }, + { + "depends_on": "eval:doc.action == \"View Report\"", + "fieldname": "reference_report", + "fieldtype": "Link", + "label": "Reference Report", + "mandatory_depends_on": "eval:doc.action == \"View Report\"", + "options": "Report" + }, + { + "depends_on": "eval:doc.action == \"Watch Video\"", + "fieldname": "video_url", + "fieldtype": "Data", + "label": "Video URL", + "mandatory_depends_on": "eval:doc.action == \"Watch Video\"" + }, + { + "depends_on": "eval:doc.action == \"View Report\"", + "fetch_from": "reference_report.report_type", + "fieldname": "report_type", + "fieldtype": "Data", + "label": "Report Type", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "is_skipped", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Skipped" + }, + { + "depends_on": "eval:doc.action == \"Update Settings\" && doc.validate_action", + "fieldname": "field", + "fieldtype": "Select", + "label": "Field", + "mandatory_depends_on": "eval:doc.action == \"Update Settings\" && doc.validate_action" + }, + { + "depends_on": "eval:doc.action == \"Update Settings\" && doc.validate_action", + "description": "Use % for any non empty value.", + "fieldname": "value_to_validate", + "fieldtype": "Data", + "label": "Value to Validate", + "mandatory_depends_on": "eval:doc.action == \"Update Settings\" && doc.validate_action" + }, + { + "depends_on": "eval:doc.action == \"View Report\"", + "description": "This will be shown to the user in a dialog after routing to the report", + "fieldname": "report_description", + "fieldtype": "Data", + "label": "Report Description", + "mandatory_depends_on": "eval:doc.action == \"View Report\"" + }, + { + "fetch_from": "reference_report.ref_doctype", + "fieldname": "report_reference_doctype", + "fieldtype": "Data", + "label": "Report Reference Doctype", + "read_only": 1 + }, + { + "default": "0", + "depends_on": "eval:doc.action == \"Create Entry\" || doc.action == \"Update Settings\" || doc.action == \"Create Entry\" || doc.action == \"Show Form Tour\"", + "fetch_from": "reference_document.issingle", + "fieldname": "is_single", + "fieldtype": "Check", + "label": "Is Single" + }, + { + "depends_on": "eval:doc.action == \"Go to Page\"", + "description": "Example: #Tree/Account", + "fieldname": "path", + "fieldtype": "Data", + "label": "Path", + "mandatory_depends_on": "eval:doc.action == \"Go to Page\"" + }, + { + "depends_on": "eval:doc.action == \"Go to Page\"", + "fieldname": "callback_title", + "fieldtype": "Data", + "label": "Callback Title" + }, + { + "depends_on": "eval:doc.action == \"Go to Page\"", + "description": "This will be shown in a modal after routing", + "fieldname": "callback_message", + "fieldtype": "Small Text", + "label": "Callback Message" + }, + { + "default": "1", + "depends_on": "eval:doc.action == \"Update Settings\"", + "fieldname": "validate_action", + "fieldtype": "Check", + "label": "Validate Field" + }, + { + "default": "0", + "depends_on": "eval:doc.action == \"Create Entry\"", + "description": "Show full form instead of a quick entry modal", + "fieldname": "show_full_form", + "fieldtype": "Check", + "label": "Show Full Form?" + }, + { + "description": "Description to inform the user about any action that is going to be performed", + "fieldname": "description_section", + "fieldtype": "Section Break", + "label": "Description" + }, + { + "fieldname": "description", + "fieldtype": "Markdown Editor", + "label": "Description" + }, + { + "fieldname": "intro_video_url", + "fieldtype": "Data", + "label": "Intro Video URL" + }, + { + "fieldname": "action_label", + "fieldtype": "Data", + "label": "Action Label" + }, + { + "default": "0", + "depends_on": "eval:doc.action==\"Create Entry\" && doc.show_full_form", + "fieldname": "show_form_tour", + "fieldtype": "Check", + "label": "Show Form Tour" + }, + { + "depends_on": "show_form_tour", + "fieldname": "form_tour", + "fieldtype": "Link", + "label": "Form Tour", + "options": "Form Tour" + } + ], + "links": [], + "modified": "2023-08-28 22:23:48.174317", + "modified_by": "Administrator", + "module": "Desk", + "name": "Onboarding Step", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Administrator", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "quick_entry": 1, + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/onboarding_step/onboarding_step.py b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.py new file mode 100644 index 0000000..a84c380 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step/onboarding_step.py @@ -0,0 +1,65 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class OnboardingStep(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action: DF.Literal[ + "Create Entry", "Update Settings", "Show Form Tour", "View Report", "Go to Page", "Watch Video" + ] + action_label: DF.Data | None + callback_message: DF.SmallText | None + callback_title: DF.Data | None + description: DF.MarkdownEditor | None + field: DF.Literal[None] + form_tour: DF.Link | None + intro_video_url: DF.Data | None + is_complete: DF.Check + is_single: DF.Check + is_skipped: DF.Check + path: DF.Data | None + reference_document: DF.Link | None + reference_report: DF.Link | None + report_description: DF.Data | None + report_reference_doctype: DF.Data | None + report_type: DF.Data | None + show_form_tour: DF.Check + show_full_form: DF.Check + title: DF.Data + validate_action: DF.Check + value_to_validate: DF.Data | None + video_url: DF.Data | None + + # end: auto-generated types + def before_export(self, doc): + doc.is_complete = 0 + doc.is_skipped = 0 + + +@xhiveframework.whitelist() +def get_onboarding_steps(ob_steps): + steps = [] + for s in json.loads(ob_steps): + doc = xhiveframework.get_doc("Onboarding Step", s.get("step")) + step = doc.as_dict().copy() + step.label = _(doc.title) + if step.action == "Create Entry": + step.is_submittable = xhiveframework.db.get_value( + "DocType", step.reference_document, "is_submittable", cache=True + ) + steps.append(step) + + return steps diff --git a/xhiveframework/desk/doctype/onboarding_step/test_onboarding_step.py b/xhiveframework/desk/doctype/onboarding_step/test_onboarding_step.py new file mode 100644 index 0000000..0f3dc98 --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step/test_onboarding_step.py @@ -0,0 +1,8 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestOnboardingStep(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/onboarding_step_map/__init__.py b/xhiveframework/desk/doctype/onboarding_step_map/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.json b/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.json new file mode 100644 index 0000000..e501a0b --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.json @@ -0,0 +1,32 @@ +{ + "actions": [], + "creation": "2020-04-28 22:06:08.544187", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "step" + ], + "fields": [ + { + "fieldname": "step", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Step", + "options": "Onboarding Step", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2020-04-28 22:06:09.503406", + "modified_by": "Administrator", + "module": "Desk", + "name": "Onboarding Step Map", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.py b/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.py new file mode 100644 index 0000000..e2675fb --- /dev/null +++ b/xhiveframework/desk/doctype/onboarding_step_map/onboarding_step_map.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class OnboardingStepMap(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + step: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/route_history/__init__.py b/xhiveframework/desk/doctype/route_history/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/route_history/route_history.js b/xhiveframework/desk/doctype/route_history/route_history.js new file mode 100644 index 0000000..a647e27 --- /dev/null +++ b/xhiveframework/desk/doctype/route_history/route_history.js @@ -0,0 +1,6 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Route History", { + refresh: function () {}, +}); diff --git a/xhiveframework/desk/doctype/route_history/route_history.json b/xhiveframework/desk/doctype/route_history/route_history.json new file mode 100644 index 0000000..0b96277 --- /dev/null +++ b/xhiveframework/desk/doctype/route_history/route_history.json @@ -0,0 +1,54 @@ +{ + "actions": [], + "allow_copy": 1, + "creation": "2018-10-05 11:26:04.601113", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "route", + "user" + ], + "fields": [ + { + "fieldname": "route", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Route", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "User", + "no_copy": 1, + "options": "User", + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2023-12-04 04:41:32.448331", + "modified_by": "Administrator", + "module": "Desk", + "name": "Route History", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "route" +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/route_history/route_history.py b/xhiveframework/desk/doctype/route_history/route_history.py new file mode 100644 index 0000000..fb87b36 --- /dev/null +++ b/xhiveframework/desk/doctype/route_history/route_history.py @@ -0,0 +1,54 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.deferred_insert import deferred_insert as _deferred_insert +from xhiveframework.model.document import Document + + +class RouteHistory(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + route: DF.Data | None + user: DF.Link | None + # end: auto-generated types + + @staticmethod + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Route History") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + +@xhiveframework.whitelist() +def deferred_insert(routes): + routes = [ + { + "user": xhiveframework.session.user, + "route": route.get("route"), + "creation": route.get("creation"), + } + for route in xhiveframework.parse_json(routes) + ] + + _deferred_insert("Route History", routes) + + +@xhiveframework.whitelist() +def frequently_visited_links(): + return xhiveframework.get_all( + "Route History", + fields=["route", "count(name) as count"], + filters={"user": xhiveframework.session.user}, + group_by="route", + order_by="count desc", + limit=5, + ) diff --git a/xhiveframework/desk/doctype/route_history/route_history_list.js b/xhiveframework/desk/doctype/route_history/route_history_list.js new file mode 100644 index 0000000..6e52d5c --- /dev/null +++ b/xhiveframework/desk/doctype/route_history/route_history_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Route History"] = { + onload: function (listview) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(cur_list.doctype); + }); + }, +}; diff --git a/xhiveframework/desk/doctype/system_console/__init__.py b/xhiveframework/desk/doctype/system_console/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/system_console/system_console.js b/xhiveframework/desk/doctype/system_console/system_console.js new file mode 100644 index 0000000..5e75e31 --- /dev/null +++ b/xhiveframework/desk/doctype/system_console/system_console.js @@ -0,0 +1,115 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("System Console", { + onload: function (frm) { + xhiveframework.ui.keys.add_shortcut({ + shortcut: "shift+enter", + action: () => frm.page.btn_primary.trigger("click"), + page: frm.page, + description: __("Execute Console script"), + ignore_inputs: true, + }); + }, + + refresh: function (frm) { + frm.disable_save(); + frm.page.set_primary_action(__("Execute"), ($btn) => { + $btn.text(__("Executing...")); + return frm + .execute_action("Execute") + .then(() => frm.trigger("render_sql_output")) + .finally(() => $btn.text(__("Execute"))); + }); + if ( + window.localStorage.getItem("system_console_code") && + window.localStorage.getItem("system_console_type") + ) { + frm.set_value("type", localStorage.getItem("system_console_type")); + frm.set_value("console", localStorage.getItem("system_console_code")); + frm.set_value("output", ""); + window.localStorage.removeItem("system_console_code"); + window.localStorage.removeItem("system_console_type"); + } + }, + + type: function (frm) { + if (frm.doc.type == "Python") { + frm.set_value("output", ""); + if (frm.sql_output) { + frm.sql_output.destroy(); + frm.get_field("sql_output").html(""); + } + } + }, + + render_sql_output: function (frm) { + if (frm.doc.type !== "SQL") return; + if (frm.sql_output) { + frm.sql_output.destroy(); + frm.get_field("sql_output").html(""); + } + + if (frm.doc.output.startsWith("Traceback")) { + return; + } + + let result = JSON.parse(frm.doc.output); + frm.set_value("output", `${result.length} ${result.length == 1 ? "row" : "rows"}`); + + if (result.length) { + let columns = Object.keys(result[0]); + frm.sql_output = new DataTable(frm.get_field("sql_output").$wrapper.get(0), { + columns, + data: result, + }); + } + }, + + show_processlist: function (frm) { + if (frm.doc.show_processlist) { + // keep refreshing every 5 seconds + frm.events.refresh_processlist(frm); + frm.processlist_interval = setInterval( + () => frm.events.refresh_processlist(frm), + 5000 + ); + } else { + if (frm.processlist_interval) { + // end it + clearInterval(frm.processlist_interval); + frm.get_field("processlist").html(""); + } + } + }, + + refresh_processlist: function (frm) { + let timestamp = new Date(); + xhiveframework + .call("xhiveframework.desk.doctype.system_console.system_console.show_processlist") + .then((r) => { + let rows = ""; + for (let row of r.message) { + rows += ` + ${row.Id} + ${row.Time} + ${row.State} + ${row.Info} + ${row.Progress} + `; + } + + frm.get_field("processlist").html(` +

    Requested on: ${timestamp}

    + + + + ${rows}`); + }); + }, +}); diff --git a/xhiveframework/desk/doctype/system_console/system_console.json b/xhiveframework/desk/doctype/system_console/system_console.json new file mode 100644 index 0000000..d367a93 --- /dev/null +++ b/xhiveframework/desk/doctype/system_console/system_console.json @@ -0,0 +1,109 @@ +{ + "actions": [ + { + "action": "/app/console-log", + "action_type": "Route", + "label": "Logs" + }, + { + "action": "xhiveframework.desk.doctype.system_console.system_console.execute_code", + "action_type": "Server Action", + "hidden": 1, + "label": "Execute" + } + ], + "creation": "2020-08-18 17:44:35.647815", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "execute_section", + "type", + "console", + "commit", + "output", + "sql_output", + "database_processes_section", + "show_processlist", + "processlist" + ], + "fields": [ + { + "description": "To print output use log(text)", + "fieldname": "console", + "fieldtype": "Code", + "label": "Console", + "options": "Python" + }, + { + "fieldname": "output", + "fieldtype": "Code", + "label": "Output", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "commit", + "fieldtype": "Check", + "label": "Commit" + }, + { + "fieldname": "execute_section", + "fieldtype": "Section Break", + "label": "Execute" + }, + { + "fieldname": "database_processes_section", + "fieldtype": "Section Break", + "label": "Database Processes" + }, + { + "default": "0", + "fieldname": "show_processlist", + "fieldtype": "Check", + "label": "Show Processlist" + }, + { + "fieldname": "processlist", + "fieldtype": "HTML", + "label": "processlist" + }, + { + "default": "Python", + "fieldname": "type", + "fieldtype": "Select", + "label": "Type", + "options": "Python\nSQL" + }, + { + "depends_on": "eval:doc.type == 'SQL'", + "fieldname": "sql_output", + "fieldtype": "HTML", + "label": "SQL Output" + } + ], + "hide_toolbar": 1, + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2022-04-15 14:15:58.398590", + "modified_by": "Administrator", + "module": "Desk", + "name": "System Console", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/desk/doctype/system_console/system_console.py b/xhiveframework/desk/doctype/system_console/system_console.py new file mode 100644 index 0000000..08dcad5 --- /dev/null +++ b/xhiveframework/desk/doctype/system_console/system_console.py @@ -0,0 +1,73 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.utils.safe_exec import read_sql, safe_exec + + +class SystemConsole(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + commit: DF.Check + console: DF.Code | None + output: DF.Code | None + show_processlist: DF.Check + type: DF.Literal["Python", "SQL"] + + # end: auto-generated types + def run(self): + xhiveframework.only_for("System Manager") + try: + xhiveframework.local.debug_log = [] + if self.type == "Python": + safe_exec(self.console, script_filename="System Console") + self.output = "\n".join(xhiveframework.debug_log) + elif self.type == "SQL": + self.output = xhiveframework.as_json(read_sql(self.console, as_dict=1)) + except Exception: + self.commit = False + self.output = xhiveframework.get_traceback() + + if self.commit: + xhiveframework.db.commit() + else: + xhiveframework.db.rollback() + xhiveframework.get_doc( + dict(doctype="Console Log", script=self.console, type=self.type, committed=self.commit) + ).insert() + xhiveframework.db.commit() + + +@xhiveframework.whitelist() +def execute_code(doc): + console = xhiveframework.get_doc(json.loads(doc)) + console.run() + return console.as_dict() + + +@xhiveframework.whitelist() +def show_processlist(): + xhiveframework.only_for("System Manager") + + return xhiveframework.db.multisql( + { + "postgres": """ + SELECT pid AS "Id", + query_start AS "Time", + state AS "State", + query AS "Info", + wait_event AS "Progress" + FROM pg_stat_activity""", + "mariadb": "show full processlist", + }, + as_dict=True, + ) diff --git a/xhiveframework/desk/doctype/system_console/test_system_console.py b/xhiveframework/desk/doctype/system_console/test_system_console.py new file mode 100644 index 0000000..f2c8cc6 --- /dev/null +++ b/xhiveframework/desk/doctype/system_console/test_system_console.py @@ -0,0 +1,36 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSystemConsole(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls) -> None: + cls.enable_safe_exec() + return super().setUpClass() + + def test_system_console(self): + system_console = xhiveframework.get_doc("System Console") + system_console.console = 'log("hello")' + system_console.run() + + self.assertEqual(system_console.output, "hello") + + system_console.console = 'log(xhiveframework.db.get_value("DocType", "DocType", "module"))' + system_console.run() + + self.assertEqual(system_console.output, "Core") + + def test_system_console_sql(self): + system_console = xhiveframework.get_doc("System Console") + system_console.type = "SQL" + system_console.console = "select 'test'" + system_console.run() + + self.assertIn("test", system_console.output) + + system_console.console = "update `tabDocType` set is_virtual = 1 where name = 'xyz'" + system_console.run() + + self.assertIn("PermissionError", system_console.output) diff --git a/xhiveframework/desk/doctype/tag/__init__.py b/xhiveframework/desk/doctype/tag/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/tag/tag.js b/xhiveframework/desk/doctype/tag/tag.js new file mode 100644 index 0000000..00a9f47 --- /dev/null +++ b/xhiveframework/desk/doctype/tag/tag.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Tag", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/desk/doctype/tag/tag.json b/xhiveframework/desk/doctype/tag/tag.json new file mode 100644 index 0000000..ad9838d --- /dev/null +++ b/xhiveframework/desk/doctype/tag/tag.json @@ -0,0 +1,50 @@ +{ + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2016-05-25 09:43:44.767581", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "description" + ], + "fields": [ + { + "fieldname": "description", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Description" + } + ], + "modified": "2019-09-25 17:47:41.712237", + "modified_by": "Administrator", + "module": "Desk", + "name": "Tag", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC" +} diff --git a/xhiveframework/desk/doctype/tag/tag.py b/xhiveframework/desk/doctype/tag/tag.py new file mode 100644 index 0000000..e5a05f3 --- /dev/null +++ b/xhiveframework/desk/doctype/tag/tag.py @@ -0,0 +1,205 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.query_builder import DocType +from xhiveframework.utils import unique + + +class Tag(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + description: DF.SmallText | None + # end: auto-generated types + pass + + +def check_user_tags(dt): + "if the user does not have a tags column, then it creates one" + try: + doctype = DocType(dt) + xhiveframework.qb.from_(doctype).select(doctype._user_tags).limit(1).run() + except Exception as e: + if xhiveframework.db.is_column_missing(e): + DocTags(dt).setup() + + +@xhiveframework.whitelist() +def add_tag(tag, dt, dn, color=None): + "adds a new tag to a record, and creates the Tag master" + DocTags(dt).add(dn, tag) + + return tag + + +@xhiveframework.whitelist() +def add_tags(tags, dt, docs, color=None): + "adds a new tag to a record, and creates the Tag master" + tags = xhiveframework.parse_json(tags) + docs = xhiveframework.parse_json(docs) + for doc in docs: + for tag in tags: + DocTags(dt).add(doc, tag) + + +@xhiveframework.whitelist() +def remove_tag(tag, dt, dn): + "removes tag from the record" + DocTags(dt).remove(dn, tag) + + +@xhiveframework.whitelist() +def get_tagged_docs(doctype, tag): + xhiveframework.has_permission(doctype, throw=True) + doctype = DocType(doctype) + return (xhiveframework.qb.from_(doctype).where(doctype._user_tags.like(tag)).select(doctype.name)).run() + + +@xhiveframework.whitelist() +def get_tags(doctype, txt): + tag = xhiveframework.get_list("Tag", filters=[["name", "like", f"%{txt}%"]]) + tags = [t.name for t in tag] + + return sorted(filter(lambda t: t and txt.casefold() in t.casefold(), list(set(tags)))) + + +class DocTags: + """Tags for a particular doctype""" + + def __init__(self, dt): + self.dt = dt + + def get_tag_fields(self): + """returns tag_fields property""" + return xhiveframework.db.get_value("DocType", self.dt, "tag_fields") + + def get_tags(self, dn): + """returns tag for a particular item""" + return (xhiveframework.db.get_value(self.dt, dn, "_user_tags", ignore=1) or "").strip() + + def add(self, dn, tag): + """add a new user tag""" + tl = self.get_tags(dn).split(",") + if tag not in tl: + tl.append(tag) + if not xhiveframework.db.exists("Tag", tag): + xhiveframework.get_doc({"doctype": "Tag", "name": tag}).insert(ignore_permissions=True) + self.update(dn, tl) + + def remove(self, dn, tag): + """remove a user tag""" + tl = self.get_tags(dn).split(",") + self.update(dn, filter(lambda x: x.lower() != tag.lower(), tl)) + + def remove_all(self, dn): + """remove all user tags (call before delete)""" + self.update(dn, []) + + def update(self, dn, tl): + """updates the _user_tag column in the table""" + + if not tl: + tags = "" + else: + tl = unique(filter(lambda x: x, tl)) + tags = "," + ",".join(tl) + try: + xhiveframework.db.sql( + "update `tab{}` set _user_tags={} where name={}".format(self.dt, "%s", "%s"), (tags, dn) + ) + doc = xhiveframework.get_doc(self.dt, dn) + update_tags(doc, tags) + except Exception as e: + if xhiveframework.db.is_column_missing(e): + if not tags: + # no tags, nothing to do + return + + self.setup() + self.update(dn, tl) + else: + raise + + def setup(self): + """adds the _user_tags column if not exists""" + from xhiveframework.database.schema import add_column + + add_column(self.dt, "_user_tags", "Data") + + +def delete_tags_for_document(doc): + """ + Delete the Tag Link entry of a document that has + been deleted + :param doc: Deleted document + """ + if not xhiveframework.db.table_exists("Tag Link"): + return + + xhiveframework.db.delete("Tag Link", {"document_type": doc.doctype, "document_name": doc.name}) + + +def update_tags(doc, tags): + """Adds tags for documents + + :param doc: Document to be added to global tags + """ + doc.check_permission("write") + new_tags = {tag.strip() for tag in tags.split(",") if tag} + existing_tags = [ + tag.tag + for tag in xhiveframework.get_list( + "Tag Link", filters={"document_type": doc.doctype, "document_name": doc.name}, fields=["tag"] + ) + ] + + added_tags = set(new_tags) - set(existing_tags) + for tag in added_tags: + xhiveframework.get_doc( + { + "doctype": "Tag Link", + "document_type": doc.doctype, + "document_name": doc.name, + "title": doc.get_title() or "", + "tag": tag, + } + ).insert(ignore_permissions=True) + + deleted_tags = list(set(existing_tags) - set(new_tags)) + for tag in deleted_tags: + xhiveframework.db.delete("Tag Link", {"document_type": doc.doctype, "document_name": doc.name, "tag": tag}) + + +@xhiveframework.whitelist() +def get_documents_for_tag(tag): + """ + Search for given text in Tag Link + :param tag: tag to be searched + """ + # remove hastag `#` from tag + tag = tag[1:] + + result = xhiveframework.get_list( + "Tag Link", filters={"tag": tag}, fields=["document_type", "document_name", "title", "tag"] + ) + + return [ + { + "doctype": res.document_type, + "name": res.document_name, + "content": res.title, + } + for res in result + ] + + +@xhiveframework.whitelist() +def get_tags_list_for_awesomebar(): + return xhiveframework.get_list("Tag", pluck="name", order_by=None) diff --git a/xhiveframework/desk/doctype/tag/test_tag.py b/xhiveframework/desk/doctype/tag/test_tag.py new file mode 100644 index 0000000..86f0513 --- /dev/null +++ b/xhiveframework/desk/doctype/tag/test_tag.py @@ -0,0 +1,34 @@ +import xhiveframework +from xhiveframework.desk.doctype.tag.tag import add_tag +from xhiveframework.desk.reportview import get_stats +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestTag(XhiveFrameworkTestCase): + def setUp(self) -> None: + xhiveframework.db.delete("Tag") + xhiveframework.db.sql("UPDATE `tabDocType` set _user_tags=''") + + def test_tag_count_query(self): + self.assertDictEqual( + get_stats('["_user_tags"]', "DocType"), + {"_user_tags": [["No Tags", xhiveframework.db.count("DocType")]]}, + ) + add_tag("Standard", "DocType", "User") + add_tag("Standard", "DocType", "ToDo") + + # count with no filter + self.assertDictEqual( + get_stats('["_user_tags"]', "DocType"), + {"_user_tags": [["Standard", 2], ["No Tags", xhiveframework.db.count("DocType") - 2]]}, + ) + + # count with child table field filter + self.assertDictEqual( + get_stats( + '["_user_tags"]', + "DocType", + filters='[["DocField", "fieldname", "like", "%last_name%"], ["DocType", "name", "like", "%use%"]]', + ), + {"_user_tags": [["Standard", 1], ["No Tags", 0]]}, + ) diff --git a/xhiveframework/desk/doctype/tag_link/__init__.py b/xhiveframework/desk/doctype/tag_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/tag_link/tag_link.js b/xhiveframework/desk/doctype/tag_link/tag_link.js new file mode 100644 index 0000000..1d99b43 --- /dev/null +++ b/xhiveframework/desk/doctype/tag_link/tag_link.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Tag Link", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/desk/doctype/tag_link/tag_link.json b/xhiveframework/desk/doctype/tag_link/tag_link.json new file mode 100644 index 0000000..9142279 --- /dev/null +++ b/xhiveframework/desk/doctype/tag_link/tag_link.json @@ -0,0 +1,83 @@ +{ + "actions": [], + "creation": "2019-09-24 13:25:36.435685", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "document_name", + "tag", + "title" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Document Title", + "read_only": 1 + }, + { + "fieldname": "tag", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Tag", + "options": "Tag", + "read_only": 1 + }, + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Type", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "document_name", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Name", + "options": "document_type", + "read_only": 1 + } + ], + "links": [], + "modified": "2021-09-20 16:53:37.217998", + "modified_by": "Administrator", + "module": "Desk", + "name": "Tag Link", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/tag_link/tag_link.py b/xhiveframework/desk/doctype/tag_link/tag_link.py new file mode 100644 index 0000000..1936f40 --- /dev/null +++ b/xhiveframework/desk/doctype/tag_link/tag_link.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class TagLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document_name: DF.DynamicLink | None + document_type: DF.Link | None + tag: DF.Link | None + title: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/tag_link/test_tag_link.py b/xhiveframework/desk/doctype/tag_link/test_tag_link.py new file mode 100644 index 0000000..8a2b49b --- /dev/null +++ b/xhiveframework/desk/doctype/tag_link/test_tag_link.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestTagLink(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/desk/doctype/todo/README.md b/xhiveframework/desk/doctype/todo/README.md new file mode 100644 index 0000000..b622358 --- /dev/null +++ b/xhiveframework/desk/doctype/todo/README.md @@ -0,0 +1 @@ +To do or assignment. \ No newline at end of file diff --git a/xhiveframework/desk/doctype/todo/__init__.py b/xhiveframework/desk/doctype/todo/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/desk/doctype/todo/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/desk/doctype/todo/test_todo.py b/xhiveframework/desk/doctype/todo/test_todo.py new file mode 100644 index 0000000..0041a00 --- /dev/null +++ b/xhiveframework/desk/doctype/todo/test_todo.py @@ -0,0 +1,153 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.core.doctype.doctype.doctype import clear_permissions_cache +from xhiveframework.model.db_query import DatabaseQuery +from xhiveframework.permissions import add_permission, reset_perms +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["User"] + + +class TestToDo(XhiveFrameworkTestCase): + def test_delete(self): + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test todo", assigned_by="Administrator") + ).insert() + + xhiveframework.db.delete("Deleted Document") + todo.delete() + + deleted = xhiveframework.get_doc( + "Deleted Document", dict(deleted_doctype=todo.doctype, deleted_name=todo.name) + ) + self.assertEqual(todo.as_json(), deleted.data) + + def test_fetch(self): + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test todo", assigned_by="Administrator") + ).insert() + self.assertEqual( + todo.assigned_by_full_name, xhiveframework.db.get_value("User", todo.assigned_by, "full_name") + ) + + def test_fetch_setup(self): + xhiveframework.db.delete("ToDo") + + todo_meta = xhiveframework.get_doc("DocType", "ToDo") + todo_meta.get("fields", dict(fieldname="assigned_by_full_name"))[0].fetch_from = "" + todo_meta.save() + + xhiveframework.clear_cache(doctype="ToDo") + + todo = xhiveframework.get_doc( + dict(doctype="ToDo", description="test todo", assigned_by="Administrator") + ).insert() + self.assertFalse(todo.assigned_by_full_name) + + todo_meta = xhiveframework.get_doc("DocType", "ToDo") + todo_meta.get("fields", dict(fieldname="assigned_by_full_name"))[ + 0 + ].fetch_from = "assigned_by.full_name" + todo_meta.save() + + todo.reload() + + self.assertEqual( + todo.assigned_by_full_name, xhiveframework.db.get_value("User", todo.assigned_by, "full_name") + ) + + def test_todo_list_access(self): + create_new_todo("Test1", "testperm@example.com") + + xhiveframework.set_user("test4@example.com") + create_new_todo("Test2", "test4@example.com") + test_user_data = DatabaseQuery("ToDo").execute() + + xhiveframework.set_user("testperm@example.com") + system_manager_data = DatabaseQuery("ToDo").execute() + + self.assertNotEqual(test_user_data, system_manager_data) + + xhiveframework.set_user("Administrator") + xhiveframework.db.rollback() + + def test_doc_read_access(self): + # owner and assigned_by is testperm + todo1 = create_new_todo("Test1", "testperm@example.com") + test_user = xhiveframework.get_doc("User", "test4@example.com") + + # owner is testperm, but assigned_by is test4 + todo2 = create_new_todo("Test2", "test4@example.com") + + xhiveframework.set_user("test4@example.com") + # owner and assigned_by is test4 + todo3 = create_new_todo("Test3", "test4@example.com") + + # user without any role to read or write todo document + self.assertFalse(todo1.has_permission("read")) + self.assertFalse(todo1.has_permission("write")) + + # user without any role but he/she is assigned_by of that todo document + self.assertTrue(todo2.has_permission("read")) + self.assertTrue(todo2.has_permission("write")) + + # user is the owner and assigned_by of the todo document + self.assertTrue(todo3.has_permission("read")) + self.assertTrue(todo3.has_permission("write")) + + xhiveframework.set_user("Administrator") + + test_user.add_roles("Blogger") + add_permission("ToDo", "Blogger") + + xhiveframework.set_user("test4@example.com") + + # user with only read access to todo document, not an owner or assigned_by + self.assertTrue(todo1.has_permission("read")) + self.assertFalse(todo1.has_permission("write")) + + xhiveframework.set_user("Administrator") + test_user.remove_roles("Blogger") + reset_perms("ToDo") + clear_permissions_cache("ToDo") + xhiveframework.db.rollback() + + def test_fetch_if_empty(self): + xhiveframework.db.delete("ToDo") + + # Allow user changes + todo_meta = xhiveframework.get_doc("DocType", "ToDo") + field = todo_meta.get("fields", dict(fieldname="assigned_by_full_name"))[0] + field.fetch_from = "assigned_by.full_name" + field.fetch_if_empty = 1 + todo_meta.save() + + xhiveframework.clear_cache(doctype="ToDo") + + todo = xhiveframework.get_doc( + dict( + doctype="ToDo", + description="test todo", + assigned_by="Administrator", + assigned_by_full_name="Admin", + ) + ).insert() + + self.assertEqual(todo.assigned_by_full_name, "Admin") + + # Overwrite user changes + todo.meta.get("fields", dict(fieldname="assigned_by_full_name"))[0].fetch_if_empty = 0 + todo.meta.save() + + todo.reload() + todo.save() + + self.assertEqual( + todo.assigned_by_full_name, xhiveframework.db.get_value("User", todo.assigned_by, "full_name") + ) + + +def create_new_todo(description, assigned_by): + todo = {"doctype": "ToDo", "description": description, "assigned_by": assigned_by} + return xhiveframework.get_doc(todo).insert() diff --git a/xhiveframework/desk/doctype/todo/todo.js b/xhiveframework/desk/doctype/todo/todo.js new file mode 100644 index 0000000..c77c7aa --- /dev/null +++ b/xhiveframework/desk/doctype/todo/todo.js @@ -0,0 +1,55 @@ +// bind events + +xhiveframework.ui.form.on("ToDo", { + onload: function (frm) { + frm.set_query("reference_type", function (txt) { + return { + filters: { + issingle: 0, + }, + }; + }); + }, + refresh: function (frm) { + if (frm.doc.reference_type && frm.doc.reference_name) { + frm.add_custom_button(__(frm.doc.reference_name), function () { + xhiveframework.set_route("Form", frm.doc.reference_type, frm.doc.reference_name); + }); + } + + if (!frm.doc.__islocal) { + if (frm.doc.status !== "Closed") { + frm.add_custom_button( + __("Close"), + function () { + frm.set_value("status", "Closed"); + frm.save(null, function () { + // back to list + xhiveframework.set_route("List", "ToDo"); + }); + }, + "fa fa-check", + "btn-success" + ); + } else { + frm.add_custom_button( + __("Reopen"), + function () { + frm.set_value("status", "Open"); + frm.save(); + }, + null, + "btn-default" + ); + } + frm.add_custom_button( + __("New"), + function () { + xhiveframework.new_doc("ToDo"); + }, + null, + "btn-default" + ); + } + }, +}); diff --git a/xhiveframework/desk/doctype/todo/todo.json b/xhiveframework/desk/doctype/todo/todo.json new file mode 100644 index 0000000..c3b534d --- /dev/null +++ b/xhiveframework/desk/doctype/todo/todo.json @@ -0,0 +1,202 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2012-07-03 13:30:35", + "doctype": "DocType", + "document_type": "Setup", + "email_append_to": 1, + "engine": "InnoDB", + "field_order": [ + "description_and_status", + "status", + "priority", + "column_break_2", + "color", + "date", + "allocated_to", + "description_section", + "description", + "section_break_6", + "reference_type", + "reference_name", + "column_break_10", + "role", + "assigned_by", + "assigned_by_full_name", + "sender", + "assignment_rule" + ], + "fields": [ + { + "fieldname": "description_and_status", + "fieldtype": "Section Break" + }, + { + "default": "Open", + "fieldname": "status", + "fieldtype": "Select", + "in_global_search": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Open\nClosed\nCancelled" + }, + { + "default": "Medium", + "fieldname": "priority", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Priority", + "oldfieldname": "priority", + "oldfieldtype": "Data", + "options": "High\nMedium\nLow" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "fieldname": "color", + "fieldtype": "Color", + "label": "Color" + }, + { + "allow_in_quick_entry": 1, + "default": "Today", + "fieldname": "date", + "fieldtype": "Date", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Due Date", + "oldfieldname": "date", + "oldfieldtype": "Date" + }, + { + "fieldname": "description_section", + "fieldtype": "Section Break" + }, + { + "fieldname": "description", + "fieldtype": "Text Editor", + "in_global_search": 1, + "label": "Description", + "oldfieldname": "description", + "oldfieldtype": "Text", + "print_width": "300px", + "reqd": 1, + "width": "300px" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break", + "label": "Reference" + }, + { + "fieldname": "reference_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference Type", + "oldfieldname": "reference_type", + "oldfieldtype": "Data", + "options": "DocType" + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "label": "Reference Name", + "oldfieldname": "reference_name", + "oldfieldtype": "Data", + "options": "reference_type" + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "fieldname": "role", + "fieldtype": "Link", + "label": "Role", + "oldfieldname": "role", + "oldfieldtype": "Link", + "options": "Role" + }, + { + "fieldname": "assigned_by", + "fieldtype": "Link", + "ignore_user_permissions": 1, + "label": "Assigned By", + "options": "User" + }, + { + "fetch_from": "assigned_by.full_name", + "fieldname": "assigned_by_full_name", + "fieldtype": "Read Only", + "label": "Assigned By Full Name" + }, + { + "fieldname": "sender", + "fieldtype": "Data", + "hidden": 1, + "label": "Sender", + "options": "Email" + }, + { + "fieldname": "assignment_rule", + "fieldtype": "Link", + "label": "Assignment Rule", + "options": "Assignment Rule", + "read_only": 1 + }, + { + "fieldname": "allocated_to", + "fieldtype": "Link", + "ignore_user_permissions": 1, + "in_global_search": 1, + "in_standard_filter": 1, + "label": "Allocated To", + "options": "User" + } + ], + "icon": "fa fa-check", + "idx": 2, + "links": [], + "modified": "2023-10-05 07:44:38.476400", + "modified_by": "Administrator", + "module": "Desk", + "name": "ToDo", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All", + "share": 1, + "write": 1 + }, + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "search_fields": "description, reference_type, reference_name", + "sender_field": "sender", + "sort_field": "modified", + "sort_order": "DESC", + "subject_field": "description", + "title_field": "description", + "track_changes": 1, + "track_seen": 1 +} diff --git a/xhiveframework/desk/doctype/todo/todo.py b/xhiveframework/desk/doctype/todo/todo.py new file mode 100644 index 0000000..ced6462 --- /dev/null +++ b/xhiveframework/desk/doctype/todo/todo.py @@ -0,0 +1,174 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.permissions import AUTOMATIC_ROLES +from xhiveframework.utils import get_fullname, parse_addr + +exclude_from_linked_with = True + + +class ToDo(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + allocated_to: DF.Link | None + assigned_by: DF.Link | None + assigned_by_full_name: DF.ReadOnly | None + assignment_rule: DF.Link | None + color: DF.Color | None + date: DF.Date | None + description: DF.TextEditor + priority: DF.Literal["High", "Medium", "Low"] + reference_name: DF.DynamicLink | None + reference_type: DF.Link | None + role: DF.Link | None + sender: DF.Data | None + status: DF.Literal["Open", "Closed", "Cancelled"] + # end: auto-generated types + DocType = "ToDo" + + def validate(self): + self._assignment = None + if self.is_new(): + if self.assigned_by == self.allocated_to: + assignment_message = xhiveframework._("{0} self assigned this task: {1}").format( + get_fullname(self.assigned_by), self.description + ) + else: + assignment_message = xhiveframework._("{0} assigned {1}: {2}").format( + get_fullname(self.assigned_by), get_fullname(self.allocated_to), self.description + ) + + self._assignment = {"text": assignment_message, "comment_type": "Assigned"} + + else: + # NOTE the previous value is only available in validate method + if self.get_db_value("status") != self.status: + if self.allocated_to == xhiveframework.session.user: + removal_message = xhiveframework._("{0} removed their assignment.").format( + get_fullname(xhiveframework.session.user) + ) + else: + removal_message = xhiveframework._("Assignment of {0} removed by {1}").format( + get_fullname(self.allocated_to), get_fullname(xhiveframework.session.user) + ) + + self._assignment = {"text": removal_message, "comment_type": "Assignment Completed"} + + def on_update(self): + if self._assignment: + self.add_assign_comment(**self._assignment) + + self.update_in_reference() + + def on_trash(self): + self.delete_communication_links() + self.update_in_reference() + + def add_assign_comment(self, text, comment_type): + if not (self.reference_type and self.reference_name): + return + + xhiveframework.get_doc(self.reference_type, self.reference_name).add_comment(comment_type, text) + + def delete_communication_links(self): + # unlink todo from linked comments + return xhiveframework.db.delete("Communication Link", {"link_doctype": self.doctype, "link_name": self.name}) + + def update_in_reference(self): + if not (self.reference_type and self.reference_name): + return + + try: + assignments = xhiveframework.get_all( + "ToDo", + filters={ + "reference_type": self.reference_type, + "reference_name": self.reference_name, + "status": ("not in", ("Cancelled", "Closed")), + "allocated_to": ("is", "set"), + }, + pluck="allocated_to", + ) + assignments.reverse() + + if xhiveframework.get_meta(self.reference_type).issingle: + xhiveframework.db.set_single_value( + self.reference_type, + "_assign", + json.dumps(assignments), + update_modified=False, + ) + else: + xhiveframework.db.set_value( + self.reference_type, + self.reference_name, + "_assign", + json.dumps(assignments), + update_modified=False, + ) + + except Exception as e: + if xhiveframework.db.is_table_missing(e) and xhiveframework.flags.in_install: + # no table + return + + elif xhiveframework.db.is_column_missing(e): + from xhiveframework.database.schema import add_column + + add_column(self.reference_type, "_assign", "Text") + self.update_in_reference() + + else: + raise + + @classmethod + def get_owners(cls, filters=None): + """Returns list of owners after applying filters on todo's.""" + rows = xhiveframework.get_all(cls.DocType, filters=filters or {}, fields=["allocated_to"]) + return [parse_addr(row.allocated_to)[1] for row in rows if row.allocated_to] + + +# NOTE: todo is viewable if a user is an owner, or set as assigned_to value, or has any role that is allowed to access ToDo doctype. +def on_doctype_update(): + xhiveframework.db.add_index("ToDo", ["reference_type", "reference_name"]) + + +def get_permission_query_conditions(user): + if not user: + user = xhiveframework.session.user + + todo_roles = xhiveframework.permissions.get_doctype_roles("ToDo") + todo_roles = set(todo_roles) - set(AUTOMATIC_ROLES) + + if any(check in todo_roles for check in xhiveframework.get_roles(user)): + return None + else: + return """(`tabToDo`.allocated_to = {user} or `tabToDo`.assigned_by = {user})""".format( + user=xhiveframework.db.escape(user) + ) + + +def has_permission(doc, ptype="read", user=None): + user = user or xhiveframework.session.user + todo_roles = xhiveframework.permissions.get_doctype_roles("ToDo", ptype) + todo_roles = set(todo_roles) - set(AUTOMATIC_ROLES) + + if any(check in todo_roles for check in xhiveframework.get_roles(user)): + return True + else: + return doc.allocated_to == user or doc.assigned_by == user + + +@xhiveframework.whitelist() +def new_todo(description): + xhiveframework.get_doc({"doctype": "ToDo", "description": description}).insert() diff --git a/xhiveframework/desk/doctype/todo/todo_calendar.js b/xhiveframework/desk/doctype/todo/todo_calendar.js new file mode 100644 index 0000000..4248b16 --- /dev/null +++ b/xhiveframework/desk/doctype/todo/todo_calendar.js @@ -0,0 +1,29 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// License: GNU General Public License v3. See license.txt + +xhiveframework.views.calendar["ToDo"] = { + field_map: { + start: "date", + end: "date", + id: "name", + title: "description", + allDay: "allDay", + progress: "progress", + }, + gantt: true, + filters: [ + { + fieldtype: "Link", + fieldname: "reference_type", + options: "Task", + label: __("Task"), + }, + { + fieldtype: "Dynamic Link", + fieldname: "reference_name", + options: "reference_type", + label: __("Task"), + }, + ], + get_events_method: "xhiveframework.desk.calendar.get_events", +}; diff --git a/xhiveframework/desk/doctype/todo/todo_list.js b/xhiveframework/desk/doctype/todo/todo_list.js new file mode 100644 index 0000000..8b1387a --- /dev/null +++ b/xhiveframework/desk/doctype/todo/todo_list.js @@ -0,0 +1,29 @@ +xhiveframework.listview_settings["ToDo"] = { + hide_name_column: true, + add_fields: ["reference_type", "reference_name"], + + onload: function (me) { + if (!Object.keys(xhiveframework.route_options).length) { + xhiveframework.route_options = { + allocated_to: xhiveframework.session.user, + status: "Open", + }; + } + me.page.set_title(__("To Do")); + }, + + button: { + show: function (doc) { + return doc.reference_name; + }, + get_label: function () { + return __("Open", null, "Access"); + }, + get_description: function (doc) { + return __("Open {0}", [`${__(doc.reference_type)}: ${doc.reference_name}`]); + }, + action: function (doc) { + xhiveframework.set_route("Form", doc.reference_type, doc.reference_name); + }, + }, +}; diff --git a/xhiveframework/desk/doctype/workspace/__init__.py b/xhiveframework/desk/doctype/workspace/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace/test_workspace.py b/xhiveframework/desk/doctype/workspace/test_workspace.py new file mode 100644 index 0000000..fab7270 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace/test_workspace.py @@ -0,0 +1,98 @@ +# Copyright (c) 2020, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestWorkspace(XhiveFrameworkTestCase): + def setUp(self): + create_module("Test Module") + + def tearDown(self): + xhiveframework.db.delete("Workspace", {"module": "Test Module"}) + xhiveframework.db.delete("DocType", {"module": "Test Module"}) + xhiveframework.delete_doc("Module Def", "Test Module") + + # TODO: FIX ME - flaky test!!! + # def test_workspace_with_cards_specific_to_a_country(self): + # workspace = create_workspace() + # insert_card(workspace, "Card Label 1", "DocType 1", "DocType 2", "France") + # insert_card(workspace, "Card Label 2", "DocType A", "DocType B") + + # workspace.insert(ignore_if_duplicate = True) + + # cards = workspace.get_link_groups() + + # if xhiveframework.get_system_settings('country') == "France": + # self.assertEqual(len(cards), 2) + # else: + # self.assertEqual(len(cards), 1) + + +def create_module(module_name): + module = xhiveframework.get_doc({"doctype": "Module Def", "module_name": module_name, "app_name": "xhiveframework"}) + module.insert(ignore_if_duplicate=True) + + return module + + +def create_workspace(**args): + workspace = xhiveframework.new_doc("Workspace") + args = xhiveframework._dict(args) + + workspace.name = args.name or "Test Workspace" + workspace.label = args.label or "Test Workspace" + workspace.category = args.category or "Modules" + workspace.is_standard = args.is_standard or 1 + workspace.module = "Test Module" + + return workspace + + +def insert_card(workspace, card_label, doctype1, doctype2, country=None): + workspace.append("links", {"type": "Card Break", "label": card_label, "only_for": country}) + + create_doctype(doctype1, "Test Module") + workspace.append( + "links", + { + "type": "Link", + "label": doctype1, + "only_for": country, + "link_type": "DocType", + "link_to": doctype1, + }, + ) + + create_doctype(doctype2, "Test Module") + workspace.append( + "links", + { + "type": "Link", + "label": doctype2, + "only_for": country, + "link_type": "DocType", + "link_to": doctype2, + }, + ) + + +def create_doctype(doctype_name, module): + xhiveframework.get_doc( + { + "doctype": "DocType", + "name": doctype_name, + "module": module, + "custom": 1, + "autoname": "field:title", + "fields": [ + {"label": "Title", "fieldname": "title", "reqd": 1, "fieldtype": "Data"}, + {"label": "Description", "fieldname": "description", "fieldtype": "Small Text"}, + {"label": "Date", "fieldname": "date", "fieldtype": "Date"}, + {"label": "Duration", "fieldname": "duration", "fieldtype": "Duration"}, + {"label": "Number", "fieldname": "number", "fieldtype": "Int"}, + {"label": "Number", "fieldname": "another_number", "fieldtype": "Int"}, + ], + "permissions": [{"role": "System Manager"}], + } + ).insert(ignore_if_duplicate=True) diff --git a/xhiveframework/desk/doctype/workspace/workspace.js b/xhiveframework/desk/doctype/workspace/workspace.js new file mode 100644 index 0000000..24818f2 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace/workspace.js @@ -0,0 +1,59 @@ +// Copyright (c) 2020, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Workspace", { + setup: function () { + xhiveframework.meta.get_field("Workspace Link", "only_for").no_default = true; + }, + + refresh: function (frm) { + frm.enable_save(); + + let url = `/app/${ + frm.doc.public + ? xhiveframework.router.slug(frm.doc.title) + : "private/" + xhiveframework.router.slug(frm.doc.title) + }`; + frm.sidebar + .add_user_action(__("Go to Workspace")) + .attr("href", url) + .attr("target", "_blank"); + + frm.layout.message.empty(); + let message = __( + "This document allows you to edit limited fields. For all kinds of workspace customization, use the Edit button located on the workspace page" + ); + + if ( + frm.doc.for_user || + (frm.doc.public && + !frm.has_perm("write") && + !xhiveframework.user.has_role("Workspace Manager")) + ) { + frm.trigger("disable_form"); + + if (frm.doc.public) { + message = __("Only Workspace Manager can edit public workspaces"); + } else { + message = __( + "We do not allow editing of this document. Simply click the Edit button on the workspace page to make your workspace editable and customize it as you wish" + ); + } + } + + if (xhiveframework.boot.developer_mode) { + frm.set_df_property("module", "read_only", 0); + } + + frm.layout.show_message(message); + }, + + disable_form: function (frm) { + frm.fields + .filter((field) => field.has_input) + .forEach((field) => { + frm.set_df_property(field.df.fieldname, "read_only", "1"); + }); + frm.disable_save(); + }, +}); diff --git a/xhiveframework/desk/doctype/workspace/workspace.json b/xhiveframework/desk/doctype/workspace/workspace.json new file mode 100644 index 0000000..e4f58b5 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace/workspace.json @@ -0,0 +1,246 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:label", + "beta": 1, + "creation": "2020-01-23 13:45:59.470592", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "label", + "title", + "sequence_id", + "for_user", + "parent_page", + "module", + "column_break_3", + "icon", + "indicator_color", + "restrict_to_domain", + "hide_custom", + "public", + "is_hidden", + "content", + "number_cards_tab", + "number_cards", + "tab_break_2", + "charts", + "tab_break_15", + "shortcuts", + "tab_break_18", + "links", + "quick_lists_tab", + "quick_lists", + "custom_blocks_tab", + "custom_blocks", + "roles_tab", + "roles" + ], + "fields": [ + { + "fieldname": "label", + "fieldtype": "Data", + "label": "Name", + "reqd": 1, + "unique": 1 + }, + { + "collapsible": 1, + "collapsible_depends_on": "charts", + "fieldname": "tab_break_2", + "fieldtype": "Tab Break", + "label": "Dashboards" + }, + { + "fieldname": "charts", + "fieldtype": "Table", + "label": "Charts", + "options": "Workspace Chart" + }, + { + "fieldname": "shortcuts", + "fieldtype": "Table", + "label": "Shortcuts", + "options": "Workspace Shortcut" + }, + { + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict to Domain", + "options": "Domain", + "search_index": 1 + }, + { + "fieldname": "module", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Module", + "options": "Module Def", + "read_only": 1 + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "collapsible": 1, + "collapsible_depends_on": "shortcuts", + "fieldname": "tab_break_15", + "fieldtype": "Tab Break", + "label": "Shortcuts" + }, + { + "collapsible": 1, + "collapsible_depends_on": "links", + "fieldname": "tab_break_18", + "fieldtype": "Tab Break", + "label": "Link Cards" + }, + { + "fieldname": "for_user", + "fieldtype": "Data", + "label": "For User", + "read_only": 1 + }, + { + "default": "0", + "description": "Checking this will hide custom doctypes and reports cards in Links section", + "fieldname": "hide_custom", + "fieldtype": "Check", + "label": "Hide Custom DocTypes and Reports" + }, + { + "fieldname": "icon", + "fieldtype": "Icon", + "label": "Icon", + "read_only": 1 + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "Workspace Link" + }, + { + "default": "0", + "fieldname": "public", + "fieldtype": "Check", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Public", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Title", + "read_only": 1, + "reqd": 1 + }, + { + "fieldname": "parent_page", + "fieldtype": "Data", + "label": "Parent Page", + "read_only": 1 + }, + { + "default": "[]", + "fieldname": "content", + "fieldtype": "Long Text", + "hidden": 1, + "label": "Content" + }, + { + "fieldname": "sequence_id", + "fieldtype": "Float", + "label": "Sequence Id", + "read_only": 1 + }, + { + "fieldname": "roles", + "fieldtype": "Table", + "label": "Roles", + "options": "Has Role" + }, + { + "fieldname": "roles_tab", + "fieldtype": "Tab Break", + "label": "Roles" + }, + { + "fieldname": "quick_lists_tab", + "fieldtype": "Tab Break", + "label": "Quick Lists" + }, + { + "fieldname": "quick_lists", + "fieldtype": "Table", + "label": "Quick Lists", + "options": "Workspace Quick List" + }, + { + "default": "0", + "fieldname": "is_hidden", + "fieldtype": "Check", + "label": "Is Hidden" + }, + { + "fieldname": "number_cards_tab", + "fieldtype": "Tab Break", + "label": "Number Cards" + }, + { + "fieldname": "number_cards", + "fieldtype": "Table", + "label": "Number Cards", + "options": "Workspace Number Card" + }, + { + "fieldname": "custom_blocks_tab", + "fieldtype": "Tab Break", + "label": "Custom Blocks" + }, + { + "fieldname": "custom_blocks", + "fieldtype": "Table", + "label": "Custom Blocks", + "options": "Workspace Custom Block" + }, + { + "depends_on": "doc.icon", + "fieldname": "indicator_color", + "fieldtype": "Select", + "label": "Indicator Color", + "options": "green\ncyan\nblue\norange\nyellow\ngray\ngrey\nred\npink\ndarkgrey\npurple\nlight-blue" + } + ], + "in_create": 1, + "links": [], + "modified": "2023-08-25 15:04:03.419848", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Workspace Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace/workspace.py b/xhiveframework/desk/doctype/workspace/workspace.py new file mode 100644 index 0000000..6bc95d9 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace/workspace.py @@ -0,0 +1,475 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from collections import defaultdict +from json import loads + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.desktop import save_new_widget +from xhiveframework.desk.utils import validate_route_conflict +from xhiveframework.model.document import Document +from xhiveframework.model.rename_doc import rename_doc +from xhiveframework.modules.export_file import delete_folder, export_to_files +from xhiveframework.utils import strip_html + + +class Workspace(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.core.doctype.has_role.has_role import HasRole + from xhiveframework.desk.doctype.workspace_chart.workspace_chart import WorkspaceChart + from xhiveframework.desk.doctype.workspace_custom_block.workspace_custom_block import ( + WorkspaceCustomBlock, + ) + from xhiveframework.desk.doctype.workspace_link.workspace_link import WorkspaceLink + from xhiveframework.desk.doctype.workspace_number_card.workspace_number_card import WorkspaceNumberCard + from xhiveframework.desk.doctype.workspace_quick_list.workspace_quick_list import WorkspaceQuickList + from xhiveframework.desk.doctype.workspace_shortcut.workspace_shortcut import WorkspaceShortcut + from xhiveframework.types import DF + + charts: DF.Table[WorkspaceChart] + content: DF.LongText | None + custom_blocks: DF.Table[WorkspaceCustomBlock] + for_user: DF.Data | None + hide_custom: DF.Check + indicator_color: DF.Literal[ + "green", + "cyan", + "blue", + "orange", + "yellow", + "gray", + "grey", + "red", + "pink", + "darkgrey", + "purple", + "light-blue", + ] + is_hidden: DF.Check + label: DF.Data + links: DF.Table[WorkspaceLink] + module: DF.Link | None + number_cards: DF.Table[WorkspaceNumberCard] + parent_page: DF.Data | None + public: DF.Check + quick_lists: DF.Table[WorkspaceQuickList] + restrict_to_domain: DF.Link | None + roles: DF.Table[HasRole] + sequence_id: DF.Float + shortcuts: DF.Table[WorkspaceShortcut] + title: DF.Data + + # end: auto-generated types + def validate(self): + self.title = strip_html(self.title) + + if self.public and not is_workspace_manager() and not disable_saving_as_public(): + xhiveframework.throw(_("You need to be Workspace Manager to edit this document")) + if self.has_value_changed("title"): + validate_route_conflict(self.doctype, self.title) + else: + validate_route_conflict(self.doctype, self.name) + + try: + if not isinstance(loads(self.content), list): + raise + except Exception: + xhiveframework.throw(_("Content data shoud be a list")) + + def clear_cache(self): + super().clear_cache() + if self.for_user: + xhiveframework.cache.hdel("bootinfo", self.for_user) + else: + xhiveframework.cache.delete_key("bootinfo") + + def on_update(self): + if disable_saving_as_public(): + return + + if xhiveframework.conf.developer_mode and self.public: + if self.module: + export_to_files(record_list=[["Workspace", self.name]], record_module=self.module) + + if self.has_value_changed("title") or self.has_value_changed("module"): + previous = self.get_doc_before_save() + if previous and previous.get("module") and previous.get("title"): + delete_folder(previous.get("module"), "Workspace", previous.get("title")) + + def before_export(self, doc): + if doc.title != doc.label and doc.label == doc.name: + self.name = doc.name = doc.label = doc.title + + def after_delete(self): + if disable_saving_as_public(): + return + + if self.module and xhiveframework.conf.developer_mode: + delete_folder(self.module, "Workspace", self.title) + + @staticmethod + def get_module_wise_workspaces(): + workspaces = xhiveframework.get_all( + "Workspace", + fields=["name", "module"], + filters={"for_user": "", "public": 1}, + order_by="creation", + ) + + module_workspaces = defaultdict(list) + + for workspace in workspaces: + if not workspace.module: + continue + module_workspaces[workspace.module].append(workspace.name) + + return module_workspaces + + def get_link_groups(self): + cards = [] + current_card = xhiveframework._dict( + { + "label": "Link", + "type": "Card Break", + "icon": None, + "hidden": False, + } + ) + + card_links = [] + + for link in self.links: + link = link.as_dict() + if link.type == "Card Break": + if card_links and ( + not current_card.get("only_for") + or current_card.get("only_for") == xhiveframework.get_system_settings("country") + ): + current_card["links"] = card_links + cards.append(current_card) + + current_card = link + card_links = [] + elif not link.get("only_for") or link.get("only_for") == xhiveframework.get_system_settings("country"): + card_links.append(link) + + current_card["links"] = card_links + cards.append(current_card) + + return cards + + def build_links_table_from_card(self, config): + for idx, card in enumerate(config): + links = loads(card.get("links")) + + # remove duplicate before adding + for idx, link in enumerate(self.links): + if link.get("label") == card.get("label") and link.get("type") == "Card Break": + # count and set number of links for the card if link_count is 0 + if link.link_count == 0: + for count, card_link in enumerate(self.links[idx + 1 :]): + if card_link.get("type") == "Card Break": + break + link.link_count = count + 1 + + del self.links[idx : idx + link.link_count + 1] + + self.append( + "links", + { + "label": card.get("label"), + "type": "Card Break", + "icon": card.get("icon"), + "description": card.get("description"), + "hidden": card.get("hidden") or False, + "link_count": card.get("link_count"), + "idx": 1 if not self.links else self.links[-1].idx + 1, + }, + ) + + for link in links: + self.append( + "links", + { + "label": link.get("label"), + "type": "Link", + "link_type": link.get("link_type"), + "link_to": link.get("link_to"), + "onboard": link.get("onboard"), + "only_for": link.get("only_for"), + "dependencies": link.get("dependencies"), + "is_query_report": link.get("is_query_report"), + "idx": self.links[-1].idx + 1, + }, + ) + + +def disable_saving_as_public(): + return ( + xhiveframework.flags.in_install + or xhiveframework.flags.in_uninstall + or xhiveframework.flags.in_patch + or xhiveframework.flags.in_test + or xhiveframework.flags.in_fixtures + or xhiveframework.flags.in_migrate + ) + + +def get_link_type(key): + key = key.lower() + + link_type_map = {"doctype": "DocType", "page": "Page", "report": "Report"} + + if key in link_type_map: + return link_type_map[key] + + return "DocType" + + +def get_report_type(report): + report_type = xhiveframework.get_value("Report", report, "report_type") + return report_type in ["Query Report", "Script Report", "Custom Report"] + + +@xhiveframework.whitelist() +def new_page(new_page): + if not loads(new_page): + return + + page = loads(new_page) + + if page.get("public") and not is_workspace_manager(): + return + elif ( + not page.get("public") and page.get("for_user") != xhiveframework.session.user and not is_workspace_manager() + ): + xhiveframework.throw(_("Cannot create private workspace of other users"), xhiveframework.PermissionError) + + doc = xhiveframework.new_doc("Workspace") + doc.title = page.get("title") + doc.icon = page.get("icon") + doc.indicator_color = page.get("indicator_color") + doc.content = page.get("content") + doc.parent_page = page.get("parent_page") + doc.label = page.get("label") + doc.for_user = page.get("for_user") + doc.public = page.get("public") + doc.sequence_id = last_sequence_id(doc) + 1 + doc.save(ignore_permissions=True) + + return doc + + +@xhiveframework.whitelist() +def save_page(title, public, new_widgets, blocks): + public = xhiveframework.parse_json(public) + + filters = {"public": public, "label": title} + + if not public: + filters = {"for_user": xhiveframework.session.user, "label": title + "-" + xhiveframework.session.user} + pages = xhiveframework.get_all("Workspace", filters=filters) + if pages: + doc = xhiveframework.get_doc("Workspace", pages[0]) + else: + xhiveframework.throw(_("Workspace not found"), xhiveframework.DoesNotExistError) + + doc.content = blocks + doc.save(ignore_permissions=True) + + save_new_widget(doc, title, blocks, new_widgets) + + return {"name": title, "public": public, "label": doc.label} + + +@xhiveframework.whitelist() +def update_page(name, title, icon, indicator_color, parent, public): + public = xhiveframework.parse_json(public) + doc = xhiveframework.get_doc("Workspace", name) + + if not doc.get("public") and doc.get("for_user") != xhiveframework.session.user and not is_workspace_manager(): + xhiveframework.throw( + _("Need Workspace Manager role to edit private workspace of other users"), + xhiveframework.PermissionError, + ) + + if doc: + child_docs = xhiveframework.get_all("Workspace", filters={"parent_page": doc.title, "public": doc.public}) + doc.title = title + doc.icon = icon + doc.indicator_color = indicator_color + doc.parent_page = parent + if doc.public != public: + doc.sequence_id = xhiveframework.db.count("Workspace", {"public": public}, cache=True) + doc.public = public + doc.for_user = "" if public else doc.for_user or xhiveframework.session.user + doc.label = new_name = f"{title}-{doc.for_user}" if doc.for_user else title + doc.save(ignore_permissions=True) + + if name != new_name: + rename_doc("Workspace", name, new_name, force=True, ignore_permissions=True) + + # update new name and public in child pages + if child_docs: + for child in child_docs: + child_doc = xhiveframework.get_doc("Workspace", child.name) + child_doc.parent_page = doc.title + if child_doc.public != public: + child_doc.public = public + child_doc.for_user = "" if public else child_doc.for_user or xhiveframework.session.user + child_doc.label = new_child_name = ( + f"{child_doc.title}-{child_doc.for_user}" if child_doc.for_user else child_doc.title + ) + child_doc.save(ignore_permissions=True) + + if child.name != new_child_name: + rename_doc("Workspace", child.name, new_child_name, force=True, ignore_permissions=True) + + return {"name": title, "public": public, "label": new_name} + + +def hide_unhide_page(page_name: str, is_hidden: bool): + page = xhiveframework.get_doc("Workspace", page_name) + + if page.get("public") and not is_workspace_manager(): + xhiveframework.throw( + _("Need Workspace Manager role to hide/unhide public workspaces"), xhiveframework.PermissionError + ) + + if not page.get("public") and page.get("for_user") != xhiveframework.session.user and not is_workspace_manager(): + xhiveframework.throw(_("Cannot update private workspace of other users"), xhiveframework.PermissionError) + + page.is_hidden = int(is_hidden) + page.save(ignore_permissions=True) + return True + + +@xhiveframework.whitelist() +def hide_page(page_name: str): + return hide_unhide_page(page_name, 1) + + +@xhiveframework.whitelist() +def unhide_page(page_name: str): + return hide_unhide_page(page_name, 0) + + +@xhiveframework.whitelist() +def duplicate_page(page_name, new_page): + if not loads(new_page): + return + + new_page = loads(new_page) + + if new_page.get("is_public") and not is_workspace_manager(): + return + + old_doc = xhiveframework.get_doc("Workspace", page_name) + doc = xhiveframework.copy_doc(old_doc) + doc.title = new_page.get("title") + doc.icon = new_page.get("icon") + doc.indicator_color = new_page.get("indicator_color") + doc.parent_page = new_page.get("parent") or "" + doc.public = new_page.get("is_public") + doc.for_user = "" + doc.label = doc.title + doc.module = "" + if not doc.public: + doc.for_user = doc.for_user or xhiveframework.session.user + doc.label = f"{doc.title}-{doc.for_user}" + doc.name = doc.label + if old_doc.public == doc.public: + doc.sequence_id += 0.1 + else: + doc.sequence_id = last_sequence_id(doc) + 1 + doc.insert(ignore_permissions=True) + + return doc + + +@xhiveframework.whitelist() +def delete_page(page): + if not loads(page): + return + + page = loads(page) + + if page.get("public") and not is_workspace_manager(): + xhiveframework.throw( + _("Cannot delete public workspace without Workspace Manager role"), + xhiveframework.PermissionError, + ) + elif not page.get("public") and not is_workspace_manager(): + workspace_owner = xhiveframework.get_value("Workspace", page.get("name"), "for_user") + if workspace_owner != xhiveframework.session.user: + xhiveframework.throw( + _("Cannot delete private workspace of other users"), + xhiveframework.PermissionError, + ) + + if xhiveframework.db.exists("Workspace", page.get("name")): + xhiveframework.get_doc("Workspace", page.get("name")).delete(ignore_permissions=True) + + return {"name": page.get("name"), "public": page.get("public"), "title": page.get("title")} + + +@xhiveframework.whitelist() +def sort_pages(sb_public_items, sb_private_items): + if not loads(sb_public_items) and not loads(sb_private_items): + return + + sb_public_items = loads(sb_public_items) + sb_private_items = loads(sb_private_items) + + workspace_public_pages = get_page_list(["name", "title"], {"public": 1}) + workspace_private_pages = get_page_list(["name", "title"], {"for_user": xhiveframework.session.user}) + + if sb_private_items: + return sort_page(workspace_private_pages, sb_private_items) + + if sb_public_items and is_workspace_manager(): + return sort_page(workspace_public_pages, sb_public_items) + + return False + + +def sort_page(workspace_pages, pages): + for seq, d in enumerate(pages): + for page in workspace_pages: + if page.title == d.get("title"): + doc = xhiveframework.get_doc("Workspace", page.name) + doc.sequence_id = seq + 1 + doc.parent_page = d.get("parent_page") or "" + doc.flags.ignore_links = True + doc.save(ignore_permissions=True) + break + + return True + + +def last_sequence_id(doc): + doc_exists = xhiveframework.db.exists({"doctype": "Workspace", "public": doc.public, "for_user": doc.for_user}) + + if not doc_exists: + return 0 + + return xhiveframework.get_all( + "Workspace", + fields=["sequence_id"], + filters={"public": doc.public, "for_user": doc.for_user}, + order_by="sequence_id desc", + )[0].sequence_id + + +def get_page_list(fields, filters): + return xhiveframework.get_all("Workspace", fields=fields, filters=filters, order_by="sequence_id asc") + + +def is_workspace_manager(): + return "Workspace Manager" in xhiveframework.get_roles() diff --git a/xhiveframework/desk/doctype/workspace_chart/__init__.py b/xhiveframework/desk/doctype/workspace_chart/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_chart/workspace_chart.json b/xhiveframework/desk/doctype/workspace_chart/workspace_chart.json new file mode 100644 index 0000000..0d80049 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_chart/workspace_chart.json @@ -0,0 +1,39 @@ +{ + "actions": [], + "creation": "2020-01-23 13:44:03.882158", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "chart_name", + "label" + ], + "fields": [ + { + "fieldname": "chart_name", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Chart Name", + "options": "Dashboard Chart", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label" + } + ], + "istable": 1, + "links": [], + "modified": "2021-01-12 13:13:25.781925", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Chart", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace_chart/workspace_chart.py b/xhiveframework/desk/doctype/workspace_chart/workspace_chart.py new file mode 100644 index 0000000..7a01999 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_chart/workspace_chart.py @@ -0,0 +1,23 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceChart(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + chart_name: DF.Link + label: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/workspace_custom_block/__init__.py b/xhiveframework/desk/doctype/workspace_custom_block/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.json b/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.json new file mode 100644 index 0000000..090719d --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.json @@ -0,0 +1,39 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2023-05-17 14:49:19.454932", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "custom_block_name", + "label" + ], + "fields": [ + { + "fieldname": "custom_block_name", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Custom Block Name", + "options": "Custom HTML Block" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-05-17 14:50:45.575609", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Custom Block", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.py b/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.py new file mode 100644 index 0000000..91e1f9e --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_custom_block/workspace_custom_block.py @@ -0,0 +1,23 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceCustomBlock(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + custom_block_name: DF.Link | None + label: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/workspace_link/__init__.py b/xhiveframework/desk/doctype/workspace_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_link/workspace_link.json b/xhiveframework/desk/doctype/workspace_link/workspace_link.json new file mode 100644 index 0000000..5f0a082 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_link/workspace_link.json @@ -0,0 +1,135 @@ +{ + "actions": [], + "creation": "2020-11-16 15:30:45.784417", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "type", + "label", + "icon", + "description", + "hidden", + "link_details_section", + "link_type", + "link_to", + "column_break_7", + "dependencies", + "only_for", + "onboard", + "is_query_report", + "link_count" + ], + "fields": [ + { + "default": "Link", + "fieldname": "type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Type", + "options": "Link\nCard Break", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "depends_on": "eval:doc.type == \"Card Break\"", + "fieldname": "icon", + "fieldtype": "Data", + "label": "Icon" + }, + { + "default": "0", + "depends_on": "eval:doc.type == \"Card Break\"", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "depends_on": "eval:doc.type == \"Link\"", + "fieldname": "link_details_section", + "fieldtype": "Section Break", + "label": "Link Details" + }, + { + "fieldname": "link_type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Link Type", + "mandatory_depends_on": "eval:doc.type==\"Link\"", + "options": "DocType\nPage\nReport", + "read_only_depends_on": "eval:doc.type!=\"Link\"" + }, + { + "fieldname": "link_to", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Link To", + "mandatory_depends_on": "eval:doc.type==\"Link\"", + "options": "link_type", + "read_only_depends_on": "eval:doc.type!=\"Link\"" + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "fieldname": "dependencies", + "fieldtype": "Data", + "label": "Dependencies" + }, + { + "fieldname": "only_for", + "fieldtype": "Link", + "label": "Only for", + "options": "Country" + }, + { + "default": "0", + "fieldname": "onboard", + "fieldtype": "Check", + "label": "Onboard" + }, + { + "default": "0", + "depends_on": "eval:doc.link_type == \"Report\"", + "fieldname": "is_query_report", + "fieldtype": "Check", + "label": "Is Query Report" + }, + { + "depends_on": "eval:doc.type == \"Card Break\"", + "fieldname": "link_count", + "fieldtype": "Int", + "hidden": 1, + "label": "Link Count" + }, + { + "depends_on": "eval:doc.type == \"Card Break\"", + "fieldname": "description", + "fieldtype": "HTML Editor", + "ignore_xss_filter": 1, + "label": "Description", + "max_height": "7rem" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2024-01-23 17:39:16.833318", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Link", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace_link/workspace_link.py b/xhiveframework/desk/doctype/workspace_link/workspace_link.py new file mode 100644 index 0000000..c4880bc --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_link/workspace_link.py @@ -0,0 +1,34 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceLink(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + dependencies: DF.Data | None + description: DF.HTMLEditor | None + hidden: DF.Check + icon: DF.Data | None + is_query_report: DF.Check + label: DF.Data + link_count: DF.Int + link_to: DF.DynamicLink | None + link_type: DF.Literal["DocType", "Page", "Report"] + onboard: DF.Check + only_for: DF.Link | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + type: DF.Literal["Link", "Card Break"] + # end: auto-generated types + + pass diff --git a/xhiveframework/desk/doctype/workspace_number_card/__init__.py b/xhiveframework/desk/doctype/workspace_number_card/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.json b/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.json new file mode 100644 index 0000000..f9e3865 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.json @@ -0,0 +1,40 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2023-02-15 01:16:26.216201", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "number_card_name", + "label" + ], + "fields": [ + { + "fieldname": "number_card_name", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Number Card Name", + "options": "Number Card", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-02-15 01:16:26.216201", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Number Card", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.py b/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.py new file mode 100644 index 0000000..0bfccf5 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_number_card/workspace_number_card.py @@ -0,0 +1,23 @@ +# Copyright (c) 2023, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceNumberCard(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + label: DF.Data | None + number_card_name: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/workspace_quick_list/__init__.py b/xhiveframework/desk/doctype/workspace_quick_list/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.json b/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.json new file mode 100644 index 0000000..1542ebe --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.json @@ -0,0 +1,60 @@ +{ + "actions": [], + "creation": "2022-05-12 12:58:41.824496", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "column_break_1", + "label", + "section_break_4", + "quick_list_filter" + ], + "fields": [ + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "column_break_1", + "fieldtype": "Column Break" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break" + }, + { + "fieldname": "quick_list_filter", + "fieldtype": "Code", + "in_list_view": 1, + "label": "Quick List Filter", + "options": "JSON" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2022-05-12 13:48:40.617623", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Quick List", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.py b/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.py new file mode 100644 index 0000000..5ac6f1e --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_quick_list/workspace_quick_list.py @@ -0,0 +1,24 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceQuickList(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document_type: DF.Link + label: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + quick_list_filter: DF.Code | None + # end: auto-generated types + pass diff --git a/xhiveframework/desk/doctype/workspace_shortcut/__init__.py b/xhiveframework/desk/doctype/workspace_shortcut/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.json b/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.json new file mode 100644 index 0000000..9adbbb1 --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.json @@ -0,0 +1,129 @@ +{ + "actions": [], + "creation": "2020-01-23 13:44:59.248426", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "type", + "link_to", + "url", + "doc_view", + "kanban_board", + "column_break_4", + "label", + "icon", + "restrict_to_domain", + "section_break_5", + "stats_filter", + "column_break_3", + "color", + "format" + ], + "fields": [ + { + "fieldname": "type", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Type", + "options": "DocType\nReport\nPage\nDashboard\nURL", + "reqd": 1 + }, + { + "depends_on": "eval:doc.type != \"URL\"", + "fieldname": "link_to", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Link To", + "options": "type" + }, + { + "depends_on": "eval:doc.type == \"DocType\"", + "description": "Which view of the associated DocType should this shortcut take you to?", + "fieldname": "doc_view", + "fieldtype": "Select", + "in_list_view": 1, + "label": "DocType View", + "options": "\nList\nReport Builder\nDashboard\nTree\nNew\nCalendar\nKanban" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + }, + { + "depends_on": "eval:xhiveframework.boot.developer_mode", + "fieldname": "icon", + "fieldtype": "Data", + "label": "Icon" + }, + { + "depends_on": "eval:xhiveframework.boot.developer_mode", + "fieldname": "restrict_to_domain", + "fieldtype": "Link", + "label": "Restrict to Domain", + "options": "Domain" + }, + { + "depends_on": "eval:doc.type == \"DocType\" && xhiveframework.boot.developer_mode", + "fieldname": "section_break_5", + "fieldtype": "Section Break", + "label": "Count Filter" + }, + { + "fieldname": "stats_filter", + "fieldtype": "Code", + "label": "Count Filter", + "options": "JSON" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "color", + "fieldtype": "Color", + "label": "Color" + }, + { + "description": "For example: {} Open", + "fieldname": "format", + "fieldtype": "Data", + "label": "Format" + }, + { + "depends_on": "eval:doc.type == \"URL\"", + "fieldname": "url", + "fieldtype": "Data", + "in_list_view": 1, + "label": "URL" + }, + { + "depends_on": "eval:doc.doc_view == \"Kanban\"", + "fieldname": "kanban_board", + "fieldtype": "Link", + "label": "Kanban Board", + "options": "Kanban Board" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-11-27 14:13:38.489737", + "modified_by": "Administrator", + "module": "Desk", + "name": "Workspace Shortcut", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.py b/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.py new file mode 100644 index 0000000..147363c --- /dev/null +++ b/xhiveframework/desk/doctype/workspace_shortcut/workspace_shortcut.py @@ -0,0 +1,32 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WorkspaceShortcut(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + color: DF.Color | None + doc_view: DF.Literal["", "List", "Report Builder", "Dashboard", "Tree", "New", "Calendar", "Kanban"] + format: DF.Data | None + icon: DF.Data | None + kanban_board: DF.Link | None + label: DF.Data + link_to: DF.DynamicLink | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + restrict_to_domain: DF.Link | None + stats_filter: DF.Code | None + type: DF.Literal["DocType", "Report", "Page", "Dashboard", "URL"] + url: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/desk/form/__init__.py b/xhiveframework/desk/form/__init__.py new file mode 100644 index 0000000..e317598 --- /dev/null +++ b/xhiveframework/desk/form/__init__.py @@ -0,0 +1,2 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE diff --git a/xhiveframework/desk/form/assign_to.py b/xhiveframework/desk/form/assign_to.py new file mode 100644 index 0000000..876d91e --- /dev/null +++ b/xhiveframework/desk/form/assign_to.py @@ -0,0 +1,286 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +"""assign/unassign to ToDo""" + +import json + +import xhiveframework +import xhiveframework.share +import xhiveframework.utils +from xhiveframework import _ +from xhiveframework.desk.doctype.notification_log.notification_log import ( + enqueue_create_notification, + get_title, + get_title_html, +) +from xhiveframework.desk.form.document_follow import follow_document + + +class DuplicateToDoError(xhiveframework.ValidationError): + pass + + +def get(args=None): + """get assigned to""" + if not args: + args = xhiveframework.local.form_dict + + return xhiveframework.get_all( + "ToDo", + fields=["allocated_to as owner", "name"], + filters={ + "reference_type": args.get("doctype"), + "reference_name": args.get("name"), + "status": ("not in", ("Cancelled", "Closed")), + }, + limit=5, + ) + + +@xhiveframework.whitelist() +def add(args=None, *, ignore_permissions=False): + """add in someone's to do list + args = { + "assign_to": [], + "doctype": , + "name": , + "description": , + "assignment_rule": + } + + """ + if not args: + args = xhiveframework.local.form_dict + + users_with_duplicate_todo = [] + shared_with_users = [] + + for assign_to in xhiveframework.parse_json(args.get("assign_to")): + filters = { + "reference_type": args["doctype"], + "reference_name": args["name"], + "status": "Open", + "allocated_to": assign_to, + } + if not ignore_permissions: + xhiveframework.get_doc(args["doctype"], args["name"]).check_permission() + + if xhiveframework.get_all("ToDo", filters=filters): + users_with_duplicate_todo.append(assign_to) + else: + from xhiveframework.utils import nowdate + + if not args.get("description"): + args["description"] = _("Assignment for {0} {1}").format(args["doctype"], args["name"]) + + d = xhiveframework.get_doc( + { + "doctype": "ToDo", + "allocated_to": assign_to, + "reference_type": args["doctype"], + "reference_name": args["name"], + "description": args.get("description"), + "priority": args.get("priority", "Medium"), + "status": "Open", + "date": args.get("date", nowdate()), + "assigned_by": args.get("assigned_by", xhiveframework.session.user), + "assignment_rule": args.get("assignment_rule"), + } + ).insert(ignore_permissions=True) + + # set assigned_to if field exists + if xhiveframework.get_meta(args["doctype"]).get_field("assigned_to"): + xhiveframework.db.set_value(args["doctype"], args["name"], "assigned_to", assign_to) + + doc = xhiveframework.get_doc(args["doctype"], args["name"]) + + # if assignee does not have permissions, share or inform + if not xhiveframework.has_permission(doc=doc, user=assign_to): + if xhiveframework.get_system_settings("disable_document_sharing"): + msg = _("User {0} is not permitted to access this document.").format( + xhiveframework.bold(assign_to) + ) + msg += "
    " + _( + "As document sharing is disabled, please give them the required permissions before assigning." + ) + xhiveframework.throw(msg, title=_("Missing Permission")) + else: + xhiveframework.share.add(doc.doctype, doc.name, assign_to) + shared_with_users.append(assign_to) + + # make this document followed by assigned user + if xhiveframework.get_cached_value("User", assign_to, "follow_assigned_documents"): + follow_document(args["doctype"], args["name"], assign_to) + + # notify + notify_assignment( + d.assigned_by, + d.allocated_to, + d.reference_type, + d.reference_name, + action="ASSIGN", + description=args.get("description"), + ) + + if shared_with_users: + user_list = format_message_for_assign_to(shared_with_users) + xhiveframework.msgprint( + _("Shared with the following Users with Read access:{0}").format(user_list, alert=True) + ) + + if users_with_duplicate_todo: + user_list = format_message_for_assign_to(users_with_duplicate_todo) + xhiveframework.msgprint(_("Already in the following Users ToDo list:{0}").format(user_list, alert=True)) + + return get(args) + + +@xhiveframework.whitelist() +def add_multiple(args=None): + if not args: + args = xhiveframework.local.form_dict + + docname_list = json.loads(args["name"]) + + for docname in docname_list: + args.update({"name": docname}) + add(args) + + +def close_all_assignments(doctype, name, ignore_permissions=False): + assignments = xhiveframework.get_all( + "ToDo", + fields=["allocated_to", "name"], + filters=dict(reference_type=doctype, reference_name=name, status=("!=", "Cancelled")), + ) + if not assignments: + return False + + for assign_to in assignments: + set_status( + doctype, + name, + todo=assign_to.name, + assign_to=assign_to.allocated_to, + status="Closed", + ignore_permissions=ignore_permissions, + ) + + return True + + +@xhiveframework.whitelist() +def remove(doctype, name, assign_to, ignore_permissions=False): + return set_status(doctype, name, "", assign_to, status="Cancelled", ignore_permissions=ignore_permissions) + + +@xhiveframework.whitelist() +def close(doctype: str, name: str, assign_to: str, ignore_permissions=False): + if assign_to != xhiveframework.session.user: + xhiveframework.throw(_("Only the assignee can complete this to-do.")) + + return set_status(doctype, name, "", assign_to, status="Closed", ignore_permissions=ignore_permissions) + + +def set_status(doctype, name, todo=None, assign_to=None, status="Cancelled", ignore_permissions=False): + """remove from todo""" + + if not ignore_permissions: + xhiveframework.get_doc(doctype, name).check_permission() + try: + if not todo: + todo = xhiveframework.db.get_value( + "ToDo", + { + "reference_type": doctype, + "reference_name": name, + "allocated_to": assign_to, + "status": ("!=", status), + }, + ) + if todo: + todo = xhiveframework.get_doc("ToDo", todo) + todo.status = status + todo.save(ignore_permissions=True) + + notify_assignment(todo.assigned_by, todo.allocated_to, todo.reference_type, todo.reference_name) + except xhiveframework.DoesNotExistError: + pass + + # clear assigned_to if field exists + if xhiveframework.get_meta(doctype).get_field("assigned_to") and status in ("Cancelled", "Closed"): + xhiveframework.db.set_value(doctype, name, "assigned_to", None) + + return get({"doctype": doctype, "name": name}) + + +def clear(doctype, name, ignore_permissions=False): + """ + Clears assignments, return False if not assigned. + """ + assignments = xhiveframework.get_all( + "ToDo", + fields=["allocated_to", "name"], + filters=dict(reference_type=doctype, reference_name=name), + ) + if not assignments: + return False + + for assign_to in assignments: + set_status( + doctype, + name, + todo=assign_to.name, + assign_to=assign_to.allocated_to, + status="Cancelled", + ignore_permissions=ignore_permissions, + ) + + return True + + +def notify_assignment(assigned_by, allocated_to, doc_type, doc_name, action="CLOSE", description=None): + """ + Notify assignee that there is a change in assignment + """ + if not (assigned_by and allocated_to and doc_type and doc_name): + return + + assigned_user = xhiveframework.db.get_value("User", allocated_to, ["language", "enabled"], as_dict=True) + + # return if self assigned or user disabled + if assigned_by == allocated_to or not assigned_user.enabled: + return + + # Search for email address in description -- i.e. assignee + user_name = xhiveframework.get_cached_value("User", xhiveframework.session.user, "full_name") + title = get_title(doc_type, doc_name) + description_html = f"
    {description}
    " if description else None + + if action == "CLOSE": + subject = _("Your assignment on {0} {1} has been removed by {2}", lang=assigned_user.language).format( + xhiveframework.bold(_(doc_type)), get_title_html(title), xhiveframework.bold(user_name) + ) + else: + user_name = xhiveframework.bold(user_name) + document_type = xhiveframework.bold(_(doc_type, lang=assigned_user.language)) + title = get_title_html(title) + subject = _("{0} assigned a new task {1} {2} to you", lang=assigned_user.language).format( + user_name, document_type, title + ) + + notification_doc = { + "type": "Assignment", + "document_type": doc_type, + "subject": subject, + "document_name": doc_name, + "from_user": xhiveframework.session.user, + "email_content": description_html, + } + + enqueue_create_notification(allocated_to, notification_doc) + + +def format_message_for_assign_to(users): + return "

    " + "
    ".join(users) diff --git a/xhiveframework/desk/form/document_follow.py b/xhiveframework/desk/form/document_follow.py new file mode 100644 index 0000000..8521928 --- /dev/null +++ b/xhiveframework/desk/form/document_follow.py @@ -0,0 +1,342 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +import xhiveframework.utils +from xhiveframework import _ +from xhiveframework.model import log_types +from xhiveframework.query_builder import DocType +from xhiveframework.utils import get_url_to_form + + +@xhiveframework.whitelist() +def update_follow(doctype, doc_name, following): + if following: + return follow_document(doctype, doc_name, xhiveframework.session.user) + else: + return unfollow_document(doctype, doc_name, xhiveframework.session.user) + + +@xhiveframework.whitelist() +def follow_document(doctype, doc_name, user): + """ + param: + Doctype name + doc name + user email + + condition: + avoided for some doctype + follow only if track changes are set to 1 + """ + if ( + doctype + in ( + "Communication", + "ToDo", + "Email Unsubscribe", + "File", + "Comment", + "Email Account", + "Email Domain", + ) + or doctype in log_types + ): + return + + if (not xhiveframework.get_meta(doctype).track_changes) or user == "Administrator": + return + + if not xhiveframework.db.get_value("User", user, "document_follow_notify", ignore=True, cache=True): + return + + if not is_document_followed(doctype, doc_name, user): + doc = xhiveframework.new_doc("Document Follow") + doc.update({"ref_doctype": doctype, "ref_docname": doc_name, "user": user}) + doc.save() + return doc + + +@xhiveframework.whitelist() +def unfollow_document(doctype, doc_name, user): + doc = xhiveframework.get_all( + "Document Follow", + filters={"ref_doctype": doctype, "ref_docname": doc_name, "user": user}, + fields=["name"], + limit=1, + ) + if doc: + xhiveframework.delete_doc("Document Follow", doc[0].name) + return 1 + return 0 + + +def get_message(doc_name, doctype, frequency, user): + activity_list = get_version(doctype, doc_name, frequency, user) + get_comments( + doctype, doc_name, frequency, user + ) + return sorted(activity_list, key=lambda k: k["time"], reverse=True) + + +def send_email_alert(receiver, docinfo, timeline): + if receiver: + xhiveframework.sendmail( + subject=_("Document Follow Notification"), + recipients=[receiver], + template="document_follow", + args={ + "docinfo": docinfo, + "timeline": timeline, + }, + ) + + +def send_document_follow_mails(frequency): + """ + param: + frequency for sanding mails + + task: + set receiver according to frequency + group document list according to user + get changes, activity, comments on doctype + call method to send mail + """ + + user_list = get_user_list(frequency) + + for user in user_list: + message, valid_document_follows = get_message_for_user(frequency, user) + if message: + send_email_alert(user, valid_document_follows, message) + # send an email if we have already spent resources creating the message + # nosemgrep + xhiveframework.db.commit() + + +def get_user_list(frequency): + DocumentFollow = DocType("Document Follow") + User = DocType("User") + return ( + xhiveframework.qb.from_(DocumentFollow) + .join(User) + .on(DocumentFollow.user == User.name) + .where(User.document_follow_notify == 1) + .where(User.document_follow_frequency == frequency) + .select(DocumentFollow.user) + .groupby(DocumentFollow.user) + ).run(pluck="user") + + +def get_message_for_user(frequency, user): + message = [] + latest_document_follows = get_document_followed_by_user(user) + valid_document_follows = [] + + for document_follow in latest_document_follows: + content = get_message(document_follow.ref_docname, document_follow.ref_doctype, frequency, user) + if content: + message = message + content + valid_document_follows.append( + { + "reference_docname": document_follow.ref_docname, + "reference_doctype": document_follow.ref_doctype, + "reference_url": get_url_to_form( + document_follow.ref_doctype, document_follow.ref_docname + ), + } + ) + return message, valid_document_follows + + +def get_document_followed_by_user(user): + DocumentFollow = DocType("Document Follow") + # at max 20 documents are sent for each user + return ( + xhiveframework.qb.from_(DocumentFollow) + .where(DocumentFollow.user == user) + .select(DocumentFollow.ref_doctype, DocumentFollow.ref_docname) + .orderby(DocumentFollow.modified) + .limit(20) + ).run(as_dict=True) + + +def get_version(doctype, doc_name, frequency, user): + timeline = [] + version = xhiveframework.get_all( + "Version", + filters=[ + ["ref_doctype", "=", doctype], + ["docname", "=", doc_name], + *_get_filters(frequency, user), + ], + fields=["data", "modified", "modified_by"], + ) + if version: + for v in version: + change = xhiveframework.parse_json(v.data) + time = xhiveframework.utils.format_datetime(v.modified, "hh:mm a") + timeline_items = [] + if change.changed: + timeline_items = get_field_changed(change.changed, time, doctype, doc_name, v) + if change.row_changed: + timeline_items = get_row_changed(change.row_changed, time, doctype, doc_name, v) + if change.added: + timeline_items = get_added_row(change.added, time, doctype, doc_name, v) + + timeline = timeline + timeline_items + + return timeline + + +def get_comments(doctype, doc_name, frequency, user): + from xhiveframework.core.utils import html2text + + timeline = [] + comments = xhiveframework.get_all( + "Comment", + filters=[ + ["reference_doctype", "=", doctype], + ["reference_name", "=", doc_name], + *_get_filters(frequency, user), + ], + fields=["content", "modified", "modified_by", "comment_type"], + ) + for comment in comments: + if comment.comment_type == "Like": + by = f""" By : {comment.modified_by}""" + elif comment.comment_type == "Comment": + by = f"""Commented by : {comment.modified_by}""" + else: + by = "" + + time = xhiveframework.utils.format_datetime(comment.modified, "hh:mm a") + timeline.append( + { + "time": comment.modified, + "data": {"time": time, "comment": html2text(str(comment.content)), "by": by}, + "doctype": doctype, + "doc_name": doc_name, + "type": "comment", + } + ) + return timeline + + +def is_document_followed(doctype, doc_name, user): + return xhiveframework.db.exists( + "Document Follow", {"ref_doctype": doctype, "ref_docname": doc_name, "user": user} + ) + + +@xhiveframework.whitelist() +def get_follow_users(doctype, doc_name): + return xhiveframework.get_all( + "Document Follow", filters={"ref_doctype": doctype, "ref_docname": doc_name}, fields=["user"] + ) + + +def get_row_changed(row_changed, time, doctype, doc_name, v): + from xhiveframework.core.utils import html2text + + items = [] + for d in row_changed: + d[2] = d[2] if d[2] else " " + d[0] = d[0] if d[0] else " " + d[3][0][1] = d[3][0][1] if d[3][0][1] else " " + items.append( + { + "time": v.modified, + "data": { + "time": time, + "table_field": d[0], + "row": str(d[1]), + "field": d[3][0][0], + "from": html2text(str(d[3][0][1])), + "to": html2text(str(d[3][0][2])), + }, + "doctype": doctype, + "doc_name": doc_name, + "type": "row changed", + "by": v.modified_by, + } + ) + return items + + +def get_added_row(added, time, doctype, doc_name, v): + return [ + { + "time": v.modified, + "data": {"to": d[0], "time": time}, + "doctype": doctype, + "doc_name": doc_name, + "type": "row added", + "by": v.modified_by, + } + for d in added + ] + + +def get_field_changed(changed, time, doctype, doc_name, v): + from xhiveframework.core.utils import html2text + + items = [] + for d in changed: + d[1] = d[1] if d[1] else " " + d[2] = d[2] if d[2] else " " + d[0] = d[0] if d[0] else " " + items.append( + { + "time": v.modified, + "data": { + "time": time, + "field": d[0], + "from": html2text(str(d[1])), + "to": html2text(str(d[2])), + }, + "doctype": doctype, + "doc_name": doc_name, + "type": "field changed", + "by": v.modified_by, + } + ) + return items + + +def send_hourly_updates(): + send_document_follow_mails("Hourly") + + +def send_daily_updates(): + send_document_follow_mails("Daily") + + +def send_weekly_updates(): + send_document_follow_mails("Weekly") + + +def _get_filters(frequency, user): + filters = [ + ["modified_by", "!=", user], + ] + + if frequency == "Weekly": + filters += [ + ["modified", ">", xhiveframework.utils.add_days(xhiveframework.utils.nowdate(), -7)], + ["modified", "<", xhiveframework.utils.nowdate()], + ] + + elif frequency == "Daily": + filters += [ + ["modified", ">", xhiveframework.utils.add_days(xhiveframework.utils.nowdate(), -1)], + ["modified", "<", xhiveframework.utils.nowdate()], + ] + + elif frequency == "Hourly": + filters += [ + ["modified", ">", xhiveframework.utils.add_to_date(xhiveframework.utils.now_datetime(), hours=-1)], + ["modified", "<", xhiveframework.utils.now_datetime()], + ] + + return filters diff --git a/xhiveframework/desk/form/linked_with.py b/xhiveframework/desk/form/linked_with.py new file mode 100644 index 0000000..cb22575 --- /dev/null +++ b/xhiveframework/desk/form/linked_with.py @@ -0,0 +1,671 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import itertools +import json +from collections import defaultdict + +import xhiveframework +import xhiveframework.desk.form.load +import xhiveframework.desk.form.meta +from xhiveframework import _ +from xhiveframework.model.meta import is_single +from xhiveframework.modules import load_doctype_module + + +@xhiveframework.whitelist() +def get_submitted_linked_docs(doctype: str, name: str) -> list[tuple]: + """Get all the nested submitted documents those are present in referencing tables (dependent tables). + + :param doctype: Document type + :param name: Name of the document + + Usecase: + * User should be able to cancel the linked documents along with the one user trying to cancel. + + Case1: If document sd1-n1 (document name n1 from sumittable doctype sd1) is linked to sd2-n2 and sd2-n2 is linked to sd3-n3, + Getting submittable linked docs of `sd1-n1`should give both sd2-n2 and sd3-n3. + Case2: If document sd1-n1 (document name n1 from sumittable doctype sd1) is linked to d2-n2 and d2-n2 is linked to sd3-n3, + Getting submittable linked docs of `sd1-n1`should give None. (because d2-n2 is not a submittable doctype) + Case3: If document sd1-n1 (document name n1 from submittable doctype sd1) is linked to d2-n2 & sd2-n2. d2-n2 is linked to sd3-n3. + Getting submittable linked docs of `sd1-n1`should give sd2-n2. + + Logic: + ----- + 1. We can find linked documents only if we know how the doctypes are related. + 2. As we need only submittable documents, we can limit doctype relations search to submittable doctypes by + finding the relationships(Foreign key references) across submittable doctypes. + 3. Searching for links is going to be a tree like structure where at every level, + you will be finding documents using parent document and parent document links. + """ + xhiveframework.has_permission(doctype, doc=name) + tree = SubmittableDocumentTree(doctype, name) + visited_documents = tree.get_all_children() + docs = [] + + for dt, names in visited_documents.items(): + docs.extend([{"doctype": dt, "name": name, "docstatus": 1} for name in names]) + + return {"docs": docs, "count": len(docs)} + + +class SubmittableDocumentTree: + def __init__(self, doctype: str, name: str): + """Construct a tree for the submitable linked documents. + + * Node has properties like doctype and docnames. Represented as Node(doctype, docnames). + * Nodes are linked by doctype relationships like table, link and dynamic links. + * Node is referenced(linked) by many other documents and those are the child nodes. + + NOTE: child document is a property of child node (not same as XhiveFramework child docs of a table field). + """ + self.root_doctype = doctype + self.root_docname = name + + # Documents those are yet to be visited for linked documents. + self.to_be_visited_documents = {doctype: [name]} + self.visited_documents = defaultdict(list) + + self._submittable_doctypes = None # All submittable doctypes in the system + self._references_across_doctypes = None # doctype wise links/references + + def get_all_children(self): + """Get all nodes of a tree except the root node (all the nested submitted + documents those are present in referencing tables dependent tables). + """ + while self.to_be_visited_documents: + next_level_children = defaultdict(list) + for parent_dt in list(self.to_be_visited_documents): + parent_docs = self.to_be_visited_documents.get(parent_dt) + if not parent_docs: + del self.to_be_visited_documents[parent_dt] + continue + + child_docs = self.get_next_level_children(parent_dt, parent_docs) + self.visited_documents[parent_dt].extend(parent_docs) + for linked_dt, linked_names in child_docs.items(): + not_visited_child_docs = set(linked_names) - set( + self.visited_documents.get(linked_dt, []) + ) + next_level_children[linked_dt].extend(not_visited_child_docs) + + self.to_be_visited_documents = next_level_children + + # Remove root node from visited documents + if self.root_docname in self.visited_documents.get(self.root_doctype, []): + self.visited_documents[self.root_doctype].remove(self.root_docname) + + return self.visited_documents + + def get_next_level_children(self, parent_dt, parent_names): + """Get immediate children of a Node(parent_dt, parent_names)""" + referencing_fields = self.get_doctype_references(parent_dt) + + child_docs = defaultdict(list) + for field in referencing_fields: + if field["fieldname"] == "amended_from": + # perf: amended_from links are always linked to cancelled documents. + continue + + links = ( + get_referencing_documents( + parent_dt, + parent_names.copy(), + field, + get_parent_if_child_table_doc=True, + parent_filters=[("docstatus", "=", 1)], + allowed_parents=self.get_link_sources(), + ) + or {} + ) + for dt, names in links.items(): + child_docs[dt].extend(names) + return child_docs + + def get_doctype_references(self, doctype): + """Get references for a given document.""" + if self._references_across_doctypes is None: + get_links_to = self.get_document_sources() + limit_link_doctypes = self.get_link_sources() + self._references_across_doctypes = get_references_across_doctypes( + get_links_to, limit_link_doctypes + ) + return self._references_across_doctypes.get(doctype, []) + + def get_document_sources(self): + """Returns list of doctypes from where we access submittable documents.""" + return list(set([*self.get_link_sources(), self.root_doctype])) + + def get_link_sources(self): + """limit doctype links to these doctypes.""" + return list(set(self.get_submittable_doctypes()) - set(get_exempted_doctypes() or [])) + + def get_submittable_doctypes(self) -> list[str]: + """Returns list of submittable doctypes.""" + if not self._submittable_doctypes: + self._submittable_doctypes = xhiveframework.get_all( + "DocType", {"is_submittable": 1}, pluck="name", order_by=None + ) + return self._submittable_doctypes + + +def get_child_tables_of_doctypes(doctypes: list[str] | None = None): + """Returns child tables by doctype.""" + filters = [["fieldtype", "=", "Table"]] + filters_for_docfield = filters + filters_for_customfield = filters + + if doctypes: + filters_for_docfield = [*filters, ["parent", "in", tuple(doctypes)]] + filters_for_customfield = [*filters, ["dt", "in", tuple(doctypes)]] + + links = xhiveframework.get_all( + "DocField", + fields=["parent", "fieldname", "options as child_table"], + filters=filters_for_docfield, + as_list=1, + order_by=None, + ) + + links += xhiveframework.get_all( + "Custom Field", + fields=["dt as parent", "fieldname", "options as child_table"], + filters=filters_for_customfield, + as_list=1, + order_by=None, + ) + + child_tables_by_doctype = defaultdict(list) + for doctype, fieldname, child_table in links: + child_tables_by_doctype[doctype].append( + {"doctype": doctype, "fieldname": fieldname, "child_table": child_table} + ) + return child_tables_by_doctype + + +def get_references_across_doctypes( + to_doctypes: list[str] | None = None, limit_link_doctypes: list[str] | None = None +) -> list: + """Find doctype wise foreign key references. + + :param to_doctypes: Get links of these doctypes. + :param limit_link_doctypes: limit links to these doctypes. + + * Include child table, link and dynamic link references. + """ + if limit_link_doctypes: + child_tables_by_doctype = get_child_tables_of_doctypes(limit_link_doctypes) + all_child_tables = [ + each["child_table"] for each in itertools.chain(*child_tables_by_doctype.values()) + ] + limit_link_doctypes = limit_link_doctypes + all_child_tables + else: + child_tables_by_doctype = get_child_tables_of_doctypes() + all_child_tables = [ + each["child_table"] for each in itertools.chain(*child_tables_by_doctype.values()) + ] + + references_by_link_fields = get_references_across_doctypes_by_link_field(to_doctypes, limit_link_doctypes) + references_by_dlink_fields = get_references_across_doctypes_by_dynamic_link_field( + to_doctypes, limit_link_doctypes + ) + + references = references_by_link_fields.copy() + for k, v in references_by_dlink_fields.items(): + references.setdefault(k, []).extend(v) + + for links in references.values(): + for link in links: + link["is_child"] = link["doctype"] in all_child_tables + return references + + +def get_references_across_doctypes_by_link_field( + to_doctypes: list[str] | None = None, limit_link_doctypes: list[str] | None = None +): + """Find doctype wise foreign key references based on link fields. + + :param to_doctypes: Get links to these doctypes. + :param limit_link_doctypes: limit links to these doctypes. + """ + filters = [["fieldtype", "=", "Link"]] + + if to_doctypes: + filters += [["options", "in", tuple(to_doctypes)]] + + filters_for_docfield = filters[:] + filters_for_customfield = filters[:] + + if limit_link_doctypes: + filters_for_docfield += [["parent", "in", tuple(limit_link_doctypes)]] + filters_for_customfield += [["dt", "in", tuple(limit_link_doctypes)]] + + links = xhiveframework.get_all( + "DocField", + fields=["parent", "fieldname", "options as linked_to"], + filters=filters_for_docfield, + as_list=1, + ) + + links += xhiveframework.get_all( + "Custom Field", + fields=["dt as parent", "fieldname", "options as linked_to"], + filters=filters_for_customfield, + as_list=1, + ) + + links_by_doctype = defaultdict(list) + for doctype, fieldname, linked_to in links: + links_by_doctype[linked_to].append({"doctype": doctype, "fieldname": fieldname}) + return links_by_doctype + + +def get_references_across_doctypes_by_dynamic_link_field( + to_doctypes: list[str] | None = None, limit_link_doctypes: list[str] | None = None +): + """Find doctype wise foreign key references based on dynamic link fields. + + :param to_doctypes: Get links to these doctypes. + :param limit_link_doctypes: limit links to these doctypes. + """ + + filters = [["fieldtype", "=", "Dynamic Link"]] + + filters_for_docfield = filters[:] + filters_for_customfield = filters[:] + + if limit_link_doctypes: + filters_for_docfield += [["parent", "in", tuple(limit_link_doctypes)]] + filters_for_customfield += [["dt", "in", tuple(limit_link_doctypes)]] + + # find dynamic links of parents + links = xhiveframework.get_all( + "DocField", + fields=["parent as doctype", "fieldname", "options as doctype_fieldname"], + filters=filters_for_docfield, + as_list=1, + order_by=None, + ) + + links += xhiveframework.get_all( + "Custom Field", + fields=["dt as doctype", "fieldname", "options as doctype_fieldname"], + filters=filters_for_customfield, + as_list=1, + order_by=None, + ) + + links_by_doctype = defaultdict(list) + for doctype, fieldname, doctype_fieldname in links: + try: + filters = [[doctype_fieldname, "in", to_doctypes]] if to_doctypes else [] + for linked_to in xhiveframework.get_all(doctype, pluck=doctype_fieldname, filters=filters, distinct=1): + if linked_to: + links_by_doctype[linked_to].append( + {"doctype": doctype, "fieldname": fieldname, "doctype_fieldname": doctype_fieldname} + ) + except xhiveframework.db.ProgrammingError: + # TODO: FIXME + continue + return links_by_doctype + + +def get_referencing_documents( + reference_doctype: str, + reference_names: list[str], + link_info: dict, + get_parent_if_child_table_doc: bool = True, + parent_filters: list[list] | None = None, + child_filters=None, + allowed_parents=None, +): + """Get linked documents based on link_info. + + :param reference_doctype: reference doctype to find links + :param reference_names: reference document names to find links for + :param link_info: linking details to get the linked documents + Ex: {'doctype': 'Purchase Invoice Advance', 'fieldname': 'reference_name', + 'doctype_fieldname': 'reference_type', 'is_child': True} + :param get_parent_if_child_table_doc: Get parent record incase linked document is a child table record. + :param parent_filters: filters to apply on if not a child table. + :param child_filters: apply filters if it is a child table. + :param allowed_parents: list of parents allowed in case of get_parent_if_child_table_doc + is enabled. + """ + from_table = link_info["doctype"] + filters = [[link_info["fieldname"], "in", tuple(reference_names)]] + if link_info.get("doctype_fieldname"): + filters.append([link_info["doctype_fieldname"], "=", reference_doctype]) + + if not link_info.get("is_child"): + filters.extend(parent_filters or []) + return {from_table: xhiveframework.get_all(from_table, filters, pluck="name", order_by=None)} + + filters.extend(child_filters or []) + res = xhiveframework.get_all(from_table, filters=filters, fields=["name", "parenttype", "parent"], order_by=None) + documents = defaultdict(list) + + for parent, rows in itertools.groupby(res, key=lambda row: row["parenttype"]): + if allowed_parents and parent not in allowed_parents: + continue + filters = (parent_filters or []) + [["name", "in", tuple(row.parent for row in rows)]] + documents[parent].extend(xhiveframework.get_all(parent, filters=filters, pluck="name", order_by=None) or []) + return documents + + +@xhiveframework.whitelist() +def cancel_all_linked_docs(docs, ignore_doctypes_on_cancel_all=None): + """ + Cancel all linked doctype, optionally ignore doctypes specified in a list. + + Arguments: + docs (json str) - It contains list of dictionaries of a linked documents. + ignore_doctypes_on_cancel_all (list) - List of doctypes to ignore while cancelling. + """ + if ignore_doctypes_on_cancel_all is None: + ignore_doctypes_on_cancel_all = [] + + docs = json.loads(docs) + if isinstance(ignore_doctypes_on_cancel_all, str): + ignore_doctypes_on_cancel_all = json.loads(ignore_doctypes_on_cancel_all) + for i, doc in enumerate(docs, 1): + if validate_linked_doc(doc, ignore_doctypes_on_cancel_all): + linked_doc = xhiveframework.get_doc(doc.get("doctype"), doc.get("name")) + linked_doc.cancel() + xhiveframework.publish_progress(percent=i / len(docs) * 100, title=_("Cancelling documents")) + + +def validate_linked_doc(docinfo, ignore_doctypes_on_cancel_all=None): + """ + Validate a document to be submitted and non-exempted from auto-cancel. + + Arguments: + docinfo (dict): The document to check for submitted and non-exempt from auto-cancel + ignore_doctypes_on_cancel_all (list) - List of doctypes to ignore while cancelling. + + Returns: + bool: True if linked document passes all validations, else False + """ + # ignore doctype to cancel + if docinfo.get("doctype") in (ignore_doctypes_on_cancel_all or []): + return False + + # skip non-submittable doctypes since they don't need to be cancelled + if not xhiveframework.get_meta(docinfo.get("doctype")).is_submittable: + return False + + # skip draft or cancelled documents + if docinfo.get("docstatus") != 1: + return False + + # skip other doctypes since they don't need to be cancelled + auto_cancel_exempt_doctypes = get_exempted_doctypes() + if docinfo.get("doctype") in auto_cancel_exempt_doctypes: + return False + + return True + + +def get_exempted_doctypes(): + """Get list of doctypes exempted from being auto-cancelled""" + return list(xhiveframework.get_hooks("auto_cancel_exempted_doctypes")) + + +def get_linked_docs(doctype: str, name: str, linkinfo: dict | None = None) -> dict[str, list]: + if isinstance(linkinfo, str): + # additional fields are added in linkinfo + linkinfo = json.loads(linkinfo) + + results = {} + + if not linkinfo: + return results + + for dt, link in linkinfo.items(): + filters = [] + link["doctype"] = dt + try: + link_meta_bundle = xhiveframework.desk.form.load.get_meta_bundle(dt) + except Exception as e: + if isinstance(e, xhiveframework.DoesNotExistError): + xhiveframework.clear_last_message() + continue + linkmeta = link_meta_bundle[0] + + if not linkmeta.get("issingle"): + fields = [ + d.fieldname + for d in linkmeta.get( + "fields", + { + "in_list_view": 1, + "fieldtype": ["not in", ("Image", "HTML", "Button", *xhiveframework.model.table_fields)], + }, + ) + ] + ["name", "modified", "docstatus"] + + if link.get("add_fields"): + fields += link["add_fields"] + + fields = [f"`tab{dt}`.`{sf.strip()}`" for sf in fields if sf and "`tab" not in sf] + + try: + if link.get("filters"): + ret = xhiveframework.get_all( + doctype=dt, fields=fields, filters=link.get("filters"), order_by=None + ) + + elif link.get("get_parent"): + ret = None + + # check for child table + if not xhiveframework.get_meta(doctype).istable: + continue + + me = xhiveframework.db.get_value( + doctype, name, ["parenttype", "parent"], as_dict=True, order_by=None + ) + if me and me.parenttype == dt: + ret = xhiveframework.get_all( + doctype=dt, fields=fields, filters=[[dt, "name", "=", me.parent]], order_by=None + ) + + elif link.get("child_doctype"): + or_filters = [ + [link.get("child_doctype"), link_fieldnames, "=", name] + for link_fieldnames in link.get("fieldname") + ] + + # dynamic link + if link.get("doctype_fieldname"): + filters.append( + [link.get("child_doctype"), link.get("doctype_fieldname"), "=", doctype] + ) + + ret = xhiveframework.get_all( + doctype=dt, + fields=fields, + filters=filters, + or_filters=or_filters, + distinct=True, + order_by=None, + ) + + else: + link_fieldnames = link.get("fieldname") + if link_fieldnames: + if isinstance(link_fieldnames, str): + link_fieldnames = [link_fieldnames] + or_filters = [[dt, fieldname, "=", name] for fieldname in link_fieldnames] + # dynamic link + if link.get("doctype_fieldname"): + filters.append([dt, link.get("doctype_fieldname"), "=", doctype]) + ret = xhiveframework.get_all( + doctype=dt, fields=fields, filters=filters, or_filters=or_filters, order_by=None + ) + + else: + ret = None + + except xhiveframework.PermissionError: + xhiveframework.clear_last_message() + + continue + + if ret: + results[dt] = ret + + return results + + +@xhiveframework.whitelist() +def get(doctype, docname): + xhiveframework.has_permission(doctype, doc=docname) + linked_doctypes = get_linked_doctypes(doctype=doctype) + return get_linked_docs(doctype=doctype, name=docname, linkinfo=linked_doctypes) + + +@xhiveframework.whitelist() +def get_linked_doctypes(doctype, without_ignore_user_permissions_enabled=False): + """add list of doctypes this doctype is 'linked' with. + + Example, for Customer: + + {"Address": {"fieldname": "customer"}..} + """ + if without_ignore_user_permissions_enabled: + return xhiveframework.cache.hget( + "linked_doctypes_without_ignore_user_permissions_enabled", + doctype, + lambda: _get_linked_doctypes(doctype, without_ignore_user_permissions_enabled), + ) + else: + return xhiveframework.cache.hget("linked_doctypes", doctype, lambda: _get_linked_doctypes(doctype)) + + +def _get_linked_doctypes(doctype, without_ignore_user_permissions_enabled=False): + ret = {} + # find fields where this doctype is linked + ret.update(get_linked_fields(doctype, without_ignore_user_permissions_enabled)) + ret.update(get_dynamic_linked_fields(doctype, without_ignore_user_permissions_enabled)) + + filters = [["fieldtype", "in", xhiveframework.model.table_fields], ["options", "=", doctype]] + if without_ignore_user_permissions_enabled: + filters.append(["ignore_user_permissions", "!=", 1]) + # find links of parents + links = xhiveframework.get_all("DocField", fields=["parent as dt"], filters=filters) + links += xhiveframework.get_all("Custom Field", fields=["dt"], filters=filters) + + for (dt,) in links: + if dt in ret: + continue + ret[dt] = {"get_parent": True} + + for dt in list(ret): + try: + doctype_module = load_doctype_module(dt) + except (ImportError, KeyError): + # in case of Custom DocType + # or in case of module rename eg. (Schools -> Education) + continue + + if getattr(doctype_module, "exclude_from_linked_with", False): + del ret[dt] + + return ret + + +def get_linked_fields(doctype, without_ignore_user_permissions_enabled=False): + filters = [["fieldtype", "=", "Link"], ["options", "=", doctype]] + if without_ignore_user_permissions_enabled: + filters.append(["ignore_user_permissions", "!=", 1]) + + # find links of parents + links = xhiveframework.get_all("DocField", fields=["parent", "fieldname"], filters=filters, as_list=1) + links += xhiveframework.get_all("Custom Field", fields=["dt as parent", "fieldname"], filters=filters, as_list=1) + + ret = {} + + if not links: + return ret + + links_dict = defaultdict(list) + for doctype, fieldname in links: + links_dict[doctype].append(fieldname) + + for doctype_name in links_dict: + ret[doctype_name] = {"fieldname": links_dict.get(doctype_name)} + table_doctypes = xhiveframework.get_all( + "DocType", filters=[["istable", "=", "1"], ["name", "in", tuple(links_dict)]] + ) + child_filters = [ + ["fieldtype", "in", xhiveframework.model.table_fields], + ["options", "in", tuple(doctype.name for doctype in table_doctypes)], + ] + if without_ignore_user_permissions_enabled: + child_filters.append(["ignore_user_permissions", "!=", 1]) + + # find out if linked in a child table + for parent, options in xhiveframework.get_all( + "DocField", fields=["parent", "options"], filters=child_filters, as_list=1 + ): + ret[parent] = {"child_doctype": options, "fieldname": links_dict[options]} + if options in ret: + del ret[options] + + virtual_doctypes = xhiveframework.get_all("DocType", {"is_virtual": 1}, pluck="name") + for dt in virtual_doctypes: + ret.pop(dt, None) + + return ret + + +def get_dynamic_linked_fields(doctype, without_ignore_user_permissions_enabled=False): + ret = {} + + filters = [["fieldtype", "=", "Dynamic Link"]] + if without_ignore_user_permissions_enabled: + filters.append(["ignore_user_permissions", "!=", 1]) + + # find dynamic links of parents + links = xhiveframework.get_all( + "DocField", + fields=["parent as doctype", "fieldname", "options as doctype_fieldname"], + filters=filters, + ) + links += xhiveframework.get_all( + "Custom Field", + fields=["dt as doctype", "fieldname", "options as doctype_fieldname"], + filters=filters, + ) + + for df in links: + if is_single(df.doctype): + continue + + meta = xhiveframework.get_meta(df.doctype) + if meta.is_virtual: + continue + + is_child = meta.istable + possible_link = xhiveframework.get_all( + df.doctype, + filters={df.doctype_fieldname: doctype}, + fields=["parenttype"] if is_child else None, + distinct=True, + ) + + if not possible_link: + continue + + if is_child: + for d in possible_link: + ret[d.parenttype] = { + "child_doctype": df.doctype, + "fieldname": [df.fieldname], + "doctype_fieldname": df.doctype_fieldname, + } + else: + ret[df.doctype] = {"fieldname": [df.fieldname], "doctype_fieldname": df.doctype_fieldname} + + return ret diff --git a/xhiveframework/desk/form/load.py b/xhiveframework/desk/form/load.py new file mode 100644 index 0000000..80665d0 --- /dev/null +++ b/xhiveframework/desk/form/load.py @@ -0,0 +1,489 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import typing +from urllib.parse import quote + +import xhiveframework +import xhiveframework.defaults +import xhiveframework.desk.form.meta +import xhiveframework.utils +from xhiveframework import _, _dict +from xhiveframework.desk.form.document_follow import is_document_followed +from xhiveframework.model.utils.user_settings import get_user_settings +from xhiveframework.permissions import get_doc_permissions +from xhiveframework.utils.data import cstr + +if typing.TYPE_CHECKING: + from xhiveframework.model.document import Document + + +@xhiveframework.whitelist() +def getdoc(doctype, name, user=None): + """ + Loads a doclist for a given document. This method is called directly from the client. + Requries "doctype", "name" as form variables. + Will also call the "onload" method on the document. + """ + + if not (doctype and name): + raise Exception("doctype and name required!") + + try: + doc = xhiveframework.get_doc(doctype, name) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_last_message() + return [] + + if not doc.has_permission("read"): + xhiveframework.flags.error_message = _("Insufficient Permission for {0}").format( + xhiveframework.bold(doctype + " " + name) + ) + raise xhiveframework.PermissionError(("read", doctype, name)) + + run_onload(doc) + doc.apply_fieldlevel_read_permissions() + + # add file list + doc.add_viewed() + get_docinfo(doc) + + doc.add_seen() + set_link_titles(doc) + if xhiveframework.response.docs is None: + xhiveframework.local.response = _dict({"docs": []}) + xhiveframework.response.docs.append(doc) + + +@xhiveframework.whitelist() +def getdoctype(doctype, with_parent=False, cached_timestamp=None): + """load doctype""" + + docs = [] + parent_dt = None + + # with parent (called from report builder) + if with_parent and (parent_dt := xhiveframework.model.meta.get_parent_dt(doctype)): + docs = get_meta_bundle(parent_dt) + xhiveframework.response["parent_dt"] = parent_dt + + if not docs: + docs = get_meta_bundle(doctype) + + xhiveframework.response["user_settings"] = get_user_settings(parent_dt or doctype) + + if cached_timestamp and docs[0].modified == cached_timestamp: + return "use_cache" + + xhiveframework.response.docs.extend(docs) + + +def get_meta_bundle(doctype): + bundle = [xhiveframework.desk.form.meta.get_meta(doctype)] + bundle.extend( + xhiveframework.desk.form.meta.get_meta(df.options) + for df in bundle[0].fields + if df.fieldtype in xhiveframework.model.table_fields + ) + return bundle + + +@xhiveframework.whitelist() +def get_docinfo(doc=None, doctype=None, name=None): + from xhiveframework.share import _get_users as get_docshares + + if not doc: + doc = xhiveframework.get_doc(doctype, name) + if not doc.has_permission("read"): + raise xhiveframework.PermissionError + + all_communications = _get_communications(doc.doctype, doc.name, limit=21) + automated_messages = [ + msg for msg in all_communications if msg["communication_type"] == "Automated Message" + ] + communications_except_auto_messages = [ + msg for msg in all_communications if msg["communication_type"] != "Automated Message" + ] + + docinfo = xhiveframework._dict(user_info={}) + + add_comments(doc, docinfo) + + docinfo.update( + { + "doctype": doc.doctype, + "name": doc.name, + "attachments": get_attachments(doc.doctype, doc.name), + "communications": communications_except_auto_messages, + "automated_messages": automated_messages, + "versions": get_versions(doc), + "assignments": get_assignments(doc.doctype, doc.name), + "permissions": get_doc_permissions(doc), + "shared": get_docshares(doc), + "views": get_view_logs(doc), + "energy_point_logs": get_point_logs(doc.doctype, doc.name), + "additional_timeline_content": get_additional_timeline_content(doc.doctype, doc.name), + "milestones": get_milestones(doc.doctype, doc.name), + "is_document_followed": is_document_followed(doc.doctype, doc.name, xhiveframework.session.user), + "tags": get_tags(doc.doctype, doc.name), + "document_email": get_document_email(doc.doctype, doc.name), + } + ) + + update_user_info(docinfo) + + xhiveframework.response["docinfo"] = docinfo + + +def add_comments(doc, docinfo): + # divide comments into separate lists + docinfo.comments = [] + docinfo.shared = [] + docinfo.assignment_logs = [] + docinfo.attachment_logs = [] + docinfo.info_logs = [] + docinfo.like_logs = [] + docinfo.workflow_logs = [] + + comments = xhiveframework.get_all( + "Comment", + fields=["name", "creation", "content", "owner", "comment_type"], + filters={"reference_doctype": doc.doctype, "reference_name": doc.name}, + ) + + for c in comments: + match c.comment_type: + case "Comment": + c.content = xhiveframework.utils.markdown(c.content) + docinfo.comments.append(c) + case "Shared" | "Unshared": + docinfo.shared.append(c) + case "Assignment Completed" | "Assigned": + docinfo.assignment_logs.append(c) + case "Attachment" | "Attachment Removed": + docinfo.attachment_logs.append(c) + case "Info" | "Edit" | "Label": + docinfo.info_logs.append(c) + case "Like": + docinfo.like_logs.append(c) + case "Workflow": + docinfo.workflow_logs.append(c) + + return comments + + +def get_milestones(doctype, name): + return xhiveframework.get_all( + "Milestone", + fields=["creation", "owner", "track_field", "value"], + filters=dict(reference_type=doctype, reference_name=name), + ) + + +def get_attachments(dt, dn): + return xhiveframework.get_all( + "File", + fields=["name", "file_name", "file_url", "is_private"], + filters={"attached_to_name": dn, "attached_to_doctype": dt}, + ) + + +def get_versions(doc: "Document") -> list[dict]: + if not doc.meta.track_changes: + return [] + return xhiveframework.get_all( + "Version", + filters=dict(ref_doctype=doc.doctype, docname=doc.name), + fields=["name", "owner", "creation", "data"], + limit=10, + order_by="creation desc", + ) + + +@xhiveframework.whitelist() +def get_communications(doctype, name, start=0, limit=20): + from xhiveframework.utils import cint + + doc = xhiveframework.get_doc(doctype, name) + if not doc.has_permission("read"): + raise xhiveframework.PermissionError + + return _get_communications(doctype, name, cint(start), cint(limit)) + + +def get_comments(doctype: str, name: str, comment_type: str | list[str] = "Comment") -> list[xhiveframework._dict]: + if isinstance(comment_type, list): + comment_types = comment_type + + elif comment_type == "share": + comment_types = ["Shared", "Unshared"] + + elif comment_type == "assignment": + comment_types = ["Assignment Completed", "Assigned"] + + elif comment_type == "attachment": + comment_types = ["Attachment", "Attachment Removed"] + + else: + comment_types = [comment_type] + + comments = xhiveframework.get_all( + "Comment", + fields=["name", "creation", "content", "owner", "comment_type"], + filters={ + "reference_doctype": doctype, + "reference_name": name, + "comment_type": ["in", comment_types], + }, + ) + + # convert to markdown (legacy ?) + for c in comments: + if c.comment_type == "Comment": + c.content = xhiveframework.utils.markdown(c.content) + + return comments + + +def get_point_logs(doctype, docname): + return xhiveframework.get_all( + "Energy Point Log", + filters={"reference_doctype": doctype, "reference_name": docname, "type": ["!=", "Review"]}, + fields=["*"], + ) + + +def _get_communications(doctype, name, start=0, limit=20): + communications = get_communication_data(doctype, name, start, limit) + for c in communications: + if c.communication_type in ("Communication", "Automated Message"): + c.attachments = json.dumps( + xhiveframework.get_all( + "File", + fields=["file_url", "is_private"], + filters={"attached_to_doctype": "Communication", "attached_to_name": c.name}, + ) + ) + + return communications + + +def get_communication_data( + doctype, name, start=0, limit=20, after=None, fields=None, group_by=None, as_dict=True +): + """Returns list of communications for a given document""" + if not fields: + fields = """ + C.name, C.communication_type, C.communication_medium, + C.comment_type, C.communication_date, C.content, + C.sender, C.sender_full_name, C.cc, C.bcc, + C.creation AS creation, C.subject, C.delivery_status, + C._liked_by, C.reference_doctype, C.reference_name, + C.read_by_recipient, C.rating, C.recipients + """ + + conditions = "" + if after: + # find after a particular date + conditions += f""" + AND C.communication_date > {after} + """ + + if doctype == "User": + conditions += """ + AND NOT (C.reference_doctype='User' AND C.communication_type='Communication') + """ + + # communications linked to reference_doctype + part1 = f""" + SELECT {fields} + FROM `tabCommunication` as C + WHERE C.communication_type IN ('Communication', 'Feedback', 'Automated Message') + AND (C.reference_doctype = %(doctype)s AND C.reference_name = %(name)s) + {conditions} + """ + + # communications linked in Timeline Links + part2 = f""" + SELECT {fields} + FROM `tabCommunication` as C + INNER JOIN `tabCommunication Link` ON C.name=`tabCommunication Link`.parent + WHERE C.communication_type IN ('Communication', 'Feedback', 'Automated Message') + AND `tabCommunication Link`.link_doctype = %(doctype)s AND `tabCommunication Link`.link_name = %(name)s + {conditions} + """ + + return xhiveframework.db.sql( + """ + SELECT * + FROM (({part1}) UNION ({part2})) AS combined + {group_by} + ORDER BY communication_date DESC + LIMIT %(limit)s + OFFSET %(start)s + """.format(part1=part1, part2=part2, group_by=(group_by or "")), + dict( + doctype=doctype, + name=name, + start=xhiveframework.utils.cint(start), + limit=limit, + ), + as_dict=as_dict, + ) + + +def get_assignments(dt, dn): + return xhiveframework.get_all( + "ToDo", + fields=["name", "allocated_to as owner", "description", "status"], + filters={ + "reference_type": dt, + "reference_name": dn, + "status": ("not in", ("Cancelled", "Closed")), + "allocated_to": ("is", "set"), + }, + ) + + +def run_onload(doc): + doc.set("__onload", xhiveframework._dict()) + doc.run_method("onload") + + +def get_view_logs(doc: "Document") -> list[dict]: + """get and return the latest view logs if available""" + if not doc.meta.track_views: + return [] + + return xhiveframework.get_all( + "View Log", + filters={ + "reference_doctype": doc.doctype, + "reference_name": doc.name, + }, + fields=["name", "creation", "owner"], + order_by="creation desc", + ) + + +def get_tags(doctype: str, name: str) -> str: + tags = xhiveframework.get_all( + "Tag Link", + filters={"document_type": doctype, "document_name": name}, + fields=["tag"], + pluck="tag", + ) + + return ",".join(tags) + + +def get_document_email(doctype, name): + email = get_automatic_email_link() + if not email: + return None + + email = email.split("@") + return f"{email[0]}+{quote(doctype)}={quote(cstr(name))}@{email[1]}" + + +def get_automatic_email_link(): + return xhiveframework.db.get_value( + "Email Account", {"enable_incoming": 1, "enable_automatic_linking": 1}, "email_id" + ) + + +def get_additional_timeline_content(doctype, docname): + contents = [] + hooks = xhiveframework.get_hooks().get("additional_timeline_content", {}) + methods_for_all_doctype = hooks.get("*", []) + methods_for_current_doctype = hooks.get(doctype, []) + + for method in methods_for_all_doctype + methods_for_current_doctype: + contents.extend(xhiveframework.get_attr(method)(doctype, docname) or []) + + 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 = xhiveframework.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 = xhiveframework.get_meta(doctype) + if not meta or not (meta.title_field and meta.show_title_field_in_link): + continue + + link_title = xhiveframework.db.get_value( + doctype, doc.get(field.fieldname), meta.title_field, cache=True, order_by=None + ) + 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 = xhiveframework.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 `xhiveframework.local.response`.""" + if "_link_titles" not in xhiveframework.local.response: + xhiveframework.local.response["_link_titles"] = {} + + xhiveframework.local.response["_link_titles"].update(link_titles) + + +def update_user_info(docinfo): + users = set() + + users.update(d.sender for d in docinfo.communications) + users.update(d.user for d in docinfo.shared) + users.update(d.owner for d in docinfo.assignments) + users.update(d.owner for d in docinfo.views) + users.update(d.owner for d in docinfo.workflow_logs) + users.update(d.owner for d in docinfo.like_logs) + users.update(d.owner for d in docinfo.info_logs) + users.update(d.owner for d in docinfo.attachment_logs) + users.update(d.owner for d in docinfo.assignment_logs) + users.update(d.owner for d in docinfo.comments) + + xhiveframework.utils.add_user_info(users, docinfo.user_info) + + +@xhiveframework.whitelist() +def get_user_info_for_viewers(users): + user_info = {} + for user in json.loads(users): + xhiveframework.utils.add_user_info(user, user_info) + + return user_info diff --git a/xhiveframework/desk/form/meta.py b/xhiveframework/desk/form/meta.py new file mode 100644 index 0000000..2fcbb43 --- /dev/null +++ b/xhiveframework/desk/form/meta.py @@ -0,0 +1,314 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import io +import os + +import xhiveframework +from xhiveframework import _ +from xhiveframework.build import scrub_html_template +from xhiveframework.model.meta import Meta +from xhiveframework.model.utils import render_include +from xhiveframework.modules import get_module_path, load_doctype_module, scrub +from xhiveframework.utils import get_bench_path, get_html_format +from xhiveframework.utils.data import get_link_to_form + +ASSET_KEYS = ( + "__js", + "__css", + "__list_js", + "__calendar_js", + "__map_js", + "__linked_with", + "__messages", + "__print_formats", + "__workflow_docs", + "__form_grid_templates", + "__listview_template", + "__tree_js", + "__dashboard", + "__kanban_column_fields", + "__templates", + "__custom_js", + "__custom_list_js", +) + + +def get_meta(doctype, cached=True) -> "FormMeta": + # don't cache for developer mode as js files, templates may be edited + cached = cached and not xhiveframework.conf.developer_mode + if cached: + meta = xhiveframework.cache.hget("doctype_form_meta", doctype) + if not meta: + # Cache miss - explicitly get meta from DB to avoid + meta = FormMeta(doctype, cached=False) + xhiveframework.cache.hset("doctype_form_meta", doctype, meta) + else: + meta = FormMeta(doctype) + + if xhiveframework.local.lang != "en": + meta.set_translations(xhiveframework.local.lang) + + return meta + + +class FormMeta(Meta): + def __init__(self, doctype, *, cached=True): + self.__dict__.update(xhiveframework.get_meta(doctype, cached=cached).__dict__) + self.load_assets() + + def load_assets(self): + if self.get("__assets_loaded", False): + return + + self.add_search_fields() + self.add_linked_document_type() + + if not self.istable: + self.add_code() + self.add_custom_script() + self.load_print_formats() + self.load_workflows() + self.load_templates() + self.load_dashboard() + self.load_kanban_meta() + + self.set("__assets_loaded", True) + + def as_dict(self, no_nulls=False): + d = super().as_dict(no_nulls=no_nulls) + + for k in ASSET_KEYS: + d[k] = self.get(k) + + # d['fields'] = d.get('fields', []) + + for i, df in enumerate(d.get("fields") or []): + for k in ("search_fields", "is_custom_field", "linked_document_type"): + df[k] = self.get("fields")[i].get(k) + + return d + + def add_code(self): + if self.custom: + return + + path = os.path.join(get_module_path(self.module), "doctype", scrub(self.name)) + + def _get_path(fname): + return os.path.join(path, scrub(fname)) + + system_country = xhiveframework.get_system_settings("country") + + self._add_code(_get_path(self.name + ".js"), "__js") + if system_country: + self._add_code(_get_path(os.path.join("regional", system_country + ".js")), "__js") + + self._add_code(_get_path(self.name + ".css"), "__css") + self._add_code(_get_path(self.name + "_list.js"), "__list_js") + if system_country: + self._add_code(_get_path(os.path.join("regional", system_country + "_list.js")), "__list_js") + + self._add_code(_get_path(self.name + "_calendar.js"), "__calendar_js") + self._add_code(_get_path(self.name + "_tree.js"), "__tree_js") + + listview_template = _get_path(self.name + "_list.html") + if os.path.exists(listview_template): + self.set("__listview_template", get_html_format(listview_template)) + + self.add_code_via_hook("doctype_js", "__js") + self.add_code_via_hook("doctype_list_js", "__list_js") + self.add_code_via_hook("doctype_tree_js", "__tree_js") + self.add_code_via_hook("doctype_calendar_js", "__calendar_js") + self.add_html_templates(path) + + def _add_code(self, path, fieldname): + js = get_js(path) + if js: + bench_path = get_bench_path() + "/" + asset_path = path.replace(bench_path, "") + comment = f"\n\n/* Adding {asset_path} */\n\n" + sourceURL = f"\n\n//# sourceURL={scrub(self.name) + fieldname}" + self.set(fieldname, (self.get(fieldname) or "") + comment + js + sourceURL) + + def add_html_templates(self, path): + if self.custom: + return + templates = dict() + for fname in os.listdir(path): + if fname.endswith(".html"): + with open(os.path.join(path, fname), encoding="utf-8") as f: + templates[fname.split(".", 1)[0]] = scrub_html_template(f.read()) + + self.set("__templates", templates or None) + + def add_code_via_hook(self, hook, fieldname): + for path in get_code_files_via_hooks(hook, self.name): + self._add_code(path, fieldname) + + def add_custom_script(self): + """embed all require files""" + # custom script + client_scripts = ( + xhiveframework.get_all( + "Client Script", + filters={"dt": self.name, "enabled": 1}, + fields=["name", "script", "view"], + order_by="creation asc", + ) + or "" + ) + + list_script = "" + form_script = "" + for script in client_scripts: + if not script.script: + continue + + if script.view == "List": + list_script += f""" +// {script.name} +{script.script} + +""" + + elif script.view == "Form": + form_script += f""" +// {script.name} +{script.script} + +""" + + file = scrub(self.name) + form_script += f"\n\n//# sourceURL={file}__custom_js" + list_script += f"\n\n//# sourceURL={file}__custom_list_js" + + self.set("__custom_js", form_script) + self.set("__custom_list_js", list_script) + + def add_search_fields(self): + """add search fields found in the doctypes indicated by link fields' options""" + for df in self.get("fields", {"fieldtype": "Link", "options": ["!=", "[Select]"]}): + if df.options: + try: + search_fields = xhiveframework.get_meta(df.options).search_fields + except xhiveframework.DoesNotExistError: + self._show_missing_doctype_msg(df) + + if search_fields: + search_fields = search_fields.split(",") + df.search_fields = [sf.strip() for sf in search_fields] + + def _show_missing_doctype_msg(self, df): + # A link field is referring to non-existing doctype, this usually happens when + # customizations are removed or some custom app is removed but hasn't cleaned + # up after itself. + xhiveframework.clear_last_message() + + msg = _("Field {0} is referring to non-existing doctype {1}.").format( + xhiveframework.bold(df.fieldname), xhiveframework.bold(df.options) + ) + + if df.get("is_custom_field"): + custom_field_link = get_link_to_form("Custom Field", df.name) + msg += " " + _("Please delete the field from {0} or add the required doctype.").format( + custom_field_link + ) + + xhiveframework.throw(msg, title=_("Missing DocType")) + + def add_linked_document_type(self): + for df in self.get("fields", {"fieldtype": "Link"}): + if df.options: + try: + df.linked_document_type = xhiveframework.get_meta(df.options).document_type + except xhiveframework.DoesNotExistError: + self._show_missing_doctype_msg(df) + + def load_print_formats(self): + print_formats = xhiveframework.db.sql( + """select * FROM `tabPrint Format` + WHERE doc_type=%s AND docstatus<2 and disabled=0""", + (self.name,), + as_dict=1, + update={"doctype": "Print Format"}, + ) + + self.set("__print_formats", print_formats) + + def load_workflows(self): + # get active workflow + workflow_name = self.get_workflow() + workflow_docs = [] + + if workflow_name and xhiveframework.db.exists("Workflow", workflow_name): + workflow = xhiveframework.get_doc("Workflow", workflow_name) + workflow_docs.append(workflow) + + workflow_docs.extend(xhiveframework.get_doc("Workflow State", d.state) for d in workflow.get("states")) + self.set("__workflow_docs", workflow_docs) + + def load_templates(self): + if not self.custom: + module = load_doctype_module(self.name) + app = module.__name__.split(".", 1)[0] + templates = {} + if hasattr(module, "form_grid_templates"): + for key, path in module.form_grid_templates.items(): + templates[key] = get_html_format(xhiveframework.get_app_path(app, path)) + + self.set("__form_grid_templates", templates) + + def set_translations(self, lang): + from xhiveframework.translate import extract_messages_from_code, make_dict_from_messages + + self.set("__messages", xhiveframework.get_lang_dict("doctype", self.name)) + + # set translations for grid templates + if self.get("__form_grid_templates"): + for content in self.get("__form_grid_templates").values(): + messages = extract_messages_from_code(content) + messages = make_dict_from_messages(messages) + self.get("__messages").update(messages) + + def load_dashboard(self): + self.set("__dashboard", self.get_dashboard_data()) + + def load_kanban_meta(self): + self.load_kanban_column_fields() + + def load_kanban_column_fields(self): + try: + values = xhiveframework.get_list( + "Kanban Board", fields=["field_name"], filters={"reference_doctype": self.name} + ) + + fields = [x["field_name"] for x in values] + fields = list(set(fields)) + self.set("__kanban_column_fields", fields) + except xhiveframework.PermissionError: + # no access to kanban board + pass + + +def get_code_files_via_hooks(hook, name): + code_files = [] + for app_name in xhiveframework.get_installed_apps(): + code_hook = xhiveframework.get_hooks(hook, default={}, app_name=app_name) + if not code_hook: + continue + + files = code_hook.get(name, []) + if not isinstance(files, list): + files = [files] + + for file in files: + path = xhiveframework.get_app_path(app_name, *file.strip("/").split("/")) + code_files.append(path) + + return code_files + + +def get_js(path): + js = xhiveframework.read_file(path) + if js: + return render_include(js) diff --git a/xhiveframework/desk/form/save.py b/xhiveframework/desk/form/save.py new file mode 100644 index 0000000..d29d45e --- /dev/null +++ b/xhiveframework/desk/form/save.py @@ -0,0 +1,85 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.core.doctype.submission_queue.submission_queue import queue_submission +from xhiveframework.desk.form.load import run_onload +from xhiveframework.model.docstatus import DocStatus +from xhiveframework.monitor import add_data_to_monitor +from xhiveframework.utils.scheduler import is_scheduler_inactive +from xhiveframework.utils.telemetry import capture_doc + + +@xhiveframework.whitelist() +def savedocs(doc, action): + """save / submit / update doclist""" + doc = xhiveframework.get_doc(json.loads(doc)) + capture_doc(doc, action) + if doc.get("__islocal") and doc.name.startswith("new-" + doc.doctype.lower().replace(" ", "-")): + # required to relink missing attachments if they exist. + doc.__temporary_name = doc.name + set_local_name(doc) + + # action + doc.docstatus = { + "Save": DocStatus.draft(), + "Submit": DocStatus.submitted(), + "Update": DocStatus.submitted(), + "Cancel": DocStatus.cancelled(), + }[action] + + if doc.docstatus.is_submitted(): + if action == "Submit" and doc.meta.queue_in_background and not is_scheduler_inactive(): + queue_submission(doc, action) + return + doc.submit() + else: + doc.save() + + # update recent documents + run_onload(doc) + send_updated_docs(doc) + + add_data_to_monitor(doctype=doc.doctype, action=action) + xhiveframework.msgprint(xhiveframework._("Saved"), indicator="green", alert=True) + + +@xhiveframework.whitelist() +def cancel(doctype=None, name=None, workflow_state_fieldname=None, workflow_state=None): + """cancel a doclist""" + doc = xhiveframework.get_doc(doctype, name) + capture_doc(doc, "Cancel") + + if workflow_state_fieldname and workflow_state: + doc.set(workflow_state_fieldname, workflow_state) + doc.cancel() + send_updated_docs(doc) + xhiveframework.msgprint(xhiveframework._("Cancelled"), indicator="red", alert=True) + + +def send_updated_docs(doc): + from .load import get_docinfo + + get_docinfo(doc) + + d = doc.as_dict() + if hasattr(doc, "localname"): + d["localname"] = doc.localname + + xhiveframework.response.docs.append(d) + + +def set_local_name(doc): + def _set_local_name(d): + if doc.get("__islocal") or d.get("__islocal"): + d.localname = d.name + d.name = None + + _set_local_name(doc) + for child in doc.get_all_children(): + _set_local_name(child) + + if doc.get("__newname"): + doc.name = doc.get("__newname") diff --git a/xhiveframework/desk/form/test_form.py b/xhiveframework/desk/form/test_form.py new file mode 100644 index 0000000..e59197f --- /dev/null +++ b/xhiveframework/desk/form/test_form.py @@ -0,0 +1,20 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.desk.form.linked_with import get_linked_docs, get_linked_doctypes +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestForm(XhiveFrameworkTestCase): + def test_linked_with(self): + results = get_linked_docs("Role", "System Manager", linkinfo=get_linked_doctypes("Role")) + self.assertTrue("User" in results) + self.assertTrue("DocType" in results) + + +if __name__ == "__main__": + import unittest + + xhiveframework.connect() + unittest.main() diff --git a/xhiveframework/desk/form/utils.py b/xhiveframework/desk/form/utils.py new file mode 100644 index 0000000..ee48665 --- /dev/null +++ b/xhiveframework/desk/form/utils.py @@ -0,0 +1,108 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +from typing import TYPE_CHECKING + +import xhiveframework +import xhiveframework.desk.form.load +import xhiveframework.desk.form.meta +from xhiveframework import _ +from xhiveframework.core.doctype.file.utils import extract_images_from_html +from xhiveframework.desk.form.document_follow import follow_document + +if TYPE_CHECKING: + from xhiveframework.core.doctype.comment.comment import Comment + + +@xhiveframework.whitelist(methods=["DELETE", "POST"]) +def remove_attach(): + """remove attachment""" + fid = xhiveframework.form_dict.get("fid") + xhiveframework.delete_doc("File", fid) + + +@xhiveframework.whitelist(methods=["POST", "PUT"]) +def add_comment( + reference_doctype: str, reference_name: str, content: str, comment_email: str, comment_by: str +) -> "Comment": + """Allow logged user with permission to read document to add a comment""" + reference_doc = xhiveframework.get_doc(reference_doctype, reference_name) + reference_doc.check_permission() + + comment = xhiveframework.new_doc("Comment") + comment.update( + { + "comment_type": "Comment", + "reference_doctype": reference_doctype, + "reference_name": reference_name, + "comment_email": comment_email, + "comment_by": comment_by, + "content": extract_images_from_html(reference_doc, content, is_private=True), + } + ) + comment.insert(ignore_permissions=True) + + if xhiveframework.get_cached_value("User", xhiveframework.session.user, "follow_commented_documents"): + follow_document(comment.reference_doctype, comment.reference_name, xhiveframework.session.user) + + return comment + + +@xhiveframework.whitelist() +def update_comment(name, content): + """allow only owner to update comment""" + doc = xhiveframework.get_doc("Comment", name) + + if xhiveframework.session.user not in ["Administrator", doc.owner]: + xhiveframework.throw(_("Comment can only be edited by the owner"), xhiveframework.PermissionError) + + if doc.reference_doctype and doc.reference_name: + reference_doc = xhiveframework.get_doc(doc.reference_doctype, doc.reference_name) + reference_doc.check_permission() + + doc.content = extract_images_from_html(reference_doc, content, is_private=True) + else: + doc.content = content + + doc.save(ignore_permissions=True) + + +@xhiveframework.whitelist() +def get_next(doctype, value, prev, filters=None, sort_order="desc", sort_field="modified"): + prev = int(prev) + if not filters: + filters = [] + if isinstance(filters, str): + filters = json.loads(filters) + + # # condition based on sort order + condition = ">" if sort_order.lower() == "asc" else "<" + + # switch the condition + if prev: + sort_order = "asc" if sort_order.lower() == "desc" else "desc" + condition = "<" if condition == ">" else ">" + + # # add condition for next or prev item + filters.append([doctype, sort_field, condition, xhiveframework.get_value(doctype, value, sort_field)]) + + res = xhiveframework.get_list( + doctype, + fields=["name"], + filters=filters, + order_by=f"`tab{doctype}`.{sort_field}" + " " + sort_order, + limit_start=0, + limit_page_length=1, + as_list=True, + ) + + if not res: + xhiveframework.msgprint(_("No further records")) + return None + else: + return res[0][0] + + +def get_pdf_link(doctype, docname, print_format="Standard", no_letterhead=0): + return f"/api/method/xhiveframework.utils.print_format.download_pdf?doctype={doctype}&name={docname}&format={print_format}&no_letterhead={no_letterhead}" diff --git a/xhiveframework/desk/gantt.py b/xhiveframework/desk/gantt.py new file mode 100644 index 0000000..f368ce1 --- /dev/null +++ b/xhiveframework/desk/gantt.py @@ -0,0 +1,17 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework + + +@xhiveframework.whitelist() +def update_task(args, field_map): + """Updates Doc (called via gantt) based on passed `field_map`""" + args = xhiveframework._dict(json.loads(args)) + field_map = xhiveframework._dict(json.loads(field_map)) + d = xhiveframework.get_doc(args.doctype, args.name) + d.set(field_map.start, args.start) + d.set(field_map.end, args.end) + d.save() diff --git a/xhiveframework/desk/leaderboard.py b/xhiveframework/desk/leaderboard.py new file mode 100644 index 0000000..5c29e4e --- /dev/null +++ b/xhiveframework/desk/leaderboard.py @@ -0,0 +1,50 @@ +import xhiveframework +from xhiveframework.utils import get_fullname + + +def get_leaderboards(): + return { + "User": { + "fields": ["points"], + "method": "xhiveframework.desk.leaderboard.get_energy_point_leaderboard", + "company_disabled": 1, + "icon": "users", + } + } + + +@xhiveframework.whitelist() +def get_energy_point_leaderboard(date_range, company=None, field=None, limit=None): + users = xhiveframework.get_list( + "User", + filters={ + "name": ["not in", ["Administrator", "Guest"]], + "enabled": 1, + "user_type": ["!=", "Website User"], + }, + pluck="name", + ) + + filters = [["type", "!=", "Review"], ["user", "in", users]] + if date_range: + date_range = xhiveframework.parse_json(date_range) + filters.append(["creation", "between", [date_range[0], date_range[1]]]) + energy_point_users = xhiveframework.get_all( + "Energy Point Log", + fields=["user as name", "sum(points) as value"], + filters=filters, + group_by="user", + order_by="value desc", + ) + + energy_point_users_list = list(map(lambda x: x["name"], energy_point_users)) + for user in users: + if user not in energy_point_users_list: + energy_point_users.append({"name": user, "value": 0}) + + for user in energy_point_users: + user_id = user["name"] + user["name"] = get_fullname(user["name"]) + user["formatted_name"] = f'{get_fullname(user_id)}' + + return energy_point_users diff --git a/xhiveframework/desk/like.py b/xhiveframework/desk/like.py new file mode 100644 index 0000000..262b690 --- /dev/null +++ b/xhiveframework/desk/like.py @@ -0,0 +1,91 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +"""Allow adding of likes to documents""" + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.database.schema import add_column +from xhiveframework.desk.form.document_follow import follow_document +from xhiveframework.utils import get_link_to_form + + +@xhiveframework.whitelist() +def toggle_like(doctype, name, add=False): + """Adds / removes the current user in the `__liked_by` property of the given document. + If column does not exist, will add it in the database. + + The `_liked_by` property is always set from this function and is ignored if set via + Document API + + :param doctype: DocType of the document to like + :param name: Name of the document to like + :param add: `Yes` if like is to be added. If not `Yes` the like will be removed.""" + + _toggle_like(doctype, name, add) + + +def _toggle_like(doctype, name, add, user=None): + """Same as toggle_like but hides param `user` from API""" + + if not user: + user = xhiveframework.session.user + + try: + liked_by = xhiveframework.db.get_value(doctype, name, "_liked_by") + + if liked_by: + liked_by = json.loads(liked_by) + else: + liked_by = [] + + if add == "Yes": + if user not in liked_by: + liked_by.append(user) + add_comment(doctype, name) + if xhiveframework.get_cached_value("User", user, "follow_liked_documents"): + follow_document(doctype, name, user) + else: + if user in liked_by: + liked_by.remove(user) + remove_like(doctype, name) + + if xhiveframework.get_meta(doctype).issingle: + xhiveframework.db.set_single_value(doctype, "_liked_by", json.dumps(liked_by), update_modified=False) + else: + xhiveframework.db.set_value(doctype, name, "_liked_by", json.dumps(liked_by), update_modified=False) + + except xhiveframework.db.ProgrammingError as e: + if xhiveframework.db.is_column_missing(e): + add_column(doctype, "_liked_by", "Text") + _toggle_like(doctype, name, add, user) + else: + raise + + +def remove_like(doctype, name): + """Remove previous Like""" + # remove Comment + xhiveframework.delete_doc( + "Comment", + [ + c.name + for c in xhiveframework.get_all( + "Comment", + filters={ + "comment_type": "Like", + "reference_doctype": doctype, + "reference_name": name, + "owner": xhiveframework.session.user, + }, + ) + ], + ignore_permissions=True, + ) + + +def add_comment(doctype, name): + doc = xhiveframework.get_doc(doctype, name) + doc.add_comment("Like", _("Liked")) diff --git a/xhiveframework/desk/link_preview.py b/xhiveframework/desk/link_preview.py new file mode 100644 index 0000000..e891810 --- /dev/null +++ b/xhiveframework/desk/link_preview.py @@ -0,0 +1,52 @@ +import xhiveframework +from xhiveframework.model import no_value_fields, table_fields + + +@xhiveframework.whitelist() +def get_preview_data(doctype, docname): + preview_fields = [] + meta = xhiveframework.get_meta(doctype) + if not meta.show_preview_popup: + return + + preview_fields = [ + field.fieldname + for field in meta.fields + if field.in_preview and field.fieldtype not in no_value_fields and field.fieldtype not in table_fields + ] + + # no preview fields defined, build list from mandatory fields + if not preview_fields: + preview_fields = [ + field.fieldname for field in meta.fields if field.reqd and field.fieldtype not in table_fields + ] + + title_field = meta.get_title_field() + image_field = meta.image_field + + preview_fields.append(title_field) + preview_fields.append(image_field) + preview_fields.append("name") + + preview_data = xhiveframework.get_list(doctype, filters={"name": docname}, fields=preview_fields, limit=1) + + if not preview_data: + return + + preview_data = preview_data[0] + + formatted_preview_data = { + "preview_image": preview_data.get(image_field), + "preview_title": preview_data.get(title_field), + "name": preview_data.get("name"), + } + + for key, val in preview_data.items(): + if val and meta.has_field(key) and key not in [image_field, title_field, "name"]: + formatted_preview_data[meta.get_field(key).label] = xhiveframework.format( + val, + meta.get_field(key).fieldtype, + translated=True, + ) + + return formatted_preview_data diff --git a/xhiveframework/desk/listview.py b/xhiveframework/desk/listview.py new file mode 100644 index 0000000..211ceef --- /dev/null +++ b/xhiveframework/desk/listview.py @@ -0,0 +1,73 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model import is_default_field +from xhiveframework.query_builder import Order +from xhiveframework.query_builder.functions import Count +from xhiveframework.query_builder.terms import SubQuery +from xhiveframework.query_builder.utils import DocType + + +@xhiveframework.whitelist() +def get_list_settings(doctype): + try: + return xhiveframework.get_cached_doc("List View Settings", doctype) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_messages() + + +@xhiveframework.whitelist() +def set_list_settings(doctype, values): + try: + doc = xhiveframework.get_doc("List View Settings", doctype) + except xhiveframework.DoesNotExistError: + doc = xhiveframework.new_doc("List View Settings") + doc.name = doctype + xhiveframework.clear_messages() + doc.update(xhiveframework.parse_json(values)) + doc.save() + + +@xhiveframework.whitelist() +def get_group_by_count(doctype: str, current_filters: str, field: str) -> list[dict]: + current_filters = xhiveframework.parse_json(current_filters) + + if field == "assigned_to": + ToDo = DocType("ToDo") + User = DocType("User") + count = Count("*").as_("count") + filtered_records = xhiveframework.qb.get_query( + doctype, + filters=current_filters, + fields=["name"], + validate_filters=True, + ) + + return ( + xhiveframework.qb.from_(ToDo) + .from_(User) + .select(ToDo.allocated_to.as_("name"), count) + .where( + (ToDo.status != "Cancelled") + & (ToDo.allocated_to == User.name) + & (User.user_type == "System User") + & (ToDo.reference_name.isin(SubQuery(filtered_records))) + ) + .groupby(ToDo.allocated_to) + .orderby(count, order=Order.desc) + .limit(50) + .run(as_dict=True) + ) + + if not xhiveframework.get_meta(doctype).has_field(field) and not is_default_field(field): + raise ValueError("Field does not belong to doctype") + + return xhiveframework.get_list( + doctype, + filters=current_filters, + group_by=f"`tab{doctype}`.{field}", + fields=["count(*) as count", f"`{field}` as name"], + order_by="count desc", + limit=50, + ) diff --git a/xhiveframework/desk/notifications.py b/xhiveframework/desk/notifications.py new file mode 100644 index 0000000..459c118 --- /dev/null +++ b/xhiveframework/desk/notifications.py @@ -0,0 +1,406 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +from bs4 import BeautifulSoup + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.doctype.notification_log.notification_log import ( + enqueue_create_notification, + get_title, + get_title_html, +) +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + get_subscribed_documents, +) +from xhiveframework.utils import get_fullname + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_notifications(): + out = { + "open_count_doctype": {}, + "targets": {}, + } + if xhiveframework.flags.in_install or not xhiveframework.db.get_single_value("System Settings", "setup_complete"): + return out + + config = get_notification_config() + + if not config: + return out + + groups = list(config.get("for_doctype")) + list(config.get("for_module")) + + notification_count = {} + notification_percent = {} + + for name in groups: + count = xhiveframework.cache.hget("notification_count:" + name, xhiveframework.session.user) + if count is not None: + notification_count[name] = count + + out["open_count_doctype"] = get_notifications_for_doctypes(config, notification_count) + out["targets"] = get_notifications_for_targets(config, notification_percent) + + return out + + +def get_notifications_for_doctypes(config, notification_count): + """Notifications for DocTypes""" + can_read = xhiveframework.get_user().get_can_read() + open_count_doctype = {} + + for d in config.for_doctype: + if d in can_read: + condition = config.for_doctype[d] + + if d in notification_count: + open_count_doctype[d] = notification_count[d] + else: + try: + if isinstance(condition, dict): + result = xhiveframework.get_list( + d, fields=["count(*) as count"], filters=condition, ignore_ifnull=True + )[0].count + else: + result = xhiveframework.get_attr(condition)() + + except xhiveframework.PermissionError: + xhiveframework.clear_messages() + pass + # xhiveframework.msgprint("Permission Error in notifications for {0}".format(d)) + + except Exception as e: + # OperationalError: (1412, 'Table definition has changed, please retry transaction') + # InternalError: (1684, 'Table definition is being modified by concurrent DDL statement') + if e.args and e.args[0] not in (1412, 1684): + raise + + else: + open_count_doctype[d] = result + xhiveframework.cache.hset("notification_count:" + d, xhiveframework.session.user, result) + + return open_count_doctype + + +def get_notifications_for_targets(config, notification_percent): + """Notifications for doc targets""" + can_read = xhiveframework.get_user().get_can_read() + doc_target_percents = {} + + # doc_target_percents = { + # "Company": { + # "Acme": 87, + # "RobotsRUs": 50, + # }, {}... + # } + + for doctype in config.targets: + if doctype in can_read: + if doctype in notification_percent: + doc_target_percents[doctype] = notification_percent[doctype] + else: + doc_target_percents[doctype] = {} + d = config.targets[doctype] + condition = d["filters"] + target_field = d["target_field"] + value_field = d["value_field"] + try: + if isinstance(condition, dict): + doc_list = xhiveframework.get_list( + doctype, + fields=["name", target_field, value_field], + filters=condition, + limit_page_length=100, + ignore_ifnull=True, + ) + + except xhiveframework.PermissionError: + xhiveframework.clear_messages() + pass + except Exception as e: + if e.args[0] not in (1412, 1684): + raise + + else: + for doc in doc_list: + value = doc[value_field] + target = doc[target_field] + doc_target_percents[doctype][doc.name] = ( + (value / target * 100) if value < target else 100 + ) + + return doc_target_percents + + +def clear_notifications(user=None): + if xhiveframework.flags.in_install: + return + config = get_notification_config() + + if not config: + return + + for_doctype = list(config.get("for_doctype")) if config.get("for_doctype") else [] + for_module = list(config.get("for_module")) if config.get("for_module") else [] + groups = for_doctype + for_module + + for name in groups: + if user: + xhiveframework.cache.hdel("notification_count:" + name, user) + else: + xhiveframework.cache.delete_key("notification_count:" + name) + + +def clear_notification_config(user): + xhiveframework.cache.hdel("notification_config", user) + + +def delete_notification_count_for(doctype): + xhiveframework.cache.delete_key("notification_count:" + doctype) + + +def clear_doctype_notifications(doc, method=None, *args, **kwargs): + config = get_notification_config() + if not config: + return + if isinstance(doc, str): + doctype = doc # assuming doctype name was passed directly + else: + doctype = doc.doctype + + if doctype in config.for_doctype: + delete_notification_count_for(doctype) + return + + +@xhiveframework.whitelist() +def get_notification_info(): + config = get_notification_config() + out = get_notifications() + can_read = xhiveframework.get_user().get_can_read() + conditions = {} + module_doctypes = {} + doctype_info = dict(xhiveframework.db.sql("""select name, module from tabDocType""")) + + for d in list(set(can_read + list(config.for_doctype))): + if d in config.for_doctype: + conditions[d] = config.for_doctype[d] + + if d in doctype_info: + module_doctypes.setdefault(doctype_info[d], []).append(d) + + out.update( + { + "conditions": conditions, + "module_doctypes": module_doctypes, + } + ) + + return out + + +def get_notification_config(): + user = xhiveframework.session.user or "Guest" + + def _get(): + subscribed_documents = get_subscribed_documents() + config = xhiveframework._dict() + hooks = xhiveframework.get_hooks() + if hooks: + for notification_config in hooks.notification_config: + nc = xhiveframework.get_attr(notification_config)() + for key in ("for_doctype", "for_module", "for_other", "targets"): + config.setdefault(key, {}) + if key == "for_doctype": + if len(subscribed_documents) > 0: + key_config = nc.get(key, {}) + subscribed_docs_config = xhiveframework._dict() + for document in subscribed_documents: + if key_config.get(document): + subscribed_docs_config[document] = key_config.get(document) + config[key].update(subscribed_docs_config) + else: + config[key].update(nc.get(key, {})) + else: + config[key].update(nc.get(key, {})) + return config + + return xhiveframework.cache.hget("notification_config", user, _get) + + +def get_filters_for(doctype): + """get open filters for doctype""" + config = get_notification_config() + doctype_config = config.get("for_doctype").get(doctype, {}) + return None if isinstance(doctype_config, str) else doctype_config + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_open_count(doctype: str, name: str, items=None): + """Get count for internal and external links for given transactions + + :param doctype: Reference DocType + :param name: Reference Name + :param items: Optional list of transactions (json/dict)""" + + if xhiveframework.flags.in_migrate or xhiveframework.flags.in_install: + return {"count": []} + + doc = xhiveframework.get_doc(doctype, name) + doc.check_permission() + meta = doc.meta + links = meta.get_dashboard_data() + + # compile all items in a list + if items is None: + items = [] + for group in links.transactions: + items.extend(group.get("items")) + + if not isinstance(items, list): + items = json.loads(items) + + out = { + "external_links_found": [], + "internal_links_found": [], + } + + for d in items: + internal_link_for_doctype = links.get("internal_links", {}).get(d) or links.get( + "internal_and_external_links", {} + ).get(d) + if internal_link_for_doctype: + internal_links_data_for_d = get_internal_links(doc, internal_link_for_doctype, d) + if internal_links_data_for_d["count"]: + out["internal_links_found"].append(internal_links_data_for_d) + else: + try: + external_links_data_for_d = get_external_links(d, name, links) + out["external_links_found"].append(external_links_data_for_d) + except Exception: + out["external_links_found"].append({"doctype": d, "open_count": 0, "count": 0}) + else: + external_links_data_for_d = get_external_links(d, name, links) + out["external_links_found"].append(external_links_data_for_d) + + out = { + "count": out, + } + + if not meta.custom: + module = xhiveframework.get_meta_module(doctype) + if hasattr(module, "get_timeline_data"): + out["timeline_data"] = module.get_timeline_data(doctype, name) + + return out + + +def get_internal_links(doc, link, link_doctype): + names = [] + data = {"doctype": link_doctype} + + if isinstance(link, str): + # get internal links in parent document + value = doc.get(link) + if value and value not in names: + names.append(value) + elif isinstance(link, list): + # get internal links in child documents + table_fieldname, link_fieldname = link + for row in doc.get(table_fieldname) or []: + value = row.get(link_fieldname) + if value and value not in names: + names.append(value) + + data["open_count"] = 0 + data["count"] = len(names) + data["names"] = names + + return data + + +def get_external_links(doctype, name, links): + filters = get_filters_for(doctype) + fieldname = links.get("non_standard_fieldnames", {}).get(doctype, links.get("fieldname")) + data = {"doctype": doctype} + + if filters: + # get the fieldname for the current document + # we only need open documents related to the current document + filters[fieldname] = name + total = len( + xhiveframework.get_all( + doctype, fields="name", filters=filters, limit=100, distinct=True, ignore_ifnull=True + ) + ) + data["open_count"] = total + else: + data["open_count"] = 0 + + total = len( + xhiveframework.get_all( + doctype, fields="name", filters={fieldname: name}, limit=100, distinct=True, ignore_ifnull=True + ) + ) + data["count"] = total + + return data + + +def notify_mentions(ref_doctype, ref_name, content): + if ref_doctype and ref_name and content: + mentions = extract_mentions(content) + + if not mentions: + return + + sender_fullname = get_fullname(xhiveframework.session.user) + title = get_title(ref_doctype, ref_name) + + recipients = [ + xhiveframework.db.get_value( + "User", + {"enabled": 1, "name": name, "user_type": "System User", "allowed_in_mentions": 1}, + "email", + ) + for name in mentions + ] + + notification_message = _("""{0} mentioned you in a comment in {1} {2}""").format( + xhiveframework.bold(sender_fullname), xhiveframework.bold(ref_doctype), get_title_html(title) + ) + + notification_doc = { + "type": "Mention", + "document_type": ref_doctype, + "document_name": ref_name, + "subject": notification_message, + "from_user": xhiveframework.session.user, + "email_content": content, + } + + enqueue_create_notification(recipients, notification_doc) + + +def extract_mentions(txt): + """Find all instances of @mentions in the html.""" + soup = BeautifulSoup(txt, "html.parser") + emails = [] + for mention in soup.find_all(class_="mention"): + if mention.get("data-is-group") == "true": + try: + user_group = xhiveframework.get_cached_doc("User Group", mention["data-id"]) + emails += [d.user for d in user_group.user_group_members] + except xhiveframework.DoesNotExistError: + pass + continue + email = mention["data-id"] + emails.append(email) + + return emails diff --git a/xhiveframework/desk/page/__init__.py b/xhiveframework/desk/page/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/page/backups/__init__.py b/xhiveframework/desk/page/backups/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/page/backups/backups.css b/xhiveframework/desk/page/backups/backups.css new file mode 100644 index 0000000..32ccb88 --- /dev/null +++ b/xhiveframework/desk/page/backups/backups.css @@ -0,0 +1,14 @@ +.download-backups { + font-size: var(--text-base); +} + +.download-backup-card { + display: block; + text-decoration: none; + margin-bottom: var(--margin-lg); +} + +.download-backup-card:hover { + box-shadow: var(--shadow-md); + text-decoration: none; +} diff --git a/xhiveframework/desk/page/backups/backups.html b/xhiveframework/desk/page/backups/backups.html new file mode 100644 index 0000000..57ad061 --- /dev/null +++ b/xhiveframework/desk/page/backups/backups.html @@ -0,0 +1,27 @@ + + diff --git a/xhiveframework/desk/page/backups/backups.js b/xhiveframework/desk/page/backups/backups.js new file mode 100644 index 0000000..ac63662 --- /dev/null +++ b/xhiveframework/desk/page/backups/backups.js @@ -0,0 +1,45 @@ +xhiveframework.pages["backups"].on_page_load = function (wrapper) { + var page = xhiveframework.ui.make_app_page({ + parent: wrapper, + title: __("Download Backups"), + single_column: true, + }); + + page.add_inner_button(__("Set Number of Backups"), function () { + xhiveframework.set_route("Form", "System Settings"); + }); + + page.add_inner_button(__("Download Files Backup"), function () { + xhiveframework.call({ + method: "xhiveframework.desk.page.backups.backups.schedule_files_backup", + args: { user_email: xhiveframework.session.user_email }, + }); + }); + + page.add_inner_button(__("Get Backup Encryption Key"), function () { + if (xhiveframework.user.has_role("System Manager")) { + xhiveframework.verify_password(function () { + xhiveframework.call({ + method: "xhiveframework.utils.backups.get_backup_encryption_key", + callback: function (r) { + xhiveframework.msgprint({ + title: __("Backup Encryption Key"), + message: __(r.message), + indicator: "blue", + }); + }, + }); + }); + } else { + xhiveframework.msgprint({ + title: __("Error"), + message: __("System Manager privileges required."), + indicator: "red", + }); + } + }); + + xhiveframework.breadcrumbs.add("Setup"); + + $(xhiveframework.render_template("backups")).appendTo(page.body.addClass("no-border")); +}; diff --git a/xhiveframework/desk/page/backups/backups.json b/xhiveframework/desk/page/backups/backups.json new file mode 100644 index 0000000..dd6e8d9 --- /dev/null +++ b/xhiveframework/desk/page/backups/backups.json @@ -0,0 +1,21 @@ +{ + "content": null, + "creation": "2015-09-24 01:26:06.225378", + "docstatus": 0, + "doctype": "Page", + "modified": "2015-09-24 01:26:06.225378", + "modified_by": "Administrator", + "module": "Desk", + "name": "backups", + "owner": "Administrator", + "page_name": "backups", + "roles": [ + { + "role": "System Manager" + } + ], + "script": null, + "standard": "Yes", + "style": null, + "title": "Download Backups" +} \ No newline at end of file diff --git a/xhiveframework/desk/page/backups/backups.py b/xhiveframework/desk/page/backups/backups.py new file mode 100644 index 0000000..f3254ff --- /dev/null +++ b/xhiveframework/desk/page/backups/backups.py @@ -0,0 +1,120 @@ +import datetime +import os + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils import cint, get_site_path, get_url +from xhiveframework.utils.data import convert_utc_to_system_timezone + + +def get_context(context): + def get_time(path): + dt = os.path.getmtime(path) + return convert_utc_to_system_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime( + "%a %b %d %H:%M %Y" + ) + + def get_encrytion_status(path): + if "-enc" in path: + return True + + def get_size(path): + size = os.path.getsize(path) + if size > 1048576: + return f"{float(size) / 1048576:.1f}M" + else: + return f"{float(size) / 1024:.1f}K" + + path = get_site_path("private", "backups") + files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))] + backup_limit = get_scheduled_backup_limit() + + if len(files) > backup_limit: + cleanup_old_backups(path, files, backup_limit) + + files = [ + ( + "/backups/" + _file, + get_time(os.path.join(path, _file)), + get_encrytion_status(os.path.join(path, _file)), + get_size(os.path.join(path, _file)), + ) + for _file in files + if _file.endswith("sql.gz") + ] + files.sort(key=lambda x: x[1], reverse=True) + + return {"files": files[:backup_limit]} + + +def get_scheduled_backup_limit(): + backup_limit = xhiveframework.db.get_singles_value("System Settings", "backup_limit") + return cint(backup_limit) + + +def cleanup_old_backups(site_path, files, limit): + backup_paths = [] + for f in files: + if f.endswith("sql.gz"): + _path = os.path.abspath(os.path.join(site_path, f)) + backup_paths.append(_path) + + backup_paths = sorted(backup_paths, key=os.path.getctime) + files_to_delete = len(backup_paths) - limit + + for idx in range(0, files_to_delete): + f = os.path.basename(backup_paths[idx]) + files.remove(f) + + os.remove(backup_paths[idx]) + + +def delete_downloadable_backups(): + path = get_site_path("private", "backups") + files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))] + backup_limit = get_scheduled_backup_limit() + + if len(files) > backup_limit: + cleanup_old_backups(path, files, backup_limit) + + +@xhiveframework.whitelist() +def schedule_files_backup(user_email): + from xhiveframework.utils.background_jobs import enqueue, get_jobs + + xhiveframework.only_for("System Manager") + + queued_jobs = get_jobs(site=xhiveframework.local.site, queue="long") + method = "xhiveframework.desk.page.backups.backups.backup_files_and_notify_user" + + if method not in queued_jobs[xhiveframework.local.site]: + enqueue( + "xhiveframework.desk.page.backups.backups.backup_files_and_notify_user", + queue="long", + user_email=user_email, + ) + xhiveframework.msgprint(_("Queued for backup. You will receive an email with the download link")) + else: + xhiveframework.msgprint(_("Backup job is already queued. You will receive an email with the download link")) + + +def backup_files_and_notify_user(user_email=None): + from xhiveframework.utils.backups import backup + + backup_files = backup(with_files=True) + get_downloadable_links(backup_files) + + subject = _("File backup is ready") + xhiveframework.sendmail( + recipients=[user_email], + subject=subject, + template="file_backup_notification", + args=backup_files, + header=[subject, "green"], + ) + + +def get_downloadable_links(backup_files): + for key in ["backup_path_files", "backup_path_private_files"]: + path = backup_files[key] + backup_files[key] = get_url("/".join(path.split("/")[-2:])) diff --git a/xhiveframework/desk/page/leaderboard/__init__.py b/xhiveframework/desk/page/leaderboard/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/page/leaderboard/leaderboard.css b/xhiveframework/desk/page/leaderboard/leaderboard.css new file mode 100644 index 0000000..d15b4ff --- /dev/null +++ b/xhiveframework/desk/page/leaderboard/leaderboard.css @@ -0,0 +1,85 @@ +.list-filters { + overflow-y: hidden; + padding: 5px +} + +.list-filter-item { + min-width: 150px; + float: left; + margin: 5px; +} + +.list-item_content { + flex: 1; + padding-right: 15px; + align-items: center; +} + +.select-time, .select-doctype, .select-filter, .select-sort { + background: #f0f4f7; +} + +.from-date-field .clearfix{ + display: none; +} + +.from-date-field { + margin-left: 10px; +} + +.select-time:focus, .select-doctype:focus, .select-filter:focus, .select-sort:focus { + background: #f0f4f7; +} + +.header-btn-base { + border: none; + outline: 0; + vertical-align: middle; + overflow: hidden; + text-decoration: none; + color: inherit; + background-color: inherit; + cursor: pointer; + white-space: nowrap; +} + +.header-btn-round { + border-radius: 4px; +} + +.item-title-bold { + font-weight: bold; +} + +.rank { + max-width: 100px; +} + +.leaderboard .result { + border-top: 1px solid var(--border-color); +} + +.leaderboard .list-item { + padding-left: 45px; +} + +.leaderboard .list-item_content { + padding-right: 60px; +} + +.leaderboard-sidebar { + padding-left: 0; + position: fixed; +} + +.leaderboard-list { + padding: var(-padding-sm) 0; + min-height: 70vh; +} + +.leaderboard-empty-state { + align-items: center; + height: 70vh; + justify-content: center; + display: flex; +} diff --git a/xhiveframework/desk/page/leaderboard/leaderboard.js b/xhiveframework/desk/page/leaderboard/leaderboard.js new file mode 100644 index 0000000..9fbf2eb --- /dev/null +++ b/xhiveframework/desk/page/leaderboard/leaderboard.js @@ -0,0 +1,409 @@ +xhiveframework.pages["leaderboard"].on_page_load = (wrapper) => { + xhiveframework.leaderboard = new Leaderboard(wrapper); + + $(wrapper).bind("show", () => { + // Get which leaderboard to show + let doctype = xhiveframework.get_route()[1]; + xhiveframework.leaderboard.show_leaderboard(doctype); + }); +}; + +class Leaderboard { + constructor(parent) { + xhiveframework.ui.make_app_page({ + parent: parent, + title: __("Leaderboard"), + single_column: false, + card_layout: true, + }); + + this.parent = parent; + this.page = this.parent.page; + this.page.sidebar.html( + `
      ` + ); + this.$sidebar_list = this.page.sidebar.find("ul"); + + this.get_leaderboard_config(); + } + + get_leaderboard_config() { + this.doctypes = []; + this.filters = {}; + this.leaderboard_limit = 20; + + xhiveframework + .xcall("xhiveframework.desk.page.leaderboard.leaderboard.get_leaderboard_config") + .then((config) => { + this.leaderboard_config = config; + for (let doctype in this.leaderboard_config) { + this.doctypes.push(doctype); + this.filters[doctype] = this.leaderboard_config[doctype].fields.map( + (field) => { + if (typeof field === "object") { + return field.label || field.fieldname; + } + return field; + } + ); + } + + // For translation. Do not remove this + // __("This Week"), __("This Month"), __("This Quarter"), __("This Year"), + // __("Last Week"), __("Last Month"), __("Last Quarter"), __("Last Year"), + // __("All Time"), __("Select From Date") + this.timespans = [ + "This Week", + "This Month", + "This Quarter", + "This Year", + "Last Week", + "Last Month", + "Last Quarter", + "Last Year", + "All Time", + "Select Date Range", + ]; + + // for saving current selected filters + const _initial_doctype = xhiveframework.get_route()[1] || this.doctypes[0]; + const _initial_timespan = this.timespans[0]; + const _initial_filter = this.filters[_initial_doctype]; + + this.options = { + selected_doctype: _initial_doctype, + selected_filter: _initial_filter, + selected_filter_item: _initial_filter[0], + selected_timespan: _initial_timespan, + }; + + this.message = null; + this.make(); + }); + } + + make() { + this.$container = $(`
      +
      +
      +
      `).appendTo(this.page.main); + + this.$graph_area = this.$container.find(".leaderboard-graph"); + + this.doctypes.map((doctype) => { + const icon = this.leaderboard_config[doctype].icon; + this.get_sidebar_item(doctype, icon).appendTo(this.$sidebar_list); + }); + + this.setup_leaderboard_fields(); + + this.render_selected_doctype(); + + this.render_search_box(); + + // Get which leaderboard to show + let doctype = xhiveframework.get_route()[1]; + this.show_leaderboard(doctype); + } + + setup_leaderboard_fields() { + this.company_select = this.page.add_field({ + fieldname: "company", + label: __("Company"), + fieldtype: "Link", + options: "Company", + default: xhiveframework.defaults.get_default("company"), + reqd: 1, + change: (e) => { + this.make_request(); + }, + }); + + this.timespan_select = this.page.add_select( + __("Timespan"), + this.timespans.map((d) => { + return { label: __(d), value: d }; + }) + ); + this.create_date_range_field(); + + this.type_select = this.page.add_select( + __("Field"), + this.options.selected_filter.map((d) => { + return { label: __(xhiveframework.model.unscrub(d)), value: d }; + }) + ); + + this.timespan_select.on("change", (e) => { + this.options.selected_timespan = e.currentTarget.value; + if (this.options.selected_timespan === "Select Date Range") { + this.date_range_field.show(); + } else { + this.date_range_field.hide(); + } + this.make_request(); + }); + + this.type_select.on("change", (e) => { + this.options.selected_filter_item = e.currentTarget.value; + this.make_request(); + }); + } + + create_date_range_field() { + let timespan_field = $(this.parent).find( + `.xhiveframework-control[data-original-title="${__("Timespan")}"]` + ); + this.date_range_field = $(`
      `) + .insertAfter(timespan_field) + .hide(); + + let date_field = xhiveframework.ui.form.make_control({ + df: { + fieldtype: "DateRange", + fieldname: "selected_date_range", + placeholder: __("Date Range"), + default: [xhiveframework.datetime.month_start(), xhiveframework.datetime.now_date()], + input_class: "input-xs", + reqd: 1, + change: () => { + this.selected_date_range = date_field.get_value(); + if (this.selected_date_range) this.make_request(); + }, + }, + parent: $(this.parent).find(".from-date-field"), + render_input: 1, + }); + } + + render_selected_doctype() { + this.$sidebar_list.on("click", "li", (e) => { + let $li = $(e.currentTarget); + let doctype = $li.find(".doctype-text").attr("doctype-value"); + + this.company_select.set_value( + xhiveframework.defaults.get_default("company") || this.company_select.get_value() + ); + this.options.selected_doctype = doctype; + this.options.selected_filter = this.filters[doctype]; + this.options.selected_filter_item = this.filters[doctype][0]; + + this.type_select.empty().add_options( + this.options.selected_filter.map((d) => { + return { label: __(xhiveframework.model.unscrub(d)), value: d }; + }) + ); + if (this.leaderboard_config[this.options.selected_doctype].company_disabled) { + $(this.parent).find("[data-original-title=Company]").hide(); + } else { + $(this.parent).find("[data-original-title=Company]").show(); + } + + this.$sidebar_list.find("li").removeClass("active selected"); + $li.addClass("active selected"); + + xhiveframework.set_route("leaderboard", this.options.selected_doctype); + this.make_request(); + }); + } + + render_search_box() { + this.$search_box = $(``); + + $(this.parent).find(".page-form").append(this.$search_box); + } + + show_leaderboard(doctype) { + if (this.doctypes.length) { + if (this.doctypes.includes(doctype)) { + this.options.selected_doctype = doctype; + this.$sidebar_list + .find(`[doctype-value = "${this.options.selected_doctype}"]`) + .trigger("click"); + } + + this.$search_box.find(".leaderboard-search-input").val(""); + xhiveframework.set_route("leaderboard", this.options.selected_doctype); + } + } + + make_request() { + xhiveframework.model.with_doctype(this.options.selected_doctype, () => { + this.get_leaderboard(this.get_leaderboard_data); + }); + } + + get_leaderboard(notify) { + let company = this.company_select.get_value(); + if (!company && !this.leaderboard_config[this.options.selected_doctype].company_disabled) { + notify(this, null); + xhiveframework.show_alert(__("Please select Company")); + return; + } + xhiveframework + .call(this.leaderboard_config[this.options.selected_doctype].method, { + date_range: this.get_date_range(), + company: company, + field: this.options.selected_filter_item, + limit: this.leaderboard_limit, + }) + .then((r) => { + let results = r.message || []; + + let graph_items = results.slice(0, 10); + + this.$graph_area.show().empty(); + + const custom_options = { + data: { + datasets: [{ values: graph_items.map((d) => d.value) }], + labels: graph_items.map((d) => d.name), + }, + format_tooltip_x: (d) => d[this.options.selected_filter_item], + height: 140, + }; + xhiveframework.utils.make_chart(".leaderboard-graph", custom_options); + + notify(this, r); + }); + } + + get_leaderboard_data(me, res) { + if (res && res.message.length) { + me.message = null; + me.$container.find(".leaderboard-list").html(me.render_list_view(res.message)); + xhiveframework.utils.setup_search($(me.parent), ".list-item-container", ".list-id"); + } else { + me.$graph_area.hide(); + me.message = __("No Items Found"); + me.$container.find(".leaderboard-list").html(me.render_list_view()); + } + } + + render_list_view(items = []) { + var html = `${this.render_message()} +
      + ${this.render_result(items)} +
      `; + + return $(html); + } + + render_result(items) { + var html = `${this.render_list_header()} + ${this.render_list_result(items)}`; + return html; + } + + render_list_header() { + const _selected_filter = this.options.selected_filter.map((i) => xhiveframework.model.unscrub(i)); + const fields = ["rank", "name", this.options.selected_filter_item]; + const filters = fields + .map((filter) => { + const col = __(xhiveframework.model.unscrub(filter)); + return `
      + + ${col} + +
      `; + }) + .join(""); + + return `
      +
      ${filters}
      +
      `; + } + + render_list_result(items) { + let _html = items + .map((item, index) => { + const $value = $(this.get_item_html(item, index + 1)); + const $item_container = $(`
      `).append($value); + return $item_container[0].outerHTML; + }) + .join(""); + + return `
      +
      + ${_html} +
      +
      `; + } + + render_message() { + const display_class = this.message ? "" : "hide"; + return `
      +
      + Empty State +
      ${this.message}
      +
      +
      `; + } + + get_item_html(item, index) { + const fields = this.leaderboard_config[this.options.selected_doctype].fields; + const value = xhiveframework.format( + item.value, + fields.find((field) => { + let fieldname = field.fieldname || field; + return fieldname === this.options.selected_filter_item; + }) + ); + + const link = `/app/${xhiveframework.router.slug(this.options.selected_doctype)}/${item.name}`; + const name_html = item.formatted_name + ? `${item.formatted_name}` + : ` ${item.name} `; + return `
      +
      + ${index} +
      +
      + ${name_html} +
      +
      + ${value} +
      +
      `; + } + + get_sidebar_item(item, icon) { + let icon_html = icon ? xhiveframework.utils.icon(icon, "md") : ""; + return $(`
    • + ${icon_html} + + ${__(item)} + +
    • `); + } + + get_date_range() { + let timespan = this.options.selected_timespan.toLowerCase(); + let current_date = xhiveframework.datetime.now_date(); + let date_range_map = { + "this week": [xhiveframework.datetime.week_start(), xhiveframework.datetime.week_end()], + "this month": [xhiveframework.datetime.month_start(), xhiveframework.datetime.month_end()], + "this quarter": [xhiveframework.datetime.quarter_start(), xhiveframework.datetime.quarter_end()], + "this year": [xhiveframework.datetime.year_start(), xhiveframework.datetime.year_end()], + "last week": [xhiveframework.datetime.add_days(current_date, -7), current_date], + "last month": [xhiveframework.datetime.add_months(current_date, -1), current_date], + "last quarter": [xhiveframework.datetime.add_months(current_date, -3), current_date], + "last year": [xhiveframework.datetime.add_months(current_date, -12), current_date], + "all time": null, + "select date range": this.selected_date_range || [ + xhiveframework.datetime.month_start(), + current_date, + ], + }; + return date_range_map[timespan]; + } +} diff --git a/xhiveframework/desk/page/leaderboard/leaderboard.json b/xhiveframework/desk/page/leaderboard/leaderboard.json new file mode 100644 index 0000000..0f0b8d8 --- /dev/null +++ b/xhiveframework/desk/page/leaderboard/leaderboard.json @@ -0,0 +1,19 @@ +{ + "content": null, + "creation": "2017-06-06 02:54:24.785360", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2019-09-27 17:44:51.909947", + "modified_by": "Administrator", + "module": "Desk", + "name": "leaderboard", + "owner": "Administrator", + "page_name": "leaderboard", + "roles": [], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0, + "title": "Leaderboard" +} \ No newline at end of file diff --git a/xhiveframework/desk/page/leaderboard/leaderboard.py b/xhiveframework/desk/page/leaderboard/leaderboard.py new file mode 100644 index 0000000..71ec4d8 --- /dev/null +++ b/xhiveframework/desk/page/leaderboard/leaderboard.py @@ -0,0 +1,13 @@ +# Copyright (c) 2017, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework + + +@xhiveframework.whitelist() +def get_leaderboard_config(): + leaderboard_config = xhiveframework._dict() + leaderboard_hooks = xhiveframework.get_hooks("leaderboards") + for hook in leaderboard_hooks: + leaderboard_config.update(xhiveframework.get_attr(hook)()) + + return leaderboard_config diff --git a/xhiveframework/desk/page/setup_wizard/__init__.py b/xhiveframework/desk/page/setup_wizard/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/page/setup_wizard/install_fixtures.py b/xhiveframework/desk/page/setup_wizard/install_fixtures.py new file mode 100644 index 0000000..2d47213 --- /dev/null +++ b/xhiveframework/desk/page/setup_wizard/install_fixtures.py @@ -0,0 +1,64 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.desk.doctype.global_search_settings.global_search_settings import ( + update_global_search_doctypes, +) +from xhiveframework.utils.dashboard import sync_dashboards + + +def install(): + update_genders() + update_salutations() + update_global_search_doctypes() + setup_email_linking() + sync_dashboards() + add_unsubscribe() + + +def update_genders(): + default_genders = [ + "Male", + "Female", + "Other", + "Transgender", + "Genderqueer", + "Non-Conforming", + "Prefer not to say", + ] + records = [{"doctype": "Gender", "gender": d} for d in default_genders] + for record in records: + xhiveframework.get_doc(record).insert(ignore_permissions=True, ignore_if_duplicate=True) + + +def update_salutations(): + default_salutations = ["Mr", "Ms", "Mx", "Dr", "Mrs", "Madam", "Miss", "Master", "Prof"] + records = [{"doctype": "Salutation", "salutation": d} for d in default_salutations] + for record in records: + doc = xhiveframework.new_doc(record.get("doctype")) + doc.update(record) + doc.insert(ignore_permissions=True, ignore_if_duplicate=True) + + +def setup_email_linking(): + doc = xhiveframework.get_doc( + { + "doctype": "Email Account", + "email_id": "email_linking@example.com", + } + ) + doc.insert(ignore_permissions=True, ignore_if_duplicate=True) + + +def add_unsubscribe(): + email_unsubscribe = [ + {"email": "admin@example.com", "global_unsubscribe": 1}, + {"email": "guest@example.com", "global_unsubscribe": 1}, + ] + + for unsubscribe in email_unsubscribe: + if not xhiveframework.get_all("Email Unsubscribe", filters=unsubscribe): + doc = xhiveframework.new_doc("Email Unsubscribe") + doc.update(unsubscribe) + doc.insert(ignore_permissions=True) diff --git a/xhiveframework/desk/page/setup_wizard/setup_wizard.js b/xhiveframework/desk/page/setup_wizard/setup_wizard.js new file mode 100644 index 0000000..0087724 --- /dev/null +++ b/xhiveframework/desk/page/setup_wizard/setup_wizard.js @@ -0,0 +1,674 @@ +xhiveframework.provide("xhiveframework.setup"); +xhiveframework.provide("xhiveframework.setup.events"); +xhiveframework.provide("xhiveframework.ui"); + +xhiveframework.setup = { + slides: [], + events: {}, + data: {}, + utils: {}, + domains: [], + + on: function (event, fn) { + if (!xhiveframework.setup.events[event]) { + xhiveframework.setup.events[event] = []; + } + xhiveframework.setup.events[event].push(fn); + }, + add_slide: function (slide) { + xhiveframework.setup.slides.push(slide); + }, + + remove_slide: function (slide_name) { + xhiveframework.setup.slides = xhiveframework.setup.slides.filter((slide) => slide.name !== slide_name); + }, + + run_event: function (event) { + $.each(xhiveframework.setup.events[event] || [], function (i, fn) { + fn(); + }); + }, +}; + +xhiveframework.pages["setup-wizard"].on_page_load = function (wrapper) { + if (xhiveframework.boot.setup_complete) { + window.location.href = "/app"; + } + let requires = xhiveframework.boot.setup_wizard_requires || []; + xhiveframework.require(requires, function () { + xhiveframework.call({ + method: "xhiveframework.desk.page.setup_wizard.setup_wizard.load_languages", + freeze: true, + callback: function (r) { + xhiveframework.setup.data.lang = r.message; + + xhiveframework.setup.run_event("before_load"); + var wizard_settings = { + parent: wrapper, + slides: xhiveframework.setup.slides, + slide_class: xhiveframework.setup.SetupWizardSlide, + unidirectional: 1, + done_state: 1, + }; + xhiveframework.wizard = new xhiveframework.setup.SetupWizard(wizard_settings); + xhiveframework.setup.run_event("after_load"); + xhiveframework.wizard.show_slide(cint(xhiveframework.get_route()[1])); + }, + }); + }); +}; + +xhiveframework.pages["setup-wizard"].on_page_show = function () { + xhiveframework.wizard && xhiveframework.wizard.show_slide(cint(xhiveframework.get_route()[1])); +}; + +xhiveframework.setup.on("before_load", function () { + // load slides + xhiveframework.setup.slides_settings.forEach((s) => { + if (!(s.name === "user" && xhiveframework.boot.developer_mode)) { + // if not user slide with developer mode + xhiveframework.setup.add_slide(s); + } + }); +}); + +xhiveframework.setup.SetupWizard = class SetupWizard extends xhiveframework.ui.Slides { + constructor(args = {}) { + super(args); + $.extend(this, args); + + this.page_name = "setup-wizard"; + this.welcomed = true; + xhiveframework.set_route("setup-wizard/0"); + } + + make() { + super.make(); + this.container.addClass("container setup-wizard-slide with-form"); + this.$next_btn.addClass("action"); + this.$complete_btn.addClass("action"); + this.setup_keyboard_nav(); + } + + setup_keyboard_nav() { + $("body").on("keydown", this.handle_enter_press.bind(this)); + } + + disable_keyboard_nav() { + $("body").off("keydown", this.handle_enter_press.bind(this)); + } + + handle_enter_press(e) { + if (e.which === xhiveframework.ui.keyCode.ENTER) { + let $target = $(e.target); + if ($target.hasClass("prev-btn") || $target.hasClass("next-btn")) { + $target.trigger("click"); + } else { + // hitting enter on autocomplete field shouldn't trigger next slide. + if ($target.data().fieldtype == "Autocomplete") return; + + this.container.find(".next-btn").trigger("click"); + e.preventDefault(); + } + } + } + + before_show_slide() { + if (!this.welcomed) { + xhiveframework.set_route(this.page_name); + return false; + } + return true; + } + + show_slide(id) { + if (id === this.slides.length) { + return; + } + super.show_slide(id); + xhiveframework.set_route(this.page_name, cstr(id)); + } + + show_hide_prev_next(id) { + super.show_hide_prev_next(id); + if (id + 1 === this.slides.length) { + this.$next_btn.removeClass("btn-primary").hide(); + this.$complete_btn + .addClass("btn-primary") + .show() + .on("click", () => this.action_on_complete()); + } else { + this.$next_btn.addClass("btn-primary").show(); + this.$complete_btn.removeClass("btn-primary").hide(); + } + } + + refresh_slides() { + // For Translations, etc. + if (this.in_refresh_slides || !this.current_slide.set_values(true)) { + return; + } + this.in_refresh_slides = true; + + this.update_values(); + xhiveframework.setup.slides = []; + xhiveframework.setup.run_event("before_load"); + + xhiveframework.setup.slides = this.get_setup_slides_filtered_by_domain(); + + this.slides = xhiveframework.setup.slides; + xhiveframework.setup.run_event("after_load"); + + // re-render all slide, only remake made slides + $.each(this.slide_dict, (id, slide) => { + if (slide.made) { + this.made_slide_ids.push(id); + } + }); + this.made_slide_ids.push(this.current_id); + this.setup(); + + this.show_slide(this.current_id); + this.refresh(this.current_id); + setTimeout(() => { + this.container.find(".form-control").first().focus(); + }, 200); + this.in_refresh_slides = false; + } + + action_on_complete() { + xhiveframework.telemetry.capture("initated_client_side", "setup"); + if (!this.current_slide.set_values()) return; + this.update_values(); + this.show_working_state(); + this.disable_keyboard_nav(); + this.listen_for_setup_stages(); + + return xhiveframework.call({ + method: "xhiveframework.desk.page.setup_wizard.setup_wizard.setup_complete", + args: { args: this.values }, + callback: (r) => { + if (r.message.status === "ok") { + this.post_setup_success(); + } else if (r.message.status === "registered") { + this.update_setup_message(__("starting the setup...")); + } else if (r.message.fail !== undefined) { + this.abort_setup(r.message.fail); + } + }, + error: () => this.abort_setup(), + }); + } + + post_setup_success() { + this.set_setup_complete_message(__("Setup Complete"), __("Refreshing...")); + if (xhiveframework.setup.welcome_page) { + localStorage.setItem("session_last_route", xhiveframework.setup.welcome_page); + } + setTimeout(function () { + // Reload + window.location.href = "/app"; + }, 2000); + } + + abort_setup(fail_msg) { + this.$working_state.find(".state-icon-container").html(""); + fail_msg = fail_msg + ? fail_msg + : xhiveframework.last_response.setup_wizard_failure_message + ? xhiveframework.last_response.setup_wizard_failure_message + : __("Failed to complete setup"); + + this.update_setup_message("Could not start up: " + fail_msg); + + this.$working_state.find(".title").html("Setup failed"); + + this.$abort_btn.show(); + } + + listen_for_setup_stages() { + xhiveframework.realtime.on("setup_task", (data) => { + // console.log('data', data); + if (data.stage_status) { + // .html('Process '+ data.progress[0] + ' of ' + data.progress[1] + ': ' + data.stage_status); + this.update_setup_message(data.stage_status); + this.set_setup_load_percent(((data.progress[0] + 1) / data.progress[1]) * 100); + } + if (data.fail_msg) { + this.abort_setup(data.fail_msg); + } + if (data.status === "ok") { + this.post_setup_success(); + } + }); + } + + update_setup_message(message) { + this.$working_state.find(".setup-message").html(message); + } + + get_setup_slides_filtered_by_domain() { + let filtered_slides = []; + xhiveframework.setup.slides.forEach(function (slide) { + if (xhiveframework.setup.domains) { + let active_domains = xhiveframework.setup.domains; + if ( + !slide.domains || + slide.domains.filter((d) => active_domains.includes(d)).length > 0 + ) { + filtered_slides.push(slide); + } + } else { + filtered_slides.push(slide); + } + }); + return filtered_slides; + } + + show_working_state() { + this.container.hide(); + xhiveframework.set_route(this.page_name); + + this.$working_state = this.get_message( + __("Setting up your system"), + __("Starting XhiveFramework ...") + ).appendTo(this.parent); + + this.attach_abort_button(); + + this.current_id = this.slides.length; + this.current_slide = null; + } + + attach_abort_button() { + this.$abort_btn = $( + `` + ); + this.$working_state.find(".content").append(this.$abort_btn); + + this.$abort_btn.on("click", () => { + $(this.parent).find(".setup-in-progress").remove(); + this.container.show(); + xhiveframework.set_route(this.page_name, this.slides.length - 1); + }); + + this.$abort_btn.hide(); + } + + get_message(title, message = "") { + const loading_html = `
      +
      +
      +
      +
      `; + + return $(`
      +
      +

      ${title}

      +
      ${loading_html}
      +

      ${message}

      +
      +
      `); + } + + set_setup_complete_message(title, message) { + this.$working_state.find(".title").html(title); + this.$working_state.find(".setup-message").html(message); + } + + set_setup_load_percent(percent) { + this.$working_state.find(".progress-bar").css({ width: percent + "%" }); + } +}; + +xhiveframework.setup.SetupWizardSlide = class SetupWizardSlide extends xhiveframework.ui.Slide { + constructor(slide = null) { + super(slide); + } + + make() { + super.make(); + this.set_init_values(); + this.setup_telemetry_events(); + this.reset_action_button_state(); + } + + set_init_values() { + let me = this; + // set values from xhiveframework.setup.values + if (xhiveframework.wizard.values && this.fields) { + this.fields.forEach(function (f) { + var value = xhiveframework.wizard.values[f.fieldname]; + if (value) { + me.get_field(f.fieldname).set_input(value); + } + }); + } + } + + setup_telemetry_events() { + let me = this; + this.fields.filter(xhiveframework.model.is_value_type).forEach((field) => { + field.fieldname && + me.get_input(field.fieldname)?.on?.("change", function () { + xhiveframework.telemetry.capture(`${field.fieldname}_set`, "setup"); + if ( + field.fieldname == "enable_telemetry" && + !me.get_value("enable_telemetry") + ) { + xhiveframework.telemetry.disable(); + } + }); + }); + } +}; + +// XhiveFramework slides settings +// ====================================================== +xhiveframework.setup.slides_settings = [ + { + // Welcome (language) slide + name: "welcome", + title: __("Welcome"), + + fields: [ + { + fieldname: "language", + label: __("Your Language"), + fieldtype: "Autocomplete", + placeholder: __("Select Language"), + default: "English", + reqd: 1, + }, + { + fieldname: "country", + label: __("Your Country"), + fieldtype: "Autocomplete", + placeholder: __("Select Country"), + reqd: 1, + }, + { + fieldtype: "Section Break", + }, + { + fieldname: "timezone", + label: __("Time Zone"), + placeholder: __("Select Time Zone"), + fieldtype: "Select", + reqd: 1, + }, + { fieldtype: "Column Break" }, + { + fieldname: "currency", + label: __("Currency"), + placeholder: __("Select Currency"), + fieldtype: "Select", + reqd: 1, + }, + { + fieldtype: "Section Break", + }, + { + fieldname: "enable_telemetry", + label: __("Allow sending usage data for improving applications"), + fieldtype: "Check", + default: cint(xhiveframework.telemetry.can_enable()), + depends_on: "eval:xhiveframework.telemetry.can_enable()", + }, + { + fieldname: "allow_recording_first_session", + label: __("Allow recording my first session to improve user experience"), + fieldtype: "Check", + default: 0, + depends_on: "eval:xhiveframework.telemetry.can_enable()", + }, + ], + + onload: function (slide) { + if (xhiveframework.setup.data.regional_data) { + this.setup_fields(slide); + } else { + xhiveframework.setup.utils.load_regional_data(slide, this.setup_fields); + } + if (!slide.get_value("language")) { + let session_language = + xhiveframework.setup.utils.get_language_name_from_code( + xhiveframework.boot.lang || navigator.language + ) || "English"; + let language_field = slide.get_field("language"); + + language_field.set_input(session_language); + if (!xhiveframework.setup._from_load_messages) { + language_field.$input.trigger("change"); + } + delete xhiveframework.setup._from_load_messages; + moment.locale("en"); + } + xhiveframework.setup.utils.bind_region_events(slide); + xhiveframework.setup.utils.bind_language_events(slide); + }, + + setup_fields: function (slide) { + xhiveframework.setup.utils.setup_region_fields(slide); + xhiveframework.setup.utils.setup_language_field(slide); + }, + }, + { + // Profile slide + name: "user", + title: __("Let's set up your account"), + icon: "fa fa-user", + fields: [ + { + fieldname: "full_name", + label: __("Full Name"), + fieldtype: "Data", + reqd: 1, + }, + { + fieldname: "email", + label: __("Email Address") + " (" + __("Will be your login ID") + ")", + fieldtype: "Data", + options: "Email", + }, + { fieldname: "password", label: __("Password"), fieldtype: "Password", length: 512 }, + ], + + onload: function (slide) { + if (xhiveframework.session.user !== "Administrator") { + const { first_name, last_name, email } = xhiveframework.boot.user; + if (first_name || last_name) { + slide.form.fields_dict.full_name.set_input( + [first_name, last_name].join(" ").trim() + ); + } + slide.form.fields_dict.email.set_input(email); + slide.form.fields_dict.email.df.read_only = 1; + slide.form.fields_dict.email.refresh(); + } else { + slide.form.fields_dict.email.df.reqd = 1; + slide.form.fields_dict.email.refresh(); + slide.form.fields_dict.password.df.reqd = 1; + slide.form.fields_dict.password.refresh(); + + xhiveframework.setup.utils.load_user_details(slide, this.setup_fields); + } + }, + + setup_fields: function (slide) { + if (xhiveframework.setup.data.full_name) { + slide.form.fields_dict.full_name.set_input(xhiveframework.setup.data.full_name); + } + if (xhiveframework.setup.data.email) { + let email = xhiveframework.setup.data.email; + slide.form.fields_dict.email.set_input(email); + } + }, + }, +]; + +xhiveframework.setup.utils = { + load_regional_data: function (slide, callback) { + xhiveframework.call({ + method: "xhiveframework.geo.country_info.get_country_timezone_info", + callback: function (data) { + xhiveframework.setup.data.regional_data = data.message; + callback(slide); + }, + }); + }, + + load_user_details: function (slide, callback) { + xhiveframework.call({ + method: "xhiveframework.desk.page.setup_wizard.setup_wizard.load_user_details", + freeze: true, + callback: function (r) { + xhiveframework.setup.data.full_name = r.message.full_name; + xhiveframework.setup.data.email = r.message.email; + callback(slide); + }, + }); + }, + + setup_language_field: function (slide) { + var language_field = slide.get_field("language"); + language_field.df.options = xhiveframework.setup.data.lang.languages; + language_field.set_options(); + }, + + setup_region_fields: function (slide) { + /* + Set a slide's country, timezone and currency fields + */ + let data = xhiveframework.setup.data.regional_data; + let country_field = slide.get_field("country"); + let translated_countries = []; + + Object.keys(data.country_info) + .sort() + .forEach((country) => { + translated_countries.push({ + label: __(country), + value: country, + }); + }); + + country_field.set_data(translated_countries); + + slide + .get_input("currency") + .empty() + .add_options( + xhiveframework.utils.unique($.map(data.country_info, (opts) => opts.currency).sort()) + ); + + slide.get_input("timezone").empty().add_options(data.all_timezones); + + slide.get_field("currency").set_input(xhiveframework.wizard.values.currency); + slide.get_field("timezone").set_input(xhiveframework.wizard.values.timezone); + + // set values if present + let country = + xhiveframework.wizard.values.country || + data.default_country || + guess_country(xhiveframework.setup.data.regional_data.country_info); + + if (country) { + country_field.set_input(country); + $(country_field.input).change(); + } + }, + + bind_language_events: function (slide) { + slide + .get_input("language") + .unbind("change") + .on("change", function () { + clearTimeout(slide.language_call_timeout); + slide.language_call_timeout = setTimeout(() => { + let lang = $(this).val() || "English"; + xhiveframework._messages = {}; + xhiveframework.call({ + method: "xhiveframework.desk.page.setup_wizard.setup_wizard.load_messages", + freeze: true, + args: { + language: lang, + }, + callback: function () { + xhiveframework.setup._from_load_messages = true; + xhiveframework.wizard.refresh_slides(); + }, + }); + }, 500); + }); + }, + + get_language_name_from_code: function (language_code) { + return xhiveframework.setup.data.lang.codes_to_names[language_code] || "English"; + }, + + bind_region_events: function (slide) { + /* + Bind a slide's country, timezone and currency fields + */ + slide.get_input("country").on("change", function () { + let country = slide.get_input("country").val(); + let $timezone = slide.get_input("timezone"); + let data = xhiveframework.setup.data.regional_data; + + $timezone.empty(); + + if (!country) return; + // add country specific timezones first + const timezone_list = data.country_info[country].timezones || []; + $timezone.add_options(timezone_list.sort()); + slide.get_field("currency").set_input(data.country_info[country].currency); + slide.get_field("currency").$input.trigger("change"); + + // add all timezones at the end, so that user has the option to change it to any timezone + $timezone.add_options(data.all_timezones); + slide.get_field("timezone").set_input($timezone.val()); + + // temporarily set date format + xhiveframework.boot.sysdefaults.date_format = + data.country_info[country].date_format || "dd-mm-yyyy"; + }); + + slide.get_input("currency").on("change", function () { + let currency = slide.get_input("currency").val(); + if (!currency) return; + xhiveframework.model.with_doc("Currency", currency, function () { + xhiveframework.provide("locals.:Currency." + currency); + let currency_doc = xhiveframework.model.get_doc("Currency", currency); + let number_format = currency_doc.number_format; + if (number_format === "#.###") { + number_format = "#.###,##"; + } else if (number_format === "#,###") { + number_format = "#,###.##"; + } + + xhiveframework.boot.sysdefaults.number_format = number_format; + locals[":Currency"][currency] = $.extend({}, currency_doc); + }); + }); + }, +}; + +// https://github.com/eggert/tz/blob/main/backward add more if required. +const TZ_BACKWARD_COMPATBILITY_MAP = { + "Asia/Calcutta": "Asia/Kolkata", +}; + +function guess_country(country_info) { + try { + let system_timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + system_timezone = TZ_BACKWARD_COMPATBILITY_MAP[system_timezone] || system_timezone; + + for (let [country, info] of Object.entries(country_info)) { + let possible_timezones = (info.timezones || []).filter((t) => t == system_timezone); + if (possible_timezones.length) return country; + } + } catch (e) { + console.log("Could not guess country", e); + } +} diff --git a/xhiveframework/desk/page/setup_wizard/setup_wizard.json b/xhiveframework/desk/page/setup_wizard/setup_wizard.json new file mode 100644 index 0000000..058549c --- /dev/null +++ b/xhiveframework/desk/page/setup_wizard/setup_wizard.json @@ -0,0 +1,23 @@ +{ + "content": null, + "creation": "2013-10-04 13:49:33", + "docstatus": 0, + "doctype": "Page", + "idx": 1, + "modified": "2017-04-12 18:45:00.774654", + "modified_by": "Administrator", + "module": "Desk", + "name": "setup-wizard", + "owner": "Administrator", + "page_name": "setup-wizard", + "roles": [ + { + "role": "System Manager" + } + ], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 1, + "title": "Setup Wizard" +} \ No newline at end of file diff --git a/xhiveframework/desk/page/setup_wizard/setup_wizard.py b/xhiveframework/desk/page/setup_wizard/setup_wizard.py new file mode 100755 index 0000000..f150402 --- /dev/null +++ b/xhiveframework/desk/page/setup_wizard/setup_wizard.py @@ -0,0 +1,438 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.geo.country_info import get_country_info +from xhiveframework.permissions import AUTOMATIC_ROLES +from xhiveframework.translate import get_messages_for_boot, send_translations, set_default_language +from xhiveframework.utils import cint, now, strip +from xhiveframework.utils.password import update_password + +from . import install_fixtures + + +def get_setup_stages(args): # nosemgrep + # App setup stage functions should not include xhiveframework.db.commit + # That is done by xhiveframework after successful completion of all stages + stages = [ + { + "status": "Updating global settings", + "fail_msg": "Failed to update global settings", + "tasks": [ + {"fn": update_global_settings, "args": args, "fail_msg": "Failed to update global settings"} + ], + } + ] + + stages += get_stages_hooks(args) + get_setup_complete_hooks(args) + + stages.append( + { + # post executing hooks + "status": "Wrapping up", + "fail_msg": "Failed to complete setup", + "tasks": [{"fn": run_post_setup_complete, "args": args, "fail_msg": "Failed to complete setup"}], + } + ) + + return stages + + +@xhiveframework.whitelist() +def setup_complete(args): + """Calls hooks for `setup_wizard_complete`, sets home page as `desktop` + and clears cache. If wizard breaks, calls `setup_wizard_exception` hook""" + + # Setup complete: do not throw an exception, let the user continue to desk + if cint(xhiveframework.db.get_single_value("System Settings", "setup_complete")): + return {"status": "ok"} + + args = parse_args(args) + stages = get_setup_stages(args) + is_background_task = xhiveframework.conf.get("trigger_site_setup_in_background") + + if is_background_task: + process_setup_stages.enqueue(stages=stages, user_input=args, is_background_task=True) + return {"status": "registered"} + else: + return process_setup_stages(stages, args) + + +@xhiveframework.task() +def process_setup_stages(stages, user_input, is_background_task=False): + from xhiveframework.utils.telemetry import capture + + capture("initated_server_side", "setup") + try: + xhiveframework.flags.in_setup_wizard = True + current_task = None + for idx, stage in enumerate(stages): + xhiveframework.publish_realtime( + "setup_task", + {"progress": [idx, len(stages)], "stage_status": stage.get("status")}, + user=xhiveframework.session.user, + ) + + for task in stage.get("tasks"): + current_task = task + task.get("fn")(task.get("args")) + except Exception: + handle_setup_exception(user_input) + message = current_task.get("fail_msg") if current_task else "Failed to complete setup" + xhiveframework.log_error(title=f"Setup failed: {message}") + if not is_background_task: + xhiveframework.response["setup_wizard_failure_message"] = message + raise + xhiveframework.publish_realtime( + "setup_task", + {"status": "fail", "fail_msg": message}, + user=xhiveframework.session.user, + ) + else: + run_setup_success(user_input) + capture("completed_server_side", "setup") + if not is_background_task: + return {"status": "ok"} + xhiveframework.publish_realtime("setup_task", {"status": "ok"}, user=xhiveframework.session.user) + finally: + xhiveframework.flags.in_setup_wizard = False + + +def update_global_settings(args): # nosemgrep + if args.language and args.language != "English": + set_default_language(get_language_code(args.lang)) + xhiveframework.db.commit() + xhiveframework.clear_cache() + + update_system_settings(args) + create_or_update_user(args) + set_timezone(args) + + +def run_post_setup_complete(args): # nosemgrep + disable_future_access() + xhiveframework.db.commit() + xhiveframework.clear_cache() + # HACK: due to race condition sometimes old doc stays in cache. + # Remove this when we have reliable cache reset for docs + xhiveframework.get_cached_doc("System Settings") and xhiveframework.get_doc("System Settings") + + +def run_setup_success(args): # nosemgrep + for hook in xhiveframework.get_hooks("setup_wizard_success"): + xhiveframework.get_attr(hook)(args) + install_fixtures.install() + + +def get_stages_hooks(args): # nosemgrep + stages = [] + for method in xhiveframework.get_hooks("setup_wizard_stages"): + stages += xhiveframework.get_attr(method)(args) + return stages + + +def get_setup_complete_hooks(args): # nosemgrep + return [ + { + "status": "Executing method", + "fail_msg": "Failed to execute method", + "tasks": [ + { + "fn": xhiveframework.get_attr(method), + "args": args, + "fail_msg": "Failed to execute method", + } + ], + } + for method in xhiveframework.get_hooks("setup_wizard_complete") + ] + + +def handle_setup_exception(args): # nosemgrep + xhiveframework.db.rollback() + if args: + traceback = xhiveframework.get_traceback(with_context=True) + print(traceback) + for hook in xhiveframework.get_hooks("setup_wizard_exception"): + xhiveframework.get_attr(hook)(traceback, args) + + +def update_system_settings(args): # nosemgrep + number_format = get_country_info(args.get("country")).get("number_format", "#,###.##") + + # replace these as float number formats, as they have 0 precision + # and are currency number formats and not for floats + if number_format == "#.###": + number_format = "#.###,##" + elif number_format == "#,###": + number_format = "#,###.##" + + system_settings = xhiveframework.get_doc("System Settings", "System Settings") + system_settings.update( + { + "country": args.get("country"), + "language": get_language_code(args.get("language")) or "en", + "time_zone": args.get("timezone"), + "float_precision": 3, + "rounding_method": "Banker's Rounding", + "date_format": xhiveframework.db.get_value("Country", args.get("country"), "date_format"), + "time_format": xhiveframework.db.get_value("Country", args.get("country"), "time_format"), + "number_format": number_format, + "enable_scheduler": 1 if not xhiveframework.flags.in_test else 0, + "backup_limit": 3, # Default for downloadable backups + "enable_telemetry": cint(args.get("enable_telemetry")), + } + ) + system_settings.save() + if args.get("allow_recording_first_session"): + xhiveframework.db.set_default("session_recording_start", now()) + + +def create_or_update_user(args): # nosemgrep + email = args.get("email") + if not email: + return + + first_name, last_name = args.get("full_name", ""), "" + if " " in first_name: + first_name, last_name = first_name.split(" ", 1) + + if user := xhiveframework.db.get_value("User", email, ["first_name", "last_name"], as_dict=True): + if user.first_name != first_name or user.last_name != last_name: + ( + xhiveframework.qb.update("User") + .set("first_name", first_name) + .set("last_name", last_name) + .set("full_name", args.get("full_name")) + ).run() + else: + _mute_emails, xhiveframework.flags.mute_emails = xhiveframework.flags.mute_emails, True + + user = xhiveframework.new_doc("User") + user.update( + { + "email": email, + "first_name": first_name, + "last_name": last_name, + } + ) + user.append_roles(*_get_default_roles()) + user.flags.no_welcome_mail = True + user.insert() + + xhiveframework.flags.mute_emails = _mute_emails + + if args.get("password"): + update_password(email, args.get("password")) + + +def set_timezone(args): # nosemgrep + if args.get("timezone"): + for name in xhiveframework.STANDARD_USERS: + xhiveframework.db.set_value("User", name, "time_zone", args.get("timezone")) + + +def parse_args(args): # nosemgrep + if not args: + args = xhiveframework.local.form_dict + if isinstance(args, str): + args = json.loads(args) + + args = xhiveframework._dict(args) + + # strip the whitespace + for key, value in args.items(): + if isinstance(value, str): + args[key] = strip(value) + + return args + + +def add_all_roles_to(name): + user = xhiveframework.get_doc("User", name) + user.append_roles(*_get_default_roles()) + user.save() + + +def _get_default_roles() -> set[str]: + skip_roles = { + "Administrator", + "Customer", + "Supplier", + "Partner", + "Employee", + }.union(AUTOMATIC_ROLES) + return set(xhiveframework.get_all("Role", pluck="name")) - skip_roles + + +def disable_future_access(): + xhiveframework.db.set_default("desktop:home_page", "workspace") + # Enable onboarding after install + xhiveframework.db.set_single_value("System Settings", "enable_onboarding", 1) + + xhiveframework.db.set_single_value("System Settings", "setup_complete", 1) + + +@xhiveframework.whitelist() +def load_messages(language): + """Load translation messages for given language from all `setup_wizard_requires` + javascript files""" + xhiveframework.clear_cache() + set_default_language(get_language_code(language)) + xhiveframework.db.commit() + send_translations(get_messages_for_boot()) + return xhiveframework.local.lang + + +@xhiveframework.whitelist() +def load_languages(): + language_codes = xhiveframework.db.sql( + "select language_code, language_name from tabLanguage order by name", as_dict=True + ) + codes_to_names = {} + for d in language_codes: + codes_to_names[d.language_code] = d.language_name + return { + "default_language": xhiveframework.db.get_value("Language", xhiveframework.local.lang, "language_name") + or xhiveframework.local.lang, + "languages": sorted(xhiveframework.db.sql_list("select language_name from tabLanguage order by name")), + "codes_to_names": codes_to_names, + } + + +@xhiveframework.whitelist() +def load_country(): + from xhiveframework.sessions import get_geo_ip_country + + return get_geo_ip_country(xhiveframework.local.request_ip) if xhiveframework.local.request_ip else None + + +@xhiveframework.whitelist() +def load_user_details(): + return { + "full_name": xhiveframework.cache.hget("full_name", "signup"), + "email": xhiveframework.cache.hget("email", "signup"), + } + + +def prettify_args(args): # nosemgrep + # remove attachments + for key, val in args.items(): + if isinstance(val, str) and "data:image" in val: + filename = val.split("data:image", 1)[0].strip(", ") + size = round((len(val) * 3 / 4) / 1048576.0, 2) + args[key] = f"Image Attached: '{filename}' of size {size} MB" + + pretty_args = [] + pretty_args.extend(f"{key} = {args[key]}" for key in sorted(args)) + return pretty_args + + +def email_setup_wizard_exception(traceback, args): # nosemgrep + if not xhiveframework.conf.setup_wizard_exception_email: + return + + pretty_args = prettify_args(args) + message = """ + +#### Traceback + +
      {traceback}
      + +--- + +#### Setup Wizard Arguments + +
      {args}
      + +--- + +#### Request Headers + +
      {headers}
      + +--- + +#### Basic Information + +- **Site:** {site} +- **User:** {user}""".format( + site=xhiveframework.local.site, + traceback=traceback, + args="\n".join(pretty_args), + user=xhiveframework.session.user, + headers=xhiveframework.request.headers if xhiveframework.request else "[no request]", + ) + + xhiveframework.sendmail( + recipients=xhiveframework.conf.setup_wizard_exception_email, + sender=xhiveframework.session.user, + subject=f"Setup failed: {xhiveframework.local.site}", + message=message, + delayed=False, + ) + + +def log_setup_wizard_exception(traceback, args): # nosemgrep + with open("../logs/setup-wizard.log", "w+") as setup_log: + setup_log.write(traceback) + setup_log.write(json.dumps(args)) + + +def get_language_code(lang): + return xhiveframework.db.get_value("Language", {"language_name": lang}) + + +def enable_twofactor_all_roles(): + all_role = xhiveframework.get_doc("Role", {"role_name": "All"}) + all_role.two_factor_auth = True + all_role.save(ignore_permissions=True) + + +def make_records(records, debug=False): + from xhiveframework import _dict + from xhiveframework.modules import scrub + + if debug: + print("make_records: in DEBUG mode") + + # LOG every success and failure + for record in records: + doctype = record.get("doctype") + condition = record.get("__condition") + + if condition and not condition(): + continue + + doc = xhiveframework.new_doc(doctype) + doc.update(record) + + # ignore mandatory for root + parent_link_field = "parent_" + scrub(doc.doctype) + if doc.meta.get_field(parent_link_field) and not doc.get(parent_link_field): + doc.flags.ignore_mandatory = True + + savepoint = "setup_fixtures_creation" + try: + xhiveframework.db.savepoint(savepoint) + doc.insert(ignore_permissions=True, ignore_if_duplicate=True) + except Exception as e: + xhiveframework.clear_last_message() + xhiveframework.db.rollback(save_point=savepoint) + exception = record.get("__exception") + if exception: + config = _dict(exception) + if isinstance(e, config.exception): + config.handler() + else: + show_document_insert_error() + else: + show_document_insert_error() + + +def show_document_insert_error(): + print("Document Insert Error") + print(xhiveframework.get_traceback()) + xhiveframework.log_error("Exception during Setup") diff --git a/xhiveframework/desk/page/user_profile/__init__.py b/xhiveframework/desk/page/user_profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/page/user_profile/user_profile.css b/xhiveframework/desk/page/user_profile/user_profile.css new file mode 100644 index 0000000..9bcfc33 --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile.css @@ -0,0 +1,30 @@ +.recent-activity .new-timeline { + padding-top: 0; +} + +.recent-activity .new-timeline:before { + top: 25px; +} + +.recent-activity-title { + font-weight: 700; + font-size: var(--text-xl); + color: var(--text-color); +} + +.recent-activity .recent-activity-footer { + margin-left: calc(var(--timeline-left-padding) + var(--timeline-item-left-margin)); + max-width: var(--timeline-content-max-width); +} + +.recent-activity .show-more-activity-btn { + display: block; + margin: auto; + width: max-content; + margin-top: 35px; + font-size: var(--text-md); +} + +.recent-activity { + padding-bottom: 60px; +} \ No newline at end of file diff --git a/xhiveframework/desk/page/user_profile/user_profile.html b/xhiveframework/desk/page/user_profile/user_profile.html new file mode 100644 index 0000000..5f3b275 --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile.html @@ -0,0 +1,44 @@ + diff --git a/xhiveframework/desk/page/user_profile/user_profile.js b/xhiveframework/desk/page/user_profile/user_profile.js new file mode 100644 index 0000000..5db3099 --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile.js @@ -0,0 +1,6 @@ +xhiveframework.pages["user-profile"].on_page_load = function (wrapper) { + xhiveframework.require("user_profile_controller.bundle.js", () => { + let user_profile = new xhiveframework.ui.UserProfile(wrapper); + user_profile.show(); + }); +}; diff --git a/xhiveframework/desk/page/user_profile/user_profile.json b/xhiveframework/desk/page/user_profile/user_profile.json new file mode 100644 index 0000000..43385f5 --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile.json @@ -0,0 +1,23 @@ +{ + "content": null, + "creation": "2019-07-22 12:23:38.425877", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2020-03-02 15:17:13.041650", + "modified_by": "Administrator", + "module": "Desk", + "name": "user-profile", + "owner": "Administrator", + "page_name": "User Profile", + "roles": [ + { + "role": "All" + } + ], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0, + "title": "User Profile" +} \ No newline at end of file diff --git a/xhiveframework/desk/page/user_profile/user_profile.py b/xhiveframework/desk/page/user_profile/user_profile.py new file mode 100644 index 0000000..500df4a --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile.py @@ -0,0 +1,112 @@ +from datetime import datetime + +import xhiveframework +from xhiveframework.query_builder import Interval, Order +from xhiveframework.query_builder.functions import Date, Sum, UnixTimestamp +from xhiveframework.utils import getdate + + +@xhiveframework.whitelist() +def get_energy_points_heatmap_data(user, date): + try: + date = getdate(date) + except Exception: + date = getdate() + + eps_log = xhiveframework.qb.DocType("Energy Point Log") + + return dict( + xhiveframework.qb.from_(eps_log) + .select(UnixTimestamp(Date(eps_log.creation)), Sum(eps_log.points)) + .where(eps_log.user == user) + .where(eps_log["type"] != "Review") + .where(Date(eps_log.creation) > Date(date) - Interval(years=1)) + .where(Date(eps_log.creation) < Date(date) + Interval(years=1)) + .groupby(Date(eps_log.creation)) + .orderby(Date(eps_log.creation), order=Order.asc) + .run() + ) + + +@xhiveframework.whitelist() +def get_energy_points_percentage_chart_data(user, field): + result = xhiveframework.get_all( + "Energy Point Log", + filters={"user": user, "type": ["!=", "Review"]}, + group_by=field, + order_by=field, + fields=[field, "ABS(sum(points)) as points"], + as_list=True, + ) + + return { + "labels": [r[0] for r in result if r[0] is not None], + "datasets": [{"values": [r[1] for r in result]}], + } + + +@xhiveframework.whitelist() +def get_user_rank(user): + month_start = datetime.today().replace(day=1) + monthly_rank = xhiveframework.get_all( + "Energy Point Log", + group_by="`tabEnergy Point Log`.`user`", + filters={"creation": [">", month_start], "type": ["!=", "Review"]}, + fields=["user", "sum(points)"], + order_by="sum(points) desc", + as_list=True, + ) + + all_time_rank = xhiveframework.get_all( + "Energy Point Log", + group_by="`tabEnergy Point Log`.`user`", + filters={"type": ["!=", "Review"]}, + fields=["user", "sum(points)"], + order_by="sum(points) desc", + as_list=True, + ) + + return { + "monthly_rank": [i + 1 for i, r in enumerate(monthly_rank) if r[0] == user], + "all_time_rank": [i + 1 for i, r in enumerate(all_time_rank) if r[0] == user], + } + + +@xhiveframework.whitelist() +def update_profile_info(profile_info): + profile_info = xhiveframework.parse_json(profile_info) + keys = ["location", "interest", "user_image", "bio"] + + for key in keys: + if key not in profile_info: + profile_info[key] = None + + user = xhiveframework.get_doc("User", xhiveframework.session.user) + user.update(profile_info) + user.save() + return user + + +@xhiveframework.whitelist() +def get_energy_points_list(start, limit, user): + return xhiveframework.db.get_list( + "Energy Point Log", + filters={"user": user, "type": ["!=", "Review"]}, + fields=[ + "name", + "user", + "points", + "reference_doctype", + "reference_name", + "reason", + "type", + "seen", + "rule", + "owner", + "creation", + "revert_of", + ], + start=start, + limit=limit, + order_by="creation desc", + ) diff --git a/xhiveframework/desk/page/user_profile/user_profile_controller.js b/xhiveframework/desk/page/user_profile/user_profile_controller.js new file mode 100644 index 0000000..359b499 --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile_controller.js @@ -0,0 +1,494 @@ +import BaseTimeline from "../../../public/js/xhiveframework/form/footer/base_timeline"; +xhiveframework.provide("xhiveframework.energy_points"); + +class UserProfile { + constructor(wrapper) { + this.wrapper = $(wrapper); + this.page = xhiveframework.ui.make_app_page({ + parent: wrapper, + }); + this.sidebar = this.wrapper.find(".layout-side-section"); + this.main_section = this.wrapper.find(".layout-main-section"); + this.wrapper.bind("show", () => { + this.show(); + }); + } + + show() { + let route = xhiveframework.get_route(); + this.user_id = route[1] || xhiveframework.session.user; + xhiveframework.dom.freeze(__("Loading user profile") + "..."); + xhiveframework.db.exists("User", this.user_id).then((exists) => { + xhiveframework.dom.unfreeze(); + if (exists) { + this.make_user_profile(); + } else { + xhiveframework.msgprint(__("User does not exist")); + } + }); + } + + make_user_profile() { + this.user = xhiveframework.user_info(this.user_id); + this.page.set_title(this.user.fullname); + this.setup_user_search(); + this.main_section.empty().append(xhiveframework.render_template("user_profile")); + this.energy_points = 0; + this.review_points = 0; + this.rank = 0; + this.month_rank = 0; + this.render_user_details(); + this.render_points_and_rank(); + this.render_heatmap(); + this.render_line_chart(); + this.render_percentage_chart("type", "Type Distribution"); + this.create_percentage_chart_filters(); + this.setup_user_activity_timeline(); + } + + setup_user_search() { + this.$user_search_button = this.page.set_secondary_action( + __("Change User"), + () => this.show_user_search_dialog(), + { icon: "change", size: "sm" } + ); + } + + show_user_search_dialog() { + let dialog = new xhiveframework.ui.Dialog({ + title: __("Change User"), + fields: [ + { + fieldtype: "Link", + fieldname: "user", + options: "User", + label: __("User"), + }, + ], + primary_action_label: __("Go"), + primary_action: ({ user }) => { + dialog.hide(); + xhiveframework.set_route("user-profile", user); + }, + }); + dialog.show(); + } + + render_heatmap() { + this.heatmap = new xhiveframework.Chart(".performance-heatmap", { + type: "heatmap", + countLabel: "Energy Points", + data: {}, + discreteDomains: 1, + radius: 3, + height: 150, + }); + this.update_heatmap_data(); + this.create_heatmap_chart_filters(); + } + + update_heatmap_data(date_from) { + xhiveframework + .xcall("xhiveframework.desk.page.user_profile.user_profile.get_energy_points_heatmap_data", { + user: this.user_id, + date: date_from || xhiveframework.datetime.year_start(), + }) + .then((r) => { + this.heatmap.update({ dataPoints: r }); + }); + } + + render_line_chart() { + this.line_chart_filters = [ + ["Energy Point Log", "user", "=", this.user_id, false], + ["Energy Point Log", "type", "!=", "Review", false], + ]; + + this.line_chart_config = { + timespan: "Last Month", + time_interval: "Daily", + type: "Line", + value_based_on: "points", + chart_type: "Sum", + document_type: "Energy Point Log", + name: "Energy Points", + width: "half", + based_on: "creation", + }; + + this.line_chart = new xhiveframework.Chart(".performance-line-chart", { + type: "line", + height: 200, + data: { + labels: [], + datasets: [{}], + }, + colors: ["purple"], + axisOptions: { + xIsSeries: 1, + }, + }); + this.update_line_chart_data(); + this.create_line_chart_filters(); + } + + update_line_chart_data() { + this.line_chart_config.filters_json = JSON.stringify(this.line_chart_filters); + + xhiveframework + .xcall("xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.get", { + chart: this.line_chart_config, + no_cache: 1, + }) + .then((chart) => { + this.line_chart.update(chart); + }); + } + + render_percentage_chart(field, title) { + xhiveframework + .xcall( + "xhiveframework.desk.page.user_profile.user_profile.get_energy_points_percentage_chart_data", + { + user: this.user_id, + field: field, + } + ) + .then((chart) => { + if (chart.labels.length) { + this.percentage_chart = new xhiveframework.Chart(".performance-percentage-chart", { + type: "percentage", + data: { + labels: chart.labels, + datasets: chart.datasets, + }, + truncateLegends: 1, + barOptions: { + height: 11, + depth: 1, + }, + height: 200, + maxSlices: 8, + colors: [ + "purple", + "blue", + "cyan", + "teal", + "pink", + "red", + "orange", + "yellow", + ], + }); + } else { + this.wrapper.find(".percentage-chart-container").hide(); + } + }); + } + + create_line_chart_filters() { + let filters = [ + { + label: "All", + options: ["All", "Auto", "Criticism", "Appreciation", "Revert"], + action: (selected_item) => { + if (selected_item === "All") { + this.line_chart_filters = [ + ["Energy Point Log", "user", "=", this.user_id, false], + ["Energy Point Log", "type", "!=", "Review", false], + ]; + } else { + this.line_chart_filters[1] = [ + "Energy Point Log", + "type", + "=", + selected_item, + false, + ]; + } + this.update_line_chart_data(); + }, + }, + { + label: "Last Month", + options: ["Last Week", "Last Month", "Last Quarter", "Last Year"], + action: (selected_item) => { + this.line_chart_config.timespan = selected_item; + this.update_line_chart_data(); + }, + }, + { + label: "Daily", + options: ["Daily", "Weekly", "Monthly"], + action: (selected_item) => { + this.line_chart_config.time_interval = selected_item; + this.update_line_chart_data(); + }, + }, + ]; + xhiveframework.dashboard_utils.render_chart_filters( + filters, + "chart-filter", + ".line-chart-options", + 1 + ); + } + + create_percentage_chart_filters() { + let filters = [ + { + label: "Type", + options: ["Type", "Reference Doctype", "Rule"], + fieldnames: ["type", "reference_doctype", "rule"], + action: (selected_item, fieldname) => { + let title = selected_item + " Distribution"; + this.render_percentage_chart(fieldname, title); + }, + }, + ]; + xhiveframework.dashboard_utils.render_chart_filters( + filters, + "chart-filter", + ".percentage-chart-options" + ); + } + + create_heatmap_chart_filters() { + let filters = [ + { + label: xhiveframework.dashboard_utils.get_year(xhiveframework.datetime.now_date()), + options: xhiveframework.dashboard_utils.get_years_since_creation( + xhiveframework.boot.user.creation + ), + action: (selected_item) => { + this.update_heatmap_data(xhiveframework.datetime.obj_to_str(selected_item)); + }, + }, + ]; + xhiveframework.dashboard_utils.render_chart_filters(filters, "chart-filter", ".heatmap-options"); + } + + edit_profile() { + let edit_profile_dialog = new xhiveframework.ui.Dialog({ + title: __("Edit Profile"), + fields: [ + { + fieldtype: "Attach Image", + fieldname: "user_image", + label: "Profile Image", + }, + { + fieldtype: "Data", + fieldname: "interest", + label: "Interests", + }, + { + fieldtype: "Column Break", + }, + { + fieldtype: "Data", + fieldname: "location", + label: "Location", + }, + { + fieldtype: "Section Break", + fieldname: "Interest", + }, + { + fieldtype: "Small Text", + fieldname: "bio", + label: "Bio", + }, + ], + primary_action: (values) => { + edit_profile_dialog.disable_primary_action(); + xhiveframework + .xcall("xhiveframework.desk.page.user_profile.user_profile.update_profile_info", { + profile_info: values, + }) + .then((user) => { + user.image = user.user_image; + this.user = Object.assign(values, user); + edit_profile_dialog.hide(); + this.render_user_details(); + }) + .finally(() => { + edit_profile_dialog.enable_primary_action(); + }); + }, + primary_action_label: __("Save"), + }); + + edit_profile_dialog.set_values({ + user_image: this.user.image, + location: this.user.location, + interest: this.user.interest, + bio: this.user.bio, + }); + edit_profile_dialog.show(); + } + + render_user_details() { + this.sidebar.empty().append( + xhiveframework.render_template("user_profile_sidebar", { + user_image: this.user.image, + user_abbr: this.user.abbr, + user_location: this.user.location, + user_interest: this.user.interest, + user_bio: this.user.bio, + }) + ); + + this.setup_user_profile_links(); + } + + setup_user_profile_links() { + if (this.user_id !== xhiveframework.session.user) { + this.wrapper.find(".profile-links").hide(); + } else { + this.wrapper.find(".edit-profile-link").on("click", () => { + this.edit_profile(); + }); + + this.wrapper.find(".user-settings-link").on("click", () => { + this.go_to_user_settings(); + }); + } + } + + get_user_rank() { + return xhiveframework + .xcall("xhiveframework.desk.page.user_profile.user_profile.get_user_rank", { + user: this.user_id, + }) + .then((r) => { + if (r.monthly_rank.length) this.month_rank = r.monthly_rank[0]; + if (r.all_time_rank.length) this.rank = r.all_time_rank[0]; + }); + } + + get_user_points() { + return xhiveframework + .xcall( + "xhiveframework.social.doctype.energy_point_log.energy_point_log.get_user_energy_and_review_points", + { + user: this.user_id, + } + ) + .then((r) => { + if (r[this.user_id]) { + this.energy_points = r[this.user_id].energy_points; + this.review_points = r[this.user_id].review_points; + } + }); + } + + render_points_and_rank() { + let $profile_details = this.wrapper.find(".user-stats"); + let $profile_details_wrapper = this.wrapper.find(".user-stats-detail"); + + const _get_stat_dom = (value, label, icon) => { + return `
      + ${xhiveframework.utils.icon(icon, "lg", "no-stroke")} +
      +
      ${value}
      +
      ${label}
      +
      +
      `; + }; + + this.get_user_rank().then(() => { + this.get_user_points().then(() => { + let html = $(` + ${_get_stat_dom(this.energy_points, __("Energy Points"), "color-energy-points")} + ${_get_stat_dom(this.review_points, __("Review Points"), "color-review-points")} + ${_get_stat_dom(this.rank, __("Rank"), "color-rank")} + ${_get_stat_dom(this.month_rank, __("Monthly Rank"), "color-monthly-rank")} + `); + + $profile_details.append(html); + $profile_details_wrapper.removeClass("hide"); + }); + }); + } + + go_to_user_settings() { + xhiveframework.set_route("Form", "User", this.user_id); + } + + setup_user_activity_timeline() { + this.user_activity_timeline = new UserProfileTimeline({ + parent: this.wrapper.find(".recent-activity-list"), + footer: this.wrapper.find(".recent-activity-footer"), + user: this.user_id, + }); + + this.user_activity_timeline.refresh(); + } +} + +class UserProfileTimeline extends BaseTimeline { + make() { + super.make(); + this.activity_start = 0; + this.activity_limit = 20; + this.setup_show_more_activity(); + } + prepare_timeline_contents() { + return this.get_user_activity_data().then((activities) => { + if (!activities.length) { + this.show_more_button.hide(); + this.timeline_wrapper.html(`
      ${__("No activities to show")}
      `); + return; + } + this.show_more_button.toggle(activities.length === this.activity_limit); + this.timeline_items = activities.map((activity) => + this.get_activity_timeline_item(activity) + ); + }); + } + + get_user_activity_data() { + return xhiveframework.xcall("xhiveframework.desk.page.user_profile.user_profile.get_energy_points_list", { + start: this.activity_start, + limit: this.activity_limit, + user: this.user, + }); + } + + get_activity_timeline_item(data) { + let icon = + data.type == "Appreciation" ? "clap" : data.type == "Criticism" ? "criticize" : null; + return { + icon: icon, + creation: data.creation, + is_card: true, + content: xhiveframework.energy_points.format_history_log(data), + }; + } + + setup_show_more_activity() { + this.show_more_button = $( + `${__("Show More Activity")}` + ); + this.show_more_button.hide(); + this.footer.append(this.show_more_button); + this.show_more_button.on("click", () => this.show_more_activity()); + } + + show_more_activity() { + this.activity_start += this.activity_limit; + this.get_user_activity_data().then((activities) => { + if (!activities.length || activities.length < this.activity_limit) { + this.show_more_button.hide(); + } + let timeline_items = activities.map((activity) => + this.get_activity_timeline_item(activity) + ); + timeline_items.map((item) => this.add_timeline_item(item, true)); + }); + } +} + +xhiveframework.provide("xhiveframework.ui"); +xhiveframework.ui.UserProfile = UserProfile; diff --git a/xhiveframework/desk/page/user_profile/user_profile_sidebar.html b/xhiveframework/desk/page/user_profile/user_profile_sidebar.html new file mode 100644 index 0000000..9f8889f --- /dev/null +++ b/xhiveframework/desk/page/user_profile/user_profile_sidebar.html @@ -0,0 +1,60 @@ + diff --git a/xhiveframework/desk/query_report.py b/xhiveframework/desk/query_report.py new file mode 100644 index 0000000..b8ee09e --- /dev/null +++ b/xhiveframework/desk/query_report.py @@ -0,0 +1,774 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import datetime +import json +import os +from datetime import timedelta + +import xhiveframework +import xhiveframework.desk.reportview +from xhiveframework import _ +from xhiveframework.core.utils import ljust_list +from xhiveframework.desk.reportview import clean_params, parse_json +from xhiveframework.model.utils import render_include +from xhiveframework.modules import get_module_path, scrub +from xhiveframework.monitor import add_data_to_monitor +from xhiveframework.permissions import get_role_permissions +from xhiveframework.utils import cint, cstr, flt, format_duration, get_html_format, sbool + + +def get_report_doc(report_name): + doc = xhiveframework.get_doc("Report", report_name) + doc.custom_columns = [] + doc.custom_filters = [] + + if doc.report_type == "Custom Report": + custom_report_doc = doc + doc = get_reference_report(doc) + doc.custom_report = report_name + if custom_report_doc.json: + data = json.loads(custom_report_doc.json) + if data: + doc.custom_columns = data.get("columns") + doc.custom_filters = data.get("filters") + doc.is_custom_report = True + + if not doc.is_permitted(): + xhiveframework.throw( + _("You don't have access to Report: {0}").format(report_name), + xhiveframework.PermissionError, + ) + + if not xhiveframework.has_permission(doc.ref_doctype, "report"): + xhiveframework.throw( + _("You don't have permission to get a report on: {0}").format(doc.ref_doctype), + xhiveframework.PermissionError, + ) + + if doc.disabled: + xhiveframework.throw(_("Report {0} is disabled").format(report_name)) + + return doc + + +def get_report_result(report, filters): + res = None + + if report.report_type == "Query Report": + res = report.execute_query_report(filters) + + elif report.report_type == "Script Report": + res = report.execute_script_report(filters) + + elif report.report_type == "Custom Report": + ref_report = get_report_doc(report.report_name) + res = get_report_result(ref_report, filters) + + return res + + +@xhiveframework.read_only() +def generate_report_result( + report, filters=None, user=None, custom_columns=None, is_tree=False, parent_field=None +): + user = user or xhiveframework.session.user + filters = filters or [] + + if filters and isinstance(filters, str): + filters = json.loads(filters) + + res = get_report_result(report, filters) or [] + + columns, result, message, chart, report_summary, skip_total_row = ljust_list(res, 6) + columns = [get_column_as_dict(col) for col in (columns or [])] + report_column_names = [col["fieldname"] for col in columns] + # convert to list of dicts + + result = normalize_result(result, columns) + + if report.custom_columns: + # saved columns (with custom columns / with different column order) + columns = report.custom_columns + + # unsaved custom_columns + if custom_columns: + for custom_column in custom_columns: + columns.insert(custom_column["insert_after_index"] + 1, custom_column) + + # all columns which are not in original report + report_custom_columns = [column for column in columns if column["fieldname"] not in report_column_names] + + if report_custom_columns: + result = add_custom_column_data(report_custom_columns, result) + + if result: + 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, is_tree=is_tree, parent_field=parent_field) + + return { + "result": result, + "columns": columns, + "message": message, + "chart": chart, + "report_summary": report_summary, + "skip_total_row": skip_total_row or 0, + "status": None, + "execution_time": xhiveframework.cache.hget("report_execution_time", report.name) or 0, + } + + +def normalize_result(result, columns): + # Converts to list of dicts from list of lists/tuples + data = [] + column_names = [column["fieldname"] for column in columns] + if result and isinstance(result[0], list | tuple): + for row in result: + row_obj = {} + for idx, column_name in enumerate(column_names): + row_obj[column_name] = row[idx] + data.append(row_obj) + else: + data = result + + return data + + +@xhiveframework.whitelist() +def get_script(report_name): + report = get_report_doc(report_name) + module = report.module or xhiveframework.db.get_value("DocType", report.ref_doctype, "module") + + is_custom_module = xhiveframework.get_cached_value("Module Def", module, "custom") + + # custom modules are virtual modules those exists in DB but not in disk. + module_path = "" if is_custom_module else get_module_path(module) + report_folder = module_path and os.path.join(module_path, "report", scrub(report.name)) + script_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".js") + print_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".html") + + script = None + if os.path.exists(script_path): + with open(script_path) as f: + script = f.read() + script += f"\n\n//# sourceURL={scrub(report.name)}.js" + + html_format = get_html_format(print_path) + + if not script and report.javascript: + script = report.javascript + script += f"\n\n//# sourceURL={scrub(report.name)}__custom" + + if not script: + script = "xhiveframework.query_reports['%s']={}" % report_name + + return { + "script": render_include(script), + "html_format": html_format, + "execution_time": xhiveframework.cache.hget("report_execution_time", report_name) or 0, + "filters": report.filters, + "custom_report_name": report.name if report.get("is_custom_report") else None, + } + + +def get_reference_report(report): + if report.report_type != "Custom Report": + return report + reference_report = xhiveframework.get_doc("Report", report.reference_report) + return get_reference_report(reference_report) + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def run( + report_name, + filters=None, + user=None, + ignore_prepared_report=False, + custom_columns=None, + is_tree=False, + parent_field=None, + are_default_filters=True, +): + report = get_report_doc(report_name) + if not user: + user = xhiveframework.session.user + if not xhiveframework.has_permission(report.ref_doctype, "report"): + xhiveframework.msgprint( + _("Must have report permission to access this report."), + raise_exception=True, + ) + + result = None + + if sbool(are_default_filters) and report.custom_filters: + filters = report.custom_filters + + if report.prepared_report and not sbool(ignore_prepared_report) and not custom_columns: + if filters: + if isinstance(filters, str): + filters = json.loads(filters) + + dn = filters.pop("prepared_report_name", None) + else: + dn = "" + result = get_prepared_report_result(report, filters, dn, user) + else: + result = generate_report_result(report, filters, user, custom_columns, is_tree, parent_field) + add_data_to_monitor(report=report.reference_report or report.name) + + result["add_total_row"] = report.add_total_row and not result.get("skip_total_row", False) + + if sbool(are_default_filters) and report.custom_filters: + result["custom_filters"] = report.custom_filters + + return result + + +def add_custom_column_data(custom_columns, result): + doctype_names_from_custom_field = [] + for column in custom_columns: + if len(column["fieldname"].split("-")) > 1: + # length greater than 1, means that the column is a custom field with confilicting fieldname + doctype_name = xhiveframework.unscrub(column["fieldname"].split("-")[1]) + doctype_names_from_custom_field.append(doctype_name) + column["fieldname"] = column["fieldname"].split("-")[0] + + custom_column_data = get_data_for_custom_report(custom_columns, result) + + for column in custom_columns: + key = (column.get("doctype"), column.get("fieldname")) + if key in custom_column_data: + for row in result: + link_field = column.get("link_field") + + # backwards compatibile `link_field` + # old custom reports which use `str` should not break. + if isinstance(link_field, str): + link_field = xhiveframework._dict({"fieldname": link_field, "names": []}) + + row_reference = row.get(link_field.get("fieldname")) + # possible if the row is empty + if not row_reference: + continue + if key[0] in doctype_names_from_custom_field: + column["fieldname"] = column.get("id") + row[column.get("fieldname")] = custom_column_data.get(key).get(row_reference) + + return result + + +def get_prepared_report_result(report, filters, dn="", user=None): + from xhiveframework.core.doctype.prepared_report.prepared_report import get_completed_prepared_report + + def get_report_data(doc, data): + # backwards compatibility - prepared report used to have a columns field, + # we now directly fetch it from the result file + if doc.get("columns") or isinstance(data, list): + columns = (doc.get("columns") and json.loads(doc.columns)) or data[0] + data = {"result": data} + else: + columns = data.get("columns") + + for column in columns: + if isinstance(column, dict) and column.get("label"): + column["label"] = _(column["label"]) + + return data | {"columns": columns} + + report_data = {} + if not dn: + dn = get_completed_prepared_report( + filters, user, report.get("custom_report") or report.get("report_name") + ) + + doc = xhiveframework.get_doc("Prepared Report", dn) if dn else None + if doc: + try: + if data := json.loads(doc.get_prepared_data().decode("utf-8")): + report_data = get_report_data(doc, data) + except Exception as e: + doc.log_error("Prepared report render failed") + xhiveframework.msgprint(_("Prepared report render failed") + f": {e!s}") + doc = None + + return report_data | {"prepared_report": True, "doc": doc} + + +@xhiveframework.whitelist() +def export_query(): + """export from query reports""" + from xhiveframework.desk.utils import get_csv_bytes, pop_csv_params, provide_binary_file + + form_params = xhiveframework._dict(xhiveframework.local.form_dict) + csv_params = pop_csv_params(form_params) + clean_params(form_params) + parse_json(form_params) + + report_name = form_params.report_name + xhiveframework.permissions.can_export( + xhiveframework.get_cached_value("Report", report_name, "ref_doctype"), + raise_exception=True, + ) + + file_format_type = form_params.file_format_type + custom_columns = xhiveframework.parse_json(form_params.custom_columns or "[]") + include_indentation = form_params.include_indentation + include_filters = form_params.include_filters + visible_idx = form_params.visible_idx + + if isinstance(visible_idx, str): + visible_idx = json.loads(visible_idx) + + data = run(report_name, form_params.filters, custom_columns=custom_columns, are_default_filters=False) + data = xhiveframework._dict(data) + data.filters = form_params.applied_filters + + if not data.columns: + xhiveframework.respond_as_web_page( + _("No data to export"), + _("You can try changing the filters of your report."), + ) + return + + format_duration_fields(data) + xlsx_data, column_widths = build_xlsx_data( + data, visible_idx, include_indentation, include_filters=include_filters + ) + + if file_format_type == "CSV": + content = get_csv_bytes(xlsx_data, csv_params) + file_extension = "csv" + elif file_format_type == "Excel": + from xhiveframework.utils.xlsxutils import make_xlsx + + file_extension = "xlsx" + content = make_xlsx(xlsx_data, "Query Report", column_widths=column_widths).getvalue() + + provide_binary_file(report_name, file_extension, content) + + +def format_duration_fields(data: xhiveframework._dict) -> None: + for i, col in enumerate(data.columns): + if col.get("fieldtype") != "Duration": + continue + + for row in data.result: + index = col.get("fieldname") if isinstance(row, dict) else i + if row[index]: + row[index] = format_duration(row[index]) + + +def build_xlsx_data(data, visible_idx, include_indentation, include_filters=False, ignore_visible_idx=False): + EXCEL_TYPES = ( + str, + bool, + type(None), + int, + float, + datetime.datetime, + datetime.date, + datetime.time, + datetime.timedelta, + ) + + if len(visible_idx) == len(data.result) or not visible_idx: + # It's not possible to have same length and different content. + ignore_visible_idx = True + else: + # Note: converted for faster lookups + visible_idx = set(visible_idx) + + result = [] + column_widths = [] + + if cint(include_filters): + filter_data = [] + filters = data.filters + for filter_name, filter_value in filters.items(): + if not filter_value: + continue + filter_value = ( + ", ".join([cstr(x) for x in filter_value]) + if isinstance(filter_value, list) + else cstr(filter_value) + ) + filter_data.append([cstr(filter_name), filter_value]) + filter_data.append([]) + result += filter_data + + column_data = [] + for column in data.columns: + if column.get("hidden"): + continue + column_data.append(_(column.get("label"))) + column_width = cint(column.get("width", 0)) + # to convert into scale accepted by openpyxl + column_width /= 10 + column_widths.append(column_width) + result.append(column_data) + + # build table from result + for row_idx, row in enumerate(data.result): + # only pick up rows that are visible in the report + if ignore_visible_idx or row_idx in visible_idx: + row_data = [] + if isinstance(row, dict): + for col_idx, column in enumerate(data.columns): + if column.get("hidden"): + continue + label = column.get("label") + fieldname = column.get("fieldname") + cell_value = row.get(fieldname, row.get(label, "")) + if not isinstance(cell_value, EXCEL_TYPES): + cell_value = cstr(cell_value) + + if cint(include_indentation) and "indent" in row and col_idx == 0: + cell_value = (" " * cint(row["indent"])) + cstr(cell_value) + row_data.append(cell_value) + elif row: + row_data = row + + result.append(row_data) + + return result, column_widths + + +def add_total_row(result, columns, meta=None, is_tree=False, parent_field=None): + total_row = [""] * len(columns) + has_percent = [] + + for i, col in enumerate(columns): + fieldtype, options, fieldname = None, None, None + if isinstance(col, str): + if meta: + # get fieldtype from the meta + field = meta.get_field(col) + if field: + fieldtype = meta.get_field(col).fieldtype + fieldname = meta.get_field(col).fieldname + else: + col = col.split(":") + if len(col) > 1: + if col[1]: + fieldtype = col[1] + if "/" in fieldtype: + fieldtype, options = fieldtype.split("/") + else: + fieldtype = "Data" + else: + fieldtype = col.get("fieldtype") + fieldname = col.get("fieldname") + options = col.get("options") + + 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): + 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) + + if fieldtype == "Time" and cell: + if not total_row[i]: + total_row[i] = timedelta(hours=0, minutes=0, seconds=0) + total_row[i] = total_row[i] + cell + + if fieldtype == "Link" and options == "Currency": + total_row[i] = result[0].get(fieldname) if isinstance(result[0], dict) else result[0][i] + + for i in has_percent: + total_row[i] = flt(total_row[i]) / len(result) + + first_col_fieldtype = None + if isinstance(columns[0], str): + first_col = columns[0].split(":") + if len(first_col) > 1: + first_col_fieldtype = first_col[1].split("/", 1)[0] + else: + first_col_fieldtype = columns[0].get("fieldtype") + + if first_col_fieldtype not in ["Currency", "Int", "Float", "Percent", "Date"]: + total_row[0] = _("Total") + + result.append(total_row) + return result + + +@xhiveframework.whitelist() +def get_data_for_custom_field(doctype, field, names=None): + if not xhiveframework.has_permission(doctype, "read"): + xhiveframework.throw(_("Not Permitted to read {0}").format(doctype), xhiveframework.PermissionError) + + filters = {} + if names: + if isinstance(names, str | bytearray): + names = xhiveframework.json.loads(names) + filters.update({"name": ["in", names]}) + + return xhiveframework._dict(xhiveframework.get_list(doctype, filters=filters, fields=["name", field], as_list=1)) + + +def get_data_for_custom_report(columns, result): + doc_field_value_map = {} + + for column in columns: + if link_field := column.get("link_field"): + # backwards compatibile `link_field` + # old custom reports which use `str` should not break + if isinstance(link_field, str): + link_field = xhiveframework._dict({"fieldname": link_field, "names": []}) + + fieldname = column.get("fieldname") + doctype = column.get("doctype") + + row_key = link_field.get("fieldname") + names = [] + for row in result: + if row.get(row_key): + names.append(row.get(row_key)) + names = list(set(names)) + + doc_field_value_map[(doctype, fieldname)] = get_data_for_custom_field(doctype, fieldname, names) + return doc_field_value_map + + +@xhiveframework.whitelist() +def save_report(reference_report, report_name, columns, filters): + report_doc = get_report_doc(reference_report) + + docname = xhiveframework.db.exists( + "Report", + { + "report_name": report_name, + "is_standard": "No", + "report_type": "Custom Report", + }, + ) + + if docname: + report = xhiveframework.get_doc("Report", docname) + existing_jd = json.loads(report.json) + existing_jd["columns"] = json.loads(columns) + existing_jd["filters"] = json.loads(filters) + report.update({"json": json.dumps(existing_jd, separators=(",", ":"))}) + report.save() + xhiveframework.msgprint(_("Report updated successfully")) + + return docname + else: + new_report = xhiveframework.get_doc( + { + "doctype": "Report", + "report_name": report_name, + "json": f'{{"columns":{columns},"filters":{filters}}}', + "ref_doctype": report_doc.ref_doctype, + "is_standard": "No", + "report_type": "Custom Report", + "reference_report": reference_report, + } + ).insert(ignore_permissions=True) + xhiveframework.msgprint(_("{0} saved successfully").format(new_report.name)) + return new_report.name + + +def get_filtered_data(ref_doctype, columns, data, user): + result = [] + linked_doctypes = get_linked_doctypes(columns, data) + match_filters_per_doctype = get_user_match_filters(linked_doctypes, user=user) + shared = xhiveframework.share.get_shared(ref_doctype, user) + columns_dict = get_columns_dict(columns) + + role_permissions = get_role_permissions(xhiveframework.get_meta(ref_doctype), user) + if_owner = role_permissions.get("if_owner", {}).get("report") + + if match_filters_per_doctype: + for row in data: + # Why linked_doctypes.get(ref_doctype)? because if column is empty, linked_doctypes[ref_doctype] is removed + if ( + linked_doctypes.get(ref_doctype) + and shared + and row.get(linked_doctypes[ref_doctype]) in shared + ): + result.append(row) + + elif has_match( + row, + linked_doctypes, + match_filters_per_doctype, + ref_doctype, + if_owner, + columns_dict, + user, + ): + result.append(row) + else: + result = list(data) + + return result + + +def has_match( + row, + linked_doctypes, + doctype_match_filters, + ref_doctype, + if_owner, + columns_dict, + user, +): + """Returns True if after evaluating permissions for each linked doctype + - There is an owner match for the ref_doctype + - `and` There is a user permission match for all linked doctypes + + Returns True if the row is empty + + Note: + Each doctype could have multiple conflicting user permission doctypes. + Hence even if one of the sets allows a match, it is true. + This behavior is equivalent to the trickling of user permissions of linked doctypes to the ref doctype. + """ + resultant_match = True + + if not row: + # allow empty rows :) + return resultant_match + + for doctype, filter_list in doctype_match_filters.items(): + matched_for_doctype = False + + if doctype == ref_doctype and if_owner: + idx = linked_doctypes.get("User") + if idx is not None and row[idx] == user and columns_dict[idx] == columns_dict.get("owner"): + # owner match is true + matched_for_doctype = True + + if not matched_for_doctype: + for match_filters in filter_list: + match = True + for dt, idx in linked_doctypes.items(): + # case handled above + if dt == "User" and columns_dict[idx] == columns_dict.get("owner"): + continue + + cell_value = None + if isinstance(row, dict): + cell_value = row.get(idx) + elif isinstance(row, list | tuple): + cell_value = row[idx] + + if ( + dt in match_filters + and cell_value not in match_filters.get(dt) + and xhiveframework.db.exists(dt, cell_value) + ): + match = False + break + + # each doctype could have multiple conflicting user permission doctypes, hence using OR + # so that even if one of the sets allows a match, it is true + matched_for_doctype = matched_for_doctype or match + + if matched_for_doctype: + break + + # each doctype's user permissions should match the row! hence using AND + resultant_match = resultant_match and matched_for_doctype + + if not resultant_match: + break + + return resultant_match + + +def get_linked_doctypes(columns, data): + linked_doctypes = {} + + columns_dict = get_columns_dict(columns) + + for idx in range(len(columns)): + df = columns_dict[idx] + if df.get("fieldtype") == "Link": + if data and isinstance(data[0], list | tuple): + linked_doctypes[df["options"]] = idx + else: + # dict + linked_doctypes[df["options"]] = df["fieldname"] + + # remove doctype if column is empty + columns_with_value = [] + for row in data: + if row: + if len(row) != len(columns_with_value): + if isinstance(row, list | tuple): + row = enumerate(row) + elif isinstance(row, dict): + row = row.items() + + for col, val in row: + if val and col not in columns_with_value: + columns_with_value.append(col) + + items = list(linked_doctypes.items()) + + for doctype, key in items: + if key not in columns_with_value: + del linked_doctypes[doctype] + + return linked_doctypes + + +def get_columns_dict(columns): + """Returns a dict with column docfield values as dict + The keys for the dict are both idx and fieldname, + so either index or fieldname can be used to search for a column's docfield properties + """ + columns_dict = xhiveframework._dict() + for idx, col in enumerate(columns): + col_dict = get_column_as_dict(col) + columns_dict[idx] = col_dict + columns_dict[col_dict["fieldname"]] = col_dict + + return columns_dict + + +def get_column_as_dict(col): + col_dict = xhiveframework._dict() + + # string + if isinstance(col, str): + col = col.split(":") + if len(col) > 1: + if "/" in col[1]: + col_dict["fieldtype"], col_dict["options"] = col[1].split("/") + else: + col_dict["fieldtype"] = col[1] + if len(col) == 3: + col_dict["width"] = col[2] + + col_dict["label"] = col[0] + col_dict["fieldname"] = xhiveframework.scrub(col[0]) + + # dict + else: + col_dict.update(col) + if "fieldname" not in col_dict: + col_dict["fieldname"] = xhiveframework.scrub(col_dict["label"]) + + return col_dict + + +def get_user_match_filters(doctypes, user): + match_filters = {} + + for dt in doctypes: + filter_list = xhiveframework.desk.reportview.build_match_conditions(dt, user, False) + if filter_list: + match_filters[dt] = filter_list + + return match_filters diff --git a/xhiveframework/desk/report/__init__.py b/xhiveframework/desk/report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/report/todo/__init__.py b/xhiveframework/desk/report/todo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/desk/report/todo/todo.js b/xhiveframework/desk/report/todo/todo.js new file mode 100644 index 0000000..b004b96 --- /dev/null +++ b/xhiveframework/desk/report/todo/todo.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.query_reports["ToDo"] = { + filters: [], +}; diff --git a/xhiveframework/desk/report/todo/todo.json b/xhiveframework/desk/report/todo/todo.json new file mode 100644 index 0000000..b42c4c9 --- /dev/null +++ b/xhiveframework/desk/report/todo/todo.json @@ -0,0 +1,23 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "creation": "2013-02-25 14:26:30", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 3, + "is_standard": "Yes", + "modified": "2017-06-21 18:18:50.748793", + "modified_by": "Administrator", + "module": "Desk", + "name": "ToDo", + "owner": "Administrator", + "ref_doctype": "ToDo", + "report_name": "ToDo", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/xhiveframework/desk/report/todo/todo.py b/xhiveframework/desk/report/todo/todo.py new file mode 100644 index 0000000..cc66a44 --- /dev/null +++ b/xhiveframework/desk/report/todo/todo.py @@ -0,0 +1,69 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils import getdate + + +def execute(filters=None): + priority_map = {"High": 3, "Medium": 2, "Low": 1} + + todo_list = xhiveframework.get_list( + "ToDo", + fields=[ + "name", + "date", + "description", + "priority", + "reference_type", + "reference_name", + "assigned_by", + "owner", + ], + filters={"status": "Open"}, + ) + + todo_list.sort( + key=lambda todo: ( + priority_map.get(todo.priority, 0), + todo.date and getdate(todo.date) or getdate("1900-01-01"), + ), + reverse=True, + ) + + columns = [ + _("ID") + ":Link/ToDo:90", + _("Priority") + "::60", + _("Date") + ":Date", + _("Description") + "::150", + _("Assigned To/Owner") + ":Data:120", + _("Assigned By") + ":Data:120", + _("Reference") + "::200", + ] + + result = [] + for todo in todo_list: + if todo.owner == xhiveframework.session.user or todo.assigned_by == xhiveframework.session.user: + if todo.reference_type: + todo.reference = """{}: {}""".format( + todo.reference_type, + todo.reference_name, + todo.reference_type, + todo.reference_name, + ) + else: + todo.reference = None + result.append( + [ + todo.name, + todo.priority, + todo.date, + todo.description, + todo.owner, + todo.assigned_by, + todo.reference, + ] + ) + + return columns, result diff --git a/xhiveframework/desk/reportview.py b/xhiveframework/desk/reportview.py new file mode 100644 index 0000000..5167612 --- /dev/null +++ b/xhiveframework/desk/reportview.py @@ -0,0 +1,717 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +"""build query for doclistview and return results""" + +import json + +import xhiveframework +import xhiveframework.permissions +from xhiveframework import _ +from xhiveframework.core.doctype.access_log.access_log import make_access_log +from xhiveframework.model import child_table_fields, default_fields, get_permitted_fields, optional_fields +from xhiveframework.model.base_document import get_controller +from xhiveframework.model.db_query import DatabaseQuery +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.utils import add_user_info, cint, format_duration +from xhiveframework.utils.data import sbool + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get(): + args = get_form_params() + # If virtual doctype, get data from controller get_list method + if is_virtual_doctype(args.doctype): + controller = get_controller(args.doctype) + data = compress(controller.get_list(args)) + else: + data = compress(execute(**args), args=args) + return data + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_list(): + args = get_form_params() + + if is_virtual_doctype(args.doctype): + controller = get_controller(args.doctype) + data = controller.get_list(args) + else: + # uncompressed (refactored from xhiveframework.model.db_query.get_list) + data = execute(**args) + + return data + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_count() -> int: + args = get_form_params() + + if is_virtual_doctype(args.doctype): + controller = get_controller(args.doctype) + count = controller.get_count(args) + else: + args.distinct = sbool(args.distinct) + distinct = "distinct " if args.distinct else "" + args.limit = cint(args.limit) + fieldname = f"{distinct}`tab{args.doctype}`.name" + args.order_by = None + + if args.limit: + args.fields = [fieldname] + partial_query = execute(**args, run=0) + count = xhiveframework.db.sql(f"""select count(*) from ( {partial_query} ) p""")[0][0] + else: + args.fields = [f"count({fieldname}) as total_count"] + count = execute(**args)[0].get("total_count") + + return count + + +def execute(doctype, *args, **kwargs): + return DatabaseQuery(doctype).execute(*args, **kwargs) + + +def get_form_params(): + """parse GET request parameters.""" + data = xhiveframework._dict(xhiveframework.local.form_dict) + clean_params(data) + validate_args(data) + return data + + +def validate_args(data): + parse_json(data) + setup_group_by(data) + + validate_fields(data) + if data.filters: + validate_filters(data, data.filters) + if data.or_filters: + validate_filters(data, data.or_filters) + + data.strict = None + + return data + + +def validate_fields(data): + wildcard = update_wildcard_field_param(data) + + for field in list(data.fields or []): + fieldname = extract_fieldname(field) + if is_standard(fieldname): + continue + + meta, df = get_meta_and_docfield(fieldname, data) + + if not df: + if wildcard: + continue + else: + raise_invalid_field(fieldname) + + # remove the field from the query if the report hide flag is set and current view is Report + if df.report_hide and data.view == "Report": + data.fields.remove(field) + continue + + if df.fieldname in [_df.fieldname for _df in meta.get_high_permlevel_fields()]: + if df.get("permlevel") not in meta.get_permlevel_access(parenttype=data.doctype): + data.fields.remove(field) + + +def validate_filters(data, filters): + if isinstance(filters, list): + # filters as list + for condition in filters: + if len(condition) == 3: + # [fieldname, condition, value] + fieldname = condition[0] + if is_standard(fieldname): + continue + meta, df = get_meta_and_docfield(fieldname, data) + if not df: + raise_invalid_field(condition[0]) + else: + # [doctype, fieldname, condition, value] + fieldname = condition[1] + if is_standard(fieldname): + continue + meta = xhiveframework.get_meta(condition[0]) + if not meta.get_field(fieldname): + raise_invalid_field(fieldname) + + else: + for fieldname in filters: + if is_standard(fieldname): + continue + meta, df = get_meta_and_docfield(fieldname, data) + if not df: + raise_invalid_field(fieldname) + + +def setup_group_by(data): + """Add columns for aggregated values e.g. count(name)""" + if data.group_by and data.aggregate_function: + if data.aggregate_function.lower() not in ("count", "sum", "avg"): + xhiveframework.throw(_("Invalid aggregate function")) + + if xhiveframework.db.has_column(data.aggregate_on_doctype, data.aggregate_on_field): + data.fields.append( + f"{data.aggregate_function}(`tab{data.aggregate_on_doctype}`.`{data.aggregate_on_field}`) AS _aggregate_column" + ) + else: + raise_invalid_field(data.aggregate_on_field) + + data.pop("aggregate_on_doctype") + data.pop("aggregate_on_field") + data.pop("aggregate_function") + + +def raise_invalid_field(fieldname): + xhiveframework.throw(_("Field not permitted in query") + f": {fieldname}", xhiveframework.DataError) + + +def is_standard(fieldname): + if "." in fieldname: + fieldname = fieldname.split(".")[1].strip("`") + return fieldname in default_fields or fieldname in optional_fields or fieldname in child_table_fields + + +def extract_fieldname(field): + for text in (",", "/*", "#"): + if text in field: + raise_invalid_field(field) + + fieldname = field + for sep in (" as ", " AS "): + if sep in fieldname: + fieldname = fieldname.split(sep, 1)[0] + + # certain functions allowed, extract the fieldname from the function + if fieldname.startswith("count(") or fieldname.startswith("sum(") or fieldname.startswith("avg("): + if not fieldname.strip().endswith(")"): + raise_invalid_field(field) + fieldname = fieldname.split("(", 1)[1][:-1] + + return fieldname + + +def get_meta_and_docfield(fieldname, data): + parenttype, fieldname = get_parenttype_and_fieldname(fieldname, data) + meta = xhiveframework.get_meta(parenttype) + df = meta.get_field(fieldname) + return meta, df + + +def update_wildcard_field_param(data): + if (isinstance(data.fields, str) and data.fields == "*") or ( + isinstance(data.fields, list | tuple) and len(data.fields) == 1 and data.fields[0] == "*" + ): + data.fields = get_permitted_fields(data.doctype, parenttype=data.parenttype) + return True + + return False + + +def clean_params(data): + for param in ("cmd", "data", "ignore_permissions", "view", "user", "csrf_token", "join"): + data.pop(param, None) + + +def parse_json(data): + if (filters := data.get("filters")) and isinstance(filters, str): + data["filters"] = json.loads(filters) + if (applied_filters := data.get("applied_filters")) and isinstance(applied_filters, str): + data["applied_filters"] = json.loads(applied_filters) + if (or_filters := data.get("or_filters")) and isinstance(or_filters, str): + data["or_filters"] = json.loads(or_filters) + if (fields := data.get("fields")) and isinstance(fields, str): + data["fields"] = ["*"] if fields == "*" else json.loads(fields) + if isinstance(data.get("docstatus"), str): + data["docstatus"] = json.loads(data["docstatus"]) + if isinstance(data.get("save_user_settings"), str): + data["save_user_settings"] = json.loads(data["save_user_settings"]) + else: + data["save_user_settings"] = True + + +def get_parenttype_and_fieldname(field, data): + if "." in field: + parts = field.split(".") + parenttype = parts[0] + fieldname = parts[1] + if parenttype.startswith("`tab"): + # `tabChild DocType`.`fieldname` + parenttype = parenttype[4:-1] + fieldname = fieldname.strip("`") + else: + # tablefield.fieldname + parenttype = xhiveframework.get_meta(data.doctype).get_field(parenttype).options + else: + parenttype = data.doctype + fieldname = field.strip("`") + + return parenttype, fieldname + + +def compress(data, args=None): + """separate keys and values""" + from xhiveframework.desk.query_report import add_total_row + + user_info = {} + + if not data: + return data + if args is None: + args = {} + values = [] + keys = list(data[0]) + for row in data: + values.append([row.get(key) for key in keys]) + + # add user info for assignments (avatar) + if row.get("_assign", ""): + for user in json.loads(row._assign): + add_user_info(user, user_info) + + if args.get("add_total_row"): + meta = xhiveframework.get_meta(args.doctype) + values = add_total_row(values, keys, meta) + + return {"keys": keys, "values": values, "user_info": user_info} + + +@xhiveframework.whitelist() +def save_report(name, doctype, report_settings): + """Save reports of type Report Builder from Report View""" + + if xhiveframework.db.exists("Report", name): + report = xhiveframework.get_doc("Report", name) + if report.is_standard == "Yes": + xhiveframework.throw(_("Standard Reports cannot be edited")) + + if report.report_type != "Report Builder": + xhiveframework.throw(_("Only reports of type Report Builder can be edited")) + + if report.owner != xhiveframework.session.user and not report.has_permission("write"): + xhiveframework.throw(_("Insufficient Permissions for editing Report"), xhiveframework.PermissionError) + else: + report = xhiveframework.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) + xhiveframework.msgprint( + _("Report {0} saved").format(xhiveframework.bold(report.name)), + indicator="green", + alert=True, + ) + return report.name + + +@xhiveframework.whitelist() +def delete_report(name): + """Delete reports of type Report Builder from Report View""" + + report = xhiveframework.get_doc("Report", name) + if report.is_standard == "Yes": + xhiveframework.throw(_("Standard Reports cannot be deleted")) + + if report.report_type != "Report Builder": + xhiveframework.throw(_("Only reports of type Report Builder can be deleted")) + + if report.owner != xhiveframework.session.user and not report.has_permission("delete"): + xhiveframework.throw(_("Insufficient Permissions for deleting Report"), xhiveframework.PermissionError) + + report.delete(ignore_permissions=True) + xhiveframework.msgprint( + _("Report {0} deleted").format(xhiveframework.bold(report.name)), + indicator="green", + alert=True, + ) + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def export_query(): + """export from report builder""" + from xhiveframework.desk.utils import get_csv_bytes, pop_csv_params, provide_binary_file + + form_params = get_form_params() + form_params["limit_page_length"] = None + form_params["as_list"] = True + doctype = form_params.pop("doctype") + file_format_type = form_params.pop("file_format_type") + title = form_params.pop("title", doctype) + csv_params = pop_csv_params(form_params) + add_totals_row = 1 if form_params.pop("add_totals_row", None) == "1" else None + + xhiveframework.permissions.can_export(doctype, raise_exception=True) + + if selection := form_params.pop("selected_items", None): + form_params["filters"] = {"name": ("in", json.loads(selection))} + + make_access_log( + doctype=doctype, + file_type=file_format_type, + report_name=form_params.report_name, + filters=form_params.filters, + ) + + db_query = DatabaseQuery(doctype) + ret = db_query.execute(**form_params) + + if add_totals_row: + ret = append_totals_row(ret) + + data = [[_("Sr"), *get_labels(db_query.fields, doctype)]] + data.extend([i + 1, *list(row)] for i, row in enumerate(ret)) + data = handle_duration_fieldtype_values(doctype, data, db_query.fields) + + if file_format_type == "CSV": + from xhiveframework.utils.xlsxutils import handle_html + + file_extension = "csv" + content = get_csv_bytes( + [[handle_html(xhiveframework.as_unicode(v)) if isinstance(v, str) else v for v in r] for r in data], + csv_params, + ) + elif file_format_type == "Excel": + from xhiveframework.utils.xlsxutils import make_xlsx + + file_extension = "xlsx" + content = make_xlsx(data, doctype).getvalue() + + provide_binary_file(title, file_extension, content) + + +def append_totals_row(data): + if not data: + return data + data = list(data) + totals = [] + totals.extend([""] * len(data[0])) + + for row in data: + for i in range(len(row)): + if isinstance(row[i], float | int): + totals[i] = (totals[i] or 0) + row[i] + + if not isinstance(totals[0], int | float): + totals[0] = "Total" + + data.append(totals) + + return data + + +def get_labels(fields, doctype): + """get column labels based on column names""" + labels = [] + for key in fields: + try: + parenttype, fieldname = parse_field(key) + except ValueError: + continue + + parenttype = parenttype or doctype + + if parenttype == doctype and fieldname == "name": + label = _("ID", context="Label of name column in report") + else: + df = xhiveframework.get_meta(parenttype).get_field(fieldname) + label = _(df.label if df else fieldname.title()) + if parenttype != doctype: + # If the column is from a child table, append the child doctype. + # For example, "Item Code (Sales Invoice Item)". + label += f" ({ _(parenttype) })" + + labels.append(label) + + return labels + + +def handle_duration_fieldtype_values(doctype, data, fields): + for field in fields: + try: + parenttype, fieldname = parse_field(field) + except ValueError: + continue + + parenttype = parenttype or doctype + df = xhiveframework.get_meta(parenttype).get_field(fieldname) + + if df and df.fieldtype == "Duration": + index = fields.index(field) + 1 + for i in range(1, len(data)): + val_in_seconds = data[i][index] + if val_in_seconds: + duration_val = format_duration(val_in_seconds, df.hide_days) + data[i][index] = duration_val + return data + + +def parse_field(field: str) -> tuple[str | None, str]: + """Parse a field into parenttype and fieldname.""" + key = field.split(" as ", 1)[0] + + if key.startswith(("count(", "sum(", "avg(")): + raise ValueError + + if "." in key: + table, column = key.split(".", 2)[:2] + return table[4:-1], column.strip("`") + + return None, key.strip("`") + + +@xhiveframework.whitelist() +def delete_items(): + """delete selected items""" + import json + + items = sorted(json.loads(xhiveframework.form_dict.get("items")), reverse=True) + doctype = xhiveframework.form_dict.get("doctype") + + if len(items) > 10: + xhiveframework.enqueue("xhiveframework.desk.reportview.delete_bulk", doctype=doctype, items=items) + else: + delete_bulk(doctype, items) + + +def delete_bulk(doctype, items): + undeleted_items = [] + for i, d in enumerate(items): + try: + xhiveframework.delete_doc(doctype, d) + if len(items) >= 5: + xhiveframework.publish_realtime( + "progress", + dict( + progress=[i + 1, len(items)], title=_("Deleting {0}").format(doctype), description=d + ), + user=xhiveframework.session.user, + ) + # Commit after successful deletion + xhiveframework.db.commit() + except Exception: + # rollback if any record failed to delete + # if not rollbacked, queries get committed on after_request method in app.py + undeleted_items.append(d) + xhiveframework.db.rollback() + if undeleted_items and len(items) != len(undeleted_items): + xhiveframework.clear_messages() + delete_bulk(doctype, undeleted_items) + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_sidebar_stats(stats, doctype, filters=None): + if filters is None: + filters = [] + + if is_virtual_doctype(doctype): + controller = get_controller(doctype) + args = {"stats": stats, "filters": filters} + data = controller.get_stats(args) + else: + data = get_stats(stats, doctype, filters) + + return {"stats": data} + + +@xhiveframework.whitelist() +@xhiveframework.read_only() +def get_stats(stats, doctype, filters=None): + """get tag info""" + import json + + if filters is None: + filters = [] + columns = json.loads(stats) + if filters: + filters = json.loads(filters) + results = {} + + try: + db_columns = xhiveframework.db.get_table_columns(doctype) + except (xhiveframework.db.InternalError, xhiveframework.db.ProgrammingError): + # raised when _user_tags column is added on the fly + # raised if its a virtual doctype + db_columns = [] + + for column in columns: + if column not in db_columns: + continue + try: + tag_count = xhiveframework.get_list( + doctype, + fields=[column, "count(*)"], + filters=[*filters, [column, "!=", ""]], + group_by=column, + as_list=True, + distinct=1, + ) + + if column == "_user_tags": + results[column] = scrub_user_tags(tag_count) + no_tag_count = xhiveframework.get_list( + doctype, + fields=[column, "count(*)"], + filters=[*filters, [column, "in", ("", ",")]], + as_list=True, + group_by=column, + order_by=column, + ) + + no_tag_count = no_tag_count[0][1] if no_tag_count else 0 + + results[column].append([_("No Tags"), no_tag_count]) + else: + results[column] = tag_count + + except xhiveframework.db.SQLError: + pass + except xhiveframework.db.InternalError: + # raised when _user_tags column is added on the fly + pass + + return results + + +@xhiveframework.whitelist() +def get_filter_dashboard_data(stats, doctype, filters=None): + """get tags info""" + import json + + tags = json.loads(stats) + filters = json.loads(filters or []) + stats = {} + + columns = xhiveframework.db.get_table_columns(doctype) + for tag in tags: + if tag["name"] not in columns: + continue + tagcount = [] + if tag["type"] not in ["Date", "Datetime"]: + tagcount = xhiveframework.get_list( + doctype, + fields=[tag["name"], "count(*)"], + filters=[*filters, "ifnull(`%s`,'')!=''" % tag["name"]], + group_by=tag["name"], + as_list=True, + ) + + if tag["type"] not in [ + "Check", + "Select", + "Date", + "Datetime", + "Int", + "Float", + "Currency", + "Percent", + ] and tag["name"] not in ["docstatus"]: + stats[tag["name"]] = list(tagcount) + if stats[tag["name"]]: + data = [ + "No Data", + xhiveframework.get_list( + doctype, + fields=[tag["name"], "count(*)"], + filters=[*filters, "({0} = '' or {0} is null)".format(tag["name"])], + as_list=True, + )[0][1], + ] + if data and data[1] != 0: + stats[tag["name"]].append(data) + else: + stats[tag["name"]] = tagcount + + return stats + + +def scrub_user_tags(tagcount): + """rebuild tag list for tags""" + rdict = {} + tagdict = dict(tagcount) + for t in tagdict: + if not t: + continue + alltags = t.split(",") + for tag in alltags: + if tag: + if tag not in rdict: + rdict[tag] = 0 + + rdict[tag] += tagdict[t] + + return [[tag, rdict[tag]] for tag in rdict] + + +# used in building query in queries.py +def get_match_cond(doctype, as_condition=True): + cond = DatabaseQuery(doctype).build_match_conditions(as_condition=as_condition) + if not as_condition: + return cond + + return ((" and " + cond) if cond else "").replace("%", "%%") + + +def build_match_conditions(doctype, user=None, as_condition=True): + match_conditions = DatabaseQuery(doctype, user=user).build_match_conditions(as_condition=as_condition) + if as_condition: + return match_conditions.replace("%", "%%") + return match_conditions + + +def get_filters_cond(doctype, filters, conditions, ignore_permissions=None, with_match_conditions=False): + if isinstance(filters, str): + filters = json.loads(filters) + + if filters: + flt = filters + if isinstance(filters, dict): + filters = filters.items() + flt = [] + for f in filters: + if isinstance(f[1], str) and f[1][0] == "!": + flt.append([doctype, f[0], "!=", f[1][1:]]) + elif isinstance(f[1], list | tuple) and f[1][0].lower() in ( + "=", + ">", + "<", + ">=", + "<=", + "!=", + "like", + "not like", + "in", + "not in", + "between", + "is", + ): + flt.append([doctype, f[0], f[1][0], f[1][1]]) + else: + flt.append([doctype, f[0], "=", f[1]]) + + query = DatabaseQuery(doctype) + query.filters = flt + query.conditions = conditions + + if with_match_conditions: + query.build_match_conditions() + + query.build_filter_conditions(flt, conditions, ignore_permissions) + + cond = " and " + " and ".join(query.conditions) + else: + cond = "" + return cond diff --git a/xhiveframework/desk/search.py b/xhiveframework/desk/search.py new file mode 100644 index 0000000..ca66f95 --- /dev/null +++ b/xhiveframework/desk/search.py @@ -0,0 +1,333 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import re +from typing import TypedDict + +from typing_extensions import NotRequired # not required in 3.11+ + +import xhiveframework + +# Backward compatbility +from xhiveframework import _, is_whitelisted, validate_and_sanitize_search_inputs +from xhiveframework.database.schema import SPECIAL_CHAR_PATTERN +from xhiveframework.model.db_query import get_order_by +from xhiveframework.permissions import has_permission +from xhiveframework.utils import cint, cstr, unique +from xhiveframework.utils.data import make_filter_tuple + + +def sanitize_searchfield(searchfield: str): + if not searchfield: + return + + if SPECIAL_CHAR_PATTERN.search(searchfield): + xhiveframework.throw(_("Invalid Search Field {0}").format(searchfield), xhiveframework.DataError) + + +class LinkSearchResults(TypedDict): + value: str + description: str + label: NotRequired[str] + + +# this is called by the Link Field +@xhiveframework.whitelist() +def search_link( + doctype: str, + txt: str, + query: str | None = None, + filters: str | dict | list | None = None, + page_length: int = 10, + searchfield: str | None = None, + reference_doctype: str | None = None, + ignore_user_permissions: bool = False, +) -> list[LinkSearchResults]: + results = search_widget( + doctype, + txt.strip(), + query, + searchfield=searchfield, + page_length=page_length, + filters=filters, + reference_doctype=reference_doctype, + ignore_user_permissions=ignore_user_permissions, + ) + return build_for_autosuggest(results, doctype=doctype) + + +# this is called by the search box +@xhiveframework.whitelist() +def search_widget( + doctype: str, + txt: str, + query: str | None = None, + searchfield: str | None = None, + start: int = 0, + page_length: int = 10, + filters: str | None | dict | list = None, + filter_fields=None, + as_dict: bool = False, + reference_doctype: str | None = None, + ignore_user_permissions: bool = False, +): + start = cint(start) + + if isinstance(filters, str): + filters = json.loads(filters) + + if searchfield: + sanitize_searchfield(searchfield) + + if not searchfield: + searchfield = "name" + + standard_queries = xhiveframework.get_hooks().standard_queries or {} + + if not query and doctype in standard_queries: + query = standard_queries[doctype][-1] + + if query: # Query = custom search query i.e. python function + try: + is_whitelisted(xhiveframework.get_attr(query)) + return xhiveframework.call( + query, + doctype, + txt, + searchfield, + start, + page_length, + filters, + as_dict=as_dict, + reference_doctype=reference_doctype, + ) + except (xhiveframework.PermissionError, xhiveframework.AppNotInstalledError, ImportError): + if xhiveframework.local.conf.developer_mode: + raise + else: + xhiveframework.respond_as_web_page( + title="Invalid Method", + html="Method not found", + indicator_color="red", + http_status_code=404, + ) + return [] + + meta = xhiveframework.get_meta(doctype) + + if isinstance(filters, dict): + filters_items = filters.items() + filters = [] + for key, value in filters_items: + filters.append(make_filter_tuple(doctype, key, value)) + + if filters is None: + filters = [] + or_filters = [] + + # build from doctype + if txt: + field_types = { + "Data", + "Text", + "Small Text", + "Long Text", + "Link", + "Select", + "Read Only", + "Text Editor", + } + search_fields = ["name"] + if meta.title_field: + search_fields.append(meta.title_field) + + if meta.search_fields: + search_fields.extend(meta.get_search_fields()) + + for f in search_fields: + fmeta = meta.get_field(f.strip()) + if not meta.translated_doctype and (f == "name" or (fmeta and fmeta.fieldtype in field_types)): + or_filters.append([doctype, f.strip(), "like", f"%{txt}%"]) + + if meta.get("fields", {"fieldname": "enabled", "fieldtype": "Check"}): + filters.append([doctype, "enabled", "=", 1]) + if meta.get("fields", {"fieldname": "disabled", "fieldtype": "Check"}): + filters.append([doctype, "disabled", "!=", 1]) + + # format a list of fields combining search fields and filter fields + fields = get_std_fields_list(meta, searchfield or "name") + if filter_fields: + fields = list(set(fields + json.loads(filter_fields))) + formatted_fields = [f"`tab{meta.name}`.`{f.strip()}`" for f in fields] + + # Insert title field query after name + if meta.show_title_field_in_link: + formatted_fields.insert(1, f"`tab{meta.name}`.{meta.title_field} as `label`") + + order_by_based_on_meta = get_order_by(doctype, meta) + # `idx` is number of times a document is referred, check link_count.py + order_by = f"`tab{doctype}`.idx desc, {order_by_based_on_meta}" + + if not meta.translated_doctype: + _txt = xhiveframework.db.escape((txt or "").replace("%", "").replace("@", "")) + # locate returns 0 if string is not found, convert 0 to null and then sort null to end in order by + _relevance = f"(1 / nullif(locate({_txt}, `tab{doctype}`.`name`), 0))" + formatted_fields.append(f"""{_relevance} as `_relevance`""") + # Since we are sorting by alias postgres needs to know number of column we are sorting + if xhiveframework.db.db_type == "mariadb": + order_by = f"ifnull(_relevance, -9999) desc, {order_by}" + elif xhiveframework.db.db_type == "postgres": + # Since we are sorting by alias postgres needs to know number of column we are sorting + order_by = f"{len(formatted_fields)} desc nulls last, {order_by}" + + ignore_permissions = doctype == "DocType" or ( + cint(ignore_user_permissions) + and has_permission( + doctype, + ptype="select" if xhiveframework.only_has_select_perm(doctype) else "read", + parent_doctype=reference_doctype, + ) + ) + + values = xhiveframework.get_list( + doctype, + filters=filters, + fields=formatted_fields, + or_filters=or_filters, + limit_start=start, + limit_page_length=None if meta.translated_doctype else page_length, + order_by=order_by, + ignore_permissions=ignore_permissions, + reference_doctype=reference_doctype, + as_list=not as_dict, + strict=False, + ) + + if meta.translated_doctype: + # Filtering the values array so that query is included in very element + values = ( + result + for result in values + if any( + re.search(f"{re.escape(txt)}.*", _(cstr(value)) or "", re.IGNORECASE) + for value in (result.values() if as_dict else result) + ) + ) + + # Sorting the values array so that relevant results always come first + # This will first bring elements on top in which query is a prefix of element + # Then it will bring the rest of the elements and sort them in lexicographical order + values = sorted(values, key=lambda x: relevance_sorter(x, txt, as_dict)) + + # remove _relevance from results + if not meta.translated_doctype: + if as_dict: + for r in values: + r.pop("_relevance", None) + else: + values = [r[:-1] for r in values] + + return values + + +def get_std_fields_list(meta, key): + # get additional search fields + sflist = ["name"] + + if meta.title_field and meta.title_field not in sflist: + sflist.append(meta.title_field) + + if key not in sflist: + sflist.append(key) + + if meta.search_fields: + for d in meta.search_fields.split(","): + if d.strip() not in sflist: + sflist.append(d.strip()) + + return sflist + + +def build_for_autosuggest(res: list[tuple], doctype: str) -> list[LinkSearchResults]: + def to_string(parts): + return ", ".join( + unique(_(cstr(part)) if meta.translated_doctype else cstr(part) for part in parts if part) + ) + + results = [] + meta = xhiveframework.get_meta(doctype) + if meta.show_title_field_in_link: + for item in res: + item = list(item) + if len(item) == 1: + item = [item[0], item[0]] + label = item[1] # use title as label + item[1] = item[0] # show name in description instead of title + if len(item) >= 3 and item[2] == label: + # remove redundant title ("label") value + del item[2] + results.append({"value": item[0], "label": label, "description": to_string(item[1:])}) + else: + results.extend({"value": item[0], "description": to_string(item[1:])} for item in res) + + return results + + +def scrub_custom_query(query, key, txt): + if "%(key)s" in query: + query = query.replace("%(key)s", key) + if "%s" in query: + query = query.replace("%s", ((txt or "") + "%")) + return query + + +def relevance_sorter(key, query, as_dict): + value = _(key.name if as_dict else key[0]) + return (cstr(value).casefold().startswith(query.casefold()) is not True, value) + + +@xhiveframework.whitelist() +def get_names_for_mentions(search_term): + users_for_mentions = xhiveframework.cache.get_value("users_for_mentions", get_users_for_mentions) + user_groups = xhiveframework.cache.get_value("user_groups", get_user_groups) + + filtered_mentions = [] + for mention_data in users_for_mentions + user_groups: + if search_term.lower() not in mention_data.value.lower(): + continue + + mention_data["link"] = xhiveframework.utils.get_url_to_form( + "User Group" if mention_data.get("is_group") else "User Profile", mention_data["id"] + ) + + filtered_mentions.append(mention_data) + + return sorted(filtered_mentions, key=lambda d: d["value"]) + + +def get_users_for_mentions(): + return xhiveframework.get_all( + "User", + fields=["name as id", "full_name as value"], + filters={ + "name": ["not in", ("Administrator", "Guest")], + "allowed_in_mentions": True, + "user_type": "System User", + "enabled": True, + }, + ) + + +def get_user_groups(): + return xhiveframework.get_all("User Group", fields=["name as id", "name as value"], update={"is_group": True}) + + +@xhiveframework.whitelist() +def get_link_title(doctype, docname): + meta = xhiveframework.get_meta(doctype) + + if meta.show_title_field_in_link: + return xhiveframework.db.get_value(doctype, docname, meta.title_field) + + return docname diff --git a/xhiveframework/desk/treeview.py b/xhiveframework/desk/treeview.py new file mode 100644 index 0000000..c752839 --- /dev/null +++ b/xhiveframework/desk/treeview.py @@ -0,0 +1,84 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ + + +@xhiveframework.whitelist() +def get_all_nodes(doctype, label, parent, tree_method, **filters): + """Recursively gets all data from tree nodes""" + + if "cmd" in filters: + del filters["cmd"] + filters.pop("data", None) + + tree_method = xhiveframework.get_attr(tree_method) + + if tree_method not in xhiveframework.whitelisted: + xhiveframework.throw(_("Not Permitted"), xhiveframework.PermissionError) + + data = tree_method(doctype, parent, **filters) + out = [dict(parent=label, data=data)] + + if "is_root" in filters: + del filters["is_root"] + to_check = [d.get("value") for d in data if d.get("expandable")] + + while to_check: + parent = to_check.pop() + data = tree_method(doctype, parent, is_root=False, **filters) + out.append(dict(parent=parent, data=data)) + for d in data: + if d.get("expandable"): + to_check.append(d.get("value")) + + return out + + +@xhiveframework.whitelist() +def get_children(doctype, parent="", **filters): + return _get_children(doctype, parent) + + +def _get_children(doctype, parent="", ignore_permissions=False): + parent_field = "parent_" + doctype.lower().replace(" ", "_") + filters = [[f"ifnull(`{parent_field}`,'')", "=", parent], ["docstatus", "<", 2]] + + meta = xhiveframework.get_meta(doctype) + + return xhiveframework.get_list( + doctype, + fields=[ + "name as value", + "{} as title".format(meta.get("title_field") or "name"), + "is_group as expandable", + ], + filters=filters, + order_by="name", + ignore_permissions=ignore_permissions, + ) + + +@xhiveframework.whitelist() +def add_node(): + args = make_tree_args(**xhiveframework.form_dict) + doc = xhiveframework.get_doc(args) + + doc.save() + + +def make_tree_args(**kwarg): + kwarg.pop("cmd", None) + + doctype = kwarg["doctype"] + parent_field = "parent_" + doctype.lower().replace(" ", "_") + + if kwarg["is_root"] == "false": + kwarg["is_root"] = False + if kwarg["is_root"] == "true": + kwarg["is_root"] = True + + kwarg.update({parent_field: kwarg.get("parent") or kwarg.get(parent_field)}) + + return xhiveframework._dict(kwarg) diff --git a/xhiveframework/desk/utils.py b/xhiveframework/desk/utils.py new file mode 100644 index 0000000..59efd39 --- /dev/null +++ b/xhiveframework/desk/utils.py @@ -0,0 +1,60 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def validate_route_conflict(doctype, name): + """ + Raises exception if name clashes with routes from other documents for /app routing + """ + + if xhiveframework.flags.in_migrate: + return + + all_names = [] + for _doctype in ["Page", "Workspace", "DocType"]: + all_names.extend( + [slug(d) for d in xhiveframework.get_all(_doctype, pluck="name") if (doctype != _doctype and d != name)] + ) + + if slug(name) in all_names: + xhiveframework.msgprint(xhiveframework._("Name already taken, please set a new name")) + raise xhiveframework.NameError + + +def slug(name): + return name.lower().replace(" ", "-") + + +def pop_csv_params(form_dict): + """Pop csv params from form_dict and return them as a dict.""" + from csv import QUOTE_NONNUMERIC + + from xhiveframework.utils.data import cint, cstr + + return { + "delimiter": cstr(form_dict.pop("csv_delimiter", ","))[0], + "quoting": cint(form_dict.pop("csv_quoting", QUOTE_NONNUMERIC)), + } + + +def get_csv_bytes(data: list[list], csv_params: dict) -> bytes: + """Convert data to csv bytes.""" + from csv import writer + from io import StringIO + + file = StringIO() + csv_writer = writer(file, **csv_params) + csv_writer.writerows(data) + + return file.getvalue().encode("utf-8") + + +def provide_binary_file(filename: str, extension: str, content: bytes) -> None: + """Provide a binary file to the client.""" + from xhiveframework import _ + + xhiveframework.response["type"] = "binary" + xhiveframework.response["filecontent"] = content + xhiveframework.response["filename"] = f"{_(filename)}.{extension}" diff --git a/xhiveframework/email/__init__.py b/xhiveframework/email/__init__.py new file mode 100644 index 0000000..96f50f7 --- /dev/null +++ b/xhiveframework/email/__init__.py @@ -0,0 +1,121 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def sendmail_to_system_managers(subject, content): + xhiveframework.sendmail(recipients=get_system_managers(), subject=subject, content=content) + + +@xhiveframework.whitelist() +def get_contact_list(txt, page_length=20) -> list[dict]: + """Return email ids for a multiselect field.""" + from xhiveframework.contacts.doctype.contact.contact import get_full_name + + if cached_contacts := get_cached_contacts(txt): + return cached_contacts[:page_length] + + fields = ["first_name", "middle_name", "last_name", "company_name"] + contacts = xhiveframework.get_list( + "Contact", + fields=[*fields, "`tabContact Email`.email_id"], + filters=[ + ["Contact Email", "email_id", "is", "set"], + ], + or_filters=[[field, "like", f"%{txt}%"] for field in fields] + + [["Contact Email", "email_id", "like", f"%{txt}%"]], + limit_page_length=page_length, + ) + + # The multiselect field will store the `label` as the selected value. + # The `value` is just used as a unique key to distinguish between the options. + # https://lab.membtech.com/xhiveframework/xhiveframework15/blob/6c6a89bcdd9454060a1333e23b855d0505c9ebc2/xhiveframework/public/js/xhiveframework/form/controls/autocomplete.js#L29-L35 + result = [ + xhiveframework._dict( + value=d.email_id, + label=d.email_id, + description=get_full_name(d.first_name, d.middle_name, d.last_name, d.company_name), + ) + for d in contacts + ] + + update_contact_cache(result) + + return result + + +def get_system_managers(): + return xhiveframework.db.sql_list( + """select parent FROM `tabHas Role` + WHERE role='System Manager' + AND parent!='Administrator' + AND parent IN (SELECT email FROM tabUser WHERE enabled=1)""" + ) + + +@xhiveframework.whitelist() +def relink(name, reference_doctype=None, reference_name=None): + xhiveframework.db.sql( + """update + `tabCommunication` + set + reference_doctype = %s, + reference_name = %s, + status = "Linked" + where + communication_type = "Communication" and + name = %s""", + (reference_doctype, reference_name, name), + ) + + +@xhiveframework.whitelist() +@xhiveframework.validate_and_sanitize_search_inputs +def get_communication_doctype(doctype, txt, searchfield, start, page_len, filters): + user_perms = xhiveframework.utils.user.UserPermissions(xhiveframework.session.user) + user_perms.build_permissions() + can_read = user_perms.can_read + from xhiveframework.modules import load_doctype_module + + com_doctypes = [] + if len(txt) < 2: + for name in xhiveframework.get_hooks("communication_doctypes"): + try: + module = load_doctype_module(name, suffix="_dashboard") + if hasattr(module, "get_data"): + for i in module.get_data()["transactions"]: + com_doctypes += i["items"] + except ImportError: + pass + else: + com_doctypes = [ + d[0] for d in xhiveframework.db.get_values("DocType", {"issingle": 0, "istable": 0, "hide_toolbar": 0}) + ] + + return [[dt] for dt in com_doctypes if txt.lower().replace("%", "") in dt.lower() and dt in can_read] + + +def get_cached_contacts(txt): + contacts = xhiveframework.cache.hget("contacts", xhiveframework.session.user) or [] + + if not contacts: + return + + if not txt: + return contacts + + return [ + d + for d in contacts + if (d.value and ((d.value and txt in d.value) or (d.description and txt in d.description))) + ] + + +def update_contact_cache(contacts): + cached_contacts = xhiveframework.cache.hget("contacts", xhiveframework.session.user) or [] + + uncached_contacts = [d for d in contacts if d not in cached_contacts] + cached_contacts.extend(uncached_contacts) + + xhiveframework.cache.hset("contacts", xhiveframework.session.user, cached_contacts) diff --git a/xhiveframework/email/assets/images/email-pull-flow.png b/xhiveframework/email/assets/images/email-pull-flow.png new file mode 100644 index 0000000..1518f9f Binary files /dev/null and b/xhiveframework/email/assets/images/email-pull-flow.png differ diff --git a/xhiveframework/email/doctype/__init__.py b/xhiveframework/email/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/auto_email_report/__init__.py b/xhiveframework/email/doctype/auto_email_report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/auto_email_report/auto_email_report.js b/xhiveframework/email/doctype/auto_email_report/auto_email_report.js new file mode 100644 index 0000000..8a29891 --- /dev/null +++ b/xhiveframework/email/doctype/auto_email_report/auto_email_report.js @@ -0,0 +1,180 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Auto Email Report", { + refresh: function (frm) { + frm.trigger("fetch_report_filters"); + if (!frm.is_new()) { + frm.add_custom_button(__("Download"), function () { + var w = window.open( + xhiveframework.urllib.get_full_url( + "/api/method/xhiveframework.email.doctype.auto_email_report.auto_email_report.download?" + + "name=" + + encodeURIComponent(frm.doc.name) + ) + ); + if (!w) { + xhiveframework.msgprint(__("Please enable pop-ups")); + return; + } + }); + frm.add_custom_button(__("Send Now"), function () { + xhiveframework.call({ + method: "xhiveframework.email.doctype.auto_email_report.auto_email_report.send_now", + args: { name: frm.doc.name }, + callback: function () { + xhiveframework.msgprint(__("Scheduled to send")); + }, + }); + }); + } else { + if (!frm.doc.user) { + frm.set_value("user", xhiveframework.session.user); + } + if (!frm.doc.email_to) { + frm.set_value("email_to", xhiveframework.session.user); + } + } + + frm.set_query("sender", function () { + return { + filters: { + enable_outgoing: 1, + awaiting_password: 0, + }, + }; + }); + }, + report: function (frm) { + frm.set_value("filters", ""); + frm.trigger("fetch_report_filters"); + }, + fetch_report_filters(frm) { + if ( + frm.doc.report && + frm.doc.report_type !== "Report Builder" && + frm.script_setup_for !== frm.doc.report + ) { + xhiveframework.call({ + method: "xhiveframework.desk.query_report.get_script", + args: { + report_name: frm.doc.report, + }, + callback: function (r) { + xhiveframework.dom.eval(r.message.script || ""); + frm.script_setup_for = frm.doc.report; + frm.trigger("show_filters"); + }, + }); + } else { + frm.trigger("show_filters"); + } + }, + show_filters: async function (frm) { + var wrapper = $(frm.get_field("filters_display").wrapper); + wrapper.empty(); + let reference_report = xhiveframework.query_reports[frm.doc.report]; + if (!reference_report || !reference_report.filters) { + reference_report = await xhiveframework.model.with_doc("Report", frm.doc.report); + } + if ( + frm.doc.report_type === "Custom Report" || + (frm.doc.report_type !== "Report Builder" && + reference_report && + reference_report.filters) + ) { + // make a table to show filters + var table = $( + '
      Id + Time + State + Info + Progress / Wait Event +
      \ + \ +
      ' + + __("Filter") + + "" + + __("Value") + + "
      " + ).appendTo(wrapper); + $('

      ' + __("Click table to edit") + "

      ").appendTo( + wrapper + ); + + var filters = {}; + + let report_filters; + + if ( + frm.doc.report_type === "Custom Report" && + reference_report && + reference_report.filters + ) { + if (frm.doc.filters) { + filters = JSON.parse(frm.doc.filters); + } else { + xhiveframework.db.get_value("Report", frm.doc.report, "json", (r) => { + if (r && r.json) { + filters = JSON.parse(r.json).filters || {}; + } + }); + } + + report_filters = xhiveframework.query_reports[frm.doc.reference_report].filters; + } else { + filters = JSON.parse(frm.doc.filters || "{}"); + report_filters = reference_report.filters; + } + + if (report_filters && report_filters.length > 0) { + frm.set_value("filter_meta", JSON.stringify(report_filters)); + if (frm.is_dirty()) { + frm.save(); + } + } + + var report_filters_list = []; + $.each(report_filters, function (key, val) { + // Remove break fieldtype from the filters + if (val.fieldtype != "Break") { + report_filters_list.push(val); + } + }); + report_filters = report_filters_list; + + const mandatory_css = { + "background-color": "var(--error-bg)", + "font-weight": "bold", + }; + + report_filters.forEach((f) => { + const css = f.reqd ? mandatory_css : {}; + const row = $("").appendTo(table.find("tbody")); + $("" + f.label + "").appendTo(row); + $("" + xhiveframework.format(filters[f.fieldname], f) + "") + .css(css) + .appendTo(row); + }); + + table.on("click", function () { + var dialog = new xhiveframework.ui.Dialog({ + fields: report_filters, + primary_action: function () { + var values = this.get_values(); + if (values) { + this.hide(); + frm.set_value("filters", JSON.stringify(values)); + frm.trigger("show_filters"); + } + }, + }); + dialog.show(); + dialog.set_values(filters); + }); + + // populate dynamic date field selection + let date_fields = report_filters + .filter((df) => df.fieldtype === "Date") + .map((df) => ({ label: df.label, value: df.fieldname })); + frm.set_df_property("from_date_field", "options", date_fields); + frm.set_df_property("to_date_field", "options", date_fields); + frm.toggle_display("dynamic_report_filters_section", date_fields.length > 0); + } + }, +}); diff --git a/xhiveframework/email/doctype/auto_email_report/auto_email_report.json b/xhiveframework/email/doctype/auto_email_report/auto_email_report.json new file mode 100644 index 0000000..41beac3 --- /dev/null +++ b/xhiveframework/email/doctype/auto_email_report/auto_email_report.json @@ -0,0 +1,258 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2016-09-01 01:34:34.985457", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "report", + "user", + "enabled", + "column_break_4", + "report_type", + "reference_report", + "filter_data", + "send_if_data", + "data_modified_till", + "no_of_rows", + "report_filters", + "filters_display", + "filters", + "filter_meta", + "dynamic_report_filters_section", + "from_date_field", + "to_date_field", + "column_break_17", + "dynamic_date_period", + "use_first_day_of_period", + "email_settings", + "email_to", + "day_of_week", + "column_break_13", + "sender", + "frequency", + "format", + "section_break_15", + "description" + ], + "fields": [ + { + "fieldname": "report", + "fieldtype": "Link", + "label": "Report", + "options": "Report", + "reqd": 1 + }, + { + "default": "User", + "fieldname": "user", + "fieldtype": "Link", + "label": "Based on Permissions For User", + "options": "User", + "reqd": 1 + }, + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fetch_from": "report.report_type", + "fieldname": "report_type", + "fieldtype": "Read Only", + "label": "Report Type" + }, + { + "fieldname": "filter_data", + "fieldtype": "Section Break", + "label": "Filter Data" + }, + { + "default": "1", + "fieldname": "send_if_data", + "fieldtype": "Check", + "label": "Send only if there is any data" + }, + { + "depends_on": "eval:doc.report_type=='Report Builder'", + "description": "Zero means send records updated at anytime", + "fieldname": "data_modified_till", + "fieldtype": "Int", + "label": "Only Send Records Updated in Last X Hours" + }, + { + "default": "100", + "depends_on": "eval:doc.report_type=='Report Builder'", + "fieldname": "no_of_rows", + "fieldtype": "Int", + "label": "No of Rows (Max 500)" + }, + { + "collapsible": 1, + "depends_on": "eval:doc.report_type !== 'Report Builder'", + "fieldname": "report_filters", + "fieldtype": "Section Break", + "label": "Report Filters" + }, + { + "fieldname": "filters_display", + "fieldtype": "HTML", + "label": "Filters Display" + }, + { + "fieldname": "filters", + "fieldtype": "Text", + "hidden": 1, + "label": "Filters" + }, + { + "fieldname": "filter_meta", + "fieldtype": "Text", + "hidden": 1, + "label": "Filter Meta", + "read_only": 1 + }, + { + "collapsible": 1, + "depends_on": "eval:doc.report_type !== 'Report Builder'", + "fieldname": "dynamic_report_filters_section", + "fieldtype": "Section Break", + "label": "Dynamic Report Filters" + }, + { + "fieldname": "from_date_field", + "fieldtype": "Select", + "label": "From Date Field" + }, + { + "fieldname": "to_date_field", + "fieldtype": "Select", + "label": "To Date Field" + }, + { + "fieldname": "column_break_17", + "fieldtype": "Column Break" + }, + { + "fieldname": "dynamic_date_period", + "fieldtype": "Select", + "label": "Period", + "options": "\nDaily\nWeekly\nMonthly\nQuarterly\nHalf Yearly\nYearly" + }, + { + "fieldname": "email_settings", + "fieldtype": "Section Break", + "label": "Email Settings" + }, + { + "description": "For multiple addresses, enter the address on different line. e.g. test@test.com \u23ce test1@test.com", + "fieldname": "email_to", + "fieldtype": "Small Text", + "label": "Email To", + "reqd": 1 + }, + { + "default": "Monday", + "depends_on": "eval:doc.frequency=='Weekly'", + "fieldname": "day_of_week", + "fieldtype": "Select", + "label": "Day of Week", + "options": "Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday" + }, + { + "fieldname": "column_break_13", + "fieldtype": "Column Break" + }, + { + "fieldname": "frequency", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Frequency", + "options": "Daily\nWeekdays\nWeekly\nMonthly", + "reqd": 1 + }, + { + "fieldname": "format", + "fieldtype": "Select", + "label": "Format", + "options": "HTML\nXLSX\nCSV", + "reqd": 1 + }, + { + "collapsible": 1, + "fieldname": "section_break_15", + "fieldtype": "Section Break", + "label": "Message" + }, + { + "fieldname": "description", + "fieldtype": "Text Editor", + "label": "Message" + }, + { + "fetch_from": "report.reference_report", + "fieldname": "reference_report", + "fieldtype": "Data", + "hidden": 1, + "label": "Reference Report", + "read_only": 1 + }, + { + "fieldname": "sender", + "fieldtype": "Link", + "label": "Sender", + "options": "Email Account" + }, + { + "default": "0", + "depends_on": "eval: doc.dynamic_date_period != 'Daily'", + "description": "To begin the date range at the start of the chosen period. For example, if 'Year' is selected as the period, the report will start from January 1st of the current year.", + "fieldname": "use_first_day_of_period", + "fieldtype": "Check", + "label": "Use First Day of Period" + } + ], + "links": [], + "modified": "2024-02-04 13:31:08.624648", + "modified_by": "Administrator", + "module": "Email", + "name": "Auto Email Report", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Report Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/auto_email_report/auto_email_report.py b/xhiveframework/email/doctype/auto_email_report/auto_email_report.py new file mode 100644 index 0000000..77ccf9d --- /dev/null +++ b/xhiveframework/email/doctype/auto_email_report/auto_email_report.py @@ -0,0 +1,381 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import calendar +import datetime +from datetime import timedelta +from email.utils import formataddr + +import xhiveframework +from xhiveframework import _ +from xhiveframework.desk.query_report import build_xlsx_data +from xhiveframework.model.document import Document +from xhiveframework.model.naming import append_number_if_name_exists +from xhiveframework.utils import ( + add_to_date, + cint, + format_time, + get_first_day, + get_first_day_of_week, + get_link_to_form, + get_quarter_start, + get_url_to_report, + get_year_start, + getdate, + global_date_format, + now, + now_datetime, + today, + validate_email_address, +) +from xhiveframework.utils.csvutils import to_csv +from xhiveframework.utils.xlsxutils import make_xlsx + + +class AutoEmailReport(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data_modified_till: DF.Int + day_of_week: DF.Literal["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + description: DF.TextEditor | None + dynamic_date_period: DF.Literal[ + "", "Daily", "Weekly", "Monthly", "Quarterly", "Half Yearly", "Yearly" + ] + email_to: DF.SmallText + enabled: DF.Check + filter_meta: DF.Text | None + filters: DF.Text | None + format: DF.Literal["HTML", "XLSX", "CSV"] + frequency: DF.Literal["Daily", "Weekdays", "Weekly", "Monthly"] + from_date_field: DF.Literal[None] + no_of_rows: DF.Int + reference_report: DF.Data | None + report: DF.Link + report_type: DF.ReadOnly | None + send_if_data: DF.Check + sender: DF.Link | None + to_date_field: DF.Literal[None] + use_first_day_of_period: DF.Check + user: DF.Link + # end: auto-generated types + + def autoname(self): + self.name = _(self.report) + if xhiveframework.db.exists("Auto Email Report", self.name): + self.name = append_number_if_name_exists("Auto Email Report", self.name) + + def validate(self): + self.validate_report_count() + self.validate_emails() + self.validate_report_format() + self.validate_mandatory_fields() + + @property + def sender_email(self): + return xhiveframework.db.get_value("Email Account", self.sender, "email_id") + + def validate_emails(self): + """Cleanup list of emails""" + if "," in self.email_to: + self.email_to.replace(",", "\n") + + valid = [] + for email in self.email_to.split(): + if email: + validate_email_address(email, True) + valid.append(email) + + self.email_to = "\n".join(valid) + + def validate_report_count(self): + count = xhiveframework.db.count("Auto Email Report", {"user": self.user, "enabled": 1}) + + max_reports_per_user = ( + cint(xhiveframework.local.conf.max_reports_per_user) # kept for backward compatibilty + or cint(xhiveframework.db.get_single_value("System Settings", "max_auto_email_report_per_user")) + or 20 + ) + + if count > max_reports_per_user + (-1 if self.flags.in_insert else 0): + msg = _("Only {0} emailed reports are allowed per user.").format(max_reports_per_user) + msg += " " + _("To allow more reports update limit in System Settings.") + xhiveframework.throw(msg, title=_("Report limit reached")) + + def validate_report_format(self): + """check if user has select correct report format""" + valid_report_formats = ["HTML", "XLSX", "CSV"] + if self.format not in valid_report_formats: + xhiveframework.throw( + _("{0} is not a valid report format. Report format should one of the following {1}").format( + xhiveframework.bold(self.format), xhiveframework.bold(", ".join(valid_report_formats)) + ) + ) + + def validate_mandatory_fields(self): + # Check if all Mandatory Report Filters are filled by the User + filters = xhiveframework.parse_json(self.filters) if self.filters else {} + filter_meta = xhiveframework.parse_json(self.filter_meta) if self.filter_meta else {} + throw_list = [ + meta["label"] for meta in filter_meta if meta.get("reqd") and not filters.get(meta["fieldname"]) + ] + if throw_list: + xhiveframework.throw( + title=_("Missing Filters Required"), + msg=_("Following Report Filters have missing values:") + + "

      • " + + "
      • ".join(throw_list) + + "
      ", + ) + + def get_report_content(self): + """Returns file in for the report in given format""" + report = xhiveframework.get_doc("Report", self.report) + + self.filters = xhiveframework.parse_json(self.filters) if self.filters else {} + + if self.report_type == "Report Builder" and self.data_modified_till: + self.filters["modified"] = (">", now_datetime() - timedelta(hours=self.data_modified_till)) + + if self.report_type != "Report Builder" and self.dynamic_date_filters_set(): + self.prepare_dynamic_filters() + + columns, data = report.get_data( + limit=self.no_of_rows or 100, + user=self.user, + filters=self.filters, + as_dict=True, + ignore_prepared_report=True, + are_default_filters=False, + ) + + # add serial numbers + columns.insert(0, xhiveframework._dict(fieldname="idx", label="", width="30px")) + for i in range(len(data)): + data[i]["idx"] = i + 1 + + if len(data) == 0 and self.send_if_data: + return None + + if self.format == "HTML": + columns, data = make_links(columns, data) + columns = update_field_types(columns) + return self.get_html_table(columns, data) + + elif self.format == "XLSX": + report_data = xhiveframework._dict() + report_data["columns"] = columns + report_data["result"] = data + + xlsx_data, column_widths = build_xlsx_data(report_data, [], 1, ignore_visible_idx=True) + xlsx_file = make_xlsx(xlsx_data, "Auto Email Report", column_widths=column_widths) + return xlsx_file.getvalue() + + elif self.format == "CSV": + report_data = xhiveframework._dict() + report_data["columns"] = columns + report_data["result"] = data + + xlsx_data, column_widths = build_xlsx_data(report_data, [], 1, ignore_visible_idx=True) + return to_csv(xlsx_data) + + else: + xhiveframework.throw(_("Invalid Output Format")) + + def get_html_table(self, columns=None, data=None): + date_time = global_date_format(now()) + " " + format_time(now()) + report_doctype = xhiveframework.db.get_value("Report", self.report, "ref_doctype") + + return xhiveframework.render_template( + "xhiveframework/templates/emails/auto_email_report.html", + { + "title": self.name, + "description": self.description, + "date_time": date_time, + "columns": columns, + "data": data, + "report_url": get_url_to_report(self.report, self.report_type, report_doctype), + "report_name": self.report, + "edit_report_settings": get_link_to_form("Auto Email Report", self.name), + }, + ) + + def get_file_name(self): + return "{}.{}".format(self.report.replace(" ", "-").replace("/", "-"), self.format.lower()) + + def prepare_dynamic_filters(self): + self.filters = xhiveframework.parse_json(self.filters) + + to_date = today() + + if self.use_first_day_of_period: + from_date = to_date + if self.dynamic_date_period == "Daily": + from_date = add_to_date(to_date, days=-1) + elif self.dynamic_date_period == "Weekly": + from_date = get_first_day_of_week(from_date) + elif self.dynamic_date_period == "Monthly": + from_date = get_first_day(from_date) + elif self.dynamic_date_period == "Quarterly": + from_date = get_quarter_start(from_date) + elif self.dynamic_date_period == "Half Yearly": + from_date = get_half_year_start(from_date) + elif self.dynamic_date_period == "Yearly": + from_date = get_year_start(from_date) + + self.set_date_filters(from_date, to_date) + else: + from_date_value = { + "Daily": ("days", -1), + "Weekly": ("weeks", -1), + "Monthly": ("months", -1), + "Quarterly": ("months", -3), + "Half Yearly": ("months", -6), + "Yearly": ("years", -1), + }[self.dynamic_date_period] + + from_date = add_to_date(to_date, **{from_date_value[0]: from_date_value[1]}) + self.set_date_filters(from_date, to_date) + + def set_date_filters(self, from_date, to_date): + self.filters[self.from_date_field] = from_date + self.filters[self.to_date_field] = to_date + + def send(self): + if self.filter_meta and not self.filters: + xhiveframework.throw(_("Please set filters value in Report Filter table.")) + + data = self.get_report_content() + if not data: + return + + attachments = None + if self.format == "HTML": + message = data + else: + message = self.get_html_table() + + if not self.format == "HTML": + attachments = [{"fname": self.get_file_name(), "fcontent": data}] + + xhiveframework.sendmail( + recipients=self.email_to.split(), + sender=formataddr((self.sender, self.sender_email)) if self.sender else "", + subject=self.name, + message=message, + attachments=attachments, + reference_doctype=self.doctype, + reference_name=self.name, + ) + + def dynamic_date_filters_set(self): + return self.dynamic_date_period and self.from_date_field and self.to_date_field + + +@xhiveframework.whitelist() +def download(name): + """Download report locally""" + auto_email_report = xhiveframework.get_doc("Auto Email Report", name) + auto_email_report.check_permission() + data = auto_email_report.get_report_content() + + if not data: + xhiveframework.msgprint(_("No Data")) + return + + xhiveframework.local.response.filecontent = data + xhiveframework.local.response.type = "download" + xhiveframework.local.response.filename = auto_email_report.get_file_name() + + +@xhiveframework.whitelist() +def send_now(name): + """Send Auto Email report now""" + auto_email_report = xhiveframework.get_doc("Auto Email Report", name) + auto_email_report.check_permission() + auto_email_report.send() + + +def send_daily(): + """Check reports to be sent daily""" + + current_day = calendar.day_name[now_datetime().weekday()] + enabled_reports = xhiveframework.get_all( + "Auto Email Report", filters={"enabled": 1, "frequency": ("in", ("Daily", "Weekdays", "Weekly"))} + ) + + for report in enabled_reports: + auto_email_report = xhiveframework.get_doc("Auto Email Report", report.name) + + # if not correct weekday, skip + if auto_email_report.frequency == "Weekdays": + if current_day in ("Saturday", "Sunday"): + continue + elif auto_email_report.frequency == "Weekly": + if auto_email_report.day_of_week != current_day: + continue + try: + auto_email_report.send() + except Exception: + auto_email_report.log_error(f"Failed to send {auto_email_report.name} Auto Email Report") + + +def send_monthly(): + """Check reports to be sent monthly""" + for report in xhiveframework.get_all("Auto Email Report", {"enabled": 1, "frequency": "Monthly"}): + xhiveframework.get_doc("Auto Email Report", report.name).send() + + +def make_links(columns, data): + for row in data: + doc_name = row.get("name") + for col in columns: + if not row.get(col.fieldname): + continue + + if col.fieldtype == "Link": + if col.options and col.options != "Currency": + row[col.fieldname] = get_link_to_form(col.options, row[col.fieldname]) + elif col.fieldtype == "Dynamic Link": + if col.options and row.get(col.options): + row[col.fieldname] = get_link_to_form(row[col.options], row[col.fieldname]) + elif col.fieldtype == "Currency": + doc = None + if doc_name and col.get("parent") and not xhiveframework.get_meta(col.parent).istable: + doc = xhiveframework.get_doc(col.parent, doc_name) + # Pass the Document to get the currency based on docfield option + row[col.fieldname] = xhiveframework.format_value(row[col.fieldname], col, doc=doc) + return columns, data + + +def update_field_types(columns): + for col in columns: + if col.fieldtype in ("Link", "Dynamic Link", "Currency") and col.options != "Currency": + col.fieldtype = "Data" + col.options = "" + return columns + + +DATE_FORMAT = "%Y-%m-%d" + + +def get_half_year_start(as_str=False): + """ + Returns the first day of the current half-year based on the current date. + """ + today_date = getdate(today()) + + half_year = 1 if today_date.month <= 6 else 2 + + year = today_date.year if half_year == 1 else today_date.year + 1 + month = 1 if half_year == 1 else 7 + day = 1 + + result_date = datetime.date(year, month, day) + + return result_date if not as_str else result_date.strftime(DATE_FORMAT) diff --git a/xhiveframework/email/doctype/auto_email_report/test_auto_email_report.py b/xhiveframework/email/doctype/auto_email_report/test_auto_email_report.py new file mode 100644 index 0000000..69dfe99 --- /dev/null +++ b/xhiveframework/email/doctype/auto_email_report/test_auto_email_report.py @@ -0,0 +1,64 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import json + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import add_to_date, get_link_to_form, today +from xhiveframework.utils.data import is_html + +# test_records = xhiveframework.get_test_records('Auto Email Report') + + +class TestAutoEmailReport(XhiveFrameworkTestCase): + def test_auto_email(self): + xhiveframework.delete_doc("Auto Email Report", "Permitted Documents For User") + + auto_email_report = get_auto_email_report() + + data = auto_email_report.get_report_content() + + self.assertTrue(is_html(data)) + self.assertTrue(str(get_link_to_form("Module Def", "Core")) in data) + + auto_email_report.format = "CSV" + + data = auto_email_report.get_report_content() + self.assertTrue('"Language","Core"' in data) + + auto_email_report.format = "XLSX" + + data = auto_email_report.get_report_content() + + def test_dynamic_date_filters(self): + auto_email_report = get_auto_email_report() + + auto_email_report.dynamic_date_period = "Weekly" + auto_email_report.from_date_field = "from_date" + auto_email_report.to_date_field = "to_date" + + auto_email_report.prepare_dynamic_filters() + + self.assertEqual(auto_email_report.filters["from_date"], add_to_date(today(), weeks=-1)) + self.assertEqual(auto_email_report.filters["to_date"], today()) + + +def get_auto_email_report(): + if not xhiveframework.db.exists("Auto Email Report", "Permitted Documents For User"): + auto_email_report = xhiveframework.get_doc( + dict( + doctype="Auto Email Report", + report="Permitted Documents For User", + report_type="Script Report", + user="Administrator", + enabled=1, + email_to="test@example.com", + format="HTML", + frequency="Daily", + filters=json.dumps(dict(user="Administrator", doctype="DocType")), + ) + ).insert() + else: + auto_email_report = xhiveframework.get_doc("Auto Email Report", "Permitted Documents For User") + + return auto_email_report diff --git a/xhiveframework/email/doctype/document_follow/__init__.py b/xhiveframework/email/doctype/document_follow/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/document_follow/document_follow.js b/xhiveframework/email/doctype/document_follow/document_follow.js new file mode 100644 index 0000000..b5f235c --- /dev/null +++ b/xhiveframework/email/doctype/document_follow/document_follow.js @@ -0,0 +1,4 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Document Follow", {}); diff --git a/xhiveframework/email/doctype/document_follow/document_follow.json b/xhiveframework/email/doctype/document_follow/document_follow.json new file mode 100644 index 0000000..5dec268 --- /dev/null +++ b/xhiveframework/email/doctype/document_follow/document_follow.json @@ -0,0 +1,79 @@ +{ + "actions": [], + "creation": "2019-01-09 16:39:23.746535", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ref_doctype", + "ref_docname", + "user" + ], + "fields": [ + { + "fieldname": "ref_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Doctype", + "options": "DocType", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "ref_docname", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Document Name", + "options": "ref_doctype", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "reqd": 1, + "search_index": 1 + } + ], + "in_create": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 22:34:53.394652", + "modified_by": "Administrator", + "module": "Email", + "name": "Document Follow", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/document_follow/document_follow.py b/xhiveframework/email/doctype/document_follow/document_follow.py new file mode 100644 index 0000000..7bcfbf2 --- /dev/null +++ b/xhiveframework/email/doctype/document_follow/document_follow.py @@ -0,0 +1,20 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class DocumentFollow(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + ref_docname: DF.DynamicLink + ref_doctype: DF.Link + user: DF.Link + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/document_follow/test_document_follow.py b/xhiveframework/email/doctype/document_follow/test_document_follow.py new file mode 100644 index 0000000..35dad86 --- /dev/null +++ b/xhiveframework/email/doctype/document_follow/test_document_follow.py @@ -0,0 +1,241 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from dataclasses import dataclass + +import xhiveframework +import xhiveframework.desk.form.document_follow as document_follow +from xhiveframework.desk.form.assign_to import add +from xhiveframework.desk.form.document_follow import get_document_followed_by_user +from xhiveframework.desk.form.utils import add_comment +from xhiveframework.desk.like import toggle_like +from xhiveframework.query_builder import DocType +from xhiveframework.query_builder.functions import Cast_ +from xhiveframework.share import add as share +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDocumentFollow(XhiveFrameworkTestCase): + def test_document_follow_version(self): + user = get_user() + event_doc = get_event() + + event_doc.description = "This is a test description for sending mail" + event_doc.save(ignore_version=False) + + document_follow.unfollow_document("Event", event_doc.name, user.name) + doc = document_follow.follow_document("Event", event_doc.name, user.name) + self.assertEqual(doc.user, user.name) + + document_follow.send_hourly_updates() + emails = get_emails(event_doc, "%This is a test description for sending mail%") + self.assertIsNotNone(emails) + + def test_document_follow_comment(self): + user = get_user() + event_doc = get_event() + + add_comment( + event_doc.doctype, event_doc.name, "This is a test comment", "Administrator@example.com", "Bosh" + ) + + document_follow.unfollow_document("Event", event_doc.name, user.name) + doc = document_follow.follow_document("Event", event_doc.name, user.name) + self.assertEqual(doc.user, user.name) + + document_follow.send_hourly_updates() + emails = get_emails(event_doc, "%This is a test comment%") + self.assertIsNotNone(emails) + + def test_follow_limit(self): + user = get_user() + for _ in range(25): + event_doc = get_event() + document_follow.unfollow_document("Event", event_doc.name, user.name) + doc = document_follow.follow_document("Event", event_doc.name, user.name) + self.assertEqual(doc.user, user.name) + self.assertEqual(len(get_document_followed_by_user(user.name)), 20) + + def test_follow_on_create(self): + user = get_user(DocumentFollowConditions(1)) + xhiveframework.set_user(user.name) + event = get_event() + + event.description = "This is a test description for sending mail" + event.save(ignore_version=False) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertTrue(documents_followed) + + def test_do_not_follow_on_create(self): + user = get_user() + xhiveframework.set_user(user.name) + + event = get_event() + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def test_do_not_follow_on_update(self): + user = get_user() + xhiveframework.set_user(user.name) + event = get_event() + + event.description = "This is a test description for sending mail" + event.save(ignore_version=False) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def test_follow_on_comment(self): + user = get_user(DocumentFollowConditions(0, 1)) + xhiveframework.set_user(user.name) + event = get_event() + + add_comment(event.doctype, event.name, "This is a test comment", "Administrator@example.com", "Bosh") + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertTrue(documents_followed) + + def test_do_not_follow_on_comment(self): + user = get_user() + xhiveframework.set_user(user.name) + event = get_event() + + add_comment(event.doctype, event.name, "This is a test comment", "Administrator@example.com", "Bosh") + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def test_follow_on_like(self): + user = get_user(DocumentFollowConditions(0, 0, 1)) + xhiveframework.set_user(user.name) + event = get_event() + + toggle_like(event.doctype, event.name, add="Yes") + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertTrue(documents_followed) + + def test_do_not_follow_on_like(self): + user = get_user() + xhiveframework.set_user(user.name) + event = get_event() + + toggle_like(event.doctype, event.name) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def test_follow_on_assign(self): + user = get_user(DocumentFollowConditions(0, 0, 0, 1)) + event = get_event() + + add({"assign_to": [user.name], "doctype": event.doctype, "name": event.name}) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertTrue(documents_followed) + + def test_do_not_follow_on_assign(self): + user = get_user() + xhiveframework.set_user(user.name) + event = get_event() + + add({"assign_to": [user.name], "doctype": event.doctype, "name": event.name}) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def test_follow_on_share(self): + user = get_user(DocumentFollowConditions(0, 0, 0, 0, 1)) + event = get_event() + + share(user=user.name, doctype=event.doctype, name=event.name) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertTrue(documents_followed) + + def test_do_not_follow_on_share(self): + user = get_user() + event = get_event() + + share(user=user.name, doctype=event.doctype, name=event.name) + + documents_followed = get_events_followed_by_user(event.name, user.name) + self.assertFalse(documents_followed) + + def tearDown(self): + xhiveframework.db.rollback() + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Email Queue Recipient") + xhiveframework.db.delete("Document Follow") + xhiveframework.db.delete("Event") + + +def get_events_followed_by_user(event_name, user_name): + DocumentFollow = DocType("Document Follow") + return ( + xhiveframework.qb.from_(DocumentFollow) + .where(DocumentFollow.ref_doctype == "Event") + .where(DocumentFollow.ref_docname == event_name) + .where(DocumentFollow.user == user_name) + .select(DocumentFollow.name) + ).run() + + +def get_event(): + doc = xhiveframework.get_doc( + { + "doctype": "Event", + "subject": "_Test_Doc_Follow", + "doc.starts_on": xhiveframework.utils.now(), + "doc.ends_on": xhiveframework.utils.add_days(xhiveframework.utils.now(), 5), + "doc.description": "Hello", + } + ) + doc.insert() + return doc + + +def get_user(document_follow=None): + xhiveframework.set_user("Administrator") + if xhiveframework.db.exists("User", "test@docsub.com"): + doc = xhiveframework.delete_doc("User", "test@docsub.com") + doc = xhiveframework.new_doc("User") + doc.email = "test@docsub.com" + doc.first_name = "Test" + doc.last_name = "User" + doc.send_welcome_email = 0 + doc.document_follow_notify = 1 + doc.document_follow_frequency = "Hourly" + doc.__dict__.update(document_follow.__dict__ if document_follow else {}) + doc.insert() + doc.add_roles("System Manager") + return doc + + +def get_emails(event_doc, search_string): + EmailQueue = DocType("Email Queue") + EmailQueueRecipient = DocType("Email Queue Recipient") + + return ( + xhiveframework.qb.from_(EmailQueue) + .join(EmailQueueRecipient) + .on(EmailQueueRecipient.parent == Cast_(EmailQueue.name, "varchar")) + .where( + EmailQueueRecipient.recipient == "test@docsub.com", + ) + .where(EmailQueue.message.like(f"%{event_doc.doctype}%")) + .where(EmailQueue.message.like(f"%{event_doc.name}%")) + .where(EmailQueue.message.like(search_string)) + .select(EmailQueue.message) + .limit(1) + ).run() + + +@dataclass +class DocumentFollowConditions: + follow_created_documents: int = 0 + follow_commented_documents: int = 0 + follow_liked_documents: int = 0 + follow_assigned_documents: int = 0 + follow_shared_documents: int = 0 diff --git a/xhiveframework/email/doctype/email_account/__init__.py b/xhiveframework/email/doctype/email_account/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_account/email_account.js b/xhiveframework/email/doctype/email_account/email_account.js new file mode 100644 index 0000000..6ff676d --- /dev/null +++ b/xhiveframework/email/doctype/email_account/email_account.js @@ -0,0 +1,232 @@ +xhiveframework.email_defaults = { + GMail: { + email_server: "imap.gmail.com", + incoming_port: 993, + use_ssl: 1, + enable_outgoing: 1, + smtp_server: "smtp.gmail.com", + smtp_port: 587, + use_tls: 1, + use_imap: 1, + }, + "Outlook.com": { + email_server: "imap-mail.outlook.com", + use_ssl: 1, + enable_outgoing: 1, + smtp_server: "smtp-mail.outlook.com", + smtp_port: 587, + use_tls: 1, + use_imap: 1, + }, + Sendgrid: { + enable_outgoing: 1, + smtp_server: "smtp.sendgrid.net", + smtp_port: 587, + use_tls: 1, + }, + SparkPost: { + enable_incoming: 0, + enable_outgoing: 1, + smtp_server: "smtp.sparkpostmail.com", + smtp_port: 587, + use_tls: 1, + }, + "Yahoo Mail": { + email_server: "imap.mail.yahoo.com", + use_ssl: 1, + enable_outgoing: 1, + smtp_server: "smtp.mail.yahoo.com", + smtp_port: 587, + use_tls: 1, + use_imap: 1, + }, + "Yandex.Mail": { + email_server: "imap.yandex.com", + use_ssl: 1, + enable_outgoing: 1, + smtp_server: "smtp.yandex.com", + smtp_port: 587, + use_tls: 1, + use_imap: 1, + }, +}; + +xhiveframework.email_defaults_pop = { + GMail: { + email_server: "pop.gmail.com", + }, + "Outlook.com": { + email_server: "pop3-mail.outlook.com", + }, + "Yahoo Mail": { + email_server: "pop.mail.yahoo.com", + }, + "Yandex.Mail": { + email_server: "pop.yandex.com", + }, +}; + +function oauth_access(frm) { + xhiveframework.model.with_doc("Connected App", frm.doc.connected_app, () => { + const connected_app = xhiveframework.get_doc("Connected App", frm.doc.connected_app); + return xhiveframework.call({ + doc: connected_app, + method: "initiate_web_application_flow", + args: { + success_uri: window.location.pathname, + user: frm.doc.connected_user, + }, + callback: function (r) { + window.open(r.message, "_self"); + }, + }); + }); +} + +function set_default_max_attachment_size(frm) { + if (frm.doc.__islocal && !frm.doc["attachment_limit"]) { + xhiveframework.call({ + method: "xhiveframework.core.api.file.get_max_file_size", + callback: function (r) { + if (!r.exc) { + frm.set_value("attachment_limit", Number(r.message) / (1024 * 1024)); + } + }, + }); + } +} + +xhiveframework.ui.form.on("Email Account", { + service: function (frm) { + $.each(xhiveframework.email_defaults[frm.doc.service], function (key, value) { + frm.set_value(key, value); + }); + if (!frm.doc.use_imap) { + $.each(xhiveframework.email_defaults_pop[frm.doc.service], function (key, value) { + frm.set_value(key, value); + }); + } + }, + + use_imap: function (frm) { + if (!frm.doc.use_imap) { + $.each(xhiveframework.email_defaults_pop[frm.doc.service], function (key, value) { + frm.set_value(key, value); + }); + } else { + $.each(xhiveframework.email_defaults[frm.doc.service], function (key, value) { + frm.set_value(key, value); + }); + } + }, + + enable_incoming: function (frm) { + frm.trigger("warn_autoreply_on_incoming"); + }, + + enable_auto_reply: function (frm) { + frm.trigger("warn_autoreply_on_incoming"); + }, + + notify_if_unreplied: function (frm) { + frm.set_df_property("send_notification_to", "reqd", frm.doc.notify_if_unreplied); + }, + + onload: function (frm) { + frm.set_df_property("append_to", "only_select", true); + frm.set_query( + "append_to", + "xhiveframework.email.doctype.email_account.email_account.get_append_to" + ); + frm.set_query("append_to", "imap_folder", function () { + return { + query: "xhiveframework.email.doctype.email_account.email_account.get_append_to", + }; + }); + if (frm.doc.__islocal) { + frm.add_child("imap_folder", { folder_name: "INBOX" }); + frm.refresh_field("imap_folder"); + } + set_default_max_attachment_size(frm); + frm.events.show_oauth_authorization_message(frm); + }, + + refresh: function (frm) { + frm.events.enable_incoming(frm); + frm.events.notify_if_unreplied(frm); + + if (xhiveframework.route_flags.delete_user_from_locals && xhiveframework.route_flags.linked_user) { + delete xhiveframework.route_flags.delete_user_from_locals; + delete locals["User"][xhiveframework.route_flags.linked_user]; + } + }, + + authorize_api_access: function (frm) { + oauth_access(frm); + }, + + show_oauth_authorization_message(frm) { + if (frm.doc.auth_method === "OAuth" && frm.doc.connected_app) { + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.connected_app.connected_app.has_token", + args: { + connected_app: frm.doc.connected_app, + connected_user: frm.doc.connected_user, + }, + callback: (r) => { + if (!r.message) { + let msg = __( + 'OAuth has been enabled but not authorised. Please use "Authorise API Access" button to do the same.' + ); + frm.dashboard.clear_headline(); + frm.dashboard.set_headline_alert(msg, "yellow"); + } + }, + }); + } + }, + + domain: xhiveframework.utils.debounce((frm) => { + if (frm.doc.domain) { + xhiveframework.call({ + method: "get_domain_values", + doc: frm.doc, + args: { + domain: frm.doc.domain, + }, + callback: function (r) { + if (!r.exc) { + for (let field in r.message) { + frm.set_value(field, r.message[field]); + } + } + }, + }); + } + }), + + email_sync_option: function (frm) { + // confirm if the ALL sync option is selected + + if (frm.doc.email_sync_option == "ALL") { + var msg = __( + "You are selecting Sync Option as ALL, It will resync all read as well as unread message from server. This may also cause the duplication of Communication (emails)." + ); + xhiveframework.confirm(msg, null, function () { + frm.set_value("email_sync_option", "UNSEEN"); + }); + } + }, + + warn_autoreply_on_incoming: function (frm) { + if (frm.doc.enable_incoming && frm.doc.enable_auto_reply && frm.doc.__islocal) { + var msg = __( + "Enabling auto reply on an incoming email account will send automated replies to all the synchronized emails. Do you wish to continue?" + ); + xhiveframework.confirm(msg, null, function () { + frm.set_value("enable_auto_reply", 0); + xhiveframework.show_alert({ message: __("Disabled Auto Reply"), indicator: "blue" }); + }); + } + }, +}); diff --git a/xhiveframework/email/doctype/email_account/email_account.json b/xhiveframework/email/doctype/email_account/email_account.json new file mode 100644 index 0000000..44e6ead --- /dev/null +++ b/xhiveframework/email/doctype/email_account/email_account.json @@ -0,0 +1,642 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:email_account_name", + "creation": "2014-09-11 12:04:34.163728", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "account_section", + "email_id", + "email_account_name", + "column_break_3", + "domain", + "service", + "authentication_column", + "auth_method", + "authorize_api_access", + "password", + "awaiting_password", + "ascii_encode_password", + "column_break_10", + "connected_app", + "connected_user", + "login_id_is_different", + "login_id", + "mailbox_settings", + "enable_incoming", + "default_incoming", + "use_imap", + "use_ssl", + "use_starttls", + "email_server", + "incoming_port", + "column_break_18", + "attachment_limit", + "email_sync_option", + "initial_sync_count", + "section_break_25", + "imap_folder", + "section_break_12", + "append_emails_to_sent_folder", + "append_to", + "create_contact", + "enable_automatic_linking", + "section_break_13", + "notify_if_unreplied", + "unreplied_for_mins", + "send_notification_to", + "outgoing_mail_settings", + "enable_outgoing", + "use_tls", + "use_ssl_for_outgoing", + "smtp_server", + "smtp_port", + "column_break_38", + "default_outgoing", + "always_use_account_email_id_as_sender", + "always_use_account_name_as_sender_name", + "send_unsubscribe_message", + "track_email_status", + "no_smtp_authentication", + "signature_section", + "add_signature", + "signature", + "auto_reply", + "enable_auto_reply", + "auto_reply_message", + "set_footer", + "footer", + "brand_logo", + "uidvalidity", + "uidnext", + "no_failed" + ], + "fields": [ + { + "fieldname": "email_id", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "in_global_search": 1, + "in_list_view": 1, + "label": "Email Address", + "options": "Email", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "fieldname": "login_id_is_different", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use different Email ID" + }, + { + "depends_on": "login_id_is_different", + "fieldname": "login_id", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Alternative Email ID" + }, + { + "depends_on": "eval: doc.auth_method === \"Basic\"", + "fieldname": "password", + "fieldtype": "Password", + "hide_days": 1, + "hide_seconds": 1, + "label": "Password" + }, + { + "default": "0", + "depends_on": "eval: doc.auth_method === \"Basic\"", + "fieldname": "awaiting_password", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Awaiting password" + }, + { + "default": "0", + "depends_on": "eval: doc.auth_method === \"Basic\"", + "fieldname": "ascii_encode_password", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use ASCII encoding for password" + }, + { + "description": "e.g. \"Support\", \"Sales\", \"Jerry Yang\"", + "fieldname": "email_account_name", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Email Account Name", + "unique": 1 + }, + { + "depends_on": "eval:!doc.service", + "fieldname": "domain", + "fieldtype": "Link", + "hide_days": 1, + "hide_seconds": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Domain", + "options": "Email Domain" + }, + { + "depends_on": "eval:!doc.domain", + "fieldname": "service", + "fieldtype": "Select", + "hide_days": 1, + "hide_seconds": 1, + "label": "Service", + "options": "\nGMail\nSendgrid\nSparkPost\nYahoo Mail\nOutlook.com\nYandex.Mail" + }, + { + "fieldname": "mailbox_settings", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Incoming (POP/IMAP) Settings" + }, + { + "default": "0", + "fieldname": "enable_incoming", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Enable Incoming" + }, + { + "default": "0", + "depends_on": "eval: !doc.domain && doc.enable_incoming", + "fetch_from": "domain.use_imap", + "fieldname": "use_imap", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use IMAP" + }, + { + "depends_on": "eval:!doc.domain && doc.enable_incoming", + "description": "e.g. pop.gmail.com / imap.gmail.com", + "fetch_from": "domain.email_server", + "fieldname": "email_server", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Incoming Server" + }, + { + "default": "0", + "depends_on": "eval:!doc.domain && doc.enable_incoming", + "fetch_from": "domain.use_ssl", + "fieldname": "use_ssl", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use SSL" + }, + { + "depends_on": "eval:!doc.domain && doc.enable_incoming", + "description": "Ignore attachments over this size", + "fetch_from": "domain.attachment_limit", + "fieldname": "attachment_limit", + "fieldtype": "Int", + "hide_days": 1, + "hide_seconds": 1, + "label": "Attachment Limit (MB)" + }, + { + "depends_on": "eval: doc.enable_incoming && !doc.use_imap", + "description": "Append as communication against this DocType (must have fields: \"Sender\" and \"Subject\"). These fields can be defined in the email settings section of the appended doctype.", + "fieldname": "append_to", + "fieldtype": "Link", + "hide_days": 1, + "hide_seconds": 1, + "in_standard_filter": 1, + "label": "Append To", + "options": "DocType" + }, + { + "default": "0", + "depends_on": "enable_incoming", + "description": "e.g. replies@yourcomany.com. All replies will come to this inbox.", + "fieldname": "default_incoming", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Default Incoming" + }, + { + "default": "UNSEEN", + "depends_on": "eval: doc.enable_incoming && doc.use_imap", + "fieldname": "email_sync_option", + "fieldtype": "Select", + "hide_days": 1, + "hide_seconds": 1, + "label": "Email Sync Option", + "options": "ALL\nUNSEEN" + }, + { + "default": "250", + "depends_on": "eval: doc.enable_incoming && doc.use_imap", + "description": "Total number of emails to sync in initial sync process ", + "fieldname": "initial_sync_count", + "fieldtype": "Select", + "hide_days": 1, + "hide_seconds": 1, + "label": "Initial Sync Count", + "options": "100\n250\n500" + }, + { + "depends_on": "enable_incoming", + "fieldname": "section_break_13", + "fieldtype": "Column Break", + "hide_days": 1, + "hide_seconds": 1 + }, + { + "default": "0", + "fieldname": "notify_if_unreplied", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Notify if unreplied" + }, + { + "default": "30", + "depends_on": "notify_if_unreplied", + "fieldname": "unreplied_for_mins", + "fieldtype": "Int", + "hide_days": 1, + "hide_seconds": 1, + "label": "Notify if unreplied for (in mins)" + }, + { + "depends_on": "notify_if_unreplied", + "description": "Email Addresses", + "fieldname": "send_notification_to", + "fieldtype": "Small Text", + "hide_days": 1, + "hide_seconds": 1, + "label": "Send Notification to" + }, + { + "fieldname": "outgoing_mail_settings", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Outgoing (SMTP) Settings" + }, + { + "default": "0", + "description": "SMTP Settings for outgoing emails", + "fieldname": "enable_outgoing", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Enable Outgoing" + }, + { + "depends_on": "eval:!doc.domain && doc.enable_outgoing", + "description": "e.g. smtp.gmail.com", + "fetch_from": "domain.smtp_server", + "fieldname": "smtp_server", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Outgoing Server" + }, + { + "default": "0", + "depends_on": "eval:!doc.domain && doc.enable_outgoing", + "fetch_from": "domain.use_tls", + "fieldname": "use_tls", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use TLS" + }, + { + "depends_on": "eval:!doc.domain && doc.enable_outgoing", + "description": "If non standard port (e.g. 587). If on Google Cloud, try port 2525.", + "fetch_from": "domain.smtp_port", + "fieldname": "smtp_port", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Port" + }, + { + "default": "0", + "depends_on": "enable_outgoing", + "description": "Notifications and bulk mails will be sent from this outgoing server.", + "fieldname": "default_outgoing", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Default Outgoing" + }, + { + "default": "0", + "depends_on": "enable_outgoing", + "fieldname": "always_use_account_email_id_as_sender", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Always use this email address as sender address" + }, + { + "default": "0", + "depends_on": "enable_outgoing", + "fieldname": "always_use_account_name_as_sender_name", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Always use this name as sender name" + }, + { + "default": "1", + "fieldname": "send_unsubscribe_message", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Send unsubscribe message in email" + }, + { + "default": "1", + "description": "Track if your email has been opened by the recipient.\n
      \nNote: If you're sending to multiple recipients, even if 1 recipient reads the email, it'll be considered \"Opened\"", + "fieldname": "track_email_status", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Track Email Status" + }, + { + "default": "0", + "fieldname": "no_smtp_authentication", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Disable SMTP server authentication" + }, + { + "collapsible": 1, + "collapsible_depends_on": "add_signature", + "fieldname": "signature_section", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Signature" + }, + { + "default": "0", + "fieldname": "add_signature", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Add Signature" + }, + { + "depends_on": "add_signature", + "fieldname": "signature", + "fieldtype": "Text Editor", + "hide_days": 1, + "hide_seconds": 1, + "label": "Signature" + }, + { + "collapsible": 1, + "collapsible_depends_on": "enable_auto_reply", + "fieldname": "auto_reply", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Auto Reply" + }, + { + "default": "0", + "fieldname": "enable_auto_reply", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Enable Auto Reply" + }, + { + "depends_on": "enable_auto_reply", + "description": "ProTip: Add Reference: {{ reference_doctype }} {{ reference_name }} to send document reference", + "fieldname": "auto_reply_message", + "fieldtype": "Text Editor", + "hide_days": 1, + "hide_seconds": 1, + "label": "Auto Reply Message" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:xhiveframework.utils.html2text(doc.footer || '')!=''", + "fieldname": "set_footer", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Footer" + }, + { + "fieldname": "footer", + "fieldtype": "Text Editor", + "hide_days": 1, + "hide_seconds": 1, + "label": "Footer Content" + }, + { + "fieldname": "uidvalidity", + "fieldtype": "Data", + "hidden": 1, + "hide_days": 1, + "hide_seconds": 1, + "label": "UIDVALIDITY", + "no_copy": 1 + }, + { + "fieldname": "uidnext", + "fieldtype": "Int", + "hidden": 1, + "hide_days": 1, + "hide_seconds": 1, + "label": "UIDNEXT", + "no_copy": 1 + }, + { + "fieldname": "no_failed", + "fieldtype": "Int", + "hidden": 1, + "hide_days": 1, + "hide_seconds": 1, + "label": "no failed attempts", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "section_break_12", + "fieldtype": "Section Break", + "hide_days": 1, + "hide_seconds": 1, + "label": "Document Linking" + }, + { + "default": "0", + "description": "For more information, click here.", + "fieldname": "enable_automatic_linking", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Enable Automatic Linking in Documents" + }, + { + "depends_on": "eval:!doc.domain && doc.enable_incoming", + "description": "If non-standard port (e.g. POP3: 995/110, IMAP: 993/143)", + "fieldname": "incoming_port", + "fieldtype": "Data", + "hide_days": 1, + "hide_seconds": 1, + "label": "Port" + }, + { + "default": "0", + "depends_on": "eval:!doc.domain && doc.enable_outgoing && doc.enable_incoming && doc.use_imap", + "fieldname": "append_emails_to_sent_folder", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Append Emails to Sent Folder" + }, + { + "default": "0", + "depends_on": "eval:!doc.domain && doc.enable_outgoing", + "fieldname": "use_ssl_for_outgoing", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Use SSL" + }, + { + "default": "1", + "fieldname": "create_contact", + "fieldtype": "Check", + "hide_days": 1, + "hide_seconds": 1, + "label": "Create Contacts from Incoming Emails" + }, + { + "fieldname": "brand_logo", + "fieldtype": "Attach Image", + "label": "Brand Logo" + }, + { + "fieldname": "authentication_column", + "fieldtype": "Section Break", + "label": "Authentication" + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_18", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_38", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "account_section", + "fieldtype": "Section Break", + "label": "Account" + }, + { + "depends_on": "eval: doc.use_imap && doc.enable_incoming", + "fieldname": "imap_folder", + "fieldtype": "Table", + "label": "IMAP Folder", + "options": "IMAP Folder" + }, + { + "fieldname": "section_break_25", + "fieldtype": "Section Break", + "label": "IMAP Details" + }, + { + "depends_on": "eval: doc.auth_method === \"OAuth\" && !doc.__islocal && !doc.__unsaved", + "fieldname": "authorize_api_access", + "fieldtype": "Button", + "label": "Authorize API Access" + }, + { + "default": "Basic", + "fieldname": "auth_method", + "fieldtype": "Select", + "label": "Method", + "options": "Basic\nOAuth" + }, + { + "default": "0", + "depends_on": "eval:!doc.domain && doc.enable_incoming && doc.use_imap && !doc.use_ssl", + "fetch_from": "domain.use_starttls", + "fieldname": "use_starttls", + "fieldtype": "Check", + "label": "Use STARTTLS" + }, + { + "depends_on": "eval: doc.auth_method === \"OAuth\"", + "fieldname": "connected_app", + "fieldtype": "Link", + "label": "Connected App", + "mandatory_depends_on": "eval: doc.auth_method === \"OAuth\"", + "options": "Connected App" + }, + { + "depends_on": "eval: doc.auth_method === \"OAuth\"", + "fieldname": "connected_user", + "fieldtype": "Link", + "label": "Connected User", + "mandatory_depends_on": "eval: doc.auth_method === \"OAuth\"", + "options": "User" + } + ], + "icon": "fa fa-inbox", + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-06-05 15:03:08.538819", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Account", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "read": 1, + "role": "System Manager", + "write": 1 + }, + { + "read": 1, + "role": "Inbox User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/email/doctype/email_account/email_account.py b/xhiveframework/email/doctype/email_account/email_account.py new file mode 100755 index 0000000..cb83e18 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/email_account.py @@ -0,0 +1,952 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import email.utils +import functools +import imaplib +import time +from datetime import datetime, timedelta +from poplib import error_proto + +import xhiveframework +from xhiveframework import _, are_emails_muted, safe_encode +from xhiveframework.desk.form import assign_to +from xhiveframework.email.doctype.email_domain.email_domain import EMAIL_DOMAIN_FIELDS +from xhiveframework.email.receive import EmailServer, InboundMail, SentEmailInInboxError +from xhiveframework.email.smtp import SMTPServer +from xhiveframework.email.utils import get_port +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, comma_or, cstr, parse_addr, validate_email_address +from xhiveframework.utils.background_jobs import enqueue, get_jobs +from xhiveframework.utils.jinja import render_template +from xhiveframework.utils.user import get_system_managers + + +class SentEmailInInbox(Exception): + pass + + +def cache_email_account(cache_name): + def decorator_cache_email_account(func): + @functools.wraps(func) + def wrapper_cache_email_account(*args, **kwargs): + if not hasattr(xhiveframework.local, cache_name): + setattr(xhiveframework.local, cache_name, {}) + + cached_accounts = getattr(xhiveframework.local, cache_name) + match_by = [*list(kwargs.values()), "default"] + matched_accounts = list(filter(None, [cached_accounts.get(key) for key in match_by])) + if matched_accounts: + return matched_accounts[0] + + matched_accounts = func(*args, **kwargs) + cached_accounts.update(matched_accounts or {}) + return matched_accounts and next(iter(matched_accounts.values())) + + return wrapper_cache_email_account + + return decorator_cache_email_account + + +class EmailAccount(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.email.doctype.imap_folder.imap_folder import IMAPFolder + from xhiveframework.types import DF + + add_signature: DF.Check + always_use_account_email_id_as_sender: DF.Check + always_use_account_name_as_sender_name: DF.Check + append_emails_to_sent_folder: DF.Check + append_to: DF.Link | None + ascii_encode_password: DF.Check + attachment_limit: DF.Int + auth_method: DF.Literal["Basic", "OAuth"] + auto_reply_message: DF.TextEditor | None + awaiting_password: DF.Check + brand_logo: DF.AttachImage | None + connected_app: DF.Link | None + connected_user: DF.Link | None + create_contact: DF.Check + default_incoming: DF.Check + default_outgoing: DF.Check + domain: DF.Link | None + email_account_name: DF.Data | None + email_id: DF.Data + email_server: DF.Data | None + email_sync_option: DF.Literal["ALL", "UNSEEN"] + enable_auto_reply: DF.Check + enable_automatic_linking: DF.Check + enable_incoming: DF.Check + enable_outgoing: DF.Check + footer: DF.TextEditor | None + imap_folder: DF.Table[IMAPFolder] + incoming_port: DF.Data | None + initial_sync_count: DF.Literal["100", "250", "500"] + login_id: DF.Data | None + login_id_is_different: DF.Check + no_failed: DF.Int + no_smtp_authentication: DF.Check + notify_if_unreplied: DF.Check + password: DF.Password | None + send_notification_to: DF.SmallText | None + send_unsubscribe_message: DF.Check + service: DF.Literal[ + "", + "GMail", + "Sendgrid", + "SparkPost", + "Yahoo Mail", + "Outlook.com", + "Yandex.Mail", + ] + signature: DF.TextEditor | None + smtp_port: DF.Data | None + smtp_server: DF.Data | None + track_email_status: DF.Check + uidnext: DF.Int + uidvalidity: DF.Data | None + unreplied_for_mins: DF.Int + use_imap: DF.Check + use_ssl: DF.Check + use_ssl_for_outgoing: DF.Check + use_starttls: DF.Check + use_tls: DF.Check + # end: auto-generated types + DOCTYPE = "Email Account" + + def autoname(self): + """Set name as `email_account_name` or make title from Email Address.""" + if not self.email_account_name: + self.email_account_name = ( + self.email_id.split("@", 1)[0].replace("_", " ").replace(".", " ").replace("-", " ").title() + ) + + self.name = self.email_account_name + + def validate(self): + """Validate Email Address and check POP3/IMAP and SMTP connections is enabled.""" + + if self.email_id: + validate_email_address(self.email_id, True) + + if self.login_id_is_different: + if not self.login_id: + xhiveframework.throw(_("Login Id is required")) + else: + self.login_id = None + + # validate the imap settings + if self.enable_incoming and self.use_imap and len(self.imap_folder) <= 0: + xhiveframework.throw(_("You need to set one IMAP folder for {0}").format(xhiveframework.bold(self.email_id))) + + if xhiveframework.local.flags.in_patch or xhiveframework.local.flags.in_test: + return + + use_oauth = self.auth_method == "OAuth" + validate_oauth = use_oauth and not (self.is_new() and not self.get_oauth_token()) + self.use_starttls = cint(self.use_imap and self.use_starttls and not self.use_ssl) + + if use_oauth: + # no need for awaiting password for oauth + self.awaiting_password = 0 + self.password = None + + if not xhiveframework.local.flags.in_install and not self.awaiting_password: + if validate_oauth or self.password or self.smtp_server in ("127.0.0.1", "localhost"): + if self.enable_incoming: + self.get_incoming_server() + self.no_failed = 0 + + if self.enable_outgoing: + self.validate_smtp_conn() + else: + if self.enable_incoming or (self.enable_outgoing and not self.no_smtp_authentication): + if not use_oauth: + xhiveframework.throw(_("Password is required or select Awaiting Password")) + + if self.notify_if_unreplied: + if not self.send_notification_to: + xhiveframework.throw(_("{0} is mandatory").format(self.meta.get_label("send_notification_to"))) + for e in self.get_unreplied_notification_emails(): + validate_email_address(e, True) + + if self.enable_incoming: + for folder in self.imap_folder: + if folder.append_to: + valid_doctypes = [d[0] for d in get_append_to()] + if folder.append_to not in valid_doctypes: + xhiveframework.throw(_("Append To can be one of {0}").format(comma_or(valid_doctypes))) + + def validate_smtp_conn(self): + if not self.smtp_server: + xhiveframework.throw(_("SMTP Server is required")) + + server = self.get_smtp_server() + return server.session + + def before_save(self): + messages = [] + as_list = 1 + if not self.enable_incoming and self.default_incoming: + self.default_incoming = False + messages.append( + _("{} has been disabled. It can only be enabled if {} is checked.").format( + xhiveframework.bold(_("Default Incoming")), + xhiveframework.bold(_("Enable Incoming")), + ) + ) + if not self.enable_outgoing and self.default_outgoing: + self.default_outgoing = False + messages.append( + _("{} has been disabled. It can only be enabled if {} is checked.").format( + xhiveframework.bold(_("Default Outgoing")), + xhiveframework.bold(_("Enable Outgoing")), + ) + ) + if messages: + if len(messages) == 1: + (as_list, messages) = (0, messages[0]) + xhiveframework.msgprint( + messages, + as_list=as_list, + indicator="orange", + title=_("Defaults Updated"), + ) + + def on_update(self): + """Check there is only one default of each type.""" + self.check_automatic_linking_email_account() + self.there_must_be_only_one_default() + setup_user_email_inbox( + email_account=self.name, + awaiting_password=self.awaiting_password, + email_id=self.email_id, + enable_outgoing=self.enable_outgoing, + used_oauth=self.auth_method == "OAuth", + ) + + def there_must_be_only_one_default(self): + """If current Email Account is default, un-default all other accounts.""" + for field in ("default_incoming", "default_outgoing"): + if not self.get(field): + continue + + for email_account in xhiveframework.get_all("Email Account", filters={field: 1}): + if email_account.name == self.name: + continue + + email_account = xhiveframework.get_doc("Email Account", email_account.name) + email_account.set(field, 0) + email_account.save() + + @xhiveframework.whitelist() + def get_domain_values(self, domain: str): + return xhiveframework.db.get_value("Email Domain", domain, EMAIL_DOMAIN_FIELDS, as_dict=True) + + def get_incoming_server(self, in_receive=False, email_sync_rule="UNSEEN"): + """Returns logged in POP3/IMAP connection object.""" + oauth_token = self.get_oauth_token() + args = xhiveframework._dict( + { + "email_account_name": self.email_account_name, + "email_account": self.name, + "host": self.email_server, + "use_ssl": self.use_ssl, + "use_starttls": self.use_starttls, + "username": getattr(self, "login_id", None) or self.email_id, + "use_imap": self.use_imap, + "email_sync_rule": email_sync_rule, + "incoming_port": get_port(self), + "initial_sync_count": self.initial_sync_count or 100, + "use_oauth": self.auth_method == "OAuth", + "access_token": oauth_token.get_password("access_token") if oauth_token else None, + } + ) + + if self.password: + args.password = self.get_password() + + if not args.get("host"): + xhiveframework.throw(_("{0} is required").format("Email Server")) + + email_server = EmailServer(xhiveframework._dict(args)) + self.check_email_server_connection(email_server, in_receive) + + if not in_receive and self.use_imap: + email_server.imap.logout() + + return email_server + + def check_email_server_connection(self, email_server, in_receive): + # tries to connect to email server and handles failure + try: + email_server.connect() + + # reset failed attempts count - do it after succesful connection + self.set_failed_attempts_count(0) + except (error_proto, imaplib.IMAP4.error) as e: + message = cstr(e).lower().replace(" ", "") + auth_error_codes = [ + "authenticationfailed", + "loginfailed", + ] + + other_error_codes = [ + "err[auth]", + "errtemporaryerror", + "loginviayourwebbrowser", + ] + + all_error_codes = auth_error_codes + other_error_codes + + if in_receive and any(map(lambda t: t in message, all_error_codes)): + # if called via self.receive and it leads to authentication error, + # disable incoming and send email to System Manager + error_message = _( + "Authentication failed while receiving emails from Email Account: {0}." + ).format(self.name) + + error_message = _("Email Account Disabled.") + " " + error_message + error_message += "
      " + _("Message from server: {0}").format(cstr(e)) + self.handle_incoming_connect_error(description=error_message) + return None + + elif not in_receive and any(map(lambda t: t in message, auth_error_codes)): + SMTPServer.throw_invalid_credentials_exception() + else: + xhiveframework.throw(cstr(e)) + + except OSError: + if in_receive: + # timeout while connecting, see receive.py connect method + description = xhiveframework.message_log.pop() if xhiveframework.message_log else "Socket Error" + self.db_set("no_failed", self.no_failed + 1) + if self.no_failed > 2: + self.handle_incoming_connect_error(description=description) + return + + raise + + @property + def _password(self): + raise_exception = not ( + self.auth_method == "OAuth" or self.no_smtp_authentication or xhiveframework.flags.in_test + ) + return self.get_password(raise_exception=raise_exception) + + @property + def default_sender(self): + return email.utils.formataddr((self.name, self.get("email_id"))) + + def is_exists_in_db(self): + """Some of the Email Accounts we create from configs and those doesn't exists in DB. + This is is to check the specific email account exists in DB or not. + """ + return self.find_one_by_filters(name=self.name) + + @classmethod + def from_record(cls, record): + email_account = xhiveframework.new_doc(cls.DOCTYPE) + email_account.update(record) + return email_account + + @classmethod + def find(cls, name): + return xhiveframework.get_doc(cls.DOCTYPE, name) + + @classmethod + def find_one_by_filters(cls, **kwargs) -> "EmailAccount": + name = xhiveframework.db.get_value(cls.DOCTYPE, kwargs) + return cls.find(name) if name else None + + @classmethod + def find_from_config(cls): + config = cls.get_account_details_from_site_config() + if config: + account = cls.from_record(config) + account._from_site_config = True + return account + + @classmethod + def create_dummy(cls): + return cls.from_record({"sender": "notifications@example.com"}) + + @classmethod + @cache_email_account("outgoing_email_account") + def find_outgoing(cls, match_by_email=None, match_by_doctype=None, _raise_error=False): + """Find the outgoing Email account to use. + + :param match_by_email: Find account using emailID + :param match_by_doctype: Find account by matching `Append To` doctype + :param _raise_error: This is used by raise_error_on_no_output decorator to raise error. + """ + if match_by_email: + match_by_email = parse_addr(match_by_email)[1] + doc = cls.find_one_by_filters(enable_outgoing=1, email_id=match_by_email) + if doc: + return {match_by_email: doc} + + if match_by_doctype: + doc = cls.find_one_by_filters(enable_outgoing=1, enable_incoming=1, append_to=match_by_doctype) + if doc: + return {match_by_doctype: doc} + + doc = cls.find_default_outgoing() + if doc: + return {"default": doc} + + if _raise_error: + xhiveframework.throw( + _("Please setup default Email Account from Settings > Email Account"), + xhiveframework.OutgoingEmailError, + ) + + @classmethod + def find_default_outgoing(cls): + """Find default outgoing account.""" + doc = cls.find_one_by_filters(enable_outgoing=1, default_outgoing=1) + doc = doc or cls.find_from_config() + return doc or (are_emails_muted() and cls.create_dummy()) + + @classmethod + def find_incoming(cls, match_by_email=None, match_by_doctype=None): + """Find the incoming Email account to use. + :param match_by_email: Find account using emailID + :param match_by_doctype: Find account by matching `Append To` doctype + """ + doc = cls.find_one_by_filters(enable_incoming=1, email_id=match_by_email) + if doc: + return doc + + doc = cls.find_one_by_filters(enable_incoming=1, append_to=match_by_doctype) + if doc: + return doc + + doc = cls.find_default_incoming() + return doc + + @classmethod + def find_default_incoming(cls): + return cls.find_one_by_filters(enable_incoming=1, default_incoming=1) + + @classmethod + def get_account_details_from_site_config(cls): + if not xhiveframework.conf.get("mail_server"): + return {} + + field_to_conf_name_map = { + "smtp_server": {"conf_names": ("mail_server",)}, + "smtp_port": {"conf_names": ("mail_port",)}, + "use_tls": {"conf_names": ("use_tls", "mail_login")}, + "login_id": {"conf_names": ("mail_login",)}, + "email_id": { + "conf_names": ("auto_email_id", "mail_login"), + "default": "notifications@example.com", + }, + "password": {"conf_names": ("mail_password",)}, + "always_use_account_email_id_as_sender": { + "conf_names": ("always_use_account_email_id_as_sender",), + "default": 0, + }, + "always_use_account_name_as_sender_name": { + "conf_names": ("always_use_account_name_as_sender_name",), + "default": 0, + }, + "name": {"conf_names": ("email_sender_name",), "default": "XhiveFramework"}, + "auth_method": {"conf_names": ("auth_method"), "default": "Basic"}, + "from_site_config": {"default": True}, + "no_smtp_authentication": { + "conf_names": ("disable_mail_smtp_authentication",), + "default": 0, + }, + } + + account_details = {} + for doc_field_name, d in field_to_conf_name_map.items(): + conf_names, default = d.get("conf_names") or [], d.get("default") + value = [xhiveframework.conf.get(k) for k in conf_names if xhiveframework.conf.get(k)] + account_details[doc_field_name] = (value and value[0]) or default + + return account_details + + def sendmail_config(self): + oauth_token = self.get_oauth_token() + + return { + "email_account": self.name, + "server": self.smtp_server, + "port": cint(self.smtp_port), + "login": getattr(self, "login_id", None) or self.email_id, + "password": self._password, + "use_ssl": cint(self.use_ssl_for_outgoing), + "use_tls": cint(self.use_tls), + "use_oauth": self.auth_method == "OAuth", + "access_token": oauth_token.get_password("access_token") if oauth_token else None, + } + + def get_smtp_server(self): + """Get SMTPServer (wrapper around actual smtplib object) for this account. + + Implementation Detail: Since SMTPServer is same for each email connection, the same *instance* + is returned every time this function is called from same EmailAccount object. + This enables reusabilty of connection for better performance.""" + return self._smtp_server_instance + + @functools.cached_property + def _smtp_server_instance(self): + config = self.sendmail_config() + return SMTPServer(**config) + + def remove_unpicklable_values(self, state): + super().remove_unpicklable_values(state) + state.pop("_smtp_server_instance", None) + + def handle_incoming_connect_error(self, description): + if self.get_failed_attempts_count() > 5: + # This is done in background to avoid committing here. + xhiveframework.enqueue(self._disable_broken_incoming_account, description=description) + else: + self.set_failed_attempts_count(self.get_failed_attempts_count() + 1) + + def _disable_broken_incoming_account(self, description): + if xhiveframework.flags.in_test: + return + self.db_set("enable_incoming", 0) + + for user in get_system_managers(only_name=True): + try: + assign_to.add( + { + "assign_to": [user], + "doctype": self.doctype, + "name": self.name, + "description": description, + "priority": "High", + "notify": 1, + } + ) + except assign_to.DuplicateToDoError: + pass + + def set_failed_attempts_count(self, value): + xhiveframework.cache.set_value(f"{self.name}:email-account-failed-attempts", value) + + def get_failed_attempts_count(self): + return cint(xhiveframework.cache.get_value(f"{self.name}:email-account-failed-attempts")) + + def receive(self): + """Called by scheduler to receive emails from this EMail account using POP3/IMAP.""" + exceptions = [] + inbound_mails = self.get_inbound_mails() + for mail in inbound_mails: + try: + communication = mail.process() + xhiveframework.db.commit() + # If email already exists in the system + # then do not send notifications for the same email. + if communication and mail.flags.is_new_communication: + # notify all participants of this thread + if self.enable_auto_reply: + self.send_auto_reply(communication, mail) + + communication.send_email(is_inbound_mail_communcation=True) + except SentEmailInInboxError: + xhiveframework.db.rollback() + except Exception: + xhiveframework.db.rollback() + self.log_error(title="EmailAccount.receive") + if self.use_imap: + self.handle_bad_emails(mail.uid, mail.raw_message, xhiveframework.get_traceback()) + exceptions.append(xhiveframework.get_traceback()) + else: + xhiveframework.db.commit() + + if exceptions: + raise Exception(xhiveframework.as_json(exceptions)) + + def get_inbound_mails(self) -> list[InboundMail]: + """retrive and return inbound mails.""" + mails = [] + + def process_mail(messages, append_to=None): + for index, message in enumerate(messages.get("latest_messages", [])): + uid = messages["uid_list"][index] if messages.get("uid_list") else None + seen_status = messages.get("seen_status", {}).get(uid) + if self.email_sync_option != "UNSEEN" or seen_status != "SEEN": + # only append the emails with status != 'SEEN' if sync option is set to 'UNSEEN' + mails.append( + InboundMail( + message, + self, + xhiveframework.safe_decode(uid), + seen_status, + append_to, + ) + ) + + if not self.enable_incoming: + return [] + + email_sync_rule = self.build_email_sync_rule() + try: + email_server = self.get_incoming_server(in_receive=True, email_sync_rule=email_sync_rule) + if self.use_imap: + # process all given imap folder + for folder in self.imap_folder: + if email_server.select_imap_folder(folder.folder_name): + email_server.settings["uid_validity"] = folder.uidvalidity + messages = email_server.get_messages(folder=f'"{folder.folder_name}"') or {} + process_mail(messages, folder.append_to) + else: + # process the pop3 account + messages = email_server.get_messages() or {} + process_mail(messages) + # close connection to mailserver + email_server.logout() + except Exception: + self.log_error(title=_("Error while connecting to email account {0}").format(self.name)) + return [] + return mails + + def handle_bad_emails(self, uid, raw, reason): + if cint(self.use_imap): + import email + + try: + if isinstance(raw, bytes): + mail = email.message_from_bytes(raw) + else: + mail = email.message_from_string(raw) + + message_id = mail.get("Message-ID") + except Exception: + message_id = "can't be parsed" + + unhandled_email = xhiveframework.get_doc( + { + "raw": raw, + "uid": uid, + "reason": reason, + "message_id": message_id, + "doctype": "Unhandled Email", + "email_account": self.name, + } + ) + unhandled_email.insert(ignore_permissions=True) + xhiveframework.db.commit() + + def send_auto_reply(self, communication, email): + """Send auto reply if set.""" + from xhiveframework.core.doctype.communication.email import ( + set_incoming_outgoing_accounts, + ) + + if self.enable_auto_reply: + set_incoming_outgoing_accounts(communication) + + unsubscribe_message = (self.send_unsubscribe_message and _("Leave this conversation")) or "" + + xhiveframework.sendmail( + recipients=[email.from_email], + sender=self.email_id, + reply_to=communication.incoming_email_account, + subject=" ".join([_("Re:"), communication.subject]), + content=render_template(self.auto_reply_message or "", communication.as_dict()) + or xhiveframework.get_template("templates/emails/auto_reply.html").render(communication.as_dict()), + reference_doctype=communication.reference_doctype, + reference_name=communication.reference_name, + in_reply_to=email.mail.get("Message-Id"), # send back the Message-Id as In-Reply-To + unsubscribe_message=unsubscribe_message, + ) + + def get_unreplied_notification_emails(self): + """Return list of emails listed""" + self.send_notification_to = self.send_notification_to.replace(",", "\n") + return [e.strip() for e in self.send_notification_to.split("\n") if e.strip()] + + def on_trash(self): + """Clear communications where email account is linked""" + Communication = xhiveframework.qb.DocType("Communication") + xhiveframework.qb.update(Communication).set(Communication.email_account, "").where( + Communication.email_account == self.name + ).run() + + remove_user_email_inbox(email_account=self.name) + + def after_rename(self, old, new, merge=False): + xhiveframework.db.set_value("Email Account", new, "email_account_name", new) + + def build_email_sync_rule(self): + if not self.use_imap: + return "UNSEEN" + + if self.email_sync_option == "ALL": + max_uid = get_max_email_uid(self.name) + last_uid = max_uid + int(self.initial_sync_count or 100) if max_uid == 1 else "*" + return f"UID {max_uid}:{last_uid}" + else: + return self.email_sync_option or "UNSEEN" + + def check_automatic_linking_email_account(self): + if self.enable_automatic_linking: + if not self.enable_incoming: + xhiveframework.throw(_("Automatic Linking can be activated only if Incoming is enabled.")) + + if xhiveframework.db.exists( + "Email Account", + {"enable_automatic_linking": 1, "name": ("!=", self.name)}, + ): + xhiveframework.throw(_("Automatic Linking can be activated only for one Email Account.")) + + def append_email_to_sent_folder(self, message): + if not (self.enable_incoming and self.use_imap): + # don't try appending if enable incoming and imap is not set + return + + try: + email_server = self.get_incoming_server(in_receive=True) + message = safe_encode(message) + email_server.imap.append("Sent", "\\Seen", imaplib.Time2Internaldate(time.time()), message) + except Exception: + self.log_error("Unable to add to Sent folder") + + def get_oauth_token(self): + if self.auth_method == "OAuth": + connected_app = xhiveframework.get_doc("Connected App", self.connected_app) + return connected_app.get_active_token(self.connected_user) + + +@xhiveframework.whitelist() +def get_append_to(doctype=None, txt=None, searchfield=None, start=None, page_len=None, filters=None): + txt = txt if txt else "" + + filters = {"istable": 0, "issingle": 0, "email_append_to": 1} + # Set Email Append To DocTypes via DocType + email_append_to_list = [ + dt.name for dt in xhiveframework.get_all("DocType", filters=filters, fields=["name", "email_append_to"]) + ] + # Set Email Append To DocTypes set via Customize Form + email_append_to_list.extend( + dt.doc_type + for dt in xhiveframework.get_list( + "Property Setter", + filters={"property": "email_append_to", "value": 1}, + fields=["doc_type"], + ) + ) + return [[d] for d in set(email_append_to_list) if txt in d] + + +def notify_unreplied(): + """Sends email notifications if there are unreplied Communications + and `notify_if_unreplied` is set as true.""" + for email_account in xhiveframework.get_all( + "Email Account", + "name", + filters={"enable_incoming": 1, "notify_if_unreplied": 1}, + ): + email_account = xhiveframework.get_doc("Email Account", email_account.name) + + if email_account.use_imap: + append_to = [folder.get("append_to") for folder in email_account.imap_folder] + else: + append_to = email_account.append_to + + if append_to: + # get open communications younger than x mins, for given doctype + for comm in xhiveframework.get_all( + "Communication", + "name", + filters=[ + {"sent_or_received": "Received"}, + {"reference_doctype": ("in", append_to)}, + {"unread_notification_sent": 0}, + {"email_account": email_account.name}, + { + "creation": ( + "<", + datetime.now() - timedelta(seconds=(email_account.unreplied_for_mins or 30) * 60), + ) + }, + { + "creation": ( + ">", + datetime.now() + - timedelta(seconds=(email_account.unreplied_for_mins or 30) * 60 * 3), + ) + }, + ], + ): + comm = xhiveframework.get_doc("Communication", comm.name) + + if xhiveframework.db.get_value(comm.reference_doctype, comm.reference_name, "status") == "Open": + # if status is still open + xhiveframework.sendmail( + recipients=email_account.get_unreplied_notification_emails(), + content=comm.content, + subject=comm.subject, + doctype=comm.reference_doctype, + name=comm.reference_name, + ) + + # update flag + comm.db_set("unread_notification_sent", 1) + + +def pull(now=False): + """Will be called via scheduler, pull emails from all enabled Email accounts.""" + from xhiveframework.integrations.doctype.connected_app.connected_app import has_token + + doctype = xhiveframework.qb.DocType("Email Account") + email_accounts = ( + xhiveframework.qb.from_(doctype) + .select( + doctype.name, + doctype.auth_method, + doctype.connected_app, + doctype.connected_user, + ) + .where(doctype.enable_incoming == 1) + .where(doctype.awaiting_password == 0) + .run(as_dict=1) + ) + + for email_account in email_accounts: + if email_account.auth_method == "OAuth" and not has_token( + email_account.connected_app, email_account.connected_user + ): + # don't try to pull from accounts which dont have access token (for Oauth) + continue + + if now: + pull_from_email_account(email_account.name) + + else: + # job_name is used to prevent duplicates in queue + job_name = f"pull_from_email_account|{email_account.name}" + + queued_jobs = get_jobs(site=xhiveframework.local.site, key="job_name")[xhiveframework.local.site] + if job_name not in queued_jobs: + enqueue( + pull_from_email_account, + "short", + event="all", + job_name=job_name, + email_account=email_account.name, + ) + + +def pull_from_email_account(email_account): + """Runs within a worker process""" + email_account = xhiveframework.get_doc("Email Account", email_account) + email_account.receive() + + +def get_max_email_uid(email_account): + """get maximum uid of emails""" + + if result := xhiveframework.get_all( + "Communication", + filters={ + "communication_medium": "Email", + "sent_or_received": "Received", + "email_account": email_account, + }, + fields=["max(uid) as uid"], + ): + return cint(result[0].get("uid", 0)) + 1 + return 1 + + +def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_outgoing, used_oauth): + """setup email inbox for user""" + from xhiveframework.core.doctype.user.user import ask_pass_update + + def add_user_email(user): + user = xhiveframework.get_doc("User", user) + row = user.append("user_emails", {}) + + row.email_id = email_id + row.email_account = email_account + row.awaiting_password = awaiting_password or 0 + row.used_oauth = used_oauth or 0 + row.enable_outgoing = enable_outgoing or 0 + + user.save(ignore_permissions=True) + + update_user_email_settings = False + if not all([email_account, email_id]): + return + + user_names = xhiveframework.db.get_values("User", {"email": email_id}, as_dict=True) + if not user_names: + return + + for user in user_names: + user_name = user.get("name") + + # check if inbox is alreay configured + user_inbox = ( + xhiveframework.db.get_value( + "User Email", + {"email_account": email_account, "parent": user_name}, + ["name"], + ) + or None + ) + + if not user_inbox: + add_user_email(user_name) + else: + # update awaiting password for email account + update_user_email_settings = True + + if update_user_email_settings: + UserEmail = xhiveframework.qb.DocType("User Email") + xhiveframework.qb.update(UserEmail).set(UserEmail.awaiting_password, (awaiting_password or 0)).set( + UserEmail.enable_outgoing, (enable_outgoing or 0) + ).set(UserEmail.used_oauth, (used_oauth or 0)).where(UserEmail.email_account == email_account).run() + + else: + users = " and ".join([xhiveframework.bold(user.get("name")) for user in user_names]) + xhiveframework.msgprint(_("Enabled email inbox for user {0}").format(users)) + ask_pass_update() + + +def remove_user_email_inbox(email_account): + """remove user email inbox settings if email account is deleted""" + if not email_account: + return + + users = xhiveframework.get_all( + "User Email", + filters={"email_account": email_account}, + fields=["parent as name"], + ) + + for user in users: + doc = xhiveframework.get_doc("User", user.get("name")) + to_remove = [row for row in doc.user_emails if row.email_account == email_account] + [doc.remove(row) for row in to_remove] + + doc.save(ignore_permissions=True) + + +@xhiveframework.whitelist() +def set_email_password(email_account, password): + account = xhiveframework.get_doc("Email Account", email_account) + if account.awaiting_password and account.auth_method != "OAuth": + account.awaiting_password = 0 + account.password = password + try: + account.save(ignore_permissions=True) + except Exception: + xhiveframework.db.rollback() + return False + + return True diff --git a/xhiveframework/email/doctype/email_account/email_account_list.js b/xhiveframework/email/doctype/email_account/email_account_list.js new file mode 100644 index 0000000..3b3bb9a --- /dev/null +++ b/xhiveframework/email/doctype/email_account/email_account_list.js @@ -0,0 +1,24 @@ +xhiveframework.listview_settings["Email Account"] = { + add_fields: ["default_incoming", "default_outgoing", "enable_incoming", "enable_outgoing"], + get_indicator: function (doc) { + if (doc.default_incoming && doc.default_outgoing) { + var color = doc.enable_incoming && doc.enable_outgoing ? "blue" : "gray"; + return [ + __("Default Sending and Inbox"), + color, + "default_incoming,=,Yes|default_outgoing,=,Yes", + ]; + } else if (doc.default_incoming) { + color = doc.enable_incoming ? "blue" : "gray"; + return [__("Default Inbox"), color, "default_incoming,=,Yes"]; + } else if (doc.default_outgoing) { + color = doc.enable_outgoing ? "blue" : "gray"; + return [__("Default Sending"), color, "default_outgoing,=,Yes"]; + } else { + color = doc.enable_incoming ? "blue" : "gray"; + return [__("Inbox"), color, "is_global,=,No|is_default=No"]; + } + }, +}; + +xhiveframework.help.youtube_id["Email Account"] = "ww"; diff --git a/xhiveframework/email/doctype/email_account/test_email_account.py b/xhiveframework/email/doctype/email_account/test_email_account.py new file mode 100644 index 0000000..59791b4 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_email_account.py @@ -0,0 +1,648 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import email +import os +import unittest +from datetime import datetime, timedelta +from unittest.mock import patch + +import xhiveframework +from xhiveframework.core.doctype.communication.email import make +from xhiveframework.desk.form.load import get_attachments +from xhiveframework.email.doctype.email_account.email_account import notify_unreplied +from xhiveframework.email.email_body import get_message_id +from xhiveframework.email.receive import Email, InboundMail, SentEmailInInboxError +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestEmailAccount(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.db_set("enable_incoming", 1) + email_account.db_set("enable_auto_reply", 1) + email_account.db_set("use_imap", 1) + + @classmethod + def tearDownClass(cls): + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.db_set("enable_incoming", 0) + + def setUp(self): + xhiveframework.flags.mute_emails = False + xhiveframework.flags.sent_mail = None + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Unhandled Email") + + def get_test_mail(self, fname): + with open(os.path.join(os.path.dirname(__file__), "test_mails", fname)) as f: + return f.read() + + def test_incoming(self): + cleanup("test_sender@example.com") + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [self.get_test_mail("incoming-1.raw")], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertTrue("test_receiver@example.com" in comm.recipients) + # check if todo is created + self.assertTrue(xhiveframework.db.get_value(comm.reference_doctype, comm.reference_name, "name")) + + def test_unread_notification(self): + todo = xhiveframework.get_last_doc("ToDo") + + comm = xhiveframework.new_doc( + "Communication", + sender="test_sender@example.com", + subject="test unread reminder", + sent_or_received="Received", + reference_doctype=todo.doctype, + reference_name=todo.name, + email_account="_Test Email Account 1", + ) + comm.insert() + comm.db_set("creation", datetime.now() - timedelta(seconds=30 * 60)) + + xhiveframework.db.delete("Email Queue") + notify_unreplied() + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + { + "reference_doctype": comm.reference_doctype, + "reference_name": comm.reference_name, + }, + ) + ) + + def test_incoming_with_attach(self): + cleanup("test_sender@example.com") + + existing_file = xhiveframework.get_doc({"doctype": "File", "file_name": "xhiveerp-conf-14.png"}) + xhiveframework.delete_doc("File", existing_file.name) + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [self.get_test_mail("incoming-2.raw")], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertTrue("test_receiver@example.com" in comm.recipients) + + # check attachment + attachments = get_attachments(comm.doctype, comm.name) + self.assertTrue("xhiveerp-conf-14.png" in [f.file_name for f in attachments]) + + # cleanup + existing_file = xhiveframework.get_doc({"doctype": "File", "file_name": "xhiveerp-conf-14.png"}) + xhiveframework.delete_doc("File", existing_file.name) + + def test_incoming_attached_email_from_outlook_plain_text_only(self): + cleanup("test_sender@example.com") + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [self.get_test_mail("incoming-3.raw")], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertTrue("From: "Microsoft Outlook" <test_sender@example.com>" in comm.content) + self.assertTrue( + "This is an e-mail message sent automatically by Microsoft Outlook while" in comm.content + ) + + def test_incoming_attached_email_from_outlook_layers(self): + cleanup("test_sender@example.com") + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [self.get_test_mail("incoming-4.raw")], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertTrue("From: "Microsoft Outlook" <test_sender@example.com>" in comm.content) + self.assertTrue( + "This is an e-mail message sent automatically by Microsoft Outlook while" in comm.content + ) + + def test_outgoing(self): + make( + subject="test-mail-000", + content="test mail 000", + recipients="test_receiver@example.com", + send_email=True, + sender="test_sender@example.com", + ) + + mail = email.message_from_string(xhiveframework.get_last_doc("Email Queue").message) + self.assertTrue("test-mail-000" in mail.get("Subject")) + + def test_sendmail(self): + xhiveframework.sendmail( + sender="test_sender@example.com", + recipients="test_recipient@example.com", + content="test mail 001", + subject="test-mail-001", + delayed=False, + ) + + sent_mail = email.message_from_string(xhiveframework.safe_decode(xhiveframework.flags.sent_mail)) + self.assertTrue("test-mail-001" in sent_mail.get("Subject")) + + def test_print_format(self): + make( + sender="test_sender@example.com", + recipients="test_recipient@example.com", + content="test mail 001", + subject="test-mail-002", + doctype="Email Account", + name="_Test Email Account 1", + print_format="Standard", + send_email=True, + ) + + sent_mail = email.message_from_string(xhiveframework.get_last_doc("Email Queue").message) + self.assertTrue("test-mail-002" in sent_mail.get("Subject")) + + def test_threading(self): + cleanup(["in", ["test_sender@example.com", "test@example.com"]]) + + # send + sent_name = make( + subject="Test", + content="test content", + recipients="test_receiver@example.com", + sender="test@example.com", + doctype="ToDo", + name=xhiveframework.get_last_doc("ToDo").name, + send_email=True, + )["name"] + + sent_mail = email.message_from_string(xhiveframework.get_last_doc("Email Queue").message) + + with open(os.path.join(os.path.dirname(__file__), "test_mails", "reply-1.raw")) as f: + raw = f.read() + raw = raw.replace("<-- in-reply-to -->", sent_mail.get("Message-Id")) + + # parse reply + messages = { + # append_to = ToDo + '"INBOX"': {"latest_messages": [raw], "seen_status": {2: "UNSEEN"}, "uid_list": [2]} + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + sent = xhiveframework.get_doc("Communication", sent_name) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertEqual(comm.reference_doctype, sent.reference_doctype) + self.assertEqual(comm.reference_name, sent.reference_name) + + def test_threading_by_subject(self): + cleanup(["in", ["test_sender@example.com", "test@example.com"]]) + + with open(os.path.join(os.path.dirname(__file__), "test_mails", "reply-2.raw")) as f: + test_mails = [f.read()] + + with open(os.path.join(os.path.dirname(__file__), "test_mails", "reply-3.raw")) as f: + test_mails.append(f.read()) + + # parse reply + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": test_mails, + "seen_status": {2: "UNSEEN", 3: "UNSEEN"}, + "uid_list": [2, 3], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm_list = xhiveframework.get_all( + "Communication", + filters={"sender": "test_sender@example.com"}, + fields=["name", "reference_doctype", "reference_name"], + ) + # both communications attached to the same reference + self.assertEqual(comm_list[0].reference_doctype, comm_list[1].reference_doctype) + self.assertEqual(comm_list[0].reference_name, comm_list[1].reference_name) + + def test_threading_by_message_id(self): + cleanup() + xhiveframework.db.delete("Email Queue") + + # reference document for testing + event = xhiveframework.get_doc(dict(doctype="Event", subject="test-message")).insert() + + # send a mail against this + xhiveframework.sendmail( + recipients="test@example.com", + subject="test message for threading", + message="testing", + reference_doctype=event.doctype, + reference_name=event.name, + ) + + last_mail = xhiveframework.get_doc("Email Queue", dict(reference_name=event.name)) + + # get test mail with message-id as in-reply-to + with open(os.path.join(os.path.dirname(__file__), "test_mails", "reply-4.raw")) as f: + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [ + f.read().replace("{{ message_id }}", "<" + last_mail.message_id + ">") + ], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + # pull the mail + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm_list = xhiveframework.get_all( + "Communication", + filters={"sender": "test_sender@example.com"}, + fields=["name", "reference_doctype", "reference_name"], + ) + + # check if threaded correctly + self.assertEqual(comm_list[0].reference_doctype, event.doctype) + self.assertEqual(comm_list[0].reference_name, event.name) + + def test_auto_reply(self): + cleanup("test_sender@example.com") + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [self.get_test_mail("incoming-1.raw")], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + } + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + TestEmailAccount.mocked_email_receive(email_account, messages) + + comm = xhiveframework.get_doc("Communication", {"sender": "test_sender@example.com"}) + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": comm.reference_doctype, "reference_name": comm.reference_name}, + ) + ) + + def test_handle_bad_emails(self): + mail_content = self.get_test_mail(fname="incoming-1.raw") + message_id = Email(mail_content).mail.get("Message-ID") + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.handle_bad_emails(uid=-1, raw=mail_content, reason="Testing") + self.assertTrue(xhiveframework.db.get_value("Unhandled Email", {"message_id": message_id})) + + def test_imap_folder(self): + # assert tests if imap_folder >= 1 and imap is checked + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + + self.assertTrue(email_account.use_imap) + self.assertTrue(email_account.enable_incoming) + self.assertTrue(len(email_account.imap_folder) > 0) + + def test_imap_folder_missing(self): + # Test the Exception in validate() that verifies the imap_folder list + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.imap_folder = [] + + with self.assertRaises(Exception): + email_account.validate() + + def test_append_to(self): + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + mail_content = self.get_test_mail(fname="incoming-2.raw") + + inbound_mail = InboundMail(mail_content, email_account, 12345, 1, "ToDo") + communication = inbound_mail.process() + # the append_to for the email is set to ToDO in "_Test Email Account 1" + self.assertEqual(communication.reference_doctype, "ToDo") + self.assertTrue(communication.reference_name) + self.assertTrue(xhiveframework.db.exists(communication.reference_doctype, communication.reference_name)) + + @unittest.skip("poorly written and flaky") + def test_append_to_with_imap_folders(self): + mail_content_1 = self.get_test_mail(fname="incoming-1.raw") + mail_content_2 = self.get_test_mail(fname="incoming-2.raw") + mail_content_3 = self.get_test_mail(fname="incoming-3.raw") + + messages = { + # append_to = ToDo + '"INBOX"': { + "latest_messages": [mail_content_1, mail_content_2], + "seen_status": {0: "UNSEEN", 1: "UNSEEN"}, + "uid_list": [0, 1], + }, + # append_to = Communication + '"Test Folder"': { + "latest_messages": [mail_content_3], + "seen_status": {2: "UNSEEN"}, + "uid_list": [2], + }, + } + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + mails = TestEmailAccount.mocked_get_inbound_mails(email_account, messages) + self.assertEqual(len(mails), 3) + + inbox_mails = 0 + test_folder_mails = 0 + + for mail in mails: + communication = mail.process() + if mail.append_to == "ToDo": + inbox_mails += 1 + self.assertEqual(communication.reference_doctype, "ToDo") + self.assertTrue(communication.reference_name) + self.assertTrue( + xhiveframework.db.exists(communication.reference_doctype, communication.reference_name) + ) + else: + test_folder_mails += 1 + self.assertEqual(communication.reference_doctype, None) + + self.assertEqual(inbox_mails, 2) + self.assertEqual(test_folder_mails, 1) + + @patch("xhiveframework.email.receive.EmailServer.select_imap_folder", return_value=True) + @patch("xhiveframework.email.receive.EmailServer.logout", side_effect=lambda: None) + def mocked_get_inbound_mails( + email_account, messages=None, mocked_logout=None, mocked_select_imap_folder=None + ): + from xhiveframework.email.receive import EmailServer + + if messages is None: + messages = {} + + def get_mocked_messages(**kwargs): + return messages.get(kwargs["folder"], {}) + + with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages): + mails = email_account.get_inbound_mails() + + return mails + + @patch("xhiveframework.email.receive.EmailServer.select_imap_folder", return_value=True) + @patch("xhiveframework.email.receive.EmailServer.logout", side_effect=lambda: None) + def mocked_email_receive( + email_account, messages=None, mocked_logout=None, mocked_select_imap_folder=None + ): + if messages is None: + messages = {} + + def get_mocked_messages(**kwargs): + return messages.get(kwargs["folder"], {}) + + from xhiveframework.email.receive import EmailServer + + with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages): + email_account.receive() + + +class TestInboundMail(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.db_set("enable_incoming", 1) + + @classmethod + def tearDownClass(cls): + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + email_account.db_set("enable_incoming", 0) + + def setUp(self): + cleanup() + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("ToDo") + + def get_test_mail(self, fname): + with open(os.path.join(os.path.dirname(__file__), "test_mails", fname)) as f: + return f.read() + + def new_doc(self, doctype, **data): + doc = xhiveframework.new_doc(doctype) + for field, value in data.items(): + setattr(doc, field, value) + doc.insert() + return doc + + def new_communication(self, **kwargs): + defaults = {"subject": "Test Subject"} + d = {**defaults, **kwargs} + return self.new_doc("Communication", **d) + + def new_email_queue(self, **kwargs): + defaults = {"message_id": get_message_id().strip(" <>")} + d = {**defaults, **kwargs} + return self.new_doc("Email Queue", **d) + + def new_todo(self, **kwargs): + defaults = {"description": "Description"} + d = {**defaults, **kwargs} + return self.new_doc("ToDo", **d) + + def test_self_sent_mail(self): + """Check that we raise SentEmailInInboxError if the inbound mail is self sent mail.""" + mail_content = self.get_test_mail(fname="incoming-self-sent.raw") + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 1, 1) + with self.assertRaises(SentEmailInInboxError): + inbound_mail.process() + + def test_mail_exist_validation(self): + """Do not create communication record if the mail is already downloaded into the system.""" + mail_content = self.get_test_mail(fname="incoming-1.raw") + message_id = Email(mail_content).message_id + # Create new communication record in DB + communication = self.new_communication(message_id=message_id) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + new_communication = inbound_mail.process() + + # Make sure that uid is changed to new uid + self.assertEqual(new_communication.uid, 12345) + self.assertEqual(communication.name, new_communication.name) + + def test_find_parent_email_queue(self): + """If the mail is reply to the already sent mail, there will be a email queue record.""" + # Create email queue record + queue_record = self.new_email_queue() + + mail_content = self.get_test_mail(fname="reply-4.raw").replace( + "{{ message_id }}", queue_record.message_id + ) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + parent_queue = inbound_mail.parent_email_queue() + self.assertEqual(queue_record.name, parent_queue.name) + + def test_find_parent_communication_through_queue(self): + """Find parent communication of an inbound mail. + Cases where parent communication does exist: + 1. No parent communication is the mail is not a reply. + + Cases where parent communication does not exist: + 2. If mail is not a reply to system sent mail, then there can exist co + """ + # Create email queue record + communication = self.new_communication() + queue_record = self.new_email_queue(communication=communication.name) + mail_content = self.get_test_mail(fname="reply-4.raw").replace( + "{{ message_id }}", queue_record.message_id + ) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + parent_communication = inbound_mail.parent_communication() + self.assertEqual(parent_communication.name, communication.name) + + def test_find_parent_communication_for_self_reply(self): + """If the inbound email is a reply but not reply to system sent mail. + + Ex: User replied to his/her mail. + """ + message_id = "new-message-id" + mail_content = self.get_test_mail(fname="reply-4.raw").replace("{{ message_id }}", message_id) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + parent_communication = inbound_mail.parent_communication() + self.assertFalse(parent_communication) + + communication = self.new_communication(message_id=message_id) + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + parent_communication = inbound_mail.parent_communication() + self.assertEqual(parent_communication.name, communication.name) + + def test_find_parent_communication_from_header(self): + """Incase of header contains parent communication name""" + communication = self.new_communication() + mail_content = self.get_test_mail(fname="reply-4.raw").replace( + "{{ message_id }}", f"<{communication.name}@{xhiveframework.local.site}>" + ) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + parent_communication = inbound_mail.parent_communication() + self.assertEqual(parent_communication.name, communication.name) + + def test_reference_document(self): + # Create email queue record + todo = self.new_todo() + # communication = self.new_communication(reference_doctype='ToDo', reference_name=todo.name) + queue_record = self.new_email_queue(reference_doctype="ToDo", reference_name=todo.name) + mail_content = self.get_test_mail(fname="reply-4.raw").replace( + "{{ message_id }}", queue_record.message_id + ) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + reference_doc = inbound_mail.reference_document() + self.assertEqual(todo.name, reference_doc.name) + + def test_reference_document_by_record_name_in_subject(self): + # Create email queue record + todo = self.new_todo() + + mail_content = self.get_test_mail(fname="incoming-subject-placeholder.raw").replace( + "{{ subject }}", f"RE: (#{todo.name})" + ) + + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + reference_doc = inbound_mail.reference_document() + self.assertEqual(todo.name, reference_doc.name) + + def test_reference_document_by_subject_match(self): + subject = "New todo" + todo = self.new_todo(sender="test_sender@example.com", description=subject) + + mail_content = self.get_test_mail(fname="incoming-subject-placeholder.raw").replace( + "{{ subject }}", f"RE: {subject}" + ) + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + reference_doc = inbound_mail.reference_document() + self.assertEqual(todo.name, reference_doc.name) + + def test_reference_document_by_subject_match_with_accents(self): + subject = "Nouvelle tâche à faire 😃" + todo = self.new_todo(sender="test_sender@example.com", description=subject) + + mail_content = ( + self.get_test_mail(fname="incoming-subject-placeholder.raw") + .replace("{{ subject }}", f"RE: {subject}") + .encode("utf-8") + ) # note: encode to bytes because that's what triggered the error + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + reference_doc = inbound_mail.reference_document() + self.assertEqual(todo.name, reference_doc.name) + + def test_create_communication_from_mail(self): + # Create email queue record + mail_content = self.get_test_mail(fname="incoming-2.raw") + email_account = xhiveframework.get_doc("Email Account", "_Test Email Account 1") + inbound_mail = InboundMail(mail_content, email_account, 12345, 1) + communication = inbound_mail.process() + self.assertTrue(communication._attachments) + + +def cleanup(sender=None): + filters = {} + if sender: + filters.update({"sender": sender}) + + names = xhiveframework.get_list("Communication", filters=filters, fields=["name"]) + for name in names: + xhiveframework.delete_doc_if_exists("Communication", name.name) + xhiveframework.delete_doc_if_exists("Communication Link", {"parent": name.name}) diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-1.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-1.raw new file mode 100644 index 0000000..287ee2b --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-1.raw @@ -0,0 +1,91 @@ +Delivered-To: test_receiver@example.com +Received: by 10.96.153.227 with SMTP id vj3csp416144qdb; + Mon, 15 Sep 2014 03:35:07 -0700 (PDT) +X-Received: by 10.66.119.103 with SMTP id kt7mr36981968pab.95.1410777306321; + Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +Return-Path: +Received: from mail-pa0-x230.google.com (mail-pa0-x230.google.com [2607:f8b0:400e:c03::230]) + by mx.google.com with ESMTPS id dg10si22178346pdb.115.2014.09.15.03.35.06 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +Received-SPF: pass (google.com: domain of test_sender@example.com designates 2607:f8b0:400e:c03::230 as permitted sender) client-ip=2607:f8b0:400e:c03::230; +Authentication-Results: mx.google.com; + spf=pass (google.com: domain of test_sender@example.com designates 2607:f8b0:400e:c03::230 as permitted sender) smtp.mail=test_sender@example.com; + dkim=pass header.i=@gmail.com; + dmarc=pass (p=NONE dis=NONE) header.from=gmail.com +Received: by mail-pa0-f48.google.com with SMTP id hz1so6118714pad.21 + for ; Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=gmail.com; s=20120113; + h=from:content-type:subject:message-id:date:to:mime-version; + bh=rwiLijtF3lfy9M6cP/7dv2Hm7NJuBwFZn1OFsN8Tlvs=; + b=x7U4Ny3Kz2ULRJ7a04NDBrBTVhP2ImIB9n3LVNGQDnDonPUM5Ro/wZcxPTVnBWZ2L1 + o1bGfP+lhBrvYUlHsd5r4FYC0Uvpad6hbzLr0DGUQgPTxW4cGKbtDEAq+BR2JWd9f803 + vdjSWdGk8w2dt2qbngTqIZkm5U2XWjICDOAYuPIseLUgCFwi9lLyOSARFB7mjAa2YL7Q + Nswk7mbWU1hbnHP6jaBb0m8QanTc7Up944HpNDRxIrB1ZHgKzYhXtx8nhnOx588ZGIAe + E6tyG8IwogR11vLkkrBhtMaOme9PohYx4F1CSTiwspmDCadEzJFGRe//lEXKmZHAYH6g + 90Zg== +X-Received: by 10.70.38.135 with SMTP id g7mr22078275pdk.100.1410777305744; + Mon, 15 Sep 2014 03:35:05 -0700 (PDT) +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id zr6sm11025126pbc.50.2014.09.15.03.35.02 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 03:35:04 -0700 (PDT) +From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA" +Subject: test mail 🦄🌈😎 +Message-Id: <9143999C-8456-4399-9CF1-4A2DA9DD7711@gmail.com> +Date: Mon, 15 Sep 2014 16:04:57 +0530 +To: Rushabh Mehta +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +test mail + + + +@rushabh_mehta +https://xhiveerp.org + + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA +Content-Transfer-Encoding: quoted-printable +Content-Type: text/html; + charset=us-ascii + +test = +mail
      +



      @rushabh_mehta
      +
      +
      = + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-2.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-2.raw new file mode 100644 index 0000000..49f3a40 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-2.raw @@ -0,0 +1,511 @@ +Delivered-To: test_receiver@example.com +Received: by 10.96.153.227 with SMTP id vj3csp576735qdb; + Mon, 15 Sep 2014 23:28:05 -0700 (PDT) +X-Received: by 10.66.65.202 with SMTP id z10mr41658290pas.20.1410848884944; + Mon, 15 Sep 2014 23:28:04 -0700 (PDT) +Return-Path: +Received: from mail-pa0-x232.google.com (mail-pa0-x232.google.com [2607:f8b0:400e:c03::232]) + by mx.google.com with ESMTPS id he2si27434308pac.73.2014.09.15.23.28.04 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 23:28:04 -0700 (PDT) +Received-SPF: pass (google.com: domain of test_sender@example.com designates 2607:f8b0:400e:c03::232 as permitted sender) client-ip=2607:f8b0:400e:c03::232; +Authentication-Results: mx.google.com; + spf=pass (google.com: domain of test_sender@example.com designates 2607:f8b0:400e:c03::232 as permitted sender) smtp.mail=test_sender@example.com; + dkim=pass header.i=@gmail.com; + dmarc=pass (p=NONE dis=NONE) header.from=gmail.com +Received: by mail-pa0-f50.google.com with SMTP id bj1so8166191pad.37 + for ; Mon, 15 Sep 2014 23:28:04 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=gmail.com; s=20120113; + h=from:content-type:subject:message-id:date:to:mime-version; + bh=RjaUdjmv0mhS2w8fKWC6N0GWDRnlLZrOgmhtagrz2gY=; + b=rKWW/c0NKT37bbAO8RHBI8JnrXjAZbAP+XPfo7tP58iJ+BKK1UE2J7iDluE5QPeuXg + WhyN0mK8hfAjLF9BDl2OfRR4yDuNIq2nuqVu4wW0NtacDqJzSEW41xdnSDDkXLCBN/c2 + BNikjY9fQkiQ2axEVbHJCCxcMA9ZNZXq3212xnd78eDUIUnVDeIc7XE/PnecwmxclRtc + bJV4zTVKsGbSP3fnyd7VX0w4ezjSX9oUPPRSvVAAjPSfREfRchptsX9LCB2LiVP0rVGO + h4IVIpdeBeMsyiM+4UcRn5raLSeX5WvD/eTyJ28qQz3t0nDZnSSVFboBkP2QcAWJNXcX + ydoQ== +X-Received: by 10.68.171.33 with SMTP id ar1mr12518010pbc.148.1410848884267; + Mon, 15 Sep 2014 23:28:04 -0700 (PDT) +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id sf1sm13174290pbb.0.2014.09.15.23.28.01 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 23:28:02 -0700 (PDT) +From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_6F6D23B5-0ADE-4347-978A-E0A21B92BB70" +Subject: test +Message-Id: <54A4EFFA-AD17-456A-9851-9715574DF0C9@gmail.com> +Date: Tue, 16 Sep 2014 11:57:58 +0530 +To: Rushabh Mehta +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_6F6D23B5-0ADE-4347-978A-E0A21B92BB70 +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +with attachment + + + + + +--Apple-Mail=_6F6D23B5-0ADE-4347-978A-E0A21B92BB70 +Content-Type: multipart/related; + type="text/html"; + boundary="Apple-Mail=_18FE9DD1-B053-4781-ACFD-7E542B418B05" + + +--Apple-Mail=_18FE9DD1-B053-4781-ACFD-7E542B418B05 +Content-Transfer-Encoding: 7bit +Content-Type: text/html; + charset=us-ascii + +
      with attachment

      +


      +
      +
      +--Apple-Mail=_18FE9DD1-B053-4781-ACFD-7E542B418B05 +Content-Transfer-Encoding: base64 +Content-Disposition: inline; + filename=xhiveerp-conf-14.png +Content-Type: image/png; + x-unix-mode=0644; + name="xhiveerp-conf-14.png" +Content-Id: <9137CC31-70BA-4232-8438-6B9D8B15A1B5@webnotes_sn> + +iVBORw0KGgoAAAANSUhEUgAAAq4AAACKCAYAAABrXcgPAAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2d +lndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji +1wnyEFDGwVFEReXdjGsJ7601896a/cdZ39nnt9fZZ+9917oAUPyCBMJ0WAGANKFYFO7rwVwSE8vE +9wIYEAEOWAHA4WZmBEf4RALU/L09mZmoSMaz9u4ugGS72yy/UCZz1v9/kSI3QyQGAApF1TY8fiYX +5QKUU7PFGTL/BMr0lSkyhjEyFqEJoqwi48SvbPan5iu7yZiXJuShGlnOGbw0noy7UN6aJeGjjASh +XJgl4GejfAdlvVRJmgDl9yjT0/icTAAwFJlfzOcmoWyJMkUUGe6J8gIACJTEObxyDov5OWieAHim +Z+SKBIlJYqYR15hp5ejIZvrxs1P5YjErlMNN4Yh4TM/0tAyOMBeAr2+WRQElWW2ZaJHtrRzt7VnW +5mj5v9nfHn5T/T3IevtV8Sbsz55BjJ5Z32zsrC+9FgD2JFqbHbO+lVUAtG0GQOXhrE/vIADyBQC0 +3pzzHoZsXpLE4gwnC4vs7GxzAZ9rLivoN/ufgm/Kv4Y595nL7vtWO6YXP4EjSRUzZUXlpqemS0TM +zAwOl89k/fcQ/+PAOWnNycMsnJ/AF/GF6FVR6JQJhIlou4U8gViQLmQKhH/V4X8YNicHGX6daxRo +dV8AfYU5ULhJB8hvPQBDIwMkbj96An3rWxAxCsi+vGitka9zjzJ6/uf6Hwtcim7hTEEiU+b2DI9k +ciWiLBmj34RswQISkAd0oAo0gS4wAixgDRyAM3AD3iAAhIBIEAOWAy5IAmlABLJBPtgACkEx2AF2 +g2pwANSBetAEToI2cAZcBFfADXALDIBHQAqGwUswAd6BaQiC8BAVokGqkBakD5lC1hAbWgh5Q0FQ +OBQDxUOJkBCSQPnQJqgYKoOqoUNQPfQjdBq6CF2D+qAH0CA0Bv0BfYQRmALTYQ3YALaA2bA7HAhH +wsvgRHgVnAcXwNvhSrgWPg63whfhG/AALIVfwpMIQMgIA9FGWAgb8URCkFgkAREha5EipAKpRZqQ +DqQbuY1IkXHkAwaHoWGYGBbGGeOHWYzhYlZh1mJKMNWYY5hWTBfmNmYQM4H5gqVi1bGmWCesP3YJ +NhGbjS3EVmCPYFuwl7ED2GHsOxwOx8AZ4hxwfrgYXDJuNa4Etw/XjLuA68MN4SbxeLwq3hTvgg/B +c/BifCG+Cn8cfx7fjx/GvyeQCVoEa4IPIZYgJGwkVBAaCOcI/YQRwjRRgahPdCKGEHnEXGIpsY7Y +QbxJHCZOkxRJhiQXUiQpmbSBVElqIl0mPSa9IZPJOmRHchhZQF5PriSfIF8lD5I/UJQoJhRPShxF +QtlOOUq5QHlAeUOlUg2obtRYqpi6nVpPvUR9Sn0vR5Mzl/OX48mtk6uRa5Xrl3slT5TXl3eXXy6f +J18hf0r+pvy4AlHBQMFTgaOwVqFG4bTCPYVJRZqilWKIYppiiWKD4jXFUSW8koGStxJPqUDpsNIl +pSEaQtOledK4tE20Otpl2jAdRzek+9OT6cX0H+i99AllJWVb5SjlHOUa5bPKUgbCMGD4M1IZpYyT +jLuMj/M05rnP48/bNq9pXv+8KZX5Km4qfJUilWaVAZWPqkxVb9UU1Z2qbapP1DBqJmphatlq+9Uu +q43Pp893ns+dXzT/5PyH6rC6iXq4+mr1w+o96pMamhq+GhkaVRqXNMY1GZpumsma5ZrnNMe0aFoL +tQRa5VrntV4wlZnuzFRmJbOLOaGtru2nLdE+pN2rPa1jqLNYZ6NOs84TXZIuWzdBt1y3U3dCT0sv +WC9fr1HvoT5Rn62fpL9Hv1t/ysDQINpgi0GbwaihiqG/YZ5ho+FjI6qRq9Eqo1qjO8Y4Y7ZxivE+ +41smsImdSZJJjclNU9jU3lRgus+0zwxr5mgmNKs1u8eisNxZWaxG1qA5wzzIfKN5m/krCz2LWIud +Ft0WXyztLFMt6ywfWSlZBVhttOqw+sPaxJprXWN9x4Zq42Ozzqbd5rWtqS3fdr/tfTuaXbDdFrtO +u8/2DvYi+yb7MQc9h3iHvQ732HR2KLuEfdUR6+jhuM7xjOMHJ3snsdNJp9+dWc4pzg3OowsMF/AX +1C0YctFx4bgccpEuZC6MX3hwodRV25XjWuv6zE3Xjed2xG3E3dg92f24+ysPSw+RR4vHlKeT5xrP +C16Il69XkVevt5L3Yu9q76c+Oj6JPo0+E752vqt9L/hh/QL9dvrd89fw5/rX+08EOASsCegKpARG +BFYHPgsyCRIFdQTDwQHBu4IfL9JfJFzUFgJC/EN2hTwJNQxdFfpzGC4sNKwm7Hm4VXh+eHcELWJF +REPEu0iPyNLIR4uNFksWd0bJR8VF1UdNRXtFl0VLl1gsWbPkRoxajCCmPRYfGxV7JHZyqffS3UuH +4+ziCuPuLjNclrPs2nK15anLz66QX8FZcSoeGx8d3xD/iRPCqeVMrvRfuXflBNeTu4f7kufGK+eN +8V34ZfyRBJeEsoTRRJfEXYljSa5JFUnjAk9BteB1sl/ygeSplJCUoykzqdGpzWmEtPi000IlYYqw +K10zPSe9L8M0ozBDuspp1e5VE6JA0ZFMKHNZZruYjv5M9UiMJJslg1kLs2qy3mdHZZ/KUcwR5vTk +muRuyx3J88n7fjVmNXd1Z752/ob8wTXuaw6thdauXNu5Tnddwbrh9b7rj20gbUjZ8MtGy41lG99u +it7UUaBRsL5gaLPv5sZCuUJR4b0tzlsObMVsFWzt3WazrWrblyJe0fViy+KK4k8l3JLr31l9V/nd +zPaE7b2l9qX7d+B2CHfc3em681iZYlle2dCu4F2t5czyovK3u1fsvlZhW3FgD2mPZI+0MqiyvUqv +akfVp+qk6oEaj5rmvep7t+2d2sfb17/fbX/TAY0DxQc+HhQcvH/I91BrrUFtxWHc4azDz+ui6rq/ +Z39ff0TtSPGRz0eFR6XHwo911TvU1zeoN5Q2wo2SxrHjccdv/eD1Q3sTq+lQM6O5+AQ4ITnx4sf4 +H++eDDzZeYp9qukn/Z/2ttBailqh1tzWibakNml7THvf6YDTnR3OHS0/m/989Iz2mZqzymdLz5HO +FZybOZ93fvJCxoXxi4kXhzpXdD66tOTSna6wrt7LgZevXvG5cqnbvfv8VZerZ645XTt9nX297Yb9 +jdYeu56WX+x+aem172296XCz/ZbjrY6+BX3n+l37L972un3ljv+dGwOLBvruLr57/17cPel93v3R +B6kPXj/Mejj9aP1j7OOiJwpPKp6qP6391fjXZqm99Oyg12DPs4hnj4a4Qy//lfmvT8MFz6nPK0a0 +RupHrUfPjPmM3Xqx9MXwy4yX0+OFvyn+tveV0auffnf7vWdiycTwa9HrmT9K3qi+OfrW9m3nZOjk +03dp76anit6rvj/2gf2h+2P0x5Hp7E/4T5WfjT93fAn88ngmbWbm3/eE8/syOll+AAAACXBIWXMA +AAsTAAALEwEAmpwYAAADqGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxu +czp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJE +RiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMi +PgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp4 +bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iCiAgICAgICAgICAgIHhtbG5zOnRpZmY9 +Imh0dHA6Ly9ucy5hZG9iZS5jb20vdGlmZi8xLjAvIgogICAgICAgICAgICB4bWxuczpleGlmPSJo +dHRwOi8vbnMuYWRvYmUuY29tL2V4aWYvMS4wLyI+CiAgICAgICAgIDx4bXA6TW9kaWZ5RGF0ZT4y +MDE0LTA3LTE4VDE0OjA3OjM3PC94bXA6TW9kaWZ5RGF0ZT4KICAgICAgICAgPHhtcDpDcmVhdG9y +VG9vbD5QaXhlbG1hdG9yIDMuMjwveG1wOkNyZWF0b3JUb29sPgogICAgICAgICA8dGlmZjpPcmll +bnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICAgICA8dGlmZjpDb21wcmVzc2lvbj41 +PC90aWZmOkNvbXByZXNzaW9uPgogICAgICAgICA8dGlmZjpSZXNvbHV0aW9uVW5pdD4xPC90aWZm +OlJlc29sdXRpb25Vbml0PgogICAgICAgICA8dGlmZjpZUmVzb2x1dGlvbj43MjwvdGlmZjpZUmVz +b2x1dGlvbj4KICAgICAgICAgPHRpZmY6WFJlc29sdXRpb24+NzI8L3RpZmY6WFJlc29sdXRpb24+ +CiAgICAgICAgIDxleGlmOlBpeGVsWERpbWVuc2lvbj42ODY8L2V4aWY6UGl4ZWxYRGltZW5zaW9u +PgogICAgICAgICA8ZXhpZjpDb2xvclNwYWNlPjE8L2V4aWY6Q29sb3JTcGFjZT4KICAgICAgICAg +PGV4aWY6UGl4ZWxZRGltZW5zaW9uPjEzODwvZXhpZjpQaXhlbFlEaW1lbnNpb24+CiAgICAgIDwv +cmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgqKhMWCAABAAElEQVR4 +Ae2dB5wURfbHu3fJCIqCOWCOZ8AcSIbzTIAoZgXxDu/OM/+NGFBRMethODGLmVOSnlnArCfmfJ4B +FTGfAsICu/3//manht7ZCd2zs7Ozy3ufz5uqrnr16tWva6pfv67p8TwjQ8AQMAQMAUPAEDAEDAFD +wBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPA +EDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQ +MAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAw +BAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAE +DAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQM +AUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwB +Q8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFD +wBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPA +EDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQ +MAQMAUPAEDAEyggBv4xsMVMMAUPAEDAEDAFDwBDIiEAwZUg7z/+pp+f763k1QTeEunqeP8erCL7x +avyvvco20/xe477P2NgKWwwC5ri2mFNpAzEEDAFDwBAwBFoeAsHUAXt4Xs1xjKy3FwTts47Q92uo +exmZcV7bdjf6O4ybl1XWKpotAua4NttTZ4YbAoaAIWAIGAItF4Hg2QHbeDXVV3uBt33sUfr+TM/3 +zvNqetzq9x2xKHZ7a1C2CJjjWranxgwzBAwBQ8AQMASWTASCaf2G4bCOJnrapmEI+C94la0G+b0e ++qZheqx1uSBgjmu5nAmzwxAwBAyB7AisQNXK2avr1bxJSVCv1AoMgWaAQDCl/yi2BpxWNFN9b5YX +4Lz2Hf980XSaoiZDoFWT9Vy6jp1zrkV8B/haeAs436KuerUVvwEfA7+UPCbJ214yRoaAIWAIFAOB +ISjhYh6Z2iK5ILK0CRoCZYJAMG2fo7yaIjqtGlfgrej5NT3ImeNaJue5IWZUNKRxM2mrMcoJlZN+ +PiyndSGcj5zDq70xanMBXAlL15KAG8M0MgQMAUPAEDAESoNA8OzAHl7g31D03nz/c69bm38UXa8p +bBIEliQHbGkQXimEsnNMQ0X1spJxkdkVyUuHkSFgCBgChoAhYAgUG4GaRReyp7V1sdUSazrH33ic +PYEoPrBNonFJclz1moxwpNU5pLmAD8so8ho+ztXO6gwBQ8AQMAQMAUMgIgLBtP474LT+IaJ4dDHf +f9vrvfnd0RuYZLkjsCQ5rjoX4ShrOJ/tPIVlwvls8lZuCBgChoAhYAgYAnERCIIhcZtwRSeY5P+3 +lrO0rvDO9P0RClwZtRAElmTHNcoptAhrFJRMxhAwBAwBQ8AQaBgCu8VsPs1r1a6733fSOmLPa70a +Duzh/KvWxyk9vv+c32vSI6ljy7QIBJY0x7WhjmhD27eISWODMAQMAUPAEDAEioVA8MKAtdkm0D2y +Pt//zFu+3e/9ncbNcG38vg99hQN7lxdUbIsD+3ii3PdPd/WWthwEWpLjqkf5mdiNUWmux/3pdenH +OuvpujL1JzkjQ8AQMAQMAUPAEIiCwMKataKIpWR8b2S2H1v5fSf8z+vTdi+vouJQv/fEF1NtLNNi +EGgJ73F1zmOmaKjKXLlL3cmLe6x2ro1ScTbn1tWrjZEhYAgYAoaAIWAIZEPA97sRcc1WW7+8onJ6 +/cLFJb4/rpqjexaXWK4lIdASHNd8TqL7j+JfOXGazI7kdMb4piTaSofI6czUPlNZbSv7NAQMAUPA +EDAEDIG6CARe17oFeY5ad/gsj4RVt2AEmrPjGnY8N+Ic/R7WePTrwXAkVHm9v60zrL9NFOmRf1QH +020PUNszYDmvbdLaS5fk9LqtJ+H3YVHYxtoS+zQEDAFDwBAwBAyBxQj4Ne0jX5HVqtOC+YsbW25J +Q6AlOK5yWh+FV4948uTYOmc0ShPJqs3KsP49Kx/NQGAPWM5rc3FcN8BW4SjnfPkkk3g/hPgD8m/B +UR1+REtKy9LbpvC68HrwCHgu3Bik87o1LNz0xxTi9vBM+CtYc+AFuBwXV/2JRm9Y3xfNaUU6foJn +wV/CU5LHJCWnljAPSw7aktZh8J9j23pfz9iGR8ursJKvwD8tsWYFXVmZZrPizuRX5V+Tn+kFHT7w ++96rNaxkFLx8aGdvwdytsW0F/rZ0eWzResp3LJjDj4a+8yqC77wauKLNR37vB/9TMsMK6Ch4bdDS +3tyq3lz9VgfX2rUi8FkramZ5lZVfeq1aT/F3GKe1w8gQKCkCugA3R3J2y4k6Ab4KdtsAnLPoZMLj +i1qmNpkctExlTr+r09/CnghfDbv+XJ2TbepUEeM94T8keY2IBmmRmgo/A98D/wwXSjpnQyM2fhe5 +HTPICt+d4WHwAFjjctSNTPiiVYz+dkHnQfDesJzVXDSbyonw/bBurNz8JBubGmq75uTBsGzXK2fC +OHFYh2Tni/C98M2wniI0FsmOUs7DjvT3CrxazAHpPB4Rs43EH4B3j9luPPK3wpPT2rXlWByV3Lam +XPL6fqydS6Bc6oLn9lvLq17E965GePZhde6Q1zbfr0HuJVbhSV7QepLf98EP87YpQCCYMmBzz69R +sIL1NOAl+oknf/k1+d6nCD2G/KNe23ZP4wTOy98ot0Qwrd9VOM0R11X/Xb/PpDrrahAMqvSmVtWu +FT5rRRDkWCv8arCtXSuWWvFmf6sxkdaKYNp+BBcW/rXOSGoUCAjq2FKnPv3Arxjt+UG+NXWO33vS +2elNy/F40KBBlePGjas+++yzewZB0HHkyJGPjRgxogKuaYC9uj4GZ5555pDq6upnLrnkkhkcJ8oa +oLMsmjbniKtzBlsnkdRxRTLvHMYoIGu/qvsCuJOqC72wcX1E0aO2bpKFbYpjS5R+GiIju4bAZ8Gr +w3FJUc2BSR5FOgaWU6UoY1xShLJzxEZLZZDblbJr4fUz1KnIzQVX3ZD+eqDkMnhnpyxC2gmZw5L8 +JqkW6pfgQqghtsvma+BNInasud8zySeRng4/CBeTmmoeKgJ/MvxYzMEcjvz98CMx2uniPyiGvERn +wf8HbwpH/W4gmpGitK/K2LKMCoPn+63vLQrO8aoXHoQTlf6dzm1prfyOrOI4RAsuCabu8yHR2Qu9 +Pj3uKcYL6RP/9FRTcxGXj96xrhTO6sBbi6zWhb96VVXfBlP7X+StsvqN/rqjCz8vNUH0tcL36qyr +wbR9dvamzV+8VuS9+gWVjLt2rZgz66Tg2QGn+70mRFgrFqzGlVIBp8IpqDk2L+a+9z0dnF14J6Vr +2aVLF83t6kWLFmmdXg5+bObMmVqLnU9BtjDyfX/nNm3avEPrGTjIFXKQC9NUPq3iLQTlY7cscQ6h +O7FuLCrPxukj0FdTDqoiGWLdXSpVmdNLNkXZ9DpbnA2urStPKWjCzIH0/REsZ3P1ItihRU+Ozaew +nLocd+bUFo+WRdXt8JPw+nA2Kgb2GtMN8Gvwztk6ilC+OTIvwLfAS0eQL4aILmB3w0/DWgwLoXVo +9E9YdsvZLAY19Tx8nEHcVMBArqeN5nwU0uPh0VEE02SO5PiHtLIl8jB4ftDqwZR+d/Ez2PdxUA6J +7bRmQi3Q1p5grDft9TeCZ/vtlUkkShltN8UJfoStAPpO947SJr8MWwuCmmu8rz//GAfyKKJuxVi/ +8neLRPDioPaM526ueE+DdWFrRRCs41VX/zOY2u+W4LVhxVorItnf3IUUbR0zZszCs846a22czBPg +o0877bTNVKa6QsanaK1O7fDhww8lPZiI6zkqk9OarCtEbdm0cY5W2RhUgCHuCy4nVHl3nElVep3G +/wH8FDw1ycrrkVIYm/R2VNcjybh71Cjy9RQ0UoEc8X/A98FrNkIfWqQUJXoZzuVIFqPr36FE2wYG +R1AWPn8RxOuJdKVEc+HPcDHOp3QMhZ+DtV+sMWkFlE+FDylSJ7L7UbghTnc5zUNFXb+IiY1u9i6K +2OY65BQ1iUPXIhw3EhxHf7ORDaYO2MNbNP8NltNDi+Kwpo88CDb1qoOHcbKexUmMtSbyKH4wbV9l +pd8zXW1RjgOCCjVs0ZnW76FgyqCoN0oFdx283G8Fb8H8qYmbg4K1hBpqm8Lsbx5N7I8NFVs2KwJ+ +0plclpuVvyO1NrxSZWXl30855ZQVk9HRuNcfX1sMcFp3QtflyZugvebPnz982LBhrVVHeVydNCkf +aujFvXxGsvhEOOcxk22uzqWS0aPu3eB9kqy8yhxJNizvyjOl5TYZumPkC/DRmYwtctkW6Hsdjvt4 +NKoZ2yE4DV4pYoOGzO1u9PEK3DNiX3HE5Hy/CDeWky/bdROxDVxM2gVl4+BCcO1Ou3Kah7OxR854 +1O81ogk6hk/Nw1y0H5X75xLIUPc+ZadmKF+iioJgRAWO4QU8MX2EM6MnK41LQdCTfl4hgspWgtyk +vZ9EJa/kh1W3I6mbsMalgD37/vyXEnt7G6+nbvx89GUwKP5aMadqnM5n45nesjSzRaALDqauCZOJ +uN5J2h3nNeq1LiMYFRUV3dHZEb4EgXc47rHMMsu0yyjczAqb88RKv+jEcTDDp2lu8mAOqVjk0tqj +aJ+Z+k+3MZqm4kithRo5X1sWR10kLR2QuhveI5J0dKHVEFX0s0v0JgU5WE69Fgzh11i0Boqfh9dt +hA5ke/dG0CuVu8HnxdRdrvPwGcZxfcyxaL3UNoPWWdopyhpX5wLaHAbPy6JziSgOpoxo5U2bPgHH +8CwcqdIFAAKvGxHUp3GY9Ug1IwXvDWrjTat6GLtOzCjQWIV6bL9o4avB1IFbN0oXQbASEe3ujaR7 +N+/Z1+OuFY1iSpkrDbQdgB9j/RcH8zVsfQbHdQLpm6NGjeKpQ+E0b9688ej8HAf4ZrS8XVNT8/Sl +l146O7n9oCl9k8IHlWyphbhcSYtXZRbWHlSxqKFjcHcgbdAlFrWvTQr+dDY5O7ONo7EWaEUr/gUv +X/AICm+oi/o/4bxRjBhdyGHtGENeoo2FbUwzsop3pUY4NXSuZe2gkSqGo7dXRN3lPg9PYxz/jTgW +J7YJGbXLRHrUF/c7dw5tGnSBymRIsyvzX78Rx1BPvZqC2uIw38WPrTJvrfmuagwO3h+awjAeCnAz +tHBSMGXgqk3TfwN6DYLh3BBEXSsa0FHzbsp2gBqNAIe1PY7m0jiYWjcbfF1AXye4LTr1FE5BpcQ1 +dOONN27WTivjaLDTJx2NSdUoz8QLKV+U7LiqgQZIv0j6nE5XlqiI8JHuJDmbpE+2ZhpD3D4imJEQ +0WOsiXBjPYqOYoe+JJPhuBfxKLqjyribh6jyTSGnX4/f0BQdN6BPzfVzI7RvDvNQT1uOhBMXjghj +ciJnkUn/fvWjLLPj41rVT6dRdFn94iWrhEfw5+EYDm3SUfv+515Fx0fTbWAf7Ck4j4PTy0t6HPDq +PX/RJH70pHW1+ZAi50GktaL5jKkRLGXPqfMffsPR1NsWRsJ6EiPSflXVR2anr1WrVvI9OuG43k66 +J7p/JW0R5KKW5TgY3RUoUrk07C4sOnkql1OiVCfCvfYlfGJVF5XULh+5ftPl0sudLtkk25WqTPaH +ZWX/L3BjOK8Xo3cnuCH0PY21XWINuFAHUFHS8+E/w01Bhdodx1adP53nhtBgGj8Nj22Ikght5yHz +Nqzo3lewIoc94HXguFjtTBvt9XwZzkbNZR4+xwCugU/MNpAM5XLKb4J7w1prNNf/AcehXxA+AnZr +W7jtmxzsHi4gfyA8NK0s1+HeVOrClYvy1edqW5Q6opwH8uv8c4qirHAlVZzFQX7Pe34Oq8C2PXGo +R4XLmiwfBFt4c2bdQf+DGt0G358HHm+zKrBW+F+BweK1Iv4ryXYOpuyznd938uK1otL7lll/f51x ++P6G9LNpnbJcB74/DvlM351Qq2bnqGk8y2gAOJlaV0QBjqjL15bk+XTybdu2XbhgwQI9AV1PTdgy +4AJzeTSUf3U5O67bA98F8IqwAHdOIdkUyXFYIXnk6t1Jdo6iS1ON0jJOPq041qF0uP7VUM7avnAm +p0aywn0WfDb8Elws6o6iYwpQJqfmWngi/AH8EyxqB68L7wIryqQ9fHHojwhL77txGhVJNnw+iqTS +ew9Ft8HahvEFPB9WVHkz+KAkC7O4dD4N7oMbw5FQ9P88WJG9TAuXLko672vBcUjz7OUsDbpT3pzm +4XDs3QtOLPCkUagnQsPgG+Gr4JXgOPRXhGdkaaDv3xNpdVukHec7fBIBF7XJJ9sk9YkI4uxvrmhQ +5z6OVe368g7p91znV2UpXo38tjg2K0fS7fsn8iL+18KyiX2t31ddh46KcHmkvO/rpv/fOH+v85L8 +6az2b3uL/JV5h+yWHOtmUf+s1T2SrrBQEOyvNy74fSY8Gi4uYr4Kj4nod4/L/L4j6q0VwZR9NyHy +O5FxxVsrfL/OWuHvNFnrqNbLFPF2h9PQG91x7db2MH/jcWU9v1ODi5ghMoq/WnvZIh+cccYZy3Es +X6Fi4cKFCT+ldevWiVRlcMpxp953dcoTba3mLQKd5KyiylkQfy67lmWWCpRypesxbPOIxukEpjsq +7my5NKKqjGLZdGQqly1aMPMtmhsjsyyshaxYNBJFbWIok+OvC6/afZOhnRwzXRDEt8PnwnqUEZXk +uF8E94vaIKbcb8hrEZ8EfwbPgufCom9rk6J8Cgc5N9fAwixM6lP8OKyxjoH7wHGoO8KHw7fGaRRB +djoyg2FdKLKRbiq2gcfDPbMJZSjXzUw2am7zUDduQ+DnYM3ZqHQJgrrAC+M4dC/C98Rp0CJlZ886 +iXGtEntsvvcbq/35XkWnMelRUqcr8Yv2597irQHV+uOCIThF7VxdndT37sFpvaFOmQ5+qPpLQc6l +70/xKlod6vd6KH091XdQNxOo5R2tz/Y7FZtGwjGvwTWjGNvjxfgDBdmymPzpXutgsL/TJOzUclqf +/L7j3w1e2Xcbb171eAZRrLWifkdLaAlOavjaUsVbAAYxVyrh9jilri7hrLIPtg3yVbDqAxzVViRy +5CvkwKoep3UmZbMp6yZIKVuo9L333kv3lVTcrKicPXA5rbooCOxcrBOaaRwB5fkYkZyUr73q00m2 +yKZcNqtOY4sbRaFJVtLd6iFZa+tXyPY/wrobTl9k60t73v8oPBG+LFNljrLdqeuUo77QKl3414D3 +h++E5XT8B56ZZPdF57BBJOdYj12vhPPpVP+7wnfDcekMGsRxmvLp/x4BYZ/LaXU6fiQzDM40n51M +eqoI4/rphRw313n4ErbHjf4tTZubM2CQq+hLKhVtXaKJd5SybzPrj9xyYOPzqqzKjf3eky/J5rSq +sRw7v9f4afzlJw5o5Yb0NaGeUp+nS12W0byvQ8HLh3bGMTurTmHeA/39qX+O17vHrhmc1jqtcTYC +2c+Dt15UfFGnMt+BHqdPfT3OOp9Po0I+33sdKndPRkJzyvvbjv+Rv88dRpvoawVvL0j8A1pOzUtu +pRxJHu8vDwKdHQrMkQ44mqvheK6azsisQv2qYlfnytyx6mi/CuWpa4qO6aez+zGY66s5pjHv9ko6 +RH0xUqDn6TnTlyjfXUW+enXpZFyax4xEtWyR85rJmU5vn8nudJmox1rM4th5CvK3R1UekpODtTXc +J1SWK9uGyt/DD+YSill3HPKjY7YpVHwIDZ+O0VjO7RGw7nI17qi0DoI7w09GbZBH7mTq5ZBGpQ8R +fAzeI2oD5HaEP0qTb87z8BzGopuUjdLGVKxDRUs0N3QTuIRT1R9xDpeKDELCUao4yu8z8bbIbZKC +ft8Jn5Pdl1+4D6TPu3G5FH2d6/mt9vM3Gzs3KbY4mTdHzmzXxQVRchX78wgf5zhztDKTBr/P+JeC +KQM2JxbGNoVg7UwyWcrkVN+Vpa6A4sqTEw5pxJZ+3wc/5Ad18daK6oxrRcQeW64YjmQFXL3uuuvu +ySj3wuHE9wz00Zv8tpSRJPYP1PEVXJmqaqsT1/6AKK0DS76ArkV6s0BCJ3XD+Actnbd/J/vVetQs +KTXKZml9ZqN1gnXScrHG7Zy81hnUOIdeMk42l746kyqDvlIUxXE49Fg4bnTJjUFfhqPdQcR0r4hy +UcQU8S2V0/osfY2LYlSajBYEOdcL08rzHe6aTyBi/TPIjY0oGxb7e/ggQl5R13RqzvOwisEMhvU0 +pDFI37mpjaG4GerUDUJ08v1TCnFawx0QfX3Iq6hUv3M9LuJ+7/EfhOtTeb+mfyofJeP7k3CO60d0 +I7Sl3f/wPPQkKwYF6wfP7btejAbZRX3/GRzu+GtF4MdcK/xMa0V2u5pnTS4fIVNd6rE9TmXiZhYP +U9eMRTibbUmXgfVUR5FYpWFOL8t03AU98lrdetaa/a8/o8dRJptylbl2TZ46B63JDSmCAQJcJ0hj +0oJ0NaxN8rq7lrOlekfOuZXs1GRh2PlU2Z9g6XOyZFMkZ3Y+rIjBCfCGsOs7rIfiktAq9LJpjJ4K +eZQdVv8xB5/D3eEo1DOKUAQZfenOiyBXDBGdR0UtCyVFIuVgnxRDQbEc1ztj9BkWfT18ECGfHpVq +CfOQ6Jc3Cj4rwvjjiLzZCDrj9F82ssEL+y7vLVy0TeSHzb7/HE5noTfadcbt95rwdPD8oI38ncbN +qFORPAheHLSsV1W1fYxdM1VeZeuYjmfdnnHIJ/ParceIBv+hbk2Oo+oaBQO0DjeM/MQ2q/g62lS+ +7i3QJS8qBelrRdSGzUku9rWfd6rqL181RueLtdEBzqaSBhN69NRarMjsctoKG1IazoeKyz/rwCp/ +S6NZKAdVY/oKHhOtSUJKTq07icp/kuREZZ6P/amX4+r6ziPeKNW7x9CqR2P/iiGfTfRJKuTcR6Fi +3W3fSGf1H+1FsSC+jJx7OTENofNpfDisbQNRaHOEloPjPOLPpFdOcyH0PY0WwInFM4IC2RqmljIP +L2BQ+8CbhQfXgLxucg+Dha3RQpyuOP+O5funFxO0bE5roo8F8zWHExf6SH36FVf4PR/8NJJsLqFW +BEAW+e/gsbTOJba4LlDk+KrFxwXmaoLC1oodHvrem9Z/AfZGWysC/ZFCyyX9G9Vaa621FI5ipMfv +HTp00Guu5sCLTjzxxPb85evrOJUH0F5+RGNQBY7rPPa5/kafbeAFp556aifs8H/77Tfn+2TsVzJU +VLAXdzZOdmPZl7HvbIUtzXEVwCJ9+TvCcnL0xcp1a6iTFj5xykuP00W2Hkl/Faw+HIa55OspKHKB +HOeo9AuCl0UVziG3Xo669Crh1AHWD50KJV30FcEsFclxbSgJ6wfgYyIqUiR/E3haRPlsYh9nq8hT +rrn/DbxGHjlXvZTLJNOWMg8114bAr8IRHQkks9NpVL2XvXoJq/GDONHWT/zeE18sHUJ+z7qXgzw9 +VxT0Q8x6Svk1/0fsG9WN8vb1KjMVBMGOODmJJ8GZqiOXtW1X0FqReAI9ZZ8Ya4WfvlZENrE5CBI5 +reSdqTdj626wnvRqLa9DiZPFSaMwQLb9mWeeecFFF110Fe9bPYqtAruw/3Q4aQdYMsWkCvpbwNsG +dPNwL9njzjrrrG1xYi8j/xsOc2IjLaYl+nV2JtNqZDrR7gnGeKg5rsU8Lbl1NaVDmduy4tXqF4lR +aWUEozpSUXVGkZONn0cRzCJzH+Uzs9Q1RnFhkYj6lnxYvyhnSdTobDYlP1DxU7bKCOVfIxPVcU1X +15LmoR7tK/KqqHlD6Akal/KGqyG2lqZtkHr3dv7+gsRr5vLLFU0iWCmyKp+AyHJt9HSuWKS1Iprj +6nltvZcO6IJ84d913/vB32FcA9r7XxNxLXStKBZmZaFHEUzeu/otzp4c9IxOetIvDNt7/vDhw5+7 +8MILryXVE563JYMDG5YpSp5XY+l1WOLzcIyVKlqvbZQJW8O2ubxLZQD5bzVG5cuB6t0VlINRDbDB +3alog7OiraIqWOH7bKw2YedWeZVlk1e5dIrUh4vmur4TFSX+iOMwlNi0VHcN3eM0JaWp8TM6v18U +qZvMPwDJrryhjmtDnfvfspuWt6alzcOLGfH0vKPOLvAjVUPgplwbslvXdDUrRO/a1w1EKSmGbd5n +xX0Jvh/vJnfhoji2ZsDQb8q1IoM9S1xRexzIIclR31mC0eu6NhYn9GBSOa3Nltxj7nIfgHMm89np +9iatiuCfYDmWOkHZbmE0/mmw7prVh0gXmXXg3rBzSsnWIfUzH9YjcPUlcn3XHpX2s6HOTimsbehN +0oxSGJnsQ/NBNyjFoLiOa0Md/GLYXKiOljYP9f0fAr8Gt4Xj0jAa6HGqURiBOBHXyprvw01LkI/j +DMZzNPMZ73sfxrrFqQlka9z1JZ8VVl8AAsl/udL6NxuW35Hveqfry1JEaP9HqoimUgUNfoXlizh/ +hGxiVrhj+Scu7+qU5iK1aQPrRrqCvubQr2yUrVHs7IB8N43x4osvlo4mp+biuAr4fCQZOY+aENpr +NwaOQnJw5ai4ySA9feCb4KikPtV3FDuj6owjt3Qc4WYqW0rH9fMiYqSohm5yot7hdipi36VW1RLn +4ZeAqMepK8UEU2vCtzHbLBnifrBs5JWyulL7xEtJevwejXz/i2iCEaUqKr/wanSvFJEqapaNKGli +jYwAj9/XJHr6Ck7hc1G6Su5j1b9f/VvyHFfT/lyyC0idL5JTFW0iyaFP73fVP2wtaNeu3XdsRXiG +H4PN03E+HWorI7CzDbJrkjXHNedZWVypkyPwXOpq0o/zlYfrdVFRe3GmlcKVqV+x7koSJ5A0E0lP +NkqvyzSWbG2jlifu2qIKN0M5YSYHolSk1zoVi7QhPqrTqj4L33NWLIsL19MS56HeVxnXaRWCWjPu +hDeD9WMNoxQCPs5oENVBXCbVrCQZRcAi2sa/GhXVpEWLVoulL/B+jiVvwo2GAE4dfmBwLA5eazpx +/kWu/iSjtxBo//vzOIiDSPeDZ6NLa0fRCHXqS39EMJF9qjfwo7DdyR8H5424aly0lT2LyE8rmlEN +VFTOEVfnVOabBAI1U7Qzm6OpclenPlw+DKUrc7KyIRulO6Y61taEXG2kV3Ka5MWg74qhpIx1aHza +n1MqWpeOss2NuDZsFLPBDzHly0m8pc3DfQH3iAYAvBZtr4GPaoCOltc0SESiu0camF+jx6+lJEXJ +14zY4QYR5SKKVaAv12UjTU2FbxH9NEia6hAnUDdY8W48ao11QZIXOBwMZ/xhV61ogz9flwZsXRmH +uTNZcWRKjjGyfGMKlrPj+iYD3zzi4PVtl6MRpvTjcJ3yrt6l4XpXlp6GZbLlZYscaXE+0hiLQaXe +B1YMm+PoKLVD5PYufxnHyCyycR3X5nwum7Pt6adPPzS7Mb2wgOOhtJkMTyigbcts4gffZQwXZBxt +EPUakLF17MJapzpiM3/t4LVhrf2txujHwEWgIJ4j3LbCHNcioF4MFURMZ+AMXocuXfdd4CtdtfwJ +V6d3mbUlivmkhMjr0b2yetqrYFwdop7qhEA2n03/tiUh57OE28snaUVf7oe3j3MsdQoGZZIPt5VR +skdbGWaEK5oynw2EprTJ9X0MmQvgFWEtDGGA3QTQCdEG9ZVgNyHINinpJOsHGVpUlHe2kk2Q7FSk +dRZ8dqKk4R9xHAbd2enHJqUmjbdQaopzuz7GFsNx3TjmoEvtpMc0L6d4S5qH2iNfrGifdL0Em6NR +O33i4PCH2ial+gyir1P6s4DZP66NZcX6kdYGkUfp+wu9bR5iW1H4shi5tQkWGYGRI0f+h0fw/8I5 +/Bj/cVX2kf5MfiHOXlteRaXrVxv93SrvUl2Jsq+QWbuqqup99pz2kSnI+snXYMlnEIepBidTDrFI +jq0e8YtFndHVifpWsI7lE9VpX+vPBvJUU5OFspHwmvDXcFf2vM4mVTvtZZ2nPPLqaxXSj0h7aIyk +ZUHl7Li+CEK7w/rBh05GmNwJ0C/wzoDPh3XWXDnZkpL6dTYqfwN8MexC8emOlyYI+7wy7q+lODa9 +G6OFfih0Qgz5JVV0fwb+VAMH34H2/WLo0B1wsaLwMbotmmhLmYdDQKR/0VCpdYBvQd/eRdTZfFUF +/luR4wyBt1YwrV8v/vL12WINOHjr8I7+ZmPnZtTnV77gBdVHZ6zLVOgvOoBiXX8aRMFz+63lLVqw +VQwlL+FcpF9XYjQ30UZAYHucvLnwxpybr9E/Dycw8W9alHXEgf0ap3UDympwFDfFif2JdEvk7sth +i3NEF6JzLPwAOt5p06ZNwnHlzwGWRreeShwEHwzLt3BtyNYn2vfAhi9JN+bPB+Qwr4levYNW/mAH +yn+lvpJU16NNsPsXZHYkP7m+tqYpKWfHVQ6gPP58v2KT8ypyX2KdOOfEurKEQPJDesPl4byTSy9L +b+PkXLmTd6lsku3cEeck1z6nUITKxyPIOJG+ZFaHZ7gCSzMi8EdKr4ffzlgbrfAcxNaIJpqQ0s3a +vBjy5SbaEuahvhval1ps2guFf4b/UWzFWfS5CE2W6qYsrniYJ4+jI1sQBKOQ3SGyfA5BnODB3s+/ +nBW8sO+O/o7j6z/dCNo/6vlzawhP6ToSgYLTgxcG3uHv+NAXEYSzi1QvvIrKttkF6tWAoVE5IYAD +OR8ehAO4LHb9iuNXnXQG5Re04nguTmBXIq/rUa59pqtQLiezDlFHVSI6qjrNQ/kRg/mjgkfrCNYe +6PH/N/Cj/BvWZPq4mbz2ybq2ZOsS+hUJPphUkVbZsgz96UZO/ogit6qXEbK/G/auRppwlOtqarqj +cnZchUq2xdc5p3IO07/sznl0aTq66eWuDz2+F+kuw5UlCvhIb5Ov3NkkfDUZ6k3OpIJs5U5/1HQW +gkQxIv23urD7C3wGbJQdAc2Bq+Gds4vkrNEWgZNyStSvfKp+UbMqae7zUN/V22H3pKTY4F+Owqfh +Qh656UlJHFod4Y/iNCiVrN93wufB1H7v4xxuFKnPwNse+bP9PpMuiCSfRSh4dsB+7NQj8s1j14XV +jxB57ZMeefX73vsDf736Ciq2z6KmbnEQtPcWLLyCwv3rVkQ/CqYO2IMob5wnM7gz/iPRezDJEiGg +11sd4/oi77KJlCimHtdrW0C4/EYdyFEMF9IW0ZTbcTp/DfvooEGDEn4Jf7tax2+gvIK/Yw14Y8A/ +edfqqrTVOpMi9CR0U55IOW5P9lgJJItSaaaypN2a42VDdRAsG6tqDdFZq87CcljFojonkWM3AdLT +hHCo3h27C0IVBWKRK6s9WqzTHbvU9eGOXepscnZmG0dqZrqGDUgfj9H2NGT/FEM+m+gaVHAh8PRI +vCVSXwZ1SAED042LFiR3MxRVxRNRBctYrjnPw+PAVee8sagjisfChQQMfohp1IYR5DtFkMkpEjzb +by+imBfg7E2Cx5M/Fwdxl5yNaivjRQyD4Hx0H+8uwhH01xHB8f0T3sE9CadVNUGwlffz/8YFU0Zk +OBd+3Eei+wVT9tm7TocRD4KXD+UmqUY3yDHI/6/fa+L7MRqYaAkQYG6667mu/84HUM96Ait2VIPD +KJ8gI2mOJ3Vpn+n7bAt4SIJyTnFa1U79pFhl7733XsIXQe+91M2A5duFbeCwLiVtCMv8D4lwZDWn +nXW1lfaonB3XfEikO43u2E2e9NTpc+XuWBcTkdJwPlGY/Ehv4+rC5erf2eDq049deWOkuiCGJ2Gu +PmSXHlkenksoT9121CsyMRQeB8d10mjSLOhOrDwDjnou10dWuGhPUBzSNoHX4jQoU9nmOg83AM+L +Y2Kqx3ThNSBK820ROiuKYJpMXMf1VNrnmrMHUq+nNAXddAYvDloFZ/BRrzp42KsJzgKFfeAB5Efw +Z+tPUXd/MOXgrmljWHxYWXkL1rngw+LyXLma4GpvWv9pwfP7bJxLLFwXvLLvctjyEI7qGLhNuA57 +9/D86frhXF2qXOof2BbvHam+h9Pef3gQjIh8TQ2e67+tN3/2m9i1Xl0D8hxVBHHnaR6FVl0MBHAE +E983Eq0Jmgea3yPhXYmo6mZOeZUlXvpKmpWSOhQJnYHQHAkSUc261rgoLHteJas2aptVXvXOOSZb +hX3D2ccqG8WXh8bAYflR5C9Z+ZlezyKdJE0ct1inp2rg6l2dyk6EFenSXbZYeZU5cm3ccTgN61F5 +zokSbtgI+XfRSUQhMunc3w7fDcvZikral3Mz/Dy8QrLRnqS3w+l4JKubdVKJ9RfBE+Fl8oxkMPXT +4c3yyGWq1qLWEqg5zsNWAH8n3D7mCVCE9vKYbSQ+HN4mZrvPY8pvj/yFcPoar+P/g++F14Q1hlgU +PDtwJW/BfDlcf8jaMAgO4Hr7WvDaoKUzyfg9x3/MlVU3z/EoCHp6i/w3gmn7XBxM6bdONkcxeG7f +9ai/3Ju36GPs3DdrJ4F3JI7t+eF6v+c9OK1+POcwIIpeUzPSm/b6U8Hz/VYO60vPJyJqU/qfzraF +57FN5yA6+f77Xq92t0dvYJKlRiDpEKrbS3nEPwJ+fdSoUW8oT91lcezBgaycOXNm5OvqUkstJVld +s/ISuhN6cVpHYN8l0FuyFT4dJ3a0FITGkldfKQW0YDd3cs6iToLLa0wu79JwmfIiRSj1SE2cTqpz +EyasIywXLlc+n3y4bWPkz0WpIimtIyrXReyQZBs5vffBH8Ofwe5RhmTkrO4Gy0EVZ7rAS8+PcOwL +IW2aA+2DkZ/CL8GvJFnj3RpWFE2sqF0hNJ1GjxbSsEzbNLd5eCY46jzGId3k3gK3gXeFt4Cjktbd +u2C10Y8iotBHCH0N67sYlfSkQBEU7YdUdLhnkruQOjqNzI1w9AhjzSKil15XpyBHuoY3Z/5V1A/N +KFPT8TyGz1OfIKNzm7GNCvUaqsA7nczp3tTpc9mi8B6lb+Ojz6NsTfL8Qn/RRhJNXQUSB1k+guBs +tiF8xZsLxqQkgmVHe/5Px9HXqqmyKJkg6MvLGz/GGZ7O1eB1L6iAvXfIr+T5QQ/09fCm9dsKVatH +si29z4rgTN9PPC5Or7HjJkZATl7SF9T3exY/fErcmA0bNqz1yiuvrD2oetfqtdQdCa8Y1Vy1jSqb +SY4+M/qfFMrB/ZRXct2kdtjXSk7ymDGJ9xLLzsFwvO+mFJWABHBzJXcy5ViJ5Gi6vOqcE6k6Jxsu +U7nkFbqXk+bqJKsTKmxcO7Ipcrpdqgon52zIZkdKSSNl5FjpIvS3mPo1Xi4gqa0DC8h/CS8F6wKl ++ih0LEJy5s6LItwMZXTBd857sczXnDmlWMrKRE9zmoc9wOzsmLjpsf2fkm30XTkU1s1Hphu6pFi9 +ZF1KroD/XK8me8GTVA3JXp2xRpHdXNFdPUXACfTkwOalxP7V6uq98wo6AUU0n+13td9rEo5lXar9 +IRTRziCBQ93K6EcdaZ8co75KBVKNd30wtf83fp+JuiHx/L63zw+mDDjW86vZZpC6NkRVjk1BL9r1 +St3/6wrhrhJRtaTL+d5kv9fkienFdlweCOAgOh9CBk279NJLZyqDIygfI0FEM7/lB1RTET3IlTV2 +Kn86Rx9P47Bqb6svx1qpZInAfsp7aV8gq+td2ZFzsMrOsAgGuZOxMCSrlcstEelpSCyVlYwc1Law +Iidi5XM5rel6dSwKr5rOJmdjrURpPuUEvdzAroTD2vAKcFSn1XU5gkxcx9m1XRLT8xn0lBY48OYw +D9uB+1g47g380bT5NnTOPiB/cug4alZ69ooqjJxsbQzSDecqkRTX1ORygjOrqPaztuFNAVdyrbwj +c8NSlhJ9CoJzwxd53n4wwQv8c0ppRda+fP8dr5V/aNZ6qygrBBTNxBFU0KcO4bR2w2lVoKxkRH/O +R6nXJ3XOTsmk/BWcVl37Uw53vYZNXNBcHdcwyE+C4QxYFx+xxiRnS6ljd6wTk34SdZyJKU6R6y+s +2+l0fbj+v6SVbBJl6q+2pvE+56O6P/xZ43WRV/PfkTgkr5QJPAUEF7RQGJrDPBwJ9rWPlKOfhLGI +PpRB/AbKHs5Qnq/oFgS65RNK1j9DOjWibBwxRYrPjdQgCDaLJBcWqsjTptOKf2KlfCLcpPR5/wWv +bdvfp1/k/b6TRmLbPaW3J9Sj73/ntW61j7/TpNmhUsuWLwLaErA/P5Q6TybiwPp6ZZXylKtMNyAl +cwrDN2OyIUSLqBuKnaeqTDbKVuWxU3tx+8Els1P9RiU5W82V5EyK3oP3gHeHNR5FPhPgk0pGE0aP +8zrDf4FXgnXHo3LJOT1k65Crkz45qd/Aujj9CisimakfnWQtwLJJlE13bW3jfX6HakVyXoTz/aCo +MayYh9KmGnsxxqNzrIt562Ioy6LjE8q1gGketVQq53nYC9BPjAm8bkoVncxGR1GhR+KKVkQlyY6B +943YQPtWn4e1JhWThqJMWxc+yq3U54Yk7lc70E1MVvK3GrMwmDJoP8+vmkLUc6usgo1V4ftsB1j2 +UH+H2zPbuUr3od7XXyyPbbs2lglZ9ertBkHQv8F/cJC1A6toRAQyfUfdNSXul6hgM3UzBmVtT109 +OylzdmZt15QVLcFxFX7vJzkXljo5A2E5rs4ZDZ9N56i6VLqUd7Lfkr8IjhPmD+unaUnpA3rTI7oH +4d+VsOfP6EsX4bdK2Gexu/ochRfDd8OJO2XSYtIbKNPNlhy7lk7lOA87AfodcJxzq+/ykfAvcDbS ++ZQD+Eg2gSzlA5Ltbs1SHy7WNqAT4b+HC4uQ1/p4CvzHnLp8/q41x0Uwc1v/zczli0v9vuPmBK8N +6+3N+fY6L6gZsrimsXP+dV7vHsf5/git8xnJX3d0Fe971auzrsBnPy6jUGMU6g0CgdefqK9uco3K +HAF5hziJYSszzalMZfpTAv31arhtKh/lrQKKlMIZ/Q2ZJWUuTSlOZmR3ehnHGe3MINckRXEW7iYx +MEKnOikah5sxSh2ruXPOFXGtd2chgSS5kxdOXV4i6kM6RE6n6yfcd9iWWumm+/wPXW8H31UiEx6n +H0VMmrPT6qC6j4wi9MWmqSjsA+tGaEmhcpuHVwJ895jgj0b+6Qht/oXMtRHk0kWupmCt9MIsx7JF +XEyairIz8yqs9J9ldQ2vi7mbJN7VGujJT14i8vobP4460vMrhvKscl7eBg0R8P0v6WcgTuHfcjmt +rgu/L78I7zP5+KRteoLXyORP5CqznTmtjQxzEdXjtDo/wGlNP9Yj+HplEuaVVNG/U057xJQuE7pd +GqVZNic3SttSyMjJau6kk+LuDtInhY5dmUuzjdfVh1OXVxvl3XE47/S5OtnSaJPQdRYj/Q3Zw+HB +8OdwY5Ai3vvBf4B/aowOmkinHuEeBv9chP51sbsQFkbairCkUbnMQ/0iPndUsf6Z+ZCi0+sXZy1R +5FLfiTikKPCdcK6b67C+4zg4Adb2pIaQ1io58rvBeZ8A+D0nTEfuBjgaBf6Ffu/Jn0UTrpXCeb2N +f0zfmqt8IXuGc3clR1r797osvSH9jM8tXL82YVsFr07zvX/Vry1Cia/fa1T8yeszcV/b01oEPEuo +IhS5TFz/OU6EUImE1iT/8UpRT/d9rSNDxNWFW+U/1IR0LUq+nkojSbTJNCT1oXKis1UkCV1JHSld +OMeJchxY11dCH30nbJKNTo8rQ1dCbxynV3Y0NrnIYWP3Uwr92U5qAngMUH02Gdnn6tJT1YlU7nQp +dXKqaw6ki+I98BGwIitrww2lT1EwAtYjdYcN2RZFGtsz8I3wPgWO7AnaHQt/XGD7ltSsKefhcgB5 +U0wwtagfDseJAGqv5CHwq3AbOCrtiOBpsLYkRaFrEHoN1raBHlEapMlM5VhOtnREpy7LnOr9/EtP +rsK/y9nIZy/uUivqZi02+TtNfo9G+/B+1S1Yac9muR1A6oIDsfXRQBf0h72g1Qi/7/h3C1Hg2iRf +7cVf3fbfAQxGwn1dXcGp782i7UXeKt3HaGvC4hhJwRqtYYkRwNlrnewykeLs7T18+PDOOJDuuIq8 +myuJdQGZ/fgF/yq883Vd8mrufLIKHSC/PW8iuI86OZtZ/3GLejXWNVh6t4BFdXRxfBJ9aYuanoqK +nL37YeeK9NVWhahaSH5X5aGEnTi9ibS2qOk/3cCa3pKmt0AnXs6oS2VRYiYpAykfPk4UNrMPXYRv +he+Ad4F3gzVB9UvhKGMTPq/DijY8Av8bbqkOK0NLkX6Y1w/uCcshGQTLCcpFX1E5Dn4AfjmX4BJY +11TzUJHCFWPifSHy8Ry72g60XeYM+Iraw8ifI5B8HJ4escULyG0Ja5+sbo52gnNdZGZTr+/v7fBj +cGzyNxs7N5gyZBte0H8+jU/m6pq4yKYUceFjJeX9rD1G+VuN0LkumPhTAO0HH6h/yeKKuifLDeuV +34c+O+VV6vs1yE3xKvx7vI5tH/S3GvdL3jYxBPzeE19EfGcc2N/RjxwCPU3ZkXwu/Bf34Htsoal4 +jJX3Ma+myzN6d+ziSss1NwRwXN/GwdNTAt0kiTQPusEuyKVrrL7Xuh4or/LWcHucxc9IP4DD12HV +t6KuM2mCav1Td1Q/pR6fM3iSGjm66brkmLaHdQ0P2yA7V4DDdqpeDqz27bZv3bq12pQNhQdWNkYV +2RA9etNJ7AJPhTeFF8KaMO5EkU2Rm1AqUN7Jvk2+D6zHxk4n2RZBXRmFIjb6kimvVHj9Cmsv5qxk +qi+W8s2R/oHRR0c0XOdaznw20tzZDl4dXgVeGdY8+TrJn5Lqi55pflFslAWBJWEeZhl60Yo7oqk3 +vA68PKzv8U+w5qbm5TTYXVjJNox4SX93r6J6G/4hiu9LjR7Dv+W1bvuKv8M49dcoxA+lWnmt3tiS +B6p89/xupN34V6puvHN1Ib9E+JJ0Bs7jl7ze6lPs0NhLRrwdYSmvooq1NFgRpxSbanQOmNf+XOz6 +Dvu+w17S4KO4WyhKNgjryBAwBJocATmZIi3g+nWrnIkFsCKFYVa5jl3q6iSrMrWVDpHTWXtkn80B +ATmuOo9R+K3mMCCz0RAwBAwBQ8AQWNIQqPuIp2WPXtHTVqEh6jhMcmhELlU+LKMoW/hY9UaGgCFg +CBgChoAhYAgYAiVCYElyXH8BUz32dhR2UFXmnFKXqiwso0fk0mFkCBgChoAhYAgYAoaAIdAECCwJ +jqse+csZ1T7Xs2E98lf0VBR2TGtLFn+6OsmqjdpKh3RJp5EhYAgYAoaAIWAIGAKGQAkRCD86L2G3 +Je3KOaDq9EV4iwb2HtbXQFXW3BAwBAwBQ8AQMAQMAUMgKgJLQsQ1KhYmZwgYAoaAIWAIGAKGgCFQ +xgiY41rGJ8dMMwQMAUPAEDAEDAFDwBBYjIA5rouxsJwhYAgYAoaAIWAIGAKGQBkjYI5rGZ8cM80Q +MAQMAUPAEDAEDAFDYDECS8KPsxaP1nKGgCHQ4hA45JBD9qyurt55ww03PH3EiIb9xWgxwDn44IMv +5Z8Sp9933333F0NfFB302bVt27atbr/99jr/bAceFRtttFH4FX/e+++/H1Ce880oRx111LJz5szp +CqaflgOmUTDIJXPggQfuzV9y7tqpU6dTxowZo3+5a3Q69thj2/7444+r33PPPf/J1tmQIUNWnDdv +Xmdw/iTfOcmmo1jlw4YN64Ata3Xt2vW/V1111byoeh944IF6f8gzaNCgGv39aFQdJldcBA499NBV ++fvZ/2PO3w3pr9kLItaVq9Hz9f33339ZQQoaqZE5ro0ErKk1BAyBhiOQXDiPz6Zp6aWXbvPrr7/2 +ov7kX375Ra+sW5RNtlTlLPTH0Nc9cKM7rjjt2+C030afG82fP78GB+0ZLlbn3HvvvS9pvB988MGb +8O+UD5H+jnjL0HEqS/tBHByP06q/NK6k7YKDDjpobKtWrc6+6667vkkJNjDDed0Ym9/FuTkaB39M +A9XlbU4/O9Hf8R07djwD4UZ1XHEaOi9atOiO77///g/cwLQDv4/p8zrG+XdnKOdtc+x5AEdxXZWB +82wwOYnzdrOOOQ/6pz/3F9XzcBw6qNwR7bek/avov4O6oa48W4runZF/2tWDx97Y84iO//rXvy71 +008/3cb3aCD6KmbOnFmNzfd37tz5Tzj5v7k2mVL0bvDggw/qr8DrEGXCeVSdwhZ8AF79wG6ihsj3 +73zO47nh4eoGhe/nZ8i0A/unwX7XcH2x8/SxvOY7qb7rBTmuuun98MMPB6DjM3SUleNqWwWKPWNM +nyFgCBQNARbNh2AtwGK9zk4XhtNc2corr6x3Ky+RxMVwGS5O/2TwCyorKw8mPRxc1uHiOJnomXtX +tUfZG/BfQnxxJsAOO+yw9Sm/F14KjA8n3Y9UzsfmpCl9HBvlQIAbiauo3pXzMBzM9yb/AflrcDZ3 +V7MjjjhiOc7bNLK/wnuC7f7IvUXZTThA+0qGsrsoGwY/r+MwyaGgjxso+6VDhw6nheuy5en/Q+mj +/up0GZzWuyj7PfwX5lFv5O5Afj9uBG9Jl812TJtb4PAcezybbEsvB7tjhw4d2ik8zqqqqpMobxcu +K/e8ngC0a9duA2i3crPVIq7ldkbMHkPAEEghwGPWZzkQe1zUNybZoX379jfceuuts1UmojyRzpo1 +aw0iVVdwsCP8MVHCY9xjMiJDenyvKNNGCWE+kN2D5B9crAfRz6voeQ6Z/3A8FifiYtL9af8Vcv25 +KB+JbF/qvyB/JXpu59hLRjwvo2xzDj+gzQlE21SVIvpW5O10CjaF9dh4XLdu3UbjMLRFdjpOyiNE +aE5wDZI6x1E+gvLbXHl6yraA/+G8borzsuj666+fo3rGsBZ9XUDEdDUOP1UZx58SlVMELydhiyKz +lYzlQfqVAyt6CK4TPWI8m6Lzcngb6n5C/gGic+e76JzDkbpH4XOoXxHZKaQngNvMZPTpSepk28Xg +O5yxHk+XE1RG+6GUn4C8xvIRRaOwf5zqkBWOf4F1Mb0Kme1J3+BcH42OBdBojnvCs2h7Ku0eJp8i +zRH0K/K5LfwhbYZz7p9yAnI45s6dOwq9+9C+I+nz6D6VaLPsSPXPtowd6OtKZKbSh5zIFDGOPw4e +PPicO++882sVckPwycKFC/vjbOod4o/T7m+060zfx4LHS5Lh+CHs+pnscHg8OuSwPs94hXGd6DjR +2aMp2xr+y2233fY9aV4S7gjdxLnbjbmdmmscK/Ldn7qrGYeLfD+LLQFlR9L/WZT/N28HnvckfdR7 +wnD44YdvwngfYaynoGMHxnkQPBtc7xw4cOBFBxxwQLWToV6PtrfDngOp/x88at99972X6O1F5A+g +vr3aUXaG2kWwqSlEnmJ8uxJJ1xy9VAbwfe7CudfxW/DKKnMEzu+TfwTshE+CKLsWHduD+5YNxQY9 +lZzjq0nr4a7OsG0N8B5JVt+ZxHefNexa5vsnqidK/CTzTWtW3qi+5EtFFnEtFdLWT1MjoC9i4mIQ +IdXjFaNmhgCL8yQubG1hneetuFhc7YZA3XLwau5YKRdJPX5dHXaRkJVpux3txlPWqk2bNlU4HZtR +JuetDXw5PBdOOBIs+psj+wLHO8CTYA/nTw5ZW+VFcoS4MNxG39+h50LSmfBlP/zww5k4xb9S9g71 +R3GB6ljbgr/l45jylXCYHnFl2VI5r85pGlrYtAAAEG5JREFUlQy6d6Ptj+jW470Ecbw8F8PjuICd +h83aVpGRaPseFXJYTkT2ZF3U0gWTjo4eUW+E3lupf5f8yTxmvickKxx353gsqS7M78KD4Bc1Thxt +Odn3wYoGT4Wv51zocXrCMUSfone/wXJCW8MPYMuhqqdsGZLVSR+GhfPLyPcF9wk4SDrvcpIfh9em +/pb0/ZeUPwYvj5wi1Zty/p4Am13Ie5LFaX2adrpIv0R6L+mO6H6BebCSZChL9E9fD6JnO+z+TuVh +QiZwTqvKcVrlZHs4BK8phXrAi1ZcccU3E0d8qA3J6+jcTBFVV56egp9svwj+Nzc/tx3FXuRwdD1d +Pt8x/W4uGdJ/p8m+hi2yY7O08oyHtN8SHEfg6J6k/ZVOCB363qxOeinz+nDyeoLSluPzH3rooWsl +52TIXkRej6bl/K2O/C3IPMGx2j0NU1RzyoQJE7LOYWSalDjHYzHga+w8kRu0xLrCHDuWsqUYx8Xp +xjHO1eDl0sq7IruKyqhL4Ee2IGxofy62ZMRd+qm7AJlNyI6hT83pI5nvibVM9ZC+y91qs+XzmfUL +Uj4mmiWGQFEQkNPRMyIrumbU/BC4m8jFrvDeLLaKMmzPxbROhCPfkGizIc7IaejYStEsnI71KKtA +3w2UXUAUZHuiYcdJD4u+LkStWrduvRV1h8NyZHQxTv1YRZHh5ZZbbl3aHUC7K5AZSP0MdO5MKofh +NpKl6EeOnXfiiSe2J1EIefLYsWPrOUWSyUbJaGQvdI9Arxwh6f+FY837q7D3HC6i03A+E5GgdD3Y ++AHyZyAvZ/tyZD8Dvwk4Ils7WeouIb+QqPcmjOUk2vSjzWjK+8uRD8npgrcXMgfCfSk/AZk1uCge +I0ebuluSso9TfwmO9vu070LZCHgCZduB1xn8aEk6Z2DPuaRhcud6TwrvR/fGpNNoty02Hcx5OI6y +5XF8NPYUUfYY9ZsjdxQyG2DHIio1Jm/8+PGHkWxN2VDqZfffyCvyuhx2/00yIfqZ/dXrYOODobJ6 +2eSNzygqpgwYMGCKBNAnx+6D9B9AYc8blLf65JNPVpBcJmKeXI5NneG/cvPTl8j6jzzS3zWTbJQy +zvFqkkNf+s26bBEl6hX5CzPzIlHOWOYiU835OYX0bPgKsPqA+j7kw1TB92ANMP0r0XndVEyl7dHM +2bVDQvPAcx1khlAmboNMj5VWWmltyv5Im00pC7B5f9KyJHCowrAr4RXZHjBU+4fJHw9/QoT5n6SF +UqHY5MSdc/FnvmNbgu9FYK+nHDeD+YZ6KlKooaVoZ45rKVC2PgwBQ6DRESBCeXeok6nkfRbh7UJl +UbIL0HOXE2QP7cPkFUGZxEV2Ok7fCccdd5yiIHJAtmShf4XHanoEmCCOFYWsQz///PM2XMgvgh+l +/WPIdKTtshJaf/31H+X4W44TN0vffvutfiDTGb65jpI8B9gmx1ePVPHL7k9EstQEZ2gwUaDd2KvW +kXFtQ/0bcjLkUGVSyQVMTtwGtDuN9C3s6I8j8ipOpXtcLSd2Bo8Q/8Z4zhJzLIdTJOcxQfTzBRfC +Z9wxju4d5KvQ5/S4qnCqrQqKmOsHYQnd/DjkTI7lwK/tIlhqkHauX1YZNquPBDHmqcrQX53zjy7d +KCSILQJfkHkC3pQoZytktyJfTbq+65/8LrSZT3lqbOTl6N2Z7+0E7GVdBewmI/4rth3sHm/TVs5e +N+kJE30lIm9EpH8Ll7s850A3JYdzrGjzHFI5wLJlVUVelY9L2CVbRF1rk9Rn4tjV4zC/RZT5HcfY +kbj5Yb58qfnF04nf4QR1wpZBaNDN140pTWSQf8A9GRBu6NV58ElTNzvk76V9oHakiXNKOs45+Myn +Hzh+B95eMuVKOOhjsO0nxnwK24GOIb8sNl/qzn8hdheKTT7cufFZha0AxzLfH4T1pGJ32cf5Lmg+ +FTK2Qtq0KqSRtTEEDAFDoNwQ4NHp1yGbflaeBd8PlbXW42B3AWGRXjpUl8hygXmVSKe7mHu6aBJp +Wh8HRBG843H6riLS1R3hE2A5sN/DKUJG/Va7AhzVY2nzd/Q+Qt00UkVkurt6HKZFOJ1ylE9S9AnZ +och8xQ8iHncy+VLa/R4Z/ZjnBaJTR4blcc4+5Vgs+jfOz6VErO6Fe3CccrgTtckPHN//kpVjcin2 +74dN/0R+MMfTGYMcy5XIHwSniLL3kNPYEiQcKEs4ISro3r37b1wg55HNes2hj8SjVWS2hes4ihx/ +wLlsTZqg8Lmm/Ff61rlOnQv6Tpx/0jrBGWR+cjqUYuePJJKpIK9ffGu+7A+H6b/UpXSrgj2uU8IC +6Xlw64qTp20jS9Nnb87DtyGZr8j3lGMb3lJA2VbwbBy7X0KyqSz49NQBtvQTuwryY3777TfdPCSc +SVceMf1ScujYmuR514bjxA0GcCTqSY+mLvVd4lhzJEGMLYzFP3GAJtJ+oKKNbCFJyCBfB3fOl3CX +g5o6p+RT399ll132V14lJpE6uKP3f5TV+95KsFxIDjoY6CnEudg0knHN5Pt8Rzb7qHfz3onUG1+h +2NAuK+5ab5hTbyIzg44fgn/A5u1Jte85da6dUeWU1vlSl5NhZoshYAgYAsVCgMVZey3bsj8uFfFj +kd4vg35dGFOkVxvpgGjPxThLa6BnIov6UTicclrfhXc/8sgju0lGhE45PZWJg9pjRex+5MLVD4dw +FFEp/fCio6tXSrTqdhLE/BHU9SV/G/prSPMSDtL2tNO+wQ+Jqu7jolOuIY7qNoomumNsdxGub1yZ +S9G1Ahezf+hX764MB+0F8nLE10uWacxzwEKPF7VdYBPGtAWPQTcjH34UujlR3fWTbbyPPvpoH/LL +wO+ojHEmPBpSRecSxBikW87uw063UpzxrZWGf5BX2yL+J+M/0LXSe0vJD4Q/AqMF9P8O9mhbyDHp +/XPu/uTaKe3SpUtG51J12teMnkfJromufjh2b6o8RIryaj/0Lq5Mj2ZpswHHiTpXHk6JbD6JvpNC +rMieJs5NtE1Ft8Nt8uXZ5vIc7RVR3jlNtg/H2tLxksqZ/zeDyU2OXTSdfn3NQcmIwFE3AJvS7tfr +rrsudQNIVQr3hCBvwFCKbGI+JMtaTMJ3ejQYaPytSK/U/Mo0OOreA4MdXR3f1y4cp58LV11ImhV3 +5pOeRnTgxko/EhwO68eO36kTyvQ9LFtKLWhla6EZZggYAoZAAxFgQZZTdTmOy61EQ3TB3wHehotE +Ts04FxcjsDcX578Taf2c/JroekMXIpw8/djqX/yC+N/Uj0a3fgU/BJmUUmQfprw/j7xH0O9rRKAO +oV4/vEg5PkR436VO0czDkm1vJU0QjvNGREWeQs9wnIXbXLlS/WAI+xTJlSP8FY/vr0BPQoSySTgl +r/LIbwp9z6L8CSoUKd0TXR/ww6BpCcHQB22O4vBo9uYNRP4x8l8xNjmccsQnwrqgncd4nvjuu+8e +5SJ7O21qGNNf+OX3q1SfKBkRfdRg2zQwGs3hCsgpkvwzWwauUz0XyS/pQz8AOgrsfiVdjrLLKbuZ +6mGklZQ9Rn9rfvPNN6fT1wE4gM+qbUMInWdh07roeAu7/0qqV3+NlE4e0d9G5PIUxncn/d9IkX54 +tjv970z0cGv3qFuyuYgfeN1N/VbCmfZHoOuIpPxMxng2Tv+9vOP1fM7rddiyGnI/gvMxkiF/ZVK2 +XsL4hbE4QeCmt1UMg8fjWL+mQvRNQ8fP9DOgVir3JxHfH7HvFnQcQ9s7aDuF/O606kv+cv2AMJcG +bBiK/M3omEb6BlH1PsivQ1u9aSNgjrrmmyDzCmV3gK8ixruT19sT3kOmhxNqKalwBc+bGM8R3ORo +LmUkMBgHFpcj+0/yz4LNENKww5+xXYzCrLhzw/0d830BfZ7MuemAzs2wZah0U2YR1xggm6ghYAgY +AkVHgIv+FzgoJ7Iw62b9cngp+JR8HeH86RfuisTJYdOPgPT88nS1w1l4lKjFYLJtqJPOgfTxR9J5 +cIL4p6Z7aC8HT1sNxlFYyfH9yepUQt3Y5IFeTv65q9AFhLpu8FquzKXUySHskjzek7yciATTxxb6 +wwDSP1NWAx8N74XsU4xpr/TIrHTQ70Uk+lX3J6Taayt8ZjGmPzPWG8gr8qao3yB4DRyvO5DRxXk+ +rJuBFFGuX+9fT8H/wXr900eku4Rf34TeMyjTnlI5t3L6faKrx5O/BpaDP4Gy4fAD4Dyd4wYTegZi ++/YougK9eiXVnxnTvVKsiC7HinZ9TN158ARkd+P4gqhOq/TQRpFTRRM3hFPnhKL9VD569Ogq7NiB +7MvInkV6Hanm5Z6cgxdJCyJFO9GjaG+i/6hKiJYL8yvgPWh7Gzr6whfzlOC0fDqItmtfqubGJrB+ +3LMWfBF7tzWuMEnX98zZa6jvTf5OMBhC2mKJH+/pPb6/yzV3eEpyJwDIsdV7f89FXmuEbjKLRVlx +13eR/k6iI333tL3jIOb/BcXq2PQYAoaAIWAIFAmB5C/3Y2ljUfeTvxDO2I7HzvX2pYUFcSraiMNl +4TwRj/2JugSkB4bL1Sfl3yi6Gi6Pm5eeOK9NkhPE3t6OufrRNor0101JnjH8F56sfD7cJKO/RlWa +Tjw+19aCRiG3BSSbcp2rfOPP1jZOOf20KmY/ityB/ZA4NoRl883jsGx6Xo+508sUTdW8pi4RddZ3 +L848TNfXUo/1HdB3rrHGlw/3XGtbY9nUEL1lHQ5uyMCsrSFgCBgC5Y4AToZ+FCGHVhG473mMvKki +cs5u6rsTFdmAoKAe3TcLwmb9cOd9oofaZmBUQgR4dH80TxfGMGdS21VK2H29ruS4sl1lOtHVwdil +6KKRIdBgBBrNw2+wZabAEDAEDIEWjICilUQk9TL/dRjmazgb/cNOq4aubQPNyWmVzYxH+2eL8mhf ++oyiI8BcubFcnNak1b9iz2NsEfg6+ihM0hAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQM +AUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwB +Q8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFD +wBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPA +EDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQ +MAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAw +BAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAE +DAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQM +AUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwBQ8AQMAQMAUPAEDAEDAFDwBAwBAwB +Q8AQMAQMAUPAEDAEDAFDoAQI/D8vID6miTPf7AAAAABJRU5ErkJggg== + +--Apple-Mail=_18FE9DD1-B053-4781-ACFD-7E542B418B05-- + +--Apple-Mail=_6F6D23B5-0ADE-4347-978A-E0A21B92BB70-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-3.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-3.raw new file mode 100644 index 0000000..1571300 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-3.raw @@ -0,0 +1,183 @@ +Return-path: +Envelope-to: test_receiver@example.com +Delivery-date: Wed, 27 Jan 2016 16:24:20 +0800 +Received: from 23-59-23-10.perm.iinet.net.au ([23.59.23.10]:62191 helo=DESKTOP7C66I2M) + by webcloud85.au.syrahost.com with esmtp (Exim 4.86) + (envelope-from ) + id 1aOLOj-002xFL-CP + for test_receiver@example.com; Wed, 27 Jan 2016 16:24:20 +0800 +From: +To: +References: +In-Reply-To: +Subject: RE: Sales Invoice: SINV-12276 +Date: Wed, 27 Jan 2016 16:24:09 +0800 +Message-ID: <000001d158dc$1b8363a0$528a2ae0$@example.com> +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_NextPart_000_0001_01D1591F.29A7DC20" +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AQJZfZxrgcB9KnMqoZ+S4Qq9hcoSeZ3+vGiQ +Content-Language: en-au + +This is a multipart message in MIME format. + +------=_NextPart_000_0001_01D1591F.29A7DC20 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_0002_01D1591F.29A7DC20" + + +------=_NextPart_001_0002_01D1591F.29A7DC20 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +Test purely for testing with the debugger has email attached + +=20 + +From: Notification [mailto:test_receiver@example.com]=20 +Sent: Wednesday, 27 January 2016 9:30 AM +To: test_receiver@example.com +Subject: Sales Invoice: SINV-12276 + +=20 + +test no 6 sent from bench to outlook to be replied to with messaging + + + + +------=_NextPart_001_0002_01D1591F.29A7DC20 +Content-Type: text/html; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +hi there

      Test purely for testing with the debugger has email = +attached

       

      From:= + = +Notification [mailto:test_receiver@example.com]
      Sent: Wednesday, 27 = +January 2016 9:30 AM
      To: = +test_receiver@example.com
      Subject: Sales Invoice: = +SINV-12276

       

      test no 3 sent from bench to outlook to be replied to with = +messaging

      fizz buzz

      This email was sent to test_receiver@example.= +com and copied to SuperUser

      Leave this conversation = +

      hi

      +------=_NextPart_001_0002_01D1591F.29A7DC20-- + +------=_NextPart_000_0001_01D1591F.29A7DC20 +Content-Type: message/rfc822 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment + +Received: from 203-59-223-10.perm.iinet.net.au ([23.59.23.10]:49772 helo=DESKTOP7C66I2M) + by webcloud85.au.syrahost.com with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) + (Exim 4.86) + (envelope-from ) + id 1aOEtO-003tI4-Kv + for test_receiver@example.com; Wed, 27 Jan 2016 09:27:30 +0800 +Return-Path: +From: "Microsoft Outlook" +To: +Subject: Microsoft Outlook Test Message +MIME-Version: 1.0 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AdFYoeN8x8wUI/+QSoCJkp33NKPVmw== + +This is an e-mail message sent automatically by Microsoft Outlook while = +testing the settings for your account. diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-4.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-4.raw new file mode 100644 index 0000000..9937488 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-4.raw @@ -0,0 +1,138 @@ +Return-path: +Envelope-to: test_receiver@example.com +Delivery-date: Tue, 09 Feb 2016 14:53:22 +0800 +Received: from 23-59-23-10.perm.iinet.net.au ([23.59.23.10]:56280) + by webcloud85.au.syrahost.com with esmtp (Exim 4.86) + (envelope-from ) + id 1aT2As-003QlT-B0 + for test_receiver@example.com; Tue, 09 Feb 2016 14:53:22 +0800 +From: +To: +Subject: test email +Date: Tue, 9 Feb 2016 14:53:13 +0800 +Message-ID: <000001d16306$8b9e5c60$a2db1520$@ia-group.com.au> +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_NextPart_000_0005_01D16349.99C37120" +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AdFjBjqdYnxyziyBQVK9mLTYYu+9Og== +Content-Language: en-au + +This is a multipart message in MIME format. + +------=_NextPart_000_0005_01D16349.99C37120 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_0006_01D16349.99C37120" + + +------=_NextPart_001_0006_01D16349.99C37120 +Content-Type: text/plain; + charset="us-ascii" +Content-Transfer-Encoding: 7bit + +4th test email + + +------=_NextPart_001_0006_01D16349.99C37120 +Content-Type: text/html; + charset="us-ascii" +Content-Transfer-Encoding: quoted-printable + +

      4th test email = +

      +------=_NextPart_001_0006_01D16349.99C37120-- + +------=_NextPart_000_0005_01D16349.99C37120 +Content-Type: message/rfc822 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment + +Received: from 23-59-23-10.perm.iinet.net.au ([23.59.23.10]:49772 helo=DESKTOP7C66I2M) + by webcloud85.au.syrahost.com with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) + (Exim 4.86) + (envelope-from ) + id 1aOEtO-003tI4-Kv + for test_receiver@example.com; Wed, 27 Jan 2016 09:27:30 +0800 +Return-Path: +From: "Microsoft Outlook" +To: +Subject: Microsoft Outlook Test Message +MIME-Version: 1.0 +Content-Type: multipart/alternative; + boundary="----=_NextPart_000_0001_01D16349.99C25FB0" +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AdFYoeN8x8wUI/+QSoCJkp33NKPVmw== + +This is a multipart message in MIME format. + +------=_NextPart_000_0001_01D16349.99C25FB0 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +This is an e-mail message sent automatically by Microsoft Outlook while = +testing the settings for your account.=20 + +------=_NextPart_000_0001_01D16349.99C25FB0 +Content-Type: text/html; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + + + +This is an e-mail message sent automatically by Microsoft Outlook while = +testing the settings for your account. + +------=_NextPart_000_0001_01D16349.99C25FB0-- + +------=_NextPart_000_0005_01D16349.99C37120-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-self-sent.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-self-sent.raw new file mode 100644 index 0000000..7744f99 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-self-sent.raw @@ -0,0 +1,91 @@ +Delivered-To: test_receiver@example.com +Received: by 10.96.153.227 with SMTP id vj3csp416144qdb; + Mon, 15 Sep 2014 03:35:07 -0700 (PDT) +X-Received: by 10.66.119.103 with SMTP id kt7mr36981968pab.95.1410777306321; + Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +Return-Path: +Received: from mail-pa0-x230.google.com (mail-pa0-x230.google.com [2607:f8b0:400e:c03::230]) + by mx.google.com with ESMTPS id dg10si22178346pdb.115.2014.09.15.03.35.06 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +Received-SPF: pass (google.com: domain of test@example.com designates 2607:f8b0:400e:c03::230 as permitted sender) client-ip=2607:f8b0:400e:c03::230; +Authentication-Results: mx.google.com; + spf=pass (google.com: domain of test@example.com designates 2607:f8b0:400e:c03::230 as permitted sender) smtp.mail=test@example.com; + dkim=pass header.i=@gmail.com; + dmarc=pass (p=NONE dis=NONE) header.from=gmail.com +Received: by mail-pa0-f48.google.com with SMTP id hz1so6118714pad.21 + for ; Mon, 15 Sep 2014 03:35:06 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=gmail.com; s=20120113; + h=from:content-type:subject:message-id:date:to:mime-version; + bh=rwiLijtF3lfy9M6cP/7dv2Hm7NJuBwFZn1OFsN8Tlvs=; + b=x7U4Ny3Kz2ULRJ7a04NDBrBTVhP2ImIB9n3LVNGQDnDonPUM5Ro/wZcxPTVnBWZ2L1 + o1bGfP+lhBrvYUlHsd5r4FYC0Uvpad6hbzLr0DGUQgPTxW4cGKbtDEAq+BR2JWd9f803 + vdjSWdGk8w2dt2qbngTqIZkm5U2XWjICDOAYuPIseLUgCFwi9lLyOSARFB7mjAa2YL7Q + Nswk7mbWU1hbnHP6jaBb0m8QanTc7Up944HpNDRxIrB1ZHgKzYhXtx8nhnOx588ZGIAe + E6tyG8IwogR11vLkkrBhtMaOme9PohYx4F1CSTiwspmDCadEzJFGRe//lEXKmZHAYH6g + 90Zg== +X-Received: by 10.70.38.135 with SMTP id g7mr22078275pdk.100.1410777305744; + Mon, 15 Sep 2014 03:35:05 -0700 (PDT) +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id zr6sm11025126pbc.50.2014.09.15.03.35.02 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 03:35:04 -0700 (PDT) +From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA" +Subject: test mail 🦄🌈😎 +Message-Id: <9143999C-8456-4399-9CF1-4A2DA9DD7711@gmail.com> +Date: Mon, 15 Sep 2014 16:04:57 +0530 +To: Rushabh Mehta +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +test mail + + + +@rushabh_mehta +https://xhiveerp.org + + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA +Content-Transfer-Encoding: quoted-printable +Content-Type: text/html; + charset=us-ascii + +test = +mail
      +



      @rushabh_mehta
      +
      +
      = + +--Apple-Mail=_57F71261-5C3A-43F6-918B-4438B96F61AA-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/incoming-subject-placeholder.raw b/xhiveframework/email/doctype/email_account/test_mails/incoming-subject-placeholder.raw new file mode 100644 index 0000000..35ddf06 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/incoming-subject-placeholder.raw @@ -0,0 +1,183 @@ +Return-path: +Envelope-to: test_receiver@example.com +Delivery-date: Wed, 27 Jan 2016 16:24:20 +0800 +Received: from 23-59-23-10.perm.iinet.net.au ([23.59.23.10]:62191 helo=DESKTOP7C66I2M) + by webcloud85.au.syrahost.com with esmtp (Exim 4.86) + (envelope-from ) + id 1aOLOj-002xFL-CP + for test_receiver@example.com; Wed, 27 Jan 2016 16:24:20 +0800 +From: +To: +References: +In-Reply-To: +Subject: RE: {{ subject }} +Date: Wed, 27 Jan 2016 16:24:09 +0800 +Message-ID: <000001d158dc$1b8363a0$528a2ae0$@example.com> +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_NextPart_000_0001_01D1591F.29A7DC20" +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AQJZfZxrgcB9KnMqoZ+S4Qq9hcoSeZ3+vGiQ +Content-Language: en-au + +This is a multipart message in MIME format. + +------=_NextPart_000_0001_01D1591F.29A7DC20 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_0002_01D1591F.29A7DC20" + + +------=_NextPart_001_0002_01D1591F.29A7DC20 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +Test purely for testing with the debugger has email attached + +=20 + +From: Notification [mailto:test_receiver@example.com]=20 +Sent: Wednesday, 27 January 2016 9:30 AM +To: test_receiver@example.com +Subject: Sales Invoice: SINV-12276 + +=20 + +test no 6 sent from bench to outlook to be replied to with messaging + + + + +------=_NextPart_001_0002_01D1591F.29A7DC20 +Content-Type: text/html; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +hi there

      Test purely for testing with the debugger has email = +attached

       

      From:= + = +Notification [mailto:test_receiver@example.com]
      Sent: Wednesday, 27 = +January 2016 9:30 AM
      To: = +test_receiver@example.com
      Subject: Sales Invoice: = +SINV-12276

       

      test no 3 sent from bench to outlook to be replied to with = +messaging

      fizz buzz

      This email was sent to test_receiver@example.= +com and copied to SuperUser

      Leave this conversation = +

      hi

      +------=_NextPart_001_0002_01D1591F.29A7DC20-- + +------=_NextPart_000_0001_01D1591F.29A7DC20 +Content-Type: message/rfc822 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment + +Received: from 203-59-223-10.perm.iinet.net.au ([23.59.23.10]:49772 helo=DESKTOP7C66I2M) + by webcloud85.au.syrahost.com with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) + (Exim 4.86) + (envelope-from ) + id 1aOEtO-003tI4-Kv + for test_receiver@example.com; Wed, 27 Jan 2016 09:27:30 +0800 +Return-Path: +From: "Microsoft Outlook" +To: +Subject: Microsoft Outlook Test Message +MIME-Version: 1.0 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable +X-Mailer: Microsoft Outlook 14.0 +Thread-Index: AdFYoeN8x8wUI/+QSoCJkp33NKPVmw== + +This is an e-mail message sent automatically by Microsoft Outlook while = +testing the settings for your account. diff --git a/xhiveframework/email/doctype/email_account/test_mails/reply-1.raw b/xhiveframework/email/doctype/email_account/test_mails/reply-1.raw new file mode 100644 index 0000000..69e5141 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/reply-1.raw @@ -0,0 +1,47 @@ +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id n15sm4041161pdj.34.2014.09.15.23.48.02 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 23:48:04 -0700 (PDT) +From: Rushabh Mehta +X-Google-Original-From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040" +Message-Id: <943C954B-A6CE-4E8B-BA0B-1714309AB8BB@xhiveerp.com> +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +Subject: Re: test +Date: Tue, 16 Sep 2014 12:17:58 +0530 +References: <54A4EFFA-AD17-456A-9851-9715574DF0C9@gmail.com> +To: Rushabh Mehta +In-Reply-To: <-- in-reply-to --> +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +test-reply + + +On 16-Sep-2014, at 11:57 am, Rushabh Mehta wrote: + +> with attachment +> +> +> +> + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/html; + charset=us-ascii + +test-reply
      +

      On 16-Sep-2014, at 11:57 am, Rushabh Mehta <test_sender@example.com> wrote:

      with attachment

      +
      <xhiveerp-conf-14.png>

      +
      +

      +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/reply-2.raw b/xhiveframework/email/doctype/email_account/test_mails/reply-2.raw new file mode 100644 index 0000000..74b3466 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/reply-2.raw @@ -0,0 +1,45 @@ +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id n15sm4041161pdj.34.2014.09.15.23.48.02 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 23:48:04 -0700 (PDT) +From: Rushabh Mehta +X-Google-Original-From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040" +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +Subject: weird subject ddwdf23r2 +Date: Tue, 16 Sep 2014 12:17:58 +0530 +References: <54A4EFFA-AD17-456A-9851-9715574DF0C9@gmail.com> +To: Rushabh Mehta +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +test-reply-2 + + +On 16-Sep-2014, at 11:57 am, Rushabh Mehta wrote: + +> with attachment +> +> +> +> + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/html; + charset=us-ascii + +test-reply
      +

      On 16-Sep-2014, at 11:57 am, Rushabh Mehta <test_sender@example.com> wrote:

      with attachment

      +
      <xhiveerp-conf-14.png>

      +
      +

      +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/reply-3.raw b/xhiveframework/email/doctype/email_account/test_mails/reply-3.raw new file mode 100644 index 0000000..fdea5e7 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/reply-3.raw @@ -0,0 +1,45 @@ +Return-Path: +Received: from [192.168.0.100] ([27.106.4.70]) + by mx.google.com with ESMTPSA id n15sm4041161pdj.34.2014.09.15.23.48.02 + for + (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); + Mon, 15 Sep 2014 23:48:04 -0700 (PDT) +From: Rushabh Mehta +X-Google-Original-From: Rushabh Mehta +Content-Type: multipart/alternative; boundary="Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040" +Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) +Subject: Re: weird subject ddwdf23r2 +Date: Tue, 16 Sep 2014 12:17:58 +0530 +References: <54A4EFFA-AD17-456A-9851-9715574DF0C9@gmail.com> +To: Rushabh Mehta +X-Mailer: Apple Mail (2.1878.6) + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; + charset=us-ascii + +test-reply-3 + + +On 16-Sep-2014, at 11:57 am, Rushabh Mehta wrote: + +> with attachment +> +> +> +> + + +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040 +Content-Transfer-Encoding: 7bit +Content-Type: text/html; + charset=us-ascii + +test-reply
      +

      On 16-Sep-2014, at 11:57 am, Rushabh Mehta <test_sender@example.com> wrote:

      with attachment

      +
      <xhiveerp-conf-14.png>

      +
      +

      +--Apple-Mail=_C996D08F-7A29-4DA2-99B3-17133FA73040-- diff --git a/xhiveframework/email/doctype/email_account/test_mails/reply-4.raw b/xhiveframework/email/doctype/email_account/test_mails/reply-4.raw new file mode 100644 index 0000000..be651c8 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_mails/reply-4.raw @@ -0,0 +1,75 @@ +From: +Content-Type: multipart/alternative; + boundary="Apple-Mail=_29597CF7-20DD-4184-B3FA-85582C5C4361" +Message-Id: <07D687F6-10AA-4B9F-82DE-27753096164E@gmail.com> +Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) +X-Smtp-Server: 73CC8281-7E8F-4B47-8324-D5DA86EEDD4F +Subject: Re: What did you work on today? +Date: Thu, 10 Nov 2016 16:04:43 +0530 +X-Universally-Unique-Identifier: A4D9669F-179C-42D8-A3D3-AA6A8C49A6F2 +References: {{ message_id }} +To: test_in@iwebnotes.com +In-Reply-To: {{ message_id }} + + +--Apple-Mail=_29597CF7-20DD-4184-B3FA-85582C5C4361 +Content-Transfer-Encoding: quoted-printable +Content-Type: text/plain; + charset=us-ascii + +Testing another reply! + +> On 10-Nov-2016, at 3:20 PM, XhiveFramework wrote: +>=20 +> Please share what did you do today. If you reply by midnight, your = +response will be recorded! +>=20 +> This email was sent to rmehta@gmail.com +> Unsubscribe from this list = + +> Sent via XhiveERP + + +--Apple-Mail=_29597CF7-20DD-4184-B3FA-85582C5C4361 +Content-Transfer-Encoding: 7bit +Content-Type: text/html; + charset=us-ascii + +Testing another reply!

      On 10-Nov-2016, at 3:20 PM, XhiveFramework <test@xhiveerp.com> wrote:

      + + + + +What did you work on today? + +
      + +

      Please share what did you do today. If you reply by midnight, your response will be recorded!

      + +
      + + + + + + +
      +

      +--Apple-Mail=_29597CF7-20DD-4184-B3FA-85582C5C4361-- diff --git a/xhiveframework/email/doctype/email_account/test_records.json b/xhiveframework/email/doctype/email_account/test_records.json new file mode 100644 index 0000000..2e204e5 --- /dev/null +++ b/xhiveframework/email/doctype/email_account/test_records.json @@ -0,0 +1,29 @@ +[ + { + "is_default": 1, + "is_global": 1, + "doctype": "Email Account", + "domain":"example.com", + "email_account_name": "_Test Email Account 1", + "enable_outgoing": 1, + "smtp_server": "test.example.com", + "email_id": "test@example.com", + "password": "password", + "add_signature": 1, + "signature": "\nBest Wishes\nTest Signature", + "enable_auto_reply": 1, + "auto_reply_message": "", + "enable_incoming": 1, + "notify_if_unreplied": 1, + "unreplied_for_mins": 20, + "send_notification_to": "test_unreplied@example.com", + "pop3_server": "pop.test.example.com", + "append_to": "ToDo", + "imap_folder": [{"folder_name": "INBOX", "append_to": "ToDo"}, {"folder_name": "Test Folder", "append_to": "Communication"}], + "track_email_status": 1 + }, + { + "doctype": "ToDo", + "description":"test doctype" + } +] diff --git a/xhiveframework/email/doctype/email_domain/__init__.py b/xhiveframework/email/doctype/email_domain/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_domain/email_domain.js b/xhiveframework/email/doctype/email_domain/email_domain.js new file mode 100644 index 0000000..fbf8b84 --- /dev/null +++ b/xhiveframework/email/doctype/email_domain/email_domain.js @@ -0,0 +1,22 @@ +xhiveframework.ui.form.on("Email Domain", { + onload: function (frm) { + if (!frm.doc.__islocal) { + frm.dashboard.clear_headline(); + let msg = __( + "Changing any setting will reflect on all the email accounts associated with this domain." + ); + frm.dashboard.set_headline_alert(msg); + } else { + if (!frm.doc.attachment_limit) { + xhiveframework.call({ + method: "xhiveframework.core.api.file.get_max_file_size", + callback: function (r) { + if (!r.exc) { + frm.set_value("attachment_limit", Number(r.message) / (1024 * 1024)); + } + }, + }); + } + } + }, +}); diff --git a/xhiveframework/email/doctype/email_domain/email_domain.json b/xhiveframework/email/doctype/email_domain/email_domain.json new file mode 100644 index 0000000..5cb4c19 --- /dev/null +++ b/xhiveframework/email/doctype/email_domain/email_domain.json @@ -0,0 +1,157 @@ +{ + "actions": [], + "autoname": "field:domain_name", + "creation": "2016-03-29 10:50:48.848239", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "email_settings", + "domain_name", + "mailbox_settings", + "email_server", + "use_imap", + "use_ssl", + "use_starttls", + "column_break_9", + "incoming_port", + "attachment_limit", + "outgoing_mail_settings", + "smtp_server", + "use_tls", + "use_ssl_for_outgoing", + "column_break_18", + "smtp_port", + "append_emails_to_sent_folder" + ], + "fields": [ + { + "fieldname": "email_settings", + "fieldtype": "Section Break" + }, + { + "fieldname": "domain_name", + "fieldtype": "Data", + "label": "Domain Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "mailbox_settings", + "fieldtype": "Section Break", + "label": "Incoming Settings" + }, + { + "description": "e.g. pop.gmail.com / imap.gmail.com", + "fieldname": "email_server", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Incoming Server", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "use_imap", + "fieldtype": "Check", + "label": "Use IMAP" + }, + { + "default": "0", + "fieldname": "use_ssl", + "fieldtype": "Check", + "label": "Use SSL" + }, + { + "default": "0", + "depends_on": "eval:doc.use_imap && !doc.use_ssl", + "fieldname": "use_starttls", + "fieldtype": "Check", + "label": "Use STARTTLS" + }, + { + "description": "Ignore attachments over this size", + "fieldname": "attachment_limit", + "fieldtype": "Int", + "label": "Attachment Limit (MB)" + }, + { + "fieldname": "outgoing_mail_settings", + "fieldtype": "Section Break", + "label": "Outgoing Settings" + }, + { + "description": "e.g. smtp.gmail.com", + "fieldname": "smtp_server", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Outgoing Server", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "use_tls", + "fieldtype": "Check", + "label": "Use TLS" + }, + { + "description": "If non standard port (e.g. 587)", + "fieldname": "smtp_port", + "fieldtype": "Data", + "label": "Port" + }, + { + "description": "If non-standard port (e.g. POP3: 995/110, IMAP: 993/143)", + "fieldname": "incoming_port", + "fieldtype": "Data", + "label": "Port" + }, + { + "default": "0", + "depends_on": "eval:doc.use_imap", + "fieldname": "append_emails_to_sent_folder", + "fieldtype": "Check", + "label": "Append Emails to Sent Folder" + }, + { + "default": "0", + "fieldname": "use_ssl_for_outgoing", + "fieldtype": "Check", + "label": "Use SSL" + }, + { + "fieldname": "column_break_9", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_18", + "fieldtype": "Column Break" + } + ], + "icon": "icon-inbox", + "links": [ + { + "link_doctype": "Email Account", + "link_fieldname": "domain" + } + ], + "modified": "2023-06-05 12:55:06.434541", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Domain", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/email/doctype/email_domain/email_domain.py b/xhiveframework/email/doctype/email_domain/email_domain.py new file mode 100644 index 0000000..136b9a9 --- /dev/null +++ b/xhiveframework/email/doctype/email_domain/email_domain.py @@ -0,0 +1,124 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import imaplib +import poplib +import smtplib +from functools import wraps + +import xhiveframework +from xhiveframework import _ +from xhiveframework.email.utils import get_port +from xhiveframework.model.document import Document +from xhiveframework.utils import cint + +EMAIL_DOMAIN_FIELDS = [ + "email_server", + "use_imap", + "use_ssl", + "use_starttls", + "use_tls", + "attachment_limit", + "smtp_server", + "smtp_port", + "use_ssl_for_outgoing", + "append_emails_to_sent_folder", + "incoming_port", +] + + +def get_error_message(event): + return { + "incoming": (_("Incoming email account not correct"), _("Error connecting via IMAP/POP3: {e}")), + "outgoing": (_("Outgoing email account not correct"), _("Error connecting via SMTP: {e}")), + }[event] + + +def handle_error(event): + def decorator(fn): + @wraps(fn) + def wrapper(*args, **kwargs): + err_title, err_message = get_error_message(event) + try: + fn(*args, **kwargs) + except Exception as e: + xhiveframework.throw( + title=err_title, + msg=err_message.format(e=e), + ) + + return wrapper + + return decorator + + +class EmailDomain(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + append_emails_to_sent_folder: DF.Check + attachment_limit: DF.Int + domain_name: DF.Data + email_server: DF.Data + incoming_port: DF.Data | None + smtp_port: DF.Data | None + smtp_server: DF.Data + use_imap: DF.Check + use_ssl: DF.Check + use_ssl_for_outgoing: DF.Check + use_starttls: DF.Check + use_tls: DF.Check + + # end: auto-generated types + def validate(self): + """Validate POP3/IMAP and SMTP connections.""" + + if xhiveframework.local.flags.in_patch or xhiveframework.local.flags.in_test or xhiveframework.local.flags.in_install: + return + + self.validate_incoming_server_conn() + self.validate_outgoing_server_conn() + + def on_update(self): + """update all email accounts using this domain""" + for email_account in xhiveframework.get_all("Email Account", filters={"domain": self.name}): + try: + email_account = xhiveframework.get_doc("Email Account", email_account.name) + for attr in EMAIL_DOMAIN_FIELDS: + email_account.set(attr, self.get(attr, default=0)) + email_account.save() + + except Exception as e: + xhiveframework.msgprint( + _("Error has occurred in {0}").format(email_account.name), raise_exception=e.__class__ + ) + + @handle_error("incoming") + def validate_incoming_server_conn(self): + self.incoming_port = get_port(self) + + if self.use_imap: + conn_method = imaplib.IMAP4_SSL if self.use_ssl else imaplib.IMAP4 + else: + conn_method = poplib.POP3_SSL if self.use_ssl else poplib.POP3 + + self.use_starttls = cint(self.use_imap and self.use_starttls and not self.use_ssl) + incoming_conn = conn_method(self.email_server, port=self.incoming_port, timeout=30) + incoming_conn.logout() if self.use_imap else incoming_conn.quit() + + @handle_error("outgoing") + def validate_outgoing_server_conn(self): + conn_method = smtplib.SMTP + + if self.use_ssl_for_outgoing: + self.smtp_port = self.smtp_port or 465 + conn_method = smtplib.SMTP_SSL + elif self.use_tls: + self.smtp_port = self.smtp_port or 587 + + conn_method((self.smtp_server or ""), cint(self.smtp_port), timeout=30).quit() diff --git a/xhiveframework/email/doctype/email_domain/test_email_domain.py b/xhiveframework/email/doctype/email_domain/test_email_domain.py new file mode 100644 index 0000000..1f7ae54 --- /dev/null +++ b/xhiveframework/email/doctype/email_domain/test_email_domain.py @@ -0,0 +1,39 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.test_runner import make_test_objects +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Email Domain") + + +class TestDomain(XhiveFrameworkTestCase): + def setUp(self): + make_test_objects("Email Domain", reset=True) + + def tearDown(self): + xhiveframework.delete_doc("Email Account", "Test") + xhiveframework.delete_doc("Email Domain", "test.com") + + def test_on_update(self): + mail_domain = xhiveframework.get_doc("Email Domain", "test.com") + mail_account = xhiveframework.get_doc("Email Account", "Test") + + # Ensure a different port + mail_account.incoming_port = int(mail_domain.incoming_port) + 5 + mail_account.save() + # Trigger update of accounts using this domain + mail_domain.on_update() + + mail_account.reload() + # After update, incoming_port in account should match the domain + self.assertEqual(mail_account.incoming_port, mail_domain.incoming_port) + + # Also make sure that the other attributes match + self.assertEqual(mail_account.use_imap, mail_domain.use_imap) + self.assertEqual(mail_account.use_ssl, mail_domain.use_ssl) + self.assertEqual(mail_account.use_starttls, mail_domain.use_starttls) + self.assertEqual(mail_account.use_tls, mail_domain.use_tls) + self.assertEqual(mail_account.attachment_limit, mail_domain.attachment_limit) + self.assertEqual(mail_account.smtp_server, mail_domain.smtp_server) + self.assertEqual(mail_account.smtp_port, mail_domain.smtp_port) diff --git a/xhiveframework/email/doctype/email_domain/test_records.json b/xhiveframework/email/doctype/email_domain/test_records.json new file mode 100644 index 0000000..a6ccc99 --- /dev/null +++ b/xhiveframework/email/doctype/email_domain/test_records.json @@ -0,0 +1,32 @@ +[ + { + "doctype": "Email Domain", + "domain_name": "test.com", + "email_id": "_test@test.com", + "email_server": "imap.test.com", + "use_imap": "imap.test.com", + "use_ssl": 1, + "use_tls": 1, + "incoming_port": "993", + "attachment_limit": "1", + "smtp_server": "smtp.test.com", + "smtp_port": "587", + "password": "password" + }, + { + "doctype": "Email Account", + "name": "_Test Email Account 1", + "enable_incoming": 1, + "email_id": "_test@test.com", + "domain": "test.com", + "email_server": "imap.test.com", + "use_imap": 1, + "use_ssl": 0, + "use_tls": 1, + "incoming_port": "143", + "attachment_limit": "1", + "smtp_server": "smtp.test.com", + "smtp_port": "587", + "password": "password" + } +] diff --git a/xhiveframework/email/doctype/email_flag_queue/__init__.py b/xhiveframework/email/doctype/email_flag_queue/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.js b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.js new file mode 100644 index 0000000..9cbf7f6 --- /dev/null +++ b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Flag Queue", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.json b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.json new file mode 100644 index 0000000..14b1ec4 --- /dev/null +++ b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.json @@ -0,0 +1,67 @@ +{ + "actions": [], + "allow_copy": 1, + "creation": "2016-04-20 15:29:39.785172", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "is_completed", + "communication", + "action", + "email_account", + "uid" + ], + "fields": [ + { + "default": "0", + "fieldname": "is_completed", + "fieldtype": "Check", + "label": "Is Completed", + "read_only": 1 + }, + { + "fieldname": "communication", + "fieldtype": "Data", + "label": "Communication" + }, + { + "fieldname": "action", + "fieldtype": "Select", + "label": "Action", + "options": "Read\nUnread" + }, + { + "fieldname": "email_account", + "fieldtype": "Data", + "hidden": 1, + "label": "Email Account" + }, + { + "fieldname": "uid", + "fieldtype": "Data", + "hidden": 1, + "label": "UID" + } + ], + "in_create": 1, + "links": [], + "modified": "2021-11-30 09:51:34.489932", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Flag Queue", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.py b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.py new file mode 100644 index 0000000..3dabf1e --- /dev/null +++ b/xhiveframework/email/doctype/email_flag_queue/email_flag_queue.py @@ -0,0 +1,22 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class EmailFlagQueue(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + action: DF.Literal["Read", "Unread"] + communication: DF.Data | None + email_account: DF.Data | None + is_completed: DF.Check + uid: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/email_flag_queue/test_email_flag_queue.py b/xhiveframework/email/doctype/email_flag_queue/test_email_flag_queue.py new file mode 100644 index 0000000..ea5939d --- /dev/null +++ b/xhiveframework/email/doctype/email_flag_queue/test_email_flag_queue.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Email Flag Queue') + + +class TestEmailFlagQueue(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/email_group/__init__.py b/xhiveframework/email/doctype/email_group/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_group/email_group.js b/xhiveframework/email/doctype/email_group/email_group.js new file mode 100644 index 0000000..208da0b --- /dev/null +++ b/xhiveframework/email/doctype/email_group/email_group.js @@ -0,0 +1,97 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Group", { + refresh: function (frm) { + if (!frm.is_new()) { + frm.add_custom_button( + __("Import Subscribers"), + function () { + xhiveframework.prompt( + { + fieldtype: "Select", + options: frm.doc.__onload.import_types, + label: __("Import Email From"), + fieldname: "doctype", + reqd: 1, + }, + function (data) { + xhiveframework.call({ + method: "xhiveframework.email.doctype.email_group.email_group.import_from", + args: { + name: frm.doc.name, + doctype: data.doctype, + }, + callback: function (r) { + frm.set_value("total_subscribers", r.message); + }, + }); + }, + __("Import Subscribers"), + __("Import") + ); + }, + __("Action") + ); + + frm.add_custom_button( + __("Add Subscribers"), + function () { + xhiveframework.prompt( + { + fieldtype: "Text", + label: __("Email Addresses"), + fieldname: "email_list", + reqd: 1, + }, + function (data) { + xhiveframework.call({ + method: "xhiveframework.email.doctype.email_group.email_group.add_subscribers", + args: { + name: frm.doc.name, + email_list: data.email_list, + }, + callback: function (r) { + frm.set_value("total_subscribers", r.message); + }, + }); + }, + __("Add Subscribers"), + __("Add") + ); + }, + __("Action") + ); + + frm.add_custom_button( + __("New Newsletter"), + function () { + xhiveframework.route_options = { email_group: frm.doc.name }; + xhiveframework.new_doc("Newsletter"); + }, + __("Action") + ); + } + + frm.trigger("preview_welcome_url"); + }, + welcome_url(frm) { + frm.trigger("preview_welcome_url"); + }, + add_query_parameters: function (frm) { + frm.trigger("preview_welcome_url"); + }, + preview_welcome_url: function (frm) { + if (frm.doc.add_query_parameters && frm.doc.welcome_url) { + frm.call("preview_welcome_url", { email: "mail@example.org" }).then((r) => { + frm.set_df_property( + "add_query_parameters", + "description", + `${__("Preview:")} ${r.message}` + ); + }); + } else { + frm.set_df_property("add_query_parameters", "description", ""); + } + }, +}); diff --git a/xhiveframework/email/doctype/email_group/email_group.json b/xhiveframework/email/doctype/email_group/email_group.json new file mode 100644 index 0000000..1e90bea --- /dev/null +++ b/xhiveframework/email/doctype/email_group/email_group.json @@ -0,0 +1,108 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "autoname": "field:title", + "creation": "2015-03-18 06:08:32.729800", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "title", + "column_break_oyyj", + "total_subscribers", + "sign_up_and_confirmation_section", + "confirmation_email_template", + "welcome_email_template", + "welcome_url", + "add_query_parameters" + ], + "fields": [ + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Title", + "no_copy": 1, + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "fieldname": "total_subscribers", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Total Subscribers", + "read_only": 1 + }, + { + "fieldname": "confirmation_email_template", + "fieldtype": "Link", + "label": "Confirmation Email Template", + "options": "Email Template" + }, + { + "fieldname": "welcome_email_template", + "fieldtype": "Link", + "label": "Welcome Email Template", + "options": "Email Template" + }, + { + "fieldname": "column_break_oyyj", + "fieldtype": "Column Break" + }, + { + "fieldname": "sign_up_and_confirmation_section", + "fieldtype": "Section Break", + "label": "Sign Up and Confirmation" + }, + { + "description": "Redirect to this URL after successful confirmation.", + "fieldname": "welcome_url", + "fieldtype": "Data", + "label": "Welcome URL", + "options": "URL" + }, + { + "default": "0", + "depends_on": "welcome_url", + "fieldname": "add_query_parameters", + "fieldtype": "Check", + "label": "Add Query Parameters" + } + ], + "index_web_pages_for_search": 1, + "links": [ + { + "group": "Members", + "link_doctype": "Email Group Member", + "link_fieldname": "email_group" + } + ], + "modified": "2023-11-24 18:35:17.268492", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Group", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Newsletter Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "show_name_in_global_search": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_group/email_group.py b/xhiveframework/email/doctype/email_group/email_group.py new file mode 100755 index 0000000..d8ba044 --- /dev/null +++ b/xhiveframework/email/doctype/email_group/email_group.py @@ -0,0 +1,161 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import contextlib + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import parse_addr, validate_email_address + + +class EmailGroup(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + add_query_parameters: DF.Check + confirmation_email_template: DF.Link | None + title: DF.Data + total_subscribers: DF.Int + welcome_email_template: DF.Link | None + welcome_url: DF.Data | None + + # end: auto-generated types + def onload(self): + singles = [d.name for d in xhiveframework.get_all("DocType", "name", {"issingle": 1})] + self.get("__onload").import_types = [ + {"value": d.parent, "label": f"{d.parent} ({d.label})"} + for d in xhiveframework.get_all("DocField", ("parent", "label"), {"options": "Email"}) + if d.parent not in singles + ] + + def import_from(self, doctype): + """Extract Email Addresses from given doctype and add them to the current list""" + meta = xhiveframework.get_meta(doctype) + email_field = next( + d.fieldname + for d in meta.fields + if d.fieldtype in ("Data", "Small Text", "Text", "Code") and d.options == "Email" + ) + unsubscribed_field = "unsubscribed" if meta.get_field("unsubscribed") else None + added = 0 + + for user in xhiveframework.get_all(doctype, [email_field, unsubscribed_field or "name"]): + with contextlib.suppress(xhiveframework.UniqueValidationError, xhiveframework.InvalidEmailAddressError): + email = parse_addr(user.get(email_field))[1] if user.get(email_field) else None + if email: + xhiveframework.get_doc( + { + "doctype": "Email Group Member", + "email_group": self.name, + "email": email, + "unsubscribed": user.get(unsubscribed_field) if unsubscribed_field else 0, + } + ).insert(ignore_permissions=True) + added += 1 + + xhiveframework.msgprint(_("{0} subscribers added").format(added)) + + return self.update_total_subscribers() + + def update_total_subscribers(self): + self.total_subscribers = self.get_total_subscribers() + self.db_update() + return self.total_subscribers + + def get_total_subscribers(self): + return xhiveframework.db.sql( + """select count(*) from `tabEmail Group Member` + where email_group=%s""", + self.name, + )[0][0] + + @xhiveframework.whitelist() + def preview_welcome_url(self, email: str | None = None) -> str | None: + """Get Welcome URL for the email group.""" + return self.get_welcome_url(email) + + def get_welcome_url(self, email: str | None = None) -> str | None: + """Get Welcome URL for the email group.""" + if not self.welcome_url: + return None + + return ( + add_query_params(self.welcome_url, {"email": email, "email_group": self.name}) + if self.add_query_parameters + else self.welcome_url + ) + + def on_trash(self): + for d in xhiveframework.get_all("Email Group Member", "name", {"email_group": self.name}): + xhiveframework.delete_doc("Email Group Member", d.name) + + +@xhiveframework.whitelist() +def import_from(name, doctype): + nlist = xhiveframework.get_doc("Email Group", name) + if nlist.has_permission("write"): + return nlist.import_from(doctype) + + +@xhiveframework.whitelist() +def add_subscribers(name, email_list): + if not isinstance(email_list, list | tuple): + email_list = email_list.replace(",", "\n").split("\n") + + template = xhiveframework.db.get_value("Email Group", name, "welcome_email_template") + welcome_email = xhiveframework.get_doc("Email Template", template) if template else None + + count = 0 + for email in email_list: + email = email.strip() + parsed_email = validate_email_address(email, False) + + if parsed_email: + if not xhiveframework.db.get_value("Email Group Member", {"email_group": name, "email": parsed_email}): + xhiveframework.get_doc( + {"doctype": "Email Group Member", "email_group": name, "email": parsed_email} + ).insert(ignore_permissions=xhiveframework.flags.ignore_permissions) + + send_welcome_email(welcome_email, parsed_email, name) + + count += 1 + else: + pass + else: + xhiveframework.msgprint(_("{0} is not a valid Email Address").format(email)) + + xhiveframework.msgprint(_("{0} subscribers added").format(count)) + + return xhiveframework.get_doc("Email Group", name).update_total_subscribers() + + +def send_welcome_email(welcome_email, email, email_group): + """Send welcome email for the subscribers of a given email group.""" + if not welcome_email: + return + + args = dict(email=email, email_group=email_group) + message = xhiveframework.render_template(welcome_email.response_, args) + xhiveframework.sendmail(email, subject=welcome_email.subject, message=message) + + +def add_query_params(url: str, params: dict) -> str: + from urllib.parse import urlencode, urlparse, urlunparse + + if not params: + return url + + query_string = urlencode(params) + parsed = list(urlparse(url)) + if parsed[4]: + parsed[4] += f"&{query_string}" + else: + parsed[4] = query_string + + return urlunparse(parsed) diff --git a/xhiveframework/email/doctype/email_group/test_email_group.py b/xhiveframework/email/doctype/email_group/test_email_group.py new file mode 100644 index 0000000..5f6cf19 --- /dev/null +++ b/xhiveframework/email/doctype/email_group/test_email_group.py @@ -0,0 +1,32 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import validate_url + +# test_records = xhiveframework.get_test_records('Email Group') + + +class TestEmailGroup(XhiveFrameworkTestCase): + def test_welcome_url(self): + email_group = xhiveframework.new_doc("Email Group") + email_group.title = "Test" + email_group.welcome_url = "http://example.com/welcome?hello=world" + email_group.add_query_parameters = 1 + email_group.insert() + + welcome_url = email_group.get_welcome_url("mail@example.org") + self.assertTrue(validate_url(welcome_url)) + self.assertIn(email_group.welcome_url, welcome_url) + self.assertIn("email_group=Test", welcome_url) + self.assertIn("email=mail%40example.org", welcome_url) + + email_group.add_query_parameters = 0 + welcome_url = email_group.get_welcome_url("mail@example.org") + self.assertTrue(validate_url(welcome_url)) + self.assertIn(email_group.welcome_url, welcome_url) + self.assertNotIn("email_group=Test", welcome_url) + self.assertNotIn("email=mail%40example.org", welcome_url) + + email_group.welcome_url = "" + self.assertEqual(email_group.get_welcome_url(), None) diff --git a/xhiveframework/email/doctype/email_group/test_records.json b/xhiveframework/email/doctype/email_group/test_records.json new file mode 100644 index 0000000..a55b117 --- /dev/null +++ b/xhiveframework/email/doctype/email_group/test_records.json @@ -0,0 +1,6 @@ +[ + { + "doctype": "Email Group", + "title": "_Test Email Group" + } +] diff --git a/xhiveframework/email/doctype/email_group_member/__init__.py b/xhiveframework/email/doctype/email_group_member/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_group_member/email_group_member.js b/xhiveframework/email/doctype/email_group_member/email_group_member.js new file mode 100644 index 0000000..2bb992e --- /dev/null +++ b/xhiveframework/email/doctype/email_group_member/email_group_member.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Group Member", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/email/doctype/email_group_member/email_group_member.json b/xhiveframework/email/doctype/email_group_member/email_group_member.json new file mode 100644 index 0000000..0d68674 --- /dev/null +++ b/xhiveframework/email/doctype/email_group_member/email_group_member.json @@ -0,0 +1,71 @@ +{ + "actions": [], + "allow_import": 1, + "autoname": "hash", + "creation": "2015-03-18 06:15:59.321619", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "email_group", + "email", + "unsubscribed" + ], + "fields": [ + { + "fieldname": "email_group", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Email Group", + "options": "Email Group", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "email", + "fieldtype": "Data", + "in_global_search": 1, + "in_list_view": 1, + "label": "Email", + "options": "Email", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "unsubscribed", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Unsubscribed", + "search_index": 1 + } + ], + "links": [], + "modified": "2023-11-25 16:54:59.828669", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Group Member", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Newsletter Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "email", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_group_member/email_group_member.py b/xhiveframework/email/doctype/email_group_member/email_group_member.py new file mode 100644 index 0000000..f1d9076 --- /dev/null +++ b/xhiveframework/email/doctype/email_group_member/email_group_member.py @@ -0,0 +1,32 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class EmailGroupMember(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email: DF.Data + email_group: DF.Link + unsubscribed: DF.Check + + # end: auto-generated types + def after_delete(self): + email_group = xhiveframework.get_doc("Email Group", self.email_group) + email_group.update_total_subscribers() + + def after_insert(self): + email_group = xhiveframework.get_doc("Email Group", self.email_group) + email_group.update_total_subscribers() + + +def after_doctype_insert(): + xhiveframework.db.add_unique("Email Group Member", ("email_group", "email")) diff --git a/xhiveframework/email/doctype/email_group_member/test_email_group_member.py b/xhiveframework/email/doctype/email_group_member/test_email_group_member.py new file mode 100644 index 0000000..f4a1c40 --- /dev/null +++ b/xhiveframework/email/doctype/email_group_member/test_email_group_member.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Email Group Member') + + +class TestEmailGroupMember(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/email_queue/__init__.py b/xhiveframework/email/doctype/email_queue/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_queue/email_queue.js b/xhiveframework/email/doctype/email_queue/email_queue.js new file mode 100644 index 0000000..ba8d1ee --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/email_queue.js @@ -0,0 +1,34 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Queue", { + refresh: function (frm) { + if (["Not Sent", "Partially Sent"].includes(frm.doc.status)) { + let button = frm.add_custom_button("Send Now", function () { + xhiveframework.call({ + method: "xhiveframework.email.doctype.email_queue.email_queue.send_now", + args: { + name: frm.doc.name, + }, + btn: button, + callback: function () { + frm.reload_doc(); + }, + }); + }); + } else if (frm.doc.status == "Error") { + frm.add_custom_button("Retry Sending", function () { + frm.call({ + method: "retry_sending", + doc: frm.doc, + args: { + name: frm.doc.name, + }, + callback: function () { + frm.reload_doc(); + }, + }); + }); + } + }, +}); diff --git a/xhiveframework/email/doctype/email_queue/email_queue.json b/xhiveframework/email/doctype/email_queue/email_queue.json new file mode 100644 index 0000000..ea1389d --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/email_queue.json @@ -0,0 +1,177 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2012-08-02 15:17:28", + "description": "Email Queue records.", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "sender", + "recipients", + "show_as_cc", + "message", + "status", + "error", + "message_id", + "reference_doctype", + "reference_name", + "communication", + "send_after", + "priority", + "add_unsubscribe_link", + "unsubscribe_param", + "unsubscribe_method", + "expose_recipients", + "attachments", + "retry", + "email_account" + ], + "fields": [ + { + "fieldname": "sender", + "fieldtype": "Data", + "ignore_xss_filter": 1, + "label": "Sender", + "options": "Email" + }, + { + "fieldname": "recipients", + "fieldtype": "Table", + "label": "Recipient", + "options": "Email Queue Recipient" + }, + { + "fieldname": "show_as_cc", + "fieldtype": "Small Text", + "label": "Show as cc" + }, + { + "fieldname": "message", + "fieldtype": "Code", + "label": "Message" + }, + { + "default": "Not Sent", + "fieldname": "status", + "fieldtype": "Select", + "hidden": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Not Sent\nSending\nSent\nPartially Sent\nError" + }, + { + "depends_on": "eval:doc.error", + "fieldname": "error", + "fieldtype": "Code", + "label": "Error" + }, + { + "fieldname": "message_id", + "fieldtype": "Small Text", + "label": "Message ID", + "read_only": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "reference_name", + "fieldtype": "Data", + "label": "Reference DocName", + "read_only": 1, + "search_index": 1 + }, + { + "fieldname": "communication", + "fieldtype": "Link", + "label": "Communication", + "options": "Communication", + "search_index": 1 + }, + { + "fieldname": "send_after", + "fieldtype": "Datetime", + "label": "Send After", + "no_copy": 1, + "read_only": 1 + }, + { + "default": "1", + "fieldname": "priority", + "fieldtype": "Int", + "label": "Priority", + "read_only": 1 + }, + { + "default": "1", + "fieldname": "add_unsubscribe_link", + "fieldtype": "Check", + "label": "Add Unsubscribe Link" + }, + { + "fieldname": "unsubscribe_param", + "fieldtype": "Data", + "label": "Unsubscribe Param", + "read_only": 1 + }, + { + "fieldname": "unsubscribe_method", + "fieldtype": "Data", + "label": "Unsubscribe Method" + }, + { + "fieldname": "expose_recipients", + "fieldtype": "Data", + "label": "Expose Recipients" + }, + { + "fieldname": "attachments", + "fieldtype": "Code", + "label": "Attachments", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "retry", + "fieldtype": "Int", + "label": "Retry", + "read_only": 1 + }, + { + "fieldname": "email_account", + "fieldtype": "Link", + "label": "Email Account", + "options": "Email Account" + } + ], + "icon": "fa fa-envelope", + "idx": 1, + "in_create": 1, + "links": [], + "modified": "2023-06-09 14:31:52.789186", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Queue", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_queue/email_queue.py b/xhiveframework/email/doctype/email_queue/email_queue.py new file mode 100644 index 0000000..45de3e0 --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/email_queue.py @@ -0,0 +1,803 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json +import quopri +import traceback +from contextlib import suppress +from email.parser import Parser +from email.policy import SMTP + +import xhiveframework +from xhiveframework import _, safe_encode, task +from xhiveframework.core.utils import html2text +from xhiveframework.database.database import savepoint +from xhiveframework.email.doctype.email_account.email_account import EmailAccount +from xhiveframework.email.email_body import add_attachment, get_email, get_formatted_html +from xhiveframework.email.queue import get_unsubcribed_url, get_unsubscribe_message +from xhiveframework.email.smtp import SMTPServer +from xhiveframework.model.document import Document +from xhiveframework.query_builder import DocType, Interval +from xhiveframework.query_builder.functions import Now +from xhiveframework.utils import ( + add_days, + cint, + cstr, + get_hook_method, + get_string_between, + get_url, + now, + nowdate, + sbool, + split_emails, +) +from xhiveframework.utils.deprecations import deprecated +from xhiveframework.utils.verified_command import get_signed_params + + +class EmailQueue(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.email.doctype.email_queue_recipient.email_queue_recipient import EmailQueueRecipient + from xhiveframework.types import DF + + add_unsubscribe_link: DF.Check + attachments: DF.Code | None + communication: DF.Link | None + email_account: DF.Link | None + error: DF.Code | None + expose_recipients: DF.Data | None + message: DF.Code | None + message_id: DF.SmallText | None + priority: DF.Int + recipients: DF.Table[EmailQueueRecipient] + reference_doctype: DF.Link | None + reference_name: DF.Data | None + retry: DF.Int + send_after: DF.Datetime | None + sender: DF.Data | None + show_as_cc: DF.SmallText | None + status: DF.Literal["Not Sent", "Sending", "Sent", "Partially Sent", "Error"] + unsubscribe_method: DF.Data | None + unsubscribe_param: DF.Data | None + # end: auto-generated types + DOCTYPE = "Email Queue" + + def set_recipients(self, recipients): + self.set("recipients", []) + for r in recipients: + self.append("recipients", {"recipient": r.strip(), "status": "Not Sent"}) + + def on_trash(self): + self.prevent_email_queue_delete() + + def prevent_email_queue_delete(self): + if xhiveframework.session.user != "Administrator": + xhiveframework.throw(_("Only Administrator can delete Email Queue")) + + def get_duplicate(self, recipients): + values = self.as_dict() + del values["name"] + duplicate = xhiveframework.get_doc(values) + duplicate.set_recipients(recipients) + return duplicate + + @classmethod + def new(cls, doc_data, ignore_permissions=False) -> "EmailQueue": + data = doc_data.copy() + if not data.get("recipients"): + return + + recipients = data.pop("recipients") + doc = xhiveframework.new_doc(cls.DOCTYPE) + doc.update(data) + doc.set_recipients(recipients) + doc.insert(ignore_permissions=ignore_permissions) + return doc + + @classmethod + def find(cls, name) -> "EmailQueue": + return xhiveframework.get_doc(cls.DOCTYPE, name) + + @classmethod + def find_one_by_filters(cls, **kwargs): + name = xhiveframework.db.get_value(cls.DOCTYPE, kwargs) + return cls.find(name) if name else None + + def update_db(self, commit=False, **kwargs): + xhiveframework.db.set_value(self.DOCTYPE, self.name, kwargs) + if commit: + xhiveframework.db.commit() + + def update_status(self, status, commit=False, **kwargs): + self.update_db(status=status, commit=commit, **kwargs) + if self.communication: + communication_doc = xhiveframework.get_doc("Communication", self.communication) + communication_doc.set_delivery_status(commit=commit) + + @property + def cc(self): + return (self.show_as_cc and self.show_as_cc.split(",")) or [] + + @property + def to(self): + return [r.recipient for r in self.recipients if r.recipient not in self.cc] + + @property + def attachments_list(self): + return json.loads(self.attachments) if self.attachments else [] + + def get_email_account(self, raise_error=False): + if self.email_account: + return xhiveframework.get_cached_doc("Email Account", self.email_account) + + return EmailAccount.find_outgoing( + match_by_email=self.sender, match_by_doctype=self.reference_doctype, _raise_error=raise_error + ) + + def is_to_be_sent(self): + return self.status in ["Not Sent", "Partially Sent"] + + def can_send_now(self): + if ( + xhiveframework.are_emails_muted() + or not self.is_to_be_sent() + or cint(xhiveframework.db.get_default("suspend_email_queue")) == 1 + ): + return False + + return True + + def send(self, smtp_server_instance: SMTPServer = None): + """Send emails to recipients.""" + if not self.can_send_now(): + return + + with SendMailContext(self, smtp_server_instance) as ctx: + ctx.fetch_smtp_server() + message = None + for recipient in self.recipients: + if recipient.is_mail_sent(): + continue + + message = ctx.build_message(recipient.recipient) + if method := get_hook_method("override_email_send"): + method(self, self.sender, recipient.recipient, message) + else: + if not xhiveframework.flags.in_test or xhiveframework.flags.testing_email: + ctx.smtp_server.session.sendmail( + from_addr=self.sender, + to_addrs=recipient.recipient, + msg=message.decode("utf-8").encode(), + ) + + ctx.update_recipient_status_to_sent(recipient) + + if xhiveframework.flags.in_test and not xhiveframework.flags.testing_email: + xhiveframework.flags.sent_mail = message + return + + if ctx.email_account_doc.append_emails_to_sent_folder: + ctx.email_account_doc.append_email_to_sent_folder(message) + + @staticmethod + def clear_old_logs(days=30): + """Remove low priority older than 31 days in Outbox or configured in Log Settings. + Note: Used separate query to avoid deadlock + """ + days = days or 31 + email_queue = xhiveframework.qb.DocType("Email Queue") + email_recipient = xhiveframework.qb.DocType("Email Queue Recipient") + + # Delete queue table + ( + xhiveframework.qb.from_(email_queue).delete().where(email_queue.modified < (Now() - Interval(days=days))) + ).run() + + # delete child tables, note that this has potential to leave some orphan + # child table behind if modified time was later than parent doc (rare). + # But it's safe since child table doesn't contain links. + ( + xhiveframework.qb.from_(email_recipient) + .delete() + .where(email_recipient.modified < (Now() - Interval(days=days))) + ).run() + + @xhiveframework.whitelist() + def retry_sending(self): + if self.status == "Error": + self.status = "Not Sent" + self.save(ignore_permissions=True) + + +@task(queue="short") +@deprecated +def send_mail(email_queue_name, smtp_server_instance: SMTPServer = None): + """This is equivalent to EmailQueue.send. + + This provides a way to make sending mail as a background job. + """ + record = EmailQueue.find(email_queue_name) + record.send(smtp_server_instance=smtp_server_instance) + + +class SendMailContext: + def __init__( + self, + queue_doc: Document, + smtp_server_instance: SMTPServer = None, + ): + self.queue_doc: EmailQueue = queue_doc + self.smtp_server: SMTPServer = smtp_server_instance + self.sent_to_atleast_one_recipient = any( + rec.recipient for rec in self.queue_doc.recipients if rec.is_mail_sent() + ) + + def fetch_smtp_server(self): + self.email_account_doc = self.queue_doc.get_email_account(raise_error=True) + if not self.smtp_server: + self.smtp_server = self.email_account_doc.get_smtp_server() + + def __enter__(self): + self.queue_doc.update_status(status="Sending", commit=True) + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type: + update_fields = {"error": "".join(traceback.format_tb(exc_tb))} + if self.queue_doc.retry < get_email_retry_limit(): + update_fields.update( + { + "status": "Partially Sent" if self.sent_to_atleast_one_recipient else "Not Sent", + "retry": self.queue_doc.retry + 1, + } + ) + else: + update_fields.update({"status": "Error"}) + self.notify_failed_email() + else: + update_fields = {"status": "Sent"} + + self.queue_doc.update_status(**update_fields, commit=True) + + @savepoint(catch=Exception) + def notify_failed_email(self): + # Parse the email body to extract the subject + subject = Parser(policy=SMTP).parsestr(self.queue_doc.message)["Subject"] + + # Construct the notification + notification = xhiveframework.new_doc("Notification Log") + notification.for_user = self.queue_doc.owner + notification.set("type", "Alert") + notification.from_user = self.queue_doc.owner + notification.document_type = self.queue_doc.doctype + notification.document_name = self.queue_doc.name + notification.subject = _("Failed to send email with subject:") + f" {subject}" + notification.insert() + + def update_recipient_status_to_sent(self, recipient): + self.sent_to_atleast_one_recipient = True + recipient.update_db(status="Sent", commit=True) + + def get_message_object(self, message): + return Parser(policy=SMTP).parsestr(message) + + def message_placeholder(self, placeholder_key): + # sourcery skip: avoid-builtin-shadow + map = { + "tracker": "", + "unsubscribe_url": "", + "cc": "", + "recipient": "", + } + return map.get(placeholder_key) + + def build_message(self, recipient_email) -> bytes: + """Build message specific to the recipient.""" + message = self.queue_doc.message + + if not message: + return "" + + message = message.replace(self.message_placeholder("tracker"), self.get_tracker_str(recipient_email)) + message = message.replace( + self.message_placeholder("unsubscribe_url"), self.get_unsubscribe_str(recipient_email) + ) + message = message.replace(self.message_placeholder("cc"), self.get_receivers_str()) + message = message.replace( + self.message_placeholder("recipient"), self.get_recipient_str(recipient_email) + ) + message = self.include_attachments(message) + return message + + def get_tracker_str(self, recipient_email) -> str: + tracker_url = "" + if self.queue_doc.get("email_read_tracker_url"): + email_read_tracker_url = self.queue_doc.email_read_tracker_url + params = { + "recipient_email": recipient_email, + "reference_name": self.queue_doc.reference_name, + "reference_doctype": self.queue_doc.reference_doctype, + } + tracker_url = get_url(f"{email_read_tracker_url}?{get_signed_params(params)}") + + elif xhiveframework.conf.use_ssl and self.email_account_doc.track_email_status: + tracker_url = f"{get_url()}/api/method/xhiveframework.core.doctype.communication.email.mark_email_as_seen?name={self.queue_doc.communication}" + + if tracker_url: + tracker_url_html = f'' + return quopri.encodestring(tracker_url_html.encode()).decode() + + return "" + + def get_unsubscribe_str(self, recipient_email: str) -> str: + unsubscribe_url = "" + + if self.queue_doc.add_unsubscribe_link and self.queue_doc.reference_doctype: + unsubscribe_url = get_unsubcribed_url( + reference_doctype=self.queue_doc.reference_doctype, + reference_name=self.queue_doc.reference_name, + email=recipient_email, + unsubscribe_method=self.queue_doc.unsubscribe_method, + unsubscribe_params=self.queue_doc.unsubscribe_param, + ) + + return quopri.encodestring(unsubscribe_url.encode()).decode() + + def get_receivers_str(self): + message = "" + if self.queue_doc.expose_recipients == "footer": + to_str = ", ".join(self.queue_doc.to) + cc_str = ", ".join(self.queue_doc.cc) + message = f"This email was sent to {to_str}" + message = f"{message} and copied to {cc_str}" if cc_str else message + return message + + def get_recipient_str(self, recipient_email): + return recipient_email if self.queue_doc.expose_recipients != "header" else "" + + def include_attachments(self, message): + message_obj = self.get_message_object(message) + attachments = self.queue_doc.attachments_list + + for attachment in attachments: + if attachment.get("fcontent"): + continue + + file_filters = {} + if attachment.get("fid"): + file_filters["name"] = attachment.get("fid") + elif attachment.get("file_url"): + file_filters["file_url"] = attachment.get("file_url") + + if file_filters: + _file = xhiveframework.get_doc("File", file_filters) + fcontent = _file.get_content() + attachment.update({"fname": _file.file_name, "fcontent": fcontent, "parent": message_obj}) + attachment.pop("fid", None) + attachment.pop("file_url", None) + add_attachment(**attachment) + + elif attachment.get("print_format_attachment") == 1: + attachment.pop("print_format_attachment", None) + print_format_file = xhiveframework.attach_print(**attachment) + self._store_file(print_format_file["fname"], print_format_file["fcontent"]) + print_format_file.update({"parent": message_obj}) + add_attachment(**print_format_file) + + return safe_encode(message_obj.as_string()) + + def _store_file(self, file_name, content): + if not xhiveframework.get_system_settings("store_attached_pdf_document"): + return + + file_data = xhiveframework._dict(file_name=file_name, is_private=1) + + # Store on communication if available, else email queue doc + if self.queue_doc.communication: + file_data.attached_to_doctype = "Communication" + file_data.attached_to_name = self.queue_doc.communication + else: + file_data.attached_to_doctype = self.queue_doc.doctype + file_data.attached_to_name = self.queue_doc.name + + if xhiveframework.db.exists("File", file_data): + return + + file = xhiveframework.new_doc("File", **file_data) + file.content = content + file.insert() + + +@xhiveframework.whitelist() +def bulk_retry(queues): + xhiveframework.only_for("System Manager") + + if isinstance(queues, str): + queues = json.loads(queues) + + if not queues: + return + + xhiveframework.msgprint( + _("Updating Email Queue Statuses. The emails will be picked up in the next scheduled run."), + _("Processing..."), + ) + + email_queue = xhiveframework.qb.DocType("Email Queue") + xhiveframework.qb.update(email_queue).set(email_queue.status, "Not Sent").set(email_queue.modified, now()).set( + email_queue.modified_by, xhiveframework.session.user + ).where(email_queue.name.isin(queues) & email_queue.status == "Error").run() + + +@xhiveframework.whitelist() +def send_now(name): + record = EmailQueue.find(name) + if record: + record.check_permission() + record.send() + + +@xhiveframework.whitelist() +def toggle_sending(enable): + xhiveframework.only_for("System Manager") + xhiveframework.db.set_default("suspend_email_queue", 0 if sbool(enable) else 1) + + +def on_doctype_update(): + """Add index in `tabCommunication` for `(reference_doctype, reference_name)`""" + xhiveframework.db.add_index("Email Queue", ("status", "send_after", "priority", "creation"), "index_bulk_flush") + + xhiveframework.db.add_index("Email Queue", ["message_id(140)"]) + + +def get_email_retry_limit(): + return cint(xhiveframework.db.get_system_setting("email_retry_limit")) or 3 + + +class QueueBuilder: + """Builds Email Queue from the given data""" + + def __init__( + self, + recipients=None, + sender=None, + subject=None, + message=None, + text_content=None, + reference_doctype=None, + reference_name=None, + unsubscribe_method=None, + unsubscribe_params=None, + unsubscribe_message=None, + attachments=None, + reply_to=None, + cc=None, + bcc=None, + message_id=None, + in_reply_to=None, + send_after=None, + expose_recipients=None, + send_priority=1, + communication=None, + read_receipt=None, + queue_separately=False, + is_notification=False, + add_unsubscribe_link=1, + inline_images=None, + header=None, + print_letterhead=False, + with_container=False, + email_read_tracker_url=None, + ): + """Add email to sending queue (Email Queue) + + :param recipients: List of recipients. + :param sender: Email sender. + :param subject: Email subject. + :param message: Email message. + :param text_content: Text version of email message. + :param reference_doctype: Reference DocType of caller document. + :param reference_name: Reference name of caller document. + :param send_priority: Priority for Email Queue, default 1. + :param unsubscribe_method: URL method for unsubscribe. Default is `/api/method/xhiveframework.email.queue.unsubscribe`. + :param unsubscribe_params: additional params for unsubscribed links. default are name, doctype, email + :param attachments: Attachments to be sent. + :param reply_to: Reply to be captured here (default inbox) + :param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To. + :param send_after: Send this email after the given datetime. If value is in integer, then `send_after` will be the automatically set to no of days from current date. + :param communication: Communication link to be set in Email Queue record + :param queue_separately: Queue each email separately + :param is_notification: Marks email as notification so will not trigger notifications from system + :param add_unsubscribe_link: Send unsubscribe link in the footer of the Email, default 1. + :param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id + :param header: Append header in email (boolean) + :param with_container: Wraps email inside styled container + :param email_read_tracker_url: A URL for tracking whether an email is read by the recipient. + """ + + self._unsubscribe_method = unsubscribe_method + self._recipients = recipients + self._cc = cc + self._bcc = bcc + self._send_after = send_after + self._sender = sender + self._text_content = text_content + self._message = message + self._add_unsubscribe_link = add_unsubscribe_link + self._unsubscribe_message = unsubscribe_message + self._attachments = attachments + + self._unsubscribed_user_emails = None + self._email_account = None + + self.unsubscribe_params = unsubscribe_params + self.subject = subject + self.reference_doctype = reference_doctype + self.reference_name = reference_name + self.expose_recipients = expose_recipients + self.with_container = with_container + self.header = header + self.reply_to = reply_to + self.message_id = message_id + self.in_reply_to = in_reply_to + self.send_priority = send_priority + self.communication = communication + self.read_receipt = read_receipt + self.queue_separately = queue_separately + self.is_notification = is_notification + self.inline_images = inline_images + self.print_letterhead = print_letterhead + self.email_read_tracker_url = email_read_tracker_url + + @property + def unsubscribe_method(self): + return self._unsubscribe_method or "/api/method/xhiveframework.email.queue.unsubscribe" + + def _get_emails_list(self, emails=None): + emails = split_emails(emails) if isinstance(emails, str) else (emails or []) + return [each for each in set(emails) if each] + + @property + def recipients(self): + return self._get_emails_list(self._recipients) + + @property + def cc(self): + return self._get_emails_list(self._cc) + + @property + def bcc(self): + return self._get_emails_list(self._bcc) + + @property + def send_after(self): + if isinstance(self._send_after, int): + return add_days(nowdate(), self._send_after) + return self._send_after + + @property + def sender(self): + if not self._sender or self._sender == "Administrator": + email_account = self.get_outgoing_email_account() + return email_account.default_sender + return self._sender + + def email_text_content(self): + unsubscribe_msg = self.unsubscribe_message() + unsubscribe_text_message = (unsubscribe_msg and unsubscribe_msg.text) or "" + + if self._text_content: + return self._text_content + unsubscribe_text_message + + try: + text_content = html2text(self._message) + except Exception: + text_content = "See html attachment" + return text_content + unsubscribe_text_message + + def email_html_content(self): + email_account = self.get_outgoing_email_account() + return get_formatted_html( + self.subject, + self._message, + header=self.header, + email_account=email_account, + unsubscribe_link=self.unsubscribe_message(), + with_container=self.with_container, + ) + + def should_include_unsubscribe_link(self): + return ( + self._add_unsubscribe_link == 1 + and self.reference_doctype + and (self._unsubscribe_message or self.reference_doctype == "Newsletter") + ) + + def unsubscribe_message(self): + if self.should_include_unsubscribe_link(): + return get_unsubscribe_message(self._unsubscribe_message, self.expose_recipients) + + def get_outgoing_email_account(self): + if self._email_account: + return self._email_account + + self._email_account = EmailAccount.find_outgoing( + match_by_doctype=self.reference_doctype, match_by_email=self._sender, _raise_error=True + ) + return self._email_account + + def get_unsubscribed_user_emails(self): + if self._unsubscribed_user_emails is not None: + return self._unsubscribed_user_emails + + all_ids = list(set(self.recipients + self.cc)) + + EmailUnsubscribe = DocType("Email Unsubscribe") + + if len(all_ids) > 0: + unsubscribed = ( + xhiveframework.qb.from_(EmailUnsubscribe) + .select(EmailUnsubscribe.email) + .where( + EmailUnsubscribe.email.isin(all_ids) + & ( + ( + (EmailUnsubscribe.reference_doctype == self.reference_doctype) + & (EmailUnsubscribe.reference_name == self.reference_name) + ) + | (EmailUnsubscribe.global_unsubscribe == 1) + ) + ) + .distinct() + ).run(pluck=True) + else: + unsubscribed = None + + self._unsubscribed_user_emails = unsubscribed or [] + return self._unsubscribed_user_emails + + def final_recipients(self): + unsubscribed_emails = self.get_unsubscribed_user_emails() + return [mail_id for mail_id in self.recipients if mail_id not in unsubscribed_emails] + + def final_cc(self): + unsubscribed_emails = self.get_unsubscribed_user_emails() + return [mail_id for mail_id in self.cc if mail_id not in unsubscribed_emails] + + def get_attachments(self): + attachments = [] + if self._attachments: + # store attachments with fid or print format details, to be attached on-demand later + for att in self._attachments: + if att.get("fid") or att.get("file_url"): + attachments.append(att) + elif att.get("print_format_attachment") == 1: + if not att.get("lang", None): + att["lang"] = xhiveframework.local.lang + att["print_letterhead"] = self.print_letterhead + attachments.append(att) + return attachments + + def prepare_email_content(self): + mail = get_email( + recipients=self.final_recipients(), + sender=self.sender, + subject=self.subject, + formatted=self.email_html_content(), + text_content=self.email_text_content(), + attachments=self._attachments, + reply_to=self.reply_to, + cc=self.final_cc(), + bcc=self.bcc, + email_account=self.get_outgoing_email_account(), + expose_recipients=self.expose_recipients, + inline_images=self.inline_images, + header=self.header, + ) + + mail.set_message_id(self.message_id, self.is_notification) + if self.read_receipt: + mail.msg_root["Disposition-Notification-To"] = self.sender + if self.in_reply_to: + mail.set_in_reply_to(self.in_reply_to) + return mail + + def process(self, send_now=False) -> EmailQueue | None: + """Build and return the email queues those are created. + + Sends email incase if it is requested to send now. + """ + final_recipients = self.final_recipients() + queue_separately = (final_recipients and self.queue_separately) or len(final_recipients) > 100 + if not (final_recipients + self.final_cc()): + return [] + + queue_data = self.as_dict(include_recipients=False) + if not queue_data: + return [] + + if not queue_separately: + recipients = list(set(final_recipients + self.final_cc() + self.bcc)) + q = EmailQueue.new({**queue_data, **{"recipients": recipients}}, ignore_permissions=True) + send_now and q.send() + return q + else: + if send_now and len(final_recipients) >= 1000: + # force queueing if there are too many recipients to avoid timeouts + send_now = False + for recipients in xhiveframework.utils.create_batch(final_recipients, 1000): + xhiveframework.enqueue( + self.send_emails, + queue_data=queue_data, + final_recipients=recipients, + job_name=xhiveframework.utils.get_job_name( + "send_bulk_emails_for", self.reference_doctype, self.reference_name + ), + now=xhiveframework.flags.in_test or send_now, + queue="long", + ) + + def send_emails(self, queue_data, final_recipients): + # This is used to bulk send emails from same sender to multiple recipients separately + # This re-uses smtp server instance to minimize the cost of new session creation + smtp_server_instance = None + for r in final_recipients: + recipients = list(set([r, *self.final_cc(), *self.bcc])) + q = EmailQueue.new({**queue_data, **{"recipients": recipients}}, ignore_permissions=True) + if not smtp_server_instance: + email_account = q.get_email_account(raise_error=True) + smtp_server_instance = email_account.get_smtp_server() + + with suppress(Exception): + q.send(smtp_server_instance=smtp_server_instance) + + smtp_server_instance.quit() + + def as_dict(self, include_recipients=True): + email_account = self.get_outgoing_email_account() + email_account_name = email_account and email_account.is_exists_in_db() and email_account.name + + mail = self.prepare_email_content() + try: + mail_to_string = cstr(mail.as_string()) + except xhiveframework.InvalidEmailAddressError: + # bad Email Address - don't add to queue + xhiveframework.log_error( + title="Invalid email address", + message="Invalid email address Sender: {}, Recipients: {}, \nTraceback: {} ".format( + self.sender, ", ".join(self.final_recipients()), traceback.format_exc() + ), + reference_doctype=self.reference_doctype, + reference_name=self.reference_name, + ) + return + + d = { + "priority": self.send_priority, + "attachments": json.dumps(self.get_attachments()), + "message_id": get_string_between("<", mail.msg_root["Message-Id"], ">"), + "message": mail_to_string, + "sender": mail.sender, + "reference_doctype": self.reference_doctype, + "reference_name": self.reference_name, + "add_unsubscribe_link": self._add_unsubscribe_link, + "unsubscribe_method": self.unsubscribe_method, + "unsubscribe_params": self.unsubscribe_params, + "expose_recipients": self.expose_recipients, + "communication": self.communication, + "send_after": self.send_after, + "show_as_cc": ",".join(self.final_cc()), + "show_as_bcc": ",".join(self.bcc), + "email_account": email_account_name or None, + "email_read_tracker_url": self.email_read_tracker_url, + } + + if include_recipients: + d["recipients"] = self.final_recipients() + + return d diff --git a/xhiveframework/email/doctype/email_queue/email_queue_list.js b/xhiveframework/email/doctype/email_queue/email_queue_list.js new file mode 100644 index 0000000..b62498a --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/email_queue_list.js @@ -0,0 +1,60 @@ +xhiveframework.listview_settings["Email Queue"] = { + get_indicator: function (doc) { + var colour = { + Sent: "green", + Sending: "blue", + "Not Sent": "grey", + Error: "red", + Expired: "orange", + }; + return [__(doc.status), colour[doc.status], "status,=," + doc.status]; + }, + refresh: function (listview) { + show_toggle_sending_button(listview); + add_bulk_retry_button_to_actions(listview); + }, + onload: function (list_view) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(list_view.doctype); + }); + }, +}; + +function show_toggle_sending_button(list_view) { + if (!has_common(xhiveframework.user_roles, ["Administrator", "System Manager"])) return; + + const sending_disabled = cint(xhiveframework.sys_defaults.suspend_email_queue); + const label = sending_disabled ? __("Resume Sending") : __("Suspend Sending"); + + list_view.page.add_inner_button(label, async () => { + await xhiveframework.xcall( + "xhiveframework.email.doctype.email_queue.email_queue.toggle_sending", + + // enable if disabled + { enable: sending_disabled } + ); + + // set new value for suspend_email_queue in sys_defaults + xhiveframework.sys_defaults.suspend_email_queue = sending_disabled ? 0 : 1; + + // clear the button and show one with the opposite label + list_view.page.remove_inner_button(label); + show_toggle_sending_button(list_view); + }); +} + +function add_bulk_retry_button_to_actions(list_view) { + list_view.page.add_actions_menu_item(__("Retry Sending"), () => { + xhiveframework.call({ + method: "xhiveframework.email.doctype.email_queue.email_queue.bulk_retry", + args: { + queues: list_view.get_checked_items(true), + }, + callback: (r) => { + if (!r.exc) { + list_view.refresh(); + } + }, + }); + }); +} diff --git a/xhiveframework/email/doctype/email_queue/patches/__init__.py b/xhiveframework/email/doctype/email_queue/patches/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_queue/patches/drop_search_index_on_message_id.py b/xhiveframework/email/doctype/email_queue/patches/drop_search_index_on_message_id.py new file mode 100644 index 0000000..e4abb83 --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/patches/drop_search_index_on_message_id.py @@ -0,0 +1,11 @@ +import xhiveframework + + +def execute(): + """Drop search index on message_id""" + + if xhiveframework.db.get_column_type("Email Queue", "message_id") == "text": + return + + if index := xhiveframework.db.get_column_index("tabEmail Queue", "message_id", unique=False): + xhiveframework.db.sql(f"ALTER TABLE `tabEmail Queue` DROP INDEX `{index.Key_name}`") diff --git a/xhiveframework/email/doctype/email_queue/test_email_queue.py b/xhiveframework/email/doctype/email_queue/test_email_queue.py new file mode 100644 index 0000000..ce88975 --- /dev/null +++ b/xhiveframework/email/doctype/email_queue/test_email_queue.py @@ -0,0 +1,95 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import textwrap + +import xhiveframework +from xhiveframework.email.doctype.email_queue.email_queue import SendMailContext, get_email_retry_limit +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestEmailQueue(XhiveFrameworkTestCase): + def test_email_queue_deletion_based_on_modified_date(self): + from xhiveframework.email.doctype.email_queue.email_queue import EmailQueue + + old_record = xhiveframework.get_doc( + { + "doctype": "Email Queue", + "sender": "Test ", + "show_as_cc": "", + "message": "Test message", + "status": "Sent", + "priority": 1, + "recipients": [ + { + "recipient": "test_auth@test.com", + } + ], + } + ).insert() + + old_record.modified = "2010-01-01 00:00:01" + old_record.recipients[0].modified = old_record.modified + old_record.db_update_all() + + new_record = xhiveframework.copy_doc(old_record) + new_record.insert() + + EmailQueue.clear_old_logs() + + self.assertFalse(xhiveframework.db.exists("Email Queue", old_record.name)) + self.assertFalse(xhiveframework.db.exists("Email Queue Recipient", {"parent": old_record.name})) + + self.assertTrue(xhiveframework.db.exists("Email Queue", new_record.name)) + self.assertTrue(xhiveframework.db.exists("Email Queue Recipient", {"parent": new_record.name})) + + def test_failed_email_notification(self): + subject = xhiveframework.generate_hash() + email_record = xhiveframework.new_doc("Email Queue") + email_record.sender = "Test " + email_record.message = textwrap.dedent( + f"""\ + MIME-Version: 1.0 + Message-Id: {xhiveframework.generate_hash()} + X-Original-From: Test + Subject: {subject} + From: Test + To: + Date: {xhiveframework.utils.now_datetime().strftime('%a, %d %b %Y %H:%M:%S %z')} + Reply-To: test@example.com + X-XhiveFramework-Site: {xhiveframework.local.site} + """ + ) + email_record.status = "Error" + email_record.retry = get_email_retry_limit() + email_record.priority = 1 + email_record.reference_doctype = "User" + email_record.reference_name = "Administrator" + email_record.insert() + + # Simulate an exception so that we get a notification + try: + with SendMailContext(queue_doc=email_record): + raise Exception("Test Exception") + except Exception: + pass + + notification_log = xhiveframework.db.get_value( + "Notification Log", + {"subject": f"Failed to send email with subject: {subject}"}, + ) + self.assertTrue(notification_log) + + def test_perf_reusing_smtp_server(self): + """Ensure that same smtpserver instance is being returned when retrieved multiple times.""" + + self.assertTrue(xhiveframework.new_doc("Email Queue").get_email_account()._from_site_config) + + def get_server(q): + return q.get_email_account().get_smtp_server() + + self.assertIs(get_server(xhiveframework.new_doc("Email Queue")), get_server(xhiveframework.new_doc("Email Queue"))) + + q1 = xhiveframework.new_doc("Email Queue", email_account="_Test Email Account 1") + q2 = xhiveframework.new_doc("Email Queue", email_account="_Test Email Account 1") + self.assertIsNot(get_server(xhiveframework.new_doc("Email Queue")), get_server(q1)) + self.assertIs(get_server(q1), get_server(q2)) diff --git a/xhiveframework/email/doctype/email_queue_recipient/__init__.py b/xhiveframework/email/doctype/email_queue_recipient/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.json b/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.json new file mode 100644 index 0000000..406449e --- /dev/null +++ b/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.json @@ -0,0 +1,46 @@ +{ + "actions": [], + "creation": "2016-12-08 12:01:07.993900", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "recipient", + "status", + "error" + ], + "fields": [ + { + "fieldname": "recipient", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Recipient", + "options": "Email" + }, + { + "default": "Not Sent", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Status", + "options": "\nNot Sent\nSent", + "search_index": 1 + }, + { + "fieldname": "error", + "fieldtype": "Code", + "label": "Error" + } + ], + "istable": 1, + "links": [], + "modified": "2022-09-06 13:38:10.644417", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Queue Recipient", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.py b/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.py new file mode 100644 index 0000000..79ca0e7 --- /dev/null +++ b/xhiveframework/email/doctype/email_queue_recipient/email_queue_recipient.py @@ -0,0 +1,40 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class EmailQueueRecipient(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + error: DF.Code | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + recipient: DF.Data | None + status: DF.Literal["", "Not Sent", "Sent"] + # end: auto-generated types + DOCTYPE = "Email Queue Recipient" + + def is_mail_to_be_sent(self): + return self.status == "Not Sent" + + def is_mail_sent(self): + return self.status == "Sent" + + def update_db(self, commit=False, **kwargs): + xhiveframework.db.set_value(self.DOCTYPE, self.name, kwargs) + if commit: + xhiveframework.db.commit() + + +def on_doctype_update(): + """Index required for log clearing, modified is not indexed on child table by default""" + xhiveframework.db.add_index("Email Queue Recipient", ["modified"]) diff --git a/xhiveframework/email/doctype/email_rule/__init__.py b/xhiveframework/email/doctype/email_rule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_rule/email_rule.js b/xhiveframework/email/doctype/email_rule/email_rule.js new file mode 100644 index 0000000..7fd2f79 --- /dev/null +++ b/xhiveframework/email/doctype/email_rule/email_rule.js @@ -0,0 +1,6 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Rule", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/email/doctype/email_rule/email_rule.json b/xhiveframework/email/doctype/email_rule/email_rule.json new file mode 100644 index 0000000..20e2962 --- /dev/null +++ b/xhiveframework/email/doctype/email_rule/email_rule.json @@ -0,0 +1,49 @@ +{ + "actions": [], + "allow_copy": 1, + "autoname": "field:email_id", + "creation": "2017-03-13 09:20:56.387135", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "email_id", + "is_spam" + ], + "fields": [ + { + "fieldname": "email_id", + "fieldtype": "Data", + "label": "Email ID", + "options": "Email", + "unique": 1 + }, + { + "default": "0", + "fieldname": "is_spam", + "fieldtype": "Check", + "label": "Is Spam" + } + ], + "links": [], + "modified": "2022-08-03 12:20:51.443237", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Rule", + "owner": "Administrator", + "permissions": [ + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_rule/email_rule.py b/xhiveframework/email/doctype/email_rule/email_rule.py new file mode 100644 index 0000000..823f82c --- /dev/null +++ b/xhiveframework/email/doctype/email_rule/email_rule.py @@ -0,0 +1,19 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class EmailRule(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email_id: DF.Data | None + is_spam: DF.Check + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/email_rule/test_email_rule.py b/xhiveframework/email/doctype/email_rule/test_email_rule.py new file mode 100644 index 0000000..27b761e --- /dev/null +++ b/xhiveframework/email/doctype/email_rule/test_email_rule.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestEmailRule(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/email_template/__init__.py b/xhiveframework/email/doctype/email_template/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_template/email_template.js b/xhiveframework/email/doctype/email_template/email_template.js new file mode 100644 index 0000000..b551156 --- /dev/null +++ b/xhiveframework/email/doctype/email_template/email_template.js @@ -0,0 +1,6 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Template", { + refresh: function () {}, +}); diff --git a/xhiveframework/email/doctype/email_template/email_template.json b/xhiveframework/email/doctype/email_template/email_template.json new file mode 100644 index 0000000..6bf08cd --- /dev/null +++ b/xhiveframework/email/doctype/email_template/email_template.json @@ -0,0 +1,88 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2014-06-19 05:20:26.331041", + "doctype": "DocType", + "document_type": "Document", + "engine": "InnoDB", + "field_order": [ + "subject", + "use_html", + "response_html", + "response", + "section_break_4", + "email_reply_help" + ], + "fields": [ + { + "fieldname": "subject", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Subject", + "reqd": 1 + }, + { + "depends_on": "eval:!doc.use_html", + "fieldname": "response", + "fieldtype": "Text Editor", + "in_list_view": 1, + "label": "Response", + "mandatory_depends_on": "eval:!doc.use_html" + }, + { + "fieldname": "section_break_4", + "fieldtype": "Section Break" + }, + { + "fieldname": "email_reply_help", + "fieldtype": "HTML", + "label": "Email Reply Help", + "options": "

      Email Reply Example

      \n\n
      Order Overdue\n\nTransaction {{ name }} has exceeded Due Date. Please take necessary action.\n\nDetails\n\n- Customer: {{ customer }}\n- Amount: {{ grand_total }}\n
      \n\n

      How to get fieldnames

      \n\n

      The fieldnames you can use in your email template are the fields in the document from which you are sending the email. You can find out the fields of any documents via Setup > Customize Form View and selecting the document type (e.g. Sales Invoice)

      \n\n

      Templating

      \n\n

      Templates are compiled using the Jinja Templating Language. To learn more about Jinja, read this documentation.

      \n" + }, + { + "default": "0", + "fieldname": "use_html", + "fieldtype": "Check", + "label": "Use HTML" + }, + { + "depends_on": "eval:doc.use_html", + "fieldname": "response_html", + "fieldtype": "Code", + "label": "Response ", + "options": "Jinja" + } + ], + "icon": "fa fa-comment", + "links": [], + "modified": "2023-12-12 20:01:07.080625", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Template", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "read": 1, + "role": "Desk User" + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_template/email_template.py b/xhiveframework/email/doctype/email_template/email_template.py new file mode 100644 index 0000000..8e14d26 --- /dev/null +++ b/xhiveframework/email/doctype/email_template/email_template.py @@ -0,0 +1,56 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.model.document import Document +from xhiveframework.utils.jinja import validate_template + + +class EmailTemplate(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + response: DF.TextEditor | None + response_html: DF.Code | None + subject: DF.Data + use_html: DF.Check + + # end: auto-generated types + + @property + def response_(self): + return self.response_html if self.use_html else self.response + + def validate(self): + validate_template(self.subject) + validate_template(self.response_) + + def get_formatted_subject(self, doc): + return xhiveframework.render_template(self.subject, doc) + + def get_formatted_response(self, doc): + return xhiveframework.render_template(self.response_, doc) + + def get_formatted_email(self, doc): + if isinstance(doc, str): + doc = json.loads(doc) + + return { + "subject": self.get_formatted_subject(doc), + "message": self.get_formatted_response(doc), + } + + +@xhiveframework.whitelist() +def get_email_template(template_name, doc): + """Returns the processed HTML of a email template with the given doc""" + + email_template = xhiveframework.get_doc("Email Template", template_name) + return email_template.get_formatted_email(doc) diff --git a/xhiveframework/email/doctype/email_template/test_email_template.py b/xhiveframework/email/doctype/email_template/test_email_template.py new file mode 100644 index 0000000..b722d4a --- /dev/null +++ b/xhiveframework/email/doctype/email_template/test_email_template.py @@ -0,0 +1,7 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestEmailTemplate(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/email_unsubscribe/__init__.py b/xhiveframework/email/doctype/email_unsubscribe/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.js b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.js new file mode 100644 index 0000000..8d880f2 --- /dev/null +++ b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Email Unsubscribe", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.json b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.json new file mode 100644 index 0000000..38df531 --- /dev/null +++ b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.json @@ -0,0 +1,70 @@ +{ + "actions": [], + "creation": "2015-03-18 09:41:20.216320", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "email", + "reference_doctype", + "reference_name", + "global_unsubscribe" + ], + "fields": [ + { + "fieldname": "email", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Email", + "reqd": 1, + "search_index": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Reference Document Type", + "options": "DocType" + }, + { + "fieldname": "reference_name", + "fieldtype": "Dynamic Link", + "in_list_view": 1, + "label": "Reference Name", + "options": "reference_doctype" + }, + { + "default": "0", + "fieldname": "global_unsubscribe", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Global Unsubscribe" + } + ], + "links": [], + "modified": "2022-08-03 12:20:51.694626", + "modified_by": "Administrator", + "module": "Email", + "name": "Email Unsubscribe", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.py b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.py new file mode 100644 index 0000000..f2d90de --- /dev/null +++ b/xhiveframework/email/doctype/email_unsubscribe/email_unsubscribe.py @@ -0,0 +1,58 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class EmailUnsubscribe(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email: DF.Data + global_unsubscribe: DF.Check + reference_doctype: DF.Link | None + reference_name: DF.DynamicLink | None + + # end: auto-generated types + def validate(self): + if not self.global_unsubscribe and not (self.reference_doctype and self.reference_name): + xhiveframework.throw(_("Reference DocType and Reference Name are required"), xhiveframework.MandatoryError) + + if not self.global_unsubscribe and xhiveframework.db.get_value(self.doctype, self.name, "global_unsubscribe"): + xhiveframework.throw(_("Delete this record to allow sending to this email address")) + + if self.global_unsubscribe: + if xhiveframework.get_all( + "Email Unsubscribe", + filters={"email": self.email, "global_unsubscribe": 1, "name": ["!=", self.name]}, + ): + xhiveframework.throw(_("{0} already unsubscribed").format(self.email), xhiveframework.DuplicateEntryError) + + else: + if xhiveframework.get_all( + "Email Unsubscribe", + filters={ + "email": self.email, + "reference_doctype": self.reference_doctype, + "reference_name": self.reference_name, + "name": ["!=", self.name], + }, + ): + xhiveframework.throw( + _("{0} already unsubscribed for {1} {2}").format( + self.email, self.reference_doctype, self.reference_name + ), + xhiveframework.DuplicateEntryError, + ) + + def on_update(self): + if self.reference_doctype and self.reference_name: + doc = xhiveframework.get_doc(self.reference_doctype, self.reference_name) + doc.add_comment("Label", _("Left this conversation"), comment_email=self.email) diff --git a/xhiveframework/email/doctype/email_unsubscribe/test_email_unsubscribe.py b/xhiveframework/email/doctype/email_unsubscribe/test_email_unsubscribe.py new file mode 100644 index 0000000..0484bf0 --- /dev/null +++ b/xhiveframework/email/doctype/email_unsubscribe/test_email_unsubscribe.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Email Unsubscribe') + + +class TestEmailUnsubscribe(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/imap_folder/__init__.py b/xhiveframework/email/doctype/imap_folder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/imap_folder/imap_folder.json b/xhiveframework/email/doctype/imap_folder/imap_folder.json new file mode 100644 index 0000000..bab50de --- /dev/null +++ b/xhiveframework/email/doctype/imap_folder/imap_folder.json @@ -0,0 +1,53 @@ +{ + "actions": [], + "creation": "2021-09-21 11:38:13.521979", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "folder_name", + "append_to", + "uidvalidity", + "uidnext" + ], + "fields": [ + { + "fieldname": "folder_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Folder Name", + "reqd": 1 + }, + { + "fieldname": "append_to", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Append To", + "options": "DocType" + }, + { + "fieldname": "uidvalidity", + "fieldtype": "Data", + "hidden": 1, + "label": "UIDVALIDITY" + }, + { + "fieldname": "uidnext", + "fieldtype": "Data", + "hidden": 1, + "label": "UIDNEXT" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-09-21 11:53:00.811236", + "modified_by": "Administrator", + "module": "Email", + "name": "IMAP Folder", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} diff --git a/xhiveframework/email/doctype/imap_folder/imap_folder.py b/xhiveframework/email/doctype/imap_folder/imap_folder.py new file mode 100644 index 0000000..42d8292 --- /dev/null +++ b/xhiveframework/email/doctype/imap_folder/imap_folder.py @@ -0,0 +1,25 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class IMAPFolder(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + append_to: DF.Link | None + folder_name: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + uidnext: DF.Data | None + uidvalidity: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/newsletter/__init__.py b/xhiveframework/email/doctype/newsletter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/newsletter/exceptions.py b/xhiveframework/email/doctype/newsletter/exceptions.py new file mode 100644 index 0000000..5dc291f --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/exceptions.py @@ -0,0 +1,16 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE + +from xhiveframework.exceptions import ValidationError + + +class NewsletterAlreadySentError(ValidationError): + pass + + +class NoRecipientFoundError(ValidationError): + pass + + +class NewsletterNotSavedError(ValidationError): + pass diff --git a/xhiveframework/email/doctype/newsletter/newsletter.js b/xhiveframework/email/doctype/newsletter/newsletter.js new file mode 100644 index 0000000..0bcd347 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/newsletter.js @@ -0,0 +1,229 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// License: GNU General Public License v3. See license.txt + +xhiveframework.ui.form.on("Newsletter", { + refresh(frm) { + let doc = frm.doc; + let can_write = xhiveframework.boot.user.can_write.includes(doc.doctype); + if (!frm.is_new() && !frm.is_dirty() && !doc.email_sent && can_write) { + frm.add_custom_button( + __("Send a test email"), + () => { + frm.events.send_test_email(frm); + }, + __("Preview") + ); + + frm.add_custom_button( + __("Check broken links"), + () => { + frm.dashboard.set_headline(__("Checking broken links...")); + frm.call("find_broken_links").then((r) => { + frm.dashboard.set_headline(""); + let links = r.message; + if (links && links.length) { + let html = + "
        " + + links.map((link) => `
      • ${link}
      • `).join("") + + "
      "; + frm.dashboard.set_headline( + __("Following links are broken in the email content: {0}", [html]) + ); + } else { + frm.dashboard.set_headline( + __("No broken links found in the email content") + ); + setTimeout(() => { + frm.dashboard.set_headline(""); + }, 3000); + } + }); + }, + __("Preview") + ); + + frm.add_custom_button( + __("Send now"), + () => { + if (frm.doc.schedule_send) { + xhiveframework.confirm( + __( + "This newsletter was scheduled to send on a later date. Are you sure you want to send it now?" + ), + function () { + frm.events.send_emails(frm); + } + ); + return; + } + xhiveframework.confirm( + __("Are you sure you want to send this newsletter now?"), + () => { + frm.events.send_emails(frm); + } + ); + }, + __("Send") + ); + + frm.add_custom_button( + __("Schedule sending"), + () => { + frm.events.schedule_send_dialog(frm); + }, + __("Send") + ); + } + + frm.events.update_sending_status(frm); + + if (frm.is_new() && !doc.sender_email) { + let { fullname, email } = xhiveframework.user_info(doc.owner); + frm.set_value("sender_email", email); + frm.set_value("sender_name", fullname); + } + + frm.trigger("update_schedule_message"); + }, + + send_emails(frm) { + xhiveframework.dom.freeze(__("Queuing emails...")); + frm.call("send_emails").then(() => { + frm.refresh(); + xhiveframework.dom.unfreeze(); + xhiveframework.show_alert( + __("Queued {0} emails", [xhiveframework.utils.shorten_number(frm.doc.total_recipients)]) + ); + }); + }, + + schedule_send_dialog(frm) { + let hours = xhiveframework.utils.range(24); + let time_slots = hours.map((hour) => { + return `${(hour + "").padStart(2, "0")}:00`; + }); + let d = new xhiveframework.ui.Dialog({ + title: __("Schedule Newsletter"), + fields: [ + { + label: __("Date"), + fieldname: "date", + fieldtype: "Date", + options: { + minDate: new Date(), + }, + reqd: true, + }, + { + label: __("Time"), + fieldname: "time", + fieldtype: "Select", + options: time_slots, + reqd: true, + }, + ], + primary_action_label: __("Schedule"), + primary_action({ date, time }) { + frm.set_value("schedule_sending", 1); + frm.set_value("schedule_send", `${date} ${time}:00`); + d.hide(); + frm.save(); + }, + secondary_action_label: __("Cancel Scheduling"), + secondary_action() { + frm.set_value("schedule_sending", 0); + frm.set_value("schedule_send", ""); + d.hide(); + frm.save(); + }, + }); + if (frm.doc.schedule_sending) { + let parts = frm.doc.schedule_send.split(" "); + if (parts.length === 2) { + let [date, time] = parts; + d.set_value("date", date); + d.set_value("time", time.slice(0, 5)); + } + } + d.show(); + }, + + send_test_email(frm) { + let d = new xhiveframework.ui.Dialog({ + title: __("Send Test Email"), + fields: [ + { + label: __("Email"), + fieldname: "email", + fieldtype: "Data", + options: "Email", + }, + ], + primary_action_label: __("Send"), + primary_action({ email }) { + d.get_primary_btn().text(__("Sending...")).prop("disabled", true); + frm.call("send_test_email", { email }).then(() => { + d.get_primary_btn().text(__("Send again")).prop("disabled", false); + }); + }, + }); + d.show(); + }, + + async update_sending_status(frm) { + if (frm.doc.email_sent && frm.$wrapper.is(":visible") && !frm.waiting_for_request) { + frm.waiting_for_request = true; + let res = await frm.call("get_sending_status"); + frm.waiting_for_request = false; + let stats = res.message; + stats && frm.events.update_sending_progress(frm, stats); + if ( + stats.sent + stats.error >= frm.doc.total_recipients || + (!stats.total && !stats.emails_queued) + ) { + frm.sending_status && clearInterval(frm.sending_status); + frm.sending_status = null; + return; + } + } + + if (frm.sending_status) return; + frm.sending_status = setInterval(() => frm.events.update_sending_status(frm), 5000); + }, + + update_sending_progress(frm, stats) { + if (stats.sent + stats.error >= frm.doc.total_recipients || !frm.doc.email_sent) { + frm.doc.email_sent && frm.page.set_indicator(__("Sent"), "green"); + frm.dashboard.hide_progress(); + return; + } + if (stats.total) { + frm.page.set_indicator(__("Sending"), "blue"); + frm.dashboard.show_progress( + __("Sending emails"), + (stats.sent * 100) / frm.doc.total_recipients, + __("{0} of {1} sent", [stats.sent, frm.doc.total_recipients]) + ); + } else if (stats.emails_queued) { + frm.page.set_indicator(__("Queued"), "blue"); + } + }, + + on_hide(frm) { + if (frm.sending_status) { + clearInterval(frm.sending_status); + frm.sending_status = null; + } + }, + + update_schedule_message(frm) { + if (!frm.doc.email_sent && frm.doc.schedule_send) { + let datetime = xhiveframework.datetime.global_date_format(frm.doc.schedule_send); + frm.dashboard.set_headline_alert( + __("This newsletter is scheduled to be sent on {0}", [datetime.bold()]) + ); + } else { + frm.dashboard.clear_headline(); + } + }, +}); diff --git a/xhiveframework/email/doctype/newsletter/newsletter.json b/xhiveframework/email/doctype/newsletter/newsletter.json new file mode 100644 index 0000000..d3a43f8 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/newsletter.json @@ -0,0 +1,280 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2013-01-10 16:34:31", + "description": "Create and send emails to a specific group of subscribers periodically.", + "doctype": "DocType", + "document_type": "Other", + "engine": "InnoDB", + "field_order": [ + "status_section", + "email_sent_at", + "column_break_3", + "total_recipients", + "column_break_12", + "total_views", + "email_sent", + "from_section", + "sender_name", + "column_break_5", + "sender_email", + "column_break_7", + "send_from", + "recipients", + "email_group", + "subject_section", + "subject", + "newsletter_content", + "content_type", + "message", + "message_md", + "message_html", + "campaign", + "attachments", + "send_unsubscribe_link", + "send_webview_link", + "schedule_settings_section", + "scheduled_to_send", + "schedule_sending", + "schedule_send", + "publish_as_a_web_page_section", + "published", + "route" + ], + "fields": [ + { + "fieldname": "email_group", + "fieldtype": "Table", + "in_standard_filter": 1, + "label": "Audience", + "options": "Newsletter Email Group", + "reqd": 1 + }, + { + "fieldname": "send_from", + "fieldtype": "Data", + "ignore_xss_filter": 1, + "label": "Sender", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "email_sent", + "fieldtype": "Check", + "hidden": 1, + "label": "Email Sent", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "newsletter_content", + "fieldtype": "Section Break", + "label": "Content" + }, + { + "fieldname": "subject", + "fieldtype": "Small Text", + "in_global_search": 1, + "in_list_view": 1, + "label": "Subject", + "reqd": 1 + }, + { + "depends_on": "eval: doc.content_type === 'Rich Text'", + "fieldname": "message", + "fieldtype": "Text Editor", + "in_list_view": 1, + "label": "Message", + "mandatory_depends_on": "eval: doc.content_type === 'Rich Text'" + }, + { + "default": "1", + "fieldname": "send_unsubscribe_link", + "fieldtype": "Check", + "label": "Send Unsubscribe Link" + }, + { + "default": "0", + "fieldname": "published", + "fieldtype": "Check", + "label": "Published" + }, + { + "depends_on": "published", + "fieldname": "route", + "fieldtype": "Data", + "label": "Route", + "read_only": 1 + }, + { + "fieldname": "scheduled_to_send", + "fieldtype": "Int", + "hidden": 1, + "label": "Scheduled To Send" + }, + { + "fieldname": "recipients", + "fieldtype": "Section Break", + "label": "To" + }, + { + "depends_on": "eval: doc.schedule_sending", + "fieldname": "schedule_send", + "fieldtype": "Datetime", + "label": "Send Email At", + "read_only": 1, + "read_only_depends_on": "eval: doc.email_sent" + }, + { + "fieldname": "content_type", + "fieldtype": "Select", + "label": "Content Type", + "options": "Rich Text\nMarkdown\nHTML" + }, + { + "depends_on": "eval:doc.content_type === 'Markdown'", + "fieldname": "message_md", + "fieldtype": "Markdown Editor", + "label": "Message (Markdown)", + "mandatory_depends_on": "eval:doc.content_type === 'Markdown'" + }, + { + "depends_on": "eval:doc.content_type === 'HTML'", + "fieldname": "message_html", + "fieldtype": "HTML Editor", + "label": "Message (HTML)", + "mandatory_depends_on": "eval:doc.content_type === 'HTML'" + }, + { + "default": "0", + "fieldname": "schedule_sending", + "fieldtype": "Check", + "label": "Schedule sending at a later time", + "read_only_depends_on": "eval: doc.email_sent" + }, + { + "default": "0", + "fieldname": "send_webview_link", + "fieldtype": "Check", + "label": "Send Web View Link" + }, + { + "fieldname": "from_section", + "fieldtype": "Section Break", + "label": "From" + }, + { + "fieldname": "sender_name", + "fieldtype": "Data", + "label": "Sender Name" + }, + { + "fieldname": "sender_email", + "fieldtype": "Data", + "label": "Sender Email", + "options": "Email", + "reqd": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_7", + "fieldtype": "Column Break" + }, + { + "fieldname": "subject_section", + "fieldtype": "Section Break", + "label": "Subject" + }, + { + "fieldname": "publish_as_a_web_page_section", + "fieldtype": "Section Break", + "label": "Publish as a web page" + }, + { + "depends_on": "schedule_sending", + "fieldname": "schedule_settings_section", + "fieldtype": "Section Break", + "label": "Scheduled Sending" + }, + { + "fieldname": "attachments", + "fieldtype": "Table", + "label": "Attachments", + "options": "Newsletter Attachment" + }, + { + "fieldname": "email_sent_at", + "fieldtype": "Datetime", + "label": "Email Sent At", + "read_only": 1 + }, + { + "fieldname": "total_recipients", + "fieldtype": "Int", + "label": "Total Recipients", + "read_only": 1 + }, + { + "depends_on": "email_sent", + "fieldname": "status_section", + "fieldtype": "Section Break", + "label": "Status" + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "total_views", + "fieldtype": "Int", + "label": "Total Views", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "campaign", + "fieldtype": "Link", + "label": "Campaign", + "options": "Marketing Campaign" + } + ], + "has_web_view": 1, + "icon": "fa fa-envelope", + "idx": 1, + "index_web_pages_for_search": 1, + "is_published_field": "published", + "links": [], + "modified": "2024-01-30 14:05:50.645802", + "modified_by": "Administrator", + "module": "Email", + "name": "Newsletter", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Newsletter Manager", + "share": 1, + "write": 1 + } + ], + "route": "newsletters", + "sort_field": "modified", + "sort_order": "ASC", + "states": [], + "title_field": "subject", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/newsletter/newsletter.py b/xhiveframework/email/doctype/newsletter/newsletter.py new file mode 100644 index 0000000..40c90ad --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/newsletter.py @@ -0,0 +1,449 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE + + +import xhiveframework +import xhiveframework.utils +from xhiveframework import _ +from xhiveframework.email.doctype.email_group.email_group import add_subscribers +from xhiveframework.rate_limiter import rate_limit +from xhiveframework.utils.safe_exec import is_job_queued +from xhiveframework.utils.verified_command import get_signed_params, verify_request +from xhiveframework.website.website_generator import WebsiteGenerator + +from .exceptions import NewsletterAlreadySentError, NewsletterNotSavedError, NoRecipientFoundError + + +class Newsletter(WebsiteGenerator): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.email.doctype.newsletter_attachment.newsletter_attachment import NewsletterAttachment + from xhiveframework.email.doctype.newsletter_email_group.newsletter_email_group import ( + NewsletterEmailGroup, + ) + from xhiveframework.types import DF + + attachments: DF.Table[NewsletterAttachment] + campaign: DF.Link | None + content_type: DF.Literal["Rich Text", "Markdown", "HTML"] + email_group: DF.Table[NewsletterEmailGroup] + email_sent: DF.Check + email_sent_at: DF.Datetime | None + message: DF.TextEditor | None + message_html: DF.HTMLEditor | None + message_md: DF.MarkdownEditor | None + published: DF.Check + route: DF.Data | None + schedule_send: DF.Datetime | None + schedule_sending: DF.Check + scheduled_to_send: DF.Int + send_from: DF.Data | None + send_unsubscribe_link: DF.Check + send_webview_link: DF.Check + sender_email: DF.Data + sender_name: DF.Data | None + subject: DF.SmallText + total_recipients: DF.Int + total_views: DF.Int + # end: auto-generated types + + def validate(self): + self.route = f"newsletters/{self.name}" + self.validate_sender_address() + self.validate_publishing() + self.validate_scheduling_date() + + @property + def newsletter_recipients(self) -> list[str]: + if getattr(self, "_recipients", None) is None: + self._recipients = self.get_recipients() + return self._recipients + + @xhiveframework.whitelist() + def get_sending_status(self): + count_by_status = xhiveframework.get_all( + "Email Queue", + filters={"reference_doctype": self.doctype, "reference_name": self.name}, + fields=["status", "count(name) as count"], + group_by="status", + order_by="status", + ) + sent = 0 + error = 0 + total = 0 + for row in count_by_status: + if row.status == "Sent": + sent = row.count + elif row.status == "Error": + error = row.count + total += row.count + emails_queued = is_job_queued( + job_name=xhiveframework.utils.get_job_name("send_bulk_emails_for", self.doctype, self.name), + queue="long", + ) + return {"sent": sent, "error": error, "total": total, "emails_queued": emails_queued} + + @xhiveframework.whitelist() + def send_test_email(self, email): + test_emails = xhiveframework.utils.validate_email_address(email, throw=True) + self.send_newsletter(emails=test_emails, test_email=True) + xhiveframework.msgprint(_("Test email sent to {0}").format(email), alert=True) + + @xhiveframework.whitelist() + def find_broken_links(self): + import requests + from bs4 import BeautifulSoup + + html = self.get_message() + soup = BeautifulSoup(html, "html.parser") + links = soup.find_all("a") + images = soup.find_all("img") + broken_links = [] + for el in links + images: + url = el.attrs.get("href") or el.attrs.get("src") + try: + response = requests.head(url, verify=False, timeout=5) + if response.status_code >= 400: + broken_links.append(url) + except Exception: + broken_links.append(url) + return broken_links + + @xhiveframework.whitelist() + def send_emails(self): + """queue sending emails to recipients""" + self.schedule_sending = False + self.schedule_send = None + self.queue_all() + + def validate_send(self): + """Validate if Newsletter can be sent.""" + self.validate_newsletter_status() + self.validate_newsletter_recipients() + + def validate_newsletter_status(self): + if self.email_sent: + xhiveframework.throw(_("Newsletter has already been sent"), exc=NewsletterAlreadySentError) + + if self.get("__islocal"): + xhiveframework.throw(_("Please save the Newsletter before sending"), exc=NewsletterNotSavedError) + + def validate_newsletter_recipients(self): + if not self.newsletter_recipients: + xhiveframework.throw(_("Newsletter should have atleast one recipient"), exc=NoRecipientFoundError) + + def validate_sender_address(self): + """Validate self.send_from is a valid email address or not.""" + if self.sender_email: + xhiveframework.utils.validate_email_address(self.sender_email, throw=True) + self.send_from = ( + f"{self.sender_name} <{self.sender_email}>" if self.sender_name else self.sender_email + ) + + def validate_publishing(self): + if self.send_webview_link and not self.published: + xhiveframework.throw(_("Newsletter must be published to send webview link in email")) + + def validate_scheduling_date(self): + if ( + self.schedule_sending + and xhiveframework.utils.get_datetime(self.schedule_send) < xhiveframework.utils.now_datetime() + ): + xhiveframework.throw(_("Past dates are not allowed for Scheduling.")) + + def get_linked_email_queue(self) -> list[str]: + """Get list of email queue linked to this newsletter.""" + return xhiveframework.get_all( + "Email Queue", + filters={ + "reference_doctype": self.doctype, + "reference_name": self.name, + }, + pluck="name", + ) + + def get_queued_recipients(self) -> list[str]: + """Recipients who have already been queued for receiving the newsletter.""" + return xhiveframework.get_all( + "Email Queue Recipient", + filters={ + "parent": ("in", self.get_linked_email_queue()), + }, + pluck="recipient", + ) + + def get_pending_recipients(self) -> list[str]: + """Get list of pending recipients of the newsletter. These + recipients may not have receive the newsletter in the previous iteration. + """ + + queued_recipients = set(self.get_queued_recipients()) + return [x for x in self.newsletter_recipients if x not in queued_recipients] + + def queue_all(self): + """Queue Newsletter to all the recipients generated from the `Email Group` table""" + self.validate() + self.validate_send() + + recipients = self.get_pending_recipients() + self.send_newsletter(emails=recipients) + + self.email_sent = True + self.email_sent_at = xhiveframework.utils.now() + self.total_recipients = len(recipients) + self.save() + + def get_newsletter_attachments(self) -> list[dict[str, str]]: + """Get list of attachments on current Newsletter""" + return [{"file_url": row.attachment} for row in self.attachments] + + def send_newsletter(self, emails: list[str], test_email: bool = False): + """Trigger email generation for `emails` and add it in Email Queue.""" + attachments = self.get_newsletter_attachments() + sender = self.send_from or xhiveframework.utils.get_formatted_email(self.owner) + args = self.as_dict() + args["message"] = self.get_message(medium="email") + + is_auto_commit_set = bool(xhiveframework.db.auto_commit_on_many_writes) + xhiveframework.db.auto_commit_on_many_writes = not xhiveframework.flags.in_test + + xhiveframework.sendmail( + subject=self.subject, + sender=sender, + recipients=emails, + attachments=attachments, + template="newsletter", + add_unsubscribe_link=self.send_unsubscribe_link, + unsubscribe_method="/unsubscribe", + unsubscribe_params={"name": self.name}, + reference_doctype=self.doctype, + reference_name=self.name, + queue_separately=True, + send_priority=0, + args=args, + email_read_tracker_url=None + if test_email + else "/api/method/xhiveframework.email.doctype.newsletter.newsletter.newsletter_email_read", + ) + + xhiveframework.db.auto_commit_on_many_writes = is_auto_commit_set + + def get_message(self, medium=None) -> str: + message = self.message + if self.content_type == "Markdown": + message = xhiveframework.utils.md_to_html(self.message_md) + if self.content_type == "HTML": + message = self.message_html + + html = xhiveframework.render_template(message, {"doc": self.as_dict()}) + + return self.add_source(html, medium=medium) + + def add_source(self, html: str, medium="None") -> str: + """Add source to the site links in the newsletter content.""" + from bs4 import BeautifulSoup + + soup = BeautifulSoup(html, "html.parser") + + links = soup.find_all("a") + for link in links: + href = link.get("href") + if href and not href.startswith("#"): + if not xhiveframework.utils.is_site_link(href): + continue + new_href = xhiveframework.utils.add_trackers_to_url( + href, source="Newsletter", campaign=self.campaign, medium=medium + ) + link["href"] = new_href + + return str(soup) + + def get_recipients(self) -> list[str]: + """Get recipients from Email Group""" + emails = xhiveframework.get_all( + "Email Group Member", + filters={"unsubscribed": 0, "email_group": ("in", self.get_email_groups())}, + pluck="email", + ) + return list(set(emails)) + + def get_email_groups(self) -> list[str]: + # wondering why the 'or'? i can't figure out why both aren't equivalent - @gavin + return [x.email_group for x in self.email_group] or xhiveframework.get_all( + "Newsletter Email Group", + filters={"parent": self.name, "parenttype": "Newsletter"}, + pluck="email_group", + ) + + def get_attachments(self) -> list[dict[str, str]]: + return xhiveframework.get_all( + "File", + fields=["name", "file_name", "file_url", "is_private"], + filters={ + "attached_to_name": self.name, + "attached_to_doctype": "Newsletter", + "is_private": 0, + }, + ) + + +def confirmed_unsubscribe(email, group): + """unsubscribe the email(user) from the mailing list(email_group)""" + xhiveframework.flags.ignore_permissions = True + doc = xhiveframework.get_doc("Email Group Member", {"email": email, "email_group": group}) + if not doc.unsubscribed: + doc.unsubscribed = 1 + doc.save(ignore_permissions=True) + + +@xhiveframework.whitelist(allow_guest=True) +@rate_limit(limit=10, seconds=60 * 60) +def subscribe(email, email_group=None): + """API endpoint to subscribe an email to a particular email group. Triggers a confirmation email.""" + + if email_group is None: + email_group = get_default_email_group() + + # build subscription confirmation URL + api_endpoint = xhiveframework.utils.get_url( + "/api/method/xhiveframework.email.doctype.newsletter.newsletter.confirm_subscription" + ) + signed_params = get_signed_params({"email": email, "email_group": email_group}) + confirm_subscription_url = f"{api_endpoint}?{signed_params}" + + # fetch custom template if available + email_confirmation_template = xhiveframework.db.get_value( + "Email Group", email_group, "confirmation_email_template" + ) + + # build email and send + if email_confirmation_template: + args = {"email": email, "confirmation_url": confirm_subscription_url, "email_group": email_group} + email_template = xhiveframework.get_doc("Email Template", email_confirmation_template) + email_subject = email_template.subject + content = xhiveframework.render_template(email_template.response, args) + else: + email_subject = _("Confirm Your Email") + translatable_content = ( + _("Thank you for your interest in subscribing to our updates"), + _("Please verify your Email Address"), + confirm_subscription_url, + _("Click here to verify"), + ) + content = """ +

      {}. {}.

      +

      {}

      + """.format(*translatable_content) + + xhiveframework.sendmail( + email, + subject=email_subject, + content=content, + ) + + +@xhiveframework.whitelist(allow_guest=True) +def confirm_subscription(email, email_group=None): + """API endpoint to confirm email subscription. + This endpoint is called when user clicks on the link sent to their mail. + """ + if not verify_request(): + return + + if email_group is None: + email_group = get_default_email_group() + + try: + group = xhiveframework.get_doc("Email Group", email_group) + except xhiveframework.DoesNotExistError: + group = xhiveframework.get_doc({"doctype": "Email Group", "title": email_group}).insert( + ignore_permissions=True + ) + + xhiveframework.flags.ignore_permissions = True + + add_subscribers(email_group, email) + xhiveframework.db.commit() + + welcome_url = group.get_welcome_url(email) + + if welcome_url: + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = welcome_url + else: + xhiveframework.respond_as_web_page( + _("Confirmed"), + _("{0} has been successfully added to the Email Group.").format(email), + indicator_color="green", + ) + + +def get_list_context(context=None): + context.update( + { + "show_search": True, + "no_breadcrumbs": True, + "title": _("Newsletters"), + "filters": {"published": 1}, + "row_template": "email/doctype/newsletter/templates/newsletter_row.html", + } + ) + + +def send_scheduled_email(): + """Send scheduled newsletter to the recipients.""" + scheduled_newsletter = xhiveframework.get_all( + "Newsletter", + filters={ + "schedule_send": ("<=", xhiveframework.utils.now_datetime()), + "email_sent": False, + "schedule_sending": True, + }, + ignore_ifnull=True, + pluck="name", + ) + + for newsletter_name in scheduled_newsletter: + try: + newsletter = xhiveframework.get_doc("Newsletter", newsletter_name) + newsletter.queue_all() + + except Exception: + xhiveframework.db.rollback() + + # wasn't able to send emails :( + xhiveframework.db.set_value("Newsletter", newsletter_name, "email_sent", 0) + newsletter.log_error("Failed to send newsletter") + + if not xhiveframework.flags.in_test: + xhiveframework.db.commit() + + +@xhiveframework.whitelist(allow_guest=True) +def newsletter_email_read(recipient_email=None, reference_doctype=None, reference_name=None): + if not (recipient_email and reference_name): + return + verify_request() + try: + doc = xhiveframework.get_cached_doc("Newsletter", reference_name) + if doc.add_viewed(recipient_email, force=True, unique_views=True): + newsletter = xhiveframework.qb.DocType("Newsletter") + ( + xhiveframework.qb.update(newsletter) + .set(newsletter.total_views, newsletter.total_views + 1) + .where(newsletter.name == doc.name) + ).run() + + except Exception: + doc.log_error(f"Unable to mark as viewed for {recipient_email}") + + finally: + xhiveframework.response.update(xhiveframework.utils.get_imaginary_pixel_response()) + + +def get_default_email_group(): + return _("Website", lang=xhiveframework.db.get_default("language")) diff --git a/xhiveframework/email/doctype/newsletter/newsletter_list.js b/xhiveframework/email/doctype/newsletter/newsletter_list.js new file mode 100644 index 0000000..c0320d7 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/newsletter_list.js @@ -0,0 +1,12 @@ +xhiveframework.listview_settings["Newsletter"] = { + add_fields: ["subject", "email_sent", "schedule_sending"], + get_indicator: function (doc) { + if (doc.email_sent) { + return [__("Sent"), "green", "email_sent,=,Yes"]; + } else if (doc.schedule_sending) { + return [__("Scheduled"), "purple", "email_sent,=,No|schedule_sending,=,Yes"]; + } else { + return [__("Not Sent"), "gray", "email_sent,=,No"]; + } + }, +}; diff --git a/xhiveframework/email/doctype/newsletter/templates/newsletter.html b/xhiveframework/email/doctype/newsletter/templates/newsletter.html new file mode 100644 index 0000000..78ef967 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/templates/newsletter.html @@ -0,0 +1,65 @@ +{% extends "templates/web.html" %} + +{% block title %} {{ doc.subject }} {% endblock %} + +{% block page_content %} + + +
      +
      +
      +

      {{ doc.subject }}

      +

      + {{ xhiveframework.format_date(doc.modified) }} +

      +
      +
      + {{ doc.get_message(medium="web_page") }} +
      +
      + + {% if doc.attachments %} +
      +
      +
      + {{ _("Attachments") }} +
      +
      +
      +
      + {% for attachment in doc.attachments %} +

      + + {{ attachment.attachment }} + +

      + {% endfor %} +
      +
      +
      + {% endif %} + +
      +{% endblock %} diff --git a/xhiveframework/email/doctype/newsletter/templates/newsletter_row.html b/xhiveframework/email/doctype/newsletter/templates/newsletter_row.html new file mode 100644 index 0000000..6e1e134 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/templates/newsletter_row.html @@ -0,0 +1,15 @@ + diff --git a/xhiveframework/email/doctype/newsletter/test_newsletter.py b/xhiveframework/email/doctype/newsletter/test_newsletter.py new file mode 100644 index 0000000..8cd5025 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter/test_newsletter.py @@ -0,0 +1,252 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See LICENSE + +from random import choice +from unittest.mock import MagicMock, PropertyMock, patch + +import xhiveframework +from xhiveframework.email.doctype.newsletter.exceptions import ( + NewsletterAlreadySentError, + NoRecipientFoundError, +) +from xhiveframework.email.doctype.newsletter.newsletter import ( + Newsletter, + confirmed_unsubscribe, + send_scheduled_email, +) +from xhiveframework.email.queue import flush +from xhiveframework.tests.utils import XhiveFrameworkTestCase +from xhiveframework.utils import add_days, getdate + +emails = [ + "test_subscriber1@example.com", + "test_subscriber2@example.com", + "test_subscriber3@example.com", + "test1@example.com", +] +newsletters = [] + + +def get_dotted_path(obj: type) -> str: + klass = obj.__class__ + module = klass.__module__ + if module == "builtins": + return klass.__qualname__ # avoid outputs like 'builtins.str' + return f"{module}.{klass.__qualname__}" + + +class TestNewsletterMixin: + def setUp(self): + xhiveframework.set_user("Administrator") + self.setup_email_group() + + def tearDown(self): + xhiveframework.set_user("Administrator") + for newsletter in newsletters: + xhiveframework.db.delete( + "Email Queue", + { + "reference_doctype": "Newsletter", + "reference_name": newsletter, + }, + ) + xhiveframework.delete_doc("Newsletter", newsletter) + xhiveframework.db.delete("Newsletter Email Group", {"parent": newsletter}) + newsletters.remove(newsletter) + + def setup_email_group(self): + if not xhiveframework.db.exists("Email Group", "_Test Email Group"): + xhiveframework.get_doc({"doctype": "Email Group", "title": "_Test Email Group"}).insert() + + for email in emails: + doctype = "Email Group Member" + email_filters = {"email": email, "email_group": "_Test Email Group"} + + savepoint = "setup_email_group" + xhiveframework.db.savepoint(savepoint) + + try: + xhiveframework.get_doc( + { + "doctype": doctype, + **email_filters, + } + ).insert(ignore_if_duplicate=True) + except Exception: + xhiveframework.db.rollback(save_point=savepoint) + xhiveframework.db.set_value(doctype, email_filters, "unsubscribed", 0) + + xhiveframework.db.release_savepoint(savepoint) + + def send_newsletter(self, published=0, schedule_send=None) -> str | None: + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Email Queue Recipient") + xhiveframework.db.delete("Newsletter") + + newsletter_options = { + "published": published, + "schedule_sending": bool(schedule_send), + "schedule_send": schedule_send, + } + newsletter = self.get_newsletter(**newsletter_options) + + if schedule_send: + send_scheduled_email() + else: + newsletter.send_emails() + return newsletter.name + + return newsletter + + @staticmethod + def get_newsletter(**kwargs) -> "Newsletter": + """Generate and return Newsletter object""" + doctype = "Newsletter" + newsletter_content = { + "subject": "_Test Newsletter", + "sender_name": "Test Sender", + "sender_email": "test_sender@example.com", + "content_type": "Rich Text", + "message": "Testing my news.", + } + similar_newsletters = xhiveframework.get_all(doctype, newsletter_content, pluck="name") + + for similar_newsletter in similar_newsletters: + xhiveframework.delete_doc(doctype, similar_newsletter) + + newsletter = xhiveframework.get_doc({"doctype": doctype, **newsletter_content, **kwargs}) + newsletter.append("email_group", {"email_group": "_Test Email Group"}) + newsletter.save(ignore_permissions=True) + newsletter.reload() + newsletters.append(newsletter.name) + + attached_files = xhiveframework.get_all( + "File", + { + "attached_to_doctype": newsletter.doctype, + "attached_to_name": newsletter.name, + }, + pluck="name", + ) + for file in attached_files: + xhiveframework.delete_doc("File", file) + + return newsletter + + +class TestNewsletter(TestNewsletterMixin, XhiveFrameworkTestCase): + def test_send(self): + self.send_newsletter() + + email_queue_list = [xhiveframework.get_doc("Email Queue", e.name) for e in xhiveframework.get_all("Email Queue")] + self.assertEqual(len(email_queue_list), 4) + + recipients = {e.recipients[0].recipient for e in email_queue_list} + self.assertTrue(set(emails).issubset(recipients)) + + def test_unsubscribe(self): + name = self.send_newsletter() + to_unsubscribe = choice(emails) + group = xhiveframework.get_all("Newsletter Email Group", filters={"parent": name}, fields=["email_group"]) + + flush() + confirmed_unsubscribe(to_unsubscribe, group[0].email_group) + + name = self.send_newsletter() + email_queue_list = [xhiveframework.get_doc("Email Queue", e.name) for e in xhiveframework.get_all("Email Queue")] + self.assertEqual(len(email_queue_list), 3) + recipients = [e.recipients[0].recipient for e in email_queue_list] + + for email in emails: + if email != to_unsubscribe: + self.assertTrue(email in recipients) + + def test_schedule_send(self): + newsletter = self.send_newsletter(schedule_send=add_days(getdate(), 1)) + newsletter.db_set("schedule_send", add_days(getdate(), -1)) # Set date in past + send_scheduled_email() + + email_queue_list = [xhiveframework.get_doc("Email Queue", e.name) for e in xhiveframework.get_all("Email Queue")] + self.assertEqual(len(email_queue_list), 4) + recipients = [e.recipients[0].recipient for e in email_queue_list] + for email in emails: + self.assertTrue(email in recipients) + + def test_newsletter_send_test_email(self): + """Test "Send Test Email" functionality of Newsletter""" + newsletter = self.get_newsletter() + test_email = choice(emails) + newsletter.send_test_email(test_email) + + self.assertFalse(newsletter.email_sent) + newsletter.save = MagicMock() + self.assertFalse(newsletter.save.called) + # check if the test email is in the queue + email_queue = xhiveframework.get_all( + "Email Queue", + filters=[ + ["reference_doctype", "=", "Newsletter"], + ["reference_name", "=", newsletter.name], + ["Email Queue Recipient", "recipient", "=", test_email], + ], + ) + self.assertTrue(email_queue) + + def test_newsletter_status(self): + """Test for Newsletter's stats on onload event""" + newsletter = self.get_newsletter() + newsletter.email_sent = True + result = newsletter.get_sending_status() + self.assertTrue("total" in result) + self.assertTrue("sent" in result) + + def test_already_sent_newsletter(self): + newsletter = self.get_newsletter() + newsletter.send_emails() + + with self.assertRaises(NewsletterAlreadySentError): + newsletter.send_emails() + + def test_newsletter_with_no_recipient(self): + newsletter = self.get_newsletter() + property_path = f"{get_dotted_path(newsletter)}.newsletter_recipients" + + with patch(property_path, new_callable=PropertyMock) as mock_newsletter_recipients: + mock_newsletter_recipients.return_value = [] + with self.assertRaises(NoRecipientFoundError): + newsletter.send_emails() + + def test_send_scheduled_email_error_handling(self): + newsletter = self.get_newsletter(schedule_send=add_days(getdate(), -1)) + job_path = "xhiveframework.email.doctype.newsletter.newsletter.Newsletter.queue_all" + m = MagicMock(side_effect=xhiveframework.OutgoingEmailError) + + with self.assertRaises(xhiveframework.OutgoingEmailError): + with patch(job_path, new_callable=m): + send_scheduled_email() + + newsletter.reload() + self.assertEqual(newsletter.email_sent, 0) + + def test_retry_partially_sent_newsletter(self): + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Email Queue Recipient") + xhiveframework.db.delete("Newsletter") + + newsletter = self.get_newsletter() + newsletter.send_emails() + email_queue_list = [xhiveframework.get_doc("Email Queue", e.name) for e in xhiveframework.get_all("Email Queue")] + self.assertEqual(len(email_queue_list), 4) + + # delete a queue document to emulate partial send + queue_recipient_name = email_queue_list[0].recipients[0].recipient + email_queue_list[0].delete() + newsletter.email_sent = False + + # make sure the pending recipient is only the one which has been deleted + self.assertEqual(newsletter.get_pending_recipients(), [queue_recipient_name]) + + # retry + newsletter.send_emails() + self.assertEqual(xhiveframework.db.count("Email Queue"), 4) + self.assertTrue(newsletter.email_sent) diff --git a/xhiveframework/email/doctype/newsletter_attachment/__init__.py b/xhiveframework/email/doctype/newsletter_attachment/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.json b/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.json new file mode 100644 index 0000000..e2add0e --- /dev/null +++ b/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.json @@ -0,0 +1,31 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2021-12-06 16:37:40.652468", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "attachment" + ], + "fields": [ + { + "fieldname": "attachment", + "fieldtype": "Attach", + "in_list_view": 1, + "label": "Attachment", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2021-12-06 16:37:47.481057", + "modified_by": "Administrator", + "module": "Email", + "name": "Newsletter Attachment", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.py b/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.py new file mode 100644 index 0000000..c82dce2 --- /dev/null +++ b/xhiveframework/email/doctype/newsletter_attachment/newsletter_attachment.py @@ -0,0 +1,22 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +# import xhiveframework +from xhiveframework.model.document import Document + + +class NewsletterAttachment(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + attachment: DF.Attach + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/newsletter_email_group/__init__.py b/xhiveframework/email/doctype/newsletter_email_group/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.json b/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.json new file mode 100644 index 0000000..b8c1afe --- /dev/null +++ b/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.json @@ -0,0 +1,42 @@ +{ + "actions": [], + "creation": "2017-02-26 16:20:52.654136", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "email_group", + "total_subscribers" + ], + "fields": [ + { + "columns": 7, + "fieldname": "email_group", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Email Group", + "options": "Email Group", + "reqd": 1 + }, + { + "columns": 3, + "fetch_from": "email_group.total_subscribers", + "fieldname": "total_subscribers", + "fieldtype": "Read Only", + "in_list_view": 1, + "label": "Total Subscribers" + } + ], + "istable": 1, + "links": [], + "modified": "2021-12-06 20:12:08.420240", + "modified_by": "Administrator", + "module": "Email", + "name": "Newsletter Email Group", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.py b/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.py new file mode 100644 index 0000000..631170e --- /dev/null +++ b/xhiveframework/email/doctype/newsletter_email_group/newsletter_email_group.py @@ -0,0 +1,22 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class NewsletterEmailGroup(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email_group: DF.Link + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + total_subscribers: DF.ReadOnly | None + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/notification/__init__.py b/xhiveframework/email/doctype/notification/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/notification/notification.js b/xhiveframework/email/doctype/notification/notification.js new file mode 100644 index 0000000..93429bb --- /dev/null +++ b/xhiveframework/email/doctype/notification/notification.js @@ -0,0 +1,201 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.notification = { + setup_fieldname_select: function (frm) { + // get the doctype to update fields + if (!frm.doc.document_type) { + return; + } + + xhiveframework.model.with_doctype(frm.doc.document_type, function () { + let get_select_options = function (df, parent_field) { + // Append parent_field name along with fieldname for child table fields + let select_value = parent_field ? df.fieldname + "," + parent_field : df.fieldname; + + return { + value: select_value, + label: df.fieldname + " (" + __(df.label, null, df.parent) + ")", + }; + }; + + let get_date_change_options = function () { + let date_options = $.map(fields, function (d) { + return d.fieldtype == "Date" || d.fieldtype == "Datetime" + ? get_select_options(d) + : null; + }); + // append creation and modified date to Date Change field + return date_options.concat([ + { value: "creation", label: `creation (${__("Created On")})` }, + { value: "modified", label: `modified (${__("Last Modified Date")})` }, + ]); + }; + + let fields = xhiveframework.get_doc("DocType", frm.doc.document_type).fields; + let options = $.map(fields, function (d) { + return xhiveframework.model.no_value_type.includes(d.fieldtype) + ? null + : get_select_options(d); + }); + + // set value changed options + frm.set_df_property("value_changed", "options", [""].concat(options)); + frm.set_df_property("set_property_after_alert", "options", [""].concat(options)); + + // set date changed options + frm.set_df_property("date_changed", "options", get_date_change_options()); + + let receiver_fields = []; + if (frm.doc.channel === "Email") { + receiver_fields = $.map(fields, function (d) { + // Add User and Email fields from child into select dropdown + if (xhiveframework.model.table_fields.includes(d.fieldtype)) { + let child_fields = xhiveframework.get_doc("DocType", d.options).fields; + return $.map(child_fields, function (df) { + return df.options == "Email" || + (df.options == "User" && df.fieldtype == "Link") + ? get_select_options(df, d.fieldname) + : null; + }); + // Add User and Email fields from parent into select dropdown + } else { + return d.options == "Email" || + (d.options == "User" && d.fieldtype == "Link") + ? get_select_options(d) + : null; + } + }); + } else if (["WhatsApp", "SMS"].includes(frm.doc.channel)) { + receiver_fields = $.map(fields, function (d) { + return d.options == "Phone" ? get_select_options(d) : null; + }); + } + + // set email recipient options + frm.fields_dict.recipients.grid.update_docfield_property( + "receiver_by_document_field", + "options", + [""].concat(["owner"]).concat(receiver_fields) + ); + }); + }, + setup_example_message: function (frm) { + let template = ""; + if (frm.doc.channel === "Email") { + template = `
      Message Example
      + +
      <h3>Order Overdue</h3>
      +
      +<p>Transaction {{ doc.name }} has exceeded Due Date. Please take necessary action.</p>
      +
      +<!-- show last comment -->
      +{% if comments %}
      +Last comment: {{ comments[-1].comment }} by {{ comments[-1].by }}
      +{% endif %}
      +
      +<h4>Details</h4>
      +
      +<ul>
      +<li>Customer: {{ doc.customer }}
      +<li>Amount: {{ doc.grand_total }}
      +</ul>
      +
      + `; + } else if (["Slack", "System Notification", "SMS"].includes(frm.doc.channel)) { + template = `
      Message Example
      + +
      *Order Overdue*
      +
      +Transaction {{ doc.name }} has exceeded Due Date. Please take necessary action.
      +
      +
      +{% if comments %}
      +Last comment: {{ comments[-1].comment }} by {{ comments[-1].by }}
      +{% endif %}
      +
      +*Details*
      +
      +• Customer: {{ doc.customer }}
      +• Amount: {{ doc.grand_total }}
      +
      `; + } + if (template) { + frm.set_df_property("message_examples", "options", template); + } + }, +}; + +xhiveframework.ui.form.on("Notification", { + onload: function (frm) { + frm.set_query("document_type", function () { + return { + filters: { + istable: 0, + }, + }; + }); + frm.set_query("print_format", function () { + return { + filters: { + doc_type: frm.doc.document_type, + }, + }; + }); + }, + refresh: function (frm) { + xhiveframework.notification.setup_fieldname_select(frm); + xhiveframework.notification.setup_example_message(frm); + + frm.add_fetch("sender", "email_id", "sender_email"); + frm.set_query("sender", () => { + return { + filters: { + enable_outgoing: 1, + }, + }; + }); + frm.get_field("is_standard").toggle(xhiveframework.boot.developer_mode); + frm.trigger("event"); + }, + document_type: function (frm) { + xhiveframework.notification.setup_fieldname_select(frm); + }, + view_properties: function (frm) { + xhiveframework.route_options = { doc_type: frm.doc.document_type }; + xhiveframework.set_route("Form", "Customize Form"); + }, + event: function (frm) { + if (["Days Before", "Days After"].includes(frm.doc.event)) { + frm.add_custom_button(__("Get Alerts for Today"), function () { + xhiveframework.call({ + method: "xhiveframework.email.doctype.notification.notification.get_documents_for_today", + args: { + notification: frm.doc.name, + }, + callback: function (r) { + if (r.message && r.message.length > 0) { + xhiveframework.msgprint(r.message.toString()); + } else { + xhiveframework.msgprint(__("No alerts for today")); + } + }, + }); + }); + } + }, + channel: function (frm) { + frm.toggle_reqd("recipients", frm.doc.channel == "Email"); + xhiveframework.notification.setup_fieldname_select(frm); + xhiveframework.notification.setup_example_message(frm); + if (frm.doc.channel === "SMS" && frm.doc.__islocal) { + frm.set_df_property( + "channel", + "description", + `To use SMS Channel, initialize SMS Settings.` + ); + } else { + frm.set_df_property("channel", "description", ` `); + } + }, +}); diff --git a/xhiveframework/email/doctype/notification/notification.json b/xhiveframework/email/doctype/notification/notification.json new file mode 100644 index 0000000..8b6900a --- /dev/null +++ b/xhiveframework/email/doctype/notification/notification.json @@ -0,0 +1,306 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2014-07-11 17:18:09.923399", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "enabled", + "column_break_2", + "channel", + "slack_webhook_url", + "filters", + "subject", + "document_type", + "is_standard", + "module", + "col_break_1", + "event", + "method", + "date_changed", + "days_in_advance", + "value_changed", + "sender", + "send_system_notification", + "sender_email", + "section_break_9", + "condition", + "column_break_6", + "html_7", + "property_section", + "set_property_after_alert", + "property_value", + "column_break_5", + "send_to_all_assignees", + "recipients", + "message_sb", + "message", + "message_examples", + "view_properties", + "column_break_25", + "attach_print", + "print_format" + ], + "fields": [ + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, + { + "default": "Email", + "depends_on": "eval: !doc.disable_channel", + "fieldname": "channel", + "fieldtype": "Select", + "label": "Channel", + "options": "Email\nSlack\nSystem Notification\nSMS", + "reqd": 1, + "set_only_once": 1 + }, + { + "depends_on": "eval:doc.channel=='Slack'", + "description": "To use Slack Channel, add a Slack Webhook URL.", + "fieldname": "slack_webhook_url", + "fieldtype": "Link", + "label": "Slack Channel", + "mandatory_depends_on": "eval:doc.channel=='Slack'", + "options": "Slack Webhook URL" + }, + { + "fieldname": "filters", + "fieldtype": "Section Break", + "label": "Filters" + }, + { + "depends_on": "eval: in_list(['Email', 'Slack', 'System Notification'], doc.channel)", + "description": "To add dynamic subject, use jinja tags like\n\n
      {{ doc.name }} Delivered
      ", + "fieldname": "subject", + "fieldtype": "Data", + "ignore_xss_filter": 1, + "in_list_view": 1, + "label": "Subject", + "mandatory_depends_on": "eval: in_list(['Email', 'Slack', 'System Notification'], doc.channel)" + }, + { + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Type", + "options": "DocType", + "reqd": 1, + "search_index": 1 + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "no_copy": 1 + }, + { + "depends_on": "is_standard", + "fieldname": "module", + "fieldtype": "Link", + "in_standard_filter": 1, + "label": "Module", + "options": "Module Def" + }, + { + "fieldname": "col_break_1", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: doc.document_type", + "fieldname": "event", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Send Alert On", + "options": "\nNew\nSave\nSubmit\nCancel\nDays After\nDays Before\nValue Change\nMethod\nCustom", + "reqd": 1, + "search_index": 1 + }, + { + "depends_on": "eval:doc.event=='Method'", + "description": "Trigger on valid methods like \"before_insert\", \"after_update\", etc (will depend on the DocType selected)", + "fieldname": "method", + "fieldtype": "Data", + "label": "Trigger Method" + }, + { + "depends_on": "eval:doc.document_type && (doc.event==\"Days After\" || doc.event==\"Days Before\")", + "description": "Send alert if date matches this field's value", + "fieldname": "date_changed", + "fieldtype": "Select", + "label": "Reference Date" + }, + { + "default": "0", + "depends_on": "eval:doc.document_type && (doc.event==\"Days After\" || doc.event==\"Days Before\")", + "description": "Send days before or after the reference date", + "fieldname": "days_in_advance", + "fieldtype": "Int", + "label": "Days Before or After" + }, + { + "depends_on": "eval:doc.document_type && doc.event==\"Value Change\"", + "description": "Send alert if this field's value changes", + "fieldname": "value_changed", + "fieldtype": "Select", + "label": "Value Changed" + }, + { + "depends_on": "eval: doc.channel == 'Email'", + "fieldname": "sender", + "fieldtype": "Link", + "label": "Sender", + "options": "Email Account" + }, + { + "fieldname": "sender_email", + "fieldtype": "Data", + "label": "Sender Email", + "options": "Email", + "read_only": 1 + }, + { + "fieldname": "section_break_9", + "fieldtype": "Section Break" + }, + { + "description": "Optional: The alert will be sent if this expression is true", + "fieldname": "condition", + "fieldtype": "Code", + "ignore_xss_filter": 1, + "in_list_view": 1, + "label": "Condition" + }, + { + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, + { + "fieldname": "html_7", + "fieldtype": "HTML", + "options": "

      Condition Examples:

      \n
      doc.status==\"Open\"
      doc.due_date==nowdate()
      doc.total > 40000\n
      \n" + }, + { + "collapsible": 1, + "fieldname": "property_section", + "fieldtype": "Section Break", + "label": "Set Property After Alert" + }, + { + "fieldname": "set_property_after_alert", + "fieldtype": "Select", + "label": "Set Property After Alert" + }, + { + "fieldname": "property_value", + "fieldtype": "Data", + "label": "Value To Be Set" + }, + { + "depends_on": "eval:doc.channel !=\"Slack\"", + "fieldname": "column_break_5", + "fieldtype": "Section Break", + "label": "Recipients" + }, + { + "fieldname": "recipients", + "fieldtype": "Table", + "label": "Recipients", + "mandatory_depends_on": "eval:doc.channel!=='Slack' && !doc.send_to_all_assignees", + "options": "Notification Recipient" + }, + { + "fieldname": "message_sb", + "fieldtype": "Section Break", + "label": "Message" + }, + { + "default": "Add your message here", + "fieldname": "message", + "fieldtype": "Code", + "ignore_xss_filter": 1, + "label": "Message" + }, + { + "fieldname": "message_examples", + "fieldtype": "HTML", + "label": "Message Examples", + "options": "
      Message Example
      \n\n
      <h3>Order Overdue</h3>\n\n<p>Transaction {{ doc.name }} has exceeded Due Date. Please take necessary action.</p>\n\n<!-- show last comment -->\n{% if comments %}\nLast comment: {{ comments[-1].comment }} by {{ comments[-1].by }}\n{% endif %}\n\n<h4>Details</h4>\n\n<ul>\n<li>Customer: {{ doc.customer }}\n<li>Amount: {{ doc.grand_total }}\n</ul>\n
      " + }, + { + "fieldname": "view_properties", + "fieldtype": "Button", + "label": "View Properties (via Customize Form)" + }, + { + "collapsible": 1, + "collapsible_depends_on": "attach_print", + "fieldname": "column_break_25", + "fieldtype": "Section Break", + "label": "Print Settings" + }, + { + "default": "0", + "fieldname": "attach_print", + "fieldtype": "Check", + "label": "Attach Print" + }, + { + "depends_on": "attach_print", + "fieldname": "print_format", + "fieldtype": "Link", + "label": "Print Format", + "options": "Print Format" + }, + { + "default": "0", + "depends_on": "eval: doc.channel !== 'System Notification'", + "description": "If enabled, the notification will show up in the notifications dropdown on the top right corner of the navigation bar.", + "fieldname": "send_system_notification", + "fieldtype": "Check", + "label": "Send System Notification" + }, + { + "default": "0", + "depends_on": "eval:doc.channel == 'Email'", + "fieldname": "send_to_all_assignees", + "fieldtype": "Check", + "label": "Send To All Assignees" + } + ], + "icon": "fa fa-envelope", + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-05-04 11:17:11.882314", + "modified_by": "Administrator", + "module": "Email", + "name": "Notification", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "export": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "subject", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/notification/notification.py b/xhiveframework/email/doctype/notification/notification.py new file mode 100644 index 0000000..ff3cb18 --- /dev/null +++ b/xhiveframework/email/doctype/notification/notification.py @@ -0,0 +1,532 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json +import os + +import xhiveframework +from xhiveframework import _ +from xhiveframework.core.doctype.role.role import get_info_based_on_role, get_user_info +from xhiveframework.core.doctype.sms_settings.sms_settings import send_sms +from xhiveframework.desk.doctype.notification_log.notification_log import enqueue_create_notification +from xhiveframework.integrations.doctype.slack_webhook_url.slack_webhook_url import send_slack_message +from xhiveframework.model.document import Document +from xhiveframework.modules.utils import export_module_json, get_doc_module +from xhiveframework.utils import add_to_date, cast, is_html, nowdate, validate_email_address +from xhiveframework.utils.jinja import validate_template +from xhiveframework.utils.safe_exec import get_safe_globals + + +class Notification(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.email.doctype.notification_recipient.notification_recipient import ( + NotificationRecipient, + ) + from xhiveframework.types import DF + + attach_print: DF.Check + channel: DF.Literal["Email", "Slack", "System Notification", "SMS"] + condition: DF.Code | None + date_changed: DF.Literal[None] + days_in_advance: DF.Int + document_type: DF.Link + enabled: DF.Check + event: DF.Literal[ + "", + "New", + "Save", + "Submit", + "Cancel", + "Days After", + "Days Before", + "Value Change", + "Method", + "Custom", + ] + is_standard: DF.Check + message: DF.Code | None + method: DF.Data | None + module: DF.Link | None + print_format: DF.Link | None + property_value: DF.Data | None + recipients: DF.Table[NotificationRecipient] + send_system_notification: DF.Check + send_to_all_assignees: DF.Check + sender: DF.Link | None + sender_email: DF.Data | None + set_property_after_alert: DF.Literal[None] + slack_webhook_url: DF.Link | None + subject: DF.Data | None + value_changed: DF.Literal[None] + # end: auto-generated types + + def onload(self): + """load message""" + if self.is_standard: + self.message = self.get_template() + + def autoname(self): + if not self.name: + self.name = self.subject + + def validate(self): + if self.channel in ("Email", "Slack", "System Notification"): + validate_template(self.subject) + + validate_template(self.message) + + if self.event in ("Days Before", "Days After") and not self.date_changed: + xhiveframework.throw(_("Please specify which date field must be checked")) + + if self.event == "Value Change" and not self.value_changed: + xhiveframework.throw(_("Please specify which value field must be checked")) + + self.validate_forbidden_types() + self.validate_condition() + self.validate_standard() + xhiveframework.cache.hdel("notifications", self.document_type) + + def on_update(self): + xhiveframework.cache.hdel("notifications", self.document_type) + path = export_module_json(self, self.is_standard, self.module) + if path: + # js + if not os.path.exists(path + ".md") and not os.path.exists(path + ".html"): + with open(path + ".md", "w") as f: + f.write(self.message) + + # py + if not os.path.exists(path + ".py"): + with open(path + ".py", "w") as f: + f.write( + """import xhiveframework + +def get_context(context): + # do your magic here + pass +""" + ) + + def validate_standard(self): + if self.is_standard and self.enabled and not xhiveframework.conf.developer_mode: + xhiveframework.throw( + _("Cannot edit Standard Notification. To edit, please disable this and duplicate it") + ) + + def validate_condition(self): + temp_doc = xhiveframework.new_doc(self.document_type) + if self.condition: + try: + xhiveframework.safe_eval(self.condition, None, get_context(temp_doc.as_dict())) + except Exception: + xhiveframework.throw(_("The Condition '{0}' is invalid").format(self.condition)) + + def validate_forbidden_types(self): + forbidden_document_types = ("Email Queue",) + if self.document_type in forbidden_document_types or xhiveframework.get_meta(self.document_type).istable: + # currently notifications don't work on child tables as events are not fired for each record of child table + + xhiveframework.throw(_("Cannot set Notification on Document Type {0}").format(self.document_type)) + + def get_documents_for_today(self): + """get list of documents that will be triggered today""" + docs = [] + + diff_days = self.days_in_advance + if self.event == "Days After": + diff_days = -diff_days + + reference_date = add_to_date(nowdate(), days=diff_days) + reference_date_start = reference_date + " 00:00:00.000000" + reference_date_end = reference_date + " 23:59:59.000000" + + doc_list = xhiveframework.get_all( + self.document_type, + fields="name", + filters=[ + {self.date_changed: (">=", reference_date_start)}, + {self.date_changed: ("<=", reference_date_end)}, + ], + ) + + for d in doc_list: + doc = xhiveframework.get_doc(self.document_type, d.name) + + if self.condition and not xhiveframework.safe_eval(self.condition, None, get_context(doc)): + continue + + docs.append(doc) + + return docs + + def send(self, doc): + """Build recipients and send Notification""" + + context = get_context(doc) + context = {"doc": doc, "alert": self, "comments": None} + if doc.get("_comments"): + context["comments"] = json.loads(doc.get("_comments")) + + if self.is_standard: + self.load_standard_properties(context) + try: + if self.channel == "Email": + self.send_an_email(doc, context) + + if self.channel == "Slack": + self.send_a_slack_msg(doc, context) + + if self.channel == "SMS": + self.send_sms(doc, context) + + if self.channel == "System Notification" or self.send_system_notification: + self.create_system_notification(doc, context) + + except Exception: + self.log_error("Failed to send Notification") + + if self.set_property_after_alert: + allow_update = True + if ( + doc.docstatus.is_submitted() + and not doc.meta.get_field(self.set_property_after_alert).allow_on_submit + ): + allow_update = False + try: + if allow_update and not doc.flags.in_notification_update: + fieldname = self.set_property_after_alert + value = self.property_value + if doc.meta.get_field(fieldname).fieldtype in xhiveframework.model.numeric_fieldtypes: + value = xhiveframework.utils.cint(value) + + doc.reload() + doc.set(fieldname, value) + doc.flags.updater_reference = { + "doctype": self.doctype, + "docname": self.name, + "label": _("via Notification"), + } + doc.flags.in_notification_update = True + doc.save(ignore_permissions=True) + doc.flags.in_notification_update = False + except Exception: + self.log_error("Document update failed") + + def create_system_notification(self, doc, context): + subject = self.subject + if "{" in subject: + subject = xhiveframework.render_template(self.subject, context) + + attachments = self.get_attachment(doc) + + recipients, cc, bcc = self.get_list_of_recipients(doc, context) + + users = recipients + cc + bcc + + if not users: + return + + notification_doc = { + "type": "Alert", + "document_type": doc.doctype, + "document_name": doc.name, + "subject": subject, + "from_user": doc.modified_by or doc.owner, + "email_content": xhiveframework.render_template(self.message, context), + "attached_file": attachments and json.dumps(attachments[0]), + } + enqueue_create_notification(users, notification_doc) + + def send_an_email(self, doc, context): + from email.utils import formataddr + + from xhiveframework.core.doctype.communication.email import _make as make_communication + + subject = self.subject + if "{" in subject: + subject = xhiveframework.render_template(self.subject, context) + + attachments = self.get_attachment(doc) + recipients, cc, bcc = self.get_list_of_recipients(doc, context) + if not (recipients or cc or bcc): + return + + sender = None + message = xhiveframework.render_template(self.message, context) + if self.sender and self.sender_email: + sender = formataddr((self.sender, self.sender_email)) + + communication = None + # Add mail notification to communication list + # No need to add if it is already a communication. + if doc.doctype != "Communication": + communication = make_communication( + doctype=doc.doctype, + name=doc.name, + content=message, + subject=subject, + sender=sender, + recipients=recipients, + communication_medium="Email", + send_email=False, + attachments=attachments, + cc=cc, + bcc=bcc, + communication_type="Automated Message", + ).get("name") + + xhiveframework.sendmail( + recipients=recipients, + subject=subject, + sender=sender, + cc=cc, + bcc=bcc, + message=message, + reference_doctype=doc.doctype, + reference_name=doc.name, + attachments=attachments, + expose_recipients="header", + print_letterhead=((attachments and attachments[0].get("print_letterhead")) or False), + communication=communication, + ) + + def send_a_slack_msg(self, doc, context): + send_slack_message( + webhook_url=self.slack_webhook_url, + message=xhiveframework.render_template(self.message, context), + reference_doctype=doc.doctype, + reference_name=doc.name, + ) + + def send_sms(self, doc, context): + send_sms( + receiver_list=self.get_receiver_list(doc, context), + msg=xhiveframework.render_template(self.message, context), + ) + + def get_list_of_recipients(self, doc, context): + recipients = [] + cc = [] + bcc = [] + for recipient in self.recipients: + if recipient.condition: + if not xhiveframework.safe_eval(recipient.condition, None, context): + continue + if recipient.receiver_by_document_field: + fields = recipient.receiver_by_document_field.split(",") + # fields from child table + if len(fields) > 1: + for d in doc.get(fields[1]): + email_id = d.get(fields[0]) + if validate_email_address(email_id): + recipients.append(email_id) + # field from parent doc + else: + email_ids_value = doc.get(fields[0]) + if validate_email_address(email_ids_value): + email_ids = email_ids_value.replace(",", "\n") + recipients = recipients + email_ids.split("\n") + + cc.extend(get_emails_from_template(recipient.cc, context)) + bcc.extend(get_emails_from_template(recipient.bcc, context)) + + # For sending emails to specified role + if recipient.receiver_by_role: + emails = get_info_based_on_role(recipient.receiver_by_role, "email", ignore_permissions=True) + + for email in emails: + recipients = recipients + email.split("\n") + + if self.send_to_all_assignees: + recipients = recipients + get_assignees(doc) + + return list(set(recipients)), list(set(cc)), list(set(bcc)) + + def get_receiver_list(self, doc, context): + """return receiver list based on the doc field and role specified""" + receiver_list = [] + for recipient in self.recipients: + if recipient.condition: + if not xhiveframework.safe_eval(recipient.condition, None, context): + continue + + # For sending messages to the owner's mobile phone number + if recipient.receiver_by_document_field == "owner": + receiver_list += get_user_info([dict(user_name=doc.get("owner"))], "mobile_no") + # For sending messages to the number specified in the receiver field + elif recipient.receiver_by_document_field: + receiver_list.append(doc.get(recipient.receiver_by_document_field)) + + # For sending messages to specified role + if recipient.receiver_by_role: + receiver_list += get_info_based_on_role( + recipient.receiver_by_role, "mobile_no", ignore_permissions=True + ) + + return receiver_list + + def get_attachment(self, doc): + """check print settings are attach the pdf""" + if not self.attach_print: + return None + + print_settings = xhiveframework.get_doc("Print Settings", "Print Settings") + if (doc.docstatus == 0 and not print_settings.allow_print_for_draft) or ( + doc.docstatus == 2 and not print_settings.allow_print_for_cancelled + ): + # ignoring attachment as draft and cancelled documents are not allowed to print + status = "Draft" if doc.docstatus == 0 else "Cancelled" + xhiveframework.throw( + _( + """Not allowed to attach {0} document, please enable Allow Print For {0} in Print Settings""" + ).format(status), + title=_("Error in Notification"), + ) + else: + return [ + { + "print_format_attachment": 1, + "doctype": doc.doctype, + "name": doc.name, + "print_format": self.print_format, + "print_letterhead": print_settings.with_letterhead, + "lang": xhiveframework.db.get_value("Print Format", self.print_format, "default_print_language") + if self.print_format + else "en", + } + ] + + def get_template(self): + module = get_doc_module(self.module, self.doctype, self.name) + + def load_template(extn): + template = "" + template_path = os.path.join(os.path.dirname(module.__file__), xhiveframework.scrub(self.name) + extn) + if os.path.exists(template_path): + with open(template_path) as f: + template = f.read() + return template + + return load_template(".html") or load_template(".md") + + def load_standard_properties(self, context): + """load templates and run get_context""" + module = get_doc_module(self.module, self.doctype, self.name) + if module: + if hasattr(module, "get_context"): + out = module.get_context(context) + if out: + context.update(out) + + self.message = self.get_template() + + if not is_html(self.message): + self.message = xhiveframework.utils.md_to_html(self.message) + + def on_trash(self): + xhiveframework.cache.hdel("notifications", self.document_type) + + +@xhiveframework.whitelist() +def get_documents_for_today(notification): + notification = xhiveframework.get_doc("Notification", notification) + notification.check_permission("read") + return [d.name for d in notification.get_documents_for_today()] + + +def trigger_daily_alerts(): + trigger_notifications(None, "daily") + + +def trigger_notifications(doc, method=None): + if xhiveframework.flags.in_import or xhiveframework.flags.in_patch: + # don't send notifications while syncing or patching + return + + if method == "daily": + doc_list = xhiveframework.get_all( + "Notification", filters={"event": ("in", ("Days Before", "Days After")), "enabled": 1} + ) + for d in doc_list: + alert = xhiveframework.get_doc("Notification", d.name) + + for doc in alert.get_documents_for_today(): + evaluate_alert(doc, alert, alert.event) + xhiveframework.db.commit() + + +def evaluate_alert(doc: Document, alert, event): + from jinja2 import TemplateError + + try: + if isinstance(alert, str): + alert = xhiveframework.get_doc("Notification", alert) + + context = get_context(doc) + + if alert.condition: + if not xhiveframework.safe_eval(alert.condition, None, context): + return + + if event == "Value Change" and not doc.is_new(): + if not xhiveframework.db.has_column(doc.doctype, alert.value_changed): + alert.db_set("enabled", 0) + alert.log_error(f"Notification {alert.name} has been disabled due to missing field") + return + + doc_before_save = doc.get_doc_before_save() + field_value_before_save = doc_before_save.get(alert.value_changed) if doc_before_save else None + + fieldtype = doc.meta.get_field(alert.value_changed).fieldtype + if cast(fieldtype, doc.get(alert.value_changed)) == cast(fieldtype, field_value_before_save): + # value not changed + return + + if event != "Value Change" and not doc.is_new(): + # reload the doc for the latest values & comments, + # except for validate type event. + doc.reload() + alert.send(doc) + except TemplateError: + message = _("Error while evaluating Notification {0}. Please fix your template.").format( + xhiveframework.utils.get_link_to_form("Notification", alert.name) + ) + xhiveframework.throw(message, title=_("Error in Notification")) + except Exception as e: + title = str(e) + xhiveframework.log_error(title=title) + + msg = f"
      {title}{message}
      " + xhiveframework.throw(msg, title=_("Error in Notification")) + + +def get_context(doc): + return { + "doc": doc, + "nowdate": nowdate, + "xhiveframework": xhiveframework._dict(utils=get_safe_globals().get("xhiveframework").get("utils")), + } + + +def get_assignees(doc): + assignees = [] + assignees = xhiveframework.get_all( + "ToDo", + filters={"status": "Open", "reference_name": doc.name, "reference_type": doc.doctype}, + fields=["allocated_to"], + ) + + return [d.allocated_to for d in assignees] + + +def get_emails_from_template(template, context): + if not template: + return () + + emails = xhiveframework.render_template(template, context) if "{" in template else template + return filter(None, emails.replace(",", "\n").split("\n")) diff --git a/xhiveframework/email/doctype/notification/test_notification.py b/xhiveframework/email/doctype/notification/test_notification.py new file mode 100644 index 0000000..db659df --- /dev/null +++ b/xhiveframework/email/doctype/notification/test_notification.py @@ -0,0 +1,380 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +from contextlib import contextmanager + +import xhiveframework +import xhiveframework.utils +import xhiveframework.utils.scheduler +from xhiveframework.desk.form import assign_to +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["User", "Notification"] + + +@contextmanager +def get_test_notification(config): + try: + notification = xhiveframework.get_doc(doctype="Notification", **config).insert() + yield notification + finally: + notification.delete() + + +class TestNotification(XhiveFrameworkTestCase): + def setUp(self): + xhiveframework.db.delete("Email Queue") + xhiveframework.set_user("test@example.com") + + if not xhiveframework.db.exists("Notification", {"name": "ToDo Status Update"}, "name"): + notification = xhiveframework.new_doc("Notification") + notification.name = "ToDo Status Update" + notification.subject = "ToDo Status Update" + notification.document_type = "ToDo" + notification.event = "Value Change" + notification.value_changed = "status" + notification.send_to_all_assignees = 1 + notification.set_property_after_alert = "description" + notification.property_value = "Changed by Notification" + notification.save() + + if not xhiveframework.db.exists("Notification", {"name": "Contact Status Update"}, "name"): + notification = xhiveframework.new_doc("Notification") + notification.name = "Contact Status Update" + notification.subject = "Contact Status Update" + notification.document_type = "Contact" + notification.event = "Value Change" + notification.value_changed = "status" + notification.message = "Test Contact Update" + notification.append("recipients", {"receiver_by_document_field": "email_id,email_ids"}) + notification.save() + + def tearDown(self): + xhiveframework.set_user("Administrator") + + def test_new_and_save(self): + """Check creating a new communication triggers a notification.""" + communication = xhiveframework.new_doc("Communication") + communication.communication_type = "Comment" + communication.subject = "test" + communication.content = "test" + communication.insert(ignore_permissions=True) + + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + { + "reference_doctype": "Communication", + "reference_name": communication.name, + "status": "Not Sent", + }, + ) + ) + xhiveframework.db.delete("Email Queue") + + communication.reload() + communication.content = "test 2" + communication.save() + + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + { + "reference_doctype": "Communication", + "reference_name": communication.name, + "status": "Not Sent", + }, + ) + ) + + self.assertEqual(xhiveframework.db.get_value("Communication", communication.name, "subject"), "__testing__") + + def test_condition(self): + """Check notification is triggered based on a condition.""" + event = xhiveframework.new_doc("Event") + event.subject = "test" + event.event_type = "Private" + event.starts_on = "2014-06-06 12:00:00" + event.insert() + + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + event.event_type = "Public" + event.save() + + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + # Make sure that we track the triggered notifications in communication doctype. + self.assertTrue( + xhiveframework.db.get_value( + "Communication", + { + "reference_doctype": "Event", + "reference_name": event.name, + "communication_type": "Automated Message", + }, + ) + ) + + def test_invalid_condition(self): + xhiveframework.set_user("Administrator") + notification = xhiveframework.new_doc("Notification") + notification.subject = "test" + notification.document_type = "ToDo" + notification.send_alert_on = "New" + notification.message = "test" + + recipent = xhiveframework.new_doc("Notification Recipient") + recipent.receiver_by_document_field = "owner" + + notification.recipents = recipent + notification.condition = "test" + + self.assertRaises(xhiveframework.ValidationError, notification.save) + notification.delete() + + def test_value_changed(self): + event = xhiveframework.new_doc("Event") + event.subject = "test" + event.event_type = "Private" + event.starts_on = "2014-06-06 12:00:00" + event.insert() + + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + event.subject = "test 1" + event.save() + + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + event.description = "test" + event.save() + + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + def test_alert_disabled_on_wrong_field(self): + xhiveframework.set_user("Administrator") + notification = xhiveframework.get_doc( + { + "doctype": "Notification", + "subject": "_Test Notification for wrong field", + "document_type": "Event", + "event": "Value Change", + "attach_print": 0, + "value_changed": "description1", + "message": "Description changed", + "recipients": [{"receiver_by_document_field": "owner"}], + } + ).insert() + xhiveframework.db.commit() + + event = xhiveframework.new_doc("Event") + event.subject = "test-2" + event.event_type = "Private" + event.starts_on = "2014-06-06 12:00:00" + event.insert() + event.subject = "test 1" + event.save() + + # verify that notification is disabled + notification.reload() + self.assertEqual(notification.enabled, 0) + notification.delete() + event.delete() + + def test_date_changed(self): + event = xhiveframework.new_doc("Event") + event.subject = "test" + event.event_type = "Private" + event.starts_on = "2014-01-01 12:00:00" + event.insert() + + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + xhiveframework.set_user("Administrator") + xhiveframework.get_doc( + "Scheduled Job Type", + dict(method="xhiveframework.email.doctype.notification.notification.trigger_daily_alerts"), + ).execute() + + # not today, so no alert + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + event.starts_on = xhiveframework.utils.add_days(xhiveframework.utils.nowdate(), 2) + " 12:00:00" + event.save() + + # Value Change notification alert will be trigger as description is not changed + # mail will not be sent + self.assertFalse( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + xhiveframework.get_doc( + "Scheduled Job Type", + dict(method="xhiveframework.email.doctype.notification.notification.trigger_daily_alerts"), + ).execute() + + # today so show alert + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "Event", "reference_name": event.name, "status": "Not Sent"}, + ) + ) + + def test_cc_jinja(self): + xhiveframework.db.delete("User", {"email": "test_jinja@example.com"}) + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Email Queue Recipient") + + test_user = xhiveframework.new_doc("User") + test_user.name = "test_jinja" + test_user.first_name = "test_jinja" + test_user.email = "test_jinja@example.com" + + test_user.insert(ignore_permissions=True) + + self.assertTrue( + xhiveframework.db.get_value( + "Email Queue", + {"reference_doctype": "User", "reference_name": test_user.name, "status": "Not Sent"}, + ) + ) + + self.assertTrue(xhiveframework.db.get_value("Email Queue Recipient", {"recipient": "test_jinja@example.com"})) + + xhiveframework.db.delete("User", {"email": "test_jinja@example.com"}) + xhiveframework.db.delete("Email Queue") + xhiveframework.db.delete("Email Queue Recipient") + + def test_notification_to_assignee(self): + todo = xhiveframework.new_doc("ToDo") + todo.description = "Test Notification" + todo.save() + + assign_to.add( + { + "assign_to": ["test2@example.com"], + "doctype": todo.doctype, + "name": todo.name, + "description": "Close this Todo", + } + ) + + assign_to.add( + { + "assign_to": ["test1@example.com"], + "doctype": todo.doctype, + "name": todo.name, + "description": "Close this Todo", + } + ) + + # change status of todo + todo.status = "Closed" + todo.save() + + email_queue = xhiveframework.get_doc( + "Email Queue", {"reference_doctype": "ToDo", "reference_name": todo.name} + ) + + self.assertTrue(email_queue) + + # check if description is changed after alert since set_property_after_alert is set + self.assertEqual(todo.description, "Changed by Notification") + + recipients = [d.recipient for d in email_queue.recipients] + self.assertTrue("test2@example.com" in recipients) + self.assertTrue("test1@example.com" in recipients) + + def test_notification_by_child_table_field(self): + contact = xhiveframework.new_doc("Contact") + contact.first_name = "John Doe" + contact.status = "Open" + contact.append("email_ids", {"email_id": "test2@example.com", "is_primary": 1}) + + contact.append("email_ids", {"email_id": "test1@example.com"}) + + contact.save() + + # change status of contact + contact.status = "Replied" + contact.save() + + email_queue = xhiveframework.get_doc( + "Email Queue", {"reference_doctype": "Contact", "reference_name": contact.name} + ) + + self.assertTrue(email_queue) + + recipients = [d.recipient for d in email_queue.recipients] + self.assertTrue("test2@example.com" in recipients) + self.assertTrue("test1@example.com" in recipients) + + def test_notification_value_change_casted_types(self): + """Make sure value change event dont fire because of incorrect type comparisons.""" + xhiveframework.set_user("Administrator") + + notification = { + "document_type": "User", + "subject": "User changed birthdate", + "event": "Value Change", + "channel": "System Notification", + "value_changed": "birth_date", + "recipients": [{"receiver_by_document_field": "email"}], + } + + with get_test_notification(notification) as n: + xhiveframework.db.delete("Notification Log", {"subject": n.subject}) + + user = xhiveframework.get_doc("User", "test@example.com") + user.birth_date = xhiveframework.utils.add_days(user.birth_date, 1) + user.save() + + user.reload() + user.birth_date = xhiveframework.utils.getdate(user.birth_date) + user.save() + self.assertEqual(1, xhiveframework.db.count("Notification Log", {"subject": n.subject})) + + @classmethod + def tearDownClass(cls): + xhiveframework.delete_doc_if_exists("Notification", "ToDo Status Update") + xhiveframework.delete_doc_if_exists("Notification", "Contact Status Update") diff --git a/xhiveframework/email/doctype/notification/test_records.json b/xhiveframework/email/doctype/notification/test_records.json new file mode 100644 index 0000000..665f800 --- /dev/null +++ b/xhiveframework/email/doctype/notification/test_records.json @@ -0,0 +1,76 @@ +[ + { + "doctype": "Notification", + "subject":"_Test Notification 1", + "document_type": "Communication", + "event": "New", + "attach_print": 0, + "message": "New comment {{ doc.content }} created", + "condition": "doc.communication_type=='Comment'", + "recipients": [ + { "receiver_by_document_field": "owner" } + ] + }, + { + "doctype": "Notification", + "subject":"_Test Notification 2", + "document_type": "Communication", + "event": "Save", + "attach_print": 0, + "message": "New comment {{ doc.content }} saved", + "condition": "doc.communication_type=='Comment'", + "recipients": [ + { "receiver_by_document_field": "owner" } + ], + "set_property_after_alert": "subject", + "property_value": "__testing__" + }, + { + "doctype": "Notification", + "subject":"_Test Notification 3", + "document_type": "Event", + "event": "Save", + "attach_print": 0, + "condition": "doc.event_type=='Public'", + "message": "A new public event {{ doc.subject }} on {{ doc.starts_on }} is created", + "recipients": [ + { "receiver_by_document_field": "owner" } + ] + }, + { + "doctype": "Notification", + "subject":"_Test Notification 4", + "document_type": "Event", + "event": "Value Change", + "attach_print": 0, + "value_changed": "description", + "message": "Description changed", + "recipients": [ + { "receiver_by_document_field": "owner" } + ] + }, + { + "doctype": "Notification", + "subject":"_Test Notification 5", + "document_type": "Event", + "event": "Days Before", + "attach_print": 0, + "date_changed": "starts_on", + "days_in_advance": 2, + "message": "Description changed", + "recipients": [ + { "receiver_by_document_field": "owner" } + ] + }, + { + "doctype": "Notification", + "subject":"_Test Notification 6", + "document_type": "User", + "event": "New", + "attach_print": 0, + "message": "New user {{ doc.name }} created", + "recipients": [ + { "receiver_by_document_field": "owner", "cc": "{{ doc.email }}" } + ] + } +] diff --git a/xhiveframework/email/doctype/notification_recipient/__init__.py b/xhiveframework/email/doctype/notification_recipient/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/notification_recipient/notification_recipient.json b/xhiveframework/email/doctype/notification_recipient/notification_recipient.json new file mode 100644 index 0000000..0670320 --- /dev/null +++ b/xhiveframework/email/doctype/notification_recipient/notification_recipient.json @@ -0,0 +1,61 @@ +{ + "actions": [], + "creation": "2014-07-11 17:19:37.037109", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "receiver_by_document_field", + "receiver_by_role", + "cc", + "bcc", + "condition" + ], + "fields": [ + { + "depends_on": "eval:parent.channel=='Email'", + "description": "Optional: Always send to these ids. Each Email Address on a new row", + "fieldname": "cc", + "fieldtype": "Code", + "label": "CC" + }, + { + "depends_on": "eval:parent.channel=='Email'", + "fieldname": "bcc", + "fieldtype": "Code", + "label": "BCC" + }, + { + "description": "Expression, Optional", + "fieldname": "condition", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Condition" + }, + { + "fieldname": "receiver_by_document_field", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Receiver By Document Field" + }, + { + "fieldname": "receiver_by_role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Receiver By Role", + "options": "Role" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2020-09-01 17:40:27.289105", + "modified_by": "Administrator", + "module": "Email", + "name": "Notification Recipient", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/notification_recipient/notification_recipient.py b/xhiveframework/email/doctype/notification_recipient/notification_recipient.py new file mode 100644 index 0000000..ffc8500 --- /dev/null +++ b/xhiveframework/email/doctype/notification_recipient/notification_recipient.py @@ -0,0 +1,25 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from xhiveframework.model.document import Document + + +class NotificationRecipient(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + bcc: DF.Code | None + cc: DF.Code | None + condition: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + receiver_by_document_field: DF.Literal[None] + receiver_by_role: DF.Link | None + # end: auto-generated types + pass diff --git a/xhiveframework/email/doctype/unhandled_email/__init__.py b/xhiveframework/email/doctype/unhandled_email/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/doctype/unhandled_email/test_unhandled_email.py b/xhiveframework/email/doctype/unhandled_email/test_unhandled_email.py new file mode 100644 index 0000000..edaf8d0 --- /dev/null +++ b/xhiveframework/email/doctype/unhandled_email/test_unhandled_email.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Unhandled Emails') + + +class TestUnhandledEmail(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/email/doctype/unhandled_email/unhandled_email.json b/xhiveframework/email/doctype/unhandled_email/unhandled_email.json new file mode 100644 index 0000000..d904536 --- /dev/null +++ b/xhiveframework/email/doctype/unhandled_email/unhandled_email.json @@ -0,0 +1,60 @@ +{ + "actions": [], + "creation": "2016-04-14 09:41:45.892975", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "email_account", + "uid", + "reason", + "message_id", + "raw" + ], + "fields": [ + { + "fieldname": "email_account", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Email Account", + "options": "Email Account" + }, + { + "fieldname": "uid", + "fieldtype": "Data", + "label": "UID" + }, + { + "fieldname": "reason", + "fieldtype": "Long Text", + "in_list_view": 1, + "label": "Reason" + }, + { + "fieldname": "message_id", + "fieldtype": "Code", + "label": "Message-id" + }, + { + "fieldname": "raw", + "fieldtype": "Code", + "label": "Raw Email" + } + ], + "in_create": 1, + "links": [], + "modified": "2022-08-03 12:20:51.822287", + "modified_by": "Administrator", + "module": "Email", + "name": "Unhandled Email", + "owner": "Administrator", + "permissions": [ + { + "read": 1, + "role": "System Manager" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/email/doctype/unhandled_email/unhandled_email.py b/xhiveframework/email/doctype/unhandled_email/unhandled_email.py new file mode 100644 index 0000000..a5cb589 --- /dev/null +++ b/xhiveframework/email/doctype/unhandled_email/unhandled_email.py @@ -0,0 +1,31 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class UnhandledEmail(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + email_account: DF.Link | None + message_id: DF.Code | None + raw: DF.Code | None + reason: DF.LongText | None + uid: DF.Data | None + # end: auto-generated types + + @staticmethod + def clear_old_logs(days=30): + xhiveframework.db.delete( + "Unhandled Email", + { + "modified": ("<", xhiveframework.utils.add_days(xhiveframework.utils.nowdate(), -1 * days)), + }, + ) diff --git a/xhiveframework/email/email.md b/xhiveframework/email/email.md new file mode 100644 index 0000000..dbd1952 --- /dev/null +++ b/xhiveframework/email/email.md @@ -0,0 +1,65 @@ +# Email Architecture + +This document describes the high-level architecture of how emails work in xhiveframework. + +> NOTE: There are mentions of different types of workers to execute jobs from different types of queues in the explanations below, however only a single type of worker can also be used to do the same. + +### High-Level View + +#### Pulling/Sending: + +![email-pull-flow](assets/images/email-pull-flow.png) + +The same follows for email sending with the only difference being in the jobs queued. + +The scheduler schedules/pushes the `email.pull`/`queue.flush` job into the default queue which is picked up by the default worker and upon execution enqueues `pull_from_email_account`/`send_mail` job into the short queue and the job is then picked up by the short worker and the email is pulled in/sent out of the system. + + +### Code Map + +This section talks briefly about various important directories/files. + +#### `doctype/email_queue` + +Contains all logic about email sending. Every email to be sent is stored in `Email Queue` DocType (Newsletter(s) included). + +#### `receive.py` + +Contains all the logic and classes about email receiving. + +#### `doctype/email_account` + +Contains logic for validating connection/auth for email account(s). + +Also, contains the abstraction for facilitating email receiving. + +#### `doctype/newsletter` + +Contains all the logic about newsletter scheduling, subscribing and unsubscribing. + +Newsletter has a similar design as to how normal emails are sent except for the initial scheduled job (`newsletter.send_scheduled_email`) and the fact that newsletter recipients are batched, primarily for SMTP connection reusing & mitigating any potential timeouts (available as a general feature as well). + +Each batch is queued as a separate job (`QueueBuilder.send_emails`) into the long queue by the default worker which is then picked up by the long worker. + +#### `doctype/notification` + +Contains all logic about handling various types of notifications by the system. + +#### `email_body.py` + +Contains logic about creating the email body. We use Python's email std lib for body generation. + +The email message format/syntax is described in [RFC5322](https://datatracker.ietf.org/doc/html/rfc5322) + + +### Observability + +At the application level, + +`RQ Job` DocType is the most useful in knowing the status of any type of "recent" email job (pull/send). + +`Email Queue` DocType is self-contained and stores all the errored out (along with traceback) as well as the sent/partially sent mails from the system. + +`Communication` DocType stores all the successfully received emails and `Unhandled Email` DocType stores all the "unhandled" ones during receiving. + +Apart from these, the `Error Log` DocType also comes into help from time to time. diff --git a/xhiveframework/email/email_body.py b/xhiveframework/email/email_body.py new file mode 100755 index 0000000..9a6048c --- /dev/null +++ b/xhiveframework/email/email_body.py @@ -0,0 +1,591 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import email.utils +import os +import re +from email import policy +from email.header import Header +from email.mime.multipart import MIMEMultipart + +import xhiveframework +from xhiveframework.email.doctype.email_account.email_account import EmailAccount +from xhiveframework.utils import ( + cint, + expand_relative_urls, + get_url, + markdown, + parse_addr, + random_string, + scrub_urls, + split_emails, + strip, + to_markdown, +) +from xhiveframework.utils.pdf import get_pdf + +EMBED_PATTERN = re.compile("""embed=["'](.*?)["']""") + + +def get_email( + recipients, + sender="", + msg="", + subject="[No Subject]", + text_content=None, + footer=None, + print_html=None, + formatted=None, + attachments=None, + content=None, + reply_to=None, + cc=None, + bcc=None, + email_account=None, + expose_recipients=None, + inline_images=None, + header=None, +): + """Prepare an email with the following format: + - multipart/mixed + - multipart/alternative + - text/plain + - multipart/related + - text/html + - inline image + - attachment + """ + content = content or msg + + if cc is None: + cc = [] + if bcc is None: + bcc = [] + if inline_images is None: + inline_images = [] + + emailobj = EMail( + sender, + recipients, + subject, + reply_to=reply_to, + cc=cc, + bcc=bcc, + email_account=email_account, + expose_recipients=expose_recipients, + ) + + if not content.strip().startswith("<"): + content = markdown(content) + + emailobj.set_html( + content, + text_content, + footer=footer, + header=header, + print_html=print_html, + formatted=formatted, + inline_images=inline_images, + ) + + if isinstance(attachments, dict): + attachments = [attachments] + + for attach in attachments or []: + # cannot attach if no filecontent + if attach.get("fcontent") is None: + continue + emailobj.add_attachment(**attach) + + return emailobj + + +class EMail: + """ + Wrapper on the email module. Email object represents emails to be sent to the client. + Also provides a clean way to add binary `FileData` attachments + Also sets all messages as multipart/alternative for cleaner reading in text-only clients + """ + + def __init__( + self, + sender="", + recipients=(), + subject="", + alternative=0, + reply_to=None, + cc=(), + bcc=(), + email_account=None, + expose_recipients=None, + ): + from email import charset as Charset + + Charset.add_charset("utf-8", Charset.QP, Charset.QP, "utf-8") + + if isinstance(recipients, str): + recipients = recipients.replace(";", ",").replace("\n", "") + recipients = split_emails(recipients) + + # remove null + recipients = filter(None, (strip(r) for r in recipients)) + + self.sender = sender + self.reply_to = reply_to or sender + self.recipients = recipients + self.subject = subject + self.expose_recipients = expose_recipients + + self.msg_root = MIMEMultipart("mixed", policy=policy.SMTP) + self.msg_alternative = MIMEMultipart("alternative", policy=policy.SMTP) + self.msg_root.attach(self.msg_alternative) + self.cc = cc or [] + self.bcc = bcc or [] + self.html_set = False + + self.email_account = email_account or EmailAccount.find_outgoing( + match_by_email=sender, _raise_error=True + ) + + def set_html( + self, + message, + text_content=None, + footer=None, + print_html=None, + formatted=None, + inline_images=None, + header=None, + ): + """Attach message in the html portion of multipart/alternative""" + if not formatted: + formatted = get_formatted_html( + self.subject, + message, + footer, + print_html, + email_account=self.email_account, + header=header, + sender=self.sender, + ) + + # this is the first html part of a multi-part message, + # convert to text well + if not self.html_set: + if text_content: + self.set_text(expand_relative_urls(text_content)) + else: + self.set_html_as_text(expand_relative_urls(formatted)) + + self.set_part_html(formatted, inline_images) + self.html_set = True + + def set_text(self, message): + """ + Attach message in the text portion of multipart/alternative + """ + from email.mime.text import MIMEText + + part = MIMEText(message, "plain", "utf-8", policy=policy.SMTP) + self.msg_alternative.attach(part) + + def set_part_html(self, message, inline_images): + from email.mime.text import MIMEText + + has_inline_images = EMBED_PATTERN.search(message) + + if has_inline_images: + # process inline images + message, _inline_images = replace_filename_with_cid(message) + + # prepare parts + msg_related = MIMEMultipart("related", policy=policy.SMTP) + + html_part = MIMEText(message, "html", "utf-8", policy=policy.SMTP) + msg_related.attach(html_part) + + for image in _inline_images: + self.add_attachment( + image.get("filename"), + image.get("filecontent"), + content_id=image.get("content_id"), + parent=msg_related, + inline=True, + ) + + self.msg_alternative.attach(msg_related) + else: + self.msg_alternative.attach(MIMEText(message, "html", "utf-8", policy=policy.SMTP)) + + def set_html_as_text(self, html): + """Set plain text from HTML""" + self.set_text(to_markdown(html)) + + def set_message(self, message, mime_type="text/html", as_attachment=0, filename="attachment.html"): + """Append the message with MIME content to the root node (as attachment)""" + from email.mime.text import MIMEText + + maintype, subtype = mime_type.split("/") + part = MIMEText(message, _subtype=subtype, policy=policy.SMTP) + + if as_attachment: + part.add_header("Content-Disposition", "attachment", filename=filename) + + self.msg_root.attach(part) + + def attach_file(self, n): + """attach a file from the `FileData` table""" + _file = xhiveframework.get_doc("File", {"file_name": n}) + content = _file.get_content() + if not content: + return + + self.add_attachment(_file.file_name, content) + + def add_attachment(self, fname, fcontent, content_type=None, parent=None, content_id=None, inline=False): + """add attachment""" + + if not parent: + parent = self.msg_root + + add_attachment(fname, fcontent, content_type, parent, content_id, inline) + + def add_pdf_attachment(self, name, html, options=None): + self.add_attachment(name, get_pdf(html, options), "application/octet-stream") + + def validate(self): + """validate the Email Addresses""" + from xhiveframework.utils import validate_email_address + + if not self.sender: + self.sender = self.email_account.default_sender + + validate_email_address(strip(self.sender), True) + self.reply_to = validate_email_address(strip(self.reply_to) or self.sender, True) + + self.set_header("X-Original-From", self.sender) + self.replace_sender() + self.replace_sender_name() + + self.recipients = [strip(r) for r in self.recipients if r not in xhiveframework.STANDARD_USERS] + self.cc = [strip(r) for r in self.cc if r not in xhiveframework.STANDARD_USERS] + self.bcc = [strip(r) for r in self.bcc if r not in xhiveframework.STANDARD_USERS] + + for e in self.recipients + (self.cc or []) + (self.bcc or []): + validate_email_address(e, True) + + def replace_sender(self): + if cint(self.email_account.always_use_account_email_id_as_sender): + sender_name, _ = parse_addr(self.sender) + self.sender = email.utils.formataddr( + (str(Header(sender_name or self.email_account.name, "utf-8")), self.email_account.email_id) + ) + + def replace_sender_name(self): + if cint(self.email_account.always_use_account_name_as_sender_name): + _, sender_email = parse_addr(self.sender) + self.sender = email.utils.formataddr( + (str(Header(self.email_account.name, "utf-8")), sender_email) + ) + + def set_message_id(self, message_id, is_notification=False): + if message_id: + message_id = "<" + message_id + ">" + else: + message_id = get_message_id() + self.set_header("isnotification", "") + + if is_notification: + self.set_header("isnotification", "") + + self.set_header("Message-Id", message_id) + + def set_in_reply_to(self, in_reply_to): + """Used to send the Message-Id of a received email back as In-Reply-To""" + self.set_header("In-Reply-To", in_reply_to) + + def make(self): + """build into msg_root""" + headers = { + "Subject": strip(self.subject), + "From": self.sender, + "To": ", ".join(self.recipients) if self.expose_recipients == "header" else "", + "Date": email.utils.formatdate(), + "Reply-To": self.reply_to if self.reply_to else None, + "CC": ", ".join(self.cc) if self.cc and self.expose_recipients == "header" else None, + "X-XhiveFramework-Site": get_url(), + } + + # reset headers as values may be changed. + for key, val in headers.items(): + if val: + self.set_header(key, val) + + # call hook to enable apps to modify msg_root before sending + for hook in xhiveframework.get_hooks("make_email_body_message"): + xhiveframework.get_attr(hook)(self) + + def set_header(self, key, value): + if key in self.msg_root: + # delete key if found + # this is done because adding the same key doesn't override + # the existing key, rather appends another header with same key. + del self.msg_root[key] + + self.msg_root[key] = sanitize_email_header(value) + + def as_string(self): + """validate, build message and convert to string""" + self.validate() + self.make() + return self.msg_root.as_string(policy=policy.SMTP) + + +def get_formatted_html( + subject, + message, + footer=None, + print_html=None, + email_account=None, + header=None, + unsubscribe_link: xhiveframework._dict | None = None, + sender=None, + with_container=False, +): + email_account = email_account or EmailAccount.find_outgoing(match_by_email=sender) + + rendered_email = xhiveframework.get_template("templates/emails/standard.html").render( + { + "brand_logo": get_brand_logo(email_account) if with_container or header else None, + "with_container": with_container, + "site_url": get_url(), + "header": get_header(header), + "content": message, + "footer": get_footer(email_account, footer), + "title": subject, + "print_html": print_html, + "subject": subject, + } + ) + + html = scrub_urls(rendered_email) + + if unsubscribe_link: + html = html.replace("", unsubscribe_link.html) + + return inline_style_in_html(html) + + +@xhiveframework.whitelist() +def get_email_html(template, args, subject, header=None, with_container=False): + import json + + with_container = cint(with_container) + args = json.loads(args) + if header and header.startswith("["): + header = json.loads(header) + email = xhiveframework.utils.jinja.get_email_from_template(template, args) + return get_formatted_html(subject, email[0], header=header, with_container=with_container) + + +def inline_style_in_html(html): + """Convert email.css and html to inline-styled html""" + from premailer import Premailer + + from xhiveframework.utils.jinja_globals import bundled_asset + + # get email css files from hooks + css_files = xhiveframework.get_hooks("email_css") + css_files = [bundled_asset(path) for path in css_files] + css_files = [path.lstrip("/") for path in css_files] + css_files = [css_file for css_file in css_files if os.path.exists(os.path.abspath(css_file))] + + p = Premailer( + html=html, external_styles=css_files, strip_important=False, allow_loading_external_files=True + ) + + return p.transform() + + +def add_attachment(fname, fcontent, content_type=None, parent=None, content_id=None, inline=False): + """Add attachment to parent which must an email object""" + import mimetypes + from email.mime.audio import MIMEAudio + from email.mime.base import MIMEBase + from email.mime.image import MIMEImage + from email.mime.text import MIMEText + + if not content_type: + content_type, encoding = mimetypes.guess_type(fname) + + if not parent: + return + + if content_type is None: + # No guess could be made, or the file is encoded (compressed), so + # use a generic bag-of-bits type. + content_type = "application/octet-stream" + + maintype, subtype = content_type.split("/", 1) + if maintype == "text": + # Note: we should handle calculating the charset + if isinstance(fcontent, str): + fcontent = fcontent.encode("utf-8") + part = MIMEText(fcontent, _subtype=subtype, _charset="utf-8") + elif maintype == "image": + part = MIMEImage(fcontent, _subtype=subtype) + elif maintype == "audio": + part = MIMEAudio(fcontent, _subtype=subtype) + else: + part = MIMEBase(maintype, subtype) + part.set_payload(fcontent) + # Encode the payload using Base64 + from email import encoders + + encoders.encode_base64(part) + + # Set the filename parameter + if fname: + attachment_type = "inline" if inline else "attachment" + part.add_header("Content-Disposition", attachment_type, filename=str(fname)) + if content_id: + part.add_header("Content-ID", f"<{content_id}>") + + parent.attach(part) + + +def get_message_id(): + """Returns Message ID created from doctype and name""" + return email.utils.make_msgid(domain=xhiveframework.local.site) + + +def get_signature(email_account): + if email_account and email_account.add_signature and email_account.signature: + return "
      " + email_account.signature + else: + return "" + + +def get_footer(email_account, footer=None): + """append a footer (signature)""" + footer = footer or "" + + args = {} + + if email_account and email_account.footer: + args.update({"email_account_footer": email_account.footer}) + + sender_address = xhiveframework.db.get_default("email_footer_address") + + if sender_address: + args.update({"sender_address": sender_address}) + + if not cint(xhiveframework.db.get_default("disable_standard_email_footer")): + args.update({"default_mail_footer": xhiveframework.get_hooks("default_mail_footer")}) + + footer += xhiveframework.utils.jinja.get_email_from_template("email_footer", args)[0] + + return footer + + +def replace_filename_with_cid(message): + """Replaces with + and return the modified message and + a list of inline_images with {filename, filecontent, content_id} + """ + + inline_images = [] + + while True: + matches = EMBED_PATTERN.search(message) + if not matches: + break + groups = matches.groups() + + # found match + img_path = groups[0] + img_path_escaped = xhiveframework.utils.html_utils.unescape_html(img_path) + filename = img_path_escaped.rsplit("/")[-1] + + filecontent = get_filecontent_from_path(img_path_escaped) + if not filecontent: + message = re.sub(f"""embed=['"]{re.escape(img_path)}['"]""", "", message) + continue + + content_id = random_string(10) + + inline_images.append({"filename": filename, "filecontent": filecontent, "content_id": content_id}) + + message = re.sub(f"""embed=['"]{re.escape(img_path)}['"]""", f'src="cid:{content_id}"', message) + + return (message, inline_images) + + +def get_filecontent_from_path(path): + if not path: + return + + if path.startswith("/"): + path = path[1:] + + if path.startswith("assets/"): + # from public folder + full_path = os.path.abspath(path) + elif path.startswith("files/"): + # public file + full_path = xhiveframework.get_site_path("public", path) + elif path.startswith("private/files/"): + # private file + full_path = xhiveframework.get_site_path(path) + else: + full_path = path + + if os.path.exists(full_path): + with open(full_path, "rb") as f: + filecontent = f.read() + + return filecontent + else: + return None + + +def get_header(header=None): + """Build header from template""" + from xhiveframework.utils.jinja import get_email_from_template + + if not header: + return None + + if isinstance(header, str): + # header = 'My Title' + header = [header, None] + if len(header) == 1: + # header = ['My Title'] + header.append(None) + # header = ['My Title', 'orange'] + title, indicator = header + + if not title: + title = xhiveframework.get_hooks("app_title")[-1] + + email_header, text = get_email_from_template( + "email_header", {"header_title": title, "indicator": indicator} + ) + + return email_header + + +def sanitize_email_header(header: str): + """ + Removes all line boundaries in the headers. + + Email Policy (python's std) has some bugs in it which uses splitlines + and raises ValueError (ref: https://github.com/python/cpython/blob/main/Lib/email/policy.py#L143). + Hence removing all line boundaries while sanitization of headers to prevent such faliures. + The line boundaries which are removed can be found here: https://docs.python.org/3/library/stdtypes.html#str.splitlines + """ + + return "".join(header.splitlines()) + + +def get_brand_logo(email_account): + return email_account.get("brand_logo") diff --git a/xhiveframework/email/inbox.py b/xhiveframework/email/inbox.py new file mode 100644 index 0000000..e9b501b --- /dev/null +++ b/xhiveframework/email/inbox.py @@ -0,0 +1,132 @@ +import json + +import xhiveframework +from xhiveframework.client import set_value + + +def get_email_accounts(user=None): + if not user: + user = xhiveframework.session.user + + email_accounts = [] + + accounts = xhiveframework.get_all( + "User Email", + filters={"parent": user}, + fields=["email_account", "email_id", "enable_outgoing"], + distinct=True, + order_by="idx", + ) + + if not accounts: + return {"email_accounts": [], "all_accounts": ""} + + all_accounts = ",".join(account.get("email_account") for account in accounts) + if len(accounts) > 1: + email_accounts.append({"email_account": all_accounts, "email_id": "All Accounts"}) + email_accounts.extend(accounts) + + email_accounts.extend( + [ + {"email_account": "Sent", "email_id": "Sent Mail"}, + {"email_account": "Spam", "email_id": "Spam"}, + {"email_account": "Trash", "email_id": "Trash"}, + ] + ) + + return {"email_accounts": email_accounts, "all_accounts": all_accounts} + + +@xhiveframework.whitelist() +def create_email_flag_queue(names, action): + """create email flag queue to mark email either as read or unread""" + + def mark_as_seen_unseen(name, action): + doc = xhiveframework.get_doc("Communication", name) + if action == "Read": + doc.add_seen() + else: + _seen = json.loads(doc._seen or "[]") + _seen = [user for user in _seen if xhiveframework.session.user != user] + doc.db_set("_seen", json.dumps(_seen), update_modified=False) + + if not all([names, action]): + return + + for name in json.loads(names or []): + uid, seen_status, email_account = xhiveframework.db.get_value( + "Communication", name, ["ifnull(uid, -1)", "ifnull(seen, 0)", "email_account"] + ) + + # can not mark email SEEN or UNSEEN without uid + if not uid or uid == -1: + continue + + seen = 1 if action == "Read" else 0 + # check if states are correct + if (action == "Read" and seen_status == 0) or (action == "Unread" and seen_status == 1): + create_new = True + email_flag_queue = xhiveframework.db.sql( + """select name, action from `tabEmail Flag Queue` + where communication = %(name)s and is_completed=0""", + {"name": name}, + as_dict=True, + ) + + for queue in email_flag_queue: + if queue.action != action: + xhiveframework.delete_doc("Email Flag Queue", queue.name, ignore_permissions=True) + elif queue.action == action: + # Read or Unread request for email is already available + create_new = False + + if create_new: + flag_queue = xhiveframework.get_doc( + { + "uid": uid, + "action": action, + "communication": name, + "doctype": "Email Flag Queue", + "email_account": email_account, + } + ) + flag_queue.save(ignore_permissions=True) + xhiveframework.db.set_value("Communication", name, "seen", seen, update_modified=False) + mark_as_seen_unseen(name, action) + + +@xhiveframework.whitelist() +def mark_as_closed_open(communication: str, status: str): + """Set status to open or close""" + set_value("Communication", communication, "status", status) + + +@xhiveframework.whitelist() +def move_email(communication: str, email_account: str): + """Move email to another email account.""" + set_value("Communication", communication, "email_account", email_account) + + +@xhiveframework.whitelist() +def mark_as_trash(communication: str): + """Set email status to trash.""" + set_value("Communication", communication, "email_status", "Trash") + + +@xhiveframework.whitelist() +def mark_as_spam(communication: str, sender: str): + """Set email status to spam.""" + email_rule = xhiveframework.db.get_value("Email Rule", {"email_id": sender}) + if not email_rule: + xhiveframework.get_doc({"doctype": "Email Rule", "email_id": sender, "is_spam": 1}).insert( + ignore_permissions=True + ) + set_value("Communication", communication, "email_status", "Spam") + + +def link_communication_to_document(doc, reference_doctype, reference_name, ignore_communication_links): + if not ignore_communication_links: + doc.reference_doctype = reference_doctype + doc.reference_name = reference_name + doc.status = "Linked" + doc.save(ignore_permissions=True) diff --git a/xhiveframework/email/oauth.py b/xhiveframework/email/oauth.py new file mode 100644 index 0000000..c6b45f5 --- /dev/null +++ b/xhiveframework/email/oauth.py @@ -0,0 +1,74 @@ +import base64 +from imaplib import IMAP4 +from poplib import POP3 +from smtplib import SMTP + +import xhiveframework + + +class Oauth: + def __init__( + self, + conn: IMAP4 | POP3 | SMTP, + email_account: str, + email: str, + access_token: str, + mechanism: str = "XOAUTH2", + ) -> None: + self.email_account = email_account + self.email = email + self._mechanism = mechanism + self._conn = conn + self._access_token = access_token + + self._validate() + + def _validate(self) -> None: + if not self._access_token: + xhiveframework.throw( + xhiveframework._("Please Authorize OAuth for Email Account {}").format(self.email_account), + title=xhiveframework._("OAuth Error"), + ) + + @property + def _auth_string(self) -> str: + return f"user={self.email}\1auth=Bearer {self._access_token}\1\1" + + def connect(self) -> None: + try: + if isinstance(self._conn, POP3): + self._connect_pop() + + elif isinstance(self._conn, IMAP4): + self._connect_imap() + + else: + # SMTP + self._connect_smtp() + + except Exception: + xhiveframework.log_error( + "Email Connection Error - Authentication Failed", + reference_doctype="Email Account", + reference_name=self.email_account, + ) + # raising a bare exception here as we have a lot of exception handling present + # where the connect method is called from - hence just logging and raising. + raise + + def _connect_pop(self) -> None: + # NOTE: poplib doesn't have AUTH command implementation + res = self._conn._shortcmd( + "AUTH {} {}".format( + self._mechanism, base64.b64encode(bytes(self._auth_string, "utf-8")).decode("utf-8") + ) + ) + + if not res.startswith(b"+OK"): + raise + + def _connect_imap(self) -> None: + self._conn.authenticate(self._mechanism, lambda x: self._auth_string) + + def _connect_smtp(self) -> None: + self._conn.auth(self._mechanism, lambda x: self._auth_string, initial_response_ok=False) diff --git a/xhiveframework/email/page/__init__.py b/xhiveframework/email/page/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/email/queue.py b/xhiveframework/email/queue.py new file mode 100755 index 0000000..5c89648 --- /dev/null +++ b/xhiveframework/email/queue.py @@ -0,0 +1,179 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _, msgprint +from xhiveframework.utils import cint, cstr, get_url, now_datetime +from xhiveframework.utils.data import getdate +from xhiveframework.utils.verified_command import get_signed_params, verify_request + +# After this percent of failures in every batch, entire batch is aborted. +# This usually indicates a systemic failure so we shouldn't keep trying to send emails. +EMAIL_QUEUE_BATCH_FAILURE_THRESHOLD_PERCENT = 0.33 +EMAIL_QUEUE_BATCH_FAILURE_THRESHOLD_COUNT = 10 + + +def get_emails_sent_this_month(email_account=None): + """Get count of emails sent from a specific email account. + + :param email_account: name of the email account used to send mail + + if email_account=None, email account filter is not applied while counting + """ + today = getdate() + month_start = today.replace(day=1) + + filters = { + "status": "Sent", + "creation": [">=", str(month_start)], + } + if email_account: + filters["email_account"] = email_account + + return xhiveframework.db.count("Email Queue", filters=filters) + + +def get_emails_sent_today(email_account=None): + """Get count of emails sent from a specific email account. + + :param email_account: name of the email account used to send mail + + if email_account=None, email account filter is not applied while counting + """ + q = """ + SELECT + COUNT(`name`) + FROM + `tabEmail Queue` + WHERE + `status` in ('Sent', 'Not Sent', 'Sending') + AND + `creation` > (NOW() - INTERVAL '24' HOUR) + """ + + q_args = {} + if email_account is not None: + if email_account: + q += " AND email_account = %(email_account)s" + q_args["email_account"] = email_account + else: + q += " AND (email_account is null OR email_account='')" + + return xhiveframework.db.sql(q, q_args)[0][0] + + +def get_unsubscribe_message(unsubscribe_message: str, expose_recipients: str) -> "xhiveframework._dict[str, str]": + unsubscribe_message = unsubscribe_message or _("Unsubscribe") + unsubscribe_link = f'{unsubscribe_message}' + unsubscribe_html = _("{0} to stop receiving emails of this type").format(unsubscribe_link) + html = f"""""" + + text = f"\n\n{unsubscribe_message}: \n" + if expose_recipients == "footer": + text = f"\n{text}" + + return xhiveframework._dict(html=html, text=text) + + +def get_unsubcribed_url(reference_doctype, reference_name, email, unsubscribe_method, unsubscribe_params): + params = { + "email": cstr(email), + "doctype": cstr(reference_doctype), + "name": cstr(reference_name), + } + if unsubscribe_params: + params.update(unsubscribe_params) + + return get_url(unsubscribe_method + "?" + get_signed_params(params)) + + +@xhiveframework.whitelist(allow_guest=True) +def unsubscribe(doctype, name, email): + # unsubsribe from comments and communications + if not xhiveframework.flags.in_test and not verify_request(): + return + + try: + xhiveframework.get_doc( + { + "doctype": "Email Unsubscribe", + "email": email, + "reference_doctype": doctype, + "reference_name": name, + } + ).insert(ignore_permissions=True) + + except xhiveframework.DuplicateEntryError: + xhiveframework.db.rollback() + + else: + xhiveframework.db.commit() + + return_unsubscribed_page(email, doctype, name) + + +def return_unsubscribed_page(email, doctype, name): + xhiveframework.respond_as_web_page( + _("Unsubscribed"), + _("{0} has left the conversation in {1} {2}").format(email, _(doctype), name), + indicator_color="green", + ) + + +def flush(): + """flush email queue, every time: called from scheduler. + + This should not be called outside of background jobs. + """ + from xhiveframework.email.doctype.email_queue.email_queue import EmailQueue + + # To avoid running jobs inside unit tests + if xhiveframework.are_emails_muted(): + msgprint(_("Emails are muted")) + + if cint(xhiveframework.db.get_default("suspend_email_queue")) == 1: + return + + email_queue_batch = get_queue() + if not email_queue_batch: + return + + failed_email_queues = [] + for row in email_queue_batch: + try: + email_queue: EmailQueue = xhiveframework.get_doc("Email Queue", row.name) + email_queue.send() + except Exception: + xhiveframework.get_doc("Email Queue", row.name).log_error() + failed_email_queues.append(row.name) + + if ( + len(failed_email_queues) / len(email_queue_batch) + > EMAIL_QUEUE_BATCH_FAILURE_THRESHOLD_PERCENT + and len(failed_email_queues) > EMAIL_QUEUE_BATCH_FAILURE_THRESHOLD_COUNT + ): + xhiveframework.throw(_("Email Queue flushing aborted due to too many failures.")) + + +def get_queue(): + batch_size = cint(xhiveframework.conf.email_queue_batch_size) or 500 + + return xhiveframework.db.sql( + f"""select + name, sender + from + `tabEmail Queue` + where + (status='Not Sent' or status='Partially Sent') and + (send_after is null or send_after < %(now)s) + order + by priority desc, retry asc, creation asc + limit {batch_size}""", + {"now": now_datetime()}, + as_dict=True, + ) diff --git a/xhiveframework/email/receive.py b/xhiveframework/email/receive.py new file mode 100644 index 0000000..13ee3cb --- /dev/null +++ b/xhiveframework/email/receive.py @@ -0,0 +1,898 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import datetime +import email +import email.utils +import imaplib +import json +import poplib +import re +import ssl +import time +from contextlib import suppress +from email.header import decode_header + +import _socket +import chardet +from email_reply_parser import EmailReplyParser + +import xhiveframework +from xhiveframework import _, safe_decode, safe_encode +from xhiveframework.core.doctype.file.exceptions import MaxFileSizeReachedError +from xhiveframework.core.doctype.file.utils import get_random_filename +from xhiveframework.email.oauth import Oauth +from xhiveframework.utils import ( + add_days, + cint, + convert_utc_to_system_timezone, + cstr, + extract_email_id, + get_datetime, + get_string_between, + markdown, + now, + parse_addr, + sanitize_html, + strip, +) +from xhiveframework.utils.html_utils import clean_email_html +from xhiveframework.utils.user import is_system_user + +# fix due to a python bug in poplib that limits it to 2048 +poplib._MAXLINE = 1_00_000 + +THREAD_ID_PATTERN = re.compile(r"(?<=\[)[\w/-]+") +WORDS_PATTERN = re.compile(r"\w+") + + +class EmailSizeExceededError(xhiveframework.ValidationError): + pass + + +class LoginLimitExceeded(xhiveframework.ValidationError): + pass + + +class SentEmailInInboxError(Exception): + pass + + +class EmailServer: + """Wrapper for POP server to pull emails.""" + + def __init__(self, args=None): + self.settings = args or xhiveframework._dict() + + def connect(self): + """Connect to **Email Account**.""" + return self.connect_imap() if cint(self.settings.use_imap) else self.connect_pop() + + def connect_imap(self): + """Connect to IMAP""" + try: + if cint(self.settings.use_ssl): + self.imap = imaplib.IMAP4_SSL( + self.settings.host, + self.settings.incoming_port, + timeout=xhiveframework.conf.pop_timeout, + ssl_context=ssl.create_default_context(), + ) + else: + self.imap = imaplib.IMAP4( + self.settings.host, self.settings.incoming_port, timeout=xhiveframework.conf.pop_timeout + ) + + if cint(self.settings.use_starttls): + self.imap.starttls() + + if self.settings.use_oauth: + Oauth( + self.imap, + self.settings.email_account, + self.settings.username, + self.settings.access_token, + ).connect() + + else: + self.imap.login(self.settings.username, self.settings.password) + + # connection established! + return True + + except _socket.error: + # Invalid mail server -- due to refusing connection + xhiveframework.msgprint(_("Invalid Mail Server. Please rectify and try again.")) + raise + + def connect_pop(self): + # this method return pop connection + try: + if cint(self.settings.use_ssl): + self.pop = poplib.POP3_SSL( + self.settings.host, + self.settings.incoming_port, + timeout=xhiveframework.conf.pop_timeout, + context=ssl.create_default_context(), + ) + else: + self.pop = poplib.POP3( + self.settings.host, self.settings.incoming_port, timeout=xhiveframework.conf.pop_timeout + ) + + if self.settings.use_oauth: + Oauth( + self.pop, + self.settings.email_account, + self.settings.username, + self.settings.access_token, + ).connect() + + else: + self.pop.user(self.settings.username) + self.pop.pass_(self.settings.password) + + # connection established! + return True + + except _socket.error: + xhiveframework.log_error("POP: Unable to connect") + + # Invalid mail server -- due to refusing connection + xhiveframework.msgprint(_("Invalid Mail Server. Please rectify and try again.")) + raise + + except poplib.error_proto as e: + if self.is_temporary_system_problem(e): + return False + + else: + xhiveframework.msgprint(_("Invalid User Name or Support Password. Please rectify and try again.")) + raise + + def select_imap_folder(self, folder): + res = self.imap.select(f'"{folder}"') + return res[0] == "OK" # The folder exsits TODO: handle other resoponses too + + def logout(self): + if cint(self.settings.use_imap): + self.imap.logout() + else: + self.pop.quit() + return + + def get_messages(self, folder="INBOX"): + """Returns new email messages.""" + + self.latest_messages = [] + self.seen_status = {} + self.uid_reindexed = False + + email_list = self.get_new_mails(folder) + + for i, uid in enumerate(email_list[:100]): + try: + self.retrieve_message(uid, i + 1) + except (_socket.timeout, LoginLimitExceeded): + # get whatever messages were retrieved + break + + out = {"latest_messages": self.latest_messages} + if self.settings.use_imap: + out.update( + {"uid_list": email_list, "seen_status": self.seen_status, "uid_reindexed": self.uid_reindexed} + ) + + return out + + def get_new_mails(self, folder): + """Return list of new mails""" + email_list = [] + if cint(self.settings.use_imap): + self.check_imap_uidvalidity(folder) + + readonly = False if self.settings.email_sync_rule == "UNSEEN" else True + + self.imap.select(folder, readonly=readonly) + response, message = self.imap.uid("search", None, self.settings.email_sync_rule) + if message[0]: + email_list = message[0].split() + else: + email_list = self.pop.list()[1] + + return email_list + + def check_imap_uidvalidity(self, folder): + # compare the UIDVALIDITY of email account and imap server + uid_validity = self.settings.uid_validity + + response, message = self.imap.status(folder, "(UIDVALIDITY UIDNEXT)") + current_uid_validity = self.parse_imap_response("UIDVALIDITY", message[0]) or 0 + + uidnext = int(self.parse_imap_response("UIDNEXT", message[0]) or "1") + xhiveframework.db.set_value("Email Account", self.settings.email_account, "uidnext", uidnext) + + if not uid_validity or uid_validity != current_uid_validity: + # uidvalidity changed & all email uids are reindexed by server + Communication = xhiveframework.qb.DocType("Communication") + xhiveframework.qb.update(Communication).set(Communication.uid, -1).where( + Communication.communication_medium == "Email" + ).where(Communication.email_account == self.settings.email_account).run() + + if self.settings.use_imap: + # Remove {"} quotes that are added to handle spaces in IMAP Folder names + if folder[0] == folder[-1] == '"': + folder = folder[1:-1] + # new update for the IMAP Folder DocType + IMAPFolder = xhiveframework.qb.DocType("IMAP Folder") + xhiveframework.qb.update(IMAPFolder).set(IMAPFolder.uidvalidity, current_uid_validity).set( + IMAPFolder.uidnext, uidnext + ).where(IMAPFolder.parent == self.settings.email_account_name).where( + IMAPFolder.folder_name == folder + ).run() + else: + EmailAccount = xhiveframework.qb.DocType("Email Account") + xhiveframework.qb.update(EmailAccount).set(EmailAccount.uidvalidity, current_uid_validity).set( + EmailAccount.uidnext, uidnext + ).where(EmailAccount.name == self.settings.email_account_name).run() + + sync_count = 100 if uid_validity else int(self.settings.initial_sync_count) + from_uid = 1 if uidnext < (sync_count + 1) or (uidnext - sync_count) < 1 else uidnext - sync_count + # sync last 100 email + self.settings.email_sync_rule = f"UID {from_uid}:{uidnext}" + self.uid_reindexed = True + + def parse_imap_response(self, cmd, response): + pattern = rf"(?<={cmd} )[0-9]*" + match = re.search(pattern, response.decode("utf-8"), re.U | re.I) + + if match: + return match.group(0) + else: + return None + + def retrieve_message(self, uid, msg_num): + try: + if cint(self.settings.use_imap): + status, message = self.imap.uid("fetch", uid, "(BODY.PEEK[] BODY.PEEK[HEADER] FLAGS)") + raw = message[0] + + self.get_email_seen_status(uid, raw[0]) + self.latest_messages.append(raw[1]) + else: + msg = self.pop.retr(msg_num) + self.latest_messages.append(b"\n".join(msg[1])) + except _socket.timeout: + # propagate this error to break the loop + raise + + except Exception as e: + if self.has_login_limit_exceeded(e): + raise LoginLimitExceeded(e) + + xhiveframework.log_error("Unable to fetch email", self.make_error_msg(uid, msg_num)) + + self._post_retrieve_cleanup(uid, msg_num) + + def get_email_seen_status(self, uid, flag_string): + """parse the email FLAGS response""" + if not flag_string: + return None + + flags = [] + for flag in imaplib.ParseFlags(flag_string) or []: + match = WORDS_PATTERN.search(xhiveframework.as_unicode(flag)) + flags.append(match.group(0)) + + if "Seen" in flags: + self.seen_status.update({uid: "SEEN"}) + else: + self.seen_status.update({uid: "UNSEEN"}) + + def has_login_limit_exceeded(self, e): + return "-ERR Exceeded the login limit" in strip(cstr(e)) + + def _post_retrieve_cleanup(self, uid, msg_num): + with suppress(Exception): + if not cint(self.settings.use_imap): + self.pop.dele(msg_num) + else: + # mark as seen if email sync rule is UNSEEN (syncing only unseen mails) + if self.settings.email_sync_rule == "UNSEEN": + self.imap.uid("STORE", uid, "+FLAGS", "(\\SEEN)") + + def is_temporary_system_problem(self, e): + messages = ( + "-ERR [SYS/TEMP] Temporary system problem. Please try again later.", + "Connection timed out", + ) + for message in messages: + if message in strip(cstr(e)) or message in strip(cstr(getattr(e, "strerror", ""))): + return True + return False + + def make_error_msg(self, uid, msg_num): + partial_mail = None + traceback = xhiveframework.get_traceback(with_context=True) + with suppress(Exception): + # retrieve headers + if not cint(self.settings.use_imap): + headers = b"\n".join(self.pop.top(msg_num, 5)[1]) + else: + headers = self.imap.uid("fetch", uid, "(BODY.PEEK[HEADER])")[1][0][1] + + partial_mail = Email(headers) + + if partial_mail: + return ( + "\nDate: {date}\nFrom: {from_email}\nSubject: {subject}\n\n\nTraceback: \n{traceback}".format( + date=partial_mail.date, + from_email=partial_mail.from_email, + subject=partial_mail.subject, + traceback=traceback, + ) + ) + return traceback + + def update_flag(self, folder, uid_list=None): + """set all uids mails the flag as seen""" + if not uid_list: + return + + if not self.connect(): + return + + self.imap.select(folder) + for uid, operation in uid_list.items(): + if not uid: + continue + + op = "+FLAGS" if operation == "Read" else "-FLAGS" + try: + self.imap.uid("STORE", uid, op, "(\\SEEN)") + except Exception: + continue + + +class Email: + """Wrapper for an email.""" + + def __init__(self, content): + """Parses headers, content, attachments from given raw message. + + :param content: Raw message.""" + if isinstance(content, bytes): + self.mail = email.message_from_bytes(content) + else: + self.mail = email.message_from_string(content) + + self.raw_message = content + self.text_content = "" + self.html_content = "" + self.attachments = [] + self.cid_map = {} + self.parse() + self.set_content_and_type() + self.set_subject() + self.set_from() + + message_id = self.mail.get("Message-ID") or "" + self.message_id = get_string_between("<", message_id, ">") + + if self.mail["Date"]: + try: + utc = email.utils.mktime_tz(email.utils.parsedate_tz(self.mail["Date"])) + utc_dt = datetime.datetime.utcfromtimestamp(utc) + self.date = convert_utc_to_system_timezone(utc_dt).strftime("%Y-%m-%d %H:%M:%S") + except Exception: + self.date = now() + else: + self.date = now() + if self.date > now(): + self.date = now() + + @property + def in_reply_to(self): + in_reply_to = self.mail.get("In-Reply-To") or "" + return get_string_between("<", in_reply_to, ">") + + def parse(self): + """Walk and process multi-part email.""" + for part in self.mail.walk(): + self.process_part(part) + + def set_subject(self): + """Parse and decode `Subject` header.""" + _subject = decode_header(self.mail.get("Subject", "No Subject")) + self.subject = _subject[0][0] or "" + + if _subject[0][1]: + # Encoding is known by decode_header (might also be unknown-8bit) + self.subject = safe_decode(self.subject, _subject[0][1]) + + if isinstance(self.subject, bytes): + # Fall back to utf-8 if the charset is unknown or decoding fails + # Replace invalid characters with '' + self.subject = self.subject.decode("utf-8", "replace") + + # Convert non-string (e.g. None) + # Truncate to 140 chars (can be used as a document name) + self.subject = str(self.subject).strip()[:140] + + if not self.subject: + self.subject = "No Subject" + + def set_from(self): + # gmail mailing-list compatibility + # use X-Original-Sender if available, as gmail sometimes modifies the 'From' + _from_email = self.decode_email(self.mail.get("X-Original-From") or self.mail["From"]) + _reply_to = self.decode_email(self.mail.get("Reply-To")) + + if _reply_to and not xhiveframework.db.get_value("Email Account", {"email_id": _reply_to}, "email_id"): + self.from_email = extract_email_id(_reply_to) + else: + self.from_email = extract_email_id(_from_email) + + if self.from_email: + self.from_email = self.from_email.lower() + + self.from_real_name = parse_addr(_from_email)[0] if "@" in _from_email else _from_email + + @staticmethod + def decode_email(email): + if not email: + return + decoded = "" + for part, encoding in decode_header(xhiveframework.as_unicode(email).replace('"', " ").replace("'", " ")): + if encoding: + decoded += part.decode(encoding, "replace") + else: + decoded += safe_decode(part) + return decoded + + def set_content_and_type(self): + self.content, self.content_type = "[Blank Email]", "text/plain" + if self.html_content: + self.content, self.content_type = self.html_content, "text/html" + else: + self.content, self.content_type = ( + EmailReplyParser.read(self.text_content).text.replace("\n", "\n\n"), + "text/plain", + ) + + def process_part(self, part): + """Parse email `part` and set it to `text_content`, `html_content` or `attachments`.""" + content_type = part.get_content_type() + if content_type == "text/plain": + self.text_content += self.get_payload(part) + + # attach txt file from received email as well aside from saving to text_content if it has filename + if part.get_filename(): + self.get_attachment(part) + + elif content_type == "text/html": + self.html_content += self.get_payload(part) + + elif content_type == "message/rfc822": + # sent by outlook when another email is sent as an attachment to this email + self.show_attached_email_headers_in_content(part) + + elif part.get_filename() or "image" in content_type: + self.get_attachment(part) + + def show_attached_email_headers_in_content(self, part): + # get the multipart/alternative message + from html import escape + + message = list(part.walk())[1] + headers = [] + for key in ("From", "To", "Subject", "Date"): + value = cstr(message.get(key)) + if value: + headers.append(f"{_(key)}: {escape(value)}") + + self.text_content += "\n".join(headers) + self.html_content += "
      " + "\n".join(f"

      {h}

      " for h in headers) + + if not message.is_multipart() and message.get_content_type() == "text/plain": + # email.parser didn't parse it! + text_content = self.get_payload(message) + self.text_content += text_content + self.html_content += markdown(text_content) + + def get_charset(self, part): + """Detect charset.""" + charset = part.get_content_charset() + if not charset: + charset = chardet.detect(safe_encode(cstr(part)))["encoding"] + + return charset + + def get_payload(self, part): + charset = self.get_charset(part) + + try: + return str(part.get_payload(decode=True), str(charset), "ignore") + except LookupError: + return part.get_payload() + + def get_attachment(self, part): + # charset = self.get_charset(part) + fcontent = part.get_payload(decode=True) + + if fcontent: + content_type = part.get_content_type() + fname = part.get_filename() + if fname: + try: + fname = fname.replace("\n", " ").replace("\r", "") + fname = cstr(decode_header(fname)[0][0]) + except Exception: + fname = get_random_filename(content_type=content_type) + else: + fname = get_random_filename(content_type=content_type) + # Don't clobber existing filename + while fname in self.cid_map: + fname = get_random_filename(content_type=content_type) + + self.attachments.append( + { + "content_type": content_type, + "fname": fname, + "fcontent": fcontent, + } + ) + + cid = (cstr(part.get("Content-Id")) or "").strip("><") + if cid: + self.cid_map[fname] = cid + + def save_attachments_in_doc(self, doc): + """Save email attachments in given document.""" + saved_attachments = [] + + for attachment in self.attachments: + try: + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_name": attachment["fname"], + "attached_to_doctype": doc.doctype, + "attached_to_name": doc.name, + "is_private": 1, + "content": attachment["fcontent"], + } + ) + _file.save() + saved_attachments.append(_file) + + if attachment["fname"] in self.cid_map: + self.cid_map[_file.name] = self.cid_map[attachment["fname"]] + + except MaxFileSizeReachedError: + # WARNING: bypass max file size exception + pass + except xhiveframework.FileAlreadyAttachedException: + pass + except xhiveframework.DuplicateEntryError: + # same file attached twice?? + pass + + return saved_attachments + + def get_thread_id(self): + """Extract thread ID from `[]`""" + l = THREAD_ID_PATTERN.findall(self.subject) + return l and l[0] or None + + def is_reply(self): + return bool(self.in_reply_to) + + +class InboundMail(Email): + """Class representation of incoming mail along with mail handlers.""" + + def __init__(self, content, email_account, uid=None, seen_status=None, append_to=None): + super().__init__(content) + self.email_account = email_account + self.uid = uid or -1 + self.append_to = append_to + self.seen_status = seen_status or 0 + + # System documents related to this mail + self._parent_email_queue = None + self._parent_communication = None + self._reference_document = None + + self.flags = xhiveframework._dict() + + def get_content(self): + if self.content_type == "text/html": + return clean_email_html(self.content) + + def process(self): + """Create communication record from email.""" + if self.is_sender_same_as_receiver() and not self.is_reply(): + if xhiveframework.flags.in_test: + print("WARN: Cannot pull email. Sender same as recipient inbox") + raise SentEmailInInboxError + + communication = self.is_exist_in_system() + if communication: + communication.update_db(uid=self.uid) + communication.reload() + return communication + + self.flags.is_new_communication = True + return self._build_communication_doc() + + def _build_communication_doc(self): + data = self.as_dict() + data["doctype"] = "Communication" + + if self.parent_communication(): + data["in_reply_to"] = self.parent_communication().name + + append_to = self.append_to if self.email_account.use_imap else self.email_account.append_to + + if self.reference_document(): + data["reference_doctype"] = self.reference_document().doctype + data["reference_name"] = self.reference_document().name + elif append_to and append_to != "Communication": + reference_name = self._create_reference_document(append_to) + if reference_name: + data["reference_doctype"] = append_to + data["reference_name"] = reference_name + + if self.is_notification(): + # Disable notifications for notification. + data["unread_notification_sent"] = 1 + + if self.seen_status: + data["_seen"] = json.dumps(self.get_users_linked_to_account(self.email_account)) + + communication = xhiveframework.get_doc(data) + communication.flags.in_receive = True + communication.insert(ignore_permissions=True) + + # Communication might have been modified by some hooks, reload before saving + communication.reload() + + # save attachments + communication._attachments = self.save_attachments_in_doc(communication) + communication.content = sanitize_html(self.replace_inline_images(communication._attachments)) + communication.save() + return communication + + def replace_inline_images(self, attachments): + # replace inline images + content = self.content + for file in attachments: + if self.cid_map.get(file.name): + content = content.replace(f"cid:{self.cid_map[file.name]}", file.unique_url) + return content + + def is_notification(self): + isnotification = self.mail.get("isnotification") + return isnotification and ("notification" in isnotification) + + def is_exist_in_system(self): + """Check if this email already exists in the system(as communication document).""" + from xhiveframework.core.doctype.communication.communication import Communication + + if not self.message_id: + return + + return Communication.find_one_by_filters(message_id=self.message_id, order_by="creation DESC") + + def is_sender_same_as_receiver(self): + return self.from_email == self.email_account.email_id + + def is_reply_to_system_sent_mail(self): + """Is it a reply to already sent mail.""" + return self.is_reply() and xhiveframework.local.site in self.in_reply_to + + def parent_email_queue(self): + """Get parent record from `Email Queue`. + + If it is a reply to already sent mail, then there will be a parent record in EMail Queue. + """ + from xhiveframework.email.doctype.email_queue.email_queue import EmailQueue + + if self._parent_email_queue is not None: + return self._parent_email_queue + + parent_email_queue = "" + if self.is_reply_to_system_sent_mail(): + parent_email_queue = EmailQueue.find_one_by_filters(message_id=self.in_reply_to) + + self._parent_email_queue = parent_email_queue or "" + return self._parent_email_queue + + def parent_communication(self): + """Find a related communication so that we can prepare a mail thread. + + The way it happens is by using in-reply-to header, and we can't make thread if it does not exist. + + Here are the cases to handle: + 1. If mail is a reply to already sent mail, then we can get parent communicaion from + Email Queue record or message_id on communication. + 2. Sometimes we send communication name in message-ID directly, use that to get parent communication. + 3. Sender sent a reply but reply is on top of what (s)he sent before, + then parent record exists directly in communication. + """ + from xhiveframework.core.doctype.communication.communication import Communication + + if self._parent_communication is not None: + return self._parent_communication + + if not self.is_reply(): + return "" + + communication = Communication.find_one_by_filters(message_id=self.in_reply_to) + if not communication: + if self.parent_email_queue() and self.parent_email_queue().communication: + communication = Communication.find(self.parent_email_queue().communication, ignore_error=True) + else: + reference = self.in_reply_to + if "@" in self.in_reply_to: + reference, _ = self.in_reply_to.split("@", 1) + communication = Communication.find(reference, ignore_error=True) + + self._parent_communication = communication or "" + return self._parent_communication + + def reference_document(self): + """Reference document is a document to which mail relate to. + + We can get reference document from Parent record(EmailQueue | Communication) if exists. + Otherwise we do subject match to find reference document if we know the reference(append_to) doctype. + """ + if self._reference_document is not None: + return self._reference_document + + reference_document = "" + parent = self.parent_email_queue() or self.parent_communication() + + if parent and parent.reference_doctype: + reference_doctype, reference_name = parent.reference_doctype, parent.reference_name + reference_document = self.get_doc(reference_doctype, reference_name, ignore_error=True) + + if not reference_document and self.email_account.append_to: + reference_document = self.match_record_by_subject_and_sender(self.email_account.append_to) + + self._reference_document = reference_document or "" + return self._reference_document + + def get_reference_name_from_subject(self): + """ + Ex: "Re: Your email (#OPP-2020-2334343)" + """ + return self.subject.rsplit("#", 1)[-1].strip(" ()") + + def match_record_by_subject_and_sender(self, doctype): + """Find a record in the given doctype that matches with email subject and sender. + + Cases: + 1. Sometimes record name is part of subject. We can get document by parsing name from subject + 2. Find by matching sender and subject + 3. Find by matching subject alone (Special case) + Ex: when a System User is using Outlook and replies to an email from their own client, + it reaches the Email Account with the threading info lost and the (sender + subject match) + doesn't work because the sender in the first communication was someone different to whom + the system user is replying to via the common email account in XhiveFramework. This fix bypasses + the sender match when the sender is a system user and subject is atleast 10 chars long + (for additional safety) + + NOTE: We consider not to match by subject if match record is very old. + """ + name = self.get_reference_name_from_subject() + email_fields = self.get_email_fields(doctype) + + record = self.get_doc(doctype, name, ignore_error=True) if name else None + + if not record: + subject = self.clean_subject(self.subject) + filters = { + email_fields.subject_field: ("like", f"%{subject}%"), + "creation": (">", self.get_relative_dt(days=-60)), + } + + # Sender check is not needed incase mail is from system user. + if not (len(subject) > 10 and is_system_user(self.from_email)): + filters[email_fields.sender_field] = self.from_email + + name = xhiveframework.db.get_value(self.email_account.append_to, filters=filters) + record = self.get_doc(doctype, name, ignore_error=True) if name else None + return record + + def _create_reference_document(self, doctype): + """Create reference document if it does not exist in the system.""" + parent = xhiveframework.new_doc(doctype) + email_fields = self.get_email_fields(doctype) + + if email_fields.subject_field: + parent.set(email_fields.subject_field, xhiveframework.as_unicode(self.subject)[:140]) + + if email_fields.sender_field: + parent.set(email_fields.sender_field, xhiveframework.as_unicode(self.from_email)) + + if email_fields.sender_name_field: + parent.set(email_fields.sender_name_field, xhiveframework.as_unicode(self.from_real_name)) + + parent.flags.ignore_mandatory = True + + try: + parent.insert(ignore_permissions=True) + return parent.name + except xhiveframework.DuplicateEntryError: + # try and find matching parent + return xhiveframework.db.get_value(doctype, {email_fields.sender_field: self.from_email}) + + @staticmethod + def get_doc(doctype, docname, ignore_error=False): + try: + return xhiveframework.get_doc(doctype, docname) + except xhiveframework.DoesNotExistError: + if ignore_error: + return + raise + + @staticmethod + def get_relative_dt(days): + """Get relative to current datetime. Only relative days are supported.""" + return add_days(get_datetime(), days) + + @staticmethod + def get_users_linked_to_account(email_account): + """Get list of users who linked to Email account.""" + users = xhiveframework.get_all("User Email", filters={"email_account": email_account.name}, fields=["parent"]) + return list({user.get("parent") for user in users}) + + @staticmethod + def clean_subject(subject): + """Remove Prefixes like 'fw', FWD', 're' etc from subject.""" + # Match strings like "fw:", "re :" etc. + regex = r"(^\s*(fw|fwd|wg)[^:]*:|\s*(re|aw)[^:]*:\s*)*" + return xhiveframework.as_unicode(strip(re.sub(regex, "", subject, count=0, flags=re.IGNORECASE))) + + @staticmethod + def get_email_fields(doctype): + """Returns Email related fields of a doctype.""" + fields = xhiveframework._dict() + + email_fields = ["subject_field", "sender_field", "sender_name_field"] + meta = xhiveframework.get_meta(doctype) + + for field in email_fields: + if hasattr(meta, field): + fields[field] = getattr(meta, field) + return fields + + @staticmethod + def get_document(self, doctype, name): + """Is same as xhiveframework.get_doc but suppresses the DoesNotExist error.""" + try: + return xhiveframework.get_doc(doctype, name) + except xhiveframework.DoesNotExistError: + return None + + def as_dict(self): + """ """ + return { + "subject": self.subject, + "content": self.get_content(), + "text_content": self.text_content, + "sent_or_received": "Received", + "sender_full_name": self.from_real_name, + "sender": self.from_email, + "recipients": self.mail.get("To"), + "cc": self.mail.get("CC"), + "email_account": self.email_account.name, + "communication_medium": "Email", + "uid": self.uid, + "message_id": self.message_id, + "communication_date": self.date, + "has_attachment": 1 if self.attachments else 0, + "seen": self.seen_status or 0, + } diff --git a/xhiveframework/email/smtp.py b/xhiveframework/email/smtp.py new file mode 100644 index 0000000..7a0c3d1 --- /dev/null +++ b/xhiveframework/email/smtp.py @@ -0,0 +1,136 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import smtplib +from contextlib import suppress + +import xhiveframework +from xhiveframework import _ +from xhiveframework.email.oauth import Oauth +from xhiveframework.utils import cint, cstr + + +class InvalidEmailCredentials(xhiveframework.ValidationError): + pass + + +class SMTPServer: + def __init__( + self, + server, + login=None, + email_account=None, + password=None, + port=None, + use_tls=None, + use_ssl=None, + use_oauth=0, + access_token=None, + ): + self.login = login + self.email_account = email_account + self.password = password + self._server = server + self._port = port + self.use_tls = use_tls + self.use_ssl = use_ssl + self.use_oauth = use_oauth + self.access_token = access_token + self._session = None + + if not self.server: + xhiveframework.msgprint( + _("Email Account not setup. Please create a new Email Account from Settings > Email Account"), + raise_exception=xhiveframework.OutgoingEmailError, + ) + + @property + def port(self): + port = self._port or (self.use_ssl and 465) or (self.use_tls and 587) + return cint(port) + + @property + def server(self): + return cstr(self._server or "") + + def secure_session(self, conn): + """Secure the connection incase of TLS.""" + if self.use_tls: + conn.ehlo() + conn.starttls() + conn.ehlo() + + @property + def session(self): + """Get SMTP session. + + We make best effort to revive connection if it's disconnected by checking the connection + health before returning it to user.""" + if self.is_session_active(): + return self._session + + SMTP = smtplib.SMTP_SSL if self.use_ssl else smtplib.SMTP + + try: + _session = SMTP(self.server, self.port, timeout=2 * 60) + if not _session: + xhiveframework.msgprint( + _("Could not connect to outgoing email server"), raise_exception=xhiveframework.OutgoingEmailError + ) + + self.secure_session(_session) + + if self.use_oauth: + Oauth(_session, self.email_account, self.login, self.access_token).connect() + + elif self.password: + res = _session.login(str(self.login or ""), str(self.password or "")) + + # check if logged correctly + if res[0] != 235: + xhiveframework.msgprint(res[1], raise_exception=xhiveframework.OutgoingEmailError) + + self._session = _session + self._enqueue_connection_closure() + return self._session + + except smtplib.SMTPAuthenticationError: + self.throw_invalid_credentials_exception() + + except OSError as e: + # Invalid mail server -- due to refusing connection + xhiveframework.throw( + _("Invalid Outgoing Mail Server or Port: {0}").format(str(e)), + title=_("Incorrect Configuration"), + ) + + def _enqueue_connection_closure(self): + if xhiveframework.request and hasattr(xhiveframework.request, "after_response"): + xhiveframework.request.after_response.add(self.quit) + elif xhiveframework.job: + xhiveframework.job.after_job.add(self.quit) + else: + # Console? + import atexit + + atexit.register(self.quit) + + def is_session_active(self): + if self._session: + try: + return self._session.noop()[0] == 250 + except Exception: + return False + + def quit(self): + with suppress(TimeoutError): + if self.is_session_active(): + self._session.quit() + + @classmethod + def throw_invalid_credentials_exception(cls): + xhiveframework.throw( + _("Please check your email login credentials."), + title=_("Invalid Credentials"), + exc=InvalidEmailCredentials, + ) diff --git a/xhiveframework/email/test_email_body.py b/xhiveframework/email/test_email_body.py new file mode 100644 index 0000000..1e541a7 --- /dev/null +++ b/xhiveframework/email/test_email_body.py @@ -0,0 +1,207 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import base64 +import os + +import xhiveframework +from xhiveframework import safe_decode +from xhiveframework.email.doctype.email_queue.email_queue import QueueBuilder, SendMailContext +from xhiveframework.email.email_body import ( + get_email, + get_header, + inline_style_in_html, + replace_filename_with_cid, +) +from xhiveframework.email.receive import Email +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestEmailBody(XhiveFrameworkTestCase): + def setUp(self): + email_html = """ +
      +

      Hey John Doe!

      +

      This is embedded image you asked for

      + +
      +""" + email_text = """ +Hey John Doe! +This is the text version of this email +""" + + img_path = os.path.abspath("assets/xhiveframework/images/xhiveframework-favicon.svg") + with open(img_path, "rb") as f: + img_content = f.read() + img_base64 = base64.b64encode(img_content).decode() + + # email body keeps 76 characters on one line + self.img_base64 = fixed_column_width(img_base64, 76) + + self.email_string = ( + get_email( + recipients=["test@example.com"], + sender="me@example.com", + subject="Test Subject", + content=email_html, + text_content=email_text, + ) + .as_string() + .replace("\r\n", "\n") + ) + + def test_prepare_message_returns_already_encoded_string(self): + uni_chr1 = chr(40960) + uni_chr2 = chr(1972) + + QueueBuilder( + recipients=["test@example.com"], + sender="me@example.com", + subject="Test Subject", + message=f"

      {uni_chr1}abcd{uni_chr2}

      ", + text_content="whatever", + ).process() + queue_doc = xhiveframework.get_last_doc("Email Queue") + mail_ctx = SendMailContext(queue_doc=queue_doc) + result = mail_ctx.build_message(recipient_email="test@test.com") + self.assertTrue(b"

      =EA=80=80abcd=DE=B4

      " in result) + + def test_prepare_message_returns_cr_lf(self): + QueueBuilder( + recipients=["test@example.com"], + sender="me@example.com", + subject="Test Subject", + message="

      \n this is a test of newlines\n" + "

      ", + text_content="whatever", + ).process() + queue_doc = xhiveframework.get_last_doc("Email Queue") + mail_ctx = SendMailContext(queue_doc=queue_doc) + result = safe_decode(mail_ctx.build_message(recipient_email="test@test.com")) + + self.assertTrue(result.count("\n") == result.count("\r")) + + def test_image(self): + img_signature = """ +Content-Type: image/svg+xml +MIME-Version: 1.0 +Content-Transfer-Encoding: base64 +Content-Disposition: inline; filename="xhiveframework-favicon.svg" +""" + self.assertTrue(img_signature in self.email_string) + self.assertTrue(self.img_base64 in self.email_string) + + def test_text_content(self): + text_content = """ +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable + + +Hey John Doe! +This is the text version of this email +""" + self.assertTrue(text_content in self.email_string) + + def test_email_content(self): + html_head = """ +Content-Type: text/html; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable + + + +""" + + html = """

      Hey John Doe!

      """ + + self.assertTrue(html_head in self.email_string) + self.assertTrue(html in self.email_string) + + def test_replace_filename_with_cid(self): + original_message = """ +
      + test + +
      + """ + message, inline_images = replace_filename_with_cid(original_message) + + processed_message = """ +
      + test + +
      + """.format(inline_images[0].get("content_id")) + self.assertEqual(message, processed_message) + + def test_inline_styling(self): + html = """ +

      Hi John

      +

      This is a test email

      +""" + transformed_html = """ +

      Hi John

      +

      This is a test email

      +""" + self.assertTrue(transformed_html in inline_style_in_html(html)) + + def test_email_header(self): + email_html = """ +

      Hey John Doe!

      +

      This is embedded image you asked for

      +""" + email_string = get_email( + recipients=["test@example.com"], + sender="me@example.com", + subject="Test Subject\u2028, with line break, \nand Line feed \rand carriage return.", + content=email_html, + header=["Email Title", "green"], + ).as_string() + # REDESIGN-TODO: Add style for indicators in email + self.assertTrue("""""" in email_string) + self.assertTrue("Email Title" in email_string) + self.assertIn( + "Subject: Test Subject, with line break, and Line feed and carriage return.", email_string + ) + + def test_get_email_header(self): + html = get_header(["This is test", "orange"]) + self.assertTrue('' in html) + self.assertTrue("This is test" in html) + + html = get_header(["This is another test"]) + self.assertTrue("This is another test" in html) + + html = get_header("This is string") + self.assertTrue("This is string" in html) + + def test_8bit_utf_8_decoding(self): + text_content_bytes = b"\xed\x95\x9c\xea\xb8\x80\xe1\xa5\xa1\xe2\x95\xa5\xe0\xba\xaa\xe0\xa4\x8f" + text_content = text_content_bytes.decode("utf-8") + + content_bytes = ( + b"""MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Disposition: inline +Content-Transfer-Encoding: 8bit +From: test1_@xhiveerp.com +Reply-To: test2_@xhiveerp.com +""" + + text_content_bytes + ) + + mail = Email(content_bytes) + self.assertEqual(mail.text_content, text_content) + + def test_poorly_encoded_messages(self): + mail = Email.decode_email( + "=?iso-2022-jp?B?VEFLQVlBTUEgS2FvcnUgWxskQnxiOzMbKEIgGyRCNzAbKEJd?=\n\t" + ) + self.assertIn("user@example.com", mail) + + +def fixed_column_width(string, chunk_size): + parts = [string[0 + i : chunk_size + i] for i in range(0, len(string), chunk_size)] + return "\n".join(parts) diff --git a/xhiveframework/email/test_smtp.py b/xhiveframework/email/test_smtp.py new file mode 100644 index 0000000..ec44b0d --- /dev/null +++ b/xhiveframework/email/test_smtp.py @@ -0,0 +1,88 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: The MIT License + +import xhiveframework +from xhiveframework.email.doctype.email_account.email_account import EmailAccount +from xhiveframework.email.smtp import SMTPServer +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSMTP(XhiveFrameworkTestCase): + def test_smtp_ssl_session(self): + for port in [None, 0, 465, "465"]: + make_server(port, 1, 0) + + def test_smtp_tls_session(self): + for port in [None, 0, 587, "587"]: + make_server(port, 0, 1) + + def test_get_email_account(self): + existing_email_accounts = xhiveframework.get_all( + "Email Account", fields=["name", "enable_outgoing", "default_outgoing", "append_to", "use_imap"] + ) + unset_details = {"enable_outgoing": 0, "default_outgoing": 0, "append_to": None, "use_imap": 0} + for email_account in existing_email_accounts: + xhiveframework.db.set_value("Email Account", email_account["name"], unset_details) + + # remove mail_server config so that test@example.com is not created + mail_server = xhiveframework.conf.get("mail_server") + del xhiveframework.conf["mail_server"] + + xhiveframework.local.outgoing_email_account = {} + + xhiveframework.local.outgoing_email_account = {} + # lowest preference given to email account with default incoming enabled + create_email_account( + email_id="default_outgoing_enabled@gmail.com", + password="password", + enable_outgoing=1, + default_outgoing=1, + ) + self.assertEqual(EmailAccount.find_outgoing().email_id, "default_outgoing_enabled@gmail.com") + + xhiveframework.local.outgoing_email_account = {} + # highest preference given to email account with append_to matching + create_email_account( + email_id="append_to@gmail.com", + password="password", + enable_outgoing=1, + default_outgoing=1, + append_to="Blog Post", + ) + self.assertEqual( + EmailAccount.find_outgoing(match_by_doctype="Blog Post").email_id, "append_to@gmail.com" + ) + + # add back the mail_server + xhiveframework.conf["mail_server"] = mail_server + for email_account in existing_email_accounts: + set_details = { + "enable_outgoing": email_account["enable_outgoing"], + "default_outgoing": email_account["default_outgoing"], + "append_to": email_account["append_to"], + } + xhiveframework.db.set_value("Email Account", email_account["name"], set_details) + + +def create_email_account(email_id, password, enable_outgoing, default_outgoing=0, append_to=None): + email_dict = { + "email_id": email_id, + "passsword": password, + "enable_outgoing": enable_outgoing, + "default_outgoing": default_outgoing, + "enable_incoming": 1, + "append_to": append_to, + "is_dummy_password": 1, + "smtp_server": "127.0.0.1", + "use_imap": 0, + } + + email_account = xhiveframework.new_doc("Email Account") + email_account.update(email_dict) + email_account.save() + + +def make_server(port, ssl, tls): + server = SMTPServer(server="smtp.gmail.com", port=port, use_ssl=ssl, use_tls=tls) + + server.session diff --git a/xhiveframework/email/utils.py b/xhiveframework/email/utils.py new file mode 100644 index 0000000..96ad95e --- /dev/null +++ b/xhiveframework/email/utils.py @@ -0,0 +1,18 @@ +# Copyright (c) 2019, XhiveFramework Technologies Pvt. Ltd. and contributors +# License: MIT. See LICENSE + +import imaplib +import poplib + +from xhiveframework.utils import cint + + +def get_port(doc): + if not doc.incoming_port: + if doc.use_imap: + doc.incoming_port = imaplib.IMAP4_SSL_PORT if doc.use_ssl else imaplib.IMAP4_PORT + + else: + doc.incoming_port = poplib.POP3_SSL_PORT if doc.use_ssl else poplib.POP3_PORT + + return cint(doc.incoming_port) diff --git a/xhiveframework/exceptions.py b/xhiveframework/exceptions.py new file mode 100644 index 0000000..b69bf0b --- /dev/null +++ b/xhiveframework/exceptions.py @@ -0,0 +1,301 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# BEWARE don't put anything in this file except exceptions +from werkzeug.exceptions import NotFound + + +class SiteNotSpecifiedError(Exception): + def __init__(self, *args, **kwargs): + self.message = "Please specify --site sitename" + super(Exception, self).__init__(self.message) + + +class UrlSchemeNotSupported(Exception): + pass + + +class ValidationError(Exception): + http_status_code = 417 + + +class XhiveFrameworkTypeError(TypeError): + http_status_code = 417 + + +class AuthenticationError(Exception): + http_status_code = 401 + + +class SessionExpired(Exception): + http_status_code = 401 + + +class PermissionError(Exception): + http_status_code = 403 + + +class DoesNotExistError(ValidationError): + http_status_code = 404 + + +class PageDoesNotExistError(ValidationError): + http_status_code = 404 + + +class NameError(Exception): + http_status_code = 409 + + +class OutgoingEmailError(Exception): + http_status_code = 501 + + +class SessionStopped(Exception): + http_status_code = 503 + + +class UnsupportedMediaType(Exception): + http_status_code = 415 + + +class RequestToken(Exception): + http_status_code = 200 + + +class Redirect(Exception): + http_status_code = 301 + + +class CSRFTokenError(Exception): + http_status_code = 400 + + +class TooManyRequestsError(Exception): + http_status_code = 429 + + +class ImproperDBConfigurationError(Exception): + """ + Used when xhiveframework detects that database or tables are not properly + configured + """ + + def __init__(self, reason, msg=None): + if not msg: + msg = "MariaDb is not properly configured" + super().__init__(msg) + self.reason = reason + + +class DuplicateEntryError(NameError): + pass + + +class DataError(ValidationError): + pass + + +class UnknownDomainError(Exception): + pass + + +class MappingMismatchError(ValidationError): + pass + + +class InvalidStatusError(ValidationError): + pass + + +class MandatoryError(ValidationError): + pass + + +class NonNegativeError(ValidationError): + pass + + +class InvalidSignatureError(ValidationError): + pass + + +class RateLimitExceededError(ValidationError): + pass + + +class CannotChangeConstantError(ValidationError): + pass + + +class CharacterLengthExceededError(ValidationError): + pass + + +class UpdateAfterSubmitError(ValidationError): + pass + + +class LinkValidationError(ValidationError): + pass + + +class CancelledLinkError(LinkValidationError): + pass + + +class DocstatusTransitionError(ValidationError): + pass + + +class TimestampMismatchError(ValidationError): + pass + + +class EmptyTableError(ValidationError): + pass + + +class LinkExistsError(ValidationError): + pass + + +class InvalidEmailAddressError(ValidationError): + pass + + +class InvalidNameError(ValidationError): + pass + + +class InvalidPhoneNumberError(ValidationError): + pass + + +class TemplateNotFoundError(ValidationError): + pass + + +class UniqueValidationError(ValidationError): + pass + + +class AppNotInstalledError(ValidationError): + pass + + +class IncorrectSitePath(NotFound): + pass + + +class ImplicitCommitError(ValidationError): + pass + + +class RetryBackgroundJobError(Exception): + pass + + +class DocumentLockedError(ValidationError): + pass + + +class CircularLinkingError(ValidationError): + pass + + +class SecurityException(Exception): + pass + + +class InvalidColumnName(ValidationError): + pass + + +class IncompatibleApp(ValidationError): + pass + + +class InvalidDates(ValidationError): + pass + + +class DataTooLongException(ValidationError): + pass + + +class FileAlreadyAttachedException(Exception): + pass + + +class DocumentAlreadyRestored(ValidationError): + pass + + +class AttachmentLimitReached(ValidationError): + pass + + +class QueryTimeoutError(Exception): + pass + + +class QueryDeadlockError(Exception): + pass + + +class InReadOnlyMode(ValidationError): + http_status_code = 503 # temporarily not available + + +class SessionBootFailed(ValidationError): + http_status_code = 500 + + +class PrintFormatError(ValidationError): + pass + + +class TooManyWritesError(Exception): + pass + + +# OAuth exceptions +class InvalidAuthorizationHeader(CSRFTokenError): + pass + + +class InvalidAuthorizationPrefix(CSRFTokenError): + pass + + +class InvalidAuthorizationToken(CSRFTokenError): + pass + + +class InvalidDatabaseFile(ValidationError): + pass + + +class ExecutableNotFound(FileNotFoundError): + pass + + +class InvalidRoundingMethod(FileNotFoundError): + pass + + +class InvalidRemoteException(Exception): + pass + + +class LinkExpired(ValidationError): + http_status_code = 410 + title = "Link Expired" + message = "The link has expired" + + +class InvalidKeyError(ValidationError): + http_status_code = 401 + title = "Invalid Key" + message = "The document key is invalid" diff --git a/xhiveframework/geo/__init__.py b/xhiveframework/geo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/geo/country_info.json b/xhiveframework/geo/country_info.json new file mode 100644 index 0000000..8ce03b9 --- /dev/null +++ b/xhiveframework/geo/country_info.json @@ -0,0 +1,3007 @@ +{ + "Afghanistan": { + "code": "af", + "currency": "AFN", + "currency_fraction": "Pul", + "currency_fraction_units": 100, + "currency_symbol": "\u060b", + "number_format": "#,###.##", + "timezones": [ + "Asia/Kabul" + ], + "isd": "+93" + }, + "Albania": { + "code": "al", + "currency": "ALL", + "currency_fraction": "Qindark\u00eb", + "currency_fraction_units": 100, + "currency_name": "Lek", + "currency_symbol": "L", + "number_format": "#,###.##", + "timezones": [ + "Europe/Tirane" + ], + "isd": "+355" + }, + "Algeria": { + "code": "dz", + "currency": "DZD", + "currency_fraction": "Santeem", + "currency_fraction_units": 100, + "currency_name": "Algerian Dinar", + "currency_symbol": "\u062f.\u062c", + "number_format": "#,###.##", + "timezones": [ + "Africa/Algiers" + ], + "isd": "+213" + }, + "American Samoa": { + "code": "as", + "number_format": "#,###.##", + "isd": "+1684" + }, + "Andorra": { + "code": "ad", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Andorra" + ], + "isd": "+376" + }, + "Angola": { + "code": "ao", + "currency": "KZ", + "currency_fraction": "C\u00eantimo", + "currency_fraction_units": 100, + "currency_symbol": "AOA", + "currency_name": "Kwanza", + "number_format": "#,###.##", + "timezones": [ + "Africa/Luanda" + ], + "isd": "+244" + }, + "Anguilla": { + "code": "ai", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/Anguilla" + ], + "isd": "+1264" + }, + "Antarctica": { + "code": "aq", + "number_format": "#,###.##", + "timezones": [ + "Antarctica/Casey", + "Antarctica/Davis", + "Antarctica/DumontDUrville", + "Antarctica/Macquarie", + "Antarctica/Mawson", + "Antarctica/McMurdo", + "Antarctica/Palmer", + "Antarctica/Rothera", + "Antarctica/Syowa", + "Antarctica/Vostok" + ], + "isd": "+672" + }, + "Antigua and Barbuda": { + "code": "ag", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/Antigua" + ], + "isd": "+1268" + }, + "Argentina": { + "code": "ar", + "currency": "ARS", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Argentine Peso", + "currency_symbol": "$", + "number_format": "#.###,##", + "timezones": [ + "America/Argentina/Buenos_Aires", + "America/Argentina/Catamarca", + "America/Argentina/Cordoba", + "America/Argentina/Jujuy", + "America/Argentina/La_Rioja", + "America/Argentina/Mendoza", + "America/Argentina/Rio_Gallegos", + "America/Argentina/Salta", + "America/Argentina/San_Juan", + "America/Argentina/San_Luis", + "America/Argentina/Tucuman", + "America/Argentina/Ushuaia" + ], + "isd": "+54" + }, + "Armenia": { + "code": "am", + "currency": "AMD", + "currency_fraction": "Luma", + "currency_fraction_units": 100, + "currency_name": "Armenian Dram", + "currency_symbol": "\u058f", + "number_format": "#,###.##", + "timezones": [ + "Asia/Yerevan" + ], + "isd": "+374" + }, + "Aruba": { + "code": "aw", + "currency": "AWG", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Aruban Florin", + "currency_symbol": "Afl", + "number_format": "#,###.##", + "timezones": [ + "America/Aruba" + ], + "isd": "+297" + }, + "Australia": { + "code": "au", + "currency": "AUD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Australian Dollar", + "currency_symbol": "$", + "number_format": "# ###.##", + "timezones": [ + "Australia/Adelaide", + "Australia/Brisbane", + "Australia/Broken_Hill", + "Australia/Currie", + "Australia/Darwin", + "Australia/Eucla", + "Australia/Hobart", + "Australia/Lindeman", + "Australia/Lord_Howe", + "Australia/Melbourne", + "Australia/Perth", + "Australia/Sydney" + ], + "isd": "+61" + }, + "Austria": { + "code": "at", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Vienna" + ], + "isd": "+43" + }, + "Azerbaijan": { + "code": "az", + "currency_fraction": "Q\u0259pik", + "currency_fraction_units": 100, + "currency_symbol": "", + "number_format": "#,###.##", + "timezones": [ + "Asia/Baku" + ], + "isd": "+994" + }, + "Bahamas": { + "code": "bs", + "currency": "BSD", + "currency_name": "Bahamian Dollar", + "number_format": "#,###.##", + "timezones": [ + "America/Nassau" + ], + "isd": "+1242" + }, + "Bahrain": { + "code": "bh", + "currency": "BHD", + "currency_fraction": "Fils", + "currency_fraction_units": 1000, + "currency_name": "Bahraini Dinar", + "currency_symbol": ".\u062f.\u0628", + "number_format": "#,###.###", + "timezones": [ + "Asia/Bahrain" + ], + "isd": "+973" + }, + "Bangladesh": { + "code": "bd", + "currency": "BDT", + "currency_fraction": "Paisa", + "currency_fraction_units": 100, + "currency_name": "Taka", + "currency_symbol": "\u09f3", + "number_format": "#,###.##", + "timezones": [ + "Asia/Dhaka" + ], + "isd": "+880" + }, + "Barbados": { + "code": "bb", + "currency": "BBD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Barbados Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Barbados" + ], + "isd": "+1246" + }, + "Belarus": { + "code": "by", + "currency_fraction": "Kapyeyka", + "currency_fraction_units": 100, + "currency_symbol": "Br", + "number_format": "#,###.##", + "timezones": [ + "Europe/Minsk" + ], + "isd": "+375" + }, + "Belgium": { + "code": "be", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Brussels" + ], + "isd": "+32" + }, + "Belize": { + "code": "bz", + "currency": "BZD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Belize Dollar", + "currency_symbol": "$", + "date_format": "mm-dd-yyyy", + "number_format": "#,###.##", + "timezones": [ + "America/Belize" + ], + "isd": "+501" + }, + "Benin": { + "code": "bj", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Porto-Novo" + ], + "isd": "+229" + }, + "Bermuda": { + "code": "bm", + "currency": "BMD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Bermudian Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Atlantic/Bermuda" + ], + "isd": "+1441" + }, + "Bhutan": { + "code": "bt", + "currency": "BTN", + "currency_fraction": "Chetrum", + "currency_fraction_units": 100, + "currency_name": "Ngultrum", + "currency_symbol": "Nu.", + "number_format": "#,###.##", + "timezones": [ + "Asia/Thimphu" + ], + "isd": "+975" + }, + "Bolivia, Plurinational State of": { + "code": "bo", + "currency": "BOB", + "currency_name": "Boliviano", + "number_format": "#,###.##", + "isd": "+591" + }, + "Bonaire, Sint Eustatius and Saba": { + "code": "bq", + "number_format": "#,###.##" + }, + "Bosnia and Herzegovina": { + "code": "ba", + "currency": "BAM", + "currency_fraction": "Fening", + "currency_fraction_units": 100, + "currency_symbol": "KM", + "number_format": "#.###,##", + "timezones": [ + "Europe/Sarajevo" + ], + "isd": "+387" + }, + "Botswana": { + "code": "bw", + "currency": "BWP", + "currency_fraction": "Thebe", + "currency_fraction_units": 100, + "currency_name": "Pula", + "currency_symbol": "P", + "number_format": "#,###.##", + "timezones": [ + "Africa/Gaborone" + ], + "isd": "+267" + }, + "Bouvet Island": { + "code": "bv", + "number_format": "#,###.##", + "isd": "+47" + }, + "Brazil": { + "code": "br", + "currency": "BRL", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_symbol": "R$", + "date_format": "dd/mm/yyyy", + "number_format": "#.###,##", + "timezones": [ + "America/Araguaina", + "America/Bahia", + "America/Belem", + "America/Boa_Vista", + "America/Campo_Grande", + "America/Cuiaba", + "America/Eirunepe", + "America/Fortaleza", + "America/Maceio", + "America/Manaus", + "America/Noronha", + "America/Porto_Velho", + "America/Recife", + "America/Rio_Branco", + "America/Santarem", + "America/Sao_Paulo" + ], + "isd": "+55" + }, + "British Indian Ocean Territory": { + "code": "io", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Indian/Chagos" + ], + "isd": "+246" + }, + "Brunei Darussalam": { + "code": "bn", + "currency": "BND", + "currency_name": "Brunei Dollar", + "number_format": "#,###.##", + "timezones": [ + "Asia/Brunei" + ], + "isd": "+673" + }, + "Bulgaria": { + "code": "bg", + "currency": "BGN", + "currency_name": "Bulgarian Lev", + "currency_fraction": "Stotinka", + "currency_fraction_units": 100, + "currency_symbol": "\u043b\u0432", + "number_format": "#,###.##", + "timezones": [ + "Europe/Sofia" + ], + "isd": "+359" + }, + "Burkina Faso": { + "code": "bf", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Ouagadougou" + ], + "isd": "+226" + }, + "Burundi": { + "code": "bi", + "currency": "BIF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Burundi Franc", + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Africa/Bujumbura" + ], + "isd": "+257" + }, + "Cambodia": { + "code": "kh", + "currency": "KHR", + "currency_fraction": "Sen", + "currency_fraction_units": 100, + "currency_name": "Riel", + "currency_symbol": "\u17db", + "number_format": "#,###.##", + "timezones": [ + "Asia/Phnom_Penh" + ], + "isd": "+855" + }, + "Cameroon": { + "code": "cm", + "currency": "XAF", + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Douala" + ], + "isd": "+237" + }, + "Canada": { + "code": "ca", + "currency": "CAD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Canadian Dollar", + "currency_symbol": "$", + "date_format": "mm-dd-yyyy", + "number_format": "#,###.##", + "timezones": [ + "America/Atikokan", + "America/Blanc-Sablon", + "America/Cambridge_Bay", + "America/Creston", + "America/Dawson", + "America/Dawson_Creek", + "America/Edmonton", + "America/Glace_Bay", + "America/Goose_Bay", + "America/Halifax", + "America/Inuvik", + "America/Iqaluit", + "America/Moncton", + "America/Montreal", + "America/Nipigon", + "America/Pangnirtung", + "America/Rainy_River", + "America/Rankin_Inlet", + "America/Regina", + "America/Resolute", + "America/St_Johns", + "America/Swift_Current", + "America/Thunder_Bay", + "America/Toronto", + "America/Vancouver", + "America/Whitehorse", + "America/Winnipeg", + "America/Yellowknife" + ], + "isd": "+1" + }, + "Cape Verde": { + "code": "cv", + "currency": "CVE", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Cape Verde Escudo", + "currency_symbol": "Esc or $", + "number_format": "#,###.##", + "timezones": [ + "Atlantic/Cape_Verde" + ], + "isd": "+238" + }, + "Cayman Islands": { + "code": "ky", + "currency": "KYD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Cayman Islands Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Cayman" + ], + "isd": "+ 345" + }, + "Central African Republic": { + "code": "cf", + "currency": "XAF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "number_format": "#,###.##", + "timezones": [ + "Africa/Bangui" + ], + "isd": "+236" + }, + "Chad": { + "code": "td", + "currency": "XAF", + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Ndjamena" + ], + "isd": "+235" + }, + "Chile": { + "code": "cl", + "currency": "CLP", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Chilean Peso", + "currency_symbol": "$", + "number_format": "#.###", + "timezones": [ + "America/Santiago", + "Pacific/Easter" + ], + "isd": "+56" + }, + "China": { + "code": "cn", + "currency": "CNY", + "currency_name": "Yuan Renminbi", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "date_format": "yyyy-mm-dd", + "number_format": "#,###.##", + "timezones": [ + "Asia/Chongqing", + "Asia/Harbin", + "Asia/Kashgar", + "Asia/Shanghai", + "Asia/Urumqi" + ], + "isd": "+86" + }, + "Christmas Island": { + "code": "cx", + "number_format": "#,###.##", + "timezones": [ + "Indian/Christmas" + ], + "isd": "+61" + }, + "Cocos (Keeling) Islands": { + "code": "cc", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Indian/Cocos" + ], + "isd": "+61" + }, + "Colombia": { + "code": "co", + "currency": "COP", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Colombian Peso", + "currency_symbol": "$", + "number_format": "#.###,##", + "timezones": [ + "America/Bogota" + ], + "isd": "+57" + }, + "Comoros": { + "code": "km", + "currency": "KMF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Comoro Franc", + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Indian/Comoro" + ], + "isd": "+269" + }, + "Congo": { + "code": "cg", + "number_format": "#,###.##", + "currency": "XAF", + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "isd": "+242" + }, + "Congo, The Democratic Republic of the": { + "code": "cd", + "number_format": "#,###.##", + "currency": "CDF", + "currency_name": "Congolese franc", + "currency_symbol": "FC", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "isd": "+243" + }, + "Cook Islands": { + "code": "ck", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Rarotonga" + ], + "isd": "+682" + }, + "Costa Rica": { + "code": "cr", + "currency": "CRC", + "currency_fraction": "C\u00e9ntimo", + "currency_fraction_units": 100, + "currency_name": "Costa Rican Colon", + "currency_symbol": "\u20a1", + "number_format": "#.###,##", + "timezones": [ + "America/Costa_Rica" + ], + "isd": "+506" + }, + "Croatia": { + "code": "hr", + "currency": "HRK", + "currency_fraction": "Lipa", + "currency_fraction_units": 100, + "currency_name": "Croatian Kuna", + "currency_symbol": "kn", + "number_format": "#.###,##", + "timezones": [ + "Europe/Zagreb" + ], + "isd": "+385" + }, + "Cuba": { + "code": "cu", + "currency": "CUP", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Cuban Peso", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Havana" + ], + "isd": "+53" + }, + "Cura\u00e7ao": { + "code": "cw", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "\u0192", + "number_format": "#,###.##" + }, + "Cyprus": { + "code": "cy", + "currency": "CYP", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Cyprus Pound", + "currency_symbol": "\u20ac", + "number_format": "#.###,##", + "timezones": [ + "Asia/Nicosia" + ], + "isd": "+357" + }, + "Czech Republic": { + "code": "cz", + "currency": "CZK", + "currency_fraction": "Hal\u00e9\u0159", + "currency_fraction_units": 100, + "currency_name": "Czech Koruna", + "currency_symbol": "K\u010d", + "number_format": "#.###,##", + "timezones": [ + "Europe/Prague" + ], + "isd": "+420" + }, + "Denmark": { + "code": "dk", + "currency": "DKK", + "currency_fraction": "\u00d8re", + "currency_fraction_units": 100, + "currency_name": "Danish Krone", + "currency_symbol": "kr", + "number_format": "#.###,##", + "timezones": [ + "Europe/Copenhagen" + ], + "isd": "+45" + }, + "Djibouti": { + "code": "dj", + "currency": "DJF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Djibouti Franc", + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Africa/Djibouti" + ], + "isd": "+253" + }, + "Dominica": { + "code": "dm", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/Dominica" + ], + "isd": "+1767" + }, + "Dominican Republic": { + "code": "do", + "currency": "DOP", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Dominican Peso", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Santo_Domingo" + ], + "isd": "+1849" + }, + "Ecuador": { + "code": "ec", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Guayaquil", + "Pacific/Galapagos" + ], + "isd": "+593" + }, + "Egypt": { + "code": "eg", + "currency": "EGP", + "currency_fraction": "Piastre[F]", + "currency_fraction_units": 100, + "currency_name": "Egyptian Pound", + "currency_symbol": "\u00a3 or \u062c.\u0645", + "number_format": "#,###.##", + "timezones": [ + "Africa/Cairo" + ], + "isd": "+20" + }, + "El Salvador": { + "code": "sv", + "currency": "USD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_name": "Dolar estadounidense", + "currency_symbol": "$", + "date_format": "dd-mm-yyyy", + "number_format": "#,###.##", + "timezones": [ + "America/El_Salvador" + ], + "isd": "+503" + }, + "Equatorial Guinea": { + "code": "gq", + "currency": "XAF", + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Malabo" + ], + "isd": "+240" + }, + "Eritrea": { + "code": "er", + "currency": "ERN", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Nakfa", + "currency_symbol": "Nfk", + "number_format": "#,###.##", + "timezones": [ + "Africa/Asmara" + ], + "isd": "+291" + }, + "Estonia": { + "code": "ee", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Euro", + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Tallinn" + ], + "isd": "+372" + }, + "Ethiopia": { + "code": "et", + "currency": "ETB", + "currency_fraction": "Santim", + "currency_fraction_units": 100, + "currency_name": "Ethiopian Birr", + "currency_symbol": "Br", + "number_format": "#,###.##", + "timezones": [ + "Africa/Addis_Ababa" + ], + "isd": "+251" + }, + "Falkland Islands (Malvinas)": { + "code": "fk", + "currency": "FKP", + "currency_name": "Falkland Islands Pound", + "number_format": "#,###.##", + "isd": "+500" + }, + "Faroe Islands": { + "code": "fo", + "currency_fraction": "\u00d8re", + "currency_fraction_units": 100, + "currency_symbol": "kr", + "number_format": "#,###.##", + "timezones": [ + "Atlantic/Faroe" + ], + "isd": "+298" + }, + "Fiji": { + "code": "fj", + "currency": "FJD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Fiji Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Fiji" + ], + "isd": "+679" + }, + "Finland": { + "code": "fi", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Helsinki" + ], + "isd": "+358" + }, + "France": { + "code": "fr", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "# ###,##", + "date_format": "dd/mm/yyyy", + "timezones": [ + "Europe/Paris" + ], + "isd": "+33" + }, + "French Guiana": { + "code": "gf", + "number_format": "#,###.##", + "timezones": [ + "America/Cayenne" + ], + "isd": "+594" + }, + "French Polynesia": { + "code": "pf", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Gambier", + "Pacific/Marquesas", + "Pacific/Tahiti" + ], + "isd": "+689" + }, + "French Southern Territories": { + "code": "tf", + "number_format": "#,###.##", + "isd": "+262" + }, + "Gabon": { + "code": "ga", + "currency": "XAF", + "currency_name": "Central African CFA Franc", + "currency_symbol": "FCFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Libreville" + ], + "isd": "+241" + }, + "Gambia": { + "code": "gm", + "currency": "GMD", + "currency_name": "Dalasi", + "number_format": "#,###.##", + "timezones": [ + "Africa/Banjul" + ], + "isd": "+220" + }, + "Georgia": { + "code": "ge", + "currency_fraction": "Tetri", + "currency_fraction_units": 100, + "currency_symbol": "\u10da", + "number_format": "#,###.##", + "timezones": [ + "Asia/Tbilisi" + ], + "isd": "+995" + }, + "Germany": { + "code": "de", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#.###,##", + "date_format": "dd.mm.yyyy", + "time_format": "HH:mm", + "timezones": [ + "Europe/Berlin" + ], + "isd": "+49" + }, + "Ghana": { + "code": "gh", + "currency": "GHS", + "currency_fraction": "Pesewa", + "currency_fraction_units": 100, + "currency_symbol": "\u20b5", + "number_format": "#,###.##", + "timezones": [ + "Africa/Accra" + ], + "isd": "+233" + }, + "Gibraltar": { + "code": "gi", + "currency": "GIP", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_name": "Gibraltar Pound", + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Europe/Gibraltar" + ], + "isd": "+350" + }, + "Greece": { + "code": "gr", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Athens" + ], + "isd": "+30" + }, + "Greenland": { + "code": "gl", + "number_format": "#,###.##", + "timezones": [ + "America/Danmarkshavn", + "America/Godthab", + "America/Scoresbysund", + "America/Thule" + ], + "isd": "+299" + }, + "Grenada": { + "code": "gd", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/Grenada" + ], + "isd": "+1473" + }, + "Guadeloupe": { + "code": "gp", + "number_format": "#,###.##", + "timezones": [ + "America/Guadeloupe" + ], + "isd": "+590" + }, + "Guam": { + "code": "gu", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Guam" + ], + "isd": "+1671" + }, + "Guatemala": { + "code": "gt", + "currency": "GTQ", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Quetzal", + "currency_symbol": "Q", + "number_format": "#,###.##", + "timezones": [ + "America/Guatemala" + ], + "isd": "+502" + }, + "Guernsey": { + "code": "gg", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Europe/London" + ], + "isd": "+44" + }, + "Guinea": { + "code": "gn", + "currency": "GNF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Guinea Franc", + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Africa/Conakry" + ], + "isd": "+224" + }, + "Guinea-Bissau": { + "code": "gw", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Bissau" + ], + "isd": "+245" + }, + "Guyana": { + "code": "gy", + "currency": "GYD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Guyana Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Guyana" + ], + "isd": "+592" + }, + "Haiti": { + "code": "ht", + "currency": "HTG", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Gourde", + "currency_symbol": "G", + "number_format": "#,###.##", + "timezones": [ + "America/Guatemala", + "America/Port-au-Prince" + ], + "isd": "+509" + }, + "Heard Island and McDonald Islands": { + "code": "hm", + "number_format": "#,###.##", + "isd": "+0" + }, + "Holy See (Vatican City State)": { + "code": "va", + "number_format": "#,###.##", + "isd": "+379" + }, + "Honduras": { + "code": "hn", + "currency": "HNL", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Lempira", + "currency_symbol": "L", + "number_format": "#,###.##", + "timezones": [ + "America/Tegucigalpa" + ], + "isd": "+504" + }, + "Hong Kong": { + "code": "hk", + "currency": "HKD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Hong Kong Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Asia/Hong_Kong" + ], + "isd": "+852" + }, + "Hungary": { + "code": "hu", + "currency": "HUF", + "currency_fraction": "Fill\u00e9r", + "currency_fraction_units": 100, + "currency_name": "Forint", + "currency_symbol": "Ft", + "date_format": "yyyy-mm-dd", + "number_format": "#.###", + "timezones": [ + "Europe/Budapest" + ], + "isd": "+36" + }, + "Iceland": { + "code": "is", + "currency": "ISK", + "currency_fraction": "Eyrir", + "currency_fraction_units": 100, + "currency_name": "Iceland Krona", + "currency_symbol": "kr", + "number_format": "#.###", + "timezones": [ + "Atlantic/Reykjavik" + ], + "isd": "+354" + }, + "India": { + "code": "in", + "currency": "INR", + "currency_fraction": "Paisa", + "currency_fraction_units": 100, + "currency_name": "Indian Rupee", + "currency_symbol": "\u20b9", + "number_format": "#,##,###.##", + "timezones": [ + "Asia/Kolkata" + ], + "isd": "+91" + }, + "Indonesia": { + "code": "id", + "currency": "IDR", + "currency_fraction": "Sen", + "currency_fraction_units": 100, + "currency_name": "Rupiah", + "currency_symbol": "Rp", + "number_format": "#.###,##", + "timezones": [ + "Asia/Jakarta", + "Asia/Jayapura", + "Asia/Makassar", + "Asia/Pontianak" + ], + "isd": "+62" + }, + "Iran": { + "code": "ir", + "currency": "IRR", + "currency_name": "Iranian Rial", + "currency_symbol": "\ufdfc", + "number_format": "#,###.##", + "timezones": [ + "Asia/Tehran" + ], + "isd": "+98" + }, + "Iraq": { + "code": "iq", + "currency": "IQD", + "currency_fraction": "Fils", + "currency_fraction_units": 1000, + "currency_name": "Iraqi Dinar", + "currency_symbol": "\u0639.\u062f", + "number_format": "#,###.###", + "timezones": [ + "Asia/Baghdad" + ], + "isd": "+964" + }, + "Ireland": { + "code": "ie", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Dublin" + ], + "isd": "+353" + }, + "Isle of Man": { + "code": "im", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Europe/London" + ], + "isd": "+44" + }, + "Israel": { + "code": "il", + "currency": "ILS", + "currency_fraction": "Agora", + "currency_fraction_units": 100, + "currency_name": "New Israeli Sheqel", + "currency_symbol": "\u20aa", + "number_format": "#,###.##", + "timezones": [ + "Asia/Jerusalem" + ], + "isd": "+972" + }, + "Italy": { + "code": "it", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#.###,##", + "date_format": "dd/mm/yyyy", + "timezones": [ + "Europe/Rome" + ], + "isd": "+39" + }, + "Ivory Coast": { + "code": "ci", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timeszones": [ + "Africa/Abidjan" + ], + "isd": "+225" + }, + "Jamaica": { + "code": "jm", + "currency": "JMD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Jamaican Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Jamaica" + ], + "isd": "+1876" + }, + "Japan": { + "code": "jp", + "currency": "JPY", + "currency_fraction": "Sen[G]", + "currency_fraction_units": 100, + "currency_name": "Yen", + "currency_symbol": "\u00a5", + "number_format": "#,###", + "timezones": [ + "Asia/Tokyo" + ], + "isd": "+81" + }, + "Jersey": { + "code": "je", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Europe/London" + ], + "isd": "+44" + }, + "Jordan": { + "code": "jo", + "currency": "JOD", + "currency_fraction": "Piastre[H]", + "currency_fraction_units": 100, + "currency_name": "Jordanian Dinar", + "currency_symbol": "\u062f.\u0627", + "number_format": "#,###.###", + "timezones": [ + "Asia/Amman" + ], + "isd": "+962" + }, + "Kazakhstan": { + "code": "kz", + "currency": "KZT", + "currency_fraction": "T\u00ef\u0131n", + "currency_fraction_units": 100, + "currency_name": "Tenge", + "currency_symbol": "\u20b8", + "number_format": "#,###.##", + "timezones": [ + "Asia/Almaty", + "Asia/Aqtau", + "Asia/Aqtobe", + "Asia/Oral", + "Asia/Qyzylorda" + ], + "isd": "+7" + }, + "Kenya": { + "code": "ke", + "currency": "KES", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Kenyan Shilling", + "currency_symbol": "Sh", + "number_format": "#,###.##", + "timezones": [ + "Africa/Nairobi" + ], + "isd": "+254" + }, + "Kiribati": { + "code": "ki", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Enderbury", + "Pacific/Kiritimati", + "Pacific/Tarawa" + ], + "isd": "+686" + }, + "Korea, Democratic Peoples Republic of": { + "code": "kp", + "currency": "KPW", + "currency_name": "North Korean Won", + "number_format": "#,###.##", + "isd": "+850" + }, + "Korea, Republic of": { + "code": "kr", + "currency": "KRW", + "currency_name": "Won", + "number_format": "#,###", + "isd": "+82" + }, + "Kuwait": { + "code": "kw", + "currency": "KWD", + "currency_fraction": "Fils", + "currency_fraction_units": 1000, + "currency_name": "Kuwaiti Dinar", + "currency_symbol": "\u062f.\u0643", + "number_format": "#,###.###", + "timezones": [ + "Asia/Kuwait" + ], + "isd": "+965" + }, + "Kyrgyzstan": { + "code": "kg", + "currency": "KGS", + "currency_fraction": "Tyiyn", + "currency_fraction_units": 100, + "currency_name": "Som", + "currency_symbol": "\u043b\u0432", + "number_format": "#,###.##", + "timezones": [ + "Asia/Bishkek" + ], + "isd": "+996" + }, + "Lao Peoples Democratic Republic": { + "code": "la", + "currency": "LAK", + "currency_name": "Kip", + "number_format": "#,###.##", + "timezones": [ + "Asia/Vientiane" + ], + "isd": "+856" + }, + "Latvia": { + "code": "lv", + "currency": "LVL", + "currency_fraction": "Sant\u012bms", + "currency_fraction_units": 100, + "currency_name": "Latvian Lats", + "currency_symbol": "Ls", + "number_format": "#,###.##", + "timezones": [ + "Europe/Riga" + ], + "isd": "+371" + }, + "Lebanon": { + "code": "lb", + "currency": "LBP", + "currency_fraction": "Piastre", + "currency_fraction_units": 100, + "currency_name": "Lebanese Pound", + "currency_symbol": "\u0644.\u0644", + "number_format": "#,###.##", + "timezones": [ + "Asia/Beirut" + ], + "isd": "+961" + }, + "Lesotho": { + "code": "ls", + "currency": "LSL", + "currency_fraction": "Sente", + "currency_fraction_units": 100, + "currency_name": "Loti", + "currency_symbol": "L", + "number_format": "#,###.##", + "timezones": [ + "Africa/Maseru" + ], + "isd": "+266" + }, + "Liberia": { + "code": "lr", + "currency": "LRD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Liberian Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Africa/Monrovia" + ], + "isd": "+231" + }, + "Libya": { + "code": "ly", + "currency": "LYD", + "currency_fraction": "Dirham", + "currency_fraction_units": 1000, + "currency_name": "Libyan Dinar", + "currency_symbol": "\u0644.\u062f", + "number_format": "#,###.###", + "timezones": [ + "Africa/Tripoli" + ], + "isd": "+218" + }, + "Liechtenstein": { + "code": "li", + "currency_fraction": "Rappen", + "currency_fraction_units": 100, + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Europe/Vaduz" + ], + "isd": "+423" + }, + "Lithuania": { + "code": "lt", + "currency": "LTL", + "currency_fraction": "Centas", + "currency_fraction_units": 100, + "currency_name": "Lithuanian Litas", + "currency_symbol": "Lt", + "date_format": "yyyy-mm-dd", + "number_format": "# ###,##", + "timezones": [ + "Europe/Vilnius" + ], + "isd": "+370" + }, + "Luxembourg": { + "code": "lu", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Luxembourg" + ], + "isd": "+352" + }, + "Macao": { + "code": "mo", + "currency": "MOP", + "currency_name": "Pataca", + "number_format": "#,###.##", + "isd": "+853" + }, + "Macedonia": { + "code": "mk", + "currency": "MKD", + "currency_fraction": "Deni", + "currency_fraction_units": 100, + "currency_name": "Denar", + "currency_symbol": "\u0434\u0435\u043d", + "number_format": "#,###.##", + "isd": "+389" + }, + "Madagascar": { + "code": "mg", + "currency_fraction": "Iraimbilanja", + "currency_fraction_units": 5, + "currency_symbol": "Ar", + "number_format": "#,###.##", + "timezones": [ + "Indian/Antananarivo" + ], + "isd": "+261" + }, + "Malawi": { + "code": "mw", + "currency": "MWK", + "currency_fraction": "Tambala", + "currency_fraction_units": 100, + "currency_name": "Kwacha", + "currency_symbol": "MK", + "number_format": "#,###.##", + "timezones": [ + "Africa/Blantyre" + ], + "isd": "+265" + }, + "Malaysia": { + "code": "my", + "currency": "MYR", + "currency_fraction": "Sen", + "currency_fraction_units": 100, + "currency_name": "Malaysian Ringgit", + "currency_symbol": "RM", + "number_format": "#,###.##", + "timezones": [ + "Asia/Kuala_Lumpur", + "Asia/Kuching" + ], + "isd": "+60" + }, + "Maldives": { + "code": "mv", + "currency": "MVR", + "currency_fraction": "Laari", + "currency_fraction_units": 100, + "currency_name": "Rufiyaa", + "currency_symbol": ".\u0783", + "number_format": "#,###.##", + "timezones": [ + "Indian/Maldives" + ], + "isd": "+960" + }, + "Mali": { + "code": "ml", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Bamako" + ], + "isd": "+223" + }, + "Malta": { + "code": "mt", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "date_format": "dd/mm/yyyy", + "timezones": [ + "Europe/Malta" + ], + "isd": "+356" + }, + "Marshall Islands": { + "code": "mh", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Kwajalein", + "Pacific/Majuro" + ], + "isd": "+692" + }, + "Martinique": { + "code": "mq", + "number_format": "#,###.##", + "timezones": [ + "America/Martinique" + ], + "isd": "+596" + }, + "Mauritania": { + "code": "mr", + "currency": "MRO", + "currency_fraction": "Khoums", + "currency_fraction_units": 5, + "currency_name": "Ouguiya", + "currency_symbol": "UM", + "number_format": "#,###.##", + "timezones": [ + "Africa/Nouakchott" + ], + "isd": "+222" + }, + "Mauritius": { + "code": "mu", + "currency": "MUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Mauritius Rupee", + "currency_symbol": "\u20a8", + "number_format": "#,###", + "timezones": [ + "Indian/Mauritius" + ], + "isd": "+230" + }, + "Mayotte": { + "code": "yt", + "number_format": "#,###.##", + "timezones": [ + "Indian/Mayotte" + ], + "isd": "+262" + }, + "Mexico": { + "code": "mx", + "currency": "MXN", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Mexican Peso", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Bahia_Banderas", + "America/Cancun", + "America/Chihuahua", + "America/Hermosillo", + "America/Matamoros", + "America/Mazatlan", + "America/Merida", + "America/Mexico_City", + "America/Monterrey", + "America/Ojinaga", + "America/Santa_Isabel", + "America/Tijuana" + ], + "isd": "+52" + }, + "Micronesia, Federated States of": { + "code": "fm", + "number_format": "#,###.##", + "isd": "+691" + }, + "Moldova, Republic of": { + "code": "md", + "currency": "MDL", + "currency_name": "Moldovan Leu", + "number_format": "#,###.##", + "isd": "+373" + }, + "Monaco": { + "code": "mc", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Monaco" + ], + "isd": "+377" + }, + "Mongolia": { + "code": "mn", + "currency": "MNT", + "currency_fraction": "M\u00f6ng\u00f6", + "currency_fraction_units": 100, + "currency_name": "Tugrik", + "currency_symbol": "\u20ae", + "date_format": "yyyy-mm-dd", + "number_format": "#,###.##", + "timezones": [ + "Asia/Choibalsan", + "Asia/Hovd", + "Asia/Ulaanbaatar" + ], + "isd": "+976" + }, + "Montenegro": { + "code": "me", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Belgrade" + ], + "isd": "+382" + }, + "Montserrat": { + "code": "ms", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/Montserrat" + ], + "isd": "+1664" + }, + "Morocco": { + "code": "ma", + "currency": "MAD", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Moroccan Dirham", + "currency_symbol": "\u062f.\u0645.", + "number_format": "#,###.##", + "timezones": [ + "Africa/Casablanca" + ], + "isd": "+212" + }, + "Mozambique": { + "code": "mz", + "currency": "MZN", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_symbol": "MZN", + "number_format": "#,###.##", + "timezones": [ + "Africa/Maputo" + ], + "isd": "+258" + }, + "Myanmar": { + "code": "mm", + "currency": "MMK", + "currency_name": "Kyat", + "number_format": "#,###.##", + "timezones": [ + "Asia/Rangoon" + ], + "isd": "+95" + }, + "Namibia": { + "code": "na", + "currency": "NAD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Namibia Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Africa/Windhoek" + ], + "isd": "+264" + }, + "Nauru": { + "code": "nr", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Nauru" + ], + "isd": "+674" + }, + "Nepal": { + "code": "np", + "currency": "NPR", + "currency_fraction": "Paisa", + "currency_fraction_units": 100, + "currency_name": "Nepalese Rupee", + "currency_symbol": "\u20a8", + "number_format": "#,##,###.##", + "timezones": [ + "Asia/Kathmandu" + ], + "isd": "+977" + }, + "Netherlands": { + "code": "nl", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Amsterdam" + ], + "isd": "+31" + }, + "New Caledonia": { + "code": "nc", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Noumea" + ], + "isd": "+687" + }, + "New Zealand": { + "code": "nz", + "currency": "NZD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "New Zealand Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Auckland", + "Pacific/Chatham" + ], + "isd": "+64" + }, + "Nicaragua": { + "code": "ni", + "currency": "NIO", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Cordoba Oro", + "currency_symbol": "C$", + "number_format": "#,###.##", + "timezones": [ + "America/Managua" + ], + "isd": "+505" + }, + "Niger": { + "code": "ne", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Niamey" + ], + "isd": "+227" + }, + "Nigeria": { + "code": "ng", + "currency": "NGN", + "currency_fraction": "Kobo", + "currency_fraction_units": 100, + "currency_name": "Naira", + "currency_symbol": "\u20a6", + "number_format": "#,###.##", + "timezones": [ + "Africa/Lagos" + ], + "isd": "+234" + }, + "Niue": { + "code": "nu", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Niue" + ], + "isd": "+683" + }, + "Norfolk Island": { + "code": "nf", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Norfolk" + ], + "isd": "+672" + }, + "Northern Mariana Islands": { + "code": "mp", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Saipan" + ], + "isd": "+1670" + }, + "Norway": { + "code": "no", + "currency": "NOK", + "currency_fraction": "\u00d8re", + "currency_fraction_units": 100, + "currency_name": "Norwegian Krone", + "currency_symbol": "kr", + "number_format": "#.###,##", + "timezones": [ + "Europe/Oslo" + ], + "isd": "+47" + }, + "Oman": { + "code": "om", + "currency": "OMR", + "currency_fraction": "Baisa", + "currency_fraction_units": 1000, + "currency_name": "Rial Omani", + "currency_symbol": "\u0631.\u0639.", + "number_format": "#,###.###", + "timezones": [ + "Asia/Muscat" + ], + "isd": "+968" + }, + "Pakistan": { + "code": "pk", + "currency": "PKR", + "currency_fraction": "Paisa", + "currency_fraction_units": 100, + "currency_name": "Pakistan Rupee", + "currency_symbol": "\u20a8", + "number_format": "#,###.##", + "timezones": [ + "Asia/Karachi" + ], + "isd": "+92" + }, + "Palau": { + "code": "pw", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "date_format": "mm-dd-yyyy", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Palau" + ], + "isd": "+680" + }, + "Palestinian Territory, Occupied": { + "code": "ps", + "currency": "ILS", + "currency_fraction": "Agora", + "currency_fraction_units": 100, + "currency_name": "New Israeli Sheqel", + "currency_symbol": "\u20aa", + "number_format": "#,###.##", + "isd": "+970", + "timezones": [ + "Asia/Hebron", + "Asia/Jerusalem" + ] + }, + "Panama": { + "code": "pa", + "currency_fraction": "Cent\u00e9simo", + "currency_fraction_units": 100, + "currency_symbol": "B/.", + "number_format": "#,###.##", + "timezones": [ + "America/Panama" + ], + "isd": "+507" + }, + "Papua New Guinea": { + "code": "pg", + "currency": "PGK", + "currency_fraction": "Toea", + "currency_fraction_units": 100, + "currency_name": "Kina", + "currency_symbol": "K", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Port_Moresby" + ], + "isd": "+675" + }, + "Paraguay": { + "code": "py", + "currency": "PYG", + "currency_fraction": "C\u00e9ntimo", + "currency_fraction_units": 100, + "currency_name": "Guarani", + "currency_symbol": "\u20b2", + "number_format": "#,###.##", + "timezones": [ + "America/Asuncion" + ], + "isd": "+595" + }, + "Peru": { + "code": "pe", + "currency": "PEN", + "currency_fraction": "C\u00e9ntimo", + "currency_fraction_units": 100, + "currency_name": "Nuevo Sol", + "currency_symbol": "S/.", + "number_format": "#,###.##", + "timezones": [ + "America/Lima" + ], + "isd": "+51" + }, + "Philippines": { + "code": "ph", + "currency": "PHP", + "currency_fraction": "Centavo", + "currency_fraction_units": 100, + "currency_name": "Philippine Peso", + "currency_symbol": "\u20b1", + "date_format": "mm-dd-yyyy", + "number_format": "#,###.##", + "timezones": [ + "Asia/Manila" + ], + "isd": "+63" + }, + "Pitcairn": { + "code": "pn", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Pitcairn" + ], + "isd": "+64" + }, + "Poland": { + "code": "pl", + "currency": "PLN", + "currency_fraction": "Grosz", + "currency_fraction_units": 100, + "currency_symbol": "z\u0142", + "number_format": "#.###,##", + "timezones": [ + "Europe/Warsaw" + ], + "isd": "+48" + }, + "Portugal": { + "code": "pt", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Atlantic/Azores", + "Atlantic/Madeira", + "Europe/Lisbon" + ], + "isd": "+351" + }, + "Puerto Rico": { + "code": "pr", + "number_format": "#,###.##", + "timezones": [ + "America/Puerto_Rico" + ], + "isd": "+1939" + }, + "Qatar": { + "code": "qa", + "currency": "QAR", + "currency_fraction": "Dirham", + "currency_fraction_units": 100, + "currency_name": "Qatari Rial", + "currency_symbol": "\u0631.\u0642", + "number_format": "#,###.##", + "timezones": [ + "Asia/Qatar" + ], + "isd": "+974" + }, + "Romania": { + "code": "ro", + "currency": "RON", + "currency_fraction": "Bani", + "currency_fraction_units": 100, + "currency_name": "Romanian New Leu", + "currency_symbol": "lei", + "number_format": "#,###.##", + "timezones": [ + "Europe/Bucharest" + ], + "isd": "+40" + }, + "Russian Federation": { + "code": "ru", + "currency": "RUB", + "currency_name": "Russian Ruble", + "number_format": "#.###,##", + "isd": "+7" + }, + "Rwanda": { + "code": "rw", + "currency": "RWF", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_name": "Rwanda Franc", + "currency_symbol": "Fr", + "number_format": "#,###.##", + "timezones": [ + "Africa/Kigali" + ], + "isd": "+250" + }, + "R\u00e9union": { + "code": "re", + "number_format": "#,###.##", + "isd": "+262" + }, + "Saint Barth\u00e9lemy": { + "code": "bl", + "number_format": "#,###.##", + "isd": "+590" + }, + "Saint Helena, Ascension and Tristan da Cunha": { + "code": "sh", + "currency": "SHP", + "currency_name": "Saint Helena Pound", + "number_format": "#,###.##", + "isd": "+290" + }, + "Saint Kitts and Nevis": { + "code": "kn", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/St_Kitts" + ], + "isd": "+1869" + }, + "Saint Lucia": { + "code": "lc", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/St_Lucia" + ], + "isd": "+1758" + }, + "Saint Martin (French part)": { + "code": "mf", + "number_format": "#,###.##", + "isd": "+590" + }, + "Saint Pierre and Miquelon": { + "code": "pm", + "number_format": "#,###.##", + "isd": "+508" + }, + "Saint Vincent and the Grenadines": { + "code": "vc", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "currency_name": "Eastern Carribean Dollar", + "currency": "XCD", + "number_format": "#,###.##", + "timezones": [ + "America/St_Vincent" + ], + "isd": "+1784" + }, + "Samoa": { + "code": "ws", + "currency": "WST", + "currency_fraction": "Sene", + "currency_fraction_units": 100, + "currency_name": "Tala", + "currency_symbol": "T", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Apia" + ], + "isd": "+685" + }, + "San Marino": { + "code": "sm", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Rome" + ], + "isd": "+378" + }, + "Sao Tome and Principe": { + "code": "st", + "currency": "STD", + "currency_name": "Dobra", + "number_format": "#,###.##", + "isd": "+239" + }, + "Saudi Arabia": { + "code": "sa", + "currency": "SAR", + "currency_fraction": "Halala", + "currency_fraction_units": 100, + "currency_name": "Saudi Riyal", + "currency_symbol": "\u0631.\u0633", + "number_format": "#,###.##", + "timezones": [ + "Asia/Riyadh" + ], + "isd": "+966" + }, + "Senegal": { + "code": "sn", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Dakar" + ], + "isd": "+221" + }, + "Serbia": { + "code": "rs", + "currency": "RSD", + "currency_fraction": "Para", + "currency_fraction_units": 100, + "currency_name": "Serbian Dinar", + "currency_symbol": "\u0434\u0438\u043d.", + "number_format": "#,###.##", + "timezones": [ + "Europe/Belgrade" + ], + "isd": "+381" + }, + "Seychelles": { + "code": "sc", + "currency": "SCR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Seychelles Rupee", + "currency_symbol": "\u20a8", + "number_format": "#,###.##", + "timezones": [ + "Indian/Mahe" + ], + "isd": "+248" + }, + "Sierra Leone": { + "code": "sl", + "currency": "SLL", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Leone", + "currency_symbol": "Le", + "number_format": "#,###.##", + "timezones": [ + "Africa/Freetown" + ], + "isd": "+232" + }, + "Singapore": { + "code": "sg", + "currency": "SGD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Singapore Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Asia/Singapore" + ], + "isd": "+65" + }, + "Sint Maarten (Dutch part)": { + "code": "sx", + "number_format": "#,###.##" + }, + "Slovakia": { + "code": "sk", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Bratislava" + ], + "isd": "+421" + }, + "Slovenia": { + "code": "si", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Europe/Belgrade" + ], + "isd": "+386" + }, + "Solomon Islands": { + "code": "sb", + "currency": "SBD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Solomon Islands Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Guadalcanal" + ], + "isd": "+677" + }, + "Somalia": { + "code": "so", + "currency": "SOS", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Somali Shilling", + "currency_symbol": "Sh", + "number_format": "#,###.##", + "timezones": [ + "Africa/Mogadishu" + ], + "isd": "+252" + }, + "South Africa": { + "code": "za", + "currency": "ZAR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Rand", + "currency_symbol": "R", + "date_format": "yyyy-mm-dd", + "number_format": "# ###.##", + "timezones": [ + "Africa/Johannesburg" + ], + "isd": "+27" + }, + "South Georgia and the South Sandwich Islands": { + "code": "gs", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "isd": "+500" + }, + "South Sudan": { + "code": "ss", + "currency_fraction": "Piastre", + "currency_fraction_units": 100, + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Africa/Juba" + ], + "isd": "+211" + }, + "Spain": { + "code": "es", + "currency": "EUR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_symbol": "\u20ac", + "number_format": "#,###.##", + "timezones": [ + "Africa/Ceuta", + "Atlantic/Canary", + "Europe/Madrid" + ], + "isd": "+34" + }, + "Sri Lanka": { + "code": "lk", + "currency": "LKR", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Sri Lanka Rupee", + "currency_symbol": "Rs", + "number_format": "#,###.##", + "timezones": [ + "Asia/Colombo" + ], + "isd": "+94" + }, + "Sudan": { + "code": "sd", + "currency": "SDG", + "currency_fraction": "Piastre", + "currency_fraction_units": 100, + "currency_name": "Sudanese Pound", + "currency_symbol": "\u062c.\u0633.", + "number_format": "#,###.##", + "timezones": [ + "Africa/Khartoum" + ], + "isd": "+249" +}, + "Suriname": { + "code": "sr", + "currency": "SRD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Paramaribo" + ], + "isd": "+597" + }, + "Svalbard and Jan Mayen": { + "code": "sj", + "number_format": "#,###.##", + "isd": "+47" + }, + "Swaziland": { + "code": "sz", + "currency": "SZL", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Lilangeni", + "currency_symbol": "L", + "number_format": "#, ###.##", + "timezones": [ + "Africa/Mbabane" + ], + "isd": "+268" + }, + "Sweden": { + "code": "se", + "currency": "SEK", + "currency_fraction": "\u00d6re", + "currency_fraction_units": 100, + "currency_name": "Swedish Krona", + "currency_symbol": "kr", + "number_format": "#.###,##", + "timezones": [ + "Europe/Stockholm" + ], + "isd": "+46" + }, + "Switzerland": { + "code": "ch", + "currency": "CHF", + "currency_fraction": "Rappen[K]", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.05, + "currency_name": "Swiss Franc", + "currency_symbol": "Fr", + "number_format": "#'###.##", + "timezones": [ + "Europe/Zurich" + ], + "isd": "+41" + }, + "Syria": { + "code": "sy", + "currency": "SYP", + "currency_name": "Syrian Pound", + "number_format": "#,###.##", + "isd": "+963" + }, + "Taiwan": { + "code": "tw", + "currency": "TWD", + "date_format": "yyyy-mm-dd", + "number_format": "#,###.##", + "isd": "+886" + }, + "Tajikistan": { + "code": "tj", + "currency_fraction": "Diram", + "currency_fraction_units": 100, + "currency_symbol": "\u0405\u041c", + "number_format": "#,###.##", + "timezones": [ + "Asia/Dushanbe" + ], + "isd": "+992" + }, + "Tanzania": { + "code": "tz", + "currency": "TZS", + "currency_name": "Tanzanian Shilling", + "number_format": "#,###.##", + "timezones": [ + "Africa/Dar_es_Salaam" + ], + "isd": "+255" + }, + "Thailand": { + "code": "th", + "currency": "THB", + "currency_fraction": "Satang", + "currency_fraction_units": 100, + "currency_name": "Baht", + "currency_symbol": "\u0e3f", + "number_format": "#,###.##", + "timezones": [ + "Asia/Bangkok" + ], + "isd": "+66" + }, + "Timor-Leste": { + "code": "tl", + "number_format": "#,###.##", + "isd": "+670" + }, + "Togo": { + "code": "tg", + "currency": "XOF", + "currency_name": "West African CFA Franc", + "currency_symbol": "CFA", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "number_format": "#,###.##", + "timezones": [ + "Africa/Lome" + ], + "isd": "+228" + }, + "Tokelau": { + "code": "tk", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Fakaofo" + ], + "isd": "+690" + }, + "Tonga": { + "code": "to", + "currency": "TOP", + "currency_fraction": "Seniti[L]", + "currency_fraction_units": 100, + "currency_name": "Pa'anga", + "currency_symbol": "T$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Tongatapu" + ], + "isd": "+676" + }, + "Trinidad and Tobago": { + "code": "tt", + "currency": "TTD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Trinidad and Tobago Dollar", + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "America/Port_of_Spain" + ], + "isd": "+1868" + }, + "Tunisia": { + "code": "tn", + "currency": "TND", + "currency_fraction": "Millime", + "currency_fraction_units": 1000, + "currency_name": "Tunisian Dinar", + "currency_symbol": "\u062f.\u062a", + "number_format": "#,###.###", + "timezones": [ + "Africa/Tunis" + ], + "isd": "+216" + }, + "Turkey": { + "code": "tr", + "currency": "TRY", + "currency_fraction": "Kuru\u015f", + "currency_fraction_units": 100, + "currency_symbol": "\u20ba", + "number_format": "#.###,##", + "timezones": [ + "Europe/Istanbul" + ], + "isd": "+90" + }, + "Turkmenistan": { + "code": "tm", + "currency": "TMM", + "currency_fraction": "Tennesi", + "currency_fraction_units": 100, + "currency_name": "Manat", + "currency_symbol": "m", + "number_format": "#,###.##", + "timezones": [ + "Asia/Ashgabat" + ], + "isd": "+993" + }, + "Turks and Caicos Islands": { + "code": "tc", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "isd": "+1649" + }, + "Tuvalu": { + "code": "tv", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_symbol": "$", + "number_format": "#,###.##", + "timezones": [ + "Pacific/Funafuti" + ], + "isd": "+688" + }, + "Uganda": { + "code": "ug", + "currency": "UGX", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Uganda Shilling", + "currency_symbol": "Sh", + "number_format": "#,###.##", + "timezones": [ + "Africa/Kampala" + ], + "isd": "+256" + }, + "Ukraine": { + "code": "ua", + "currency": "UAH", + "currency_fraction": "Kopiyka", + "currency_fraction_units": 100, + "currency_name": "Ukrainian Hryvnia", + "currency_symbol": "\u20b4", + "number_format": "#,###.##", + "timezones": [ + "Europe/Kiev", + "Europe/Simferopol", + "Europe/Uzhgorod", + "Europe/Zaporozhye" + ], + "isd": "+380" + }, + "United Arab Emirates": { + "code": "ae", + "currency": "AED", + "currency_fraction": "Fils", + "currency_fraction_units": 100, + "currency_name": "UAE Dirham", + "currency_symbol": "\u062f.\u0625", + "number_format": "#,###.##", + "timezones": [ + "Asia/Dubai" + ], + "isd": "+971" + }, + "United Kingdom": { + "code": "gb", + "currency": "GBP", + "currency_fraction": "Penny", + "currency_fraction_units": 100, + "currency_name": "Pound Sterling", + "currency_symbol": "\u00a3", + "number_format": "#,###.##", + "timezones": [ + "Europe/London" + ], + "isd": "+44" + }, + "United States": { + "code": "us", + "currency": "USD", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_name": "US Dollar", + "currency_symbol": "$", + "date_format": "mm-dd-yyyy", + "number_format": "#,###.##", + "timezones": [ + "America/Adak", + "America/Anchorage", + "America/Boise", + "America/Chicago", + "America/Denver", + "America/Detroit", + "America/Indiana/Indianapolis", + "America/Indiana/Knox", + "America/Indiana/Marengo", + "America/Indiana/Petersburg", + "America/Indiana/Tell_City", + "America/Indiana/Vevay", + "America/Indiana/Vincennes", + "America/Indiana/Winamac", + "America/Juneau", + "America/Kentucky/Louisville", + "America/Kentucky/Monticello", + "America/Los_Angeles", + "America/Menominee", + "America/Metlakatla", + "America/New_York", + "America/Nome", + "America/North_Dakota/Beulah", + "America/North_Dakota/Center", + "America/North_Dakota/New_Salem", + "America/Phoenix", + "America/Denver", + "America/Sitka", + "America/Yakutat", + "Pacific/Honolulu" + ], + "isd": "+1" + }, + "United States Minor Outlying Islands": { + "code": "um", + "number_format": "#,###.##" + }, + "Uruguay": { + "code": "uy", + "currency": "UYU", + "currency_fraction": "Cent\u00e9simo", + "currency_fraction_units": 100, + "currency_name": "Peso Uruguayo", + "currency_symbol": "$", + "number_format": "#.###,##", + "timezones": [ + "America/Montevideo" + ], + "isd": "+598" + }, + "Uzbekistan": { + "code": "uz", + "currency": "UZS", + "currency_fraction": "Tiyin", + "currency_fraction_units": 100, + "currency_name": "Uzbekistan Sum", + "currency_symbol": "\u043b\u0432", + "number_format": "#,###.##", + "timezones": [ + "Asia/Samarkand", + "Asia/Tashkent" + ], + "isd": "+998" + }, + "Vanuatu": { + "code": "vu", + "currency": "VUV", + "currency_fraction": "None", + "currency_fraction_units": 0, + "currency_name": "Vatu", + "currency_symbol": "Vt", + "number_format": "#,###", + "timezones": [ + "Pacific/Efate" + ], + "isd": "+678" + }, + "Venezuela, Bolivarian Republic of": { + "code": "ve", + "number_format": "#.###,##", + "currency": "VEF", + "currency_symbol": "Bs.", + "currency_fraction": "Centimos", + "currency_fraction_units": 100, + "isd": "+58" + }, + "Vietnam": { + "code": "vn", + "currency": "VND", + "currency_name": "Dong", + "number_format": "#.###", + "isd": "+84" + }, + "Virgin Islands, British": { + "code": "vg", + "number_format": "#,###.##", + "isd": "+1284" + }, + "Virgin Islands, U.S.": { + "code": "vi", + "number_format": "#,###.##", + "isd": "+1340" + }, + "Wallis and Futuna": { + "code": "wf", + "currency_fraction": "Centime", + "currency_fraction_units": 100, + "currency_symbol": "Fr", + "number_format": "#,###.##", + "isd": "+681" + }, + "Western Sahara": { + "code": "eh", + "number_format": "#,###.##", + "timezones": [ + "Africa/El_Aaiun" + ] + }, + "Yemen": { + "code": "ye", + "currency": "YER", + "currency_fraction": "Fils", + "currency_fraction_units": 100, + "smallest_currency_fraction_value": 0.01, + "currency_name": "Yemeni Rial", + "currency_symbol": "\ufdfc", + "number_format": "#,###.##", + "timezones": [ + "Asia/Aden" + ], + "isd": "+967" + }, + "Zambia": { + "code": "zm", + "currency": "ZMW", + "currency_fraction": "Ngwee", + "currency_fraction_units": 100, + "currency_name": "Zambian Kwacha", + "currency_symbol": "ZK", + "number_format": "#,###.##", + "timezones": [ + "Africa/Lusaka" + ], + "isd": "+260" + }, + "Zimbabwe": { + "code": "zw", + "currency": "ZWL", + "currency_fraction": "Cent", + "currency_fraction_units": 100, + "currency_name": "Zimbabwe Dollar", + "currency_symbol": "ZWL$", + "number_format": "# ###.##", + "timezones": [ + "Africa/Harare" + ], + "isd": "+263" + }, + "\u00c5land Islands": { + "code": "ax", + "number_format": "#,###.##", + "isd": "+358" + } +} diff --git a/xhiveframework/geo/country_info.py b/xhiveframework/geo/country_info.py new file mode 100644 index 0000000..8c0593c --- /dev/null +++ b/xhiveframework/geo/country_info.py @@ -0,0 +1,80 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +# all country info +import os +from functools import lru_cache + +import xhiveframework +from xhiveframework.utils.momentjs import get_all_timezones + + +def get_country_info(country=None): + data = get_all() + data = xhiveframework._dict(data.get(country, {})) + if "date_format" not in data: + data.date_format = "dd-mm-yyyy" + if "time_format" not in data: + data.time_format = "HH:mm:ss" + + return data + + +def get_all(): + with open(os.path.join(os.path.dirname(__file__), "country_info.json")) as local_info: + all_data = json.loads(local_info.read()) + return all_data + + +@xhiveframework.whitelist(allow_guest=True) +def get_country_timezone_info(): + return _get_country_timezone_info() + + +@lru_cache(maxsize=2) +def _get_country_timezone_info(): + return {"country_info": get_all(), "all_timezones": get_all_timezones()} + + +def get_translated_dict(): + from babel.dates import Locale, get_timezone, get_timezone_name + + translated_dict = {} + locale = Locale.parse(xhiveframework.local.lang, sep="-") + + # timezones + for tz in get_all_timezones(): + timezone_name = get_timezone_name(get_timezone(tz), locale=locale, width="short") + if timezone_name: + translated_dict[tz] = timezone_name + " - " + tz + + # country names && currencies + for country, info in get_all().items(): + country_name = locale.territories.get((info.get("code") or "").upper()) + if country_name: + translated_dict[country] = country_name + + currency = info.get("currency") + currency_name = locale.currencies.get(currency) + if currency_name: + translated_dict[currency] = currency_name + + return translated_dict + + +def update(): + with open(os.path.join(os.path.dirname(__file__), "currency_info.json")) as nformats: + nformats = json.loads(nformats.read()) + + all_data = get_all() + + for country in all_data: + data = all_data[country] + data["number_format"] = nformats.get(data.get("currency", "default"), nformats.get("default"))[ + "display" + ] + + with open(os.path.join(os.path.dirname(__file__), "country_info.json"), "w") as local_info: + local_info.write(json.dumps(all_data, indent=1)) diff --git a/xhiveframework/geo/doctype/__init__.py b/xhiveframework/geo/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/geo/doctype/country/README.md b/xhiveframework/geo/doctype/country/README.md new file mode 100644 index 0000000..0e3f46c --- /dev/null +++ b/xhiveframework/geo/doctype/country/README.md @@ -0,0 +1 @@ +Country Master. \ No newline at end of file diff --git a/xhiveframework/geo/doctype/country/__init__.py b/xhiveframework/geo/doctype/country/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/geo/doctype/country/country.js b/xhiveframework/geo/doctype/country/country.js new file mode 100644 index 0000000..ed5ff88 --- /dev/null +++ b/xhiveframework/geo/doctype/country/country.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Country", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/geo/doctype/country/country.json b/xhiveframework/geo/doctype/country/country.json new file mode 100644 index 0000000..8f62458 --- /dev/null +++ b/xhiveframework/geo/doctype/country/country.json @@ -0,0 +1,89 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "autoname": "field:country_name", + "creation": "2013-01-19 10:23:30", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "country_name", + "date_format", + "time_format", + "time_zones", + "code" + ], + "fields": [ + { + "fieldname": "country_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Country Name", + "oldfieldname": "country_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "date_format", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Date Format" + }, + { + "default": "HH:mm:ss", + "fieldname": "time_format", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Time format" + }, + { + "fieldname": "time_zones", + "fieldtype": "Text", + "in_list_view": 1, + "label": "Time Zones" + }, + { + "fieldname": "code", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Code" + } + ], + "icon": "fa fa-globe", + "idx": 1, + "links": [], + "modified": "2022-08-05 18:33:27.880783", + "modified_by": "Administrator", + "module": "Geo", + "name": "Country", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "All" + } + ], + "quick_entry": 1, + "sort_field": "country_name", + "sort_order": "ASC", + "states": [], + "track_changes": 1, + "translated_doctype": 1 +} diff --git a/xhiveframework/geo/doctype/country/country.py b/xhiveframework/geo/doctype/country/country.py new file mode 100644 index 0000000..1037682 --- /dev/null +++ b/xhiveframework/geo/doctype/country/country.py @@ -0,0 +1,77 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document, bulk_insert + + +class Country(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + code: DF.Data | None + country_name: DF.Data + date_format: DF.Data | None + time_format: DF.Data | None + time_zones: DF.Text | None + # end: auto-generated types + # NOTE: During installation country docs are bulk inserted. + pass + + +def import_country_and_currency(): + from xhiveframework.geo.doctype.currency.currency import enable_default_currencies + + countries, currencies = get_countries_and_currencies() + + bulk_insert("Country", countries, ignore_duplicates=True) + bulk_insert("Currency", currencies, ignore_duplicates=True) + + enable_default_currencies() + + +def get_countries_and_currencies(): + from xhiveframework.geo.country_info import get_all as get_geo_data + + data = get_geo_data() + + countries = [] + currencies = [] + + added_currencies = set() + + for name, country in data.items(): + country = xhiveframework._dict(country) + countries.append( + xhiveframework.get_doc( + doctype="Country", + name=name, + country_name=name, + code=country.code, + date_format=country.date_format or "dd-mm-yyyy", + time_format=country.time_format or "HH:mm:ss", + time_zones="\n".join(country.timezones or []), + ) + ) + if country.currency and country.currency not in added_currencies: + added_currencies.add(country.currency) + + currencies.append( + xhiveframework.get_doc( + doctype="Currency", + name=country.currency, + currency_name=country.currency, + fraction=country.currency_fraction, + symbol=country.currency_symbol, + fraction_units=country.currency_fraction_units, + smallest_currency_fraction_value=country.smallest_currency_fraction_value, + number_format=country.number_format, + ) + ) + + return countries, currencies diff --git a/xhiveframework/geo/doctype/country/test_country.py b/xhiveframework/geo/doctype/country/test_country.py new file mode 100644 index 0000000..4e10544 --- /dev/null +++ b/xhiveframework/geo/doctype/country/test_country.py @@ -0,0 +1,53 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.geo.doctype.country.country import ( + get_countries_and_currencies, + import_country_and_currency, +) +from xhiveframework.geo.doctype.currency.currency import enable_default_currencies +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_records = xhiveframework.get_test_records("Country") + + +def get_table_snapshot(doctype): + data = xhiveframework.db.sql(f"select * from `tab{doctype}` order by name", as_dict=True) + + inconsequential_keys = ["modified", "creation"] + for row in data: + for key in inconsequential_keys: + row.pop(key, None) + return data + + +class TestCountry(XhiveFrameworkTestCase): + def test_bulk_insert_correctness(self): + def clear_tables(): + xhiveframework.db.delete("Currency") + xhiveframework.db.delete("Country") + + # Clear data + clear_tables() + + # Reimport and verify same results + import_country_and_currency() + + countries_before = get_table_snapshot("Country") + currencies_before = get_table_snapshot("Currency") + + clear_tables() + + countries, currencies = get_countries_and_currencies() + for country in countries: + country.db_insert(ignore_if_duplicate=True) + for currency in currencies: + currency.db_insert(ignore_if_duplicate=True) + enable_default_currencies() + + countries_after = get_table_snapshot("Country") + currencies_after = get_table_snapshot("Currency") + + self.assertEqual(countries_before, countries_after) + self.assertEqual(currencies_before, currencies_after) diff --git a/xhiveframework/geo/doctype/country/test_records.json b/xhiveframework/geo/doctype/country/test_records.json new file mode 100644 index 0000000..5a7c8a5 --- /dev/null +++ b/xhiveframework/geo/doctype/country/test_records.json @@ -0,0 +1,6 @@ +[ + { + "country_name": "_Test Country", + "doctype": "Country" + } +] \ No newline at end of file diff --git a/xhiveframework/geo/doctype/currency/README.md b/xhiveframework/geo/doctype/currency/README.md new file mode 100644 index 0000000..3e1558e --- /dev/null +++ b/xhiveframework/geo/doctype/currency/README.md @@ -0,0 +1 @@ +Currency Master with details about abbreviation, symbol etc. \ No newline at end of file diff --git a/xhiveframework/geo/doctype/currency/__init__.py b/xhiveframework/geo/doctype/currency/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/geo/doctype/currency/currency.js b/xhiveframework/geo/doctype/currency/currency.js new file mode 100644 index 0000000..7023553 --- /dev/null +++ b/xhiveframework/geo/doctype/currency/currency.js @@ -0,0 +1,11 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// License: See license.txt + +xhiveframework.ui.form.on("Currency", { + refresh(frm) { + frm.set_intro(""); + if (!frm.doc.enabled) { + frm.set_intro(__("This Currency is disabled. Enable to use in transactions")); + } + }, +}); diff --git a/xhiveframework/geo/doctype/currency/currency.json b/xhiveframework/geo/doctype/currency/currency.json new file mode 100644 index 0000000..9a4df0f --- /dev/null +++ b/xhiveframework/geo/doctype/currency/currency.json @@ -0,0 +1,126 @@ +{ + "actions": [], + "allow_import": 1, + "allow_rename": 1, + "autoname": "field:currency_name", + "creation": "2013-01-28 10:06:02", + "description": "Currency list stores the currency value, its symbol and fraction unit", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "currency_name", + "enabled", + "fraction", + "fraction_units", + "smallest_currency_fraction_value", + "symbol", + "symbol_on_right", + "number_format" + ], + "fields": [ + { + "fieldname": "currency_name", + "fieldtype": "Data", + "label": "Currency Name", + "oldfieldname": "currency_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Enabled" + }, + { + "description": "Sub-currency. For e.g. \"Cent\"", + "fieldname": "fraction", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Fraction" + }, + { + "description": "1 Currency = [?] Fraction\nFor e.g. 1 USD = 100 Cent", + "fieldname": "fraction_units", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Fraction Units" + }, + { + "description": "Smallest circulating fraction unit (coin). For e.g. 1 cent for USD and it should be entered as 0.01", + "fieldname": "smallest_currency_fraction_value", + "fieldtype": "Currency", + "label": "Smallest Currency Fraction Value", + "non_negative": 1 + }, + { + "description": "A symbol for this currency. For e.g. $", + "fieldname": "symbol", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Symbol" + }, + { + "description": "How should this currency be formatted? If not set, will use system defaults", + "fieldname": "number_format", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Number Format", + "options": "\n#,###.##\n#.###,##\n# ###.##\n# ###,##\n#'###.##\n#, ###.##\n#,##,###.##\n#,###.###\n#.###\n#,###" + }, + { + "default": "0", + "fieldname": "symbol_on_right", + "fieldtype": "Check", + "label": "Show Currency Symbol on Right Side" + } + ], + "icon": "fa fa-bitcoin", + "idx": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-01-30 13:18:12.053557", + "modified_by": "Administrator", + "module": "Geo", + "name": "Currency", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "import": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Accounts Manager" + }, + { + "read": 1, + "role": "Accounts User" + }, + { + "read": 1, + "role": "Sales User" + }, + { + "read": 1, + "role": "Purchase User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/geo/doctype/currency/currency.py b/xhiveframework/geo/doctype/currency/currency.py new file mode 100644 index 0000000..5420ccf --- /dev/null +++ b/xhiveframework/geo/doctype/currency/currency.py @@ -0,0 +1,47 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + +DEFAULT_ENABLED_CURRENCIES = ("INR", "USD", "GBP", "EUR", "AED", "AUD", "JPY", "CNY", "CHF") + + +class Currency(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + currency_name: DF.Data + enabled: DF.Check + fraction: DF.Data | None + fraction_units: DF.Int + number_format: DF.Literal[ + "", + "#,###.##", + "#.###,##", + "# ###.##", + "# ###,##", + "#'###.##", + "#, ###.##", + "#,##,###.##", + "#,###.###", + "#.###", + "#,###", + ] + smallest_currency_fraction_value: DF.Currency + symbol: DF.Data | None + symbol_on_right: DF.Check + + # end: auto-generated types + # NOTE: During installation country docs are bulk inserted. + def validate(self): + xhiveframework.clear_cache() + + +def enable_default_currencies(): + xhiveframework.db.set_value("Currency", {"name": ("in", DEFAULT_ENABLED_CURRENCIES)}, "enabled", 1) diff --git a/xhiveframework/geo/doctype/currency/test_currency.py b/xhiveframework/geo/doctype/currency/test_currency.py new file mode 100644 index 0000000..15a23d7 --- /dev/null +++ b/xhiveframework/geo/doctype/currency/test_currency.py @@ -0,0 +1,13 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# pre loaded + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestUser(XhiveFrameworkTestCase): + def test_default_currency_on_setup(self): + usd = xhiveframework.get_doc("Currency", "USD") + self.assertDocumentEqual({"enabled": 1, "fraction": "Cent"}, usd) diff --git a/xhiveframework/geo/doctype/currency/test_records.json b/xhiveframework/geo/doctype/currency/test_records.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/xhiveframework/geo/doctype/currency/test_records.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/xhiveframework/geo/languages.json b/xhiveframework/geo/languages.json new file mode 100644 index 0000000..33092a2 --- /dev/null +++ b/xhiveframework/geo/languages.json @@ -0,0 +1,330 @@ +[ + { + "code": "af", + "name": "Afrikaans" + }, + { + "code": "am", + "name": "\u12a0\u121b\u122d\u129b" + }, + { + "code": "ar", + "name": "\u0627\u0644\u0639\u0631\u0628\u064a\u0629" + }, + { + "code": "bg", + "name": "B\u01celgarski" + }, + { + "code": "bn", + "name": "\u09ac\u09be\u0999\u09be\u09b2\u09bf" + }, + { + "code": "bo", + "name": "\u0f63\u0fb7\u0f0b\u0f66\u0f60\u0f72\u0f0b\u0f66\u0f90\u0f51\u0f0b" + }, + { + "code": "bs", + "name": "Bosanski" + }, + { + "code": "ca", + "name": "Catal\u00e0" + }, + { + "code": "cs", + "name": "\u010desky" + }, + { + "code": "da", + "name": "Dansk" + }, + { + "code": "da-DK", + "name": "Dansk (Danmark)" + }, + { + "code": "de", + "name": "Deutsch" + }, + { + "code": "el", + "name": "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac" + }, + { + "code": "en", + "name": "English" + }, + { + "code": "en-GB", + "name": "English (United Kingdom)" + }, + { + "code": "en-US", + "name": "English (United States)" + }, + { + "code": "es", + "name": "Espa\u00f1ol" + }, + { + "code": "es-AR", + "name": "Espa\u00f1ol (Argentina)" + }, + { + "code": "es-BO", + "name": "Espa\u00f1ol (Bolivia)" + }, + { + "code": "es-CL", + "name": "Espa\u00f1ol (Chile)" + }, + { + "code": "es-CO", + "name": "Espa\u00f1ol (Colombia)" + }, + { + "code": "es-DO", + "name": "Espa\u00f1ol (Rep\u00fablica Dominicana)" + }, + { + "code": "es-EC", + "name": "Espa\u00f1ol (Ecuador)" + }, + { + "code": "es-GT", + "name": "Espa\u00f1ol (Guatemala)" + }, + { + "code": "es-MX", + "name": "Espa\u00f1ol (M\u00e9xico)" + }, + { + "code": "es-NI", + "name": "Espa\u00f1ol (Nicaragua)" + }, + { + "code": "es-PE", + "name": "Espa\u00f1ol (Per\u00fa)" + }, + { + "code": "et", + "name": "Eesti" + }, + { + "code": "fa", + "name": "\u067e\u0627\u0631\u0633\u06cc" + }, + { + "code": "fi", + "name": "Suomi" + }, + { + "code": "fil", + "name": "Filipino" + }, + { + "code": "fr", + "name": "Fran\u00e7ais" + }, + { + "code": "fr-CA", + "name": "Fran\u00e7ais Canadien" + }, + { + "code": "gu", + "name": "\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0" + }, + { + "code": "he", + "name": "\u05e2\u05d1\u05e8\u05d9\u05ea" + }, + { + "code": "hi", + "name": "\u0939\u093f\u0902\u0926\u0940" + }, + { + "code": "hr", + "name": "Hrvatski" + }, + { + "code": "hu", + "name": "Magyar" + }, + { + "code": "id", + "name": "Indonesia" + }, + { + "code": "is", + "name": "\u00edslenska" + }, + { + "code": "it", + "name": "Italiano" + }, + { + "code": "ja", + "name": "\u65e5\u672c\u8a9e" + }, + { + "code": "km", + "name": "\u1797\u17b6\u179f\u17b6\u1781\u17d2\u1798\u17c2\u179a" + }, + { + "code": "kn", + "name": "\u0c95\u0ca8\u0ccd\u0ca8\u0ca1" + }, + { + "code": "ko", + "name": "\ud55c\uad6d\uc758" + }, + { + "code": "ku", + "name": "\u06a9\u0648\u0631\u062f\u06cc" + }, + { + "code": "lo", + "name": "\u0ea5\u0eb2\u0ea7" + }, + { + "code": "lt", + "name": "Lietuvi\u0173 kalba" + }, + { + "code": "lv", + "name": "Latvie\u0161u valoda" + }, + { + "code": "mk", + "name": "\u043c\u0430\u043a\u0435\u0434\u043e\u043d\u0441\u043a\u0438" + }, + { + "code": "ml", + "name": "\u0d2e\u0d32\u0d2f\u0d3e\u0d33\u0d02" + }, + { + "code": "mn", + "name": "\u041c\u043e\u043d\u0433\u043e\u043b (Mongolian)" + }, + { + "code": "mr", + "name": "\u092e\u0930\u093e\u0920\u0940" + }, + { + "code": "ms", + "name": "Melayu" + }, + { + "code": "my", + "name": "\u1019\u103c\u1014\u103a\u1019\u102c" + }, + { + "code": "nl", + "name": "Nederlands" + }, + { + "code": "no", + "name": "Norsk" + }, + { + "code": "pl", + "name": "Polski" + }, + { + "code": "ps", + "name": "\u067e\u069a\u062a\u0648" + }, + { + "code": "pt", + "name": "Portugu\u00eas" + }, + { + "code": "pt-BR", + "name": "Portugu\u00eas Brasileiro" + }, + { + "code": "ro", + "name": "Rom\u00e2n" + }, + { + "code": "ru", + "name": "\u0440\u0443\u0441\u0441\u043a\u0438\u0439" + }, + { + "code": "rw", + "name": "Kinyarwanda" + }, + { + "code": "si", + "name": "\u0dc3\u0dd2\u0d82\u0dc4\u0dbd" + }, + { + "code": "sk", + "name": "Sloven\u010dina (Slovak)" + }, + { + "code": "sl", + "name": "Sloven\u0161\u010dina (Slovene)" + }, + { + "code": "sq", + "name": "Shqiptar" + }, + { + "code": "sr", + "name": "\u0441\u0440\u043f\u0441\u043a\u0438" + }, + { + "code": "sr-BA", + "name": "Srpski" + }, + { + "code": "sv", + "name": "Svenska" + }, + { + "code": "sw", + "name": "Swahili" + }, + { + "code": "ta", + "name": "\u0ba4\u0bae\u0bbf\u0bb4\u0bcd" + }, + { + "code": "te", + "name": "\u0c24\u0c46\u0c32\u0c41\u0c17\u0c41" + }, + { + "code": "th", + "name": "\u0e44\u0e17\u0e22" + }, + { + "code": "tr", + "name": "Türkçe" + }, + { + "code": "uk", + "name": "\u0443\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430" + }, + { + "code": "ur", + "name": "\u0627\u0631\u062f\u0648" + }, + { + "code": "uz", + "name": "\u040e\u0437\u0431\u0435\u043a" + }, + { + "code": "vi", + "name": "Vi\u1ec7t" + }, + { + "code": "zh", + "name": "\u7b80\u4f53\u4e2d\u6587" + }, + { + "code": "zh-TW", + "name": "\u7e41\u9ad4\u4e2d\u6587" + } +] diff --git a/xhiveframework/geo/report/__init__.py b/xhiveframework/geo/report/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/geo/utils.py b/xhiveframework/geo/utils.py new file mode 100644 index 0000000..5fee53e --- /dev/null +++ b/xhiveframework/geo/utils.py @@ -0,0 +1,99 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework + + +@xhiveframework.whitelist() +def get_coords(doctype, filters, type): + """Get a geojson dict representing a doctype.""" + filters_sql = get_coords_conditions(doctype, filters)[4:] + + coords = None + if type == "location_field": + coords = return_location(doctype, filters_sql) + elif type == "coordinates": + coords = return_coordinates(doctype, filters_sql) + + return convert_to_geojson(type, coords) + + +def convert_to_geojson(type, coords): + """Converts GPS coordinates to geoJSON string.""" + geojson = {"type": "FeatureCollection", "features": None} + + if type == "location_field": + geojson["features"] = merge_location_features_in_one(coords) + elif type == "coordinates": + geojson["features"] = create_gps_markers(coords) + + return geojson + + +def merge_location_features_in_one(coords): + """Merging all features from location field.""" + geojson_dict = [] + for element in coords: + geojson_loc = xhiveframework.parse_json(element["location"]) + if not geojson_loc: + continue + for coord in geojson_loc["features"]: + coord["properties"]["name"] = element["name"] + geojson_dict.append(coord.copy()) + + return geojson_dict + + +def create_gps_markers(coords): + """Build Marker based on latitude and longitude.""" + geojson_dict = [] + for i in coords: + node = {"type": "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates": None}} + node["properties"]["name"] = i.name + node["geometry"]["coordinates"] = [i.longitude, i.latitude] # geojson needs it reverse! + geojson_dict.append(node.copy()) + + return geojson_dict + + +def return_location(doctype, filters_sql): + """Get name and location fields for Doctype.""" + if filters_sql: + try: + coords = xhiveframework.db.sql( + f"""SELECT name, location FROM `tab{doctype}` WHERE {filters_sql}""", as_dict=True + ) + except xhiveframework.db.InternalError: + xhiveframework.msgprint(xhiveframework._("This Doctype does not contain location fields"), raise_exception=True) + return + else: + coords = xhiveframework.get_all(doctype, fields=["name", "location"]) + return coords + + +def return_coordinates(doctype, filters_sql): + """Get name, latitude and longitude fields for Doctype.""" + if filters_sql: + try: + coords = xhiveframework.db.sql( + f"""SELECT name, latitude, longitude FROM `tab{doctype}` WHERE {filters_sql}""", + as_dict=True, + ) + except xhiveframework.db.InternalError: + xhiveframework.msgprint( + xhiveframework._("This Doctype does not contain latitude and longitude fields"), raise_exception=True + ) + return + else: + coords = xhiveframework.get_all(doctype, fields=["name", "latitude", "longitude"]) + return coords + + +def get_coords_conditions(doctype, filters=None): + """Returns SQL conditions with user permissions and filters for event queries.""" + from xhiveframework.desk.reportview import get_filters_cond + + if not xhiveframework.has_permission(doctype): + xhiveframework.throw(xhiveframework._("Not Permitted"), xhiveframework.PermissionError) + + return get_filters_cond(doctype, filters, [], with_match_conditions=True) diff --git a/xhiveframework/gettext/extractors/navbar.py b/xhiveframework/gettext/extractors/navbar.py new file mode 100644 index 0000000..963e8c3 --- /dev/null +++ b/xhiveframework/gettext/extractors/navbar.py @@ -0,0 +1,51 @@ +import importlib +from pathlib import Path + +from xhiveframework.utils import get_bench_path + + +def extract(fileobj, *args, **kwargs): + """Extract standard navbar and help items from a python file. + + :param fileobj: file-like object to extract messages from. Should be a + python file containing two global variables `standard_navbar_items` and + `standard_help_items` which are lists of dicts. + """ + module = get_module(fileobj.name) + + if hasattr(module, "standard_navbar_items"): + standard_navbar_items = module.standard_navbar_items + for nav_item in standard_navbar_items: + if label := nav_item.get("item_label"): + item_type = nav_item.get("item_type") + yield ( + None, + "_", + label, + [ + "Label of a standard navbar item", + f"Type: {item_type}", + ], + ) + + if hasattr(module, "standard_help_items"): + standard_help_items = module.standard_help_items + for help_item in standard_help_items: + if label := help_item.get("item_label"): + item_type = nav_item.get("item_type") + yield ( + None, + "_", + label, + [ + "Label of a standard help item", + f"Type: {item_type}", + ], + ) + + +def get_module(path): + _path = Path(path) + rel_path = _path.relative_to(get_bench_path()) + import_path = ".".join(rel_path.parts[2:]).rstrip(".py") + return importlib.import_module(import_path) diff --git a/xhiveframework/handler.py b/xhiveframework/handler.py new file mode 100644 index 0000000..0add94f --- /dev/null +++ b/xhiveframework/handler.py @@ -0,0 +1,349 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import os +from mimetypes import guess_type +from typing import TYPE_CHECKING + +from werkzeug.wrappers import Response + +import xhiveframework +import xhiveframework.sessions +import xhiveframework.utils +from xhiveframework import _, is_whitelisted, ping +from xhiveframework.core.doctype.server_script.server_script_utils import get_server_script_map +from xhiveframework.monitor import add_data_to_monitor +from xhiveframework.utils import cint +from xhiveframework.utils.csvutils import build_csv_response +from xhiveframework.utils.deprecations import deprecation_warning +from xhiveframework.utils.image import optimize_image +from xhiveframework.utils.response import build_response + +if TYPE_CHECKING: + from xhiveframework.core.doctype.file.file import File + from xhiveframework.core.doctype.user.user import User + +ALLOWED_MIMETYPES = ( + "image/png", + "image/jpeg", + "application/pdf", + "application/msword", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/vnd.ms-excel", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.oasis.opendocument.text", + "application/vnd.oasis.opendocument.spreadsheet", + "text/plain", + "video/quicktime", + "video/mp4", +) + + +def handle(): + """handle request""" + + cmd = xhiveframework.local.form_dict.cmd + data = None + + if cmd != "login": + data = execute_cmd(cmd) + + # data can be an empty string or list which are valid responses + if data is not None: + if isinstance(data, Response): + # method returns a response object, pass it on + return data + + # add the response to `message` label + xhiveframework.response["message"] = data + + +def execute_cmd(cmd, from_async=False): + """execute a request as python module""" + for hook in reversed(xhiveframework.get_hooks("override_whitelisted_methods", {}).get(cmd, [])): + # override using the last hook + cmd = hook + break + + # via server script + server_script = get_server_script_map().get("_api", {}).get(cmd) + if server_script: + return run_server_script(server_script) + + try: + method = get_attr(cmd) + except Exception as e: + xhiveframework.throw(_("Failed to get method for command {0} with {1}").format(cmd, e)) + + if from_async: + method = method.queue + + if method != run_doc_method: + is_whitelisted(method) + is_valid_http_method(method) + + return xhiveframework.call(method, **xhiveframework.form_dict) + + +def run_server_script(server_script): + response = xhiveframework.get_doc("Server Script", server_script).execute_method() + + # some server scripts return output using flags (empty dict by default), + # while others directly modify xhiveframework.response + # return flags if not empty dict (this overwrites xhiveframework.response.message) + if response != {}: + return response + + +def is_valid_http_method(method): + if xhiveframework.flags.in_safe_exec: + return + + http_method = xhiveframework.local.request.method + + if http_method not in xhiveframework.allowed_http_methods_for_whitelisted_func[method]: + throw_permission_error() + + +def throw_permission_error(): + xhiveframework.throw(_("Not permitted"), xhiveframework.PermissionError) + + +@xhiveframework.whitelist(allow_guest=True) +def logout(): + xhiveframework.local.login_manager.logout() + xhiveframework.db.commit() + + +@xhiveframework.whitelist(allow_guest=True) +def web_logout(): + xhiveframework.local.login_manager.logout() + xhiveframework.db.commit() + xhiveframework.respond_as_web_page( + _("Logged Out"), _("You have been successfully logged out"), indicator_color="green" + ) + + +@xhiveframework.whitelist() +def uploadfile(): + ret = None + check_write_permission(xhiveframework.form_dict.doctype, xhiveframework.form_dict.docname) + + try: + if xhiveframework.form_dict.get("from_form"): + try: + ret = xhiveframework.get_doc( + { + "doctype": "File", + "attached_to_name": xhiveframework.form_dict.docname, + "attached_to_doctype": xhiveframework.form_dict.doctype, + "attached_to_field": xhiveframework.form_dict.docfield, + "file_url": xhiveframework.form_dict.file_url, + "file_name": xhiveframework.form_dict.filename, + "is_private": xhiveframework.utils.cint(xhiveframework.form_dict.is_private), + "content": xhiveframework.form_dict.filedata, + "decode": True, + } + ) + ret.save() + except xhiveframework.DuplicateEntryError: + # ignore pass + ret = None + xhiveframework.db.rollback() + else: + if xhiveframework.form_dict.get("method"): + method = xhiveframework.get_attr(xhiveframework.form_dict.method) + is_whitelisted(method) + ret = method() + except Exception: + xhiveframework.errprint(xhiveframework.utils.get_traceback()) + xhiveframework.response["http_status_code"] = 500 + ret = None + + return ret + + +@xhiveframework.whitelist(allow_guest=True) +def upload_file(): + user = None + if xhiveframework.session.user == "Guest": + if xhiveframework.get_system_settings("allow_guests_to_upload_files"): + ignore_permissions = True + else: + raise xhiveframework.PermissionError + else: + user: "User" = xhiveframework.get_doc("User", xhiveframework.session.user) + ignore_permissions = False + + files = xhiveframework.request.files + is_private = xhiveframework.form_dict.is_private + doctype = xhiveframework.form_dict.doctype + docname = xhiveframework.form_dict.docname + fieldname = xhiveframework.form_dict.fieldname + file_url = xhiveframework.form_dict.file_url + folder = xhiveframework.form_dict.folder or "Home" + method = xhiveframework.form_dict.method + filename = xhiveframework.form_dict.file_name + optimize = xhiveframework.form_dict.optimize + content = None + + if library_file := xhiveframework.form_dict.get("library_file_name"): + xhiveframework.has_permission("File", doc=library_file, throw=True) + doc = xhiveframework.get_value( + "File", + xhiveframework.form_dict.library_file_name, + ["is_private", "file_url", "file_name"], + as_dict=True, + ) + is_private = doc.is_private + file_url = doc.file_url + filename = doc.file_name + + if not ignore_permissions: + check_write_permission(doctype, docname) + + if "file" in files: + file = files["file"] + content = file.stream.read() + filename = file.filename + + content_type = guess_type(filename)[0] + if optimize and content_type and content_type.startswith("image/"): + args = {"content": content, "content_type": content_type} + if xhiveframework.form_dict.max_width: + args["max_width"] = int(xhiveframework.form_dict.max_width) + if xhiveframework.form_dict.max_height: + args["max_height"] = int(xhiveframework.form_dict.max_height) + content = optimize_image(**args) + + xhiveframework.local.uploaded_file = content + xhiveframework.local.uploaded_filename = filename + + if content is not None and (xhiveframework.session.user == "Guest" or (user and not user.has_desk_access())): + filetype = guess_type(filename)[0] + if filetype not in ALLOWED_MIMETYPES: + xhiveframework.throw(_("You can only upload JPG, PNG, PDF, TXT or Microsoft documents.")) + + if method: + method = xhiveframework.get_attr(method) + is_whitelisted(method) + return method() + else: + return xhiveframework.get_doc( + { + "doctype": "File", + "attached_to_doctype": doctype, + "attached_to_name": docname, + "attached_to_field": fieldname, + "folder": folder, + "file_name": filename, + "file_url": file_url, + "is_private": cint(is_private), + "content": content, + } + ).save(ignore_permissions=ignore_permissions) + + +def check_write_permission(doctype: str | None = None, name: str | None = None): + check_doctype = doctype and not name + if doctype and name: + try: + doc = xhiveframework.get_doc(doctype, name) + doc.has_permission("write") + except xhiveframework.DoesNotExistError: + # doc has not been inserted yet, name is set to "new-some-doctype" + check_doctype = True + + if check_doctype: + xhiveframework.has_permission(doctype, "write", throw=True) + + +@xhiveframework.whitelist(allow_guest=True) +def download_file(file_url: str): + """ + Download file using token and REST API. Valid session or + token is required to download private files. + + Method : GET + Endpoints : download_file, xhiveframework.core.doctype.file.file.download_file + URL Params : file_name = /path/to/file relative to site path + """ + file: "File" = xhiveframework.get_doc("File", {"file_url": file_url}) + if not file.is_downloadable(): + raise xhiveframework.PermissionError + + xhiveframework.local.response.filename = os.path.basename(file_url) + xhiveframework.local.response.filecontent = file.get_content() + xhiveframework.local.response.type = "download" + + +def get_attr(cmd): + """get method object from cmd""" + if "." in cmd: + method = xhiveframework.get_attr(cmd) + else: + deprecation_warning( + f"Calling shorthand for {cmd} is deprecated, please specify full path in RPC call." + ) + method = globals()[cmd] + return method + + +def run_doc_method(method, docs=None, dt=None, dn=None, arg=None, args=None): + """run a whitelisted controller method""" + from inspect import signature + + if not args and arg: + args = arg + + if dt: # not called from a doctype (from a page) + if not dn: + dn = dt # single + doc = xhiveframework.get_doc(dt, dn) + + else: + docs = xhiveframework.parse_json(docs) + doc = xhiveframework.get_doc(docs) + doc._original_modified = doc.modified + doc.check_if_latest() + + if not doc or not doc.has_permission("read"): + throw_permission_error() + + try: + args = xhiveframework.parse_json(args) + except ValueError: + pass + + method_obj = getattr(doc, method) + fn = getattr(method_obj, "__func__", method_obj) + is_whitelisted(fn) + is_valid_http_method(fn) + + fnargs = list(signature(method_obj).parameters) + + if not fnargs or (len(fnargs) == 1 and fnargs[0] == "self"): + response = doc.run_method(method) + + elif "args" in fnargs or not isinstance(args, dict): + response = doc.run_method(method, args) + + else: + response = doc.run_method(method, **args) + + xhiveframework.response.docs.append(doc) + if response is None: + return + + # build output as csv + if cint(xhiveframework.form_dict.get("as_csv")): + build_csv_response(response, _(doc.doctype).replace(" ", "")) + return + + xhiveframework.response["message"] = response + + add_data_to_monitor(methodname=method) + + +# for backwards compatibility +runserverobj = run_doc_method diff --git a/xhiveframework/hooks.py b/xhiveframework/hooks.py new file mode 100644 index 0000000..e57eb8b --- /dev/null +++ b/xhiveframework/hooks.py @@ -0,0 +1,473 @@ +import os + +from . import __version__ as app_version + +app_name = "xhiveframework" +app_title = "Xhive Framework" +app_publisher = "XhiveFramework Technologies" +app_description = "Full stack web framework with Python, Javascript, MariaDB, Redis, Node" +source_link = "https://lab.membtech.com/xhiveframework/xhiveframework15" +app_license = "MIT" +app_logo_url = "/assets/xhiveframework/images/xhiveframework-framework-logo.svg" + +develop_version = "15.x.x-develop" + +app_email = "developers@xhiveframework.io" + +docs_app = "xhiveframework_docs" + +translator_url = "https://translate.xhiveerp.com" + +before_install = "xhiveframework.utils.install.before_install" +after_install = "xhiveframework.utils.install.after_install" + +page_js = {"setup-wizard": "public/js/xhiveframework/setup_wizard.js"} + +# website +app_include_js = [ + "libs.bundle.js", + "desk.bundle.js", + "list.bundle.js", + "form.bundle.js", + "controls.bundle.js", + "report.bundle.js", + "telemetry.bundle.js", +] + +app_include_css = [ + "desk.bundle.css", + "report.bundle.css", +] +app_include_icons = [ + "xhiveframework/icons/timeless/icons.svg", + "xhiveframework/icons/espresso/icons.svg", +] + +doctype_js = { + "Web Page": "public/js/xhiveframework/utils/web_template.js", + "Website Settings": "public/js/xhiveframework/utils/web_template.js", +} + +web_include_js = ["website_script.js"] + +web_include_css = [] + +email_css = ["email.bundle.css"] + +website_route_rules = [ + {"from_route": "/blog/", "to_route": "Blog Post"}, + {"from_route": "/kb/", "to_route": "Help Article"}, + {"from_route": "/newsletters", "to_route": "Newsletter"}, + {"from_route": "/profile", "to_route": "me"}, + {"from_route": "/app/", "to_route": "app"}, +] + +website_redirects = [ + {"source": r"/desk(.*)", "target": r"/app\1"}, + { + "source": "/.well-known/openid-configuration", + "target": "/api/method/xhiveframework.integrations.oauth2.openid_configuration", + }, +] + +base_template = "templates/base.html" + +write_file_keys = ["file_url", "file_name"] + +notification_config = "xhiveframework.core.notifications.get_notification_config" + +before_tests = "xhiveframework.utils.install.before_tests" + +email_append_to = ["Event", "ToDo", "Communication"] + +calendars = ["Event"] + +leaderboards = "xhiveframework.desk.leaderboard.get_leaderboards" + +# login + +on_session_creation = [ + "xhiveframework.core.doctype.activity_log.feed.login_feed", + "xhiveframework.core.doctype.user.user.notify_admin_access_to_system_manager", +] + +on_logout = "xhiveframework.core.doctype.session_default_settings.session_default_settings.clear_session_defaults" + +# PDF +pdf_header_html = "xhiveframework.utils.pdf.pdf_header_html" +pdf_body_html = "xhiveframework.utils.pdf.pdf_body_html" +pdf_footer_html = "xhiveframework.utils.pdf.pdf_footer_html" + +# permissions + +permission_query_conditions = { + "Event": "xhiveframework.desk.doctype.event.event.get_permission_query_conditions", + "ToDo": "xhiveframework.desk.doctype.todo.todo.get_permission_query_conditions", + "User": "xhiveframework.core.doctype.user.user.get_permission_query_conditions", + "Dashboard Settings": "xhiveframework.desk.doctype.dashboard_settings.dashboard_settings.get_permission_query_conditions", + "Notification Log": "xhiveframework.desk.doctype.notification_log.notification_log.get_permission_query_conditions", + "Dashboard": "xhiveframework.desk.doctype.dashboard.dashboard.get_permission_query_conditions", + "Dashboard Chart": "xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.get_permission_query_conditions", + "Number Card": "xhiveframework.desk.doctype.number_card.number_card.get_permission_query_conditions", + "Notification Settings": "xhiveframework.desk.doctype.notification_settings.notification_settings.get_permission_query_conditions", + "Note": "xhiveframework.desk.doctype.note.note.get_permission_query_conditions", + "Kanban Board": "xhiveframework.desk.doctype.kanban_board.kanban_board.get_permission_query_conditions", + "Contact": "xhiveframework.contacts.address_and_contact.get_permission_query_conditions_for_contact", + "Address": "xhiveframework.contacts.address_and_contact.get_permission_query_conditions_for_address", + "Communication": "xhiveframework.core.doctype.communication.communication.get_permission_query_conditions_for_communication", + "Workflow Action": "xhiveframework.workflow.doctype.workflow_action.workflow_action.get_permission_query_conditions", + "Prepared Report": "xhiveframework.core.doctype.prepared_report.prepared_report.get_permission_query_condition", + "File": "xhiveframework.core.doctype.file.file.get_permission_query_conditions", +} + +has_permission = { + "Event": "xhiveframework.desk.doctype.event.event.has_permission", + "ToDo": "xhiveframework.desk.doctype.todo.todo.has_permission", + "Note": "xhiveframework.desk.doctype.note.note.has_permission", + "User": "xhiveframework.core.doctype.user.user.has_permission", + "Dashboard Chart": "xhiveframework.desk.doctype.dashboard_chart.dashboard_chart.has_permission", + "Number Card": "xhiveframework.desk.doctype.number_card.number_card.has_permission", + "Kanban Board": "xhiveframework.desk.doctype.kanban_board.kanban_board.has_permission", + "Contact": "xhiveframework.contacts.address_and_contact.has_permission", + "Address": "xhiveframework.contacts.address_and_contact.has_permission", + "Communication": "xhiveframework.core.doctype.communication.communication.has_permission", + "Workflow Action": "xhiveframework.workflow.doctype.workflow_action.workflow_action.has_permission", + "File": "xhiveframework.core.doctype.file.file.has_permission", + "Prepared Report": "xhiveframework.core.doctype.prepared_report.prepared_report.has_permission", + "Notification Settings": "xhiveframework.desk.doctype.notification_settings.notification_settings.has_permission", +} + +has_website_permission = {"Address": "xhiveframework.contacts.doctype.address.address.has_website_permission"} + +jinja = { + "methods": "xhiveframework.utils.jinja_globals", + "filters": [ + "xhiveframework.utils.data.global_date_format", + "xhiveframework.utils.markdown", + "xhiveframework.website.utils.abs_url", + ], +} + +standard_queries = {"User": "xhiveframework.core.doctype.user.user.user_query"} + +doc_events = { + "*": { + "on_update": [ + "xhiveframework.desk.notifications.clear_doctype_notifications", + "xhiveframework.workflow.doctype.workflow_action.workflow_action.process_workflow_actions", + "xhiveframework.core.doctype.file.utils.attach_files_to_document", + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.apply", + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.update_due_date", + "xhiveframework.core.doctype.user_type.user_type.apply_permissions_for_non_standard_user_type", + ], + "after_rename": "xhiveframework.desk.notifications.clear_doctype_notifications", + "on_cancel": [ + "xhiveframework.desk.notifications.clear_doctype_notifications", + "xhiveframework.workflow.doctype.workflow_action.workflow_action.process_workflow_actions", + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.apply", + ], + "on_trash": [ + "xhiveframework.desk.notifications.clear_doctype_notifications", + "xhiveframework.workflow.doctype.workflow_action.workflow_action.process_workflow_actions", + ], + "on_update_after_submit": [ + "xhiveframework.workflow.doctype.workflow_action.workflow_action.process_workflow_actions", + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.apply", + "xhiveframework.automation.doctype.assignment_rule.assignment_rule.update_due_date", + "xhiveframework.core.doctype.file.utils.attach_files_to_document", + ], + "on_change": [ + "xhiveframework.social.doctype.energy_point_rule.energy_point_rule.process_energy_points", + "xhiveframework.automation.doctype.milestone_tracker.milestone_tracker.evaluate_milestone", + ], + }, + "Event": { + "after_insert": "xhiveframework.integrations.doctype.google_calendar.google_calendar.insert_event_in_google_calendar", + "on_update": "xhiveframework.integrations.doctype.google_calendar.google_calendar.update_event_in_google_calendar", + "on_trash": "xhiveframework.integrations.doctype.google_calendar.google_calendar.delete_event_from_google_calendar", + }, + "Contact": { + "after_insert": "xhiveframework.integrations.doctype.google_contacts.google_contacts.insert_contacts_to_google_contacts", + "on_update": "xhiveframework.integrations.doctype.google_contacts.google_contacts.update_contacts_to_google_contacts", + }, + "DocType": { + "on_update": "xhiveframework.cache_manager.build_domain_restriced_doctype_cache", + }, + "Page": { + "on_update": "xhiveframework.cache_manager.build_domain_restriced_page_cache", + }, +} + +scheduler_events = { + "cron": { + "0/15 * * * *": [ + "xhiveframework.oauth.delete_oauth2_data", + "xhiveframework.website.doctype.web_page.web_page.check_publish_status", + "xhiveframework.twofactor.delete_all_barcodes_for_users", + ], + "0/10 * * * *": [ + "xhiveframework.email.doctype.email_account.email_account.pull", + ], + # Hourly but offset by 30 minutes + "30 * * * *": [ + "xhiveframework.core.doctype.prepared_report.prepared_report.expire_stalled_report", + ], + # Daily but offset by 45 minutes + "45 0 * * *": [ + "xhiveframework.core.doctype.log_settings.log_settings.run_log_clean_up", + ], + }, + "all": [ + "xhiveframework.email.queue.flush", + "xhiveframework.email.doctype.email_account.email_account.notify_unreplied", + "xhiveframework.utils.global_search.sync_global_search", + "xhiveframework.monitor.flush", + "xhiveframework.automation.doctype.reminder.reminder.send_reminders", + ], + "hourly": [ + "xhiveframework.model.utils.link_count.update_link_count", + "xhiveframework.model.utils.user_settings.sync_user_settings", + "xhiveframework.desk.page.backups.backups.delete_downloadable_backups", + "xhiveframework.deferred_insert.save_to_db", + "xhiveframework.desk.form.document_follow.send_hourly_updates", + "xhiveframework.integrations.doctype.google_calendar.google_calendar.sync", + "xhiveframework.email.doctype.newsletter.newsletter.send_scheduled_email", + "xhiveframework.website.doctype.personal_data_deletion_request.personal_data_deletion_request.process_data_deletion_request", + ], + "daily": [ + "xhiveframework.desk.notifications.clear_notifications", + "xhiveframework.desk.doctype.event.event.send_event_digest", + "xhiveframework.sessions.clear_expired_sessions", + "xhiveframework.email.doctype.notification.notification.trigger_daily_alerts", + "xhiveframework.website.doctype.personal_data_deletion_request.personal_data_deletion_request.remove_unverified_record", + "xhiveframework.desk.form.document_follow.send_daily_updates", + "xhiveframework.social.doctype.energy_point_settings.energy_point_settings.allocate_review_points", + "xhiveframework.integrations.doctype.google_contacts.google_contacts.sync", + "xhiveframework.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat_entry", + "xhiveframework.automation.doctype.auto_repeat.auto_repeat.set_auto_repeat_as_completed", + ], + "daily_long": [ + "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.take_backups_daily", + "xhiveframework.utils.change_log.check_for_update", + "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_daily", + "xhiveframework.email.doctype.auto_email_report.auto_email_report.send_daily", + "xhiveframework.integrations.doctype.google_drive.google_drive.daily_backup", + ], + "weekly_long": [ + "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.take_backups_weekly", + "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_weekly", + "xhiveframework.desk.form.document_follow.send_weekly_updates", + "xhiveframework.social.doctype.energy_point_log.energy_point_log.send_weekly_summary", + "xhiveframework.integrations.doctype.google_drive.google_drive.weekly_backup", + ], + "monthly": [ + "xhiveframework.email.doctype.auto_email_report.auto_email_report.send_monthly", + "xhiveframework.social.doctype.energy_point_log.energy_point_log.send_monthly_summary", + ], + "monthly_long": [ + "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_monthly" + ], +} + +get_translated_dict = { + ("doctype", "System Settings"): "xhiveframework.geo.country_info.get_translated_dict", + ("page", "setup-wizard"): "xhiveframework.geo.country_info.get_translated_dict", +} + +sounds = [ + {"name": "email", "src": "/assets/xhiveframework/sounds/email.mp3", "volume": 0.1}, + {"name": "submit", "src": "/assets/xhiveframework/sounds/submit.mp3", "volume": 0.1}, + {"name": "cancel", "src": "/assets/xhiveframework/sounds/cancel.mp3", "volume": 0.1}, + {"name": "delete", "src": "/assets/xhiveframework/sounds/delete.mp3", "volume": 0.05}, + {"name": "click", "src": "/assets/xhiveframework/sounds/click.mp3", "volume": 0.05}, + {"name": "error", "src": "/assets/xhiveframework/sounds/error.mp3", "volume": 0.1}, + {"name": "alert", "src": "/assets/xhiveframework/sounds/alert.mp3", "volume": 0.2}, + # {"name": "chime", "src": "/assets/xhiveframework/sounds/chime.mp3"}, +] + +setup_wizard_exception = [ + "xhiveframework.desk.page.setup_wizard.setup_wizard.email_setup_wizard_exception", + "xhiveframework.desk.page.setup_wizard.setup_wizard.log_setup_wizard_exception", +] + +before_migrate = ["xhiveframework.core.doctype.patch_log.patch_log.before_migrate"] +after_migrate = ["xhiveframework.website.doctype.website_theme.website_theme.after_migrate"] + +otp_methods = ["OTP App", "Email", "SMS"] + +user_data_fields = [ + {"doctype": "Access Log", "strict": True}, + {"doctype": "Activity Log", "strict": True}, + {"doctype": "Comment", "strict": True}, + { + "doctype": "Contact", + "filter_by": "email_id", + "redact_fields": ["first_name", "last_name", "phone", "mobile_no"], + "rename": True, + }, + {"doctype": "Contact Email", "filter_by": "email_id"}, + { + "doctype": "Address", + "filter_by": "email_id", + "redact_fields": [ + "address_title", + "address_line1", + "address_line2", + "city", + "county", + "state", + "pincode", + "phone", + "fax", + ], + }, + { + "doctype": "Communication", + "filter_by": "sender", + "redact_fields": ["sender_full_name", "phone_no", "content"], + }, + {"doctype": "Communication", "filter_by": "recipients"}, + {"doctype": "Email Group Member", "filter_by": "email"}, + {"doctype": "Email Unsubscribe", "filter_by": "email", "partial": True}, + {"doctype": "Email Queue", "filter_by": "sender"}, + {"doctype": "Email Queue Recipient", "filter_by": "recipient"}, + { + "doctype": "File", + "filter_by": "attached_to_name", + "redact_fields": ["file_name", "file_url"], + }, + { + "doctype": "User", + "filter_by": "name", + "redact_fields": [ + "email", + "username", + "first_name", + "middle_name", + "last_name", + "full_name", + "birth_date", + "user_image", + "phone", + "mobile_no", + "location", + "banner_image", + "interest", + "bio", + "email_signature", + ], + }, + {"doctype": "Version", "strict": True}, +] + +global_search_doctypes = { + "Default": [ + {"doctype": "Contact"}, + {"doctype": "Address"}, + {"doctype": "ToDo"}, + {"doctype": "Note"}, + {"doctype": "Event"}, + {"doctype": "Blog Post"}, + {"doctype": "Dashboard"}, + {"doctype": "Country"}, + {"doctype": "Currency"}, + {"doctype": "Newsletter"}, + {"doctype": "Letter Head"}, + {"doctype": "Workflow"}, + {"doctype": "Web Page"}, + {"doctype": "Web Form"}, + ] +} + +override_whitelisted_methods = { + # Legacy File APIs + "xhiveframework.core.doctype.file.file.download_file": "download_file", + "xhiveframework.core.doctype.file.file.unzip_file": "xhiveframework.core.api.file.unzip_file", + "xhiveframework.core.doctype.file.file.get_attached_images": "xhiveframework.core.api.file.get_attached_images", + "xhiveframework.core.doctype.file.file.get_files_in_folder": "xhiveframework.core.api.file.get_files_in_folder", + "xhiveframework.core.doctype.file.file.get_files_by_search_text": "xhiveframework.core.api.file.get_files_by_search_text", + "xhiveframework.core.doctype.file.file.get_max_file_size": "xhiveframework.core.api.file.get_max_file_size", + "xhiveframework.core.doctype.file.file.create_new_folder": "xhiveframework.core.api.file.create_new_folder", + "xhiveframework.core.doctype.file.file.move_file": "xhiveframework.core.api.file.move_file", + "xhiveframework.core.doctype.file.file.zip_files": "xhiveframework.core.api.file.zip_files", + # Legacy (& Consistency) OAuth2 APIs + "xhiveframework.www.login.login_via_google": "xhiveframework.integrations.oauth2_logins.login_via_google", + "xhiveframework.www.login.login_via_github": "xhiveframework.integrations.oauth2_logins.login_via_github", + "xhiveframework.www.login.login_via_facebook": "xhiveframework.integrations.oauth2_logins.login_via_facebook", + "xhiveframework.www.login.login_via_xhiveframework": "xhiveframework.integrations.oauth2_logins.login_via_xhiveframework", + "xhiveframework.www.login.login_via_office365": "xhiveframework.integrations.oauth2_logins.login_via_office365", + "xhiveframework.www.login.login_via_salesforce": "xhiveframework.integrations.oauth2_logins.login_via_salesforce", + "xhiveframework.www.login.login_via_fairlogin": "xhiveframework.integrations.oauth2_logins.login_via_fairlogin", +} + +ignore_links_on_delete = [ + "Communication", + "ToDo", + "DocShare", + "Email Unsubscribe", + "Activity Log", + "File", + "Version", + "Document Follow", + "Comment", + "View Log", + "Tag Link", + "Notification Log", + "Email Queue", + "Document Share Key", + "Integration Request", + "Unhandled Email", + "Webhook Request Log", + "Workspace", + "Route History", + "Access Log", +] + +# Request Hooks +before_request = [ + "xhiveframework.recorder.record", + "xhiveframework.monitor.start", + "xhiveframework.rate_limiter.apply", +] + +# Background Job Hooks +before_job = [ + "xhiveframework.recorder.record", + "xhiveframework.monitor.start", +] + +if os.getenv("XHIVEFRAMEWORK_SENTRY_DSN") and ( + os.getenv("ENABLE_SENTRY_DB_MONITORING") or os.getenv("SENTRY_TRACING_SAMPLE_RATE") +): + before_request.append("xhiveframework.utils.sentry.set_sentry_context") + before_job.append("xhiveframework.utils.sentry.set_sentry_context") + +after_job = [ + "xhiveframework.recorder.dump", + "xhiveframework.monitor.stop", + "xhiveframework.utils.file_lock.release_document_locks", + "xhiveframework.utils.background_jobs.flush_telemetry", +] + +extend_bootinfo = [ + "xhiveframework.utils.telemetry.add_bootinfo", + "xhiveframework.core.doctype.user_permission.user_permission.send_user_permissions", + "xhiveframework.utils.sentry.add_bootinfo", +] + +export_python_type_annotations = True + +# log doctype cleanups to automatically add in log settings +default_log_clearing_doctypes = { + "Error Log": 14, + "Email Queue": 30, + "Scheduled Job Log": 7, + "Submission Queue": 7, + "Prepared Report": 14, + "Webhook Request Log": 30, + "Unhandled Email": 30, + "Reminder": 30, + "Integration Request": 90, + "Activity Log": 90, + "Route History": 90, +} diff --git a/xhiveframework/installer.py b/xhiveframework/installer.py new file mode 100644 index 0000000..a0df017 --- /dev/null +++ b/xhiveframework/installer.py @@ -0,0 +1,869 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import configparser +import gzip +import json +import os +import re +import subprocess +import sys +from collections import OrderedDict +from contextlib import suppress +from shutil import which + +import click +from semantic_version import Version + +import xhiveframework +from xhiveframework.defaults import _clear_cache +from xhiveframework.utils import cint, is_git_url +from xhiveframework.utils.dashboard import sync_dashboards +from xhiveframework.utils.synchronization import filelock + + +def _is_scheduler_enabled() -> bool: + enable_scheduler = False + try: + xhiveframework.connect() + enable_scheduler = cint(xhiveframework.db.get_single_value("System Settings", "enable_scheduler")) + except Exception: + pass + finally: + xhiveframework.db.close() + + return bool(enable_scheduler) + + +def _new_site( + db_name, + site, + db_root_username=None, + db_root_password=None, + admin_password=None, + verbose=False, + install_apps=None, + source_sql=None, + force=False, + no_mariadb_socket=False, + reinstall=False, + db_password=None, + db_type=None, + db_host=None, + db_port=None, +): + """Install a new XhiveFramework site""" + + from xhiveframework.utils import scheduler + + if not force and os.path.exists(site): + print(f"Site {site} already exists") + sys.exit(1) + + if no_mariadb_socket and not db_type == "mariadb": + print("--no-mariadb-socket requires db_type to be set to mariadb.") + sys.exit(1) + + xhiveframework.init(site=site) + + if not db_name: + import hashlib + + db_name = ( + "_" + + hashlib.sha1( + os.path.realpath(xhiveframework.get_site_path()).encode(), usedforsecurity=False + ).hexdigest()[:16] + ) + + try: + # enable scheduler post install? + enable_scheduler = _is_scheduler_enabled() + except Exception: + enable_scheduler = False + + make_site_dirs() + + with filelock("bench_new_site", timeout=1): + install_db( + root_login=db_root_username, + root_password=db_root_password, + db_name=db_name, + admin_password=admin_password, + verbose=verbose, + source_sql=source_sql, + force=force, + reinstall=reinstall, + db_password=db_password, + db_type=db_type, + db_host=db_host, + db_port=db_port, + no_mariadb_socket=no_mariadb_socket, + ) + + apps_to_install = ["xhiveframework"] + (xhiveframework.conf.get("install_apps") or []) + (list(install_apps) or []) + + for app in apps_to_install: + # NOTE: not using force here for 2 reasons: + # 1. It's not really needed here as we've freshly installed a new db + # 2. If someone uses a sql file to do restore and that file already had + # installed_apps then it might cause problems as that sql file can be of any previous version(s) + # which might be incompatible with the current version and using force might cause problems. + # Example: the DocType DocType might not have `migration_hash` column which will cause failure in the restore. + install_app(app, verbose=verbose, set_as_patched=not source_sql, force=False) + + scheduler.toggle_scheduler(enable_scheduler) + xhiveframework.db.commit() + + scheduler_status = "disabled" if xhiveframework.utils.scheduler.is_scheduler_disabled() else "enabled" + print("*** Scheduler is", scheduler_status, "***") + + +def install_db( + root_login=None, + root_password=None, + db_name=None, + source_sql=None, + admin_password=None, + verbose=True, + force=0, + site_config=None, + reinstall=False, + db_password=None, + db_type=None, + db_host=None, + db_port=None, + no_mariadb_socket=False, +): + import xhiveframework.database + from xhiveframework.database import setup_database + + if not db_type: + db_type = xhiveframework.conf.db_type + + if not root_login and db_type == "mariadb": + root_login = "root" + elif not root_login and db_type == "postgres": + root_login = "postgres" + + make_conf( + db_name, + site_config=site_config, + db_password=db_password, + db_type=db_type, + db_host=db_host, + db_port=db_port, + ) + xhiveframework.flags.in_install_db = True + + xhiveframework.flags.root_login = root_login + xhiveframework.flags.root_password = root_password + setup_database(force, source_sql, verbose, no_mariadb_socket) + + xhiveframework.conf.admin_password = xhiveframework.conf.admin_password or admin_password + + remove_missing_apps() + + xhiveframework.db.create_auth_table() + xhiveframework.db.create_global_search_table() + xhiveframework.db.create_user_settings_table() + + xhiveframework.flags.in_install_db = False + + +def find_org(org_repo: str) -> tuple[str, str]: + """find the org a repo is in + + find_org() + ref -> https://lab.membtech.com/xhiveframework/bench_new/blob/develop/bench/utils/__init__.py#L390 + + :param org_repo: + :type org_repo: str + + :raises InvalidRemoteException: if the org is not found + + :return: organisation and repository + :rtype: Tuple[str, str] + """ + import requests + + from xhiveframework.exceptions import InvalidRemoteException + + for org in ["xhiveframework", "xhiveerp"]: + response = requests.head(f"https://api.github.com/repos/{org}/{org_repo}") + if response.status_code == 400: + response = requests.head(f"https://github.com/{org}/{org_repo}") + if response.ok: + return org, org_repo + + raise InvalidRemoteException + + +def fetch_details_from_tag(_tag: str) -> tuple[str, str, str]: + """parse org, repo, tag from string + + fetch_details_from_tag() + ref -> https://lab.membtech.com/xhiveframework/bench_new/blob/develop/bench/utils/__init__.py#L403 + + :param _tag: input string + :type _tag: str + + :return: organisation, repostitory, tag + :rtype: Tuple[str, str, str] + """ + app_tag = _tag.split("@") + org_repo = app_tag[0].split("/") + + try: + repo, tag = app_tag + except ValueError: + repo, tag = [*app_tag, None] + + try: + org, repo = org_repo + except Exception: + org, repo = find_org(org_repo[0]) + + return org, repo, tag + + +def parse_app_name(name: str) -> str: + """parse repo name from name + + __setup_details_from_git() + ref -> https://lab.membtech.com/xhiveframework/bench_new/blob/develop/bench/app.py#L114 + + + :param name: git tag + :type name: str + + :return: repository name + :rtype: str + """ + name = name.rstrip("/") + if os.path.exists(name): + repo = os.path.split(name)[-1] + elif is_git_url(name): + if name.startswith("git@") or name.startswith("ssh://"): + _repo = name.split(":")[1].rsplit("/", 1)[1] + else: + _repo = name.rsplit("/", 2)[2] + repo = _repo.split(".", 1)[0] + else: + _, repo, _ = fetch_details_from_tag(name) + return repo + + +def install_app(name, verbose=False, set_as_patched=True, force=False): + from xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs + from xhiveframework.model.sync import sync_for + from xhiveframework.modules.utils import sync_customizations + from xhiveframework.utils.fixtures import sync_fixtures + + xhiveframework.flags.in_install = name + xhiveframework.flags.ignore_in_install = False + + xhiveframework.clear_cache() + app_hooks = xhiveframework.get_hooks(app_name=name) + installed_apps = xhiveframework.get_installed_apps() + + # install pre-requisites + if app_hooks.required_apps: + for app in app_hooks.required_apps: + required_app = parse_app_name(app) + install_app(required_app, verbose=verbose) + + xhiveframework.flags.in_install = name + xhiveframework.clear_cache() + + if name not in xhiveframework.get_all_apps(): + raise Exception(f"App {name} not in apps.txt") + + if not force and name in installed_apps: + click.secho(f"App {name} already installed", fg="yellow") + return + + print(f"\nInstalling {name}...") + + if name != "xhiveframework": + xhiveframework.only_for("System Manager") + + for before_install in app_hooks.before_install or []: + out = xhiveframework.get_attr(before_install)() + if out is False: + return + + for fn in xhiveframework.get_hooks("before_app_install"): + xhiveframework.get_attr(fn)(name) + + if name != "xhiveframework": + add_module_defs(name, ignore_if_duplicate=force) + + sync_for(name, force=force, reset_permissions=True) + + add_to_installed_apps(name) + + xhiveframework.get_doc("Portal Settings", "Portal Settings").sync_menu() + + if set_as_patched: + set_all_patches_as_completed(name) + + for after_install in app_hooks.after_install or []: + xhiveframework.get_attr(after_install)() + + for fn in xhiveframework.get_hooks("after_app_install"): + xhiveframework.get_attr(fn)(name) + + sync_jobs() + sync_fixtures(name) + sync_customizations(name) + sync_dashboards(name) + + for after_sync in app_hooks.after_sync or []: + xhiveframework.get_attr(after_sync)() # + + xhiveframework.flags.in_install = False + + +def add_to_installed_apps(app_name, rebuild_website=True): + installed_apps = xhiveframework.get_installed_apps() + if app_name not in installed_apps: + installed_apps.append(app_name) + xhiveframework.db.set_global("installed_apps", json.dumps(installed_apps)) + xhiveframework.db.commit() + if xhiveframework.flags.in_install: + post_install(rebuild_website) + + +def remove_from_installed_apps(app_name): + installed_apps = xhiveframework.get_installed_apps() + if app_name in installed_apps: + installed_apps.remove(app_name) + xhiveframework.db.set_value( + "DefaultValue", {"defkey": "installed_apps"}, "defvalue", json.dumps(installed_apps) + ) + _clear_cache("__global") + xhiveframework.db.commit() + if xhiveframework.flags.in_install: + post_install() + + +def remove_app(app_name, dry_run=False, yes=False, no_backup=False, force=False): + """Remove app and all linked to the app's module with the app from a site.""" + + site = xhiveframework.local.site + app_hooks = xhiveframework.get_hooks(app_name=app_name) + + # dont allow uninstall app if not installed unless forced + if not force: + if app_name not in xhiveframework.get_installed_apps(): + click.secho(f"App {app_name} not installed on Site {site}", fg="yellow") + return + + # Don't allow uninstalling if we have dependent apps installed + for app in xhiveframework.get_installed_apps(): + if app != app_name: + hooks = xhiveframework.get_hooks(app_name=app) + if hooks.required_apps and any(app_name in required_app for required_app in hooks.required_apps): + click.secho(f"App {app_name} is a dependency of {app}. Uninstall {app} first.", fg="yellow") + return + + print(f"Uninstalling App {app_name} from Site {site}...") + + if not dry_run and not yes: + confirm = click.confirm( + "All doctypes (including custom), modules related to this app will be" + " deleted. Are you sure you want to continue?" + ) + if not confirm: + return + + if not (dry_run or no_backup): + from xhiveframework.utils.backups import scheduled_backup + + print("Backing up...") + scheduled_backup(ignore_files=True) + + xhiveframework.flags.in_uninstall = True + + for before_uninstall in app_hooks.before_uninstall or []: + xhiveframework.get_attr(before_uninstall)() + + for fn in xhiveframework.get_hooks("before_app_uninstall"): + xhiveframework.get_attr(fn)(app_name) + + modules = xhiveframework.get_all("Module Def", filters={"app_name": app_name}, pluck="name") + + drop_doctypes = _delete_modules(modules, dry_run=dry_run) + _delete_doctypes(drop_doctypes, dry_run=dry_run) + + if not dry_run: + remove_from_installed_apps(app_name) + xhiveframework.get_single("Installed Applications").update_versions() + xhiveframework.db.commit() + + for after_uninstall in app_hooks.after_uninstall or []: + xhiveframework.get_attr(after_uninstall)() + + for fn in xhiveframework.get_hooks("after_app_uninstall"): + xhiveframework.get_attr(fn)(app_name) + + click.secho(f"Uninstalled App {app_name} from Site {site}", fg="green") + xhiveframework.flags.in_uninstall = False + + +def _delete_modules(modules: list[str], dry_run: bool) -> list[str]: + """Delete modules belonging to the app and all related doctypes. + + Note: All record linked linked to Module Def are also deleted. + + Returns: list of deleted doctypes.""" + drop_doctypes = [] + + doctype_link_field_map = _get_module_linked_doctype_field_map() + for module_name in modules: + print(f"Deleting Module '{module_name}'") + + for doctype in xhiveframework.get_all( + "DocType", filters={"module": module_name}, fields=["name", "issingle"] + ): + print(f"* removing DocType '{doctype.name}'...") + + if not dry_run: + if doctype.issingle: + xhiveframework.delete_doc("DocType", doctype.name, ignore_on_trash=True, force=True) + else: + drop_doctypes.append(doctype.name) + + _delete_linked_documents(module_name, doctype_link_field_map, dry_run=dry_run) + + print(f"* removing Module Def '{module_name}'...") + if not dry_run: + xhiveframework.delete_doc("Module Def", module_name, ignore_on_trash=True, force=True) + + return drop_doctypes + + +def _delete_linked_documents(module_name: str, doctype_linkfield_map: dict[str, str], dry_run: bool) -> None: + """Deleted all records linked with module def""" + for doctype, fieldname in doctype_linkfield_map.items(): + for record in xhiveframework.get_all(doctype, filters={fieldname: module_name}, pluck="name"): + print(f"* removing {doctype} '{record}'...") + if not dry_run: + xhiveframework.delete_doc(doctype, record, ignore_on_trash=True, force=True) + + +def _get_module_linked_doctype_field_map() -> dict[str, str]: + """Get all the doctypes which have module linked with them. + + returns ordered dictionary with doctype->link field mapping.""" + + # Hardcoded to change order of deletion + ordered_doctypes = [ + ("Workspace", "module"), + ("Report", "module"), + ("Page", "module"), + ("Web Form", "module"), + ] + doctype_to_field_map = OrderedDict(ordered_doctypes) + + linked_doctypes = xhiveframework.get_all( + "DocField", + filters={"fieldtype": "Link", "options": "Module Def"}, + fields=["parent", "fieldname"], + ) + existing_linked_doctypes = [d for d in linked_doctypes if xhiveframework.db.exists("DocType", d.parent)] + + for d in existing_linked_doctypes: + # DocType deletion is handled separately in the end + if d.parent not in doctype_to_field_map and d.parent != "DocType": + doctype_to_field_map[d.parent] = d.fieldname + + return doctype_to_field_map + + +def _delete_doctypes(doctypes: list[str], dry_run: bool) -> None: + for doctype in set(doctypes): + print(f"* dropping Table for '{doctype}'...") + if not dry_run: + xhiveframework.delete_doc("DocType", doctype, ignore_on_trash=True, force=True) + xhiveframework.db.sql_ddl(f"DROP TABLE IF EXISTS `tab{doctype}`") + + +def post_install(rebuild_website=False): + from xhiveframework.website.utils import clear_website_cache + + if rebuild_website: + clear_website_cache() + + init_singles() + xhiveframework.db.commit() + xhiveframework.clear_cache() + + +def set_all_patches_as_completed(app): + from xhiveframework.modules.patch_handler import get_patches_from_app + + patches = get_patches_from_app(app) + for patch in patches: + xhiveframework.get_doc({"doctype": "Patch Log", "patch": patch}).insert(ignore_permissions=True) + xhiveframework.db.commit() + + +def init_singles(): + singles = xhiveframework.get_all("DocType", filters={"issingle": True}, pluck="name") + for single in singles: + if xhiveframework.db.get_singles_dict(single): + continue + + try: + doc = xhiveframework.new_doc(single) + doc.flags.ignore_mandatory = True + doc.flags.ignore_validate = True + doc.save() + except (ImportError, xhiveframework.DoesNotExistError): + # The doctype exists, but controller is deleted, + # no need to attempt to init such single, ref: #16917 + continue + + +def make_conf(db_name=None, db_password=None, site_config=None, db_type=None, db_host=None, db_port=None): + site = xhiveframework.local.site + make_site_config(db_name, db_password, site_config, db_type=db_type, db_host=db_host, db_port=db_port) + sites_path = xhiveframework.local.sites_path + xhiveframework.destroy() + xhiveframework.init(site, sites_path=sites_path) + + +def make_site_config( + db_name=None, db_password=None, site_config=None, db_type=None, db_host=None, db_port=None +): + xhiveframework.create_folder(os.path.join(xhiveframework.local.site_path)) + site_file = get_site_config_path() + + if not os.path.exists(site_file): + if not (site_config and isinstance(site_config, dict)): + site_config = get_conf_params(db_name, db_password) + + if db_type: + site_config["db_type"] = db_type + + if db_host: + site_config["db_host"] = db_host + + if db_port: + site_config["db_port"] = db_port + + with open(site_file, "w") as f: + f.write(json.dumps(site_config, indent=1, sort_keys=True)) + + +def update_site_config(key, value, validate=True, site_config_path=None): + """Update a value in site_config""" + from xhiveframework.utils.synchronization import filelock + + if not site_config_path: + site_config_path = get_site_config_path() + + # Sometimes global config file is passed directly to this function + _is_global_conf = "common_site_config" in site_config_path + + with filelock("site_config", is_global=_is_global_conf): + _update_config_file(key=key, value=value, config_file=site_config_path) + + +def _update_config_file(key: str, value, config_file: str): + """Updates site or common config""" + with open(config_file) as f: + site_config = json.loads(f.read()) + + # In case of non-int value + if value in ("0", "1"): + value = int(value) + + # boolean + if value == "false": + value = False + if value == "true": + value = True + + # remove key if value is None + if value == "None": + if key in site_config: + del site_config[key] + else: + site_config[key] = value + + with open(config_file, "w") as f: + f.write(json.dumps(site_config, indent=1, sort_keys=True)) + + if hasattr(xhiveframework.local, "conf"): + xhiveframework.local.conf[key] = value + + +def get_site_config_path(): + return os.path.join(xhiveframework.local.site_path, "site_config.json") + + +def get_conf_params(db_name=None, db_password=None): + if not db_name: + db_name = input("Database Name: ") + if not db_name: + raise Exception("Database Name Required") + + if not db_password: + from xhiveframework.utils import random_string + + db_password = random_string(16) + + return {"db_name": db_name, "db_password": db_password} + + +def make_site_dirs(): + for dir_path in [ + os.path.join("public", "files"), + os.path.join("private", "backups"), + os.path.join("private", "files"), + "locks", + "logs", + ]: + path = xhiveframework.get_site_path(dir_path) + os.makedirs(path, exist_ok=True) + + +def add_module_defs(app, ignore_if_duplicate=False): + modules = xhiveframework.get_module_list(app) + for module in modules: + d = xhiveframework.new_doc("Module Def") + d.app_name = app + d.module_name = module + d.insert(ignore_permissions=True, ignore_if_duplicate=ignore_if_duplicate) + + +def remove_missing_apps(): + import importlib + + apps = ("xhiveframework_subscription", "shopping_cart") + installed_apps = json.loads(xhiveframework.db.get_global("installed_apps") or "[]") + for app in apps: + if app in installed_apps: + try: + importlib.import_module(app) + + except ImportError: + installed_apps.remove(app) + xhiveframework.db.set_global("installed_apps", json.dumps(installed_apps)) + + +def convert_archive_content(sql_file_path): + if xhiveframework.conf.db_type == "mariadb": + # ever since mariaDB 10.6, row_format COMPRESSED has been deprecated and removed + # this step is added to ease restoring sites depending on older mariaDB servers + # This change was reverted by mariadb in 10.6.6 + # Ref: https://mariadb.com/kb/en/innodb-compressed-row-format/#read-only + from pathlib import Path + + from xhiveframework.utils import random_string + + version = _guess_mariadb_version() + if not version or (version <= (10, 6, 0) or version >= (10, 6, 6)): + return + + click.secho( + "MariaDB version being used does not support ROW_FORMAT=COMPRESSED, " + "converting into DYNAMIC format.", + fg="yellow", + ) + + old_sql_file_path = Path(f"{sql_file_path}_{random_string(10)}") + sql_file_path = Path(sql_file_path) + + os.rename(sql_file_path, old_sql_file_path) + sql_file_path.touch() + + with open(old_sql_file_path) as r, open(sql_file_path, "a") as w: + for line in r: + w.write(line.replace("ROW_FORMAT=COMPRESSED", "ROW_FORMAT=DYNAMIC")) + + old_sql_file_path.unlink() + + +def _guess_mariadb_version() -> tuple[int] | None: + # Using command-line because we *might* not have a connection yet and this command is required + # in non-interactive mode. + # Use db.sql("select version()") instead if connection is available. + with suppress(Exception): + mariadb = which("mariadb") or which("mysql") + version_output = subprocess.getoutput(f"{mariadb} --version") + version_regex = r"(?P\d+\.\d+\.\d+)-MariaDB" + + version = re.search(version_regex, version_output).group("version") + + return tuple(int(v) for v in version.split(".")) + + +def extract_files(site_name, file_path): + import shutil + import subprocess + + from xhiveframework.utils import get_bench_relative_path + + file_path = get_bench_relative_path(file_path) + + # Need to do xhiveframework.init to maintain the site locals + xhiveframework.init(site=site_name) + abs_site_path = os.path.abspath(xhiveframework.get_site_path()) + + # Copy the files to the parent directory and extract + shutil.copy2(os.path.abspath(file_path), abs_site_path) + + # Get the file name splitting the file path on + tar_name = os.path.split(file_path)[1] + tar_path = os.path.join(abs_site_path, tar_name) + + try: + if file_path.endswith(".tar"): + subprocess.check_output(["tar", "xvf", tar_path, "--strip", "2"], cwd=abs_site_path) + elif file_path.endswith(".tgz"): + subprocess.check_output(["tar", "zxvf", tar_path, "--strip", "2"], cwd=abs_site_path) + except Exception: + raise + finally: + xhiveframework.destroy() + + return tar_path + + +def is_downgrade(sql_file_path, verbose=False): + """Check if input db backup will get downgraded on current bench + + This function is only tested with mariadb. + TODO: Add postgres support + """ + if xhiveframework.conf.db_type != "mariadb": + return False + + backup_version = get_backup_version(sql_file_path) or get_old_backup_version(sql_file_path) + current_version = Version(xhiveframework.__version__) + + # Assume it's not a downgrade if we can't determine backup version + if backup_version is None: + return False + + is_downgrade = backup_version > current_version + + if verbose and is_downgrade: + print(f"Your site is currently on XhiveFramework {current_version} and your backup is {backup_version}.") + + return is_downgrade + + +def get_old_backup_version(sql_file_path: str) -> Version | None: + """Return the xhiveframework version used to create the specified database dump. + + This methods supports older versions of XhiveFramework wich used a different format. + """ + header = get_db_dump_header(sql_file_path).split("\n") + if match := re.search(r"XhiveFramework (\d+\.\d+\.\d+)", header[0]): + backup_version = match[1] + + return Version(backup_version) if backup_version else None + + +def get_backup_version(sql_file_path: str) -> Version | None: + """Return the xhiveframework version used to create the specified database dump.""" + header = get_db_dump_header(sql_file_path).split("\n") + metadata = "" + if "begin xhiveframework metadata" in header[0]: + for line in header[1:]: + if "end xhiveframework metadata" in line: + break + metadata += line.replace("--", "").strip() + "\n" + parser = configparser.ConfigParser() + parser.read_string(metadata) + return Version(parser["xhiveframework"]["version"]) + + return None + + +def is_partial(sql_file_path: str) -> bool: + """ + Function to return whether the database dump is a partial backup or not + + :param sql_file_path: path to the database dump file + :return: True if the database dump is a partial backup, False otherwise + """ + header = get_db_dump_header(sql_file_path) + return "Partial Backup" in header + + +def partial_restore(sql_file_path, verbose=False): + if xhiveframework.conf.db_type == "mariadb": + from xhiveframework.database.mariadb.setup_db import import_db_from_sql + elif xhiveframework.conf.db_type == "postgres": + import warnings + + from xhiveframework.database.postgres.setup_db import import_db_from_sql + + warn = click.style( + "Delete the tables you want to restore manually before attempting" + " partial restore operation for PostreSQL databases", + fg="yellow", + ) + warnings.warn(warn, stacklevel=1) + else: + click.secho("Unsupported database type", fg="red") + return + + import_db_from_sql(source_sql=sql_file_path, verbose=verbose) + + +def validate_database_sql(path: str, _raise: bool = True) -> None: + """Check if file has contents and if `__Auth` table exists + + Args: + path (str): Path of the decompressed SQL file + _raise (bool, optional): Raise exception if invalid file. Defaults to True. + """ + + if path.endswith(".gz"): + executable_name = "zgrep" + else: + executable_name = "grep" + + if os.path.getsize(path): + if (executable := which(executable_name)) is None: + xhiveframework.throw( + f"`{executable_name}` not found in PATH! This is required to take a backup.", + exc=xhiveframework.ExecutableNotFound, + ) + try: + xhiveframework.utils.execute_in_shell(f"{executable} -m1 __Auth {path}", check_exit_code=True) + return + except Exception: + error_message = "Table `__Auth` not found in file." + else: + error_message = f"{path} is an empty file!" + + if error_message: + click.secho(error_message, fg="red") + + if _raise: + raise xhiveframework.InvalidDatabaseFile + + +def get_db_dump_header(file_path: str, file_bytes: int = 256) -> str: + """ + Get the header of a database dump file + + :param file_path: path to the database dump file + :param file_bytes: number of bytes to read from the file + :return: The first few bytes of the file as requested + """ + + # Use `gzip` to open the file if the extension is `.gz` + if file_path.endswith(".gz"): + with gzip.open(file_path, "rb") as f: + return f.read(file_bytes).decode() + + with open(file_path, "rb") as f: + return f.read(file_bytes).decode() diff --git a/xhiveframework/integrations/__init__.py b/xhiveframework/integrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/__init__.py b/xhiveframework/integrations/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/connected_app/__init__.py b/xhiveframework/integrations/doctype/connected_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/connected_app/connected_app.js b/xhiveframework/integrations/doctype/connected_app/connected_app.js new file mode 100644 index 0000000..540003a --- /dev/null +++ b/xhiveframework/integrations/doctype/connected_app/connected_app.js @@ -0,0 +1,38 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Connected App", { + refresh: (frm) => { + frm.add_custom_button(__("Get OpenID Configuration"), async () => { + if (!frm.doc.openid_configuration) { + xhiveframework.msgprint(__("Please enter OpenID Configuration URL")); + } else { + try { + const response = await fetch(frm.doc.openid_configuration); + const oidc = await response.json(); + frm.set_value("authorization_uri", oidc.authorization_endpoint); + frm.set_value("token_uri", oidc.token_endpoint); + frm.set_value("userinfo_uri", oidc.userinfo_endpoint); + frm.set_value("introspection_uri", oidc.introspection_endpoint); + frm.set_value("revocation_uri", oidc.revocation_endpoint); + } catch (error) { + xhiveframework.msgprint(__("Please check OpenID Configuration URL")); + } + } + }); + + if (!frm.is_new()) { + frm.add_custom_button(__("Connect to {}", [frm.doc.provider_name]), async () => { + xhiveframework.call({ + method: "initiate_web_application_flow", + doc: frm.doc, + callback: function (r) { + window.open(r.message, "_blank"); + }, + }); + }); + } + + frm.toggle_display("sb_client_credentials_section", !frm.is_new()); + }, +}); diff --git a/xhiveframework/integrations/doctype/connected_app/connected_app.json b/xhiveframework/integrations/doctype/connected_app/connected_app.json new file mode 100644 index 0000000..b66cd90 --- /dev/null +++ b/xhiveframework/integrations/doctype/connected_app/connected_app.json @@ -0,0 +1,169 @@ +{ + "actions": [], + "beta": 1, + "creation": "2019-01-24 15:51:06.362222", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "provider_name", + "cb_00", + "openid_configuration", + "sb_client_credentials_section", + "client_id", + "redirect_uri", + "cb_01", + "client_secret", + "sb_scope_section", + "scopes", + "sb_endpoints_section", + "authorization_uri", + "token_uri", + "revocation_uri", + "cb_02", + "userinfo_uri", + "introspection_uri", + "section_break_18", + "query_parameters" + ], + "fields": [ + { + "fieldname": "provider_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Provider Name", + "reqd": 1 + }, + { + "fieldname": "cb_00", + "fieldtype": "Column Break" + }, + { + "fieldname": "openid_configuration", + "fieldtype": "Data", + "label": "OpenID Configuration" + }, + { + "collapsible": 1, + "fieldname": "sb_client_credentials_section", + "fieldtype": "Section Break", + "label": "Client Credentials" + }, + { + "fieldname": "client_id", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Client Id", + "mandatory_depends_on": "eval:doc.redirect_uri" + }, + { + "fieldname": "redirect_uri", + "fieldtype": "Data", + "label": "Redirect URI", + "read_only": 1 + }, + { + "fieldname": "cb_01", + "fieldtype": "Column Break" + }, + { + "fieldname": "client_secret", + "fieldtype": "Password", + "label": "Client Secret" + }, + { + "collapsible": 1, + "fieldname": "sb_scope_section", + "fieldtype": "Section Break", + "label": "Scopes" + }, + { + "collapsible": 1, + "fieldname": "sb_endpoints_section", + "fieldtype": "Section Break", + "label": "Endpoints" + }, + { + "fieldname": "cb_02", + "fieldtype": "Column Break" + }, + { + "fieldname": "scopes", + "fieldtype": "Table", + "label": "Scopes", + "options": "OAuth Scope" + }, + { + "fieldname": "authorization_uri", + "fieldtype": "Small Text", + "label": "Authorization URI", + "mandatory_depends_on": "eval:doc.redirect_uri" + }, + { + "fieldname": "token_uri", + "fieldtype": "Data", + "label": "Token URI", + "mandatory_depends_on": "eval:doc.redirect_uri" + }, + { + "fieldname": "revocation_uri", + "fieldtype": "Data", + "label": "Revocation URI" + }, + { + "fieldname": "userinfo_uri", + "fieldtype": "Data", + "label": "Userinfo URI" + }, + { + "fieldname": "introspection_uri", + "fieldtype": "Data", + "label": "Introspection URI" + }, + { + "fieldname": "section_break_18", + "fieldtype": "Section Break", + "label": "Extra Parameters" + }, + { + "fieldname": "query_parameters", + "fieldtype": "Table", + "label": "Query Parameters", + "options": "Query Parameters" + } + ], + "links": [ + { + "link_doctype": "Token Cache", + "link_fieldname": "connected_app" + } + ], + "modified": "2022-01-07 05:28:45.073041", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Connected App", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "All" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "provider_name", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/connected_app/connected_app.py b/xhiveframework/integrations/doctype/connected_app/connected_app.py new file mode 100644 index 0000000..5969c54 --- /dev/null +++ b/xhiveframework/integrations/doctype/connected_app/connected_app.py @@ -0,0 +1,193 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import os +from urllib.parse import urlencode, urljoin + +from requests_oauthlib import OAuth2Session + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + +if any((os.getenv("CI"), xhiveframework.conf.developer_mode, xhiveframework.conf.allow_tests)): + # Disable mandatory TLS in developer mode and tests + os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" + +os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1" + + +class ConnectedApp(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.integrations.doctype.oauth_scope.oauth_scope import OAuthScope + from xhiveframework.integrations.doctype.query_parameters.query_parameters import QueryParameters + from xhiveframework.types import DF + + authorization_uri: DF.SmallText | None + client_id: DF.Data | None + client_secret: DF.Password | None + introspection_uri: DF.Data | None + openid_configuration: DF.Data | None + provider_name: DF.Data + query_parameters: DF.Table[QueryParameters] + redirect_uri: DF.Data | None + revocation_uri: DF.Data | None + scopes: DF.Table[OAuthScope] + token_uri: DF.Data | None + userinfo_uri: DF.Data | None + # end: auto-generated types + """Connect to a remote oAuth Server. Retrieve and store user's access token + in a Token Cache. + """ + + def validate(self): + base_url = xhiveframework.utils.get_url() + callback_path = ( + "/api/method/xhiveframework.integrations.doctype.connected_app.connected_app.callback/" + self.name + ) + self.redirect_uri = urljoin(base_url, callback_path) + + def get_oauth2_session(self, user=None, init=False): + """Return an auto-refreshing OAuth2 session which is an extension of a requests.Session()""" + token = None + token_updater = None + auto_refresh_kwargs = None + + if not init: + user = user or xhiveframework.session.user + token_cache = self.get_user_token(user) + token = token_cache.get_json() + token_updater = token_cache.update_data + auto_refresh_kwargs = {"client_id": self.client_id} + client_secret = self.get_password("client_secret") + if client_secret: + auto_refresh_kwargs["client_secret"] = client_secret + + return OAuth2Session( + client_id=self.client_id, + token=token, + token_updater=token_updater, + auto_refresh_url=self.token_uri, + auto_refresh_kwargs=auto_refresh_kwargs, + redirect_uri=self.redirect_uri, + scope=self.get_scopes(), + ) + + @xhiveframework.whitelist() + def initiate_web_application_flow(self, user=None, success_uri=None): + """Return an authorization URL for the user. Save state in Token Cache.""" + user = user or xhiveframework.session.user + oauth = self.get_oauth2_session(user, init=True) + query_params = self.get_query_params() + authorization_url, state = oauth.authorization_url(self.authorization_uri, **query_params) + token_cache = self.get_token_cache(user) + + if not token_cache: + token_cache = xhiveframework.new_doc("Token Cache") + token_cache.user = user + token_cache.connected_app = self.name + + token_cache.success_uri = success_uri + token_cache.state = state + token_cache.save(ignore_permissions=True) + xhiveframework.db.commit() + + return authorization_url + + def get_user_token(self, user=None, success_uri=None): + """Return an existing user token or initiate a Web Application Flow.""" + user = user or xhiveframework.session.user + token_cache = self.get_token_cache(user) + + if token_cache: + return token_cache + + redirect = self.initiate_web_application_flow(user, success_uri) + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = redirect + return redirect + + def get_token_cache(self, user): + token_cache = None + token_cache_name = self.name + "-" + user + + if xhiveframework.db.exists("Token Cache", token_cache_name): + token_cache = xhiveframework.get_doc("Token Cache", token_cache_name) + + return token_cache + + def get_scopes(self): + return [row.scope for row in self.scopes] + + def get_query_params(self): + return {param.key: param.value for param in self.query_parameters} + + def get_active_token(self, user=None): + user = user or xhiveframework.session.user + token_cache = self.get_token_cache(user) + if token_cache and token_cache.is_expired(): + oauth_session = self.get_oauth2_session(user) + + try: + token = oauth_session.refresh_token( + body=f"redirect_uri={self.redirect_uri}", + token_url=self.token_uri, + ) + except Exception: + self.log_error("Token Refresh Error") + return None + + token_cache.update_data(token) + + return token_cache + + +@xhiveframework.whitelist(methods=["GET"], allow_guest=True) +def callback(code=None, state=None): + """Handle client's code. + + Called during the oauthorization flow by the remote oAuth2 server to + transmit a code that can be used by the local server to obtain an access + token. + """ + + if xhiveframework.session.user == "Guest": + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = "/login?" + urlencode({"redirect-to": xhiveframework.request.url}) + return + + path = xhiveframework.request.path[1:].split("/") + if len(path) != 4 or not path[3]: + xhiveframework.throw(_("Invalid Parameters.")) + + connected_app = xhiveframework.get_doc("Connected App", path[3]) + token_cache = xhiveframework.get_doc("Token Cache", connected_app.name + "-" + xhiveframework.session.user) + + if state != token_cache.state: + xhiveframework.throw(_("Invalid state.")) + + oauth_session = connected_app.get_oauth2_session(init=True) + query_params = connected_app.get_query_params() + token = oauth_session.fetch_token( + connected_app.token_uri, + code=code, + client_secret=connected_app.get_password("client_secret"), + include_client_id=True, + **query_params, + ) + token_cache.update_data(token) + + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = token_cache.get("success_uri") or connected_app.get_url() + + +@xhiveframework.whitelist() +def has_token(connected_app, connected_user=None): + app = xhiveframework.get_doc("Connected App", connected_app) + token_cache = app.get_token_cache(connected_user or xhiveframework.session.user) + return bool(token_cache and token_cache.get_password("access_token", False)) diff --git a/xhiveframework/integrations/doctype/connected_app/test_connected_app.py b/xhiveframework/integrations/doctype/connected_app/test_connected_app.py new file mode 100644 index 0000000..fe43ac8 --- /dev/null +++ b/xhiveframework/integrations/doctype/connected_app/test_connected_app.py @@ -0,0 +1,148 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +from urllib.parse import urljoin + +import requests + +import xhiveframework +from xhiveframework.integrations.doctype.social_login_key.test_social_login_key import ( + create_or_update_social_login_key, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +def get_user(usr, pwd): + user = xhiveframework.new_doc("User") + user.email = usr + user.enabled = 1 + user.first_name = "_Test" + user.new_password = pwd + user.roles = [] + user.append("roles", {"doctype": "Has Role", "parentfield": "roles", "role": "System Manager"}) + user.insert() + + return user + + +def get_connected_app(): + doctype = "Connected App" + connected_app = xhiveframework.new_doc(doctype) + connected_app.provider_name = "xhiveframework" + connected_app.scopes = [] + connected_app.append("scopes", {"scope": "all"}) + connected_app.insert() + + return connected_app + + +def get_oauth_client(): + oauth_client = xhiveframework.new_doc("OAuth Client") + oauth_client.app_name = "_Test Connected App" + oauth_client.redirect_uris = "to be replaced" + oauth_client.default_redirect_uri = "to be replaced" + oauth_client.grant_type = "Authorization Code" + oauth_client.response_type = "Code" + oauth_client.skip_authorization = 1 + oauth_client.insert() + + return oauth_client + + +class TestConnectedApp(XhiveFrameworkTestCase): + def setUp(self): + """Set up a Connected App that connects to our own oAuth provider. + + XhiveFramework comes with it's own oAuth2 provider that we can test against. The + client credentials can be obtained from an "OAuth Client". All depends + on "Social Login Key" so we create one as well. + + The redirect URIs from "Connected App" and "OAuth Client" have to match. + XhiveFramework's "Authorization URL" and "Access Token URL" (actually they're + just endpoints) are stored in "Social Login Key" so we get them from + there. + """ + self.user_name = "test-connected-app@example.com" + self.user_password = "Eastern_43A1W" + + self.user = get_user(self.user_name, self.user_password) + self.connected_app = get_connected_app() + self.oauth_client = get_oauth_client() + social_login_key = create_or_update_social_login_key() + self.base_url = social_login_key.get("base_url") + + xhiveframework.db.commit() + self.connected_app.reload() + self.oauth_client.reload() + + redirect_uri = self.connected_app.get("redirect_uri") + self.oauth_client.update({"redirect_uris": redirect_uri, "default_redirect_uri": redirect_uri}) + self.oauth_client.save() + + self.connected_app.update( + { + "authorization_uri": urljoin(self.base_url, social_login_key.get("authorize_url")), + "client_id": self.oauth_client.get("client_id"), + "client_secret": self.oauth_client.get("client_secret"), + "token_uri": urljoin(self.base_url, social_login_key.get("access_token_url")), + } + ) + self.connected_app.save() + + xhiveframework.db.commit() + self.connected_app.reload() + self.oauth_client.reload() + + def test_web_application_flow(self): + """Simulate a logged in user who opens the authorization URL.""" + + def login(): + return session.get( + urljoin(self.base_url, "/api/method/login"), + params={"usr": self.user_name, "pwd": self.user_password}, + ) + + session = requests.Session() + + first_login = login() + self.assertEqual(first_login.status_code, 200) + + authorization_url = self.connected_app.initiate_web_application_flow(user=self.user_name) + + auth_response = session.get(authorization_url) + self.assertEqual(auth_response.status_code, 200) + + callback_response = session.get(auth_response.url) + self.assertEqual(callback_response.status_code, 200) + + self.token_cache = self.connected_app.get_token_cache(self.user_name) + token = self.token_cache.get_password("access_token") + self.assertNotEqual(token, None) + + oauth2_session = self.connected_app.get_oauth2_session(self.user_name) + resp = oauth2_session.get(urljoin(self.base_url, "/api/method/xhiveframework.auth.get_logged_user")) + self.assertEqual(resp.json().get("message"), self.user_name) + + def tearDown(self): + def delete_if_exists(attribute): + doc = getattr(self, attribute, None) + if doc: + doc.delete(force=True) + + delete_if_exists("token_cache") + delete_if_exists("connected_app") + + if getattr(self, "oauth_client", None): + tokens = xhiveframework.get_all("OAuth Bearer Token", filters={"client": self.oauth_client.name}) + for token in tokens: + doc = xhiveframework.get_doc("OAuth Bearer Token", token.name) + doc.delete() + + codes = xhiveframework.get_all("OAuth Authorization Code", filters={"client": self.oauth_client.name}) + for code in codes: + doc = xhiveframework.get_doc("OAuth Authorization Code", code.name) + doc.delete() + + delete_if_exists("user") + delete_if_exists("oauth_client") + + xhiveframework.db.commit() diff --git a/xhiveframework/integrations/doctype/connected_app/test_records.json b/xhiveframework/integrations/doctype/connected_app/test_records.json new file mode 100644 index 0000000..d311f35 --- /dev/null +++ b/xhiveframework/integrations/doctype/connected_app/test_records.json @@ -0,0 +1,13 @@ +[ + { + "doctype": "Connected App", + "provider_name": "xhiveframework", + "client_id": "test_client_id", + "client_secret": "test_client_secret", + "scopes": [ + { + "scope": "all" + } + ] + } +] diff --git a/xhiveframework/integrations/doctype/dropbox_settings/__init__.py b/xhiveframework/integrations/doctype/dropbox_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.js b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.js new file mode 100644 index 0000000..ceda15c --- /dev/null +++ b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.js @@ -0,0 +1,47 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Dropbox Settings", { + refresh: function (frm) { + frm.toggle_display( + ["app_access_key", "app_secret_key"], + !frm.doc.__onload?.dropbox_setup_via_site_config + ); + frm.events.take_backup(frm); + }, + + are_keys_present: function (frm) { + return ( + (frm.doc.app_access_key && frm.doc.app_secret_key) || + frm.doc.__onload?.dropbox_setup_via_site_config + ); + }, + + allow_dropbox_access: function (frm) { + if (!frm.events.are_keys_present(frm)) { + xhiveframework.msgprint(__("App Access Key and/or Secret Key are not present.")); + return; + } + + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.get_dropbox_authorize_url", + freeze: true, + callback: function (r) { + if (!r.exc) { + window.open(r.message.auth_url); + } + }, + }); + }, + + take_backup: function (frm) { + if (frm.doc.enabled && (frm.doc.dropbox_refresh_token || frm.doc.dropbox_access_token)) { + frm.add_custom_button(__("Take Backup Now"), function () { + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.take_backup", + freeze: true, + }); + }); + } + }, +}); diff --git a/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.json b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.json new file mode 100644 index 0000000..15535f0 --- /dev/null +++ b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.json @@ -0,0 +1,126 @@ +{ + "actions": [], + "creation": "2016-09-21 10:12:57.399174", + "doctype": "DocType", + "document_type": "System", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enabled", + "send_notifications_to", + "send_email_for_successful_backup", + "backup_frequency", + "limit_no_of_backups", + "no_of_backups", + "file_backup", + "app_access_key", + "app_secret_key", + "allow_dropbox_access", + "dropbox_refresh_token", + "dropbox_access_token" + ], + "fields": [ + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "fieldname": "send_notifications_to", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Send Notifications To", + "reqd": 1 + }, + { + "default": "1", + "description": "Note: By default emails for failed backups are sent.", + "fieldname": "send_email_for_successful_backup", + "fieldtype": "Check", + "label": "Send Email for Successful Backup" + }, + { + "fieldname": "backup_frequency", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Backup Frequency", + "options": "\nDaily\nWeekly", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "limit_no_of_backups", + "fieldtype": "Check", + "label": "Limit Number of DB Backups" + }, + { + "default": "5", + "depends_on": "eval:doc.limit_no_of_backups", + "fieldname": "no_of_backups", + "fieldtype": "Int", + "label": "Number of DB Backups" + }, + { + "default": "1", + "fieldname": "file_backup", + "fieldtype": "Check", + "label": "File Backup" + }, + { + "fieldname": "app_access_key", + "fieldtype": "Data", + "label": "App Access Key" + }, + { + "fieldname": "app_secret_key", + "fieldtype": "Password", + "label": "App Secret Key" + }, + { + "fieldname": "allow_dropbox_access", + "fieldtype": "Button", + "label": "Allow Dropbox Access" + }, + { + "fieldname": "dropbox_refresh_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Dropbox Refresh Token", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "dropbox_access_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Dropbox Access Token" + } + ], + "in_create": 1, + "issingle": 1, + "links": [], + "modified": "2023-03-20 14:20:19.180611", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Dropbox Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.py b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.py new file mode 100644 index 0000000..4e11769 --- /dev/null +++ b/xhiveframework/integrations/doctype/dropbox_settings/dropbox_settings.py @@ -0,0 +1,378 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import os +from urllib.parse import parse_qs, urlparse + +import dropbox +from rq.timeouts import JobTimeoutException + +import xhiveframework +from xhiveframework import _ +from xhiveframework.integrations.offsite_backup_utils import ( + get_chunk_site, + get_latest_backup_file, + send_email, + validate_file_size, +) +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, encode, get_backups_path, get_files_path, get_request_site_address +from xhiveframework.utils.background_jobs import enqueue +from xhiveframework.utils.backups import new_backup + +ignore_list = [".DS_Store"] + + +class DropboxSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + app_access_key: DF.Data | None + app_secret_key: DF.Password | None + backup_frequency: DF.Literal["", "Daily", "Weekly"] + dropbox_access_token: DF.Password | None + dropbox_refresh_token: DF.Password | None + enabled: DF.Check + file_backup: DF.Check + limit_no_of_backups: DF.Check + no_of_backups: DF.Int + send_email_for_successful_backup: DF.Check + send_notifications_to: DF.Data + + # end: auto-generated types + def onload(self): + if not self.app_access_key and xhiveframework.conf.dropbox_access_key: + self.set_onload("dropbox_setup_via_site_config", 1) + + def validate(self): + if self.enabled and self.limit_no_of_backups and self.no_of_backups < 1: + xhiveframework.throw(_("Number of DB backups cannot be less than 1")) + + +@xhiveframework.whitelist() +def take_backup(): + """Enqueue longjob for taking backup to dropbox""" + enqueue( + "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.take_backup_to_dropbox", + queue="long", + timeout=1500, + ) + xhiveframework.msgprint(_("Queued for backup. It may take a few minutes to an hour.")) + + +def take_backups_daily(): + take_backups_if("Daily") + + +def take_backups_weekly(): + take_backups_if("Weekly") + + +def take_backups_if(freq): + if xhiveframework.db.get_single_value("Dropbox Settings", "backup_frequency") == freq: + take_backup_to_dropbox() + + +def take_backup_to_dropbox(retry_count=0, upload_db_backup=True): + did_not_upload, error_log = [], [] + try: + if cint(xhiveframework.db.get_single_value("Dropbox Settings", "enabled")): + validate_file_size() + + did_not_upload, error_log = backup_to_dropbox(upload_db_backup) + if did_not_upload: + raise Exception + + if cint(xhiveframework.db.get_single_value("Dropbox Settings", "send_email_for_successful_backup")): + send_email(True, "Dropbox", "Dropbox Settings", "send_notifications_to") + except JobTimeoutException: + if retry_count < 2: + args = { + "retry_count": retry_count + 1, + "upload_db_backup": False, # considering till worker timeout db backup is uploaded + } + enqueue( + "xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.take_backup_to_dropbox", + queue="long", + timeout=1500, + **args, + ) + except Exception: + if isinstance(error_log, str): + error_message = error_log + "\n" + xhiveframework.get_traceback() + else: + file_and_error = [" - ".join(f) for f in zip(did_not_upload, error_log, strict=False)] + error_message = "\n".join(file_and_error) + "\n" + xhiveframework.get_traceback() + + send_email(False, "Dropbox", "Dropbox Settings", "send_notifications_to", error_message) + + +def backup_to_dropbox(upload_db_backup=True): + # upload database + dropbox_settings = get_dropbox_settings() + dropbox_client = get_dropbox_client(dropbox_settings) + + if upload_db_backup: + if xhiveframework.flags.create_new_backup: + backup = new_backup(ignore_files=True) + filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) + site_config = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_conf)) + else: + filename, site_config = get_latest_backup_file() + + upload_file_to_dropbox(filename, "/database", dropbox_client) + upload_file_to_dropbox(site_config, "/database", dropbox_client) + + # delete older databases + if dropbox_settings["no_of_backups"]: + delete_older_backups(dropbox_client, "/database", dropbox_settings["no_of_backups"]) + + # upload files to files folder + did_not_upload = [] + error_log = [] + + if dropbox_settings["file_backup"]: + upload_from_folder(get_files_path(), 0, "/files", dropbox_client, did_not_upload, error_log) + upload_from_folder( + get_files_path(is_private=1), 1, "/private/files", dropbox_client, did_not_upload, error_log + ) + + return did_not_upload, list(set(error_log)) + + +def upload_from_folder(path, is_private, dropbox_folder, dropbox_client, did_not_upload, error_log): + if not os.path.exists(path): + return + + if is_fresh_upload(): + response = get_uploaded_files_meta(dropbox_folder, dropbox_client) + else: + response = xhiveframework._dict({"entries": []}) + + path = str(path) + + for f in xhiveframework.get_all( + "File", + filters={"is_folder": 0, "is_private": is_private, "uploaded_to_dropbox": 0}, + fields=["file_url", "name", "file_name"], + ): + if not f.file_url: + continue + filename = f.file_url.rsplit("/", 1)[-1] + + filepath = os.path.join(path, filename) + + if filename in ignore_list: + continue + + found = False + for file_metadata in response.entries: + try: + if os.path.basename(filepath) == file_metadata.name and os.stat( + encode(filepath) + ).st_size == int(file_metadata.size): + found = True + update_file_dropbox_status(f.name) + break + except Exception: + error_log.append(xhiveframework.get_traceback()) + + if not found: + try: + upload_file_to_dropbox(filepath, dropbox_folder, dropbox_client) + update_file_dropbox_status(f.name) + except Exception: + did_not_upload.append(filepath) + error_log.append(xhiveframework.get_traceback()) + + +def upload_file_to_dropbox(filename, folder, dropbox_client): + """upload files with chunk of 15 mb to reduce session append calls""" + if not os.path.exists(filename): + return + + create_folder_if_not_exists(folder, dropbox_client) + file_size = os.path.getsize(encode(filename)) + chunk_size = get_chunk_site(file_size) + + mode = dropbox.files.WriteMode.overwrite + + f = open(encode(filename), "rb") + path = f"{folder}/{os.path.basename(filename)}" + + try: + if file_size <= chunk_size: + dropbox_client.files_upload(f.read(), path, mode) + else: + upload_session_start_result = dropbox_client.files_upload_session_start(f.read(chunk_size)) + cursor = dropbox.files.UploadSessionCursor( + session_id=upload_session_start_result.session_id, offset=f.tell() + ) + commit = dropbox.files.CommitInfo(path=path, mode=mode) + + while f.tell() < file_size: + if (file_size - f.tell()) <= chunk_size: + dropbox_client.files_upload_session_finish(f.read(chunk_size), cursor, commit) + else: + dropbox_client.files_upload_session_append( + f.read(chunk_size), cursor.session_id, cursor.offset + ) + cursor.offset = f.tell() + except dropbox.exceptions.ApiError as e: + if isinstance(e.error, dropbox.files.UploadError): + error = f"File Path: {path}\n" + error += xhiveframework.get_traceback() + xhiveframework.log_error(error) + else: + raise + + +def create_folder_if_not_exists(folder, dropbox_client): + try: + dropbox_client.files_get_metadata(folder) + except dropbox.exceptions.ApiError as e: + # folder not found + if isinstance(e.error, dropbox.files.GetMetadataError): + dropbox_client.files_create_folder(folder) + else: + raise + + +def update_file_dropbox_status(file_name): + xhiveframework.db.set_value("File", file_name, "uploaded_to_dropbox", 1, update_modified=False) + + +def is_fresh_upload(): + file_name = xhiveframework.db.get_value("File", {"uploaded_to_dropbox": 1}, "name") + return not file_name + + +def get_uploaded_files_meta(dropbox_folder, dropbox_client): + try: + return dropbox_client.files_list_folder(dropbox_folder) + except dropbox.exceptions.ApiError as e: + # folder not found + if isinstance(e.error, dropbox.files.ListFolderError): + return xhiveframework._dict({"entries": []}) + raise + + +def get_dropbox_client(dropbox_settings): + dropbox_client = dropbox.Dropbox( + oauth2_access_token=dropbox_settings["access_token"], + oauth2_refresh_token=dropbox_settings["refresh_token"], + app_key=dropbox_settings["app_key"], + app_secret=dropbox_settings["app_secret"], + timeout=None, + ) + + # checking if the access token has expired + dropbox_client.files_list_folder("") + if dropbox_settings["access_token"] != dropbox_client._oauth2_access_token: + set_dropbox_token(dropbox_client._oauth2_access_token) + + return dropbox_client + + +def get_dropbox_settings(redirect_uri=False): + # NOTE: access token is kept for legacy dropbox apps + settings = xhiveframework.get_doc("Dropbox Settings") + app_details = { + "app_key": settings.app_access_key or xhiveframework.conf.dropbox_access_key, + "app_secret": settings.get_password(fieldname="app_secret_key", raise_exception=False) + if settings.app_secret_key + else xhiveframework.conf.dropbox_secret_key, + "refresh_token": settings.get_password("dropbox_refresh_token", raise_exception=False), + "access_token": settings.get_password("dropbox_access_token", raise_exception=False), + "file_backup": settings.file_backup, + "no_of_backups": settings.no_of_backups if settings.limit_no_of_backups else None, + } + + if redirect_uri: + app_details.update( + { + "redirect_uri": get_request_site_address(True) + + "/api/method/xhiveframework.integrations.doctype.dropbox_settings.dropbox_settings.dropbox_auth_finish" + } + ) + + if not (app_details["app_key"] and app_details["app_secret"]): + raise Exception(_("Please set Dropbox access keys in site config or doctype")) + + return app_details + + +def delete_older_backups(dropbox_client, folder_path, to_keep): + res = dropbox_client.files_list_folder(path=folder_path) + files = [f for f in res.entries if isinstance(f, dropbox.files.FileMetadata) and "sql" in f.name] + + if len(files) <= to_keep: + return + + files.sort(key=lambda item: item.client_modified, reverse=True) + for f in files[to_keep:]: + dropbox_client.files_delete(os.path.join(folder_path, f.name)) + + +@xhiveframework.whitelist() +def get_dropbox_authorize_url(): + app_details = get_dropbox_settings(redirect_uri=True) + dropbox_oauth_flow = dropbox.DropboxOAuth2Flow( + consumer_key=app_details["app_key"], + redirect_uri=app_details["redirect_uri"], + session={}, + csrf_token_session_key="dropbox-auth-csrf-token", + consumer_secret=app_details["app_secret"], + token_access_type="offline", + ) + + auth_url = dropbox_oauth_flow.start() + + return {"auth_url": auth_url, "args": parse_qs(urlparse(auth_url).query)} + + +@xhiveframework.whitelist() +def dropbox_auth_finish(): + app_details = get_dropbox_settings(redirect_uri=True) + callback = xhiveframework.form_dict + close = '

      ' + _("Please close this window") + "

      " + + if not callback.state or not callback.code: + xhiveframework.respond_as_web_page( + _("Dropbox Setup"), + _("Illegal Access Token. Please try again") + close, + indicator_color="red", + http_status_code=xhiveframework.AuthenticationError.http_status_code, + ) + return + + dropbox_oauth_flow = dropbox.DropboxOAuth2Flow( + consumer_key=app_details["app_key"], + redirect_uri=app_details["redirect_uri"], + session={"dropbox-auth-csrf-token": callback.state}, + csrf_token_session_key="dropbox-auth-csrf-token", + consumer_secret=app_details["app_secret"], + ) + + token = dropbox_oauth_flow.finish({"state": callback.state, "code": callback.code}) + set_dropbox_token(token.access_token, token.refresh_token) + + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = "/app/dropbox-settings" + + +def set_dropbox_token(access_token, refresh_token=None): + # NOTE: used doc object instead of db.set_value so that password field is set properly + dropbox_settings = xhiveframework.get_single("Dropbox Settings") + dropbox_settings.dropbox_access_token = access_token + if refresh_token: + dropbox_settings.dropbox_refresh_token = refresh_token + + dropbox_settings.save() + + xhiveframework.db.commit() diff --git a/xhiveframework/integrations/doctype/dropbox_settings/test_dropbox_settings.py b/xhiveframework/integrations/doctype/dropbox_settings/test_dropbox_settings.py new file mode 100644 index 0000000..833e82f --- /dev/null +++ b/xhiveframework/integrations/doctype/dropbox_settings/test_dropbox_settings.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestDropboxSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/google_calendar/__init__.py b/xhiveframework/integrations/doctype/google_calendar/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/google_calendar/google_calendar.js b/xhiveframework/integrations/doctype/google_calendar/google_calendar.js new file mode 100644 index 0000000..03c0dfb --- /dev/null +++ b/xhiveframework/integrations/doctype/google_calendar/google_calendar.js @@ -0,0 +1,67 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Google Calendar", { + refresh: function (frm) { + if (frm.is_new()) { + frm.dashboard.set_headline( + __("To use Google Calendar, enable {0}.", [ + `${__("Google Settings")}`, + ]) + ); + } + + xhiveframework.realtime.on("import_google_calendar", (data) => { + if (data.progress) { + frm.dashboard.show_progress( + "Syncing Google Calendar", + (data.progress / data.total) * 100, + __("Syncing {0} of {1}", [data.progress, data.total]) + ); + if (data.progress === data.total) { + frm.dashboard.hide_progress("Syncing Google Calendar"); + } + } + }); + + if (frm.doc.refresh_token) { + frm.add_custom_button(__("Sync Calendar"), function () { + xhiveframework.show_alert({ + indicator: "green", + message: __("Syncing"), + }); + xhiveframework + .call({ + method: "xhiveframework.integrations.doctype.google_calendar.google_calendar.sync", + args: { + g_calendar: frm.doc.name, + }, + }) + .then((r) => { + xhiveframework.hide_progress(); + xhiveframework.msgprint(r.message); + }); + }); + } + }, + authorize_google_calendar_access: function (frm) { + let reauthorize = 0; + if (frm.doc.authorization_code) { + reauthorize = 1; + } + + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.google_calendar.google_calendar.authorize_access", + args: { + g_calendar: frm.doc.name, + reauthorize: reauthorize, + }, + callback: function (r) { + if (!r.exc) { + frm.save(); + window.open(r.message.url); + } + }, + }); + }, +}); diff --git a/xhiveframework/integrations/doctype/google_calendar/google_calendar.json b/xhiveframework/integrations/doctype/google_calendar/google_calendar.json new file mode 100644 index 0000000..102569e --- /dev/null +++ b/xhiveframework/integrations/doctype/google_calendar/google_calendar.json @@ -0,0 +1,150 @@ +{ + "actions": [], + "autoname": "field:calendar_name", + "creation": "2019-07-06 17:54:09.450100", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enable", + "sb_00", + "calendar_name", + "user", + "authorize_google_calendar_access", + "sb_01", + "pull_from_google_calendar", + "cb_01", + "push_to_google_calendar", + "section_break_3", + "google_calendar_id", + "refresh_token", + "authorization_code", + "next_sync_token" + ], + "fields": [ + { + "default": "1", + "fieldname": "enable", + "fieldtype": "Check", + "label": "Enable" + }, + { + "depends_on": "eval: doc.enable", + "fieldname": "sb_00", + "fieldtype": "Section Break", + "label": "Google Calendar" + }, + { + "fieldname": "user", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User", + "options": "User", + "reqd": 1 + }, + { + "description": "The name that will appear in Google Calendar", + "fieldname": "calendar_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Calendar Name", + "reqd": 1, + "unique": 1 + }, + { + "depends_on": "eval: doc.enable", + "fieldname": "section_break_3", + "fieldtype": "Section Break" + }, + { + "fieldname": "refresh_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Refresh Token" + }, + { + "fieldname": "authorization_code", + "fieldtype": "Password", + "hidden": 1, + "label": "Authorization Code" + }, + { + "fieldname": "next_sync_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Next Sync Token" + }, + { + "fieldname": "google_calendar_id", + "fieldtype": "Data", + "label": "Google Calendar ID", + "read_only": 1 + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "authorize_google_calendar_access", + "fieldtype": "Button", + "label": "Authorize Google Calendar Access" + }, + { + "depends_on": "eval: doc.enable", + "fieldname": "sb_01", + "fieldtype": "Section Break", + "label": "Sync" + }, + { + "default": "1", + "fieldname": "pull_from_google_calendar", + "fieldtype": "Check", + "label": "Pull from Google Calendar" + }, + { + "fieldname": "cb_01", + "fieldtype": "Column Break" + }, + { + "default": "1", + "fieldname": "push_to_google_calendar", + "fieldtype": "Check", + "label": "Push to Google Calendar" + } + ], + "links": [], + "modified": "2023-08-28 22:21:44.238862", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Google Calendar", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "if_owner": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/google_calendar/google_calendar.py b/xhiveframework/integrations/doctype/google_calendar/google_calendar.py new file mode 100644 index 0000000..6e72bef --- /dev/null +++ b/xhiveframework/integrations/doctype/google_calendar/google_calendar.py @@ -0,0 +1,871 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + + +from datetime import datetime, timedelta +from urllib.parse import quote +from zoneinfo import ZoneInfo + +import google.oauth2.credentials +import requests +from dateutil import parser +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError + +import xhiveframework +from xhiveframework import _ +from xhiveframework.integrations.google_oauth import GoogleOAuth +from xhiveframework.model.document import Document +from xhiveframework.utils import ( + add_days, + add_to_date, + get_datetime, + get_request_site_address, + get_system_timezone, + get_weekdays, + now_datetime, +) +from xhiveframework.utils.password import set_encrypted_password + +SCOPES = "https://www.googleapis.com/auth/calendar" + +google_calendar_frequencies = { + "RRULE:FREQ=DAILY": "Daily", + "RRULE:FREQ=WEEKLY": "Weekly", + "RRULE:FREQ=MONTHLY": "Monthly", + "RRULE:FREQ=YEARLY": "Yearly", +} + +google_calendar_days = { + "MO": "monday", + "TU": "tuesday", + "WE": "wednesday", + "TH": "thursday", + "FR": "friday", + "SA": "saturday", + "SU": "sunday", +} + +framework_frequencies = { + "Daily": "RRULE:FREQ=DAILY;", + "Weekly": "RRULE:FREQ=WEEKLY;", + "Monthly": "RRULE:FREQ=MONTHLY;", + "Yearly": "RRULE:FREQ=YEARLY;", +} + +framework_days = { + "monday": "MO", + "tuesday": "TU", + "wednesday": "WE", + "thursday": "TH", + "friday": "FR", + "saturday": "SA", + "sunday": "SU", +} + + +class GoogleCalendar(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + authorization_code: DF.Password | None + calendar_name: DF.Data + enable: DF.Check + google_calendar_id: DF.Data | None + next_sync_token: DF.Password | None + pull_from_google_calendar: DF.Check + push_to_google_calendar: DF.Check + refresh_token: DF.Password | None + user: DF.Link + + # end: auto-generated types + def validate(self): + google_settings = xhiveframework.get_single("Google Settings") + if not google_settings.enable: + xhiveframework.throw(_("Enable Google API in Google Settings.")) + + if not google_settings.client_id or not google_settings.client_secret: + xhiveframework.throw(_("Enter Client Id and Client Secret in Google Settings.")) + + return google_settings + + def get_access_token(self): + google_settings = self.validate() + + if not self.refresh_token: + button_label = xhiveframework.bold(_("Allow Google Calendar Access")) + raise xhiveframework.ValidationError(_("Click on {0} to generate Refresh Token.").format(button_label)) + + data = { + "client_id": google_settings.client_id, + "client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False), + "refresh_token": self.get_password(fieldname="refresh_token", raise_exception=False), + "grant_type": "refresh_token", + "scope": SCOPES, + } + + try: + r = requests.post(GoogleOAuth.OAUTH_URL, data=data).json() + except requests.exceptions.HTTPError: + button_label = xhiveframework.bold(_("Allow Google Calendar Access")) + xhiveframework.throw( + _( + "Something went wrong during the token generation. Click on {0} to generate a new one." + ).format(button_label) + ) + + return r.get("access_token") + + +@xhiveframework.whitelist() +def authorize_access(g_calendar, reauthorize=None): + """ + If no Authorization code get it from Google and then request for Refresh Token. + Google Calendar Name is set to flags to set_value after Authorization Code is obtained. + """ + google_settings = xhiveframework.get_doc("Google Settings") + google_calendar = xhiveframework.get_doc("Google Calendar", g_calendar) + google_calendar.check_permission("write") + + redirect_uri = ( + get_request_site_address(True) + + "?cmd=xhiveframework.integrations.doctype.google_calendar.google_calendar.google_callback" + ) + + if not google_calendar.authorization_code or reauthorize: + xhiveframework.cache.hset("google_calendar", "google_calendar", google_calendar.name) + return get_authentication_url(client_id=google_settings.client_id, redirect_uri=redirect_uri) + else: + try: + data = { + "code": google_calendar.get_password(fieldname="authorization_code", raise_exception=False), + "client_id": google_settings.client_id, + "client_secret": google_settings.get_password( + fieldname="client_secret", raise_exception=False + ), + "redirect_uri": redirect_uri, + "grant_type": "authorization_code", + } + r = requests.post(GoogleOAuth.OAUTH_URL, data=data).json() + + if "refresh_token" in r: + xhiveframework.db.set_value( + "Google Calendar", google_calendar.name, "refresh_token", r.get("refresh_token") + ) + xhiveframework.db.commit() + + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = "/app/Form/{}/{}".format( + quote("Google Calendar"), quote(google_calendar.name) + ) + + xhiveframework.msgprint(_("Google Calendar has been configured.")) + except Exception as e: + xhiveframework.throw(e) + + +def get_authentication_url(client_id=None, redirect_uri=None): + return { + "url": "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&prompt=consent&client_id={}&include_granted_scopes=true&scope={}&redirect_uri={}".format( + client_id, SCOPES, redirect_uri + ) + } + + +@xhiveframework.whitelist() +def google_callback(code=None): + """ + Authorization code is sent to callback as per the API configuration + """ + google_calendar = xhiveframework.cache.hget("google_calendar", "google_calendar") + xhiveframework.db.set_value("Google Calendar", google_calendar, "authorization_code", code) + xhiveframework.db.commit() + + authorize_access(google_calendar) + + +@xhiveframework.whitelist() +def sync(g_calendar=None): + filters = {"enable": 1} + + if g_calendar: + filters.update({"name": g_calendar}) + + google_calendars = xhiveframework.get_list("Google Calendar", filters=filters) + + for g in google_calendars: + return sync_events_from_google_calendar(g.name) + + +def get_google_calendar_object(g_calendar): + """ + Returns an object of Google Calendar along with Google Calendar doc. + """ + google_settings = xhiveframework.get_doc("Google Settings") + account = xhiveframework.get_doc("Google Calendar", g_calendar) + + credentials_dict = { + "token": account.get_access_token(), + "refresh_token": account.get_password(fieldname="refresh_token", raise_exception=False), + "token_uri": GoogleOAuth.OAUTH_URL, + "client_id": google_settings.client_id, + "client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False), + "scopes": [SCOPES], + } + + credentials = google.oauth2.credentials.Credentials(**credentials_dict) + google_calendar = build( + serviceName="calendar", version="v3", credentials=credentials, static_discovery=False + ) + + check_google_calendar(account, google_calendar) + + account.load_from_db() + return google_calendar, account + + +def check_google_calendar(account, google_calendar): + """ + Checks if Google Calendar is present with the specified name. + If not, creates one. + """ + account.load_from_db() + try: + if account.google_calendar_id: + google_calendar.calendars().get(calendarId=account.google_calendar_id).execute() + else: + # If no Calendar ID create a new Calendar + calendar = { + "summary": account.calendar_name, + "timeZone": xhiveframework.db.get_single_value("System Settings", "time_zone"), + } + created_calendar = google_calendar.calendars().insert(body=calendar).execute() + xhiveframework.db.set_value( + "Google Calendar", account.name, "google_calendar_id", created_calendar.get("id") + ) + xhiveframework.db.commit() + except HttpError as err: + xhiveframework.throw( + _("Google Calendar - Could not create Calendar for {0}, error code {1}.").format( + account.name, err.resp.status + ) + ) + + +def sync_events_from_google_calendar(g_calendar, method=None): + """ + Syncs Events from Google Calendar in Framework Calendar. + Google Calendar returns nextSyncToken when all the events in Google Calendar are fetched. + nextSyncToken is returned at the very last page + https://developers.google.com/calendar/v3/sync + """ + google_calendar, account = get_google_calendar_object(g_calendar) + + if not account.pull_from_google_calendar: + return + + sync_token = account.get_password(fieldname="next_sync_token", raise_exception=False) or None + events = xhiveframework._dict() + results = [] + while True: + try: + # API Response listed at EOF + events = ( + google_calendar.events() + .list( + calendarId=account.google_calendar_id, + maxResults=2000, + pageToken=events.get("nextPageToken"), + singleEvents=False, + showDeleted=True, + syncToken=sync_token, + ) + .execute() + ) + except HttpError as err: + msg = _("Google Calendar - Could not fetch event from Google Calendar, error code {0}.").format( + err.resp.status + ) + + if err.resp.status == 410: + set_encrypted_password("Google Calendar", account.name, "", "next_sync_token") + xhiveframework.db.commit() + msg += " " + _("Sync token was invalid and has been reset, Retry syncing.") + xhiveframework.msgprint(msg, title="Invalid Sync Token", indicator="blue") + else: + xhiveframework.throw(msg) + + results.extend(event for event in events.get("items", [])) + if not events.get("nextPageToken"): + if events.get("nextSyncToken"): + account.next_sync_token = events.get("nextSyncToken") + account.save() + break + + for idx, event in enumerate(results): + xhiveframework.publish_realtime( + "import_google_calendar", dict(progress=idx + 1, total=len(results)), user=xhiveframework.session.user + ) + + # If Google Calendar Event if confirmed, then create an Event + if event.get("status") == "confirmed": + recurrence = None + if event.get("recurrence"): + try: + recurrence = event.get("recurrence")[0] + except IndexError: + pass + + if not xhiveframework.db.exists("Event", {"google_calendar_event_id": event.get("id")}): + insert_event_to_calendar(account, event, recurrence) + else: + update_event_in_calendar(account, event, recurrence) + elif event.get("status") == "cancelled": + # If any synced Google Calendar Event is cancelled, then close the Event + xhiveframework.db.set_value( + "Event", + { + "google_calendar_id": account.google_calendar_id, + "google_calendar_event_id": event.get("id"), + }, + "status", + "Closed", + ) + xhiveframework.get_doc( + { + "doctype": "Comment", + "comment_type": "Info", + "reference_doctype": "Event", + "reference_name": xhiveframework.db.get_value( + "Event", + { + "google_calendar_id": account.google_calendar_id, + "google_calendar_event_id": event.get("id"), + }, + "name", + ), + "content": " - Event deleted from Google Calendar.", + } + ).insert(ignore_permissions=True) + else: + pass + + if not results: + return _("No Google Calendar Event to sync.") + elif len(results) == 1: + return _("1 Google Calendar Event synced.") + else: + return _("{0} Google Calendar Events synced.").format(len(results)) + + +def insert_event_to_calendar(account, event, recurrence=None): + """ + Inserts event in XhiveFramework Calendar during Sync + """ + calendar_event = { + "doctype": "Event", + "subject": event.get("summary"), + "description": event.get("description"), + "google_calendar_event": 1, + "google_calendar": account.name, + "google_calendar_id": account.google_calendar_id, + "google_calendar_event_id": event.get("id"), + "google_meet_link": event.get("hangoutLink"), + "pulled_from_google_calendar": 1, + } + calendar_event.update( + google_calendar_to_repeat_on(recurrence=recurrence, start=event.get("start"), end=event.get("end")) + ) + xhiveframework.get_doc(calendar_event).insert(ignore_permissions=True) + + +def update_event_in_calendar(account, event, recurrence=None): + """ + Updates Event in XhiveFramework Calendar if any existing Google Calendar Event is updated + """ + calendar_event = xhiveframework.get_doc("Event", {"google_calendar_event_id": event.get("id")}) + calendar_event.subject = event.get("summary") + calendar_event.description = event.get("description") + calendar_event.google_meet_link = event.get("hangoutLink") + calendar_event.update( + google_calendar_to_repeat_on(recurrence=recurrence, start=event.get("start"), end=event.get("end")) + ) + calendar_event.save(ignore_permissions=True) + + +def insert_event_in_google_calendar(doc, method=None): + """ + Insert Events in Google Calendar if sync_with_google_calendar is checked. + """ + if ( + not doc.sync_with_google_calendar + or doc.pulled_from_google_calendar + or not xhiveframework.db.exists("Google Calendar", {"name": doc.google_calendar}) + ): + return + + google_calendar, account = get_google_calendar_object(doc.google_calendar) + + if not account.push_to_google_calendar: + return + + event = {"summary": doc.subject, "description": doc.description, "google_calendar_event": 1} + event.update( + format_date_according_to_google_calendar( + doc.all_day, get_datetime(doc.starts_on), get_datetime(doc.ends_on) if doc.ends_on else None + ) + ) + + if doc.repeat_on: + event.update({"recurrence": repeat_on_to_google_calendar_recurrence_rule(doc)}) + + event.update({"attendees": get_attendees(doc)}) + + conference_data_version = 0 + + if doc.add_video_conferencing: + event.update({"conferenceData": get_conference_data(doc)}) + conference_data_version = 1 + + try: + event = ( + google_calendar.events() + .insert( + calendarId=doc.google_calendar_id, + body=event, + conferenceDataVersion=conference_data_version, + sendUpdates="all", + ) + .execute() + ) + + xhiveframework.db.set_value( + "Event", + doc.name, + {"google_calendar_event_id": event.get("id"), "google_meet_link": event.get("hangoutLink")}, + update_modified=False, + ) + + xhiveframework.msgprint(_("Event Synced with Google Calendar.")) + except HttpError as err: + xhiveframework.throw( + _("Google Calendar - Could not insert event in Google Calendar {0}, error code {1}.").format( + account.name, err.resp.status + ) + ) + + +def update_event_in_google_calendar(doc, method=None): + """ + Updates Events in Google Calendar if any existing event is modified in XhiveFramework Calendar + """ + # Workaround to avoid triggering updation when Event is being inserted since + # creation and modified are same when inserting doc + if ( + not doc.sync_with_google_calendar + or doc.modified == doc.creation + or not xhiveframework.db.exists("Google Calendar", {"name": doc.google_calendar}) + ): + return + + if doc.sync_with_google_calendar and not doc.google_calendar_event_id: + # If sync_with_google_calendar is checked later, then insert the event rather than updating it. + insert_event_in_google_calendar(doc) + return + + google_calendar, account = get_google_calendar_object(doc.google_calendar) + + if not account.push_to_google_calendar: + return + + try: + event = ( + google_calendar.events() + .get(calendarId=doc.google_calendar_id, eventId=doc.google_calendar_event_id) + .execute() + ) + + event["summary"] = doc.subject + event["description"] = doc.description + event["recurrence"] = repeat_on_to_google_calendar_recurrence_rule(doc) + event["status"] = ( + "cancelled" if doc.status == "Cancelled" or doc.status == "Closed" else event.get("status") + ) + event.update( + format_date_according_to_google_calendar( + doc.all_day, get_datetime(doc.starts_on), get_datetime(doc.ends_on) if doc.ends_on else None + ) + ) + + conference_data_version = 0 + + if doc.add_video_conferencing: + event.update({"conferenceData": get_conference_data(doc)}) + conference_data_version = 1 + elif doc.get_doc_before_save().add_video_conferencing or event.get("hangoutLink"): + # remove google meet from google calendar event, if turning off add_video_conferencing + event.update({"conferenceData": None}) + conference_data_version = 1 + + event.update({"attendees": get_attendees(doc)}) + + event = ( + google_calendar.events() + .update( + calendarId=doc.google_calendar_id, + eventId=doc.google_calendar_event_id, + body=event, + conferenceDataVersion=conference_data_version, + sendUpdates="all", + ) + .execute() + ) + + # if add_video_conferencing enabled or disabled during update, overwrite + xhiveframework.db.set_value( + "Event", + doc.name, + {"google_meet_link": event.get("hangoutLink")}, + update_modified=False, + ) + doc.notify_update() + + xhiveframework.msgprint(_("Event Synced with Google Calendar.")) + except HttpError as err: + xhiveframework.throw( + _("Google Calendar - Could not update Event {0} in Google Calendar, error code {1}.").format( + doc.name, err.resp.status + ) + ) + + +def delete_event_from_google_calendar(doc, method=None): + """ + Delete Events from Google Calendar if XhiveFramework Event is deleted. + """ + + if not xhiveframework.db.exists("Google Calendar", {"name": doc.google_calendar}): + return + + google_calendar, account = get_google_calendar_object(doc.google_calendar) + + if not account.push_to_google_calendar: + return + + try: + event = ( + google_calendar.events() + .get(calendarId=doc.google_calendar_id, eventId=doc.google_calendar_event_id) + .execute() + ) + event["recurrence"] = None + event["status"] = "cancelled" + + google_calendar.events().update( + calendarId=doc.google_calendar_id, eventId=doc.google_calendar_event_id, body=event + ).execute() + except HttpError as err: + xhiveframework.msgprint( + _("Google Calendar - Could not delete Event {0} from Google Calendar, error code {1}.").format( + doc.name, err.resp.status + ) + ) + + +def google_calendar_to_repeat_on(start, end, recurrence=None): + """ + recurrence is in the form ['RRULE:FREQ=WEEKLY;BYDAY=MO,TU,TH'] + has the frequency and then the days on which the event recurs + + Both have been mapped in a dict for easier mapping. + """ + repeat_on = { + "starts_on": ( + get_datetime(start.get("date")) + if start.get("date") + else parser.parse(start.get("dateTime")) + .astimezone(ZoneInfo(get_system_timezone())) + .replace(tzinfo=None) + ), + "ends_on": ( + get_datetime(end.get("date")) + if end.get("date") + else parser.parse(end.get("dateTime")) + .astimezone(ZoneInfo(get_system_timezone())) + .replace(tzinfo=None) + ), + "all_day": 1 if start.get("date") else 0, + "repeat_this_event": 1 if recurrence else 0, + "repeat_on": None, + "repeat_till": None, + "sunday": 0, + "monday": 0, + "tuesday": 0, + "wednesday": 0, + "thursday": 0, + "friday": 0, + "saturday": 0, + } + + # recurrence rule "RRULE:FREQ=WEEKLY;BYDAY=MO,TU,TH" + if recurrence: + # google_calendar_frequency = RRULE:FREQ=WEEKLY, byday = BYDAY=MO,TU,TH, until = 20191028 + google_calendar_frequency, until, byday = get_recurrence_parameters(recurrence) + repeat_on["repeat_on"] = google_calendar_frequencies.get(google_calendar_frequency) + + if repeat_on["repeat_on"] == "Daily": + repeat_on["ends_on"] = None + repeat_on["repeat_till"] = datetime.strptime(until, "%Y%m%d") if until else None + + if byday and repeat_on["repeat_on"] == "Weekly": + repeat_on["repeat_till"] = datetime.strptime(until, "%Y%m%d") if until else None + byday = byday.split("=")[1].split(",") + for repeat_day in byday: + repeat_on[google_calendar_days[repeat_day]] = 1 + + if byday and repeat_on["repeat_on"] == "Monthly": + byday = byday.split("=")[1] + repeat_day_week_number, repeat_day_name = None, None + + for num in ["-2", "-1", "1", "2", "3", "4", "5"]: + if num in byday: + repeat_day_week_number = num + break + + for day in ["MO", "TU", "WE", "TH", "FR", "SA", "SU"]: + if day in byday: + repeat_day_name = google_calendar_days.get(day) + break + + # Only Set starts_on for the event to repeat monthly + start_date = parse_google_calendar_recurrence_rule(int(repeat_day_week_number), repeat_day_name) + repeat_on["starts_on"] = start_date + repeat_on["ends_on"] = add_to_date(start_date, minutes=5) + repeat_on["repeat_till"] = datetime.strptime(until, "%Y%m%d") if until else None + + if repeat_on["repeat_till"] == "Yearly": + repeat_on["ends_on"] = None + repeat_on["repeat_till"] = datetime.strptime(until, "%Y%m%d") if until else None + + return repeat_on + + +def format_date_according_to_google_calendar(all_day, starts_on, ends_on=None): + if not ends_on: + ends_on = starts_on + timedelta(minutes=10) + + date_format = { + "start": { + "dateTime": starts_on.isoformat(), + "timeZone": get_system_timezone(), + }, + "end": { + "dateTime": ends_on.isoformat(), + "timeZone": get_system_timezone(), + }, + } + + if all_day: + # If all_day event, Google Calendar takes date as a parameter and not dateTime + date_format["start"].pop("dateTime") + date_format["end"].pop("dateTime") + + date_format["start"].update({"date": starts_on.date().isoformat()}) + date_format["end"].update({"date": ends_on.date().isoformat()}) + + return date_format + + +def parse_google_calendar_recurrence_rule(repeat_day_week_number, repeat_day_name): + """ + Returns (repeat_on) exact date for combination eg 4TH viz. 4th thursday of a month + """ + if repeat_day_week_number < 0: + # Consider a month with 5 weeks and event is to be repeated in last week of every month, google caledar considers + # a month has 4 weeks and hence itll return -1 for a month with 5 weeks. + repeat_day_week_number = 4 + + weekdays = get_weekdays() + current_date = now_datetime() + isset_day_name, isset_day_number = False, False + + # Set the proper day ie if recurrence is 4TH, then align the day to Thursday + while not isset_day_name: + isset_day_name = True if weekdays[current_date.weekday()].lower() == repeat_day_name else False + current_date = add_days(current_date, 1) if not isset_day_name else current_date + + # One the day is set to Thursday, now set the week number ie 4 + while not isset_day_number: + week_number = get_week_number(current_date) + isset_day_number = True if week_number == repeat_day_week_number else False + # check if current_date week number is greater or smaller than repeat_day week number + weeks = 1 if week_number < repeat_day_week_number else -1 + current_date = add_to_date(current_date, weeks=weeks) if not isset_day_number else current_date + + return current_date + + +def repeat_on_to_google_calendar_recurrence_rule(doc): + """ + Returns event (repeat_on) in Google Calendar format ie RRULE:FREQ=WEEKLY;BYDAY=MO,TU,TH + """ + recurrence = framework_frequencies.get(doc.repeat_on) + weekdays = get_weekdays() + + if doc.repeat_on == "Weekly": + byday = [framework_days.get(day.lower()) for day in weekdays if doc.get(day.lower())] + recurrence = recurrence + "BYDAY=" + ",".join(byday) + elif doc.repeat_on == "Monthly": + week_number = str(get_week_number(get_datetime(doc.starts_on))) + week_day = weekdays[get_datetime(doc.starts_on).weekday()].lower() + recurrence = recurrence + "BYDAY=" + week_number + framework_days.get(week_day) + + return [recurrence] + + +def get_week_number(dt): + """ + Returns the week number of the month for the specified date. + https://stackoverflow.com/questions/3806473/python-week-number-of-the-month/16804556 + """ + from math import ceil + + first_day = dt.replace(day=1) + + dom = dt.day + adjusted_dom = dom + first_day.weekday() + + return int(ceil(adjusted_dom / 7.0)) + + +def get_recurrence_parameters(recurrence): + recurrence = recurrence.split(";") + frequency, until, byday = None, None, None + + for r in recurrence: + if "RRULE:FREQ" in r: + frequency = r + elif "UNTIL" in r: + until = r + elif "BYDAY" in r: + byday = r + else: + pass + + return frequency, until, byday + + +def get_conference_data(doc): + return { + "createRequest": {"requestId": doc.name, "conferenceSolutionKey": {"type": "hangoutsMeet"}}, + "notes": doc.description, + } + + +def get_attendees(doc): + """ + Returns a list of dicts with attendee emails, if available in event_participants table + """ + attendees, email_not_found = [], [] + + for participant in doc.event_participants: + if participant.get("email"): + attendees.append({"email": participant.email}) + else: + email_not_found.append({"dt": participant.reference_doctype, "dn": participant.reference_docname}) + + if email_not_found: + xhiveframework.msgprint( + _("Google Calendar - Contact / email not found. Did not add attendee for -
      {0}").format( + "
      ".join(f"{d.get('dt')} {d.get('dn')}" for d in email_not_found) + ), + alert=True, + indicator="yellow", + ) + + return attendees + + +"""API Response + { + 'kind': 'calendar#events', + 'etag': '"etag"', + 'summary': 'Test Calendar', + 'updated': '2019-07-25T06:09:34.681Z', + 'timeZone': 'Asia/Kolkata', + 'accessRole': 'owner', + 'defaultReminders': [], + 'nextSyncToken': 'token', + 'items': [ + { + 'kind': 'calendar#event', + 'etag': '"etag"', + 'id': 'id', + 'status': 'confirmed' or 'cancelled', + 'htmlLink': 'link', + 'created': '2019-07-25T06:08:21.000Z', + 'updated': '2019-07-25T06:09:34.681Z', + 'summary': 'asdf', + 'creator': { + 'email': 'email' + }, + 'organizer': { + 'email': 'email', + 'displayName': 'Test Calendar', + 'self': True + }, + 'start': { + 'dateTime': '2019-07-27T12:00:00+05:30', (if all day event the its 'date' instead of 'dateTime') + 'timeZone': 'Asia/Kolkata' + }, + 'end': { + 'dateTime': '2019-07-27T13:00:00+05:30', (if all day event the its 'date' instead of 'dateTime') + 'timeZone': 'Asia/Kolkata' + }, + 'recurrence': *recurrence, + 'iCalUID': 'uid', + 'sequence': 1, + 'hangoutLink': 'https://meet.google.com/mee-ting-uri', + 'conferenceData': { + 'createRequest': { + 'requestId': 'EV00001', + 'conferenceSolutionKey': { + 'type': 'hangoutsMeet' + }, + 'status': { + 'statusCode': 'success' + } + }, + 'entryPoints': [ + { + 'entryPointType': 'video', + 'uri': 'https://meet.google.com/mee-ting-uri', + 'label': 'meet.google.com/mee-ting-uri' + } + ], + 'conferenceSolution': { + 'key': { + 'type': 'hangoutsMeet' + }, + 'name': 'Google Meet', + 'iconUri': 'https://fonts.gstatic.com/s/i/productlogos/meet_2020q4/v6/web-512dp/logo_meet_2020q4_color_2x_web_512dp.png' + }, + 'conferenceId': 'mee-ting-uri' + 'reminders': { + 'useDefault': True + } + } + ] + } + *recurrence + - Daily Event: ['RRULE:FREQ=DAILY'] + - Weekly Event: ['RRULE:FREQ=WEEKLY;BYDAY=MO,TU,TH'] + - Monthly Event: ['RRULE:FREQ=MONTHLY;BYDAY=4TH'] + - BYDAY: -2, -1, 1, 2, 3, 4 with weekdays (-2 edge case for April 2017 had 6 weeks in a month) + - Yearly Event: ['RRULE:FREQ=YEARLY;'] + - Custom Event: ['RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20191028;BYDAY=MO,WE']""" diff --git a/xhiveframework/integrations/doctype/google_contacts/__init__.py b/xhiveframework/integrations/doctype/google_contacts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/google_contacts/google_contacts.js b/xhiveframework/integrations/doctype/google_contacts/google_contacts.js new file mode 100644 index 0000000..0efe776 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_contacts/google_contacts.js @@ -0,0 +1,63 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Google Contacts", { + refresh: function (frm) { + if (!frm.doc.enable) { + frm.dashboard.set_headline( + __("To use Google Contacts, enable {0}.", [ + `${__("Google Settings")}`, + ]) + ); + } + + xhiveframework.realtime.on("import_google_contacts", (data) => { + if (data.progress) { + frm.dashboard.show_progress( + "Import Google Contacts", + (data.progress / data.total) * 100, + __("Importing {0} of {1}", [data.progress, data.total]) + ); + if (data.progress === data.total) { + frm.dashboard.hide_progress("Import Google Contacts"); + } + } + }); + + if (frm.doc.refresh_token) { + let sync_button = frm.add_custom_button(__("Sync Contacts"), function () { + xhiveframework.show_alert({ + indicator: "green", + message: __("Syncing"), + }); + xhiveframework + .call({ + method: "xhiveframework.integrations.doctype.google_contacts.google_contacts.sync", + args: { + g_contact: frm.doc.name, + }, + btn: sync_button, + }) + .then((r) => { + xhiveframework.hide_progress(); + xhiveframework.msgprint(r.message); + }); + }); + } + }, + authorize_google_contacts_access: function (frm) { + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.google_contacts.google_contacts.authorize_access", + args: { + g_contact: frm.doc.name, + reauthorize: frm.doc.authorization_code ? 1 : 0, + }, + callback: function (r) { + if (!r.exc) { + frm.save(); + window.open(r.message.url); + } + }, + }); + }, +}); diff --git a/xhiveframework/integrations/doctype/google_contacts/google_contacts.json b/xhiveframework/integrations/doctype/google_contacts/google_contacts.json new file mode 100644 index 0000000..3858217 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_contacts/google_contacts.json @@ -0,0 +1,132 @@ +{ + "actions": [], + "autoname": "format:GC-{email_id}", + "creation": "2019-06-14 00:09:39.441961", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "enable", + "sb_00", + "email_id", + "authorize_google_contacts_access", + "cb_00", + "last_sync_on", + "authorization_code", + "refresh_token", + "next_sync_token", + "sync", + "pull_from_google_contacts", + "column_break_12", + "push_to_google_contacts" + ], + "fields": [ + { + "default": "0", + "fieldname": "enable", + "fieldtype": "Check", + "label": "Enable" + }, + { + "fieldname": "authorization_code", + "fieldtype": "Password", + "hidden": 1, + "label": "Authorization Code" + }, + { + "fieldname": "refresh_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Refresh Token" + }, + { + "fieldname": "last_sync_on", + "fieldtype": "Datetime", + "label": "Last Sync On", + "read_only": 1 + }, + { + "description": "Email Address whose Google Contacts are to be synced.", + "fieldname": "email_id", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Email Address", + "options": "Email", + "reqd": 1 + }, + { + "depends_on": "enable", + "fieldname": "sb_00", + "fieldtype": "Section Break", + "label": "Google Contacts" + }, + { + "fieldname": "cb_00", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "authorize_google_contacts_access", + "fieldtype": "Button", + "label": "Authorize Google Contacts Access" + }, + { + "fieldname": "next_sync_token", + "fieldtype": "Password", + "hidden": 1, + "label": "Next Sync Token" + }, + { + "depends_on": "enable", + "fieldname": "sync", + "fieldtype": "Section Break", + "label": "Sync" + }, + { + "default": "0", + "fieldname": "pull_from_google_contacts", + "fieldtype": "Check", + "label": "Pull from Google Contacts" + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "push_to_google_contacts", + "fieldtype": "Check", + "label": "Push to Google Contacts" + } + ], + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Google Contacts", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "create": 1, + "delete": 1, + "if_owner": 1, + "read": 1, + "role": "Desk User", + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/google_contacts/google_contacts.py b/xhiveframework/integrations/doctype/google_contacts/google_contacts.py new file mode 100644 index 0000000..10b1a39 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_contacts/google_contacts.py @@ -0,0 +1,309 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + + +from urllib.parse import quote + +from googleapiclient.errors import HttpError + +import xhiveframework +from xhiveframework import _ +from xhiveframework.integrations.google_oauth import GoogleOAuth +from xhiveframework.model.document import Document + + +class GoogleContacts(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + authorization_code: DF.Password | None + email_id: DF.Data + enable: DF.Check + last_sync_on: DF.Datetime | None + next_sync_token: DF.Password | None + pull_from_google_contacts: DF.Check + push_to_google_contacts: DF.Check + refresh_token: DF.Password | None + + # end: auto-generated types + def validate(self): + if not xhiveframework.db.get_single_value("Google Settings", "enable"): + xhiveframework.throw(_("Enable Google API in Google Settings.")) + + def get_access_token(self): + if not self.refresh_token: + button_label = xhiveframework.bold(_("Allow Google Contacts Access")) + raise xhiveframework.ValidationError(_("Click on {0} to generate Refresh Token.").format(button_label)) + + oauth_obj = GoogleOAuth("contacts") + r = oauth_obj.refresh_access_token( + self.get_password(fieldname="refresh_token", raise_exception=False) + ) + + return r.get("access_token") + + +@xhiveframework.whitelist(methods=["POST"]) +def authorize_access(g_contact, reauthorize=False, code=None): + """ + If no Authorization code get it from Google and then request for Refresh Token. + Google Contact Name is set to flags to set_value after Authorization Code is obtained. + """ + contact = xhiveframework.get_doc("Google Contacts", g_contact) + contact.check_permission("write") + + oauth_code = code or contact.get_password("authorization_code") + oauth_obj = GoogleOAuth("contacts") + + if not oauth_code or reauthorize: + return oauth_obj.get_authentication_url( + { + "g_contact": g_contact, + "redirect": f"/app/Form/{quote('Google Contacts')}/{quote(g_contact)}", + }, + ) + + r = oauth_obj.authorize(oauth_code) + contact.authorization_code = oauth_code + contact.refresh_token = r.get("refresh_token") + contact.save() + + +def get_google_contacts_object(g_contact): + """ + Returns an object of Google Calendar along with Google Calendar doc. + """ + account = xhiveframework.get_doc("Google Contacts", g_contact) + oauth_obj = GoogleOAuth("contacts") + + google_contacts = oauth_obj.get_google_service_object( + account.get_access_token(), + account.get_password(fieldname="indexing_refresh_token", raise_exception=False), + ) + + return google_contacts, account + + +@xhiveframework.whitelist() +def sync(g_contact=None): + filters = {"enable": 1} + + if g_contact: + filters.update({"name": g_contact}) + + google_contacts = xhiveframework.get_list("Google Contacts", filters=filters) + + for g in google_contacts: + return sync_contacts_from_google_contacts(g.name) + + +def sync_contacts_from_google_contacts(g_contact): + """ + Syncs Contacts from Google Contacts. + https://developers.google.com/people/api/rest/v1/people.connections/list + """ + google_contacts, account = get_google_contacts_object(g_contact) + + if not account.pull_from_google_contacts: + return + + results = [] + contacts_updated = 0 + + sync_token = account.get_password(fieldname="next_sync_token", raise_exception=False) or None + contacts = xhiveframework._dict() + + while True: + try: + contacts = ( + google_contacts.people() + .connections() + .list( + resourceName="people/me", + pageToken=contacts.get("nextPageToken"), + syncToken=sync_token, + pageSize=2000, + requestSyncToken=True, + personFields="names,emailAddresses,organizations,phoneNumbers", + ) + .execute() + ) + + except HttpError as err: + xhiveframework.throw( + _( + "Google Contacts - Could not sync contacts from Google Contacts {0}, error code {1}." + ).format(account.name, err.resp.status) + ) + + results.extend(contact for contact in contacts.get("connections", [])) + if not contacts.get("nextPageToken"): + if contacts.get("nextSyncToken"): + xhiveframework.db.set_value( + "Google Contacts", account.name, "next_sync_token", contacts.get("nextSyncToken") + ) + xhiveframework.db.commit() + break + + xhiveframework.db.set_value("Google Contacts", account.name, "last_sync_on", xhiveframework.utils.now_datetime()) + + for idx, connection in enumerate(results): + xhiveframework.publish_realtime( + "import_google_contacts", dict(progress=idx + 1, total=len(results)), user=xhiveframework.session.user + ) + # Work-around to fix + # https://lab.membtech.com/xhiveframework/xhiveframework15/issues/22648 + if not connection.get("names"): + continue + + for name in connection.get("names"): + if name.get("metadata").get("primary"): + contacts_updated += 1 + contact = xhiveframework.get_doc( + { + "doctype": "Contact", + "first_name": name.get("givenName") or "", + "middle_name": name.get("middleName") or "", + "last_name": name.get("familyName") or "", + "designation": get_indexed_value(connection.get("organizations"), 0, "title"), + "pulled_from_google_contacts": 1, + "google_contacts": account.name, + "company_name": get_indexed_value(connection.get("organizations"), 0, "name"), + } + ) + + for email in connection.get("emailAddresses", []): + contact.add_email( + email_id=email.get("value"), + is_primary=1 if email.get("metadata").get("primary") else 0, + ) + + for phone in connection.get("phoneNumbers", []): + contact.add_phone( + phone=phone.get("value"), + is_primary_phone=1 if phone.get("metadata").get("primary") else 0, + ) + + contact.insert(ignore_permissions=True) + + return ( + _("{0} Google Contacts synced.").format(contacts_updated) + if contacts_updated > 0 + else _("No new Google Contacts synced.") + ) + + +def insert_contacts_to_google_contacts(doc, method=None): + """ + Syncs Contacts from Google Contacts. + https://developers.google.com/people/api/rest/v1/people/createContact + """ + if ( + not xhiveframework.db.exists("Google Contacts", {"name": doc.google_contacts}) + or doc.pulled_from_google_contacts + or not doc.sync_with_google_contacts + ): + return + + google_contacts, account = get_google_contacts_object(doc.google_contacts) + + if not account.push_to_google_contacts: + return + + names = {"givenName": doc.first_name, "middleName": doc.middle_name, "familyName": doc.last_name} + + phoneNumbers = [{"value": phone_no.phone} for phone_no in doc.phone_nos] + emailAddresses = [{"value": email_id.email_id} for email_id in doc.email_ids] + + try: + contact = ( + google_contacts.people() + .createContact( + body={"names": [names], "phoneNumbers": phoneNumbers, "emailAddresses": emailAddresses} + ) + .execute() + ) + xhiveframework.db.set_value("Contact", doc.name, "google_contacts_id", contact.get("resourceName")) + except HttpError as err: + xhiveframework.msgprint( + _("Google Calendar - Could not insert contact in Google Contacts {0}, error code {1}.").format( + account.name, err.resp.status + ) + ) + + +def update_contacts_to_google_contacts(doc, method=None): + """ + Syncs Contacts from Google Contacts. + https://developers.google.com/people/api/rest/v1/people/updateContact + """ + # Workaround to avoid triggering updation when Event is being inserted since + # creation and modified are same when inserting doc + if ( + not xhiveframework.db.exists("Google Contacts", {"name": doc.google_contacts}) + or doc.modified == doc.creation + or not doc.sync_with_google_contacts + ): + return + + if doc.sync_with_google_contacts and not doc.google_contacts_id: + # If sync_with_google_contacts is checked later, then insert the contact rather than updating it. + insert_contacts_to_google_contacts(doc) + return + + google_contacts, account = get_google_contacts_object(doc.google_contacts) + + if not account.push_to_google_contacts: + return + + names = {"givenName": doc.first_name, "middleName": doc.middle_name, "familyName": doc.last_name} + + phoneNumbers = [{"value": phone_no.phone} for phone_no in doc.phone_nos] + emailAddresses = [{"value": email_id.email_id} for email_id in doc.email_ids] + + try: + contact = ( + google_contacts.people() + .get( + resourceName=doc.google_contacts_id, + personFields="names,emailAddresses,organizations,phoneNumbers", + ) + .execute() + ) + + contact["names"] = [names] + contact["phoneNumbers"] = phoneNumbers + contact["emailAddresses"] = emailAddresses + + google_contacts.people().updateContact( + resourceName=doc.google_contacts_id, + body={ + "names": [names], + "phoneNumbers": phoneNumbers, + "emailAddresses": emailAddresses, + "etag": contact.get("etag"), + }, + updatePersonFields="names,emailAddresses,organizations,phoneNumbers", + ).execute() + xhiveframework.msgprint(_("Contact Synced with Google Contacts.")) + except HttpError as err: + xhiveframework.msgprint( + _("Google Contacts - Could not update contact in Google Contacts {0}, error code {1}.").format( + account.name, err.resp.status + ) + ) + + +def get_indexed_value(d, index, key): + if not d: + return "" + + try: + return d[index].get(key) + except IndexError: + return "" diff --git a/xhiveframework/integrations/doctype/google_contacts/test_google_contacts.py b/xhiveframework/integrations/doctype/google_contacts/test_google_contacts.py new file mode 100644 index 0000000..04a33a1 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_contacts/test_google_contacts.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestGoogleContacts(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/google_drive/__init__.py b/xhiveframework/integrations/doctype/google_drive/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/google_drive/google_drive.js b/xhiveframework/integrations/doctype/google_drive/google_drive.js new file mode 100644 index 0000000..f50a07f --- /dev/null +++ b/xhiveframework/integrations/doctype/google_drive/google_drive.js @@ -0,0 +1,71 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Google Drive", { + refresh: function (frm) { + if (!frm.doc.enable) { + frm.dashboard.set_headline( + __("To use Google Drive, enable {0}.", [ + `${__("Google Settings")}`, + ]) + ); + } + + xhiveframework.realtime.on("upload_to_google_drive", (data) => { + if (data.progress) { + const progress_title = __("Uploading to Google Drive"); + frm.dashboard.show_progress( + progress_title, + (data.progress / data.total) * 100, + data.message + ); + if (data.progress === data.total) { + frm.dashboard.hide_progress(progress_title); + } + } + }); + + if (frm.doc.enable && frm.doc.refresh_token) { + let sync_button = frm.add_custom_button(__("Take Backup"), function () { + xhiveframework.show_alert({ + indicator: "green", + message: __("Backing up to Google Drive."), + }); + xhiveframework + .call({ + method: "xhiveframework.integrations.doctype.google_drive.google_drive.take_backup", + btn: sync_button, + }) + .then((r) => { + xhiveframework.msgprint(r.message); + }); + }); + } + + if (frm.doc.enable && frm.doc.backup_folder_name && !frm.doc.refresh_token) { + frm.dashboard.set_headline( + __( + "Click on Authorize Google Drive Access to authorize Google Drive Access." + ) + ); + } + + if (frm.doc.enable && frm.doc.refresh_token && frm.doc.authorization_code) { + frm.page.set_indicator("Authorized", "green"); + } + }, + authorize_google_drive_access: function (frm) { + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.google_drive.google_drive.authorize_access", + args: { + reauthorize: frm.doc.authorization_code ? 1 : 0, + }, + callback: function (r) { + if (!r.exc) { + frm.save(); + window.open(r.message.url); + } + }, + }); + }, +}); diff --git a/xhiveframework/integrations/doctype/google_drive/google_drive.json b/xhiveframework/integrations/doctype/google_drive/google_drive.json new file mode 100644 index 0000000..7bc967a --- /dev/null +++ b/xhiveframework/integrations/doctype/google_drive/google_drive.json @@ -0,0 +1,126 @@ +{ + "actions": [], + "creation": "2019-08-13 17:24:05.470876", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "enable", + "google_drive_section", + "backup_folder_name", + "frequency", + "email", + "send_email_for_successful_backup", + "file_backup", + "authorize_google_drive_access", + "column_break_5", + "backup_folder_id", + "last_backup_on", + "refresh_token", + "authorization_code" + ], + "fields": [ + { + "default": "0", + "fieldname": "enable", + "fieldtype": "Check", + "label": "Enable" + }, + { + "fieldname": "backup_folder_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Backup Folder Name", + "reqd": 1 + }, + { + "depends_on": "eval:!doc.__islocal", + "fieldname": "authorize_google_drive_access", + "fieldtype": "Button", + "label": "Authorize Google Drive Access" + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "backup_folder_id", + "fieldtype": "Data", + "label": "Backup Folder ID", + "read_only": 1 + }, + { + "fieldname": "frequency", + "fieldtype": "Select", + "label": "Frequency", + "options": "\nDaily\nWeekly", + "reqd": 1 + }, + { + "fieldname": "refresh_token", + "fieldtype": "Data", + "hidden": 1, + "label": "Refresh Token" + }, + { + "fieldname": "authorization_code", + "fieldtype": "Data", + "hidden": 1, + "label": "Authorization Code" + }, + { + "fieldname": "last_backup_on", + "fieldtype": "Datetime", + "label": "Last Backup On", + "read_only": 1 + }, + { + "default": "0", + "description": "Note: By default emails for failed backups are sent.", + "fieldname": "send_email_for_successful_backup", + "fieldtype": "Check", + "label": "Send Email for Successful backup" + }, + { + "default": "0", + "fieldname": "file_backup", + "fieldtype": "Check", + "label": "File Backup" + }, + { + "depends_on": "enable", + "fieldname": "google_drive_section", + "fieldtype": "Section Break", + "label": "Google Drive" + }, + { + "fieldname": "email", + "fieldtype": "Data", + "label": "Send Notification To", + "options": "Email", + "reqd": 1 + } + ], + "issingle": 1, + "links": [], + "modified": "2023-12-08 15:52:37.525003", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Google Drive", + "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": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/google_drive/google_drive.py b/xhiveframework/integrations/doctype/google_drive/google_drive.py new file mode 100644 index 0000000..de95a8e --- /dev/null +++ b/xhiveframework/integrations/doctype/google_drive/google_drive.py @@ -0,0 +1,231 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import os +from urllib.parse import quote + +from apiclient.http import MediaFileUpload +from googleapiclient.errors import HttpError + +import xhiveframework +from xhiveframework import _ +from xhiveframework.integrations.google_oauth import GoogleOAuth +from xhiveframework.integrations.offsite_backup_utils import ( + get_latest_backup_file, + send_email, + validate_file_size, +) +from xhiveframework.model.document import Document +from xhiveframework.utils import get_backups_path, get_bench_path +from xhiveframework.utils.background_jobs import enqueue +from xhiveframework.utils.backups import new_backup + + +class GoogleDrive(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + authorization_code: DF.Data | None + backup_folder_id: DF.Data | None + backup_folder_name: DF.Data + email: DF.Data + enable: DF.Check + file_backup: DF.Check + frequency: DF.Literal["", "Daily", "Weekly"] + last_backup_on: DF.Datetime | None + refresh_token: DF.Data | None + send_email_for_successful_backup: DF.Check + + # end: auto-generated types + def validate(self): + doc_before_save = self.get_doc_before_save() + if doc_before_save and doc_before_save.backup_folder_name != self.backup_folder_name: + self.backup_folder_id = "" + + def get_access_token(self): + if not self.refresh_token: + button_label = xhiveframework.bold(_("Allow Google Drive Access")) + raise xhiveframework.ValidationError(_("Click on {0} to generate Refresh Token.").format(button_label)) + + oauth_obj = GoogleOAuth("drive") + r = oauth_obj.refresh_access_token( + self.get_password(fieldname="refresh_token", raise_exception=False) + ) + + return r.get("access_token") + + +@xhiveframework.whitelist(methods=["POST"]) +def authorize_access(reauthorize=False, code=None): + """ + If no Authorization code get it from Google and then request for Refresh Token. + Google Contact Name is set to flags to set_value after Authorization Code is obtained. + """ + + oauth_code = xhiveframework.db.get_single_value("Google Drive", "authorization_code") if not code else code + oauth_obj = GoogleOAuth("drive") + + if not oauth_code or reauthorize: + if reauthorize: + xhiveframework.db.set_single_value("Google Drive", "backup_folder_id", "") + return oauth_obj.get_authentication_url( + { + "redirect": f"/app/Form/{quote('Google Drive')}", + }, + ) + + r = oauth_obj.authorize(oauth_code) + xhiveframework.db.set_single_value( + "Google Drive", + {"authorization_code": oauth_code, "refresh_token": r.get("refresh_token")}, + ) + + +def get_google_drive_object(): + """ + Returns an object of Google Drive. + """ + account = xhiveframework.get_doc("Google Drive") + oauth_obj = GoogleOAuth("drive") + + google_drive = oauth_obj.get_google_service_object( + account.get_access_token(), + account.get_password(fieldname="indexing_refresh_token", raise_exception=False), + ) + + return google_drive, account + + +def check_for_folder_in_google_drive(): + """Checks if folder exists in Google Drive else create it.""" + + def _create_folder_in_google_drive(google_drive, account): + file_metadata = { + "name": account.backup_folder_name, + "mimeType": "application/vnd.google-apps.folder", + } + + try: + folder = google_drive.files().create(body=file_metadata, fields="id").execute() + xhiveframework.db.set_single_value("Google Drive", "backup_folder_id", folder.get("id")) + xhiveframework.db.commit() + except HttpError as e: + xhiveframework.throw( + _("Google Drive - Could not create folder in Google Drive - Error Code {0}").format(e) + ) + + google_drive, account = get_google_drive_object() + + if account.backup_folder_id: + return + + backup_folder_exists = False + + try: + google_drive_folders = ( + google_drive.files().list(q="mimeType='application/vnd.google-apps.folder'").execute() + ) + except HttpError as e: + xhiveframework.throw(_("Google Drive - Could not find folder in Google Drive - Error Code {0}").format(e)) + + for f in google_drive_folders.get("files"): + if f.get("name") == account.backup_folder_name: + xhiveframework.db.set_single_value("Google Drive", "backup_folder_id", f.get("id")) + xhiveframework.db.commit() + backup_folder_exists = True + break + + if not backup_folder_exists: + _create_folder_in_google_drive(google_drive, account) + + +@xhiveframework.whitelist() +def take_backup(): + """Enqueue longjob for taking backup to Google Drive""" + enqueue( + "xhiveframework.integrations.doctype.google_drive.google_drive.upload_system_backup_to_google_drive", + queue="long", + timeout=1500, + ) + xhiveframework.msgprint(_("Queued for backup. It may take a few minutes to an hour.")) + + +def upload_system_backup_to_google_drive(): + """ + Upload system backup to Google Drive + """ + # Get Google Drive Object + google_drive, account = get_google_drive_object() + + # Check if folder exists in Google Drive + check_for_folder_in_google_drive() + account.load_from_db() + + validate_file_size() + + if xhiveframework.flags.create_new_backup: + set_progress(1, _("Backing up Data.")) + backup = new_backup() + file_urls = [] + file_urls.append(backup.backup_path_db) + file_urls.append(backup.backup_path_conf) + + if account.file_backup: + file_urls.append(backup.backup_path_files) + file_urls.append(backup.backup_path_private_files) + else: + file_urls = get_latest_backup_file(with_files=account.file_backup) + + for fileurl in file_urls: + if not fileurl: + continue + + file_metadata = {"name": os.path.basename(fileurl), "parents": [account.backup_folder_id]} + + try: + media = MediaFileUpload( + get_absolute_path(filename=fileurl), mimetype="application/gzip", resumable=True + ) + except OSError as e: + xhiveframework.throw(_("Google Drive - Could not locate - {0}").format(e)) + + try: + set_progress(2, _("Uploading backup to Google Drive.")) + google_drive.files().create(body=file_metadata, media_body=media, fields="id").execute() + except HttpError as e: + send_email(False, "Google Drive", "Google Drive", "email", error_status=e) + + set_progress(3, _("Uploading successful.")) + xhiveframework.db.set_single_value("Google Drive", "last_backup_on", xhiveframework.utils.now_datetime()) + send_email(True, "Google Drive", "Google Drive", "email") + return _("Google Drive Backup Successful.") + + +def daily_backup(): + drive_settings = xhiveframework.db.get_singles_dict("Google Drive", cast=True) + if drive_settings.enable and drive_settings.frequency == "Daily": + upload_system_backup_to_google_drive() + + +def weekly_backup(): + drive_settings = xhiveframework.db.get_singles_dict("Google Drive", cast=True) + if drive_settings.enable and drive_settings.frequency == "Weekly": + upload_system_backup_to_google_drive() + + +def get_absolute_path(filename): + file_path = os.path.join(get_backups_path()[2:], os.path.basename(filename)) + return f"{get_bench_path()}/sites/{file_path}" + + +def set_progress(progress, message): + xhiveframework.publish_realtime( + "upload_to_google_drive", + dict(progress=progress, total=3, message=message), + user=xhiveframework.session.user, + ) diff --git a/xhiveframework/integrations/doctype/google_drive/test_google_drive.py b/xhiveframework/integrations/doctype/google_drive/test_google_drive.py new file mode 100644 index 0000000..77dddbd --- /dev/null +++ b/xhiveframework/integrations/doctype/google_drive/test_google_drive.py @@ -0,0 +1,8 @@ +# Copyright (c) 2019, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestGoogleDrive(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/google_settings/__init__.py b/xhiveframework/integrations/doctype/google_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/google_settings/google_settings.js b/xhiveframework/integrations/doctype/google_settings/google_settings.js new file mode 100644 index 0000000..015a8c8 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_settings/google_settings.js @@ -0,0 +1,14 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Google Settings", { + refresh: function (frm) { + frm.dashboard.set_headline( + __("For more information, {0}.", [ + `${__( + "Click here" + )}`, + ]) + ); + }, +}); diff --git a/xhiveframework/integrations/doctype/google_settings/google_settings.json b/xhiveframework/integrations/doctype/google_settings/google_settings.json new file mode 100644 index 0000000..ee48fde --- /dev/null +++ b/xhiveframework/integrations/doctype/google_settings/google_settings.json @@ -0,0 +1,100 @@ +{ + "actions": [], + "creation": "2019-06-14 00:08:37.255003", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "enable", + "sb_00", + "client_id", + "client_secret", + "sb_01", + "api_key", + "section_break_7", + "google_drive_picker_enabled", + "app_id" + ], + "fields": [ + { + "default": "0", + "fieldname": "enable", + "fieldtype": "Check", + "label": "Enable" + }, + { + "description": "The Client ID obtained from the Google Cloud Console under \n\"APIs & Services\" > \"Credentials\"\n", + "fieldname": "client_id", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Client ID", + "mandatory_depends_on": "google_drive_picker_enabled" + }, + { + "fieldname": "client_secret", + "fieldtype": "Password", + "in_list_view": 1, + "label": "Client Secret" + }, + { + "description": "The browser API key obtained from the Google Cloud Console under \n\"APIs & Services\" > \"Credentials\"\n", + "fieldname": "api_key", + "fieldtype": "Data", + "label": "API Key" + }, + { + "depends_on": "enable", + "fieldname": "sb_00", + "fieldtype": "Section Break", + "label": "OAuth Client ID" + }, + { + "depends_on": "enable", + "fieldname": "sb_01", + "fieldtype": "Section Break", + "label": "API Key" + }, + { + "depends_on": "google_drive_picker_enabled", + "description": "The project number obtained from Google Cloud Console under \n\"IAM & Admin\" > \"Settings\"\n", + "fieldname": "app_id", + "fieldtype": "Data", + "label": "App ID", + "mandatory_depends_on": "google_drive_picker_enabled" + }, + { + "fieldname": "section_break_7", + "fieldtype": "Section Break", + "label": "Google Drive Picker" + }, + { + "default": "0", + "fieldname": "google_drive_picker_enabled", + "fieldtype": "Check", + "label": "Google Drive Picker Enabled" + } + ], + "issingle": 1, + "links": [], + "modified": "2024-01-16 13:19:22.365362", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Google Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "ASC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/google_settings/google_settings.py b/xhiveframework/integrations/doctype/google_settings/google_settings.py new file mode 100644 index 0000000..d141ddd --- /dev/null +++ b/xhiveframework/integrations/doctype/google_settings/google_settings.py @@ -0,0 +1,39 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class GoogleSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + api_key: DF.Data | None + app_id: DF.Data | None + client_id: DF.Data | None + client_secret: DF.Password | None + enable: DF.Check + google_drive_picker_enabled: DF.Check + # end: auto-generated types + + pass + + +@xhiveframework.whitelist() +def get_file_picker_settings(): + """Return all the data FileUploader needs to start the Google Drive Picker.""" + google_settings = xhiveframework.get_single("Google Settings") + if not (google_settings.enable and google_settings.google_drive_picker_enabled): + return {} + + return { + "enabled": True, + "appId": google_settings.app_id, + "clientId": google_settings.client_id, + } diff --git a/xhiveframework/integrations/doctype/google_settings/test_google_settings.py b/xhiveframework/integrations/doctype/google_settings/test_google_settings.py new file mode 100644 index 0000000..6fc6358 --- /dev/null +++ b/xhiveframework/integrations/doctype/google_settings/test_google_settings.py @@ -0,0 +1,42 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +from .google_settings import get_file_picker_settings + + +class TestGoogleSettings(XhiveFrameworkTestCase): + def setUp(self): + settings = xhiveframework.get_single("Google Settings") + settings.client_id = "test_client_id" + settings.app_id = "test_app_id" + settings.api_key = "test_api_key" + settings.save() + + def test_picker_disabled(self): + """Google Drive Picker should be disabled if it is not enabled in Google Settings.""" + xhiveframework.db.set_single_value("Google Settings", "enable", 1) + xhiveframework.db.set_single_value("Google Settings", "google_drive_picker_enabled", 0) + settings = get_file_picker_settings() + + self.assertEqual(settings, {}) + + def test_google_disabled(self): + """Google Drive Picker should be disabled if Google integration is not enabled.""" + xhiveframework.db.set_single_value("Google Settings", "enable", 0) + xhiveframework.db.set_single_value("Google Settings", "google_drive_picker_enabled", 1) + settings = get_file_picker_settings() + + self.assertEqual(settings, {}) + + def test_picker_enabled(self): + """If picker is enabled, get_file_picker_settings should return the credentials.""" + xhiveframework.db.set_single_value("Google Settings", "enable", 1) + xhiveframework.db.set_single_value("Google Settings", "google_drive_picker_enabled", 1) + settings = get_file_picker_settings() + + self.assertEqual(True, settings.get("enabled", False)) + self.assertEqual("test_client_id", settings.get("clientId", "")) + self.assertEqual("test_app_id", settings.get("appId", "")) diff --git a/xhiveframework/integrations/doctype/integration_request/__init__.py b/xhiveframework/integrations/doctype/integration_request/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/integration_request/integration_request.js b/xhiveframework/integrations/doctype/integration_request/integration_request.js new file mode 100644 index 0000000..df401a8 --- /dev/null +++ b/xhiveframework/integrations/doctype/integration_request/integration_request.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Integration Request", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/integration_request/integration_request.json b/xhiveframework/integrations/doctype/integration_request/integration_request.json new file mode 100644 index 0000000..8565b2a --- /dev/null +++ b/xhiveframework/integrations/doctype/integration_request/integration_request.json @@ -0,0 +1,154 @@ +{ + "actions": [], + "creation": "2022-03-28 12:25:29.929952", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "request_id", + "integration_request_service", + "is_remote_request", + "column_break_5", + "request_description", + "status", + "section_break_8", + "url", + "request_headers", + "data", + "response_section", + "output", + "error", + "reference_section", + "reference_doctype", + "column_break_16", + "reference_docname" + ], + "fields": [ + { + "fieldname": "integration_request_service", + "fieldtype": "Data", + "label": "Service", + "read_only": 1 + }, + { + "default": "Queued", + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "\nQueued\nAuthorized\nCompleted\nCancelled\nFailed", + "read_only": 1 + }, + { + "fieldname": "data", + "fieldtype": "Code", + "label": "Request Data", + "read_only": 1 + }, + { + "fieldname": "output", + "fieldtype": "Code", + "label": "Output", + "read_only": 1 + }, + { + "fieldname": "error", + "fieldtype": "Code", + "label": "Error", + "read_only": 1 + }, + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "label": "Reference Document Type", + "options": "DocType", + "read_only": 1 + }, + { + "fieldname": "reference_docname", + "fieldtype": "Dynamic Link", + "label": "Reference Document Name", + "options": "reference_doctype", + "read_only": 1 + }, + { + "default": "0", + "fieldname": "is_remote_request", + "fieldtype": "Check", + "label": "Is Remote Request?", + "read_only": 1 + }, + { + "fieldname": "request_description", + "fieldtype": "Data", + "label": "Request Description", + "read_only": 1 + }, + { + "fieldname": "request_id", + "fieldtype": "Data", + "label": "Request ID", + "read_only": 1 + }, + { + "fieldname": "column_break_5", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break" + }, + { + "fieldname": "url", + "fieldtype": "Small Text", + "label": "URL", + "read_only": 1 + }, + { + "fieldname": "response_section", + "fieldtype": "Section Break", + "label": "Response" + }, + { + "depends_on": "eval:doc.reference_doctype", + "fieldname": "reference_section", + "fieldtype": "Section Break", + "label": "Reference" + }, + { + "fieldname": "column_break_16", + "fieldtype": "Column Break" + }, + { + "fieldname": "request_headers", + "fieldtype": "Code", + "label": "Request Headers", + "read_only": 1 + } + ], + "in_create": 1, + "links": [], + "modified": "2023-10-09 09:36:23.856188", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Integration Request", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "integration_request_service", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/integration_request/integration_request.py b/xhiveframework/integrations/doctype/integration_request/integration_request.py new file mode 100644 index 0000000..23a2c00 --- /dev/null +++ b/xhiveframework/integrations/doctype/integration_request/integration_request.py @@ -0,0 +1,66 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework.integrations.utils import json_handler +from xhiveframework.model.document import Document + + +class IntegrationRequest(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data: DF.Code | None + error: DF.Code | None + integration_request_service: DF.Data | None + is_remote_request: DF.Check + output: DF.Code | None + reference_docname: DF.DynamicLink | None + reference_doctype: DF.Link | None + request_description: DF.Data | None + request_headers: DF.Code | None + request_id: DF.Data | None + status: DF.Literal["", "Queued", "Authorized", "Completed", "Cancelled", "Failed"] + url: DF.SmallText | None + + # end: auto-generated types + def autoname(self): + if self.flags._name: + self.name = self.flags._name + + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Integration Request") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) + + def update_status(self, params, status): + data = json.loads(self.data) + data.update(params) + + self.data = json.dumps(data) + self.status = status + self.save(ignore_permissions=True) + xhiveframework.db.commit() + + def handle_success(self, response): + """update the output field with the response along with the relevant status""" + if isinstance(response, str): + response = json.loads(response) + self.db_set("status", "Completed") + self.db_set("output", json.dumps(response, default=json_handler)) + + def handle_failure(self, response): + """update the error field with the response along with the relevant status""" + if isinstance(response, str): + response = json.loads(response) + self.db_set("status", "Failed") + self.db_set("error", json.dumps(response, default=json_handler)) diff --git a/xhiveframework/integrations/doctype/integration_request/integration_request_list.js b/xhiveframework/integrations/doctype/integration_request/integration_request_list.js new file mode 100644 index 0000000..289a2ff --- /dev/null +++ b/xhiveframework/integrations/doctype/integration_request/integration_request_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Integration Request"] = { + onload: function (list_view) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(list_view.doctype); + }); + }, +}; diff --git a/xhiveframework/integrations/doctype/integration_request/test_integration_request.py b/xhiveframework/integrations/doctype/integration_request/test_integration_request.py new file mode 100644 index 0000000..92848ff --- /dev/null +++ b/xhiveframework/integrations/doctype/integration_request/test_integration_request.py @@ -0,0 +1,10 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('Integration Request') + + +class TestIntegrationRequest(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/ldap_group_mapping/__init__.py b/xhiveframework/integrations/doctype/ldap_group_mapping/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.json b/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.json new file mode 100644 index 0000000..95c7dc2 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.json @@ -0,0 +1,41 @@ +{ + "actions": [], + "creation": "2019-05-29 01:24:29.585060", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "ldap_group", + "xhiveerp_role" + ], + "fields": [ + { + "fieldname": "ldap_group", + "fieldtype": "Data", + "in_list_view": 1, + "label": "LDAP Group", + "reqd": 1 + }, + { + "fieldname": "xhiveerp_role", + "fieldtype": "Link", + "in_list_view": 1, + "label": "User Role", + "options": "Role", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2022-07-07 16:28:44.828514", + "modified_by": "Administrator", + "module": "Integrations", + "name": "LDAP Group Mapping", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "ASC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.py b/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.py new file mode 100644 index 0000000..47aa1ee --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_group_mapping/ldap_group_mapping.py @@ -0,0 +1,23 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class LDAPGroupMapping(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + xhiveerp_role: DF.Link + ldap_group: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/ldap_settings/__init__.py b/xhiveframework/integrations/doctype/ldap_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.js b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.js new file mode 100644 index 0000000..2ccd434 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("LDAP Settings", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.json b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.json new file mode 100644 index 0000000..0b3bf06 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.json @@ -0,0 +1,327 @@ +{ + "actions": [], + "creation": "2016-09-22 04:16:48.829658", + "doctype": "DocType", + "document_type": "System", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enabled", + "ldap_server_settings_section", + "ldap_directory_server", + "column_break_4", + "ldap_server_url", + "ldap_auth_section", + "base_dn", + "column_break_8", + "password", + "ldap_search_and_paths_section", + "ldap_search_path_user", + "ldap_search_string", + "column_break_12", + "ldap_search_path_group", + "ldap_user_creation_and_mapping_section", + "ldap_email_field", + "ldap_username_field", + "ldap_first_name_field", + "do_not_create_new_user", + "column_break_19", + "ldap_middle_name_field", + "ldap_last_name_field", + "ldap_phone_field", + "ldap_mobile_field", + "ldap_security", + "ssl_tls_mode", + "require_trusted_certificate", + "column_break_27", + "local_private_key_file", + "local_server_certificate_file", + "local_ca_certs_file", + "ldap_custom_settings_section", + "ldap_group_objectclass", + "ldap_custom_group_search", + "column_break_33", + "ldap_group_member_attribute", + "ldap_group_mappings_section", + "default_user_type", + "column_break_38", + "default_role", + "section_break_40", + "ldap_groups", + "ldap_group_field" + ], + "fields": [ + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "fieldname": "ldap_server_url", + "fieldtype": "Data", + "in_list_view": 1, + "label": "LDAP Server Url", + "reqd": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "base_dn", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Base Distinguished Name (DN)", + "reqd": 1 + }, + { + "fieldname": "password", + "fieldtype": "Password", + "in_list_view": 1, + "label": "Password for Base DN", + "reqd": 1 + }, + { + "depends_on": "eval: doc.default_user_type == \"System User\"", + "fieldname": "default_role", + "fieldtype": "Link", + "label": "Default User Role", + "mandatory_depends_on": "eval: doc.default_user_type == \"System User\"", + "options": "Role" + }, + { + "description": "Must be enclosed in '()' and include '{0}', which is a placeholder for the user/login name. i.e. (&(objectclass=user)(uid={0}))", + "fieldname": "ldap_search_string", + "fieldtype": "Data", + "label": "LDAP Search String", + "reqd": 1 + }, + { + "fieldname": "ldap_email_field", + "fieldtype": "Data", + "label": "LDAP Email Field", + "reqd": 1 + }, + { + "fieldname": "ldap_username_field", + "fieldtype": "Data", + "label": "LDAP Username Field", + "reqd": 1 + }, + { + "fieldname": "ldap_first_name_field", + "fieldtype": "Data", + "label": "LDAP First Name Field", + "reqd": 1 + }, + { + "fieldname": "ldap_middle_name_field", + "fieldtype": "Data", + "label": "LDAP Middle Name Field" + }, + { + "fieldname": "ldap_last_name_field", + "fieldtype": "Data", + "label": "LDAP Last Name Field" + }, + { + "fieldname": "ldap_phone_field", + "fieldtype": "Data", + "label": "LDAP Phone Field" + }, + { + "fieldname": "ldap_mobile_field", + "fieldtype": "Data", + "label": "LDAP Mobile Field" + }, + { + "fieldname": "ldap_security", + "fieldtype": "Section Break", + "label": "LDAP Security" + }, + { + "default": "Off", + "fieldname": "ssl_tls_mode", + "fieldtype": "Select", + "label": "SSL/TLS Mode", + "options": "Off\nStartTLS" + }, + { + "default": "No", + "fieldname": "require_trusted_certificate", + "fieldtype": "Select", + "label": "Require Trusted Certificate", + "options": "No\nYes", + "reqd": 1 + }, + { + "fieldname": "local_private_key_file", + "fieldtype": "Data", + "label": "Path to private Key File" + }, + { + "fieldname": "local_server_certificate_file", + "fieldtype": "Data", + "label": "Path to Server Certificate" + }, + { + "fieldname": "local_ca_certs_file", + "fieldtype": "Data", + "label": "Path to CA Certs File" + }, + { + "fieldname": "ldap_group_mappings_section", + "fieldtype": "Section Break", + "label": "LDAP Group Mappings" + }, + { + "description": "NOTE: This box is due for depreciation. Please re-setup LDAP to work with the newer settings", + "fieldname": "ldap_group_field", + "fieldtype": "Data", + "label": "LDAP Group Field" + }, + { + "fieldname": "ldap_groups", + "fieldtype": "Table", + "label": "LDAP Group Mappings", + "options": "LDAP Group Mapping" + }, + { + "fieldname": "ldap_server_settings_section", + "fieldtype": "Section Break", + "label": "LDAP Server Settings" + }, + { + "fieldname": "ldap_auth_section", + "fieldtype": "Section Break", + "label": "LDAP Auth" + }, + { + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, + { + "fieldname": "ldap_search_and_paths_section", + "fieldtype": "Section Break", + "label": "LDAP Search and Paths" + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, + { + "fieldname": "ldap_user_creation_and_mapping_section", + "fieldtype": "Section Break", + "label": "LDAP User Creation and Mapping" + }, + { + "fieldname": "column_break_19", + "fieldtype": "Column Break" + }, + { + "fieldname": "column_break_27", + "fieldtype": "Column Break" + }, + { + "description": "These settings are required if 'Custom' LDAP Directory is used", + "fieldname": "ldap_custom_settings_section", + "fieldtype": "Section Break", + "label": "LDAP Custom Settings" + }, + { + "fieldname": "column_break_33", + "fieldtype": "Column Break" + }, + { + "description": "string value, i.e. member", + "fieldname": "ldap_group_member_attribute", + "fieldtype": "Data", + "label": "LDAP Group Member attribute" + }, + { + "description": "Please select the LDAP Directory being used", + "fieldname": "ldap_directory_server", + "fieldtype": "Select", + "label": "Directory Server", + "options": "\nActive Directory\nOpenLDAP\nCustom", + "reqd": 1 + }, + { + "description": "string value, i.e. group", + "fieldname": "ldap_group_objectclass", + "fieldtype": "Data", + "label": "Group Object Class" + }, + { + "description": "string value, i.e. {0} or uid={0},ou=users,dc=example,dc=com", + "fieldname": "ldap_custom_group_search", + "fieldtype": "Data", + "label": "Custom Group Search" + }, + { + "description": "Requires any valid fdn path. i.e. ou=users,dc=example,dc=com", + "fieldname": "ldap_search_path_user", + "fieldtype": "Data", + "in_list_view": 1, + "label": "LDAP search path for Users", + "reqd": 1 + }, + { + "description": "Requires any valid fdn path. i.e. ou=groups,dc=example,dc=com", + "fieldname": "ldap_search_path_group", + "fieldtype": "Data", + "label": "LDAP search path for Groups", + "reqd": 1 + }, + { + "fieldname": "default_user_type", + "fieldtype": "Link", + "label": "Default User Type", + "options": "User Type", + "reqd": 1 + }, + { + "fieldname": "column_break_38", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_40", + "fieldtype": "Section Break", + "hide_border": 1 + }, + { + "default": "0", + "description": "Do not create new user if user with email does not exist in the system", + "fieldname": "do_not_create_new_user", + "fieldtype": "Check", + "label": "Do Not Create New User " + } + ], + "in_create": 1, + "issingle": 1, + "links": [], + "modified": "2023-01-24 11:20:06.049708", + "modified_by": "Administrator", + "module": "Integrations", + "name": "LDAP Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "read_only": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.py b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.py new file mode 100644 index 0000000..eabad69 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/ldap_settings.py @@ -0,0 +1,426 @@ +# Copyright (c) 2022, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import ssl +from typing import TYPE_CHECKING + +import ldap3 +from ldap3 import AUTO_BIND_TLS_BEFORE_BIND, HASHED_SALTED_SHA, MODIFY_REPLACE +from ldap3.abstract.entry import Entry +from ldap3.core.exceptions import ( + LDAPAttributeError, + LDAPInvalidCredentialsResult, + LDAPInvalidFilterError, + LDAPNoSuchObjectResult, +) +from ldap3.utils.hashed import hashed + +import xhiveframework +from xhiveframework import _, safe_encode +from xhiveframework.model.document import Document +from xhiveframework.twofactor import authenticate_for_2factor, confirm_otp_token, should_run_2fa + +if TYPE_CHECKING: + from xhiveframework.core.doctype.user.user import User + + +class LDAPSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.integrations.doctype.ldap_group_mapping.ldap_group_mapping import LDAPGroupMapping + from xhiveframework.types import DF + + base_dn: DF.Data + default_role: DF.Link | None + default_user_type: DF.Link + do_not_create_new_user: DF.Check + enabled: DF.Check + ldap_custom_group_search: DF.Data | None + ldap_directory_server: DF.Literal["", "Active Directory", "OpenLDAP", "Custom"] + ldap_email_field: DF.Data + ldap_first_name_field: DF.Data + ldap_group_field: DF.Data | None + ldap_group_member_attribute: DF.Data | None + ldap_group_objectclass: DF.Data | None + ldap_groups: DF.Table[LDAPGroupMapping] + ldap_last_name_field: DF.Data | None + ldap_middle_name_field: DF.Data | None + ldap_mobile_field: DF.Data | None + ldap_phone_field: DF.Data | None + ldap_search_path_group: DF.Data + ldap_search_path_user: DF.Data + ldap_search_string: DF.Data + ldap_server_url: DF.Data + ldap_username_field: DF.Data + local_ca_certs_file: DF.Data | None + local_private_key_file: DF.Data | None + local_server_certificate_file: DF.Data | None + password: DF.Password + require_trusted_certificate: DF.Literal["No", "Yes"] + ssl_tls_mode: DF.Literal["Off", "StartTLS"] + + # end: auto-generated types + def validate(self): + self.default_user_type = self.default_user_type or "Website User" + + if not self.enabled: + return + + if not self.flags.ignore_mandatory: + if ( + self.ldap_search_string.count("(") == self.ldap_search_string.count(")") + and self.ldap_search_string.startswith("(") + and self.ldap_search_string.endswith(")") + and self.ldap_search_string + and "{0}" in self.ldap_search_string + ): + conn = self.connect_to_ldap( + base_dn=self.base_dn, password=self.get_password(raise_exception=False) + ) + + try: + if conn.result["type"] == "bindResponse" and self.base_dn: + conn.search( + search_base=self.ldap_search_path_user, + search_filter="(objectClass=*)", + attributes=self.get_ldap_attributes(), + ) + + conn.search( + search_base=self.ldap_search_path_group, + search_filter="(objectClass=*)", + attributes=["cn"], + ) + + except LDAPAttributeError as ex: + xhiveframework.throw( + _("LDAP settings incorrect. validation response was: {0}").format(ex), + title=_("Misconfigured"), + ) + + except LDAPNoSuchObjectResult: + xhiveframework.throw( + _("Ensure the user and group search paths are correct."), title=_("Misconfigured") + ) + + if self.ldap_directory_server.lower() == "custom": + if not self.ldap_group_member_attribute or not self.ldap_group_objectclass: + xhiveframework.throw( + _( + "Custom LDAP Directoy Selected, please ensure 'LDAP Group Member attribute' and 'Group Object Class' are entered" + ), + title=_("Misconfigured"), + ) + + if self.ldap_custom_group_search and "{0}" not in self.ldap_custom_group_search: + xhiveframework.throw( + _( + "Custom Group Search if filled needs to contain the user placeholder {0}, eg uid={0},ou=users,dc=example,dc=com" + ), + title=_("Misconfigured"), + ) + + else: + xhiveframework.throw( + _( + "LDAP Search String must be enclosed in '()' and needs to contian the user placeholder {0}, eg sAMAccountName={0}" + ) + ) + + def connect_to_ldap(self, base_dn, password, read_only=True) -> ldap3.Connection: + try: + if self.require_trusted_certificate == "Yes": + tls_configuration = ldap3.Tls(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLS_CLIENT) + else: + tls_configuration = ldap3.Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLS_CLIENT) + + if self.local_private_key_file: + tls_configuration.private_key_file = self.local_private_key_file + if self.local_server_certificate_file: + tls_configuration.certificate_file = self.local_server_certificate_file + if self.local_ca_certs_file: + tls_configuration.ca_certs_file = self.local_ca_certs_file + + server = ldap3.Server(host=self.ldap_server_url, tls=tls_configuration) + bind_type = AUTO_BIND_TLS_BEFORE_BIND if self.ssl_tls_mode == "StartTLS" else True + + return ldap3.Connection( + server=server, + user=base_dn, + password=password, + auto_bind=bind_type, + read_only=read_only, + raise_exceptions=True, + ) + + except ImportError: + msg = _("Please Install the ldap3 library via pip to use ldap functionality.") + xhiveframework.throw(msg, title=_("LDAP Not Installed")) + except LDAPInvalidCredentialsResult: + xhiveframework.throw(_("Invalid username or password")) + except Exception as ex: + xhiveframework.throw(_(str(ex))) + + @staticmethod + def get_ldap_client_settings() -> dict: + # return the settings to be used on the client side. + result = {"enabled": False} + ldap = xhiveframework.get_cached_doc("LDAP Settings") + if ldap.enabled: + result["enabled"] = True + result["method"] = "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.login" + return result + + @classmethod + def update_user_fields(cls, user: "User", user_data: dict): + updatable_data = {key: value for key, value in user_data.items() if key != "email"} + + for key, value in updatable_data.items(): + setattr(user, key, value) + user.save(ignore_permissions=True) + + def sync_roles(self, user: "User", additional_groups: list | None = None): + current_roles = {d.role for d in user.get("roles")} + if self.default_user_type == "System User": + needed_roles = {self.default_role} + else: + needed_roles = set() + lower_groups = [g.lower() for g in additional_groups or []] + + all_mapped_roles = {r.xhiveerp_role for r in self.ldap_groups} + matched_roles = {r.xhiveerp_role for r in self.ldap_groups if r.ldap_group.lower() in lower_groups} + unmatched_roles = all_mapped_roles.difference(matched_roles) + needed_roles.update(matched_roles) + roles_to_remove = current_roles.intersection(unmatched_roles) + + if not needed_roles.issubset(current_roles): + missing_roles = needed_roles.difference(current_roles) + user.add_roles(*missing_roles) + + user.remove_roles(*roles_to_remove) + + def create_or_update_user(self, user_data: dict, groups: list | None = None): + user: "User" = None + role: str = None + + if xhiveframework.db.exists("User", user_data["email"]): + user = xhiveframework.get_doc("User", user_data["email"]) + LDAPSettings.update_user_fields(user=user, user_data=user_data) + elif not self.do_not_create_new_user: + doc = user_data | { + "doctype": "User", + "send_welcome_email": 0, + "language": "", + "user_type": self.default_user_type, + } + user = xhiveframework.get_doc(doc) + user.insert(ignore_permissions=True) + else: + xhiveframework.throw( + _( + "User with email: {0} does not exist in the system. Please ask 'System Administrator' to create the user for you." + ).format(user_data["email"]) + ) + + if self.default_user_type == "System User": + role = self.default_role + else: + role = xhiveframework.db.get_value("User Type", user.user_type, "role") + + if role: + user.add_roles(role) + + self.sync_roles(user, groups) + + return user + + def get_ldap_attributes(self): + ldap_attributes = [self.ldap_email_field, self.ldap_username_field, self.ldap_first_name_field] + + if self.ldap_group_field: + ldap_attributes.append(self.ldap_group_field) + + if self.ldap_middle_name_field: + ldap_attributes.append(self.ldap_middle_name_field) + + if self.ldap_last_name_field: + ldap_attributes.append(self.ldap_last_name_field) + + if self.ldap_phone_field: + ldap_attributes.append(self.ldap_phone_field) + + if self.ldap_mobile_field: + ldap_attributes.append(self.ldap_mobile_field) + + return ldap_attributes + + def fetch_ldap_groups(self, user: Entry, conn: ldap3.Connection) -> list: + if not isinstance(user, Entry): + raise TypeError("Invalid type, attribute 'user' must be of type 'ldap3.abstract.entry.Entry'") + + if not isinstance(conn, ldap3.Connection): + raise TypeError("Invalid type, attribute 'conn' must be of type 'ldap3.Connection'") + + fetch_ldap_groups = None + ldap_object_class = None + ldap_group_members_attribute = None + + if self.ldap_directory_server.lower() == "active directory": + ldap_object_class = "Group" + ldap_group_members_attribute = "member" + user_search_str = user.entry_dn + + elif self.ldap_directory_server.lower() == "openldap": + ldap_object_class = "posixgroup" + ldap_group_members_attribute = "memberuid" + user_search_str = getattr(user, self.ldap_username_field).value + + elif self.ldap_directory_server.lower() == "custom": + ldap_object_class = self.ldap_group_objectclass + ldap_group_members_attribute = self.ldap_group_member_attribute + ldap_custom_group_search = self.ldap_custom_group_search or "{0}" + user_search_str = ldap_custom_group_search.format(getattr(user, self.ldap_username_field).value) + + else: + # NOTE: depreciate this else path + # this path will be hit for everyone with preconfigured ldap settings. this must be taken into account so as not to break ldap for those users. + + if self.ldap_group_field: + fetch_ldap_groups = getattr(user, self.ldap_group_field).values + + if ldap_object_class is not None: + conn.search( + search_base=self.ldap_search_path_group, + search_filter=f"(&(objectClass={ldap_object_class})({ldap_group_members_attribute}={user_search_str}))", + attributes=["cn"], + ) # Build search query + + if len(conn.entries) >= 1: + fetch_ldap_groups = [group["cn"].value for group in conn.entries] + return fetch_ldap_groups + + def authenticate(self, username: str, password: str): + if not self.enabled: + xhiveframework.throw(_("LDAP is not enabled.")) + + user_filter = self.ldap_search_string.format(username) + ldap_attributes = self.get_ldap_attributes() + conn = self.connect_to_ldap(self.base_dn, self.get_password(raise_exception=False)) + + try: + conn.search( + search_base=self.ldap_search_path_user, + search_filter=f"{user_filter}", + attributes=ldap_attributes, + ) + + if len(conn.entries) == 1 and conn.entries[0]: + user = conn.entries[0] + groups = self.fetch_ldap_groups(user, conn) + + # only try and connect as the user, once we have their fqdn entry. + if user.entry_dn and password and conn.rebind(user=user.entry_dn, password=password): + return self.create_or_update_user(self.convert_ldap_entry_to_dict(user), groups=groups) + + raise LDAPInvalidCredentialsResult # even though nothing foundor failed authentication raise invalid credentials + + except LDAPInvalidFilterError: + xhiveframework.throw(_("Please use a valid LDAP search filter"), title=_("Misconfigured")) + + except LDAPInvalidCredentialsResult: + xhiveframework.throw(_("Invalid username or password")) + + def reset_password(self, user, password, logout_sessions=False): + search_filter = f"({self.ldap_email_field}={user})" + + conn = self.connect_to_ldap(self.base_dn, self.get_password(raise_exception=False), read_only=False) + + if conn.search( + search_base=self.ldap_search_path_user, + search_filter=search_filter, + attributes=self.get_ldap_attributes(), + ): + if conn.entries and conn.entries[0]: + entry_dn = conn.entries[0].entry_dn + hashed_password = hashed(HASHED_SALTED_SHA, safe_encode(password)) + changes = {"userPassword": [(MODIFY_REPLACE, [hashed_password])]} + if conn.modify(entry_dn, changes=changes): + if logout_sessions: + from xhiveframework.sessions import clear_sessions + + clear_sessions(user=user, force=True) + xhiveframework.msgprint(_("Password changed successfully.")) + else: + xhiveframework.throw(_("Failed to change password.")) + else: + xhiveframework.throw(_("No Entry for the User {0} found within LDAP!").format(user)) + else: + xhiveframework.throw(_("No LDAP User found for email: {0}").format(user)) + + def convert_ldap_entry_to_dict(self, user_entry: Entry): + # support multiple email values + email = user_entry[self.ldap_email_field].value + + if isinstance(email, list): + # check if any of the email in the list already exist + for e in email: + if xhiveframework.db.exists("User", e): + email = e + break + else: + # if none of the email exist, use the first email + email = email[0] + + data = { + "username": user_entry[self.ldap_username_field].value, + "email": email, + "first_name": user_entry[self.ldap_first_name_field].value, + } + + # optional fields + if self.ldap_middle_name_field: + data["middle_name"] = user_entry[self.ldap_middle_name_field].value + + if self.ldap_last_name_field: + data["last_name"] = user_entry[self.ldap_last_name_field].value + + if self.ldap_phone_field: + data["phone"] = user_entry[self.ldap_phone_field].value + + if self.ldap_mobile_field: + data["mobile_no"] = user_entry[self.ldap_mobile_field].value + + return data + + +@xhiveframework.whitelist(allow_guest=True) +def login(): + # LDAP LOGIN LOGIC + args = xhiveframework.form_dict + ldap: LDAPSettings = xhiveframework.get_doc("LDAP Settings") + + user = ldap.authenticate(xhiveframework.as_unicode(args.usr), xhiveframework.as_unicode(args.pwd)) + + xhiveframework.local.login_manager.user = user.name + if should_run_2fa(user.name): + authenticate_for_2factor(user.name) + if not confirm_otp_token(xhiveframework.local.login_manager): + return False + + xhiveframework.form_dict.pop("pwd", None) + xhiveframework.local.login_manager.post_login() + + # because of a GET request! + xhiveframework.db.commit() + + +@xhiveframework.whitelist() +def reset_password(user, password, logout): + ldap: LDAPSettings = xhiveframework.get_doc("LDAP Settings") + if not ldap.enabled: + xhiveframework.throw(_("LDAP is not enabled.")) + ldap.reset_password(user, password, logout_sessions=int(logout)) diff --git a/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_activedirectory.json b/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_activedirectory.json new file mode 100644 index 0000000..9777452 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_activedirectory.json @@ -0,0 +1,338 @@ +{ + "entries": [ + { + "attributes": { + "cn": "base_dn_user", + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Enterprise Administrators,ou=Groups,dc=unit,dc=testing" + ], + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": "cn=base_dn_user,dc=unit,dc=testing", + "sn": "user_sn", + "userPassword": [ + "my_password" + ] + }, + "dn": "cn=base_dn_user,dc=unit,dc=testing", + "raw": { + "cn": [ + "base_dn_user" + ], + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Enterprise Administrators,ou=Groups,dc=unit,dc=testing" + ], + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": [ + "cn=base_dn_user,dc=unit,dc=testing" + ], + "sn": [ + "user_sn" + ], + "userPassword": [ + "my_password" + ] + } + }, + { + "attributes": { + "cn": "Posix User1", + "description": [ + "ACCESS:test1,ACCESS:test2" + ], + "givenname": "Posix", + "mail": "posix.user1@unit.testing", + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Domain Administrators,ou=Groups,dc=unit,dc=testing" + ], + "mobile": "0421 123 456", + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": "posix.user", + "sn": "User1", + "telephonenumber": "08 8912 3456", + "userpassword": [ + "posix_user_password" + ] + }, + "dn": "cn=Posix User1,ou=Users,dc=unit,dc=testing", + "raw": { + "cn": [ + "Posix User1" + ], + "description": [ + "ACCESS:test1,ACCESS:test2" + ], + "givenname": [ + "Posix" + ], + "mail": [ + "posix.user1@unit.testing" + ], + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Domain Administrators,ou=Groups,dc=unit,dc=testing" + ], + "mobile": [ + "0421 123 456" + ], + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": [ + "posix.user" + ], + "sn": [ + "User1" + ], + "telephonenumber": [ + "08 8912 3456" + ], + "userpassword": [ + "posix_user_password" + ] + } + }, + { + "attributes": { + "cn": "Posix User2", + "description": [ + "ACCESS:test1,ACCESS:test3" + ], + "givenname": "Posix", + "homedirectory": "/home/users/posix.user2", + "mail": "posix.user2@unit.testing", + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Enterprise Administrators,ou=Groups,dc=unit,dc=testing" + ], + "mobile": "0421 456 789", + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": "posix.user2", + "sn": "User2", + "telephonenumber": "08 8978 1234", + "userpassword": [ + "posix_user2_password" + ] + }, + "dn": "cn=Posix User2,ou=Users,dc=unit,dc=testing", + "raw": { + "cn": [ + "Posix User2" + ], + "description": [ + "ACCESS:test1,ACCESS:test3" + ], + "givenname": [ + "Posix" + ], + "homedirectory": [ + "/home/users/posix.user2" + ], + "mail": [ + "posix.user2@unit.testing" + ], + "memberOf": [ + "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "cn=Enterprise Administrators,ou=Groups,dc=unit,dc=testing" + ], + "mobile": [ + "0421 456 789" + ], + "objectClass": [ + "user", + "top", + "person", + "organizationalPerson" + ], + "samaccountname": [ + "posix.user2" + ], + "sn": [ + "User2" + ], + "telephonenumber": [ + "08 8978 1234" + ], + "userpassword": [ + "posix_user2_password" + ] + } + }, + { + "attributes": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users" + ] + }, + "dn": "ou=Users,dc=unit,dc=testing", + "raw": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users" + ] + } + }, + { + "attributes": { + "Member": [ + "cn=Posix User2,ou=Users,dc=unit,dc=testing" + ], + "cn": "Enterprise Administrators", + "description": [ + "group contains only posix.user2" + ], + "groupType": 2147483652, + "objectClass": [ + "top", + "group" + ] + }, + "dn": "cn=Enterprise Administrators,ou=Groups,dc=unit,dc=testing", + "raw": { + "Member": [ + "cn=Posix User2,ou=Users,dc=unit,dc=testing" + ], + "cn": [ + "Enterprise Administrators" + ], + "description": [ + "group contains only posix.user2" + ], + "groupType": [ + "2147483652" + ], + "objectClass": [ + "top", + "group" + ] + } + }, + { + "attributes": { + "Member": [ + "cn=Posix User1,ou=Users,dc=unit,dc=testing", + "cn=Posix User2,ou=Users,dc=unit,dc=testing" + ], + "cn": "Domain Users", + "description": [ + "group2 Users contains only posix.user and posix.user2" + ], + "groupType": 2147483652, + "objectClass": [ + "top", + "group" + ] + }, + "dn": "cn=Domain Users,ou=Groups,dc=unit,dc=testing", + "raw": { + "Member": [ + "cn=Posix User1,ou=Users,dc=unit,dc=testing", + "cn=Posix User2,ou=Users,dc=unit,dc=testing" + ], + "cn": [ + "Domain Users" + ], + "description": [ + "group2 Users contains only posix.user and posix.user2" + ], + "groupType": [ + "2147483652" + ], + "objectClass": [ + "top", + "group" + ] + } + }, + { + "attributes": { + "Member": [ + "cn=Posix User1,ou=Users,dc=unit,dc=testing", + "cn=base_dn_user,dc=unit,dc=testing" + ], + "cn": "Domain Administrators", + "description": [ + "group1 Administrators contains only posix.user only" + ], + "groupType": 2147483652, + "objectClass": [ + "top", + "group" + ] + }, + "dn": "cn=Domain Administrators,ou=Groups,dc=unit,dc=testing", + "raw": { + "Member": [ + "cn=Posix User1,ou=Users,dc=unit,dc=testing", + "cn=base_dn_user,dc=unit,dc=testing" + ], + "cn": [ + "Domain Administrators" + ], + "description": [ + "group1 Administrators contains only posix.user only" + ], + "groupType": [ + "2147483652" + ], + "objectClass": [ + "top", + "group" + ] + } + }, + { + "attributes": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Groups" + ] + }, + "dn": "ou=Groups,dc=unit,dc=testing", + "raw": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Groups" + ] + } + } + ] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_openldap.json b/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_openldap.json new file mode 100644 index 0000000..86a76c1 --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/test_data_ldif_openldap.json @@ -0,0 +1,400 @@ +{ + "entries": [ + { + "attributes": { + "cn": [ + "base_dn_user" + ], + "objectClass": [ + "simpleSecurityObject", + "organizationalRole", + "top" + ], + "sn": [ + "user_sn" + ], + "userPassword": [ + "my_password" + ] + }, + "dn": "cn=base_dn_user,dc=unit,dc=testing", + "raw": { + "cn": [ + "base_dn_user" + ], + "objectClass": [ + "simpleSecurityObject", + "organizationalRole", + "top" + ], + "sn": [ + "user_sn" + ], + "userPassword": [ + "my_password" + ] + } + }, + { + "attributes": { + "cn": [ + "Posix User2" + ], + "description": [ + "ACCESS:test1,ACCESS:test3" + ], + "gidnumber": 501, + "givenname": [ + "Posix2" + ], + "homedirectory": "/home/users/posix.user2", + "mail": [ + "posix.user2@unit.testing" + ], + "mobile": [ + "0421 456 789" + ], + "objectClass": [ + "posixAccount", + "top", + "inetOrgPerson", + "person", + "organizationalPerson" + ], + "sn": [ + "User2" + ], + "telephonenumber": [ + "08 8978 1234" + ], + "uid": [ + "posix.user2" + ], + "uidnumber": 1000, + "userpassword": [ + "posix_user2_password" + ] + }, + "dn": "cn=Posix User2,ou=users,dc=unit,dc=testing", + "raw": { + "cn": [ + "Posix User2" + ], + "description": [ + "ACCESS:test1,ACCESS:test3" + ], + "gidnumber": [ + "501" + ], + "givenname": [ + "Posix2" + ], + "homedirectory": [ + "/home/users/posix.user2" + ], + "mail": [ + "posix.user2@unit.testing" + ], + "mobile": [ + "0421 456 789" + ], + "objectClass": [ + "posixAccount", + "top", + "inetOrgPerson", + "person", + "organizationalPerson" + ], + "sn": [ + "User2" + ], + "telephonenumber": [ + "08 8978 1234" + ], + "uid": [ + "posix.user2" + ], + "uidnumber": [ + "1000" + ], + "userpassword": [ + "posix_user2_password" + ] + } + }, + { + "attributes": { + "cn": [ + "Posix User1" + ], + "description": [ + "ACCESS:test1,ACCESS:test2" + ], + "gidnumber": 501, + "givenname": [ + "Posix" + ], + "homedirectory": "/home/users/posix.user", + "mail": [ + "posix.user1@unit.testing" + ], + "mobile": [ + "0421 123 456" + ], + "objectClass": [ + "posixAccount", + "top", + "inetOrgPerson", + "person", + "organizationalPerson" + ], + "sn": [ + "User1" + ], + "telephonenumber": [ + "08 8912 3456" + ], + "uid": [ + "posix.user" + ], + "uidnumber": 1000, + "userpassword": [ + "posix_user_password" + ] + }, + "dn": "cn=Posix User1,ou=users,dc=unit,dc=testing", + "raw": { + "cn": [ + "Posix User1" + ], + "description": [ + "ACCESS:test1,ACCESS:test2" + ], + "gidnumber": [ + "501" + ], + "givenname": [ + "Posix" + ], + "homedirectory": [ + "/home/users/posix.user" + ], + "mail": [ + "posix.user1@unit.testing" + ], + "mobile": [ + "0421 123 456" + ], + "objectClass": [ + "posixAccount", + "top", + "inetOrgPerson", + "person", + "organizationalPerson" + ], + "sn": [ + "User1" + ], + "telephonenumber": [ + "08 8912 3456" + ], + "uid": [ + "posix.user" + ], + "uidnumber": [ + "1000" + ], + "userpassword": [ + "posix_user_password" + ] + } + }, + { + "attributes": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users", + "users" + ] + }, + "dn": "ou=users,dc=unit,dc=testing", + "raw": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users", + "users" + ] + } + }, + { + "attributes": { + "dc": "testing", + "o": [ + "Testing" + ], + "objectClass": [ + "top", + "organization", + "dcObject" + ] + }, + "dn": "dc=unit,dc=testing", + "raw": { + "dc": [ + "testing", + "unit" + ], + "o": [ + "Testing" + ], + "objectClass": [ + "top", + "organization", + "dcObject" + ] + } + }, + { + "attributes": { + "cn": [ + "Users" + ], + "description": [ + "group2 Users contains only posix.user and posix.user2" + ], + "gidnumber": 501, + "memberuid": [ + "posix.user2", + "posix.user" + ], + "objectClass": [ + "top", + "posixGroup" + ] + }, + "dn": "cn=Users,ou=groups,dc=unit,dc=testing", + "raw": { + "cn": [ + "Users" + ], + "description": [ + "group2 Users contains only posix.user and posix.user2" + ], + "gidnumber": [ + "501" + ], + "memberuid": [ + "posix.user2", + "posix.user" + ], + "objectClass": [ + "top", + "posixGroup" + ] + } + }, + { + "attributes": { + "cn": [ + "Administrators" + ], + "description": [ + "group1 Administrators contains only posix.user only" + ], + "gidnumber": 500, + "memberuid": [ + "posix.user" + ], + "objectClass": [ + "top", + "posixGroup" + ] + }, + "dn": "cn=Administrators,ou=groups,dc=unit,dc=testing", + "raw": { + "cn": [ + "Administrators" + ], + "description": [ + "group1 Administrators contains only posix.user only" + ], + "gidnumber": [ + "500" + ], + "memberuid": [ + "posix.user" + ], + "objectClass": [ + "top", + "posixGroup" + ] + } + }, + { + "attributes": { + "cn": [ + "Group3" + ], + "description": [ + "group3 Group3 contains only posix.user2 only" + ], + "gidnumber": 502, + "memberuid": [ + "posix.user2" + ], + "objectClass": [ + "top", + "posixGroup" + ] + }, + "dn": "cn=Group3,ou=groups,dc=unit,dc=testing", + "raw": { + "cn": [ + "Group3" + ], + "description": [ + "group3 Group3 contains only posix.user2 only" + ], + "gidnumber": [ + "502" + ], + "memberuid": [ + "posix.user2" + ], + "objectClass": [ + "top", + "posixGroup" + ] + } + }, + { + "attributes": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users", + "groups" + ] + }, + "dn": "ou=groups,dc=unit,dc=testing", + "raw": { + "objectClass": [ + "top", + "organizationalUnit" + ], + "ou": [ + "Users", + "groups" + ] + } + } + ] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/ldap_settings/test_ldap_settings.py b/xhiveframework/integrations/doctype/ldap_settings/test_ldap_settings.py new file mode 100644 index 0000000..d220d2a --- /dev/null +++ b/xhiveframework/integrations/doctype/ldap_settings/test_ldap_settings.py @@ -0,0 +1,656 @@ +# Copyright (c) 2022, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import contextlib +import functools +import os +import ssl +import typing +from unittest import TestCase, mock + +import ldap3 +from ldap3 import MOCK_SYNC, OFFLINE_AD_2012_R2, OFFLINE_SLAPD_2_4, Connection, Server + +import xhiveframework +from xhiveframework.exceptions import MandatoryError, ValidationError +from xhiveframework.integrations.doctype.ldap_settings.ldap_settings import LDAPSettings + + +class LDAP_TestCase: + TEST_LDAP_SERVER = None # must match the 'LDAP Settings' field option + TEST_LDAP_SEARCH_STRING = None + LDAP_USERNAME_FIELD = None + DOCUMENT_GROUP_MAPPINGS: typing.ClassVar[list] = [] + LDAP_SCHEMA = None + LDAP_LDIF_JSON = None + TEST_VALUES_LDAP_COMPLEX_SEARCH_STRING = None + + # for adding type hints during development ^_^ + assertTrue = TestCase.assertTrue + assertEqual = TestCase.assertEqual + assertIn = TestCase.assertIn + + def mock_ldap_connection(f): + @functools.wraps(f) + def wrapped(self, *args, **kwargs): + with mock.patch( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.LDAPSettings.connect_to_ldap", + return_value=self.connection, + ): + self.test_class = LDAPSettings(self.doc) + + # Create a clean doc + localdoc = self.doc.copy() + xhiveframework.get_doc(localdoc).save() + + rv = f(self, *args, **kwargs) + + # Clean-up + self.test_class = None + + return rv + + return wrapped + + def clean_test_users(): + with contextlib.suppress(Exception): + xhiveframework.get_doc("User", "posix.user1@unit.testing").delete() + with contextlib.suppress(Exception): + xhiveframework.get_doc("User", "posix.user2@unit.testing").delete() + with contextlib.suppress(Exception): + xhiveframework.get_doc("User", "website_ldap_user@test.com").delete() + + @classmethod + def setUpClass(cls): + cls.clean_test_users() + # Save user data for restoration in tearDownClass() + cls.user_ldap_settings = xhiveframework.get_doc("LDAP Settings") + + # Create test user1 + cls.user1doc = { + "username": "posix.user", + "email": "posix.user1@unit.testing", + "first_name": "posix", + "doctype": "User", + "send_welcome_email": 0, + "language": "", + "user_type": "System User", + } + + user = xhiveframework.get_doc(cls.user1doc) + user.insert(ignore_permissions=True) + + cls.user2doc = { + "username": "posix.user2", + "email": "posix.user2@unit.testing", + "first_name": "posix", + "doctype": "User", + "send_welcome_email": 0, + "language": "", + "user_type": "System User", + } + user = xhiveframework.get_doc(cls.user2doc) + user.insert(ignore_permissions=True) + + # Setup Mock OpenLDAP Directory + cls.ldap_dc_path = "dc=unit,dc=testing" + cls.ldap_user_path = f"ou=users,{cls.ldap_dc_path}" + cls.ldap_group_path = f"ou=groups,{cls.ldap_dc_path}" + cls.base_dn = f"cn=base_dn_user,{cls.ldap_dc_path}" + cls.base_password = "my_password" + cls.ldap_server = "ldap://my_fake_server:389" + + cls.doc = { + "doctype": "LDAP Settings", + "enabled": True, + "ldap_directory_server": cls.TEST_LDAP_SERVER, + "ldap_server_url": cls.ldap_server, + "base_dn": cls.base_dn, + "password": cls.base_password, + "ldap_search_path_user": cls.ldap_user_path, + "ldap_search_string": cls.TEST_LDAP_SEARCH_STRING, + "ldap_search_path_group": cls.ldap_group_path, + "ldap_user_creation_and_mapping_section": "", + "ldap_email_field": "mail", + "ldap_username_field": cls.LDAP_USERNAME_FIELD, + "ldap_first_name_field": "givenname", + "ldap_middle_name_field": "", + "ldap_last_name_field": "sn", + "ldap_phone_field": "telephonenumber", + "ldap_mobile_field": "mobile", + "ldap_security": "", + "ssl_tls_mode": "", + "require_trusted_certificate": "No", + "local_private_key_file": "", + "local_server_certificate_file": "", + "local_ca_certs_file": "", + "ldap_group_objectclass": "", + "ldap_group_member_attribute": "", + "default_role": "Newsletter Manager", + "ldap_groups": cls.DOCUMENT_GROUP_MAPPINGS, + "ldap_group_field": "", + "default_user_type": "System User", + } + + cls.server = Server(host=cls.ldap_server, port=389, get_info=cls.LDAP_SCHEMA) + cls.connection = Connection( + cls.server, + user=cls.base_dn, + password=cls.base_password, + read_only=True, + client_strategy=MOCK_SYNC, + ) + cls.connection.strategy.entries_from_json( + f"{os.path.abspath(os.path.dirname(__file__))}/{cls.LDAP_LDIF_JSON}" + ) + cls.connection.bind() + + @classmethod + def tearDownClass(cls): + with contextlib.suppress(Exception): + xhiveframework.get_doc("LDAP Settings").delete() + + # return doc back to user data + with contextlib.suppress(Exception): + cls.user_ldap_settings.save() + + # Clean-up test users + cls.clean_test_users() + + # Clear OpenLDAP connection + cls.connection = None + + @mock_ldap_connection + def test_mandatory_fields(self): + mandatory_fields = [ + "ldap_server_url", + "ldap_directory_server", + "base_dn", + "password", + "ldap_search_path_user", + "ldap_search_path_group", + "ldap_search_string", + "ldap_email_field", + "ldap_username_field", + "ldap_first_name_field", + "require_trusted_certificate", + ] # fields that are required to have ldap functioning need to be mandatory + + for mandatory_field in mandatory_fields: + localdoc = self.doc.copy() + localdoc[mandatory_field] = "" + + with contextlib.suppress(MandatoryError, ValidationError): + xhiveframework.get_doc(localdoc).save() + self.fail(f"Document LDAP Settings field [{mandatory_field}] is not mandatory") + + for non_mandatory_field in self.doc: # Ensure remaining fields have not been made mandatory + if non_mandatory_field == "doctype" or non_mandatory_field in mandatory_fields: + continue + + localdoc = self.doc.copy() + localdoc[non_mandatory_field] = "" + + try: + xhiveframework.get_doc(localdoc).save() + except MandatoryError: + self.fail(f"Document LDAP Settings field [{non_mandatory_field}] should not be mandatory") + + @mock_ldap_connection + def test_validation_ldap_search_string(self): + invalid_ldap_search_strings = [ + "", + "uid={0}", + "(uid={0}", + "uid={0})", + "(&(objectclass=posixgroup)(uid={0})", + "&(objectclass=posixgroup)(uid={0}))", + "(uid=no_placeholder)", + ] # ldap search string must be enclosed in '()' for ldap search to work for finding user and have the same number of opening and closing brackets. + + for invalid_search_string in invalid_ldap_search_strings: + localdoc = self.doc.copy() + localdoc["ldap_search_string"] = invalid_search_string + + with contextlib.suppress(ValidationError): + xhiveframework.get_doc(localdoc).save() + self.fail(f"LDAP search string [{invalid_search_string}] should not validate") + + def test_connect_to_ldap(self): + # prevent these parameters for security or lack of the und user from being able to configure + prevent_connection_parameters = { + "mode": { + "IP_V4_ONLY": "Locks the user to IPv4 without xhiveframework providing a way to configure", + "IP_V6_ONLY": "Locks the user to IPv6 without xhiveframework providing a way to configure", + }, + "auto_bind": { + "NONE": "ldap3.Connection must autobind with base_dn", + "NO_TLS": "ldap3.Connection must have TLS", + "TLS_AFTER_BIND": "[Security] ldap3.Connection TLS bind must occur before bind", + }, + } + + # setup a clean doc with ldap disabled so no validation occurs (this is tested seperatly) + local_doc = self.doc.copy() + local_doc["enabled"] = False + self.test_class = LDAPSettings(self.doc) + + with mock.patch("ldap3.Server") as ldap3_server_method: + with mock.patch("ldap3.Connection", return_value=self.connection) as ldap3_connection_method: + with mock.patch("ldap3.Tls") as ldap3_Tls_method: + function_return = self.test_class.connect_to_ldap( + base_dn=self.base_dn, password=self.base_password + ) + args, kwargs = ldap3_connection_method.call_args + + for connection_arg in kwargs: + if ( + connection_arg in prevent_connection_parameters + and kwargs[connection_arg] in prevent_connection_parameters[connection_arg] + ): + self.fail( + f"ldap3.Connection was called with {kwargs[connection_arg]}, failed reason: [{prevent_connection_parameters[connection_arg][kwargs[connection_arg]]}]" + ) + + tls_version = ssl.PROTOCOL_TLS_CLIENT + if local_doc["require_trusted_certificate"] == "Yes": + tls_validate = ssl.CERT_REQUIRED + tls_configuration = ldap3.Tls(validate=tls_validate, version=tls_version) + + self.assertTrue( + kwargs["auto_bind"] == ldap3.AUTO_BIND_TLS_BEFORE_BIND, + "Security: [ldap3.Connection] autobind TLS before bind with value ldap3.AUTO_BIND_TLS_BEFORE_BIND", + ) + + else: + tls_validate = ssl.CERT_NONE + tls_configuration = ldap3.Tls(validate=tls_validate, version=tls_version) + + self.assertTrue(kwargs["auto_bind"], "ldap3.Connection must autobind") + + ldap3_Tls_method.assert_called_with(validate=tls_validate, version=tls_version) + + ldap3_server_method.assert_called_with( + host=self.doc["ldap_server_url"], tls=tls_configuration + ) + + self.assertTrue( + kwargs["password"] == self.base_password, + "ldap3.Connection password does not match provided password", + ) + + self.assertTrue( + kwargs["raise_exceptions"], + "ldap3.Connection must raise exceptions for error handling", + ) + + self.assertTrue( + kwargs["user"] == self.base_dn, "ldap3.Connection user does not match provided user" + ) + + ldap3_connection_method.assert_called_with( + server=ldap3_server_method.return_value, + auto_bind=True, + password=self.base_password, + raise_exceptions=True, + read_only=True, + user=self.base_dn, + ) + + self.assertTrue( + type(function_return) is Connection, + "The return type must be of ldap3.Connection", + ) + + function_return = self.test_class.connect_to_ldap( + base_dn=self.base_dn, password=self.base_password, read_only=False + ) + + args, kwargs = ldap3_connection_method.call_args + + self.assertFalse( + kwargs["read_only"], + "connect_to_ldap() read_only parameter supplied as False but does not match the ldap3.Connection() read_only named parameter", + ) + + @mock_ldap_connection + def test_get_ldap_client_settings(self): + result = self.test_class.get_ldap_client_settings() + + self.assertIsInstance(result, dict) + self.assertTrue(result["enabled"] == self.doc["enabled"]) # settings should match doc + + localdoc = self.doc.copy() + localdoc["enabled"] = False + xhiveframework.get_doc(localdoc).save() + result = self.test_class.get_ldap_client_settings() + + self.assertFalse(result["enabled"]) # must match the edited doc + + @mock_ldap_connection + def test_update_user_fields(self): + test_user_data = { + "username": "posix.user", + "email": "posix.user1@unit.testing", + "first_name": "posix", + "middle_name": "another", + "last_name": "user", + "phone": "08 1234 5678", + "mobile_no": "0421 123 456", + } + test_user = xhiveframework.get_doc("User", test_user_data["email"]) + self.test_class.update_user_fields(test_user, test_user_data) + updated_user = xhiveframework.get_doc("User", test_user_data["email"]) + + self.assertTrue(updated_user.middle_name == test_user_data["middle_name"]) + self.assertTrue(updated_user.last_name == test_user_data["last_name"]) + self.assertTrue(updated_user.phone == test_user_data["phone"]) + self.assertTrue(updated_user.mobile_no == test_user_data["mobile_no"]) + + self.assertEqual(updated_user.user_type, self.test_class.default_user_type) + self.assertIn(self.test_class.default_role, xhiveframework.get_roles(updated_user.name)) + + @mock_ldap_connection + def test_create_website_user(self): + new_test_user_data = { + "username": "website_ldap_user.test", + "email": "website_ldap_user@test.com", + "first_name": "Website User - LDAP Test", + } + self.test_class.default_user_type = "Website User" + self.test_class.create_or_update_user(user_data=new_test_user_data, groups=[]) + new_user = xhiveframework.get_doc("User", new_test_user_data["email"]) + self.assertEqual(new_user.user_type, "Website User") + + @mock_ldap_connection + def test_sync_roles(self): + if self.TEST_LDAP_SERVER.lower() == "openldap": + test_user_data = { + "posix.user1": [ + "Users", + "Administrators", + "default_role", + "xhiveframework_default_all", + "xhiveframework_default_guest", + "xhiveframework_default_desk_user", + ], + "posix.user2": [ + "Users", + "Group3", + "default_role", + "xhiveframework_default_all", + "xhiveframework_default_guest", + "xhiveframework_default_desk_user", + ], + } + + elif self.TEST_LDAP_SERVER.lower() == "active directory": + test_user_data = { + "posix.user1": [ + "Domain Users", + "Domain Administrators", + "default_role", + "xhiveframework_default_all", + "xhiveframework_default_guest", + "xhiveframework_default_desk_user", + ], + "posix.user2": [ + "Domain Users", + "Enterprise Administrators", + "default_role", + "xhiveframework_default_all", + "xhiveframework_default_guest", + "xhiveframework_default_desk_user", + ], + } + + role_to_group_map = { + self.doc["ldap_groups"][0]["xhiveerp_role"]: self.doc["ldap_groups"][0]["ldap_group"], + self.doc["ldap_groups"][1]["xhiveerp_role"]: self.doc["ldap_groups"][1]["ldap_group"], + self.doc["ldap_groups"][2]["xhiveerp_role"]: self.doc["ldap_groups"][2]["ldap_group"], + "Newsletter Manager": "default_role", + "All": "xhiveframework_default_all", + "Guest": "xhiveframework_default_guest", + "Desk User": "xhiveframework_default_desk_user", + } + + # re-create user1 to ensure clean + xhiveframework.get_doc("User", "posix.user1@unit.testing").delete() + user = xhiveframework.get_doc(self.user1doc) + user.insert(ignore_permissions=True) + + for test_user in test_user_data: + test_user_doc = xhiveframework.get_doc("User", f"{test_user}@unit.testing") + test_user_roles = xhiveframework.get_roles(f"{test_user}@unit.testing") + + self.assertTrue( + len(test_user_roles) == 2, "User should only be a part of the All and Guest roles" + ) # check default xhiveframework roles + + self.test_class.sync_roles(test_user_doc, test_user_data[test_user]) # update user roles + + xhiveframework.get_doc("User", f"{test_user}@unit.testing") + updated_user_roles = xhiveframework.get_roles(f"{test_user}@unit.testing") + + self.assertTrue( + len(updated_user_roles) == len(test_user_data[test_user]), + f"syncing of the user roles failed. {len(updated_user_roles)} != {len(test_user_data[test_user])} for user {test_user}", + ) + + for user_role in updated_user_roles: # match each users role mapped to ldap groups + self.assertTrue( + role_to_group_map[user_role] in test_user_data[test_user], + f"during sync_roles(), the user was given role {user_role} which should not have occured", + ) + + @mock_ldap_connection + def test_create_or_update_user(self): + test_user_data = { + "posix.user1": [ + "Users", + "Administrators", + "default_role", + "xhiveframework_default_all", + "xhiveframework_default_guest", + ], + } + test_user = "posix.user1" + + xhiveframework.get_doc("User", f"{test_user}@unit.testing").delete() + + with self.assertRaises( + xhiveframework.exceptions.DoesNotExistError + ): # ensure user deleted so function can be tested + xhiveframework.get_doc("User", f"{test_user}@unit.testing") + + with mock.patch( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.LDAPSettings.update_user_fields" + ) as update_user_fields_method: + with mock.patch( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.LDAPSettings.sync_roles" + ) as sync_roles_method: + # New user + self.test_class.create_or_update_user(self.user1doc, test_user_data[test_user]) + + self.assertTrue(sync_roles_method.called, "User roles need to be updated for a new user") + self.assertFalse( + update_user_fields_method.called, + "User roles are not required to be updated for a new user, this will occur during logon", + ) + + # Existing user + self.test_class.create_or_update_user(self.user1doc, test_user_data[test_user]) + + self.assertTrue( + sync_roles_method.called, "User roles need to be updated for an existing user" + ) + self.assertTrue( + update_user_fields_method.called, "User fields need to be updated for an existing user" + ) + + @mock_ldap_connection + def test_get_ldap_attributes(self): + method_return = self.test_class.get_ldap_attributes() + self.assertTrue(isinstance(method_return, list)) + + @mock_ldap_connection + def test_fetch_ldap_groups(self): + if self.TEST_LDAP_SERVER.lower() == "openldap": + test_users = {"posix.user": ["Users", "Administrators"], "posix.user2": ["Users", "Group3"]} + elif self.TEST_LDAP_SERVER.lower() == "active directory": + test_users = { + "posix.user": ["Domain Users", "Domain Administrators"], + "posix.user2": ["Domain Users", "Enterprise Administrators"], + } + + for test_user in test_users: + self.connection.search( + search_base=self.ldap_user_path, + search_filter=self.TEST_LDAP_SEARCH_STRING.format(test_user), + attributes=self.test_class.get_ldap_attributes(), + ) + + method_return = self.test_class.fetch_ldap_groups(self.connection.entries[0], self.connection) + + self.assertIsInstance(method_return, list) + self.assertTrue(len(method_return) == len(test_users[test_user])) + + for returned_group in method_return: + self.assertTrue(returned_group in test_users[test_user]) + + @mock_ldap_connection + def test_authenticate(self): + with mock.patch( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.LDAPSettings.fetch_ldap_groups" + ) as fetch_ldap_groups_function: + self.assertTrue(self.test_class.authenticate("posix.user", "posix_user_password")) + + self.assertTrue( + fetch_ldap_groups_function.called, + "As part of authentication function fetch_ldap_groups_function needs to be called", + ) + + invalid_users = [ + {"prefix_posix.user": "posix_user_password"}, + {"posix.user_postfix": "posix_user_password"}, + {"posix.user": "posix_user_password_postfix"}, + {"posix.user": "prefix_posix_user_password"}, + {"posix.user": ""}, + {"": "posix_user_password"}, + {"": ""}, + ] # All invalid users should return 'invalid username or password' + + for username, password in enumerate(invalid_users): + with self.assertRaises(xhiveframework.exceptions.ValidationError) as display_massage: + self.test_class.authenticate(username, password) + + self.assertTrue( + str(display_massage.exception).lower() == "invalid username or password", + f"invalid credentials passed authentication [user: {username}, password: {password}]", + ) + + @mock_ldap_connection + def test_complex_ldap_search_filter(self): + ldap_search_filters = self.TEST_VALUES_LDAP_COMPLEX_SEARCH_STRING + + for search_filter in ldap_search_filters: + self.test_class.ldap_search_string = search_filter + + if ( + "ACCESS:test3" in search_filter + ): # posix.user does not have str in ldap.description auth should fail + with self.assertRaises(xhiveframework.exceptions.ValidationError) as display_massage: + self.test_class.authenticate("posix.user", "posix_user_password") + + self.assertTrue(str(display_massage.exception).lower() == "invalid username or password") + + else: + self.assertTrue(self.test_class.authenticate("posix.user", "posix_user_password")) + + def test_reset_password(self): + self.test_class = LDAPSettings(self.doc) + + # Create a clean doc + localdoc = self.doc.copy() + localdoc["enabled"] = False + xhiveframework.get_doc(localdoc).save() + + with mock.patch( + "xhiveframework.integrations.doctype.ldap_settings.ldap_settings.LDAPSettings.connect_to_ldap", + return_value=self.connection, + ) as connect_to_ldap: + with self.assertRaises( + xhiveframework.exceptions.ValidationError + ) as validation: # Fail if username string used + self.test_class.reset_password("posix.user", "posix_user_password") + self.assertTrue(str(validation.exception) == "No LDAP User found for email: posix.user") + + with contextlib.suppress(Exception): + self.test_class.reset_password( + "posix.user1@unit.testing", "posix_user_password" + ) # Change Password + connect_to_ldap.assert_called_with(self.base_dn, self.base_password, read_only=False) + + @mock_ldap_connection + def test_convert_ldap_entry_to_dict(self): + self.connection.search( + search_base=self.ldap_user_path, + search_filter=self.TEST_LDAP_SEARCH_STRING.format("posix.user"), + attributes=self.test_class.get_ldap_attributes(), + ) + test_ldap_entry = self.connection.entries[0] + method_return = self.test_class.convert_ldap_entry_to_dict(test_ldap_entry) + + self.assertTrue(isinstance(method_return, dict)) # must be dict + self.assertTrue(len(method_return) == 6) # there are 6 fields in mock_ldap for use + + +class Test_OpenLDAP(LDAP_TestCase, TestCase): + TEST_LDAP_SERVER = "OpenLDAP" + TEST_LDAP_SEARCH_STRING = "(uid={0})" + DOCUMENT_GROUP_MAPPINGS: typing.ClassVar[list] = [ + { + "doctype": "LDAP Group Mapping", + "ldap_group": "Administrators", + "xhiveerp_role": "System Manager", + }, + {"doctype": "LDAP Group Mapping", "ldap_group": "Users", "xhiveerp_role": "Blogger"}, + {"doctype": "LDAP Group Mapping", "ldap_group": "Group3", "xhiveerp_role": "Accounts User"}, + ] + LDAP_USERNAME_FIELD = "uid" + LDAP_SCHEMA = OFFLINE_SLAPD_2_4 + LDAP_LDIF_JSON = "test_data_ldif_openldap.json" + + TEST_VALUES_LDAP_COMPLEX_SEARCH_STRING: typing.ClassVar[list] = [ + "(uid={0})", + "(&(objectclass=posixaccount)(uid={0}))", + "(&(description=*ACCESS:test1*)(uid={0}))", # OpenLDAP has no member of group, use description to filter posix.user has equivilent of AD 'memberOf' + "(&(objectclass=posixaccount)(description=*ACCESS:test3*)(uid={0}))", # OpenLDAP has no member of group, use description to filter posix.user doesn't have. equivilent of AD 'memberOf' + ] + + +class Test_ActiveDirectory(LDAP_TestCase, TestCase): + TEST_LDAP_SERVER = "Active Directory" + TEST_LDAP_SEARCH_STRING = "(samaccountname={0})" + DOCUMENT_GROUP_MAPPINGS: typing.ClassVar[list] = [ + { + "doctype": "LDAP Group Mapping", + "ldap_group": "Domain Administrators", + "xhiveerp_role": "System Manager", + }, + {"doctype": "LDAP Group Mapping", "ldap_group": "Domain Users", "xhiveerp_role": "Blogger"}, + { + "doctype": "LDAP Group Mapping", + "ldap_group": "Enterprise Administrators", + "xhiveerp_role": "Accounts User", + }, + ] + LDAP_USERNAME_FIELD = "samaccountname" + LDAP_SCHEMA = OFFLINE_AD_2012_R2 + LDAP_LDIF_JSON = "test_data_ldif_activedirectory.json" + + TEST_VALUES_LDAP_COMPLEX_SEARCH_STRING: typing.ClassVar[dict] = [ + "(samaccountname={0})", + "(&(objectclass=user)(samaccountname={0}))", + "(&(description=*ACCESS:test1*)(samaccountname={0}))", # OpenLDAP has no member of group, use description to filter posix.user has equivilent of AD 'memberOf' + "(&(objectclass=user)(description=*ACCESS:test3*)(samaccountname={0}))", # OpenLDAP has no member of group, use description to filter posix.user doesn't have. equivilent of AD 'memberOf' + ] diff --git a/xhiveframework/integrations/doctype/oauth_authorization_code/__init__.py b/xhiveframework/integrations/doctype/oauth_authorization_code/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.js b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.js new file mode 100644 index 0000000..9f1998b --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("OAuth Authorization Code", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.json b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.json new file mode 100644 index 0000000..2cd21bc --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.json @@ -0,0 +1,112 @@ +{ + "actions": [], + "autoname": "field:authorization_code", + "creation": "2016-08-24 14:12:13.647159", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "client", + "user", + "scopes", + "authorization_code", + "expiration_time", + "redirect_uri_bound_to_authorization_code", + "validity", + "nonce", + "code_challenge", + "code_challenge_method" + ], + "fields": [ + { + "fieldname": "client", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Client", + "options": "OAuth Client", + "read_only": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "scopes", + "fieldtype": "Text", + "label": "Scopes", + "read_only": 1 + }, + { + "fieldname": "authorization_code", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Authorization Code", + "read_only": 1, + "unique": 1 + }, + { + "fieldname": "expiration_time", + "fieldtype": "Datetime", + "label": "Expiration time", + "read_only": 1 + }, + { + "fieldname": "redirect_uri_bound_to_authorization_code", + "fieldtype": "Data", + "label": "Redirect URI Bound To Auth Code", + "read_only": 1 + }, + { + "fieldname": "validity", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Validity", + "options": "Valid\nInvalid", + "read_only": 1 + }, + { + "fieldname": "nonce", + "fieldtype": "Data", + "label": "nonce", + "read_only": 1 + }, + { + "fieldname": "code_challenge", + "fieldtype": "Data", + "label": "Code Challenge", + "read_only": 1 + }, + { + "fieldname": "code_challenge_method", + "fieldtype": "Select", + "label": "Code challenge method", + "options": "\ns256\nplain", + "read_only": 1 + } + ], + "links": [], + "modified": "2021-04-26 07:23:02.980612", + "modified_by": "Administrator", + "module": "Integrations", + "name": "OAuth Authorization Code", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.py b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.py new file mode 100644 index 0000000..e50c8b8 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_authorization_code/oauth_authorization_code.py @@ -0,0 +1,28 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class OAuthAuthorizationCode(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + authorization_code: DF.Data | None + client: DF.Link | None + code_challenge: DF.Data | None + code_challenge_method: DF.Literal["", "s256", "plain"] + expiration_time: DF.Datetime | None + nonce: DF.Data | None + redirect_uri_bound_to_authorization_code: DF.Data | None + scopes: DF.Text | None + user: DF.Link | None + validity: DF.Literal["Valid", "Invalid"] + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/oauth_authorization_code/test_oauth_authorization_code.py b/xhiveframework/integrations/doctype/oauth_authorization_code/test_oauth_authorization_code.py new file mode 100644 index 0000000..4999a50 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_authorization_code/test_oauth_authorization_code.py @@ -0,0 +1,10 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('OAuth Authorization Code') + + +class TestOAuthAuthorizationCode(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/oauth_bearer_token/__init__.py b/xhiveframework/integrations/doctype/oauth_bearer_token/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.js b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.js new file mode 100644 index 0000000..82cb952 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("OAuth Bearer Token", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.json b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.json new file mode 100644 index 0000000..2060c48 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.json @@ -0,0 +1,99 @@ +{ + "actions": [], + "autoname": "field:access_token", + "creation": "2016-08-24 14:10:17.471264", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "client", + "user", + "scopes", + "access_token", + "refresh_token", + "expiration_time", + "expires_in", + "status" + ], + "fields": [ + { + "fieldname": "client", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Client", + "options": "OAuth Client", + "read_only": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "scopes", + "fieldtype": "Text", + "label": "Scopes", + "read_only": 1 + }, + { + "fieldname": "access_token", + "fieldtype": "Data", + "label": "Access Token", + "read_only": 1, + "unique": 1 + }, + { + "fieldname": "refresh_token", + "fieldtype": "Data", + "label": "Refresh Token", + "read_only": 1 + }, + { + "fieldname": "expiration_time", + "fieldtype": "Datetime", + "label": "Expiration time", + "read_only": 1 + }, + { + "fieldname": "expires_in", + "fieldtype": "Int", + "label": "Expires In", + "read_only": 1 + }, + { + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Status", + "options": "Active\nRevoked", + "read_only": 1 + } + ], + "links": [], + "modified": "2023-04-07 07:08:00.249740", + "modified_by": "Administrator", + "module": "Integrations", + "name": "OAuth Bearer Token", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.py b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.py new file mode 100644 index 0000000..1796eec --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_bearer_token/oauth_bearer_token.py @@ -0,0 +1,31 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class OAuthBearerToken(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + access_token: DF.Data | None + client: DF.Link | None + expiration_time: DF.Datetime | None + expires_in: DF.Int + refresh_token: DF.Data | None + scopes: DF.Text | None + status: DF.Literal["Active", "Revoked"] + user: DF.Link | None + + # end: auto-generated types + def validate(self): + if not self.expiration_time: + self.expiration_time = xhiveframework.utils.datetime.datetime.strptime( + self.creation, "%Y-%m-%d %H:%M:%S.%f" + ) + xhiveframework.utils.datetime.timedelta(seconds=self.expires_in) diff --git a/xhiveframework/integrations/doctype/oauth_bearer_token/test_oauth_bearer_token.py b/xhiveframework/integrations/doctype/oauth_bearer_token/test_oauth_bearer_token.py new file mode 100644 index 0000000..2f5da74 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_bearer_token/test_oauth_bearer_token.py @@ -0,0 +1,10 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('OAuth Bearer Token') + + +class TestOAuthBearerToken(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/oauth_client/__init__.py b/xhiveframework/integrations/doctype/oauth_client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/oauth_client/oauth_client.js b/xhiveframework/integrations/doctype/oauth_client/oauth_client.js new file mode 100644 index 0000000..2318796 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_client/oauth_client.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("OAuth Client", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/oauth_client/oauth_client.json b/xhiveframework/integrations/doctype/oauth_client/oauth_client.json new file mode 100644 index 0000000..597cd06 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_client/oauth_client.json @@ -0,0 +1,144 @@ +{ + "actions": [], + "creation": "2016-08-24 14:07:21.955052", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "client_id", + "app_name", + "user", + "cb_1", + "client_secret", + "skip_authorization", + "sb_1", + "scopes", + "cb_3", + "redirect_uris", + "default_redirect_uri", + "sb_advanced", + "grant_type", + "cb_2", + "response_type" + ], + "fields": [ + { + "fieldname": "client_id", + "fieldtype": "Data", + "label": "App Client ID", + "read_only": 1 + }, + { + "fieldname": "app_name", + "fieldtype": "Data", + "label": "App Name", + "reqd": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "hidden": 1, + "label": "User", + "options": "User" + }, + { + "fieldname": "cb_1", + "fieldtype": "Column Break" + }, + { + "fieldname": "client_secret", + "fieldtype": "Data", + "label": "App Client Secret", + "read_only": 1 + }, + { + "default": "0", + "description": "If checked, users will not see the Confirm Access dialog.", + "fieldname": "skip_authorization", + "fieldtype": "Check", + "label": "Skip Authorization" + }, + { + "fieldname": "sb_1", + "fieldtype": "Section Break" + }, + { + "default": "all openid", + "description": "A list of resources which the Client App will have access to after the user allows it.
      e.g. project", + "fieldname": "scopes", + "fieldtype": "Text", + "label": "Scopes", + "reqd": 1 + }, + { + "fieldname": "cb_3", + "fieldtype": "Column Break" + }, + { + "description": "URIs for receiving authorization code once the user allows access, as well as failure responses. Typically a REST endpoint exposed by the Client App.\n
      e.g. http://hostname/api/method/xhiveframework.integrations.oauth2_logins.login_via_facebook", + "fieldname": "redirect_uris", + "fieldtype": "Text", + "label": "Redirect URIs" + }, + { + "fieldname": "default_redirect_uri", + "fieldtype": "Data", + "label": "Default Redirect URI", + "reqd": 1 + }, + { + "collapsible": 1, + "collapsible_depends_on": "1", + "fieldname": "sb_advanced", + "fieldtype": "Section Break", + "label": "Advanced Settings" + }, + { + "fieldname": "grant_type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Grant Type", + "options": "Authorization Code\nImplicit" + }, + { + "fieldname": "cb_2", + "fieldtype": "Column Break" + }, + { + "default": "Code", + "fieldname": "response_type", + "fieldtype": "Select", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Response Type", + "options": "Code\nToken" + } + ], + "links": [], + "modified": "2023-07-17 07:06:35.765981", + "modified_by": "Administrator", + "module": "Integrations", + "name": "OAuth Client", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "app_name", + "track_changes": 1 +} diff --git a/xhiveframework/integrations/doctype/oauth_client/oauth_client.py b/xhiveframework/integrations/doctype/oauth_client/oauth_client.py new file mode 100644 index 0000000..7ceb15e --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_client/oauth_client.py @@ -0,0 +1,47 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class OAuthClient(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + app_name: DF.Data + client_id: DF.Data | None + client_secret: DF.Data | None + default_redirect_uri: DF.Data + grant_type: DF.Literal["Authorization Code", "Implicit"] + redirect_uris: DF.Text | None + response_type: DF.Literal["Code", "Token"] + scopes: DF.Text + skip_authorization: DF.Check + user: DF.Link | None + + # end: auto-generated types + def validate(self): + self.client_id = self.name + if not self.client_secret: + self.client_secret = xhiveframework.generate_hash(length=10) + self.validate_grant_and_response() + + def validate_grant_and_response(self): + if ( + self.grant_type == "Authorization Code" + and self.response_type != "Code" + or self.grant_type == "Implicit" + and self.response_type != "Token" + ): + xhiveframework.throw( + _( + "Combination of Grant Type ({0}) and Response Type ({1}) not allowed" + ).format(self.grant_type, self.response_type) + ) diff --git a/xhiveframework/integrations/doctype/oauth_client/test_oauth_client.py b/xhiveframework/integrations/doctype/oauth_client/test_oauth_client.py new file mode 100644 index 0000000..6952844 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_client/test_oauth_client.py @@ -0,0 +1,10 @@ +# Copyright (c) 2015, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +# test_records = xhiveframework.get_test_records('OAuth Client') + + +class TestOAuthClient(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/oauth_provider_settings/__init__.py b/xhiveframework/integrations/doctype/oauth_provider_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.js b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.js new file mode 100644 index 0000000..9093bbb --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.js @@ -0,0 +1,6 @@ +// Copyright (c) 2016, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("OAuth Provider Settings", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.json b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.json new file mode 100644 index 0000000..219a87f --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.json @@ -0,0 +1,43 @@ +{ + "actions": [], + "creation": "2016-09-03 11:42:42.575525", + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "skip_authorization" + ], + "fields": [ + { + "fieldname": "skip_authorization", + "fieldtype": "Select", + "label": "Skip Authorization", + "options": "Force\nAuto" + } + ], + "issingle": 1, + "links": [], + "modified": "2022-08-03 12:20:52.328415", + "modified_by": "Administrator", + "module": "Integrations", + "name": "OAuth Provider Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.py b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.py new file mode 100644 index 0000000..4488334 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_provider_settings/oauth_provider_settings.py @@ -0,0 +1,27 @@ +# Copyright (c) 2015, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class OAuthProviderSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + skip_authorization: DF.Literal["Force", "Auto"] + # end: auto-generated types + pass + + +def get_oauth_settings(): + """Returns oauth settings""" + return xhiveframework._dict( + {"skip_authorization": xhiveframework.db.get_single_value("OAuth Provider Settings", "skip_authorization")} + ) diff --git a/xhiveframework/integrations/doctype/oauth_scope/__init__.py b/xhiveframework/integrations/doctype/oauth_scope/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.json b/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.json new file mode 100644 index 0000000..3a6e528 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.json @@ -0,0 +1,30 @@ +{ + "actions": [], + "creation": "2020-07-15 22:08:14.616585", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "scope" + ], + "fields": [ + { + "fieldname": "scope", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Scope" + } + ], + "istable": 1, + "links": [], + "modified": "2020-07-15 22:15:18.930632", + "modified_by": "Administrator", + "module": "Integrations", + "name": "OAuth Scope", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.py b/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.py new file mode 100644 index 0000000..fcb2e12 --- /dev/null +++ b/xhiveframework/integrations/doctype/oauth_scope/oauth_scope.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class OAuthScope(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + scope: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/push_notification_settings/__init__.py b/xhiveframework/integrations/doctype/push_notification_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.js b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.js new file mode 100644 index 0000000..a1ad92a --- /dev/null +++ b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.js @@ -0,0 +1,8 @@ +// Copyright (c) 2024, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +// xhiveframework.ui.form.on("Push Notification Settings", { +// refresh(frm) { + +// }, +// }); diff --git a/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.json b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.json new file mode 100644 index 0000000..da1d637 --- /dev/null +++ b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.json @@ -0,0 +1,69 @@ +{ + "actions": [], + "allow_rename": 1, + "beta": 1, + "creation": "2024-01-04 11:36:08.013039", + "description": "Enabling this will register your site on a central relay server to send push notifications for all installed apps through Firebase Cloud Messaging. This server only stores user tokens and error logs, and no messages are saved.", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "section_break_qgjr", + "enable_push_notification_relay", + "authentication_credential_section", + "api_key", + "api_secret" + ], + "fields": [ + { + "default": "0", + "fieldname": "enable_push_notification_relay", + "fieldtype": "Check", + "label": "Enable Push Notification Relay" + }, + { + "description": "API Key and Secret to interact with the relay server. These will be auto-generated when the first push notification is sent from any of the apps installed on this site.", + "fieldname": "authentication_credential_section", + "fieldtype": "Section Break", + "label": "Authentication" + }, + { + "fieldname": "api_key", + "fieldtype": "Data", + "label": "API Key" + }, + { + "fieldname": "api_secret", + "fieldtype": "Password", + "label": "API Secret" + }, + { + "description": "Enabling this will register your site on a central relay server to send push notifications for all installed apps through Firebase Cloud Messaging. This server only stores user tokens and error logs, and no messages are saved. ", + "fieldname": "section_break_qgjr", + "fieldtype": "Section Break", + "label": "Relay Settings" + } + ], + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2024-02-28 11:03:30.518196", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Push Notification Settings", + "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": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.py b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.py new file mode 100644 index 0000000..9d247f7 --- /dev/null +++ b/xhiveframework/integrations/doctype/push_notification_settings/push_notification_settings.py @@ -0,0 +1,31 @@ +# Copyright (c) 2024, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class PushNotificationSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + api_key: DF.Data | None + api_secret: DF.Password | None + enable_push_notification_relay: DF.Check + # end: auto-generated types + + def validate(self): + self.validate_relay_server_setup() + + def validate_relay_server_setup(self): + if self.enable_push_notification_relay and not xhiveframework.conf.get("push_relay_server_url"): + xhiveframework.throw( + _("The Push Relay Server URL key (`push_relay_server_url`) is missing in your site config"), + title=_("Relay Server URL missing"), + ) diff --git a/xhiveframework/integrations/doctype/push_notification_settings/test_push_notification_settings.py b/xhiveframework/integrations/doctype/push_notification_settings/test_push_notification_settings.py new file mode 100644 index 0000000..3a268bb --- /dev/null +++ b/xhiveframework/integrations/doctype/push_notification_settings/test_push_notification_settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2024, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPushNotificationSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/query_parameters/__init__.py b/xhiveframework/integrations/doctype/query_parameters/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/query_parameters/query_parameters.json b/xhiveframework/integrations/doctype/query_parameters/query_parameters.json new file mode 100644 index 0000000..de31c28 --- /dev/null +++ b/xhiveframework/integrations/doctype/query_parameters/query_parameters.json @@ -0,0 +1,37 @@ +{ + "actions": [], + "creation": "2020-11-16 14:54:37.226914", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "key", + "value" + ], + "fields": [ + { + "fieldname": "key", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Key", + "reqd": 1 + }, + { + "fieldname": "value", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Value", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2020-11-16 15:18:35.887149", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Query Parameters", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC" +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/query_parameters/query_parameters.py b/xhiveframework/integrations/doctype/query_parameters/query_parameters.py new file mode 100644 index 0000000..8c640b7 --- /dev/null +++ b/xhiveframework/integrations/doctype/query_parameters/query_parameters.py @@ -0,0 +1,23 @@ +# Copyright (c) 2020, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class QueryParameters(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + key: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + value: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/s3_backup_settings/__init__.py b/xhiveframework/integrations/doctype/s3_backup_settings/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.js b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.js new file mode 100755 index 0000000..af00001 --- /dev/null +++ b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.js @@ -0,0 +1,26 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("S3 Backup Settings", { + refresh: function (frm) { + frm.clear_custom_buttons(); + frm.events.take_backup(frm); + }, + + take_backup: function (frm) { + if (frm.doc.access_key_id && frm.doc.secret_access_key) { + frm.add_custom_button(__("Take Backup Now"), function () { + frm.dashboard.set_headline_alert("S3 Backup Started!"); + xhiveframework.call({ + method: "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_s3", + callback: function (r) { + if (!r.exc) { + xhiveframework.msgprint(__("S3 Backup complete!")); + frm.dashboard.clear_headline(); + } + }, + }); + }).addClass("btn-primary"); + } + }, +}); diff --git a/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.json b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.json new file mode 100755 index 0000000..e225686 --- /dev/null +++ b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.json @@ -0,0 +1,155 @@ +{ + "actions": [], + "creation": "2017-09-04 20:57:20.129205", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enabled", + "api_access_section", + "access_key_id", + "column_break_4", + "secret_access_key", + "notification_section", + "notify_email", + "column_break_8", + "send_email_for_successful_backup", + "s3_bucket_details_section", + "bucket", + "endpoint_url", + "column_break_13", + "backup_details_section", + "frequency", + "backup_files" + ], + "fields": [ + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enable Automatic Backup" + }, + { + "fieldname": "notify_email", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Send Notifications To", + "mandatory_depends_on": "enabled", + "reqd": 1 + }, + { + "default": "1", + "description": "By default, emails are only sent for failed backups.", + "fieldname": "send_email_for_successful_backup", + "fieldtype": "Check", + "label": "Send Email for Successful Backup" + }, + { + "fieldname": "frequency", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Backup Frequency", + "mandatory_depends_on": "enabled", + "options": "Daily\nWeekly\nMonthly\nNone", + "reqd": 1 + }, + { + "fieldname": "access_key_id", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Access Key ID", + "mandatory_depends_on": "enabled", + "reqd": 1 + }, + { + "fieldname": "secret_access_key", + "fieldtype": "Password", + "in_list_view": 1, + "label": "Access Key Secret", + "mandatory_depends_on": "enabled", + "reqd": 1 + }, + { + "default": "https://s3.amazonaws.com", + "description": "Only change this if you want to use other S3 compatible object storage backends.", + "fieldname": "endpoint_url", + "fieldtype": "Data", + "label": "Endpoint URL" + }, + { + "fieldname": "bucket", + "fieldtype": "Data", + "label": "Bucket Name", + "mandatory_depends_on": "enabled", + "reqd": 1 + }, + { + "depends_on": "enabled", + "fieldname": "api_access_section", + "fieldtype": "Section Break", + "label": "API Access" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "depends_on": "enabled", + "fieldname": "notification_section", + "fieldtype": "Section Break", + "label": "Notification" + }, + { + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, + { + "depends_on": "enabled", + "fieldname": "s3_bucket_details_section", + "fieldtype": "Section Break", + "label": "S3 Bucket Details" + }, + { + "fieldname": "column_break_13", + "fieldtype": "Column Break" + }, + { + "depends_on": "enabled", + "fieldname": "backup_details_section", + "fieldtype": "Section Break", + "label": "Backup Details" + }, + { + "default": "1", + "description": "Backup public and private files along with the database.", + "fieldname": "backup_files", + "fieldtype": "Check", + "label": "Backup Files" + } + ], + "hide_toolbar": 1, + "issingle": 1, + "links": [], + "modified": "2023-01-11 15:38:20.333833", + "modified_by": "Administrator", + "module": "Integrations", + "name": "S3 Backup Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.py b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.py new file mode 100755 index 0000000..1430932 --- /dev/null +++ b/xhiveframework/integrations/doctype/s3_backup_settings/s3_backup_settings.py @@ -0,0 +1,196 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +import os +import os.path + +import boto3 +from botocore.exceptions import ClientError +from rq.timeouts import JobTimeoutException + +import xhiveframework +from xhiveframework import _ +from xhiveframework.integrations.offsite_backup_utils import ( + generate_files_backup, + get_latest_backup_file, + send_email, + validate_file_size, +) +from xhiveframework.model.document import Document +from xhiveframework.utils import cint +from xhiveframework.utils.background_jobs import enqueue + + +class S3BackupSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + access_key_id: DF.Data + backup_files: DF.Check + bucket: DF.Data + enabled: DF.Check + endpoint_url: DF.Data | None + frequency: DF.Literal["Daily", "Weekly", "Monthly", "None"] + notify_email: DF.Data + secret_access_key: DF.Password + send_email_for_successful_backup: DF.Check + + # end: auto-generated types + def validate(self): + if not self.enabled: + return + + if not self.endpoint_url: + self.endpoint_url = "https://s3.amazonaws.com" + + conn = boto3.client( + "s3", + aws_access_key_id=self.access_key_id, + aws_secret_access_key=self.get_password("secret_access_key"), + endpoint_url=self.endpoint_url, + ) + + try: + # Head_bucket returns a 200 OK if the bucket exists and have access to it. + # Requires ListBucket permission + conn.head_bucket(Bucket=self.bucket) + except ClientError as e: + error_code = e.response["Error"]["Code"] + bucket_name = xhiveframework.bold(self.bucket) + if error_code == "403": + msg = _("Do not have permission to access bucket {0}.").format(bucket_name) + elif error_code == "404": + msg = _("Bucket {0} not found.").format(bucket_name) + else: + msg = e.args[0] + + xhiveframework.throw(msg) + + +@xhiveframework.whitelist() +def take_backup(): + """Enqueue longjob for taking backup to s3""" + enqueue( + "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_s3", + queue="long", + timeout=1500, + ) + xhiveframework.msgprint(_("Queued for backup. It may take a few minutes to an hour.")) + + +def take_backups_daily(): + take_backups_if("Daily") + + +def take_backups_weekly(): + take_backups_if("Weekly") + + +def take_backups_monthly(): + take_backups_if("Monthly") + + +def take_backups_if(freq): + if cint(xhiveframework.db.get_single_value("S3 Backup Settings", "enabled")): + if xhiveframework.db.get_single_value("S3 Backup Settings", "frequency") == freq: + take_backups_s3() + + +@xhiveframework.whitelist() +def take_backups_s3(retry_count=0): + try: + validate_file_size() + backup_to_s3() + send_email(True, "Amazon S3", "S3 Backup Settings", "notify_email") + except JobTimeoutException: + if retry_count < 2: + args = {"retry_count": retry_count + 1} + enqueue( + "xhiveframework.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_s3", + queue="long", + timeout=1500, + **args, + ) + else: + notify() + except Exception: + notify() + + +def notify(): + error_message = xhiveframework.get_traceback() + send_email(False, "Amazon S3", "S3 Backup Settings", "notify_email", error_message) + + +def backup_to_s3(): + from xhiveframework.utils import get_backups_path + from xhiveframework.utils.backups import new_backup + + doc = xhiveframework.get_single("S3 Backup Settings") + bucket = doc.bucket + backup_files = cint(doc.backup_files) + + conn = boto3.client( + "s3", + aws_access_key_id=doc.access_key_id, + aws_secret_access_key=doc.get_password("secret_access_key"), + endpoint_url=doc.endpoint_url or "https://s3.amazonaws.com", + ) + + if xhiveframework.flags.create_new_backup: + backup = new_backup( + ignore_files=False, + backup_path_db=None, + backup_path_files=None, + backup_path_private_files=None, + force=True, + ) + db_filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) + site_config = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_conf)) + if backup_files: + files_filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_files)) + private_files = os.path.join( + get_backups_path(), os.path.basename(backup.backup_path_private_files) + ) + else: + if backup_files: + db_filename, site_config, files_filename, private_files = get_latest_backup_file( + with_files=backup_files + ) + + if not files_filename or not private_files: + generate_files_backup() + db_filename, site_config, files_filename, private_files = get_latest_backup_file( + with_files=backup_files + ) + + else: + db_filename, site_config = get_latest_backup_file() + + folder = os.path.basename(db_filename)[:15] + "/" + # for adding datetime to folder name + + upload_file_to_s3(db_filename, folder, conn, bucket) + upload_file_to_s3(site_config, folder, conn, bucket) + + if backup_files: + if private_files: + upload_file_to_s3(private_files, folder, conn, bucket) + + if files_filename: + upload_file_to_s3(files_filename, folder, conn, bucket) + + +def upload_file_to_s3(filename, folder, conn, bucket): + destpath = os.path.join(folder, os.path.basename(filename)) + try: + print("Uploading file:", filename) + conn.upload_file(filename, bucket, destpath) # Requires PutObject permission + + except Exception as e: + xhiveframework.log_error() + print("Error uploading: %s" % (e)) diff --git a/xhiveframework/integrations/doctype/s3_backup_settings/test_s3_backup_settings.py b/xhiveframework/integrations/doctype/s3_backup_settings/test_s3_backup_settings.py new file mode 100755 index 0000000..ac58c6f --- /dev/null +++ b/xhiveframework/integrations/doctype/s3_backup_settings/test_s3_backup_settings.py @@ -0,0 +1,7 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestS3BackupSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/slack_webhook_url/__init__.py b/xhiveframework/integrations/doctype/slack_webhook_url/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.js b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.js new file mode 100644 index 0000000..655a064 --- /dev/null +++ b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.js @@ -0,0 +1,4 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Slack Webhook URL", {}); diff --git a/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.json b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.json new file mode 100644 index 0000000..56a76b9 --- /dev/null +++ b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.json @@ -0,0 +1,61 @@ +{ + "actions": [], + "autoname": "field:webhook_name", + "creation": "2018-05-22 13:20:51.450815", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "webhook_name", + "webhook_url", + "show_document_link" + ], + "fields": [ + { + "fieldname": "webhook_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Name", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "webhook_url", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Webhook URL", + "reqd": 1 + }, + { + "allow_in_quick_entry": 1, + "default": "1", + "fieldname": "show_document_link", + "fieldtype": "Check", + "label": "Show link to document" + } + ], + "links": [], + "modified": "2021-05-12 18:24:37.810235", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Slack Webhook URL", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.py b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.py new file mode 100644 index 0000000..f44f3bc --- /dev/null +++ b/xhiveframework/integrations/doctype/slack_webhook_url/slack_webhook_url.py @@ -0,0 +1,67 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import requests + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import get_url_to_form + +error_messages = { + 400: "400: Invalid Payload or User not found", + 403: "403: Action Prohibited", + 404: "404: Channel not found", + 410: "410: The Channel is Archived", + 500: "500: Rollup Error, Slack seems to be down", +} + + +class SlackWebhookURL(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + show_document_link: DF.Check + webhook_name: DF.Data + webhook_url: DF.Data + # end: auto-generated types + pass + + +def send_slack_message(webhook_url, message, reference_doctype, reference_name): + data = {"text": message, "attachments": []} + + slack_url, show_link = xhiveframework.db.get_value( + "Slack Webhook URL", webhook_url, ["webhook_url", "show_document_link"] + ) + + if show_link: + doc_url = get_url_to_form(reference_doctype, reference_name) + link_to_doc = { + "fallback": _("See the document at {0}").format(doc_url), + "actions": [ + { + "type": "button", + "text": _("Go to the document"), + "url": doc_url, + "style": "primary", + } + ], + } + data["attachments"].append(link_to_doc) + + r = requests.post(slack_url, data=json.dumps(data)) + + if not r.ok: + message = error_messages.get(r.status_code, r.status_code) + xhiveframework.log_error(message, _("Slack Webhook Error")) + return "error" + + return "success" diff --git a/xhiveframework/integrations/doctype/slack_webhook_url/test_slack_webhook_url.py b/xhiveframework/integrations/doctype/slack_webhook_url/test_slack_webhook_url.py new file mode 100644 index 0000000..7f8ae1a --- /dev/null +++ b/xhiveframework/integrations/doctype/slack_webhook_url/test_slack_webhook_url.py @@ -0,0 +1,7 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestSlackWebhookURL(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/social_login_key/__init__.py b/xhiveframework/integrations/doctype/social_login_key/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/social_login_key/social_login_key.js b/xhiveframework/integrations/doctype/social_login_key/social_login_key.js new file mode 100644 index 0000000..4031e6d --- /dev/null +++ b/xhiveframework/integrations/doctype/social_login_key/social_login_key.js @@ -0,0 +1,84 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt +const fields = [ + "provider_name", + "base_url", + "custom_base_url", + "icon", + "authorize_url", + "access_token_url", + "redirect_url", + "api_endpoint", + "api_endpoint_args", + "auth_url_data", +]; + +xhiveframework.ui.form.on("Social Login Key", { + refresh(frm) { + frm.trigger("setup_fields"); + }, + + custom_base_url(frm) { + frm.trigger("setup_fields"); + }, + + social_login_provider(frm) { + if (frm.doc.social_login_provider != "Custom") { + xhiveframework + .call({ + doc: frm.doc, + method: "get_social_login_provider", + args: { + provider: frm.doc.social_login_provider, + }, + }) + .done((r) => { + const provider = r.message; + for (var field of fields) { + frm.set_value(field, provider[field]); + frm.set_df_property(field, "read_only", 1); + if (frm.doc.custom_base_url) { + frm.toggle_enable("base_url", 1); + } + } + }); + } else { + frm.trigger("clear_fields"); + frm.trigger("setup_fields"); + } + }, + + setup_fields(frm) { + // set custom_base_url to read only for "Custom" provider + if (frm.doc.social_login_provider == "Custom") { + frm.set_value("custom_base_url", 1); + frm.set_df_property("custom_base_url", "read_only", 1); + } + + // set fields to read only for providers from template + for (var f of fields) { + if (frm.doc.social_login_provider != "Custom") { + frm.set_df_property(f, "read_only", 1); + } + } + + // enable base_url for providers with custom_base_url + if (frm.doc.custom_base_url) { + frm.set_df_property("base_url", "read_only", 0); + frm.fields_dict["sb_identity_details"].collapse(false); + } + + // hide social_login_provider and provider_name for non local + if (!frm.doc.__islocal && (frm.doc.social_login_provider || frm.doc.provider_name)) { + frm.set_df_property("social_login_provider", "hidden", 1); + frm.set_df_property("provider_name", "hidden", 1); + } + }, + + clear_fields(frm) { + for (var field of fields) { + frm.set_value(field, ""); + frm.set_df_property(field, "read_only", 0); + } + }, +}); diff --git a/xhiveframework/integrations/doctype/social_login_key/social_login_key.json b/xhiveframework/integrations/doctype/social_login_key/social_login_key.json new file mode 100644 index 0000000..5b0c501 --- /dev/null +++ b/xhiveframework/integrations/doctype/social_login_key/social_login_key.json @@ -0,0 +1,203 @@ +{ + "actions": [], + "allow_import": 1, + "creation": "2017-11-18 15:36:09.676722", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "enable_social_login", + "client_credentials", + "social_login_provider", + "client_id", + "column_break_0", + "provider_name", + "client_secret", + "sb_identity_details", + "icon", + "column_break_1", + "base_url", + "configuration_section", + "sign_ups", + "client_urls", + "authorize_url", + "access_token_url", + "column_break_3", + "redirect_url", + "api_endpoint", + "custom_base_url", + "client_information", + "api_endpoint_args", + "auth_url_data", + "user_id_property" + ], + "fields": [ + { + "default": "0", + "fieldname": "enable_social_login", + "fieldtype": "Check", + "label": "Enable Social Login" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:doc.enable_social_login", + "fieldname": "client_credentials", + "fieldtype": "Section Break", + "label": "Client Credentials" + }, + { + "default": "Custom", + "depends_on": "eval:doc.custom!=1", + "fieldname": "social_login_provider", + "fieldtype": "Select", + "label": "Social Login Provider", + "options": "Custom\nFacebook\nXhiveFramework\nGitHub\nGoogle\nOffice 365\nSalesforce\nfairlogin", + "set_only_once": 1 + }, + { + "fieldname": "client_id", + "fieldtype": "Data", + "label": "Client ID" + }, + { + "fieldname": "column_break_0", + "fieldtype": "Column Break" + }, + { + "fieldname": "provider_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Provider Name", + "reqd": 1, + "set_only_once": 1 + }, + { + "fieldname": "client_secret", + "fieldtype": "Password", + "label": "Client Secret" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:doc.custom_base_url", + "fieldname": "sb_identity_details", + "fieldtype": "Section Break", + "label": "Identity Details" + }, + { + "depends_on": "eval:doc.social_login_provider==\"Custom\"", + "fieldname": "icon", + "fieldtype": "Data", + "label": "Icon" + }, + { + "fieldname": "column_break_1", + "fieldtype": "Column Break" + }, + { + "fieldname": "base_url", + "fieldtype": "Data", + "label": "Base URL" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:doc.social_login_provider===\"Custom\"", + "fieldname": "client_urls", + "fieldtype": "Section Break", + "label": "Client URLs" + }, + { + "fieldname": "authorize_url", + "fieldtype": "Data", + "label": "Authorize URL" + }, + { + "fieldname": "access_token_url", + "fieldtype": "Data", + "label": "Access Token URL" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "redirect_url", + "fieldtype": "Data", + "label": "Redirect URL" + }, + { + "fieldname": "api_endpoint", + "fieldtype": "Data", + "label": "API Endpoint" + }, + { + "default": "0", + "fieldname": "custom_base_url", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom Base URL" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval:doc.social_login_provider===\"Custom\"", + "fieldname": "client_information", + "fieldtype": "Section Break", + "label": "Client Information" + }, + { + "fieldname": "api_endpoint_args", + "fieldtype": "Code", + "label": "API Endpoint Args" + }, + { + "fieldname": "auth_url_data", + "fieldtype": "Code", + "label": "Auth URL Data" + }, + { + "depends_on": "eval:doc.social_login_provider===\"Custom\"", + "fieldname": "user_id_property", + "fieldtype": "Data", + "label": "User ID Property" + }, + { + "collapsible": 1, + "fieldname": "configuration_section", + "fieldtype": "Section Break", + "label": "Configuration" + }, + { + "description": "Controls whether new users can sign up using this Social Login Key. If unset, Website Settings is respected. ", + "fieldname": "sign_ups", + "fieldtype": "Select", + "label": "Sign ups", + "options": "\nAllow\nDeny" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-10-14 12:22:23.601130", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Social Login Key", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "title_field": "provider_name", + "track_changes": 1 +} diff --git a/xhiveframework/integrations/doctype/social_login_key/social_login_key.py b/xhiveframework/integrations/doctype/social_login_key/social_login_key.py new file mode 100644 index 0000000..7a5e22a --- /dev/null +++ b/xhiveframework/integrations/doctype/social_login_key/social_login_key.py @@ -0,0 +1,226 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class BaseUrlNotSetError(xhiveframework.ValidationError): + pass + + +class AuthorizeUrlNotSetError(xhiveframework.ValidationError): + pass + + +class AccessTokenUrlNotSetError(xhiveframework.ValidationError): + pass + + +class RedirectUrlNotSetError(xhiveframework.ValidationError): + pass + + +class ClientIDNotSetError(xhiveframework.ValidationError): + pass + + +class ClientSecretNotSetError(xhiveframework.ValidationError): + pass + + +class SocialLoginKey(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + access_token_url: DF.Data | None + api_endpoint: DF.Data | None + api_endpoint_args: DF.Code | None + auth_url_data: DF.Code | None + authorize_url: DF.Data | None + base_url: DF.Data | None + client_id: DF.Data | None + client_secret: DF.Password | None + custom_base_url: DF.Check + enable_social_login: DF.Check + icon: DF.Data | None + provider_name: DF.Data + redirect_url: DF.Data | None + sign_ups: DF.Literal["", "Allow", "Deny"] + social_login_provider: DF.Literal[ + "Custom", "Facebook", "XhiveFramework", "GitHub", "Google", "Office 365", "Salesforce", "fairlogin" + ] + user_id_property: DF.Data | None + + # end: auto-generated types + def autoname(self): + self.name = xhiveframework.scrub(self.provider_name) + + def validate(self): + self.set_icon() + if self.custom_base_url and not self.base_url: + xhiveframework.throw(_("Please enter Base URL"), exc=BaseUrlNotSetError) + if not self.authorize_url: + xhiveframework.throw(_("Please enter Authorize URL"), exc=AuthorizeUrlNotSetError) + if not self.access_token_url: + xhiveframework.throw(_("Please enter Access Token URL"), exc=AccessTokenUrlNotSetError) + if not self.redirect_url: + xhiveframework.throw(_("Please enter Redirect URL"), exc=RedirectUrlNotSetError) + if self.enable_social_login and not self.client_id: + xhiveframework.throw(_("Please enter Client ID before social login is enabled"), exc=ClientIDNotSetError) + if self.enable_social_login and not self.client_secret: + xhiveframework.throw( + _("Please enter Client Secret before social login is enabled"), exc=ClientSecretNotSetError + ) + + def set_icon(self): + icon_map = { + "Google": "google.svg", + "XhiveFramework": "xhiveframework.svg", + "Facebook": "facebook.svg", + "Office 365": "office_365.svg", + "GitHub": "github.svg", + "Salesforce": "salesforce.svg", + "fairlogin": "fair.svg", + } + + if self.provider_name in icon_map: + icon_file = icon_map[self.provider_name] + self.icon = f"/assets/xhiveframework/icons/social/{icon_file}" + + @xhiveframework.whitelist() + def get_social_login_provider(self, provider, initialize=False): + providers = {} + + providers["Office 365"] = { + "provider_name": "Office 365", + "enable_social_login": 1, + "base_url": "https://login.microsoftonline.com", + "custom_base_url": 0, + "icon": "fa fa-windows", + "authorize_url": "https://login.microsoftonline.com/common/oauth2/authorize", + "access_token_url": "https://login.microsoftonline.com/common/oauth2/token", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_office365", + "api_endpoint": None, + "api_endpoint_args": None, + "auth_url_data": json.dumps({"response_type": "code", "scope": "openid"}), + } + + providers["GitHub"] = { + "provider_name": "GitHub", + "enable_social_login": 1, + "base_url": "https://api.github.com/", + "custom_base_url": 0, + "icon": "fa fa-github", + "authorize_url": "https://github.com/login/oauth/authorize", + "access_token_url": "https://github.com/login/oauth/access_token", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_github", + "api_endpoint": "user", + "api_endpoint_args": None, + "auth_url_data": json.dumps({"scope": "user:email"}), + } + + providers["Google"] = { + "provider_name": "Google", + "enable_social_login": 1, + "base_url": "https://www.googleapis.com", + "custom_base_url": 0, + "icon": "fa fa-google", + "authorize_url": "https://accounts.google.com/o/oauth2/auth", + "access_token_url": "https://accounts.google.com/o/oauth2/token", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_google", + "api_endpoint": "oauth2/v2/userinfo", + "api_endpoint_args": None, + "auth_url_data": json.dumps( + { + "scope": "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email", + "response_type": "code", + } + ), + } + + providers["Facebook"] = { + "provider_name": "Facebook", + "enable_social_login": 1, + "base_url": "https://graph.facebook.com", + "custom_base_url": 0, + "icon": "fa fa-facebook", + "authorize_url": "https://www.facebook.com/dialog/oauth", + "access_token_url": "https://graph.facebook.com/oauth/access_token", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_facebook", + "api_endpoint": "/v2.5/me", + "api_endpoint_args": json.dumps( + {"fields": "first_name,last_name,email,gender,location,verified,picture"} + ), + "auth_url_data": json.dumps( + {"display": "page", "response_type": "code", "scope": "email,public_profile"} + ), + } + + providers["XhiveFramework"] = { + "provider_name": "XhiveFramework", + "enable_social_login": 1, + "custom_base_url": 1, + "icon": "/assets/xhiveframework/images/xhiveframework-favicon.svg", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_xhiveframework", + "api_endpoint": "/api/method/xhiveframework.integrations.oauth2.openid_profile", + "api_endpoint_args": None, + "authorize_url": "/api/method/xhiveframework.integrations.oauth2.authorize", + "access_token_url": "/api/method/xhiveframework.integrations.oauth2.get_token", + "auth_url_data": json.dumps({"response_type": "code", "scope": "openid"}), + } + + providers["Salesforce"] = { + "provider_name": "Salesforce", + "enable_social_login": 1, + "base_url": "https://login.salesforce.com", + "custom_base_url": 0, + "icon": "fa fa-cloud", # https://github.com/FortAwesome/Font-Awesome/issues/1744 + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_salesforce", + "api_endpoint": "https://login.salesforce.com/services/oauth2/userinfo", + "api_endpoint_args": None, + "authorize_url": "https://login.salesforce.com/services/oauth2/authorize", + "access_token_url": "https://login.salesforce.com/services/oauth2/token", + "auth_url_data": json.dumps({"response_type": "code", "scope": "openid"}), + } + + providers["fairlogin"] = { + "provider_name": "fairlogin", + "enable_social_login": 1, + "base_url": "https://id.fairkom.net/auth/realms/fairlogin/", + "custom_base_url": 0, + "icon": "fa fa-key", + "redirect_url": "/api/method/xhiveframework.integrations.oauth2_logins.login_via_fairlogin", + "api_endpoint": "https://id.fairkom.net/auth/realms/fairlogin/protocol/openid-connect/userinfo", + "api_endpoint_args": None, + "authorize_url": "https://id.fairkom.net/auth/realms/fairlogin/protocol/openid-connect/auth", + "access_token_url": "https://id.fairkom.net/auth/realms/fairlogin/protocol/openid-connect/token", + "auth_url_data": json.dumps({"response_type": "code", "scope": "openid"}), + } + + # Initialize the doc and return, used in patch + # Or can be used for creating key from controller + if initialize and provider: + for k, v in providers[provider].items(): + setattr(self, k, v) + return + + return providers.get(provider) if provider else providers + + +def provider_allows_signup(provider: str) -> bool: + from xhiveframework.website.utils import is_signup_disabled + + sign_up_config = xhiveframework.db.get_value("Social Login Key", provider, "sign_ups") + + if not sign_up_config: # fallback to global settings + return not is_signup_disabled() + return sign_up_config == "Allow" diff --git a/xhiveframework/integrations/doctype/social_login_key/test_social_login_key.py b/xhiveframework/integrations/doctype/social_login_key/test_social_login_key.py new file mode 100644 index 0000000..4648780 --- /dev/null +++ b/xhiveframework/integrations/doctype/social_login_key/test_social_login_key.py @@ -0,0 +1,172 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from unittest.mock import MagicMock, patch + +from rauth import OAuth2Service + +import xhiveframework +from xhiveframework.auth import CookieManager, LoginManager +from xhiveframework.integrations.doctype.social_login_key.social_login_key import BaseUrlNotSetError +from xhiveframework.tests.utils import XhiveFrameworkTestCase, change_settings +from xhiveframework.utils import set_request +from xhiveframework.utils.oauth import login_via_oauth2 + +TEST_GITHUB_USER = "githublogin@example.com" + + +class TestSocialLoginKey(XhiveFrameworkTestCase): + def setUp(self) -> None: + xhiveframework.set_user("Administrator") + xhiveframework.delete_doc("User", TEST_GITHUB_USER, force=True) + super().setUp() + xhiveframework.set_user("Guest") + + def test_adding_xhiveframework_social_login_provider(self): + xhiveframework.set_user("Administrator") + provider_name = "XhiveFramework" + social_login_key = make_social_login_key(social_login_provider=provider_name) + social_login_key.get_social_login_provider(provider_name, initialize=True) + self.assertRaises(BaseUrlNotSetError, social_login_key.insert) + + def test_github_login_with_private_email(self): + github_social_login_setup() + + mock_session = MagicMock() + mock_session.get.side_effect = github_response_for_private_email + + with patch.object(OAuth2Service, "get_auth_session", return_value=mock_session): + login_via_oauth2("github", "iwriu", {"token": "ewrwerwer"}) # Dummy code and state token + + def test_github_login_with_public_email(self): + github_social_login_setup() + + mock_session = MagicMock() + mock_session.get.side_effect = github_response_for_public_email + + with patch.object(OAuth2Service, "get_auth_session", return_value=mock_session): + login_via_oauth2("github", "iwriu", {"token": "ewrwerwer"}) # Dummy code and state token + + def test_normal_signup_and_github_login(self): + github_social_login_setup() + + if not xhiveframework.db.exists("User", TEST_GITHUB_USER): + user = xhiveframework.new_doc("User", email=TEST_GITHUB_USER, first_name="GitHub Login") + user.insert(ignore_permissions=True) + + mock_session = MagicMock() + mock_session.get.side_effect = github_response_for_login + + with patch.object(OAuth2Service, "get_auth_session", return_value=mock_session): + login_via_oauth2("github", "iwriu", {"token": "ewrwerwer"}) + self.assertEqual(xhiveframework.session.user, TEST_GITHUB_USER) + + def test_force_disabled_signups(self): + key = github_social_login_setup() + key.sign_ups = "Deny" + key.save(ignore_permissions=True) + + mock_session = MagicMock() + mock_session.get.side_effect = github_response_for_login + + with patch.object(OAuth2Service, "get_auth_session", return_value=mock_session): + login_via_oauth2("github", "iwriu", {"token": "ewrwerwer"}) + self.assertEqual(xhiveframework.session.user, "Guest") + + @change_settings("Website Settings", disable_signup=1) + def test_force_enabled_signups(self): + """Social login key can override website settings for disabled signups.""" + key = github_social_login_setup() + key.sign_ups = "Allow" + key.save(ignore_permissions=True) + + mock_session = MagicMock() + mock_session.get.side_effect = github_response_for_login + + with patch.object(OAuth2Service, "get_auth_session", return_value=mock_session): + login_via_oauth2("github", "iwriu", {"token": "ewrwerwer"}) + + self.assertEqual(xhiveframework.session.user, TEST_GITHUB_USER) + + +def make_social_login_key(**kwargs): + kwargs["doctype"] = "Social Login Key" + if "provider_name" not in kwargs: + kwargs["provider_name"] = "Test OAuth2 Provider" + return xhiveframework.get_doc(kwargs) + + +def create_or_update_social_login_key(): + # used in other tests (connected app, oauth20) + try: + social_login_key = xhiveframework.get_doc("Social Login Key", "xhiveframework") + except xhiveframework.DoesNotExistError: + social_login_key = xhiveframework.new_doc("Social Login Key") + social_login_key.get_social_login_provider("XhiveFramework", initialize=True) + social_login_key.base_url = xhiveframework.utils.get_url() + social_login_key.enable_social_login = 0 + social_login_key.save() + xhiveframework.db.commit() + + return social_login_key + + +def create_github_social_login_key(): + if xhiveframework.db.exists("Social Login Key", "github"): + return xhiveframework.get_doc("Social Login Key", "github") + else: + provider_name = "GitHub" + social_login_key = make_social_login_key(social_login_provider=provider_name) + social_login_key.get_social_login_provider(provider_name, initialize=True) + + social_login_key.client_id = "h6htd6q" + social_login_key.client_secret = "keoererk988ekkhf8w9e8ewrjhhkjer9889" + social_login_key.insert(ignore_permissions=True) + return social_login_key + + +def github_response_for_private_email(url, *args, **kwargs): + if url == "user": + return_value = { + "login": "dummy_username", + "id": "223342", + "email": None, + "first_name": "Github Private", + } + else: + return_value = [{"email": "github@example.com", "primary": True, "verified": True}] + + return MagicMock(status_code=200, json=MagicMock(return_value=return_value)) + + +def github_response_for_public_email(url, *args, **kwargs): + if url == "user": + return_value = { + "login": "dummy_username", + "id": "223343", + "email": "github_public@example.com", + "first_name": "Github Public", + } + + return MagicMock(status_code=200, json=MagicMock(return_value=return_value)) + + +def github_response_for_login(url, *args, **kwargs): + if url == "user": + return_value = { + "login": "dummy_username", + "id": "223346", + "email": None, + "first_name": "Github Login", + } + else: + return_value = [{"email": TEST_GITHUB_USER, "primary": True, "verified": True}] + + return MagicMock(status_code=200, json=MagicMock(return_value=return_value)) + + +def github_social_login_setup(): + set_request(path="/random") + xhiveframework.local.cookie_manager = CookieManager() + xhiveframework.local.login_manager = LoginManager() + + return create_github_social_login_key() diff --git a/xhiveframework/integrations/doctype/social_login_keys/__init__.py b/xhiveframework/integrations/doctype/social_login_keys/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/social_login_keys/social_login_keys.py b/xhiveframework/integrations/doctype/social_login_keys/social_login_keys.py new file mode 100644 index 0000000..14ff019 --- /dev/null +++ b/xhiveframework/integrations/doctype/social_login_keys/social_login_keys.py @@ -0,0 +1,6 @@ +# see license +from xhiveframework.model.document import Document + + +class SocialLoginKeys(Document): + pass diff --git a/xhiveframework/integrations/doctype/token_cache/__init__.py b/xhiveframework/integrations/doctype/token_cache/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/token_cache/test_records.json b/xhiveframework/integrations/doctype/token_cache/test_records.json new file mode 100644 index 0000000..0584022 --- /dev/null +++ b/xhiveframework/integrations/doctype/token_cache/test_records.json @@ -0,0 +1,18 @@ +[ + { + "doctype": "Token Cache", + "user": "test@example.com", + "access_token": "test-access-token", + "refresh_token": "test-refresh-token", + "token_type": "Bearer", + "expires_in": 1000, + "scopes": [ + { + "scope": "all" + }, + { + "scope": "openid" + } + ] + } +] \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/token_cache/test_token_cache.py b/xhiveframework/integrations/doctype/token_cache/test_token_cache.py new file mode 100644 index 0000000..7c5313e --- /dev/null +++ b/xhiveframework/integrations/doctype/token_cache/test_token_cache.py @@ -0,0 +1,36 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +test_dependencies = ["User", "Connected App", "Token Cache"] + + +class TestTokenCache(XhiveFrameworkTestCase): + def setUp(self): + self.token_cache = xhiveframework.get_last_doc("Token Cache") + self.token_cache.update({"connected_app": xhiveframework.get_last_doc("Connected App").name}) + self.token_cache.save(ignore_permissions=True) + + def test_get_auth_header(self): + self.token_cache.get_auth_header() + + def test_update_data(self): + self.token_cache.update_data( + { + "access_token": "new-access-token", + "refresh_token": "new-refresh-token", + "token_type": "bearer", + "expires_in": 2000, + "scope": "new scope", + } + ) + + def test_get_expires_in(self): + self.token_cache.get_expires_in() + + def test_is_expired(self): + self.token_cache.is_expired() + + def get_json(self): + self.token_cache.get_json() diff --git a/xhiveframework/integrations/doctype/token_cache/token_cache.js b/xhiveframework/integrations/doctype/token_cache/token_cache.js new file mode 100644 index 0000000..d3c198b --- /dev/null +++ b/xhiveframework/integrations/doctype/token_cache/token_cache.js @@ -0,0 +1,7 @@ +// Copyright (c) 2019, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Token Cache", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/integrations/doctype/token_cache/token_cache.json b/xhiveframework/integrations/doctype/token_cache/token_cache.json new file mode 100644 index 0000000..0e6601f --- /dev/null +++ b/xhiveframework/integrations/doctype/token_cache/token_cache.json @@ -0,0 +1,111 @@ +{ + "actions": [], + "autoname": "format:{connected_app}-{user}", + "beta": 1, + "creation": "2019-01-24 16:56:55.631096", + "doctype": "DocType", + "document_type": "System", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "user", + "connected_app", + "provider_name", + "access_token", + "refresh_token", + "expires_in", + "state", + "scopes", + "success_uri", + "token_type" + ], + "fields": [ + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "connected_app", + "fieldtype": "Link", + "label": "Connected App", + "options": "Connected App", + "read_only": 1 + }, + { + "fieldname": "access_token", + "fieldtype": "Password", + "label": "Access Token", + "read_only": 1 + }, + { + "fieldname": "refresh_token", + "fieldtype": "Password", + "label": "Refresh Token", + "read_only": 1 + }, + { + "fieldname": "expires_in", + "fieldtype": "Int", + "label": "Expires In", + "read_only": 1 + }, + { + "fieldname": "state", + "fieldtype": "Data", + "label": "State", + "read_only": 1 + }, + { + "fieldname": "scopes", + "fieldtype": "Table", + "label": "Scopes", + "options": "OAuth Scope", + "read_only": 1 + }, + { + "fieldname": "success_uri", + "fieldtype": "Data", + "label": "Success URI", + "read_only": 1 + }, + { + "fieldname": "token_type", + "fieldtype": "Data", + "label": "Token Type", + "read_only": 1 + }, + { + "fetch_from": "connected_app.provider_name", + "fieldname": "provider_name", + "fieldtype": "Data", + "label": "Provider Name", + "read_only": 1 + } + ], + "links": [], + "modified": "2023-01-01 21:01:24.405729", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Token Cache", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "delete": 1, + "read": 1, + "role": "System Manager" + }, + { + "delete": 1, + "if_owner": 1, + "read": 1, + "role": "All" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/token_cache/token_cache.py b/xhiveframework/integrations/doctype/token_cache/token_cache.py new file mode 100644 index 0000000..e005831 --- /dev/null +++ b/xhiveframework/integrations/doctype/token_cache/token_cache.py @@ -0,0 +1,90 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +from datetime import datetime, timedelta + +import pytz + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import cint, cstr, get_system_timezone + + +class TokenCache(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.integrations.doctype.oauth_scope.oauth_scope import OAuthScope + from xhiveframework.types import DF + + access_token: DF.Password | None + connected_app: DF.Link | None + expires_in: DF.Int + provider_name: DF.Data | None + refresh_token: DF.Password | None + scopes: DF.Table[OAuthScope] + state: DF.Data | None + success_uri: DF.Data | None + token_type: DF.Data | None + user: DF.Link | None + + # end: auto-generated types + def get_auth_header(self): + if self.access_token: + return {"Authorization": "Bearer " + self.get_password("access_token")} + raise xhiveframework.exceptions.DoesNotExistError + + def update_data(self, data): + """ + Store data returned by authorization flow. + + Params: + data - Dict with access_token, refresh_token, expires_in and scope. + """ + token_type = cstr(data.get("token_type", "")).lower() + if token_type not in ["bearer", "mac"]: + xhiveframework.throw(_("Received an invalid token type.")) + # 'Bearer' or 'MAC' + token_type = token_type.title() if token_type == "bearer" else token_type.upper() + + self.token_type = token_type + self.access_token = cstr(data.get("access_token", "")) + self.refresh_token = cstr(data.get("refresh_token", "")) + self.expires_in = cint(data.get("expires_in", 0)) + + new_scopes = data.get("scope") + if new_scopes: + if isinstance(new_scopes, str): + new_scopes = new_scopes.split(" ") + if isinstance(new_scopes, list): + self.scopes = None + for scope in new_scopes: + self.append("scopes", {"scope": scope}) + + self.state = None + self.save(ignore_permissions=True) + xhiveframework.db.commit() + return self + + def get_expires_in(self): + system_timezone = pytz.timezone(get_system_timezone()) + modified = xhiveframework.utils.get_datetime(self.modified) + modified = system_timezone.localize(modified) + expiry_utc = modified.astimezone(pytz.utc) + timedelta(seconds=self.expires_in) + now_utc = datetime.utcnow().replace(tzinfo=pytz.utc) + return cint((expiry_utc - now_utc).total_seconds()) + + def is_expired(self): + return self.get_expires_in() < 0 + + def get_json(self): + return { + "access_token": self.get_password("access_token", False), + "refresh_token": self.get_password("refresh_token", False), + "expires_in": self.get_expires_in(), + "token_type": self.token_type, + } diff --git a/xhiveframework/integrations/doctype/webhook/__init__.py b/xhiveframework/integrations/doctype/webhook/__init__.py new file mode 100644 index 0000000..f586b76 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook/__init__.py @@ -0,0 +1,108 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def get_all_webhooks(): + # query webhooks + webhooks_list = xhiveframework.get_all( + "Webhook", + fields=["name", "condition", "webhook_docevent", "webhook_doctype", "background_jobs_queue"], + filters={"enabled": True}, + ) + + # make webhooks map + webhooks = {} + for w in webhooks_list: + webhooks.setdefault(w.webhook_doctype, []).append(w) + + return webhooks + + +def run_webhooks(doc, method): + """Run webhooks for this method""" + + xhiveframework_flags = xhiveframework.local.flags + + if xhiveframework_flags.in_import or xhiveframework_flags.in_patch or xhiveframework_flags.in_install or xhiveframework_flags.in_migrate: + return + + # load all webhooks from cache / DB + webhooks = xhiveframework.cache.get_value("webhooks", get_all_webhooks) + + # get webhooks for this doctype + webhooks_for_doc = webhooks.get(doc.doctype, None) + + if not webhooks_for_doc: + # no webhooks, quit + return + + event_list = ["on_update", "after_insert", "on_submit", "on_cancel", "on_trash"] + + if not doc.flags.in_insert: + # value change is not applicable in insert + event_list.append("on_change") + event_list.append("before_update_after_submit") + + from xhiveframework.integrations.doctype.webhook.webhook import get_context + + for webhook in webhooks_for_doc: + trigger_webhook = False + event = method if method in event_list else None + if not webhook.condition: + trigger_webhook = True + elif xhiveframework.safe_eval(webhook.condition, eval_locals=get_context(doc)): + trigger_webhook = True + + if trigger_webhook and event and webhook.webhook_docevent == event: + _add_webhook_to_queue(webhook, doc) + + +def _add_webhook_to_queue(webhook, doc): + # Maintain a queue and flush on commit + if not getattr(xhiveframework.local, "_webhook_queue", None): + xhiveframework.local._webhook_queue = [] + xhiveframework.db.after_commit.add(flush_webhook_execution_queue) + + xhiveframework.local._webhook_queue.append(xhiveframework._dict(doc=doc, webhook=webhook)) + + +def flush_webhook_execution_queue(): + """Enqueue all pending webhook executions. + + Each webhook can trigger multiple times on same document or even different instance of same + document. We assume that last enqueued version of document is the final document for this DB + transaction. + """ + if not getattr(xhiveframework.local, "_webhook_queue", None): + return + + uniq_hooks = set() + unique_last_instances = [] + + # reverse + xhiveframework.local._webhook_queue.reverse() + + # deduplicate on (doc.name, webhook.name) + # 'doc' holds the last instance values + for execution in xhiveframework.local._webhook_queue: + key = (execution.webhook.get("name"), execution.doc.get("name")) + if key not in uniq_hooks: + uniq_hooks.add(key) + unique_last_instances.append(execution) + + # Clear original queue so next enqueue computation happens correctly. + del xhiveframework.local._webhook_queue + + # reverse again, to get back the original order on which to execute webhooks + unique_last_instances.reverse() + + for instance in unique_last_instances: + xhiveframework.enqueue( + "xhiveframework.integrations.doctype.webhook.webhook.enqueue_webhook", + doc=instance.doc, + webhook=instance.webhook, + now=xhiveframework.flags.in_test, + queue=instance.webhook.background_jobs_queue or "default", + ) diff --git a/xhiveframework/integrations/doctype/webhook/test_webhook.py b/xhiveframework/integrations/doctype/webhook/test_webhook.py new file mode 100644 index 0000000..a7f23b7 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook/test_webhook.py @@ -0,0 +1,311 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import json +from contextlib import contextmanager + +import responses +from responses.matchers import json_params_matcher + +import xhiveframework +from xhiveframework.integrations.doctype.webhook import flush_webhook_execution_queue +from xhiveframework.integrations.doctype.webhook.webhook import ( + enqueue_webhook, + get_webhook_data, + get_webhook_headers, +) +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +@contextmanager +def get_test_webhook(config): + wh = xhiveframework.get_doc(config) + if not wh.name: + wh.name = xhiveframework.generate_hash() + wh.insert() + wh.reload() + try: + yield wh + finally: + wh.delete() + + +class TestWebhook(XhiveFrameworkTestCase): + @classmethod + def setUpClass(cls): + # delete any existing webhooks + xhiveframework.db.delete("Webhook") + # Delete existing logs if any + xhiveframework.db.delete("Webhook Request Log") + super().setUpClass() + # create test webhooks + cls.create_sample_webhooks() + + @classmethod + def create_sample_webhooks(cls): + samples_webhooks_data = [ + { + "name": xhiveframework.generate_hash(), + "webhook_doctype": "User", + "webhook_docevent": "after_insert", + "request_url": "https://httpbin.org/post", + "condition": "doc.email", + "enabled": True, + }, + { + "name": xhiveframework.generate_hash(), + "webhook_doctype": "User", + "webhook_docevent": "after_insert", + "request_url": "https://httpbin.org/post", + "condition": "doc.first_name", + "enabled": False, + }, + ] + + cls.sample_webhooks = [] + for wh_fields in samples_webhooks_data: + wh = xhiveframework.new_doc("Webhook") + wh.update(wh_fields) + wh.insert() + cls.sample_webhooks.append(wh) + + @classmethod + def tearDownClass(cls): + # delete any existing webhooks + xhiveframework.db.delete("Webhook") + + def setUp(self): + # retrieve or create a User webhook for `after_insert` + webhook_fields = { + "webhook_doctype": "User", + "webhook_docevent": "after_insert", + "request_url": "https://httpbin.org/post", + } + + if xhiveframework.db.exists("Webhook", webhook_fields): + self.webhook = xhiveframework.get_doc("Webhook", webhook_fields) + else: + self.webhook = xhiveframework.new_doc("Webhook") + self.webhook.update(webhook_fields) + + # create a User document + self.user = xhiveframework.new_doc("User") + self.user.first_name = xhiveframework.mock("name") + self.user.email = xhiveframework.mock("email") + self.user.save() + + # Create another test user specific to this test + self.test_user = xhiveframework.new_doc("User") + self.test_user.email = "user1@integration.webhooks.test.com" + self.test_user.first_name = "user1" + self.test_user.send_welcome_email = False + + self.responses = responses.RequestsMock() + self.responses.start() + + def tearDown(self) -> None: + self.user.delete() + self.test_user.delete() + + self.responses.stop() + self.responses.reset() + super().tearDown() + + def test_webhook_trigger_with_enabled_webhooks(self): + """Test webhook trigger for enabled webhooks""" + + xhiveframework.cache.delete_value("webhooks") + + # Insert the user to db + self.test_user.insert() + + webhooks = xhiveframework.cache.get_value("webhooks") + self.assertTrue("User" in webhooks) + self.assertEqual(len(webhooks.get("User")), 1) + + # only 1 hook (enabled) must be queued + self.assertEqual(len(xhiveframework.local._webhook_queue), 1) + execution = xhiveframework.local._webhook_queue[0] + self.assertEqual(execution.webhook.name, self.sample_webhooks[0].name) + self.assertEqual(execution.doc.name, self.test_user.name) + + def test_validate_doc_events(self): + "Test creating a submit-related webhook for a non-submittable DocType" + + self.webhook.webhook_docevent = "on_submit" + self.assertRaises(xhiveframework.ValidationError, self.webhook.save) + + def test_validate_request_url(self): + "Test validation for the webhook request URL" + + self.webhook.request_url = "httpbin.org?post" + self.assertRaises(xhiveframework.ValidationError, self.webhook.save) + + def test_validate_headers(self): + "Test validation for request headers" + + # test incomplete headers + self.webhook.set("webhook_headers", [{"key": "Content-Type"}]) + self.webhook.save() + headers = get_webhook_headers(doc=None, webhook=self.webhook) + self.assertEqual(headers, {}) + + # test complete headers + self.webhook.set("webhook_headers", [{"key": "Content-Type", "value": "application/json"}]) + self.webhook.save() + headers = get_webhook_headers(doc=None, webhook=self.webhook) + self.assertEqual(headers, {"Content-Type": "application/json"}) + + def test_validate_request_body_form(self): + "Test validation of Form URL-Encoded request body" + + self.webhook.request_structure = "Form URL-Encoded" + self.webhook.set("webhook_data", [{"fieldname": "name", "key": "name"}]) + self.webhook.webhook_json = """{ + "name": "{{ doc.name }}" + }""" + self.webhook.save() + self.assertEqual(self.webhook.webhook_json, None) + + data = get_webhook_data(doc=self.user, webhook=self.webhook) + self.assertEqual(data, {"name": self.user.name}) + + def test_validate_request_body_json(self): + "Test validation of JSON request body" + + self.webhook.request_structure = "JSON" + self.webhook.set("webhook_data", [{"fieldname": "name", "key": "name"}]) + self.webhook.webhook_json = """{ + "name": "{{ doc.name }}" + }""" + self.webhook.save() + self.assertEqual(self.webhook.webhook_data, []) + + data = get_webhook_data(doc=self.user, webhook=self.webhook) + self.assertEqual(data, {"name": self.user.name}) + + def test_webhook_req_log_creation(self): + self.responses.add( + responses.POST, + "https://httpbin.org/post", + status=200, + json={}, + ) + + if not xhiveframework.db.get_value("User", "user2@integration.webhooks.test.com"): + user = xhiveframework.get_doc( + {"doctype": "User", "email": "user2@integration.webhooks.test.com", "first_name": "user2"} + ).insert() + else: + user = xhiveframework.get_doc("User", "user2@integration.webhooks.test.com") + + webhook = xhiveframework.get_doc("Webhook", {"webhook_doctype": "User"}) + enqueue_webhook(user, webhook) + + self.assertTrue(xhiveframework.get_all("Webhook Request Log", pluck="name")) + + def test_webhook_with_array_body(self): + """Check if array request body are supported.""" + wh_config = { + "doctype": "Webhook", + "webhook_doctype": "Note", + "webhook_docevent": "on_change", + "enabled": 1, + "request_url": "https://httpbin.org/post", + "request_method": "POST", + "request_structure": "JSON", + "webhook_json": '[\r\n{% for n in range(3) %}\r\n {\r\n "title": "{{ doc.title }}" }\r\n {%- if not loop.last -%}\r\n , \r\n {%endif%}\r\n{%endfor%}\r\n]', + "meets_condition": "Yes", + "webhook_headers": [ + { + "key": "Content-Type", + "value": "application/json", + } + ], + } + + doc = xhiveframework.new_doc("Note") + doc.title = "Test Webhook Note" + final_title = xhiveframework.generate_hash() + + expected_req = [{"title": final_title} for _ in range(3)] + self.responses.add( + responses.POST, + "https://httpbin.org/post", + status=200, + json=expected_req, + match=[json_params_matcher(expected_req)], + ) + + with get_test_webhook(wh_config): + # It should only execute once in a transaction + doc.insert() + doc.reload() + doc.save() + doc = xhiveframework.get_doc(doc.doctype, doc.name) + doc.title = final_title + doc.save() + flush_webhook_execution_queue() + log = xhiveframework.get_last_doc("Webhook Request Log") + self.assertEqual(len(json.loads(log.response)), 3) + + def test_webhook_with_dynamic_url_enabled(self): + wh_config = { + "doctype": "Webhook", + "webhook_doctype": "Note", + "webhook_docevent": "after_insert", + "enabled": 1, + "request_url": "https://httpbin.org/anything/{{ doc.doctype }}", + "is_dynamic_url": 1, + "request_method": "POST", + "request_structure": "JSON", + "webhook_json": "{}", + "meets_condition": "Yes", + "webhook_headers": [ + { + "key": "Content-Type", + "value": "application/json", + } + ], + } + + self.responses.add( + responses.POST, + "https://httpbin.org/anything/Note", + status=200, + ) + + with get_test_webhook(wh_config) as wh: + doc = xhiveframework.new_doc("Note") + doc.title = "Test Webhook Note" + enqueue_webhook(doc, wh) + + def test_webhook_with_dynamic_url_disabled(self): + wh_config = { + "doctype": "Webhook", + "webhook_doctype": "Note", + "webhook_docevent": "after_insert", + "enabled": 1, + "request_url": "https://httpbin.org/anything/{{doc.doctype}}", + "is_dynamic_url": 0, + "request_method": "POST", + "request_structure": "JSON", + "webhook_json": "{}", + "meets_condition": "Yes", + "webhook_headers": [ + { + "key": "Content-Type", + "value": "application/json", + } + ], + } + + self.responses.add( + responses.POST, + "https://httpbin.org/anything/{{doc.doctype}}", + status=200, + ) + + with get_test_webhook(wh_config) as wh: + doc = xhiveframework.new_doc("Note") + doc.title = "Test Webhook Note" + enqueue_webhook(doc, wh) diff --git a/xhiveframework/integrations/doctype/webhook/webhook.js b/xhiveframework/integrations/doctype/webhook/webhook.js new file mode 100644 index 0000000..365c2fb --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook/webhook.js @@ -0,0 +1,129 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.webhook = { + set_fieldname_select: (frm) => { + if (frm.doc.webhook_doctype) { + xhiveframework.model.with_doctype(frm.doc.webhook_doctype, () => { + // get doctype fields + let fields = $.map( + xhiveframework.get_doc("DocType", frm.doc.webhook_doctype).fields, + (d) => { + if ( + xhiveframework.model.no_value_type.includes(d.fieldtype) && + !xhiveframework.model.table_fields.includes(d.fieldtype) + ) { + return null; + } else { + return { + label: `${__(d.label, null, d.parent)} (${__(d.fieldtype)})`, + value: d.fieldname, + }; + } + } + ); + + // add meta fields + for (let field of xhiveframework.model.std_fields) { + if (field.fieldname == "name") { + fields.unshift({ label: __("Name (Doc Name)"), value: "name" }); + } else { + fields.push({ + label: `${__(field.label, null, field.parent)} (${__( + field.fieldtype + )})`, + value: field.fieldname, + }); + } + } + + frm.fields_dict.webhook_data.grid.update_docfield_property( + "fieldname", + "options", + [""].concat(fields) + ); + }); + } + }, + + set_request_headers: (frm) => { + if (frm.doc.request_structure) { + let header_value; + if (frm.doc.request_structure == "Form URL-Encoded") { + header_value = "application/x-www-form-urlencoded"; + } else if (frm.doc.request_structure == "JSON") { + header_value = "application/json"; + } + + if (header_value) { + let header_row = (frm.doc.webhook_headers || []).find( + (row) => row.key === "Content-Type" + ); + if (header_row) { + xhiveframework.model.set_value( + header_row.doctype, + header_row.name, + "value", + header_value + ); + } else { + frm.add_child("webhook_headers", { + key: "Content-Type", + value: header_value, + }); + } + frm.refresh(); + } + } + }, +}; + +xhiveframework.ui.form.on("Webhook", { + refresh: (frm) => { + xhiveframework.webhook.set_fieldname_select(frm); + frm.set_query( + "background_jobs_queue", + "xhiveframework.integrations.doctype.webhook.webhook.get_all_queues" + ); + }, + + request_structure: (frm) => { + xhiveframework.webhook.set_request_headers(frm); + }, + + webhook_doctype: (frm) => { + xhiveframework.webhook.set_fieldname_select(frm); + }, + + enable_security: (frm) => { + frm.toggle_reqd("webhook_secret", frm.doc.enable_security); + }, + + preview_document: (frm) => { + xhiveframework.call({ + method: "generate_preview", + doc: frm.doc, + callback: (r) => { + frm.refresh_field("meets_condition"); + frm.refresh_field("preview_request_body"); + }, + }); + }, +}); + +xhiveframework.ui.form.on("Webhook Data", { + fieldname: (frm, cdt, cdn) => { + let row = locals[cdt][cdn]; + let df = xhiveframework + .get_meta(frm.doc.webhook_doctype) + .fields.filter((field) => field.fieldname == row.fieldname); + + if (!df.length) { + // check if field is a meta field + df = xhiveframework.model.std_fields.filter((field) => field.fieldname == row.fieldname); + } + + row.key = df.length ? df[0].fieldname : "name"; + frm.refresh_field("webhook_data"); + }, +}); diff --git a/xhiveframework/integrations/doctype/webhook/webhook.json b/xhiveframework/integrations/doctype/webhook/webhook.json new file mode 100644 index 0000000..a4d924f --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook/webhook.json @@ -0,0 +1,253 @@ +{ + "actions": [], + "autoname": "prompt", + "creation": "2017-09-08 16:16:13.060641", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "sb_doc_events", + "webhook_doctype", + "cb_doc_events", + "webhook_docevent", + "enabled", + "sb_condition", + "condition", + "cb_condition", + "html_condition", + "sb_webhook", + "request_url", + "is_dynamic_url", + "timeout", + "background_jobs_queue", + "cb_webhook", + "request_method", + "request_structure", + "sb_security", + "enable_security", + "webhook_secret", + "sb_webhook_headers", + "webhook_headers", + "sb_webhook_data", + "webhook_data", + "webhook_json", + "preview_tab", + "preview_document", + "column_break_26", + "meets_condition", + "section_break_28", + "preview_request_body" + ], + "fields": [ + { + "fieldname": "sb_doc_events", + "fieldtype": "Section Break", + "label": "Doc Events" + }, + { + "fieldname": "webhook_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1, + "set_only_once": 1 + }, + { + "fieldname": "cb_doc_events", + "fieldtype": "Column Break" + }, + { + "fieldname": "webhook_docevent", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Doc Event", + "options": "after_insert\non_update\non_submit\non_cancel\non_trash\non_update_after_submit\non_change", + "set_only_once": 1 + }, + { + "fieldname": "sb_condition", + "fieldtype": "Section Break", + "label": "Webhook Trigger" + }, + { + "description": "The webhook will be triggered if this expression is true", + "fieldname": "condition", + "fieldtype": "Small Text", + "label": "Condition" + }, + { + "fieldname": "cb_condition", + "fieldtype": "Column Break" + }, + { + "fieldname": "html_condition", + "fieldtype": "HTML", + "options": "

      Condition Examples:

      \n
      doc.status==\"Open\"
      doc.due_date==nowdate()
      doc.total > 40000\n
      " + }, + { + "fieldname": "sb_webhook", + "fieldtype": "Section Break", + "label": "Webhook Request" + }, + { + "fieldname": "request_url", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Request URL", + "reqd": 1 + }, + { + "fieldname": "sb_webhook_headers", + "fieldtype": "Section Break", + "label": "Webhook Headers" + }, + { + "fieldname": "webhook_headers", + "fieldtype": "Table", + "label": "Headers", + "options": "Webhook Header" + }, + { + "fieldname": "sb_webhook_data", + "fieldtype": "Section Break", + "label": "Webhook Data" + }, + { + "depends_on": "eval: !doc.request_structure || doc.request_structure == \"Form URL-Encoded\"", + "fieldname": "webhook_data", + "fieldtype": "Table", + "label": "Data", + "options": "Webhook Data" + }, + { + "fieldname": "cb_webhook", + "fieldtype": "Column Break" + }, + { + "fieldname": "request_structure", + "fieldtype": "Select", + "label": "Request Structure", + "options": "\nForm URL-Encoded\nJSON" + }, + { + "depends_on": "eval: doc.request_structure == \"JSON\"", + "description": "To add dynamic values from the document, use jinja tags like\n\n
      \n
      { \"id\": \"{{ doc.name }}\" }\n
      \n
      ", + "fieldname": "webhook_json", + "fieldtype": "Code", + "label": "JSON Request Body", + "options": "JSON" + }, + { + "fieldname": "sb_security", + "fieldtype": "Section Break", + "label": "Webhook Security" + }, + { + "default": "0", + "fieldname": "enable_security", + "fieldtype": "Check", + "label": "Enable Security" + }, + { + "depends_on": "eval:doc.enable_security == 1", + "fieldname": "webhook_secret", + "fieldtype": "Password", + "label": "Webhook Secret" + }, + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" + }, + { + "default": "POST", + "fieldname": "request_method", + "fieldtype": "Select", + "label": "Request Method", + "options": "POST\nPUT\nDELETE", + "reqd": 1 + }, + { + "fieldname": "preview_tab", + "fieldtype": "Tab Break", + "label": "Preview" + }, + { + "fieldname": "preview_document", + "fieldtype": "Dynamic Link", + "label": "Select Document", + "options": "webhook_doctype" + }, + { + "fieldname": "preview_request_body", + "fieldtype": "Code", + "is_virtual": 1, + "label": "Request Body" + }, + { + "fieldname": "meets_condition", + "fieldtype": "Data", + "is_virtual": 1, + "label": "Meets Condition?" + }, + { + "fieldname": "column_break_26", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_28", + "fieldtype": "Section Break" + }, + { + "default": "0", + "description": "On checking this option, URL will be treated like a jinja template string", + "fieldname": "is_dynamic_url", + "fieldtype": "Check", + "label": "Is Dynamic URL?" + }, + { + "default": "5", + "description": "The number of seconds until the request expires", + "fieldname": "timeout", + "fieldtype": "Int", + "label": "Request Timeout" + }, + { + "fieldname": "background_jobs_queue", + "fieldtype": "Autocomplete", + "label": "Background Jobs Queue" + } + ], + "links": [ + { + "link_doctype": "Webhook Request Log", + "link_fieldname": "webhook" + } + ], + "modified": "2024-02-19 11:40:58.387233", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Webhook", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/webhook/webhook.py b/xhiveframework/integrations/doctype/webhook/webhook.py new file mode 100644 index 0000000..eea219f --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook/webhook.py @@ -0,0 +1,264 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import base64 +import hashlib +import hmac +import json +from time import sleep +from urllib.parse import urlparse + +import requests + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils.background_jobs import get_queues_timeout +from xhiveframework.utils.jinja import validate_template +from xhiveframework.utils.safe_exec import get_safe_globals + +WEBHOOK_SECRET_HEADER = "X-XhiveFramework-Webhook-Signature" + + +class Webhook(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.integrations.doctype.webhook_data.webhook_data import WebhookData + from xhiveframework.integrations.doctype.webhook_header.webhook_header import WebhookHeader + from xhiveframework.types import DF + + background_jobs_queue: DF.Autocomplete | None + condition: DF.SmallText | None + enable_security: DF.Check + enabled: DF.Check + is_dynamic_url: DF.Check + meets_condition: DF.Data | None + preview_document: DF.DynamicLink | None + preview_request_body: DF.Code | None + request_method: DF.Literal["POST", "PUT", "DELETE"] + request_structure: DF.Literal["", "Form URL-Encoded", "JSON"] + request_url: DF.SmallText + timeout: DF.Int + webhook_data: DF.Table[WebhookData] + webhook_docevent: DF.Literal[ + "after_insert", + "on_update", + "on_submit", + "on_cancel", + "on_trash", + "on_update_after_submit", + "on_change", + ] + webhook_doctype: DF.Link + webhook_headers: DF.Table[WebhookHeader] + webhook_json: DF.Code | None + webhook_secret: DF.Password | None + # end: auto-generated types + + def validate(self): + self.validate_docevent() + self.validate_condition() + self.validate_request_url() + self.validate_request_body() + self.validate_repeating_fields() + self.validate_secret() + self.preview_document = None + + def on_update(self): + xhiveframework.cache.delete_value("webhooks") + + def validate_docevent(self): + if self.webhook_doctype: + is_submittable = xhiveframework.get_value("DocType", self.webhook_doctype, "is_submittable") + if not is_submittable and self.webhook_docevent in [ + "on_submit", + "on_cancel", + "on_update_after_submit", + ]: + xhiveframework.throw(_("DocType must be Submittable for the selected Doc Event")) + + def validate_condition(self): + temp_doc = xhiveframework.new_doc(self.webhook_doctype) + if self.condition: + try: + xhiveframework.safe_eval(self.condition, eval_locals=get_context(temp_doc)) + except Exception as e: + xhiveframework.throw(_("Invalid Condition: {}").format(e)) + + def validate_request_url(self): + try: + request_url = urlparse(self.request_url).netloc + if not request_url: + raise xhiveframework.ValidationError + except Exception as e: + xhiveframework.throw(_("Check Request URL"), exc=e) + + def validate_request_body(self): + if self.request_structure: + if self.request_structure == "Form URL-Encoded": + self.webhook_json = None + elif self.request_structure == "JSON": + validate_template(self.webhook_json) + self.webhook_data = [] + + def validate_repeating_fields(self): + """Error when Same Field is entered multiple times in webhook_data""" + webhook_data = [entry.fieldname for entry in self.webhook_data] + if len(webhook_data) != len(set(webhook_data)): + xhiveframework.throw(_("Same Field is entered more than once")) + + def validate_secret(self): + if self.enable_security: + try: + self.get_password("webhook_secret", False).encode("utf8") + except Exception: + xhiveframework.throw(_("Invalid Webhook Secret")) + + @xhiveframework.whitelist() + def generate_preview(self): + # This function doesn't need to do anything specific as virtual fields + # get evaluated automatically. + pass + + @property + def meets_condition(self): + if not self.condition: + return _("Yes") + + if not (self.preview_document and self.webhook_doctype): + return _("Select a document to check if it meets conditions.") + + try: + doc = xhiveframework.get_cached_doc(self.webhook_doctype, self.preview_document) + met_condition = xhiveframework.safe_eval(self.condition, eval_locals=get_context(doc)) + except Exception as e: + return _("Failed to evaluate conditions: {}").format(e) + return _("Yes") if met_condition else _("No") + + @property + def preview_request_body(self): + if not (self.preview_document and self.webhook_doctype): + return _("Select a document to preview request data") + + try: + doc = xhiveframework.get_cached_doc(self.webhook_doctype, self.preview_document) + return xhiveframework.as_json(get_webhook_data(doc, self)) + except Exception as e: + return _("Failed to compute request body: {}").format(e) + + +def get_context(doc): + return {"doc": doc, "utils": get_safe_globals().get("xhiveframework").get("utils")} + + +def enqueue_webhook(doc, webhook) -> None: + request_url = headers = data = None + try: + webhook: Webhook = xhiveframework.get_doc("Webhook", webhook.get("name")) + request_url = webhook.request_url + if webhook.is_dynamic_url: + request_url = xhiveframework.render_template(webhook.request_url, get_context(doc)) + headers = get_webhook_headers(doc, webhook) + data = get_webhook_data(doc, webhook) + + except Exception as e: + xhiveframework.logger().debug({"enqueue_webhook_error": e}) + log_request(webhook.name, doc.name, request_url, headers, data) + return + + for i in range(3): + try: + r = requests.request( + method=webhook.request_method, + url=request_url, + data=json.dumps(data, default=str), + headers=headers, + timeout=webhook.timeout or 5, + ) + r.raise_for_status() + xhiveframework.logger().debug({"webhook_success": r.text}) + log_request(webhook.name, doc.name, request_url, headers, data, r) + break + + except requests.exceptions.ReadTimeout as e: + xhiveframework.logger().debug({"webhook_error": e, "try": i + 1}) + log_request(webhook.name, doc.name, request_url, headers, data) + + except Exception as e: + xhiveframework.logger().debug({"webhook_error": e, "try": i + 1}) + log_request(webhook.name, doc.name, request_url, headers, data, r) + sleep(3 * i + 1) + if i != 2: + continue + + +def log_request( + webhook: str, + docname: str, + url: str, + headers: dict, + data: dict, + res: requests.Response | None = None, +): + request_log = xhiveframework.get_doc( + { + "doctype": "Webhook Request Log", + "webhook": webhook, + "reference_document": docname, + "user": xhiveframework.session.user if xhiveframework.session.user else None, + "url": url, + "headers": xhiveframework.as_json(headers) if headers else None, + "data": xhiveframework.as_json(data) if data else None, + "response": res and res.text, + "error": xhiveframework.get_traceback(), + } + ) + + request_log.save(ignore_permissions=True) + + +def get_webhook_headers(doc, webhook): + headers = {} + + if webhook.enable_security: + data = get_webhook_data(doc, webhook) + signature = base64.b64encode( + hmac.new( + webhook.get_password("webhook_secret").encode("utf8"), + json.dumps(data).encode("utf8"), + hashlib.sha256, + ).digest() + ) + headers[WEBHOOK_SECRET_HEADER] = signature + + if webhook.webhook_headers: + for h in webhook.webhook_headers: + if h.get("key") and h.get("value"): + headers[h.get("key")] = h.get("value") + + return headers + + +def get_webhook_data(doc, webhook): + data = {} + doc = doc.as_dict(convert_dates_to_str=True) + + if webhook.webhook_data: + data = {w.key: doc.get(w.fieldname) for w in webhook.webhook_data} + elif webhook.webhook_json: + data = xhiveframework.render_template(webhook.webhook_json, get_context(doc)) + data = json.loads(data) + + return data + + +@xhiveframework.whitelist() +def get_all_queues(): + """Fetches all workers and returns a list of available queue names.""" + xhiveframework.only_for("System Manager") + + return get_queues_timeout().keys() diff --git a/xhiveframework/integrations/doctype/webhook_data/__init__.py b/xhiveframework/integrations/doctype/webhook_data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/webhook_data/webhook_data.json b/xhiveframework/integrations/doctype/webhook_data/webhook_data.json new file mode 100644 index 0000000..2ace6a9 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_data/webhook_data.json @@ -0,0 +1,43 @@ +{ + "actions": [], + "creation": "2017-09-14 12:08:50.302810", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "fieldname", + "cb_doc_data", + "key" + ], + "fields": [ + { + "fieldname": "fieldname", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Fieldname", + "reqd": 1 + }, + { + "fieldname": "cb_doc_data", + "fieldtype": "Column Break" + }, + { + "fieldname": "key", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Key", + "reqd": 1 + } + ], + "istable": 1, + "links": [], + "modified": "2022-08-03 12:20:52.208987", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Webhook Data", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/webhook_data/webhook_data.py b/xhiveframework/integrations/doctype/webhook_data/webhook_data.py new file mode 100644 index 0000000..2a7e8fd --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_data/webhook_data.py @@ -0,0 +1,23 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WebhookData(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + fieldname: DF.Literal[None] + key: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/webhook_header/__init__.py b/xhiveframework/integrations/doctype/webhook_header/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/webhook_header/webhook_header.json b/xhiveframework/integrations/doctype/webhook_header/webhook_header.json new file mode 100644 index 0000000..6a7e8f9 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_header/webhook_header.json @@ -0,0 +1,38 @@ +{ + "actions": [], + "creation": "2017-09-08 16:27:39.195379", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "key", + "value" + ], + "fields": [ + { + "fieldname": "key", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Key" + }, + { + "fieldname": "value", + "fieldtype": "Small Text", + "in_list_view": 1, + "label": "Value" + } + ], + "istable": 1, + "links": [], + "modified": "2023-12-11 12:20:51.949422", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Webhook Header", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/webhook_header/webhook_header.py b/xhiveframework/integrations/doctype/webhook_header/webhook_header.py new file mode 100644 index 0000000..18c20c9 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_header/webhook_header.py @@ -0,0 +1,23 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.model.document import Document + + +class WebhookHeader(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + key: DF.Data | None + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + value: DF.Data | None + # end: auto-generated types + pass diff --git a/xhiveframework/integrations/doctype/webhook_request_log/__init__.py b/xhiveframework/integrations/doctype/webhook_request_log/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/integrations/doctype/webhook_request_log/test_webhook_request_log.py b/xhiveframework/integrations/doctype/webhook_request_log/test_webhook_request_log.py new file mode 100644 index 0000000..bb7150d --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_request_log/test_webhook_request_log.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestWebhookRequestLog(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.js b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.js new file mode 100644 index 0000000..bd3bbae --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Webhook Request Log", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.json b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.json new file mode 100644 index 0000000..b07197b --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.json @@ -0,0 +1,105 @@ +{ + "actions": [], + "autoname": "hash", + "creation": "2021-05-24 21:35:59.104776", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "webhook", + "reference_document", + "headers", + "data", + "column_break_4", + "user", + "url", + "response", + "error" + ], + "fields": [ + { + "fieldname": "url", + "fieldtype": "Data", + "label": "URL", + "read_only": 1 + }, + { + "fieldname": "headers", + "fieldtype": "Code", + "label": "Headers", + "options": "JSON", + "read_only": 1 + }, + { + "fieldname": "response", + "fieldtype": "Code", + "label": "Response", + "options": "JSON", + "read_only": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "data", + "fieldtype": "Code", + "label": "Data", + "options": "JSON", + "read_only": 1 + }, + { + "fieldname": "user", + "fieldtype": "Link", + "label": "User", + "options": "User", + "read_only": 1 + }, + { + "fieldname": "reference_document", + "fieldtype": "Data", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Reference Document", + "read_only": 1 + }, + { + "fieldname": "error", + "fieldtype": "Text", + "label": "Error", + "read_only": 1 + }, + { + "fieldname": "webhook", + "fieldtype": "Link", + "label": "Webhook", + "options": "Webhook" + } + ], + "in_create": 1, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-05-21 15:50:10.414002", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Webhook Request Log", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.py b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.py new file mode 100644 index 0000000..7b79a42 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log.py @@ -0,0 +1,33 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class WebhookRequestLog(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + data: DF.Code | None + error: DF.Text | None + headers: DF.Code | None + reference_document: DF.Data | None + response: DF.Code | None + url: DF.Data | None + user: DF.Link | None + webhook: DF.Link | None + + # end: auto-generated types + @staticmethod + def clear_old_logs(days=30): + from xhiveframework.query_builder import Interval + from xhiveframework.query_builder.functions import Now + + table = xhiveframework.qb.DocType("Webhook Request Log") + xhiveframework.db.delete(table, filters=(table.modified < (Now() - Interval(days=days)))) diff --git a/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log_list.js b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log_list.js new file mode 100644 index 0000000..d3e5740 --- /dev/null +++ b/xhiveframework/integrations/doctype/webhook_request_log/webhook_request_log_list.js @@ -0,0 +1,7 @@ +xhiveframework.listview_settings["Webhook Request Log"] = { + onload: function (list_view) { + xhiveframework.require("logtypes.bundle.js", () => { + xhiveframework.utils.logtypes.show_log_retention_message(list_view.doctype); + }); + }, +}; diff --git a/xhiveframework/integrations/google_oauth.py b/xhiveframework/integrations/google_oauth.py new file mode 100644 index 0000000..712a535 --- /dev/null +++ b/xhiveframework/integrations/google_oauth.py @@ -0,0 +1,195 @@ +import json + +from google.oauth2.credentials import Credentials +from googleapiclient.discovery import build +from requests import get, post + +import xhiveframework +from xhiveframework.utils import get_request_site_address + +CALLBACK_METHOD = "/api/method/xhiveframework.integrations.google_oauth.callback" +_SCOPES = { + "mail": ("https://mail.google.com/"), + "contacts": ("https://www.googleapis.com/auth/contacts"), + "drive": ("https://www.googleapis.com/auth/drive"), + "indexing": ("https://www.googleapis.com/auth/indexing"), +} +_SERVICES = { + "contacts": ("people", "v1"), + "drive": ("drive", "v3"), + "indexing": ("indexing", "v3"), +} +_DOMAIN_CALLBACK_METHODS = { + "mail": "xhiveframework.email.oauth.authorize_google_access", + "contacts": "xhiveframework.integrations.doctype.google_contacts.google_contacts.authorize_access", + "drive": "xhiveframework.integrations.doctype.google_drive.google_drive.authorize_access", + "indexing": "xhiveframework.website.doctype.website_settings.google_indexing.authorize_access", +} + + +class GoogleAuthenticationError(Exception): + pass + + +class GoogleOAuth: + OAUTH_URL = "https://oauth2.googleapis.com/token" + + def __init__(self, domain: str, validate: bool = True): + self.google_settings = xhiveframework.get_single("Google Settings") + self.domain = domain.lower() + self.scopes = ( + " ".join(_SCOPES[self.domain]) + if isinstance(_SCOPES[self.domain], list | tuple) + else _SCOPES[self.domain] + ) + + if validate: + self.validate_google_settings() + + def validate_google_settings(self): + google_settings = "Google Settings" + + if not self.google_settings.enable: + xhiveframework.throw(xhiveframework._("Please enable {} before continuing.").format(google_settings)) + + if not (self.google_settings.client_id and self.google_settings.client_secret): + xhiveframework.throw(xhiveframework._("Please update {} before continuing.").format(google_settings)) + + def authorize(self, oauth_code: str) -> dict[str, str | int]: + """Returns a dict with access and refresh token. + + :param oauth_code: code got back from google upon successful auhtorization + """ + + data = { + "code": oauth_code, + "client_id": self.google_settings.client_id, + "client_secret": self.google_settings.get_password( + fieldname="client_secret", raise_exception=False + ), + "grant_type": "authorization_code", + "scope": self.scopes, + "redirect_uri": get_request_site_address(True) + CALLBACK_METHOD, + } + + return handle_response( + post(self.OAUTH_URL, data=data).json(), + "Google Oauth Authorization Error", + "Something went wrong during the authorization.", + ) + + def refresh_access_token(self, refresh_token: str) -> dict[str, str | int]: + """Refreshes google access token using refresh token""" + + data = { + "client_id": self.google_settings.client_id, + "client_secret": self.google_settings.get_password( + fieldname="client_secret", raise_exception=False + ), + "refresh_token": refresh_token, + "grant_type": "refresh_token", + "scope": self.scopes, + } + + return handle_response( + post(self.OAUTH_URL, data=data).json(), + "Google Oauth Access Token Refresh Error", + "Something went wrong during the access token generation.", + raise_err=True, + ) + + def get_authentication_url(self, state: dict[str, str]) -> dict[str, str]: + """Returns google authentication url. + + :param state: dict of values which you need on callback (for calling methods, redirection back to the form, doc name, etc) + """ + + state.update({"domain": self.domain}) + state = json.dumps(state) + callback_url = get_request_site_address(True) + CALLBACK_METHOD + + return { + "url": "https://accounts.google.com/o/oauth2/v2/auth?" + + "access_type=offline&response_type=code&prompt=consent&include_granted_scopes=true&" + + "client_id={}&scope={}&redirect_uri={}&state={}".format( + self.google_settings.client_id, self.scopes, callback_url, state + ) + } + + def get_google_service_object(self, access_token: str, refresh_token: str): + """Returns google service object""" + + credentials_dict = { + "token": access_token, + "refresh_token": refresh_token, + "token_uri": self.OAUTH_URL, + "client_id": self.google_settings.client_id, + "client_secret": self.google_settings.get_password( + fieldname="client_secret", raise_exception=False + ), + "scopes": self.scopes, + } + + return build( + serviceName=_SERVICES[self.domain][0], + version=_SERVICES[self.domain][1], + credentials=Credentials(**credentials_dict), + static_discovery=False, + ) + + +def handle_response( + response: dict[str, str | int], + error_title: str, + error_message: str, + raise_err: bool = False, +): + if "error" in response: + xhiveframework.log_error(xhiveframework._(error_title), xhiveframework._(response.get("error_description", error_message))) + + if raise_err: + xhiveframework.throw(xhiveframework._(error_title), GoogleAuthenticationError, xhiveframework._(error_message)) + + return {} + + return response + + +def is_valid_access_token(access_token: str) -> bool: + response = get("https://oauth2.googleapis.com/tokeninfo", params={"access_token": access_token}).json() + + if "error" in response: + return False + + return True + + +@xhiveframework.whitelist(methods=["GET"]) +def callback(state: str, code: str | None = None, error: str | None = None) -> None: + """Common callback for google integrations. + Invokes functions using `xhiveframework.get_attr` and also adds required (keyworded) arguments + along with committing and redirecting us back to xhiveframework site.""" + + state = json.loads(state) + redirect = state.pop("redirect", "/app") + success_query_param = state.pop("success_query_param", "") + failure_query_param = state.pop("failure_query_param", "") + + if not error: + if (domain := state.pop("domain")) in _DOMAIN_CALLBACK_METHODS: + state.update({"code": code}) + xhiveframework.get_attr(_DOMAIN_CALLBACK_METHODS[domain])(**state) + + # GET request, hence using commit to persist changes + xhiveframework.db.commit() # nosemgrep + else: + return xhiveframework.respond_as_web_page( + "Invalid Google Callback", + "The callback domain provided is not valid for Google Authentication", + http_status_code=400, + indicator_color="red", + width=640, + ) + + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = f"{redirect}?{failure_query_param if error else success_query_param}" diff --git a/xhiveframework/integrations/oauth2.py b/xhiveframework/integrations/oauth2.py new file mode 100644 index 0000000..67b02d7 --- /dev/null +++ b/xhiveframework/integrations/oauth2.py @@ -0,0 +1,244 @@ +import json +from urllib.parse import quote, urlencode + +from oauthlib.oauth2 import FatalClientError, OAuth2Error +from oauthlib.openid.connect.core.endpoints.pre_configured import Server as WebApplicationServer + +import xhiveframework +from xhiveframework.integrations.doctype.oauth_provider_settings.oauth_provider_settings import ( + get_oauth_settings, +) +from xhiveframework.oauth import ( + OAuthWebRequestValidator, + generate_json_error_response, + get_server_url, + get_userinfo, +) + + +def get_oauth_server(): + if not getattr(xhiveframework.local, "oauth_server", None): + oauth_validator = OAuthWebRequestValidator() + xhiveframework.local.oauth_server = WebApplicationServer(oauth_validator) + + return xhiveframework.local.oauth_server + + +def sanitize_kwargs(param_kwargs): + """Remove 'data' and 'cmd' keys, if present.""" + arguments = param_kwargs + arguments.pop("data", None) + arguments.pop("cmd", None) + + return arguments + + +def encode_params(params): + """ + Encode a dict of params into a query string. + + Use `quote_via=urllib.parse.quote` so that whitespaces will be encoded as + `%20` instead of as `+`. This is needed because oauthlib cannot handle `+` + as a whitespace. + """ + return urlencode(params, quote_via=quote) + + +@xhiveframework.whitelist() +def approve(*args, **kwargs): + r = xhiveframework.request + + try: + ( + scopes, + xhiveframework.flags.oauth_credentials, + ) = get_oauth_server().validate_authorization_request(r.url, r.method, r.get_data(), r.headers) + + headers, body, status = get_oauth_server().create_authorization_response( + uri=xhiveframework.flags.oauth_credentials["redirect_uri"], + body=r.get_data(), + headers=r.headers, + scopes=scopes, + credentials=xhiveframework.flags.oauth_credentials, + ) + uri = headers.get("Location", None) + + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = uri + return + + except (FatalClientError, OAuth2Error) as e: + return generate_json_error_response(e) + + +@xhiveframework.whitelist(allow_guest=True) +def authorize(**kwargs): + success_url = "/api/method/xhiveframework.integrations.oauth2.approve?" + encode_params(sanitize_kwargs(kwargs)) + failure_url = xhiveframework.form_dict["redirect_uri"] + "?error=access_denied" + + if xhiveframework.session.user == "Guest": + # Force login, redirect to preauth again. + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = "/login?" + encode_params({"redirect-to": xhiveframework.request.url}) + else: + try: + r = xhiveframework.request + ( + scopes, + xhiveframework.flags.oauth_credentials, + ) = get_oauth_server().validate_authorization_request(r.url, r.method, r.get_data(), r.headers) + + skip_auth = xhiveframework.db.get_value( + "OAuth Client", + xhiveframework.flags.oauth_credentials["client_id"], + "skip_authorization", + ) + unrevoked_tokens = xhiveframework.get_all("OAuth Bearer Token", filters={"status": "Active"}) + + if skip_auth or (get_oauth_settings().skip_authorization == "Auto" and unrevoked_tokens): + xhiveframework.local.response["type"] = "redirect" + xhiveframework.local.response["location"] = success_url + else: + if "openid" in scopes: + scopes.remove("openid") + scopes.extend(["Full Name", "Email", "User Image", "Roles"]) + + # Show Allow/Deny screen. + response_html_params = xhiveframework._dict( + { + "client_id": xhiveframework.db.get_value("OAuth Client", kwargs["client_id"], "app_name"), + "success_url": success_url, + "failure_url": failure_url, + "details": scopes, + } + ) + resp_html = xhiveframework.render_template( + "templates/includes/oauth_confirmation.html", response_html_params + ) + xhiveframework.respond_as_web_page("Confirm Access", resp_html, primary_action=None) + except (FatalClientError, OAuth2Error) as e: + return generate_json_error_response(e) + + +@xhiveframework.whitelist(allow_guest=True) +def get_token(*args, **kwargs): + try: + r = xhiveframework.request + headers, body, status = get_oauth_server().create_token_response( + r.url, r.method, r.form, r.headers, xhiveframework.flags.oauth_credentials + ) + body = xhiveframework._dict(json.loads(body)) + + if body.error: + xhiveframework.local.response = body + xhiveframework.local.response["http_status_code"] = 400 + return + + xhiveframework.local.response = body + return + + except (FatalClientError, OAuth2Error) as e: + return generate_json_error_response(e) + + +@xhiveframework.whitelist(allow_guest=True) +def revoke_token(*args, **kwargs): + try: + r = xhiveframework.request + headers, body, status = get_oauth_server().create_revocation_response( + r.url, + headers=r.headers, + body=r.form, + http_method=r.method, + ) + except (FatalClientError, OAuth2Error): + pass + + # status_code must be 200 + xhiveframework.local.response = xhiveframework._dict({}) + xhiveframework.local.response["http_status_code"] = status or 200 + return + + +@xhiveframework.whitelist() +def openid_profile(*args, **kwargs): + try: + r = xhiveframework.request + headers, body, status = get_oauth_server().create_userinfo_response( + r.url, + headers=r.headers, + body=r.form, + ) + body = xhiveframework._dict(json.loads(body)) + xhiveframework.local.response = body + return + + except (FatalClientError, OAuth2Error) as e: + return generate_json_error_response(e) + + +@xhiveframework.whitelist(allow_guest=True) +def openid_configuration(): + xhiveframework_server_url = get_server_url() + xhiveframework.local.response = xhiveframework._dict( + { + "issuer": xhiveframework_server_url, + "authorization_endpoint": f"{xhiveframework_server_url}/api/method/xhiveframework.integrations.oauth2.authorize", + "token_endpoint": f"{xhiveframework_server_url}/api/method/xhiveframework.integrations.oauth2.get_token", + "userinfo_endpoint": f"{xhiveframework_server_url}/api/method/xhiveframework.integrations.oauth2.openid_profile", + "revocation_endpoint": f"{xhiveframework_server_url}/api/method/xhiveframework.integrations.oauth2.revoke_token", + "introspection_endpoint": f"{xhiveframework_server_url}/api/method/xhiveframework.integrations.oauth2.introspect_token", + "response_types_supported": [ + "code", + "token", + "code id_token", + "code token id_token", + "id_token", + "id_token token", + ], + "subject_types_supported": ["public"], + "id_token_signing_alg_values_supported": ["HS256"], + } + ) + + +@xhiveframework.whitelist(allow_guest=True) +def introspect_token(token=None, token_type_hint=None): + if token_type_hint not in ["access_token", "refresh_token"]: + token_type_hint = "access_token" + try: + bearer_token = None + if token_type_hint == "access_token": + bearer_token = xhiveframework.get_doc("OAuth Bearer Token", {"access_token": token}) + elif token_type_hint == "refresh_token": + bearer_token = xhiveframework.get_doc("OAuth Bearer Token", {"refresh_token": token}) + + client = xhiveframework.get_doc("OAuth Client", bearer_token.client) + + token_response = xhiveframework._dict( + { + "client_id": client.client_id, + "trusted_client": client.skip_authorization, + "active": bearer_token.status == "Active", + "exp": round(bearer_token.expiration_time.timestamp()), + "scope": bearer_token.scopes, + } + ) + + if "openid" in bearer_token.scopes: + sub = xhiveframework.get_value( + "User Social Login", + {"provider": "xhiveframework", "parent": bearer_token.user}, + "userid", + ) + + if sub: + token_response.update({"sub": sub}) + user = xhiveframework.get_doc("User", bearer_token.user) + userinfo = get_userinfo(user) + token_response.update(userinfo) + + xhiveframework.local.response = token_response + + except Exception: + xhiveframework.local.response = xhiveframework._dict({"active": False}) diff --git a/xhiveframework/integrations/oauth2_logins.py b/xhiveframework/integrations/oauth2_logins.py new file mode 100644 index 0000000..a5aa631 --- /dev/null +++ b/xhiveframework/integrations/oauth2_logins.py @@ -0,0 +1,63 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +import xhiveframework.utils +from xhiveframework.utils.oauth import login_via_oauth2, login_via_oauth2_id_token + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_google(code: str, state: str): + login_via_oauth2("google", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_github(code: str, state: str): + login_via_oauth2("github", code, state) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_facebook(code: str, state: str): + login_via_oauth2("facebook", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_xhiveframework(code: str, state: str): + login_via_oauth2("xhiveframework", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_office365(code: str, state: str): + login_via_oauth2_id_token("office_365", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_salesforce(code: str, state: str): + login_via_oauth2("salesforce", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def login_via_fairlogin(code: str, state: str): + login_via_oauth2("fairlogin", code, state, decoder=decoder_compat) + + +@xhiveframework.whitelist(allow_guest=True) +def custom(code: str, state: str): + """ + Callback for processing code and state for user added providers + + process social login from /api/method/xhiveframework.integrations.oauth2_logins.custom/ + """ + path = xhiveframework.request.path[1:].split("/") + if len(path) == 4 and path[3]: + provider = path[3] + # Validates if provider doctype exists + if xhiveframework.db.exists("Social Login Key", provider): + login_via_oauth2(provider, code, state, decoder=decoder_compat) + + +def decoder_compat(b): + # https://github.com/litl/rauth/issues/145#issuecomment-31199471 + return json.loads(bytes(b).decode("utf-8")) diff --git a/xhiveframework/integrations/offsite_backup_utils.py b/xhiveframework/integrations/offsite_backup_utils.py new file mode 100644 index 0000000..4a044d8 --- /dev/null +++ b/xhiveframework/integrations/offsite_backup_utils.py @@ -0,0 +1,119 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import glob +import os + +import xhiveframework +from xhiveframework.utils import cint, split_emails + + +def send_email(success, service_name, doctype, email_field, error_status=None): + recipients = get_recipients(doctype, email_field) + if not recipients: + xhiveframework.log_error( + f"No Email Recipient found for {service_name}", + f"{service_name}: Failed to send backup status email", + ) + return + + if success: + if not xhiveframework.db.get_single_value(doctype, "send_email_for_successful_backup"): + return + + subject = "Backup Upload Successful" + message = """ +

      Backup Uploaded Successfully!

      +

      Hi there, this is just to inform you that your backup was successfully uploaded to your {} bucket. So relax!

      """.format( + service_name + ) + else: + subject = "[Warning] Backup Upload Failed" + message = f""" +

      Backup Upload Failed!

      +

      Oops, your automated backup to {service_name} failed.

      +

      Error message: {error_status}

      +

      Please contact your system manager for more information.

      """ + + xhiveframework.sendmail(recipients=recipients, subject=subject, message=message) + + +def get_recipients(doctype, email_field): + if not xhiveframework.db: + xhiveframework.connect() + + return split_emails(xhiveframework.db.get_value(doctype, None, email_field)) + + +def get_latest_backup_file(with_files=False): + from xhiveframework.utils.backups import BackupGenerator + + odb = BackupGenerator( + xhiveframework.conf.db_name, + xhiveframework.conf.db_name, + xhiveframework.conf.db_password, + db_host=xhiveframework.conf.db_host, + db_port=xhiveframework.conf.db_port, + db_type=xhiveframework.conf.db_type, + ) + database, public, private, config = odb.get_recent_backup(older_than=24 * 30) + + if with_files: + return database, config, public, private + + return database, config + + +def get_file_size(file_path, unit="MB"): + file_size = os.path.getsize(file_path) + + memory_size_unit_mapper = {"KB": 1, "MB": 2, "GB": 3, "TB": 4} + i = 0 + while i < memory_size_unit_mapper[unit]: + file_size = file_size / 1000.0 + i += 1 + + return file_size + + +def get_chunk_site(file_size): + """this function will return chunk size in megabytes based on file size""" + + file_size_in_gb = cint(file_size / 1024 / 1024) + + MB = 1024 * 1024 + if file_size_in_gb > 5000: + return 200 * MB + elif file_size_in_gb >= 3000: + return 150 * MB + elif file_size_in_gb >= 1000: + return 100 * MB + elif file_size_in_gb >= 500: + return 50 * MB + else: + return 15 * MB + + +def validate_file_size(): + xhiveframework.flags.create_new_backup = True + latest_file, site_config = get_latest_backup_file() + file_size = get_file_size(latest_file, unit="GB") if latest_file else 0 + + if file_size > 1: + xhiveframework.flags.create_new_backup = False + + +def generate_files_backup(): + from xhiveframework.utils.backups import BackupGenerator + + backup = BackupGenerator( + xhiveframework.conf.db_name, + xhiveframework.conf.db_name, + xhiveframework.conf.db_password, + db_host=xhiveframework.conf.db_host, + db_port=xhiveframework.conf.db_port, + db_type=xhiveframework.conf.db_type, + ) + + backup.set_backup_file_name() + backup.zip_files() diff --git a/xhiveframework/integrations/utils.py b/xhiveframework/integrations/utils.py new file mode 100644 index 0000000..e6720c9 --- /dev/null +++ b/xhiveframework/integrations/utils.py @@ -0,0 +1,111 @@ +# Copyright (c) 2019, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import datetime +import json +from urllib.parse import parse_qs + +import xhiveframework +from xhiveframework import _ +from xhiveframework.utils import get_request_session + + +def make_request(method, url, auth=None, headers=None, data=None, json=None, params=None): + auth = auth or "" + data = data or {} + headers = headers or {} + + try: + s = get_request_session() + xhiveframework.flags.integration_request = s.request( + method, url, data=data, auth=auth, headers=headers, json=json, params=params + ) + xhiveframework.flags.integration_request.raise_for_status() + + if xhiveframework.flags.integration_request.headers.get("content-type") == "text/plain; charset=utf-8": + return parse_qs(xhiveframework.flags.integration_request.text) + + return xhiveframework.flags.integration_request.json() + except Exception as exc: + xhiveframework.log_error() + raise exc + + +def make_get_request(url, **kwargs): + return make_request("GET", url, **kwargs) + + +def make_post_request(url, **kwargs): + return make_request("POST", url, **kwargs) + + +def make_put_request(url, **kwargs): + return make_request("PUT", url, **kwargs) + + +def make_patch_request(url, **kwargs): + return make_request("PATCH", url, **kwargs) + + +def make_delete_request(url, **kwargs): + return make_request("DELETE", url, **kwargs) + + +def create_request_log( + data, + integration_type=None, + service_name=None, + name=None, + error=None, + request_headers=None, + output=None, + **kwargs, +): + """ + DEPRECATED: The parameter integration_type will be removed in the next major release. + Use is_remote_request instead. + """ + if integration_type == "Remote": + kwargs["is_remote_request"] = 1 + + elif integration_type == "Subscription Notification": + kwargs["request_description"] = integration_type + + reference_doctype = reference_docname = None + if "reference_doctype" not in kwargs: + if isinstance(data, str): + data = json.loads(data) + + reference_doctype = data.get("reference_doctype") + reference_docname = data.get("reference_docname") + + integration_request = xhiveframework.get_doc( + { + "doctype": "Integration Request", + "integration_request_service": service_name, + "request_headers": get_json(request_headers), + "data": get_json(data), + "output": get_json(output), + "error": get_json(error), + "reference_doctype": reference_doctype, + "reference_docname": reference_docname, + **kwargs, + } + ) + + if name: + integration_request.flags._name = name + + integration_request.insert(ignore_permissions=True) + xhiveframework.db.commit() + + return integration_request + + +def get_json(obj): + return obj if isinstance(obj, str) else xhiveframework.as_json(obj, indent=1) + + +def json_handler(obj): + if isinstance(obj, datetime.date | datetime.timedelta | datetime.datetime): + return str(obj) diff --git a/xhiveframework/integrations/workspace/integrations/integrations.json b/xhiveframework/integrations/workspace/integrations/integrations.json new file mode 100644 index 0000000..5dab61a --- /dev/null +++ b/xhiveframework/integrations/workspace/integrations/integrations.json @@ -0,0 +1,235 @@ +{ + "charts": [], + "content": "[{\"id\":\"NPK_AfSLQ2\",\"type\":\"header\",\"data\":{\"text\":\"Reports & Masters\",\"col\":12}},{\"id\":\"lDOo58F7ZI\",\"type\":\"card\",\"data\":{\"card_name\":\"Backup\",\"col\":4}},{\"id\":\"ij1pcK8jst\",\"type\":\"card\",\"data\":{\"card_name\":\"Google Services\",\"col\":4}},{\"id\":\"aTlMujEHpN\",\"type\":\"card\",\"data\":{\"card_name\":\"Authentication\",\"col\":4}},{\"id\":\"gY5NXKtXss\",\"type\":\"card\",\"data\":{\"card_name\":\"Settings\",\"col\":4}},{\"id\":\"n_CI3GGqW-\",\"type\":\"card\",\"data\":{\"card_name\":\"Push Notifications\",\"col\":4}}]", + "creation": "2020-03-02 15:16:18.714190", + "custom_blocks": [], + "docstatus": 0, + "doctype": "Workspace", + "for_user": "", + "hide_custom": 0, + "icon": "integration", + "idx": 0, + "is_hidden": 0, + "label": "Integrations", + "links": [ + { + "hidden": 0, + "is_query_report": 0, + "label": "Backup", + "link_count": 0, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Dropbox Settings", + "link_count": 0, + "link_to": "Dropbox Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "S3 Backup Settings", + "link_count": 0, + "link_to": "S3 Backup Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Google Drive", + "link_count": 0, + "link_to": "Google Drive", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Google Services", + "link_count": 0, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Google Settings", + "link_count": 0, + "link_to": "Google Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Google Contacts", + "link_count": 0, + "link_to": "Google Contacts", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Google Calendar", + "link_count": 0, + "link_to": "Google Calendar", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Google Drive", + "link_count": 0, + "link_to": "Google Drive", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Settings", + "link_count": 0, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Webhook", + "link_count": 0, + "link_to": "Webhook", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Slack Webhook URL", + "link_count": 0, + "link_to": "Slack Webhook URL", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "SMS Settings", + "link_count": 0, + "link_to": "SMS Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Authentication", + "link_count": 4, + "onboard": 0, + "type": "Card Break" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "Social Login Key", + "link_count": 0, + "link_to": "Social Login Key", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "LDAP Settings", + "link_count": 0, + "link_to": "LDAP Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "OAuth Client", + "link_count": 0, + "link_to": "OAuth Client", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "dependencies": "", + "hidden": 0, + "is_query_report": 0, + "label": "OAuth Provider Settings", + "link_count": 0, + "link_to": "OAuth Provider Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Push Notifications", + "link_count": 1, + "link_type": "DocType", + "onboard": 0, + "type": "Card Break" + }, + { + "hidden": 0, + "is_query_report": 0, + "label": "Push Notification Settings", + "link_count": 0, + "link_to": "Push Notification Settings", + "link_type": "DocType", + "onboard": 0, + "type": "Link" + } + ], + "modified": "2024-02-28 10:47:38.188832", + "modified_by": "Administrator", + "module": "Integrations", + "name": "Integrations", + "number_cards": [], + "owner": "Administrator", + "parent_page": "", + "public": 1, + "quick_lists": [], + "restrict_to_domain": "", + "roles": [], + "sequence_id": 20.0, + "shortcuts": [], + "title": "Integrations" +} \ No newline at end of file diff --git a/xhiveframework/integrations/xhiveframework_providers/__init__.py b/xhiveframework/integrations/xhiveframework_providers/__init__.py new file mode 100644 index 0000000..c945f15 --- /dev/null +++ b/xhiveframework/integrations/xhiveframework_providers/__init__.py @@ -0,0 +1,13 @@ +# imports - standard imports +import sys + +# imports - module imports +from xhiveframework.integrations.xhiveframework_providers.xhiveframeworkcloud import xhiveframeworkcloud_migrator + + +def migrate_to(local_site, xhiveframework_provider): + if xhiveframework_provider in ("xhiveframework.cloud", "xhiveframeworkcloud.com"): + return xhiveframeworkcloud_migrator(local_site) + else: + print(f"{xhiveframework_provider} is not supported yet") + sys.exit(1) diff --git a/xhiveframework/integrations/xhiveframework_providers/xhiveframeworkcloud.py b/xhiveframework/integrations/xhiveframework_providers/xhiveframeworkcloud.py new file mode 100644 index 0000000..f087c10 --- /dev/null +++ b/xhiveframework/integrations/xhiveframework_providers/xhiveframeworkcloud.py @@ -0,0 +1,32 @@ +import click +import requests + +import xhiveframework +from xhiveframework.core.utils import html2text + + +def xhiveframeworkcloud_migrator(local_site): + print("Retrieving Site Migrator...") + remote_site = xhiveframework.conf.xhiveframeworkcloud_url or "xhiveframeworkcloud.com" + request_url = f"https://{remote_site}/api/method/press.api.script" + request = requests.get(request_url) + + if request.status_code / 100 != 2: + print(f"Request exited with Status Code: {request.status_code}\nPayload: {html2text(request.text)}") + click.secho( + "Some errors occurred while recovering the migration script. Please contact us @ XhiveFramework Cloud if this issue persists", + fg="yellow", + ) + return + + script_contents = request.json()["message"] + + import os + import sys + import tempfile + + py = sys.executable + script = tempfile.NamedTemporaryFile(mode="w") + script.write(script_contents) + print(f"Site Migrator stored at {script.name}") + os.execv(py, [py, script.name, local_site]) diff --git a/xhiveframework/middlewares.py b/xhiveframework/middlewares.py new file mode 100644 index 0000000..483eba5 --- /dev/null +++ b/xhiveframework/middlewares.py @@ -0,0 +1,28 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import os + +from werkzeug.exceptions import NotFound +from werkzeug.middleware.shared_data import SharedDataMiddleware + +import xhiveframework +from xhiveframework.utils import cstr, get_site_name + + +class StaticDataMiddleware(SharedDataMiddleware): + def __call__(self, environ, start_response): + self.environ = environ + return super().__call__(environ, start_response) + + def get_directory_loader(self, directory): + def loader(path): + site = get_site_name(xhiveframework.app._site or self.environ.get("HTTP_HOST")) + path = os.path.join(directory, site, "public", "files", cstr(path)) + if os.path.isfile(path): + return os.path.basename(path), self._opener(path) + else: + raise NotFound + # return None, None + + return loader diff --git a/xhiveframework/migrate.py b/xhiveframework/migrate.py new file mode 100644 index 0000000..a717ee8 --- /dev/null +++ b/xhiveframework/migrate.py @@ -0,0 +1,190 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import contextlib +import functools +import json +import os +from textwrap import dedent + +import xhiveframework +import xhiveframework.model.sync +import xhiveframework.modules.patch_handler +import xhiveframework.translate +from xhiveframework.cache_manager import clear_global_cache +from xhiveframework.core.doctype.language.language import sync_languages +from xhiveframework.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs +from xhiveframework.database.schema import add_column +from xhiveframework.deferred_insert import save_to_db as flush_deferred_inserts +from xhiveframework.desk.notifications import clear_notifications +from xhiveframework.modules.patch_handler import PatchType +from xhiveframework.modules.utils import sync_customizations +from xhiveframework.search.website_search import build_index_for_all_routes +from xhiveframework.utils.connections import check_connection +from xhiveframework.utils.dashboard import sync_dashboards +from xhiveframework.utils.fixtures import sync_fixtures +from xhiveframework.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 atomic(method): + @functools.wraps(method) + def wrapper(*args, **kwargs): + try: + ret = method(*args, **kwargs) + xhiveframework.db.commit() + return ret + except Exception as e: + # database itself can be gone while attempting rollback. + # We should preserve original exception in this case. + with contextlib.suppress(Exception): + xhiveframework.db.rollback() + raise e + + return wrapper + + +class SiteMigration: + """Migrate all apps to the current version, will: + - run before migrate hooks + - run patches + - sync doctypes (schema) + - sync dashboards + - sync jobs + - sync fixtures + - sync customizations + - sync languages + - sync web pages (from /www) + - run after migrate hooks + """ + + 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 + + def setUp(self): + """Complete setup required for site migration""" + xhiveframework.flags.touched_tables = set() + self.touched_tables_file = xhiveframework.get_site_path("touched_tables.json") + xhiveframework.clear_cache() + add_column(doctype="DocType", column_name="migration_hash", fieldtype="Data") + clear_global_cache() + + if os.path.exists(self.touched_tables_file): + os.remove(self.touched_tables_file) + + xhiveframework.flags.in_migrate = True + + def tearDown(self): + """Run operations that should be run post schema updation processes + This should be executed irrespective of outcome + """ + xhiveframework.translate.clear_cache() + clear_website_cache() + clear_notifications() + + with open(self.touched_tables_file, "w") as f: + json.dump(list(xhiveframework.flags.touched_tables), f, sort_keys=True, indent=4) + + if not self.skip_search_index: + print(f"Queued rebuilding of search index for {xhiveframework.local.site}") + xhiveframework.enqueue(build_index_for_all_routes, queue="long") + + xhiveframework.publish_realtime("version-update") + xhiveframework.flags.touched_tables.clear() + xhiveframework.flags.in_migrate = False + + @atomic + def pre_schema_updates(self): + """Executes `before_migrate` hooks""" + for app in xhiveframework.get_installed_apps(): + for fn in xhiveframework.get_hooks("before_migrate", app_name=app): + xhiveframework.get_attr(fn)() + + @atomic + def run_schema_updates(self): + """Run patches as defined in patches.txt, sync schema changes as defined in the {doctype}.json files""" + xhiveframework.modules.patch_handler.run_all( + skip_failing=self.skip_failing, patch_type=PatchType.pre_model_sync + ) + xhiveframework.model.sync.sync_all() + xhiveframework.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 XhiveFramework's internal language master + * Flush deferred inserts made during maintenance mode. + * Sync Portal Menu Items + * Sync Installed Applications Version History + * Execute `after_migrate` hooks + """ + sync_jobs() + sync_fixtures() + sync_dashboards() + sync_customizations() + sync_languages() + flush_deferred_inserts() + xhiveframework.model.sync.remove_orphan_doctypes() + + xhiveframework.get_single("Portal Settings").sync_menu() + xhiveframework.get_single("Installed Applications").update_versions() + + for app in xhiveframework.get_installed_apps(): + for fn in xhiveframework.get_hooks("after_migrate", app_name=app): + xhiveframework.get_attr(fn)() + + 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. + """ + from xhiveframework.utils.synchronization import filelock + + if site: + xhiveframework.init(site=site) + xhiveframework.connect() + + if not self.required_services_running(): + raise SystemExit(1) + + with filelock("bench_migrate", timeout=1): + self.setUp() + try: + self.pre_schema_updates() + self.run_schema_updates() + self.post_schema_updates() + finally: + self.tearDown() + xhiveframework.destroy() diff --git a/xhiveframework/model/__init__.py b/xhiveframework/model/__init__.py new file mode 100644 index 0000000..14e0ccb --- /dev/null +++ b/xhiveframework/model/__init__.py @@ -0,0 +1,254 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# model __init__.py +import xhiveframework +from xhiveframework import _ + +data_fieldtypes = ( + "Currency", + "Int", + "Long Int", + "Float", + "Percent", + "Check", + "Small Text", + "Long Text", + "Code", + "Text Editor", + "Markdown Editor", + "HTML Editor", + "Date", + "Datetime", + "Time", + "Text", + "Data", + "Link", + "Dynamic Link", + "Password", + "Select", + "Rating", + "Read Only", + "Attach", + "Attach Image", + "Signature", + "Color", + "Barcode", + "Geolocation", + "Duration", + "Icon", + "Phone", + "Autocomplete", + "JSON", +) + +float_like_fields = {"Float", "Currency", "Percent"} +datetime_fields = {"Datetime", "Date", "Time"} + +attachment_fieldtypes = ( + "Attach", + "Attach Image", +) + +no_value_fields = ( + "Section Break", + "Column Break", + "Tab Break", + "HTML", + "Table", + "Table MultiSelect", + "Button", + "Image", + "Fold", + "Heading", +) + +display_fieldtypes = ( + "Section Break", + "Column Break", + "Tab Break", + "HTML", + "Button", + "Image", + "Fold", + "Heading", +) + +numeric_fieldtypes = ("Currency", "Int", "Long Int", "Float", "Percent", "Check") + +data_field_options = ("Email", "Name", "Phone", "URL", "Barcode") + +default_fields = ( + "doctype", + "name", + "owner", + "creation", + "modified", + "modified_by", + "docstatus", + "idx", +) + +child_table_fields = ("parent", "parentfield", "parenttype") + +optional_fields = ("_user_tags", "_comments", "_assign", "_liked_by", "_seen") + +table_fields = ("Table", "Table MultiSelect") + +core_doctypes_list = ( + "DefaultValue", + "DocType", + "DocField", + "DocPerm", + "DocType Action", + "DocType Link", + "User", + "Role", + "Has Role", + "Page", + "Module Def", + "Print Format", + "Report", + "Customize Form", + "Customize Form Field", + "Property Setter", + "Custom Field", + "Client Script", +) + +log_types = ( + "Version", + "Error Log", + "Scheduled Job Log", + "Event Sync Log", + "Event Update Log", + "Access Log", + "View Log", + "Activity Log", + "Energy Point Log", + "Notification Log", + "Email Queue", + "DocShare", + "Document Follow", + "Console Log", +) + +std_fields = [ + {"fieldname": "name", "fieldtype": "Link", "label": "ID"}, + {"fieldname": "owner", "fieldtype": "Link", "label": "Created By", "options": "User"}, + {"fieldname": "idx", "fieldtype": "Int", "label": "Index"}, + {"fieldname": "creation", "fieldtype": "Datetime", "label": "Created On"}, + {"fieldname": "modified", "fieldtype": "Datetime", "label": "Last Updated On"}, + { + "fieldname": "modified_by", + "fieldtype": "Link", + "label": "Last Updated By", + "options": "User", + }, + {"fieldname": "_user_tags", "fieldtype": "Data", "label": "Tags"}, + {"fieldname": "_liked_by", "fieldtype": "Data", "label": "Liked By"}, + {"fieldname": "_comments", "fieldtype": "Text", "label": "Comments"}, + {"fieldname": "_assign", "fieldtype": "Text", "label": "Assigned To"}, + {"fieldname": "docstatus", "fieldtype": "Int", "label": "Document Status"}, +] + + +def delete_fields(args_dict, delete=0): + """ + Delete a field. + * Deletes record from `tabDocField` + * If not single doctype: Drops column from table + * If single, deletes record from `tabSingles` + args_dict = { dt: [field names] } + """ + import xhiveframework.utils + + for dt in args_dict: + fields = args_dict[dt] + if not fields: + continue + + xhiveframework.db.delete( + "DocField", + { + "parent": dt, + "fieldname": ("in", fields), + }, + ) + + # Delete the data/column only if delete is specified + if not delete: + continue + + if xhiveframework.db.get_value("DocType", dt, "issingle"): + xhiveframework.db.delete( + "Singles", + { + "doctype": dt, + "field": ("in", fields), + }, + ) + else: + existing_fields = xhiveframework.db.describe(dt) + existing_fields = existing_fields and [e[0] for e in existing_fields] or [] + fields_need_to_delete = set(fields) & set(existing_fields) + if not fields_need_to_delete: + continue + + if xhiveframework.db.db_type == "mariadb": + # mariadb implicitly commits before DDL, make it explicit + xhiveframework.db.commit() + + query = "ALTER TABLE `tab%s` " % dt + ", ".join( + "DROP COLUMN `%s`" % f for f in fields_need_to_delete + ) + xhiveframework.db.sql(query) + + if xhiveframework.db.db_type == "postgres": + # commit the results to db + xhiveframework.db.commit() + + +def get_permitted_fields( + doctype: str, + parenttype: str | None = None, + user: str | None = None, + permission_type: str | None = None, + *, + ignore_virtual=False, +) -> list[str]: + meta = xhiveframework.get_meta(doctype) + valid_columns = meta.get_valid_columns() + + if doctype in core_doctypes_list: + return valid_columns + + # DocType has only fields of type Table (Table, Table MultiSelect) + if set(valid_columns).issubset(default_fields): + return valid_columns + + if permission_type is None: + permission_type = "select" if xhiveframework.only_has_select_perm(doctype, user=user) else "read" + + meta_fields = meta.default_fields.copy() + optional_meta_fields = [x for x in optional_fields if x in valid_columns] + + if permitted_fields := meta.get_permitted_fieldnames( + parenttype=parenttype, + user=user, + permission_type=permission_type, + with_virtual_fields=not ignore_virtual, + ): + if permission_type == "select": + return permitted_fields + + if meta.istable: + meta_fields.extend(child_table_fields) + + return meta_fields + permitted_fields + optional_meta_fields + + return meta_fields + optional_meta_fields + + +def is_default_field(fieldname: str) -> bool: + return fieldname in default_fields diff --git a/xhiveframework/model/base_document.py b/xhiveframework/model/base_document.py new file mode 100644 index 0000000..88f54bd --- /dev/null +++ b/xhiveframework/model/base_document.py @@ -0,0 +1,1308 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import datetime +import json +import weakref +from functools import cached_property +from typing import TYPE_CHECKING, TypeVar + +import xhiveframework +from xhiveframework import _, _dict +from xhiveframework.model import ( + child_table_fields, + datetime_fields, + default_fields, + display_fieldtypes, + float_like_fields, + get_permitted_fields, + table_fields, +) +from xhiveframework.model.docstatus import DocStatus +from xhiveframework.model.naming import set_new_name +from xhiveframework.model.utils.link_count import notify_link_count +from xhiveframework.modules import load_doctype_module +from xhiveframework.utils import ( + cast_fieldtype, + cint, + compare, + cstr, + flt, + is_a_property, + now, + sanitize_html, + strip_html, +) +from xhiveframework.utils.html_utils import unescape_html + +if TYPE_CHECKING: + from xhiveframework.model.document import Document + +D = TypeVar("D", bound="Document") + + +max_positive_value = {"smallint": 2**15 - 1, "int": 2**31 - 1, "bigint": 2**63 - 1} + +DOCTYPE_TABLE_FIELDS = [ + _dict(fieldname="fields", options="DocField"), + _dict(fieldname="permissions", options="DocPerm"), + _dict(fieldname="actions", options="DocType Action"), + _dict(fieldname="links", options="DocType Link"), + _dict(fieldname="states", options="DocType State"), +] + +TABLE_DOCTYPES_FOR_DOCTYPE = {df["fieldname"]: df["options"] for df in DOCTYPE_TABLE_FIELDS} +DOCTYPES_FOR_DOCTYPE = {"DocType", *TABLE_DOCTYPES_FOR_DOCTYPE.values()} + + +def get_controller(doctype): + """ + Returns the locally cached **class** object of the given DocType. + For `custom` type, returns `xhiveframework.model.document.Document`. + + :param doctype: DocType name as string. + """ + + if xhiveframework.local.dev_server or xhiveframework.flags.in_migrate: + return import_controller(doctype) + + site_controllers = xhiveframework.controllers.setdefault(xhiveframework.local.site, {}) + if doctype not in site_controllers: + site_controllers[doctype] = import_controller(doctype) + + return site_controllers[doctype] + + +def import_controller(doctype): + from xhiveframework.model.document import Document + from xhiveframework.utils.nestedset import NestedSet + + module_name = "Core" + if doctype not in DOCTYPES_FOR_DOCTYPE: + doctype_info = xhiveframework.db.get_value("DocType", doctype, fieldname="*") + if doctype_info: + if doctype_info.custom: + return NestedSet if doctype_info.is_tree else Document + module_name = doctype_info.module + + module_path = None + class_overrides = xhiveframework.get_hooks("override_doctype_class") + if class_overrides and class_overrides.get(doctype): + import_path = class_overrides[doctype][-1] + module_path, classname = import_path.rsplit(".", 1) + module = xhiveframework.get_module(module_path) + + else: + module = load_doctype_module(doctype, module_name) + classname = doctype.replace(" ", "").replace("-", "") + + class_ = getattr(module, classname, None) + if class_ is None: + raise ImportError( + doctype + if module_path is None + else f"{doctype}: {classname} does not exist in module {module_path}" + ) + + if not issubclass(class_, BaseDocument): + raise ImportError(f"{doctype}: {classname} is not a subclass of BaseDocument") + + return class_ + + +class BaseDocument: + _reserved_keywords = frozenset( + ( + "doctype", + "meta", + "flags", + "parent_doc", + "_table_fields", + "_valid_columns", + "_doc_before_save", + "_table_fieldnames", + "_reserved_keywords", + "permitted_fieldnames", + "dont_update_if_missing", + ) + ) + + def __init__(self, d): + if d.get("doctype"): + self.doctype = d["doctype"] + + self._table_fieldnames = {df.fieldname for df in self._get_table_fields()} + self.update(d) + self.dont_update_if_missing = [] + + if hasattr(self, "__setup__"): + self.__setup__() + + @cached_property + def meta(self): + return xhiveframework.get_meta(self.doctype) + + @cached_property + def permitted_fieldnames(self): + return get_permitted_fields(doctype=self.doctype, parenttype=getattr(self, "parenttype", None)) + + def __getstate__(self): + """ + Called when pickling. + Returns a copy of `__dict__` excluding unpicklable values like `meta`. + More info: https://docs.python.org/3/library/pickle.html#handling-stateful-objects + """ + + # Always use the dict.copy() method to avoid modifying the original state + state = self.__dict__.copy() + self.remove_unpicklable_values(state) + + return state + + def remove_unpicklable_values(self, state): + """Remove unpicklable values before pickling""" + + state.pop("meta", None) + state.pop("permitted_fieldnames", None) + state.pop("_parent_doc", None) + + def update(self, d): + """Update multiple fields of a doctype using a dictionary of key-value pairs. + + Example: + doc.update({ + "user": "admin", + "balance": 42000 + }) + """ + + # set name first, as it is used a reference in child document + if "name" in d: + self.name = d["name"] + + ignore_children = hasattr(self, "flags") and self.flags.ignore_children + for key, value in d.items(): + self.set(key, value, as_value=ignore_children) + + return self + + def update_if_missing(self, d): + """Set default values for fields without existing values""" + if isinstance(d, BaseDocument): + d = d.get_valid_dict() + + for key, value in d.items(): + if ( + value is not None + and self.get(key) is None + # dont_update_if_missing is a list of fieldnames + # for which you don't want to set default value + and key not in self.dont_update_if_missing + ): + self.set(key, value) + + def get_db_value(self, key): + return xhiveframework.db.get_value(self.doctype, self.name, key) + + def get(self, key, filters=None, limit=None, default=None): + if isinstance(key, dict): + return _filter(self.get_all_children(), key, limit=limit) + + if filters: + if isinstance(filters, dict): + return _filter(self.__dict__.get(key, []), filters, limit=limit) + + # perhaps you wanted to set a default instead + default = filters + + value = self.__dict__.get(key, default) + + if limit and isinstance(value, list | tuple) and len(value) > limit: + value = value[:limit] + + return value + + def getone(self, key, filters=None): + return self.get(key, filters=filters, limit=1)[0] + + def set(self, key, value, as_value=False): + if key in self._reserved_keywords: + return + + if not as_value and key in self._table_fieldnames: + self.__dict__[key] = [] + + # if value is falsy, just init to an empty list + if value: + self.extend(key, value) + + return + + self.__dict__[key] = value + + def delete_key(self, key): + if key in self.__dict__: + del self.__dict__[key] + + def append(self, key: str, value: D | dict | None = None) -> D: + """Append an item to a child table. + + Example: + doc.append("childtable", { + "child_table_field": "value", + "child_table_int_field": 0, + ... + }) + """ + if value is None: + value = {} + + if (table := self.__dict__.get(key)) is None: + self.__dict__[key] = table = [] + + ret_value = self._init_child(value, key) + table.append(ret_value) + + # reference parent document but with weak reference, parent_doc will be deleted if self is garbage collected. + ret_value.parent_doc = weakref.ref(self) + + return ret_value + + @property + def parent_doc(self): + parent_doc_ref = getattr(self, "_parent_doc", None) + + if isinstance(parent_doc_ref, BaseDocument): + return parent_doc_ref + elif isinstance(parent_doc_ref, weakref.ReferenceType): + return parent_doc_ref() + + @parent_doc.setter + def parent_doc(self, value): + self._parent_doc = value + + @parent_doc.deleter + def parent_doc(self): + self._parent_doc = None + + def extend(self, key, value): + try: + value = iter(value) + except TypeError: + raise ValueError + + for v in value: + self.append(key, v) + + def remove(self, doc): + # Usage: from the parent doc, pass the child table doc + # to remove that child doc from the child table, thus removing it from the parent doc + if doc.get("parentfield"): + self.get(doc.parentfield).remove(doc) + + def _init_child(self, value, key): + if not isinstance(value, BaseDocument): + if not (doctype := self.get_table_field_doctype(key)): + raise AttributeError(key) + + value["doctype"] = doctype + value = get_controller(doctype)(value) + + value.parent = self.name + value.parenttype = self.doctype + value.parentfield = key + + if value.docstatus is None: + value.docstatus = DocStatus.draft() + + if not getattr(value, "idx", None): + if table := getattr(self, key, None): + value.idx = len(table) + 1 + else: + value.idx = 1 + + if not getattr(value, "name", None): + value.__dict__["__islocal"] = 1 + + return value + + def _get_table_fields(self): + """ + To get table fields during Document init + Meta.get_table_fields goes into recursion for special doctypes + """ + + if self.doctype == "DocType": + return DOCTYPE_TABLE_FIELDS + + # child tables don't have child tables + if self.doctype in DOCTYPES_FOR_DOCTYPE: + return () + + return self.meta.get_table_fields() + + def get_valid_dict( + self, sanitize=True, convert_dates_to_str=False, ignore_nulls=False, ignore_virtual=False + ) -> _dict: + d = _dict() + field_values = self.__dict__ + + for fieldname in self.meta.get_valid_columns(): + value = field_values.get(fieldname) + + # if no need for sanitization and value is None, continue + if not sanitize and value is None: + d[fieldname] = None + continue + + df = self.meta.get_field(fieldname) + is_virtual_field = getattr(df, "is_virtual", False) + + if df: + if is_virtual_field: + if ignore_virtual or fieldname not in self.permitted_fieldnames: + continue + + if (prop := getattr(type(self), fieldname, None)) and is_a_property(prop): + value = getattr(self, fieldname) + + elif options := getattr(df, "options", None): + from xhiveframework.utils.safe_exec import get_safe_globals + + value = xhiveframework.safe_eval( + code=options, + eval_globals=get_safe_globals(), + eval_locals={"doc": self}, + ) + + if isinstance(value, list) and df.fieldtype not in table_fields: + xhiveframework.throw(_("Value for {0} cannot be a list").format(_(df.label, context=df.parent))) + + if df.fieldtype == "Check": + value = 1 if cint(value) else 0 + + elif df.fieldtype == "Int" and not isinstance(value, int): + value = cint(value) + + elif df.fieldtype == "JSON" and isinstance(value, dict): + value = json.dumps(value, separators=(",", ":")) + + elif df.fieldtype in float_like_fields and not isinstance(value, float): + value = flt(value) + + elif (df.fieldtype in datetime_fields and value == "") or ( + getattr(df, "unique", False) and cstr(value).strip() == "" + ): + value = None + + if convert_dates_to_str and isinstance( + value, datetime.datetime | datetime.date | datetime.time | datetime.timedelta + ): + value = str(value) + + if ignore_nulls and not is_virtual_field and value is None: + continue + + d[fieldname] = value + + return d + + def init_child_tables(self): + """ + This is needed so that one can loop over child table properties + without worrying about whether or not they have values + """ + + for fieldname in self._table_fieldnames: + if self.__dict__.get(fieldname) is None: + self.__dict__[fieldname] = [] + + def init_valid_columns(self): + for key in default_fields: + if key not in self.__dict__: + self.__dict__[key] = None + + if self.__dict__[key] is None: + if key == "docstatus": + self.docstatus = DocStatus.draft() + elif key == "idx": + self.__dict__[key] = 0 + + for key in self.get_valid_columns(): + if key not in self.__dict__: + self.__dict__[key] = None + + def get_valid_columns(self) -> list[str]: + if self.doctype not in xhiveframework.local.valid_columns: + if self.doctype in DOCTYPES_FOR_DOCTYPE: + from xhiveframework.model.meta import get_table_columns + + valid = get_table_columns(self.doctype) + else: + valid = self.meta.get_valid_columns() + + xhiveframework.local.valid_columns[self.doctype] = valid + + return xhiveframework.local.valid_columns[self.doctype] + + def is_new(self) -> bool: + return self.get("__islocal") + + @property + def docstatus(self): + return DocStatus(cint(self.get("docstatus"))) + + @docstatus.setter + def docstatus(self, value): + self.__dict__["docstatus"] = DocStatus(cint(value)) + + def as_dict( + self, + no_nulls=False, + no_default_fields=False, + convert_dates_to_str=False, + no_child_table_fields=False, + ) -> dict: + doc = self.get_valid_dict(convert_dates_to_str=convert_dates_to_str, ignore_nulls=no_nulls) + doc["doctype"] = self.doctype + + for fieldname in self._table_fieldnames: + children = self.get(fieldname) or [] + doc[fieldname] = [ + d.as_dict( + convert_dates_to_str=convert_dates_to_str, + no_nulls=no_nulls, + no_default_fields=no_default_fields, + no_child_table_fields=no_child_table_fields, + ) + for d in children + ] + + if no_default_fields: + for key in default_fields: + if key in doc: + del doc[key] + + if no_child_table_fields: + for key in child_table_fields: + if key in doc: + del doc[key] + + for key in ( + "_user_tags", + "__islocal", + "__onload", + "_liked_by", + "__run_link_triggers", + "__unsaved", + ): + if value := getattr(self, key, None): + doc[key] = value + + return doc + + def as_json(self): + return xhiveframework.as_json(self.as_dict()) + + def get_table_field_doctype(self, fieldname): + try: + return self.meta.get_field(fieldname).options + except AttributeError: + if self.doctype == "DocType" and (table_doctype := TABLE_DOCTYPES_FOR_DOCTYPE.get(fieldname)): + return table_doctype + + raise + + def get_parentfield_of_doctype(self, doctype): + 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, 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 xhiveframework.db.db_type == "postgres": + conflict_handler = "on conflict (name) do nothing" + + if not self.creation: + self.creation = self.modified = now() + self.owner = self.modified_by = xhiveframework.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, + ignore_virtual=True, + ) + + columns = list(d) + try: + xhiveframework.db.sql( + """INSERT INTO `tab{doctype}` ({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 xhiveframework.db.is_primary_key_violation(e): + if self.meta.autoname == "hash": + # hash collision? try again + xhiveframework.flags.retry_count = (xhiveframework.flags.retry_count or 0) + 1 + if xhiveframework.flags.retry_count > 5 and not xhiveframework.flags.in_test: + raise + self.name = None + self.db_insert() + return + + if not ignore_if_duplicate: + xhiveframework.msgprint( + _("{0} {1} already exists").format(_(self.doctype), xhiveframework.bold(self.name)), + title=_("Duplicate Name"), + indicator="red", + ) + raise xhiveframework.DuplicateEntryError(self.doctype, self.name, e) + + elif xhiveframework.db.is_unique_key_violation(e): + # unique constraint + self.show_unique_validation_message(e) + + else: + raise + + self.set("__islocal", False) + + def db_update(self): + if self.get("__islocal") or not self.name: + self.db_insert() + return + + d = self.get_valid_dict( + convert_dates_to_str=True, + ignore_nulls=self.doctype in DOCTYPES_FOR_DOCTYPE, + ignore_virtual=True, + ) + + # don't update name, as case might've been changed + name = cstr(d["name"]) + del d["name"] + + columns = list(d) + + try: + xhiveframework.db.sql( + """UPDATE `tab{doctype}` + SET {values} WHERE `name`=%s""".format( + doctype=self.doctype, values=", ".join("`" + c + "`=%s" for c in columns) + ), + [*list(d.values()), name], + ) + except Exception as e: + if xhiveframework.db.is_unique_key_violation(e): + self.show_unique_validation_message(e) + else: + raise + + def db_update_all(self): + """Raw update parent + children + DOES NOT VALIDATE AND CALL TRIGGERS""" + self.db_update() + for fieldname in self._table_fieldnames: + for doc in self.get(fieldname): + doc.db_update() + + def show_unique_validation_message(self, e): + if xhiveframework.db.db_type != "postgres": + fieldname = str(e).split("'")[-2] + label = None + + # MariaDB gives key_name in error. Extracting fieldname from key name + try: + fieldname = self.get_field_name_by_key_name(fieldname) + except IndexError: + pass + + label = self.get_label_from_fieldname(fieldname) + + xhiveframework.msgprint(_("{0} must be unique").format(label or fieldname)) + + # this is used to preserve traceback + raise xhiveframework.UniqueValidationError(self.doctype, self.name, e) + + def get_field_name_by_key_name(self, key_name): + """MariaDB stores a mapping between `key_name` and `column_name`. + This function returns the `column_name` associated with the `key_name` passed + + Args: + key_name (str): The name of the database index. + + Raises: + IndexError: If the key is not found in the table. + + Returns: + str: The column name associated with the key. + """ + return xhiveframework.db.sql( + f""" + SHOW + INDEX + FROM + `tab{self.doctype}` + WHERE + key_name=%s + AND + Non_unique=0 + """, + key_name, + as_dict=True, + )[0].get("Column_name") + + def get_label_from_fieldname(self, fieldname): + """Returns the associated label for fieldname + + Args: + fieldname (str): The fieldname in the DocType to use to pull the label. + + Returns: + str: The label associated with the fieldname, if found, otherwise `None`. + """ + df = self.meta.get_field(fieldname) + if df: + return df.label + + def update_modified(self): + """Update modified timestamp""" + self.set("modified", now()) + if getattr(self.meta, "issingle", False): + xhiveframework.db.set_single_value(self.doctype, "modified", self.modified, update_modified=False) + else: + xhiveframework.db.set_value(self.doctype, self.name, "modified", self.modified, update_modified=False) + + def _fix_numeric_types(self): + for df in self.meta.get("fields"): + if df.fieldtype == "Check": + self.set(df.fieldname, cint(self.get(df.fieldname))) + + elif self.get(df.fieldname) is not None: + if df.fieldtype == "Int": + self.set(df.fieldname, cint(self.get(df.fieldname))) + + elif df.fieldtype in ("Float", "Currency", "Percent"): + self.set(df.fieldname, flt(self.get(df.fieldname))) + + if self.docstatus is not None: + self.docstatus = DocStatus(cint(self.docstatus)) + + def _get_missing_mandatory_fields(self): + """Get mandatory fields that do not have any values""" + + def get_msg(df): + if df.fieldtype in table_fields: + return "{}: {}: {}".format( + _("Error"), _("Data missing in table"), _(df.label, context=df.parent) + ) + + # check if parentfield exists (only applicable for child table doctype) + elif self.get("parentfield"): + return "{}: {} {} #{}: {}: {}".format( + _("Error"), + xhiveframework.bold(_(self.doctype)), + _("Row"), + self.idx, + _("Value missing for"), + _(df.label, context=df.parent), + ) + + return _("Error: Value missing for {0}: {1}").format(_(df.parent), _(df.label, context=df.parent)) + + def has_content(df): + value = cstr(self.get(df.fieldname)) + has_text_content = strip_html(value).strip() + has_img_tag = " max_length: + self.throw_length_exceeded_error(df, max_length, value) + + elif column_type in ("int", "bigint", "smallint"): + max_length = max_positive_value[column_type] + + if abs(cint(value)) > max_length: + self.throw_length_exceeded_error(df, max_length, value) + + def _validate_code_fields(self): + for field in self.meta.get_code_fields(): + code_string = self.get(field.fieldname) + language = field.get("options") + + if language == "Python": + xhiveframework.utils.validate_python_code(code_string, fieldname=field.label, is_expression=False) + + elif language == "PythonExpression": + xhiveframework.utils.validate_python_code(code_string, fieldname=field.label) + + def _sync_autoname_field(self): + """Keep autoname field in sync with `name`""" + autoname = self.meta.autoname or "" + _empty, _field_specifier, fieldname = autoname.partition("field:") + + if fieldname and self.name and self.name != self.get(fieldname): + self.set(fieldname, self.name) + + def throw_length_exceeded_error(self, df, max_length, value): + # check if parentfield exists (only applicable for child table doctype) + if self.get("parentfield"): + reference = _("{0}, Row {1}").format(_(self.doctype), self.idx) + else: + reference = f"{_(self.doctype)} {self.name}" + + xhiveframework.throw( + _("{0}: '{1}' ({3}) will get truncated, as max characters allowed is {2}").format( + reference, _(df.label, context=df.parent), max_length, value + ), + xhiveframework.CharacterLengthExceededError, + title=_("Value too big"), + ) + + def _validate_update_after_submit(self): + # get the full doc with children + db_values = xhiveframework.get_doc(self.doctype, self.name).as_dict() + + for key in self.as_dict(): + df = self.meta.get_field(key) + db_value = db_values.get(key) + + if df and not df.allow_on_submit and (self.get(key) or db_value): + if df.fieldtype in table_fields: + # just check if the table size has changed + # individual fields will be checked in the loop for children + self_value = len(self.get(key)) + db_value = len(db_value) + + else: + self_value = self.get_value(key) + # Postgres stores values as `datetime.time`, MariaDB as `timedelta` + if isinstance(self_value, datetime.timedelta) and isinstance(db_value, datetime.time): + db_value = datetime.timedelta( + hours=db_value.hour, + minutes=db_value.minute, + seconds=db_value.second, + microseconds=db_value.microsecond, + ) + if self_value != db_value: + xhiveframework.throw( + _("{0} Not allowed to change {1} after submission from {2} to {3}").format( + f"Row #{self.idx}:" if self.get("parent") else "", + xhiveframework.bold(_(df.label, context=df.parent)), + xhiveframework.bold(db_value), + xhiveframework.bold(self_value), + ), + xhiveframework.UpdateAfterSubmitError, + title=_("Cannot Update After Submit"), + ) + + def _sanitize_content(self): + """Sanitize HTML and Email in field values. Used to prevent XSS. + + - Ignore if 'Ignore XSS Filter' is checked or fieldtype is 'Code' + """ + from bs4 import BeautifulSoup + + if xhiveframework.flags.in_install: + return + + for fieldname, value in self.get_valid_dict(ignore_virtual=True).items(): + if not value or not isinstance(value, str): + continue + + value = xhiveframework.as_unicode(value) + + if "<" not in value and ">" not in value: + # doesn't look like html so no need + continue + + elif "" in value and not bool(BeautifulSoup(value, "html.parser").find()): + # should be handled separately via the markdown converter function + continue + + df = self.meta.get_field(fieldname) + sanitized_value = value + + if df and ( + df.get("ignore_xss_filter") + or (df.get("fieldtype") in ("Data", "Small Text", "Text") and df.get("options") == "Email") + or df.get("fieldtype") in ("Attach", "Attach Image", "Barcode", "Code") + # cancelled and submit but not update after submit should be ignored + or self.docstatus.is_cancelled() + or (self.docstatus.is_submitted() and not df.get("allow_on_submit")) + ): + continue + + else: + sanitized_value = sanitize_html(value, linkify=df and df.fieldtype == "Text Editor") + + self.set(fieldname, sanitized_value) + + def _save_passwords(self): + """Save password field values in __Auth table""" + from xhiveframework.utils.password import remove_encrypted_password, set_encrypted_password + + if self.flags.ignore_save_passwords is True: + return + + for df in self.meta.get("fields", {"fieldtype": ("=", "Password")}): + if self.flags.ignore_save_passwords and df.fieldname in self.flags.ignore_save_passwords: + continue + new_password = self.get(df.fieldname) + + if not new_password: + remove_encrypted_password(self.doctype, self.name, df.fieldname) + + if new_password and not self.is_dummy_password(new_password): + # is not a dummy password like '*****' + set_encrypted_password(self.doctype, self.name, new_password, df.fieldname) + + # set dummy password like '*****' + self.set(df.fieldname, "*" * len(new_password)) + + def get_password(self, fieldname="password", raise_exception=True): + from xhiveframework.utils.password import get_decrypted_password + + if self.get(fieldname) and not self.is_dummy_password(self.get(fieldname)): + return self.get(fieldname) + + return get_decrypted_password(self.doctype, self.name, fieldname, raise_exception=raise_exception) + + def is_dummy_password(self, pwd): + return "".join(set(pwd)) == "*" + + def precision(self, fieldname, parentfield=None) -> int | None: + """Returns float precision for a particular field (or get global default). + + :param fieldname: Fieldname for which precision is required. + :param parentfield: If fieldname is in child table.""" + from xhiveframework.model.meta import get_field_precision + + if parentfield and not isinstance(parentfield, str) and parentfield.get("parentfield"): + parentfield = parentfield.parentfield + + cache_key = parentfield or "main" + + if not hasattr(self, "_precision"): + self._precision = _dict() + + if cache_key not in self._precision: + self._precision[cache_key] = _dict() + + if fieldname not in self._precision[cache_key]: + self._precision[cache_key][fieldname] = None + + doctype = self.meta.get_field(parentfield).options if parentfield else self.doctype + df = xhiveframework.get_meta(doctype).get_field(fieldname) + + if df.fieldtype in ("Currency", "Float", "Percent"): + self._precision[cache_key][fieldname] = get_field_precision(df, self) + + return self._precision[cache_key][fieldname] + + def get_formatted( + self, fieldname, doc=None, currency=None, absolute_value=False, translated=False, format=None + ): + from xhiveframework.utils.formatters import format_value + + df = self.meta.get_field(fieldname) + if not df: + from xhiveframework.model.meta import get_default_df + + df = get_default_df(fieldname) + + if ( + df + and df.fieldtype == "Currency" + and not currency + and (currency_field := df.get("options")) + and (currency_value := self.get(currency_field)) + ): + currency = xhiveframework.db.get_value("Currency", currency_value, cache=True) + + val = self.get(fieldname) + + if translated: + val = _(val) + + if not doc: + doc = getattr(self, "parent_doc", None) or self + + if (absolute_value or doc.get("absolute_value")) and isinstance(val, int | float): + val = abs(self.get(fieldname)) + + return format_value(val, df=df, doc=doc, currency=currency, format=format) + + def is_print_hide(self, fieldname, df=None, for_print=True): + """Returns true if fieldname is to be hidden for print. + + Print Hide can be set via the Print Format Builder or in the controller as a list + of hidden fields. Example + + class MyDoc(Document): + def __setup__(self): + self.print_hide = ["field1", "field2"] + + :param fieldname: Fieldname to be checked if hidden. + """ + meta_df = self.meta.get_field(fieldname) + if meta_df and meta_df.get("__print_hide"): + return True + + print_hide = 0 + + if self.get(fieldname) == 0 and not self.meta.istable: + print_hide = (df and df.print_hide_if_no_value) or (meta_df and meta_df.print_hide_if_no_value) + + if not print_hide: + if df and df.print_hide is not None: + print_hide = df.print_hide + elif meta_df: + print_hide = meta_df.print_hide + + return print_hide + + def in_format_data(self, fieldname): + """Returns True if shown via Print Format::`format_data` property. + Called from within standard print format.""" + doc = getattr(self, "parent_doc", self) + + if hasattr(doc, "format_data_map"): + return fieldname in doc.format_data_map + else: + return True + + def reset_values_if_no_permlevel_access(self, has_access_to, high_permlevel_fields): + """If the user does not have permissions at permlevel > 0, then reset the values to original / default""" + to_reset = [ + df + for df in high_permlevel_fields + if ( + df.permlevel not in has_access_to + and df.fieldtype not in display_fieldtypes + and df.fieldname not in self.flags.get("ignore_permlevel_for_fields", []) + ) + ] + + if to_reset: + if self.is_new(): + # if new, set default value + ref_doc = xhiveframework.new_doc(self.doctype) + else: + # get values from old doc + if self.parent_doc: + parent_doc = self.parent_doc.get_latest() + child_docs = [d for d in parent_doc.get(self.parentfield) if d.name == self.name] + if not child_docs: + return + ref_doc = child_docs[0] + else: + ref_doc = self.get_latest() + + for df in to_reset: + self.set(df.fieldname, ref_doc.get(df.fieldname)) + + def get_value(self, fieldname): + df = self.meta.get_field(fieldname) + val = self.get(fieldname) + + return self.cast(val, df) + + def cast(self, value, df): + return cast_fieldtype(df.fieldtype, value, show_warning=False) + + def _extract_images_from_text_editor(self): + from xhiveframework.core.doctype.file.utils import extract_images_from_doc + + if self.doctype != "DocType": + for df in self.meta.get("fields", {"fieldtype": ("=", "Text Editor")}): + extract_images_from_doc(self, df.fieldname) + + +def _filter(data, filters, limit=None): + """pass filters as: + {"key": "val", "key": ["!=", "val"], + "key": ["in", "val"], "key": ["not in", "val"], "key": "^val", + "key" : True (exists), "key": False (does not exist) }""" + + out, _filters = [], {} + + if not data: + return out + + # setup filters as tuples + if filters: + for f in filters: + fval = filters[f] + + if not isinstance(fval, tuple | list): + if fval is True: + fval = ("not None", fval) + elif fval is False: + fval = ("None", fval) + elif isinstance(fval, str) and fval.startswith("^"): + fval = ("^", fval[1:]) + else: + fval = ("=", fval) + + _filters[f] = fval + + for d in data: + for f, fval in _filters.items(): + if not compare(getattr(d, f, None), fval[0], fval[1]): + break + else: + out.append(d) + if limit and len(out) >= limit: + break + + return out diff --git a/xhiveframework/model/create_new.py b/xhiveframework/model/create_new.py new file mode 100644 index 0000000..5134770 --- /dev/null +++ b/xhiveframework/model/create_new.py @@ -0,0 +1,182 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +""" +Create a new document with defaults set +""" + +import copy + +import xhiveframework +import xhiveframework.defaults +from xhiveframework.core.doctype.user_permission.user_permission import get_user_permissions +from xhiveframework.model import data_fieldtypes +from xhiveframework.permissions import filter_allowed_docs_for_doctype +from xhiveframework.utils import cstr, now_datetime, nowdate, nowtime + + +def get_new_doc(doctype, parent_doc=None, parentfield=None, as_dict=False): + if doctype not in xhiveframework.local.new_doc_templates: + # cache a copy of new doc as it is called + # frequently for inserts + xhiveframework.local.new_doc_templates[doctype] = make_new_doc(doctype) + + doc = copy.deepcopy(xhiveframework.local.new_doc_templates[doctype]) + + set_dynamic_default_values(doc, parent_doc, parentfield) + + if as_dict: + return doc + else: + return xhiveframework.get_doc(doc) + + +def make_new_doc(doctype): + doc = xhiveframework.get_doc({"doctype": doctype, "__islocal": 1, "owner": xhiveframework.session.user, "docstatus": 0}) + + set_user_and_static_default_values(doc) + + doc._fix_numeric_types() + doc = doc.get_valid_dict(sanitize=False) + doc["doctype"] = doctype + doc["__islocal"] = 1 + + if not xhiveframework.model.meta.is_single(doctype): + doc["__unsaved"] = 1 + + return doc + + +def set_user_and_static_default_values(doc): + user_permissions = get_user_permissions() + defaults = xhiveframework.defaults.get_defaults() + + for df in doc.meta.get("fields"): + if df.fieldtype in data_fieldtypes: + # user permissions for link options + doctype_user_permissions = user_permissions.get(df.options, []) + # Allowed records for the reference doctype (link field) along with default doc + allowed_records, default_doc = filter_allowed_docs_for_doctype( + doctype_user_permissions, df.parent, with_default_doc=True + ) + + user_default_value = get_user_default_value( + df, defaults, doctype_user_permissions, allowed_records, default_doc + ) + if user_default_value is not None: + # if fieldtype is link check if doc exists + if not df.fieldtype == "Link" or xhiveframework.db.exists(df.options, user_default_value): + doc.set(df.fieldname, user_default_value) + + else: + if df.fieldname != doc.meta.title_field: + static_default_value = get_static_default_value( + df, doctype_user_permissions, allowed_records + ) + if static_default_value is not None: + doc.set(df.fieldname, static_default_value) + + +def get_user_default_value(df, defaults, doctype_user_permissions, allowed_records, default_doc): + # don't set defaults for "User" link field using User Permissions! + if df.fieldtype == "Link" and df.options != "User": + # If user permission has Is Default enabled or single-user permission has found against respective doctype. + if not df.ignore_user_permissions and default_doc: + return default_doc + + # 2 - Look in user defaults + user_default = defaults.get(df.fieldname) + + allowed_by_user_permission = validate_value_via_user_permissions( + df, doctype_user_permissions, allowed_records, user_default=user_default + ) + + # is this user default also allowed as per user permissions? + if user_default and allowed_by_user_permission: + return user_default + + +def get_static_default_value(df, doctype_user_permissions, allowed_records): + # 3 - look in default of docfield + if df.get("default"): + if df.default == "__user": + return xhiveframework.session.user + + elif df.default == "Today": + return nowdate() + + elif not cstr(df.default).startswith(":"): + # a simple default value + is_allowed_default_value = validate_value_via_user_permissions( + df, doctype_user_permissions, allowed_records + ) + + if df.fieldtype != "Link" or df.options == "User" or is_allowed_default_value: + return df.default + + elif df.fieldtype == "Select" and df.options and df.options not in ("[Select]", "Loading..."): + return df.options.split("\n", 1)[0] + + +def validate_value_via_user_permissions(df, doctype_user_permissions, allowed_records, user_default=None): + is_valid = True + # If User Permission exists and allowed records is empty, + # that means there are User Perms, but none applicable to this new doctype. + + if user_permissions_exist(df, doctype_user_permissions) and allowed_records: + # If allowed records is not empty, + # check if this field value is allowed via User Permissions applied to this doctype. + value = user_default if user_default else df.default + is_valid = value in allowed_records + + return is_valid + + +def set_dynamic_default_values(doc, parent_doc, parentfield): + # these values should not be cached + user_permissions = get_user_permissions() + + for df in xhiveframework.get_meta(doc["doctype"]).get("fields"): + if df.get("default"): + if cstr(df.default).startswith(":"): + default_value = get_default_based_on_another_field(df, user_permissions, parent_doc) + if default_value is not None and not doc.get(df.fieldname): + doc[df.fieldname] = default_value + + elif df.fieldtype == "Datetime" and df.default.lower() == "now": + doc[df.fieldname] = now_datetime() + + if df.fieldtype == "Time": + doc[df.fieldname] = nowtime() + + if parent_doc: + doc["parent"] = parent_doc.name + doc["parenttype"] = parent_doc.doctype + + if parentfield: + doc["parentfield"] = parentfield + + +def user_permissions_exist(df, doctype_user_permissions): + return ( + df.fieldtype == "Link" + and not getattr(df, "ignore_user_permissions", False) + and doctype_user_permissions + ) + + +def get_default_based_on_another_field(df, user_permissions, parent_doc): + # default value based on another document + from xhiveframework.permissions import get_allowed_docs_for_doctype + + ref_doctype = df.default[1:] + ref_fieldname = ref_doctype.lower().replace(" ", "_") + reference_name = parent_doc.get(ref_fieldname) if parent_doc else xhiveframework.db.get_default(ref_fieldname) + default_value = xhiveframework.db.get_value(ref_doctype, reference_name, df.fieldname) + is_allowed_default_value = not user_permissions_exist(df, user_permissions.get(df.options)) or ( + default_value in get_allowed_docs_for_doctype(user_permissions[df.options], df.parent) + ) + + # is this allowed as per user permissions + if is_allowed_default_value: + return default_value diff --git a/xhiveframework/model/db_query.py b/xhiveframework/model/db_query.py new file mode 100644 index 0000000..5e32a9a --- /dev/null +++ b/xhiveframework/model/db_query.py @@ -0,0 +1,1380 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +"""build query for doclistview and return results""" + +import copy +import datetime +import json +import re +from collections import Counter + +import xhiveframework +import xhiveframework.defaults +import xhiveframework.permissions +import xhiveframework.share +from xhiveframework import _ +from xhiveframework.core.doctype.server_script.server_script_utils import get_server_script_map +from xhiveframework.database.utils import DefaultOrderBy, FallBackDateTimeStr, NestedSetHierarchy +from xhiveframework.model import get_permitted_fields, optional_fields +from xhiveframework.model.meta import get_table_columns +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.model.utils.user_settings import get_user_settings, update_user_settings +from xhiveframework.query_builder.utils import Column +from xhiveframework.utils import ( + cint, + cstr, + flt, + get_filter, + get_time, + get_timespan_date_range, + make_filter_tuple, +) +from xhiveframework.utils.data import DateTimeLikeObject, get_datetime, getdate, sbool + +LOCATE_PATTERN = re.compile(r"locate\([^,]+,\s*[`\"]?name[`\"]?\s*\)", flags=re.IGNORECASE) +LOCATE_CAST_PATTERN = re.compile(r"locate\(([^,]+),\s*([`\"]?name[`\"]?)\s*\)", flags=re.IGNORECASE) +FUNC_IFNULL_PATTERN = re.compile(r"(strpos|ifnull|coalesce)\(\s*[`\"]?name[`\"]?\s*,", flags=re.IGNORECASE) +CAST_VARCHAR_PATTERN = re.compile(r"([`\"]?tab[\w`\" -]+\.[`\"]?name[`\"]?)(?!\w)", flags=re.IGNORECASE) +ORDER_BY_PATTERN = re.compile(r"\ order\ by\ |\ asc|\ ASC|\ desc|\ DESC", flags=re.IGNORECASE) +SUB_QUERY_PATTERN = re.compile("^.*[,();@].*") +IS_QUERY_PATTERN = re.compile(r"^(select|delete|update|drop|create)\s") +IS_QUERY_PREDICATE_PATTERN = re.compile(r"\s*[0-9a-zA-z]*\s*( from | group by | order by | where | join )") +FIELD_QUOTE_PATTERN = re.compile(r"[0-9a-zA-Z]+\s*'") +FIELD_COMMA_PATTERN = re.compile(r"[0-9a-zA-Z]+\s*,") +STRICT_FIELD_PATTERN = re.compile(r".*/\*.*") +STRICT_UNION_PATTERN = re.compile(r".*\s(union).*\s") +ORDER_GROUP_PATTERN = re.compile(r".*[^a-z0-9-_ ,`'\"\.\(\)].*") +FN_PARAMS_PATTERN = re.compile(r".*?\((.*)\).*") +SPECIAL_FIELD_CHARS = frozenset(("(", "`", ".", "'", '"', "*")) + + +class DatabaseQuery: + def __init__(self, doctype, user=None): + self.doctype = doctype + self.tables = [] + self.link_tables = [] + self.linked_table_aliases = {} + self.linked_table_counter = Counter() + self.conditions = [] + self.or_conditions = [] + self.fields = None + self.user = user or xhiveframework.session.user + self.ignore_ifnull = False + self.flags = xhiveframework._dict() + self.reference_doctype = None + self.permission_map = {} + self.shared = [] + self._fetch_shared_documents = False + + @property + def doctype_meta(self): + if not hasattr(self, "_doctype_meta"): + self._doctype_meta = xhiveframework.get_meta(self.doctype) + return self._doctype_meta + + @property + def query_tables(self): + return self.tables + [d.table_alias for d in self.link_tables] + + def execute( + self, + fields=None, + filters=None, + or_filters=None, + docstatus=None, + group_by=None, + order_by=DefaultOrderBy, + limit_start=False, + limit_page_length=None, + as_list=False, + with_childnames=False, + debug=False, + ignore_permissions=False, + user=None, + with_comment_count=False, + join="left join", + distinct=False, + start=None, + page_length=None, + limit=None, + ignore_ifnull=False, + save_user_settings=False, + save_user_settings_fields=False, + update=None, + add_total_row=None, + user_settings=None, + reference_doctype=None, + run=True, + strict=True, + pluck=None, + ignore_ddl=False, + *, + parent_doctype=None, + ) -> list: + if not ignore_permissions: + self.check_read_permission(self.doctype, parent_doctype=parent_doctype) + + # filters and fields swappable + # its hard to remember what comes first + if isinstance(fields, dict) or (fields and isinstance(fields, list) and isinstance(fields[0], list)): + # if fields is given as dict/list of list, its probably filters + filters, fields = fields, filters + + elif fields and isinstance(filters, list) and len(filters) > 1 and isinstance(filters[0], str): + # if `filters` is a list of strings, its probably fields + filters, fields = fields, filters + + if fields: + self.fields = fields + else: + self.fields = [f"`tab{self.doctype}`.`{pluck or 'name'}`"] + + if start: + limit_start = start + if page_length: + limit_page_length = page_length + if limit: + limit_page_length = limit + + self.filters = filters or [] + self.or_filters = or_filters or [] + self.docstatus = docstatus or [] + self.group_by = group_by + self.order_by = order_by + self.limit_start = cint(limit_start) + self.limit_page_length = cint(limit_page_length) if limit_page_length else None + self.with_childnames = with_childnames + self.debug = debug + self.join = join + self.distinct = distinct + self.as_list = as_list + self.ignore_ifnull = ignore_ifnull + self.flags.ignore_permissions = ignore_permissions + self.user = user or xhiveframework.session.user + self.update = update + self.user_settings_fields = copy.deepcopy(self.fields) + self.run = run + self.strict = strict + self.ignore_ddl = ignore_ddl + self.parent_doctype = parent_doctype + + # for contextual user permission check + # to determine which user permission is applicable on link field of specific doctype + self.reference_doctype = reference_doctype or self.doctype + + if user_settings: + self.user_settings = json.loads(user_settings) + + if is_virtual_doctype(self.doctype): + from xhiveframework.model.base_document import get_controller + + controller = get_controller(self.doctype) + if not hasattr(controller, "get_list"): + return [] + + self.parse_args() + kwargs = { + "as_list": as_list, + "with_comment_count": with_comment_count, + "save_user_settings": save_user_settings, + "save_user_settings_fields": save_user_settings_fields, + "pluck": pluck, + "parent_doctype": parent_doctype, + } | self.__dict__ + return controller.get_list(kwargs) + + self.columns = self.get_table_columns() + + # no table & ignore_ddl, return + if not self.columns: + return [] + + result = self.build_and_run() + + if sbool(with_comment_count) and not as_list and self.doctype: + self.add_comment_count(result) + + if save_user_settings: + self.save_user_settings_fields = save_user_settings_fields + self.update_user_settings() + + if pluck: + return [d[pluck] for d in result] + + return result + + def build_and_run(self): + args = self.prepare_args() + args.limit = self.add_limit() + + if not args.fields: + # apply_fieldlevel_read_permissions has likely removed ALL the fields that user asked for + return [] + + if args.conditions: + args.conditions = "where " + args.conditions + + if self.distinct: + args.fields = "distinct " + args.fields + args.order_by = "" # TODO: recheck for alternative + + # Postgres requires any field that appears in the select clause to also + # appear in the order by and group by clause + if xhiveframework.db.db_type == "postgres" and args.order_by and args.group_by: + args = self.prepare_select_args(args) + + query = """select {fields} + from {tables} + {conditions} + {group_by} + {order_by} + {limit}""".format(**args) + + return xhiveframework.db.sql( + query, + as_dict=not self.as_list, + debug=self.debug, + update=self.update, + ignore_ddl=self.ignore_ddl, + run=self.run, + ) + + def prepare_args(self): + self.parse_args() + self.sanitize_fields() + self.extract_tables() + self.set_optional_columns() + self.build_conditions() + self.apply_fieldlevel_read_permissions() + + args = xhiveframework._dict() + + if self.with_childnames: + for t in self.tables: + if t != f"`tab{self.doctype}`": + self.fields.append(f"{t}.name as '{t[4:-1]}:name'") + + # query dict + args.tables = self.tables[0] + + # left join parent, child tables + for child in self.tables[1:]: + parent_name = cast_name(f"{self.tables[0]}.name") + args.tables += f" {self.join} {child} on ({child}.parenttype = {xhiveframework.db.escape(self.doctype)} and {child}.parent = {parent_name})" + + # left join link tables + for link in self.link_tables: + args.tables += f" {self.join} {link.table_name} {link.table_alias} on ({link.table_alias}.`name` = {self.tables[0]}.`{link.fieldname}`)" + + if self.grouped_or_conditions: + self.conditions.append(f"({' or '.join(self.grouped_or_conditions)})") + + args.conditions = " and ".join(self.conditions) + + if self.or_conditions: + args.conditions += (" or " if args.conditions else "") + " or ".join(self.or_conditions) + + self.set_field_tables() + self.cast_name_fields() + + fields = [] + + # Wrapping fields with grave quotes to allow support for sql keywords + # TODO: Add support for wrapping fields with sql functions and distinct keyword + for field in self.fields: + if field is None: + fields.append("NULL") + continue + + stripped_field = field.strip().lower() + + if ( + stripped_field[0] in {"`", "*", '"', "'"} + or "(" in stripped_field + or "distinct" in stripped_field + ): + fields.append(field) + elif "as" in stripped_field.split(" "): + col, _, new = field.split() + fields.append(f"`{col}` as {new}") + else: + fields.append(f"`{field}`") + + args.fields = ", ".join(fields) + + self.set_order_by(args) + + self.validate_order_by_and_group_by(args.order_by) + args.order_by = args.order_by and (" order by " + args.order_by) or "" + + self.validate_order_by_and_group_by(self.group_by) + args.group_by = self.group_by and (" group by " + self.group_by) or "" + + return args + + def prepare_select_args(self, args): + order_field = ORDER_BY_PATTERN.sub("", args.order_by) + + if order_field not in args.fields: + extracted_column = order_column = order_field.replace("`", "") + if "." in extracted_column: + extracted_column = extracted_column.split(".")[1] + + args.fields += f", MAX({extracted_column}) as `{order_column}`" + args.order_by = args.order_by.replace(order_field, f"`{order_column}`") + + return args + + def parse_args(self): + """Convert fields and filters from strings to list, dicts""" + if isinstance(self.fields, str): + if self.fields == "*": + self.fields = ["*"] + else: + try: + self.fields = json.loads(self.fields) + except ValueError: + self.fields = [f.strip() for f in self.fields.split(",")] + + # remove empty strings / nulls in fields + self.fields = [f for f in self.fields if f] + + # convert child_table.fieldname to `tabChild DocType`.`fieldname` + for field in self.fields: + if "." in field: + original_field = field + alias = None + if " as " in field: + field, alias = field.split(" as ", 1) + linked_fieldname, fieldname = field.split(".", 1) + linked_field = xhiveframework.get_meta(self.doctype).get_field(linked_fieldname) + # this is not a link field + if not linked_field: + continue + linked_doctype = linked_field.options + if linked_field.fieldtype == "Link": + linked_table = self.append_link_table(linked_doctype, linked_fieldname) + field = f"{linked_table.table_alias}.`{fieldname}`" + else: + field = f"`tab{linked_doctype}`.`{fieldname}`" + if alias: + field = f"{field} as {alias}" + self.fields[self.fields.index(original_field)] = field + + for filter_name in ["filters", "or_filters"]: + filters = getattr(self, filter_name) + if isinstance(filters, str): + filters = json.loads(filters) + + if isinstance(filters, dict): + fdict = filters + filters = [make_filter_tuple(self.doctype, key, value) for key, value in fdict.items()] + setattr(self, filter_name, filters) + + def sanitize_fields(self): + """ + regex : ^.*[,();].* + purpose : The regex will look for malicious patterns like `,`, '(', ')', '@', ;' in each + field which may leads to sql injection. + example : + field = "`DocType`.`issingle`, version()" + As field contains `,` and mysql function `version()`, with the help of regex + the system will filter out this field. + """ + blacklisted_keywords = ["select", "create", "insert", "delete", "drop", "update", "case", "show"] + blacklisted_functions = [ + "concat", + "concat_ws", + "if", + "ifnull", + "nullif", + "coalesce", + "connection_id", + "current_user", + "database", + "last_insert_id", + "session_user", + "system_user", + "user", + "version", + "global", + ] + + def _raise_exception(): + xhiveframework.throw(_("Use of sub-query or function is restricted"), xhiveframework.DataError) + + def _is_query(field): + if IS_QUERY_PATTERN.match(field): + _raise_exception() + + elif IS_QUERY_PREDICATE_PATTERN.match(field): + _raise_exception() + + for field in self.fields: + lower_field = field.lower().strip() + + if SUB_QUERY_PATTERN.match(field): + if lower_field[0] == "(": + subquery_token = lower_field[1:].lstrip().split(" ", 1)[0] + if subquery_token in blacklisted_keywords: + _raise_exception() + + function = lower_field.split("(", 1)[0].rstrip() + if function in blacklisted_functions: + xhiveframework.throw( + _("Use of function {0} in field is restricted").format(function), exc=xhiveframework.DataError + ) + + if "@" in lower_field: + # prevent access to global variables + _raise_exception() + + if FIELD_QUOTE_PATTERN.match(field): + _raise_exception() + + if FIELD_COMMA_PATTERN.match(field): + _raise_exception() + + _is_query(field) + + if self.strict: + if STRICT_FIELD_PATTERN.match(field): + xhiveframework.throw(_("Illegal SQL Query")) + + if STRICT_UNION_PATTERN.match(lower_field): + xhiveframework.throw(_("Illegal SQL Query")) + + def extract_tables(self): + """extract tables from fields""" + self.tables = [f"`tab{self.doctype}`"] + sql_functions = [ + "dayofyear(", + "extract(", + "locate(", + "strpos(", + "count(", + "sum(", + "avg(", + ] + # add tables from fields + if self.fields: + for field in self.fields: + if not ("tab" in field and "." in field) or any(x for x in sql_functions if x in field): + continue + + table_name = field.split(".", 1)[0] + + # Check if table_name is a linked_table alias + for linked_table in self.link_tables: + if linked_table.table_alias == table_name: + table_name = linked_table.table_name + break + + if table_name.lower().startswith("group_concat("): + table_name = table_name[13:] + if table_name.lower().startswith("distinct"): + table_name = table_name[8:].strip() + if table_name[0] != "`": + table_name = f"`{table_name}`" + if ( + table_name not in self.query_tables + and table_name not in self.linked_table_aliases.values() + ): + self.append_table(table_name) + + def append_table(self, table_name): + self.tables.append(table_name) + doctype = table_name[4:-1] + self.check_read_permission(doctype) + + def append_link_table(self, doctype, fieldname): + for linked_table in self.link_tables: + if linked_table.doctype == doctype and linked_table.fieldname == fieldname: + return linked_table + + self.check_read_permission(doctype) + self.linked_table_counter.update((doctype,)) + linked_table = xhiveframework._dict( + doctype=doctype, + fieldname=fieldname, + table_name=f"`tab{doctype}`", + table_alias=f"`tab{doctype}_{self.linked_table_counter[doctype]}`", + ) + self.linked_table_aliases[linked_table.table_alias.replace("`", "")] = linked_table.table_name + self.link_tables.append(linked_table) + return linked_table + + def check_read_permission(self, doctype: str, parent_doctype: str | None = None): + if self.flags.ignore_permissions: + return + + if doctype not in self.permission_map: + self._set_permission_map(doctype, parent_doctype) + + return self.permission_map[doctype] + + def _set_permission_map(self, doctype: str, parent_doctype: str | None = None): + ptype = "select" if xhiveframework.only_has_select_perm(doctype) else "read" + xhiveframework.has_permission( + doctype, + ptype=ptype, + parent_doctype=parent_doctype or self.doctype, + throw=True, + user=self.user, + ) + self.permission_map[doctype] = ptype + + def set_field_tables(self): + """If there are more than one table, the fieldname must not be ambiguous. + If the fieldname is not explicitly mentioned, set the default table""" + + def _in_standard_sql_methods(field): + methods = ("count(", "avg(", "sum(", "extract(", "dayofyear(") + return field.lower().startswith(methods) + + if len(self.tables) > 1 or len(self.link_tables) > 0: + for idx, field in enumerate(self.fields): + if field is not None and "." not in field and not _in_standard_sql_methods(field): + self.fields[idx] = f"{self.tables[0]}.{field}" + + def cast_name_fields(self): + for i, field in enumerate(self.fields): + if field is not None: + self.fields[i] = cast_name(field) + + def get_table_columns(self): + try: + return get_table_columns(self.doctype) + except xhiveframework.db.TableMissingError: + if self.ignore_ddl: + return None + else: + raise + + def set_optional_columns(self): + """Removes optional columns like `_user_tags`, `_comments` etc. if not in table""" + # remove from fields + to_remove = [] + for fld in self.fields: + to_remove.extend(fld for f in optional_fields if f in fld and f not in self.columns) + for fld in to_remove: + del self.fields[self.fields.index(fld)] + + # remove from filters + to_remove = [] + for each in self.filters: + if isinstance(each, str): + each = [each] + + to_remove.extend( + each for element in each if element in optional_fields and element not in self.columns + ) + for each in to_remove: + if isinstance(self.filters, dict): + del self.filters[each] + else: + self.filters.remove(each) + + def build_conditions(self): + self.conditions = [] + self.grouped_or_conditions = [] + self.build_filter_conditions(self.filters, self.conditions) + self.build_filter_conditions(self.or_filters, self.grouped_or_conditions) + + # match conditions + if not self.flags.ignore_permissions: + match_conditions = self.build_match_conditions() + if match_conditions: + self.conditions.append(f"({match_conditions})") + + def build_filter_conditions(self, filters, conditions: list, ignore_permissions=None): + """build conditions from user filters""" + if ignore_permissions is not None: + self.flags.ignore_permissions = ignore_permissions + + if isinstance(filters, dict): + filters = [filters] + + for f in filters: + if isinstance(f, str): + conditions.append(f) + else: + conditions.append(self.prepare_filter_condition(f)) + + def remove_field(self, idx: int): + if self.as_list: + self.fields[idx] = None + else: + self.fields.pop(idx) + + def apply_fieldlevel_read_permissions(self): + """Apply fieldlevel read permissions to the query + + Note: Does not apply to `xhiveframework.model.core_doctype_list` + + Remove fields that user is not allowed to read. If `fields=["*"]` is passed, only permitted fields will + be returned. + + Example: + - User has read permission only on `title` for DocType `Note` + - Query: fields=["*"] + - Result: fields=["title", ...] // will also include XhiveFramework's meta field like `name`, `owner`, etc. + """ + if self.flags.ignore_permissions: + return + + asterisk_fields = [] + permitted_fields = get_permitted_fields( + doctype=self.doctype, + parenttype=self.parent_doctype, + permission_type=self.permission_map.get(self.doctype), + ignore_virtual=True, + ) + + for i, field in enumerate(self.fields): + if "distinct" in field.lower(): + # field: 'count(distinct `tabPhoto`.name) as total_count' + # column: 'tabPhoto.name' + if _fn := FN_PARAMS_PATTERN.findall(field): + column = _fn[0].replace("distinct ", "").replace("DISTINCT ", "").replace("`", "") + # field: 'distinct name' + # column: 'name' + else: + column = field.split(" ", 1)[1].replace("`", "") + else: + # field: 'count(`tabPhoto`.name) as total_count' + # column: 'tabPhoto.name' + column = field.split("(")[-1].split(")", 1)[0] + column = strip_alias(column).replace("`", "") + + if column == "*" and not in_function("*", field): + asterisk_fields.append(i) + continue + + # handle pseudo columns + elif not column or column.isnumeric(): + continue + + # labels / pseudo columns or xhiveframework internals + elif column[0] in {"'", '"'} or column in optional_fields: + continue + + # handle child / joined table fields + elif "." in field: + table, column = column.split(".", 1) + ch_doctype = table + + if ch_doctype in self.linked_table_aliases: + ch_doctype = self.linked_table_aliases[ch_doctype] + + ch_doctype = ch_doctype.replace("`", "").replace("tab", "", 1) + + if wrap_grave_quotes(table) in self.query_tables: + permitted_child_table_fields = get_permitted_fields( + doctype=ch_doctype, parenttype=self.doctype + ) + if column in permitted_child_table_fields or column in optional_fields: + continue + else: + self.remove_field(i) + else: + raise xhiveframework.PermissionError(ch_doctype) + + elif column in permitted_fields: + continue + + # field inside function calls / * handles things like count(*) + elif "(" in field: + if "*" in field: + continue + elif _params := FN_PARAMS_PATTERN.findall(field): + params = (x.strip() for x in _params[0].split(",")) + for param in params: + if not ( + not param + or param in permitted_fields + or param.isnumeric() + or "'" in param + or '"' in param + ): + self.remove_field(i) + break + continue + self.remove_field(i) + + # remove if access not allowed + else: + self.remove_field(i) + + # handle * fields + j = 0 + for i in asterisk_fields: + self.fields[i + j : i + j + 1] = permitted_fields + j = j + len(permitted_fields) - 1 + + def prepare_filter_condition(self, f): + """Returns a filter condition in the format: + ifnull(`tabDocType`.`fieldname`, fallback) operator "value" + """ + + # TODO: refactor + + from xhiveframework.boot import get_additional_filters_from_hooks + + additional_filters_config = get_additional_filters_from_hooks() + f = get_filter(self.doctype, f, additional_filters_config) + + tname = "`tab" + f.doctype + "`" + if tname not in self.tables: + self.append_table(tname) + + column_name = cast_name(f.fieldname if "ifnull(" in f.fieldname else f"{tname}.`{f.fieldname}`") + + if f.operator.lower() in additional_filters_config: + f.update(get_additional_filter_field(additional_filters_config, f, f.value)) + + meta = xhiveframework.get_meta(f.doctype) + + # primary key is never nullable, modified is usually indexed by default and always present + can_be_null = f.fieldname not in ("name", "modified") + + # prepare in condition + if f.operator.lower() in NestedSetHierarchy: + values = f.value or "" + + # TODO: handle list and tuple + # if not isinstance(values, (list, tuple)): + # values = values.split(",") + field = meta.get_field(f.fieldname) + ref_doctype = field.options if field else f.doctype + lft, rgt = "", "" + if f.value: + lft, rgt = xhiveframework.db.get_value(ref_doctype, f.value, ["lft", "rgt"]) or (0, 0) + + # Get descendants elements of a DocType with a tree structure + if f.operator.lower() in ( + "descendants of", + "not descendants of", + "descendants of (inclusive)", + ): + nodes = xhiveframework.get_all( + ref_doctype, + filters={"lft": [">", lft], "rgt": ["<", rgt]}, + order_by="`lft` ASC", + pluck="name", + ) + if f.operator.lower() == "descendants of (inclusive)": + nodes += [f.value] + else: + # Get ancestor elements of a DocType with a tree structure + nodes = xhiveframework.get_all( + ref_doctype, + filters={"lft": ["<", lft], "rgt": [">", rgt]}, + order_by="`lft` DESC", + pluck="name", + ) + + fallback = "''" + value = [xhiveframework.db.escape((cstr(v)).strip(), percent=False) for v in nodes] + if len(value): + value = f"({', '.join(value)})" + else: + value = "('')" + + # changing operator to IN as the above code fetches all the parent / child values and convert into tuple + # which can be directly used with IN operator to query. + f.operator = ( + "not in" if f.operator.lower() in ("not ancestors of", "not descendants of") else "in" + ) + + elif f.operator.lower() in ("in", "not in"): + # if values contain '' or falsy values then only coalesce column + # for `in` query this is only required if values contain '' or values are empty. + # for `not in` queries we can't be sure as column values might contain null. + if f.operator.lower() == "in": + can_be_null &= not f.value or any(v is None or v == "" for v in f.value) + + values = f.value or "" + if isinstance(values, str): + values = values.split(",") + + fallback = "''" + value = [xhiveframework.db.escape((cstr(v) or "").strip(), percent=False) for v in values] + if len(value): + value = f"({', '.join(value)})" + else: + value = "('')" + + else: + escape = True + df = meta.get("fields", {"fieldname": f.fieldname}) + df = df[0] if df else None + + if df and df.fieldtype in ("Check", "Float", "Int", "Currency", "Percent"): + can_be_null = False + + if f.operator.lower() in ("previous", "next", "timespan"): + date_range = get_date_range(f.operator.lower(), f.value) + f.operator = "Between" + f.value = date_range + fallback = f"'{FallBackDateTimeStr}'" + + if f.operator in (">", "<", ">=", "<=") and (f.fieldname in ("creation", "modified")): + value = cstr(f.value) + fallback = f"'{FallBackDateTimeStr}'" + + elif f.operator.lower() in ("between") and ( + f.fieldname in ("creation", "modified") + or (df and (df.fieldtype == "Date" or df.fieldtype == "Datetime")) + ): + escape = False + value = get_between_date_filter(f.value, df) + fallback = f"'{FallBackDateTimeStr}'" + + elif f.operator.lower() == "is": + if f.value == "set": + f.operator = "!=" + # Value can technically be null, but comparing with null will always be falsy + # Not using coalesce here is faster because indexes can be used. + # null != '' -> null ~ falsy + # '' != '' -> false + can_be_null = False + elif f.value == "not set": + f.operator = "=" + fallback = "''" + can_be_null = True + + value = "" + + if can_be_null and "ifnull" not in column_name.lower(): + column_name = f"ifnull({column_name}, {fallback})" + + elif df and df.fieldtype == "Date": + value = xhiveframework.db.format_date(f.value) + fallback = "'0001-01-01'" + + elif (df and df.fieldtype == "Datetime") or isinstance(f.value, datetime.datetime): + value = xhiveframework.db.format_datetime(f.value) + fallback = f"'{FallBackDateTimeStr}'" + + elif df and df.fieldtype == "Time": + value = get_time(f.value).strftime("%H:%M:%S.%f") + fallback = "'00:00:00'" + + elif f.operator.lower() in ("like", "not like") or ( + isinstance(f.value, str) + and (not df or df.fieldtype not in ["Float", "Int", "Currency", "Percent", "Check"]) + ): + value = "" if f.value is None else f.value + fallback = "''" + + if f.operator.lower() in ("like", "not like") and isinstance(value, str): + # because "like" uses backslash (\) for escaping + value = value.replace("\\", "\\\\").replace("%", "%%") + + elif f.operator == "=" and df and df.fieldtype in ["Link", "Data"]: # TODO: Refactor if possible + value = f.value or "''" + fallback = "''" + + elif f.fieldname == "name": + value = f.value or "''" + fallback = "''" + + else: + value = flt(f.value) + fallback = 0 + + if isinstance(f.value, Column): + can_be_null = False # added to avoid the ifnull/coalesce addition + quote = '"' if xhiveframework.conf.db_type == "postgres" else "`" + value = f"{tname}.{quote}{f.value.name}{quote}" + + # escape value + elif escape and isinstance(value, str): + value = f"{xhiveframework.db.escape(value, percent=False)}" + + if ( + self.ignore_ifnull + or not can_be_null + or (f.value and f.operator.lower() in ("=", "like")) + or "ifnull(" in column_name.lower() + ): + if f.operator.lower() == "like" and xhiveframework.conf.get("db_type") == "postgres": + f.operator = "ilike" + condition = f"{column_name} {f.operator} {value}" + else: + condition = f"ifnull({column_name}, {fallback}) {f.operator} {value}" + + return condition + + def build_match_conditions(self, as_condition=True) -> str | list: + """add match conditions if applicable""" + self.match_filters = [] + self.match_conditions = [] + only_if_shared = False + if not self.user: + self.user = xhiveframework.session.user + + if not self.tables: + self.extract_tables() + + role_permissions = xhiveframework.permissions.get_role_permissions(self.doctype_meta, user=self.user) + if ( + not self.doctype_meta.istable + and not (role_permissions.get("select") or role_permissions.get("read")) + and not self.flags.ignore_permissions + and not has_any_user_permission_for_doctype(self.doctype, self.user, self.reference_doctype) + ): + only_if_shared = True + self.shared = xhiveframework.share.get_shared(self.doctype, self.user) + if not self.shared: + xhiveframework.throw(_("No permission to read {0}").format(_(self.doctype)), xhiveframework.PermissionError) + else: + self.conditions.append(self.get_share_condition()) + + else: + # skip user perm check if owner constraint is required + if requires_owner_constraint(role_permissions): + self._fetch_shared_documents = True + self.match_conditions.append( + f"`tab{self.doctype}`.`owner` = {xhiveframework.db.escape(self.user, percent=False)}" + ) + + # add user permission only if role has read perm + elif role_permissions.get("read") or role_permissions.get("select"): + # get user permissions + user_permissions = xhiveframework.permissions.get_user_permissions(self.user) + self.add_user_permissions(user_permissions) + + # Only when full read access is not present fetch shared docuemnts. + # This is done to avoid extra query. + # Only following cases can require explicit addition of shared documents. + # 1. DocType has if_owner constraint and hence can't see shared documents + # 2. DocType has user permissions and hence can't see shared documents + if self._fetch_shared_documents: + self.shared = xhiveframework.share.get_shared(self.doctype, self.user) + + if as_condition: + conditions = "" + if self.match_conditions: + # will turn out like ((blog_post in (..) and blogger in (...)) or (blog_category in (...))) + conditions = "((" + ") or (".join(self.match_conditions) + "))" + + doctype_conditions = self.get_permission_query_conditions() + if doctype_conditions: + conditions += (" and " + doctype_conditions) if conditions else doctype_conditions + + # share is an OR condition, if there is a role permission + if not only_if_shared and self.shared and conditions: + conditions = f"(({conditions}) or ({self.get_share_condition()}))" + + return conditions + + else: + return self.match_filters + + def get_share_condition(self): + return ( + cast_name(f"`tab{self.doctype}`.name") + + f" in ({', '.join(xhiveframework.db.escape(s, percent=False) for s in self.shared)})" + ) + + def add_user_permissions(self, user_permissions): + doctype_link_fields = [] + doctype_link_fields = self.doctype_meta.get_link_fields() + + # append current doctype with fieldname as 'name' as first link field + doctype_link_fields.append( + dict( + options=self.doctype, + fieldname="name", + ) + ) + + match_filters = {} + match_conditions = [] + for df in doctype_link_fields: + if df.get("ignore_user_permissions"): + continue + + user_permission_values = user_permissions.get(df.get("options"), {}) + + if user_permission_values: + docs = [] + if xhiveframework.get_system_settings("apply_strict_user_permissions"): + condition = "" + else: + empty_value_condition = cast_name( + f"ifnull(`tab{self.doctype}`.`{df.get('fieldname')}`, '')=''" + ) + condition = empty_value_condition + " or " + + for permission in user_permission_values: + if not permission.get("applicable_for"): + docs.append(permission.get("doc")) + + # append docs based on user permission applicable on reference doctype + # this is useful when getting list of docs from a link field + # in this case parent doctype of the link + # will be the reference doctype + + elif df.get("fieldname") == "name" and self.reference_doctype: + if permission.get("applicable_for") == self.reference_doctype: + docs.append(permission.get("doc")) + + elif permission.get("applicable_for") == self.doctype: + docs.append(permission.get("doc")) + + if docs: + values = ", ".join(xhiveframework.db.escape(doc, percent=False) for doc in docs) + condition += cast_name(f"`tab{self.doctype}`.`{df.get('fieldname')}`") + f" in ({values})" + match_conditions.append(f"({condition})") + match_filters[df.get("options")] = docs + + if match_conditions: + self._fetch_shared_documents = True + self.match_conditions.append(" and ".join(match_conditions)) + + if match_filters: + self._fetch_shared_documents = True + self.match_filters.append(match_filters) + + def get_permission_query_conditions(self): + conditions = [] + condition_methods = xhiveframework.get_hooks("permission_query_conditions", {}).get(self.doctype, []) + if condition_methods: + for method in condition_methods: + c = xhiveframework.call(xhiveframework.get_attr(method), self.user) + if c: + conditions.append(c) + + permision_script_name = get_server_script_map().get("permission_query", {}).get(self.doctype) + if permision_script_name: + script = xhiveframework.get_doc("Server Script", permision_script_name) + condition = script.get_permission_query_conditions(self.user) + if condition: + conditions.append(condition) + + return " and ".join(conditions) if conditions else "" + + def set_order_by(self, args): + if self.order_by and self.order_by != "KEEP_DEFAULT_ORDERING": + args.order_by = self.order_by + else: + args.order_by = "" + + # don't add order by from meta if a mysql group function is used without group by clause + group_function_without_group_by = ( + len(self.fields) == 1 + and ( + self.fields[0].lower().startswith("count(") + or self.fields[0].lower().startswith("min(") + or self.fields[0].lower().startswith("max(") + or self.fields[0].lower().startswith("sum(") + or self.fields[0].lower().startswith("avg(") + ) + and not self.group_by + ) + + if not group_function_without_group_by: + sort_field = sort_order = None + if self.doctype_meta.sort_field and "," in self.doctype_meta.sort_field: + # multiple sort given in doctype definition + # Example: + # `idx desc, modified desc` + # will covert to + # `tabItem`.`idx` desc, `tabItem`.`modified` desc + args.order_by = ", ".join( + f"`tab{self.doctype}`.`{f_split[0].strip()}` {f_split[1].strip()}" + for f in self.doctype_meta.sort_field.split(",") + if (f_split := f.split(maxsplit=2)) + ) + else: + sort_field = self.doctype_meta.sort_field or "modified" + sort_order = (self.doctype_meta.sort_field and self.doctype_meta.sort_order) or "desc" + if self.order_by: + args.order_by = ( + f"`tab{self.doctype}`.`{sort_field or 'modified'}` {sort_order or 'desc'}" + ) + + # draft docs always on top + if hasattr(self.doctype_meta, "is_submittable") and self.doctype_meta.is_submittable: + if self.order_by: + args.order_by = f"`tab{self.doctype}`.docstatus asc, {args.order_by}" + + def validate_order_by_and_group_by(self, parameters: str): + """Check order by, group by so that atleast one column is selected and does not have subquery""" + if not parameters: + return + + blacklisted_sql_functions = { + "sleep", + } + _lower = parameters.lower() + + if "select" in _lower and "from" in _lower: + xhiveframework.throw(_("Cannot use sub-query in order by")) + + if ORDER_GROUP_PATTERN.match(_lower): + xhiveframework.throw(_("Illegal SQL Query")) + + for field in parameters.split(","): + field = field.strip() + function = field.split("(", 1)[0].rstrip().lower() + full_field_name = "." in field and field.startswith("`tab") + + if full_field_name: + tbl = field.split(".", 1)[0] + if tbl not in self.tables: + if tbl.startswith("`"): + tbl = tbl[4:-1] + xhiveframework.throw(_("Please select atleast 1 column from {0} to sort/group").format(tbl)) + + if function in blacklisted_sql_functions: + xhiveframework.throw(_("Cannot use {0} in order/group by").format(field)) + + def add_limit(self): + if self.limit_page_length: + return f"limit {self.limit_page_length} offset {self.limit_start}" + else: + return "" + + def add_comment_count(self, result): + for r in result: + if not r.name: + continue + + r._comment_count = 0 + if "_comments" in r: + r._comment_count = len(json.loads(r._comments or "[]")) + + def update_user_settings(self): + # update user settings if new search + user_settings = json.loads(get_user_settings(self.doctype)) + + if hasattr(self, "user_settings"): + user_settings.update(self.user_settings) + + if self.save_user_settings_fields: + user_settings["fields"] = self.user_settings_fields + + update_user_settings(self.doctype, user_settings) + + +def cast_name(column: str) -> str: + """Casts name field to varchar for postgres + + Handles majorly 4 cases: + 1. locate + 2. strpos + 3. ifnull + 4. coalesce + + Uses regex substitution. + + Example: + input - "ifnull(`tabBlog Post`.`name`, '')=''" + output - "ifnull(cast(`tabBlog Post`.`name` as varchar), '')=''" """ + + if xhiveframework.db.db_type == "mariadb": + return column + + kwargs = {"string": column} + if "cast(" not in column.lower() and "::" not in column: + if LOCATE_PATTERN.search(**kwargs): + return LOCATE_CAST_PATTERN.sub(r"locate(\1, cast(\2 as varchar))", **kwargs) + + elif match := FUNC_IFNULL_PATTERN.search(**kwargs): + func = match.groups()[0] + return re.sub(rf"{func}\(\s*([`\"]?name[`\"]?)\s*,", rf"{func}(cast(\1 as varchar),", **kwargs) + + return CAST_VARCHAR_PATTERN.sub(r"cast(\1 as varchar)", **kwargs) + + return column + + +def check_parent_permission(parent, child_doctype): + if parent: + # User may pass fake parent and get the information from the child table + if child_doctype and not ( + xhiveframework.db.exists("DocField", {"parent": parent, "options": child_doctype}) + or xhiveframework.db.exists("Custom Field", {"dt": parent, "options": child_doctype}) + ): + raise xhiveframework.PermissionError + + if xhiveframework.permissions.has_permission(parent): + return + + # Either parent not passed or the user doesn't have permission on parent doctype of child table! + raise xhiveframework.PermissionError + + +def get_order_by(doctype, meta): + order_by = "" + + sort_field = sort_order = None + if meta.sort_field and "," in meta.sort_field: + # multiple sort given in doctype definition + # Example: + # `idx desc, modified desc` + # will covert to + # `tabItem`.`idx` desc, `tabItem`.`modified` desc + order_by = ", ".join( + f"`tab{doctype}`.`{f_split[0].strip()}` {f_split[1].strip()}" + for f in meta.sort_field.split(",") + if (f_split := f.split(maxsplit=2)) + ) + + else: + sort_field = meta.sort_field or "modified" + sort_order = (meta.sort_field and meta.sort_order) or "desc" + order_by = f"`tab{doctype}`.`{sort_field or 'modified'}` {sort_order or 'desc'}" + + # draft docs always on top + if meta.is_submittable: + order_by = f"`tab{doctype}`.docstatus asc, {order_by}" + + return order_by + + +def has_any_user_permission_for_doctype(doctype, user, applicable_for): + user_permissions = xhiveframework.permissions.get_user_permissions(user=user) + doctype_user_permissions = user_permissions.get(doctype, []) + + for permission in doctype_user_permissions: + if not permission.applicable_for or permission.applicable_for == applicable_for: + return True + + return False + + +def get_between_date_filter(value, df=None): + """Handle datetime filter bounds for between filter values. + + If date is passed but fieldtype is datetime then + from part is converted to start of day and to part is converted to end of day. + If any of filter part (to or from) are missing then: + start or end of current day is assumed as fallback. + If fieldtypes match with filter values then: + no change is applied. + """ + + fieldtype = df and df.fieldtype or "Datetime" + + from_date = xhiveframework.utils.nowdate() + to_date = xhiveframework.utils.nowdate() + + if value and isinstance(value, list | tuple): + if len(value) >= 1: + from_date = value[0] + if len(value) >= 2: + to_date = value[1] + + # if filter value is date but fieldtype is datetime: + if fieldtype == "Datetime": + from_date = _convert_type_for_between_filters(from_date, set_time=datetime.time()) + to_date = _convert_type_for_between_filters(to_date, set_time=datetime.time(23, 59, 59, 999999)) + + # If filter value is already datetime, do nothing. + if fieldtype == "Datetime": + cond = f"'{xhiveframework.db.format_datetime(from_date)}' AND '{xhiveframework.db.format_datetime(to_date)}'" + else: + cond = f"'{xhiveframework.db.format_date(from_date)}' AND '{xhiveframework.db.format_date(to_date)}'" + + return cond + + +def _convert_type_for_between_filters( + value: DateTimeLikeObject, set_time: datetime.time +) -> datetime.datetime: + if isinstance(value, str): + if " " in value.strip(): + value = get_datetime(value) + else: + value = getdate(value) + + if isinstance(value, datetime.datetime): + return value + elif isinstance(value, datetime.date): + return datetime.datetime.combine(value, set_time) + + return value + + +def get_additional_filter_field(additional_filters_config, f, value): + additional_filter = additional_filters_config[f.operator.lower()] + f = xhiveframework._dict(xhiveframework.get_attr(additional_filter["get_field"])()) + if f.query_value: + for option in f.options: + option = xhiveframework._dict(option) + if option.value == value: + f.value = option.query_value + return f + + +def get_date_range(operator: str, value: str): + timespan_map = { + "1 week": "week", + "1 month": "month", + "3 months": "quarter", + "6 months": "6 months", + "1 year": "year", + } + period_map = { + "previous": "last", + "next": "next", + } + + if operator != "timespan": + timespan = f"{period_map[operator]} {timespan_map[value]}" + else: + timespan = value + + return get_timespan_date_range(timespan) + + +def requires_owner_constraint(role_permissions): + """Returns True if "select" or "read" isn't available without being creator.""" + + if not role_permissions.get("has_if_owner_enabled"): + return + + if_owner_perms = role_permissions.get("if_owner") + if not if_owner_perms: + return + + # has select or read without if owner, no need for constraint + for perm_type in ("select", "read"): + if role_permissions.get(perm_type) and perm_type not in if_owner_perms: + return + + # not checking if either select or read if present in if_owner_perms + # because either of those is required to perform a query + return True + + +def wrap_grave_quotes(table: str) -> str: + if table[0] != "`": + table = f"`{table}`" + return table + + +def is_plain_field(field: str) -> bool: + for char in field: + if char in SPECIAL_FIELD_CHARS: + return False + return True + + +def in_function(substr: str, field: str) -> bool: + try: + return substr in field and field.index("(") < field.index(substr) < field.index(")") + except ValueError: + return False + + +def strip_alias(field: str) -> str: + # Note: Currently only supports aliases that use the " AS " syntax + if " as " in field.lower(): + return field.split(" as ", 1)[0] + return field diff --git a/xhiveframework/model/delete_doc.py b/xhiveframework/model/delete_doc.py new file mode 100644 index 0000000..8153c42 --- /dev/null +++ b/xhiveframework/model/delete_doc.py @@ -0,0 +1,463 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import os +import shutil + +import xhiveframework +import xhiveframework.defaults +import xhiveframework.model.meta +from xhiveframework import _, get_module_path +from xhiveframework.desk.doctype.tag.tag import delete_tags_for_document +from xhiveframework.model.docstatus import DocStatus +from xhiveframework.model.dynamic_links import get_dynamic_link_map +from xhiveframework.model.naming import revert_series_if_last +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.utils.file_manager import remove_all +from xhiveframework.utils.global_search import delete_for_document +from xhiveframework.utils.password import delete_all_passwords_for + + +def delete_doc( + doctype=None, + name=None, + force=0, + ignore_doctypes=None, + for_reload=False, + ignore_permissions=False, + flags=None, + ignore_on_trash=False, + ignore_missing=True, + delete_permanently=False, +): + """ + Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record + """ + if not ignore_doctypes: + ignore_doctypes = [] + + # get from form + if not doctype: + doctype = xhiveframework.form_dict.get("dt") + name = xhiveframework.form_dict.get("dn") + + is_virtual = is_virtual_doctype(doctype) + + names = name + if isinstance(name, str) or isinstance(name, int): + names = [name] + + for name in names or []: + if is_virtual: + xhiveframework.get_doc(doctype, name).delete() + continue + + # already deleted..? + if not xhiveframework.db.exists(doctype, name): + if not ignore_missing: + raise xhiveframework.DoesNotExistError + else: + return False + + # delete passwords + delete_all_passwords_for(doctype, name) + + doc = None + if doctype == "DocType": + if for_reload: + try: + doc = xhiveframework.get_doc(doctype, name) + except xhiveframework.DoesNotExistError: + pass + else: + doc.run_method("before_reload") + + else: + doc = xhiveframework.get_doc(doctype, name) + if not (doc.custom or xhiveframework.conf.developer_mode or xhiveframework.flags.in_patch or force): + xhiveframework.throw(_("Standard DocType can not be deleted.")) + + update_flags(doc, flags, ignore_permissions) + check_permission_and_not_submitted(doc) + # delete custom table fields using this doctype. + xhiveframework.db.delete( + "Custom Field", {"options": name, "fieldtype": ("in", xhiveframework.model.table_fields)} + ) + xhiveframework.db.delete("__global_search", {"doctype": name}) + + delete_from_table(doctype, name, ignore_doctypes, None) + + if ( + xhiveframework.conf.developer_mode + and not doc.custom + and not ( + for_reload + or xhiveframework.flags.in_migrate + or xhiveframework.flags.in_install + or xhiveframework.flags.in_uninstall + ) + ): + try: + delete_controllers(name, doc.module) + except (OSError, KeyError): + # in case a doctype doesnt have any controller code nor any app and module + pass + + else: + # Lock the doc without waiting + try: + xhiveframework.db.get_value(doctype, name, for_update=True, wait=False) + except xhiveframework.QueryTimeoutError: + xhiveframework.throw( + _( + "This document can not be deleted right now as it's being modified by another user. Please try again after some time." + ), + exc=xhiveframework.QueryTimeoutError, + ) + doc = xhiveframework.get_doc(doctype, name) + + if not for_reload: + update_flags(doc, flags, ignore_permissions) + check_permission_and_not_submitted(doc) + + if not ignore_on_trash: + doc.run_method("on_trash") + doc.flags.in_delete = True + doc.run_method("on_change") + + # check if links exist + if not force: + check_if_doc_is_linked(doc) + check_if_doc_is_dynamically_linked(doc) + + update_naming_series(doc) + delete_from_table(doctype, name, ignore_doctypes, doc) + doc.run_method("after_delete") + + # delete attachments + remove_all(doctype, name, from_delete=True, delete_permanently=delete_permanently) + + if not for_reload: + # Enqueued at the end, because it gets committed + # All the linked docs should be checked beforehand + xhiveframework.enqueue( + "xhiveframework.model.delete_doc.delete_dynamic_links", + doctype=doc.doctype, + name=doc.name, + now=xhiveframework.flags.in_test, + enqueue_after_commit=True, + ) + + # clear cache for Document + doc.clear_cache() + # delete global search entry + delete_for_document(doc) + # delete tag link entry + delete_tags_for_document(doc) + + if for_reload: + delete_permanently = True + + if not delete_permanently: + add_to_deleted_document(doc) + + if doc and not for_reload: + if not xhiveframework.flags.in_patch: + try: + doc.notify_update() + insert_feed(doc) + except ImportError: + pass + + +def add_to_deleted_document(doc): + """Add this document to Deleted Document table. Called after delete""" + if doc.doctype != "Deleted Document" and xhiveframework.flags.in_install != "xhiveframework": + xhiveframework.get_doc( + dict( + doctype="Deleted Document", + deleted_doctype=doc.doctype, + deleted_name=doc.name, + data=doc.as_json(), + owner=xhiveframework.session.user, + ) + ).db_insert() + + +def update_naming_series(doc): + if doc.meta.autoname: + if doc.meta.autoname.startswith("naming_series:") and getattr(doc, "naming_series", None): + revert_series_if_last(doc.naming_series, doc.name, doc) + + elif doc.meta.autoname.split(":", 1)[0] not in ("Prompt", "field", "hash", "autoincrement"): + revert_series_if_last(doc.meta.autoname, doc.name, doc) + + +def delete_from_table(doctype: str, name: str, ignore_doctypes: list[str], doc): + if doctype != "DocType" and doctype == name: + xhiveframework.db.delete("Singles", {"doctype": name}) + else: + xhiveframework.db.delete(doctype, {"name": name}) + if doc: + child_doctypes = [ + d.options for d in doc.meta.get_table_fields() if xhiveframework.get_meta(d.options).is_virtual == 0 + ] + + else: + child_doctypes = xhiveframework.get_all( + "DocField", + fields="options", + filters={"fieldtype": ["in", xhiveframework.model.table_fields], "parent": doctype}, + pluck="options", + ) + + child_doctypes_to_delete = set(child_doctypes) - set(ignore_doctypes) + for child_doctype in child_doctypes_to_delete: + xhiveframework.db.delete(child_doctype, {"parenttype": doctype, "parent": name}) + + +def update_flags(doc, flags=None, ignore_permissions=False): + if ignore_permissions: + if not flags: + flags = {} + flags["ignore_permissions"] = ignore_permissions + + if flags: + doc.flags.update(flags) + + +def check_permission_and_not_submitted(doc): + # permission + if ( + not doc.flags.ignore_permissions + and xhiveframework.session.user != "Administrator" + and (not doc.has_permission("delete") or (doc.doctype == "DocType" and not doc.custom)) + ): + xhiveframework.msgprint( + _("User not allowed to delete {0}: {1}").format(doc.doctype, doc.name), + raise_exception=xhiveframework.PermissionError, + ) + + # check if submitted + if doc.docstatus.is_submitted(): + xhiveframework.msgprint( + _("{0} {1}: Submitted Record cannot be deleted. You must {2} Cancel {3} it first.").format( + _(doc.doctype), + doc.name, + "", + "", + ), + raise_exception=True, + ) + + +def check_if_doc_is_linked(doc, method="Delete"): + """ + Raises excption if the given doc(dt, dn) is linked in another record. + """ + from xhiveframework.model.rename_doc import get_link_fields + + link_fields = get_link_fields(doc.doctype) + ignored_doctypes = set() + + if method == "Cancel" and (doc_ignore_flags := doc.get("ignore_linked_doctypes")): + ignored_doctypes.update(doc_ignore_flags) + if method == "Delete": + ignored_doctypes.update(xhiveframework.get_hooks("ignore_links_on_delete")) + + for lf in link_fields: + link_dt, link_field, issingle = lf["parent"], lf["fieldname"], lf["issingle"] + if link_dt in ignored_doctypes or (link_field == "amended_from" and method == "Cancel"): + continue + + try: + meta = xhiveframework.get_meta(link_dt) + except xhiveframework.DoesNotExistError: + xhiveframework.clear_last_message() + # This mostly happens when app do not remove their customizations, we shouldn't + # prevent link checks from failing in those cases + continue + + if issingle: + if xhiveframework.db.get_single_value(link_dt, link_field) == doc.name: + raise_link_exists_exception(doc, link_dt, link_dt) + continue + + fields = ["name", "docstatus"] + + if meta.istable: + fields.extend(["parent", "parenttype"]) + + for item in xhiveframework.db.get_values(link_dt, {link_field: doc.name}, fields, as_dict=True): + # available only in child table cases + item_parent = getattr(item, "parent", None) + linked_parent_doctype = item.parenttype if item_parent else link_dt + + if linked_parent_doctype in ignored_doctypes: + continue + + if method != "Delete" and (method != "Cancel" or not DocStatus(item.docstatus).is_submitted()): + # don't raise exception if not + # linked to a non-cancelled doc when deleting or to a submitted doc when cancelling + continue + elif link_dt == doc.doctype and (item_parent or item.name) == doc.name: + # don't raise exception if not + # linked to same item or doc having same name as the item + continue + else: + reference_docname = item_parent or item.name + raise_link_exists_exception(doc, linked_parent_doctype, reference_docname) + + +def check_if_doc_is_dynamically_linked(doc, method="Delete"): + """Raise `xhiveframework.LinkExistsError` if the document is dynamically linked""" + for df in get_dynamic_link_map().get(doc.doctype, []): + ignore_linked_doctypes = doc.get("ignore_linked_doctypes") or [] + + if df.parent in xhiveframework.get_hooks("ignore_links_on_delete") or ( + df.parent in ignore_linked_doctypes and method == "Cancel" + ): + # don't check for communication and todo! + continue + + meta = xhiveframework.get_meta(df.parent) + if meta.issingle: + # dynamic link in single doc + refdoc = xhiveframework.db.get_singles_dict(df.parent) + if ( + refdoc.get(df.options) == doc.doctype + and refdoc.get(df.fieldname) == doc.name + and ( + # linked to an non-cancelled doc when deleting + (method == "Delete" and not DocStatus(refdoc.docstatus).is_cancelled()) + # linked to a submitted doc when cancelling + or (method == "Cancel" and DocStatus(refdoc.docstatus).is_submitted()) + ) + ): + raise_link_exists_exception(doc, df.parent, df.parent) + else: + # dynamic link in table + df["table"] = ", `parent`, `parenttype`, `idx`" if meta.istable else "" + for refdoc in xhiveframework.db.sql( + """select `name`, `docstatus` {table} from `tab{parent}` where + {options}=%s and {fieldname}=%s""".format(**df), + (doc.doctype, doc.name), + as_dict=True, + ): + # linked to an non-cancelled doc when deleting + # or linked to a submitted doc when cancelling + if (method == "Delete" and not DocStatus(refdoc.docstatus).is_cancelled()) or ( + method == "Cancel" and DocStatus(refdoc.docstatus).is_submitted() + ): + reference_doctype = refdoc.parenttype if meta.istable else df.parent + reference_docname = refdoc.parent if meta.istable else refdoc.name + + if reference_doctype in xhiveframework.get_hooks("ignore_links_on_delete") or ( + reference_doctype in ignore_linked_doctypes and method == "Cancel" + ): + # don't check for communication and todo! + continue + + at_position = f"at Row: {refdoc.idx}" if meta.istable else "" + + raise_link_exists_exception(doc, reference_doctype, reference_docname, at_position) + + +def raise_link_exists_exception(doc, reference_doctype, reference_docname, row=""): + doc_link = f'{doc.name}' + reference_link = f'{reference_docname}' + + # hack to display Single doctype only once in message + if reference_doctype == reference_docname: + reference_doctype = "" + + xhiveframework.throw( + _("Cannot delete or cancel because {0} {1} is linked with {2} {3} {4}").format( + _(doc.doctype), doc_link, _(reference_doctype), reference_link, row + ), + xhiveframework.LinkExistsError, + ) + + +def delete_dynamic_links(doctype, name): + delete_references("ToDo", doctype, name, "reference_type") + delete_references("Email Unsubscribe", doctype, name) + delete_references("DocShare", doctype, name, "share_doctype", "share_name") + delete_references("Version", doctype, name, "ref_doctype", "docname") + delete_references("Comment", doctype, name) + delete_references("View Log", doctype, name) + delete_references("Document Follow", doctype, name, "ref_doctype", "ref_docname") + delete_references("Notification Log", doctype, name, "document_type", "document_name") + + # unlink communications + clear_timeline_references(doctype, name) + clear_references("Communication", doctype, name) + + clear_references("Activity Log", doctype, name) + clear_references("Activity Log", doctype, name, "timeline_doctype", "timeline_name") + + +def delete_references( + doctype, + reference_doctype, + reference_name, + reference_doctype_field="reference_doctype", + reference_name_field="reference_name", +): + xhiveframework.db.delete( + doctype, {reference_doctype_field: reference_doctype, reference_name_field: reference_name} + ) + + +def clear_references( + doctype, + reference_doctype, + reference_name, + reference_doctype_field="reference_doctype", + reference_name_field="reference_name", +): + xhiveframework.db.sql( + f"""update + `tab{doctype}` + set + {reference_doctype_field}=NULL, {reference_name_field}=NULL + where + {reference_doctype_field}=%s and {reference_name_field}=%s""", # nosec + (reference_doctype, reference_name), + ) + + +def clear_timeline_references(link_doctype, link_name): + xhiveframework.db.delete("Communication Link", {"link_doctype": link_doctype, "link_name": link_name}) + + +def insert_feed(doc): + if ( + xhiveframework.flags.in_install + or xhiveframework.flags.in_uninstall + or xhiveframework.flags.in_import + or getattr(doc, "no_feed_on_delete", False) + ): + return + + from xhiveframework.utils import get_fullname + + xhiveframework.get_doc( + { + "doctype": "Comment", + "comment_type": "Deleted", + "reference_doctype": doc.doctype, + "subject": f"{_(doc.doctype)} {doc.name}", + "full_name": get_fullname(doc.owner), + } + ).insert(ignore_permissions=True) + + +def delete_controllers(doctype, module): + """ + Delete controller code in the doctype folder + """ + module_path = get_module_path(module) + dir_path = os.path.join(module_path, "doctype", xhiveframework.scrub(doctype)) + + shutil.rmtree(dir_path) diff --git a/xhiveframework/model/docfield.py b/xhiveframework/model/docfield.py new file mode 100644 index 0000000..b7d8f19 --- /dev/null +++ b/xhiveframework/model/docfield.py @@ -0,0 +1,10 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +"""docfield utililtes""" + +import xhiveframework + + +def supports_translation(fieldtype): + return fieldtype in ["Data", "Select", "Text", "Small Text", "Text Editor"] diff --git a/xhiveframework/model/docstatus.py b/xhiveframework/model/docstatus.py new file mode 100644 index 0000000..9b8f13a --- /dev/null +++ b/xhiveframework/model/docstatus.py @@ -0,0 +1,25 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + + +class DocStatus(int): + def is_draft(self): + return self == self.draft() + + def is_submitted(self): + return self == self.submitted() + + def is_cancelled(self): + return self == self.cancelled() + + @classmethod + def draft(cls): + return cls(0) + + @classmethod + def submitted(cls): + return cls(1) + + @classmethod + def cancelled(cls): + return cls(2) diff --git a/xhiveframework/model/document.py b/xhiveframework/model/document.py new file mode 100644 index 0000000..0c58fcd --- /dev/null +++ b/xhiveframework/model/document.py @@ -0,0 +1,1736 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import hashlib +import json +import time +from collections.abc import Generator, Iterable +from typing import TYPE_CHECKING, Any, Optional + +from werkzeug.exceptions import NotFound + +import xhiveframework +from xhiveframework import _, is_whitelisted, msgprint +from xhiveframework.core.doctype.file.utils import relink_mismatched_files +from xhiveframework.core.doctype.server_script.server_script_utils import run_server_script_for_doc_event +from xhiveframework.desk.form.document_follow import follow_document +from xhiveframework.integrations.doctype.webhook import run_webhooks +from xhiveframework.model import optional_fields, table_fields +from xhiveframework.model.base_document import BaseDocument, get_controller +from xhiveframework.model.docstatus import DocStatus +from xhiveframework.model.naming import set_new_name, validate_name +from xhiveframework.model.utils import is_virtual_doctype +from xhiveframework.model.workflow import set_workflow_state_on_action, validate_workflow +from xhiveframework.types import DF +from xhiveframework.utils import compare, cstr, date_diff, file_lock, flt, get_datetime_str, now +from xhiveframework.utils.data import get_absolute_url, get_datetime, get_timedelta, getdate +from xhiveframework.utils.global_search import update_global_search + +if TYPE_CHECKING: + from xhiveframework.core.doctype.docfield.docfield import DocField + + +DOCUMENT_LOCK_EXPIRTY = 12 * 60 * 60 # All locks expire in 12 hours automatically +DOCUMENT_LOCK_SOFT_EXPIRY = 60 * 60 # Let users force-unlock after 60 minutes + + +def get_doc(*args, **kwargs): + """returns a xhiveframework.model.Document object. + + :param arg1: Document dict or DocType name. + :param arg2: [optional] document name. + :param for_update: [optional] select document for update. + + There are multiple ways to call `get_doc` + + # will fetch the latest user object (with child table) from the database + user = get_doc("User", "test@example.com") + + # create a new object + user = get_doc({ + "doctype":"User" + "email_id": "test@example.com", + "roles: [ + {"role": "System Manager"} + ] + }) + + # create new object with keyword arguments + user = get_doc(doctype='User', email_id='test@example.com') + + # select a document for update + user = get_doc("User", "test@example.com", for_update=True) + """ + if args: + if isinstance(args[0], BaseDocument): + # already a document + return args[0] + elif isinstance(args[0], str): + doctype = args[0] + + elif isinstance(args[0], dict): + # passed a dict + kwargs = args[0] + + else: + raise ValueError("First non keyword argument must be a string or dict") + + if len(args) < 2 and kwargs: + if "doctype" in kwargs: + doctype = kwargs["doctype"] + else: + raise ValueError('"doctype" is a required key') + + controller = get_controller(doctype) + if controller: + return controller(*args, **kwargs) + + raise ImportError(doctype) + + +class Document(BaseDocument): + """All controllers inherit from `Document`.""" + + doctype: DF.Data + name: DF.Data | None + flags: xhiveframework._dict[str, Any] + owner: DF.Link + creation: DF.Datetime + modified: DF.Datetime + modified_by: DF.Link + idx: DF.Int + + def __init__(self, *args, **kwargs): + """Constructor. + + :param arg1: DocType name as string or document **dict** + :param arg2: Document name, if `arg1` is DocType name. + + If DocType name and document name are passed, the object will load + all values (including child documents) from the database. + """ + self.doctype = None + self.name = None + self.flags = xhiveframework._dict() + + if args and args[0]: + if isinstance(args[0], str): + # first arugment is doctype + self.doctype = args[0] + + # doctype for singles, string value or filters for other documents + self.name = self.doctype if len(args) == 1 else args[1] + + # for_update is set in flags to avoid changing load_from_db signature + # since it is used in virtual doctypes and inherited in child classes + self.flags.for_update = kwargs.get("for_update") + self.load_from_db() + return + + if isinstance(args[0], dict): + # first argument is a dict + kwargs = args[0] + + if kwargs: + # init base document + super().__init__(kwargs) + self.init_child_tables() + self.init_valid_columns() + + else: + # incorrect arguments. let's not proceed. + raise ValueError("Illegal arguments") + + @property + def is_locked(self): + return file_lock.lock_exists(self.get_signature()) + + def load_from_db(self): + """Load document and children from database and create properties + from fields""" + self.flags.ignore_children = True + if not getattr(self, "_metaclass", False) and self.meta.issingle: + single_doc = xhiveframework.db.get_singles_dict(self.doctype, for_update=self.flags.for_update) + if not single_doc: + single_doc = xhiveframework.new_doc(self.doctype, as_dict=True) + single_doc["name"] = self.doctype + del single_doc["__islocal"] + + super().__init__(single_doc) + self.init_valid_columns() + self._fix_numeric_types() + + else: + get_value_kwargs = {"for_update": self.flags.for_update, "as_dict": True} + if not isinstance(self.name, dict | list): + get_value_kwargs["order_by"] = None + + d = xhiveframework.db.get_value( + doctype=self.doctype, filters=self.name, fieldname="*", **get_value_kwargs + ) + + if not d: + xhiveframework.throw( + _("{0} {1} not found").format(_(self.doctype), self.name), xhiveframework.DoesNotExistError + ) + + super().__init__(d) + self.flags.pop("ignore_children", None) + + for df in self._get_table_fields(): + # Make sure not to query the DB for a child table, if it is a virtual one. + # During xhiveframework is installed, the property "is_virtual" is not available in tabDocType, so + # we need to filter those cases for the access to xhiveframework.db.get_value() as it would crash otherwise. + if hasattr(self, "doctype") and not hasattr(self, "module") and is_virtual_doctype(df.options): + self.set(df.fieldname, []) + continue + + children = ( + xhiveframework.db.get_values( + df.options, + {"parent": self.name, "parenttype": self.doctype, "parentfield": df.fieldname}, + "*", + as_dict=True, + order_by="idx asc", + for_update=self.flags.for_update, + ) + or [] + ) + + self.set(df.fieldname, children) + + # sometimes __setup__ can depend on child values, hence calling again at the end + if hasattr(self, "__setup__"): + self.__setup__() + + def reload(self): + """Reload document from database""" + self.load_from_db() + + def get_latest(self): + if not getattr(self, "_doc_before_save", None): + self.load_doc_before_save() + + return self._doc_before_save + + def check_permission(self, permtype="read", permlevel=None): + """Raise `xhiveframework.PermissionError` if not permitted""" + if not self.has_permission(permtype): + self.raise_no_permission_to(permtype) + + def has_permission(self, permtype="read", *, debug=False, user=None) -> bool: + """ + Call `xhiveframework.permissions.has_permission` if `ignore_permissions` flag isn't truthy + + :param permtype: `read`, `write`, `submit`, `cancel`, `delete`, etc. + """ + + if self.flags.ignore_permissions: + return True + + import xhiveframework.permissions + + return xhiveframework.permissions.has_permission(self.doctype, permtype, self, debug=debug, user=user) + + def raise_no_permission_to(self, perm_type): + """Raise `xhiveframework.PermissionError`.""" + xhiveframework.flags.error_message = ( + _("Insufficient Permission for {0}").format(self.doctype) + f" ({xhiveframework.bold(_(perm_type))})" + ) + raise xhiveframework.PermissionError + + def insert( + self, + ignore_permissions=None, + ignore_links=None, + ignore_if_duplicate=False, + ignore_mandatory=None, + set_name=None, + set_child_names=True, + ) -> "Document": + """Insert the document in the database (as a new document). + This will check for user permissions and execute `before_insert`, + `validate`, `on_update`, `after_insert` methods if they are written. + + :param ignore_permissions: Do not check permissions if True. + :param ignore_links: Do not check validity of links if True. + :param ignore_if_duplicate: Do not raise error if a duplicate entry exists. + :param ignore_mandatory: Do not check missing mandatory fields if True. + :param set_name: Name to set for the document, if valid. + :param set_child_names: Whether to set names for the child documents. + """ + if self.flags.in_print: + return self + + self.flags.notifications_executed = [] + + if ignore_permissions is not None: + self.flags.ignore_permissions = ignore_permissions + + if ignore_links is not None: + self.flags.ignore_links = ignore_links + + if ignore_mandatory is not None: + self.flags.ignore_mandatory = ignore_mandatory + + self.set("__islocal", True) + + self._set_defaults() + self.set_user_and_timestamp() + self.set_docstatus() + self.check_if_latest() + self._validate_links() + self.check_permission("create") + self.run_method("before_insert") + self.set_new_name(set_name=set_name, set_child_names=set_child_names) + self.set_parent_in_children() + self.validate_higher_perm_levels() + + self.flags.in_insert = True + self.run_before_save_methods() + self._validate() + self.set_docstatus() + self.flags.in_insert = False + + # run validate, on update etc. + + # parent + if getattr(self.meta, "issingle", 0): + self.update_single(self.get_valid_dict()) + else: + self.db_insert(ignore_if_duplicate=ignore_if_duplicate) + + # children + for d in self.get_all_children(): + d.db_insert() + + self.run_method("after_insert") + self.flags.in_insert = True + + if self.get("amended_from"): + self.copy_attachments_from_amended_from() + + relink_mismatched_files(self) + self.run_post_save_methods() + self.flags.in_insert = False + + # delete __islocal + if hasattr(self, "__islocal"): + delattr(self, "__islocal") + + # clear unsaved flag + if hasattr(self, "__unsaved"): + delattr(self, "__unsaved") + + if not (xhiveframework.flags.in_migrate or xhiveframework.local.flags.in_install or xhiveframework.flags.in_setup_wizard): + if xhiveframework.get_cached_value("User", xhiveframework.session.user, "follow_created_documents"): + follow_document(self.doctype, self.name, xhiveframework.session.user) + return self + + def check_if_locked(self): + if self.creation and self.is_locked: + raise xhiveframework.DocumentLockedError + + def save(self, *args, **kwargs): + """Wrapper for _save""" + return self._save(*args, **kwargs) + + def _save(self, ignore_permissions=None, ignore_version=None) -> "Document": + """Save the current document in the database in the **DocType**'s table or + `tabSingles` (for single types). + + This will check for user permissions and execute + `validate` before updating, `on_update` after updating triggers. + + :param ignore_permissions: Do not check permissions if True. + :param ignore_version: Do not save version if True.""" + if self.flags.in_print: + return + + self.flags.notifications_executed = [] + + if ignore_permissions is not None: + self.flags.ignore_permissions = ignore_permissions + + self.flags.ignore_version = xhiveframework.flags.in_test if ignore_version is None else ignore_version + + if self.get("__islocal") or not self.get("name"): + return self.insert() + + self.check_if_locked() + self._set_defaults() + self.check_permission("write", "save") + + self.set_user_and_timestamp() + self.set_docstatus() + self.check_if_latest() + self.set_parent_in_children() + self.set_name_in_children() + + self.validate_higher_perm_levels() + self._validate_links() + self.run_before_save_methods() + + if self._action != "cancel": + self._validate() + + if self._action == "update_after_submit": + self.validate_update_after_submit() + + self.set_docstatus() + + # parent + if self.meta.issingle: + self.update_single(self.get_valid_dict()) + else: + self.db_update() + + self.update_children() + self.run_post_save_methods() + + # clear unsaved flag + if hasattr(self, "__unsaved"): + delattr(self, "__unsaved") + + return self + + def copy_attachments_from_amended_from(self): + """Copy attachments from `amended_from`""" + from xhiveframework.desk.form.load import get_attachments + + # loop through attachments + for attach_item in get_attachments(self.doctype, self.amended_from): + # save attachments to new doc + _file = xhiveframework.get_doc( + { + "doctype": "File", + "file_url": attach_item.file_url, + "file_name": attach_item.file_name, + "attached_to_name": self.name, + "attached_to_doctype": self.doctype, + "folder": "Home/Attachments", + "is_private": attach_item.is_private, + } + ) + _file.save() + + def update_children(self): + """update child tables""" + for df in self.meta.get_table_fields(): + self.update_child_table(df.fieldname, df) + + def update_child_table(self, fieldname: str, df: Optional["DocField"] = None): + """sync child table for given fieldname""" + df: "DocField" = df or self.meta.get_field(fieldname) + all_rows = self.get(df.fieldname) + + # delete rows that do not match the ones in the document + # if the doctype isn't in ignore_children_type flag and isn't virtual + if not ( + df.options in (self.flags.ignore_children_type or ()) + or xhiveframework.get_meta(df.options).is_virtual == 1 + ): + existing_row_names = [row.name for row in all_rows if row.name and not row.is_new()] + + tbl = xhiveframework.qb.DocType(df.options) + qry = ( + xhiveframework.qb.from_(tbl) + .where(tbl.parent == self.name) + .where(tbl.parenttype == self.doctype) + .where(tbl.parentfield == fieldname) + .delete() + ) + + if existing_row_names: + qry = qry.where(tbl.name.notin(existing_row_names)) + + qry.run() + + # update / insert + for d in all_rows: + d: Document + d.db_update() + + def get_doc_before_save(self) -> "Document": + return getattr(self, "_doc_before_save", None) + + def has_value_changed(self, fieldname): + """Return True if value has changed before and after saving.""" + from datetime import date, datetime, timedelta + + previous = self.get_doc_before_save() + + if not previous: + return True + + previous_value = previous.get(fieldname) + current_value = self.get(fieldname) + + if isinstance(previous_value, datetime): + current_value = get_datetime(current_value) + elif isinstance(previous_value, date): + current_value = getdate(current_value) + elif isinstance(previous_value, timedelta): + current_value = get_timedelta(current_value) + + return previous_value != current_value + + def set_new_name(self, force=False, set_name=None, set_child_names=True): + """Calls `xhiveframework.naming.set_new_name` for parent and child docs.""" + + if self.flags.name_set and not force: + return + + autoname = self.meta.autoname or "" + + # If autoname has set as Prompt (name) + if self.get("__newname") and autoname.lower() == "prompt": + self.name = validate_name(self.doctype, self.get("__newname")) + self.flags.name_set = True + return + + if set_name: + self.name = validate_name(self.doctype, set_name) + else: + set_new_name(self) + + if set_child_names: + # set name for children + for d in self.get_all_children(): + set_new_name(d) + + self.flags.name_set = True + + def get_title(self): + """Get the document title based on title_field or `title` or `name`""" + return self.get(self.meta.get_title_field()) or "" + + def set_title_field(self): + """Set title field based on template""" + + def get_values(): + values = self.as_dict() + # format values + for key, value in values.items(): + if value is None: + values[key] = "" + return values + + if self.meta.get("title_field") == "title": + df = self.meta.get_field(self.meta.title_field) + + if df.options: + self.set(df.fieldname, df.options.format(**get_values())) + elif self.is_new() and not self.get(df.fieldname) and df.default: + # set default title for new transactions (if default) + self.set(df.fieldname, df.default.format(**get_values())) + + def update_single(self, d): + """Updates values for Single type Document in `tabSingles`.""" + xhiveframework.db.delete("Singles", {"doctype": self.doctype}) + for field, value in d.items(): + if field != "doctype": + xhiveframework.db.sql( + """insert into `tabSingles` (doctype, field, value) + values (%s, %s, %s)""", + (self.doctype, field, value), + ) + + if self.doctype in xhiveframework.db.value_cache: + del xhiveframework.db.value_cache[self.doctype] + + def set_user_and_timestamp(self): + self._original_modified = self.modified + self.modified = now() + self.modified_by = xhiveframework.session.user + + # We'd probably want the creation and owner to be set via API + # or Data import at some point, that'd have to be handled here + if self.is_new() and not ( + xhiveframework.flags.in_install or xhiveframework.flags.in_patch or xhiveframework.flags.in_migrate + ): + self.creation = self.modified + self.owner = self.modified_by + + for d in self.get_all_children(): + d.modified = self.modified + d.modified_by = self.modified_by + if not d.owner: + d.owner = self.owner + if not d.creation: + d.creation = self.creation + + xhiveframework.flags.currently_saving.append((self.doctype, self.name)) + + def set_docstatus(self): + if self.docstatus is None: + self.docstatus = DocStatus.draft() + + for d in self.get_all_children(): + d.docstatus = self.docstatus + + def _validate(self): + self._validate_mandatory() + self._validate_data_fields() + self._validate_selects() + self._validate_non_negative() + self._validate_length() + self._fix_rating_value() + self._validate_code_fields() + self._sync_autoname_field() + self._extract_images_from_text_editor() + self._sanitize_content() + self._save_passwords() + self.validate_workflow() + + for d in self.get_all_children(): + d._validate_data_fields() + d._validate_selects() + d._validate_non_negative() + d._validate_length() + d._fix_rating_value() + d._validate_code_fields() + d._sync_autoname_field() + d._extract_images_from_text_editor() + d._sanitize_content() + d._save_passwords() + if self.is_new(): + # don't set fields like _assign, _comments for new doc + for fieldname in optional_fields: + self.set(fieldname, None) + else: + self.validate_set_only_once() + + def _validate_non_negative(self): + def get_msg(df): + if self.get("parentfield"): + return "{} {} #{}: {} {}".format( + xhiveframework.bold(_(self.doctype)), + _("Row"), + self.idx, + _("Value cannot be negative for"), + xhiveframework.bold(_(df.label, context=df.parent)), + ) + else: + return _("Value cannot be negative for {0}: {1}").format( + _(df.parent), xhiveframework.bold(_(df.label, context=df.parent)) + ) + + for df in self.meta.get( + "fields", {"non_negative": ("=", 1), "fieldtype": ("in", ["Int", "Float", "Currency"])} + ): + if flt(self.get(df.fieldname)) < 0: + msg = get_msg(df) + xhiveframework.throw(msg, xhiveframework.NonNegativeError, title=_("Negative Value")) + + def _fix_rating_value(self): + for field in self.meta.get("fields", {"fieldtype": "Rating"}): + value = self.get(field.fieldname) + if not isinstance(value, float): + value = flt(value) + + # Make sure rating is between 0 and 1 + self.set(field.fieldname, max(0, min(value, 1))) + + def validate_workflow(self): + """Validate if the workflow transition is valid""" + if xhiveframework.flags.in_install == "xhiveframework": + return + workflow = self.meta.get_workflow() + if workflow: + validate_workflow(self) + if not self._action == "save": + set_workflow_state_on_action(self, workflow, self._action) + + def validate_set_only_once(self): + """Validate that fields are not changed if not in insert""" + set_only_once_fields = self.meta.get_set_only_once_fields() + + if set_only_once_fields and self._doc_before_save: + # document exists before saving + for field in set_only_once_fields: + fail = False + value = self.get(field.fieldname) + original_value = self._doc_before_save.get(field.fieldname) + + if field.fieldtype in table_fields: + fail = not self.is_child_table_same(field.fieldname) + elif field.fieldtype in ("Date", "Datetime", "Time"): + fail = str(value) != str(original_value) + else: + fail = value != original_value + + if fail: + xhiveframework.throw( + _("Value cannot be changed for {0}").format( + xhiveframework.bold(self.meta.get_label(field.fieldname)) + ), + exc=xhiveframework.CannotChangeConstantError, + ) + + return False + + def is_child_table_same(self, fieldname): + """Validate child table is same as original table before saving""" + value = self.get(fieldname) + original_value = self._doc_before_save.get(fieldname) + same = True + + if len(original_value) != len(value): + same = False + else: + # check all child entries + for i, d in enumerate(original_value): + new_child = value[i].as_dict(convert_dates_to_str=True) + original_child = d.as_dict(convert_dates_to_str=True) + + # all fields must be same other than modified and modified_by + for key in ("modified", "modified_by", "creation"): + del new_child[key] + del original_child[key] + + if original_child != new_child: + same = False + break + + return same + + def apply_fieldlevel_read_permissions(self): + """Remove values the user is not allowed to read.""" + if xhiveframework.session.user == "Administrator": + return + + all_fields = self.meta.fields.copy() + for table_field in self.meta.get_table_fields(): + all_fields += xhiveframework.get_meta(table_field.options).fields or [] + + if all(df.permlevel == 0 for df in all_fields): + return + + has_access_to = self.get_permlevel_access("read") + + for df in self.meta.fields: + if df.permlevel and hasattr(self, df.fieldname) and df.permlevel not in has_access_to: + try: + delattr(self, df.fieldname) + except AttributeError: + # hasattr might return True for class attribute which can't be delattr-ed. + continue + + for table_field in self.meta.get_table_fields(): + for df in xhiveframework.get_meta(table_field.options).fields or []: + if df.permlevel and df.permlevel not in has_access_to: + for child in self.get(table_field.fieldname) or []: + if hasattr(child, df.fieldname): + delattr(child, df.fieldname) + + def validate_higher_perm_levels(self): + """If the user does not have permissions at permlevel > 0, then reset the values to original / default""" + if self.flags.ignore_permissions or xhiveframework.flags.in_install: + return + + if xhiveframework.session.user == "Administrator": + return + + has_access_to = self.get_permlevel_access() + high_permlevel_fields = self.meta.get_high_permlevel_fields() + + if high_permlevel_fields: + self.reset_values_if_no_permlevel_access(has_access_to, high_permlevel_fields) + + # If new record then don't reset the values for child table + if self.is_new(): + return + + # check for child tables + for df in self.meta.get_table_fields(): + high_permlevel_fields = xhiveframework.get_meta(df.options).get_high_permlevel_fields() + if high_permlevel_fields: + for d in self.get(df.fieldname): + d.reset_values_if_no_permlevel_access(has_access_to, high_permlevel_fields) + + def get_permlevel_access(self, permission_type="write"): + allowed_permlevels = [] + roles = xhiveframework.get_roles() + + for perm in self.get_permissions(): + if perm.role in roles and perm.get(permission_type) and perm.permlevel not in allowed_permlevels: + allowed_permlevels.append(perm.permlevel) + + return allowed_permlevels + + def has_permlevel_access_to(self, fieldname, df=None, permission_type="read"): + if not df: + df = self.meta.get_field(fieldname) + + return df.permlevel in self.get_permlevel_access(permission_type) + + def get_permissions(self): + if self.meta.istable: + # use parent permissions + permissions = xhiveframework.get_meta(self.parenttype).permissions + else: + permissions = self.meta.permissions + + return permissions + + def _set_defaults(self): + if xhiveframework.flags.in_import: + return + + if self.is_new(): + new_doc = xhiveframework.new_doc(self.doctype, as_dict=True) + self.update_if_missing(new_doc) + + # children + for df in self.meta.get_table_fields(): + new_doc = xhiveframework.new_doc(df.options, parent_doc=self, parentfield=df.fieldname, as_dict=True) + value = self.get(df.fieldname) + if isinstance(value, list): + for d in value: + if d.is_new(): + d.update_if_missing(new_doc) + + def check_if_latest(self): + """Checks if `modified` timestamp provided by document being updated is same as the + `modified` timestamp in the database. If there is a different, the document has been + updated in the database after the current copy was read. Will throw an error if + timestamps don't match. + + Will also validate document transitions (Save > Submit > Cancel) calling + `self.check_docstatus_transition`.""" + + self.load_doc_before_save(raise_exception=True) + + self._action = "save" + previous = self._doc_before_save + + # previous is None for new document insert + if not previous: + self.check_docstatus_transition(0) + return + + if cstr(previous.modified) != cstr(self._original_modified): + xhiveframework.msgprint( + _("Error: Document has been modified after you have opened it") + + (f" ({previous.modified}, {self.modified}). ") + + _("Please refresh to get the latest document."), + raise_exception=xhiveframework.TimestampMismatchError, + ) + + if not self.meta.issingle: + self.check_docstatus_transition(previous.docstatus) + + def check_docstatus_transition(self, to_docstatus): + """Ensures valid `docstatus` transition. + Valid transitions are (number in brackets is `docstatus`): + + - Save (0) > Save (0) + - Save (0) > Submit (1) + - Submit (1) > Submit (1) + - Submit (1) > Cancel (2) + + """ + if not self.docstatus: + self.docstatus = DocStatus.draft() + + if to_docstatus == DocStatus.draft(): + if self.docstatus.is_draft(): + self._action = "save" + elif self.docstatus.is_submitted(): + self._action = "submit" + self.check_permission("submit") + elif self.docstatus.is_cancelled(): + raise xhiveframework.DocstatusTransitionError( + _("Cannot change docstatus from 0 (Draft) to 2 (Cancelled)") + ) + else: + raise xhiveframework.ValidationError(_("Invalid docstatus"), self.docstatus) + + elif to_docstatus == DocStatus.submitted(): + if self.docstatus.is_submitted(): + self._action = "update_after_submit" + self.check_permission("submit") + elif self.docstatus.is_cancelled(): + self._action = "cancel" + self.check_permission("cancel") + elif self.docstatus.is_draft(): + raise xhiveframework.DocstatusTransitionError( + _("Cannot change docstatus from 1 (Submitted) to 0 (Draft)") + ) + else: + raise xhiveframework.ValidationError(_("Invalid docstatus"), self.docstatus) + + elif to_docstatus == DocStatus.cancelled(): + raise xhiveframework.ValidationError(_("Cannot edit cancelled document")) + + def set_parent_in_children(self): + """Updates `parent` and `parenttype` property in all children.""" + for d in self.get_all_children(): + d.parent = self.name + d.parenttype = self.doctype + + def set_name_in_children(self): + # Set name for any new children + for d in self.get_all_children(): + if not d.name: + set_new_name(d) + + def validate_update_after_submit(self): + if self.flags.ignore_validate_update_after_submit: + return + + self._validate_update_after_submit() + for d in self.get_all_children(): + if d.is_new() and self.meta.get_field(d.parentfield).allow_on_submit: + # in case of a new row, don't validate allow on submit, if table is allow on submit + continue + + d._validate_update_after_submit() + + # TODO check only allowed values are updated + + def _validate_mandatory(self): + if self.flags.ignore_mandatory: + return + + missing = self._get_missing_mandatory_fields() + for d in self.get_all_children(): + missing.extend(d._get_missing_mandatory_fields()) + + if not missing: + return + + for idx, msg in missing: # noqa: B007 + msgprint(msg) + + if xhiveframework.flags.print_messages: + print(self.as_json().encode("utf-8")) + + raise xhiveframework.MandatoryError( + "[{doctype}, {name}]: {fields}".format( + fields=", ".join(each[0] for each in missing), doctype=self.doctype, name=self.name + ) + ) + + def _validate_links(self): + if self.flags.ignore_links or self._action == "cancel": + return + + invalid_links, cancelled_links = self.get_invalid_links() + + for d in self.get_all_children(): + result = d.get_invalid_links(is_submittable=self.meta.is_submittable) + invalid_links.extend(result[0]) + cancelled_links.extend(result[1]) + + if invalid_links: + msg = ", ".join(each[2] for each in invalid_links) + xhiveframework.throw(_("Could not find {0}").format(msg), xhiveframework.LinkValidationError) + + if cancelled_links: + msg = ", ".join(each[2] for each in cancelled_links) + xhiveframework.throw(_("Cannot link cancelled document: {0}").format(msg), xhiveframework.CancelledLinkError) + + def get_all_children(self, parenttype=None) -> list["Document"]: + """Returns all children documents from **Table** type fields in a list.""" + + children = [] + + for df in self.meta.get_table_fields(): + if parenttype and df.options != parenttype: + continue + + if value := self.get(df.fieldname): + children.extend(value) + + return children + + def run_method(self, method, *args, **kwargs): + """run standard triggers, plus those in hooks""" + + def fn(self, *args, **kwargs): + method_object = getattr(self, method, None) + + # Cannot have a field with same name as method + # If method found in __dict__, expect it to be callable + if method in self.__dict__ or callable(method_object): + return method_object(*args, **kwargs) + + fn.__name__ = str(method) + out = Document.hook(fn)(self, *args, **kwargs) + + self.run_notifications(method) + run_webhooks(self, method) + run_server_script_for_doc_event(self, method) + + return out + + def run_trigger(self, method, *args, **kwargs): + return self.run_method(method, *args, **kwargs) + + def run_notifications(self, method): + """Run notifications for this method""" + if ( + (xhiveframework.flags.in_import and xhiveframework.flags.mute_emails) + or xhiveframework.flags.in_patch + or xhiveframework.flags.in_install + ): + return + + if self.flags.notifications_executed is None: + self.flags.notifications_executed = [] + + from xhiveframework.email.doctype.notification.notification import evaluate_alert + + if self.flags.notifications is None: + + def _get_notifications(): + """returns enabled notifications for the current doctype""" + + return xhiveframework.get_all( + "Notification", + fields=["name", "event", "method"], + filters={"enabled": 1, "document_type": self.doctype}, + ) + + self.flags.notifications = xhiveframework.cache.hget("notifications", self.doctype, _get_notifications) + + if not self.flags.notifications: + return + + def _evaluate_alert(alert): + if alert.name in self.flags.notifications_executed: + return + + evaluate_alert(self, alert.name, alert.event) + self.flags.notifications_executed.append(alert.name) + + event_map = { + "on_update": "Save", + "after_insert": "New", + "on_submit": "Submit", + "on_cancel": "Cancel", + } + + if not self.flags.in_insert: + # value change is not applicable in insert + event_map["on_change"] = "Value Change" + + for alert in self.flags.notifications: + event = event_map.get(method, None) + if event and alert.event == event: + _evaluate_alert(alert) + elif alert.event == "Method" and method == alert.method: + _evaluate_alert(alert) + + def _submit(self): + """Submit the document. Sets `docstatus` = 1, then saves.""" + self.docstatus = DocStatus.submitted() + return self.save() + + def _cancel(self): + """Cancel the document. Sets `docstatus` = 2, then saves.""" + self.docstatus = DocStatus.cancelled() + return self.save() + + def _rename(self, name: str, merge: bool = False, force: bool = False, validate_rename: bool = True): + """Rename the document. Triggers xhiveframework.rename_doc, then reloads.""" + from xhiveframework.model.rename_doc import rename_doc + + self.name = rename_doc(doc=self, new=name, merge=merge, force=force, validate=validate_rename) + self.reload() + + @xhiveframework.whitelist() + def submit(self): + """Submit the document. Sets `docstatus` = 1, then saves.""" + return self._submit() + + @xhiveframework.whitelist() + def cancel(self): + """Cancel the document. Sets `docstatus` = 2, then saves.""" + return self._cancel() + + @xhiveframework.whitelist() + def rename(self, name: str, merge=False, force=False, validate_rename=True): + """Rename the document to `name`. This transforms the current object.""" + return self._rename(name=name, merge=merge, force=force, validate_rename=validate_rename) + + def delete(self, ignore_permissions=False, force=False, *, delete_permanently=False): + """Delete document.""" + return xhiveframework.delete_doc( + self.doctype, + self.name, + ignore_permissions=ignore_permissions, + flags=self.flags, + force=force, + delete_permanently=delete_permanently, + ) + + def run_before_save_methods(self): + """Run standard methods before `INSERT` or `UPDATE`. Standard Methods are: + + - `validate`, `before_save` for **Save**. + - `validate`, `before_submit` for **Submit**. + - `before_cancel` for **Cancel** + - `before_update_after_submit` for **Update after Submit** + + Will also update title_field if set""" + + self.reset_seen() + + # before_validate method should be executed before ignoring validations + if self._action in ("save", "submit"): + self.run_method("before_validate") + + if self.flags.ignore_validate: + return + + if self._action == "save": + self.run_method("validate") + self.run_method("before_save") + elif self._action == "submit": + self.run_method("validate") + self.run_method("before_submit") + elif self._action == "cancel": + self.run_method("before_cancel") + elif self._action == "update_after_submit": + self.run_method("before_update_after_submit") + + self.set_title_field() + + def load_doc_before_save(self, *, raise_exception: bool = False): + """load existing document from db before saving""" + + self._doc_before_save = None + + if self.is_new(): + return + + try: + self._doc_before_save = xhiveframework.get_doc(self.doctype, self.name, for_update=True) + except xhiveframework.DoesNotExistError: + if raise_exception: + raise + + xhiveframework.clear_last_message() + + def run_post_save_methods(self): + """Run standard methods after `INSERT` or `UPDATE`. Standard Methods are: + + - `on_update` for **Save**. + - `on_update`, `on_submit` for **Submit**. + - `on_cancel` for **Cancel** + - `update_after_submit` for **Update after Submit**""" + + if self._action == "save": + self.run_method("on_update") + elif self._action == "submit": + self.run_method("on_update") + self.run_method("on_submit") + elif self._action == "cancel": + self.run_method("on_cancel") + self.check_no_back_links_exist() + elif self._action == "update_after_submit": + self.run_method("on_update_after_submit") + + self.clear_cache() + + if self.flags.get("notify_update", True): + self.notify_update() + + update_global_search(self) + + self.save_version() + + self.run_method("on_change") + + if (self.doctype, self.name) in xhiveframework.flags.currently_saving: + xhiveframework.flags.currently_saving.remove((self.doctype, self.name)) + + def clear_cache(self): + xhiveframework.clear_document_cache(self.doctype, self.name) + + def reset_seen(self): + """Clear _seen property and set current user as seen""" + if ( + getattr(self.meta, "track_seen", False) + and not getattr(self.meta, "issingle", False) + and not self.is_new() + ): + xhiveframework.db.set_value( + self.doctype, self.name, "_seen", json.dumps([xhiveframework.session.user]), update_modified=False + ) + + def notify_update(self): + """Publish realtime that the current document is modified""" + if xhiveframework.flags.in_patch: + return + + xhiveframework.publish_realtime( + "doc_update", + {"modified": self.modified, "doctype": self.doctype, "name": self.name}, + doctype=self.doctype, + docname=self.name, + after_commit=True, + ) + + if not self.meta.get("read_only") and not self.meta.get("issingle") and not self.meta.get("istable"): + data = {"doctype": self.doctype, "name": self.name, "user": xhiveframework.session.user} + xhiveframework.publish_realtime("list_update", data, after_commit=True) + + def db_set(self, fieldname, value=None, update_modified=True, notify=False, commit=False): + """Set a value in the document object, update the timestamp and update the database. + + WARNING: This method does not trigger controller validations and should + be used very carefully. + + :param fieldname: fieldname of the property to be updated, or a {"field":"value"} dictionary + :param value: value of the property to be updated + :param update_modified: default True. updates the `modified` and `modified_by` properties + :param notify: default False. run doc.notify_update() to send updates via socketio + :param commit: default False. run xhiveframework.db.commit() + """ + if isinstance(fieldname, dict): + self.update(fieldname) + else: + self.set(fieldname, value) + + if update_modified and (self.doctype, self.name) not in xhiveframework.flags.currently_saving: + # don't update modified timestamp if called from post save methods + # like on_update or on_submit + self.set("modified", now()) + self.set("modified_by", xhiveframework.session.user) + + # load but do not reload doc_before_save because before_change or on_change might expect it + if not self.get_doc_before_save(): + self.load_doc_before_save() + + # to trigger notification on value change + self.run_method("before_change") + + if self.name is None: + return + + if self.meta.issingle: + xhiveframework.db.set_single_value( + self.doctype, + fieldname, + value, + modified=self.modified, + modified_by=self.modified_by, + update_modified=update_modified, + ) + else: + xhiveframework.db.set_value( + self.doctype, + self.name, + fieldname, + value, + self.modified, + self.modified_by, + update_modified=update_modified, + ) + + self.run_method("on_change") + + if notify: + self.notify_update() + + if commit: + xhiveframework.db.commit() + + def db_get(self, fieldname): + """get database value for this fieldname""" + return xhiveframework.db.get_value(self.doctype, self.name, fieldname) + + def check_no_back_links_exist(self): + """Check if document links to any active document before Cancel.""" + from xhiveframework.model.delete_doc import check_if_doc_is_dynamically_linked, check_if_doc_is_linked + + if not self.flags.ignore_links: + check_if_doc_is_linked(self, method="Cancel") + check_if_doc_is_dynamically_linked(self, method="Cancel") + + def save_version(self): + """Save version info""" + + # don't track version under following conditions + if ( + not getattr(self.meta, "track_changes", False) + or self.doctype == "Version" + or self.flags.ignore_version + or xhiveframework.flags.in_install + or (not self._doc_before_save and xhiveframework.flags.in_patch) + ): + return + + doc_to_compare = self._doc_before_save + if not doc_to_compare and (amended_from := self.get("amended_from")): + doc_to_compare = xhiveframework.get_doc(self.doctype, amended_from) + + version = xhiveframework.new_doc("Version") + if version.update_version_info(doc_to_compare, self): + version.insert(ignore_permissions=True) + + if not xhiveframework.flags.in_migrate: + # follow since you made a change? + if xhiveframework.get_cached_value("User", xhiveframework.session.user, "follow_created_documents"): + follow_document(self.doctype, self.name, xhiveframework.session.user) + + @staticmethod + def hook(f): + """Decorator: Make method `hookable` (i.e. extensible by another app). + + Note: If each hooked method returns a value (dict), then all returns are + collated in one dict and returned. Ideally, don't return values in hookable + methods, set properties in the document.""" + + def add_to_return_value(self, new_return_value): + if new_return_value is None: + self._return_value = self.get("_return_value") + return + + if isinstance(new_return_value, dict): + if not self.get("_return_value"): + self._return_value = {} + self._return_value.update(new_return_value) + else: + self._return_value = new_return_value + + def compose(fn, *hooks): + def runner(self, method, *args, **kwargs): + add_to_return_value(self, fn(self, *args, **kwargs)) + for f in hooks: + add_to_return_value(self, f(self, method, *args, **kwargs)) + + return self.__dict__.pop("_return_value", None) + + return runner + + def composer(self, *args, **kwargs): + hooks = [] + method = f.__name__ + doc_events = xhiveframework.get_doc_hooks() + for handler in doc_events.get(self.doctype, {}).get(method, []) + doc_events.get("*", {}).get( + method, [] + ): + hooks.append(xhiveframework.get_attr(handler)) + + composed = compose(f, *hooks) + return composed(self, method, *args, **kwargs) + + return composer + + def is_whitelisted(self, method_name): + method = getattr(self, method_name, None) + if not method: + raise NotFound(f"Method {method_name} not found") + + is_whitelisted(getattr(method, "__func__", method)) + + def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None): + """Check that value of fieldname should be 'condition' val2 + else throw Exception.""" + error_condition_map = { + "in": _("one of"), + "not in": _("none of"), + "^": _("beginning with"), + } + + if not doc: + doc = self + + val1 = doc.get_value(fieldname) + + df = doc.meta.get_field(fieldname) + val2 = doc.cast(val2, df) + + if not compare(val1, condition, val2): + label = doc.meta.get_label(fieldname) + condition_str = error_condition_map.get(condition, condition) + if doc.get("parentfield"): + msg = _("Incorrect value in row {0}: {1} must be {2} {3}").format( + doc.idx, label, condition_str, val2 + ) + else: + msg = _("Incorrect value: {0} must be {1} {2}").format(label, condition_str, val2) + + # raise passed exception or True + msgprint(msg, raise_exception=raise_exception or True) + + def validate_table_has_rows(self, parentfield, raise_exception=None): + """Raise exception if Table field is empty.""" + if not (isinstance(self.get(parentfield), list) and len(self.get(parentfield)) > 0): + label = self.meta.get_label(parentfield) + xhiveframework.throw( + _("Table {0} cannot be empty").format(label), raise_exception or xhiveframework.EmptyTableError + ) + + def round_floats_in(self, doc, fieldnames=None): + """Round floats for all `Currency`, `Float`, `Percent` fields for the given doc. + + :param doc: Document whose numeric properties are to be rounded. + :param fieldnames: [Optional] List of fields to be rounded.""" + if not fieldnames: + fieldnames = ( + df.fieldname + for df in doc.meta.get("fields", {"fieldtype": ["in", ["Currency", "Float", "Percent"]]}) + ) + + for fieldname in fieldnames: + doc.set(fieldname, flt(doc.get(fieldname), self.precision(fieldname, doc.get("parentfield")))) + + def get_url(self): + """Returns Desk URL for this document.""" + return get_absolute_url(self.doctype, self.name) + + def add_comment( + self, + comment_type="Comment", + text=None, + comment_email=None, + comment_by=None, + ): + """Add a comment to this document. + + :param comment_type: e.g. `Comment`. See Communication for more info.""" + + return xhiveframework.get_doc( + { + "doctype": "Comment", + "comment_type": comment_type, + "comment_email": comment_email or xhiveframework.session.user, + "comment_by": comment_by, + "reference_doctype": self.doctype, + "reference_name": self.name, + "content": text or comment_type, + } + ).insert(ignore_permissions=True) + + def add_seen(self, user=None): + """add the given/current user to list of users who have seen this document (_seen)""" + if not user: + user = xhiveframework.session.user + + if self.meta.track_seen and not xhiveframework.flags.read_only and not self.meta.issingle: + _seen = self.get("_seen") or [] + _seen = xhiveframework.parse_json(_seen) + + if user not in _seen: + _seen.append(user) + xhiveframework.db.set_value( + self.doctype, self.name, "_seen", json.dumps(_seen), update_modified=False + ) + xhiveframework.local.flags.commit = True + + def add_viewed(self, user=None, force=False, unique_views=False): + """add log to communication when a user views a document""" + if not user: + user = xhiveframework.session.user + + if unique_views and xhiveframework.db.exists( + "View Log", {"reference_doctype": self.doctype, "reference_name": self.name, "viewed_by": user} + ): + return + + if (hasattr(self.meta, "track_views") and self.meta.track_views) or force: + view_log = xhiveframework.get_doc( + { + "doctype": "View Log", + "viewed_by": user, + "reference_doctype": self.doctype, + "reference_name": self.name, + } + ) + if xhiveframework.flags.read_only: + view_log.deferred_insert() + else: + view_log.insert(ignore_permissions=True) + xhiveframework.local.flags.commit = True + + return view_log + + def log_error(self, title=None, message=None): + """Helper function to create an Error Log""" + return xhiveframework.log_error( + message=message, title=title, reference_doctype=self.doctype, reference_name=self.name + ) + + def get_signature(self): + """Return signature (hash) for private URL.""" + return hashlib.sha224(f"{self.doctype}:{self.name}".encode(), usedforsecurity=False).hexdigest() + + def get_document_share_key(self, expires_on=None, no_expiry=False): + if no_expiry: + expires_on = None + + existing_key = xhiveframework.db.exists( + "Document Share Key", + { + "reference_doctype": self.doctype, + "reference_docname": self.name, + "expires_on": expires_on, + }, + ) + if existing_key: + doc = xhiveframework.get_doc("Document Share Key", existing_key) + else: + doc = xhiveframework.new_doc("Document Share Key") + doc.reference_doctype = self.doctype + doc.reference_docname = self.name + doc.expires_on = expires_on + doc.flags.no_expiry = no_expiry + doc.insert(ignore_permissions=True) + + return doc.key + + def get_liked_by(self): + liked_by = getattr(self, "_liked_by", None) + if liked_by: + return json.loads(liked_by) + else: + return [] + + def set_onload(self, key, value): + if not self.get("__onload"): + self.set("__onload", xhiveframework._dict()) + self.get("__onload")[key] = value + + def get_onload(self, key=None): + if not key: + return self.get("__onload", xhiveframework._dict()) + + return self.get("__onload")[key] + + def queue_action(self, action, **kwargs): + """Run an action in background. If the action has an inner function, + like _submit for submit, it will call that instead""" + # call _submit instead of submit, so you can override submit to call + # run_delayed based on some action + # See: Stock Reconciliation + from xhiveframework.utils.background_jobs import enqueue + + if hasattr(self, f"_{action}"): + action = f"_{action}" + + try: + self.lock() + except xhiveframework.DocumentLockedError: + # Allow unlocking if created more than 60 minutes ago + primary_action = None + if file_lock.lock_age(self.get_signature()) > DOCUMENT_LOCK_SOFT_EXPIRY: + primary_action = { + "label": "Force Unlock", + "server_action": "xhiveframework.model.document.unlock_document", + "hide_on_success": True, + "args": { + "doctype": self.doctype, + "name": self.name, + }, + } + + xhiveframework.throw( + _( + "This document is currently locked and queued for execution. Please try again after some time." + ), + title=_("Document Queued"), + primary_action=primary_action, + ) + + enqueue_after_commit = kwargs.pop("enqueue_after_commit", None) + if enqueue_after_commit is None: + enqueue_after_commit = True + + return enqueue( + "xhiveframework.model.document.execute_action", + __doctype=self.doctype, + __name=self.name, + __action=action, + enqueue_after_commit=enqueue_after_commit, + **kwargs, + ) + + def lock(self, timeout=None): + """Creates a lock file for the given document. If timeout is set, + it will retry every 1 second for acquiring the lock again + + :param timeout: Timeout in seconds, default 0""" + signature = self.get_signature() + if file_lock.lock_exists(signature): + lock_exists = True + if file_lock.lock_age(signature) > DOCUMENT_LOCK_EXPIRTY: + file_lock.delete_lock(signature) + lock_exists = False + if timeout: + for _ in range(timeout): + time.sleep(1) + if not file_lock.lock_exists(signature): + lock_exists = False + break + if lock_exists: + raise xhiveframework.DocumentLockedError + file_lock.create_lock(signature) + xhiveframework.local.locked_documents.append(self) + + def unlock(self): + """Delete the lock file for this document""" + file_lock.delete_lock(self.get_signature()) + if self in xhiveframework.local.locked_documents: + xhiveframework.local.locked_documents.remove(self) + + def validate_from_to_dates(self, from_date_field: str, to_date_field: str) -> None: + """Validate that the value of `from_date_field` is not later than the value of `to_date_field`.""" + from_date = self.get(from_date_field) + to_date = self.get(to_date_field) + if not (from_date and to_date): + return + + if date_diff(to_date, from_date) < 0: + xhiveframework.throw( + _("{0} must be after {1}").format( + xhiveframework.bold(_(self.meta.get_label(to_date_field))), + xhiveframework.bold(_(self.meta.get_label(from_date_field))), + ), + xhiveframework.exceptions.InvalidDates, + ) + + def get_assigned_users(self): + assigned_users = xhiveframework.get_all( + "ToDo", + fields=["allocated_to"], + filters={ + "reference_type": self.doctype, + "reference_name": self.name, + "status": ("!=", "Cancelled"), + }, + pluck="allocated_to", + ) + + return set(assigned_users) + + def add_tag(self, tag): + """Add a Tag to this document""" + from xhiveframework.desk.doctype.tag.tag import DocTags + + DocTags(self.doctype).add(self.name, tag) + + def get_tags(self): + """Return a list of Tags attached to this document""" + from xhiveframework.desk.doctype.tag.tag import DocTags + + return DocTags(self.doctype).get_tags(self.name).split(",")[1:] + + def deferred_insert(self) -> None: + """Push the document to redis temporarily and insert later. + + WARN: This doesn't guarantee insertion as redis can be restarted + before data is flushed to database. + """ + + from xhiveframework.deferred_insert import deferred_insert + + self.set_user_and_timestamp() + + doc = self.get_valid_dict(convert_dates_to_str=True, ignore_virtual=True) + deferred_insert(doctype=self.doctype, records=doc) + + def __repr__(self): + name = self.name or "unsaved" + doctype = self.__class__.__name__ + + docstatus = f" docstatus={self.docstatus}" if self.docstatus else "" + parent = f" parent={self.parent}" if getattr(self, "parent", None) else "" + + return f"<{doctype}: {name}{docstatus}{parent}>" + + def __str__(self): + name = self.name or "unsaved" + doctype = self.__class__.__name__ + + return f"{doctype}({name})" + + +def execute_action(__doctype, __name, __action, **kwargs): + """Execute an action on a document (called by background worker)""" + doc = xhiveframework.get_doc(__doctype, __name) + doc.unlock() + try: + getattr(doc, __action)(**kwargs) + except Exception: + xhiveframework.db.rollback() + + # add a comment (?) + if xhiveframework.message_log: + msg = xhiveframework.message_log[-1].get("message") + else: + msg = "
      " + xhiveframework.get_traceback() + "
      " + + doc.add_comment("Comment", _("Action Failed") + "

      " + msg) + doc.notify_update() + + +def bulk_insert( + doctype: str, + documents: Iterable["Document"], + ignore_duplicates: bool = False, + chunk_size=10_000, +): + """Insert simple Documents objects to database in bulk. + + Warning/Info: + - All documents are inserted without triggering ANY hooks. + - This function assumes you've done the due dilligence and inserts in similar fashion as db_insert + - Documents can be any iterable / generator containing Document objects + """ + + doctype_meta = xhiveframework.get_meta(doctype) + documents = list(documents) + + valid_column_map = { + doctype: doctype_meta.get_valid_columns(), + } + values_map = { + doctype: _document_values_generator(documents, valid_column_map[doctype]), + } + + for child_table in doctype_meta.get_table_fields(): + valid_column_map[child_table.options] = xhiveframework.get_meta(child_table.options).get_valid_columns() + values_map[child_table.options] = _document_values_generator( + ( + ch_doc + for ch_doc in ( + child_docs for doc in documents for child_docs in doc.get(child_table.fieldname) + ) + ), + valid_column_map[child_table.options], + ) + + for dt, docs in values_map.items(): + xhiveframework.db.bulk_insert( + dt, valid_column_map[dt], docs, ignore_duplicates=ignore_duplicates, chunk_size=chunk_size + ) + + +def _document_values_generator( + documents: Iterable["Document"], + columns: list[str], +) -> Generator[tuple[Any], None, None]: + for doc in documents: + doc.creation = doc.modified = now() + doc.owner = doc.modified_by = xhiveframework.session.user + doc_values = doc.get_valid_dict( + convert_dates_to_str=True, + ignore_nulls=True, + ignore_virtual=True, + ) + yield tuple(doc_values.get(col) for col in columns) + + +@xhiveframework.whitelist() +def unlock_document(doctype: str | None = None, name: str | None = None, args=None): + if not doctype and not name and args: + # Backward compatibility + doctype = str(args["doctype"]) + name = str(args["name"]) + xhiveframework.get_doc(doctype, name).unlock() + xhiveframework.msgprint(xhiveframework._("Document Unlocked"), alert=True) diff --git a/xhiveframework/model/dynamic_links.py b/xhiveframework/model/dynamic_links.py new file mode 100644 index 0000000..ed3594c --- /dev/null +++ b/xhiveframework/model/dynamic_links.py @@ -0,0 +1,63 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + +# select doctypes that are accessed by the user (not read_only) first, so that the +# the validation message shows the user-facing doctype first. +# For example Journal Entry should be validated before GL Entry (which is an internal doctype) + +dynamic_link_queries = [ + """select `tabDocField`.parent, + `tabDocType`.read_only, `tabDocType`.in_create, + `tabDocField`.fieldname, `tabDocField`.options + from `tabDocField`, `tabDocType` + where `tabDocField`.fieldtype='Dynamic Link' and + `tabDocType`.`name`=`tabDocField`.parent and `tabDocType`.is_virtual = 0 + order by `tabDocType`.read_only, `tabDocType`.in_create""", + """select `tabCustom Field`.dt as parent, + `tabDocType`.read_only, `tabDocType`.in_create, + `tabCustom Field`.fieldname, `tabCustom Field`.options + from `tabCustom Field`, `tabDocType` + where `tabCustom Field`.fieldtype='Dynamic Link' and + `tabDocType`.`name`=`tabCustom Field`.dt + order by `tabDocType`.read_only, `tabDocType`.in_create""", +] + + +def get_dynamic_link_map(for_delete=False): + """Build a map of all dynamically linked tables. For example, + if Note is dynamically linked to ToDo, the function will return + `{"Note": ["ToDo"], "Sales Invoice": ["Journal Entry Detail"]}` + + Note: Will not map single doctypes + """ + if getattr(xhiveframework.local, "dynamic_link_map", None) is None or xhiveframework.flags.in_test: + # Build from scratch + dynamic_link_map = {} + for df in get_dynamic_links(): + meta = xhiveframework.get_meta(df.parent) + if meta.issingle: + # always check in Single DocTypes + dynamic_link_map.setdefault(meta.name, []).append(df) + else: + try: + links = xhiveframework.db.sql_list( + """select distinct {options} from `tab{parent}`""".format(**df) + ) + for doctype in links: + dynamic_link_map.setdefault(doctype, []).append(df) + except xhiveframework.db.TableMissingError: + pass + + xhiveframework.local.dynamic_link_map = dynamic_link_map + return xhiveframework.local.dynamic_link_map + + +def get_dynamic_links(): + """Return list of dynamic link fields as DocField. + Uses cache if possible""" + df = [] + for query in dynamic_link_queries: + df += xhiveframework.db.sql(query, as_dict=True) + return df diff --git a/xhiveframework/model/mapper.py b/xhiveframework/model/mapper.py new file mode 100644 index 0000000..7997ce9 --- /dev/null +++ b/xhiveframework/model/mapper.py @@ -0,0 +1,267 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import json + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import child_table_fields, default_fields, table_fields +from xhiveframework.utils import cstr + + +@xhiveframework.whitelist() +def make_mapped_doc(method, source_name, selected_children=None, args=None): + """Returns the mapped document calling the given mapper method. + Sets selected_children as flags for the `get_mapped_doc` method. + + Called from `open_mapped_doc` from create_new.js""" + + for hook in reversed(xhiveframework.get_hooks("override_whitelisted_methods", {}).get(method, [])): + # override using the first hook + method = hook + break + + method = xhiveframework.get_attr(method) + + if method not in xhiveframework.whitelisted: + raise xhiveframework.PermissionError + + if selected_children: + selected_children = json.loads(selected_children) + + if args: + xhiveframework.flags.args = xhiveframework._dict(json.loads(args)) + + xhiveframework.flags.selected_children = selected_children or None + + return method(source_name) + + +@xhiveframework.whitelist() +def map_docs(method, source_names, target_doc, args=None): + '''Returns the mapped document calling the given mapper method + with each of the given source docs on the target doc + + :param args: Args as string to pass to the mapper method + E.g. args: "{ 'supplier': 'XYZ' }"''' + + method = xhiveframework.get_attr(method) + if method not in xhiveframework.whitelisted: + raise xhiveframework.PermissionError + + for src in json.loads(source_names): + _args = (src, target_doc, json.loads(args)) if args else (src, target_doc) + target_doc = method(*_args) + return target_doc + + +def get_mapped_doc( + from_doctype, + from_docname, + table_maps, + target_doc=None, + postprocess=None, + ignore_permissions=False, + ignore_child_tables=False, + cached=False, +): + apply_strict_user_permissions = xhiveframework.get_system_settings("apply_strict_user_permissions") + + # main + if not target_doc: + target_doc = xhiveframework.new_doc(table_maps[from_doctype]["doctype"]) + elif isinstance(target_doc, str): + target_doc = xhiveframework.get_doc(json.loads(target_doc)) + + if ( + not apply_strict_user_permissions + and not ignore_permissions + and not target_doc.has_permission("create") + ): + target_doc.raise_no_permission_to("create") + + if cached: + source_doc = xhiveframework.get_cached_doc(from_doctype, from_docname) + else: + source_doc = xhiveframework.get_doc(from_doctype, from_docname) + + if not ignore_permissions: + if not source_doc.has_permission("read"): + source_doc.raise_no_permission_to("read") + + map_doc(source_doc, target_doc, table_maps[source_doc.doctype]) + + row_exists_for_parentfield = {} + + # children + if not ignore_child_tables: + for df in source_doc.meta.get_table_fields(): + source_child_doctype = df.options + table_map = table_maps.get(source_child_doctype) + + # if table_map isn't explicitly specified check if both source and target have the same fieldname and same table options and both of them don't have no_copy + if not table_map: + target_df = target_doc.meta.get_field(df.fieldname) + if target_df: + target_child_doctype = target_df.options + if ( + target_df + and target_child_doctype == source_child_doctype + and not df.no_copy + and not target_df.no_copy + ): + table_map = {"doctype": target_child_doctype} + + if table_map: + for source_d in source_doc.get(df.fieldname): + if "condition" in table_map: + if not table_map["condition"](source_d): + continue + + # if children are selected (checked from UI) for this table type, + # and this record is not in the selected children, then continue + if ( + xhiveframework.flags.selected_children + and (df.fieldname in xhiveframework.flags.selected_children) + and source_d.name not in xhiveframework.flags.selected_children[df.fieldname] + ): + continue + + target_child_doctype = table_map["doctype"] + target_parentfield = target_doc.get_parentfield_of_doctype(target_child_doctype) + + # does row exist for a parentfield? + if target_parentfield not in row_exists_for_parentfield: + row_exists_for_parentfield[target_parentfield] = ( + True if target_doc.get(target_parentfield) else False + ) + + if table_map.get("ignore"): + continue + + if table_map.get("add_if_empty") and row_exists_for_parentfield.get(target_parentfield): + continue + + if table_map.get("filter") and table_map.get("filter")(source_d): + continue + + map_child_doc(source_d, target_doc, table_map, source_doc) + + if postprocess: + postprocess(source_doc, target_doc) + + target_doc.run_method("after_mapping", source_doc) + target_doc.set_onload("load_after_mapping", True) + + if apply_strict_user_permissions and not ignore_permissions and not target_doc.has_permission("create"): + target_doc.raise_no_permission_to("create") + + return target_doc + + +def map_doc(source_doc, target_doc, table_map, source_parent=None): + if table_map.get("validation"): + for key, condition in table_map["validation"].items(): + if condition[0] == "=" and source_doc.get(key) != condition[1]: + xhiveframework.throw( + _("Cannot map because following condition fails:") + f" {key}={cstr(condition[1])}" + ) + + map_fields(source_doc, target_doc, table_map, source_parent) + + if "postprocess" in table_map: + table_map["postprocess"](source_doc, target_doc, source_parent) + + +def map_fields(source_doc, target_doc, table_map, source_parent): + no_copy_fields = set( + [ + d.fieldname + for d in source_doc.meta.get("fields") + if (d.no_copy == 1 or d.fieldtype in table_fields) + ] + + [ + d.fieldname + for d in target_doc.meta.get("fields") + if (d.no_copy == 1 or d.fieldtype in table_fields) + ] + + list(default_fields) + + list(child_table_fields) + + list(table_map.get("field_no_map", [])) + ) + + for df in target_doc.meta.get("fields"): + if df.fieldname not in no_copy_fields: + # map same fields + val = source_doc.get(df.fieldname) + if val not in (None, ""): + target_doc.set(df.fieldname, val) + + elif df.fieldtype == "Link": + if not target_doc.get(df.fieldname): + # map link fields having options == source doctype + if df.options == source_doc.doctype: + target_doc.set(df.fieldname, source_doc.name) + + elif source_parent and df.options == source_parent.doctype: + target_doc.set(df.fieldname, source_parent.name) + + # map other fields + field_map = table_map.get("field_map") + + if field_map: + if isinstance(field_map, dict): + for source_key, target_key in field_map.items(): + val = source_doc.get(source_key) + if val not in (None, ""): + target_doc.set(target_key, val) + else: + for fmap in field_map: + val = source_doc.get(fmap[0]) + if val not in (None, ""): + target_doc.set(fmap[1], val) + + # map idx + if source_doc.idx: + target_doc.idx = source_doc.idx + + # add fetch + for df in target_doc.meta.get("fields", {"fieldtype": "Link"}): + if target_doc.get(df.fieldname): + map_fetch_fields(target_doc, df, no_copy_fields) + + +def map_fetch_fields(target_doc, df, no_copy_fields): + linked_doc = None + + # options should be like "link_fieldname.fieldname_in_liked_doc" + for fetch_df in target_doc.meta.get("fields", {"fetch_from": f"^{df.fieldname}."}): + if not (fetch_df.fieldtype == "Read Only" or fetch_df.read_only): + continue + + if ( + not target_doc.get(fetch_df.fieldname) or fetch_df.fieldtype == "Read Only" + ) and fetch_df.fieldname not in no_copy_fields: + source_fieldname = fetch_df.fetch_from.split(".")[1] + + if not linked_doc: + try: + linked_doc = xhiveframework.get_doc(df.options, target_doc.get(df.fieldname)) + except Exception: + return + + val = linked_doc.get(source_fieldname) + + if val not in (None, ""): + target_doc.set(fetch_df.fieldname, val) + + +def map_child_doc(source_d, target_parent, table_map, source_parent=None): + target_child_doctype = table_map["doctype"] + target_parentfield = target_parent.get_parentfield_of_doctype(target_child_doctype) + target_d = xhiveframework.new_doc(target_child_doctype, parent_doc=target_parent, parentfield=target_parentfield) + + map_doc(source_d, target_d, table_map, source_parent) + + target_d.idx = None + target_parent.append(target_parentfield, target_d) + return target_d diff --git a/xhiveframework/model/meta.py b/xhiveframework/model/meta.py new file mode 100644 index 0000000..36b69ff --- /dev/null +++ b/xhiveframework/model/meta.py @@ -0,0 +1,895 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +# metadata + +""" +Load metadata (DocType) class + +Example: + + meta = xhiveframework.get_meta('User') + if meta.has_field('first_name'): + print("DocType" table has field "first_name") + + +""" +import json +import os +from datetime import datetime + +import click + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import ( + child_table_fields, + data_fieldtypes, + default_fields, + no_value_fields, + optional_fields, + table_fields, +) +from xhiveframework.model.base_document import ( + DOCTYPE_TABLE_FIELDS, + TABLE_DOCTYPES_FOR_DOCTYPE, + BaseDocument, +) +from xhiveframework.model.document import Document +from xhiveframework.model.workflow import get_workflow_name +from xhiveframework.modules import load_doctype_module +from xhiveframework.utils import cast, cint, cstr + +DEFAULT_FIELD_LABELS = { + "name": lambda: _("ID"), + "creation": lambda: _("Created On"), + "docstatus": lambda: _("Document Status"), + "idx": lambda: _("Index"), + "modified": lambda: _("Last Updated On"), + "modified_by": lambda: _("Last Updated By"), + "owner": lambda: _("Created By"), + "_user_tags": lambda: _("Tags"), + "_liked_by": lambda: _("Liked By"), + "_comments": lambda: _("Comments"), + "_assign": lambda: _("Assigned To"), +} + + +def get_meta(doctype, cached=True) -> "Meta": + cached = cached and isinstance(doctype, str) + if cached and (meta := xhiveframework.cache.hget("doctype_meta", doctype)): + return meta + + meta = Meta(doctype) + xhiveframework.cache.hset("doctype_meta", meta.name, meta) + return meta + + +def load_meta(doctype): + return Meta(doctype) + + +def get_table_columns(doctype): + return xhiveframework.db.get_table_columns(doctype) + + +def load_doctype_from_file(doctype): + fname = xhiveframework.scrub(doctype) + with open(xhiveframework.get_app_path("xhiveframework", "core", "doctype", fname, fname + ".json")) as f: + txt = json.loads(f.read()) + + for d in txt.get("fields", []): + d["doctype"] = "DocField" + + for d in txt.get("permissions", []): + d["doctype"] = "DocPerm" + + txt["fields"] = [BaseDocument(d) for d in txt["fields"]] + if "permissions" in txt: + txt["permissions"] = [BaseDocument(d) for d in txt["permissions"]] + + return txt + + +class Meta(Document): + _metaclass = True + default_fields = list(default_fields)[1:] + special_doctypes = frozenset( + ( + "DocField", + "DocPerm", + "DocType", + "Module Def", + "DocType Action", + "DocType Link", + "DocType State", + ) + ) + standard_set_once_fields = ( + xhiveframework._dict(fieldname="creation", fieldtype="Datetime"), + xhiveframework._dict(fieldname="owner", fieldtype="Data"), + ) + + def __init__(self, doctype): + if isinstance(doctype, Document): + super().__init__(doctype.as_dict()) + else: + super().__init__("DocType", doctype) + + self.process() + + def load_from_db(self): + try: + super().load_from_db() + except xhiveframework.DoesNotExistError: + if self.doctype == "DocType" and self.name in self.special_doctypes: + self.__dict__.update(load_doctype_from_file(self.name)) + else: + raise + + def process(self): + # don't process for special doctypes + # prevents circular dependency + if self.name in self.special_doctypes: + self.init_field_caches() + return + + self.add_custom_fields() + self.apply_property_setters() + self.init_field_caches() + self.sort_fields() + self.get_valid_columns() + self.set_custom_permissions() + self.add_custom_links_and_actions() + + def as_dict(self, no_nulls=False): + def serialize(doc): + out = {} + for key, value in doc.__dict__.items(): + if isinstance(value, list | tuple): + if not value or not isinstance(value[0], BaseDocument): + # non standard list object, skip + continue + + value = [serialize(d) for d in value] + + if (not no_nulls and value is None) or isinstance( + value, str | int | float | datetime | list | tuple + ): + out[key] = value + + # set empty lists for unset table fields + for fieldname in TABLE_DOCTYPES_FOR_DOCTYPE.keys(): + if out.get(fieldname) is None: + out[fieldname] = [] + + return out + + return serialize(self) + + def get_link_fields(self): + return self.get("fields", {"fieldtype": "Link", "options": ["!=", "[Select]"]}) + + def get_data_fields(self): + return self.get("fields", {"fieldtype": "Data"}) + + def get_phone_fields(self): + return self.get("fields", {"fieldtype": "Phone"}) + + def get_dynamic_link_fields(self): + if not hasattr(self, "_dynamic_link_fields"): + self._dynamic_link_fields = self.get("fields", {"fieldtype": "Dynamic Link"}) + return self._dynamic_link_fields + + def get_select_fields(self): + return self.get("fields", {"fieldtype": "Select", "options": ["not in", ["[Select]", "Loading..."]]}) + + def get_image_fields(self): + return self.get("fields", {"fieldtype": "Attach Image"}) + + def get_code_fields(self): + return self.get("fields", {"fieldtype": "Code"}) + + def get_set_only_once_fields(self): + """Return fields with `set_only_once` set""" + if not hasattr(self, "_set_only_once_fields"): + self._set_only_once_fields = self.get("fields", {"set_only_once": 1}) + fieldnames = [d.fieldname for d in self._set_only_once_fields] + + for df in self.standard_set_once_fields: + if df.fieldname not in fieldnames: + self._set_only_once_fields.append(df) + + return self._set_only_once_fields + + def get_table_fields(self): + return self._table_fields + + def get_global_search_fields(self): + """Returns list of fields with `in_global_search` set and `name` if set""" + fields = self.get("fields", {"in_global_search": 1, "fieldtype": ["not in", no_value_fields]}) + if getattr(self, "show_name_in_global_search", None): + fields.append(xhiveframework._dict(fieldtype="Data", fieldname="name", label="Name")) + + return fields + + def get_valid_columns(self) -> list[str]: + if not hasattr(self, "_valid_columns"): + table_exists = xhiveframework.db.table_exists(self.name) + if self.name in self.special_doctypes and table_exists: + self._valid_columns = get_table_columns(self.name) + else: + self._valid_columns = self.default_fields + [ + df.fieldname for df in self.get("fields") if df.fieldtype in data_fieldtypes + ] + if self.istable: + self._valid_columns += list(child_table_fields) + + return self._valid_columns + + def get_table_field_doctype(self, fieldname): + return TABLE_DOCTYPES_FOR_DOCTYPE.get(fieldname) + + def get_field(self, fieldname): + """Return docfield from meta""" + + return self._fields.get(fieldname) + + def has_field(self, fieldname): + """Returns True if fieldname exists""" + + return fieldname in self._fields + + def get_label(self, fieldname): + """Get label of the given fieldname""" + if df := self.get_field(fieldname): + return df.get("label") + + if fieldname in DEFAULT_FIELD_LABELS: + return DEFAULT_FIELD_LABELS[fieldname]() + + return "No Label" + + def get_options(self, fieldname): + return self.get_field(fieldname).options + + def get_link_doctype(self, fieldname): + df = self.get_field(fieldname) + + if df.fieldtype == "Link": + return df.options + + if df.fieldtype == "Dynamic Link": + return self.get_options(df.options) + + def get_search_fields(self): + search_fields = self.search_fields or "name" + search_fields = [d.strip() for d in search_fields.split(",")] + if "name" not in search_fields: + search_fields.append("name") + + return search_fields + + def get_fields_to_fetch(self, link_fieldname=None): + """Returns a list of docfield objects for fields whose values + are to be fetched and updated for a particular link field + + These fields are of type Data, Link, Text, Readonly and their + fetch_from property is set as `link_fieldname`.`source_fieldname`""" + + out = [] + + if not link_fieldname: + link_fields = [df.fieldname for df in self.get_link_fields()] + + for df in self.fields: + if df.fieldtype not in no_value_fields and getattr(df, "fetch_from", None): + if link_fieldname: + if df.fetch_from.startswith(link_fieldname + "."): + out.append(df) + else: + if "." in df.fetch_from: + fieldname = df.fetch_from.split(".", 1)[0] + if fieldname in link_fields: + out.append(df) + + return out + + def get_list_fields(self): + list_fields = ["name"] + [ + d.fieldname for d in self.fields if (d.in_list_view and d.fieldtype in data_fieldtypes) + ] + if self.title_field and self.title_field not in list_fields: + list_fields.append(self.title_field) + return list_fields + + def get_custom_fields(self): + return [d for d in self.fields if getattr(d, "is_custom_field", False)] + + def get_title_field(self): + """Return the title field of this doctype, + explict via `title_field`, or `title` or `name`""" + title_field = getattr(self, "title_field", None) + if not title_field and self.has_field("title"): + title_field = "title" + if not title_field: + title_field = "name" + + return title_field + + def get_translatable_fields(self): + """Return all fields that are translation enabled""" + return [d.fieldname for d in self.fields if d.translatable] + + def is_translatable(self, fieldname): + """Return true of false given a field""" + + if field := self.get_field(fieldname): + return field.translatable + + def get_workflow(self): + return get_workflow_name(self.name) + + def get_naming_series_options(self) -> list[str]: + """Get list naming series options.""" + + if field := self.get_field("naming_series"): + options = field.options or "" + return options.split("\n") + + return [] + + def add_custom_fields(self): + if not xhiveframework.db.table_exists("Custom Field"): + return + + custom_fields = xhiveframework.db.get_values( + "Custom Field", + filters={"dt": self.name}, + fieldname="*", + as_dict=True, + order_by="idx", + update={"is_custom_field": 1}, + ) + + if not custom_fields: + return + + self.extend("fields", custom_fields) + + def apply_property_setters(self): + """ + Property Setters are set via Customize Form. They override standard properties + of the doctype or its child properties like fields, links etc. This method + applies the customized properties over the standard meta object + """ + if not xhiveframework.db.table_exists("Property Setter"): + return + + property_setters = xhiveframework.db.get_values( + "Property Setter", + filters={"doc_type": self.name}, + fieldname="*", + as_dict=True, + ) + + if not property_setters: + return + + for ps in property_setters: + if ps.doctype_or_field == "DocType": + self.set(ps.property, cast(ps.property_type, ps.value)) + + elif ps.doctype_or_field == "DocField": + for d in self.fields: + if d.fieldname == ps.field_name: + d.set(ps.property, cast(ps.property_type, ps.value)) + break + + elif ps.doctype_or_field == "DocType Link": + for d in self.links: + if d.name == ps.row_name: + d.set(ps.property, cast(ps.property_type, ps.value)) + break + + elif ps.doctype_or_field == "DocType Action": + for d in self.actions: + if d.name == ps.row_name: + d.set(ps.property, cast(ps.property_type, ps.value)) + break + + elif ps.doctype_or_field == "DocType State": + for d in self.states: + if d.name == ps.row_name: + d.set(ps.property, cast(ps.property_type, ps.value)) + break + + def add_custom_links_and_actions(self): + for doctype, fieldname in ( + ("DocType Link", "links"), + ("DocType Action", "actions"), + ("DocType State", "states"), + ): + # ignore_ddl because the `custom` column was added later via a patch + for d in xhiveframework.get_all( + doctype, fields="*", filters=dict(parent=self.name, custom=1), ignore_ddl=True + ): + self.append(fieldname, d) + + # set the fields in order if specified + # order is saved as `links_order` + order = json.loads(self.get(f"{fieldname}_order") or "[]") + if order: + name_map = {d.name: d for d in self.get(fieldname)} + new_list = [name_map[name] for name in order if name in name_map] + # add the missing items that have not be added + # maybe these items were added to the standard product + # after the customization was done + for d in self.get(fieldname): + if d not in new_list: + new_list.append(d) + + self.set(fieldname, new_list) + + def init_field_caches(self): + # field map + self._fields = {field.fieldname: field for field in self.fields} + + # table fields + if self.name == "DocType": + self._table_fields = DOCTYPE_TABLE_FIELDS + else: + self._table_fields = self.get("fields", {"fieldtype": ["in", table_fields]}) + + def sort_fields(self): + """ + Sort fields on the basis of following rules (priority descending): + - `field_order` property setter + - `insert_after` computed based on default order for standard fields + - `insert_after` property for custom fields + """ + + if field_order := getattr(self, "field_order", []): + field_order = [fieldname for fieldname in json.loads(field_order) if fieldname in self._fields] + + # all fields match, best case scenario + if len(field_order) == len(self.fields): + self._update_fields_based_on_order(field_order) + return + + # if the first few standard fields are not in the field order, prepare to prepend them + if self.fields[0].fieldname not in field_order: + fields_to_prepend = [] + standard_field_found = False + + for fieldname, field in self._fields.items(): + if getattr(field, "is_custom_field", False): + # all custom fields from here on + break + + if fieldname in field_order: + standard_field_found = True + break + + fields_to_prepend.append(fieldname) + + if standard_field_found: + field_order = fields_to_prepend + field_order + else: + # worst case scenario, invalidate field_order + field_order = fields_to_prepend + + existing_fields = set(field_order) if field_order else False + insert_after_map = {} + + for index, field in enumerate(self.fields): + if existing_fields and field.fieldname in existing_fields: + continue + + if not getattr(field, "is_custom_field", False): + if existing_fields: + # compute insert_after from previous field + insert_after_map.setdefault(self.fields[index - 1].fieldname, []).append(field.fieldname) + else: + field_order.append(field.fieldname) + + elif insert_after := getattr(field, "insert_after", None): + insert_after_map.setdefault(insert_after, []).append(field.fieldname) + + else: + # if custom field is at the top, insert after is None + field_order.insert(0, field.fieldname) + + if insert_after_map: + _update_field_order_based_on_insert_after(field_order, insert_after_map) + + self._update_fields_based_on_order(field_order) + + def _update_fields_based_on_order(self, field_order): + sorted_fields = [] + + for idx, fieldname in enumerate(field_order, 1): + field = self._fields[fieldname] + field.idx = idx + sorted_fields.append(field) + + self.fields = sorted_fields + + def set_custom_permissions(self): + """Reset `permissions` with Custom DocPerm if exists""" + if xhiveframework.flags.in_patch or xhiveframework.flags.in_install: + return + + if not self.istable and self.name not in ("DocType", "DocField", "DocPerm", "Custom DocPerm"): + custom_perms = xhiveframework.get_all( + "Custom DocPerm", + fields="*", + filters=dict(parent=self.name), + update=dict(doctype="Custom DocPerm"), + ) + if custom_perms: + self.permissions = [Document(d) for d in custom_perms] + + def get_fieldnames_with_value(self, with_field_meta=False, with_virtual_fields=False): + def is_value_field(docfield): + return not ( + not with_virtual_fields + and 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", + { + "fieldtype": "Link", + "parent": self.name, + "ignore_user_permissions": ("!=", 1), + "options": ("in", user_permission_doctypes), + }, + ) + + if self.name in user_permission_doctypes: + fields.append(xhiveframework._dict({"label": "Name", "fieldname": "name", "options": self.name})) + + return fields + + def get_high_permlevel_fields(self): + """Build list of fields with high perm level and all the higher perm levels defined.""" + if not hasattr(self, "high_permlevel_fields"): + self.high_permlevel_fields = [df for df in self.fields if df.permlevel > 0] + return self.high_permlevel_fields + + def get_permitted_fieldnames( + self, + parenttype=None, + *, + user=None, + permission_type="read", + with_virtual_fields=True, + ): + """Build list of `fieldname` with read perm level and all the higher perm levels defined. + + Note: If permissions are not defined for DocType, return all the fields with value. + """ + permitted_fieldnames = [] + + if self.istable and not parenttype: + return permitted_fieldnames + + if not permission_type: + permission_type = "select" if xhiveframework.only_has_select_perm(self.name, user=user) else "read" + + if permission_type == "select": + return self.get_search_fields() + + if not self.get_permissions(parenttype=parenttype): + return self.get_fieldnames_with_value() + + permlevel_access = set( + self.get_permlevel_access(permission_type=permission_type, parenttype=parenttype, user=user) + ) + + if 0 not in permlevel_access and permission_type in ("read", "select"): + if xhiveframework.share.get_shared(self.name, user, rights=[permission_type], limit=1): + permlevel_access.add(0) + + permitted_fieldnames.extend( + df.fieldname + for df in self.get_fieldnames_with_value( + with_field_meta=True, with_virtual_fields=with_virtual_fields + ) + if df.permlevel in permlevel_access + ) + return permitted_fieldnames + + def get_permlevel_access(self, permission_type="read", parenttype=None, *, user=None): + has_access_to = [] + roles = xhiveframework.get_roles(user) + for perm in self.get_permissions(parenttype): + if perm.role in roles and perm.get(permission_type): + if perm.permlevel not in has_access_to: + has_access_to.append(perm.permlevel) + + return has_access_to + + def get_permissions(self, parenttype=None): + if self.istable and parenttype: + # use parent permissions + permissions = xhiveframework.get_meta(parenttype).permissions + else: + permissions = self.get("permissions", []) + + return permissions + + def get_dashboard_data(self): + """Returns dashboard setup related to this doctype. + + This method will return the `data` property in the `[doctype]_dashboard.py` + file in the doctype's folder, along with any overrides or extensions + implemented in other XhiveFramework applications via hooks. + """ + data = xhiveframework._dict() + if not self.custom: + try: + module = load_doctype_module(self.name, suffix="_dashboard") + if hasattr(module, "get_data"): + data = xhiveframework._dict(module.get_data()) + except ImportError: + pass + + self.add_doctype_links(data) + + if not self.custom: + for hook in xhiveframework.get_hooks("override_doctype_dashboards", {}).get(self.name, []): + data = xhiveframework._dict(xhiveframework.get_attr(hook)(data=data)) + + return data + + def add_doctype_links(self, data): + """add `links` child table in standard link dashboard format""" + dashboard_links = [] + + if getattr(self, "links", None): + dashboard_links.extend(self.links) + + if not data.transactions: + # init groups + data.transactions = [] + + if not data.non_standard_fieldnames: + data.non_standard_fieldnames = {} + + if not data.internal_links: + data.internal_links = {} + + for link in dashboard_links: + link.added = False + if link.hidden: + continue + + for group in data.transactions: + group = xhiveframework._dict(group) + + # 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 doctype not in group.get("items"): + group.get("items").append(doctype) + link.added = True + + if not link.added: + # group not found, make a new group + data.transactions.append( + dict(label=link.group, items=[link.parent_doctype or link.link_doctype]) + ) + + if not data.fieldname and link.link_fieldname: + data.fieldname = link.link_fieldname + + if not link.is_child_table: + data.non_standard_fieldnames[link.link_doctype] = link.link_fieldname + elif link.is_child_table: + data.internal_links[link.parent_doctype] = [link.table_fieldname, link.link_fieldname] + + def get_row_template(self): + return self.get_web_template(suffix="_row") + + def get_list_template(self): + return self.get_web_template(suffix="_list") + + def get_web_template(self, suffix=""): + """Returns the relative path of the row template for this doctype""" + module_name = xhiveframework.scrub(self.module) + doctype = xhiveframework.scrub(self.name) + template_path = xhiveframework.get_module_path( + module_name, "doctype", doctype, "templates", doctype + suffix + ".html" + ) + if os.path.exists(template_path): + return f"{module_name}/doctype/{doctype}/templates/{doctype}{suffix}.html" + return None + + def is_nested_set(self): + return self.has_field("lft") and self.has_field("rgt") + + +####### + + +def is_single(doctype): + try: + return xhiveframework.db.get_value("DocType", doctype, "issingle") + except IndexError: + raise Exception("Cannot determine whether %s is single" % doctype) + + +def get_parent_dt(dt): + if not xhiveframework.is_table(dt): + return "" + + return ( + xhiveframework.db.get_value( + "DocField", + {"fieldtype": ("in", xhiveframework.model.table_fields), "options": dt}, + "parent", + ) + or "" + ) + + +def set_fieldname(field_id, fieldname): + xhiveframework.db.set_value("DocField", field_id, "fieldname", fieldname) + + +def get_field_currency(df, doc=None): + """get currency based on DocField options and fieldvalue in doc""" + currency = None + + if not df.get("options"): + return None + + if not doc: + return None + + if not getattr(xhiveframework.local, "field_currency", None): + xhiveframework.local.field_currency = xhiveframework._dict() + + if not ( + xhiveframework.local.field_currency.get((doc.doctype, doc.name), {}).get(df.fieldname) + or ( + doc.get("parent") + and xhiveframework.local.field_currency.get((doc.doctype, doc.parent), {}).get(df.fieldname) + ) + ): + ref_docname = doc.get("parent") or doc.name + + if ":" in cstr(df.get("options")): + split_opts = df.get("options").split(":") + if len(split_opts) == 3 and doc.get(split_opts[1]): + currency = xhiveframework.get_cached_value(split_opts[0], doc.get(split_opts[1]), split_opts[2]) + else: + currency = doc.get(df.get("options")) + if doc.get("parenttype"): + if currency: + ref_docname = doc.name + else: + if xhiveframework.get_meta(doc.parenttype).has_field(df.get("options")): + # only get_value if parent has currency field + currency = xhiveframework.db.get_value(doc.parenttype, doc.parent, df.get("options")) + + if currency: + xhiveframework.local.field_currency.setdefault((doc.doctype, ref_docname), xhiveframework._dict()).setdefault( + df.fieldname, currency + ) + + return xhiveframework.local.field_currency.get((doc.doctype, doc.name), {}).get(df.fieldname) or ( + doc.get("parent") and xhiveframework.local.field_currency.get((doc.doctype, doc.parent), {}).get(df.fieldname) + ) + + +def get_field_precision(df, doc=None, currency=None): + """get precision based on DocField options and fieldvalue in doc""" + from xhiveframework.utils import get_number_format_info + + if df.precision: + precision = cint(df.precision) + + elif df.fieldtype == "Currency": + precision = cint(xhiveframework.db.get_default("currency_precision")) + if not precision: + number_format = xhiveframework.db.get_default("number_format") or "#,###.##" + decimal_str, comma_str, precision = get_number_format_info(number_format) + else: + precision = cint(xhiveframework.db.get_default("float_precision")) or 3 + + return precision + + +def get_default_df(fieldname): + if fieldname in (default_fields + child_table_fields): + if fieldname in ("creation", "modified"): + return xhiveframework._dict(fieldname=fieldname, fieldtype="Datetime") + + elif fieldname in ("idx", "docstatus"): + return xhiveframework._dict(fieldname=fieldname, fieldtype="Int") + + return xhiveframework._dict(fieldname=fieldname, fieldtype="Data") + + +def trim_tables(doctype=None, dry_run=False, quiet=False): + """ + Removes database fields that don't exist in the doctype (json or custom field). This may be needed + as maintenance since removing a field in a DocType doesn't automatically + delete the db field. + """ + UPDATED_TABLES = {} + filters = {"issingle": 0, "is_virtual": 0} + if doctype: + filters["name"] = doctype + + for doctype in xhiveframework.get_all("DocType", filters=filters, pluck="name"): + try: + dropped_columns = trim_table(doctype, dry_run=dry_run) + if dropped_columns: + UPDATED_TABLES[doctype] = dropped_columns + except xhiveframework.db.TableMissingError: + if quiet: + continue + click.secho(f"Ignoring missing table for DocType: {doctype}", fg="yellow", err=True) + click.secho(f"Consider removing record in the DocType table for {doctype}", fg="yellow", err=True) + except Exception as e: + if quiet: + continue + click.echo(e, err=True) + + return UPDATED_TABLES + + +def trim_table(doctype, dry_run=True): + xhiveframework.cache.hdel("table_columns", f"tab{doctype}") + ignore_fields = default_fields + optional_fields + child_table_fields + columns = xhiveframework.db.get_table_columns(doctype) + fields = xhiveframework.get_meta(doctype, cached=False).get_fieldnames_with_value() + + def is_internal(field): + return field not in ignore_fields and not field.startswith("_") + + columns_to_remove = [f for f in list(set(columns) - set(fields)) if is_internal(f)] + DROPPED_COLUMNS = columns_to_remove[:] + + if columns_to_remove and not dry_run: + columns_to_remove = ", ".join(f"DROP `{c}`" for c in columns_to_remove) + xhiveframework.db.sql_ddl(f"ALTER TABLE `tab{doctype}` {columns_to_remove}") + + return DROPPED_COLUMNS + + +def _update_field_order_based_on_insert_after(field_order, insert_after_map): + """Update the field order based on insert_after_map""" + + retry_field_insertion = True + + while retry_field_insertion: + retry_field_insertion = False + + for fieldname in list(insert_after_map): + if fieldname not in field_order: + continue + + custom_field_index = field_order.index(fieldname) + for custom_field_name in insert_after_map.pop(fieldname): + custom_field_index += 1 + field_order.insert(custom_field_index, custom_field_name) + + retry_field_insertion = True + + if insert_after_map: + # insert_after is an invalid fieldname, add these fields to the end + for fields in insert_after_map.values(): + field_order.extend(fields) diff --git a/xhiveframework/model/naming.py b/xhiveframework/model/naming.py new file mode 100644 index 0000000..2268f25 --- /dev/null +++ b/xhiveframework/model/naming.py @@ -0,0 +1,544 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import datetime +import re +from collections.abc import Callable +from typing import TYPE_CHECKING, Optional + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model import log_types +from xhiveframework.query_builder import DocType +from xhiveframework.utils import cint, cstr, now_datetime + +if TYPE_CHECKING: + from xhiveframework.model.document import Document + from xhiveframework.model.meta import Meta + + +NAMING_SERIES_PATTERN = re.compile(r"^[\w\- \/.#{}]+$", re.UNICODE) +BRACED_PARAMS_PATTERN = re.compile(r"(\{[\w | #]+\})") + + +# Types that can be using in naming series fields +NAMING_SERIES_PART_TYPES = ( + int, + str, + datetime.datetime, + datetime.date, + datetime.time, + datetime.timedelta, +) + + +class InvalidNamingSeriesError(xhiveframework.ValidationError): + pass + + +class NamingSeries: + __slots__ = ("series",) + + def __init__(self, series: str): + self.series = series + + # Add default number part if missing + if "#" not in self.series: + self.series += ".#####" + + def validate(self): + if "." not in self.series: + xhiveframework.throw( + _("Invalid naming series {}: dot (.) missing").format(xhiveframework.bold(self.series)), + exc=InvalidNamingSeriesError, + ) + + if not NAMING_SERIES_PATTERN.match(self.series): + xhiveframework.throw( + _( + "Special Characters except '-', '#', '.', '/', '{{' and '}}' not allowed in naming series {0}" + ).format(xhiveframework.bold(self.series)), + exc=InvalidNamingSeriesError, + ) + + def generate_next_name(self, doc: "Document", *, ignore_validate=False) -> str: + if not ignore_validate: + self.validate() + + parts = self.series.split(".") + return parse_naming_series(parts, doc=doc) + + def get_prefix(self) -> str: + """Naming series stores prefix to maintain a counter in DB. This prefix can be used to update counter or validations. + + e.g. `SINV-.YY.-.####` has prefix of `SINV-22-` in database for year 2022. + """ + + prefix = None + + def fake_counter_backend(partial_series, digits): + nonlocal prefix + prefix = partial_series + return "#" * digits + + # This function evaluates all parts till we hit numerical parts and then + # sends prefix + digits to DB to find next number. + # Instead of reimplementing the whole parsing logic in multiple places we + # can just ask this function to give us the prefix. + parse_naming_series(self.series, number_generator=fake_counter_backend) + + if prefix is None: + xhiveframework.throw(_("Invalid Naming Series: {}").format(self.series)) + + return prefix + + def get_preview(self, doc=None) -> list[str]: + """Generate preview of naming series without using DB counters""" + generated_names = [] + for count in range(1, 4): + + def fake_counter(_prefix, digits): + # ignore B023: binding `count` is not necessary because + # function is evaluated immediately and it can not be done + # because of function signature requirement + return str(count).zfill(digits) + + generated_names.append(parse_naming_series(self.series, doc=doc, number_generator=fake_counter)) + return generated_names + + def update_counter(self, new_count: int) -> None: + """Warning: Incorrectly updating series can result in unusable transactions""" + Series = xhiveframework.qb.DocType("Series") + prefix = self.get_prefix() + + # Initialize if not present in DB + if xhiveframework.db.get_value("Series", prefix, "name", order_by="name") is None: + xhiveframework.qb.into(Series).insert(prefix, 0).columns("name", "current").run() + + (xhiveframework.qb.update(Series).set(Series.current, cint(new_count)).where(Series.name == prefix)).run() + + def get_current_value(self) -> int: + prefix = self.get_prefix() + return cint(xhiveframework.db.get_value("Series", prefix, "current", order_by="name")) + + +def set_new_name(doc): + """ + Sets the `name` property for the document based on various rules. + + 1. If amended doc, set suffix. + 2. If `autoname` method is declared, then call it. + 3. If `autoname` property is set in the DocType (`meta`), then build it using the `autoname` property. + 4. If no rule defined, use hash. + + :param doc: Document to be named. + """ + + doc.run_method("before_naming") + + meta = xhiveframework.get_meta(doc.doctype) + autoname = meta.autoname or "" + + if autoname.lower() != "prompt" and not xhiveframework.flags.in_import: + doc.name = None + + if is_autoincremented(doc.doctype, meta): + doc.name = xhiveframework.db.get_next_sequence_val(doc.doctype) + return + + if getattr(doc, "amended_from", None): + _set_amended_name(doc) + if doc.name: + return + + elif getattr(doc.meta, "issingle", False): + doc.name = doc.doctype + + if not doc.name: + set_naming_from_document_naming_rule(doc) + + if not doc.name: + doc.run_method("autoname") + + if not doc.name and autoname: + set_name_from_naming_options(autoname, doc) + + # at this point, we fall back to name generation with the hash option + if not doc.name: + doc.name = make_autoname("hash", doc.doctype) + + doc.name = validate_name(doc.doctype, doc.name) + + +def is_autoincremented(doctype: str, meta: Optional["Meta"] = None) -> bool: + """Checks if the doctype has autoincrement autoname set""" + + if not meta: + meta = xhiveframework.get_meta(doctype) + + if not getattr(meta, "issingle", False) and meta.autoname == "autoincrement": + return True + + return False + + +def set_name_from_naming_options(autoname, doc): + """ + Get a name based on the autoname field option + """ + + _autoname = autoname.lower() + + if _autoname.startswith("field:"): + doc.name = _field_autoname(autoname, doc) + + # if the autoname option is 'field:' and no name was derived, we need to + # notify + if not doc.name: + fieldname = autoname[6:] + xhiveframework.throw(_("{0} is required").format(doc.meta.get_label(fieldname))) + + elif _autoname.startswith("naming_series:"): + set_name_by_naming_series(doc) + elif _autoname.startswith("prompt"): + _prompt_autoname(autoname, doc) + elif _autoname.startswith("format:"): + doc.name = _format_autoname(autoname, doc) + elif "#" in autoname: + doc.name = make_autoname(autoname, doc=doc) + + +def set_naming_from_document_naming_rule(doc): + """ + Evaluate rules based on "Document Naming Series" doctype + """ + from xhiveframework.model.base_document import DOCTYPES_FOR_DOCTYPE + + IGNORED_DOCTYPES = {*log_types, *DOCTYPES_FOR_DOCTYPE, "DefaultValue", "Patch Log"} + + if doc.doctype in IGNORED_DOCTYPES: + return + + document_naming_rules = xhiveframework.cache_manager.get_doctype_map( + "Document Naming Rule", + doc.doctype, + filters={"document_type": doc.doctype, "disabled": 0}, + order_by="priority desc", + ) + + for d in document_naming_rules: + xhiveframework.get_cached_doc("Document Naming Rule", d.name).apply(doc) + if doc.name: + break + + +def set_name_by_naming_series(doc): + """Sets name by the `naming_series` property""" + if not doc.naming_series: + doc.naming_series = get_default_naming_series(doc.doctype) + + if not doc.naming_series: + xhiveframework.throw(xhiveframework._("Naming Series mandatory")) + + doc.name = make_autoname(doc.naming_series + ".#####", "", doc) + + +def make_autoname(key="", doctype="", doc="", *, ignore_validate=False): + """ + Creates an autoname from the given key: + + **Autoname rules:** + + * The key is separated by '.' + * '####' represents a series. The string before this part becomes the prefix: + Example: ABC.#### creates a series ABC0001, ABC0002 etc + * 'MM' represents the current month + * 'YY' and 'YYYY' represent the current year + + + *Example:* + + * DE./.YY./.MM./.##### will create a series like + DE/09/01/00001 where 09 is the year, 01 is the month and 00001 is the series + """ + if key == "hash": + return xhiveframework.generate_hash(length=10) + + series = NamingSeries(key) + return series.generate_next_name(doc, ignore_validate=ignore_validate) + + +def parse_naming_series( + parts: list[str] | str, + doctype=None, + doc: Optional["Document"] = None, + number_generator: Callable[[str, int], str] | None = None, +) -> str: + """Parse the naming series and get next name. + + args: + parts: naming series parts (split by `.`) + doc: document to use for series that have parts using fieldnames + number_generator: Use different counter backend other than `tabSeries`. Primarily used for testing. + """ + + name = "" + _sentinel = object() + if isinstance(parts, str): + parts = parts.split(".") + + if not number_generator: + number_generator = getseries + + series_set = False + today = now_datetime() + for e in parts: + if not e: + continue + + part = "" + if e.startswith("#"): + if not series_set: + digits = len(e) + part = number_generator(name, digits) + series_set = True + elif e == "YY": + part = today.strftime("%y") + elif e == "MM": + part = today.strftime("%m") + elif e == "DD": + part = today.strftime("%d") + elif e == "YYYY": + part = today.strftime("%Y") + elif e == "WW": + part = determine_consecutive_week_number(today) + elif e == "timestamp": + part = str(today) + elif doc and (e.startswith("{") or doc.get(e, _sentinel) is not _sentinel): + e = e.replace("{", "").replace("}", "") + part = doc.get(e) + elif method := has_custom_parser(e): + part = xhiveframework.get_attr(method[0])(doc, e) + else: + part = e + + if isinstance(part, str): + name += part + elif isinstance(part, NAMING_SERIES_PART_TYPES): + name += cstr(part).strip() + + return name + + +def has_custom_parser(e): + """Returns true if the naming series part has a custom parser""" + return xhiveframework.get_hooks("naming_series_variables", {}).get(e) + + +def determine_consecutive_week_number(datetime): + """Determines the consecutive calendar week""" + m = datetime.month + # ISO 8601 calandar week + w = datetime.strftime("%V") + # Ensure consecutiveness for the first and last days of a year + if m == 1 and int(w) >= 52: + w = "00" + elif m == 12 and int(w) <= 1: + w = "53" + return w + + +def getseries(key, digits): + # series created ? + # Using xhiveframework.qb as xhiveframework.get_values does not allow order_by=None + series = DocType("Series") + current = (xhiveframework.qb.from_(series).where(series.name == key).for_update().select("current")).run() + + if current and current[0][0] is not None: + current = current[0][0] + # yes, update it + xhiveframework.db.sql("UPDATE `tabSeries` SET `current` = `current` + 1 WHERE `name`=%s", (key,)) + current = cint(current) + 1 + else: + # no, create it + xhiveframework.db.sql("INSERT INTO `tabSeries` (`name`, `current`) VALUES (%s, 1)", (key,)) + current = 1 + return ("%0" + str(digits) + "d") % current + + +def revert_series_if_last(key, name, doc=None): + """ + Reverts the series for particular naming series: + * key is naming series - SINV-.YYYY-.#### + * name is actual name - SINV-2021-0001 + + 1. This function split the key into two parts prefix (SINV-YYYY) & hashes (####). + 2. Use prefix to get the current index of that naming series from Series table + 3. Then revert the current index. + + *For custom naming series:* + 1. hash can exist anywhere, if it exist in hashes then it take normal flow. + 2. If hash doesn't exit in hashes, we get the hash from prefix, then update name and prefix accordingly. + + *Example:* + 1. key = SINV-.YYYY.- + * If key doesn't have hash it will add hash at the end + * prefix will be SINV-YYYY based on this will get current index from Series table. + 2. key = SINV-.####.-2021 + * now prefix = SINV-#### and hashes = 2021 (hash doesn't exist) + * will search hash in key then accordingly get prefix = SINV- + 3. key = ####.-2021 + * prefix = #### and hashes = 2021 (hash doesn't exist) + * will search hash in key then accordingly get prefix = "" + """ + if ".#" in key: + prefix, hashes = key.rsplit(".", 1) + if "#" not in hashes: + # get the hash part from the key + hash = re.search("#+", key) + if not hash: + return + name = name.replace(hashes, "") + prefix = prefix.replace(hash.group(), "") + else: + prefix = key + + if "." in prefix: + prefix = parse_naming_series(prefix.split("."), doc=doc) + + count = cint(name.replace(prefix, "")) + series = DocType("Series") + current = (xhiveframework.qb.from_(series).where(series.name == prefix).for_update().select("current")).run() + + if current and current[0][0] == count: + xhiveframework.db.sql("UPDATE `tabSeries` SET `current` = `current` - 1 WHERE `name`=%s", prefix) + + +def get_default_naming_series(doctype: str) -> str | None: + """get default value for `naming_series` property""" + naming_series_options = xhiveframework.get_meta(doctype).get_naming_series_options() + + # Return first truthy options + # Empty strings are used to avoid populating forms by default + for option in naming_series_options: + if option: + return option + + +def validate_name(doctype: str, name: int | str): + if not name: + xhiveframework.throw(_("No Name Specified for {0}").format(doctype)) + + if isinstance(name, int): + if is_autoincremented(doctype): + # this will set the sequence value to be the provided name/value and set it to be used + # so that the sequence will start from the next value + xhiveframework.db.set_next_sequence_val(doctype, name, is_val_used=True) + return name + + xhiveframework.throw(_("Invalid name type (integer) for varchar name column"), xhiveframework.NameError) + + if name.startswith("New " + doctype): + xhiveframework.throw( + _("There were some errors setting the name, please contact the administrator"), xhiveframework.NameError + ) + name = name.strip() + + if not xhiveframework.get_meta(doctype).get("issingle") and (doctype == name) and (name != "DocType"): + xhiveframework.throw(_("Name of {0} cannot be {1}").format(doctype, name), xhiveframework.NameError) + + special_characters = "<>" + if re.findall(f"[{special_characters}]+", name): + message = ", ".join(f"'{c}'" for c in special_characters) + xhiveframework.throw(_("Name cannot contain special characters like {0}").format(message), xhiveframework.NameError) + + return name + + +def append_number_if_name_exists(doctype, value, fieldname="name", separator="-", filters=None): + if not filters: + filters = dict() + filters.update({fieldname: value}) + exists = xhiveframework.db.exists(doctype, filters) + + regex = f"^{re.escape(value)}{separator}\\d+$" + + if exists: + last = xhiveframework.db.sql( + f"""SELECT `{fieldname}` FROM `tab{doctype}` + WHERE `{fieldname}` {xhiveframework.db.REGEX_CHARACTER} %s + ORDER BY length({fieldname}) DESC, + `{fieldname}` DESC LIMIT 1""", + regex, + ) + + if last: + count = str(cint(last[0][0].rsplit(separator, 1)[1]) + 1) + else: + count = "1" + + value = f"{value}{separator}{count}" + + return value + + +def _set_amended_name(doc): + amend_naming_rule = xhiveframework.db.get_value( + "Amended Document Naming Settings", {"document_type": doc.doctype}, "action", cache=True + ) + if not amend_naming_rule: + amend_naming_rule = xhiveframework.db.get_single_value( + "Document Naming Settings", "default_amend_naming", cache=True + ) + + if amend_naming_rule == "Default Naming": + return + + am_id = 1 + am_prefix = doc.amended_from + if xhiveframework.db.get_value(doc.doctype, doc.amended_from, "amended_from"): + am_id = cint(doc.amended_from.split("-")[-1]) + 1 + am_prefix = "-".join(doc.amended_from.split("-")[:-1]) # except the last hyphen + + doc.name = am_prefix + "-" + str(am_id) + return doc.name + + +def _field_autoname(autoname, doc, skip_slicing=None): + """ + Generate a name using `DocType` field. This is called when the doctype's + `autoname` field starts with 'field:' + """ + fieldname = autoname if skip_slicing else autoname[6:] + return (cstr(doc.get(fieldname)) or "").strip() + + +def _prompt_autoname(autoname, doc): + """ + Generate a name using Prompt option. This simply means the user will have to set the name manually. + This is called when the doctype's `autoname` field starts with 'prompt'. + """ + # set from __newname in save.py + if not doc.name: + xhiveframework.throw(_("Please set the document name")) + + +def _format_autoname(autoname: str, doc): + """ + Generate autoname by replacing all instances of braced params (fields, date params ('DD', 'MM', 'YY'), series) + Independent of remaining string or separators. + + Example pattern: 'format:LOG-{MM}-{fieldname1}-{fieldname2}-{#####}' + """ + + first_colon_index = autoname.find(":") + autoname_value = autoname[first_colon_index + 1 :] + + def get_param_value_for_match(match): + param = match.group() + return parse_naming_series([param[1:-1]], doc=doc) + + # Replace braced params with their parsed value + name = BRACED_PARAMS_PATTERN.sub(get_param_value_for_match, autoname_value) + + return name diff --git a/xhiveframework/model/rename_doc.py b/xhiveframework/model/rename_doc.py new file mode 100644 index 0000000..be6d62f --- /dev/null +++ b/xhiveframework/model/rename_doc.py @@ -0,0 +1,693 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +from types import NoneType +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework import _, bold +from xhiveframework.model.document import Document +from xhiveframework.model.dynamic_links import get_dynamic_link_map +from xhiveframework.model.naming import validate_name +from xhiveframework.model.utils.user_settings import sync_user_settings, update_user_settings_data +from xhiveframework.query_builder import Field +from xhiveframework.utils.data import sbool +from xhiveframework.utils.password import rename_password +from xhiveframework.utils.scheduler import is_scheduler_inactive + +if TYPE_CHECKING: + from xhiveframework.model.meta import Meta + + +@xhiveframework.whitelist() +def update_document_title( + *, + doctype: str, + docname: str, + title: str | None = None, + name: str | None = None, + merge: bool = False, + enqueue: bool = False, + **kwargs, +) -> str: + """ + Update the name or title of a document. Returns `name` if document was renamed, + `docname` if renaming operation was queued. + + :param doctype: DocType of the document + :param docname: Name of the document + :param title: New Title of the document + :param name: New Name of the document + :param merge: Merge the current Document with the existing one if exists + :param enqueue: Enqueue the rename operation, title is updated in current process + """ + + # to maintain backwards API compatibility + updated_title = kwargs.get("new_title") or title + updated_name = kwargs.get("new_name") or name + + # TODO: omit this after runtime type checking (ref: https://lab.membtech.com/xhiveframework/xhiveframework15/pull/14927) + for obj in [docname, updated_title, updated_name]: + if not isinstance(obj, str | NoneType): + xhiveframework.throw(f"{obj=} must be of type str or None") + + # handle bad API usages + merge = sbool(merge) + enqueue = sbool(enqueue) + action_enqueued = enqueue and not is_scheduler_inactive() + + doc = xhiveframework.get_doc(doctype, docname) + doc.check_permission(permtype="write") + + title_field = doc.meta.get_title_field() + + title_updated = updated_title and (title_field != "name") and (updated_title != doc.get(title_field)) + name_updated = updated_name and (updated_name != doc.name) + + queue = kwargs.get("queue") or "default" + + if name_updated: + if action_enqueued: + current_name = doc.name + + # before_name hook may have DocType specific validations or transformations + transformed_name = doc.run_method("before_rename", current_name, updated_name, merge) + if isinstance(transformed_name, dict): + transformed_name = transformed_name.get("new") + transformed_name = transformed_name or updated_name + + # run rename validations before queueing + # use savepoints to avoid partial renames / commits + validate_rename( + doctype=doctype, + old=current_name, + new=transformed_name, + meta=doc.meta, + merge=merge, + save_point=True, + ) + + doc.queue_action("rename", name=transformed_name, merge=merge, queue=queue) + else: + doc.rename(updated_name, merge=merge) + + if title_updated: + if action_enqueued and name_updated: + xhiveframework.enqueue( + "xhiveframework.client.set_value", + doctype=doc.doctype, + name=updated_name, + fieldname=title_field, + value=updated_title, + ) + else: + try: + setattr(doc, title_field, updated_title) + doc.save() + xhiveframework.msgprint(_("Saved"), alert=True, indicator="green") + except Exception as e: + if xhiveframework.db.is_duplicate_entry(e): + xhiveframework.throw( + _("{0} {1} already exists").format(doctype, xhiveframework.bold(docname)), + title=_("Duplicate Name"), + exc=xhiveframework.DuplicateEntryError, + ) + raise + + return doc.name + + +def rename_doc( + doctype: str | None = None, + old: str | None = None, + new: str | None = None, + force: bool = False, + merge: bool = False, + ignore_permissions: bool = False, + ignore_if_exists: bool = False, + show_alert: bool = True, + rebuild_search: bool = True, + doc: Document | None = None, + validate: bool = True, +) -> str: + """Rename a doc(dt, old) to doc(dt, new) and update all linked fields of type "Link". + + doc: Document object to be renamed. + new: New name for the record. If None, and doctype is specified, new name may be automatically generated via before_rename hooks. + doctype: DocType of the document. Not required if doc is passed. + old: Current name of the document. Not required if doc is passed. + force: Allow even if document is not allowed to be renamed. + merge: Merge with existing document of new name. + ignore_permissions: Ignore user permissions while renaming. + ignore_if_exists: Don't raise exception if document with new name already exists. This will quietely overwrite the existing document. + show_alert: Display alert if document is renamed successfully. + rebuild_search: Rebuild linked doctype search after renaming. + validate: Validate before renaming. If False, it is assumed that the caller has already validated. + """ + old_usage_style = doctype and old and new + new_usage_style = doc and new + + if not (new_usage_style or old_usage_style): + raise TypeError( + "{doctype, old, new} or {doc, new} are required arguments for xhiveframework.model.rename_doc" + ) + + old = old or doc.name + doctype = doctype or doc.doctype + force = sbool(force) + merge = sbool(merge) + meta = xhiveframework.get_meta(doctype) + + if validate: + old_doc = doc or xhiveframework.get_doc(doctype, old) + out = old_doc.run_method("before_rename", old, new, merge) or {} + new = (out.get("new") or new) if isinstance(out, dict) else (out or new) + new = validate_rename( + doctype=doctype, + old=old, + new=new, + meta=meta, + merge=merge, + force=force, + ignore_permissions=ignore_permissions, + ignore_if_exists=ignore_if_exists, + ) + + if not merge: + rename_parent_and_child(doctype, old, new, meta) + else: + update_assignments(old, new, doctype) + + # update link fields' values + link_fields = get_link_fields(doctype) + update_link_field_values(link_fields, old, new, doctype) + + rename_dynamic_links(doctype, old, new) + + # save the user settings in the db + update_user_settings(old, new, link_fields) + + if doctype == "DocType": + rename_doctype(doctype, old, new) + update_customizations(old, new) + + update_attachments(doctype, old, new) + + rename_versions(doctype, old, new) + + rename_eps_records(doctype, old, new) + + # call after_rename + new_doc = xhiveframework.get_doc(doctype, new) + + if validate: + # copy any flags if required + new_doc._local = getattr(old_doc, "_local", None) + + new_doc.run_method("after_rename", old, new, merge) + + if not merge: + rename_password(doctype, old, new) + + if merge: + new_doc.add_comment("Edit", _("merged {0} into {1}").format(xhiveframework.bold(old), xhiveframework.bold(new))) + else: + new_doc.add_comment("Edit", _("renamed from {0} to {1}").format(xhiveframework.bold(old), xhiveframework.bold(new))) + + if merge: + xhiveframework.delete_doc(doctype, old) + + new_doc.clear_cache() + xhiveframework.clear_cache() + if rebuild_search: + xhiveframework.enqueue("xhiveframework.utils.global_search.rebuild_for_doctype", doctype=doctype) + + if show_alert: + xhiveframework.msgprint( + _("Document renamed from {0} to {1}").format(bold(old), bold(new)), + alert=True, + indicator="green", + ) + + return new + + +def update_assignments(old: str, new: str, doctype: str) -> None: + old_assignments = xhiveframework.parse_json(xhiveframework.db.get_value(doctype, old, "_assign")) or [] + new_assignments = xhiveframework.parse_json(xhiveframework.db.get_value(doctype, new, "_assign")) or [] + common_assignments = list(set(old_assignments).intersection(new_assignments)) + + for user in common_assignments: + # delete todos linked to old doc + todos = xhiveframework.get_all( + "ToDo", + { + "owner": user, + "reference_type": doctype, + "reference_name": old, + }, + ["name", "description"], + ) + + for todo in todos: + xhiveframework.delete_doc("ToDo", todo.name) + + unique_assignments = list(set(old_assignments + new_assignments)) + xhiveframework.db.set_value(doctype, new, "_assign", xhiveframework.as_json(unique_assignments, indent=0)) + + +def update_user_settings(old: str, new: str, link_fields: list[dict]) -> None: + """ + Update the user settings of all the linked doctypes while renaming. + """ + + # store the user settings data from the redis to db + sync_user_settings() + + if not link_fields: + return + + # find the user settings for the linked doctypes + linked_doctypes = {d.parent for d in link_fields if not d.issingle} + UserSettings = xhiveframework.qb.Table("__UserSettings") + + user_settings_details = ( + xhiveframework.qb.from_(UserSettings) + .select("user", "doctype", "data") + .where(UserSettings.data.like(old) & UserSettings.doctype.isin(linked_doctypes)) + .run(as_dict=True) + ) + + # create the dict using the doctype name as key and values as list of the user settings + from collections import defaultdict + + user_settings_dict = defaultdict(list) + for user_setting in user_settings_details: + user_settings_dict[user_setting.doctype].append(user_setting) + + # update the name in linked doctype whose user settings exists + for fields in link_fields: + user_settings = user_settings_dict.get(fields.parent) + if user_settings: + for user_setting in user_settings: + update_user_settings_data(user_setting, "value", old, new, "docfield", fields.fieldname) + else: + continue + + +def update_customizations(old: str, new: str) -> None: + xhiveframework.db.set_value("Custom DocPerm", {"parent": old}, "parent", new, update_modified=False) + + +def update_attachments(doctype: str, old: str, new: str) -> None: + if doctype != "DocType": + File = xhiveframework.qb.DocType("File") + + xhiveframework.qb.update(File).set(File.attached_to_name, new).where( + (File.attached_to_name == old) & (File.attached_to_doctype == doctype) + ).run() + + +def rename_versions(doctype: str, old: str, new: str) -> None: + Version = xhiveframework.qb.DocType("Version") + + xhiveframework.qb.update(Version).set(Version.docname, new).where( + (Version.docname == old) & (Version.ref_doctype == doctype) + ).run() + + +def rename_eps_records(doctype: str, old: str, new: str) -> None: + EPL = xhiveframework.qb.DocType("Energy Point Log") + + xhiveframework.qb.update(EPL).set(EPL.reference_name, new).where( + (EPL.reference_doctype == doctype) & (EPL.reference_name == old) + ).run() + + +def rename_parent_and_child(doctype: str, old: str, new: str, meta: "Meta") -> None: + xhiveframework.qb.update(doctype).set("name", new).where(Field("name") == old).run() + + update_autoname_field(doctype, new, meta) + update_child_docs(old, new, meta) + + +def update_autoname_field(doctype: str, new: str, meta: "Meta") -> None: + # update the value of the autoname field on rename of the docname + if meta.get("autoname"): + field = meta.get("autoname").split(":") + if field and field[0] == "field": + xhiveframework.qb.update(doctype).set(field[1], new).where(Field("name") == new).run() + + +def validate_rename( + doctype: str, + old: str, + new: str, + meta: "Meta", + merge: bool, + force: bool = False, + ignore_permissions: bool = False, + ignore_if_exists: bool = False, + save_point=False, +) -> str: + # using for update so that it gets locked and someone else cannot edit it while this rename is going on! + if save_point: + _SAVE_POINT = f"validate_rename_{xhiveframework.generate_hash(length=8)}" + xhiveframework.db.savepoint(_SAVE_POINT) + + exists = xhiveframework.qb.from_(doctype).where(Field("name") == new).for_update().select("name").run(pluck=True) + exists = exists[0] if exists else None + + if not xhiveframework.db.exists(doctype, old): + xhiveframework.throw(_("Can't rename {0} to {1} because {0} doesn't exist.").format(old, new)) + + if old == new: + xhiveframework.throw(_("No changes made because old and new name are the same.").format(old, new)) + + if exists and exists != new: + # for fixing case, accents + exists = None + + if merge and not exists: + xhiveframework.throw(_("{0} {1} does not exist, select a new target to merge").format(doctype, new)) + + if not merge and exists and not ignore_if_exists: + xhiveframework.throw(_("Another {0} with name {1} exists, select another name").format(doctype, new)) + + if not (ignore_permissions or xhiveframework.permissions.has_permission(doctype, "write", raise_exception=False)): + xhiveframework.throw(_("You need write permission to rename")) + + if not (force or ignore_permissions) and not meta.allow_rename: + xhiveframework.throw(_("{0} not allowed to be renamed").format(_(doctype))) + + # validate naming like it's done in doc.py + new = validate_name(doctype, new) + + if save_point: + xhiveframework.db.rollback(save_point=_SAVE_POINT) + + return new + + +def rename_doctype(doctype: str, old: str, new: str) -> None: + # change options for fieldtype Table, Table MultiSelect and Link + fields_with_options = ("Link", *xhiveframework.model.table_fields) + + for fieldtype in fields_with_options: + update_options_for_fieldtype(fieldtype, old, new) + + # change parenttype for fieldtype Table + update_parenttype_values(old, new) + + +def update_child_docs(old: str, new: str, meta: "Meta") -> None: + # update "parent" + for df in meta.get_table_fields(): + ( + xhiveframework.qb.update(df.options) + .set("parent", new) + .where((Field("parent") == old) & (Field("parenttype") == meta.name)) + ).run() + + +def update_link_field_values(link_fields: list[dict], old: str, new: str, doctype: str) -> None: + for field in link_fields: + if field["issingle"]: + try: + single_doc = xhiveframework.get_doc(field["parent"]) + if single_doc.get(field["fieldname"]) == old: + single_doc.set(field["fieldname"], new) + # update single docs using ORM rather then query + # as single docs also sometimes sets defaults! + single_doc.flags.ignore_mandatory = True + single_doc.save(ignore_permissions=True) + except ImportError: + # fails in patches where the doctype has been renamed + # or no longer exists + pass + else: + parent = field["parent"] + docfield = field["fieldname"] + + # Handles the case where one of the link fields belongs to + # the DocType being renamed. + # Here this field could have the current DocType as its value too. + + # In this case while updating link field value, the field's parent + # or the current DocType table name hasn't been renamed yet, + # so consider it's old name. + if parent == new and doctype == "DocType": + parent = old + + xhiveframework.db.set_value(parent, {docfield: old}, docfield, new, update_modified=False) + + # update cached link_fields as per new + if doctype == "DocType" and field["parent"] == old: + field["parent"] = new + + +def get_link_fields(doctype: str) -> list[dict]: + # get link fields from tabDocField + if not xhiveframework.flags.link_fields: + xhiveframework.flags.link_fields = {} + + if doctype not in xhiveframework.flags.link_fields: + virtual_doctypes = xhiveframework.get_all("DocType", {"is_virtual": 1}, pluck="name") + + dt = xhiveframework.qb.DocType("DocType") + df = xhiveframework.qb.DocType("DocField") + cf = xhiveframework.qb.DocType("Custom Field") + ps = xhiveframework.qb.DocType("Property Setter") + + standard_fields = ( + xhiveframework.qb.from_(df) + .inner_join(dt) + .on(df.parent == dt.name) + .select(df.parent, df.fieldname, dt.issingle.as_("issingle")) + .where((df.options == doctype) & (df.fieldtype == "Link") & (dt.is_virtual == 0)) + .run(as_dict=True) + ) + + cf_issingle = xhiveframework.qb.from_(dt).select(dt.issingle).where(dt.name == cf.dt).as_("issingle") + custom_fields = ( + xhiveframework.qb.from_(cf) + .select(cf.dt.as_("parent"), cf.fieldname, cf_issingle) + .where((cf.options == doctype) & (cf.fieldtype == "Link") & (cf.dt.notin(virtual_doctypes))) + .run(as_dict=True) + ) + + ps_issingle = xhiveframework.qb.from_(dt).select(dt.issingle).where(dt.name == ps.doc_type).as_("issingle") + property_setter_fields = ( + xhiveframework.qb.from_(ps) + .select(ps.doc_type.as_("parent"), ps.field_name.as_("fieldname"), ps_issingle) + .where( + (ps.property == "options") + & (ps.value == doctype) + & (ps.field_name.notnull()) + & (ps.doc_type.notin(virtual_doctypes)) + ) + .run(as_dict=True) + ) + + xhiveframework.flags.link_fields[doctype] = standard_fields + custom_fields + property_setter_fields + + return xhiveframework.flags.link_fields[doctype] + + +def update_options_for_fieldtype(fieldtype: str, old: str, new: str) -> None: + CustomField = xhiveframework.qb.DocType("Custom Field") + PropertySetter = xhiveframework.qb.DocType("Property Setter") + + if xhiveframework.conf.developer_mode: + for name in xhiveframework.get_all("DocField", filters={"options": old}, pluck="parent"): + if name in (old, new): + continue + + doctype = xhiveframework.get_doc("DocType", name) + save = False + for f in doctype.fields: + if f.options == old: + f.options = new + save = True + if save: + doctype.save() + + DocField = xhiveframework.qb.DocType("DocField") + xhiveframework.qb.update(DocField).set(DocField.options, new).where( + (DocField.fieldtype == fieldtype) & (DocField.options == old) + ).run() + + xhiveframework.qb.update(CustomField).set(CustomField.options, new).where( + (CustomField.fieldtype == fieldtype) & (CustomField.options == old) + ).run() + + xhiveframework.qb.update(PropertySetter).set(PropertySetter.value, new).where( + (PropertySetter.property == "options") & (PropertySetter.value == old) + ).run() + + +def get_select_fields(old: str, new: str) -> list[dict]: + """ + get select type fields where doctype's name is hardcoded as + new line separated list + """ + df = xhiveframework.qb.DocType("DocField") + dt = xhiveframework.qb.DocType("DocType") + cf = xhiveframework.qb.DocType("Custom Field") + ps = xhiveframework.qb.DocType("Property Setter") + + # get link fields from tabDocField + st_issingle = xhiveframework.qb.from_(dt).select(dt.issingle).where(dt.name == df.parent).as_("issingle") + standard_fields = ( + xhiveframework.qb.from_(df) + .select(df.parent, df.fieldname, st_issingle) + .where( + (df.parent != new) + & (df.fieldname != "fieldtype") + & (df.fieldtype == "Select") + & (df.options.like(f"%{old}%")) + ) + .run(as_dict=True) + ) + + # get link fields from tabCustom Field + cf_issingle = xhiveframework.qb.from_(dt).select(dt.issingle).where(dt.name == cf.dt).as_("issingle") + custom_select_fields = ( + xhiveframework.qb.from_(cf) + .select(cf.dt.as_("parent"), cf.fieldname, cf_issingle) + .where((cf.dt != new) & (cf.fieldtype == "Select") & (cf.options.like(f"%{old}%"))) + .run(as_dict=True) + ) + + # remove fields whose options have been changed using property setter + ps_issingle = xhiveframework.qb.from_(dt).select(dt.issingle).where(dt.name == ps.doc_type).as_("issingle") + property_setter_select_fields = ( + xhiveframework.qb.from_(ps) + .select(ps.doc_type.as_("parent"), ps.field_name.as_("fieldname"), ps_issingle) + .where( + (ps.doc_type != new) + & (ps.property == "options") + & (ps.field_name.notnull()) + & (ps.value.like(f"%{old}%")) + ) + .run(as_dict=True) + ) + + return standard_fields + custom_select_fields + property_setter_select_fields + + +def update_select_field_values(old: str, new: str): + from xhiveframework.query_builder.functions import Replace + + DocField = xhiveframework.qb.DocType("DocField") + CustomField = xhiveframework.qb.DocType("Custom Field") + PropertySetter = xhiveframework.qb.DocType("Property Setter") + + xhiveframework.qb.update(DocField).set(DocField.options, Replace(DocField.options, old, new)).where( + (DocField.fieldtype == "Select") + & (DocField.parent != new) + & (DocField.options.like(f"%\n{old}%") | DocField.options.like(f"%{old}\n%")) + ).run() + + xhiveframework.qb.update(CustomField).set(CustomField.options, Replace(CustomField.options, old, new)).where( + (CustomField.fieldtype == "Select") + & (CustomField.dt != new) + & (CustomField.options.like(f"%\n{old}%") | CustomField.options.like(f"%{old}\n%")) + ).run() + + xhiveframework.qb.update(PropertySetter).set(PropertySetter.value, Replace(PropertySetter.value, old, new)).where( + (PropertySetter.property == "options") + & (PropertySetter.field_name.notnull()) + & (PropertySetter.doc_type != new) + & (PropertySetter.value.like(f"%\n{old}%") | PropertySetter.value.like(f"%{old}\n%")) + ).run() + + +def update_parenttype_values(old: str, new: str): + child_doctypes = xhiveframework.get_all( + "DocField", + fields=["options", "fieldname"], + filters={"parent": new, "fieldtype": ["in", xhiveframework.model.table_fields]}, + ) + + custom_child_doctypes = xhiveframework.get_all( + "Custom Field", + fields=["options", "fieldname"], + filters={"dt": new, "fieldtype": ["in", xhiveframework.model.table_fields]}, + ) + + child_doctypes += custom_child_doctypes + fields = [d["fieldname"] for d in child_doctypes] + + property_setter_child_doctypes = xhiveframework.get_all( + "Property Setter", + filters={"doc_type": new, "property": "options", "field_name": ("in", fields)}, + pluck="value", + ) + + child_doctypes = set(list(d["options"] for d in child_doctypes) + property_setter_child_doctypes) + + for doctype in child_doctypes: + table = xhiveframework.qb.DocType(doctype) + xhiveframework.qb.update(table).set(table.parenttype, new).where(table.parenttype == old).run() + + +def rename_dynamic_links(doctype: str, old: str, new: str): + Singles = xhiveframework.qb.DocType("Singles") + for df in get_dynamic_link_map().get(doctype, []): + # dynamic link in single, just one value to check + meta = xhiveframework.get_meta(df.parent) + if meta.is_virtual: + continue + if meta.issingle: + refdoc = xhiveframework.db.get_singles_dict(df.parent) + if refdoc.get(df.options) == doctype and refdoc.get(df.fieldname) == old: + xhiveframework.qb.update(Singles).set(Singles.value, new).where( + (Singles.field == df.fieldname) & (Singles.doctype == df.parent) & (Singles.value == old) + ).run() + else: + # because the table hasn't been renamed yet! + parent = df.parent if df.parent != new else old + + xhiveframework.qb.update(parent).set(df.fieldname, new).where( + (Field(df.options) == doctype) & (Field(df.fieldname) == old) + ).run() + + +def bulk_rename(doctype: str, rows: list[list] | None = None, via_console: bool = False) -> list[str] | None: + """Bulk rename documents + + :param doctype: DocType to be renamed + :param rows: list of documents as `((oldname, newname, merge(optional)), ..)`""" + if not rows: + xhiveframework.throw(_("Please select a valid csv file with data")) + + if not via_console: + max_rows = 500 + if len(rows) > max_rows: + xhiveframework.throw(_("Maximum {0} rows allowed").format(max_rows)) + + rename_log = [] + for row in rows: + # if row has some content + if len(row) > 1 and row[0] and row[1]: + merge = len(row) > 2 and (row[2] == "1" or row[2].lower() == "true") + try: + if rename_doc(doctype, row[0], row[1], merge=merge, rebuild_search=False): + msg = _("Successful: {0} to {1}").format(row[0], row[1]) + xhiveframework.db.commit() + else: + msg = None + except Exception as e: + msg = _("** Failed: {0} to {1}: {2}").format(row[0], row[1], repr(e)) + xhiveframework.db.rollback() + + if msg: + if via_console: + print(msg) + else: + rename_log.append(msg) + + xhiveframework.enqueue("xhiveframework.utils.global_search.rebuild_for_doctype", doctype=doctype) + + if not via_console: + return rename_log diff --git a/xhiveframework/model/sync.py b/xhiveframework/model/sync.py new file mode 100644 index 0000000..baba0c6 --- /dev/null +++ b/xhiveframework/model/sync.py @@ -0,0 +1,176 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +""" + Sync's doctype and docfields from txt files to database + perms will get synced only if none exist +""" +import os + +import xhiveframework +from xhiveframework.cache_manager import clear_controller_cache +from xhiveframework.model.base_document import get_controller +from xhiveframework.modules.import_file import import_file_by_path +from xhiveframework.modules.patch_handler import _patch_mode +from xhiveframework.utils import update_progress_bar + +IMPORTABLE_DOCTYPES = [ + ("core", "doctype"), + ("core", "page"), + ("core", "report"), + ("desk", "dashboard_chart_source"), + ("printing", "print_format"), + ("website", "web_page"), + ("website", "website_theme"), + ("website", "web_form"), + ("website", "web_template"), + ("email", "notification"), + ("printing", "print_style"), + ("desk", "workspace"), + ("desk", "onboarding_step"), + ("desk", "module_onboarding"), + ("desk", "form_tour"), + ("custom", "client_script"), + ("core", "server_script"), + ("custom", "custom_field"), + ("custom", "property_setter"), +] + + +def sync_all(force=0, reset_permissions=False): + _patch_mode(True) + + for app in xhiveframework.get_installed_apps(): + sync_for(app, force, reset_permissions=reset_permissions) + + _patch_mode(False) + + xhiveframework.clear_cache() + + +def sync_for(app_name, force=0, reset_permissions=False): + files = [] + + if app_name == "xhiveframework": + # these need to go first at time of install + + XHIVEFRAMEWORK_PATH = xhiveframework.get_app_path("xhiveframework") + + for core_module in [ + "docfield", + "docperm", + "doctype_action", + "doctype_link", + "doctype_state", + "role", + "has_role", + "doctype", + ]: + files.append(os.path.join(XHIVEFRAMEWORK_PATH, "core", "doctype", core_module, f"{core_module}.json")) + + for custom_module in ["custom_field", "property_setter"]: + files.append( + os.path.join(XHIVEFRAMEWORK_PATH, "custom", "doctype", custom_module, f"{custom_module}.json") + ) + + for website_module in ["web_form", "web_template", "web_form_field", "portal_menu_item"]: + files.append( + os.path.join(XHIVEFRAMEWORK_PATH, "website", "doctype", website_module, f"{website_module}.json") + ) + + for desk_module in [ + "number_card", + "dashboard_chart", + "dashboard", + "onboarding_permission", + "onboarding_step", + "onboarding_step_map", + "module_onboarding", + "workspace_link", + "workspace_chart", + "workspace_shortcut", + "workspace_quick_list", + "workspace_number_card", + "workspace_custom_block", + "workspace", + ]: + files.append(os.path.join(XHIVEFRAMEWORK_PATH, "desk", "doctype", desk_module, f"{desk_module}.json")) + + for module_name, document_type in IMPORTABLE_DOCTYPES: + file = os.path.join(XHIVEFRAMEWORK_PATH, module_name, "doctype", document_type, f"{document_type}.json") + if file not in files: + files.append(file) + + for module_name in xhiveframework.local.app_modules.get(app_name) or []: + folder = os.path.dirname(xhiveframework.get_module(app_name + "." + module_name).__file__) + files = get_doc_files(files=files, start_path=folder) + + l = len(files) + + if l: + for i, doc_path in enumerate(files): + import_file_by_path( + doc_path, force=force, ignore_version=True, reset_permissions=reset_permissions + ) + + xhiveframework.db.commit() + + # show progress bar + update_progress_bar(f"Updating DocTypes for {app_name}", i, l) + + # print each progress bar on new line + print() + + +def get_doc_files(files, start_path): + """walk and sync all doctypes and pages""" + + files = files or [] + + for _module, doctype in IMPORTABLE_DOCTYPES: + doctype_path = os.path.join(start_path, doctype) + if os.path.exists(doctype_path): + for docname in os.listdir(doctype_path): + if os.path.isdir(os.path.join(doctype_path, docname)): + doc_path = os.path.join(doctype_path, docname, docname) + ".json" + if os.path.exists(doc_path): + if doc_path not in files: + files.append(doc_path) + + return files + + +def remove_orphan_doctypes(): + """Find and remove any orphaned doctypes. + + These are doctypes for which code and schema file is + deleted but entry is present in DocType table. + + Note: Deleting the entry doesn't delete any data. + So this is supposed to be non-destrictive operation. + """ + + doctype_names = xhiveframework.get_all("DocType", {"custom": 0}, pluck="name") + orphan_doctypes = [] + + clear_controller_cache() + class_overrides = xhiveframework.get_hooks("override_doctype_class", {}) + + for doctype in doctype_names: + if doctype in class_overrides: + continue + try: + get_controller(doctype=doctype) + except ImportError: + orphan_doctypes.append(doctype) + except Exception: + continue + + if not orphan_doctypes: + return + + print(f"Orphaned DocType(s) found: {', '.join(orphan_doctypes)}") + for i, name in enumerate(orphan_doctypes): + xhiveframework.delete_doc("DocType", name, force=True, ignore_missing=True) + update_progress_bar("Deleting orphaned DocTypes", i, len(orphan_doctypes)) + xhiveframework.db.commit() + print() diff --git a/xhiveframework/model/utils/__init__.py b/xhiveframework/model/utils/__init__.py new file mode 100644 index 0000000..703fa8e --- /dev/null +++ b/xhiveframework/model/utils/__init__.py @@ -0,0 +1,143 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import re + +import xhiveframework +from xhiveframework.build import html_to_js_template +from xhiveframework.utils import cstr +from xhiveframework.utils.caching import site_cache + +STANDARD_FIELD_CONVERSION_MAP = { + "name": "Link", + "owner": "Data", + "idx": "Int", + "creation": "Data", + "modified": "Data", + "modified_by": "Data", + "_user_tags": "Data", + "_liked_by": "Data", + "_comments": "Text", + "_assign": "Text", + "docstatus": "Int", +} +INCLUDE_DIRECTIVE_PATTERN = re.compile(r"""{% include\s['"](.*)['"]\s%}""") + + +def set_default(doc, key): + """Set is_default property of given doc and unset all others filtered by given key.""" + if not doc.is_default: + xhiveframework.db.set(doc, "is_default", 1) + + xhiveframework.db.sql( + """update `tab{}` set `is_default`=0 + where `{}`={} and name!={}""".format(doc.doctype, key, "%s", "%s"), + (doc.get(key), doc.name), + ) + + +def set_field_property(filters, key, value): + """utility set a property in all fields of a particular type""" + docs = [ + xhiveframework.get_doc("DocType", d.parent) + for d in xhiveframework.get_all("DocField", fields=["parent"], filters=filters) + ] + + for d in docs: + d.get("fields", filters)[0].set(key, value) + d.save() + print(f"Updated {d.name}") + + xhiveframework.db.commit() + + +class InvalidIncludePath(xhiveframework.ValidationError): + pass + + +def render_include(content): + """render {% raw %}{% include "app/path/filename" %}{% endraw %} in js file""" + + content = cstr(content) + + # try 5 levels of includes + for _ in range(5): + if "{% include" in content: + paths = INCLUDE_DIRECTIVE_PATTERN.findall(content) + if not paths: + xhiveframework.throw(_("Invalid include path"), InvalidIncludePath) + + for path in paths: + app, app_path = path.split("/", 1) + with open(xhiveframework.get_app_path(app, app_path), encoding="utf-8") as f: + include = f.read() + if path.endswith(".html"): + include = html_to_js_template(path, include) + + content = re.sub(rf"""{{% include\s['"]{path}['"]\s%}}""", include, content) + + else: + break + + return content + + +def get_fetch_values(doctype, fieldname, value): + """Returns fetch value dict for the given object + + :param doctype: Target doctype + :param fieldname: Link fieldname selected + :param value: Value selected + """ + + result = xhiveframework._dict() + meta = xhiveframework.get_meta(doctype) + + # fieldname in target doctype: fieldname in source doctype + fields_to_fetch = { + df.fieldname: df.fetch_from.split(".", 1)[1] for df in meta.get_fields_to_fetch(fieldname) + } + + # nothing to fetch + if not fields_to_fetch: + return result + + # initialise empty values for target fields + for target_fieldname in fields_to_fetch: + result[target_fieldname] = None + + # fetch only if Link field has a truthy value + if not value: + return result + + db_values = xhiveframework.db.get_value( + meta.get_options(fieldname), # source doctype + value, + tuple(set(fields_to_fetch.values())), # unique source fieldnames + as_dict=True, + ) + + # if value doesn't exist in source doctype, get_value returns None + if not db_values: + return result + + for target_fieldname, source_fieldname in fields_to_fetch.items(): + result[target_fieldname] = db_values.get(source_fieldname) + + return result + + +@site_cache() +def is_virtual_doctype(doctype: str): + if xhiveframework.db.has_column("DocType", "is_virtual"): + return xhiveframework.db.get_value("DocType", doctype, "is_virtual") + return False + + +@site_cache() +def is_single_doctype(doctype: str) -> bool: + from xhiveframework.model.base_document import DOCTYPES_FOR_DOCTYPE + + if doctype in DOCTYPES_FOR_DOCTYPE: + return False + + return xhiveframework.db.get_value("DocType", doctype, "issingle") diff --git a/xhiveframework/model/utils/link_count.py b/xhiveframework/model/utils/link_count.py new file mode 100644 index 0000000..bcf4dd8 --- /dev/null +++ b/xhiveframework/model/utils/link_count.py @@ -0,0 +1,78 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from collections import defaultdict + +import xhiveframework + +ignore_doctypes = { + "DocType", + "Print Format", + "Role", + "Module Def", + "Communication", + "ToDo", + "Version", + "Error Log", + "Scheduled Job Log", + "Event Sync Log", + "Event Update Log", + "Access Log", + "View Log", + "Activity Log", + "Energy Point Log", + "Notification Log", + "Email Queue", + "DocShare", + "Document Follow", + "Console Log", + "User", +} + + +def notify_link_count(doctype, name): + """updates link count for given document""" + + if doctype in ignore_doctypes or not xhiveframework.request: + return + + if not hasattr(xhiveframework.local, "_link_count"): + xhiveframework.local._link_count = defaultdict(int) + xhiveframework.db.after_commit.add(flush_local_link_count) + + xhiveframework.local._link_count[(doctype, name)] += 1 + + +def flush_local_link_count(): + """flush from local before ending request""" + new_links = getattr(xhiveframework.local, "_link_count", None) + if not new_links: + return + + link_count = xhiveframework.cache.get_value("_link_count") or {} + + for key, value in new_links.items(): + if key in link_count: + link_count[key] += value + else: + link_count[key] = value + + xhiveframework.cache.set_value("_link_count", link_count) + new_links.clear() + + +def update_link_count(): + """increment link count in the `idx` column for the given document""" + link_count = xhiveframework.cache.get_value("_link_count") + + if link_count: + for (doctype, name), count in link_count.items(): + try: + table = xhiveframework.qb.DocType(doctype) + xhiveframework.qb.update(table).set(table.idx, table.idx + count).where(table.name == name).run() + xhiveframework.db.commit() + except Exception as e: + if not xhiveframework.db.is_table_missing(e): # table not found, single + raise e + # reset the count + xhiveframework.cache.delete_value("_link_count") diff --git a/xhiveframework/model/utils/rename_doc.py b/xhiveframework/model/utils/rename_doc.py new file mode 100644 index 0000000..8819a9b --- /dev/null +++ b/xhiveframework/model/utils/rename_doc.py @@ -0,0 +1,63 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from itertools import product + +import xhiveframework +from xhiveframework.model.rename_doc import get_link_fields + + +def update_linked_doctypes( + doctype: str, docname: str, linked_to: str, value: str, ignore_doctypes: list | None = None +): + """ + linked_doctype_info_list = list formed by get_fetch_fields() function + docname = Master DocType's name in which modification are made + value = Value for the field thats set in other DocType's by fetching from Master DocType + """ + linked_doctype_info_list = get_fetch_fields(doctype, linked_to, ignore_doctypes) + + for d in linked_doctype_info_list: + xhiveframework.db.set_value( + d.doctype, + { + d.master_fieldname: docname, + d.linked_to_fieldname: ("!=", value), + }, + d.linked_to_fieldname, + value, + ) + + +def get_fetch_fields(doctype: str, linked_to: str, ignore_doctypes: list | None = None) -> list[dict]: + """ + doctype = Master DocType in which the changes are being made + linked_to = DocType name of the field thats being updated in Master + This function fetches list of all DocType where both doctype and linked_to is found + as link fields. + Forms a list of dict in the form - + [{doctype: , master_fieldname: , linked_to_fieldname: ] + where + doctype = DocType where changes need to be made + master_fieldname = Fieldname where options = doctype + linked_to_fieldname = Fieldname where options = linked_to + """ + + out = [] + master_list = get_link_fields(doctype) + linked_to_list = get_link_fields(linked_to) + product_list = product(master_list, linked_to_list) + + for d in product_list: + linked_doctype_info = xhiveframework._dict() + if ( + d[0]["parent"] == d[1]["parent"] + and (not ignore_doctypes or d[0]["parent"] not in ignore_doctypes) + and not d[1]["issingle"] + ): + linked_doctype_info.doctype = d[0]["parent"] + linked_doctype_info.master_fieldname = d[0]["fieldname"] + linked_doctype_info.linked_to_fieldname = d[1]["fieldname"] + out.append(linked_doctype_info) + + return out diff --git a/xhiveframework/model/utils/rename_field.py b/xhiveframework/model/utils/rename_field.py new file mode 100644 index 0000000..55961ce --- /dev/null +++ b/xhiveframework/model/utils/rename_field.py @@ -0,0 +1,176 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import json + +import xhiveframework +from xhiveframework.model import no_value_fields, table_fields +from xhiveframework.model.utils.user_settings import sync_user_settings, update_user_settings_data +from xhiveframework.utils.password import rename_password_field + + +def rename_field(doctype, old_fieldname, new_fieldname, validate=True): + """This functions assumes that doctype is already synced""" + + meta = xhiveframework.get_meta(doctype, cached=False) + new_field = meta.get_field(new_fieldname) + + if validate: + if not new_field: + print("rename_field: " + (new_fieldname) + " not found in " + doctype) + return + + if not meta.issingle and not xhiveframework.db.has_column(doctype, old_fieldname): + print("rename_field: " + (old_fieldname) + " not found in table for: " + doctype) + # never had the field? + return + + if new_field.fieldtype in table_fields: + # change parentfield of table mentioned in options + xhiveframework.db.sql( + """update `tab{}` set parentfield={} + where parentfield={}""".format(new_field.options.split("\n", 1)[0], "%s", "%s"), + (new_fieldname, old_fieldname), + ) + + elif new_field.fieldtype not in no_value_fields: + if meta.issingle: + xhiveframework.db.sql( + """update `tabSingles` set field=%s + where doctype=%s and field=%s""", + (new_fieldname, doctype, old_fieldname), + ) + else: + # copy field value + xhiveframework.db.sql(f"""update `tab{doctype}` set `{new_fieldname}`=`{old_fieldname}`""") + + update_reports(doctype, old_fieldname, new_fieldname) + update_users_report_view_settings(doctype, old_fieldname, new_fieldname) + + if new_field.fieldtype == "Password": + rename_password_field(doctype, old_fieldname, new_fieldname) + + # update in property setter + update_property_setters(doctype, old_fieldname, new_fieldname) + + # update in user settings + update_user_settings(doctype, old_fieldname, new_fieldname) + + +def update_reports(doctype, old_fieldname, new_fieldname): + def _get_new_sort_by(report_dict, report, key): + sort_by = report_dict.get(key) or "" + if sort_by: + sort_by = sort_by.split(".") + if len(sort_by) > 1: + if sort_by[0] == doctype and sort_by[1] == old_fieldname: + sort_by = doctype + "." + new_fieldname + report_dict["updated"] = True + elif report.ref_doctype == doctype and sort_by[0] == old_fieldname: + sort_by = doctype + "." + new_fieldname + report_dict["updated"] = True + + if isinstance(sort_by, list): + sort_by = ".".join(sort_by) + + return sort_by + + reports = xhiveframework.db.sql( + """select name, ref_doctype, json from tabReport + where report_type = 'Report Builder' and ifnull(is_standard, 'No') = 'No' + and json like %s and json like %s""", + ("%%%s%%" % old_fieldname, "%%%s%%" % doctype), + as_dict=True, + ) + + for r in reports: + report_dict = json.loads(r.json) + + # update filters + new_filters = [] + if report_dict.get("filters"): + for f in report_dict.get("filters"): + if f and len(f) > 1 and f[0] == doctype and f[1] == old_fieldname: + new_filters.append([doctype, new_fieldname, f[2], f[3]]) + report_dict["updated"] = True + else: + new_filters.append(f) + + # update columns + new_columns = [] + if report_dict.get("columns"): + for c in report_dict.get("columns"): + if c and len(c) > 1 and c[0] == old_fieldname and c[1] == doctype: + new_columns.append([new_fieldname, doctype]) + report_dict["updated"] = True + else: + new_columns.append(c) + + # update sort by + new_sort_by = _get_new_sort_by(report_dict, r, "sort_by") + new_sort_by_next = _get_new_sort_by(report_dict, r, "sort_by_next") + + if report_dict.get("updated"): + new_val = json.dumps( + { + "filters": new_filters, + "columns": new_columns, + "sort_by": new_sort_by, + "sort_order": report_dict.get("sort_order"), + "sort_by_next": new_sort_by_next, + "sort_order_next": report_dict.get("sort_order_next"), + } + ) + + xhiveframework.db.sql("""update `tabReport` set `json`=%s where name=%s""", (new_val, r.name)) + + +def update_users_report_view_settings(doctype, ref_fieldname, new_fieldname): + user_report_cols = xhiveframework.db.sql( + """select defkey, defvalue from `tabDefaultValue` where + defkey like '_list_settings:%'""" + ) + for key, value in user_report_cols: + new_columns = [] + columns_modified = False + for field, field_doctype in json.loads(value): + if field == ref_fieldname and field_doctype == doctype: + new_columns.append([new_fieldname, field_doctype]) + columns_modified = True + else: + new_columns.append([field, field_doctype]) + + if columns_modified: + xhiveframework.db.sql( + """update `tabDefaultValue` set defvalue={} + where defkey={}""".format("%s", "%s"), + (json.dumps(new_columns), key), + ) + + +def update_property_setters(doctype, old_fieldname, new_fieldname): + xhiveframework.db.sql( + """update `tabProperty Setter` set field_name = %s + where doc_type=%s and field_name=%s""", + (new_fieldname, doctype, old_fieldname), + ) + + xhiveframework.db.sql( + """update `tabCustom Field` set insert_after=%s + where insert_after=%s and dt=%s""", + (new_fieldname, old_fieldname, doctype), + ) + + +def update_user_settings(doctype, old_fieldname, new_fieldname): + # store the user settings data from the redis to db + sync_user_settings() + + user_settings = xhiveframework.db.sql( + ''' select user, doctype, data from `__UserSettings` + where doctype=%s and data like "%%%s%%"''', + (doctype, old_fieldname), + as_dict=1, + ) + + for user_setting in user_settings: + update_user_settings_data(user_setting, "docfield", old_fieldname, new_fieldname) diff --git a/xhiveframework/model/utils/user_settings.py b/xhiveframework/model/utils/user_settings.py new file mode 100644 index 0000000..7fe5678 --- /dev/null +++ b/xhiveframework/model/utils/user_settings.py @@ -0,0 +1,105 @@ +# Settings saved per user basis +# such as page_limit, filters, last_view + +import json + +import xhiveframework +from xhiveframework import safe_decode + +# dict for mapping the index and index type for the filters of different views +filter_dict = {"doctype": 0, "docfield": 1, "operator": 2, "value": 3} + + +def get_user_settings(doctype, for_update=False): + user_settings = xhiveframework.cache.hget("_user_settings", f"{doctype}::{xhiveframework.session.user}") + + if user_settings is None: + user_settings = xhiveframework.db.sql( + """select data from `__UserSettings` + where `user`=%s and `doctype`=%s""", + (xhiveframework.session.user, doctype), + ) + user_settings = user_settings and user_settings[0][0] or "{}" + + if not for_update: + update_user_settings(doctype, user_settings, True) + + return user_settings or "{}" + + +def update_user_settings(doctype, user_settings, for_update=False): + """update user settings in cache""" + + if for_update: + current = json.loads(user_settings) + else: + current = json.loads(get_user_settings(doctype, for_update=True)) + + if isinstance(current, str): + # corrupt due to old code, remove this in a future release + current = {} + + current.update(user_settings) + + xhiveframework.cache.hset("_user_settings", f"{doctype}::{xhiveframework.session.user}", json.dumps(current)) + + +def sync_user_settings(): + """Sync from cache to database (called asynchronously via the browser)""" + for key, data in xhiveframework.cache.hgetall("_user_settings").items(): + key = safe_decode(key) + doctype, user = key.split("::") # WTF? + xhiveframework.db.multisql( + { + "mariadb": """INSERT INTO `__UserSettings`(`user`, `doctype`, `data`) + VALUES (%s, %s, %s) + ON DUPLICATE key UPDATE `data`=%s""", + "postgres": """INSERT INTO `__UserSettings` (`user`, `doctype`, `data`) + VALUES (%s, %s, %s) + ON CONFLICT ("user", "doctype") DO UPDATE SET `data`=%s""", + }, + (user, doctype, data, data), + as_dict=1, + ) + + +@xhiveframework.whitelist() +def save(doctype, user_settings): + user_settings = json.loads(user_settings or "{}") + update_user_settings(doctype, user_settings) + return user_settings + + +@xhiveframework.whitelist() +def get(doctype): + return get_user_settings(doctype) + + +def update_user_settings_data( + user_setting, fieldname, old, new, condition_fieldname=None, condition_values=None +): + data = user_setting.get("data") + if data: + update = False + data = json.loads(data) + for view in ["List", "Gantt", "Kanban", "Calendar", "Image", "Inbox", "Report"]: + view_settings = data.get(view) + if view_settings and view_settings.get("filters"): + view_filters = view_settings.get("filters") + for view_filter in view_filters: + if ( + condition_fieldname + and view_filter[filter_dict[condition_fieldname]] != condition_values + ): + continue + if view_filter[filter_dict[fieldname]] == old: + view_filter[filter_dict[fieldname]] = new + update = True + if update: + xhiveframework.db.sql( + "update __UserSettings set data=%s where doctype=%s and user=%s", + (json.dumps(data), user_setting.doctype, user_setting.user), + ) + + # clear that user settings from the redis cache + xhiveframework.cache.hset("_user_settings", f"{user_setting.doctype}::{user_setting.user}", None) diff --git a/xhiveframework/model/virtual_doctype.py b/xhiveframework/model/virtual_doctype.py new file mode 100644 index 0000000..36fc73a --- /dev/null +++ b/xhiveframework/model/virtual_doctype.py @@ -0,0 +1,93 @@ +import inspect +from typing import Protocol, runtime_checkable + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.base_document import get_controller + + +@runtime_checkable +class VirtualDoctype(Protocol): + """This class documents requirements that must be met by a doctype controller to function as virtual doctype + + + Additional requirements: + - DocType controller has to inherit from `xhiveframework.model.document.Document` class + + Note: + - "Backend" here means any storage service, it can be a database, flat file or network call to API. + """ + + # ============ class/static methods ============ + + @staticmethod + def get_list(args) -> list[xhiveframework._dict]: + """Similar to reportview.get_list""" + ... + + @staticmethod + def get_count(args) -> int: + """Similar to reportview.get_count, return total count of documents on listview.""" + ... + + @staticmethod + def get_stats(args): + """Similar to reportview.get_stats, return sidebar stats.""" + ... + + # ============ instance methods ============ + + def db_insert(self, *args, **kwargs) -> None: + """Serialize the `Document` object and insert it in backend.""" + ... + + def load_from_db(self) -> None: + """Using self.name initialize current document from backend data. + + This is responsible for updatinng __dict__ of class with all the fields on doctype.""" + ... + + def db_update(self, *args, **kwargs) -> None: + """Serialize the `Document` object and update existing document in backend.""" + ... + + def delete(self, *args, **kwargs) -> None: + """Delete the current document from backend""" + ... + + +def validate_controller(doctype: str) -> None: + try: + controller = get_controller(doctype) + except ImportError: + xhiveframework.msgprint(_("Failed to import virtual doctype {}, is controller file present?").format(doctype)) + return + + def _as_str(method): + if hasattr(method, "__module__"): + return f"{method.__module__}.{method.__qualname__}" + return "None" + + expected_static_method = ["get_list", "get_count", "get_stats"] + for m in expected_static_method: + method = inspect.getattr_static(controller, m, None) + if not isinstance(method, staticmethod): + xhiveframework.msgprint( + _("Virtual DocType {} requires a static method called {} found {}").format( + xhiveframework.bold(doctype), xhiveframework.bold(m), xhiveframework.bold(_as_str(method)) + ), + title=_("Incomplete Virtual Doctype Implementation"), + ) + + expected_instance_methods = ["db_insert", "db_update", "load_from_db", "delete"] + parent_class = controller.mro()[1] + for m in expected_instance_methods: + method = getattr(controller, m, None) + original_method = getattr(parent_class, m, None) + if method == original_method: + xhiveframework.msgprint( + _("Virtual DocType {} requires overriding an instance method called {} found {}").format( + xhiveframework.bold(doctype), xhiveframework.bold(m), xhiveframework.bold(_as_str(method)) + ), + title=_("Incomplete Virtual Doctype Implementation"), + ) diff --git a/xhiveframework/model/workflow.py b/xhiveframework/model/workflow.py new file mode 100644 index 0000000..56e2069 --- /dev/null +++ b/xhiveframework/model/workflow.py @@ -0,0 +1,369 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import json +from collections import defaultdict +from typing import TYPE_CHECKING, Union + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.docstatus import DocStatus +from xhiveframework.utils import cint + +if TYPE_CHECKING: + from xhiveframework.model.document import Document + from xhiveframework.workflow.doctype.workflow.workflow import Workflow + + +class WorkflowStateError(xhiveframework.ValidationError): + pass + + +class WorkflowTransitionError(xhiveframework.ValidationError): + pass + + +class WorkflowPermissionError(xhiveframework.ValidationError): + pass + + +def get_workflow_name(doctype): + workflow_name = xhiveframework.cache.hget("workflow", doctype) + if workflow_name is None: + workflow_name = xhiveframework.db.get_value("Workflow", {"document_type": doctype, "is_active": 1}, "name") + xhiveframework.cache.hset("workflow", doctype, workflow_name or "") + + return workflow_name + + +@xhiveframework.whitelist() +def get_transitions( + doc: Union["Document", str, dict], workflow: "Workflow" = None, raise_exception: bool = False +) -> list[dict]: + """Return list of possible transitions for the given doc""" + from xhiveframework.model.document import Document + + if not isinstance(doc, Document): + doc = xhiveframework.get_doc(xhiveframework.parse_json(doc)) + doc.load_from_db() + + if doc.is_new(): + return [] + + doc.check_permission("read") + + workflow = workflow or get_workflow(doc.doctype) + current_state = doc.get(workflow.workflow_state_field) + + if not current_state: + if raise_exception: + raise WorkflowStateError + else: + xhiveframework.throw(_("Workflow State not set"), WorkflowStateError) + + transitions = [] + roles = xhiveframework.get_roles() + + for transition in workflow.transitions: + if transition.state == current_state and transition.allowed in roles: + if not is_transition_condition_satisfied(transition, doc): + continue + transitions.append(transition.as_dict()) + + return transitions + + +def get_workflow_safe_globals(): + # access to xhiveframework.db.get_value, xhiveframework.db.get_list, and date time utils. + return dict( + xhiveframework=xhiveframework._dict( + db=xhiveframework._dict(get_value=xhiveframework.db.get_value, get_list=xhiveframework.db.get_list), + session=xhiveframework.session, + utils=xhiveframework._dict( + now_datetime=xhiveframework.utils.now_datetime, + add_to_date=xhiveframework.utils.add_to_date, + get_datetime=xhiveframework.utils.get_datetime, + now=xhiveframework.utils.now, + ), + ) + ) + + +def is_transition_condition_satisfied(transition, doc) -> bool: + if not transition.condition: + return True + else: + return xhiveframework.safe_eval(transition.condition, get_workflow_safe_globals(), dict(doc=doc.as_dict())) + + +@xhiveframework.whitelist() +def apply_workflow(doc, action): + """Allow workflow action on the current doc""" + doc = xhiveframework.get_doc(xhiveframework.parse_json(doc)) + doc.load_from_db() + workflow = get_workflow(doc.doctype) + transitions = get_transitions(doc, workflow) + user = xhiveframework.session.user + + # find the transition + transition = None + for t in transitions: + if t.action == action: + transition = t + + if not transition: + xhiveframework.throw(_("Not a valid Workflow Action"), WorkflowTransitionError) + + if not has_approval_access(user, doc, transition): + xhiveframework.throw(_("Self approval is not allowed")) + + # update workflow state field + doc.set(workflow.workflow_state_field, transition.next_state) + + # find settings for the next state + next_state = next(d for d in workflow.states if d.state == transition.next_state) + + # update any additional field + if next_state.update_field: + doc.set(next_state.update_field, next_state.update_value) + + new_docstatus = cint(next_state.doc_status) + if doc.docstatus.is_draft() and new_docstatus == DocStatus.draft(): + doc.save() + elif doc.docstatus.is_draft() and new_docstatus == DocStatus.submitted(): + doc.submit() + elif doc.docstatus.is_submitted() and new_docstatus == DocStatus.submitted(): + doc.save() + elif doc.docstatus.is_submitted() and new_docstatus == DocStatus.cancelled(): + doc.cancel() + else: + xhiveframework.throw(_("Illegal Document Status for {0}").format(next_state.state)) + + doc.add_comment("Workflow", _(next_state.state)) + + return doc + + +@xhiveframework.whitelist() +def can_cancel_document(doctype): + workflow = get_workflow(doctype) + cancelling_states = [s.state for s in workflow.states if s.doc_status == "2"] + if not cancelling_states: + return True + + for transition in workflow.transitions: + if transition.next_state in cancelling_states: + return False + return True + + +def validate_workflow(doc): + """Validate Workflow State and Transition for the current user. + + - Check if user is allowed to edit in current state + - Check if user is allowed to transition to the next state (if changed) + """ + workflow = get_workflow(doc.doctype) + + current_state = None + if getattr(doc, "_doc_before_save", None): + current_state = doc._doc_before_save.get(workflow.workflow_state_field) + next_state = doc.get(workflow.workflow_state_field) + + if not next_state: + next_state = workflow.states[0].state + doc.set(workflow.workflow_state_field, next_state) + + if not current_state: + current_state = workflow.states[0].state + + state_row = [d for d in workflow.states if d.state == current_state] + if not state_row: + xhiveframework.throw( + _("{0} is not a valid Workflow State. Please update your Workflow and try again.").format( + xhiveframework.bold(current_state) + ) + ) + state_row = state_row[0] + + # if transitioning, check if user is allowed to transition + if current_state != next_state: + bold_current = xhiveframework.bold(current_state) + bold_next = xhiveframework.bold(next_state) + + if not doc._doc_before_save: + # transitioning directly to a state other than the first + # e.g from data import + xhiveframework.throw( + _("Workflow State transition not allowed from {0} to {1}").format(bold_current, bold_next), + WorkflowPermissionError, + ) + + transitions = get_transitions(doc._doc_before_save) + transition = [d for d in transitions if d.next_state == next_state] + if not transition: + xhiveframework.throw( + _("Workflow State transition not allowed from {0} to {1}").format(bold_current, bold_next), + WorkflowPermissionError, + ) + + +def get_workflow(doctype) -> "Workflow": + return xhiveframework.get_cached_doc("Workflow", get_workflow_name(doctype)) + + +def has_approval_access(user, doc, transition): + return user == "Administrator" or transition.get("allow_self_approval") or user != doc.get("owner") + + +def get_workflow_state_field(workflow_name): + return get_workflow_field_value(workflow_name, "workflow_state_field") + + +def send_email_alert(workflow_name): + return get_workflow_field_value(workflow_name, "send_email_alert") + + +def get_workflow_field_value(workflow_name, field): + return xhiveframework.get_cached_value("Workflow", workflow_name, field) + + +@xhiveframework.whitelist() +def bulk_workflow_approval(docnames, doctype, action): + docnames = json.loads(docnames) + if len(docnames) < 20: + _bulk_workflow_action(docnames, doctype, action) + elif len(docnames) <= 500: + xhiveframework.msgprint(_("Bulk {0} is enqueued in background.").format(action), alert=True) + xhiveframework.enqueue( + _bulk_workflow_action, + docnames=docnames, + doctype=doctype, + action=action, + queue="short", + timeout=1000, + ) + else: + xhiveframework.throw(_("Bulk approval only support up to 500 documents."), title=_("Too Many Documents")) + + +def _bulk_workflow_action(docnames, doctype, action): + # dictionaries for logging + failed_transactions = defaultdict(list) + successful_transactions = defaultdict(list) + + xhiveframework.clear_messages() + for idx, docname in enumerate(docnames, 1): + message_dict = {} + try: + show_progress(docnames, _("Applying: {0}").format(action), idx, docname) + apply_workflow(xhiveframework.get_doc(doctype, docname), action) + xhiveframework.db.commit() + except Exception as e: + if not xhiveframework.message_log: + # Exception is raised manually and not from msgprint or throw + message = f"{e.__class__.__name__}" + if e.args: + message += f" : {e.args[0]}" + message_dict = {"docname": docname, "message": message} + failed_transactions[docname].append(message_dict) + + xhiveframework.db.rollback() + xhiveframework.log_error( + title=f"Workflow {action} threw an error for {doctype} {docname}", + reference_doctype="Workflow", + reference_name=action, + ) + finally: + if not message_dict: + if xhiveframework.message_log: + messages = xhiveframework.get_message_log() + for message in messages: + xhiveframework.message_log.pop() + message_dict = {"docname": docname, "message": message.get("message")} + + if message.get("raise_exception", False): + failed_transactions[docname].append(message_dict) + else: + successful_transactions[docname].append(message_dict) + else: + successful_transactions[docname].append({"docname": docname, "message": None}) + + if failed_transactions and successful_transactions: + indicator = "orange" + elif failed_transactions: + indicator = "red" + else: + indicator = "green" + + print_workflow_log(failed_transactions, _("Failed Transactions"), doctype, indicator) + print_workflow_log(successful_transactions, _("Successful Transactions"), doctype, indicator) + + +def print_workflow_log(messages, title, doctype, indicator): + if messages.keys(): + msg = f"

      {title}

      " + + for doc in messages.keys(): + if len(messages[doc]): + html = f"
      {xhiveframework.utils.get_link_to_form(doctype, doc)}" + for log in messages[doc]: + if log.get("message"): + html += "
      {}
      ".format( + log.get("message") + ) + html += "
      " + else: + html = f"
      {doc}
      " + msg += html + + xhiveframework.msgprint( + msg, title=_("Workflow Status"), indicator=indicator, is_minimizable=True, realtime=True + ) + + +@xhiveframework.whitelist() +def get_common_transition_actions(docs, doctype): + common_actions = [] + if isinstance(docs, str): + docs = json.loads(docs) + try: + for i, doc in enumerate(docs, 1): + if not doc.get("doctype"): + doc["doctype"] = doctype + actions = [ + t.get("action") + for t in get_transitions(doc, raise_exception=True) + if has_approval_access(xhiveframework.session.user, doc, t) + ] + if not actions: + return [] + common_actions = actions if i == 1 else set(common_actions).intersection(actions) + if not common_actions: + return [] + except WorkflowStateError: + pass + + return list(common_actions) + + +def show_progress(docnames, message, i, description): + n = len(docnames) + if n >= 5: + xhiveframework.publish_progress(float(i) * 100 / n, title=message, description=description) + + +def set_workflow_state_on_action(doc, workflow_name, action): + workflow = xhiveframework.get_doc("Workflow", workflow_name) + workflow_state_field = workflow.workflow_state_field + + # If workflow state of doc is already correct, don't set workflow state + for state in workflow.states: + if state.state == doc.get(workflow_state_field) and doc.docstatus == cint(state.doc_status): + return + + action_map = {"update_after_submit": "1", "submit": "1", "cancel": "2"} + docstatus = action_map[action] + for state in workflow.states: + if state.doc_status == docstatus: + doc.set(workflow_state_field, state.state) + return diff --git a/xhiveframework/modules.txt b/xhiveframework/modules.txt new file mode 100644 index 0000000..863c448 --- /dev/null +++ b/xhiveframework/modules.txt @@ -0,0 +1,12 @@ +Core +Website +Workflow +Email +Custom +Geo +Desk +Integrations +Printing +Contacts +Social +Automation \ No newline at end of file diff --git a/xhiveframework/modules/__init__.py b/xhiveframework/modules/__init__.py new file mode 100644 index 0000000..16281fe --- /dev/null +++ b/xhiveframework/modules/__init__.py @@ -0,0 +1 @@ +from .utils import * diff --git a/xhiveframework/modules/export_file.py b/xhiveframework/modules/export_file.py new file mode 100644 index 0000000..c7ddc9b --- /dev/null +++ b/xhiveframework/modules/export_file.py @@ -0,0 +1,160 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import os +import shutil + +import xhiveframework +import xhiveframework.model +from xhiveframework.modules import get_module_path, scrub, scrub_dt_dn + + +def export_doc(doc): + write_document_file(doc) + + +def export_to_files(record_list=None, record_module=None, verbose=0, create_init=None): + """ + Export record_list to files. record_list is a list of lists ([doctype, docname, folder name],) , + """ + if xhiveframework.flags.in_import: + return + + if record_list: + for record in record_list: + folder_name = record[2] if len(record) == 3 else None + write_document_file( + xhiveframework.get_doc(record[0], record[1]), + record_module, + create_init=create_init, + folder_name=folder_name, + ) + + +def write_document_file(doc, record_module=None, create_init=True, folder_name=None): + doc_export = doc.as_dict(no_nulls=True) + doc.run_method("before_export", doc_export) + + doc_export = strip_default_fields(doc, doc_export) + module = record_module or get_module_name(doc) + + # create folder + if folder_name: + folder = create_folder(module, folder_name, doc.name, create_init) + else: + folder = create_folder(module, doc.doctype, doc.name, create_init) + + fname = scrub(doc.name) + write_code_files(folder, fname, doc, doc_export) + + # write the data file + path = os.path.join(folder, f"{fname}.json") + with open(path, "w+") as txtfile: + txtfile.write(xhiveframework.as_json(doc_export)) + print(f"Wrote document file for {doc.doctype} {doc.name} at {path}") + + +def strip_default_fields(doc, doc_export): + # strip out default fields from children + if doc.doctype == "DocType" and doc.migration_hash: + del doc_export["migration_hash"] + + for df in doc.meta.get_table_fields(): + for d in doc_export.get(df.fieldname): + for fieldname in xhiveframework.model.default_fields + xhiveframework.model.child_table_fields: + if fieldname in d: + del d[fieldname] + + return doc_export + + +def write_code_files(folder, fname, doc, doc_export): + """Export code files and strip from values""" + if hasattr(doc, "get_code_fields"): + for key, extn in doc.get_code_fields().items(): + if doc.get(key): + with open(os.path.join(folder, fname + "." + extn), "w+") as txtfile: + txtfile.write(doc.get(key)) + + # remove from exporting + del doc_export[key] + + +def get_module_name(doc): + if doc.doctype == "Module Def": + module = doc.name + elif doc.doctype == "Workflow": + module = xhiveframework.db.get_value("DocType", doc.document_type, "module") + elif hasattr(doc, "module"): + module = doc.module + else: + module = xhiveframework.db.get_value("DocType", doc.doctype, "module") + + return module + + +def delete_folder(module, dt, dn): + if xhiveframework.db.get_value("Module Def", module, "custom"): + module_path = get_custom_module_path(module) + else: + module_path = get_module_path(module) + + dt, dn = scrub_dt_dn(dt, dn) + + # delete folder + folder = os.path.join(module_path, dt, dn) + + if os.path.exists(folder): + shutil.rmtree(folder) + + +def create_folder(module, dt, dn, create_init): + if xhiveframework.db.get_value("Module Def", module, "custom"): + module_path = get_custom_module_path(module) + else: + module_path = get_module_path(module) + + dt, dn = scrub_dt_dn(dt, dn) + + # create folder + folder = os.path.join(module_path, dt, dn) + + xhiveframework.create_folder(folder) + + # create init_py_files + if create_init: + create_init_py(module_path, dt, dn) + + return folder + + +def get_custom_module_path(module): + package = xhiveframework.db.get_value("Module Def", module, "package") + if not package: + xhiveframework.throw(f"Package must be set for custom Module {module}") + + path = os.path.join(get_package_path(package), scrub(module)) + if not os.path.exists(path): + os.makedirs(path) + + return path + + +def get_package_path(package): + path = os.path.join( + xhiveframework.get_site_path("packages"), xhiveframework.db.get_value("Package", package, "package_name") + ) + if not os.path.exists(path): + os.makedirs(path) + return path + + +def create_init_py(module_path, dt, dn): + def create_if_not_exists(path): + initpy = os.path.join(path, "__init__.py") + if not os.path.exists(initpy): + open(initpy, "w").close() + + create_if_not_exists(os.path.join(module_path)) + create_if_not_exists(os.path.join(module_path, dt)) + create_if_not_exists(os.path.join(module_path, dt, dn)) diff --git a/xhiveframework/modules/import_file.py b/xhiveframework/modules/import_file.py new file mode 100644 index 0000000..0209205 --- /dev/null +++ b/xhiveframework/modules/import_file.py @@ -0,0 +1,287 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import hashlib +import json +import os + +import xhiveframework +from xhiveframework.model.base_document import get_controller +from xhiveframework.modules import get_module_path, scrub_dt_dn +from xhiveframework.query_builder import DocType +from xhiveframework.utils import get_datetime, now + + +def calculate_hash(path: str) -> str: + """Calculate md5 hash of the file in binary mode + + Args: + path (str): Path to the file to be hashed + + Returns: + str: The calculated hash + """ + hash_md5 = hashlib.md5(usedforsecurity=False) + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() + + +ignore_values = { + "Report": ["disabled", "prepared_report", "add_total_row"], + "Print Format": ["disabled"], + "Notification": ["enabled"], + "Print Style": ["disabled"], + "Module Onboarding": ["is_complete"], + "Onboarding Step": ["is_complete", "is_skipped"], + "Workspace": ["is_hidden"], +} + +ignore_doctypes = [""] + + +def import_files(module, dt=None, dn=None, force=False, pre_process=None, reset_permissions=False): + if isinstance(module, list): + return [ + import_file( + m[0], + m[1], + m[2], + force=force, + pre_process=pre_process, + reset_permissions=reset_permissions, + ) + for m in module + ] + else: + return import_file( + module, dt, dn, force=force, pre_process=pre_process, reset_permissions=reset_permissions + ) + + +def import_file(module, dt, dn, force=False, pre_process=None, reset_permissions=False): + """Sync a file from txt if modifed, return false if not updated""" + path = get_file_path(module, dt, dn) + return import_file_by_path(path, force, pre_process=pre_process, reset_permissions=reset_permissions) + + +def get_file_path(module, dt, dn): + dt, dn = scrub_dt_dn(dt, dn) + + path = os.path.join(get_module_path(module), os.path.join(dt, dn, f"{dn}.json")) + + return path + + +def import_file_by_path( + path: str, + force: bool = False, + data_import: bool = False, + pre_process=None, + ignore_version: bool | None = None, + reset_permissions: bool = False, +): + """Import file from the given path + + Some conditions decide if a file should be imported or not. + Evaluation takes place in the order they are mentioned below. + + - Check if `force` is true. Import the file. If not, move ahead. + - Get `db_modified_timestamp`(value of the modified field in the database for the file). + If the return is `none,` this file doesn't exist in the DB, so Import the file. If not, move ahead. + - Check if there is a hash in DB for that file. If there is, Calculate the Hash of the file to import and compare it with the one in DB if they are not equal. + Import the file. If Hash doesn't exist, move ahead. + - Check if `db_modified_timestamp` is older than the timestamp in the file; if it is, we import the file. + + If timestamp comparison happens for doctypes, that means the Hash for it doesn't exist. + So, even if the timestamp is newer on DB (When comparing timestamps), we import the file and add the calculated Hash to the DB. + So in the subsequent imports, we can use hashes to compare. As a precautionary measure, the timestamp is updated to the current time as well. + + Args: + path (str): Path to the file. + force (bool, optional): Load the file without checking any conditions. Defaults to False. + data_import (bool, optional): [description]. Defaults to False. + pre_process ([type], optional): Any preprocesing that may need to take place on the doc. Defaults to None. + ignore_version (bool, optional): ignore current version. Defaults to None. + reset_permissions (bool, optional): reset permissions for the file. Defaults to False. + + Returns: + [bool]: True if import takes place. False if it wasn't imported. + """ + try: + docs = read_doc_from_file(path) + except OSError: + print(f"{path} missing") + return + + calculated_hash = calculate_hash(path) + + if docs: + if not isinstance(docs, list): + docs = [docs] + + for doc in docs: + # modified timestamp in db, none if doctype's first import + db_modified_timestamp = xhiveframework.db.get_value(doc["doctype"], doc["name"], "modified") + is_db_timestamp_latest = db_modified_timestamp and ( + get_datetime(doc.get("modified")) <= get_datetime(db_modified_timestamp) + ) + + if not force and db_modified_timestamp: + stored_hash = None + if doc["doctype"] == "DocType": + try: + stored_hash = xhiveframework.db.get_value(doc["doctype"], doc["name"], "migration_hash") + except Exception: + pass + + # if hash exists and is equal no need to update + if stored_hash and stored_hash == calculated_hash: + continue + + # if hash doesn't exist, check if db timestamp is same as json timestamp, add hash if from doctype + if is_db_timestamp_latest and doc["doctype"] != "DocType": + continue + + import_doc( + docdict=doc, + data_import=data_import, + pre_process=pre_process, + ignore_version=ignore_version, + reset_permissions=reset_permissions, + path=path, + ) + + if doc["doctype"] == "DocType": + doctype_table = DocType("DocType") + xhiveframework.qb.update(doctype_table).set(doctype_table.migration_hash, calculated_hash).where( + doctype_table.name == doc["name"] + ).run() + + new_modified_timestamp = doc.get("modified") + + # if db timestamp is newer, hash must have changed, must update db timestamp + if is_db_timestamp_latest and doc["doctype"] == "DocType": + new_modified_timestamp = now() + + if new_modified_timestamp: + update_modified(new_modified_timestamp, doc) + + return True + + +def read_doc_from_file(path): + doc = None + if os.path.exists(path): + with open(path) as f: + try: + doc = json.loads(f.read()) + except ValueError: + print(f"bad json: {path}") + raise + else: + raise OSError("%s missing" % path) + + return doc + + +def update_modified(original_modified, doc): + # since there is a new timestamp on the file, update timestamp in + if doc["doctype"] == doc["name"] and doc["name"] != "DocType": + singles_table = DocType("Singles") + + xhiveframework.qb.update(singles_table).set(singles_table.value, original_modified).where( + singles_table["field"] == "modified", # singles_table.field is a method of pypika Selectable + ).where(singles_table.doctype == doc["name"]).run() + else: + doctype_table = DocType(doc["doctype"]) + + xhiveframework.qb.update(doctype_table).set(doctype_table.modified, original_modified).where( + doctype_table.name == doc["name"] + ).run() + + +def import_doc( + docdict, + data_import=False, + pre_process=None, + ignore_version=None, + reset_permissions=False, + path=None, +): + xhiveframework.flags.in_import = True + docdict["__islocal"] = 1 + + controller = get_controller(docdict["doctype"]) + if controller and hasattr(controller, "prepare_for_import") and callable(controller.prepare_for_import): + controller.prepare_for_import(docdict) + + doc = xhiveframework.get_doc(docdict) + + reset_tree_properties(doc) + load_code_properties(doc, path) + + doc.run_method("before_import") + + doc.flags.ignore_version = ignore_version + if pre_process: + pre_process(doc) + + if xhiveframework.db.exists(doc.doctype, doc.name): + delete_old_doc(doc, reset_permissions) + + doc.flags.ignore_links = True + if not data_import: + doc.flags.ignore_validate = True + doc.flags.ignore_permissions = True + doc.flags.ignore_mandatory = True + + doc.insert() + + xhiveframework.flags.in_import = False + + return doc + + +def load_code_properties(doc, path): + """Load code files stored in separate files with extensions""" + if path: + if hasattr(doc, "get_code_fields"): + dirname, filename = os.path.split(path) + for key, extn in doc.get_code_fields().items(): + codefile = os.path.join(dirname, filename.split(".", 1)[0] + "." + extn) + if os.path.exists(codefile): + with open(codefile) as txtfile: + doc.set(key, txtfile.read()) + + +def delete_old_doc(doc, reset_permissions): + ignore = [] + old_doc = xhiveframework.get_doc(doc.doctype, doc.name) + + if doc.doctype in ignore_values: + # update ignore values + for key in ignore_values.get(doc.doctype) or []: + doc.set(key, old_doc.get(key)) + + # update ignored docs into new doc + for df in doc.meta.get_table_fields(): + if df.options in ignore_doctypes and not reset_permissions: + doc.set(df.fieldname, []) + ignore.append(df.options) + + # delete old + xhiveframework.delete_doc(doc.doctype, doc.name, force=1, ignore_doctypes=ignore, for_reload=True) + + doc.flags.ignore_children_type = ignore + + +def reset_tree_properties(doc): + # Note on Tree DocTypes: + # The tree structure is maintained in the database via the fields "lft" and + # "rgt". They are automatically set and kept up-to-date. Importing them + # would destroy any existing tree structure. + if getattr(doc.meta, "is_tree", None) and any([doc.lft, doc.rgt]): + print(f'Ignoring values of `lft` and `rgt` for {doc.doctype} "{doc.name}"') + doc.lft = None + doc.rgt = None diff --git a/xhiveframework/modules/patch_handler.py b/xhiveframework/modules/patch_handler.py new file mode 100644 index 0000000..2e104e8 --- /dev/null +++ b/xhiveframework/modules/patch_handler.py @@ -0,0 +1,234 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +""" Patch Handler. + +This file manages execution of manaully written patches. Patches are script +that apply changes in database schema or data to accomodate for changes in the +code. + +Ways to specify patches: + +1. patches.txt file specifies patches that run before doctype schema +migration. Each line represents one patch (old format). +2. patches.txt can alternatively also separate pre and post model sync +patches by using INI like file format: + ```patches.txt + [pre_model_sync] + app.module.patch1 + app.module.patch2 + + + [post_model_sync] + app.module.patch3 + ``` + + When different sections are specified patches are executed in this order: + 1. Run pre_model_sync patches + 2. Reload/resync all doctype schema + 3. Run post_model_sync patches + + Hence any patch that just needs to modify data but doesn't depend on + old schema should be added to post_model_sync section of file. + +3. simple python commands can be added by starting line with `execute:` +`execute:` example: `execute:print("hello world")` +""" + +import configparser +import time +from enum import Enum +from textwrap import dedent, indent + +import xhiveframework + + +class PatchError(Exception): + pass + + +class PatchType(Enum): + pre_model_sync = "pre_model_sync" + post_model_sync = "post_model_sync" + + +def run_all(skip_failing: bool = False, patch_type: PatchType | None = None) -> None: + """run all pending patches""" + executed = set(xhiveframework.get_all("Patch Log", filters={"skipped": 0}, fields="patch", pluck="patch")) + + xhiveframework.flags.final_patches = [] + + def run_patch(patch): + try: + if not run_single(patchmodule=patch): + print(patch + ": failed: STOPPED") + raise PatchError(patch) + except Exception: + if not skip_failing: + raise + + print("Failed to execute patch") + update_patch_log(patch, skipped=True) + + patches = get_all_patches(patch_type=patch_type) + + for patch in patches: + if patch and (patch not in executed): + run_patch(patch) + + # patches to be run in the end + for patch in xhiveframework.flags.final_patches: + patch = patch.replace("finally:", "") + run_patch(patch) + + +def get_all_patches(patch_type: PatchType | None = None) -> list[str]: + if patch_type and not isinstance(patch_type, PatchType): + xhiveframework.throw(f"Unsupported patch type specified: {patch_type}") + + patches = [] + for app in xhiveframework.get_installed_apps(): + patches.extend(get_patches_from_app(app, patch_type=patch_type)) + + return patches + + +def get_patches_from_app(app: str, patch_type: PatchType | None = None) -> list[str]: + """Get patches from an app's patches.txt + + patches.txt can be: + 1. ini like file with section for different patch_type + 2. plain text file with each line representing a patch. + """ + patches_file = xhiveframework.get_app_path(app, "patches.txt") + + try: + return parse_as_configfile(patches_file, patch_type) + except configparser.MissingSectionHeaderError: + # treat as old format with each line representing a single patch + # backward compatbility with old patches.txt format + if not patch_type or patch_type == PatchType.pre_model_sync: + return xhiveframework.get_file_items(patches_file) + + return [] + + +def parse_as_configfile(patches_file: str, patch_type: PatchType | None = None) -> list[str]: + # Attempt to parse as ini file with pre/post patches + # allow_no_value: patches are not key value pairs + # delimiters = '\n' to avoid treating default `:` and `=` in execute as k:v delimiter + parser = configparser.ConfigParser(allow_no_value=True, delimiters="\n") + # preserve case + parser.optionxform = str + parser.read(patches_file) + + # empty file + if not parser.sections(): + return [] + + if not patch_type: + return [patch for patch in parser[PatchType.pre_model_sync.value]] + [ + patch for patch in parser[PatchType.post_model_sync.value] + ] + + if patch_type.value in parser.sections(): + return [patch for patch in parser[patch_type.value]] + else: + xhiveframework.throw(xhiveframework._("Patch type {} not found in patches.txt").format(patch_type)) + + +def reload_doc(args): + import xhiveframework.modules + + run_single(method=xhiveframework.modules.reload_doc, methodargs=args) + + +def run_single(patchmodule=None, method=None, methodargs=None, force=False): + from xhiveframework import conf + + # don't write txt files + conf.developer_mode = 0 + + if force or method or not executed(patchmodule): + return execute_patch(patchmodule, method, methodargs) + else: + return True + + +def execute_patch(patchmodule: str, method=None, methodargs=None): + """execute the patch""" + _patch_mode(True) + + if patchmodule.startswith("execute:"): + has_patch_file = False + patch = patchmodule.split("execute:")[1] + docstring = "" + else: + has_patch_file = True + patch = f"{patchmodule.split(maxsplit=1)[0]}.execute" + _patch = xhiveframework.get_attr(patch) + docstring = _patch.__doc__ or "" + + if docstring: + docstring = "\n" + indent(dedent(docstring), "\t") + + print( + f"Executing {patchmodule or methodargs} in {xhiveframework.local.site} ({xhiveframework.db.cur_db_name}){docstring}" + ) + + start_time = time.monotonic() + xhiveframework.db.begin() + xhiveframework.db.auto_commit_on_many_writes = 0 + try: + if patchmodule: + if patchmodule.startswith("finally:"): + # run run patch at the end + xhiveframework.flags.final_patches.append(patchmodule) + else: + if has_patch_file: + _patch() + else: + exec(patch, globals()) + update_patch_log(patchmodule) + + elif method: + method(**methodargs) + + except Exception: + xhiveframework.db.rollback() + raise + + else: + xhiveframework.db.commit() + end_time = time.monotonic() + _patch_mode(False) + print(f"Success: Done in {round(end_time - start_time, 3)}s") + + return True + + +def update_patch_log(patchmodule, skipped=False): + """update patch_file in patch log""" + + patch = xhiveframework.get_doc({"doctype": "Patch Log", "patch": patchmodule}) + + if skipped: + traceback = xhiveframework.get_traceback(with_context=True) + patch.skipped = 1 + patch.traceback = traceback + print(traceback, end="\n\n") + + patch.insert(ignore_permissions=True) + + +def executed(patchmodule): + """return True if is executed""" + if patchmodule.startswith("finally:"): + # patches are saved without the finally: tag + patchmodule = patchmodule.replace("finally:", "") + return xhiveframework.db.get_value("Patch Log", {"patch": patchmodule, "skipped": 0}) + + +def _patch_mode(enable): + """stop/start execution till patch is run""" + xhiveframework.local.flags.in_patch = enable + xhiveframework.db.commit() diff --git a/xhiveframework/modules/utils.py b/xhiveframework/modules/utils.py new file mode 100644 index 0000000..0145909 --- /dev/null +++ b/xhiveframework/modules/utils.py @@ -0,0 +1,335 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +""" + Utilities for using modules +""" +import json +import os +from textwrap import dedent, indent +from typing import TYPE_CHECKING, Union + +import xhiveframework +from xhiveframework import _, get_module_path, scrub +from xhiveframework.utils import cint, cstr, now_datetime + +if TYPE_CHECKING: + from types import ModuleType + + from xhiveframework.model.document import Document + + +doctype_python_modules = {} + + +def export_module_json(doc: "Document", is_standard: bool, module: str) -> str | None: + """Make a folder for the given doc and add its json file (make it a standard + object that will be synced) + + Returns the absolute file_path without the extension. + Eg: For exporting a Print Format "_Test Print Format 1", the return value will be + `/home/gavin/xhiveframework-bench/apps/xhiveframework/xhiveframework/core/print_format/_test_print_format_1/_test_print_format_1` + """ + if not xhiveframework.flags.in_import and is_standard and xhiveframework.conf.developer_mode: + from xhiveframework.modules.export_file import export_to_files + + # json + export_to_files(record_list=[[doc.doctype, doc.name]], record_module=module, create_init=is_standard) + + return os.path.join( + xhiveframework.get_module_path(module), scrub(doc.doctype), scrub(doc.name), scrub(doc.name) + ) + + +def get_doc_module(module: str, doctype: str, name: str) -> "ModuleType": + """Get custom module for given document""" + module_name = "{app}.{module}.{doctype}.{name}.{name}".format( + app=xhiveframework.local.module_app[scrub(module)], + doctype=scrub(doctype), + module=scrub(module), + name=scrub(name), + ) + return xhiveframework.get_module(module_name) + + +@xhiveframework.whitelist() +def export_customizations( + module: str, doctype: str, sync_on_migrate: bool = False, with_permissions: bool = False +): + """Export Custom Field and Property Setter for the current document to the app folder. + This will be synced with bench migrate""" + + sync_on_migrate = cint(sync_on_migrate) + with_permissions = cint(with_permissions) + + if not xhiveframework.conf.developer_mode: + xhiveframework.throw(_("Only allowed to export customizations in developer mode")) + + custom = { + "custom_fields": xhiveframework.get_all("Custom Field", fields="*", filters={"dt": doctype}), + "property_setters": xhiveframework.get_all("Property Setter", fields="*", filters={"doc_type": doctype}), + "custom_perms": [], + "links": xhiveframework.get_all("DocType Link", fields="*", filters={"parent": doctype}), + "doctype": doctype, + "sync_on_migrate": sync_on_migrate, + } + + if with_permissions: + custom["custom_perms"] = xhiveframework.get_all("Custom DocPerm", fields="*", filters={"parent": doctype}) + + # also update the custom fields and property setters for all child tables + for d in xhiveframework.get_meta(doctype).get_table_fields(): + export_customizations(module, d.options, sync_on_migrate, with_permissions) + + if custom["custom_fields"] or custom["property_setters"] or custom["custom_perms"]: + folder_path = os.path.join(get_module_path(module), "custom") + if not os.path.exists(folder_path): + os.makedirs(folder_path) + + path = os.path.join(folder_path, scrub(doctype) + ".json") + with open(path, "w") as f: + f.write(xhiveframework.as_json(custom)) + + xhiveframework.msgprint(_("Customizations for {0} exported to:
      {1}").format(doctype, path)) + return path + + +def sync_customizations(app=None): + """Sync custom fields and property setters from custom folder in each app module""" + + if app: + apps = [app] + else: + apps = xhiveframework.get_installed_apps() + + for app_name in apps: + for module_name in xhiveframework.local.app_modules.get(app_name) or []: + folder = xhiveframework.get_app_path(app_name, module_name, "custom") + if os.path.exists(folder): + for fname in os.listdir(folder): + if fname.endswith(".json"): + with open(os.path.join(folder, fname)) as f: + data = json.loads(f.read()) + if data.get("sync_on_migrate"): + sync_customizations_for_doctype(data, folder, fname) + + +def sync_customizations_for_doctype(data: dict, folder: str, filename: str = ""): + """Sync doctype customzations for a particular data set""" + from xhiveframework.core.doctype.doctype.doctype import validate_fields_for_doctype + + doctype = data["doctype"] + update_schema = False + + def sync(key, custom_doctype, doctype_fieldname): + doctypes = list(set(map(lambda row: row.get(doctype_fieldname), data[key]))) + + # sync single doctype exculding the child doctype + def sync_single_doctype(doc_type): + def _insert(data): + if data.get(doctype_fieldname) == doc_type: + data["doctype"] = custom_doctype + doc = xhiveframework.get_doc(data) + doc.db_insert() + + if custom_doctype != "Custom Field": + xhiveframework.db.delete(custom_doctype, {doctype_fieldname: doc_type}) + + for d in data[key]: + _insert(d) + + else: + for d in data[key]: + field = xhiveframework.db.get_value("Custom Field", {"dt": doc_type, "fieldname": d["fieldname"]}) + if not field: + d["owner"] = "Administrator" + _insert(d) + else: + custom_field = xhiveframework.get_doc("Custom Field", field) + custom_field.flags.ignore_validate = True + custom_field.update(d) + custom_field.db_update() + + for doc_type in doctypes: + # only sync the parent doctype and child doctype if there isn't any other child table json file + if doc_type == doctype or not os.path.exists(os.path.join(folder, scrub(doc_type) + ".json")): + sync_single_doctype(doc_type) + + if not xhiveframework.db.exists("DocType", doctype): + print(_("DocType {0} does not exist.").format(doctype)) + print(_("Skipping fixture syncing for doctype {0} from file {1}").format(doctype, filename)) + return + + if data["custom_fields"]: + sync("custom_fields", "Custom Field", "dt") + update_schema = True + + if data["property_setters"]: + sync("property_setters", "Property Setter", "doc_type") + + print(f"Updating customizations for {doctype}") + if data.get("custom_perms"): + sync("custom_perms", "Custom DocPerm", "parent") + + validate_fields_for_doctype(doctype) + + if update_schema and not xhiveframework.db.get_value("DocType", doctype, "issingle"): + xhiveframework.db.updatedb(doctype) + + +def scrub_dt_dn(dt: str, dn: str) -> tuple[str, str]: + """Returns in lowercase and code friendly names of doctype and name for certain types""" + return scrub(dt), scrub(dn) + + +def get_doc_path(module: str, doctype: str, name: str) -> str: + """Returns path of a doc in a module""" + return os.path.join(get_module_path(module), *scrub_dt_dn(doctype, name)) + + +def reload_doc( + module: str, + dt: str | None = None, + dn: str | None = None, + force: bool = False, + reset_permissions: bool = False, +): + """Reload Document from model (`[module]//[name]/[name].json`) files""" + from xhiveframework.modules.import_file import import_files + + return import_files(module, dt, dn, force=force, reset_permissions=reset_permissions) + + +def export_doc(doctype, name, module=None): + """Write a doc to standard path.""" + from xhiveframework.modules.export_file import write_document_file + + print(f"Exporting Document {doctype} {name}") + module = module or xhiveframework.db.get_value("DocType", name, "module") + write_document_file(xhiveframework.get_doc(doctype, name), module) + + +def get_doctype_module(doctype: str) -> str: + """Returns **Module Def** name of given doctype.""" + doctype_module_map = xhiveframework.cache.get_value( + "doctype_modules", + generator=lambda: dict(xhiveframework.qb.from_("DocType").select("name", "module").run()), + ) + + if module_name := doctype_module_map.get(doctype): + return module_name + else: + xhiveframework.throw(_("DocType {} not found").format(doctype), exc=xhiveframework.DoesNotExistError) + + +def load_doctype_module(doctype, module=None, prefix="", suffix=""): + """Returns the module object for given doctype. + + Note: This will return the standard defined module object for the doctype irrespective + of the `override_doctype_class` hook. + """ + module = module or get_doctype_module(doctype) + app = get_module_app(module) + key = (app, doctype, prefix, suffix) + module_name = get_module_name(doctype, module, prefix, suffix) + + if key not in doctype_python_modules: + try: + doctype_python_modules[key] = xhiveframework.get_module(module_name) + except ImportError as e: + msg = f"Module import failed for {doctype}, the DocType you're trying to open might be deleted." + msg += f"
      Error: {e}" + raise ImportError(msg) from e + + return doctype_python_modules[key] + + +def get_module_name(doctype: str, module: str, prefix: str = "", suffix: str = "", app: str | None = None): + app = scrub(app or get_module_app(module)) + module = scrub(module) + doctype = scrub(doctype) + return f"{app}.{module}.doctype.{doctype}.{prefix}{doctype}{suffix}" + + +def get_module_app(module: str) -> str: + app = xhiveframework.local.module_app.get(scrub(module)) + if app is None: + xhiveframework.throw(_("Module {} not found").format(module), exc=xhiveframework.DoesNotExistError) + return app + + +def get_app_publisher(module: str) -> str: + app = get_module_app(module) + if not app: + xhiveframework.throw(_("App not found for module: {0}").format(module)) + return xhiveframework.get_hooks(hook="app_publisher", app_name=app)[0] + + +def make_boilerplate( + template: str, doc: Union["Document", "xhiveframework._dict"], opts: Union[dict, "xhiveframework._dict"] = None +): + target_path = get_doc_path(doc.module, doc.doctype, doc.name) + template_name = template.replace("controller", scrub(doc.name)) + if template_name.endswith("._py"): + template_name = template_name[:-4] + ".py" + target_file_path = os.path.join(target_path, template_name) + template_file_path = os.path.join( + get_module_path("core"), "doctype", scrub(doc.doctype), "boilerplate", template + ) + + if os.path.exists(target_file_path): + print(f"{target_file_path} already exists, skipping...") + return + + doc = doc or xhiveframework._dict() + opts = opts or xhiveframework._dict() + app_publisher = get_app_publisher(doc.module) + base_class = "Document" + base_class_import = "from xhiveframework.model.document import Document" + controller_body = "pass" + + if doc.get("is_tree"): + base_class = "NestedSet" + base_class_import = "from xhiveframework.utils.nestedset import NestedSet" + + if doc.get("is_virtual"): + controller_body = indent( + dedent( + """ + def db_insert(self, *args, **kwargs): + pass + + def load_from_db(self): + pass + + def db_update(self): + pass + + @staticmethod + def get_list(args): + pass + + @staticmethod + def get_count(args): + pass + + @staticmethod + def get_stats(args): + pass + """ + ), + "\t", + ) + + with open(target_file_path, "w") as target, open(template_file_path) as source: + template = source.read() + controller_file_content = cstr(template).format( + app_publisher=app_publisher, + year=now_datetime().year, + classname=doc.name.replace(" ", "").replace("-", ""), + base_class_import=base_class_import, + base_class=base_class, + doctype=doc.name, + **opts, + custom_controller=controller_body, + ) + target.write(xhiveframework.as_unicode(controller_file_content)) diff --git a/xhiveframework/monitor.py b/xhiveframework/monitor.py new file mode 100644 index 0000000..9d73f12 --- /dev/null +++ b/xhiveframework/monitor.py @@ -0,0 +1,135 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json +import os +import traceback +import uuid +from datetime import datetime + +import rq + +import xhiveframework + +MONITOR_REDIS_KEY = "monitor-transactions" +MONITOR_MAX_ENTRIES = 1000000 + + +def start(transaction_type="request", method=None, kwargs=None): + if xhiveframework.conf.monitor: + xhiveframework.local.monitor = Monitor(transaction_type, method, kwargs) + + +def stop(response=None): + if hasattr(xhiveframework.local, "monitor"): + xhiveframework.local.monitor.dump(response) + + +def add_data_to_monitor(**kwargs) -> None: + """Add additional custom key-value pairs along with monitor log. + Note: Key-value pairs should be simple JSON exportable types.""" + if hasattr(xhiveframework.local, "monitor"): + xhiveframework.local.monitor.add_custom_data(**kwargs) + + +def get_trace_id() -> str | None: + """Get unique ID for current transaction.""" + if monitor := getattr(xhiveframework.local, "monitor", None): + return monitor.data.uuid + + +def log_file(): + return os.path.join(xhiveframework.utils.get_bench_path(), "logs", "monitor.json.log") + + +class Monitor: + __slots__ = ("data",) + + def __init__(self, transaction_type, method, kwargs): + try: + self.data = xhiveframework._dict( + { + "site": xhiveframework.local.site, + "timestamp": datetime.utcnow(), + "transaction_type": transaction_type, + "uuid": str(uuid.uuid4()), + } + ) + + if transaction_type == "request": + self.collect_request_meta() + else: + self.collect_job_meta(method, kwargs) + except Exception: + traceback.print_exc() + + def collect_request_meta(self): + self.data.request = xhiveframework._dict( + { + "ip": xhiveframework.local.request_ip, + "method": xhiveframework.request.method, + "path": xhiveframework.request.path, + } + ) + + if request_id := xhiveframework.request.headers.get("X-XhiveFramework-Request-Id"): + self.data.uuid = request_id + + def collect_job_meta(self, method, kwargs): + self.data.job = xhiveframework._dict({"method": method, "scheduled": False, "wait": 0}) + if "run_scheduled_job" in method: + self.data.job.method = kwargs["job_type"] + self.data.job.scheduled = True + + if job := rq.get_current_job(): + self.data.uuid = job.id + waitdiff = self.data.timestamp - job.enqueued_at + self.data.job.wait = int(waitdiff.total_seconds() * 1000000) + + def add_custom_data(self, **kwargs): + if self.data: + self.data.update(kwargs) + + def dump(self, response=None): + try: + timediff = datetime.utcnow() - self.data.timestamp + # Obtain duration in microseconds + self.data.duration = int(timediff.total_seconds() * 1000000) + + if self.data.transaction_type == "request": + if response: + self.data.request.status_code = response.status_code + self.data.request.response_length = int(response.headers.get("Content-Length", 0)) + else: + self.data.request.status_code = 500 + + if hasattr(xhiveframework.local, "rate_limiter"): + limiter = xhiveframework.local.rate_limiter + self.data.request.counter = limiter.counter + if limiter.rejected: + self.data.request.reset = limiter.reset + + self.store() + except Exception: + traceback.print_exc() + + def store(self): + if xhiveframework.cache.llen(MONITOR_REDIS_KEY) > MONITOR_MAX_ENTRIES: + xhiveframework.cache.ltrim(MONITOR_REDIS_KEY, 1, -1) + serialized = json.dumps(self.data, sort_keys=True, default=str, separators=(",", ":")) + xhiveframework.cache.rpush(MONITOR_REDIS_KEY, serialized) + + +def flush(): + try: + # Fetch all the logs without removing from cache + logs = xhiveframework.cache.lrange(MONITOR_REDIS_KEY, 0, -1) + if logs: + logs = list(map(xhiveframework.safe_decode, logs)) + with open(log_file(), "a", os.O_NONBLOCK) as f: + f.write("\n".join(logs)) + f.write("\n") + # Remove fetched entries from cache + xhiveframework.cache.ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) + except Exception: + traceback.print_exc() diff --git a/xhiveframework/oauth.py b/xhiveframework/oauth.py new file mode 100644 index 0000000..a3bad53 --- /dev/null +++ b/xhiveframework/oauth.py @@ -0,0 +1,592 @@ +import base64 +import datetime +import hashlib +import re +from http import cookies +from urllib.parse import unquote, urljoin, urlparse + +import jwt +import pytz +from oauthlib.openid import RequestValidator + +import xhiveframework +from xhiveframework.auth import LoginManager +from xhiveframework.utils.data import get_system_timezone, now_datetime + + +class OAuthWebRequestValidator(RequestValidator): + # Pre- and post-authorization. + def validate_client_id(self, client_id, request, *args, **kwargs): + # Simple validity check, does client exist? Not banned? + cli_id = xhiveframework.db.get_value("OAuth Client", {"name": client_id}) + if cli_id: + request.client = xhiveframework.get_doc("OAuth Client", client_id).as_dict() + return True + else: + return False + + def validate_redirect_uri(self, client_id, redirect_uri, request, *args, **kwargs): + # Is the client allowed to use the supplied redirect_uri? i.e. has + # the client previously registered this EXACT redirect uri. + + redirect_uris = xhiveframework.db.get_value("OAuth Client", client_id, "redirect_uris").split( + get_url_delimiter() + ) + + if redirect_uri in redirect_uris: + return True + else: + return False + + def get_default_redirect_uri(self, client_id, request, *args, **kwargs): + # The redirect used if none has been supplied. + # Prefer your clients to pre register a redirect uri rather than + # supplying one on each authorization request. + return xhiveframework.db.get_value("OAuth Client", client_id, "default_redirect_uri") + + def validate_scopes(self, client_id, scopes, client, request, *args, **kwargs): + # Is the client allowed to access the requested scopes? + allowed_scopes = get_client_scopes(client_id) + return all(scope in allowed_scopes for scope in scopes) + + def get_default_scopes(self, client_id, request, *args, **kwargs): + # Scopes a client will authorize for if none are supplied in the + # authorization request. + scopes = get_client_scopes(client_id) + request.scopes = scopes # Apparently this is possible. + return scopes + + def validate_response_type(self, client_id, response_type, client, request, *args, **kwargs): + allowed_response_types = [ + # From OAuth Client response_type field + client.response_type.lower(), + # OIDC + "id_token", + "id_token token", + "code id_token", + "code token id_token", + ] + + return response_type in allowed_response_types + + # Post-authorization + + def save_authorization_code(self, client_id, code, request, *args, **kwargs): + cookie_dict = get_cookie_dict_from_headers(request) + + oac = xhiveframework.new_doc("OAuth Authorization Code") + oac.scopes = get_url_delimiter().join(request.scopes) + oac.redirect_uri_bound_to_authorization_code = request.redirect_uri + oac.client = client_id + oac.user = unquote(cookie_dict["user_id"].value) + oac.authorization_code = code["code"] + + if request.nonce: + oac.nonce = request.nonce + + if request.code_challenge and request.code_challenge_method: + oac.code_challenge = request.code_challenge + oac.code_challenge_method = request.code_challenge_method.lower() + + oac.save(ignore_permissions=True) + xhiveframework.db.commit() + + def authenticate_client(self, request, *args, **kwargs): + # Get ClientID in URL + if request.client_id: + oc = xhiveframework.get_doc("OAuth Client", request.client_id) + else: + # Extract token, instantiate OAuth Bearer Token and use clientid from there. + if "refresh_token" in xhiveframework.form_dict: + oc = xhiveframework.get_doc( + "OAuth Client", + xhiveframework.db.get_value( + "OAuth Bearer Token", + {"refresh_token": xhiveframework.form_dict["refresh_token"]}, + "client", + ), + ) + elif "token" in xhiveframework.form_dict: + oc = xhiveframework.get_doc( + "OAuth Client", + xhiveframework.db.get_value("OAuth Bearer Token", xhiveframework.form_dict["token"], "client"), + ) + else: + oc = xhiveframework.get_doc( + "OAuth Client", + xhiveframework.db.get_value( + "OAuth Bearer Token", + xhiveframework.get_request_header("Authorization").split(" ")[1], + "client", + ), + ) + try: + request.client = request.client or oc.as_dict() + except Exception as e: + return generate_json_error_response(e) + + cookie_dict = get_cookie_dict_from_headers(request) + user_id = unquote(cookie_dict.get("user_id").value) if "user_id" in cookie_dict else "Guest" + return xhiveframework.session.user == user_id + + def authenticate_client_id(self, client_id, request, *args, **kwargs): + cli_id = xhiveframework.db.get_value("OAuth Client", client_id, "name") + if not cli_id: + # Don't allow public (non-authenticated) clients + return False + else: + request["client"] = xhiveframework.get_doc("OAuth Client", cli_id) + return True + + def validate_code(self, client_id, code, client, request, *args, **kwargs): + # Validate the code belongs to the client. Add associated scopes, + # state and user to request.scopes and request.user. + + validcodes = xhiveframework.get_all( + "OAuth Authorization Code", + filters={"client": client_id, "validity": "Valid"}, + ) + + if code in [vcode["name"] for vcode in validcodes]: + request.scopes = xhiveframework.db.get_value("OAuth Authorization Code", code, "scopes").split( + get_url_delimiter() + ) + request.user = xhiveframework.db.get_value("OAuth Authorization Code", code, "user") + code_challenge_method = xhiveframework.db.get_value( + "OAuth Authorization Code", code, "code_challenge_method" + ) + code_challenge = xhiveframework.db.get_value("OAuth Authorization Code", code, "code_challenge") + + if code_challenge and not request.code_verifier: + if xhiveframework.db.exists("OAuth Authorization Code", code): + xhiveframework.delete_doc("OAuth Authorization Code", code, ignore_permissions=True) + xhiveframework.db.commit() + return False + + if code_challenge_method == "s256": + m = hashlib.sha256() + m.update(bytes(request.code_verifier, "utf-8")) + code_verifier = base64.b64encode(m.digest()).decode("utf-8") + code_verifier = re.sub(r"\+", "-", code_verifier) + code_verifier = re.sub(r"\/", "_", code_verifier) + code_verifier = re.sub(r"=", "", code_verifier) + return code_challenge == code_verifier + + elif code_challenge_method == "plain": + return code_challenge == request.code_verifier + + return True + + return False + + def confirm_redirect_uri(self, client_id, code, redirect_uri, client, *args, **kwargs): + saved_redirect_uri = xhiveframework.db.get_value("OAuth Client", client_id, "default_redirect_uri") + + redirect_uris = xhiveframework.db.get_value("OAuth Client", client_id, "redirect_uris") + + if redirect_uris: + redirect_uris = redirect_uris.split(get_url_delimiter()) + return redirect_uri in redirect_uris + + return saved_redirect_uri == redirect_uri + + def validate_grant_type(self, client_id, grant_type, client, request, *args, **kwargs): + # Clients should only be allowed to use one type of grant. + # In this case, it must be "authorization_code" or "refresh_token" + return grant_type in ["authorization_code", "refresh_token", "password"] + + def save_bearer_token(self, token, request, *args, **kwargs): + # Remember to associate it with request.scopes, request.user and + # request.client. The two former will be set when you validate + # the authorization code. Don't forget to save both the + # access_token and the refresh_token and set expiration for the + # access_token to now + expires_in seconds. + + otoken = xhiveframework.new_doc("OAuth Bearer Token") + otoken.client = request.client["name"] + try: + otoken.user = ( + request.user + if request.user + else xhiveframework.db.get_value( + "OAuth Bearer Token", + {"refresh_token": request.body.get("refresh_token")}, + "user", + ) + ) + except Exception: + otoken.user = xhiveframework.session.user + + otoken.scopes = get_url_delimiter().join(request.scopes) + otoken.access_token = token["access_token"] + otoken.refresh_token = token.get("refresh_token") + otoken.expires_in = token["expires_in"] + otoken.save(ignore_permissions=True) + xhiveframework.db.commit() + + return xhiveframework.db.get_value("OAuth Client", request.client["name"], "default_redirect_uri") + + def invalidate_authorization_code(self, client_id, code, request, *args, **kwargs): + # Authorization codes are use once, invalidate it when a Bearer token + # has been acquired. + + xhiveframework.db.set_value("OAuth Authorization Code", code, "validity", "Invalid") + xhiveframework.db.commit() + + # Protected resource request + + def validate_bearer_token(self, token, scopes, request): + # Remember to check expiration and scope membership + otoken = xhiveframework.get_doc("OAuth Bearer Token", token) + is_token_valid = (now_datetime() < otoken.expiration_time) and otoken.status != "Revoked" + client_scopes = xhiveframework.db.get_value("OAuth Client", otoken.client, "scopes").split( + get_url_delimiter() + ) + are_scopes_valid = True + for scp in scopes: + are_scopes_valid = are_scopes_valid and True if scp in client_scopes else False + + return is_token_valid and are_scopes_valid + + # Token refresh request + + def get_original_scopes(self, refresh_token, request, *args, **kwargs): + # Obtain the token associated with the given refresh_token and + # return its scopes, these will be passed on to the refreshed + # access token if the client did not specify a scope during the + # request. + obearer_token = xhiveframework.get_doc("OAuth Bearer Token", {"refresh_token": refresh_token}) + return obearer_token.scopes + + def revoke_token(self, token, token_type_hint, request, *args, **kwargs): + """Revoke an access or refresh token. + + :param token: The token string. + :param token_type_hint: access_token or refresh_token. + :param request: The HTTP Request (oauthlib.common.Request) + + Method is used by: + - Revocation Endpoint + """ + if token_type_hint == "access_token": + xhiveframework.db.set_value("OAuth Bearer Token", token, "status", "Revoked") + elif token_type_hint == "refresh_token": + xhiveframework.db.set_value("OAuth Bearer Token", {"refresh_token": token}, "status", "Revoked") + else: + xhiveframework.db.set_value("OAuth Bearer Token", token, "status", "Revoked") + xhiveframework.db.commit() + + def validate_refresh_token(self, refresh_token, client, request, *args, **kwargs): + """Ensure the Bearer token is valid and authorized access to scopes. + + OBS! The request.user attribute should be set to the resource owner + associated with this refresh token. + + :param refresh_token: Unicode refresh token + :param client: Client object set by you, see authenticate_client. + :param request: The HTTP Request (oauthlib.common.Request) + :rtype: True or False + + Method is used by: + - Authorization Code Grant (indirectly by issuing refresh tokens) + - Resource Owner Password Credentials Grant (also indirectly) + - Refresh Token Grant + """ + + otoken = xhiveframework.get_doc("OAuth Bearer Token", {"refresh_token": refresh_token, "status": "Active"}) + + if not otoken: + return False + else: + return True + + # OpenID Connect + + def finalize_id_token(self, id_token, token, token_handler, request): + # Check whether xhiveframework server URL is set + id_token_header = {"typ": "jwt", "alg": "HS256"} + + user = xhiveframework.get_doc("User", request.user) + + if request.nonce: + id_token["nonce"] = request.nonce + + userinfo = get_userinfo(user) + + id_token["exp"] = id_token.get("iat") + token.get("expires_in") + + if userinfo.get("iss"): + id_token["iss"] = userinfo.get("iss") + + if "openid" in request.scopes: + id_token.update(userinfo) + + id_token_encoded = jwt.encode( + payload=id_token, + key=request.client.client_secret, + algorithm="HS256", + headers=id_token_header, + ) + + return xhiveframework.safe_decode(id_token_encoded) + + def get_authorization_code_nonce(self, client_id, code, redirect_uri, request): + if xhiveframework.get_value("OAuth Authorization Code", code, "validity") == "Valid": + return xhiveframework.get_value("OAuth Authorization Code", code, "nonce") + + return None + + def get_authorization_code_scopes(self, client_id, code, redirect_uri, request): + scope = xhiveframework.get_value("OAuth Client", client_id, "scopes") + if not scope: + scope = [] + else: + scope = scope.split(get_url_delimiter()) + + return scope + + def get_jwt_bearer_token(self, token, token_handler, request): + now = datetime.datetime.now() + + id_token = dict( + aud=token.client_id, + iat=round(now.timestamp()), + at_hash=calculate_at_hash(token.access_token, hashlib.sha256), + ) + return self.finalize_id_token(id_token, token, token_handler, request) + + def get_userinfo_claims(self, request): + user = xhiveframework.get_doc("User", xhiveframework.session.user) + return get_userinfo(user) + + def validate_id_token(self, token, scopes, request): + try: + id_token = xhiveframework.get_doc("OAuth Bearer Token", token) + if id_token.status == "Active": + return True + except Exception: + return False + + return False + + def validate_jwt_bearer_token(self, token, scopes, request): + try: + jwt = xhiveframework.get_doc("OAuth Bearer Token", token) + if jwt.status == "Active": + return True + except Exception: + return False + + return False + + def validate_silent_authorization(self, request): + """Ensure the logged in user has authorized silent OpenID authorization. + + Silent OpenID authorization allows access tokens and id tokens to be + granted to clients without any user prompt or interaction. + + :param request: The HTTP Request (oauthlib.common.Request) + :rtype: True or False + + Method is used by: + - OpenIDConnectAuthCode + - OpenIDConnectImplicit + - OpenIDConnectHybrid + """ + if request.prompt == "login": + return False + else: + return True + + def validate_silent_login(self, request): + """Ensure session user has authorized silent OpenID login. + + If no user is logged in or has not authorized silent login, this + method should return False. + + If the user is logged in but associated with multiple accounts and + not selected which one to link to the token then this method should + raise an oauthlib.oauth2.AccountSelectionRequired error. + + :param request: The HTTP Request (oauthlib.common.Request) + :rtype: True or False + + Method is used by: + - OpenIDConnectAuthCode + - OpenIDConnectImplicit + - OpenIDConnectHybrid + """ + if xhiveframework.session.user == "Guest" or request.prompt.lower() == "login": + return False + else: + return True + + def validate_user_match(self, id_token_hint, scopes, claims, request): + """Ensure client supplied user id hint matches session user. + + If the sub claim or id_token_hint is supplied then the session + user must match the given ID. + + :param id_token_hint: User identifier string. + :param scopes: List of OAuth 2 scopes and OpenID claims (strings). + :param claims: OpenID Connect claims dict. + :param request: The HTTP Request (oauthlib.common.Request) + :rtype: True or False + + Method is used by: + - OpenIDConnectAuthCode + - OpenIDConnectImplicit + - OpenIDConnectHybrid + """ + if id_token_hint: + try: + user = None + payload = jwt.decode( + id_token_hint, + algorithms=["HS256"], + options={ + "verify_signature": False, + "verify_aud": False, + }, + ) + client_id, client_secret = xhiveframework.get_value( + "OAuth Client", + payload.get("aud"), + ["client_id", "client_secret"], + ) + + if payload.get("sub") and client_id and client_secret: + user = xhiveframework.db.get_value( + "User Social Login", + {"userid": payload.get("sub"), "provider": "xhiveframework"}, + "parent", + ) + user = xhiveframework.get_doc("User", user) + verified_payload = jwt.decode( + id_token_hint, + key=client_secret, + audience=client_id, + algorithms=["HS256"], + options={ + "verify_exp": False, + }, + ) + + if verified_payload: + return user.name == xhiveframework.session.user + + except Exception: + return False + + elif xhiveframework.session.user != "Guest": + return True + + return False + + def validate_user(self, username, password, client, request, *args, **kwargs): + """Ensure the username and password is valid. + + Method is used by: + - Resource Owner Password Credentials Grant + """ + login_manager = LoginManager() + login_manager.authenticate(username, password) + + if login_manager.user == "Guest": + return False + + request.user = login_manager.user + return True + + +def get_cookie_dict_from_headers(r): + cookie = cookies.BaseCookie() + if r.headers.get("Cookie"): + cookie.load(r.headers.get("Cookie")) + return cookie + + +def calculate_at_hash(access_token, hash_alg): + """Helper method for calculating an access token + hash, as described in http://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken + Its value is the base64url encoding of the left-most half of the hash of the octets + of the ASCII representation of the access_token value, where the hash algorithm + used is the hash algorithm used in the alg Header Parameter of the ID Token's JOSE + Header. For instance, if the alg is RS256, hash the access_token value with SHA-256, + then take the left-most 128 bits and base64url encode them. The at_hash value is a + case sensitive string. + Args: + access_token (str): An access token string. + hash_alg (callable): A callable returning a hash object, e.g. hashlib.sha256 + """ + hash_digest = hash_alg(access_token.encode("utf-8")).digest() + cut_at = int(len(hash_digest) / 2) + truncated = hash_digest[:cut_at] + from jwt.utils import base64url_encode + + at_hash = base64url_encode(truncated) + return at_hash.decode("utf-8") + + +def delete_oauth2_data(): + xhiveframework.db.delete("OAuth Authorization Code", {"validity": "Invalid"}) + xhiveframework.db.delete("OAuth Bearer Token", {"status": "Revoked"}) + + +def get_client_scopes(client_id): + scopes_string = xhiveframework.db.get_value("OAuth Client", client_id, "scopes") + return scopes_string.split() + + +def get_userinfo(user): + picture = None + xhiveframework_server_url = get_server_url() + valid_url_schemes = ("http", "https", "ftp", "ftps") + + if user.user_image: + if xhiveframework.utils.validate_url(user.user_image, valid_schemes=valid_url_schemes): + picture = user.user_image + else: + picture = urljoin(xhiveframework_server_url, user.user_image) + + return xhiveframework._dict( + { + "sub": xhiveframework.db.get_value( + "User Social Login", + {"parent": user.name, "provider": "xhiveframework"}, + "userid", + ), + "name": " ".join(filter(None, [user.first_name, user.last_name])), + "given_name": user.first_name, + "family_name": user.last_name, + "email": user.email, + "picture": picture, + "roles": xhiveframework.get_roles(user.name), + "iss": xhiveframework_server_url, + } + ) + + +def get_url_delimiter(separator_character=" "): + return separator_character + + +def generate_json_error_response(e): + if not e: + e = xhiveframework._dict({}) + + xhiveframework.local.response = xhiveframework._dict( + { + "description": getattr(e, "description", "Internal Server Error"), + "status_code": getattr(e, "status_code", 500), + "error": getattr(e, "error", "internal_server_error"), + } + ) + xhiveframework.local.response["http_status_code"] = getattr(e, "status_code", 500) + return + + +def get_server_url(): + request_url = urlparse(xhiveframework.request.url) + request_url = f"{request_url.scheme}://{request_url.netloc}" + return xhiveframework.get_value("Social Login Key", "xhiveframework", "base_url") or request_url diff --git a/xhiveframework/parallel_test_runner.py b/xhiveframework/parallel_test_runner.py new file mode 100644 index 0000000..69fd770 --- /dev/null +++ b/xhiveframework/parallel_test_runner.py @@ -0,0 +1,313 @@ +import faulthandler +import json +import os +import re +import signal +import sys +import time +import unittest + +import click +import requests + +import xhiveframework + +from .test_runner import SLOW_TEST_THRESHOLD, make_test_records + +click_ctx = click.get_current_context(True) +if click_ctx: + click_ctx.color = True + + +class ParallelTestRunner: + def __init__(self, app, site, build_number=1, total_builds=1, dry_run=False): + self.app = app + self.site = site + self.build_number = xhiveframework.utils.cint(build_number) or 1 + self.total_builds = xhiveframework.utils.cint(total_builds) + self.dry_run = dry_run + self.setup_test_site() + self.run_tests() + + def setup_test_site(self): + xhiveframework.init(site=self.site) + if not xhiveframework.db: + xhiveframework.connect() + + if self.dry_run: + return + + xhiveframework.flags.in_test = True + xhiveframework.clear_cache() + xhiveframework.utils.scheduler.disable_scheduler() + self.before_test_setup() + + def before_test_setup(self): + start_time = time.monotonic() + for fn in xhiveframework.get_hooks("before_tests", app_name=self.app): + xhiveframework.get_attr(fn)() + + test_module = xhiveframework.get_module(f"{self.app}.tests") + + if hasattr(test_module, "global_test_dependencies"): + for doctype in test_module.global_test_dependencies: + make_test_records(doctype, commit=True) + + elapsed = time.monotonic() - start_time + elapsed = click.style(f" ({elapsed:.03}s)", fg="red") + click.echo(f"Before Test {elapsed}") + + def run_tests(self): + self.test_result = ParallelTestResult(stream=sys.stderr, descriptions=True, verbosity=2) + + for test_file_info in self.get_test_file_list(): + self.run_tests_for_file(test_file_info) + + self.print_result() + + def run_tests_for_file(self, file_info): + if not file_info: + return + + if self.dry_run: + print("running tests from", "/".join(file_info)) + return + + xhiveframework.set_user("Administrator") + path, filename = file_info + module = self.get_module(path, filename) + self.create_test_dependency_records(module, path, filename) + test_suite = unittest.TestSuite() + module_test_cases = unittest.TestLoader().loadTestsFromModule(module) + test_suite.addTest(module_test_cases) + test_suite(self.test_result) + + def create_test_dependency_records(self, module, path, filename): + if hasattr(module, "test_dependencies"): + for doctype in module.test_dependencies: + make_test_records(doctype, commit=True) + + if os.path.basename(os.path.dirname(path)) == "doctype": + # test_data_migration_connector.py > data_migration_connector.json + test_record_filename = re.sub("^test_", "", filename).replace(".py", ".json") + test_record_file_path = os.path.join(path, test_record_filename) + if os.path.exists(test_record_file_path): + with open(test_record_file_path) as f: + doc = json.loads(f.read()) + doctype = doc["name"] + make_test_records(doctype, commit=True) + + def get_module(self, path, filename): + app_path = xhiveframework.get_app_path(self.app) + relative_path = os.path.relpath(path, app_path) + if relative_path == ".": + module_name = self.app + else: + relative_path = relative_path.replace("/", ".") + module_name = os.path.splitext(filename)[0] + module_name = f"{self.app}.{relative_path}.{module_name}" + + return xhiveframework.get_module(module_name) + + def print_result(self): + # XXX: Added to debug tests getting stuck AFTER completion + # the process should terminate before this, we don't need to reset the signal. + signal.alarm(60) + faulthandler.register(signal.SIGALRM) + + self.test_result.printErrors() + click.echo(self.test_result) + if self.test_result.failures or self.test_result.errors: + if os.environ.get("CI"): + sys.exit(1) + + def get_test_file_list(self): + # Load balance based on total # of tests ~ each runner should get roughly same # of tests. + test_list = get_all_tests(self.app) + + test_counts = [self.get_test_count(test) for test in test_list] + test_chunks = split_by_weight(test_list, test_counts, chunk_count=self.total_builds) + + return test_chunks[self.build_number - 1] + + @staticmethod + def get_test_count(test): + """Get approximate count of tests inside a file""" + file_name = "/".join(test) + + with open(file_name) as f: + test_count = f.read().count("def test_") + + return test_count + + +def split_by_weight(work, weights, chunk_count): + """Roughly split work by respective weight while keep ordering.""" + expected_weight = sum(weights) // chunk_count + + chunks = [[] for _ in range(chunk_count)] + + chunk_no = 0 + chunk_weight = 0 + + for task, weight in zip(work, weights, strict=False): + if chunk_weight > expected_weight: + chunk_weight = 0 + chunk_no += 1 + assert chunk_no < chunk_count + + chunks[chunk_no].append(task) + chunk_weight += weight + + assert len(work) == sum(len(chunk) for chunk in chunks) + assert len(chunks) == chunk_count + + return chunks + + +class ParallelTestResult(unittest.TextTestResult): + def startTest(self, test): + self.tb_locals = True + self._started_at = time.monotonic() + super(unittest.TextTestResult, self).startTest(test) + test_class = unittest.util.strclass(test.__class__) + if not hasattr(self, "current_test_class") or self.current_test_class != test_class: + click.echo(f"\n{unittest.util.strclass(test.__class__)}") + self.current_test_class = test_class + + def getTestMethodName(self, test): + return test._testMethodName if hasattr(test, "_testMethodName") else str(test) + + def addSuccess(self, test): + super(unittest.TextTestResult, self).addSuccess(test) + elapsed = time.monotonic() - self._started_at + threshold_passed = elapsed >= SLOW_TEST_THRESHOLD + elapsed = click.style(f" ({elapsed:.03}s)", fg="red") if threshold_passed else "" + click.echo(f" {click.style(' ✔ ', fg='green')} {self.getTestMethodName(test)}{elapsed}") + + def addError(self, test, err): + super(unittest.TextTestResult, self).addError(test, err) + click.echo(f" {click.style(' ✖ ', fg='red')} {self.getTestMethodName(test)}") + + def addFailure(self, test, err): + super(unittest.TextTestResult, self).addFailure(test, err) + click.echo(f" {click.style(' ✖ ', fg='red')} {self.getTestMethodName(test)}") + + def addSkip(self, test, reason): + super(unittest.TextTestResult, self).addSkip(test, reason) + click.echo(f" {click.style(' = ', fg='white')} {self.getTestMethodName(test)}") + + def addExpectedFailure(self, test, err): + super(unittest.TextTestResult, self).addExpectedFailure(test, err) + click.echo(f" {click.style(' ✖ ', fg='red')} {self.getTestMethodName(test)}") + + def addUnexpectedSuccess(self, test): + super(unittest.TextTestResult, self).addUnexpectedSuccess(test) + click.echo(f" {click.style(' ✔ ', fg='green')} {self.getTestMethodName(test)}") + + def printErrors(self): + click.echo("\n") + self.printErrorList(" ERROR ", self.errors, "red") + self.printErrorList(" FAIL ", self.failures, "red") + + def printErrorList(self, flavour, errors, color): + for test, err in errors: + click.echo(self.separator1) + click.echo(f"{click.style(flavour, bg=color)} {self.getDescription(test)}") + click.echo(self.separator2) + click.echo(err) + + def __str__(self): + return f"Tests: {self.testsRun}, Failing: {len(self.failures)}, Errors: {len(self.errors)}" + + +def get_all_tests(app): + test_file_list = [] + for path, folders, files in os.walk(xhiveframework.get_app_path(app)): + for dontwalk in ("locals", ".git", "public", "__pycache__"): + if dontwalk in folders: + folders.remove(dontwalk) + + # for predictability + folders.sort() + files.sort() + + if os.path.sep.join(["doctype", "doctype", "boilerplate"]) in path: + # in /doctype/doctype/boilerplate/ + continue + + test_file_list.extend( + [path, filename] + for filename in files + if filename.startswith("test_") and filename.endswith(".py") and filename != "test_runner.py" + ) + return test_file_list + + +class ParallelTestWithOrchestrator(ParallelTestRunner): + """ + This can be used to balance-out test time across multiple instances + This is dependent on external orchestrator which returns next test to run + + orchestrator endpoints + - register-instance (, , test_spec_list) + - get-next-test-spec (, ) + - test-completed (, ) + """ + + def __init__(self, app, site): + self.orchestrator_url = os.environ.get("ORCHESTRATOR_URL") + if not self.orchestrator_url: + click.echo("ORCHESTRATOR_URL environment variable not found!") + click.echo("Pass public URL after hosting https://lab.membtech.com/xhiveframework/test-orchestrator") + sys.exit(1) + + self.ci_build_id = os.environ.get("CI_BUILD_ID") + self.ci_instance_id = os.environ.get("CI_INSTANCE_ID") or xhiveframework.generate_hash(length=10) + if not self.ci_build_id: + click.echo("CI_BUILD_ID environment variable not found!") + sys.exit(1) + + ParallelTestRunner.__init__(self, app, site) + + def run_tests(self): + self.test_status = "ongoing" + self.register_instance() + super().run_tests() + + def get_test_file_list(self): + while self.test_status == "ongoing": + yield self.get_next_test() + + def register_instance(self): + test_spec_list = get_all_tests(self.app) + response_data = self.call_orchestrator("register-instance", data={"test_spec_list": test_spec_list}) + self.is_master = response_data.get("is_master") + + def get_next_test(self): + response_data = self.call_orchestrator("get-next-test-spec") + self.test_status = response_data.get("status") + return response_data.get("next_test") + + def print_result(self): + self.call_orchestrator("test-completed") + return super().print_result() + + def call_orchestrator(self, endpoint, data=None): + if data is None: + data = {} + # add repo token header + # build id in header + headers = { + "CI-BUILD-ID": self.ci_build_id, + "CI-INSTANCE-ID": self.ci_instance_id, + "REPO-TOKEN": "2948288382838DE", + } + url = f"{self.orchestrator_url}/{endpoint}" + res = requests.get(url, json=data, headers=headers) + res.raise_for_status() + response_data = {} + if "application/json" in res.headers.get("content-type"): + response_data = res.json() + + return response_data diff --git a/xhiveframework/patches.txt b/xhiveframework/patches.txt new file mode 100644 index 0000000..f5b07b6 --- /dev/null +++ b/xhiveframework/patches.txt @@ -0,0 +1,236 @@ +[pre_model_sync] +xhiveframework.patches.v15_0.remove_implicit_primary_key +xhiveframework.patches.v12_0.remove_deprecated_fields_from_doctype #3 +execute:xhiveframework.utils.global_search.setup_global_search_table() +execute:xhiveframework.reload_doc('core', 'doctype', 'doctype_action', force=True) #2019-09-23 +execute:xhiveframework.reload_doc('core', 'doctype', 'doctype_link', force=True) #2020-10-17 +execute:xhiveframework.reload_doc('core', 'doctype', 'doctype_state', force=True) #2021-12-15 +execute:xhiveframework.reload_doc('core', 'doctype', 'doctype', force=True) #2017-09-22 +execute:xhiveframework.reload_doc('core', 'doctype', 'docfield', force=True) #2018-02-20 +xhiveframework.patches.v11_0.drop_column_apply_user_permissions +execute:xhiveframework.reload_doc('core', 'doctype', 'custom_docperm') +execute:xhiveframework.reload_doc('core', 'doctype', 'docperm') #2018-05-29 +execute:xhiveframework.reload_doc('core', 'doctype', 'comment') +execute:xhiveframework.reload_doc('core', 'doctype', 'document_naming_rule', force=True) +execute:xhiveframework.reload_doc('core', 'doctype', 'module_def') #2020-08-28 +execute:xhiveframework.reload_doc('core', 'doctype', 'version') #2017-04-01 +execute:xhiveframework.reload_doc('email', 'doctype', 'document_follow') +execute:xhiveframework.reload_doc('core', 'doctype', 'communication_link') #2019-10-02 +execute:xhiveframework.reload_doc('core', 'doctype', 'has_role') +execute:xhiveframework.reload_doc('core', 'doctype', 'communication') #2019-10-02 +execute:xhiveframework.reload_doc('core', 'doctype', 'server_script') +xhiveframework.patches.v11_0.replicate_old_user_permissions +xhiveframework.patches.v11_0.reload_and_rename_view_log #2019-01-03 +xhiveframework.patches.v11_0.copy_fetch_data_from_options +xhiveframework.patches.v11_0.change_email_signature_fieldtype +execute:xhiveframework.reload_doc('core', 'doctype', 'activity_log') +execute:xhiveframework.reload_doc('core', 'doctype', 'deleted_document') +execute:xhiveframework.reload_doc('core', 'doctype', 'domain_settings') +xhiveframework.patches.v13_0.rename_custom_client_script +execute:xhiveframework.reload_doc('core', 'doctype', 'role') #2017-05-23 +execute:xhiveframework.reload_doc('core', 'doctype', 'user') #2017-10-27 +execute:xhiveframework.reload_doc('core', 'doctype', 'report_column') +execute:xhiveframework.reload_doc('core', 'doctype', 'report_filter') +execute:xhiveframework.reload_doc('core', 'doctype', 'report') #2020-08-25 +execute:xhiveframework.get_doc("User", "Guest").save() +execute:xhiveframework.delete_doc("DocType", "Control Panel", force=1) +execute:xhiveframework.delete_doc("DocType", "Tag") +execute:xhiveframework.db.sql("delete from `tabProperty Setter` where `property` in ('idx', '_idx')") +execute:xhiveframework.db.sql("update tabUser set new_password='' where ifnull(new_password, '')!=''") +execute:xhiveframework.permissions.reset_perms("DocType") +execute:xhiveframework.db.sql("delete from `tabProperty Setter` where `property` = 'idx'") +execute:xhiveframework.db.sql("delete from tabSessions where user is null") +execute:xhiveframework.delete_doc("DocType", "Backup Manager") +execute:xhiveframework.permissions.reset_perms("Web Page") +execute:xhiveframework.db.sql("delete from `tabWeb Page` where ifnull(template_path, '')!=''") +execute:xhiveframework.core.doctype.language.language.update_language_names() # 2017-04-12 +execute:xhiveframework.db.set_value("Print Settings", "Print Settings", "add_draft_heading", 1) +execute:xhiveframework.db.set_default('language', '') +execute:xhiveframework.db.sql("update tabCommunication set communication_date = creation where time(communication_date) = 0") +execute:xhiveframework.rename_doc('Country', 'Macedonia, Republic of', 'Macedonia', ignore_if_exists=True) +execute:xhiveframework.rename_doc('Country', 'Iran, Islamic Republic of', 'Iran', ignore_if_exists=True) +execute:xhiveframework.rename_doc('Country', 'Tanzania, United Republic of', 'Tanzania', ignore_if_exists=True) +execute:xhiveframework.rename_doc('Country', 'Syrian Arab Republic', 'Syria', ignore_if_exists=True) +execute:xhiveframework.reload_doc('desk', 'doctype', 'notification_log') +execute:xhiveframework.db.sql('update tabReport set module="Desk" where name="ToDo"') +execute:xhiveframework.delete_doc('Page', 'data-import-tool', ignore_missing=True) +xhiveframework.patches.v10_0.reload_countries_and_currencies # 2021-02-03 +xhiveframework.patches.v10_0.refactor_social_login_keys +xhiveframework.patches.v10_0.enable_chat_by_default_within_system_settings +xhiveframework.patches.v10_0.remove_custom_field_for_disabled_domain +execute:xhiveframework.delete_doc("Page", "chat") +xhiveframework.patches.v11_0.rename_standard_reply_to_email_template +execute:xhiveframework.delete_doc_if_exists('Page', 'user-permissions') +xhiveframework.patches.v10_0.set_no_copy_to_workflow_state +xhiveframework.patches.v10_0.increase_single_table_column_length +xhiveframework.patches.v11_0.create_contact_for_user +xhiveframework.patches.v11_0.update_list_user_settings +xhiveframework.patches.v11_0.rename_workflow_action_to_workflow_action_master #13-06-2018 +xhiveframework.patches.v11_0.rename_email_alert_to_notification #13-06-2018 +xhiveframework.patches.v11_0.delete_duplicate_user_permissions +xhiveframework.patches.v11_0.set_dropbox_file_backup +xhiveframework.patches.v10_0.set_default_locking_time +xhiveframework.patches.v10_0.modify_smallest_currency_fraction +xhiveframework.patches.v10_0.modify_naming_series_table +xhiveframework.patches.v10_0.enhance_security +xhiveframework.patches.v11_0.multiple_references_in_events +xhiveframework.patches.v11_0.set_allow_self_approval_in_workflow +xhiveframework.patches.v11_0.remove_skip_for_doctype +xhiveframework.patches.v11_0.migrate_report_settings_for_new_listview +xhiveframework.patches.v11_0.delete_all_prepared_reports +xhiveframework.patches.v11_0.fix_order_by_in_reports_json +execute:xhiveframework.delete_doc('Page', 'applications', ignore_missing=True) +xhiveframework.patches.v11_0.set_missing_creation_and_modified_value_for_user_permissions +xhiveframework.patches.v11_0.set_default_letter_head_source +xhiveframework.patches.v12_0.set_primary_key_in_series +execute:xhiveframework.delete_doc("Page", "modules", ignore_missing=True) +xhiveframework.patches.v12_0.setup_comments_from_communications +xhiveframework.patches.v12_0.replace_null_values_in_tables +xhiveframework.patches.v12_0.reset_home_settings +xhiveframework.patches.v12_0.update_print_format_type +xhiveframework.patches.v11_0.remove_doctype_user_permissions_for_page_and_report #2019-05-01 +xhiveframework.patches.v11_0.apply_customization_to_custom_doctype +xhiveframework.patches.v12_0.remove_feedback_rating +xhiveframework.patches.v12_0.move_form_attachments_to_attachments_folder +xhiveframework.patches.v12_0.move_timeline_links_to_dynamic_links +xhiveframework.patches.v12_0.delete_feedback_request_if_exists #1 +xhiveframework.patches.v12_0.rename_events_repeat_on +xhiveframework.patches.v12_0.fix_public_private_files +xhiveframework.patches.v12_0.move_email_and_phone_to_child_table +xhiveframework.patches.v12_0.delete_duplicate_indexes # 2022-12-15 +xhiveframework.patches.v12_0.set_default_incoming_email_port +xhiveframework.patches.v12_0.update_global_search +xhiveframework.patches.v12_0.setup_tags +xhiveframework.patches.v12_0.update_auto_repeat_status_and_not_submittable +xhiveframework.patches.v12_0.create_notification_settings_for_user +xhiveframework.patches.v11_0.make_all_prepared_report_attachments_private #2019-11-26 +xhiveframework.patches.v12_0.setup_email_linking +xhiveframework.patches.v12_0.change_existing_dashboard_chart_filters +xhiveframework.patches.v12_0.set_correct_assign_value_in_docs #2020-07-13 +execute:xhiveframework.delete_doc('DocType', 'Test Runner') # 2022-05-19 +execute:xhiveframework.delete_doc_if_exists('DocType', 'Google Maps Settings') +execute:xhiveframework.db.set_default('desktop:home_page', 'workspace') +execute:xhiveframework.delete_doc_if_exists('DocType', 'GSuite Settings') +execute:xhiveframework.delete_doc_if_exists('DocType', 'GSuite Templates') +execute:xhiveframework.delete_doc_if_exists('DocType', 'GCalendar Account') +execute:xhiveframework.delete_doc_if_exists('DocType', 'GCalendar Settings') +xhiveframework.patches.v12_0.remove_example_email_thread_notify +execute:from xhiveframework.desk.page.setup_wizard.install_fixtures import update_genders;update_genders() +xhiveframework.patches.v12_0.set_correct_url_in_files +execute:xhiveframework.reload_doc('core', 'doctype', 'doctype') #2022-06-21 +execute:xhiveframework.reload_doc('custom', 'doctype', 'property_setter') +xhiveframework.patches.v13_0.remove_invalid_options_for_data_fields +xhiveframework.patches.v13_0.website_theme_custom_scss +xhiveframework.patches.v13_0.make_user_type +xhiveframework.patches.v13_0.set_existing_dashboard_charts_as_public +xhiveframework.patches.v13_0.set_path_for_homepage_in_web_page_view +xhiveframework.patches.v13_0.migrate_translation_column_data +xhiveframework.patches.v13_0.set_read_times +xhiveframework.patches.v13_0.remove_web_view +xhiveframework.patches.v13_0.site_wise_logging +xhiveframework.patches.v13_0.set_unique_for_page_view +xhiveframework.patches.v13_0.remove_tailwind_from_page_builder +xhiveframework.patches.v13_0.rename_onboarding +xhiveframework.patches.v13_0.email_unsubscribe +execute:xhiveframework.delete_doc("Web Template", "Section with Left Image", force=1) +execute:xhiveframework.delete_doc("DocType", "Onboarding Slide") +execute:xhiveframework.delete_doc("DocType", "Onboarding Slide Field") +execute:xhiveframework.delete_doc("DocType", "Onboarding Slide Help Link") +xhiveframework.patches.v13_0.update_date_filters_in_user_settings +xhiveframework.patches.v13_0.update_duration_options +xhiveframework.patches.v13_0.replace_old_data_import # 2020-06-24 +xhiveframework.patches.v13_0.create_custom_dashboards_cards_and_charts +xhiveframework.patches.v13_0.rename_is_custom_field_in_dashboard_chart +xhiveframework.patches.v13_0.add_standard_navbar_items # 2020-12-15 +xhiveframework.patches.v13_0.generate_theme_files_in_public_folder +xhiveframework.patches.v13_0.increase_password_length +xhiveframework.patches.v12_0.fix_email_id_formatting +xhiveframework.patches.v13_0.add_toggle_width_in_navbar_settings +xhiveframework.patches.v13_0.rename_notification_fields +xhiveframework.patches.v13_0.remove_duplicate_navbar_items +xhiveframework.patches.v13_0.set_social_icons +xhiveframework.patches.v12_0.set_default_password_reset_limit +xhiveframework.patches.v13_0.set_route_for_blog_category +xhiveframework.patches.v13_0.enable_custom_script +xhiveframework.patches.v13_0.update_newsletter_content_type +execute:xhiveframework.db.set_value('Website Settings', 'Website Settings', {'navbar_template': 'Standard Navbar', 'footer_template': 'Standard Footer'}) +xhiveframework.patches.v13_0.web_template_set_module #2020-10-05 +xhiveframework.patches.v13_0.remove_custom_link +execute:xhiveframework.delete_doc("DocType", "Footer Item") +execute:xhiveframework.reload_doctype('user') +execute:xhiveframework.reload_doctype('docperm') +xhiveframework.patches.v13_0.replace_field_target_with_open_in_new_tab +xhiveframework.patches.v13_0.add_switch_theme_to_navbar_settings +xhiveframework.patches.v13_0.update_icons_in_customized_desk_pages +execute:xhiveframework.db.set_default('desktop:home_page', 'space') +execute:xhiveframework.delete_doc_if_exists('Page', 'workspace') +execute:xhiveframework.delete_doc_if_exists('Page', 'dashboard', force=1) +xhiveframework.core.doctype.page.patches.drop_unused_pages +xhiveframework.patches.v13_0.remove_chat +xhiveframework.patches.v13_0.rename_desk_page_to_workspace # 02.02.2021 +xhiveframework.patches.v13_0.delete_package_publish_tool +xhiveframework.patches.v13_0.rename_list_view_setting_to_list_view_settings +xhiveframework.patches.v13_0.remove_twilio_settings +xhiveframework.patches.v12_0.rename_uploaded_files_with_proper_name +xhiveframework.patches.v13_0.queryreport_columns +xhiveframework.patches.v13_0.jinja_hook +xhiveframework.patches.v13_0.update_notification_channel_if_empty +xhiveframework.patches.v13_0.set_first_day_of_the_week +xhiveframework.patches.v13_0.encrypt_2fa_secrets +xhiveframework.patches.v13_0.reset_corrupt_defaults +xhiveframework.patches.v13_0.remove_share_for_std_users +execute:xhiveframework.reload_doc('custom', 'doctype', 'custom_field') +xhiveframework.email.doctype.email_queue.patches.drop_search_index_on_message_id +xhiveframework.patches.v14_0.save_ratings_in_fraction #23-12-2021 +xhiveframework.patches.v14_0.transform_todo_schema +xhiveframework.patches.v14_0.remove_post_and_post_comment +xhiveframework.patches.v14_0.reset_creation_datetime +xhiveframework.patches.v14_0.remove_is_first_startup +xhiveframework.patches.v14_0.clear_long_pending_stale_logs +xhiveframework.patches.v14_0.log_settings_migration +xhiveframework.patches.v14_0.setup_likes_from_feedback +xhiveframework.patches.v14_0.update_webforms +xhiveframework.patches.v14_0.delete_payment_gateways +xhiveframework.patches.v15_0.remove_event_streaming +xhiveframework.patches.v15_0.copy_disable_prepared_report_to_prepared_report +execute:xhiveframework.reload_doc("desk", "doctype", "Form Tour") +execute:xhiveframework.delete_doc('Page', 'recorder', ignore_missing=True, force=True) +xhiveframework.patches.v14_0.modify_value_column_size_for_singles + +[post_model_sync] +execute:xhiveframework.get_doc('Role', 'Guest').save() # remove desk access +xhiveframework.core.doctype.role.patches.v13_set_default_desk_properties +xhiveframework.patches.v14_0.update_workspace2 # 06.06.2023 +xhiveframework.patches.v14_0.drop_data_import_legacy +xhiveframework.patches.v14_0.copy_mail_data #08.03.21 +xhiveframework.patches.v14_0.update_github_endpoints #08-11-2021 +xhiveframework.patches.v14_0.remove_db_aggregation +xhiveframework.patches.v14_0.update_color_names_in_kanban_board_column +xhiveframework.patches.v14_0.update_is_system_generated_flag +xhiveframework.patches.v14_0.update_auto_account_deletion_duration +xhiveframework.patches.v14_0.update_integration_request +xhiveframework.patches.v14_0.set_document_expiry_default +xhiveframework.patches.v14_0.delete_data_migration_tool +xhiveframework.patches.v14_0.set_suspend_email_queue_default +xhiveframework.patches.v14_0.different_encryption_key +xhiveframework.patches.v14_0.update_multistep_webforms +execute:xhiveframework.delete_doc('Page', 'background_jobs', ignore_missing=True, force=True) +xhiveframework.patches.v14_0.drop_unused_indexes +xhiveframework.patches.v15_0.drop_modified_index +xhiveframework.patches.v14_0.update_attachment_comment +xhiveframework.patches.v15_0.set_contact_full_name +execute:xhiveframework.delete_doc("Page", "activity", force=1) +xhiveframework.patches.v14_0.disable_email_accounts_with_oauth +execute:xhiveframework.delete_doc("Page", "translation-tool", force=1) +xhiveframework.patches.v15_0.remove_prepared_report_settings_from_system_settings +xhiveframework.patches.v14_0.remove_manage_subscriptions_from_navbar +xhiveframework.patches.v15_0.remove_background_jobs_from_dropdown +xhiveframework.desk.doctype.form_tour.patches.introduce_ui_tours +execute:xhiveframework.delete_doc_if_exists("Workspace", "Customization") +execute:xhiveframework.db.set_single_value("Document Naming Settings", "default_amend_naming", "Amend Counter") +xhiveframework.patches.v15_0.move_event_cancelled_to_status +xhiveframework.patches.v15_0.set_file_type +xhiveframework.core.doctype.data_import.patches.remove_stale_docfields_from_legacy_version +xhiveframework.patches.v15_0.validate_newsletter_recipients +xhiveframework.patches.v15_0.sanitize_workspace_titles +xhiveframework.custom.doctype.property_setter.patches.remove_invalid_fetch_from_expressions diff --git a/xhiveframework/patches/__init__.py b/xhiveframework/patches/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v10_0/__init__.py b/xhiveframework/patches/v10_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v10_0/enable_chat_by_default_within_system_settings.py b/xhiveframework/patches/v10_0/enable_chat_by_default_within_system_settings.py new file mode 100644 index 0000000..2fe25b9 --- /dev/null +++ b/xhiveframework/patches/v10_0/enable_chat_by_default_within_system_settings.py @@ -0,0 +1,13 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("System Settings") + doc = xhiveframework.get_single("System Settings") + doc.enable_chat = 1 + + # Changes prescribed by Nabin Hait (nabin@xhiveframework.io) + doc.flags.ignore_mandatory = True + doc.flags.ignore_permissions = True + + doc.save() diff --git a/xhiveframework/patches/v10_0/enhance_security.py b/xhiveframework/patches/v10_0/enhance_security.py new file mode 100644 index 0000000..028f12e --- /dev/null +++ b/xhiveframework/patches/v10_0/enhance_security.py @@ -0,0 +1,32 @@ +import xhiveframework +from xhiveframework.utils import cint + + +def execute(): + """ + The motive of this patch is to increase the overall security in xhiveframework framework + + Existing passwords won't be affected, however, newly created accounts + will have to adheare to the new password policy guidelines. Once can always + loosen up the security by modifying the values in System Settings, however, + we strongly advice against doing so! + + Security is something we take very seriously at xhiveframework, + and hence we chose to make security tighter by default. + """ + doc = xhiveframework.get_single("System Settings") + + # Enforce a Password Policy + if cint(doc.enable_password_policy) == 0: + doc.enable_password_policy = 1 + + # Enforce a password score as calculated by zxcvbn + if cint(doc.minimum_password_score) <= 2: + doc.minimum_password_score = 2 + + # Disallow more than 3 consecutive login attempts in a span of 60 seconds + if cint(doc.allow_consecutive_login_attempts) <= 3: + doc.allow_consecutive_login_attempts = 3 + + doc.flags.ignore_mandatory = True + doc.save() diff --git a/xhiveframework/patches/v10_0/increase_single_table_column_length.py b/xhiveframework/patches/v10_0/increase_single_table_column_length.py new file mode 100644 index 0000000..43adbe9 --- /dev/null +++ b/xhiveframework/patches/v10_0/increase_single_table_column_length.py @@ -0,0 +1,9 @@ +""" +Run this after updating country_info.json and or +""" +import xhiveframework + + +def execute(): + for col in ("field", "doctype"): + xhiveframework.db.sql_ddl(f"alter table `tabSingles` modify column `{col}` varchar(255)") diff --git a/xhiveframework/patches/v10_0/modify_naming_series_table.py b/xhiveframework/patches/v10_0/modify_naming_series_table.py new file mode 100644 index 0000000..14840ee --- /dev/null +++ b/xhiveframework/patches/v10_0/modify_naming_series_table.py @@ -0,0 +1,10 @@ +""" + Modify the Integer 10 Digits Value to BigInt 20 Digit value + to generate long Naming Series + +""" +import xhiveframework + + +def execute(): + xhiveframework.db.sql(""" ALTER TABLE `tabSeries` MODIFY current BIGINT """) diff --git a/xhiveframework/patches/v10_0/modify_smallest_currency_fraction.py b/xhiveframework/patches/v10_0/modify_smallest_currency_fraction.py new file mode 100644 index 0000000..21c6255 --- /dev/null +++ b/xhiveframework/patches/v10_0/modify_smallest_currency_fraction.py @@ -0,0 +1,8 @@ +# Copyright (c) 2018, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.db.set_value("Currency", "USD", "smallest_currency_fraction_value", "0.01") diff --git a/xhiveframework/patches/v10_0/refactor_social_login_keys.py b/xhiveframework/patches/v10_0/refactor_social_login_keys.py new file mode 100644 index 0000000..dd14ccb --- /dev/null +++ b/xhiveframework/patches/v10_0/refactor_social_login_keys.py @@ -0,0 +1,153 @@ +import xhiveframework +from xhiveframework.utils import cstr + + +def execute(): + # Update Social Logins in User + run_patch() + + # Create Social Login Key(s) from Social Login Keys + xhiveframework.reload_doc("integrations", "doctype", "social_login_key", force=True) + + if not xhiveframework.db.exists("DocType", "Social Login Keys"): + return + + social_login_keys = xhiveframework.get_doc("Social Login Keys", "Social Login Keys") + if social_login_keys.get("facebook_client_id") or social_login_keys.get("facebook_client_secret"): + facebook_login_key = xhiveframework.new_doc("Social Login Key") + facebook_login_key.get_social_login_provider("Facebook", initialize=True) + facebook_login_key.social_login_provider = "Facebook" + facebook_login_key.client_id = social_login_keys.get("facebook_client_id") + facebook_login_key.client_secret = social_login_keys.get("facebook_client_secret") + if not (facebook_login_key.client_secret and facebook_login_key.client_id): + facebook_login_key.enable_social_login = 0 + facebook_login_key.save() + + if social_login_keys.get("xhiveframework_server_url"): + xhiveframework_login_key = xhiveframework.new_doc("Social Login Key") + xhiveframework_login_key.get_social_login_provider("XhiveFramework", initialize=True) + xhiveframework_login_key.social_login_provider = "XhiveFramework" + xhiveframework_login_key.base_url = social_login_keys.get("xhiveframework_server_url") + xhiveframework_login_key.client_id = social_login_keys.get("xhiveframework_client_id") + xhiveframework_login_key.client_secret = social_login_keys.get("xhiveframework_client_secret") + if not (xhiveframework_login_key.client_secret and xhiveframework_login_key.client_id and xhiveframework_login_key.base_url): + xhiveframework_login_key.enable_social_login = 0 + xhiveframework_login_key.save() + + if social_login_keys.get("github_client_id") or social_login_keys.get("github_client_secret"): + github_login_key = xhiveframework.new_doc("Social Login Key") + github_login_key.get_social_login_provider("GitHub", initialize=True) + github_login_key.social_login_provider = "GitHub" + github_login_key.client_id = social_login_keys.get("github_client_id") + github_login_key.client_secret = social_login_keys.get("github_client_secret") + if not (github_login_key.client_secret and github_login_key.client_id): + github_login_key.enable_social_login = 0 + github_login_key.save() + + if social_login_keys.get("google_client_id") or social_login_keys.get("google_client_secret"): + google_login_key = xhiveframework.new_doc("Social Login Key") + google_login_key.get_social_login_provider("Google", initialize=True) + google_login_key.social_login_provider = "Google" + google_login_key.client_id = social_login_keys.get("google_client_id") + google_login_key.client_secret = social_login_keys.get("google_client_secret") + if not (google_login_key.client_secret and google_login_key.client_id): + google_login_key.enable_social_login = 0 + google_login_key.save() + + xhiveframework.delete_doc("DocType", "Social Login Keys") + + +def run_patch(): + xhiveframework.reload_doc("core", "doctype", "user", force=True) + xhiveframework.reload_doc("core", "doctype", "user_social_login", force=True) + + users = xhiveframework.get_all("User", fields=["*"], filters={"name": ("not in", ["Administrator", "Guest"])}) + + for user in users: + idx = 0 + if user.xhiveframework_userid: + insert_user_social_login(user.name, user.modified_by, "xhiveframework", idx, userid=user.xhiveframework_userid) + idx += 1 + + if user.fb_userid or user.fb_username: + insert_user_social_login( + user.name, user.modified_by, "facebook", idx, userid=user.fb_userid, username=user.fb_username + ) + idx += 1 + + if user.github_userid or user.github_username: + insert_user_social_login( + user.name, + user.modified_by, + "github", + idx, + userid=user.github_userid, + username=user.github_username, + ) + idx += 1 + + if user.google_userid: + insert_user_social_login(user.name, user.modified_by, "google", idx, userid=user.google_userid) + idx += 1 + + +def insert_user_social_login(user, modified_by, provider, idx, userid=None, username=None): + source_cols = get_standard_cols() + + creation_time = xhiveframework.utils.get_datetime_str(xhiveframework.utils.get_datetime()) + values = [ + xhiveframework.generate_hash(length=10), + creation_time, + creation_time, + user, + modified_by, + user, + "User", + "social_logins", + cstr(idx), + provider, + ] + + if userid: + source_cols.append("userid") + values.append(userid) + + if username: + source_cols.append("username") + values.append(username) + + query = """INSERT INTO `tabUser Social Login` (`{source_cols}`) + VALUES ({values}) + """.format(source_cols="`, `".join(source_cols), values=", ".join([xhiveframework.db.escape(d) for d in values])) + + xhiveframework.db.sql(query) + + +def get_provider_field_map(): + return xhiveframework._dict( + { + "xhiveframework": ["xhiveframework_userid"], + "facebook": ["fb_userid", "fb_username"], + "github": ["github_userid", "github_username"], + "google": ["google_userid"], + } + ) + + +def get_provider_fields(provider): + return get_provider_field_map().get(provider) + + +def get_standard_cols(): + return [ + "name", + "creation", + "modified", + "owner", + "modified_by", + "parent", + "parenttype", + "parentfield", + "idx", + "provider", + ] diff --git a/xhiveframework/patches/v10_0/reload_countries_and_currencies.py b/xhiveframework/patches/v10_0/reload_countries_and_currencies.py new file mode 100644 index 0000000..3c6034a --- /dev/null +++ b/xhiveframework/patches/v10_0/reload_countries_and_currencies.py @@ -0,0 +1,8 @@ +""" +Run this after updating country_info.json and or +""" +from xhiveframework.utils.install import import_country_and_currency + + +def execute(): + import_country_and_currency() diff --git a/xhiveframework/patches/v10_0/remove_custom_field_for_disabled_domain.py b/xhiveframework/patches/v10_0/remove_custom_field_for_disabled_domain.py new file mode 100644 index 0000000..f3f0caa --- /dev/null +++ b/xhiveframework/patches/v10_0/remove_custom_field_for_disabled_domain.py @@ -0,0 +1,14 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "domain") + xhiveframework.reload_doc("core", "doctype", "has_domain") + active_domains = xhiveframework.get_active_domains() + all_domains = xhiveframework.get_all("Domain") + + for d in all_domains: + if d.name not in active_domains: + inactive_domain = xhiveframework.get_doc("Domain", d.name) + inactive_domain.setup_data() + inactive_domain.remove_custom_field() diff --git a/xhiveframework/patches/v10_0/set_default_locking_time.py b/xhiveframework/patches/v10_0/set_default_locking_time.py new file mode 100644 index 0000000..69ec5d6 --- /dev/null +++ b/xhiveframework/patches/v10_0/set_default_locking_time.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "system_settings") + xhiveframework.db.set_single_value("System Settings", "allow_login_after_fail", 60) diff --git a/xhiveframework/patches/v10_0/set_no_copy_to_workflow_state.py b/xhiveframework/patches/v10_0/set_no_copy_to_workflow_state.py new file mode 100644 index 0000000..9bb9035 --- /dev/null +++ b/xhiveframework/patches/v10_0/set_no_copy_to_workflow_state.py @@ -0,0 +1,13 @@ +import xhiveframework + + +def execute(): + for dt in xhiveframework.get_all("Workflow", fields=["name", "document_type", "workflow_state_field"]): + fieldname = xhiveframework.db.get_value( + "Custom Field", filters={"dt": dt.document_type, "fieldname": dt.workflow_state_field} + ) + + if fieldname: + custom_field = xhiveframework.get_doc("Custom Field", fieldname) + custom_field.no_copy = 1 + custom_field.save() diff --git a/xhiveframework/patches/v11_0/__init__.py b/xhiveframework/patches/v11_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v11_0/apply_customization_to_custom_doctype.py b/xhiveframework/patches/v11_0/apply_customization_to_custom_doctype.py new file mode 100644 index 0000000..6dcf0a4 --- /dev/null +++ b/xhiveframework/patches/v11_0/apply_customization_to_custom_doctype.py @@ -0,0 +1,52 @@ +import xhiveframework +from xhiveframework.utils import cint + +# This patch aims to apply & delete all the customization +# on custom doctypes done through customize form + +# This is required because customize form in now blocked +# for custom doctypes and user may not be able to +# see previous customization + + +def execute(): + custom_doctypes = xhiveframework.get_all("DocType", filters={"custom": 1}) + + for doctype in custom_doctypes: + property_setters = xhiveframework.get_all( + "Property Setter", + filters={"doc_type": doctype.name, "doctype_or_field": "DocField"}, + fields=["name", "property", "value", "property_type", "field_name"], + ) + + custom_fields = xhiveframework.get_all("Custom Field", filters={"dt": doctype.name}, fields=["*"]) + + property_setter_map = {} + + for prop in property_setters: + property_setter_map[prop.field_name] = prop + xhiveframework.db.delete("Property Setter", {"name": prop.name}) + + meta = xhiveframework.get_meta(doctype.name) + + for df in meta.fields: + ps = property_setter_map.get(df.fieldname, None) + if ps: + value = cint(ps.value) if ps.property_type == "Int" else ps.value + df.set(ps.property, value) + + for cf in custom_fields: + cf.pop("parenttype") + cf.pop("parentfield") + cf.pop("parent") + cf.pop("name") + field = meta.get_field(cf.fieldname) + if field: + field.update(cf) + else: + df = xhiveframework.new_doc("DocField", parent_doc=meta, parentfield="fields") + df.update(cf) + meta.fields.append(df) + xhiveframework.db.delete("Custom Field", {"name": cf.name}) + + meta.save() diff --git a/xhiveframework/patches/v11_0/change_email_signature_fieldtype.py b/xhiveframework/patches/v11_0/change_email_signature_fieldtype.py new file mode 100644 index 0000000..8821058 --- /dev/null +++ b/xhiveframework/patches/v11_0/change_email_signature_fieldtype.py @@ -0,0 +1,14 @@ +# Copyright (c) 2018, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + signatures = xhiveframework.db.get_list("User", {"email_signature": ["!=", ""]}, ["name", "email_signature"]) + xhiveframework.reload_doc("core", "doctype", "user") + for d in signatures: + signature = d.get("email_signature") + signature = signature.replace("\n", "
      ") + signature = "
      " + signature + "
      " + xhiveframework.db.set_value("User", d.get("name"), "email_signature", signature) diff --git a/xhiveframework/patches/v11_0/copy_fetch_data_from_options.py b/xhiveframework/patches/v11_0/copy_fetch_data_from_options.py new file mode 100644 index 0000000..00a86e9 --- /dev/null +++ b/xhiveframework/patches/v11_0/copy_fetch_data_from_options.py @@ -0,0 +1,38 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "docfield", force=True) + xhiveframework.reload_doc("custom", "doctype", "custom_field", force=True) + xhiveframework.reload_doc("custom", "doctype", "customize_form_field", force=True) + xhiveframework.reload_doc("custom", "doctype", "property_setter", force=True) + + xhiveframework.db.sql( + """ + update `tabDocField` + set fetch_from = options, options='' + where options like '%.%' and (fetch_from is NULL OR fetch_from='') + and fieldtype in ('Data', 'Read Only', 'Text', 'Small Text', 'Text Editor', 'Code', 'Link', 'Check') + and fieldname!='naming_series' + """ + ) + + xhiveframework.db.sql( + """ + update `tabCustom Field` + set fetch_from = options, options='' + where options like '%.%' and (fetch_from is NULL OR fetch_from='') + and fieldtype in ('Data', 'Read Only', 'Text', 'Small Text', 'Text Editor', 'Code', 'Link', 'Check') + and fieldname!='naming_series' + """ + ) + + xhiveframework.db.sql( + """ + update `tabProperty Setter` + set property="fetch_from", name=concat(doc_type, '-', field_name, '-', property) + where property="options" and value like '%.%' + and property_type in ('Data', 'Read Only', 'Text', 'Small Text', 'Text Editor', 'Code', 'Link', 'Check') + and field_name!='naming_series' + """ + ) diff --git a/xhiveframework/patches/v11_0/create_contact_for_user.py b/xhiveframework/patches/v11_0/create_contact_for_user.py new file mode 100644 index 0000000..6606c06 --- /dev/null +++ b/xhiveframework/patches/v11_0/create_contact_for_user.py @@ -0,0 +1,28 @@ +import re + +import xhiveframework +from xhiveframework.core.doctype.user.user import create_contact + + +def execute(): + """Create Contact for each User if not present""" + xhiveframework.reload_doc("integrations", "doctype", "google_contacts") + xhiveframework.reload_doc("contacts", "doctype", "contact") + xhiveframework.reload_doc("core", "doctype", "dynamic_link") + + contact_meta = xhiveframework.get_meta("Contact") + if contact_meta.has_field("phone_nos") and contact_meta.has_field("email_ids"): + xhiveframework.reload_doc("contacts", "doctype", "contact_phone") + xhiveframework.reload_doc("contacts", "doctype", "contact_email") + + users = xhiveframework.get_all("User", filters={"name": ("not in", "Administrator, Guest")}, fields=["*"]) + for user in users: + if xhiveframework.db.exists("Contact", {"email_id": user.email}) or xhiveframework.db.exists( + "Contact Email", {"email_id": user.email} + ): + continue + if user.first_name: + user.first_name = re.sub("[<>]+", "", xhiveframework.safe_decode(user.first_name)) + if user.last_name: + user.last_name = re.sub("[<>]+", "", xhiveframework.safe_decode(user.last_name)) + create_contact(user, ignore_links=True, ignore_mandatory=True) diff --git a/xhiveframework/patches/v11_0/delete_all_prepared_reports.py b/xhiveframework/patches/v11_0/delete_all_prepared_reports.py new file mode 100644 index 0000000..365706d --- /dev/null +++ b/xhiveframework/patches/v11_0/delete_all_prepared_reports.py @@ -0,0 +1,9 @@ +import xhiveframework + + +def execute(): + if xhiveframework.db.table_exists("Prepared Report"): + xhiveframework.reload_doc("core", "doctype", "prepared_report") + prepared_reports = xhiveframework.get_all("Prepared Report") + for report in prepared_reports: + xhiveframework.delete_doc("Prepared Report", report.name) diff --git a/xhiveframework/patches/v11_0/delete_duplicate_user_permissions.py b/xhiveframework/patches/v11_0/delete_duplicate_user_permissions.py new file mode 100644 index 0000000..f5e9850 --- /dev/null +++ b/xhiveframework/patches/v11_0/delete_duplicate_user_permissions.py @@ -0,0 +1,18 @@ +import xhiveframework + + +def execute(): + duplicateRecords = xhiveframework.db.sql( + """select count(name) as `count`, allow, user, for_value + from `tabUser Permission` + group by allow, user, for_value + having count(*) > 1 """, + as_dict=1, + ) + + for record in duplicateRecords: + xhiveframework.db.sql( + f"""delete from `tabUser Permission` + where allow=%s and user=%s and for_value=%s limit {record.count - 1}""", + (record.allow, record.user, record.for_value), + ) diff --git a/xhiveframework/patches/v11_0/drop_column_apply_user_permissions.py b/xhiveframework/patches/v11_0/drop_column_apply_user_permissions.py new file mode 100644 index 0000000..a367e8b --- /dev/null +++ b/xhiveframework/patches/v11_0/drop_column_apply_user_permissions.py @@ -0,0 +1,14 @@ +import xhiveframework + + +def execute(): + column = "apply_user_permissions" + to_remove = ["DocPerm", "Custom DocPerm"] + + for doctype in to_remove: + if xhiveframework.db.table_exists(doctype): + if column in xhiveframework.db.get_table_columns(doctype): + xhiveframework.db.sql(f"alter table `tab{doctype}` drop column {column}") + + xhiveframework.reload_doc("core", "doctype", "docperm", force=True) + xhiveframework.reload_doc("core", "doctype", "custom_docperm", force=True) diff --git a/xhiveframework/patches/v11_0/fix_order_by_in_reports_json.py b/xhiveframework/patches/v11_0/fix_order_by_in_reports_json.py new file mode 100644 index 0000000..4040d5a --- /dev/null +++ b/xhiveframework/patches/v11_0/fix_order_by_in_reports_json.py @@ -0,0 +1,35 @@ +import json + +import xhiveframework + + +def execute(): + reports_data = xhiveframework.get_all( + "Report", + filters={ + "json": ["not like", '%%%"order_by": "`tab%%%'], + "report_type": "Report Builder", + "is_standard": "No", + }, + fields=["name"], + ) + + for d in reports_data: + doc = xhiveframework.get_doc("Report", d.get("name")) + + if not doc.get("json"): + continue + + json_data = json.loads(doc.get("json")) + + parts = [] + if ("order_by" in json_data) and ("." in json_data.get("order_by")): + parts = json_data.get("order_by").split(".") + + sort_by = parts[1].split(" ") + + json_data["order_by"] = f"`tab{doc.ref_doctype}`.`{sort_by[0]}`" + json_data["order_by"] += f" {sort_by[1]}" if len(sort_by) > 1 else "" + + doc.json = json.dumps(json_data) + doc.save() diff --git a/xhiveframework/patches/v11_0/make_all_prepared_report_attachments_private.py b/xhiveframework/patches/v11_0/make_all_prepared_report_attachments_private.py new file mode 100644 index 0000000..2c3efa3 --- /dev/null +++ b/xhiveframework/patches/v11_0/make_all_prepared_report_attachments_private.py @@ -0,0 +1,28 @@ +import xhiveframework + + +def execute(): + if xhiveframework.db.count("File", filters={"attached_to_doctype": "Prepared Report", "is_private": 0}) > 10000: + xhiveframework.db.auto_commit_on_many_writes = True + + files = xhiveframework.get_all( + "File", + fields=["name", "attached_to_name"], + filters={"attached_to_doctype": "Prepared Report", "is_private": 0}, + ) + for file_dict in files: + # For some reason Prepared Report doc might not exist, check if it exists first + if xhiveframework.db.exists("Prepared Report", file_dict.attached_to_name): + try: + file_doc = xhiveframework.get_doc("File", file_dict.name) + file_doc.is_private = 1 + file_doc.save() + except Exception: + # File might not exist on the file system in that case delete both Prepared Report and File doc + xhiveframework.delete_doc("Prepared Report", file_dict.attached_to_name) + else: + # If Prepared Report doc doesn't exist then the file doc is useless. Delete it. + xhiveframework.delete_doc("File", file_dict.name) + + if xhiveframework.db.auto_commit_on_many_writes: + xhiveframework.db.auto_commit_on_many_writes = False diff --git a/xhiveframework/patches/v11_0/migrate_report_settings_for_new_listview.py b/xhiveframework/patches/v11_0/migrate_report_settings_for_new_listview.py new file mode 100644 index 0000000..26886af --- /dev/null +++ b/xhiveframework/patches/v11_0/migrate_report_settings_for_new_listview.py @@ -0,0 +1,34 @@ +import json + +import xhiveframework + + +def execute(): + """ + Migrate JSON field of Report according to changes in New ListView + Rename key columns to fields + Rename key add_total_row to add_totals_row + Convert sort_by and sort_order to order_by + """ + + reports = xhiveframework.get_all("Report", {"report_type": "Report Builder"}) + + for report_name in reports: + settings = xhiveframework.db.get_value("Report", report_name, "json") + if not settings: + continue + + settings = xhiveframework._dict(json.loads(settings)) + + # columns -> fields + settings.fields = settings.columns or [] + settings.pop("columns", None) + + # sort_by + order_by -> order_by + settings.order_by = (settings.sort_by or "modified") + " " + (settings.order_by or "desc") + + # add_total_row -> add_totals_row + settings.add_totals_row = settings.add_total_row + settings.pop("add_total_row", None) + + xhiveframework.db.set_value("Report", report_name, "json", json.dumps(settings)) diff --git a/xhiveframework/patches/v11_0/multiple_references_in_events.py b/xhiveframework/patches/v11_0/multiple_references_in_events.py new file mode 100644 index 0000000..9ff990e --- /dev/null +++ b/xhiveframework/patches/v11_0/multiple_references_in_events.py @@ -0,0 +1,24 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("Event") + # Rename "Cancel" to "Cancelled" + xhiveframework.db.sql("""UPDATE tabEvent set event_type='Cancelled' where event_type='Cancel'""") + # Move references to Participants table + events = xhiveframework.db.sql( + """SELECT name, ref_type, ref_name FROM tabEvent WHERE ref_type!=''""", as_dict=True + ) + for event in events: + if event.ref_type and event.ref_name: + try: + e = xhiveframework.get_doc("Event", event.name) + e.append( + "event_participants", + {"reference_doctype": event.ref_type, "reference_docname": event.ref_name}, + ) + e.flags.ignore_mandatory = True + e.flags.ignore_permissions = True + e.save() + except Exception: + xhiveframework.log_error(xhiveframework.get_traceback()) diff --git a/xhiveframework/patches/v11_0/reload_and_rename_view_log.py b/xhiveframework/patches/v11_0/reload_and_rename_view_log.py new file mode 100644 index 0000000..cd90661 --- /dev/null +++ b/xhiveframework/patches/v11_0/reload_and_rename_view_log.py @@ -0,0 +1,28 @@ +import xhiveframework + + +def execute(): + if xhiveframework.db.table_exists("View log"): + # for mac users direct renaming would not work since mysql for mac saves table name in lower case + # so while renaming `tabView log` to `tabView Log` we get "Table 'tabView Log' already exists" error + # more info https://stackoverflow.com/a/44753093/5955589 , + # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_lower_case_table_names + + # here we are creating a temp table to store view log data + xhiveframework.db.sql("CREATE TABLE `ViewLogTemp` AS SELECT * FROM `tabView log`") + + # deleting old View log table + xhiveframework.db.sql("DROP table `tabView log`") + xhiveframework.delete_doc("DocType", "View log") + + # reloading view log doctype to create `tabView Log` table + xhiveframework.reload_doc("core", "doctype", "view_log") + + # Move the data to newly created `tabView Log` table + xhiveframework.db.sql("INSERT INTO `tabView Log` SELECT * FROM `ViewLogTemp`") + xhiveframework.db.commit() + + # Delete temporary table + xhiveframework.db.sql("DROP table `ViewLogTemp`") + else: + xhiveframework.reload_doc("core", "doctype", "view_log") diff --git a/xhiveframework/patches/v11_0/remove_doctype_user_permissions_for_page_and_report.py b/xhiveframework/patches/v11_0/remove_doctype_user_permissions_for_page_and_report.py new file mode 100644 index 0000000..206936a --- /dev/null +++ b/xhiveframework/patches/v11_0/remove_doctype_user_permissions_for_page_and_report.py @@ -0,0 +1,8 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.delete_doc_if_exists("DocType", "User Permission for Page and Report") diff --git a/xhiveframework/patches/v11_0/remove_skip_for_doctype.py b/xhiveframework/patches/v11_0/remove_skip_for_doctype.py new file mode 100644 index 0000000..a700a11 --- /dev/null +++ b/xhiveframework/patches/v11_0/remove_skip_for_doctype.py @@ -0,0 +1,89 @@ +import xhiveframework +from xhiveframework.desk.form.linked_with import get_linked_doctypes +from xhiveframework.patches.v11_0.replicate_old_user_permissions import get_doctypes_to_skip +from xhiveframework.query_builder import Field + +# `skip_for_doctype` was a un-normalized way of storing for which +# doctypes the user permission was applicable. +# in this patch, we normalize this into `applicable_for` where +# a new record will be created for each doctype where the user permission +# is applicable +# +# if the user permission is applicable for all doctypes, then only +# one record is created + + +def execute(): + xhiveframework.reload_doctype("User Permission") + + # to check if we need to migrate from skip_for_doctype + has_skip_for_doctype = xhiveframework.db.has_column("User Permission", "skip_for_doctype") + skip_for_doctype_map = {} + + new_user_permissions_list = [] + + user_permissions_to_delete = [] + + for user_permission in xhiveframework.get_all("User Permission", fields=["*"]): + skip_for_doctype = [] + + # while migrating from v11 -> v11 + if has_skip_for_doctype: + if not user_permission.skip_for_doctype: + continue + skip_for_doctype = user_permission.skip_for_doctype.split("\n") + else: # while migrating from v10 -> v11 + if skip_for_doctype_map.get((user_permission.allow, user_permission.user)) is None: + skip_for_doctype = get_doctypes_to_skip(user_permission.allow, user_permission.user) + # cache skip for doctype for same user and doctype + skip_for_doctype_map[(user_permission.allow, user_permission.user)] = skip_for_doctype + else: + skip_for_doctype = skip_for_doctype_map[(user_permission.allow, user_permission.user)] + + if skip_for_doctype: + # only specific doctypes are selected + # split this into multiple records and delete + linked_doctypes = get_linked_doctypes(user_permission.allow, True).keys() + + linked_doctypes = list(linked_doctypes) + + # append the doctype for which we have build the user permission + linked_doctypes += [user_permission.allow] + + applicable_for_doctypes = list(set(linked_doctypes) - set(skip_for_doctype)) + + user_permissions_to_delete.append(user_permission.name) + user_permission.name = None + user_permission.skip_for_doctype = None + new_user_permissions_list.extend( + ( + xhiveframework.generate_hash(length=10), + user_permission.user, + user_permission.allow, + user_permission.for_value, + doctype, + 0, + user_permission.creation, + user_permission.modified, + ) + for doctype in applicable_for_doctypes + if doctype + ) + else: + # No skip_for_doctype found! Just update apply_to_all_doctypes. + xhiveframework.db.set_value("User Permission", user_permission.name, "apply_to_all_doctypes", 1) + + if new_user_permissions_list: + xhiveframework.qb.into("User Permission").columns( + "name", + "user", + "allow", + "for_value", + "applicable_for", + "apply_to_all_doctypes", + "creation", + "modified", + ).insert(*new_user_permissions_list).run() + + if user_permissions_to_delete: + xhiveframework.db.delete("User Permission", filters=(Field("name").isin(tuple(user_permissions_to_delete)))) diff --git a/xhiveframework/patches/v11_0/rename_email_alert_to_notification.py b/xhiveframework/patches/v11_0/rename_email_alert_to_notification.py new file mode 100644 index 0000000..5b2bc96 --- /dev/null +++ b/xhiveframework/patches/v11_0/rename_email_alert_to_notification.py @@ -0,0 +1,14 @@ +import xhiveframework +from xhiveframework.model.rename_doc import rename_doc + + +def execute(): + if xhiveframework.db.table_exists("Email Alert Recipient") and not xhiveframework.db.table_exists( + "Notification Recipient" + ): + rename_doc("DocType", "Email Alert Recipient", "Notification Recipient") + xhiveframework.reload_doc("email", "doctype", "notification_recipient") + + if xhiveframework.db.table_exists("Email Alert") and not xhiveframework.db.table_exists("Notification"): + rename_doc("DocType", "Email Alert", "Notification") + xhiveframework.reload_doc("email", "doctype", "notification") diff --git a/xhiveframework/patches/v11_0/rename_standard_reply_to_email_template.py b/xhiveframework/patches/v11_0/rename_standard_reply_to_email_template.py new file mode 100644 index 0000000..192da00 --- /dev/null +++ b/xhiveframework/patches/v11_0/rename_standard_reply_to_email_template.py @@ -0,0 +1,8 @@ +import xhiveframework +from xhiveframework.model.rename_doc import rename_doc + + +def execute(): + if xhiveframework.db.table_exists("Standard Reply") and not xhiveframework.db.table_exists("Email Template"): + rename_doc("DocType", "Standard Reply", "Email Template") + xhiveframework.reload_doc("email", "doctype", "email_template") diff --git a/xhiveframework/patches/v11_0/rename_workflow_action_to_workflow_action_master.py b/xhiveframework/patches/v11_0/rename_workflow_action_to_workflow_action_master.py new file mode 100644 index 0000000..577224d --- /dev/null +++ b/xhiveframework/patches/v11_0/rename_workflow_action_to_workflow_action_master.py @@ -0,0 +1,8 @@ +import xhiveframework +from xhiveframework.model.rename_doc import rename_doc + + +def execute(): + if xhiveframework.db.table_exists("Workflow Action") and not xhiveframework.db.table_exists("Workflow Action Master"): + rename_doc("DocType", "Workflow Action", "Workflow Action Master") + xhiveframework.reload_doc("workflow", "doctype", "workflow_action_master") diff --git a/xhiveframework/patches/v11_0/replicate_old_user_permissions.py b/xhiveframework/patches/v11_0/replicate_old_user_permissions.py new file mode 100644 index 0000000..500ee8b --- /dev/null +++ b/xhiveframework/patches/v11_0/replicate_old_user_permissions.py @@ -0,0 +1,97 @@ +import json + +import xhiveframework +from xhiveframework.permissions import get_valid_perms +from xhiveframework.utils import cint + + +def execute(): + xhiveframework.reload_doctype("User Permission") + user_permissions = xhiveframework.get_all("User Permission", fields=["allow", "name", "user"]) + + doctype_to_skip_map = {} + + for permission in user_permissions: + if (permission.allow, permission.user) not in doctype_to_skip_map: + doctype_to_skip_map[(permission.allow, permission.user)] = get_doctypes_to_skip( + permission.allow, permission.user + ) + + if not doctype_to_skip_map: + return + for key, doctype_to_skip in doctype_to_skip_map.items(): + if not doctype_to_skip: + continue + if not xhiveframework.db.has_column("User Permission", "applicable_for") and xhiveframework.db.has_column( + "User Permission", "skip_for_doctype" + ): + doctype_to_skip = "\n".join(doctype_to_skip) + xhiveframework.db.sql( + """ + update `tabUser Permission` + set skip_for_doctype = %s + where user=%s and allow=%s + """, + (doctype_to_skip, key[1], key[0]), + ) + + +def get_doctypes_to_skip(doctype, user): + """Returns doctypes to be skipped from user permission check""" + doctypes_to_skip = [] + valid_perms = get_user_valid_perms(user) or [] + for perm in valid_perms: + parent_doctype = perm.parent + try: + linked_doctypes = get_linked_doctypes(parent_doctype) + if doctype not in linked_doctypes: + continue + except xhiveframework.DoesNotExistError: + # if doctype not found (may be due to rename) it should not be considered for skip + continue + + if not cint(perm.apply_user_permissions): + # add doctype to skip list if any of the perm does not apply user permission + doctypes_to_skip.append(parent_doctype) + + elif parent_doctype not in doctypes_to_skip: + user_permission_doctypes = get_user_permission_doctypes(perm) + + # "No doctypes present" indicates that user permission will be applied to each link field + if not user_permission_doctypes: + continue + + elif doctype in user_permission_doctypes: + continue + + else: + doctypes_to_skip.append(parent_doctype) + # remove possible duplicates + return list(set(doctypes_to_skip)) + + +# store user's valid perms to avoid repeated query +user_valid_perm = {} + + +def get_user_valid_perms(user): + if not user_valid_perm.get(user): + user_valid_perm[user] = get_valid_perms(user=user) + return user_valid_perm.get(user) + + +def get_user_permission_doctypes(perm): + try: + return json.loads(perm.user_permission_doctypes or "[]") + except ValueError: + return [] + + +def get_linked_doctypes(doctype): + from xhiveframework.permissions import get_linked_doctypes + + linked_doctypes = get_linked_doctypes(doctype) + child_doctypes = [d.options for d in xhiveframework.get_meta(doctype).get_table_fields()] + for child_dt in child_doctypes: + linked_doctypes += get_linked_doctypes(child_dt) + return linked_doctypes diff --git a/xhiveframework/patches/v11_0/set_allow_self_approval_in_workflow.py b/xhiveframework/patches/v11_0/set_allow_self_approval_in_workflow.py new file mode 100644 index 0000000..b748fa7 --- /dev/null +++ b/xhiveframework/patches/v11_0/set_allow_self_approval_in_workflow.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("workflow", "doctype", "workflow_transition") + xhiveframework.db.sql("update `tabWorkflow Transition` set allow_self_approval=1") diff --git a/xhiveframework/patches/v11_0/set_default_letter_head_source.py b/xhiveframework/patches/v11_0/set_default_letter_head_source.py new file mode 100644 index 0000000..c77ac99 --- /dev/null +++ b/xhiveframework/patches/v11_0/set_default_letter_head_source.py @@ -0,0 +1,8 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("Letter Head") + + # source of all existing letter heads must be HTML + xhiveframework.db.sql("update `tabLetter Head` set source = 'HTML'") diff --git a/xhiveframework/patches/v11_0/set_dropbox_file_backup.py b/xhiveframework/patches/v11_0/set_dropbox_file_backup.py new file mode 100644 index 0000000..6cd4284 --- /dev/null +++ b/xhiveframework/patches/v11_0/set_dropbox_file_backup.py @@ -0,0 +1,9 @@ +import xhiveframework +from xhiveframework.utils import cint + + +def execute(): + xhiveframework.reload_doctype("Dropbox Settings") + check_dropbox_enabled = cint(xhiveframework.db.get_single_value("Dropbox Settings", "enabled")) + if check_dropbox_enabled == 1: + xhiveframework.db.set_single_value("Dropbox Settings", "file_backup", 1) diff --git a/xhiveframework/patches/v11_0/set_missing_creation_and_modified_value_for_user_permissions.py b/xhiveframework/patches/v11_0/set_missing_creation_and_modified_value_for_user_permissions.py new file mode 100644 index 0000000..55a0465 --- /dev/null +++ b/xhiveframework/patches/v11_0/set_missing_creation_and_modified_value_for_user_permissions.py @@ -0,0 +1,9 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.sql( + """UPDATE `tabUser Permission` + SET `modified`=NOW(), `creation`=NOW() + WHERE `creation` IS NULL""" + ) diff --git a/xhiveframework/patches/v11_0/update_list_user_settings.py b/xhiveframework/patches/v11_0/update_list_user_settings.py new file mode 100644 index 0000000..b92d25b --- /dev/null +++ b/xhiveframework/patches/v11_0/update_list_user_settings.py @@ -0,0 +1,33 @@ +import json + +import xhiveframework +from xhiveframework.model.utils.user_settings import sync_user_settings, update_user_settings + + +def execute(): + """Update list_view's order by property from __UserSettings""" + + users = xhiveframework.db.sql("select distinct(user) from `__UserSettings`", as_dict=True) + + for user in users: + # get user_settings for each user + settings = xhiveframework.db.sql( + f"select * from `__UserSettings` \ + where user={xhiveframework.db.escape(user.user)}", + as_dict=True, + ) + + # traverse through each doctype's settings for a user + for d in settings: + data = json.loads(d["data"]) + if data and ("List" in data) and ("order_by" in data["List"]) and data["List"]["order_by"]: + # convert order_by to sort_order & sort_by and delete order_by + order_by = data["List"]["order_by"] + if "`" in order_by and "." in order_by: + order_by = order_by.replace("`", "").split(".")[1] + + data["List"]["sort_by"], data["List"]["sort_order"] = order_by.split(" ") + data["List"].pop("order_by") + update_user_settings(d["doctype"], json.dumps(data), for_update=True) + + sync_user_settings() diff --git a/xhiveframework/patches/v12_0/__init__.py b/xhiveframework/patches/v12_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v12_0/change_existing_dashboard_chart_filters.py b/xhiveframework/patches/v12_0/change_existing_dashboard_chart_filters.py new file mode 100644 index 0000000..6a8af96 --- /dev/null +++ b/xhiveframework/patches/v12_0/change_existing_dashboard_chart_filters.py @@ -0,0 +1,31 @@ +import json + +import xhiveframework + + +def execute(): + if not xhiveframework.db.table_exists("Dashboard Chart"): + return + + charts_to_modify = xhiveframework.get_all( + "Dashboard Chart", + fields=["name", "filters_json", "document_type"], + filters={"chart_type": ["not in", ["Report", "Custom"]]}, + ) + + for chart in charts_to_modify: + old_filters = xhiveframework.parse_json(chart.filters_json) + + if chart.filters_json and isinstance(old_filters, dict): + new_filters = [] + doctype = chart.document_type + + for key in old_filters.keys(): + filter_value = old_filters[key] + if isinstance(filter_value, list): + new_filters.append([doctype, key, filter_value[0], filter_value[1], 0]) + else: + new_filters.append([doctype, key, "=", filter_value, 0]) + + new_filters_json = json.dumps(new_filters) + xhiveframework.db.set_value("Dashboard Chart", chart.name, "filters_json", new_filters_json) diff --git a/xhiveframework/patches/v12_0/create_notification_settings_for_user.py b/xhiveframework/patches/v12_0/create_notification_settings_for_user.py new file mode 100644 index 0000000..dd3edec --- /dev/null +++ b/xhiveframework/patches/v12_0/create_notification_settings_for_user.py @@ -0,0 +1,13 @@ +import xhiveframework +from xhiveframework.desk.doctype.notification_settings.notification_settings import ( + create_notification_settings, +) + + +def execute(): + xhiveframework.reload_doc("desk", "doctype", "notification_settings") + xhiveframework.reload_doc("desk", "doctype", "notification_subscribed_document") + + users = xhiveframework.get_all("User", fields=["name"]) + for user in users: + create_notification_settings(user.name) diff --git a/xhiveframework/patches/v12_0/delete_duplicate_indexes.py b/xhiveframework/patches/v12_0/delete_duplicate_indexes.py new file mode 100644 index 0000000..b259e35 --- /dev/null +++ b/xhiveframework/patches/v12_0/delete_duplicate_indexes.py @@ -0,0 +1,55 @@ +import xhiveframework + +# This patch deletes all the duplicate indexes created for same column +# The patch only checks for indexes with UNIQUE constraints + + +def execute(): + if xhiveframework.db.db_type != "mariadb": + return + + all_tables = xhiveframework.db.get_tables() + final_deletion_map = xhiveframework._dict() + + for table in all_tables: + indexes_to_keep_map = xhiveframework._dict() + indexes_to_delete = [] + index_info = xhiveframework.db.sql( + f"""SHOW INDEX FROM `{table}` + WHERE Seq_in_index = 1 + AND Non_unique=0""", + as_dict=1, + ) + + for index in index_info: + if not indexes_to_keep_map.get(index.Column_name): + indexes_to_keep_map[index.Column_name] = index + else: + indexes_to_delete.append(index.Key_name) + + if indexes_to_delete: + final_deletion_map[table] = indexes_to_delete + + for table_name, index_list in final_deletion_map.items(): + for index in index_list: + try: + if is_clustered_index(table_name, index): + continue + xhiveframework.db.sql_ddl(f"ALTER TABLE `{table_name}` DROP INDEX `{index}`") + except Exception as e: + xhiveframework.log_error("Failed to drop index") + print(f"x Failed to drop index {index} from {table_name}\n {e!s}") + else: + print(f"✓ dropped {index} index from {table}") + + +def is_clustered_index(table, index_name): + return bool( + xhiveframework.db.sql( + f"""SHOW INDEX FROM `{table}` + WHERE Key_name = "{index_name}" + AND Seq_in_index = 2 + """, + as_dict=True, + ) + ) diff --git a/xhiveframework/patches/v12_0/delete_feedback_request_if_exists.py b/xhiveframework/patches/v12_0/delete_feedback_request_if_exists.py new file mode 100644 index 0000000..df5e039 --- /dev/null +++ b/xhiveframework/patches/v12_0/delete_feedback_request_if_exists.py @@ -0,0 +1,5 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.delete("DocType", {"name": "Feedback Request"}) diff --git a/xhiveframework/patches/v12_0/fix_email_id_formatting.py b/xhiveframework/patches/v12_0/fix_email_id_formatting.py new file mode 100644 index 0000000..49baf0a --- /dev/null +++ b/xhiveframework/patches/v12_0/fix_email_id_formatting.py @@ -0,0 +1,56 @@ +import xhiveframework + + +def execute(): + fix_communications() + fix_show_as_cc_email_queue() + fix_email_queue_recipients() + + +def fix_communications(): + for communication in xhiveframework.db.sql( + """select name, recipients, cc, bcc from tabCommunication + where creation > '2020-06-01' + and communication_medium='Email' + and communication_type='Communication' + and (cc like '%<%' or bcc like '%<%' or recipients like '%<%') + """, + as_dict=1, + ): + communication["recipients"] = format_email_id(communication.recipients) + communication["cc"] = format_email_id(communication.cc) + communication["bcc"] = format_email_id(communication.bcc) + + xhiveframework.db.sql( + """update `tabCommunication` set recipients=%s,cc=%s,bcc=%s + where name =%s """, + (communication["recipients"], communication["cc"], communication["bcc"], communication["name"]), + ) + + +def fix_show_as_cc_email_queue(): + for queue in xhiveframework.get_all( + "Email Queue", + {"creation": [">", "2020-06-01"], "status": "Not Sent", "show_as_cc": ["like", "%<%"]}, + ["name", "show_as_cc"], + ): + xhiveframework.db.set_value("Email Queue", queue["name"], "show_as_cc", format_email_id(queue["show_as_cc"])) + + +def fix_email_queue_recipients(): + for recipient in xhiveframework.db.sql( + """select recipient, name from + `tabEmail Queue Recipient` where recipient like '%<%' + and status='Not Sent' and creation > '2020-06-01' """, + as_dict=1, + ): + xhiveframework.db.set_value( + "Email Queue Recipient", recipient["name"], "recipient", format_email_id(recipient["recipient"]) + ) + + +def format_email_id(email): + if email and ("<" in email and ">" in email): + return email.replace(">", ">").replace("<", "<") + + return email diff --git a/xhiveframework/patches/v12_0/fix_public_private_files.py b/xhiveframework/patches/v12_0/fix_public_private_files.py new file mode 100644 index 0000000..649ca6c --- /dev/null +++ b/xhiveframework/patches/v12_0/fix_public_private_files.py @@ -0,0 +1,34 @@ +import xhiveframework + + +def execute(): + files = xhiveframework.get_all("File", fields=["is_private", "file_url", "name"], filters={"is_folder": 0}) + + for file in files: + file_url = file.file_url or "" + if file.is_private: + if not file_url.startswith("/private/files/"): + generate_file(file.name) + else: + if file_url.startswith("/private/files/"): + generate_file(file.name) + + +def generate_file(file_name): + try: + file_doc = xhiveframework.get_doc("File", file_name) + # private + new_doc = xhiveframework.new_doc("File") + new_doc.is_private = file_doc.is_private + new_doc.file_name = file_doc.file_name + # to create copy of file in right location + # if the file doc is private then the file will be created in /private folder + # if the file doc is public then the file will be created in /files folder + new_doc.save_file(content=file_doc.get_content(), ignore_existing_file_check=True) + + file_doc.file_url = new_doc.file_url + file_doc.save() + except OSError: + pass + except Exception as e: + print(e) diff --git a/xhiveframework/patches/v12_0/move_email_and_phone_to_child_table.py b/xhiveframework/patches/v12_0/move_email_and_phone_to_child_table.py new file mode 100644 index 0000000..7a8a8fe --- /dev/null +++ b/xhiveframework/patches/v12_0/move_email_and_phone_to_child_table.py @@ -0,0 +1,105 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("contacts", "doctype", "contact_email") + xhiveframework.reload_doc("contacts", "doctype", "contact_phone") + xhiveframework.reload_doc("contacts", "doctype", "contact") + + contact_details = xhiveframework.db.sql( + """ + SELECT + `name`, `email_id`, `phone`, `mobile_no`, `modified_by`, `creation`, `modified` + FROM `tabContact` + where not exists (select * from `tabContact Email` + where `tabContact Email`.parent=`tabContact`.name + and `tabContact Email`.email_id=`tabContact`.email_id) + """, + as_dict=True, + ) + + email_values = [] + phone_values = [] + for count, contact_detail in enumerate(contact_details): + phone_counter = 1 + if contact_detail.email_id: + email_values.append( + ( + 1, + xhiveframework.generate_hash(length=10), + contact_detail.email_id, + "email_ids", + "Contact", + contact_detail.name, + 1, + contact_detail.creation, + contact_detail.modified, + contact_detail.modified_by, + ) + ) + + if contact_detail.phone: + is_primary_phone = 1 if phone_counter == 1 else 0 + phone_values.append( + ( + phone_counter, + xhiveframework.generate_hash(length=10), + contact_detail.phone, + "phone_nos", + "Contact", + contact_detail.name, + is_primary_phone, + 0, + contact_detail.creation, + contact_detail.modified, + contact_detail.modified_by, + ) + ) + phone_counter += 1 + + if contact_detail.mobile_no: + is_primary_mobile_no = 1 if phone_counter == 1 else 0 + phone_values.append( + ( + phone_counter, + xhiveframework.generate_hash(length=10), + contact_detail.mobile_no, + "phone_nos", + "Contact", + contact_detail.name, + 0, + is_primary_mobile_no, + contact_detail.creation, + contact_detail.modified, + contact_detail.modified_by, + ) + ) + + if email_values and (count % 10000 == 0 or count == len(contact_details) - 1): + xhiveframework.db.sql( + """ + INSERT INTO `tabContact Email` + (`idx`, `name`, `email_id`, `parentfield`, `parenttype`, `parent`, `is_primary`, `creation`, + `modified`, `modified_by`) + VALUES {} + """.format(", ".join(["%s"] * len(email_values))), + tuple(email_values), + ) + + email_values = [] + + if phone_values and (count % 10000 == 0 or count == len(contact_details) - 1): + xhiveframework.db.sql( + """ + INSERT INTO `tabContact Phone` + (`idx`, `name`, `phone`, `parentfield`, `parenttype`, `parent`, `is_primary_phone`, `is_primary_mobile_no`, `creation`, + `modified`, `modified_by`) + VALUES {} + """.format(", ".join(["%s"] * len(phone_values))), + tuple(phone_values), + ) + + phone_values = [] + + xhiveframework.db.add_index("Contact Phone", ["phone"]) + xhiveframework.db.add_index("Contact Email", ["email_id"]) diff --git a/xhiveframework/patches/v12_0/move_form_attachments_to_attachments_folder.py b/xhiveframework/patches/v12_0/move_form_attachments_to_attachments_folder.py new file mode 100644 index 0000000..ec0fa65 --- /dev/null +++ b/xhiveframework/patches/v12_0/move_form_attachments_to_attachments_folder.py @@ -0,0 +1,12 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.sql( + """ + UPDATE tabFile + SET folder = 'Home/Attachments' + WHERE ifnull(attached_to_doctype, '') != '' + AND folder = 'Home' + """ + ) diff --git a/xhiveframework/patches/v12_0/move_timeline_links_to_dynamic_links.py b/xhiveframework/patches/v12_0/move_timeline_links_to_dynamic_links.py new file mode 100644 index 0000000..bde8b64 --- /dev/null +++ b/xhiveframework/patches/v12_0/move_timeline_links_to_dynamic_links.py @@ -0,0 +1,64 @@ +import xhiveframework + + +def execute(): + communications = xhiveframework.db.sql( + """ + SELECT + `tabCommunication`.name, `tabCommunication`.creation, `tabCommunication`.modified, + `tabCommunication`.modified_by,`tabCommunication`.timeline_doctype, `tabCommunication`.timeline_name, + `tabCommunication`.link_doctype, `tabCommunication`.link_name + FROM `tabCommunication` + WHERE `tabCommunication`.communication_medium='Email' + """, + as_dict=True, + ) + + name = 1000000000 + values = [] + + for count, communication in enumerate(communications): + counter = 1 + if communication.timeline_doctype and communication.timeline_name: + name += 1 + values.append( + """({}, "{}", "timeline_links", "Communication", "{}", "{}", "{}", "{}", "{}", "{}")""".format( + counter, + str(name), + xhiveframework.db.escape(communication.name), + xhiveframework.db.escape(communication.timeline_doctype), + xhiveframework.db.escape(communication.timeline_name), + communication.creation, + communication.modified, + communication.modified_by, + ) + ) + counter += 1 + if communication.link_doctype and communication.link_name: + name += 1 + values.append( + """({}, "{}", "timeline_links", "Communication", "{}", "{}", "{}", "{}", "{}", "{}")""".format( + counter, + str(name), + xhiveframework.db.escape(communication.name), + xhiveframework.db.escape(communication.link_doctype), + xhiveframework.db.escape(communication.link_name), + communication.creation, + communication.modified, + communication.modified_by, + ) + ) + + if values and (count % 10000 == 0 or count == len(communications) - 1): + xhiveframework.db.sql( + """ + INSERT INTO `tabCommunication Link` + (`idx`, `name`, `parentfield`, `parenttype`, `parent`, `link_doctype`, `link_name`, `creation`, + `modified`, `modified_by`) + VALUES {} + """.format(", ".join([d for d in values])) + ) + + values = [] + + xhiveframework.db.add_index("Communication Link", ["link_doctype", "link_name"]) diff --git a/xhiveframework/patches/v12_0/remove_deprecated_fields_from_doctype.py b/xhiveframework/patches/v12_0/remove_deprecated_fields_from_doctype.py new file mode 100644 index 0000000..ad1dfb7 --- /dev/null +++ b/xhiveframework/patches/v12_0/remove_deprecated_fields_from_doctype.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "doctype_link") + xhiveframework.reload_doc("core", "doctype", "doctype_action") + xhiveframework.reload_doc("core", "doctype", "doctype") + xhiveframework.model.delete_fields({"DocType": ["hide_heading", "image_view", "read_only_onload"]}, delete=1) + + xhiveframework.db.delete("Property Setter", {"property": "read_only_onload"}) diff --git a/xhiveframework/patches/v12_0/remove_example_email_thread_notify.py b/xhiveframework/patches/v12_0/remove_example_email_thread_notify.py new file mode 100644 index 0000000..4ba7db0 --- /dev/null +++ b/xhiveframework/patches/v12_0/remove_example_email_thread_notify.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + # remove all example.com email user accounts from notifications + xhiveframework.db.sql( + """UPDATE `tabUser` + SET thread_notify=0, send_me_a_copy=0 + WHERE email like '%@example.com'""" + ) diff --git a/xhiveframework/patches/v12_0/remove_feedback_rating.py b/xhiveframework/patches/v12_0/remove_feedback_rating.py new file mode 100644 index 0000000..73b1c54 --- /dev/null +++ b/xhiveframework/patches/v12_0/remove_feedback_rating.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + """ + Deprecate Feedback Trigger and Rating. This feature was not customizable. + Now can be achieved via custom Web Forms + """ + xhiveframework.delete_doc("DocType", "Feedback Trigger") + xhiveframework.delete_doc("DocType", "Feedback Rating") diff --git a/xhiveframework/patches/v12_0/rename_events_repeat_on.py b/xhiveframework/patches/v12_0/rename_events_repeat_on.py new file mode 100644 index 0000000..9ddfbbd --- /dev/null +++ b/xhiveframework/patches/v12_0/rename_events_repeat_on.py @@ -0,0 +1,37 @@ +import xhiveframework +from xhiveframework.utils import get_datetime + + +def execute(): + weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] + + weekly_events = xhiveframework.get_list( + "Event", + filters={"repeat_this_event": 1, "repeat_on": "Every Week"}, + fields=["name", "starts_on"], + ) + xhiveframework.reload_doc("desk", "doctype", "event") + + # Initially Daily Events had option to choose days, but now Weekly does, so just changing from Daily -> Weekly does the job + xhiveframework.db.sql( + """UPDATE `tabEvent` SET `tabEvent`.repeat_on='Weekly' WHERE `tabEvent`.repeat_on='Every Day'""" + ) + xhiveframework.db.sql( + """UPDATE `tabEvent` SET `tabEvent`.repeat_on='Weekly' WHERE `tabEvent`.repeat_on='Every Week'""" + ) + xhiveframework.db.sql( + """UPDATE `tabEvent` SET `tabEvent`.repeat_on='Monthly' WHERE `tabEvent`.repeat_on='Every Month'""" + ) + xhiveframework.db.sql( + """UPDATE `tabEvent` SET `tabEvent`.repeat_on='Yearly' WHERE `tabEvent`.repeat_on='Every Year'""" + ) + + for weekly_event in weekly_events: + # Set WeekDay based on the starts_on so that event can repeat Weekly + xhiveframework.db.set_value( + "Event", + weekly_event.name, + weekdays[get_datetime(weekly_event.starts_on).weekday()], + 1, + update_modified=False, + ) diff --git a/xhiveframework/patches/v12_0/rename_uploaded_files_with_proper_name.py b/xhiveframework/patches/v12_0/rename_uploaded_files_with_proper_name.py new file mode 100644 index 0000000..ef8bad0 --- /dev/null +++ b/xhiveframework/patches/v12_0/rename_uploaded_files_with_proper_name.py @@ -0,0 +1,33 @@ +import os + +import xhiveframework + + +def execute(): + file_names_with_url = xhiveframework.get_all( + "File", + filters={"is_folder": 0, "file_name": ["like", "%/%"]}, + fields=["name", "file_name", "file_url"], + ) + + for f in file_names_with_url: + filename = f.file_name.rsplit("/", 1)[-1] + + if not f.file_url: + f.file_url = f.file_name + + try: + if not file_exists(f.file_url): + continue + xhiveframework.db.set_value( + "File", f.name, {"file_name": filename, "file_url": f.file_url}, update_modified=False + ) + except Exception: + continue + + +def file_exists(file_path): + file_path = xhiveframework.utils.get_files_path( + file_path.rsplit("/", 1)[-1], is_private=file_path.startswith("/private") + ) + return os.path.exists(file_path) diff --git a/xhiveframework/patches/v12_0/replace_null_values_in_tables.py b/xhiveframework/patches/v12_0/replace_null_values_in_tables.py new file mode 100644 index 0000000..d0a84ca --- /dev/null +++ b/xhiveframework/patches/v12_0/replace_null_values_in_tables.py @@ -0,0 +1,30 @@ +import re + +import xhiveframework + + +def execute(): + fields = xhiveframework.db.sql( + """ + SELECT COLUMN_NAME , TABLE_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS + WHERE DATA_TYPE IN ('INT', 'FLOAT', 'DECIMAL') AND IS_NULLABLE = 'YES' + """, + as_dict=1, + ) + + update_column_table_map = {} + + for field in fields: + update_column_table_map.setdefault(field.TABLE_NAME, []) + + update_column_table_map[field.TABLE_NAME].append( + f"`{field.COLUMN_NAME}`=COALESCE(`{field.COLUMN_NAME}`, 0)" + ) + + for table in xhiveframework.db.get_tables(): + if update_column_table_map.get(table) and xhiveframework.db.exists("DocType", re.sub("^tab", "", table)): + xhiveframework.db.sql( + """UPDATE `{table}` SET {columns}""".format( + table=table, columns=", ".join(update_column_table_map.get(table)) + ) + ) diff --git a/xhiveframework/patches/v12_0/reset_home_settings.py b/xhiveframework/patches/v12_0/reset_home_settings.py new file mode 100644 index 0000000..87bf85a --- /dev/null +++ b/xhiveframework/patches/v12_0/reset_home_settings.py @@ -0,0 +1,12 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "user") + xhiveframework.db.sql( + """ + UPDATE `tabUser` + SET `home_settings` = '' + WHERE `user_type` = 'System User' + """ + ) diff --git a/xhiveframework/patches/v12_0/set_correct_assign_value_in_docs.py b/xhiveframework/patches/v12_0/set_correct_assign_value_in_docs.py new file mode 100644 index 0000000..e786733 --- /dev/null +++ b/xhiveframework/patches/v12_0/set_correct_assign_value_in_docs.py @@ -0,0 +1,28 @@ +import xhiveframework +from xhiveframework.query_builder.functions import Coalesce, GroupConcat + + +def execute(): + xhiveframework.reload_doc("desk", "doctype", "todo") + + ToDo = xhiveframework.qb.DocType("ToDo") + assignees = GroupConcat("owner").distinct().as_("assignees") + + assignments = ( + xhiveframework.qb.from_(ToDo) + .select(ToDo.name, ToDo.reference_type, assignees) + .where(Coalesce(ToDo.reference_type, "") != "") + .where(Coalesce(ToDo.reference_name, "") != "") + .where(ToDo.status != "Cancelled") + .groupby(ToDo.reference_type, ToDo.reference_name) + ).run(as_dict=True) + + for doc in assignments: + assignments = doc.assignees.split(",") + xhiveframework.db.set_value( + doc.reference_type, + doc.reference_name, + "_assign", + xhiveframework.as_json(assignments), + update_modified=False, + ) diff --git a/xhiveframework/patches/v12_0/set_correct_url_in_files.py b/xhiveframework/patches/v12_0/set_correct_url_in_files.py new file mode 100644 index 0000000..0e35b1b --- /dev/null +++ b/xhiveframework/patches/v12_0/set_correct_url_in_files.py @@ -0,0 +1,41 @@ +import os + +import xhiveframework + + +def execute(): + files = xhiveframework.get_all( + "File", + fields=["name", "file_name", "file_url"], + filters={ + "is_folder": 0, + "file_url": ["!=", ""], + }, + ) + + private_file_path = xhiveframework.get_site_path("private", "files") + public_file_path = xhiveframework.get_site_path("public", "files") + + for file in files: + file_path = file.file_url + file_name = file_path.split("/")[-1] + + if not file_path.startswith(("/private/", "/files/")): + continue + + file_is_private = file_path.startswith("/private/files/") + full_path = xhiveframework.utils.get_files_path(file_name, is_private=file_is_private) + + if not os.path.exists(full_path): + if file_is_private: + public_file_url = os.path.join(public_file_path, file_name) + if os.path.exists(public_file_url): + xhiveframework.db.set_value( + "File", file.name, {"file_url": f"/files/{file_name}", "is_private": 0} + ) + else: + private_file_url = os.path.join(private_file_path, file_name) + if os.path.exists(private_file_url): + xhiveframework.db.set_value( + "File", file.name, {"file_url": f"/private/files/{file_name}", "is_private": 1} + ) diff --git a/xhiveframework/patches/v12_0/set_default_incoming_email_port.py b/xhiveframework/patches/v12_0/set_default_incoming_email_port.py new file mode 100644 index 0000000..373ea5b --- /dev/null +++ b/xhiveframework/patches/v12_0/set_default_incoming_email_port.py @@ -0,0 +1,43 @@ +import xhiveframework +from xhiveframework.email.utils import get_port + + +def execute(): + """ + 1. Set default incoming email port in email domain + 2. Set default incoming email port in all email account (for those account where domain is missing) + """ + xhiveframework.reload_doc("email", "doctype", "email_domain", force=True) + xhiveframework.reload_doc("email", "doctype", "email_account", force=True) + + setup_incoming_email_port_in_email_domains() + setup_incoming_email_port_in_email_accounts() + + +def setup_incoming_email_port_in_email_domains(): + email_domains = xhiveframework.get_all("Email Domain", ["incoming_port", "use_imap", "use_ssl", "name"]) + for domain in email_domains: + if not domain.incoming_port: + incoming_port = get_port(domain) + xhiveframework.db.set_value( + "Email Domain", domain.name, "incoming_port", incoming_port, update_modified=False + ) + + # update incoming email port in all + xhiveframework.db.sql( + """update `tabEmail Account` set incoming_port=%s where domain = %s""", + (domain.incoming_port, domain.name), + ) + + +def setup_incoming_email_port_in_email_accounts(): + email_accounts = xhiveframework.get_all( + "Email Account", ["incoming_port", "use_imap", "use_ssl", "name", "enable_incoming"] + ) + + for account in email_accounts: + if account.enable_incoming and not account.incoming_port: + incoming_port = get_port(account) + xhiveframework.db.set_value( + "Email Account", account.name, "incoming_port", incoming_port, update_modified=False + ) diff --git a/xhiveframework/patches/v12_0/set_default_password_reset_limit.py b/xhiveframework/patches/v12_0/set_default_password_reset_limit.py new file mode 100644 index 0000000..c43e497 --- /dev/null +++ b/xhiveframework/patches/v12_0/set_default_password_reset_limit.py @@ -0,0 +1,9 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "system_settings", force=1) + xhiveframework.db.set_single_value("System Settings", "password_reset_limit", 3) diff --git a/xhiveframework/patches/v12_0/set_primary_key_in_series.py b/xhiveframework/patches/v12_0/set_primary_key_in_series.py new file mode 100644 index 0000000..e8ecc2b --- /dev/null +++ b/xhiveframework/patches/v12_0/set_primary_key_in_series.py @@ -0,0 +1,26 @@ +import xhiveframework + + +def execute(): + # if current = 0, simply delete the key as it'll be recreated on first entry + xhiveframework.db.delete("Series", {"current": 0}) + + duplicate_keys = xhiveframework.db.sql( + """ + SELECT name, max(current) as current + from + `tabSeries` + group by + name + having count(name) > 1 + """, + as_dict=True, + ) + + for row in duplicate_keys: + xhiveframework.db.delete("Series", {"name": row.name}) + if row.current: + xhiveframework.db.sql("insert into `tabSeries`(`name`, `current`) values (%(name)s, %(current)s)", row) + xhiveframework.db.commit() + + xhiveframework.db.sql("ALTER table `tabSeries` ADD PRIMARY KEY IF NOT EXISTS (name)") diff --git a/xhiveframework/patches/v12_0/setup_comments_from_communications.py b/xhiveframework/patches/v12_0/setup_comments_from_communications.py new file mode 100644 index 0000000..a04d8f8 --- /dev/null +++ b/xhiveframework/patches/v12_0/setup_comments_from_communications.py @@ -0,0 +1,31 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("Comment") + + if xhiveframework.db.count("Communication", filters=dict(communication_type="Comment")) > 20000: + xhiveframework.db.auto_commit_on_many_writes = True + + for comment in xhiveframework.get_all("Communication", fields=["*"], filters=dict(communication_type="Comment")): + new_comment = xhiveframework.new_doc("Comment") + new_comment.comment_type = comment.comment_type + new_comment.comment_email = comment.sender + new_comment.comment_by = comment.sender_full_name + new_comment.subject = comment.subject + new_comment.content = comment.content or comment.subject + new_comment.reference_doctype = comment.reference_doctype + new_comment.reference_name = comment.reference_name + new_comment.link_doctype = comment.link_doctype + new_comment.link_name = comment.link_name + new_comment.creation = comment.creation + new_comment.modified = comment.modified + new_comment.owner = comment.owner + new_comment.modified_by = comment.modified_by + new_comment.db_insert() + + if xhiveframework.db.auto_commit_on_many_writes: + xhiveframework.db.auto_commit_on_many_writes = False + + # clean up + xhiveframework.db.delete("Communication", {"communication_type": "Comment"}) diff --git a/xhiveframework/patches/v12_0/setup_email_linking.py b/xhiveframework/patches/v12_0/setup_email_linking.py new file mode 100644 index 0000000..fd47442 --- /dev/null +++ b/xhiveframework/patches/v12_0/setup_email_linking.py @@ -0,0 +1,5 @@ +from xhiveframework.desk.page.setup_wizard.install_fixtures import setup_email_linking + + +def execute(): + setup_email_linking() diff --git a/xhiveframework/patches/v12_0/setup_tags.py b/xhiveframework/patches/v12_0/setup_tags.py new file mode 100644 index 0000000..3efce11 --- /dev/null +++ b/xhiveframework/patches/v12_0/setup_tags.py @@ -0,0 +1,47 @@ +import xhiveframework + + +def execute(): + xhiveframework.delete_doc_if_exists("DocType", "Tag Category") + xhiveframework.delete_doc_if_exists("DocType", "Tag Doc Category") + + xhiveframework.reload_doc("desk", "doctype", "tag") + xhiveframework.reload_doc("desk", "doctype", "tag_link") + + tag_list = [] + tag_links = [] + time = xhiveframework.utils.get_datetime() + + for doctype in xhiveframework.get_list("DocType", filters={"istable": 0, "issingle": 0, "is_virtual": 0}): + if not xhiveframework.db.count(doctype.name) or not xhiveframework.db.has_column(doctype.name, "_user_tags"): + continue + + for _user_tags in xhiveframework.db.sql( + f"select `name`, `_user_tags` from `tab{doctype.name}`", as_dict=True + ): + if not _user_tags.get("_user_tags"): + continue + + for tag in _user_tags.get("_user_tags").split(",") if _user_tags.get("_user_tags") else []: + if not tag: + continue + + tag_list.append((tag.strip(), time, time, "Administrator")) + + tag_link_name = xhiveframework.generate_hash(length=10) + tag_links.append( + (tag_link_name, doctype.name, _user_tags.name, tag.strip(), time, time, "Administrator") + ) + + xhiveframework.db.bulk_insert( + "Tag", + fields=["name", "creation", "modified", "modified_by"], + values=set(tag_list), + ignore_duplicates=True, + ) + xhiveframework.db.bulk_insert( + "Tag Link", + fields=["name", "document_type", "document_name", "tag", "creation", "modified", "modified_by"], + values=set(tag_links), + ignore_duplicates=True, + ) diff --git a/xhiveframework/patches/v12_0/update_auto_repeat_status_and_not_submittable.py b/xhiveframework/patches/v12_0/update_auto_repeat_status_and_not_submittable.py new file mode 100644 index 0000000..f812a89 --- /dev/null +++ b/xhiveframework/patches/v12_0/update_auto_repeat_status_and_not_submittable.py @@ -0,0 +1,34 @@ +import xhiveframework +from xhiveframework.custom.doctype.custom_field.custom_field import create_custom_field + + +def execute(): + # auto repeat is not submittable in v12 + xhiveframework.reload_doc("automation", "doctype", "Auto Repeat") + xhiveframework.db.sql("update `tabDocPerm` set submit=0, cancel=0, amend=0 where parent='Auto Repeat'") + xhiveframework.db.sql("update `tabAuto Repeat` set docstatus=0 where docstatus=1 or docstatus=2") + + for entry in xhiveframework.get_all("Auto Repeat"): + doc = xhiveframework.get_doc("Auto Repeat", entry.name) + + # create custom field for allow auto repeat + fields = xhiveframework.get_meta(doc.reference_doctype).fields + insert_after = fields[len(fields) - 1].fieldname + df = dict( + fieldname="auto_repeat", + label="Auto Repeat", + fieldtype="Link", + insert_after=insert_after, + options="Auto Repeat", + hidden=1, + print_hide=1, + read_only=1, + ) + create_custom_field(doc.reference_doctype, df) + + if doc.status in ["Draft", "Stopped", "Cancelled"]: + doc.disabled = 1 + + doc.flags.ignore_links = 1 + # updates current status as Active, Disabled or Completed on validate + doc.save() diff --git a/xhiveframework/patches/v12_0/update_global_search.py b/xhiveframework/patches/v12_0/update_global_search.py new file mode 100644 index 0000000..0e51825 --- /dev/null +++ b/xhiveframework/patches/v12_0/update_global_search.py @@ -0,0 +1,8 @@ +import xhiveframework +from xhiveframework.desk.page.setup_wizard.install_fixtures import update_global_search_doctypes + + +def execute(): + xhiveframework.reload_doc("desk", "doctype", "global_search_doctype") + xhiveframework.reload_doc("desk", "doctype", "global_search_settings") + update_global_search_doctypes() diff --git a/xhiveframework/patches/v12_0/update_print_format_type.py b/xhiveframework/patches/v12_0/update_print_format_type.py new file mode 100644 index 0000000..5e3e55f --- /dev/null +++ b/xhiveframework/patches/v12_0/update_print_format_type.py @@ -0,0 +1,18 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.sql( + """ + UPDATE `tabPrint Format` + SET `print_format_type` = 'Jinja' + WHERE `print_format_type` in ('Server', 'Client') + """ + ) + xhiveframework.db.sql( + """ + UPDATE `tabPrint Format` + SET `print_format_type` = 'JS' + WHERE `print_format_type` = 'Js' + """ + ) diff --git a/xhiveframework/patches/v13_0/__init__.py b/xhiveframework/patches/v13_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v13_0/add_standard_navbar_items.py b/xhiveframework/patches/v13_0/add_standard_navbar_items.py new file mode 100644 index 0000000..fe9b4c6 --- /dev/null +++ b/xhiveframework/patches/v13_0/add_standard_navbar_items.py @@ -0,0 +1,9 @@ +import xhiveframework +from xhiveframework.utils.install import add_standard_navbar_items + + +def execute(): + # Add standard navbar items for XhiveERP in Navbar Settings + xhiveframework.reload_doc("core", "doctype", "navbar_settings") + xhiveframework.reload_doc("core", "doctype", "navbar_item") + add_standard_navbar_items() diff --git a/xhiveframework/patches/v13_0/add_switch_theme_to_navbar_settings.py b/xhiveframework/patches/v13_0/add_switch_theme_to_navbar_settings.py new file mode 100644 index 0000000..3ee4171 --- /dev/null +++ b/xhiveframework/patches/v13_0/add_switch_theme_to_navbar_settings.py @@ -0,0 +1,24 @@ +import xhiveframework + + +def execute(): + navbar_settings = xhiveframework.get_single("Navbar Settings") + + if xhiveframework.db.exists("Navbar Item", {"item_label": "Toggle Theme"}): + return + + for navbar_item in navbar_settings.settings_dropdown[6:]: + navbar_item.idx = navbar_item.idx + 1 + + navbar_settings.append( + "settings_dropdown", + { + "item_label": "Toggle Theme", + "item_type": "Action", + "action": "new xhiveframework.ui.ThemeSwitcher().show()", + "is_standard": 1, + "idx": 7, + }, + ) + + navbar_settings.save() diff --git a/xhiveframework/patches/v13_0/add_toggle_width_in_navbar_settings.py b/xhiveframework/patches/v13_0/add_toggle_width_in_navbar_settings.py new file mode 100644 index 0000000..4e57bfa --- /dev/null +++ b/xhiveframework/patches/v13_0/add_toggle_width_in_navbar_settings.py @@ -0,0 +1,24 @@ +import xhiveframework + + +def execute(): + navbar_settings = xhiveframework.get_single("Navbar Settings") + + if xhiveframework.db.exists("Navbar Item", {"item_label": "Toggle Full Width"}): + return + + for navbar_item in navbar_settings.settings_dropdown[5:]: + navbar_item.idx = navbar_item.idx + 1 + + navbar_settings.append( + "settings_dropdown", + { + "item_label": "Toggle Full Width", + "item_type": "Action", + "action": "xhiveframework.ui.toolbar.toggle_full_width()", + "is_standard": 1, + "idx": 6, + }, + ) + + navbar_settings.save() diff --git a/xhiveframework/patches/v13_0/create_custom_dashboards_cards_and_charts.py b/xhiveframework/patches/v13_0/create_custom_dashboards_cards_and_charts.py new file mode 100644 index 0000000..841531b --- /dev/null +++ b/xhiveframework/patches/v13_0/create_custom_dashboards_cards_and_charts.py @@ -0,0 +1,46 @@ +import xhiveframework +from xhiveframework.model.naming import append_number_if_name_exists +from xhiveframework.utils.dashboard import get_dashboards_with_link + + +def execute(): + if ( + not xhiveframework.db.table_exists("Dashboard Chart") + or not xhiveframework.db.table_exists("Number Card") + or not xhiveframework.db.table_exists("Dashboard") + ): + return + + xhiveframework.reload_doc("desk", "doctype", "dashboard_chart") + xhiveframework.reload_doc("desk", "doctype", "number_card") + xhiveframework.reload_doc("desk", "doctype", "dashboard") + + modified_charts = get_modified_docs("Dashboard Chart") + modified_cards = get_modified_docs("Number Card") + modified_dashboards = [doc.name for doc in get_modified_docs("Dashboard")] + + for chart in modified_charts: + modified_dashboards += get_dashboards_with_link(chart.name, "Dashboard Chart") + rename_modified_doc(chart.name, "Dashboard Chart") + + for card in modified_cards: + modified_dashboards += get_dashboards_with_link(card.name, "Number Card") + rename_modified_doc(card.name, "Number Card") + + modified_dashboards = list(set(modified_dashboards)) + + for dashboard in modified_dashboards: + rename_modified_doc(dashboard, "Dashboard") + + +def get_modified_docs(doctype): + return xhiveframework.get_all(doctype, filters={"owner": "Administrator", "modified_by": ["!=", "Administrator"]}) + + +def rename_modified_doc(docname, doctype): + new_name = docname + " Custom" + try: + xhiveframework.rename_doc(doctype, docname, new_name) + except xhiveframework.ValidationError: + new_name = append_number_if_name_exists(doctype, new_name) + xhiveframework.rename_doc(doctype, docname, new_name) diff --git a/xhiveframework/patches/v13_0/delete_package_publish_tool.py b/xhiveframework/patches/v13_0/delete_package_publish_tool.py new file mode 100644 index 0000000..72b5d1b --- /dev/null +++ b/xhiveframework/patches/v13_0/delete_package_publish_tool.py @@ -0,0 +1,10 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.delete_doc("DocType", "Package Publish Tool", ignore_missing=True) + xhiveframework.delete_doc("DocType", "Package Document Type", ignore_missing=True) + xhiveframework.delete_doc("DocType", "Package Publish Target", ignore_missing=True) diff --git a/xhiveframework/patches/v13_0/email_unsubscribe.py b/xhiveframework/patches/v13_0/email_unsubscribe.py new file mode 100644 index 0000000..b709d72 --- /dev/null +++ b/xhiveframework/patches/v13_0/email_unsubscribe.py @@ -0,0 +1,14 @@ +import xhiveframework + + +def execute(): + email_unsubscribe = [ + {"email": "admin@example.com", "global_unsubscribe": 1}, + {"email": "guest@example.com", "global_unsubscribe": 1}, + ] + + for unsubscribe in email_unsubscribe: + if not xhiveframework.get_all("Email Unsubscribe", filters=unsubscribe): + doc = xhiveframework.new_doc("Email Unsubscribe") + doc.update(unsubscribe) + doc.insert(ignore_permissions=True) diff --git a/xhiveframework/patches/v13_0/enable_custom_script.py b/xhiveframework/patches/v13_0/enable_custom_script.py new file mode 100644 index 0000000..5ab2aa5 --- /dev/null +++ b/xhiveframework/patches/v13_0/enable_custom_script.py @@ -0,0 +1,14 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + """Enable all the existing Client script""" + + xhiveframework.db.sql( + """ + UPDATE `tabClient Script` SET enabled=1 + """ + ) diff --git a/xhiveframework/patches/v13_0/encrypt_2fa_secrets.py b/xhiveframework/patches/v13_0/encrypt_2fa_secrets.py new file mode 100644 index 0000000..d281025 --- /dev/null +++ b/xhiveframework/patches/v13_0/encrypt_2fa_secrets.py @@ -0,0 +1,45 @@ +import xhiveframework +import xhiveframework.defaults +from xhiveframework.cache_manager import clear_defaults_cache +from xhiveframework.twofactor import PARENT_FOR_DEFAULTS +from xhiveframework.utils.password import encrypt + +DOCTYPE = "DefaultValue" +OLD_PARENT = "__default" + + +def execute(): + table = xhiveframework.qb.DocType(DOCTYPE) + + # set parent for `*_otplogin` + ( + xhiveframework.qb.update(table) + .set(table.parent, PARENT_FOR_DEFAULTS) + .where(table.parent == OLD_PARENT) + .where(table.defkey.like("%_otplogin")) + ).run() + + # update records for `*_otpsecret` + secrets = { + key: value + for key, value in xhiveframework.defaults.get_defaults_for(parent=OLD_PARENT).items() + if key.endswith("_otpsecret") + } + + if not secrets: + return + + defvalue_cases = xhiveframework.qb.terms.Case() + + for key, value in secrets.items(): + defvalue_cases.when(table.defkey == key, encrypt(value)) + + ( + xhiveframework.qb.update(table) + .set(table.parent, PARENT_FOR_DEFAULTS) + .set(table.defvalue, defvalue_cases) + .where(table.parent == OLD_PARENT) + .where(table.defkey.like("%_otpsecret")) + ).run() + + clear_defaults_cache() diff --git a/xhiveframework/patches/v13_0/generate_theme_files_in_public_folder.py b/xhiveframework/patches/v13_0/generate_theme_files_in_public_folder.py new file mode 100644 index 0000000..a0838f3 --- /dev/null +++ b/xhiveframework/patches/v13_0/generate_theme_files_in_public_folder.py @@ -0,0 +1,16 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "website_theme_ignore_app") + themes = xhiveframework.get_all("Website Theme", filters={"theme_url": ("not like", "/files/website_theme/%")}) + for theme in themes: + doc = xhiveframework.get_doc("Website Theme", theme.name) + try: + doc.save() + except Exception: + print("Ignoring....") + print(xhiveframework.get_traceback()) diff --git a/xhiveframework/patches/v13_0/increase_password_length.py b/xhiveframework/patches/v13_0/increase_password_length.py new file mode 100644 index 0000000..28776ef --- /dev/null +++ b/xhiveframework/patches/v13_0/increase_password_length.py @@ -0,0 +1,5 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.change_column_type("__Auth", column="password", type="TEXT") diff --git a/xhiveframework/patches/v13_0/jinja_hook.py b/xhiveframework/patches/v13_0/jinja_hook.py new file mode 100644 index 0000000..8db8361 --- /dev/null +++ b/xhiveframework/patches/v13_0/jinja_hook.py @@ -0,0 +1,17 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from click import secho + +import xhiveframework + + +def execute(): + if xhiveframework.get_hooks("jenv"): + print() + secho( + 'WARNING: The hook "jenv" is deprecated. Follow the migration guide to use the new "jinja" hook.', + fg="yellow", + ) + secho("https://lab.membtech.com/xhiveframework/xhiveframework15/wiki/Migrating-to-Version-13", fg="yellow") + print() diff --git a/xhiveframework/patches/v13_0/make_user_type.py b/xhiveframework/patches/v13_0/make_user_type.py new file mode 100644 index 0000000..855efbd --- /dev/null +++ b/xhiveframework/patches/v13_0/make_user_type.py @@ -0,0 +1,12 @@ +import xhiveframework +from xhiveframework.utils.install import create_user_type + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "role") + xhiveframework.reload_doc("core", "doctype", "user_document_type") + xhiveframework.reload_doc("core", "doctype", "user_type_module") + xhiveframework.reload_doc("core", "doctype", "user_select_document_type") + xhiveframework.reload_doc("core", "doctype", "user_type") + + create_user_type() diff --git a/xhiveframework/patches/v13_0/migrate_translation_column_data.py b/xhiveframework/patches/v13_0/migrate_translation_column_data.py new file mode 100644 index 0000000..c877458 --- /dev/null +++ b/xhiveframework/patches/v13_0/migrate_translation_column_data.py @@ -0,0 +1,8 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("Translation") + xhiveframework.db.sql( + "UPDATE `tabTranslation` SET `translated_text`=`target_name`, `source_text`=`source_name`, `contributed`=0" + ) diff --git a/xhiveframework/patches/v13_0/queryreport_columns.py b/xhiveframework/patches/v13_0/queryreport_columns.py new file mode 100644 index 0000000..5502172 --- /dev/null +++ b/xhiveframework/patches/v13_0/queryreport_columns.py @@ -0,0 +1,18 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework + + +def execute(): + """Convert Query Report json to support other content""" + records = xhiveframework.get_all("Report", filters={"json": ["!=", ""]}, fields=["name", "json"]) + for record in records: + jstr = record["json"] + data = json.loads(jstr) + if isinstance(data, list): + # double escape braces + jstr = f'{{"columns":{jstr}}}' + xhiveframework.db.set_value("Report", record["name"], "json", jstr) diff --git a/xhiveframework/patches/v13_0/remove_chat.py b/xhiveframework/patches/v13_0/remove_chat.py new file mode 100644 index 0000000..817808d --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_chat.py @@ -0,0 +1,19 @@ +import click + +import xhiveframework + + +def execute(): + xhiveframework.delete_doc_if_exists("DocType", "Chat Message") + xhiveframework.delete_doc_if_exists("DocType", "Chat Message Attachment") + xhiveframework.delete_doc_if_exists("DocType", "Chat Profile") + xhiveframework.delete_doc_if_exists("DocType", "Chat Token") + xhiveframework.delete_doc_if_exists("DocType", "Chat Room User") + xhiveframework.delete_doc_if_exists("DocType", "Chat Room") + xhiveframework.delete_doc_if_exists("Module Def", "Chat") + + click.secho( + "Chat Module is moved to a separate app and is removed from XhiveFramework in version-13.\n" + "Please install the app to continue using the chat feature: https://lab.membtech.com/xhiveframework/chat", + fg="yellow", + ) diff --git a/xhiveframework/patches/v13_0/remove_custom_link.py b/xhiveframework/patches/v13_0/remove_custom_link.py new file mode 100644 index 0000000..ef22d07 --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_custom_link.py @@ -0,0 +1,18 @@ +import xhiveframework + + +def execute(): + """ + Remove the doctype "Custom Link" that was used to add Custom Links to the + Dashboard since this is now managed by Customize Form. + Update `parent` property to the DocType and delte the doctype + """ + xhiveframework.reload_doctype("DocType Link") + if xhiveframework.db.has_table("Custom Link"): + for custom_link in xhiveframework.get_all("Custom Link", ["name", "document_type"]): + xhiveframework.db.sql( + "update `tabDocType Link` set custom=1, parent=%s where parent=%s", + (custom_link.document_type, custom_link.name), + ) + + xhiveframework.delete_doc("DocType", "Custom Link") diff --git a/xhiveframework/patches/v13_0/remove_duplicate_navbar_items.py b/xhiveframework/patches/v13_0/remove_duplicate_navbar_items.py new file mode 100644 index 0000000..9323d84 --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_duplicate_navbar_items.py @@ -0,0 +1,14 @@ +import xhiveframework + + +def execute(): + navbar_settings = xhiveframework.get_single("Navbar Settings") + duplicate_items = [ + navbar_item + for navbar_item in navbar_settings.settings_dropdown + if navbar_item.item_label == "Toggle Full Width" + ] + + if len(duplicate_items) > 1: + navbar_settings.remove(duplicate_items[0]) + navbar_settings.save() diff --git a/xhiveframework/patches/v13_0/remove_invalid_options_for_data_fields.py b/xhiveframework/patches/v13_0/remove_invalid_options_for_data_fields.py new file mode 100644 index 0000000..1b4f96c --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_invalid_options_for_data_fields.py @@ -0,0 +1,15 @@ +# Copyright (c) 2022, XhiveFramework and Contributors +# License: MIT. See LICENSE + + +import xhiveframework +from xhiveframework.model import data_field_options + + +def execute(): + custom_field = xhiveframework.qb.DocType("Custom Field") + ( + xhiveframework.qb.update(custom_field) + .set(custom_field.options, None) + .where((custom_field.fieldtype == "Data") & (custom_field.options.notin(data_field_options))) + ).run() diff --git a/xhiveframework/patches/v13_0/remove_share_for_std_users.py b/xhiveframework/patches/v13_0/remove_share_for_std_users.py new file mode 100644 index 0000000..dd5ce59 --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_share_for_std_users.py @@ -0,0 +1,7 @@ +import xhiveframework +import xhiveframework.share + + +def execute(): + for user in xhiveframework.STANDARD_USERS: + xhiveframework.share.remove("User", user, user) diff --git a/xhiveframework/patches/v13_0/remove_tailwind_from_page_builder.py b/xhiveframework/patches/v13_0/remove_tailwind_from_page_builder.py new file mode 100644 index 0000000..9c5b24a --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_tailwind_from_page_builder.py @@ -0,0 +1,11 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "web_page_block") + # remove unused templates + xhiveframework.delete_doc("Web Template", "Navbar with Links on Right", force=1) + xhiveframework.delete_doc("Web Template", "Footer Horizontal", force=1) diff --git a/xhiveframework/patches/v13_0/remove_twilio_settings.py b/xhiveframework/patches/v13_0/remove_twilio_settings.py new file mode 100644 index 0000000..cd57a3a --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_twilio_settings.py @@ -0,0 +1,20 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + """Add missing Twilio patch. + + While making Twilio as a standaone app, we missed to delete Twilio records from DB through migration. Adding the missing patch. + """ + xhiveframework.delete_doc_if_exists("DocType", "Twilio Number Group") + if twilio_settings_doctype_in_integrations(): + xhiveframework.delete_doc_if_exists("DocType", "Twilio Settings") + xhiveframework.db.delete("Singles", {"doctype": "Twilio Settings"}) + + +def twilio_settings_doctype_in_integrations() -> bool: + """Check Twilio Settings doctype exists in integrations module or not.""" + return xhiveframework.db.exists("DocType", {"name": "Twilio Settings", "module": "Integrations"}) diff --git a/xhiveframework/patches/v13_0/remove_web_view.py b/xhiveframework/patches/v13_0/remove_web_view.py new file mode 100644 index 0000000..9d7814f --- /dev/null +++ b/xhiveframework/patches/v13_0/remove_web_view.py @@ -0,0 +1,7 @@ +import xhiveframework + + +def execute(): + xhiveframework.delete_doc_if_exists("DocType", "Web View") + xhiveframework.delete_doc_if_exists("DocType", "Web View Component") + xhiveframework.delete_doc_if_exists("DocType", "CSS Class") diff --git a/xhiveframework/patches/v13_0/rename_custom_client_script.py b/xhiveframework/patches/v13_0/rename_custom_client_script.py new file mode 100644 index 0000000..f4bcc0b --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_custom_client_script.py @@ -0,0 +1,13 @@ +import xhiveframework +from xhiveframework.model.rename_doc import rename_doc + + +def execute(): + if xhiveframework.db.exists("DocType", "Client Script"): + return + + xhiveframework.flags.ignore_route_conflict_validation = True + rename_doc("DocType", "Custom Script", "Client Script") + xhiveframework.flags.ignore_route_conflict_validation = False + + xhiveframework.reload_doctype("Client Script", force=True) diff --git a/xhiveframework/patches/v13_0/rename_desk_page_to_workspace.py b/xhiveframework/patches/v13_0/rename_desk_page_to_workspace.py new file mode 100644 index 0000000..889cf23 --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_desk_page_to_workspace.py @@ -0,0 +1,22 @@ +import xhiveframework +from xhiveframework.model.rename_doc import rename_doc + + +def execute(): + if xhiveframework.db.exists("DocType", "Desk Page"): + if xhiveframework.db.exists("DocType", "Workspace"): + # this patch was not added initially, so this page might still exist + xhiveframework.delete_doc("DocType", "Desk Page") + else: + xhiveframework.flags.ignore_route_conflict_validation = True + rename_doc("DocType", "Desk Page", "Workspace") + xhiveframework.flags.ignore_route_conflict_validation = False + + rename_doc("DocType", "Desk Chart", "Workspace Chart", ignore_if_exists=True) + rename_doc("DocType", "Desk Shortcut", "Workspace Shortcut", ignore_if_exists=True) + rename_doc("DocType", "Desk Link", "Workspace Link", ignore_if_exists=True) + + xhiveframework.reload_doc("desk", "doctype", "workspace", force=True) + xhiveframework.reload_doc("desk", "doctype", "workspace_link", force=True) + xhiveframework.reload_doc("desk", "doctype", "workspace_chart", force=True) + xhiveframework.reload_doc("desk", "doctype", "workspace_shortcut", force=True) diff --git a/xhiveframework/patches/v13_0/rename_is_custom_field_in_dashboard_chart.py b/xhiveframework/patches/v13_0/rename_is_custom_field_in_dashboard_chart.py new file mode 100644 index 0000000..1faf120 --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_is_custom_field_in_dashboard_chart.py @@ -0,0 +1,12 @@ +import xhiveframework +from xhiveframework.model.utils.rename_field import rename_field + + +def execute(): + if not xhiveframework.db.table_exists("Dashboard Chart"): + return + + xhiveframework.reload_doc("desk", "doctype", "dashboard_chart") + + if xhiveframework.db.has_column("Dashboard Chart", "is_custom"): + rename_field("Dashboard Chart", "is_custom", "use_report_chart") diff --git a/xhiveframework/patches/v13_0/rename_list_view_setting_to_list_view_settings.py b/xhiveframework/patches/v13_0/rename_list_view_setting_to_list_view_settings.py new file mode 100644 index 0000000..253d8fb --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_list_view_setting_to_list_view_settings.py @@ -0,0 +1,29 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + if not xhiveframework.db.table_exists("List View Setting"): + return + if not xhiveframework.db.exists("DocType", "List View Setting"): + return + + xhiveframework.reload_doc("desk", "doctype", "List View Settings") + + existing_list_view_settings = xhiveframework.get_all("List View Settings", as_list=True, order_by="modified") + for list_view_setting in xhiveframework.get_all( + "List View Setting", + fields=["disable_count", "disable_sidebar_stats", "disable_auto_refresh", "name"], + order_by="modified", + ): + name = list_view_setting.pop("name") + if name not in [x[0] for x in existing_list_view_settings]: + list_view_setting["doctype"] = "List View Settings" + list_view_settings = xhiveframework.get_doc(list_view_setting) + # setting name here is necessary because autoname is set as prompt + list_view_settings.name = name + list_view_settings.insert() + + xhiveframework.delete_doc("DocType", "List View Setting", force=True) diff --git a/xhiveframework/patches/v13_0/rename_notification_fields.py b/xhiveframework/patches/v13_0/rename_notification_fields.py new file mode 100644 index 0000000..1c52efb --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_notification_fields.py @@ -0,0 +1,16 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.utils.rename_field import rename_field + + +def execute(): + """ + Change notification recipient fields from email to receiver fields + """ + xhiveframework.reload_doc("Email", "doctype", "Notification Recipient") + xhiveframework.reload_doc("Email", "doctype", "Notification") + + rename_field("Notification Recipient", "email_by_document_field", "receiver_by_document_field") + rename_field("Notification Recipient", "email_by_role", "receiver_by_role") diff --git a/xhiveframework/patches/v13_0/rename_onboarding.py b/xhiveframework/patches/v13_0/rename_onboarding.py new file mode 100644 index 0000000..15c0fbe --- /dev/null +++ b/xhiveframework/patches/v13_0/rename_onboarding.py @@ -0,0 +1,9 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + if xhiveframework.db.exists("DocType", "Onboarding"): + xhiveframework.rename_doc("DocType", "Onboarding", "Module Onboarding", ignore_if_exists=True) diff --git a/xhiveframework/patches/v13_0/replace_field_target_with_open_in_new_tab.py b/xhiveframework/patches/v13_0/replace_field_target_with_open_in_new_tab.py new file mode 100644 index 0000000..78615d4 --- /dev/null +++ b/xhiveframework/patches/v13_0/replace_field_target_with_open_in_new_tab.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + doctype = "Top Bar Item" + if not xhiveframework.db.table_exists(doctype) or not xhiveframework.db.has_column(doctype, "target"): + return + + xhiveframework.reload_doc("website", "doctype", "top_bar_item") + xhiveframework.db.set_value(doctype, {"target": 'target = "_blank"'}, "open_in_new_tab", 1) diff --git a/xhiveframework/patches/v13_0/replace_old_data_import.py b/xhiveframework/patches/v13_0/replace_old_data_import.py new file mode 100644 index 0000000..0257131 --- /dev/null +++ b/xhiveframework/patches/v13_0/replace_old_data_import.py @@ -0,0 +1,20 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + if not xhiveframework.db.table_exists("Data Import"): + return + + meta = xhiveframework.get_meta("Data Import") + # if Data Import is the new one, return early + if meta.fields[1].fieldname == "import_type": + return + + xhiveframework.db.sql("DROP TABLE IF EXISTS `tabData Import Legacy`") + xhiveframework.rename_doc("DocType", "Data Import", "Data Import Legacy") + xhiveframework.db.commit() + xhiveframework.db.sql("DROP TABLE IF EXISTS `tabData Import`") + xhiveframework.rename_doc("DocType", "Data Import Beta", "Data Import") diff --git a/xhiveframework/patches/v13_0/reset_corrupt_defaults.py b/xhiveframework/patches/v13_0/reset_corrupt_defaults.py new file mode 100644 index 0000000..0232a81 --- /dev/null +++ b/xhiveframework/patches/v13_0/reset_corrupt_defaults.py @@ -0,0 +1,33 @@ +import xhiveframework +from xhiveframework.patches.v13_0.encrypt_2fa_secrets import DOCTYPE +from xhiveframework.patches.v13_0.encrypt_2fa_secrets import PARENT_FOR_DEFAULTS as TWOFACTOR_PARENT +from xhiveframework.utils import cint + + +def execute(): + """ + This patch is needed to fix parent incorrectly set as `__2fa` because of + https://lab.membtech.com/xhiveframework/xhiveframework15/commit/a822092211533ff17ff9b92dd86f6f868ed63e2e + """ + + if not xhiveframework.db.get_value( + DOCTYPE, {"parent": TWOFACTOR_PARENT, "defkey": ("not like", "%_otp%")}, "defkey" + ): + return + + # system settings + system_settings = xhiveframework.get_single("System Settings") + system_settings.set_defaults() + + # home page + xhiveframework.db.set_default( + "desktop:home_page", "workspace" if cint(system_settings.setup_complete) else "setup-wizard" + ) + + # letter head + try: + letter_head = xhiveframework.get_doc("Letter Head", {"is_default": 1}) + letter_head.set_as_default() + + except xhiveframework.DoesNotExistError: + pass diff --git a/xhiveframework/patches/v13_0/set_existing_dashboard_charts_as_public.py b/xhiveframework/patches/v13_0/set_existing_dashboard_charts_as_public.py new file mode 100644 index 0000000..c56b341 --- /dev/null +++ b/xhiveframework/patches/v13_0/set_existing_dashboard_charts_as_public.py @@ -0,0 +1,21 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("desk", "doctype", "dashboard_chart") + + if not xhiveframework.db.table_exists("Dashboard Chart"): + return + + users_with_permission = xhiveframework.get_all( + "Has Role", + fields=["parent"], + filters={"role": ["in", ["System Manager", "Dashboard Manager"]], "parenttype": "User"}, + distinct=True, + ) + + users = [item.parent for item in users_with_permission] + charts = xhiveframework.get_all("Dashboard Chart", filters={"owner": ["in", users]}) + + for chart in charts: + xhiveframework.db.set_value("Dashboard Chart", chart.name, "is_public", 1) diff --git a/xhiveframework/patches/v13_0/set_first_day_of_the_week.py b/xhiveframework/patches/v13_0/set_first_day_of_the_week.py new file mode 100644 index 0000000..97b93b2 --- /dev/null +++ b/xhiveframework/patches/v13_0/set_first_day_of_the_week.py @@ -0,0 +1,8 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("System Settings") + # setting first_day_of_the_week value as "Monday" to avoid breaking change + # because before the configuration was introduced, system used to consider "Monday" as start of the week + xhiveframework.db.set_single_value("System Settings", "first_day_of_the_week", "Monday") diff --git a/xhiveframework/patches/v13_0/set_path_for_homepage_in_web_page_view.py b/xhiveframework/patches/v13_0/set_path_for_homepage_in_web_page_view.py new file mode 100644 index 0000000..6fe21ad --- /dev/null +++ b/xhiveframework/patches/v13_0/set_path_for_homepage_in_web_page_view.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "web_page_view", force=True) + xhiveframework.db.sql("""UPDATE `tabWeb Page View` set path='/' where path=''""") diff --git a/xhiveframework/patches/v13_0/set_read_times.py b/xhiveframework/patches/v13_0/set_read_times.py new file mode 100644 index 0000000..ca4a3df --- /dev/null +++ b/xhiveframework/patches/v13_0/set_read_times.py @@ -0,0 +1,21 @@ +from math import ceil + +import xhiveframework +from xhiveframework.utils import markdown, strip_html_tags + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "blog_post") + + for blog in xhiveframework.get_all("Blog Post"): + blog = xhiveframework.get_doc("Blog Post", blog.name) + xhiveframework.db.set_value("Blog Post", blog.name, "read_time", get_read_time(blog), update_modified=False) + + +def get_read_time(blog): + content = blog.content or blog.content_html + if blog.content_type == "Markdown": + content = markdown(blog.content_md) + + total_words = len(strip_html_tags(content or "").split()) + return ceil(total_words / 250) diff --git a/xhiveframework/patches/v13_0/set_route_for_blog_category.py b/xhiveframework/patches/v13_0/set_route_for_blog_category.py new file mode 100644 index 0000000..d72ca2c --- /dev/null +++ b/xhiveframework/patches/v13_0/set_route_for_blog_category.py @@ -0,0 +1,9 @@ +import xhiveframework + + +def execute(): + categories = xhiveframework.get_list("Blog Category") + for category in categories: + doc = xhiveframework.get_doc("Blog Category", category["name"]) + doc.set_route() + doc.save() diff --git a/xhiveframework/patches/v13_0/set_social_icons.py b/xhiveframework/patches/v13_0/set_social_icons.py new file mode 100644 index 0000000..0532c45 --- /dev/null +++ b/xhiveframework/patches/v13_0/set_social_icons.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + providers = xhiveframework.get_all("Social Login Key") + + for provider in providers: + doc = xhiveframework.get_doc("Social Login Key", provider) + doc.set_icon() + doc.save() diff --git a/xhiveframework/patches/v13_0/set_unique_for_page_view.py b/xhiveframework/patches/v13_0/set_unique_for_page_view.py new file mode 100644 index 0000000..6e741de --- /dev/null +++ b/xhiveframework/patches/v13_0/set_unique_for_page_view.py @@ -0,0 +1,7 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "web_page_view", force=True) + site_url = xhiveframework.utils.get_site_url(xhiveframework.local.site) + xhiveframework.db.sql(f"""UPDATE `tabWeb Page View` set is_unique=1 where referrer LIKE '%{site_url}%'""") diff --git a/xhiveframework/patches/v13_0/site_wise_logging.py b/xhiveframework/patches/v13_0/site_wise_logging.py new file mode 100644 index 0000000..d4d7ef2 --- /dev/null +++ b/xhiveframework/patches/v13_0/site_wise_logging.py @@ -0,0 +1,11 @@ +import os + +import xhiveframework + + +def execute(): + site = xhiveframework.local.site + + log_folder = os.path.join(site, "logs") + if not os.path.exists(log_folder): + os.mkdir(log_folder) diff --git a/xhiveframework/patches/v13_0/update_date_filters_in_user_settings.py b/xhiveframework/patches/v13_0/update_date_filters_in_user_settings.py new file mode 100644 index 0000000..db476eb --- /dev/null +++ b/xhiveframework/patches/v13_0/update_date_filters_in_user_settings.py @@ -0,0 +1,54 @@ +import json + +import xhiveframework +from xhiveframework.model.utils.user_settings import sync_user_settings, update_user_settings + + +def execute(): + users = xhiveframework.db.sql("select distinct(user) from `__UserSettings`", as_dict=True) + + for user in users: + user_settings = xhiveframework.db.sql( + f""" + select + * from `__UserSettings` + where + user='{user.user}' + """, + as_dict=True, + ) + + for setting in user_settings: + data = xhiveframework.parse_json(setting.get("data")) + if data: + for key in data: + update_user_setting_filters(data, key, setting) + + sync_user_settings() + + +def update_user_setting_filters(data, key, user_setting): + timespan_map = { + "1 week": "week", + "1 month": "month", + "3 months": "quarter", + "6 months": "6 months", + "1 year": "year", + } + + period_map = {"Previous": "last", "Next": "next"} + + if data.get(key): + update = False + if isinstance(data.get(key), dict): + filters = data.get(key).get("filters") + if filters and isinstance(filters, list): + for f in filters: + if f[2] == "Next" or f[2] == "Previous": + update = True + f[3] = period_map[f[2]] + " " + timespan_map[f[3]] + f[2] = "Timespan" + + if update: + data[key]["filters"] = filters + update_user_settings(user_setting["doctype"], json.dumps(data), for_update=True) diff --git a/xhiveframework/patches/v13_0/update_duration_options.py b/xhiveframework/patches/v13_0/update_duration_options.py new file mode 100644 index 0000000..fc39a09 --- /dev/null +++ b/xhiveframework/patches/v13_0/update_duration_options.py @@ -0,0 +1,32 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("core", "doctype", "DocField") + + if xhiveframework.db.has_column("DocField", "show_days"): + xhiveframework.db.sql( + """ + UPDATE + tabDocField + SET + hide_days = 1 WHERE show_days = 0 + """ + ) + xhiveframework.db.sql_ddl("alter table tabDocField drop column show_days") + + if xhiveframework.db.has_column("DocField", "show_seconds"): + xhiveframework.db.sql( + """ + UPDATE + tabDocField + SET + hide_seconds = 1 WHERE show_seconds = 0 + """ + ) + xhiveframework.db.sql_ddl("alter table tabDocField drop column show_seconds") + + xhiveframework.clear_cache(doctype="DocField") diff --git a/xhiveframework/patches/v13_0/update_icons_in_customized_desk_pages.py b/xhiveframework/patches/v13_0/update_icons_in_customized_desk_pages.py new file mode 100644 index 0000000..5522189 --- /dev/null +++ b/xhiveframework/patches/v13_0/update_icons_in_customized_desk_pages.py @@ -0,0 +1,18 @@ +import xhiveframework + + +def execute(): + if not xhiveframework.db.exists("Desk Page"): + return + + pages = xhiveframework.get_all( + "Desk Page", filters={"is_standard": False}, fields=["name", "extends", "for_user"] + ) + default_icon = {} + for page in pages: + if page.extends and page.for_user: + if not default_icon.get(page.extends): + default_icon[page.extends] = xhiveframework.db.get_value("Desk Page", page.extends, "icon") + + icon = default_icon.get(page.extends) + xhiveframework.db.set_value("Desk Page", page.name, "icon", icon) diff --git a/xhiveframework/patches/v13_0/update_newsletter_content_type.py b/xhiveframework/patches/v13_0/update_newsletter_content_type.py new file mode 100644 index 0000000..64784e6 --- /dev/null +++ b/xhiveframework/patches/v13_0/update_newsletter_content_type.py @@ -0,0 +1,14 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("email", "doctype", "Newsletter") + xhiveframework.db.sql( + """ + UPDATE tabNewsletter + SET content_type = 'Rich Text' + """ + ) diff --git a/xhiveframework/patches/v13_0/update_notification_channel_if_empty.py b/xhiveframework/patches/v13_0/update_notification_channel_if_empty.py new file mode 100644 index 0000000..9c778ac --- /dev/null +++ b/xhiveframework/patches/v13_0/update_notification_channel_if_empty.py @@ -0,0 +1,14 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("Email", "doctype", "Notification") + + notifications = xhiveframework.get_all("Notification", {"is_standard": 1}, {"name", "channel"}) + for notification in notifications: + if not notification.channel: + xhiveframework.db.set_value("Notification", notification.name, "channel", "Email", update_modified=False) + xhiveframework.db.commit() diff --git a/xhiveframework/patches/v13_0/web_template_set_module.py b/xhiveframework/patches/v13_0/web_template_set_module.py new file mode 100644 index 0000000..d77c585 --- /dev/null +++ b/xhiveframework/patches/v13_0/web_template_set_module.py @@ -0,0 +1,17 @@ +# Copyright (c) 2020, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework + + +def execute(): + """Set default module for standard Web Template, if none.""" + xhiveframework.reload_doc("website", "doctype", "Web Template Field") + xhiveframework.reload_doc("website", "doctype", "web_template") + + standard_templates = xhiveframework.get_list("Web Template", {"standard": 1}) + for template in standard_templates: + doc = xhiveframework.get_doc("Web Template", template.name) + if not doc.module: + doc.module = "Website" + doc.save() diff --git a/xhiveframework/patches/v13_0/website_theme_custom_scss.py b/xhiveframework/patches/v13_0/website_theme_custom_scss.py new file mode 100644 index 0000000..a837843 --- /dev/null +++ b/xhiveframework/patches/v13_0/website_theme_custom_scss.py @@ -0,0 +1,38 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "website_theme_ignore_app") + xhiveframework.reload_doc("website", "doctype", "color") + xhiveframework.reload_doc("website", "doctype", "website_theme", force=True) + + for theme in xhiveframework.get_all("Website Theme"): + doc = xhiveframework.get_doc("Website Theme", theme.name) + setup_color_record(doc) + if not doc.get("custom_scss") and doc.theme_scss: + # move old theme to new theme + doc.custom_scss = doc.theme_scss + doc.save() + + +def setup_color_record(doc): + color_fields = [ + "primary_color", + "text_color", + "light_color", + "dark_color", + "background_color", + ] + + for color_field in color_fields: + color_code = doc.get(color_field) + if not color_code or xhiveframework.db.exists("Color", color_code): + continue + + xhiveframework.get_doc( + { + "doctype": "Color", + "__newname": color_code, + "color": color_code, + } + ).insert() diff --git a/xhiveframework/patches/v14_0/__init__.py b/xhiveframework/patches/v14_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/patches/v14_0/clear_long_pending_stale_logs.py b/xhiveframework/patches/v14_0/clear_long_pending_stale_logs.py new file mode 100644 index 0000000..6147a17 --- /dev/null +++ b/xhiveframework/patches/v14_0/clear_long_pending_stale_logs.py @@ -0,0 +1,40 @@ +import xhiveframework +from xhiveframework.core.doctype.log_settings.log_settings import clear_log_table +from xhiveframework.utils import add_to_date, today + + +def execute(): + """Due to large size of log tables on old sites some table cleanups never finished during daily log clean up. This patch discards such data by using "big delete" code. + + ref: https://lab.membtech.com/xhiveframework/xhiveframework15/issues/16971 + """ + + DOCTYPE_RETENTION_MAP = { + "Error Log": get_current_setting("clear_error_log_after") or 90, + "Activity Log": get_current_setting("clear_activity_log_after") or 90, + "Email Queue": get_current_setting("clear_email_queue_after") or 30, + # child table on email queue + "Email Queue Recipient": get_current_setting("clear_email_queue_after") or 30, + # newly added + "Scheduled Job Log": 90, + } + + for doctype, retention in DOCTYPE_RETENTION_MAP.items(): + if is_log_cleanup_stuck(doctype, retention): + print(f"Clearing old {doctype} records") + clear_log_table(doctype, retention) + + +def is_log_cleanup_stuck(doctype: str, retention: int) -> bool: + """Check if doctype has data significantly older than configured cleanup period""" + threshold = add_to_date(today(), days=retention * -2) + + return bool(xhiveframework.db.exists(doctype, {"modified": ("<", threshold)})) + + +def get_current_setting(fieldname): + try: + return xhiveframework.db.get_single_value("Log Settings", fieldname) + except Exception: + # Field might be gone if patch is reattempted + pass diff --git a/xhiveframework/patches/v14_0/copy_mail_data.py b/xhiveframework/patches/v14_0/copy_mail_data.py new file mode 100644 index 0000000..0d706c8 --- /dev/null +++ b/xhiveframework/patches/v14_0/copy_mail_data.py @@ -0,0 +1,23 @@ +import xhiveframework + + +def execute(): + # patch for all Email Account with the flag use_imap + for email_account in xhiveframework.get_list("Email Account", filters={"enable_incoming": 1, "use_imap": 1}): + # get all data from Email Account + doc = xhiveframework.get_doc("Email Account", email_account.name) + + imap_list = [folder.folder_name for folder in doc.imap_folder] + # and append the old data to the child table + if doc.uidvalidity or doc.uidnext and "INBOX" not in imap_list: + doc.append( + "imap_folder", + { + "folder_name": "INBOX", + "append_to": doc.append_to, + "uid_validity": doc.uidvalidity, + "uidnext": doc.uidnext, + }, + ) + + doc.save() diff --git a/xhiveframework/patches/v14_0/delete_data_migration_tool.py b/xhiveframework/patches/v14_0/delete_data_migration_tool.py new file mode 100644 index 0000000..0ec6560 --- /dev/null +++ b/xhiveframework/patches/v14_0/delete_data_migration_tool.py @@ -0,0 +1,12 @@ +# Copyright (c) 2022, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +import xhiveframework + + +def execute(): + doctypes = xhiveframework.get_all("DocType", {"module": "Data Migration", "custom": 0}, pluck="name") + for doctype in doctypes: + xhiveframework.delete_doc("DocType", doctype, ignore_missing=True) + + xhiveframework.delete_doc("Module Def", "Data Migration", ignore_missing=True, force=True) diff --git a/xhiveframework/patches/v14_0/delete_payment_gateways.py b/xhiveframework/patches/v14_0/delete_payment_gateways.py new file mode 100644 index 0000000..ddb5d1d --- /dev/null +++ b/xhiveframework/patches/v14_0/delete_payment_gateways.py @@ -0,0 +1,16 @@ +import xhiveframework + + +def execute(): + if "payments" in xhiveframework.get_installed_apps(): + return + + for doctype in ( + "Payment Gateway", + "Razorpay Settings", + "Braintree Settings", + "PayPal Settings", + "Paytm Settings", + "Stripe Settings", + ): + xhiveframework.delete_doc_if_exists("DocType", doctype, force=True) diff --git a/xhiveframework/patches/v14_0/different_encryption_key.py b/xhiveframework/patches/v14_0/different_encryption_key.py new file mode 100644 index 0000000..4739767 --- /dev/null +++ b/xhiveframework/patches/v14_0/different_encryption_key.py @@ -0,0 +1,16 @@ +import pathlib + +import xhiveframework +from xhiveframework.installer import update_site_config +from xhiveframework.utils.backups import BACKUP_ENCRYPTION_CONFIG_KEY, get_backup_path + + +def execute(): + if xhiveframework.conf.get(BACKUP_ENCRYPTION_CONFIG_KEY): + return + + backup_path = pathlib.Path(get_backup_path()) + encrypted_backups_present = bool(list(backup_path.glob("*-enc*"))) + + if encrypted_backups_present: + update_site_config(BACKUP_ENCRYPTION_CONFIG_KEY, xhiveframework.local.conf.encryption_key) diff --git a/xhiveframework/patches/v14_0/disable_email_accounts_with_oauth.py b/xhiveframework/patches/v14_0/disable_email_accounts_with_oauth.py new file mode 100644 index 0000000..d9cda89 --- /dev/null +++ b/xhiveframework/patches/v14_0/disable_email_accounts_with_oauth.py @@ -0,0 +1,36 @@ +import xhiveframework +from xhiveframework.desk.doctype.notification_log.notification_log import make_notification_logs + + +def execute(): + if xhiveframework.get_all("Email Account", {"auth_method": "OAuth", "connected_user": ["is", "set"]}, limit=1): + return + + # Setting awaiting password to 1 for email accounts where Oauth is enabled. + # This is done so that people can resetup their email accounts with connected app mechanism. + xhiveframework.db.set_value("Email Account", {"auth_method": "OAuth"}, "awaiting_password", 1) + + message = "Email Accounts with auth method as OAuth have been disabled.\ + Please re-setup your OAuth based email accounts with the connected app mechanism to re-enable them." + + if sysmanagers := get_system_managers(): + make_notification_logs( + { + "type": "Alert", + "subject": xhiveframework._(message), + }, + sysmanagers, + ) + + +def get_system_managers(): + user_doctype = xhiveframework.qb.DocType("User").as_("user") + user_role_doctype = xhiveframework.qb.DocType("Has Role").as_("user_role") + return ( + xhiveframework.qb.from_(user_doctype) + .from_(user_role_doctype) + .select(user_doctype.email) + .where(user_role_doctype.role == "System Manager") + .where(user_doctype.enabled == 1) + .where(user_role_doctype.parent == user_doctype.name) + ).run(pluck=True) diff --git a/xhiveframework/patches/v14_0/drop_data_import_legacy.py b/xhiveframework/patches/v14_0/drop_data_import_legacy.py new file mode 100644 index 0000000..99e1aa2 --- /dev/null +++ b/xhiveframework/patches/v14_0/drop_data_import_legacy.py @@ -0,0 +1,23 @@ +import click + +import xhiveframework + + +def execute(): + doctype = "Data Import Legacy" + table = xhiveframework.utils.get_table_name(doctype) + + # delete the doctype record to avoid broken links + xhiveframework.delete_doc("DocType", doctype, force=True) + + # leaving table in database for manual cleanup + click.secho( + f"`{doctype}` has been deprecated. The DocType is deleted, but the data still" + " exists on the database. If this data is worth recovering, you may export it" + f" using\n\n\tbench --site {xhiveframework.local.site} backup -i '{doctype}'\n\nAfter" + " this, the table will continue to persist in the database, until you choose" + " to remove it yourself. If you want to drop the table, you may run\n\n\tbench" + f" --site {xhiveframework.local.site} execute xhiveframework.db.sql --args \"('DROP TABLE IF" + f" EXISTS `{table}`', )\"\n", + fg="yellow", + ) diff --git a/xhiveframework/patches/v14_0/drop_unused_indexes.py b/xhiveframework/patches/v14_0/drop_unused_indexes.py new file mode 100644 index 0000000..5c3d8f6 --- /dev/null +++ b/xhiveframework/patches/v14_0/drop_unused_indexes.py @@ -0,0 +1,56 @@ +""" +This patch just drops some known indexes which aren't being used anymore or never were used. +""" + +import click + +import xhiveframework + +UNUSED_INDEXES = [ + ("Comment", ["link_doctype", "link_name"]), + ("Activity Log", ["link_doctype", "link_name"]), +] + + +def execute(): + if xhiveframework.db.db_type == "postgres": + return + + db_tables = xhiveframework.db.get_tables(cached=False) + + # All parent indexes + parent_doctypes = xhiveframework.get_all( + "DocType", + {"istable": 0, "is_virtual": 0, "issingle": 0}, + pluck="name", + ) + db_tables = xhiveframework.db.get_tables(cached=False) + + for doctype in parent_doctypes: + table = f"tab{doctype}" + if table not in db_tables: + continue + drop_index_if_exists(table, "parent") + + # Unused composite indexes + for doctype, index_fields in UNUSED_INDEXES: + table = f"tab{doctype}" + index_name = xhiveframework.db.get_index_name(index_fields) + if table not in db_tables: + continue + drop_index_if_exists(table, index_name) + + +def drop_index_if_exists(table: str, index: str): + if not xhiveframework.db.has_index(table, index): + click.echo(f"- Skipped {index} index for {table} because it doesn't exist") + return + + try: + xhiveframework.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`") + except Exception as e: + xhiveframework.log_error("Failed to drop index") + click.secho(f"x Failed to drop index {index} from {table}\n {e!s}", fg="red") + return + + click.echo(f"✓ dropped {index} index from {table}") diff --git a/xhiveframework/patches/v14_0/log_settings_migration.py b/xhiveframework/patches/v14_0/log_settings_migration.py new file mode 100644 index 0000000..280eb7e --- /dev/null +++ b/xhiveframework/patches/v14_0/log_settings_migration.py @@ -0,0 +1,29 @@ +import xhiveframework + + +def execute(): + old_settings = { + "Error Log": get_current_setting("clear_error_log_after"), + "Activity Log": get_current_setting("clear_activity_log_after"), + "Email Queue": get_current_setting("clear_email_queue_after"), + } + + xhiveframework.reload_doc("core", "doctype", "Logs To Clear") + xhiveframework.reload_doc("core", "doctype", "Log Settings") + + log_settings = xhiveframework.get_doc("Log Settings") + log_settings.add_default_logtypes() + + for doctype, retention in old_settings.items(): + if retention: + log_settings.register_doctype(doctype, retention) + + log_settings.save() + + +def get_current_setting(fieldname): + try: + return xhiveframework.db.get_single_value("Log Settings", fieldname) + except Exception: + # Field might be gone if patch is reattempted + pass diff --git a/xhiveframework/patches/v14_0/modify_value_column_size_for_singles.py b/xhiveframework/patches/v14_0/modify_value_column_size_for_singles.py new file mode 100644 index 0000000..f018d86 --- /dev/null +++ b/xhiveframework/patches/v14_0/modify_value_column_size_for_singles.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + if xhiveframework.db.db_type == "mariadb": + xhiveframework.db.sql_ddl("alter table `tabSingles` modify column `value` longtext") diff --git a/xhiveframework/patches/v14_0/remove_db_aggregation.py b/xhiveframework/patches/v14_0/remove_db_aggregation.py new file mode 100644 index 0000000..bb0c6b8 --- /dev/null +++ b/xhiveframework/patches/v14_0/remove_db_aggregation.py @@ -0,0 +1,35 @@ +import re + +import xhiveframework +from xhiveframework.query_builder import DocType + + +def execute(): + """Replace temporarily available Database Aggregate APIs on xhiveframework (develop) + + APIs changed: + * xhiveframework.db.max => xhiveframework.qb.max + * xhiveframework.db.min => xhiveframework.qb.min + * xhiveframework.db.sum => xhiveframework.qb.sum + * xhiveframework.db.avg => xhiveframework.qb.avg + """ + ServerScript = DocType("Server Script") + server_scripts = ( + xhiveframework.qb.from_(ServerScript) + .where( + ServerScript.script.like("%xhiveframework.db.max(%") + | ServerScript.script.like("%xhiveframework.db.min(%") + | ServerScript.script.like("%xhiveframework.db.sum(%") + | ServerScript.script.like("%xhiveframework.db.avg(%") + ) + .select("name", "script") + .run(as_dict=True) + ) + + for server_script in server_scripts: + name, script = server_script["name"], server_script["script"] + + for agg in ["avg", "max", "min", "sum"]: + script = re.sub(f"xhiveframework.db.{agg}\\(", f"xhiveframework.qb.{agg}(", script) + + xhiveframework.db.set_value("Server Script", name, "script", script) diff --git a/xhiveframework/patches/v14_0/remove_is_first_startup.py b/xhiveframework/patches/v14_0/remove_is_first_startup.py new file mode 100644 index 0000000..1e7b656 --- /dev/null +++ b/xhiveframework/patches/v14_0/remove_is_first_startup.py @@ -0,0 +1,8 @@ +import xhiveframework + + +def execute(): + singles = xhiveframework.qb.Table("tabSingles") + xhiveframework.qb.from_(singles).delete().where( + (singles.doctype == "System Settings") & (singles.field == "is_first_startup") + ).run() diff --git a/xhiveframework/patches/v14_0/remove_manage_subscriptions_from_navbar.py b/xhiveframework/patches/v14_0/remove_manage_subscriptions_from_navbar.py new file mode 100644 index 0000000..94b5504 --- /dev/null +++ b/xhiveframework/patches/v14_0/remove_manage_subscriptions_from_navbar.py @@ -0,0 +1,10 @@ +import xhiveframework + + +def execute(): + navbar_settings = xhiveframework.get_single("Navbar Settings") + for i, l in enumerate(navbar_settings.settings_dropdown): + if l.item_label == "Manage Subscriptions": + navbar_settings.settings_dropdown.pop(i) + navbar_settings.save() + break diff --git a/xhiveframework/patches/v14_0/remove_post_and_post_comment.py b/xhiveframework/patches/v14_0/remove_post_and_post_comment.py new file mode 100644 index 0000000..3ab1f1e --- /dev/null +++ b/xhiveframework/patches/v14_0/remove_post_and_post_comment.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + xhiveframework.delete_doc_if_exists("DocType", "Post") + xhiveframework.delete_doc_if_exists("DocType", "Post Comment") diff --git a/xhiveframework/patches/v14_0/reset_creation_datetime.py b/xhiveframework/patches/v14_0/reset_creation_datetime.py new file mode 100644 index 0000000..dc8220b --- /dev/null +++ b/xhiveframework/patches/v14_0/reset_creation_datetime.py @@ -0,0 +1,38 @@ +import glob +import json +import os + +import xhiveframework +from xhiveframework.query_builder import DocType as _DocType + + +def execute(): + """Resetting creation datetimes for DocTypes""" + DocType = _DocType("DocType") + doctype_jsons = glob.glob(os.path.join("..", "apps", "xhiveframework", "xhiveframework", "**", "doctype", "**", "*.json")) + + xhiveframework_modules = xhiveframework.get_all("Module Def", filters={"app_name": "xhiveframework"}, pluck="name") + site_doctypes = xhiveframework.get_all( + "DocType", + filters={"module": ("in", xhiveframework_modules), "custom": False}, + fields=["name", "creation"], + ) + + for dt_path in doctype_jsons: + with open(dt_path) as f: + try: + file_schema = xhiveframework._dict(json.load(f)) + except Exception: + continue + + if not file_schema.name: + continue + + _site_schema = [x for x in site_doctypes if x.name == file_schema.name] + if not _site_schema: + continue + + if file_schema.creation != _site_schema[0].creation: + xhiveframework.qb.update(DocType).set(DocType.creation, file_schema.creation).where( + DocType.name == file_schema.name + ).run() diff --git a/xhiveframework/patches/v14_0/save_ratings_in_fraction.py b/xhiveframework/patches/v14_0/save_ratings_in_fraction.py new file mode 100644 index 0000000..59b45f4 --- /dev/null +++ b/xhiveframework/patches/v14_0/save_ratings_in_fraction.py @@ -0,0 +1,37 @@ +import xhiveframework +from xhiveframework.query_builder import DocType + + +def execute(): + RATING_FIELD_TYPE = "decimal(3,2)" + rating_fields = xhiveframework.get_all( + "DocField", fields=["parent", "fieldname"], filters={"fieldtype": "Rating"} + ) + + custom_rating_fields = xhiveframework.get_all( + "Custom Field", fields=["dt", "fieldname"], filters={"fieldtype": "Rating"} + ) + + for _field in rating_fields + custom_rating_fields: + doctype_name = _field.get("parent") or _field.get("dt") + doctype = DocType(doctype_name) + field = _field.fieldname + + # TODO: Add postgres support (for the check) + if ( + xhiveframework.conf.db_type == "mariadb" + and xhiveframework.db.get_column_type(doctype_name, field) == RATING_FIELD_TYPE + ): + continue + + # commit any changes so far for upcoming DDL + xhiveframework.db.commit() + + # alter column types for rating fieldtype + xhiveframework.db.change_column_type(doctype_name, column=field, type=RATING_FIELD_TYPE, nullable=True) + + # update data: int => decimal + xhiveframework.qb.update(doctype).set(doctype[field], doctype[field] / 5).run() + + # commit to flush updated rows + xhiveframework.db.commit() diff --git a/xhiveframework/patches/v14_0/set_document_expiry_default.py b/xhiveframework/patches/v14_0/set_document_expiry_default.py new file mode 100644 index 0000000..55eff85 --- /dev/null +++ b/xhiveframework/patches/v14_0/set_document_expiry_default.py @@ -0,0 +1,8 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.set_single_value( + "System Settings", + {"document_share_key_expiry": 30, "allow_older_web_view_links": 1}, + ) diff --git a/xhiveframework/patches/v14_0/set_suspend_email_queue_default.py b/xhiveframework/patches/v14_0/set_suspend_email_queue_default.py new file mode 100644 index 0000000..e6bfc58 --- /dev/null +++ b/xhiveframework/patches/v14_0/set_suspend_email_queue_default.py @@ -0,0 +1,13 @@ +import xhiveframework +from xhiveframework.cache_manager import clear_defaults_cache + + +def execute(): + xhiveframework.db.set_default( + "suspend_email_queue", + xhiveframework.db.get_default("hold_queue", "Administrator") or 0, + parent="__default", + ) + + xhiveframework.db.delete("DefaultValue", {"defkey": "hold_queue"}) + clear_defaults_cache() diff --git a/xhiveframework/patches/v14_0/setup_likes_from_feedback.py b/xhiveframework/patches/v14_0/setup_likes_from_feedback.py new file mode 100644 index 0000000..28ab602 --- /dev/null +++ b/xhiveframework/patches/v14_0/setup_likes_from_feedback.py @@ -0,0 +1,32 @@ +import xhiveframework + + +def execute(): + xhiveframework.reload_doctype("Comment") + + if xhiveframework.db.count("Feedback") > 20000: + xhiveframework.db.auto_commit_on_many_writes = True + + for feedback in xhiveframework.get_all("Feedback", fields=["*"]): + if feedback.like: + new_comment = xhiveframework.new_doc("Comment") + new_comment.comment_type = "Like" + new_comment.comment_email = feedback.owner + new_comment.content = "Liked by: " + feedback.owner + new_comment.reference_doctype = feedback.reference_doctype + new_comment.reference_name = feedback.reference_name + new_comment.creation = feedback.creation + new_comment.modified = feedback.modified + new_comment.owner = feedback.owner + new_comment.modified_by = feedback.modified_by + new_comment.ip_address = feedback.ip_address + new_comment.db_insert() + + if xhiveframework.db.auto_commit_on_many_writes: + xhiveframework.db.auto_commit_on_many_writes = False + + # clean up + xhiveframework.db.delete("Feedback") + xhiveframework.db.commit() + + xhiveframework.delete_doc("DocType", "Feedback") diff --git a/xhiveframework/patches/v14_0/transform_todo_schema.py b/xhiveframework/patches/v14_0/transform_todo_schema.py new file mode 100644 index 0000000..f06ac7a --- /dev/null +++ b/xhiveframework/patches/v14_0/transform_todo_schema.py @@ -0,0 +1,12 @@ +import xhiveframework +from xhiveframework.query_builder.utils import DocType + + +def execute(): + # Email Template & Help Article have owner field that doesn't have any additional functionality + # Only ToDo has to be updated. + + ToDo = DocType("ToDo") + xhiveframework.reload_doctype("ToDo", force=True) + + xhiveframework.qb.update(ToDo).set(ToDo.allocated_to, ToDo.owner).run() diff --git a/xhiveframework/patches/v14_0/update_attachment_comment.py b/xhiveframework/patches/v14_0/update_attachment_comment.py new file mode 100644 index 0000000..f76a695 --- /dev/null +++ b/xhiveframework/patches/v14_0/update_attachment_comment.py @@ -0,0 +1,33 @@ +import xhiveframework + + +def execute(): + xhiveframework.db.auto_commit_on_many_writes = 1 + + # Strip everything except link to attachment and icon from comments of type "Attached" + for name, content in xhiveframework.get_all( + "Comment", filters={"comment_type": "Attachment"}, fields=["name", "content"], as_list=True + ): + if not content: + continue + + start = content.find("") + end = content.find("") if end == -1 else end + if end != -1: + content = content[: end + 4] + + xhiveframework.db.set_value("Comment", name, "content", content, update_modified=False) + + # Strip "Removed " from comments of type "Attachment Removed" + for name, content in xhiveframework.get_all( + "Comment", + filters={"comment_type": "Attachment Removed"}, + fields=["name", "content"], + as_list=True, + ): + if content and content.startswith("Removed "): + xhiveframework.db.set_value("Comment", name, "content", content[8:], update_modified=False) diff --git a/xhiveframework/patches/v14_0/update_auto_account_deletion_duration.py b/xhiveframework/patches/v14_0/update_auto_account_deletion_duration.py new file mode 100644 index 0000000..b5049ed --- /dev/null +++ b/xhiveframework/patches/v14_0/update_auto_account_deletion_duration.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + days = xhiveframework.db.get_single_value("Website Settings", "auto_account_deletion") + xhiveframework.db.set_single_value("Website Settings", "auto_account_deletion", days * 24) diff --git a/xhiveframework/patches/v14_0/update_color_names_in_kanban_board_column.py b/xhiveframework/patches/v14_0/update_color_names_in_kanban_board_column.py new file mode 100644 index 0000000..706fefd --- /dev/null +++ b/xhiveframework/patches/v14_0/update_color_names_in_kanban_board_column.py @@ -0,0 +1,22 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + + +import xhiveframework + + +def execute(): + indicator_map = { + "blue": "Blue", + "orange": "Orange", + "red": "Red", + "green": "Green", + "darkgrey": "Gray", + "gray": "Gray", + "purple": "Purple", + "yellow": "Yellow", + "lightblue": "Light Blue", + } + for d in xhiveframework.get_all("Kanban Board Column", fields=["name", "indicator"]): + color_name = indicator_map.get(d.indicator, "Gray") + xhiveframework.db.set_value("Kanban Board Column", d.name, "indicator", color_name) diff --git a/xhiveframework/patches/v14_0/update_github_endpoints.py b/xhiveframework/patches/v14_0/update_github_endpoints.py new file mode 100644 index 0000000..20fa9d9 --- /dev/null +++ b/xhiveframework/patches/v14_0/update_github_endpoints.py @@ -0,0 +1,10 @@ +import json + +import xhiveframework + + +def execute(): + if xhiveframework.db.exists("Social Login Key", "github"): + xhiveframework.db.set_value( + "Social Login Key", "github", "auth_url_data", json.dumps({"scope": "user:email"}) + ) diff --git a/xhiveframework/patches/v14_0/update_integration_request.py b/xhiveframework/patches/v14_0/update_integration_request.py new file mode 100644 index 0000000..cdafc4c --- /dev/null +++ b/xhiveframework/patches/v14_0/update_integration_request.py @@ -0,0 +1,21 @@ +import xhiveframework + + +def execute(): + doctype = "Integration Request" + + if not xhiveframework.db.has_column(doctype, "integration_type"): + return + + xhiveframework.db.set_value( + doctype, + {"integration_type": "Remote", "integration_request_service": ("!=", "PayPal")}, + "is_remote_request", + 1, + ) + xhiveframework.db.set_value( + doctype, + {"integration_type": "Subscription Notification"}, + "request_description", + "Subscription Notification", + ) diff --git a/xhiveframework/patches/v14_0/update_is_system_generated_flag.py b/xhiveframework/patches/v14_0/update_is_system_generated_flag.py new file mode 100644 index 0000000..05b0a54 --- /dev/null +++ b/xhiveframework/patches/v14_0/update_is_system_generated_flag.py @@ -0,0 +1,20 @@ +import xhiveframework + + +def execute(): + # assuming all customization generated by Admin is system generated customization + custom_field = xhiveframework.qb.DocType("Custom Field") + ( + xhiveframework.qb.update(custom_field) + .set(custom_field.is_system_generated, True) + .where(custom_field.owner == "Administrator") + .run() + ) + + property_setter = xhiveframework.qb.DocType("Property Setter") + ( + xhiveframework.qb.update(property_setter) + .set(property_setter.is_system_generated, True) + .where(property_setter.owner == "Administrator") + .run() + ) diff --git a/xhiveframework/patches/v14_0/update_multistep_webforms.py b/xhiveframework/patches/v14_0/update_multistep_webforms.py new file mode 100644 index 0000000..65fe748 --- /dev/null +++ b/xhiveframework/patches/v14_0/update_multistep_webforms.py @@ -0,0 +1,12 @@ +import xhiveframework + + +def execute(): + if not xhiveframework.db.has_column("Web Form", "is_multi_step_form"): + return + + for web_form in xhiveframework.get_all("Web Form", filters={"is_multi_step_form": 1}): + web_form_fields = xhiveframework.get_doc("Web Form", web_form.name).web_form_fields + for web_form_field in web_form_fields: + if web_form_field.fieldtype == "Section Break" and web_form_field.idx != 1: + xhiveframework.db.set_value("Web Form Field", web_form_field.name, "fieldtype", "Page Break") diff --git a/xhiveframework/patches/v14_0/update_webforms.py b/xhiveframework/patches/v14_0/update_webforms.py new file mode 100644 index 0000000..0a15c32 --- /dev/null +++ b/xhiveframework/patches/v14_0/update_webforms.py @@ -0,0 +1,14 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + + +import xhiveframework + + +def execute(): + xhiveframework.reload_doc("website", "doctype", "web_form_list_column") + xhiveframework.reload_doctype("Web Form") + + for web_form in xhiveframework.get_all("Web Form", fields=["*"]): + if web_form.allow_multiple and not web_form.show_list: + xhiveframework.db.set_value("Web Form", web_form.name, "show_list", True) diff --git a/xhiveframework/patches/v14_0/update_workspace2.py b/xhiveframework/patches/v14_0/update_workspace2.py new file mode 100644 index 0000000..cc30afb --- /dev/null +++ b/xhiveframework/patches/v14_0/update_workspace2.py @@ -0,0 +1,85 @@ +import json + +import xhiveframework +from xhiveframework import _ + + +def execute(): + for seq, workspace in enumerate(xhiveframework.get_all("Workspace")): + doc = xhiveframework.get_doc("Workspace", workspace.name) + content = create_content(doc) + update_workspace(doc, seq, content) + + +def create_content(doc): + content = [] + if doc.get("onboarding"): + content.append({"type": "onboarding", "data": {"onboarding_name": doc.onboarding, "col": 12}}) + if doc.charts: + invalid_links = [] + for c in doc.charts: + if c.get_invalid_links()[0]: + invalid_links.append(c) + else: + content.append({"type": "chart", "data": {"chart_name": c.label, "col": 12}}) + for l in invalid_links: + del doc.charts[doc.charts.index(l)] + if doc.shortcuts: + invalid_links = [] + if doc.charts: + content.append({"type": "spacer", "data": {"col": 12}}) + content.append( + { + "type": "header", + "data": {"text": doc.get("shortcuts_label") or _("Your Shortcuts"), "level": 4, "col": 12}, + } + ) + for s in doc.shortcuts: + if s.get_invalid_links()[0]: + invalid_links.append(s) + else: + content.append({"type": "shortcut", "data": {"shortcut_name": s.label, "col": 4}}) + for l in invalid_links: + del doc.shortcuts[doc.shortcuts.index(l)] + if doc.links: + invalid_links = [] + content.append({"type": "spacer", "data": {"col": 12}}) + content.append( + { + "type": "header", + "data": {"text": doc.get("cards_label") or _("Reports & Masters"), "level": 4, "col": 12}, + } + ) + for l in doc.links: + if l.type == "Card Break": + content.append({"type": "card", "data": {"card_name": l.label, "col": 4}}) + if l.get_invalid_links()[0]: + invalid_links.append(l) + for l in invalid_links: + del doc.links[doc.links.index(l)] + return content + + +def update_workspace(doc, seq, content): + if ( + not doc.title + and (not doc.content or doc.content == "[]") + and not doc.get("is_standard") + and not doc.public + ): + doc.sequence_id = seq + 1 + doc.content = json.dumps(content) + doc.public = 0 if doc.for_user else 1 + doc.title = doc.get("extends") or doc.get("label") + doc.extends = "" + doc.category = "" + doc.onboarding = "" + doc.extends_another_page = 0 + doc.is_default = 0 + doc.is_standard = 0 + doc.developer_mode_only = 0 + doc.disable_user_customization = 0 + doc.pin_to_top = 0 + doc.pin_to_bottom = 0 + doc.hide_custom = 0 + doc.save(ignore_permissions=True) diff --git a/xhiveframework/patches/v15_0/copy_disable_prepared_report_to_prepared_report.py b/xhiveframework/patches/v15_0/copy_disable_prepared_report_to_prepared_report.py new file mode 100644 index 0000000..536f861 --- /dev/null +++ b/xhiveframework/patches/v15_0/copy_disable_prepared_report_to_prepared_report.py @@ -0,0 +1,6 @@ +import xhiveframework + + +def execute(): + table = xhiveframework.qb.DocType("Report") + xhiveframework.qb.update(table).set(table.prepared_report, 0).where(table.disable_prepared_report == 1) diff --git a/xhiveframework/patches/v15_0/drop_modified_index.py b/xhiveframework/patches/v15_0/drop_modified_index.py new file mode 100644 index 0000000..a5d85ef --- /dev/null +++ b/xhiveframework/patches/v15_0/drop_modified_index.py @@ -0,0 +1,21 @@ +import xhiveframework +from xhiveframework.patches.v14_0.drop_unused_indexes import drop_index_if_exists + + +def execute(): + if xhiveframework.db.db_type == "postgres": + return + + db_tables = xhiveframework.db.get_tables(cached=False) + + child_tables = xhiveframework.get_all( + "DocType", + {"istable": 1, "is_virtual": 0}, + pluck="name", + ) + + for doctype in child_tables: + table = f"tab{doctype}" + if table not in db_tables: + continue + drop_index_if_exists(table, "modified") diff --git a/xhiveframework/patches/v15_0/move_event_cancelled_to_status.py b/xhiveframework/patches/v15_0/move_event_cancelled_to_status.py new file mode 100644 index 0000000..d1f3474 --- /dev/null +++ b/xhiveframework/patches/v15_0/move_event_cancelled_to_status.py @@ -0,0 +1,12 @@ +import xhiveframework + + +def execute(): + Event = xhiveframework.qb.DocType("Event") + query = ( + xhiveframework.qb.update(Event) + .set(Event.event_type, "Private") + .set(Event.status, "Cancelled") + .where(Event.event_type == "Cancelled") + ) + query.run() diff --git a/xhiveframework/patches/v15_0/remove_background_jobs_from_dropdown.py b/xhiveframework/patches/v15_0/remove_background_jobs_from_dropdown.py new file mode 100644 index 0000000..3794a09 --- /dev/null +++ b/xhiveframework/patches/v15_0/remove_background_jobs_from_dropdown.py @@ -0,0 +1,9 @@ +import xhiveframework + + +def execute(): + item = xhiveframework.db.exists("Navbar Item", {"item_label": "Background Jobs"}) + if not item: + return + + xhiveframework.delete_doc("Navbar Item", item) diff --git a/xhiveframework/patches/v15_0/remove_event_streaming.py b/xhiveframework/patches/v15_0/remove_event_streaming.py new file mode 100644 index 0000000..0f88875 --- /dev/null +++ b/xhiveframework/patches/v15_0/remove_event_streaming.py @@ -0,0 +1,22 @@ +import xhiveframework + + +def execute(): + if "event_streaming" in xhiveframework.get_installed_apps(): + return + + xhiveframework.delete_doc_if_exists("Module Def", "Event Streaming", force=True) + + for doc in [ + "Event Consumer Document Type", + "Document Type Mapping", + "Event Producer", + "Event Producer Last Update", + "Event Producer Document Type", + "Event Consumer", + "Document Type Field Mapping", + "Event Update Log", + "Event Update Log Consumer", + "Event Sync Log", + ]: + xhiveframework.delete_doc_if_exists("DocType", doc, force=True) diff --git a/xhiveframework/patches/v15_0/remove_implicit_primary_key.py b/xhiveframework/patches/v15_0/remove_implicit_primary_key.py new file mode 100644 index 0000000..a15d41b --- /dev/null +++ b/xhiveframework/patches/v15_0/remove_implicit_primary_key.py @@ -0,0 +1,50 @@ +import xhiveframework +from xhiveframework.model.naming import is_autoincremented + +possible_log_types = ( + "Version", + "Error Log", + "Scheduled Job Log", + "Event Sync Log", + "Event Update Log", + "Access Log", + "View Log", + "Activity Log", + "Energy Point Log", + "Notification Log", + "Email Queue", + "DocShare", + "Document Follow", + "Console Log", +) + + +def execute(): + """Few doctypes had int PKs even though schema didn't mention them, this requires detecting it + at runtime which is prone to bugs and adds unnecessary overhead. + + This patch converts them back to varchar. + """ + for doctype in possible_log_types: + if ( + xhiveframework.db.exists("DocType", doctype) + and _is_implicit_int_pk(doctype) + and not is_autoincremented(doctype) + ): + xhiveframework.db.change_column_type( + doctype, + "name", + type=f"varchar({xhiveframework.db.VARCHAR_LEN})", + nullable=True, + ) + + +def _is_implicit_int_pk(doctype: str) -> bool: + query = f"""select data_type FROM information_schema.columns where column_name = 'name' and table_name = 'tab{doctype}'""" + values = () + if xhiveframework.db.db_type == "mariadb": + query += " and table_schema = %s" + values = (xhiveframework.db.db_name,) + + col_type = xhiveframework.db.sql(query, values) + return bool(col_type and col_type[0][0] == "bigint") diff --git a/xhiveframework/patches/v15_0/remove_prepared_report_settings_from_system_settings.py b/xhiveframework/patches/v15_0/remove_prepared_report_settings_from_system_settings.py new file mode 100644 index 0000000..71b62e0 --- /dev/null +++ b/xhiveframework/patches/v15_0/remove_prepared_report_settings_from_system_settings.py @@ -0,0 +1,9 @@ +import xhiveframework +from xhiveframework.utils import cint + + +def execute(): + expiry_period = ( + cint(xhiveframework.db.get_singles_dict("System Settings").get("prepared_report_expiry_period")) or 30 + ) + xhiveframework.get_single("Log Settings").register_doctype("Prepared Report", expiry_period) diff --git a/xhiveframework/patches/v15_0/sanitize_workspace_titles.py b/xhiveframework/patches/v15_0/sanitize_workspace_titles.py new file mode 100644 index 0000000..c1e9491 --- /dev/null +++ b/xhiveframework/patches/v15_0/sanitize_workspace_titles.py @@ -0,0 +1,25 @@ +import xhiveframework +from xhiveframework.desk.doctype.workspace.workspace import update_page +from xhiveframework.utils import strip_html +from xhiveframework.utils.html_utils import unescape_html + + +def execute(): + workspaces_to_update = xhiveframework.get_all( + "Workspace", + filters={"module": ("is", "not set")}, + fields=["name", "title", "icon", "indicator_color", "parent_page as parent", "public"], + ) + for workspace in workspaces_to_update: + new_title = strip_html(unescape_html(workspace.title)) + + if new_title == workspace.title: + continue + + workspace.title = new_title + try: + update_page(**workspace) + xhiveframework.db.commit() + + except Exception: + xhiveframework.db.rollback() diff --git a/xhiveframework/patches/v15_0/set_contact_full_name.py b/xhiveframework/patches/v15_0/set_contact_full_name.py new file mode 100644 index 0000000..d0b1cef --- /dev/null +++ b/xhiveframework/patches/v15_0/set_contact_full_name.py @@ -0,0 +1,31 @@ +import xhiveframework +from xhiveframework.contacts.doctype.contact.contact import get_full_name +from xhiveframework.utils import update_progress_bar + + +def execute(): + """Set full name for all contacts""" + xhiveframework.db.auto_commit_on_many_writes = 1 + + contacts = xhiveframework.get_all( + "Contact", + fields=["name", "first_name", "middle_name", "last_name", "company_name"], + filters={"full_name": ("is", "not set")}, + as_list=True, + ) + total = len(contacts) + for idx, (name, first, middle, last, company) in enumerate(contacts): + update_progress_bar("Setting full name for contacts", idx, total) + try: + xhiveframework.db.set_value( + "Contact", + name, + "full_name", + get_full_name(first, middle, last, company), + update_modified=False, + ) + except xhiveframework.db.DataError as e: + if xhiveframework.db.is_data_too_long(e): + print("Full name is too long for DB column, skipping") + continue + raise e diff --git a/xhiveframework/patches/v15_0/set_file_type.py b/xhiveframework/patches/v15_0/set_file_type.py new file mode 100644 index 0000000..40fb8bb --- /dev/null +++ b/xhiveframework/patches/v15_0/set_file_type.py @@ -0,0 +1,32 @@ +import mimetypes + +import xhiveframework + + +def execute(): + """Set 'File Type' for all files based on file extension.""" + files = xhiveframework.db.get_all( + "File", + fields=["name", "file_name", "file_url"], + filters={"is_folder": 0, "file_type": ("is", "not set")}, + ) + + xhiveframework.db.auto_commit_on_many_writes = 1 + + for file in files: + file_extension = get_file_extension(file.file_name or file.file_url) + if file_extension: + xhiveframework.db.set_value("File", file.name, "file_type", file_extension, update_modified=False) + + xhiveframework.db.auto_commit_on_many_writes = 0 + + +def get_file_extension(file_name): + if not file_name: + return None + file_type = mimetypes.guess_type(file_name)[0] + if not file_type: + return None + + file_extension = mimetypes.guess_extension(file_type) + return file_extension.lstrip(".").upper() if file_extension else None diff --git a/xhiveframework/patches/v15_0/validate_newsletter_recipients.py b/xhiveframework/patches/v15_0/validate_newsletter_recipients.py new file mode 100644 index 0000000..debe9f0 --- /dev/null +++ b/xhiveframework/patches/v15_0/validate_newsletter_recipients.py @@ -0,0 +1,9 @@ +import xhiveframework +from xhiveframework.utils import validate_email_address + + +def execute(): + for name, email in xhiveframework.get_all("Email Group Member", fields=["name", "email"], as_list=True): + if not validate_email_address(email, throw=False): + xhiveframework.db.set_value("Email Group Member", name, "unsubscribed", 1) + xhiveframework.db.commit() diff --git a/xhiveframework/permissions.py b/xhiveframework/permissions.py new file mode 100644 index 0000000..e9bc5b8 --- /dev/null +++ b/xhiveframework/permissions.py @@ -0,0 +1,842 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import copy +import functools + +import xhiveframework +import xhiveframework.share +from xhiveframework import _, msgprint +from xhiveframework.query_builder import DocType +from xhiveframework.utils import cint, cstr + +rights = ( + "select", + "read", + "write", + "create", + "delete", + "submit", + "cancel", + "amend", + "print", + "email", + "report", + "import", + "export", + "share", +) + + +GUEST_ROLE = "Guest" +ALL_USER_ROLE = "All" # This includes website users too. +SYSTEM_USER_ROLE = "Desk User" +ADMIN_ROLE = "Administrator" + + +# These roles are automatically assigned based on user type +AUTOMATIC_ROLES = (GUEST_ROLE, ALL_USER_ROLE, SYSTEM_USER_ROLE, ADMIN_ROLE) + + +def print_has_permission_check_logs(func): + @functools.wraps(func) + def inner(*args, **kwargs): + raise_exception = kwargs.get("raise_exception", True) + self_perm_check = True if not kwargs.get("user") else kwargs.get("user") == xhiveframework.session.user + + if raise_exception: + xhiveframework.flags["has_permission_check_logs"] = [] + + result = func(*args, **kwargs) + + # print only if access denied + # and if user is checking his own permission + if not result and self_perm_check and raise_exception: + msgprint(("
      ").join(xhiveframework.flags.get("has_permission_check_logs", []))) + + if raise_exception: + xhiveframework.flags.pop("has_permission_check_logs", None) + return result + + return inner + + +def _debug_log(log: str): + if not hasattr(xhiveframework.local, "permission_debug_log"): + xhiveframework.local.permission_debug_log = [] + xhiveframework.local.permission_debug_log.append(log) + + +def _pop_debug_log() -> list[str]: + if log := getattr(xhiveframework.local, "permission_debug_log", None): + del xhiveframework.local.permission_debug_log + return log + return [] + + +@print_has_permission_check_logs +def has_permission( + doctype, + ptype="read", + doc=None, + user=None, + raise_exception=True, + *, + parent_doctype=None, + debug=False, +) -> bool: + """Return True if user has permission `ptype` for given `doctype`. + If `doc` is passed, also check user, share and owner permissions. + + :param doctype: DocType to check permission for + :param ptype: Permission Type to check + :param doc: Check User Permissions for specified document. + :param user: User to check permission for. Defaults to current user. + :param raise_exception: + DOES NOT raise an exception. + If not False, will display a message using xhiveframework.msgprint + which explains why the permission check failed. + + :param parent_doctype: + Required when checking permission for a child DocType (unless doc is specified) + """ + + if not user: + user = xhiveframework.session.user + + if user == "Administrator": + debug and _debug_log("Allowed everything because user is Administrator") + return True + + if ptype == "share" and xhiveframework.get_system_settings("disable_document_sharing"): + debug and _debug_log("User can't share because sharing is disabled globally from system settings") + return False + + if not doc and hasattr(doctype, "doctype"): + # first argument can be doc or doctype + doc = doctype + doctype = doc.doctype + + if xhiveframework.is_table(doctype): + return has_child_permission(doctype, ptype, doc, user, raise_exception, parent_doctype, debug=debug) + + meta = xhiveframework.get_meta(doctype) + + if doc: + if isinstance(doc, str | int): + doc = xhiveframework.get_doc(meta.name, doc) + perm = get_doc_permissions(doc, user=user, ptype=ptype, debug=debug).get(ptype) + if not perm: + debug and _debug_log( + "Permission check failed from role permission system. Check if user's role grant them permission to the document." + ) + msg = _("User {0} does not have access to this document").format(xhiveframework.bold(user)) + if xhiveframework.has_permission(doc.doctype): + msg += f": {_(doc.doctype)} - {doc.name}" + push_perm_check_log(msg, debug=debug) + else: + if ptype == "submit" and not cint(meta.is_submittable): + push_perm_check_log(_("Document Type is not submittable"), debug=debug) + return False + + if ptype == "import" and not cint(meta.allow_import): + push_perm_check_log(_("Document Type is not importable"), debug=debug) + return False + + role_permissions = get_role_permissions(meta, user=user, debug=debug) + debug and _debug_log( + "User has following permissions using role permission system: " + + xhiveframework.as_json(role_permissions, indent=8) + ) + + perm = role_permissions.get(ptype) + + if not perm: + push_perm_check_log( + _("User {0} does not have doctype access via role permission for document {1}").format( + xhiveframework.bold(user), xhiveframework.bold(_(doctype)) + ), + debug=debug, + ) + + def false_if_not_shared(): + if ptype not in ("read", "write", "share", "submit", "email", "print"): + debug and _debug_log(f"Permission type {ptype} can not be shared") + return False + + rights = ["read" if ptype in ("email", "print") else ptype] + + if doc: + doc_name = get_doc_name(doc) + shared = xhiveframework.share.get_shared( + doctype, + user, + rights=rights, + filters=[["share_name", "=", doc_name]], + limit=1, + ) + debug and _debug_log(f"Document is shared with user for {ptype}? {bool(shared)}") + return bool(shared) + + elif xhiveframework.share.get_shared(doctype, user, rights=rights, limit=1): + # if atleast one shared doc of that type, then return True + # this is used in db_query to check if permission on DocType + debug and _debug_log(f"At least one document is shared with user with perm: {rights}") + return True + + return False + + if not perm: + debug and _debug_log("Checking if document/doctype is explicitly shared with user") + perm = false_if_not_shared() + + return bool(perm) + + +def get_doc_permissions(doc, user=None, ptype=None, debug=False): + """Return a dict of evaluated permissions for given `doc` like `{"read":1, "write":1}`""" + if not user: + user = xhiveframework.session.user + + meta = xhiveframework.get_meta(doc.doctype) + + def is_user_owner(): + return (doc.get("owner") or "").lower() == user.lower() + + if not has_controller_permissions(doc, ptype, user=user, debug=debug): + push_perm_check_log(_("Not allowed via controller permission check"), debug=debug) + return {ptype: 0} + + permissions = copy.deepcopy(get_role_permissions(meta, user=user, is_owner=is_user_owner(), debug=debug)) + + debug and _debug_log( + "User has following permissions using role permission system: " + + xhiveframework.as_json(permissions, indent=8) + ) + + if not cint(meta.is_submittable): + permissions["submit"] = 0 + + if not cint(meta.allow_import): + permissions["import"] = 0 + + # Override with `if_owner` perms irrespective of user + if permissions.get("has_if_owner_enabled"): + # apply owner permissions on top of existing permissions + # some access might be only for the owner + # eg. everyone might have read access but only owner can delete + permissions.update(permissions.get("if_owner", {})) + debug and _debug_log( + "User is owner of document, so permissions are updated to: " + xhiveframework.as_json(permissions) + ) + + if not has_user_permission(doc, user, debug=debug): + if is_user_owner(): + # replace with owner permissions + permissions = permissions.get("if_owner", {}) + # if_owner does not come with create rights... + permissions["create"] = 0 + debug and _debug_log("User has only 'If owner' permissions because of User Permissions") + else: + debug and _debug_log("User has no permissions because of User Permissions") + permissions = {} + + debug and _debug_log( + "Final applicable permissions after evaluating user permissions: " + + xhiveframework.as_json(permissions, indent=8) + ) + return permissions + + +def get_role_permissions(doctype_meta, user=None, is_owner=None, debug=False): + """ + Returns dict of evaluated role permissions like + { + "read": 1, + "write": 0, + // if "if_owner" is enabled + "if_owner": + { + "read": 1, + "write": 0 + } + } + """ + if isinstance(doctype_meta, str): + doctype_meta = xhiveframework.get_meta(doctype_meta) # assuming doctype name was passed + + if not user: + user = xhiveframework.session.user + + cache_key = (doctype_meta.name, user, bool(is_owner)) + + if user == "Administrator": + debug and _debug_log("all permissions granted because user is Administrator") + return allow_everything() + + if not xhiveframework.local.role_permissions.get(cache_key) or debug: + perms = xhiveframework._dict(if_owner={}) + + roles = xhiveframework.get_roles(user) + debug and _debug_log("User has following roles: " + str(roles)) + + def is_perm_applicable(perm): + return perm.role in roles and cint(perm.permlevel) == 0 + + def has_permission_without_if_owner_enabled(ptype): + return any(p.get(ptype, 0) and not p.get("if_owner", 0) for p in applicable_permissions) + + applicable_permissions = list(filter(is_perm_applicable, getattr(doctype_meta, "permissions", []))) + has_if_owner_enabled = any(p.get("if_owner", 0) for p in applicable_permissions) + perms["has_if_owner_enabled"] = has_if_owner_enabled + + for ptype in rights: + pvalue = any(p.get(ptype, 0) for p in applicable_permissions) + # check if any perm object allows perm type + perms[ptype] = cint(pvalue) + if ( + pvalue + and has_if_owner_enabled + and not has_permission_without_if_owner_enabled(ptype) + and ptype != "create" + ): + perms["if_owner"][ptype] = cint(pvalue and is_owner) + # has no access if not owner + # only provide select or read access so that user is able to at-least access list + # (and the documents will be filtered based on owner sin further checks) + perms[ptype] = 1 if ptype in ("select", "read") else 0 + + xhiveframework.local.role_permissions[cache_key] = perms + + return xhiveframework.local.role_permissions[cache_key] + + +def get_user_permissions(user): + from xhiveframework.core.doctype.user_permission.user_permission import get_user_permissions + + return get_user_permissions(user) + + +def has_user_permission(doc, user=None, debug=False): + """Return True if User is allowed to view considering User Permissions.""" + from xhiveframework.core.doctype.user_permission.user_permission import get_user_permissions + + user_permissions = get_user_permissions(user) + + if not user_permissions: + # no user permission rules specified for this doctype + debug and _debug_log("User is not affected by any user permissions") + return True + + # user can create own role permissions, so nothing applies + if get_role_permissions("User Permission", user=user).get("write"): + debug and _debug_log("User permission bypassed because user can modify user permissions.") + return True + + # don't apply strict user permissions for single doctypes since they contain empty link fields + apply_strict_user_permissions = ( + False if doc.meta.issingle else xhiveframework.get_system_settings("apply_strict_user_permissions") + ) + if apply_strict_user_permissions: + debug and _debug_log("Strict user permissions will be applied") + + doctype = doc.get("doctype") + docname = doc.get("name") + + # STEP 1: --------------------- + # check user permissions on self + if doctype in user_permissions: + allowed_docs = get_allowed_docs_for_doctype(user_permissions.get(doctype, []), doctype) + + # if allowed_docs is empty it states that there is no applicable permission under the current doctype + + # only check if allowed_docs is not empty + if allowed_docs and str(docname) not in allowed_docs: + # no user permissions for this doc specified + debug and _debug_log( + "User doesn't have access to this document because of User Permissions, allowed documents: " + + str(allowed_docs) + ) + push_perm_check_log(_("Not allowed for {0}: {1}").format(_(doctype), docname), debug=debug) + return False + else: + debug and _debug_log(f"User Has access to {docname} via User Permissions.") + + # STEP 2: --------------------------------- + # check user permissions in all link fields + + def check_user_permission_on_link_fields(d): + # check user permissions for all the link fields of the given + # document object d + # + # called for both parent and child records + + meta = xhiveframework.get_meta(d.get("doctype")) + + # check all link fields for user permissions + for field in meta.get_link_fields(): + if field.ignore_user_permissions: + continue + + # empty value, do you still want to apply user permissions? + if not d.get(field.fieldname) and not apply_strict_user_permissions: + # nah, not strict + continue + + if field.options not in user_permissions: + continue + + # get the list of all allowed values for this link + allowed_docs = get_allowed_docs_for_doctype(user_permissions.get(field.options, []), doctype) + + if allowed_docs and d.get(field.fieldname) not in allowed_docs: + # restricted for this link field, and no matching values found + # make the right message and exit + if d.get("parentfield"): + # "You are not allowed to access this Employee record because it is linked + # to Company 'Restricted Company' in row 3, field Reference Type" + msg = _( + "You are not allowed to access this {0} record because it is linked to {1} '{2}' in row {3}, field {4}" + ).format( + _(meta.doctype), + _(field.options), + d.get(field.fieldname) or _("empty"), + d.idx, + _(field.label, context=field.parent) if field.label else field.fieldname, + ) + else: + # "You are not allowed to access Company 'Restricted Company' in field Reference Type" + msg = _( + "You are not allowed to access this {0} record because it is linked to {1} '{2}' in field {3}" + ).format( + _(meta.doctype), + _(field.options), + d.get(field.fieldname) or _("empty"), + _(field.label, context=field.parent) if field.label else field.fieldname, + ) + + push_perm_check_log(msg, debug=debug) + + return False + + return True + + if not check_user_permission_on_link_fields(doc): + return False + + for d in doc.get_all_children(): + if not check_user_permission_on_link_fields(d): + return False + + return True + + +def has_controller_permissions(doc, ptype, user=None, debug=False) -> bool: + """Return controller permissions if denied, True if not defined. + + Controllers can only deny permission, they can not explicitly grant any permission that wasn't + already present.""" + if not user: + user = xhiveframework.session.user + + methods = xhiveframework.get_hooks("has_permission").get(doc.doctype, []) + + if not methods: + return True + + for method in reversed(methods): + controller_permission = xhiveframework.call(method, doc=doc, ptype=ptype, user=user, debug=debug) + debug and _debug_log(f"Controller permission check from {method}: {controller_permission}") + if controller_permission is not None: + return bool(controller_permission) + + # None of the controller hooks returned anything conclusive + return True + + +def get_doctypes_with_read(): + return list({cstr(p.parent) for p in get_valid_perms() if p.parent}) + + +def get_valid_perms(doctype=None, user=None): + """Get valid permissions for the current user from DocPerm and Custom DocPerm""" + roles = get_roles(user) + + perms = get_perms_for(roles) + custom_perms = get_perms_for(roles, "Custom DocPerm") + + doctypes_with_custom_perms = get_doctypes_with_custom_docperms() + for p in perms: + if p.parent not in doctypes_with_custom_perms: + custom_perms.append(p) + + if doctype: + return [p for p in custom_perms if p.parent == doctype] + else: + return custom_perms + + +def get_all_perms(role): + """Returns valid permissions for a given role""" + perms = xhiveframework.get_all("DocPerm", fields="*", filters=dict(role=role)) + custom_perms = xhiveframework.get_all("Custom DocPerm", fields="*", filters=dict(role=role)) + doctypes_with_custom_perms = xhiveframework.get_all("Custom DocPerm", pluck="parent", distinct=True) + + for p in perms: + if p.parent not in doctypes_with_custom_perms: + custom_perms.append(p) + return custom_perms + + +def get_roles(user=None, with_standard=True): + """get roles of current user""" + if not user: + user = xhiveframework.session.user + + if user == "Guest" or not user: + return [GUEST_ROLE] + + def get(): + if user == "Administrator": + return xhiveframework.get_all("Role", pluck="name") # return all available roles + else: + table = DocType("Has Role") + roles = ( + xhiveframework.qb.from_(table) + .where( + (table.parenttype == "User") + & (table.parent == user) + & (table.role.notin(AUTOMATIC_ROLES)) + ) + .select(table.role) + .run(pluck=True) + ) + roles += [ALL_USER_ROLE, GUEST_ROLE] + if is_system_user(user): + roles.append(SYSTEM_USER_ROLE) + return roles + + roles = xhiveframework.cache.hget("roles", user, get) + + # filter standard if required + if not with_standard: + roles = [r for r in roles if r not in AUTOMATIC_ROLES] + + return roles + + +def get_doctype_roles(doctype, access_type="read"): + """Returns a list of roles that are allowed to access passed doctype.""" + meta = xhiveframework.get_meta(doctype) + return [d.role for d in meta.get("permissions") if d.get(access_type)] + + +def get_perms_for(roles, perm_doctype="DocPerm"): + """Get perms for given roles""" + filters = {"permlevel": 0, "docstatus": 0, "role": ["in", roles]} + return xhiveframework.get_all(perm_doctype, fields=["*"], filters=filters) + + +def get_doctypes_with_custom_docperms(): + """Returns all the doctypes with Custom Docperms""" + + doctypes = xhiveframework.get_all("Custom DocPerm", fields=["parent"], distinct=1) + return [d.parent for d in doctypes] + + +def add_user_permission( + doctype, + name, + user, + ignore_permissions=False, + applicable_for=None, + is_default=0, + hide_descendants=0, +): + """Add user permission""" + from xhiveframework.core.doctype.user_permission.user_permission import user_permission_exists + + if not user_permission_exists(user, doctype, name, applicable_for): + if not xhiveframework.db.exists(doctype, name): + xhiveframework.throw(_("{0} {1} not found").format(_(doctype), name), xhiveframework.DoesNotExistError) + + xhiveframework.get_doc( + dict( + doctype="User Permission", + user=user, + allow=doctype, + for_value=name, + is_default=is_default, + applicable_for=applicable_for, + apply_to_all_doctypes=0 if applicable_for else 1, + hide_descendants=hide_descendants, + ) + ).insert(ignore_permissions=ignore_permissions) + + +def remove_user_permission(doctype, name, user): + user_permission_name = xhiveframework.db.get_value( + "User Permission", dict(user=user, allow=doctype, for_value=name) + ) + xhiveframework.delete_doc("User Permission", user_permission_name) + + +def clear_user_permissions_for_doctype(doctype, user=None): + filters = {"allow": doctype} + if user: + filters["user"] = user + user_permissions_for_doctype = xhiveframework.get_all("User Permission", filters=filters) + for d in user_permissions_for_doctype: + xhiveframework.delete_doc("User Permission", d.name) + + +def can_import(doctype, raise_exception=False): + if not ("System Manager" in xhiveframework.get_roles() or has_permission(doctype, "import")): + if raise_exception: + raise xhiveframework.PermissionError(f"You are not allowed to import: {doctype}") + else: + return False + return True + + +def can_export(doctype, raise_exception=False): + if "System Manager" in xhiveframework.get_roles(): + return True + else: + role_permissions = xhiveframework.permissions.get_role_permissions(doctype) + has_access = role_permissions.get("export") or role_permissions.get("if_owner").get("export") + if not has_access and raise_exception: + raise xhiveframework.PermissionError(_("You are not allowed to export {} doctype").format(doctype)) + return has_access + + +def update_permission_property( + doctype, + role, + permlevel, + ptype, + value=None, + validate=True, + if_owner=0, +): + """Update a property in Custom Perm""" + from xhiveframework.core.doctype.doctype.doctype import validate_permissions_for_doctype + + out = setup_custom_perms(doctype) + + name = xhiveframework.db.get_value( + "Custom DocPerm", dict(parent=doctype, role=role, permlevel=permlevel, if_owner=if_owner) + ) + table = DocType("Custom DocPerm") + xhiveframework.qb.update(table).set(ptype, value).where(table.name == name).run() + + if validate: + validate_permissions_for_doctype(doctype) + + return out + + +def setup_custom_perms(parent): + """if custom permssions are not setup for the current doctype, set them up""" + if not xhiveframework.db.exists("Custom DocPerm", dict(parent=parent)): + copy_perms(parent) + return True + + +def add_permission(doctype, role, permlevel=0, ptype=None): + """Add a new permission rule to the given doctype + for the given Role and Permission Level""" + from xhiveframework.core.doctype.doctype.doctype import validate_permissions_for_doctype + + setup_custom_perms(doctype) + + if xhiveframework.db.get_value( + "Custom DocPerm", dict(parent=doctype, role=role, permlevel=permlevel, if_owner=0) + ): + xhiveframework.msgprint( + _("Rule for this doctype, role, permlevel and if-owner combination already exists.").format( + doctype, + ), + alert=True, + ) + return + + if not ptype: + ptype = "read" + + custom_docperm = xhiveframework.get_doc( + { + "doctype": "Custom DocPerm", + "__islocal": 1, + "parent": doctype, + "parenttype": "DocType", + "parentfield": "permissions", + "role": role, + "permlevel": permlevel, + ptype: 1, + } + ) + + custom_docperm.save() + + validate_permissions_for_doctype(doctype) + return custom_docperm.name + + +def copy_perms(parent): + """Copy all DocPerm in to Custom DocPerm for the given document""" + for d in xhiveframework.get_all("DocPerm", fields="*", filters=dict(parent=parent)): + custom_perm = xhiveframework.new_doc("Custom DocPerm") + custom_perm.update(d) + custom_perm.insert(ignore_permissions=True) + + +def reset_perms(doctype): + """Reset permissions for given doctype.""" + from xhiveframework.desk.notifications import delete_notification_count_for + + delete_notification_count_for(doctype) + xhiveframework.db.delete("Custom DocPerm", {"parent": doctype}) + + +def get_linked_doctypes(dt: str) -> list: + meta = xhiveframework.get_meta(dt) + linked_doctypes = [dt] + [ + d.options + for d in meta.get( + "fields", + {"fieldtype": "Link", "ignore_user_permissions": ("!=", 1), "options": ("!=", "[Select]")}, + ) + ] + + return list(set(linked_doctypes)) + + +def get_doc_name(doc): + if not doc: + return None + return doc if isinstance(doc, str) else str(doc.name) + + +def allow_everything(): + """ + returns a dict with access to everything + eg. {"read": 1, "write": 1, ...} + """ + return {ptype: 1 for ptype in rights} + + +def get_allowed_docs_for_doctype(user_permissions, doctype): + """Returns all the docs from the passed user_permissions that are + allowed under provided doctype""" + return filter_allowed_docs_for_doctype(user_permissions, doctype, with_default_doc=False) + + +def filter_allowed_docs_for_doctype(user_permissions, doctype, with_default_doc=True): + """Returns all the docs from the passed user_permissions that are + allowed under provided doctype along with default doc value if with_default_doc is set""" + allowed_doc = [] + default_doc = None + for doc in user_permissions: + if not doc.get("applicable_for") or doc.get("applicable_for") == doctype: + allowed_doc.append(doc.get("doc")) + if doc.get("is_default") or len(user_permissions) == 1 and with_default_doc: + default_doc = doc.get("doc") + + return (allowed_doc, default_doc) if with_default_doc else allowed_doc + + +def push_perm_check_log(log, debug=False): + debug and _debug_log(log) + if xhiveframework.flags.get("has_permission_check_logs") is None: + return + + xhiveframework.flags.get("has_permission_check_logs").append(log) + + +def has_child_permission( + child_doctype, + ptype="read", + child_doc=None, + user=None, + raise_exception=True, + parent_doctype=None, + *, + debug=False, +) -> bool: + debug and _debug_log("This doctype is a child table, permissions will be checked on parent.") + if isinstance(child_doc, str): + child_doc = xhiveframework.db.get_value( + child_doctype, + child_doc, + ("parent", "parenttype", "parentfield"), + as_dict=True, + ) + + if child_doc: + parent_doctype = child_doc.parenttype + + if not parent_doctype: + push_perm_check_log( + _("Please specify a valid parent DocType for {0}").format(xhiveframework.bold(child_doctype)), + debug=debug, + ) + return False + + parent_meta = xhiveframework.get_meta(parent_doctype) + + if parent_meta.istable or not ( + valid_parentfields := [ + df.fieldname for df in parent_meta.get_table_fields() if df.options == child_doctype + ] + ): + push_perm_check_log( + _("{0} is not a valid parent DocType for {1}").format( + xhiveframework.bold(parent_doctype), xhiveframework.bold(child_doctype) + ), + debug=debug, + ) + return False + + if child_doc: + parentfield = child_doc.parentfield + if not parentfield: + push_perm_check_log( + _("Parentfield not specified in {0}: {1}").format( + xhiveframework.bold(child_doctype), xhiveframework.bold(child_doc.name) + ), + debug=debug, + ) + return False + + if parentfield not in valid_parentfields: + push_perm_check_log( + _("{0} is not a valid parentfield for {1}").format( + xhiveframework.bold(parentfield), xhiveframework.bold(child_doctype) + ), + debug=debug, + ) + return False + + permlevel = parent_meta.get_field(parentfield).permlevel + accessible_permlevels = parent_meta.get_permlevel_access(ptype, user=user) + if permlevel > 0 and permlevel not in accessible_permlevels: + push_perm_check_log( + _("Insufficient Permission Level for {0}").format(xhiveframework.bold(parent_doctype)), debug=debug + ) + debug and _debug_log( + f"This table is perm level {permlevel} but user only has access to {accessible_permlevels}" + ) + return False + + return has_permission( + parent_doctype, + ptype=ptype, + doc=child_doc and getattr(child_doc, "parent_doc", child_doc.parent), + user=user, + raise_exception=raise_exception, + debug=debug, + ) + + +def is_system_user(user: str | None = None) -> bool: + return xhiveframework.get_cached_value("User", user or xhiveframework.session.user, "user_type") == "System User" diff --git a/xhiveframework/printing/__init__.py b/xhiveframework/printing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/__init__.py b/xhiveframework/printing/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/letter_head/__init__.py b/xhiveframework/printing/doctype/letter_head/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/letter_head/letter_head.js b/xhiveframework/printing/doctype/letter_head/letter_head.js new file mode 100644 index 0000000..9c0d7e1 --- /dev/null +++ b/xhiveframework/printing/doctype/letter_head/letter_head.js @@ -0,0 +1,61 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Letter Head", { + setup(frm) { + frm.get_field("instructions").html(INSTRUCTIONS); + }, + + refresh: function (frm) { + frm.flag_public_attachments = true; + }, + + validate: (frm) => { + ["header_script", "footer_script"].forEach((field) => { + if (!frm.doc[field]) return; + + try { + eval(frm.doc[field]); + } catch (e) { + xhiveframework.throw({ + title: __("Error in Header/Footer Script"), + indicator: "orange", + message: '
      ' + e.stack + "
      ", + }); + } + }); + }, +}); + +const INSTRUCTIONS = `

      ${__("Letter Head Scripts")}

      +

      ${__("Header/Footer scripts can be used to add dynamic behaviours.")}

      +
      +
      +// ${__(
      +	"The following Header Script will add the current date to an element in 'Header HTML' with class 'header-content'"
      +)}
      +var el = document.getElementsByClassName("header-content");
      +if (el.length > 0) {
      +	el[0].textContent += " " + new Date().toGMTString();
      +}
      +
      +
      +

      ${__("You can also access wkhtmltopdf variables (valid only in PDF print):")}

      +
      +
      +// ${__("Get Header and Footer wkhtmltopdf variables")}
      +// ${__("Snippet and more variables:  {0}", ["https://wkhtmltopdf.org/usage/wkhtmltopdf.txt"])}
      +var vars = {};
      +var query_strings_from_url = document.location.search.substring(1).split('&');
      +for (var query_string in query_strings_from_url) {
      +	if (query_strings_from_url.hasOwnProperty(query_string)) {
      +		var temp_var = query_strings_from_url[query_string].split('=', 2);
      +		vars[temp_var[0]] = decodeURI(temp_var[1]);
      +	}
      +}
      +var el = document.getElementsByClassName("header-content");
      +if (el.length > 0 && vars["page"] == 1) {
      +	el[0].textContent += " : " + vars["date"];
      +}
      +
      +
      `; diff --git a/xhiveframework/printing/doctype/letter_head/letter_head.json b/xhiveframework/printing/doctype/letter_head/letter_head.json new file mode 100644 index 0000000..4ffca13 --- /dev/null +++ b/xhiveframework/printing/doctype/letter_head/letter_head.json @@ -0,0 +1,229 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:letter_head_name", + "creation": "2012-11-22 17:45:46", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "letter_head_name", + "source", + "footer_source", + "column_break_3", + "disabled", + "is_default", + "letter_head_image_section", + "image", + "image_height", + "image_width", + "align", + "header_section", + "content", + "footer_section", + "footer", + "footer_image_section", + "footer_image", + "footer_image_height", + "footer_image_width", + "footer_align", + "scripts_section", + "header_script", + "footer_script", + "instructions" + ], + "fields": [ + { + "fieldname": "letter_head_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Letter Head Name", + "oldfieldname": "letter_head_name", + "oldfieldtype": "Data", + "reqd": 1, + "unique": 1 + }, + { + "depends_on": "letter_head_name", + "fieldname": "source", + "fieldtype": "Select", + "label": "Letter Head Based On", + "options": "Image\nHTML" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "default": "0", + "depends_on": "letter_head_name", + "fieldname": "disabled", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Disabled", + "oldfieldname": "disabled", + "oldfieldtype": "Check" + }, + { + "default": "0", + "depends_on": "letter_head_name", + "fieldname": "is_default", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Default Letter Head", + "oldfieldname": "is_default", + "oldfieldtype": "Check", + "search_index": 1 + }, + { + "depends_on": "eval:doc.letter_head_name && doc.source === 'Image'", + "fieldname": "letter_head_image_section", + "fieldtype": "Section Break", + "label": "Letter Head Image" + }, + { + "depends_on": "eval:doc.letter_head_name && doc.source === 'Image'", + "fieldname": "image", + "fieldtype": "Attach Image", + "label": "Image" + }, + { + "depends_on": "eval:doc.source==='HTML' && doc.letter_head_name", + "fieldname": "header_section", + "fieldtype": "Section Break", + "label": "Header" + }, + { + "depends_on": "eval:!doc.__islocal && doc.source==='HTML'", + "description": "Letter Head in HTML", + "fieldname": "content", + "fieldtype": "HTML Editor", + "label": "Header HTML", + "oldfieldname": "content", + "oldfieldtype": "Text Editor" + }, + { + "depends_on": "eval:doc.footer_source==='HTML' && doc.letter_head_name", + "fieldname": "footer_section", + "fieldtype": "Section Break", + "label": "Footer" + }, + { + "depends_on": "eval:!doc.__islocal", + "description": "Footer will display correctly only in PDF", + "fieldname": "footer", + "fieldtype": "HTML Editor", + "label": "Footer HTML" + }, + { + "default": "Left", + "fieldname": "align", + "fieldtype": "Select", + "label": "Align", + "options": "Left\nRight\nCenter" + }, + { + "fieldname": "image_height", + "fieldtype": "Float", + "label": "Image Height" + }, + { + "fieldname": "image_width", + "fieldtype": "Float", + "label": "Image Width" + }, + { + "depends_on": "eval:doc.footer_source==='Image' && doc.letter_head_name", + "fieldname": "footer_image_section", + "fieldtype": "Section Break", + "label": "Footer Image" + }, + { + "fieldname": "footer_image", + "fieldtype": "Attach Image", + "label": "Image" + }, + { + "fieldname": "footer_image_height", + "fieldtype": "Float", + "label": "Image Height" + }, + { + "fieldname": "footer_image_width", + "fieldtype": "Float", + "label": "Image Width" + }, + { + "fieldname": "footer_align", + "fieldtype": "Select", + "label": "Align", + "options": "Left\nRight\nCenter" + }, + { + "default": "HTML", + "depends_on": "letter_head_name", + "fieldname": "footer_source", + "fieldtype": "Select", + "label": "Footer Based On", + "options": "Image\nHTML" + }, + { + "depends_on": "eval:!doc.__islocal && doc.source==='HTML'", + "fieldname": "header_script", + "fieldtype": "Code", + "label": "Header Script", + "options": "Javascript" + }, + { + "depends_on": "eval:!doc.__islocal && doc.footer_source==='HTML'", + "fieldname": "footer_script", + "fieldtype": "Code", + "label": "Footer Script", + "options": "Javascript" + }, + { + "collapsible": 1, + "collapsible_depends_on": "eval: doc.header_script || doc.footer_script", + "fieldname": "scripts_section", + "fieldtype": "Section Break", + "label": "Scripts" + }, + { + "fieldname": "instructions", + "fieldtype": "HTML", + "label": "Instructions", + "read_only": 1 + } + ], + "icon": "fa fa-font", + "idx": 1, + "links": [], + "max_attachments": 3, + "modified": "2023-12-21 16:19:37.525003", + "modified_by": "Administrator", + "module": "Printing", + "name": "Letter Head", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "read": 1, + "role": "Desk User" + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/printing/doctype/letter_head/letter_head.py b/xhiveframework/printing/doctype/letter_head/letter_head.py new file mode 100644 index 0000000..71cecde --- /dev/null +++ b/xhiveframework/printing/doctype/letter_head/letter_head.py @@ -0,0 +1,123 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import flt, is_image + + +class LetterHead(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + align: DF.Literal["Left", "Right", "Center"] + content: DF.HTMLEditor | None + disabled: DF.Check + footer: DF.HTMLEditor | None + footer_align: DF.Literal["Left", "Right", "Center"] + footer_image: DF.AttachImage | None + footer_image_height: DF.Float + footer_image_width: DF.Float + footer_source: DF.Literal["Image", "HTML"] + image: DF.AttachImage | None + image_height: DF.Float + image_width: DF.Float + is_default: DF.Check + letter_head_name: DF.Data + source: DF.Literal["Image", "HTML"] + + # end: auto-generated types + def before_insert(self): + # for better UX, let user set from attachment + self.source = "Image" + + def validate(self): + self.set_image() + self.validate_disabled_and_default() + + def validate_disabled_and_default(self): + if self.disabled and self.is_default: + xhiveframework.throw(_("Letter Head cannot be both disabled and default")) + + if not self.is_default and not self.disabled: + if not xhiveframework.db.exists("Letter Head", dict(is_default=1)): + self.is_default = 1 + + def set_image(self): + if self.source == "Image": + self.set_image_as_html( + field="image", + width="image_width", + height="image_height", + align="align", + html_field="content", + dimension_prefix="image_", + success_msg=_("Header HTML set from attachment {0}").format(self.image), + failure_msg=_("Please attach an image file to set HTML for Letter Head."), + ) + + if self.footer_source == "Image": + self.set_image_as_html( + field="footer_image", + width="footer_image_width", + height="footer_image_height", + align="footer_align", + html_field="footer", + dimension_prefix="footer_image_", + success_msg=_("Footer HTML set from attachment {0}").format(self.footer_image), + failure_msg=_("Please attach an image file to set HTML for Footer."), + ) + + def set_image_as_html( + self, field, width, height, dimension_prefix, align, html_field, success_msg, failure_msg + ): + if not self.get(field) or not is_image(self.get(field)): + xhiveframework.msgprint(failure_msg, alert=True, indicator="orange") + return + + self.set(width, flt(self.get(width))) + self.set(height, flt(self.get(height))) + + # To preserve the aspect ratio of the image, apply constraints only on + # the greater dimension and allow the other to scale accordingly + dimension = "width" if self.get(width) > self.get(height) else "height" + dimension_value = self.get(f"{dimension_prefix}{dimension}") + + if not dimension_value: + dimension_value = "" + + self.set( + html_field, + f"""
      +{self.get( +
      """, + ) + + xhiveframework.msgprint(success_msg, alert=True) + + def on_update(self): + self.set_as_default() + + # clear the cache so that the new letter head is uploaded + xhiveframework.clear_cache() + + def set_as_default(self): + from xhiveframework.utils import set_default + + if self.is_default: + xhiveframework.db.sql("update `tabLetter Head` set is_default=0 where name != %s", self.name) + + set_default("letter_head", self.name) + + # update control panel - so it loads new letter directly + xhiveframework.db.set_default("default_letter_head_content", self.content) + else: + xhiveframework.defaults.clear_default("letter_head", self.name) + xhiveframework.defaults.clear_default("default_letter_head_content", self.content) diff --git a/xhiveframework/printing/doctype/letter_head/test_letter_head.py b/xhiveframework/printing/doctype/letter_head/test_letter_head.py new file mode 100644 index 0000000..2c01d77 --- /dev/null +++ b/xhiveframework/printing/doctype/letter_head/test_letter_head.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestLetterHead(XhiveFrameworkTestCase): + def test_auto_image(self): + letter_head = xhiveframework.get_doc( + dict(doctype="Letter Head", letter_head_name="Test", source="Image", image="/public/test.png") + ).insert() + + # test if image is automatically set + self.assertTrue(letter_head.image in letter_head.content) diff --git a/xhiveframework/printing/doctype/network_printer_settings/__init__.py b/xhiveframework/printing/doctype/network_printer_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.js b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.js new file mode 100644 index 0000000..7af5682 --- /dev/null +++ b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.js @@ -0,0 +1,29 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Network Printer Settings", { + onload(frm) { + frm.trigger("connect_print_server"); + }, + server_ip(frm) { + frm.trigger("connect_print_server"); + }, + port(frm) { + frm.trigger("connect_print_server"); + }, + connect_print_server(frm) { + if (frm.doc.server_ip && frm.doc.port) { + xhiveframework.call({ + doc: frm.doc, + method: "get_printers_list", + args: { + ip: frm.doc.server_ip, + port: frm.doc.port, + }, + callback: function (data) { + frm.set_df_property("printer_name", "options", [""].concat(data.message)); + }, + }); + } + }, +}); diff --git a/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.json b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.json new file mode 100644 index 0000000..019d4cc --- /dev/null +++ b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.json @@ -0,0 +1,77 @@ +{ + "actions": [], + "autoname": "Prompt", + "creation": "2021-09-17 11:26:06.943999", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "server_ip", + "port", + "column_break_4", + "printer_name" + ], + "fields": [ + { + "default": "localhost", + "fieldname": "server_ip", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Server IP", + "reqd": 1 + }, + { + "default": "631", + "fieldname": "port", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Port", + "reqd": 1 + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "printer_name", + "fieldtype": "Select", + "label": "Printer Name", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2023-08-28 22:25:22.236295", + "modified_by": "Administrator", + "module": "Printing", + "name": "Network Printer Settings", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "Desk User", + "share": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.py b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.py new file mode 100644 index 0000000..88d5db1 --- /dev/null +++ b/xhiveframework/printing/doctype/network_printer_settings/network_printer_settings.py @@ -0,0 +1,54 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class NetworkPrinterSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + port: DF.Int + printer_name: DF.Literal[None] + server_ip: DF.Data + + # end: auto-generated types + @xhiveframework.whitelist() + def get_printers_list(self, ip="127.0.0.1", port=631): + printer_list = [] + try: + import cups + except ImportError: + xhiveframework.throw( + _( + """This feature can not be used as dependencies are missing. + Please contact your system manager to enable this by installing pycups!""" + ) + ) + return + try: + cups.setServer(self.server_ip) + cups.setPort(self.port) + conn = cups.Connection() + printers = conn.getPrinters() + printer_list.extend( + {"value": printer_id, "label": printer["printer-make-and-model"]} + for printer_id, printer in printers.items() + ) + except RuntimeError: + xhiveframework.throw(_("Failed to connect to server")) + except xhiveframework.ValidationError: + xhiveframework.throw(_("Failed to connect to server")) + return printer_list + + +@xhiveframework.whitelist() +def get_network_printer_settings(): + return xhiveframework.db.get_list("Network Printer Settings", pluck="name") diff --git a/xhiveframework/printing/doctype/network_printer_settings/test_network_printer_settings.py b/xhiveframework/printing/doctype/network_printer_settings/test_network_printer_settings.py new file mode 100644 index 0000000..8209680 --- /dev/null +++ b/xhiveframework/printing/doctype/network_printer_settings/test_network_printer_settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestNetworkPrinterSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/printing/doctype/print_format/__init__.py b/xhiveframework/printing/doctype/print_format/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/print_format/print_format.js b/xhiveframework/printing/doctype/print_format/print_format.js new file mode 100644 index 0000000..31d2cca --- /dev/null +++ b/xhiveframework/printing/doctype/print_format/print_format.js @@ -0,0 +1,88 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Print Format", "onload", function (frm) { + frm.add_fetch("doc_type", "module", "module"); +}); + +xhiveframework.ui.form.on("Print Format", { + refresh: function (frm) { + frm.set_intro(""); + frm.toggle_enable(["html", "doc_type", "module"], false); + if (xhiveframework.session.user === "Administrator" || frm.doc.standard === "No") { + frm.toggle_enable(["html", "doc_type", "module"], true); + frm.enable_save(); + } + + if (frm.doc.standard === "Yes" && xhiveframework.session.user !== "Administrator") { + frm.set_intro(__("Please duplicate this to make changes")); + } + frm.trigger("render_buttons"); + frm.toggle_display("standard", xhiveframework.boot.developer_mode); + frm.trigger("hide_absolute_value_field"); + }, + render_buttons: function (frm) { + frm.page.clear_inner_toolbar(); + if (!frm.is_new()) { + if (!frm.doc.custom_format) { + frm.add_custom_button(__("Edit Format"), function () { + if (!frm.doc.doc_type) { + xhiveframework.msgprint(__("Please select DocType first")); + return; + } + if (frm.doc.print_format_builder_beta) { + xhiveframework.set_route("print-format-builder-beta", frm.doc.name); + } else { + xhiveframework.set_route("print-format-builder", frm.doc.name); + } + }); + } else if (frm.doc.custom_format && !frm.doc.raw_printing) { + frm.set_df_property("html", "reqd", 1); + } + if (xhiveframework.model.can_write("Customize Form")) { + xhiveframework.model.with_doctype(frm.doc.doc_type, function () { + let current_format = xhiveframework.get_meta(frm.doc.doc_type).default_print_format; + if (current_format == frm.doc.name) { + return; + } + + frm.add_custom_button(__("Set as Default"), function () { + xhiveframework.call({ + method: "xhiveframework.printing.doctype.print_format.print_format.make_default", + args: { + name: frm.doc.name, + }, + callback: function () { + frm.refresh(); + }, + }); + }); + }); + } + } + }, + custom_format: function (frm) { + var value = frm.doc.custom_format ? 0 : 1; + frm.set_value("align_labels_right", value); + frm.set_value("show_section_headings", value); + frm.set_value("line_breaks", value); + frm.trigger("render_buttons"); + }, + doc_type: function (frm) { + frm.trigger("hide_absolute_value_field"); + }, + hide_absolute_value_field: function (frm) { + // TODO: make it work with frm.doc.doc_type + // Problem: frm isn't updated in some random cases + const doctype = locals[frm.doc.doctype][frm.doc.name].doc_type; + if (doctype) { + xhiveframework.model.with_doctype(doctype, () => { + const meta = xhiveframework.get_meta(doctype); + const has_int_float_currency_field = meta.fields.filter((df) => + ["Int", "Float", "Currency"].includes(df.fieldtype) + ); + frm.toggle_display("absolute_value", has_int_float_currency_field.length); + }); + } + }, +}); diff --git a/xhiveframework/printing/doctype/print_format/print_format.json b/xhiveframework/printing/doctype/print_format/print_format.json new file mode 100644 index 0000000..d182cc8 --- /dev/null +++ b/xhiveframework/printing/doctype/print_format/print_format.json @@ -0,0 +1,290 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2013-01-23 19:54:43", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "doc_type", + "module", + "default_print_language", + "column_break_3", + "standard", + "custom_format", + "disabled", + "section_break_6", + "print_format_type", + "raw_printing", + "html", + "raw_commands", + "section_break_9", + "margin_top", + "margin_bottom", + "margin_left", + "margin_right", + "align_labels_right", + "show_section_headings", + "line_breaks", + "absolute_value", + "column_break_11", + "font_size", + "font", + "page_number", + "css_section", + "css", + "custom_html_help", + "section_break_13", + "print_format_help", + "format_data", + "print_format_builder", + "print_format_builder_beta" + ], + "fields": [ + { + "fieldname": "doc_type", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "in_standard_filter": 1, + "label": "DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "options": "Module Def" + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "label": "Disabled" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "default": "No", + "fieldname": "standard", + "fieldtype": "Select", + "in_filter": 1, + "label": "Standard", + "no_copy": 1, + "oldfieldname": "standard", + "oldfieldtype": "Select", + "options": "No\nYes", + "reqd": 1, + "search_index": 1 + }, + { + "default": "0", + "fieldname": "custom_format", + "fieldtype": "Check", + "label": "Custom Format" + }, + { + "depends_on": "custom_format", + "fieldname": "section_break_6", + "fieldtype": "Section Break" + }, + { + "default": "Jinja", + "depends_on": "custom_format", + "fieldname": "print_format_type", + "fieldtype": "Select", + "label": "Print Format Type", + "options": "Jinja\nJS" + }, + { + "default": "0", + "fieldname": "raw_printing", + "fieldtype": "Check", + "label": "Raw Printing" + }, + { + "depends_on": "eval:!doc.raw_printing", + "fieldname": "html", + "fieldtype": "Code", + "label": "HTML", + "oldfieldname": "html", + "oldfieldtype": "Text Editor", + "options": "Jinja" + }, + { + "depends_on": "raw_printing", + "description": "Any string-based printer languages can be used. Writing raw commands requires knowledge of the printer's native language provided by the printer manufacturer. Please refer to the developer manual provided by the printer manufacturer on how to write their native commands. These commands are rendered on the server side using the Jinja Templating Language.", + "fieldname": "raw_commands", + "fieldtype": "Code", + "label": "Raw Commands", + "options": "Jinja" + }, + { + "depends_on": "eval:!doc.custom_format", + "fieldname": "section_break_9", + "fieldtype": "Section Break", + "label": "Style Settings" + }, + { + "default": "0", + "fieldname": "align_labels_right", + "fieldtype": "Check", + "label": "Align Labels to the Right" + }, + { + "default": "0", + "fieldname": "show_section_headings", + "fieldtype": "Check", + "label": "Show Section Headings" + }, + { + "default": "0", + "fieldname": "line_breaks", + "fieldtype": "Check", + "label": "Show Line Breaks after Sections" + }, + { + "fieldname": "column_break_11", + "fieldtype": "Column Break" + }, + { + "fieldname": "default_print_language", + "fieldtype": "Link", + "label": "Default Print Language", + "options": "Language" + }, + { + "depends_on": "eval:!doc.custom_format", + "fieldname": "font", + "fieldtype": "Data", + "label": "Google Font" + }, + { + "depends_on": "eval:!doc.raw_printing", + "fieldname": "css_section", + "fieldtype": "Section Break" + }, + { + "fieldname": "css", + "fieldtype": "Code", + "label": "Custom CSS", + "options": "CSS" + }, + { + "fieldname": "custom_html_help", + "fieldtype": "HTML", + "label": "Custom HTML Help", + "options": "

      Custom CSS Help

      \n\n

      Notes:

      \n\n
        \n
      1. All field groups (label + value) are set attributes data-fieldtype and data-fieldname
      2. \n
      3. All values are given class value
      4. \n
      5. All Section Breaks are given class section-break
      6. \n
      7. All Column Breaks are given class column-break
      8. \n
      \n\n

      Examples

      \n\n

      1. Left align integers

      \n\n
      [data-fieldtype=\"Int\"] .value { text-align: left; }
      \n\n

      1. Add border to sections except the last section

      \n\n
      .section-break { padding: 30px 0px; border-bottom: 1px solid #eee; }\n.section-break:last-child { padding-bottom: 0px; border-bottom: 0px;  }
      \n" + }, + { + "depends_on": "custom_format", + "fieldname": "section_break_13", + "fieldtype": "Section Break" + }, + { + "depends_on": "custom_format", + "fieldname": "print_format_help", + "fieldtype": "HTML", + "label": "Print Format Help", + "options": "

      Print Format Help

      \n
      \n

      Introduction

      \n

      Print Formats are rendered on the server side using the Jinja Templating Language. All forms have access to the doc object which contains information about the document that is being formatted. You can also access common utilities via the xhiveframework module.

      \n

      For styling, the Boostrap CSS framework is provided and you can enjoy the full range of classes.

      \n
      \n

      References

      \n
        \n\t
      1. Jinja Templating Language
      2. \n\t
      3. Bootstrap CSS Framework
      4. \n
      \n
      \n

      Example

      \n
      <h3>{{ doc.select_print_heading or \"Invoice\" }}</h3>\n<div class=\"row\">\n\t<div class=\"col-md-3 text-right\">Customer Name</div>\n\t<div class=\"col-md-9\">{{ doc.customer_name }}</div>\n</div>\n<div class=\"row\">\n\t<div class=\"col-md-3 text-right\">Date</div>\n\t<div class=\"col-md-9\">{{ doc.get_formatted(\"invoice_date\") }}</div>\n</div>\n<table class=\"table table-bordered\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<th>Sr</th>\n\t\t\t<th>Item Name</th>\n\t\t\t<th>Description</th>\n\t\t\t<th class=\"text-right\">Qty</th>\n\t\t\t<th class=\"text-right\">Rate</th>\n\t\t\t<th class=\"text-right\">Amount</th>\n\t\t</tr>\n\t\t{%- for row in doc.items -%}\n\t\t<tr>\n\t\t\t<td style=\"width: 3%;\">{{ row.idx }}</td>\n\t\t\t<td style=\"width: 20%;\">\n\t\t\t\t{{ row.item_name }}\n\t\t\t\t{% if row.item_code != row.item_name -%}\n\t\t\t\t<br>Item Code: {{ row.item_code}}\n\t\t\t\t{%- endif %}\n\t\t\t</td>\n\t\t\t<td style=\"width: 37%;\">\n\t\t\t\t<div style=\"border: 0px;\">{{ row.description }}</div></td>\n\t\t\t<td style=\"width: 10%; text-align: right;\">{{ row.qty }} {{ row.uom or row.stock_uom }}</td>\n\t\t\t<td style=\"width: 15%; text-align: right;\">{{\n\t\t\t\trow.get_formatted(\"rate\", doc) }}</td>\n\t\t\t<td style=\"width: 15%; text-align: right;\">{{\n\t\t\t\trow.get_formatted(\"amount\", doc) }}</td>\n\t\t</tr>\n\t\t{%- endfor -%}\n\t</tbody>\n</table>
      \n
      \n

      Common Functions

      \n\n\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\n\t\n
      doc.get_formatted(\"[fieldname]\", [parent_doc])Get document value formatted as Date, Currency, etc. Pass parent doc for currency type fields.
      xhiveframework.db.get_value(\"[doctype]\", \"[name]\", \"fieldname\")Get value from another document.
      \n" + }, + { + "fieldname": "format_data", + "fieldtype": "Code", + "hidden": 1, + "label": "Format Data" + }, + { + "default": "0", + "fieldname": "print_format_builder", + "fieldtype": "Check", + "hidden": 1, + "label": "Print Format Builder" + }, + { + "default": "0", + "depends_on": "doc_type", + "description": "If checked, negative numeric values of Currency, Quantity or Count would be shown as positive", + "fieldname": "absolute_value", + "fieldtype": "Check", + "label": "Show Absolute Values" + }, + { + "default": "0", + "fieldname": "print_format_builder_beta", + "fieldtype": "Check", + "label": "Print Format Builder Beta" + }, + { + "default": "15", + "fieldname": "margin_top", + "fieldtype": "Float", + "label": "Margin Top" + }, + { + "default": "15", + "fieldname": "margin_bottom", + "fieldtype": "Float", + "label": "Margin Bottom" + }, + { + "default": "15", + "fieldname": "margin_left", + "fieldtype": "Float", + "label": "Margin Left" + }, + { + "default": "15", + "fieldname": "margin_right", + "fieldtype": "Float", + "label": "Margin Right" + }, + { + "default": "14", + "fieldname": "font_size", + "fieldtype": "Int", + "hidden": 1, + "label": "Font Size" + }, + { + "default": "Hide", + "fieldname": "page_number", + "fieldtype": "Select", + "label": "Page Number", + "options": "Hide\nTop Left\nTop Center\nTop Right\nBottom Left\nBottom Center\nBottom Right" + } + ], + "icon": "fa fa-print", + "idx": 1, + "links": [], + "modified": "2023-12-12 19:59:37.133301", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Format", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + }, + { + "role": "Desk User", + "select": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} diff --git a/xhiveframework/printing/doctype/print_format/print_format.py b/xhiveframework/printing/doctype/print_format/print_format.py new file mode 100644 index 0000000..2168b31 --- /dev/null +++ b/xhiveframework/printing/doctype/print_format/print_format.py @@ -0,0 +1,166 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import json + +import xhiveframework +import xhiveframework.utils +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils.jinja import validate_template +from xhiveframework.utils.weasyprint import download_pdf, get_html + + +class PrintFormat(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + absolute_value: DF.Check + align_labels_right: DF.Check + css: DF.Code | None + custom_format: DF.Check + default_print_language: DF.Link | None + disabled: DF.Check + doc_type: DF.Link + font: DF.Data | None + font_size: DF.Int + format_data: DF.Code | None + html: DF.Code | None + line_breaks: DF.Check + margin_bottom: DF.Float + margin_left: DF.Float + margin_right: DF.Float + margin_top: DF.Float + module: DF.Link | None + page_number: DF.Literal[ + "Hide", "Top Left", "Top Center", "Top Right", "Bottom Left", "Bottom Center", "Bottom Right" + ] + print_format_builder: DF.Check + print_format_builder_beta: DF.Check + print_format_type: DF.Literal["Jinja", "JS"] + raw_commands: DF.Code | None + raw_printing: DF.Check + show_section_headings: DF.Check + standard: DF.Literal["No", "Yes"] + + # end: auto-generated types + + def onload(self): + templates = xhiveframework.get_all( + "Print Format Field Template", + fields=["template", "field", "name"], + filters={"document_type": self.doc_type}, + ) + self.set_onload("print_templates", templates) + + def get_html(self, docname, letterhead=None): + return get_html(self.doc_type, docname, self.name, letterhead) + + def download_pdf(self, docname, letterhead=None): + return download_pdf(self.doc_type, docname, self.name, letterhead) + + def validate(self): + if ( + self.standard == "Yes" + and not xhiveframework.local.conf.get("developer_mode") + and not (xhiveframework.flags.in_import or xhiveframework.flags.in_test) + ): + xhiveframework.throw(xhiveframework._("Standard Print Format cannot be updated")) + + # old_doc_type is required for clearing item cache + self.old_doc_type = xhiveframework.db.get_value("Print Format", self.name, "doc_type") + + self.extract_images() + + if not self.module: + self.module = xhiveframework.db.get_value("DocType", self.doc_type, "module") + + if self.html and self.print_format_type != "JS": + validate_template(self.html) + + if self.custom_format and self.raw_printing and not self.raw_commands: + xhiveframework.throw(_("{0} are required").format(xhiveframework.bold(_("Raw Commands"))), xhiveframework.MandatoryError) + + if self.custom_format and not self.html and not self.raw_printing: + xhiveframework.throw(_("{0} is required").format(xhiveframework.bold(_("HTML"))), xhiveframework.MandatoryError) + + def extract_images(self): + from xhiveframework.core.doctype.file.utils import extract_images_from_html + + if self.print_format_builder_beta: + return + + if self.format_data: + data = json.loads(self.format_data) + for df in data: + if df.get("fieldtype") and df["fieldtype"] in ("HTML", "Custom HTML") and df.get("options"): + df["options"] = extract_images_from_html(self, df["options"]) + self.format_data = json.dumps(data) + + def on_update(self): + if hasattr(self, "old_doc_type") and self.old_doc_type: + xhiveframework.clear_cache(doctype=self.old_doc_type) + if self.doc_type: + xhiveframework.clear_cache(doctype=self.doc_type) + + self.export_doc() + + def after_rename(self, old: str, new: str, *args, **kwargs): + if self.doc_type: + xhiveframework.clear_cache(doctype=self.doc_type) + + # update property setter default_print_format if set + xhiveframework.db.set_value( + "Property Setter", + { + "doctype_or_field": "DocType", + "doc_type": self.doc_type, + "property": "default_print_format", + "value": old, + }, + "value", + new, + ) + + def export_doc(self): + from xhiveframework.modules.utils import export_module_json + + return export_module_json(self, self.standard == "Yes", self.module) + + def on_trash(self): + if self.doc_type: + xhiveframework.clear_cache(doctype=self.doc_type) + + +@xhiveframework.whitelist() +def make_default(name): + """Set print format as default""" + xhiveframework.has_permission("Print Format", "write") + + print_format = xhiveframework.get_doc("Print Format", name) + + doctype = xhiveframework.get_doc("DocType", print_format.doc_type) + if doctype.custom: + doctype.default_print_format = name + doctype.save() + else: + # "Customize form" + xhiveframework.make_property_setter( + { + "doctype_or_field": "DocType", + "doctype": print_format.doc_type, + "property": "default_print_format", + "value": name, + } + ) + + xhiveframework.msgprint( + xhiveframework._("{0} is now default print format for {1} doctype").format( + xhiveframework.bold(name), xhiveframework.bold(print_format.doc_type) + ) + ) diff --git a/xhiveframework/printing/doctype/print_format/test_print_format.py b/xhiveframework/printing/doctype/print_format/test_print_format.py new file mode 100644 index 0000000..9046b99 --- /dev/null +++ b/xhiveframework/printing/doctype/print_format/test_print_format.py @@ -0,0 +1,65 @@ +# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE +import os +import re +import unittest +from typing import TYPE_CHECKING + +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + +if TYPE_CHECKING: + from xhiveframework.printing.doctype.print_format.print_format import PrintFormat + +test_records = xhiveframework.get_test_records("Print Format") + + +class TestPrintFormat(XhiveFrameworkTestCase): + def test_print_user(self, style=None): + print_html = xhiveframework.get_print("User", "Administrator", style=style) + self.assertTrue("" in print_html) + self.assertTrue(re.findall(r'
      [\s]*administrator[\s]*
      ', print_html)) + return print_html + + def test_print_user_standard(self): + print_html = self.test_print_user("Standard") + self.assertTrue(re.findall(r"\.print-format {[\s]*font-size: 9pt;", print_html)) + self.assertFalse(re.findall(r"th {[\s]*background-color: #eee;[\s]*}", print_html)) + self.assertFalse("font-family: serif;" in print_html) + + def test_print_user_modern(self): + print_html = self.test_print_user("Modern") + self.assertTrue("/* modern format: for-test */" in print_html) + + def test_print_user_classic(self): + print_html = self.test_print_user("Classic") + self.assertTrue("/* classic format: for-test */" in print_html) + + @unittest.skipUnless( + os.access(xhiveframework.get_app_path("xhiveframework"), os.W_OK), "Only run if xhiveframework app paths is writable" + ) + def test_export_doc(self): + doc: "PrintFormat" = xhiveframework.get_doc("Print Format", test_records[0]["name"]) + + # this is only to make export_doc happy + doc.standard = "Yes" + _before = xhiveframework.conf.developer_mode + xhiveframework.conf.developer_mode = True + export_path = doc.export_doc() + xhiveframework.conf.developer_mode = _before + + exported_doc_path = f"{export_path}.json" + doc.reload() + doc_dict = doc.as_dict(no_nulls=True, convert_dates_to_str=True) + + self.assertTrue(os.path.exists(exported_doc_path)) + + with open(exported_doc_path) as f: + exported_doc = xhiveframework.parse_json(f.read()) + + for key, value in exported_doc.items(): + if key in doc_dict: + with self.subTest(key=key): + self.assertEqual(value, doc_dict[key]) + + self.addCleanup(os.remove, exported_doc_path) diff --git a/xhiveframework/printing/doctype/print_format/test_records.json b/xhiveframework/printing/doctype/print_format/test_records.json new file mode 100644 index 0000000..c772383 --- /dev/null +++ b/xhiveframework/printing/doctype/print_format/test_records.json @@ -0,0 +1,9 @@ +[ + { + "doctype": "Print Format", + "name": "_Test Print Format 1", + "module": "Core", + "doc_type": "User", + "html": "" + } +] diff --git a/xhiveframework/printing/doctype/print_format_field_template/__init__.py b/xhiveframework/printing/doctype/print_format_field_template/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.js b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.js new file mode 100644 index 0000000..059dc98 --- /dev/null +++ b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.js @@ -0,0 +1,7 @@ +// Copyright (c) 2021, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Print Format Field Template", { + // refresh: function(frm) { + // } +}); diff --git a/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.json b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.json new file mode 100644 index 0000000..3b79aae --- /dev/null +++ b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.json @@ -0,0 +1,101 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "Prompt", + "creation": "2021-10-05 14:23:56.508499", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "document_type", + "field", + "template_file", + "column_break_3", + "module", + "standard", + "section_break_5", + "template" + ], + "fields": [ + { + "depends_on": "eval:!doc.multiple", + "fieldname": "document_type", + "fieldtype": "Link", + "in_list_view": 1, + "in_standard_filter": 1, + "label": "Document Type", + "mandatory_depends_on": "eval:!doc.multiple", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "field", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Default Template For Field" + }, + { + "depends_on": "eval:!doc.standard", + "fieldname": "template", + "fieldtype": "Code", + "label": "Template", + "mandatory_depends_on": "eval:!doc.standard", + "options": "HTML" + }, + { + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_5", + "fieldtype": "Section Break", + "hide_border": 1 + }, + { + "depends_on": "standard", + "fieldname": "module", + "fieldtype": "Link", + "label": "Module", + "options": "Module Def" + }, + { + "default": "0", + "fieldname": "standard", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Standard" + }, + { + "depends_on": "eval:doc.standard", + "fieldname": "template_file", + "fieldtype": "Data", + "label": "Template File", + "mandatory_depends_on": "eval:doc.standard" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2021-10-19 17:47:59.577949", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Format Field Template", + "naming_rule": "Set by user", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.py b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.py new file mode 100644 index 0000000..a7f90cd --- /dev/null +++ b/xhiveframework/printing/doctype/print_format_field_template/print_format_field_template.py @@ -0,0 +1,59 @@ +# Copyright (c) 2021, XhiveFramework Technologies and contributors +# For license information, please see license.txt + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document + + +class PrintFormatFieldTemplate(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + document_type: DF.Link + field: DF.Data | None + module: DF.Link | None + standard: DF.Check + template: DF.Code | None + template_file: DF.Data | None + + # end: auto-generated types + def validate(self): + if self.standard and not (xhiveframework.conf.developer_mode or xhiveframework.flags.in_patch): + xhiveframework.throw(_("Enable developer mode to create a standard Print Template")) + + def before_insert(self): + self.validate_duplicate() + + def on_update(self): + self.validate_duplicate() + self.export_doc() + + def validate_duplicate(self): + if not self.standard: + return + if not self.field: + return + + filters = {"document_type": self.document_type, "field": self.field} + if not self.is_new(): + filters.update({"name": ("!=", self.name)}) + result = xhiveframework.get_all("Print Format Field Template", filters=filters, limit=1) + if result: + xhiveframework.throw( + _("A template already exists for field {0} of {1}").format( + xhiveframework.bold(self.field), xhiveframework.bold(self.document_type) + ), + xhiveframework.DuplicateEntryError, + title=_("Duplicate Entry"), + ) + + def export_doc(self): + from xhiveframework.modules.utils import export_module_json + + export_module_json(self, self.standard, self.module) diff --git a/xhiveframework/printing/doctype/print_format_field_template/test_print_format_field_template.py b/xhiveframework/printing/doctype/print_format_field_template/test_print_format_field_template.py new file mode 100644 index 0000000..60af6ef --- /dev/null +++ b/xhiveframework/printing/doctype/print_format_field_template/test_print_format_field_template.py @@ -0,0 +1,9 @@ +# Copyright (c) 2021, XhiveFramework Technologies and Contributors +# See license.txt + +# import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPrintFormatFieldTemplate(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/printing/doctype/print_heading/__init__.py b/xhiveframework/printing/doctype/print_heading/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/print_heading/print_heading.js b/xhiveframework/printing/doctype/print_heading/print_heading.js new file mode 100644 index 0000000..c1507b5 --- /dev/null +++ b/xhiveframework/printing/doctype/print_heading/print_heading.js @@ -0,0 +1,6 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Print Heading", { + refresh: function (frm) {}, +}); diff --git a/xhiveframework/printing/doctype/print_heading/print_heading.json b/xhiveframework/printing/doctype/print_heading/print_heading.json new file mode 100644 index 0000000..9951b53 --- /dev/null +++ b/xhiveframework/printing/doctype/print_heading/print_heading.json @@ -0,0 +1,143 @@ +{ + "allow_copy": 0, + "allow_guest_to_view": 0, + "allow_import": 1, + "allow_rename": 1, + "autoname": "field:print_heading", + "beta": 0, + "creation": "2013-01-10 16:34:24", + "custom": 0, + "docstatus": 0, + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 0, + "fields": [ + { + "allow_bulk_edit": 0, + "allow_on_submit": 1, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "print_heading", + "fieldtype": "Data", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 1, + "in_global_search": 0, + "in_list_view": 1, + "in_standard_filter": 0, + "label": "Print Heading", + "length": 0, + "no_copy": 0, + "oldfieldname": "print_heading", + "oldfieldtype": "Data", + "permlevel": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 1, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "description", + "fieldtype": "Small Text", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 1, + "in_standard_filter": 0, + "label": "Description", + "length": 0, + "no_copy": 0, + "oldfieldname": "description", + "oldfieldtype": "Small Text", + "permlevel": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0, + "width": "300px" + } + ], + "has_web_view": 0, + "hide_heading": 0, + "hide_toolbar": 0, + "icon": "fa fa-font", + "idx": 1, + "image_view": 0, + "in_create": 0, + "is_submittable": 0, + "issingle": 0, + "istable": 0, + "max_attachments": 0, + "modified": "2017-05-03 05:59:09.131569", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Heading", + "owner": "Administrator", + "permissions": [ + { + "amend": 0, + "apply_user_permissions": 0, + "cancel": 0, + "create": 1, + "delete": 1, + "email": 1, + "export": 0, + "if_owner": 0, + "import": 0, + "permlevel": 0, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "submit": 0, + "write": 1 + }, + { + "amend": 0, + "apply_user_permissions": 0, + "cancel": 0, + "create": 0, + "delete": 0, + "email": 0, + "export": 0, + "if_owner": 0, + "import": 0, + "permlevel": 0, + "print": 0, + "read": 1, + "report": 0, + "role": "All", + "share": 0, + "submit": 0, + "write": 0 + } + ], + "quick_entry": 1, + "read_only": 0, + "read_only_onload": 0, + "search_fields": "print_heading", + "show_name_in_global_search": 0, + "sort_order": "DESC", + "track_changes": 0, + "track_seen": 0 +} diff --git a/xhiveframework/printing/doctype/print_heading/print_heading.py b/xhiveframework/printing/doctype/print_heading/print_heading.py new file mode 100644 index 0000000..099eb40 --- /dev/null +++ b/xhiveframework/printing/doctype/print_heading/print_heading.py @@ -0,0 +1,20 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class PrintHeading(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + description: DF.SmallText | None + print_heading: DF.Data + # end: auto-generated types + pass diff --git a/xhiveframework/printing/doctype/print_heading/test_print_heading.py b/xhiveframework/printing/doctype/print_heading/test_print_heading.py new file mode 100644 index 0000000..ab3b660 --- /dev/null +++ b/xhiveframework/printing/doctype/print_heading/test_print_heading.py @@ -0,0 +1,8 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPrintHeading(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/printing/doctype/print_settings/__init__.py b/xhiveframework/printing/doctype/print_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/print_settings/print_settings.js b/xhiveframework/printing/doctype/print_settings/print_settings.js new file mode 100644 index 0000000..23012f1 --- /dev/null +++ b/xhiveframework/printing/doctype/print_settings/print_settings.js @@ -0,0 +1,23 @@ +// Copyright (c) 2018, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Print Settings", { + print_style: function (frm) { + xhiveframework.db.get_value("Print Style", frm.doc.print_style, "preview").then((r) => { + if (r.message.preview) { + frm.get_field("print_style_preview").$wrapper.html( + `` + ); + } else { + frm.get_field("print_style_preview").$wrapper.html( + `

      ${__( + "No Preview" + )}

      ` + ); + } + }); + }, + onload: function (frm) { + frm.script_manager.trigger("print_style"); + }, +}); diff --git a/xhiveframework/printing/doctype/print_settings/print_settings.json b/xhiveframework/printing/doctype/print_settings/print_settings.json new file mode 100644 index 0000000..a67440b --- /dev/null +++ b/xhiveframework/printing/doctype/print_settings/print_settings.json @@ -0,0 +1,198 @@ +{ + "actions": [], + "creation": "2014-07-17 06:54:20.782907", + "doctype": "DocType", + "document_type": "System", + "engine": "InnoDB", + "field_order": [ + "pdf_settings", + "send_print_as_pdf", + "repeat_header_footer", + "column_break_4", + "pdf_page_size", + "pdf_page_height", + "pdf_page_width", + "view_link_in_email", + "with_letterhead", + "allow_print_for_draft", + "add_draft_heading", + "column_break_10", + "allow_page_break_inside_tables", + "allow_print_for_cancelled", + "server_printer", + "enable_print_server", + "raw_printing_section", + "enable_raw_printing", + "print_style_section", + "print_style", + "print_style_preview", + "section_break_8", + "font", + "font_size" + ], + "fields": [ + { + "fieldname": "pdf_settings", + "fieldtype": "Section Break", + "label": "PDF Settings" + }, + { + "default": "1", + "description": "Send Email Print Attachments as PDF (Recommended)", + "fieldname": "send_print_as_pdf", + "fieldtype": "Check", + "label": "Send Print as PDF" + }, + { + "default": "1", + "fieldname": "repeat_header_footer", + "fieldtype": "Check", + "label": "Repeat Header and Footer" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "default": "A4", + "fieldname": "pdf_page_size", + "fieldtype": "Select", + "label": "PDF Page Size", + "options": "A0\nA1\nA2\nA3\nA4\nA5\nA6\nA7\nA8\nA9\nB0\nB1\nB2\nB3\nB4\nB5\nB6\nB7\nB8\nB9\nB10\nC5E\nComm10E\nDLE\nExecutive\nFolio\nLedger\nLegal\nLetter\nTabloid\nCustom" + }, + { + "fieldname": "view_link_in_email", + "fieldtype": "Section Break", + "label": "Page Settings" + }, + { + "default": "1", + "fieldname": "with_letterhead", + "fieldtype": "Check", + "label": "Print with letterhead" + }, + { + "default": "1", + "fieldname": "allow_print_for_draft", + "fieldtype": "Check", + "label": "Allow Print for Draft" + }, + { + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, + { + "default": "1", + "fieldname": "add_draft_heading", + "fieldtype": "Check", + "label": "Always add \"Draft\" Heading for printing draft documents" + }, + { + "default": "0", + "fieldname": "allow_page_break_inside_tables", + "fieldtype": "Check", + "label": "Allow page break inside tables" + }, + { + "default": "0", + "fieldname": "allow_print_for_cancelled", + "fieldtype": "Check", + "label": "Allow Print for Cancelled" + }, + { + "fieldname": "server_printer", + "fieldtype": "Section Break", + "label": "Print Server" + }, + { + "default": "0", + "depends_on": "enable_print_server", + "fieldname": "enable_print_server", + "fieldtype": "Check", + "label": "Enable Print Server", + "mandatory_depends_on": "enable_print_server" + }, + { + "fieldname": "raw_printing_section", + "fieldtype": "Section Break", + "label": "Raw Printing" + }, + { + "default": "0", + "fieldname": "enable_raw_printing", + "fieldtype": "Check", + "label": "Enable Raw Printing" + }, + { + "fieldname": "print_style_section", + "fieldtype": "Section Break", + "label": "Print Style" + }, + { + "default": "Redesign", + "fieldname": "print_style", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Print Style", + "options": "Print Style" + }, + { + "fieldname": "print_style_preview", + "fieldtype": "HTML", + "label": "Print Style Preview" + }, + { + "fieldname": "section_break_8", + "fieldtype": "Section Break", + "label": "Fonts" + }, + { + "default": "Default", + "fieldname": "font", + "fieldtype": "Select", + "label": "Font", + "options": "Default\nHelvetica Neue\nArial\nHelvetica\nInter\nVerdana\nMonospace" + }, + { + "description": "In points. Default is 9.", + "fieldname": "font_size", + "fieldtype": "Float", + "label": "Font Size" + }, + { + "depends_on": "eval:doc.pdf_page_size == \"Custom\"", + "fieldname": "pdf_page_height", + "fieldtype": "Float", + "label": "PDF Page Height (in mm)" + }, + { + "depends_on": "eval:doc.pdf_page_size == \"Custom\"", + "fieldname": "pdf_page_width", + "fieldtype": "Float", + "label": "PDF Page Width (in mm)" + } + ], + "icon": "fa fa-cog", + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2023-05-30 14:55:25.740691", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/printing/doctype/print_settings/print_settings.py b/xhiveframework/printing/doctype/print_settings/print_settings.py new file mode 100644 index 0000000..e587ff1 --- /dev/null +++ b/xhiveframework/printing/doctype/print_settings/print_settings.py @@ -0,0 +1,83 @@ +# Copyright (c) 2018, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework import _ +from xhiveframework.model.document import Document +from xhiveframework.utils import cint + + +class PrintSettings(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + add_draft_heading: DF.Check + allow_page_break_inside_tables: DF.Check + allow_print_for_cancelled: DF.Check + allow_print_for_draft: DF.Check + enable_print_server: DF.Check + enable_raw_printing: DF.Check + font: DF.Literal["Default", "Helvetica Neue", "Arial", "Helvetica", "Inter", "Verdana", "Monospace"] + font_size: DF.Float + pdf_page_height: DF.Float + pdf_page_size: DF.Literal[ + "A0", + "A1", + "A2", + "A3", + "A4", + "A5", + "A6", + "A7", + "A8", + "A9", + "B0", + "B1", + "B2", + "B3", + "B4", + "B5", + "B6", + "B7", + "B8", + "B9", + "B10", + "C5E", + "Comm10E", + "DLE", + "Executive", + "Folio", + "Ledger", + "Legal", + "Letter", + "Tabloid", + "Custom", + ] + pdf_page_width: DF.Float + print_style: DF.Link | None + repeat_header_footer: DF.Check + send_print_as_pdf: DF.Check + with_letterhead: DF.Check + + # end: auto-generated types + def validate(self): + if self.pdf_page_size == "Custom" and not (self.pdf_page_height and self.pdf_page_width): + xhiveframework.throw(_("Page height and width cannot be zero")) + + def on_update(self): + xhiveframework.clear_cache() + + +@xhiveframework.whitelist() +def is_print_server_enabled(): + if not hasattr(xhiveframework.local, "enable_print_server"): + xhiveframework.local.enable_print_server = cint( + xhiveframework.db.get_single_value("Print Settings", "enable_print_server") + ) + + return xhiveframework.local.enable_print_server diff --git a/xhiveframework/printing/doctype/print_settings/test_print_settings.py b/xhiveframework/printing/doctype/print_settings/test_print_settings.py new file mode 100644 index 0000000..1c18f35 --- /dev/null +++ b/xhiveframework/printing/doctype/print_settings/test_print_settings.py @@ -0,0 +1,7 @@ +# Copyright (c) 2018, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPrintSettings(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/printing/doctype/print_style/__init__.py b/xhiveframework/printing/doctype/print_style/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/doctype/print_style/print_style.js b/xhiveframework/printing/doctype/print_style/print_style.js new file mode 100644 index 0000000..7d0ba8c --- /dev/null +++ b/xhiveframework/printing/doctype/print_style/print_style.js @@ -0,0 +1,10 @@ +// Copyright (c) 2017, XhiveFramework Technologies and contributors +// For license information, please see license.txt + +xhiveframework.ui.form.on("Print Style", { + refresh: function (frm) { + frm.add_custom_button(__("Print Settings"), () => { + xhiveframework.set_route("Form", "Print Settings"); + }); + }, +}); diff --git a/xhiveframework/printing/doctype/print_style/print_style.json b/xhiveframework/printing/doctype/print_style/print_style.json new file mode 100644 index 0000000..1d3c9a6 --- /dev/null +++ b/xhiveframework/printing/doctype/print_style/print_style.json @@ -0,0 +1,75 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:print_style_name", + "creation": "2017-08-17 01:25:56.910716", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "print_style_name", + "disabled", + "standard", + "css", + "preview" + ], + "fields": [ + { + "fieldname": "print_style_name", + "fieldtype": "Data", + "label": "Print Style Name", + "reqd": 1, + "unique": 1 + }, + { + "default": "0", + "fieldname": "disabled", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Disabled" + }, + { + "default": "0", + "fieldname": "standard", + "fieldtype": "Check", + "label": "Standard" + }, + { + "fieldname": "css", + "fieldtype": "Code", + "label": "CSS", + "reqd": 1 + }, + { + "fieldname": "preview", + "fieldtype": "Attach Image", + "hidden": 1, + "label": "Preview" + } + ], + "image_field": "preview", + "links": [], + "modified": "2022-08-03 12:20:51.295775", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Style", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_changes": 1 +} \ No newline at end of file diff --git a/xhiveframework/printing/doctype/print_style/print_style.py b/xhiveframework/printing/doctype/print_style/print_style.py new file mode 100644 index 0000000..9f331ce --- /dev/null +++ b/xhiveframework/printing/doctype/print_style/print_style.py @@ -0,0 +1,39 @@ +# Copyright (c) 2017, XhiveFramework Technologies and contributors +# License: MIT. See LICENSE + +import xhiveframework +from xhiveframework.model.document import Document + + +class PrintStyle(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from xhiveframework.types import DF + + css: DF.Code + disabled: DF.Check + preview: DF.AttachImage | None + print_style_name: DF.Data + standard: DF.Check + + # end: auto-generated types + def validate(self): + if ( + self.standard == 1 + and not xhiveframework.local.conf.get("developer_mode") + and not (xhiveframework.flags.in_import or xhiveframework.flags.in_test) + ): + xhiveframework.throw(xhiveframework._("Standard Print Style cannot be changed. Please duplicate to edit.")) + + def on_update(self): + self.export_doc() + + def export_doc(self): + # export + from xhiveframework.modules.utils import export_module_json + + export_module_json(self, self.standard == 1, "Printing") diff --git a/xhiveframework/printing/doctype/print_style/test_print_style.py b/xhiveframework/printing/doctype/print_style/test_print_style.py new file mode 100644 index 0000000..c3d6592 --- /dev/null +++ b/xhiveframework/printing/doctype/print_style/test_print_style.py @@ -0,0 +1,8 @@ +# Copyright (c) 2017, XhiveFramework Technologies and Contributors +# License: MIT. See LICENSE +import xhiveframework +from xhiveframework.tests.utils import XhiveFrameworkTestCase + + +class TestPrintStyle(XhiveFrameworkTestCase): + pass diff --git a/xhiveframework/printing/form_tour/letter_head/letter_head.json b/xhiveframework/printing/form_tour/letter_head/letter_head.json new file mode 100644 index 0000000..66730b4 --- /dev/null +++ b/xhiveframework/printing/form_tour/letter_head/letter_head.json @@ -0,0 +1,53 @@ +{ + "creation": "2021-11-22 15:26:53.878805", + "docstatus": 0, + "doctype": "Form Tour", + "idx": 0, + "is_standard": 1, + "modified": "2021-11-22 15:26:53.878805", + "modified_by": "Administrator", + "module": "Printing", + "name": "Letter Head", + "owner": "Administrator", + "reference_doctype": "Letter Head", + "save_on_complete": 1, + "steps": [ + { + "description": "Let's name your first Letter Head with your company's name", + "field": "", + "fieldname": "letter_head_name", + "fieldtype": "Data", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Letter Head Name", + "parent_field": "", + "position": "Right", + "title": "Letter Head Name" + }, + { + "description": "Select the image containing only header part of your letter Head.", + "field": "", + "fieldname": "image", + "fieldtype": "Attach Image", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Image", + "parent_field": "", + "position": "Right", + "title": "Image" + }, + { + "description": "You can mark the Letter Head as default", + "field": "", + "fieldname": "is_default", + "fieldtype": "Check", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Default Letter Head", + "parent_field": "", + "position": "Right", + "title": "Default Letter Head" + } + ], + "title": "Letter Head" +} \ No newline at end of file diff --git a/xhiveframework/printing/form_tour/print_format/print_format.json b/xhiveframework/printing/form_tour/print_format/print_format.json new file mode 100644 index 0000000..0f2b303 --- /dev/null +++ b/xhiveframework/printing/form_tour/print_format/print_format.json @@ -0,0 +1,94 @@ +{ + "creation": "2021-11-24 17:31:44.978996", + "docstatus": 0, + "doctype": "Form Tour", + "first_document": 0, + "idx": 0, + "include_name_field": 1, + "is_standard": 1, + "modified": "2021-11-24 17:58:00.807972", + "modified_by": "Administrator", + "module": "Printing", + "name": "Print Format", + "owner": "Administrator", + "reference_doctype": "Print Format", + "save_on_complete": 1, + "steps": [ + { + "description": "Select a Doctype for which you want to create a Print Format", + "field": "", + "fieldname": "doc_type", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "DocType", + "parent_field": "", + "position": "Right", + "title": "Doctype" + }, + { + "description": "You can modify the style of the Print Format from this section", + "field": "", + "fieldname": "section_break_9", + "fieldtype": "Section Break", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Style Settings", + "parent_field": "", + "position": "Top", + "title": "Style Settings" + }, + { + "description": "You can add custom css for your Print Format from this section", + "field": "", + "fieldname": "css", + "fieldtype": "Code", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Custom CSS", + "parent_field": "", + "position": "Top", + "title": "Custom CSS" + }, + { + "description": "Check this if you want to add custom Jinja Code or JavaScript to your Print Format", + "field": "", + "fieldname": "custom_format", + "fieldtype": "Check", + "has_next_condition": 1, + "is_table_field": 0, + "label": "Custom Format", + "next_step_condition": "eval: doc.custom_format", + "parent_field": "", + "position": "Left", + "title": "Custom Format" + }, + { + "description": "Select the type of Print Format", + "field": "", + "fieldname": "print_format_type", + "fieldtype": "Select", + "has_next_condition": 1, + "is_table_field": 0, + "label": "Print Format Type", + "next_step_condition": "eval: doc.custom_format", + "parent_field": "", + "position": "Right", + "title": "Print Format Type" + }, + { + "description": "Enter the code based on the Print Format Type you selected above", + "field": "", + "fieldname": "html", + "fieldtype": "Code", + "has_next_condition": 1, + "is_table_field": 0, + "label": "HTML", + "next_step_condition": "eval:doc.html", + "parent_field": "", + "position": "Right", + "title": "Code" + } + ], + "title": "Print Format" +} \ No newline at end of file diff --git a/xhiveframework/printing/page/__init__.py b/xhiveframework/printing/page/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/page/print/__init__.py b/xhiveframework/printing/page/print/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/page/print/print.js b/xhiveframework/printing/page/print/print.js new file mode 100644 index 0000000..9706eb1 --- /dev/null +++ b/xhiveframework/printing/page/print/print.js @@ -0,0 +1,846 @@ +xhiveframework.pages["print"].on_page_load = function (wrapper) { + xhiveframework.ui.make_app_page({ + parent: wrapper, + }); + + let print_view = new xhiveframework.ui.form.PrintView(wrapper); + + $(wrapper).bind("show", () => { + const route = xhiveframework.get_route(); + const doctype = route[1]; + const docname = route.slice(2).join("/"); + if (!xhiveframework.route_options || !xhiveframework.route_options.frm) { + xhiveframework.model.with_doc(doctype, docname, () => { + let frm = { doctype: doctype, docname: docname }; + frm.doc = xhiveframework.get_doc(doctype, docname); + xhiveframework.model.with_doctype(doctype, () => { + frm.meta = xhiveframework.get_meta(route[1]); + print_view.show(frm); + }); + }); + } else { + print_view.frm = xhiveframework.route_options.frm.doctype + ? xhiveframework.route_options.frm + : xhiveframework.route_options.frm.frm; + xhiveframework.route_options.frm = null; + print_view.show(print_view.frm); + } + }); +}; + +xhiveframework.ui.form.PrintView = class { + constructor(wrapper) { + this.wrapper = $(wrapper); + this.page = wrapper.page; + this.make(); + } + + make() { + this.print_wrapper = this.page.main.empty().html( + ` +
      + +
      + ` + ); + + this.print_settings = xhiveframework.model.get_doc(":Print Settings", "Print Settings"); + this.setup_menu(); + this.setup_toolbar(); + this.setup_sidebar(); + this.setup_keyboard_shortcuts(); + } + + set_title() { + this.page.set_title(this.frm.docname); + } + + setup_toolbar() { + this.page.set_primary_action(__("Print"), () => this.printit(), "printer"); + + this.page.add_button(__("Full Page"), () => this.render_page("/printview?"), { + icon: "full-page", + }); + + this.page.add_button(__("PDF"), () => this.render_pdf(), { icon: "small-file" }); + + this.page.add_button(__("Refresh"), () => this.refresh_print_format(), { + icon: "refresh", + }); + + this.page.add_action_icon( + "es-line-filetype", + () => { + this.go_to_form_view(); + }, + "", + __("Form") + ); + } + + setup_sidebar() { + this.sidebar = this.page.sidebar.addClass("print-preview-sidebar"); + + this.print_format_selector = this.add_sidebar_item({ + fieldtype: "Link", + fieldname: "print_format", + options: "Print Format", + label: __("Print Format"), + get_query: () => { + return { filters: { doc_type: this.frm.doctype } }; + }, + change: () => this.refresh_print_format(), + }).$input; + + this.language_selector = this.add_sidebar_item({ + fieldtype: "Link", + fieldname: "language", + label: __("Language"), + options: "Language", + change: () => { + this.set_user_lang(); + this.preview(); + }, + }).$input; + + let description = ""; + if (!cint(this.print_settings.repeat_header_footer)) { + description = + "
      " + + __("Footer might not be visible as {0} option is disabled
      ", [ + `${__( + "Repeat Header and Footer" + )}`, + ]); + } + const print_view = this; + this.letterhead_selector = this.add_sidebar_item({ + fieldtype: "Link", + fieldname: "letterhead", + options: "Letter Head", + label: __("Letter Head"), + description: description, + change: function () { + this.set_description(this.get_value() ? description : ""); + print_view.preview(); + }, + }).$input; + this.sidebar_dynamic_section = $(`
      `).appendTo( + this.sidebar + ); + } + + add_sidebar_item(df, is_dynamic) { + if (df.fieldtype == "Select") { + df.input_class = "btn btn-default btn-sm text-left"; + } + + let field = xhiveframework.ui.form.make_control({ + df: df, + parent: is_dynamic ? this.sidebar_dynamic_section : this.sidebar, + render_input: 1, + }); + + if (df.default != null) { + field.set_input(df.default); + } + + return field; + } + + setup_menu() { + this.page.clear_menu(); + + this.page.add_menu_item(__("Print Settings"), () => { + xhiveframework.set_route("Form", "Print Settings"); + }); + + if (this.print_settings.enable_raw_printing == "1") { + this.page.add_menu_item(__("Raw Printing Setting"), () => { + this.printer_setting_dialog(); + }); + } + + if (xhiveframework.model.can_create("Print Format")) { + this.page.add_menu_item(__("Customize"), () => this.edit_print_format()); + } + + if (cint(this.print_settings.enable_print_server)) { + this.page.add_menu_item(__("Select Network Printer"), () => + this.network_printer_setting_dialog() + ); + } + } + + show(frm) { + this.frm = frm; + this.set_title(); + this.set_breadcrumbs(); + this.setup_customize_dialog(); + + // print designer link + if (Object.keys(xhiveframework.boot.versions).includes("print_designer")) { + this.page.add_inner_message(` + + ${__("Try the new Print Designer")} + + `); + } else { + this.page.add_inner_message(` + + ${__("Try the new Print Designer")} + + `); + } + let tasks = [ + this.set_default_print_format, + this.set_default_print_language, + this.set_default_letterhead, + this.preview, + ].map((fn) => fn.bind(this)); + + this.setup_additional_settings(); + return xhiveframework.run_serially(tasks); + } + + set_breadcrumbs() { + xhiveframework.breadcrumbs.add(this.frm.meta.module, this.frm.doctype); + } + + setup_additional_settings() { + this.additional_settings = {}; + this.sidebar_dynamic_section.empty(); + xhiveframework + .xcall("xhiveframework.printing.page.print.print.get_print_settings_to_show", { + doctype: this.frm.doc.doctype, + docname: this.frm.doc.name, + }) + .then((settings) => this.add_settings_to_sidebar(settings)); + } + + add_settings_to_sidebar(settings) { + for (let df of settings) { + let field = this.add_sidebar_item( + { + ...df, + change: () => { + const val = field.get_value(); + this.additional_settings[field.df.fieldname] = val; + this.preview(); + }, + }, + true + ); + } + } + + edit_print_format() { + let print_format = this.get_print_format(); + let is_custom_format = + print_format.name && + (print_format.print_format_builder || print_format.print_format_builder_beta) && + print_format.standard === "No"; + let is_standard_but_editable = print_format.name && print_format.custom_format; + + if (is_standard_but_editable) { + xhiveframework.set_route("Form", "Print Format", print_format.name); + return; + } + if (is_custom_format) { + if (print_format.print_format_builder_beta) { + xhiveframework.set_route("print-format-builder-beta", print_format.name); + } else { + xhiveframework.set_route("print-format-builder", print_format.name); + } + return; + } + // start a new print format + xhiveframework.prompt( + [ + { + label: __("New Print Format Name"), + fieldname: "print_format_name", + fieldtype: "Data", + reqd: 1, + }, + { + label: __("Based On"), + fieldname: "based_on", + fieldtype: "Read Only", + default: print_format.name || "Standard", + }, + { + label: __("Use the new Print Format Builder"), + fieldname: "beta", + fieldtype: "Check", + }, + ], + (data) => { + xhiveframework.route_options = { + make_new: true, + doctype: this.frm.doctype, + name: data.print_format_name, + based_on: data.based_on, + beta: data.beta, + }; + xhiveframework.set_route("print-format-builder"); + this.print_format_selector.val(data.print_format_name); + }, + __("New Custom Print Format"), + __("Start") + ); + } + + refresh_print_format() { + this.set_default_print_language(); + this.toggle_raw_printing(); + this.preview(); + } + + // bind_events () { + // // // hide print view on pressing escape, only if there is no focus on any input + // // $(document).on("keydown", function (e) { + // // if (e.which === 27 && me.frm && e.target === document.body) { + // // me.hide(); + // // } + // // }); + // } + + setup_customize_dialog() { + let print_format = this.get_print_format(); + $(document).on("new-print-format", (e) => { + xhiveframework.prompt( + [ + { + label: __("New Print Format Name"), + fieldname: "print_format_name", + fieldtype: "Data", + reqd: 1, + }, + { + label: __("Based On"), + fieldname: "based_on", + fieldtype: "Read Only", + default: print_format.name || "Standard", + }, + ], + (data) => { + xhiveframework.route_options = { + make_new: true, + doctype: this.frm.doctype, + name: data.print_format_name, + based_on: data.based_on, + }; + xhiveframework.set_route("print-format-builder"); + }, + __("New Custom Print Format"), + __("Start") + ); + }); + } + + setup_keyboard_shortcuts() { + this.wrapper.find(".print-toolbar a.btn-default").each((i, el) => { + xhiveframework.ui.keys.get_shortcut_group(this.frm.page).add($(el)); + }); + } + + set_default_letterhead() { + if (this.frm.doc.letter_head) { + this.letterhead_selector.val(this.frm.doc.letter_head); + return; + } + + return xhiveframework.db + .get_value("Letter Head", { disabled: 0, is_default: 1 }, "name") + .then(({ message }) => this.letterhead_selector.val(message.name)); + } + + set_user_lang() { + this.lang_code = this.language_selector.val(); + } + + set_default_print_language() { + let print_format = this.get_print_format(); + this.lang_code = + this.frm.doc.language || print_format.default_print_language || xhiveframework.boot.lang; + this.language_selector.val(this.lang_code); + } + + toggle_raw_printing() { + const is_raw_printing = this.is_raw_printing(); + this.wrapper.find(".btn-print-preview").toggle(!is_raw_printing); + this.wrapper.find(".btn-download-pdf").toggle(!is_raw_printing); + } + + preview() { + let print_format = this.get_print_format(); + if (print_format.print_format_builder_beta) { + this.print_wrapper.find(".print-preview-wrapper").hide(); + this.print_wrapper.find(".preview-beta-wrapper").show(); + this.preview_beta(); + return; + } + + this.print_wrapper.find(".preview-beta-wrapper").hide(); + this.print_wrapper.find(".print-preview-wrapper").show(); + + const $print_format = this.print_wrapper.find("iframe"); + this.$print_format_body = $print_format.contents(); + this.get_print_html((out) => { + if (!out.html) { + out.html = this.get_no_preview_html(); + } + + this.setup_print_format_dom(out, $print_format); + + const print_height = $print_format.get(0).offsetHeight; + const $message = this.wrapper.find(".page-break-message"); + + const print_height_inches = xhiveframework.dom.pixel_to_inches(print_height); + // if contents are large enough, indicate that it will get printed on multiple pages + // Maximum height for an A4 document is 11.69 inches + if (print_height_inches > 11.69) { + $message.text(__("This may get printed on multiple pages")); + } else { + $message.text(""); + } + }); + } + + preview_beta() { + let print_format = this.get_print_format(); + const iframe = this.print_wrapper.find(".preview-beta-wrapper iframe"); + let params = new URLSearchParams({ + doctype: this.frm.doc.doctype, + name: this.frm.doc.name, + print_format: print_format.name, + }); + let letterhead = this.get_letterhead(); + if (letterhead) { + params.append("letterhead", letterhead); + } + iframe.prop("src", `/printpreview?${params.toString()}`); + } + + setup_print_format_dom(out, $print_format) { + this.print_wrapper.find(".print-format-skeleton").remove(); + let base_url = xhiveframework.urllib.get_base_url(); + let print_css = xhiveframework.assets.bundled_asset( + "print.bundle.css", + xhiveframework.utils.is_rtl(this.lang_code) + ); + this.$print_format_body + .find("html") + .attr("dir", xhiveframework.utils.is_rtl(this.lang_code) ? "rtl" : "ltr"); + this.$print_format_body.find("html").attr("lang", this.lang_code); + this.$print_format_body.find("head").html( + ` + ` + ); + + this.$print_format_body + .find("body") + .html(``); + + this.show_footer(); + + this.$print_format_body.find(".print-format").css({ + display: "flex", + flexDirection: "column", + }); + + this.$print_format_body.find(".page-break").css({ + display: "flex", + "flex-direction": "column", + flex: "1", + }); + + setTimeout(() => { + $print_format.height(this.$print_format_body.find(".print-format").outerHeight()); + }, 500); + } + + hide() { + if (this.frm.setup_done && this.frm.page.current_view_name === "print") { + this.frm.page.set_view( + this.frm.page.previous_view_name === "print" + ? "main" + : this.frm.page.previous_view_name || "main" + ); + } + } + + go_to_form_view() { + xhiveframework.route_options = { + frm: this, + }; + xhiveframework.set_route("Form", this.frm.doctype, this.frm.docname); + } + + show_footer() { + // footer is hidden by default as reqd by pdf generation + // simple hack to show it in print preview + + this.$print_format_body.find("#footer-html").attr( + "style", + ` + display: block !important; + order: 1; + margin-top: auto; + padding-top: var(--padding-xl) + ` + ); + } + + printit() { + let me = this; + + if (cint(me.print_settings.enable_print_server)) { + if (localStorage.getItem("network_printer")) { + me.print_by_server(); + } else { + me.network_printer_setting_dialog(() => me.print_by_server()); + } + } else if (me.get_mapped_printer().length === 1) { + // printer is already mapped in localstorage (applies for both raw and pdf ) + if (me.is_raw_printing()) { + me.get_raw_commands(function (out) { + xhiveframework.ui.form + .qz_connect() + .then(function () { + let printer_map = me.get_mapped_printer()[0]; + let data = [out.raw_commands]; + let config = qz.configs.create(printer_map.printer); + return qz.print(config, data); + }) + .then(xhiveframework.ui.form.qz_success) + .catch((err) => { + xhiveframework.ui.form.qz_fail(err); + }); + }); + } else { + xhiveframework.show_alert( + { + message: __('PDF printing via "Raw Print" is not supported.'), + subtitle: __( + "Please remove the printer mapping in Printer Settings and try again." + ), + indicator: "info", + }, + 14 + ); + //Note: need to solve "Error: Cannot parse (FILE) as a PDF file" to enable qz pdf printing. + } + } else if (me.is_raw_printing()) { + // printer not mapped in localstorage and the current print format is raw printing + xhiveframework.show_alert( + { + message: __("Printer mapping not set."), + subtitle: __( + "Please set a printer mapping for this print format in the Printer Settings" + ), + indicator: "warning", + }, + 14 + ); + me.printer_setting_dialog(); + } else { + me.render_page("/printview?", true); + } + } + + print_by_server() { + let me = this; + if (localStorage.getItem("network_printer")) { + xhiveframework.call({ + method: "xhiveframework.utils.print_format.print_by_server", + args: { + doctype: me.frm.doc.doctype, + name: me.frm.doc.name, + printer_setting: localStorage.getItem("network_printer"), + print_format: me.selected_format(), + no_letterhead: me.with_letterhead(), + letterhead: me.get_letterhead(), + }, + callback: function () {}, + }); + } + } + network_printer_setting_dialog(callback) { + xhiveframework.call({ + method: "xhiveframework.printing.doctype.network_printer_settings.network_printer_settings.get_network_printer_settings", + callback: function (r) { + if (r.message) { + let d = new xhiveframework.ui.Dialog({ + title: __("Select Network Printer"), + fields: [ + { + label: "Printer", + fieldname: "printer", + fieldtype: "Select", + reqd: 1, + options: r.message, + }, + ], + primary_action: function () { + localStorage.setItem("network_printer", d.get_values().printer); + if (typeof callback == "function") { + callback(); + } + d.hide(); + }, + primary_action_label: __("Select"), + }); + d.show(); + } + }, + }); + } + + render_pdf() { + let print_format = this.get_print_format(); + if (print_format.print_format_builder_beta) { + let params = new URLSearchParams({ + doctype: this.frm.doc.doctype, + name: this.frm.doc.name, + print_format: print_format.name, + letterhead: this.get_letterhead(), + }); + let w = window.open(`/api/method/xhiveframework.utils.weasyprint.download_pdf?${params}`); + if (!w) { + xhiveframework.msgprint(__("Please enable pop-ups")); + return; + } + } else { + this.render_page("/api/method/xhiveframework.utils.print_format.download_pdf?"); + } + } + + render_page(method, printit = false) { + let w = window.open( + xhiveframework.urllib.get_full_url( + method + + "doctype=" + + encodeURIComponent(this.frm.doc.doctype) + + "&name=" + + encodeURIComponent(this.frm.doc.name) + + (printit ? "&trigger_print=1" : "") + + "&format=" + + encodeURIComponent(this.selected_format()) + + "&no_letterhead=" + + (this.with_letterhead() ? "0" : "1") + + "&letterhead=" + + encodeURIComponent(this.get_letterhead()) + + "&settings=" + + encodeURIComponent(JSON.stringify(this.additional_settings)) + + (this.lang_code ? "&_lang=" + this.lang_code : "") + ) + ); + if (!w) { + xhiveframework.msgprint(__("Please enable pop-ups")); + return; + } + } + + get_print_html(callback) { + let print_format = this.get_print_format(); + if (print_format.raw_printing) { + callback({ + html: this.get_no_preview_html(), + }); + return; + } + if (this._req) { + this._req.abort(); + } + this._req = xhiveframework.call({ + method: "xhiveframework.www.printview.get_html_and_style", + args: { + doc: this.frm.doc, + print_format: this.selected_format(), + no_letterhead: !this.with_letterhead() ? 1 : 0, + letterhead: this.get_letterhead(), + settings: this.additional_settings, + _lang: this.lang_code, + }, + callback: function (r) { + if (!r.exc) { + callback(r.message); + } + }, + }); + } + + get_letterhead() { + return this.letterhead_selector.val() || __("No Letterhead"); + } + + get_no_preview_html() { + return `
      + ${__("No Preview Available")} +
      `; + } + + get_raw_commands(callback) { + // fetches rendered raw commands from the server for the current print format. + xhiveframework.call({ + method: "xhiveframework.www.printview.get_rendered_raw_commands", + args: { + doc: this.frm.doc, + print_format: this.selected_format(), + _lang: this.lang_code, + }, + callback: function (r) { + if (!r.exc) { + callback(r.message); + } + }, + }); + } + + get_mapped_printer() { + // returns a list of "print format: printer" mapping filtered by the current print format + let print_format_printer_map = this.get_print_format_printer_map(); + if (print_format_printer_map[this.frm.doctype]) { + return print_format_printer_map[this.frm.doctype].filter( + (printer_map) => printer_map.print_format == this.selected_format() + ); + } else { + return []; + } + } + + get_print_format_printer_map() { + // returns the whole object "print_format_printer_map" stored in the localStorage. + try { + return JSON.parse(localStorage.print_format_printer_map); + } catch (e) { + return {}; + } + } + + set_default_print_format() { + if ( + xhiveframework.meta + .get_print_formats(this.frm.doctype) + .includes(this.print_format_selector.val()) || + !this.frm.meta.default_print_format + ) + return; + + this.print_format_selector.empty(); + this.print_format_selector.val(this.frm.meta.default_print_format); + } + + selected_format() { + return this.print_format_selector.val() || "Standard"; + } + + is_raw_printing(format) { + return this.get_print_format(format).raw_printing === 1; + } + + get_print_format(format) { + let print_format = {}; + if (!format) { + format = this.selected_format(); + } + + if (locals["Print Format"] && locals["Print Format"][format]) { + print_format = locals["Print Format"][format]; + } + + return print_format; + } + + with_letterhead() { + return cint(this.get_letterhead() !== __("No Letterhead")); + } + + set_style(style) { + xhiveframework.dom.set_style(style || xhiveframework.boot.print_css, "print-style"); + } + + printer_setting_dialog() { + // dialog for the Printer Settings + this.print_format_printer_map = this.get_print_format_printer_map(); + this.data = this.print_format_printer_map[this.frm.doctype] || []; + this.printer_list = []; + xhiveframework.ui.form.qz_get_printer_list().then((data) => { + this.printer_list = data; + const dialog = new xhiveframework.ui.Dialog({ + title: __("Printer Settings"), + fields: [ + { + fieldtype: "Section Break", + }, + { + fieldname: "printer_mapping", + fieldtype: "Table", + label: __("Printer Mapping"), + in_place_edit: true, + data: this.data, + get_data: () => { + return this.data; + }, + fields: [ + { + fieldtype: "Select", + fieldname: "print_format", + default: 0, + options: xhiveframework.meta.get_print_formats(this.frm.doctype), + read_only: 0, + in_list_view: 1, + label: __("Print Format"), + }, + { + fieldtype: "Select", + fieldname: "printer", + default: 0, + options: this.printer_list, + read_only: 0, + in_list_view: 1, + label: __("Printer"), + }, + ], + }, + ], + primary_action: () => { + let printer_mapping = dialog.get_values()["printer_mapping"]; + if (printer_mapping && printer_mapping.length) { + let print_format_list = printer_mapping.map((a) => a.print_format); + let has_duplicate = print_format_list.some( + (item, idx) => print_format_list.indexOf(item) != idx + ); + if (has_duplicate) + xhiveframework.throw( + __( + "Cannot have multiple printers mapped to a single print format." + ) + ); + } else { + printer_mapping = []; + } + dialog.print_format_printer_map = this.get_print_format_printer_map(); + dialog.print_format_printer_map[this.frm.doctype] = printer_mapping; + localStorage.print_format_printer_map = JSON.stringify( + dialog.print_format_printer_map + ); + dialog.hide(); + }, + primary_action_label: __("Save"), + }); + dialog.show(); + if (!(this.printer_list && this.printer_list.length)) { + xhiveframework.throw(__("No Printer is Available.")); + } + }); + } +}; diff --git a/xhiveframework/printing/page/print/print.json b/xhiveframework/printing/page/print/print.json new file mode 100644 index 0000000..bea659c --- /dev/null +++ b/xhiveframework/printing/page/print/print.json @@ -0,0 +1,18 @@ +{ + "content": null, + "creation": "2020-10-09 17:23:15.163030", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2020-10-09 17:23:15.163030", + "modified_by": "Administrator", + "module": "Printing", + "name": "print", + "owner": "Administrator", + "page_name": "Print", + "roles": [], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0 +} \ No newline at end of file diff --git a/xhiveframework/printing/page/print/print.py b/xhiveframework/printing/page/print/print.py new file mode 100644 index 0000000..7dd333c --- /dev/null +++ b/xhiveframework/printing/page/print/print.py @@ -0,0 +1,22 @@ +import xhiveframework + + +@xhiveframework.whitelist() +def get_print_settings_to_show(doctype, docname): + doc = xhiveframework.get_doc(doctype, docname) + print_settings = xhiveframework.get_single("Print Settings") + + if hasattr(doc, "get_print_settings"): + fields = doc.get_print_settings() or [] + else: + return [] + + print_settings_fields = [] + for fieldname in fields: + df = print_settings.meta.get_field(fieldname) + if not df: + continue + df.default = print_settings.get(fieldname) + print_settings_fields.append(df) + + return print_settings_fields diff --git a/xhiveframework/printing/page/print/print_skeleton_loading.html b/xhiveframework/printing/page/print/print_skeleton_loading.html new file mode 100644 index 0000000..c1e6a0d --- /dev/null +++ b/xhiveframework/printing/page/print/print_skeleton_loading.html @@ -0,0 +1,164 @@ + diff --git a/xhiveframework/printing/page/print_format_builder/__init__.py b/xhiveframework/printing/page/print_format_builder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder.css b/xhiveframework/printing/page/print_format_builder/print_format_builder.css new file mode 100644 index 0000000..b2ca181 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder.css @@ -0,0 +1,161 @@ +.print-format-builder-section { + border: 1px solid var(--dark-border-color); + border-radius: var(--border-radius); + margin: 0px; + margin-bottom: var(--margin-md); +} + +.print-format-builder-add-section, .print-format-builder-header { + border: 1px dashed var(--dark-border-color); + border-radius: var(--border-radius); + padding: var(--padding-sm); + width: 100%; + display: inline-block; + background: var(--bg-color); + cursor: pointer; +} + +.print-format-builder-header-edit { + margin-bottom: var(--margin-sm); +} + +.print-format-builder-header { + margin-bottom: var(--margin-md); +} + +.print-format-builder-add-section { + color: var(--text-light); + align-items: center; + padding: var(--padding-lg); + display: flex; + justify-content: center; +} + +.print-format-builder-add-section .icon { + margin-right: var(--margin-sm); +} + +.print-format-builder-column { + border-radius: var(--border-radius); +} + +.print-format-builder-section .section-column { + padding: var(--padding-xs) 0 var(--padding-md) var(--padding-md); +} + +.print-format-builder-section .section-column:last-child { + padding-right: var(--padding-md); +} + +.print-format-builder-field { + padding: 8px; + width: 100%; + display: inline-block; + border-radius: var(--border-radius); + background: var(--bg-light-gray); + margin-bottom: var(--margin-sm); + font-size: var(--text-md); + color: var(--text-color); +} + +.print-format-builder-field:last-child { + margin-bottom: 0; +} + +.print-format-builder-field .field-label { + vertical-align: middle; +} + +.print-format-builder-column .print-format-builder-field { + cursor: move; +} + +.print-format-builder-section-head .section-label { + font-size: var(--text-lg); + font-weight: var(--weight-medium); + letter-spacing: 0.015em; + color: var(--text-color); + vertical-align: middle; + margin-left: var(--margin-sm); +} + +.print-format-builder-section-head { + cursor: move; + padding: var(--padding-md) calc(var(--padding-md) + 8px) + var(--padding-sm) calc(var(--padding-md) + 8px); +} + +.column-selector-row { + margin-bottom: var(--margin-xs); + padding: var(--padding-xs) 0; + cursor: grab; +} + +.column-selector-row:hover { + background-color: var(--highlight-color); +} + +.column-selector-row .drag-handle { + margin-right: var(--margin-sm); +} + +.print-format-builder-field .drag-handle { + margin-right: var(--margin-sm); +} + +.print-format-builder-sidebar .sidebar-field { + width: 100%; + padding: 4px 8px; + color: var(--text-on-light-gray); + /* color: var(--text-light); */ + text-align: left; + cursor: grab; +} + +.print-format-builder-sidebar .sidebar-custom-field { + background-color: var(--gray-300); +} + +.print-format-builder-sidebar { + top: calc(var(--navbar-height) + 70px); + position: sticky; +} + +.print-format-builder-sidebar-fields { + padding: var(--padding-xs); + overflow-y: auto; + height: 75vh; +} + +.print-format-builder-field-placeholder { + margin-bottom: var(--margin-sm); +} + +.print-format-builder-field-placeholder:last-child { + margin-bottom: 0; +} + +.print-format-builder-field-placeholder .drag-handle { + margin-right: var(--margin-sm); +} + +.filter-searchbox { + padding: 0 var(--padding-xs); + margin-bottom: var(--margin-sm); +} + +.filter-searchbox input { + background-color: var(--control-bg-on-gray); +} + +.print-format-builder-main { + display: inline-block; + vertical-align: top; + border-top: 0px; + padding: var(--padding-lg); +} + +.print-format-help-message { + font-size: var(--text-md); + margin-bottom: var(--margin-md); +} diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder.js b/xhiveframework/printing/page/print_format_builder/print_format_builder.js new file mode 100644 index 0000000..32fc607 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder.js @@ -0,0 +1,849 @@ +xhiveframework.pages["print-format-builder"].on_page_load = function (wrapper) { + xhiveframework.print_format_builder = new xhiveframework.PrintFormatBuilder(wrapper); + xhiveframework.breadcrumbs.add("Setup", "Print Format"); +}; + +xhiveframework.pages["print-format-builder"].on_page_show = function (wrapper) { + var route = xhiveframework.get_route(); + if (route.length > 1) { + xhiveframework.model.with_doc("Print Format", route[1], function () { + xhiveframework.print_format_builder.print_format = xhiveframework.get_doc("Print Format", route[1]); + xhiveframework.print_format_builder.refresh(); + }); + } else if (xhiveframework.route_options) { + if (xhiveframework.route_options.make_new) { + let { doctype, name, based_on, beta } = xhiveframework.route_options; + xhiveframework.route_options = null; + xhiveframework.print_format_builder.setup_new_print_format(doctype, name, based_on, beta); + } else { + xhiveframework.print_format_builder.print_format = xhiveframework.route_options.doc; + xhiveframework.route_options = null; + xhiveframework.print_format_builder.refresh(); + } + } +}; + +xhiveframework.PrintFormatBuilder = class PrintFormatBuilder { + constructor(parent) { + this.parent = parent; + this.make(); + this.refresh(); + } + refresh() { + this.custom_html_count = 0; + if (!this.print_format) { + this.show_start(); + } else { + this.page.set_title(this.print_format.name); + this.setup_print_format(); + } + } + make() { + this.page = xhiveframework.ui.make_app_page({ + parent: this.parent, + title: __("Print Format Builder"), + }); + + this.page.main.css({ "border-color": "transparent" }); + + this.page.sidebar = $('').appendTo( + this.page.sidebar + ); + this.page.main = $( + '' + ).appendTo(this.page.main); + + // future-bindings for buttons on sections / fields + // bind only once + this.setup_section_settings(); + this.setup_column_selector(); + this.setup_edit_custom_html(); + // $(this.page.sidebar).css({"position": 'fixed'}); + // $(this.page.main).parent().css({"margin-left": '16.67%'}); + } + show_start() { + this.page.main.html(xhiveframework.render_template("print_format_builder_start", {})); + this.page.clear_actions(); + this.page.set_title(__("Print Format Builder")); + this.start_edit_print_format(); + this.start_new_print_format(); + } + start_edit_print_format() { + // print format control + var me = this; + this.print_format_input = xhiveframework.ui.form.make_control({ + parent: this.page.main.find(".print-format-selector"), + df: { + fieldtype: "Link", + options: "Print Format", + filters: { + print_format_builder: 1, + }, + label: __("Select Print Format to Edit"), + only_select: true, + }, + render_input: true, + }); + + // create a new print format. + this.page.main.find(".btn-edit-print-format").on("click", function () { + var name = me.print_format_input.get_value(); + if (!name) return; + xhiveframework.model.with_doc("Print Format", name, function (doc) { + xhiveframework.set_route("print-format-builder", name); + }); + }); + } + start_new_print_format() { + var me = this; + this.doctype_input = xhiveframework.ui.form.make_control({ + parent: this.page.main.find(".doctype-selector"), + df: { + fieldtype: "Link", + options: "DocType", + filters: { + istable: 0, + issingle: 0, + }, + label: __("Select a DocType to make a new format"), + }, + render_input: true, + }); + + this.name_input = xhiveframework.ui.form.make_control({ + parent: this.page.main.find(".name-selector"), + df: { + fieldtype: "Data", + label: __("Name of the new Print Format"), + }, + render_input: true, + }); + + this.page.main.find(".btn-new-print-format").on("click", function () { + var doctype = me.doctype_input.get_value(), + name = me.name_input.get_value(); + if (!(doctype && name)) { + xhiveframework.msgprint(__("Both DocType and Name required")); + return; + } + me.setup_new_print_format(doctype, name); + }); + } + setup_new_print_format(doctype, name, based_on, beta) { + xhiveframework.call({ + method: "xhiveframework.printing.page.print_format_builder.print_format_builder.create_custom_format", + args: { + doctype: doctype, + name: name, + based_on: based_on, + beta: Boolean(beta), + }, + callback: (r) => { + if (r.message) { + let print_format = r.message; + if (print_format.print_format_builder_beta) { + xhiveframework.set_route("print-format-builder-beta", print_format.name); + } else { + this.print_format = print_format; + this.refresh(); + } + } + }, + }); + } + setup_print_format() { + var me = this; + xhiveframework.model.with_doctype(this.print_format.doc_type, function (doctype) { + me.meta = xhiveframework.get_meta(me.print_format.doc_type); + me.setup_sidebar(); + me.render_layout(); + me.page.set_primary_action(__("Save"), function () { + me.save_print_format(); + }); + me.page.clear_menu(); + me.page.add_menu_item( + __("Start new Format"), + function () { + me.print_format = null; + me.refresh(); + }, + true + ); + me.page.clear_inner_toolbar(); + me.page.add_inner_button(__("Edit Properties"), function () { + xhiveframework.set_route("Form", "Print Format", me.print_format.name); + }); + }); + } + setup_sidebar() { + // prepend custom HTML field + var fields = [this.get_custom_html_field()].concat(this.meta.fields); + this.page.sidebar.html( + $(xhiveframework.render_template("print_format_builder_sidebar", { fields: fields })) + ); + this.setup_field_filter(); + } + get_custom_html_field() { + return { + fieldtype: "Custom HTML", + fieldname: "_custom_html", + label: __("Custom HTML"), + }; + } + render_layout() { + this.page.main.empty(); + this.prepare_data(); + $( + xhiveframework.render_template("print_format_builder_layout", { + data: this.layout_data, + me: this, + }) + ).appendTo(this.page.main); + this.setup_sortable(); + this.setup_add_section(); + this.setup_edit_heading(); + this.setup_field_settings(); + this.setup_html_data(); + } + prepare_data() { + this.print_heading_template = null; + this.data = JSON.parse(this.print_format.format_data || "[]"); + if (!this.data.length) { + // new layout + this.data = this.meta.fields; + } else { + // extract print_heading_template if found + if (this.data[0].fieldname === "print_heading_template") { + this.print_heading_template = this.data[0].options; + this.data = this.data.splice(1); + } + } + + if (!this.print_heading_template) { + // default print heading template + this.print_heading_template = + ''; + } + + this.layout_data = []; + this.fields_dict = {}; + this.custom_html_dict = {}; + var section = null, + column = null, + me = this, + custom_html_count = 0; + + // create a new placeholder for column and set + // it as "column" + var set_column = function () { + if (!section) set_section(); + column = me.get_new_column(); + section.columns.push(column); + section.no_of_columns += 1; + }; + + var set_section = function (label) { + section = me.get_new_section(); + if (label) section.label = label; + column = null; + me.layout_data.push(section); + }; + + // break the layout into sections and columns + // so that it is easier to render in a template + $.each(this.data, function (i, f) { + me.fields_dict[f.fieldname] = f; + if (!f.name && f.fieldname) { + // from format_data (designed format) + // print_hide should always be false + if (f.fieldname === "_custom_html") { + f.label = "Custom HTML"; + f.fieldtype = "Custom HTML"; + + // set custom html id to map data properties later + custom_html_count++; + f.custom_html_id = custom_html_count; + me.custom_html_dict[f.custom_html_id] = f; + } else { + f = $.extend( + xhiveframework.meta.get_docfield(me.print_format.doc_type, f.fieldname) || {}, + f + ); + } + } + + if (f.fieldtype === "Section Break") { + set_section(f.label); + } else if (f.fieldtype === "Column Break") { + set_column(); + } else if (!xhiveframework.model.layout_fields.includes(f.fieldtype)) { + if (!column) set_column(); + + if (f.fieldtype === "Table") { + me.add_table_properties(f); + } + + if (!f.print_hide) { + column.fields.push(f); + section.has_fields = true; + } + } + }); + + // strip out empty sections + this.layout_data = $.map(this.layout_data, function (s) { + return s.has_fields ? s : null; + }); + } + get_new_section() { + return { columns: [], no_of_columns: 0, label: "" }; + } + get_new_column() { + return { fields: [] }; + } + add_table_properties(f) { + // build table columns and widths in a dict + // visible_columns + var me = this; + if (!f.visible_columns) { + me.init_visible_columns(f); + } + } + init_visible_columns(f) { + f.visible_columns = []; + $.each(xhiveframework.get_meta(f.options).fields, function (i, _f) { + if ( + !["Section Break", "Column Break", "Tab Break"].includes(_f.fieldtype) && + !_f.print_hide && + f.label + ) { + // column names set as fieldname|width + f.visible_columns.push({ + fieldname: _f.fieldname, + print_width: _f.width || "", + print_hide: 0, + }); + } + }); + } + setup_sortable() { + var me = this; + + // drag from fields library + Sortable.create(this.page.sidebar.find(".print-format-builder-sidebar-fields").get(0), { + group: { + name: "field", + put: true, + pull: "clone", + }, + sort: false, + onAdd: function (evt) { + // on drop, trash! + $(evt.item).fadeOut(); + }, + }); + + // sort, drag and drop between columns + this.page.main.find(".print-format-builder-column").each(function () { + me.setup_sortable_for_column(this); + }); + + // section sorting + Sortable.create(this.page.main.find(".print-format-builder-layout").get(0), { + handle: ".print-format-builder-section-head", + }); + } + setup_sortable_for_column(col) { + var me = this; + Sortable.create(col, { + group: { + name: "field", + put: true, + pull: true, + }, + onAdd: function (evt) { + // on drop, change the HTML + + var $item = $(evt.item); + if (!$item.hasClass("print-format-builder-field")) { + var fieldname = $item.attr("data-fieldname"); + + let field; + if (fieldname === "_custom_html") { + field = me.get_custom_html_field(); + } else { + field = xhiveframework.meta.get_docfield(me.print_format.doc_type, fieldname); + } + + var html = xhiveframework.render_template("print_format_builder_field", { + field: field, + me: me, + }); + + $item.replaceWith(html); + } + }, + }); + } + setup_field_filter() { + var me = this; + this.page.sidebar.find(".filter-fields").on("keyup", function () { + var text = $(this).val(); + me.page.sidebar.find(".field-label").each(function () { + var show = + !text || $(this).text().toLowerCase().indexOf(text.toLowerCase()) !== -1; + $(this).parent().toggle(show); + }); + }); + } + setup_section_settings() { + var me = this; + this.page.main.on("click", ".section-settings", function () { + var section = $(this).parent().parent(); + var no_of_columns = section.find(".section-column").length; + var label = section.attr("data-label"); + + // new dialog + var d = new xhiveframework.ui.Dialog({ + title: "Edit Section", + fields: [ + { + label: __("No of Columns"), + fieldname: "no_of_columns", + fieldtype: "Select", + options: ["1", "2", "3", "4"], + }, + { + label: __("Section Heading"), + fieldname: "label", + fieldtype: "Data", + description: __("Will only be shown if section headings are enabled"), + }, + { + label: __("Remove Section"), + fieldname: "remove_section", + fieldtype: "Button", + click: function () { + d.hide(); + section.fadeOut(function () { + section.remove(); + }); + }, + input_class: "btn-danger", + input_css: { + "margin-top": "20px", + }, + }, + ], + }); + + d.set_input("no_of_columns", no_of_columns + ""); + d.set_input("label", label || ""); + + d.set_primary_action(__("Update"), function () { + // resize number of columns + me.update_columns_in_section( + section, + no_of_columns, + cint(d.get_value("no_of_columns")) + ); + + section.attr("data-label", d.get_value("label") || ""); + section.find(".section-label").html(d.get_value("label") || ""); + + d.hide(); + }); + + d.show(); + + return false; + }); + } + setup_field_settings() { + this.page.main.find(".field-settings").on("click", (e) => { + const field = $(e.currentTarget).parent(); + // new dialog + var d = new xhiveframework.ui.Dialog({ + title: __("Set Properties"), + fields: [ + { + label: __("Label"), + fieldname: "label", + fieldtype: "Data", + }, + { + label: __("Align Value"), + fieldname: "align", + fieldtype: "Select", + options: [ + { label: __("Left", null, "alignment"), value: "left" }, + { label: __("Right", null, "alignment"), value: "right" }, + ], + }, + { + label: __("Remove Field"), + fieldtype: "Button", + click: function () { + d.hide(); + field.remove(); + }, + input_class: "btn-danger", + }, + ], + }); + + d.set_value("label", field.attr("data-label")); + + d.set_primary_action(__("Update"), function () { + field.attr("data-align", d.get_value("align")); + field.attr("data-label", d.get_value("label")); + field.find(".field-label").html(d.get_value("label")); + d.hide(); + }); + + // set current value + if (field.attr("data-align")) { + d.set_value("align", field.attr("data-align")); + } else { + d.set_value("align", "left"); + } + + d.show(); + + return false; + }); + } + setup_html_data() { + // set JQuery `data` for Custom HTML fields, since editing the HTML + // directly causes problem becuase of HTML reformatting + // + // this is based on a dummy attribute custom_html_id, since all custom html + // fields have the same fieldname `_custom_html` + var me = this; + this.page.main.find('[data-fieldtype="Custom HTML"]').each(function () { + var fieldname = $(this).attr("data-fieldname"); + var content = $($(this).find(".html-content")[0]); + var html = me.custom_html_dict[parseInt(content.attr("data-custom-html-id"))].options; + content.data("content", html); + }); + } + update_columns_in_section(section, no_of_columns, new_no_of_columns) { + var col_size = 12 / new_no_of_columns, + me = this, + resize = function () { + section + .find(".section-column") + .removeClass() + .addClass("section-column") + .addClass("col-md-" + col_size); + }; + + if (new_no_of_columns < no_of_columns) { + // move contents of last n columns to previous column + for (var i = no_of_columns; i > new_no_of_columns; i--) { + var $col = $(section.find(".print-format-builder-column").get(i - 1)); + var prev = section.find(".print-format-builder-column").get(i - 2); + + // append each field to prev + $col.parent().addClass("to-drop"); + $col.find(".print-format-builder-field").each(function () { + $(this).appendTo(prev); + }); + } + + // drop columns + section.find(".to-drop").remove(); + + // resize + resize(); + } else if (new_no_of_columns > no_of_columns) { + // add empty column and resize old columns + for (let i = no_of_columns; i < new_no_of_columns; i++) { + var col = $( + '
      \ +
      ' + ).appendTo(section); + me.setup_sortable_for_column(col.find(".print-format-builder-column").get(0)); + } + // resize + resize(); + } + } + setup_add_section() { + var me = this; + this.page.main.find(".print-format-builder-add-section").on("click", function () { + // boostrap new section info + var section = me.get_new_section(); + section.columns.push(me.get_new_column()); + section.no_of_columns = 1; + + var $section = $( + xhiveframework.render_template("print_format_builder_section", { + section: section, + me: me, + }) + ).appendTo(me.page.main.find(".print-format-builder-layout")); + + me.setup_sortable_for_column($section.find(".print-format-builder-column").get(0)); + }); + } + setup_edit_heading() { + var me = this; + var $heading = this.page.main.find(".print-format-builder-print-heading"); + + // set content property + $heading.data("content", this.print_heading_template); + + this.page.main.find(".edit-heading").on("click", function () { + var d = me.get_edit_html_dialog(__("Edit Heading"), __("Heading"), $heading); + }); + } + setup_column_selector() { + var me = this; + this.page.main.on("click", ".select-columns", function () { + var parent = $(this).parents(".print-format-builder-field:first"), + doctype = parent.attr("data-doctype"), + label = parent.attr("data-label"), + columns = parent.attr("data-columns").split(","), + column_names = $.map(columns, function (v) { + return v.split("|")[0]; + }), + widths = {}; + + $.each(columns, function (i, v) { + var parts = v.split("|"); + widths[parts[0]] = parts[1] || ""; + }); + + var d = new xhiveframework.ui.Dialog({ + title: __("Select Table Columns for {0}", [label]), + }); + + var $body = $(d.body); + + var doc_fields = xhiveframework.get_meta(doctype).fields; + var docfields_by_name = {}; + + // docfields by fieldname + $.each(doc_fields, function (j, f) { + if (f) docfields_by_name[f.fieldname] = f; + }); + + // add field which are in column_names first to preserve order + var fields = []; + $.each(column_names, function (i, v) { + if (Object.keys(docfields_by_name).includes(v)) { + fields.push(docfields_by_name[v]); + } + }); + // add remaining fields + $.each(doc_fields, function (j, f) { + if ( + f && + !column_names.includes(f.fieldname) && + !["Section Break", "Column Break", "Tab Break"].includes(f.fieldtype) && + f.label + ) { + fields.push(f); + } + }); + // render checkboxes + $( + xhiveframework.render_template("print_format_builder_column_selector", { + fields: fields, + column_names: column_names, + widths: widths, + }) + ).appendTo(d.body); + + Sortable.create($body.find(".column-selector-list").get(0)); + + var get_width_input = function (fieldname) { + return $body.find(".column-width[data-fieldname='" + fieldname + "']"); + }; + + // update data-columns property on update + d.set_primary_action(__("Update"), function () { + var visible_columns = []; + $body.find("input:checked").each(function () { + var fieldname = $(this).attr("data-fieldname"), + width = get_width_input(fieldname).val() || ""; + visible_columns.push(fieldname + "|" + width); + }); + parent.attr("data-columns", visible_columns.join(",")); + d.hide(); + }); + + let update_column_count_message = () => { + // show a warning if user selects more than 10 columns for a table + let columns_count = $body.find("input:checked").length; + $body.find(".help-message").toggle(columns_count > 10); + }; + update_column_count_message(); + + // enable / disable input based on selection + $body.on("click", "input[type='checkbox']", function () { + var disabled = !$(this).prop("checked"), + input = get_width_input($(this).attr("data-fieldname")); + + input.prop("disabled", disabled); + if (disabled) input.val(""); + + update_column_count_message(); + }); + + d.show(); + + return false; + }); + } + get_visible_columns_string(f) { + if (!f.visible_columns) { + this.init_visible_columns(f); + } + return $.map(f.visible_columns, function (v) { + return v.fieldname + "|" + (v.print_width || ""); + }).join(","); + } + get_no_content() { + return __("Edit to add content"); + } + setup_edit_custom_html() { + var me = this; + this.page.main.on("click", ".edit-html", function () { + me.get_edit_html_dialog( + __("Edit Custom HTML"), + __("Custom HTML"), + $(this).parents(".print-format-builder-field:first").find(".html-content") + ); + }); + } + get_edit_html_dialog(title, label, $content) { + var me = this; + var d = new xhiveframework.ui.Dialog({ + title: title, + fields: [ + { + fieldname: "content", + fieldtype: "Code", + label: label, + options: "HTML", + }, + { + fieldname: "help", + fieldtype: "HTML", + options: + "

      " + + __( + "You can add dynamic properties from the document by using Jinja templating." + ) + + __("For example: If you want to include the document ID, use {0}", [ + "{{ doc.name }}", + ]) + + "

      ", + }, + ], + }); + + // set existing content in input + var content = $content.data("content") || ""; + if (content.indexOf(me.get_no_content()) !== -1) content = ""; + d.set_input("content", content); + + d.set_primary_action(__("Update"), function () { + $($content[0]).data("content", d.get_value("content")); + $content.html(d.get_value("content")); + d.hide(); + }); + + d.show(); + + return d; + } + save_print_format() { + var data = [], + me = this; + + // add print heading as the first field + // this will be removed and set as a doc property + // before rendering + data.push({ + fieldname: "print_heading_template", + fieldtype: "Custom HTML", + options: this.page.main.find(".print-format-builder-print-heading").data("content"), + }); + + // add pages + this.page.main.find(".print-format-builder-section").each(function () { + var section = { fieldtype: "Section Break", label: $(this).attr("data-label") || "" }; + data.push(section); + $(this) + .find(".print-format-builder-column") + .each(function () { + data.push({ fieldtype: "Column Break" }); + $(this) + .find(".print-format-builder-field") + .each(function () { + var $this = $(this), + fieldtype = $this.attr("data-fieldtype"), + align = $this.attr("data-align"), + label = $this.attr("data-label"), + df = { + fieldname: $this.attr("data-fieldname"), + print_hide: 0, + }; + + if (align) { + df.align = align; + } + + if (label) { + df.label = label; + } + + if (fieldtype === "Table") { + // append the user selected columns to visible_columns + var columns = $this.attr("data-columns").split(","); + df.visible_columns = []; + $.each(columns, function (i, c) { + var parts = c.split("|"); + df.visible_columns.push({ + fieldname: parts[0], + print_width: parts[1], + print_hide: 0, + }); + }); + } + if (fieldtype === "Custom HTML") { + // custom html as HTML field + df.fieldtype = "HTML"; + df.options = $($this.find(".html-content")[0]).data("content"); + } + data.push(df); + }); + }); + }); + + // save format_data + xhiveframework.call({ + method: "xhiveframework.client.set_value", + args: { + doctype: "Print Format", + name: this.print_format.name, + fieldname: "format_data", + value: JSON.stringify(data), + }, + freeze: true, + btn: this.page.btn_primary, + callback: function (r) { + me.print_format = r.message; + locals["Print Format"][me.print_format.name] = r.message; + xhiveframework.show_alert({ message: __("Saved"), indicator: "green" }); + }, + }); + } +}; diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder.json b/xhiveframework/printing/page/print_format_builder/print_format_builder.json new file mode 100644 index 0000000..81cb213 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder.json @@ -0,0 +1,23 @@ +{ + "content": null, + "creation": "2015-01-27 04:35:43.872918", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2017-05-03 05:59:33.702308", + "modified_by": "Administrator", + "module": "Printing", + "name": "print-format-builder", + "owner": "Administrator", + "page_name": "print-format-builder", + "roles": [ + { + "role": "System Manager" + } + ], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0, + "title": "Print Format Builder" +} \ No newline at end of file diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder.py b/xhiveframework/printing/page/print_format_builder/print_format_builder.py new file mode 100644 index 0000000..b05bb63 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder.py @@ -0,0 +1,19 @@ +import xhiveframework + + +@xhiveframework.whitelist() +def create_custom_format(doctype, name, based_on="Standard", beta=False): + doc = xhiveframework.new_doc("Print Format") + doc.doc_type = doctype + doc.name = name + beta = xhiveframework.parse_json(beta) + + if beta: + doc.print_format_builder_beta = 1 + else: + doc.print_format_builder = 1 + doc.format_data = ( + xhiveframework.db.get_value("Print Format", based_on, "format_data") if based_on != "Standard" else None + ) + doc.insert() + return doc diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_column_selector.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_column_selector.html new file mode 100644 index 0000000..adc87ff --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_column_selector.html @@ -0,0 +1,36 @@ +

      {{ __("Check columns to select, drag to set order.") }} + {{ __("Widths can be set in px or %.") }}

      +

      + {{ __("Some columns might get cut off when printing to PDF. Try to keep number of columns under 10.") }} +

      +
      +

      {{ __("Column") }}

      +

      {{ __("Width") }}

      +
      +
      + {% for (i=0; i < fields.length; i++) { var f = fields[i]; %} + {% var selected = in_list(column_names, f.fieldname) %} +
      +
      +
      + +
      +
      + +
      +
      +
      + +
      +
      + {% } %} +
      diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_field.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_field.html new file mode 100644 index 0000000..5e900df --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_field.html @@ -0,0 +1,46 @@ + diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_layout.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_layout.html new file mode 100644 index 0000000..a4b3962 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_layout.html @@ -0,0 +1,30 @@ +
      + + + + +
      diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_section.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_section.html new file mode 100644 index 0000000..902c85b --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_section.html @@ -0,0 +1,23 @@ + diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_sidebar.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_sidebar.html new file mode 100644 index 0000000..2b5b040 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_sidebar.html @@ -0,0 +1,21 @@ + + diff --git a/xhiveframework/printing/page/print_format_builder/print_format_builder_start.html b/xhiveframework/printing/page/print_format_builder/print_format_builder_start.html new file mode 100644 index 0000000..dc01bba --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder/print_format_builder_start.html @@ -0,0 +1,18 @@ +
      +

      {%= __("Select an existing format to edit or start a new format.") %}

      +
      +
      + +

      + +

      +
      +
      +
      +
      +

      + +

      +
      diff --git a/xhiveframework/printing/page/print_format_builder_beta/__init__.py b/xhiveframework/printing/page/print_format_builder_beta/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.css b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.css new file mode 100644 index 0000000..0bd8d9c --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.css @@ -0,0 +1,3 @@ +.layout-main-section-wrapper { + margin-bottom: 0; +} diff --git a/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.js b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.js new file mode 100644 index 0000000..1955cb9 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.js @@ -0,0 +1,110 @@ +xhiveframework.pages["print-format-builder-beta"].on_page_load = function (wrapper) { + xhiveframework.ui.make_app_page({ + parent: wrapper, + title: __("Print Format Builder"), + single_column: true, + }); + + // hot reload in development + if (xhiveframework.boot.developer_mode) { + xhiveframework.hot_update = xhiveframework.hot_update || []; + xhiveframework.hot_update.push(() => load_print_format_builder_beta(wrapper)); + } +}; + +xhiveframework.pages["print-format-builder-beta"].on_page_show = function (wrapper) { + load_print_format_builder_beta(wrapper); +}; + +function load_print_format_builder_beta(wrapper) { + let route = xhiveframework.get_route(); + let $parent = $(wrapper).find(".layout-main-section"); + $parent.empty(); + + if (route.length > 1) { + xhiveframework.require("print_format_builder.bundle.js").then(() => { + xhiveframework.print_format_builder = new xhiveframework.ui.PrintFormatBuilder({ + wrapper: $parent, + page: wrapper.page, + print_format: route[1], + }); + }); + } else { + let d = new xhiveframework.ui.Dialog({ + title: __("Create or Edit Print Format"), + fields: [ + { + label: __("Action"), + fieldname: "action", + fieldtype: "Select", + options: [ + { label: __("Create New"), value: "Create" }, + { label: __("Edit Existing"), value: "Edit" }, + ], + change() { + let action = d.get_value("action"); + d.get_primary_btn().text(action === "Create" ? __("Create") : __("Edit")); + }, + }, + { + label: __("Select Document Type"), + fieldname: "doctype", + fieldtype: "Link", + options: "DocType", + filters: { + istable: 0, + }, + reqd: 1, + default: xhiveframework.route_options ? xhiveframework.route_options.doctype : null, + }, + { + label: __("New Print Format Name"), + fieldname: "print_format_name", + fieldtype: "Data", + depends_on: (doc) => doc.action === "Create", + mandatory_depends_on: (doc) => doc.action === "Create", + }, + { + label: __("Select Print Format"), + fieldname: "print_format", + fieldtype: "Link", + options: "Print Format", + only_select: 1, + depends_on: (doc) => doc.action === "Edit", + get_query() { + return { + filters: { + doc_type: d.get_value("doctype"), + print_format_builder_beta: 1, + }, + }; + }, + mandatory_depends_on: (doc) => doc.action === "Edit", + }, + ], + primary_action_label: __("Edit"), + primary_action({ action, doctype, print_format, print_format_name }) { + if (action === "Edit") { + xhiveframework.set_route("print-format-builder-beta", print_format); + } else if (action === "Create") { + d.get_primary_btn().prop("disabled", true); + xhiveframework.db + .insert({ + doctype: "Print Format", + name: print_format_name, + doc_type: doctype, + print_format_builder_beta: 1, + }) + .then((doc) => { + xhiveframework.set_route("print-format-builder-beta", doc.name); + }) + .finally(() => { + d.get_primary_btn().prop("disabled", false); + }); + } + }, + }); + d.set_value("action", "Create"); + d.show(); + } +} diff --git a/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.json b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.json new file mode 100644 index 0000000..a5b1288 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.json @@ -0,0 +1,22 @@ +{ + "content": null, + "creation": "2021-07-10 12:22:16.138485", + "docstatus": 0, + "doctype": "Page", + "idx": 0, + "modified": "2021-07-10 12:22:16.138485", + "modified_by": "Administrator", + "module": "Printing", + "name": "print-format-builder-beta", + "owner": "Administrator", + "page_name": "Print Format Builder Beta", + "roles": [ + { + "role": "System Manager" + } + ], + "script": null, + "standard": "Yes", + "style": null, + "system_page": 0 +} \ No newline at end of file diff --git a/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.py b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.py new file mode 100644 index 0000000..4e37c50 --- /dev/null +++ b/xhiveframework/printing/page/print_format_builder_beta/print_format_builder_beta.py @@ -0,0 +1,18 @@ +# Copyright (c) 2021, XhiveFramework Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + + +import functools + +import xhiveframework + + +@xhiveframework.whitelist() +def get_google_fonts(): + return _get_google_fonts() + + +@functools.lru_cache +def _get_google_fonts(): + file_path = xhiveframework.get_app_path("xhiveframework", "data", "google_fonts.json") + return xhiveframework.parse_json(xhiveframework.read_file(file_path)) diff --git a/xhiveframework/printing/print_style/__init__.py b/xhiveframework/printing/print_style/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/print_style/classic/__init__.py b/xhiveframework/printing/print_style/classic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/print_style/classic/classic.json b/xhiveframework/printing/print_style/classic/classic.json new file mode 100644 index 0000000..f732fd9 --- /dev/null +++ b/xhiveframework/printing/print_style/classic/classic.json @@ -0,0 +1,15 @@ +{ + "creation": "2017-08-17 02:00:12.502887", + "css": "/*\n\tcommon style for whole page\n\tThis should include:\n\t+ page size related settings\n\t+ font family settings\n\t+ line spacing settings\n*/\n.print-format div,\n.print-format span,\n.print-format td,\n.print-format h1,\n.print-format h2,\n.print-format h3,\n.print-format h4 {\n\tfont-family: Georgia, serif;\n}\n\n/* classic format: for-test */", + "disabled": 0, + "docstatus": 0, + "doctype": "Print Style", + "idx": 1, + "modified": "2017-08-18 00:43:48.675833", + "modified_by": "Administrator", + "name": "Classic", + "owner": "Administrator", + "preview": "/assets/xhiveframework/images/help/print-style-classic.png", + "print_style_name": "Classic", + "standard": 1 +} diff --git a/xhiveframework/printing/print_style/modern/__init__.py b/xhiveframework/printing/print_style/modern/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/print_style/modern/modern.json b/xhiveframework/printing/print_style/modern/modern.json new file mode 100644 index 0000000..4508288 --- /dev/null +++ b/xhiveframework/printing/print_style/modern/modern.json @@ -0,0 +1,15 @@ +{ + "creation": "2017-08-17 02:16:58.060374", + "css": ".print-heading {\n\ttext-align: right;\n\ttext-transform: uppercase;\n\tcolor: #666;\n\tpadding-bottom: 20px;\n\tmargin-bottom: 20px;\n\tborder-bottom: 1px solid #d1d8dd;\n}\n\n.print-heading h2 {\n\tfont-size: 24px;\n}\n\n.print-format th {\n\tbackground-color: #eee !important;\n\tborder-bottom: 0px !important;\n}\n\n.print-format .primary.compact-item {\n font-weight: bold;\n}\n\n/* modern format: for-test */", + "disabled": 0, + "docstatus": 0, + "doctype": "Print Style", + "idx": 1, + "modified": "2020-11-10 13:59:09.976381", + "modified_by": "Administrator", + "name": "Modern", + "owner": "Administrator", + "preview": "/assets/xhiveframework/images/help/print-style-modern.png", + "print_style_name": "Modern", + "standard": 1 +} diff --git a/xhiveframework/printing/print_style/monochrome/__init__.py b/xhiveframework/printing/print_style/monochrome/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/print_style/monochrome/monochrome.json b/xhiveframework/printing/print_style/monochrome/monochrome.json new file mode 100644 index 0000000..7d2f925 --- /dev/null +++ b/xhiveframework/printing/print_style/monochrome/monochrome.json @@ -0,0 +1,15 @@ +{ + "creation": "2017-08-17 02:16:20.992989", + "css": ".print-format * {\n\tcolor: #000 !important;\n}\n\n.print-format .alert {\n\tbackground-color: inherit;\n\tborder: 1px dashed #333;\n}\n\n.print-format .table-bordered,\n.print-format .table-bordered > thead > tr > th,\n.print-format .table-bordered > tbody > tr > th,\n.print-format .table-bordered > tfoot > tr > th,\n.print-format .table-bordered > thead > tr > td,\n.print-format .table-bordered > tbody > tr > td,\n.print-format .table-bordered > tfoot > tr > td {\n\tborder: 1px solid #333;\n}\n\n.print-format hr {\n\tborder-top: 1px solid #333;\n}\n\n.print-heading {\n\tborder-bottom: 2px solid #333;\n}\n", + "disabled": 0, + "docstatus": 0, + "doctype": "Print Style", + "idx": 0, + "modified": "2017-08-18 00:44:25.023898", + "modified_by": "Administrator", + "name": "Monochrome", + "owner": "Administrator", + "preview": "/assets/xhiveframework/images/help/print-style-monochrome.png", + "print_style_name": "Monochrome", + "standard": 1 +} diff --git a/xhiveframework/printing/print_style/redesign/__init__.py b/xhiveframework/printing/print_style/redesign/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xhiveframework/printing/print_style/redesign/redesign.json b/xhiveframework/printing/print_style/redesign/redesign.json new file mode 100644 index 0000000..952b667 --- /dev/null +++ b/xhiveframework/printing/print_style/redesign/redesign.json @@ -0,0 +1,14 @@ +{ + "creation": "2020-10-22 00:00:08.161999", + "css": ".print-format {\n font-size: 13px;\n background: white;\n}\n\n.print-heading {\n border-bottom: 1px solid #f4f5f6;\n padding-bottom: 5px;\n margin-bottom: 10px;\n}\n\n.print-heading h2 {\n font-size: 24px;\n}\n\n.print-heading h2 div {\n font-weight: 600;\n}\n\n.print-heading small {\n font-size: 13px !important;\n font-weight: normal;\n line-height: 2.5;\n color: #4c5a67;\n}\n\n.print-format .letter-head {\n margin-bottom: 30px;\n}\n\n.print-format label {\n font-weight: normal;\n font-size: 13px;\n color: #4C5A67;\n margin-bottom: 0;\n}\n\n.print-format .data-field {\n margin-top: 0;\n margin-bottom: 0;\n}\n\n.print-format .value {\n color: #192734;\n line-height: 1.8;\n}\n\n.print-format .section-break:not(:last-child) {\n margin-bottom: 0;\n}\n\n.print-format .row:not(.section-break) {\n line-height: 1.6;\n margin-top: 15px !important;\n}\n\n.print-format .important .value {\n font-size: 13px;\n font-weight: 600;\n}\n\n.print-format th {\n color: #74808b;\n font-weight: normal;\n border-bottom-width: 1px !important;\n}\n\n.print-format .table-bordered td, .print-format .table-bordered th {\n border: 1px solid #f4f5f6;\n}\n\n.print-format .table-bordered {\n border: 1px solid #f4f5f6;\n}\n\n.print-format td, .print-format th {\n padding: 10px !important;\n}\n\n.print-format .primary.compact-item {\n font-weight: normal;\n}\n\n.print-format table td .value {\n font-size: 12px;\n line-height: 1.8;\n}\n", + "disabled": 0, + "docstatus": 0, + "doctype": "Print Style", + "idx": 0, + "modified": "2020-12-14 17:56:37.421390", + "modified_by": "Administrator", + "name": "Redesign", + "owner": "Administrator", + "print_style_name": "Redesign", + "standard": 1 +} \ No newline at end of file diff --git a/xhiveframework/public/css/bootstrap.css b/xhiveframework/public/css/bootstrap.css new file mode 100644 index 0000000..ab16552 --- /dev/null +++ b/xhiveframework/public/css/bootstrap.css @@ -0,0 +1,5819 @@ +/*! + * Bootstrap v3.3.1 (http://getbootstrap.com) + * Copyright 2011-2015 Twitter, Inc. + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + */ + +/*! normalize.css v3.0.2 | MIT License | git.io/normalize */ +html { + font-family: sans-serif; + -webkit-text-size-adjust: 100%; + -ms-text-size-adjust: 100%; +} +body { + margin: 0; +} +article, +aside, +figcaption, +figure, +footer, +header, +hgroup, +main, +menu, +nav, +section { + display: block; +} +audio, +canvas, +progress, +video { + display: inline-block; + vertical-align: baseline; +} +audio:not([controls]) { + display: none; + height: 0; +} +[hidden], +template { + display: none; +} +a { + background-color: transparent; +} +a:active, +a:hover { + outline: 0; +} +abbr[title] { + border-bottom: 1px dotted; +} +b, +strong { + font-weight: bold; +} +dfn { + font-style: italic; +} +h1 { + margin: .67em 0; + font-size: 2em; +} +mark { + color: #000; + background: #ff0; +} +small { + font-size: 80%; +} +sub, +sup { + position: relative; + font-size: 75%; + line-height: 0; + vertical-align: baseline; +} +sup { + top: -.5em; +} +sub { + bottom: -.25em; +} +img { + border: 0; +} +svg:not(:root) { + overflow: hidden; +} +figure { + margin: 1em 40px; +} +hr { + height: 0; + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} +pre { + overflow: auto; +} +code, +kbd, +pre, +samp { + font-family: monospace, monospace; + font-size: 1em; +} +button, +input, +optgroup, +select, +textarea { + margin: 0; + font: inherit; + color: inherit; +} +button { + overflow: visible; +} +button, +select { + text-transform: none; +} +button, +html input[type="button"], +input[type="reset"], +input[type="submit"] { + -webkit-appearance: button; + cursor: pointer; +} +button[disabled], +html input[disabled] { + cursor: default; +} +button::-moz-focus-inner, +input::-moz-focus-inner { + padding: 0; + border: 0; +} +input { + line-height: normal; +} +input[type="checkbox"], +input[type="radio"] { + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; + padding: 0; +} +input[type="number"]::-webkit-inner-spin-button, +input[type="number"]::-webkit-outer-spin-button { + height: auto; +} +input[type="search"] { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; + -webkit-appearance: textfield; +} +input[type="search"]::-webkit-search-cancel-button, +input[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} +fieldset { + padding: .35em .625em .75em; + margin: 0 2px; + border: 1px solid #c0c0c0; +} +legend { + padding: 0; + border: 0; +} +textarea { + overflow: auto; +} +optgroup { + font-weight: bold; +} +table { + border-spacing: 0; + border-collapse: collapse; +} +td, +th { + padding: 0; +} +/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */ +@media print { + *, + *:before, + *:after { + color: #000; + background: transparent; + text-shadow: none !important; + -webkit-box-shadow: none !important; + box-shadow: none !important; + } + a, + a:visited { + text-decoration: underline; + } + abbr[title]:after { + content: " (" attr(title) ")"; + } + a[href^="#"]:after, + a[href^="javascript:"]:after { + content: ""; + } + pre, + blockquote { + border: 1px solid #999; + + page-break-inside: avoid; + } + thead { + display: table-header-group; + } + tr, + img { + page-break-inside: avoid; + } + img { + max-width: 100% !important; + } + p, + h2, + h3 { + orphans: 3; + widows: 3; + } + h2, + h3 { + page-break-after: avoid; + } + select { + background: #fff !important; + } + .navbar { + display: none; + } + .btn > .caret, + .dropup > .btn > .caret { + border-top-color: #000 !important; + } + .label { + border: 1px solid #000; + } + .table { + border-collapse: collapse !important; + } + .table td, + .table th { + background-color: #fff; + } + .table-bordered th, + .table-bordered td { + border: 1px solid #ddd; + } +} +* { + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; +} +*:before, +*:after { + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; +} +html { + font-size: 10px; + + -webkit-tap-highlight-color: rgba(0, 0, 0, 0); +} +body { + font-family: "Helvetica Neue", Helvetica, Arial, "Open Sans", sans-serif; + font-size: 14px; + line-height: 1.42857143; + color: #36414c; + background-color: #fff; +} +input, +button, +select, +textarea { + font-family: inherit; + font-size: inherit; + line-height: inherit; +} +a { + color: #36414c; + text-decoration: none; +} +a:hover, +a:focus { + color: #161b1f; + text-decoration: underline; +} +a:focus { + outline: thin dotted; + outline: 5px auto -webkit-focus-ring-color; + outline-offset: -2px; +} +figure { + margin: 0; +} +img { + vertical-align: middle; +} +.img-responsive, +.thumbnail > img, +.thumbnail a > img, +.carousel-inner > .item > img, +.carousel-inner > .item > a > img { + display: block; + max-width: 100%; + height: auto; +} +.img-rounded { + border-radius: 6px; +} +.img-thumbnail { + display: inline-block; + max-width: 100%; + height: auto; + padding: 4px; + line-height: 1.42857143; + background-color: #fff; + border: 1px solid #ddd; + border-radius: 4px; + -webkit-transition: all .2s ease-in-out; + -o-transition: all .2s ease-in-out; + transition: all .2s ease-in-out; +} +.img-circle { + border-radius: 50%; +} +hr { + margin-top: 20px; + margin-bottom: 20px; + border: 0; + border-top: 1px solid #d1d8dd; +} +.sr-only { + position: absolute; + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); + border: 0; +} +.sr-only-focusable:active, +.sr-only-focusable:focus { + position: static; + width: auto; + height: auto; + margin: 0; + overflow: visible; + clip: auto; +} +h1, +h2, +h3, +h4, +h5, +h6, +.h1, +.h2, +.h3, +.h4, +.h5, +.h6 { + font-family: inherit; + font-weight: 500; + line-height: 1.3em; + color: inherit; +} +h1 small, +h2 small, +h3 small, +h4 small, +h5 small, +h6 small, +.h1 small, +.h2 small, +.h3 small, +.h4 small, +.h5 small, +.h6 small, +h1 .small, +h2 .small, +h3 .small, +h4 .small, +h5 .small, +h6 .small, +.h1 .small, +.h2 .small, +.h3 .small, +.h4 .small, +.h5 .small, +.h6 .small { + font-weight: normal; + line-height: 1; + color: #777; +} +h1, +.h1, +h2, +.h2, +h3, +.h3 { + margin-top: 20px; + margin-bottom: 10px; +} +h1 small, +.h1 small, +h2 small, +.h2 small, +h3 small, +.h3 small, +h1 .small, +.h1 .small, +h2 .small, +.h2 .small, +h3 .small, +.h3 .small { + font-size: 65%; +} +h4, +.h4, +h5, +.h5, +h6, +.h6 { + margin-top: 10px; + margin-bottom: 10px; +} +h4 small, +.h4 small, +h5 small, +.h5 small, +h6 small, +.h6 small, +h4 .small, +.h4 .small, +h5 .small, +.h5 .small, +h6 .small, +.h6 .small { + font-size: 75%; +} +h1, +.h1 { + font-size: 24px; +} +h2, +.h2 { + font-size: 20px; +} +h3, +.h3 { + font-size: 18px; +} +h4, +.h4 { + font-size: 16px; +} +h5, +.h5 { + font-size: 14px; +} +h6, +.h6 { + font-size: 12px; +} +p { + margin: 0 0 10px; +} +.lead { + margin-bottom: 20px; + font-size: 16px; + font-weight: 300; + line-height: 1.4; +} +@media (min-width: 768px) { + .lead { + font-size: 21px; + } +} +small, +.small { + font-size: 85%; +} +mark, +.mark { + padding: .2em; + background-color: transparent; +} +.text-left { + text-align: left; +} +.text-right { + text-align: right; +} +.text-center { + text-align: center; +} +.text-justify { + text-align: justify; +} +.text-nowrap { + white-space: nowrap; +} +.text-lowercase { + text-transform: lowercase; +} +.text-uppercase { + text-transform: uppercase; +} +.text-capitalize { + text-transform: capitalize; +} +.text-muted { + color: #8d99a6; +} +.text-primary { + color: #5e64ff; +} +a.text-primary:hover { + color: #2b33ff; +} +.text-success { + color: #98d85b; +} +a.text-success:hover { + color: #7ece32; +} +.text-info { + color: #935eff; +} +a.text-info:hover { + color: #712bff; +} +.text-warning { + color: #ffa00a; +} +a.text-warning:hover { + color: #d68300; +} +.text-danger { + color: #ff5858; +} +a.text-danger:hover { + color: #ff2525; +} +.bg-primary { + color: #fff; + background-color: #5e64ff; +} +a.bg-primary:hover { + background-color: #2b33ff; +} +.bg-success { + background-color: transparent; +} +a.bg-success:hover { + background-color: rgba(0, 0, 0, 0); +} +.bg-info { + background-color: transparent; +} +a.bg-info:hover { + background-color: rgba(0, 0, 0, 0); +} +.bg-warning { + background-color: transparent; +} +a.bg-warning:hover { + background-color: rgba(0, 0, 0, 0); +} +.bg-danger { + background-color: transparent; +} +a.bg-danger:hover { + background-color: rgba(0, 0, 0, 0); +} +.page-header { + padding-bottom: 9px; + margin: 40px 0 20px; + border-bottom: 1px solid #eee; +} +ul, +ol { + margin-top: 0; + margin-bottom: 10px; +} +ul ul, +ol ul, +ul ol, +ol ol { + margin-bottom: 0; +} +.list-unstyled { + padding-left: 0; + list-style: none; +} +.list-inline { + padding-left: 0; + margin-left: -5px; + list-style: none; +} +.list-inline > li { + display: inline-block; + padding-right: 5px; + padding-left: 5px; +} +dl { + margin-top: 0; + margin-bottom: 20px; +} +dt, +dd { + line-height: 1.42857143; +} +dt { + font-weight: bold; +} +dd { + margin-left: 0; +} +@media (min-width: 768px) { + .dl-horizontal dt { + float: left; + width: 160px; + overflow: hidden; + clear: left; + text-align: right; + text-overflow: ellipsis; + white-space: nowrap; + } + .dl-horizontal dd { + margin-left: 180px; + } +} +abbr[title], +abbr[data-original-title] { + cursor: help; + border-bottom: 1px dotted #777; +} +.initialism { + font-size: 90%; + text-transform: uppercase; +} +blockquote { + padding: 10px 20px; + margin: 0 0 20px; + font-size: 17.5px; + border-left: 5px solid #eee; +} +blockquote p:last-child, +blockquote ul:last-child, +blockquote ol:last-child { + margin-bottom: 0; +} +blockquote footer, +blockquote small, +blockquote .small { + display: block; + font-size: 80%; + line-height: 1.42857143; + color: #777; +} +blockquote footer:before, +blockquote small:before, +blockquote .small:before { + content: '\2014 \00A0'; +} +.blockquote-reverse, +blockquote.pull-right { + padding-right: 15px; + padding-left: 0; + text-align: right; + border-right: 5px solid #eee; + border-left: 0; +} +.blockquote-reverse footer:before, +blockquote.pull-right footer:before, +.blockquote-reverse small:before, +blockquote.pull-right small:before, +.blockquote-reverse .small:before, +blockquote.pull-right .small:before { + content: ''; +} +.blockquote-reverse footer:after, +blockquote.pull-right footer:after, +.blockquote-reverse small:after, +blockquote.pull-right small:after, +.blockquote-reverse .small:after, +blockquote.pull-right .small:after { + content: '\00A0 \2014'; +} +address { + margin-bottom: 20px; + font-style: normal; + line-height: 1.42857143; +} +code, +kbd, +pre, +samp { + font-family: Menlo, Monaco, Consolas, "Courier New", monospace; +} +code { + padding: 2px 4px; + font-size: 90%; + color: #c7254e; + background-color: #f9f2f4; + border-radius: 4px; +} +kbd { + padding: 2px 4px; + font-size: 90%; + color: #fff; + background-color: #333; + border-radius: 3px; + -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .25); + box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .25); +} +kbd kbd { + padding: 0; + font-size: 100%; + font-weight: bold; + -webkit-box-shadow: none; + box-shadow: none; +} +pre { + display: block; + padding: 9.5px; + margin: 0 0 10px; + font-size: 13px; + line-height: 1.42857143; + color: #333; + word-break: break-all; + word-wrap: break-word; + background-color: #f5f5f5; + border: 1px solid #ccc; + border-radius: 4px; +} +pre code { + padding: 0; + font-size: inherit; + color: inherit; + white-space: pre-wrap; + background-color: transparent; + border-radius: 0; +} +.pre-scrollable { + max-height: 340px; + overflow-y: scroll; +} +.container { + padding-right: 15px; + padding-left: 15px; + margin-right: auto; + margin-left: auto; +} +@media (min-width: 768px) { + .container { + width: 750px; + } +} +@media (min-width: 992px) { + .container { + width: 970px; + } +} +@media (min-width: 1200px) { + .container { + width: 1170px; + } +} +.container-fluid { + padding-right: 15px; + padding-left: 15px; + margin-right: auto; + margin-left: auto; +} +.row { + margin-right: -15px; + margin-left: -15px; +} +.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1, .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2, .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3, .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4, .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5, .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7, .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8, .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9, .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10, .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11, .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12 { + position: relative; + min-height: 1px; + padding-right: 15px; + padding-left: 15px; +} +.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 { + float: left; +} +.col-xs-12 { + width: 100%; +} +.col-xs-11 { + width: 91.66666667%; +} +.col-xs-10 { + width: 83.33333333%; +} +.col-xs-9 { + width: 75%; +} +.col-xs-8 { + width: 66.66666667%; +} +.col-xs-7 { + width: 58.33333333%; +} +.col-xs-6 { + width: 50%; +} +.col-xs-5 { + width: 41.66666667%; +} +.col-xs-4 { + width: 33.33333333%; +} +.col-xs-3 { + width: 25%; +} +.col-xs-2 { + width: 16.66666667%; +} +.col-xs-1 { + width: 8.33333333%; +} +.col-xs-pull-12 { + right: 100%; +} +.col-xs-pull-11 { + right: 91.66666667%; +} +.col-xs-pull-10 { + right: 83.33333333%; +} +.col-xs-pull-9 { + right: 75%; +} +.col-xs-pull-8 { + right: 66.66666667%; +} +.col-xs-pull-7 { + right: 58.33333333%; +} +.col-xs-pull-6 { + right: 50%; +} +.col-xs-pull-5 { + right: 41.66666667%; +} +.col-xs-pull-4 { + right: 33.33333333%; +} +.col-xs-pull-3 { + right: 25%; +} +.col-xs-pull-2 { + right: 16.66666667%; +} +.col-xs-pull-1 { + right: 8.33333333%; +} +.col-xs-pull-0 { + right: auto; +} +.col-xs-push-12 { + left: 100%; +} +.col-xs-push-11 { + left: 91.66666667%; +} +.col-xs-push-10 { + left: 83.33333333%; +} +.col-xs-push-9 { + left: 75%; +} +.col-xs-push-8 { + left: 66.66666667%; +} +.col-xs-push-7 { + left: 58.33333333%; +} +.col-xs-push-6 { + left: 50%; +} +.col-xs-push-5 { + left: 41.66666667%; +} +.col-xs-push-4 { + left: 33.33333333%; +} +.col-xs-push-3 { + left: 25%; +} +.col-xs-push-2 { + left: 16.66666667%; +} +.col-xs-push-1 { + left: 8.33333333%; +} +.col-xs-push-0 { + left: auto; +} +.col-xs-offset-12 { + margin-left: 100%; +} +.col-xs-offset-11 { + margin-left: 91.66666667%; +} +.col-xs-offset-10 { + margin-left: 83.33333333%; +} +.col-xs-offset-9 { + margin-left: 75%; +} +.col-xs-offset-8 { + margin-left: 66.66666667%; +} +.col-xs-offset-7 { + margin-left: 58.33333333%; +} +.col-xs-offset-6 { + margin-left: 50%; +} +.col-xs-offset-5 { + margin-left: 41.66666667%; +} +.col-xs-offset-4 { + margin-left: 33.33333333%; +} +.col-xs-offset-3 { + margin-left: 25%; +} +.col-xs-offset-2 { + margin-left: 16.66666667%; +} +.col-xs-offset-1 { + margin-left: 8.33333333%; +} +.col-xs-offset-0 { + margin-left: 0; +} +@media (min-width: 768px) { + .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 { + float: left; + } + .col-sm-12 { + width: 100%; + } + .col-sm-11 { + width: 91.66666667%; + } + .col-sm-10 { + width: 83.33333333%; + } + .col-sm-9 { + width: 75%; + } + .col-sm-8 { + width: 66.66666667%; + } + .col-sm-7 { + width: 58.33333333%; + } + .col-sm-6 { + width: 50%; + } + .col-sm-5 { + width: 41.66666667%; + } + .col-sm-4 { + width: 33.33333333%; + } + .col-sm-3 { + width: 25%; + } + .col-sm-2 { + width: 16.66666667%; + } + .col-sm-1 { + width: 8.33333333%; + } + .col-sm-pull-12 { + right: 100%; + } + .col-sm-pull-11 { + right: 91.66666667%; + } + .col-sm-pull-10 { + right: 83.33333333%; + } + .col-sm-pull-9 { + right: 75%; + } + .col-sm-pull-8 { + right: 66.66666667%; + } + .col-sm-pull-7 { + right: 58.33333333%; + } + .col-sm-pull-6 { + right: 50%; + } + .col-sm-pull-5 { + right: 41.66666667%; + } + .col-sm-pull-4 { + right: 33.33333333%; + } + .col-sm-pull-3 { + right: 25%; + } + .col-sm-pull-2 { + right: 16.66666667%; + } + .col-sm-pull-1 { + right: 8.33333333%; + } + .col-sm-pull-0 { + right: auto; + } + .col-sm-push-12 { + left: 100%; + } + .col-sm-push-11 { + left: 91.66666667%; + } + .col-sm-push-10 { + left: 83.33333333%; + } + .col-sm-push-9 { + left: 75%; + } + .col-sm-push-8 { + left: 66.66666667%; + } + .col-sm-push-7 { + left: 58.33333333%; + } + .col-sm-push-6 { + left: 50%; + } + .col-sm-push-5 { + left: 41.66666667%; + } + .col-sm-push-4 { + left: 33.33333333%; + } + .col-sm-push-3 { + left: 25%; + } + .col-sm-push-2 { + left: 16.66666667%; + } + .col-sm-push-1 { + left: 8.33333333%; + } + .col-sm-push-0 { + left: auto; + } + .col-sm-offset-12 { + margin-left: 100%; + } + .col-sm-offset-11 { + margin-left: 91.66666667%; + } + .col-sm-offset-10 { + margin-left: 83.33333333%; + } + .col-sm-offset-9 { + margin-left: 75%; + } + .col-sm-offset-8 { + margin-left: 66.66666667%; + } + .col-sm-offset-7 { + margin-left: 58.33333333%; + } + .col-sm-offset-6 { + margin-left: 50%; + } + .col-sm-offset-5 { + margin-left: 41.66666667%; + } + .col-sm-offset-4 { + margin-left: 33.33333333%; + } + .col-sm-offset-3 { + margin-left: 25%; + } + .col-sm-offset-2 { + margin-left: 16.66666667%; + } + .col-sm-offset-1 { + margin-left: 8.33333333%; + } + .col-sm-offset-0 { + margin-left: 0; + } +} +@media (min-width: 992px) { + .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 { + float: left; + } + .col-md-12 { + width: 100%; + } + .col-md-11 { + width: 91.66666667%; + } + .col-md-10 { + width: 83.33333333%; + } + .col-md-9 { + width: 75%; + } + .col-md-8 { + width: 66.66666667%; + } + .col-md-7 { + width: 58.33333333%; + } + .col-md-6 { + width: 50%; + } + .col-md-5 { + width: 41.66666667%; + } + .col-md-4 { + width: 33.33333333%; + } + .col-md-3 { + width: 25%; + } + .col-md-2 { + width: 16.66666667%; + } + .col-md-1 { + width: 8.33333333%; + } + .col-md-pull-12 { + right: 100%; + } + .col-md-pull-11 { + right: 91.66666667%; + } + .col-md-pull-10 { + right: 83.33333333%; + } + .col-md-pull-9 { + right: 75%; + } + .col-md-pull-8 { + right: 66.66666667%; + } + .col-md-pull-7 { + right: 58.33333333%; + } + .col-md-pull-6 { + right: 50%; + } + .col-md-pull-5 { + right: 41.66666667%; + } + .col-md-pull-4 { + right: 33.33333333%; + } + .col-md-pull-3 { + right: 25%; + } + .col-md-pull-2 { + right: 16.66666667%; + } + .col-md-pull-1 { + right: 8.33333333%; + } + .col-md-pull-0 { + right: auto; + } + .col-md-push-12 { + left: 100%; + } + .col-md-push-11 { + left: 91.66666667%; + } + .col-md-push-10 { + left: 83.33333333%; + } + .col-md-push-9 { + left: 75%; + } + .col-md-push-8 { + left: 66.66666667%; + } + .col-md-push-7 { + left: 58.33333333%; + } + .col-md-push-6 { + left: 50%; + } + .col-md-push-5 { + left: 41.66666667%; + } + .col-md-push-4 { + left: 33.33333333%; + } + .col-md-push-3 { + left: 25%; + } + .col-md-push-2 { + left: 16.66666667%; + } + .col-md-push-1 { + left: 8.33333333%; + } + .col-md-push-0 { + left: auto; + } + .col-md-offset-12 { + margin-left: 100%; + } + .col-md-offset-11 { + margin-left: 91.66666667%; + } + .col-md-offset-10 { + margin-left: 83.33333333%; + } + .col-md-offset-9 { + margin-left: 75%; + } + .col-md-offset-8 { + margin-left: 66.66666667%; + } + .col-md-offset-7 { + margin-left: 58.33333333%; + } + .col-md-offset-6 { + margin-left: 50%; + } + .col-md-offset-5 { + margin-left: 41.66666667%; + } + .col-md-offset-4 { + margin-left: 33.33333333%; + } + .col-md-offset-3 { + margin-left: 25%; + } + .col-md-offset-2 { + margin-left: 16.66666667%; + } + .col-md-offset-1 { + margin-left: 8.33333333%; + } + .col-md-offset-0 { + margin-left: 0; + } +} +@media (min-width: 1200px) { + .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12 { + float: left; + } + .col-lg-12 { + width: 100%; + } + .col-lg-11 { + width: 91.66666667%; + } + .col-lg-10 { + width: 83.33333333%; + } + .col-lg-9 { + width: 75%; + } + .col-lg-8 { + width: 66.66666667%; + } + .col-lg-7 { + width: 58.33333333%; + } + .col-lg-6 { + width: 50%; + } + .col-lg-5 { + width: 41.66666667%; + } + .col-lg-4 { + width: 33.33333333%; + } + .col-lg-3 { + width: 25%; + } + .col-lg-2 { + width: 16.66666667%; + } + .col-lg-1 { + width: 8.33333333%; + } + .col-lg-pull-12 { + right: 100%; + } + .col-lg-pull-11 { + right: 91.66666667%; + } + .col-lg-pull-10 { + right: 83.33333333%; + } + .col-lg-pull-9 { + right: 75%; + } + .col-lg-pull-8 { + right: 66.66666667%; + } + .col-lg-pull-7 { + right: 58.33333333%; + } + .col-lg-pull-6 { + right: 50%; + } + .col-lg-pull-5 { + right: 41.66666667%; + } + .col-lg-pull-4 { + right: 33.33333333%; + } + .col-lg-pull-3 { + right: 25%; + } + .col-lg-pull-2 { + right: 16.66666667%; + } + .col-lg-pull-1 { + right: 8.33333333%; + } + .col-lg-pull-0 { + right: auto; + } + .col-lg-push-12 { + left: 100%; + } + .col-lg-push-11 { + left: 91.66666667%; + } + .col-lg-push-10 { + left: 83.33333333%; + } + .col-lg-push-9 { + left: 75%; + } + .col-lg-push-8 { + left: 66.66666667%; + } + .col-lg-push-7 { + left: 58.33333333%; + } + .col-lg-push-6 { + left: 50%; + } + .col-lg-push-5 { + left: 41.66666667%; + } + .col-lg-push-4 { + left: 33.33333333%; + } + .col-lg-push-3 { + left: 25%; + } + .col-lg-push-2 { + left: 16.66666667%; + } + .col-lg-push-1 { + left: 8.33333333%; + } + .col-lg-push-0 { + left: auto; + } + .col-lg-offset-12 { + margin-left: 100%; + } + .col-lg-offset-11 { + margin-left: 91.66666667%; + } + .col-lg-offset-10 { + margin-left: 83.33333333%; + } + .col-lg-offset-9 { + margin-left: 75%; + } + .col-lg-offset-8 { + margin-left: 66.66666667%; + } + .col-lg-offset-7 { + margin-left: 58.33333333%; + } + .col-lg-offset-6 { + margin-left: 50%; + } + .col-lg-offset-5 { + margin-left: 41.66666667%; + } + .col-lg-offset-4 { + margin-left: 33.33333333%; + } + .col-lg-offset-3 { + margin-left: 25%; + } + .col-lg-offset-2 { + margin-left: 16.66666667%; + } + .col-lg-offset-1 { + margin-left: 8.33333333%; + } + .col-lg-offset-0 { + margin-left: 0; + } +} +table { + background-color: transparent; +} +caption { + padding-top: 8px; + padding-bottom: 8px; + color: #8d99a6; + text-align: left; +} +th { + text-align: left; +} +.table { + width: 100%; + max-width: 100%; + margin-bottom: 20px; +} +.table > thead > tr > th, +.table > tbody > tr > th, +.table > tfoot > tr > th, +.table > thead > tr > td, +.table > tbody > tr > td, +.table > tfoot > tr > td { + padding: 8px; + line-height: 1.42857143; + vertical-align: top; + border-top: 1px solid #d1d8dd; +} +.table > thead > tr > th { + vertical-align: bottom; + border-bottom: 2px solid #d1d8dd; +} +.table > caption + thead > tr:first-child > th, +.table > colgroup + thead > tr:first-child > th, +.table > thead:first-child > tr:first-child > th, +.table > caption + thead > tr:first-child > td, +.table > colgroup + thead > tr:first-child > td, +.table > thead:first-child > tr:first-child > td { + border-top: 0; +} +.table > tbody + tbody { + border-top: 2px solid #d1d8dd; +} +.table .table { + background-color: #fff; +} +.table-condensed > thead > tr > th, +.table-condensed > tbody > tr > th, +.table-condensed > tfoot > tr > th, +.table-condensed > thead > tr > td, +.table-condensed > tbody > tr > td, +.table-condensed > tfoot > tr > td { + padding: 5px; +} +.table-bordered { + border: 1px solid #d1d8dd; +} +.table-bordered > thead > tr > th, +.table-bordered > tbody > tr > th, +.table-bordered > tfoot > tr > th, +.table-bordered > thead > tr > td, +.table-bordered > tbody > tr > td, +.table-bordered > tfoot > tr > td { + border: 1px solid #d1d8dd; +} +.table-bordered > thead > tr > th, +.table-bordered > thead > tr > td { + border-bottom-width: 2px; +} +.table-striped > tbody > tr:nth-child(odd) { + background-color: #f9f9f9; +} +.table-hover > tbody > tr:hover { + background-color: #f5f5f5; +} +table col[class*="col-"] { + position: static; + display: table-column; + float: none; +} +table td[class*="col-"], +table th[class*="col-"] { + position: static; + display: table-cell; + float: none; +} +.table > thead > tr > td.active, +.table > tbody > tr > td.active, +.table > tfoot > tr > td.active, +.table > thead > tr > th.active, +.table > tbody > tr > th.active, +.table > tfoot > tr > th.active, +.table > thead > tr.active > td, +.table > tbody > tr.active > td, +.table > tfoot > tr.active > td, +.table > thead > tr.active > th, +.table > tbody > tr.active > th, +.table > tfoot > tr.active > th { + background-color: #f5f5f5; +} +.table-hover > tbody > tr > td.active:hover, +.table-hover > tbody > tr > th.active:hover, +.table-hover > tbody > tr.active:hover > td, +.table-hover > tbody > tr:hover > .active, +.table-hover > tbody > tr.active:hover > th { + background-color: #e8e8e8; +} +.table > thead > tr > td.success, +.table > tbody > tr > td.success, +.table > tfoot > tr > td.success, +.table > thead > tr > th.success, +.table > tbody > tr > th.success, +.table > tfoot > tr > th.success, +.table > thead > tr.success > td, +.table > tbody > tr.success > td, +.table > tfoot > tr.success > td, +.table > thead > tr.success > th, +.table > tbody > tr.success > th, +.table > tfoot > tr.success > th { + background-color: transparent; +} +.table-hover > tbody > tr > td.success:hover, +.table-hover > tbody > tr > th.success:hover, +.table-hover > tbody > tr.success:hover > td, +.table-hover > tbody > tr:hover > .success, +.table-hover > tbody > tr.success:hover > th { + background-color: rgba(0, 0, 0, 0); +} +.table > thead > tr > td.info, +.table > tbody > tr > td.info, +.table > tfoot > tr > td.info, +.table > thead > tr > th.info, +.table > tbody > tr > th.info, +.table > tfoot > tr > th.info, +.table > thead > tr.info > td, +.table > tbody > tr.info > td, +.table > tfoot > tr.info > td, +.table > thead > tr.info > th, +.table > tbody > tr.info > th, +.table > tfoot > tr.info > th { + background-color: transparent; +} +.table-hover > tbody > tr > td.info:hover, +.table-hover > tbody > tr > th.info:hover, +.table-hover > tbody > tr.info:hover > td, +.table-hover > tbody > tr:hover > .info, +.table-hover > tbody > tr.info:hover > th { + background-color: rgba(0, 0, 0, 0); +} +.table > thead > tr > td.warning, +.table > tbody > tr > td.warning, +.table > tfoot > tr > td.warning, +.table > thead > tr > th.warning, +.table > tbody > tr > th.warning, +.table > tfoot > tr > th.warning, +.table > thead > tr.warning > td, +.table > tbody > tr.warning > td, +.table > tfoot > tr.warning > td, +.table > thead > tr.warning > th, +.table > tbody > tr.warning > th, +.table > tfoot > tr.warning > th { + background-color: transparent; +} +.table-hover > tbody > tr > td.warning:hover, +.table-hover > tbody > tr > th.warning:hover, +.table-hover > tbody > tr.warning:hover > td, +.table-hover > tbody > tr:hover > .warning, +.table-hover > tbody > tr.warning:hover > th { + background-color: rgba(0, 0, 0, 0); +} +.table > thead > tr > td.danger, +.table > tbody > tr > td.danger, +.table > tfoot > tr > td.danger, +.table > thead > tr > th.danger, +.table > tbody > tr > th.danger, +.table > tfoot > tr > th.danger, +.table > thead > tr.danger > td, +.table > tbody > tr.danger > td, +.table > tfoot > tr.danger > td, +.table > thead > tr.danger > th, +.table > tbody > tr.danger > th, +.table > tfoot > tr.danger > th { + background-color: transparent; +} +.table-hover > tbody > tr > td.danger:hover, +.table-hover > tbody > tr > th.danger:hover, +.table-hover > tbody > tr.danger:hover > td, +.table-hover > tbody > tr:hover > .danger, +.table-hover > tbody > tr.danger:hover > th { + background-color: rgba(0, 0, 0, 0); +} +.table-responsive { + min-height: .01%; + overflow-x: auto; +} +@media screen and (max-width: 767px) { + .table-responsive { + width: 100%; + margin-bottom: 15px; + overflow-y: hidden; + -ms-overflow-style: -ms-autohiding-scrollbar; + border: 1px solid #d1d8dd; + } + .table-responsive > .table { + margin-bottom: 0; + } + .table-responsive > .table > thead > tr > th, + .table-responsive > .table > tbody > tr > th, + .table-responsive > .table > tfoot > tr > th, + .table-responsive > .table > thead > tr > td, + .table-responsive > .table > tbody > tr > td, + .table-responsive > .table > tfoot > tr > td { + white-space: nowrap; + } + .table-responsive > .table-bordered { + border: 0; + } + .table-responsive > .table-bordered > thead > tr > th:first-child, + .table-responsive > .table-bordered > tbody > tr > th:first-child, + .table-responsive > .table-bordered > tfoot > tr > th:first-child, + .table-responsive > .table-bordered > thead > tr > td:first-child, + .table-responsive > .table-bordered > tbody > tr > td:first-child, + .table-responsive > .table-bordered > tfoot > tr > td:first-child { + border-left: 0; + } + .table-responsive > .table-bordered > thead > tr > th:last-child, + .table-responsive > .table-bordered > tbody > tr > th:last-child, + .table-responsive > .table-bordered > tfoot > tr > th:last-child, + .table-responsive > .table-bordered > thead > tr > td:last-child, + .table-responsive > .table-bordered > tbody > tr > td:last-child, + .table-responsive > .table-bordered > tfoot > tr > td:last-child { + border-right: 0; + } + .table-responsive > .table-bordered > tbody > tr:last-child > th, + .table-responsive > .table-bordered > tfoot > tr:last-child > th, + .table-responsive > .table-bordered > tbody > tr:last-child > td, + .table-responsive > .table-bordered > tfoot > tr:last-child > td { + border-bottom: 0; + } +} +fieldset { + min-width: 0; + padding: 0; + margin: 0; + border: 0; +} +legend { + display: block; + width: 100%; + padding: 0; + margin-bottom: 20px; + font-size: 21px; + line-height: inherit; + color: #333; + border: 0; + border-bottom: 1px solid #e5e5e5; +} +label { + display: inline-block; + max-width: 100%; + margin-bottom: 5px; + font-weight: bold; +} +input[type="search"] { + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; +} +input[type="radio"], +input[type="checkbox"] { + margin: 4px 0 0; + margin-top: 1px \9; + line-height: normal; +} +input[type="file"] { + display: block; +} +input[type="range"] { + display: block; + width: 100%; +} +select[multiple], +select[size] { + height: auto; +} +input[type="file"]:focus, +input[type="radio"]:focus, +input[type="checkbox"]:focus { + outline: thin dotted; + outline: 5px auto -webkit-focus-ring-color; + outline-offset: -2px; +} +output { + display: block; + padding-top: 5px; + font-size: 14px; + line-height: 1.42857143; + color: #555; +} +.form-control { + display: block; + width: 100%; + height: 30px; + padding: 4px 10px; + font-size: 14px; + line-height: 1.42857143; + color: #555; + background-color: #fff; + background-image: none; + border: 1px solid #d1d8dd; + border-radius: 4px; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); + -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s; + -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s; + transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s; +} +.form-control:focus { + border-color: #ced5db; + outline: 0; + -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(206, 213, 219, .6); + box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(206, 213, 219, .6); +} +.form-control:focus { + border-color: #ced5db; + outline: 0; + -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 3px rgba(206, 213, 219, .6); + box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 3px rgba(206, 213, 219, .6); +} +.form-control::-moz-placeholder { + color: #d1d8dd; + opacity: 1; +} +.form-control:-ms-input-placeholder { + color: #d1d8dd; +} +.form-control::-webkit-input-placeholder { + color: #d1d8dd; +} +.form-control[disabled], +.form-control[readonly], +fieldset[disabled] .form-control { + cursor: not-allowed; + background-color: #eee; + opacity: 1; +} +textarea.form-control { + height: auto; +} +input[type="search"] { + -webkit-appearance: none; +} +@media screen and (-webkit-min-device-pixel-ratio: 0) { + input[type="date"], + input[type="time"], + input[type="datetime-local"], + input[type="month"] { + line-height: 30px; + } + input[type="date"].input-sm, + input[type="time"].input-sm, + input[type="datetime-local"].input-sm, + input[type="month"].input-sm, + .input-group-sm input[type="date"], + .input-group-sm input[type="time"], + .input-group-sm input[type="datetime-local"], + .input-group-sm input[type="month"] { + line-height: 30px; + } + input[type="date"].input-lg, + input[type="time"].input-lg, + input[type="datetime-local"].input-lg, + input[type="month"].input-lg, + .input-group-lg input[type="date"], + .input-group-lg input[type="time"], + .input-group-lg input[type="datetime-local"], + .input-group-lg input[type="month"] { + line-height: 46px; + } +} +.form-group { + margin-bottom: 15px; +} +.radio, +.checkbox { + position: relative; + display: block; + margin-top: 10px; + margin-bottom: 10px; +} +.radio label, +.checkbox label { + min-height: 20px; + padding-left: 20px; + margin-bottom: 0; + font-weight: normal; + cursor: pointer; +} +.radio input[type="radio"], +.radio-inline input[type="radio"], +.checkbox input[type="checkbox"], +.checkbox-inline input[type="checkbox"] { + position: absolute; + margin-top: 4px \9; + margin-left: -20px; +} +.radio + .radio, +.checkbox + .checkbox { + margin-top: -5px; +} +.radio-inline, +.checkbox-inline { + display: inline-block; + padding-left: 20px; + margin-bottom: 0; + font-weight: normal; + vertical-align: middle; + cursor: pointer; +} +.radio-inline + .radio-inline, +.checkbox-inline + .checkbox-inline { + margin-top: 0; + margin-left: 10px; +} +input[type="radio"][disabled], +input[type="checkbox"][disabled], +input[type="radio"].disabled, +input[type="checkbox"].disabled, +fieldset[disabled] input[type="radio"], +fieldset[disabled] input[type="checkbox"] { + cursor: not-allowed; +} +.radio-inline.disabled, +.checkbox-inline.disabled, +fieldset[disabled] .radio-inline, +fieldset[disabled] .checkbox-inline { + cursor: not-allowed; +} +.radio.disabled label, +.checkbox.disabled label, +fieldset[disabled] .radio label, +fieldset[disabled] .checkbox label { + cursor: not-allowed; +} +.form-control-static { + padding-top: 5px; + padding-bottom: 5px; + margin-bottom: 0; +} +.form-control-static.input-lg, +.form-control-static.input-sm { + padding-right: 0; + padding-left: 0; +} +.input-sm { + height: 30px; + padding: 5px 10px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; +} +select.input-sm { + height: 30px; + line-height: 30px; +} +textarea.input-sm, +select[multiple].input-sm { + height: auto; +} +.form-group-sm .form-control { + height: 30px; + padding: 5px 10px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; +} +select.form-group-sm .form-control { + height: 30px; + line-height: 30px; +} +textarea.form-group-sm .form-control, +select[multiple].form-group-sm .form-control { + height: auto; +} +.input-lg { + height: 46px; + padding: 10px 16px; + font-size: 18px; + line-height: 1.33; + border-radius: 6px; +} +select.input-lg { + height: 46px; + line-height: 46px; +} +textarea.input-lg, +select[multiple].input-lg { + height: auto; +} +.form-group-lg .form-control { + height: 46px; + padding: 10px 16px; + font-size: 18px; + line-height: 1.33; + border-radius: 6px; +} +select.form-group-lg .form-control { + height: 46px; + line-height: 46px; +} +textarea.form-group-lg .form-control, +select[multiple].form-group-lg .form-control { + height: auto; +} +.has-feedback { + position: relative; +} +.has-feedback .form-control { + padding-right: 37.5px; +} +.form-control-feedback { + position: absolute; + top: 0; + right: 0; + z-index: 2; + display: block; + width: 30px; + height: 30px; + line-height: 30px; + text-align: center; + pointer-events: none; +} +.input-lg + .form-control-feedback { + width: 46px; + height: 46px; + line-height: 46px; +} +.input-sm + .form-control-feedback { + width: 30px; + height: 30px; + line-height: 30px; +} +.has-success .help-block, +.has-success .control-label, +.has-success .radio, +.has-success .checkbox, +.has-success .radio-inline, +.has-success .checkbox-inline, +.has-success.radio label, +.has-success.checkbox label, +.has-success.radio-inline label, +.has-success.checkbox-inline label { + color: #98d85b; +} +.has-success .form-control { + border-color: #98d85b; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); +} +.has-success .form-control:focus { + border-color: #7ece32; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ccecad; + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ccecad; +} +.has-success .input-group-addon { + color: #98d85b; + background-color: transparent; + border-color: #98d85b; +} +.has-success .form-control-feedback { + color: #98d85b; +} +.has-warning .help-block, +.has-warning .control-label, +.has-warning .radio, +.has-warning .checkbox, +.has-warning .radio-inline, +.has-warning .checkbox-inline, +.has-warning.radio label, +.has-warning.checkbox label, +.has-warning.radio-inline label, +.has-warning.checkbox-inline label { + color: #ffa00a; +} +.has-warning .form-control { + border-color: #ffa00a; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); +} +.has-warning .form-control:focus { + border-color: #d68300; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ffc870; + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ffc870; +} +.has-warning .input-group-addon { + color: #ffa00a; + background-color: transparent; + border-color: #ffa00a; +} +.has-warning .form-control-feedback { + color: #ffa00a; +} +.has-error .help-block, +.has-error .control-label, +.has-error .radio, +.has-error .checkbox, +.has-error .radio-inline, +.has-error .checkbox-inline, +.has-error.radio label, +.has-error.checkbox label, +.has-error.radio-inline label, +.has-error.checkbox-inline label { + color: #ff5858; +} +.has-error .form-control { + border-color: #ff5858; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); +} +.has-error .form-control:focus { + border-color: #ff2525; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ffbebe; + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ffbebe; +} +.has-error .input-group-addon { + color: #ff5858; + background-color: transparent; + border-color: #ff5858; +} +.has-error .form-control-feedback { + color: #ff5858; +} +.has-feedback label ~ .form-control-feedback { + top: 25px; +} +.has-feedback label.sr-only ~ .form-control-feedback { + top: 0; +} +.help-block { + display: block; + margin-top: 5px; + margin-bottom: 10px; + color: #6b8196; +} +@media (min-width: 768px) { + .form-inline .form-group { + display: inline-block; + margin-bottom: 0; + vertical-align: middle; + } + .form-inline .form-control { + display: inline-block; + width: auto; + vertical-align: middle; + } + .form-inline .form-control-static { + display: inline-block; + } + .form-inline .input-group { + display: inline-table; + vertical-align: middle; + } + .form-inline .input-group .input-group-addon, + .form-inline .input-group .input-group-btn, + .form-inline .input-group .form-control { + width: auto; + } + .form-inline .input-group > .form-control { + width: 100%; + } + .form-inline .control-label { + margin-bottom: 0; + vertical-align: middle; + } + .form-inline .radio, + .form-inline .checkbox { + display: inline-block; + margin-top: 0; + margin-bottom: 0; + vertical-align: middle; + } + .form-inline .radio label, + .form-inline .checkbox label { + padding-left: 0; + } + .form-inline .radio input[type="radio"], + .form-inline .checkbox input[type="checkbox"] { + position: relative; + margin-left: 0; + } + .form-inline .has-feedback .form-control-feedback { + top: 0; + } +} +.form-horizontal .radio, +.form-horizontal .checkbox, +.form-horizontal .radio-inline, +.form-horizontal .checkbox-inline { + padding-top: 5px; + margin-top: 0; + margin-bottom: 0; +} +.form-horizontal .radio, +.form-horizontal .checkbox { + min-height: 25px; +} +.form-horizontal .form-group { + margin-right: -15px; + margin-left: -15px; +} +@media (min-width: 768px) { + .form-horizontal .control-label { + padding-top: 5px; + margin-bottom: 0; + text-align: right; + } +} +.form-horizontal .has-feedback .form-control-feedback { + right: 15px; +} +@media (min-width: 768px) { + .form-horizontal .form-group-lg .control-label { + padding-top: 14.3px; + } +} +@media (min-width: 768px) { + .form-horizontal .form-group-sm .control-label { + padding-top: 6px; + } +} +.btn { + display: inline-block; + padding: 4px 10px; + margin-bottom: 0; + font-size: 14px; + font-weight: normal; + line-height: 1.42857143; + text-align: center; + white-space: nowrap; + vertical-align: middle; + -ms-touch-action: manipulation; + touch-action: manipulation; + cursor: pointer; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + background-image: none; + border: 1px solid transparent; + border-radius: 4px; +} +.btn:focus, +.btn:active:focus, +.btn.active:focus, +.btn.focus, +.btn:active.focus, +.btn.active.focus { + outline: thin dotted; + outline: 5px auto -webkit-focus-ring-color; + outline-offset: -2px; +} +.btn:hover, +.btn:focus, +.btn.focus { + color: inherit; + text-decoration: none; +} +.btn:active, +.btn.active { + background-image: none; + outline: 0; + -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125); + box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125); +} +.btn.disabled, +.btn[disabled], +fieldset[disabled] .btn { + pointer-events: none; + cursor: not-allowed; + filter: alpha(opacity=65); + -webkit-box-shadow: none; + box-shadow: none; + opacity: .65; +} +.btn-default { + color: inherit; + background-color: #f0f4f7; + border-color: transparent; +} +.btn-default:hover, +.btn-default:focus, +.btn-default.focus, +.btn-default:active, +.btn-default.active, +.open > .dropdown-toggle.btn-default { + color: inherit; + background-color: #cfdce5; + border-color: rgba(0, 0, 0, 0); +} +.btn-default:active, +.btn-default.active, +.open > .dropdown-toggle.btn-default { + background-image: none; +} +.btn-default.disabled, +.btn-default[disabled], +fieldset[disabled] .btn-default, +.btn-default.disabled:hover, +.btn-default[disabled]:hover, +fieldset[disabled] .btn-default:hover, +.btn-default.disabled:focus, +.btn-default[disabled]:focus, +fieldset[disabled] .btn-default:focus, +.btn-default.disabled.focus, +.btn-default[disabled].focus, +fieldset[disabled] .btn-default.focus, +.btn-default.disabled:active, +.btn-default[disabled]:active, +fieldset[disabled] .btn-default:active, +.btn-default.disabled.active, +.btn-default[disabled].active, +fieldset[disabled] .btn-default.active { + background-color: #f0f4f7; + border-color: transparent; +} +.btn-default .badge { + color: #f0f4f7; + background-color: inherit; +} +.btn-primary { + color: #fff; + background-color: #5e64ff; + border-color: #444bff; +} +.btn-primary:hover, +.btn-primary:focus, +.btn-primary.focus, +.btn-primary:active, +.btn-primary.active, +.open > .dropdown-toggle.btn-primary { + color: #fff; + background-color: #2b33ff; + border-color: #0711ff; +} +.btn-primary:active, +.btn-primary.active, +.open > .dropdown-toggle.btn-primary { + background-image: none; +} +.btn-primary.disabled, +.btn-primary[disabled], +fieldset[disabled] .btn-primary, +.btn-primary.disabled:hover, +.btn-primary[disabled]:hover, +fieldset[disabled] .btn-primary:hover, +.btn-primary.disabled:focus, +.btn-primary[disabled]:focus, +fieldset[disabled] .btn-primary:focus, +.btn-primary.disabled.focus, +.btn-primary[disabled].focus, +fieldset[disabled] .btn-primary.focus, +.btn-primary.disabled:active, +.btn-primary[disabled]:active, +fieldset[disabled] .btn-primary:active, +.btn-primary.disabled.active, +.btn-primary[disabled].active, +fieldset[disabled] .btn-primary.active { + background-color: #5e64ff; + border-color: #444bff; +} +.btn-primary .badge { + color: #5e64ff; + background-color: #fff; +} +.btn-success { + color: #fff; + background-color: #98d85b; + border-color: #8bd346; +} +.btn-success:hover, +.btn-success:focus, +.btn-success.focus, +.btn-success:active, +.btn-success.active, +.open > .dropdown-toggle.btn-success { + color: #fff; + background-color: #7ece32; + border-color: #6db22a; +} +.btn-success:active, +.btn-success.active, +.open > .dropdown-toggle.btn-success { + background-image: none; +} +.btn-success.disabled, +.btn-success[disabled], +fieldset[disabled] .btn-success, +.btn-success.disabled:hover, +.btn-success[disabled]:hover, +fieldset[disabled] .btn-success:hover, +.btn-success.disabled:focus, +.btn-success[disabled]:focus, +fieldset[disabled] .btn-success:focus, +.btn-success.disabled.focus, +.btn-success[disabled].focus, +fieldset[disabled] .btn-success.focus, +.btn-success.disabled:active, +.btn-success[disabled]:active, +fieldset[disabled] .btn-success:active, +.btn-success.disabled.active, +.btn-success[disabled].active, +fieldset[disabled] .btn-success.active { + background-color: #98d85b; + border-color: #8bd346; +} +.btn-success .badge { + color: #98d85b; + background-color: #fff; +} +.btn-info { + color: #fff; + background-color: #8d99a6; + border-color: #7f8c9b; +} +.btn-info:hover, +.btn-info:focus, +.btn-info.focus, +.btn-info:active, +.btn-info.active, +.open > .dropdown-toggle.btn-info { + color: #fff; + background-color: #707f90; + border-color: #616e7c; +} +.btn-info:active, +.btn-info.active, +.open > .dropdown-toggle.btn-info { + background-image: none; +} +.btn-info.disabled, +.btn-info[disabled], +fieldset[disabled] .btn-info, +.btn-info.disabled:hover, +.btn-info[disabled]:hover, +fieldset[disabled] .btn-info:hover, +.btn-info.disabled:focus, +.btn-info[disabled]:focus, +fieldset[disabled] .btn-info:focus, +.btn-info.disabled.focus, +.btn-info[disabled].focus, +fieldset[disabled] .btn-info.focus, +.btn-info.disabled:active, +.btn-info[disabled]:active, +fieldset[disabled] .btn-info:active, +.btn-info.disabled.active, +.btn-info[disabled].active, +fieldset[disabled] .btn-info.active { + background-color: #8d99a6; + border-color: #7f8c9b; +} +.btn-info .badge { + color: #8d99a6; + background-color: #fff; +} +.btn-warning { + color: #fff; + background-color: #ffa00a; + border-color: #f09300; +} +.btn-warning:hover, +.btn-warning:focus, +.btn-warning.focus, +.btn-warning:active, +.btn-warning.active, +.open > .dropdown-toggle.btn-warning { + color: #fff; + background-color: #d68300; + border-color: #b26d00; +} +.btn-warning:active, +.btn-warning.active, +.open > .dropdown-toggle.btn-warning { + background-image: none; +} +.btn-warning.disabled, +.btn-warning[disabled], +fieldset[disabled] .btn-warning, +.btn-warning.disabled:hover, +.btn-warning[disabled]:hover, +fieldset[disabled] .btn-warning:hover, +.btn-warning.disabled:focus, +.btn-warning[disabled]:focus, +fieldset[disabled] .btn-warning:focus, +.btn-warning.disabled.focus, +.btn-warning[disabled].focus, +fieldset[disabled] .btn-warning.focus, +.btn-warning.disabled:active, +.btn-warning[disabled]:active, +fieldset[disabled] .btn-warning:active, +.btn-warning.disabled.active, +.btn-warning[disabled].active, +fieldset[disabled] .btn-warning.active { + background-color: #ffa00a; + border-color: #f09300; +} +.btn-warning .badge { + color: #ffa00a; + background-color: #fff; +} +.btn-danger { + color: #fff; + background-color: #ff5858; + border-color: #ff3f3f; +} +.btn-danger:hover, +.btn-danger:focus, +.btn-danger.focus, +.btn-danger:active, +.btn-danger.active, +.open > .dropdown-toggle.btn-danger { + color: #fff; + background-color: #ff2525; + border-color: #ff0101; +} +.btn-danger:active, +.btn-danger.active, +.open > .dropdown-toggle.btn-danger { + background-image: none; +} +.btn-danger.disabled, +.btn-danger[disabled], +fieldset[disabled] .btn-danger, +.btn-danger.disabled:hover, +.btn-danger[disabled]:hover, +fieldset[disabled] .btn-danger:hover, +.btn-danger.disabled:focus, +.btn-danger[disabled]:focus, +fieldset[disabled] .btn-danger:focus, +.btn-danger.disabled.focus, +.btn-danger[disabled].focus, +fieldset[disabled] .btn-danger.focus, +.btn-danger.disabled:active, +.btn-danger[disabled]:active, +fieldset[disabled] .btn-danger:active, +.btn-danger.disabled.active, +.btn-danger[disabled].active, +fieldset[disabled] .btn-danger.active { + background-color: #ff5858; + border-color: #ff3f3f; +} +.btn-danger .badge { + color: #ff5858; + background-color: #fff; +} +.btn-link { + font-weight: normal; + color: #36414c; + border-radius: 0; +} +.btn-link, +.btn-link:active, +.btn-link.active, +.btn-link[disabled], +fieldset[disabled] .btn-link { + background-color: transparent; + -webkit-box-shadow: none; + box-shadow: none; +} +.btn-link, +.btn-link:hover, +.btn-link:focus, +.btn-link:active { + border-color: transparent; +} +.btn-link:hover, +.btn-link:focus { + color: #161b1f; + text-decoration: underline; + background-color: transparent; +} +.btn-link[disabled]:hover, +fieldset[disabled] .btn-link:hover, +.btn-link[disabled]:focus, +fieldset[disabled] .btn-link:focus { + color: #777; + text-decoration: none; +} +.btn-lg, +.btn-group-lg > .btn { + padding: 10px 16px; + font-size: 18px; + line-height: 1.33; + border-radius: 6px; +} +.btn-sm, +.btn-group-sm > .btn { + padding: 5px 10px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; +} +.btn-xs, +.btn-group-xs > .btn { + padding: 1px 5px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; +} +.btn-block { + display: block; + width: 100%; +} +.btn-block + .btn-block { + margin-top: 5px; +} +input[type="submit"].btn-block, +input[type="reset"].btn-block, +input[type="button"].btn-block { + width: 100%; +} +.fade { + opacity: 0; + -webkit-transition: opacity .15s linear; + -o-transition: opacity .15s linear; + transition: opacity .15s linear; +} +.fade.in { + opacity: 1; +} +.collapse { + display: none; + visibility: hidden; +} +.collapse.in { + display: block; + visibility: visible; +} +tr.collapse.in { + display: table-row; +} +tbody.collapse.in { + display: table-row-group; +} +.collapsing { + position: relative; + height: 0; + overflow: hidden; + -webkit-transition-timing-function: ease; + -o-transition-timing-function: ease; + transition-timing-function: ease; + -webkit-transition-duration: .35s; + -o-transition-duration: .35s; + transition-duration: .35s; + -webkit-transition-property: height, visibility; + -o-transition-property: height, visibility; + transition-property: height, visibility; +} +.caret { + display: inline-block; + width: 0; + height: 0; + margin-left: 2px; + vertical-align: middle; + border-top: 4px solid; + border-right: 4px solid transparent; + border-left: 4px solid transparent; +} +.dropdown { + position: relative; +} +.dropdown-toggle:focus { + outline: 0; +} +.dropdown-menu { + position: absolute; + top: 100%; + left: 0; + z-index: 1000; + display: none; + float: left; + min-width: 160px; + padding: 5px 0; + margin: 2px 0 0; + font-size: 14px; + text-align: left; + list-style: none; + background-color: #fff; + -webkit-background-clip: padding-box; + background-clip: padding-box; + border: 1px solid #ccc; + border: 1px solid rgba(0, 0, 0, .15); + border-radius: 4px; + -webkit-box-shadow: 0px 0px 1px rgba(0,0,0,0.200), 0px 1px 3px rgba(0,0,0,0.050), 0px 10px 24px -3px rgba(0,0,0,0.100); + box-shadow: 0px 0px 1px rgba(0,0,0,0.200), 0px 1px 3px rgba(0,0,0,0.050), 0px 10px 24px -3px rgba(0,0,0,0.100); +} +.dropdown-menu.pull-right { + right: 0; + left: auto; +} +.dropdown-menu .divider { + height: 1px; + margin: 9px 0; + overflow: hidden; + background-color: #d8dfe5; +} +.dropdown-menu > li > a { + display: block; + padding: 3px 20px; + clear: both; + font-weight: normal; + line-height: 1.42857143; + color: #333; + white-space: nowrap; +} +.dropdown-menu > li > a:hover, +.dropdown-menu > li > a:focus { + color: #262626; + text-decoration: none; + background-color: #f0f4f7; +} +.dropdown-menu > .active > a, +.dropdown-menu > .active > a:hover, +.dropdown-menu > .active > a:focus { + color: inherit; + text-decoration: none; + background-color: #f0f4f7; + outline: 0; +} +.dropdown-menu > .disabled > a, +.dropdown-menu > .disabled > a:hover, +.dropdown-menu > .disabled > a:focus { + color: #777; +} +.dropdown-menu > .disabled > a:hover, +.dropdown-menu > .disabled > a:focus { + text-decoration: none; + cursor: not-allowed; + background-color: transparent; + background-image: none; + filter: progid:DXImageTransform.Microsoft.gradient(enabled = false); +} +.open > .dropdown-menu { + display: block; +} +.open > a { + outline: 0; +} +.dropdown-menu-right { + right: 0; + left: auto; +} +.dropdown-menu-left { + right: auto; + left: 0; +} +.dropdown-header { + display: block; + padding: 3px 20px; + font-size: 12px; + line-height: 1.42857143; + color: #777; + white-space: nowrap; +} +.dropdown-backdrop { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: 99; +} +.pull-right > .dropdown-menu { + right: 0; + left: auto; +} +.dropup .caret, +.navbar-fixed-bottom .dropdown .caret { + content: ""; + border-top: 0; + border-bottom: 4px solid; +} +.dropup .dropdown-menu, +.navbar-fixed-bottom .dropdown .dropdown-menu { + top: auto; + bottom: 100%; + margin-bottom: 2px; +} +@media (min-width: 768px) { + .navbar-right .dropdown-menu { + right: 0; + left: auto; + } + .navbar-right .dropdown-menu-left { + right: auto; + left: 0; + } +} +.btn-group, +.btn-group-vertical { + position: relative; + display: inline-block; + vertical-align: middle; +} +.btn-group > .btn, +.btn-group-vertical > .btn { + position: relative; + float: left; +} +.btn-group > .btn:hover, +.btn-group-vertical > .btn:hover, +.btn-group > .btn:focus, +.btn-group-vertical > .btn:focus, +.btn-group > .btn:active, +.btn-group-vertical > .btn:active, +.btn-group > .btn.active, +.btn-group-vertical > .btn.active { + z-index: 2; +} +.btn-group .btn + .btn, +.btn-group .btn + .btn-group, +.btn-group .btn-group + .btn, +.btn-group .btn-group + .btn-group { + margin-left: -1px; +} +.btn-toolbar { + margin-left: -5px; +} +.btn-toolbar .btn-group, +.btn-toolbar .input-group { + float: left; +} +.btn-toolbar > .btn, +.btn-toolbar > .btn-group, +.btn-toolbar > .input-group { + margin-left: 5px; +} +.btn-group > .btn:not(:first-child):not(:last-child):not(.dropdown-toggle) { + border-radius: 0; +} +.btn-group > .btn:first-child { + margin-left: 0; +} +.btn-group > .btn:first-child:not(:last-child):not(.dropdown-toggle) { + border-top-right-radius: 0; + border-bottom-right-radius: 0; +} +.btn-group > .btn:last-child:not(:first-child), +.btn-group > .dropdown-toggle:not(:first-child) { + border-top-left-radius: 0; + border-bottom-left-radius: 0; +} +.btn-group > .btn-group { + float: left; +} +.btn-group > .btn-group:not(:first-child):not(:last-child) > .btn { + border-radius: 0; +} +.btn-group > .btn-group:first-child > .btn:last-child, +.btn-group > .btn-group:first-child > .dropdown-toggle { + border-top-right-radius: 0; + border-bottom-right-radius: 0; +} +.btn-group > .btn-group:last-child > .btn:first-child { + border-top-left-radius: 0; + border-bottom-left-radius: 0; +} +.btn-group .dropdown-toggle:active, +.btn-group.open .dropdown-toggle { + outline: 0; +} +.btn-group > .btn + .dropdown-toggle { + padding-right: 8px; + padding-left: 8px; +} +.btn-group > .btn-lg + .dropdown-toggle { + padding-right: 12px; + padding-left: 12px; +} +.btn-group.open .dropdown-toggle { + -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125); + box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125); +} +.btn-group.open .dropdown-toggle.btn-link { + -webkit-box-shadow: none; + box-shadow: none; +} +.btn .caret { + margin-left: 0; +} +.btn-lg .caret { + border-width: 5px 5px 0; + border-bottom-width: 0; +} +.dropup .btn-lg .caret { + border-width: 0 5px 5px; +} +.btn-group-vertical > .btn, +.btn-group-vertical > .btn-group, +.btn-group-vertical > .btn-group > .btn { + display: block; + float: none; + width: 100%; + max-width: 100%; +} +.btn-group-vertical > .btn-group > .btn { + float: none; +} +.btn-group-vertical > .btn + .btn, +.btn-group-vertical > .btn + .btn-group, +.btn-group-vertical > .btn-group + .btn, +.btn-group-vertical > .btn-group + .btn-group { + margin-top: -1px; + margin-left: 0; +} +.btn-group-vertical > .btn:not(:first-child):not(:last-child) { + border-radius: 0; +} +.btn-group-vertical > .btn:first-child:not(:last-child) { + border-top-right-radius: 4px; + border-bottom-right-radius: 0; + border-bottom-left-radius: 0; +} +.btn-group-vertical > .btn:last-child:not(:first-child) { + border-top-left-radius: 0; + border-top-right-radius: 0; + border-bottom-left-radius: 4px; +} +.btn-group-vertical > .btn-group:not(:first-child):not(:last-child) > .btn { + border-radius: 0; +} +.btn-group-vertical > .btn-group:first-child:not(:last-child) > .btn:last-child, +.btn-group-vertical > .btn-group:first-child:not(:last-child) > .dropdown-toggle { + border-bottom-right-radius: 0; + border-bottom-left-radius: 0; +} +.btn-group-vertical > .btn-group:last-child:not(:first-child) > .btn:first-child { + border-top-left-radius: 0; + border-top-right-radius: 0; +} +.btn-group-justified { + display: table; + width: 100%; + table-layout: fixed; + border-collapse: separate; +} +.btn-group-justified > .btn, +.btn-group-justified > .btn-group { + display: table-cell; + float: none; + width: 1%; +} +.btn-group-justified > .btn-group .btn { + width: 100%; +} +.btn-group-justified > .btn-group .dropdown-menu { + left: auto; +} +[data-toggle="buttons"] > .btn input[type="radio"], +[data-toggle="buttons"] > .btn-group > .btn input[type="radio"], +[data-toggle="buttons"] > .btn input[type="checkbox"], +[data-toggle="buttons"] > .btn-group > .btn input[type="checkbox"] { + position: absolute; + clip: rect(0, 0, 0, 0); + pointer-events: none; +} +.input-group { + position: relative; + display: table; + border-collapse: separate; +} +.input-group[class*="col-"] { + float: none; + padding-right: 0; + padding-left: 0; +} +.input-group .form-control { + position: relative; + z-index: 2; + float: left; + width: 100%; + margin-bottom: 0; +} +.input-group-lg > .form-control, +.input-group-lg > .input-group-addon, +.input-group-lg > .input-group-btn > .btn { + height: 46px; + padding: 10px 16px; + font-size: 18px; + line-height: 1.33; + border-radius: 6px; +} +select.input-group-lg > .form-control, +select.input-group-lg > .input-group-addon, +select.input-group-lg > .input-group-btn > .btn { + height: 46px; + line-height: 46px; +} +textarea.input-group-lg > .form-control, +textarea.input-group-lg > .input-group-addon, +textarea.input-group-lg > .input-group-btn > .btn, +select[multiple].input-group-lg > .form-control, +select[multiple].input-group-lg > .input-group-addon, +select[multiple].input-group-lg > .input-group-btn > .btn { + height: auto; +} +.input-group-sm > .form-control, +.input-group-sm > .input-group-addon, +.input-group-sm > .input-group-btn > .btn { + height: 30px; + padding: 5px 10px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; +} +select.input-group-sm > .form-control, +select.input-group-sm > .input-group-addon, +select.input-group-sm > .input-group-btn > .btn { + height: 30px; + line-height: 30px; +} +textarea.input-group-sm > .form-control, +textarea.input-group-sm > .input-group-addon, +textarea.input-group-sm > .input-group-btn > .btn, +select[multiple].input-group-sm > .form-control, +select[multiple].input-group-sm > .input-group-addon, +select[multiple].input-group-sm > .input-group-btn > .btn { + height: auto; +} +.input-group-addon, +.input-group-btn, +.input-group .form-control { + display: table-cell; +} +.input-group-addon:not(:first-child):not(:last-child), +.input-group-btn:not(:first-child):not(:last-child), +.input-group .form-control:not(:first-child):not(:last-child) { + border-radius: 0; +} +.input-group-addon, +.input-group-btn { + width: 1%; + white-space: nowrap; + vertical-align: middle; +} +.input-group-addon { + padding: 4px 10px; + font-size: 14px; + font-weight: normal; + line-height: 1; + color: #555; + text-align: center; + background-color: #f0f4f7; + border: 1px solid #d1d8dd; + border-radius: 4px; +} +.input-group-addon.input-sm { + padding: 5px 10px; + font-size: 12px; + border-radius: 3px; +} +.input-group-addon.input-lg { + padding: 10px 16px; + font-size: 18px; + border-radius: 6px; +} +.input-group-addon input[type="radio"], +.input-group-addon input[type="checkbox"] { + margin-top: 0; +} +.input-group .form-control:first-child, +.input-group-addon:first-child, +.input-group-btn:first-child > .btn, +.input-group-btn:first-child > .btn-group > .btn, +.input-group-btn:first-child > .dropdown-toggle, +.input-group-btn:last-child > .btn:not(:last-child):not(.dropdown-toggle), +.input-group-btn:last-child > .btn-group:not(:last-child) > .btn { + border-top-right-radius: 0; + border-bottom-right-radius: 0; +} +.input-group-addon:first-child { + border-right: 0; +} +.input-group .form-control:last-child, +.input-group-addon:last-child, +.input-group-btn:last-child > .btn, +.input-group-btn:last-child > .btn-group > .btn, +.input-group-btn:last-child > .dropdown-toggle, +.input-group-btn:first-child > .btn:not(:first-child), +.input-group-btn:first-child > .btn-group:not(:first-child) > .btn { + border-top-left-radius: 0; + border-bottom-left-radius: 0; +} +.input-group-addon:last-child { + border-left: 0; +} +.input-group-btn { + position: relative; + font-size: 0; + white-space: nowrap; +} +.input-group-btn > .btn { + position: relative; +} +.input-group-btn > .btn + .btn { + margin-left: -1px; +} +.input-group-btn > .btn:hover, +.input-group-btn > .btn:focus, +.input-group-btn > .btn:active { + z-index: 2; +} +.input-group-btn:first-child > .btn, +.input-group-btn:first-child > .btn-group { + margin-right: -1px; +} +.input-group-btn:last-child > .btn, +.input-group-btn:last-child > .btn-group { + margin-left: -1px; +} +.nav { + padding-left: 0; + margin-bottom: 0; + list-style: none; +} +.nav > li { + position: relative; + display: block; +} +.nav > li > a { + position: relative; + display: block; + padding: 10px 15px; +} +.nav > li > a:hover, +.nav > li > a:focus { + text-decoration: none; + background-color: #f7fafc; +} +.nav > li.disabled > a { + color: #777; +} +.nav > li.disabled > a:hover, +.nav > li.disabled > a:focus { + color: #777; + text-decoration: none; + cursor: not-allowed; + background-color: transparent; +} +.nav .open > a, +.nav .open > a:hover, +.nav .open > a:focus { + background-color: #f7fafc; + border-color: #36414c; +} +.nav .nav-divider { + height: 1px; + margin: 9px 0; + overflow: hidden; + background-color: #e5e5e5; +} +.nav > li > a > img { + max-width: none; +} +.nav-tabs { + border-bottom: 1px solid #ddd; +} +.nav-tabs > li { + float: left; + margin-bottom: -1px; +} +.nav-tabs > li > a { + margin-right: 2px; + line-height: 1.42857143; + border: 1px solid transparent; + border-radius: 4px 4px 0 0; +} +.nav-tabs > li > a:hover { + border-color: #eee #eee #ddd; +} +.nav-tabs > li.active > a, +.nav-tabs > li.active > a:hover, +.nav-tabs > li.active > a:focus { + color: #555; + cursor: default; + background-color: #fff; + border: 1px solid #ddd; + border-bottom-color: transparent; +} +.nav-tabs.nav-justified { + width: 100%; + border-bottom: 0; +} +.nav-tabs.nav-justified > li { + float: none; +} +.nav-tabs.nav-justified > li > a { + margin-bottom: 5px; + text-align: center; +} +.nav-tabs.nav-justified > .dropdown .dropdown-menu { + top: auto; + left: auto; +} +@media (min-width: 768px) { + .nav-tabs.nav-justified > li { + display: table-cell; + width: 1%; + } + .nav-tabs.nav-justified > li > a { + margin-bottom: 0; + } +} +.nav-tabs.nav-justified > li > a { + margin-right: 0; + border-radius: 4px; +} +.nav-tabs.nav-justified > .active > a, +.nav-tabs.nav-justified > .active > a:hover, +.nav-tabs.nav-justified > .active > a:focus { + border: 1px solid #ddd; +} +@media (min-width: 768px) { + .nav-tabs.nav-justified > li > a { + border-bottom: 1px solid #ddd; + border-radius: 4px 4px 0 0; + } + .nav-tabs.nav-justified > .active > a, + .nav-tabs.nav-justified > .active > a:hover, + .nav-tabs.nav-justified > .active > a:focus { + border-bottom-color: #fff; + } +} +.nav-pills > li { + float: left; +} +.nav-pills > li > a { + border-radius: 4px; +} +.nav-pills > li + li { + margin-left: 2px; +} +.nav-pills > li.active > a, +.nav-pills > li.active > a:hover, +.nav-pills > li.active > a:focus { + color: inherit; + background-color: #f7fafc; +} +.nav-stacked > li { + float: none; +} +.nav-stacked > li + li { + margin-top: 2px; + margin-left: 0; +} +.nav-justified { + width: 100%; +} +.nav-justified > li { + float: none; +} +.nav-justified > li > a { + margin-bottom: 5px; + text-align: center; +} +.nav-justified > .dropdown .dropdown-menu { + top: auto; + left: auto; +} +@media (min-width: 768px) { + .nav-justified > li { + display: table-cell; + width: 1%; + } + .nav-justified > li > a { + margin-bottom: 0; + } +} +.nav-tabs-justified { + border-bottom: 0; +} +.nav-tabs-justified > li > a { + margin-right: 0; + border-radius: 4px; +} +.nav-tabs-justified > .active > a, +.nav-tabs-justified > .active > a:hover, +.nav-tabs-justified > .active > a:focus { + border: 1px solid #ddd; +} +@media (min-width: 768px) { + .nav-tabs-justified > li > a { + border-bottom: 1px solid #ddd; + border-radius: 4px 4px 0 0; + } + .nav-tabs-justified > .active > a, + .nav-tabs-justified > .active > a:hover, + .nav-tabs-justified > .active > a:focus { + border-bottom-color: #fff; + } +} +.tab-content > .tab-pane { + display: none; + visibility: hidden; +} +.tab-content > .active { + display: block; + visibility: visible; +} +.nav-tabs .dropdown-menu { + margin-top: -1px; + border-top-left-radius: 0; + border-top-right-radius: 0; +} +.navbar { + position: relative; + min-height: 40px; + margin-bottom: 20px; + border: 1px solid transparent; +} +@media (min-width: 768px) { + .navbar { + border-radius: 4px; + } +} +@media (min-width: 768px) { + .navbar-header { + float: left; + } +} +.navbar-collapse { + padding-right: 15px; + padding-left: 15px; + overflow-x: visible; + -webkit-overflow-scrolling: touch; + border-top: 1px solid transparent; + -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1); + box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1); +} +.navbar-collapse.in { + overflow-y: auto; +} +@media (min-width: 768px) { + .navbar-collapse { + width: auto; + border-top: 0; + -webkit-box-shadow: none; + box-shadow: none; + } + .navbar-collapse.collapse { + display: block !important; + height: auto !important; + padding-bottom: 0; + overflow: visible !important; + visibility: visible !important; + } + .navbar-collapse.in { + overflow-y: visible; + } + .navbar-fixed-top .navbar-collapse, + .navbar-static-top .navbar-collapse, + .navbar-fixed-bottom .navbar-collapse { + padding-right: 0; + padding-left: 0; + } +} +.navbar-fixed-top .navbar-collapse, +.navbar-fixed-bottom .navbar-collapse { + max-height: 340px; +} +@media (max-device-width: 480px) and (orientation: landscape) { + .navbar-fixed-top .navbar-collapse, + .navbar-fixed-bottom .navbar-collapse { + max-height: 200px; + } +} +.container > .navbar-header, +.container-fluid > .navbar-header, +.container > .navbar-collapse, +.container-fluid > .navbar-collapse { + margin-right: -15px; + margin-left: -15px; +} +@media (min-width: 768px) { + .container > .navbar-header, + .container-fluid > .navbar-header, + .container > .navbar-collapse, + .container-fluid > .navbar-collapse { + margin-right: 0; + margin-left: 0; + } +} +.navbar-static-top { + z-index: 1000; + border-width: 0 0 1px; +} +@media (min-width: 768px) { + .navbar-static-top { + border-radius: 0; + } +} +.navbar-fixed-top, +.navbar-fixed-bottom { + position: fixed; + right: 0; + left: 0; + z-index: 1030; +} +@media (min-width: 768px) { + .navbar-fixed-top, + .navbar-fixed-bottom { + border-radius: 0; + } +} +.navbar-fixed-top { + top: 0; + border-width: 0 0 1px; +} +.navbar-fixed-bottom { + bottom: 0; + margin-bottom: 0; + border-width: 1px 0 0; +} +.navbar-brand { + float: left; + height: 40px; + padding: 10px 15px; + font-size: 18px; + line-height: 20px; +} +.navbar-brand:hover, +.navbar-brand:focus { + text-decoration: none; +} +.navbar-brand > img { + display: block; +} +@media (min-width: 768px) { + .navbar > .container .navbar-brand, + .navbar > .container-fluid .navbar-brand { + margin-left: -15px; + } +} +.navbar-toggle { + position: relative; + float: right; + padding: 9px 10px; + margin-top: 3px; + margin-right: 15px; + margin-bottom: 3px; + background-color: transparent; + background-image: none; + border: 1px solid transparent; + border-radius: 4px; +} +.navbar-toggle:focus { + outline: 0; +} +.navbar-toggle .icon-bar { + display: block; + width: 22px; + height: 2px; + border-radius: 1px; +} +.navbar-toggle .icon-bar + .icon-bar { + margin-top: 4px; +} +@media (min-width: 768px) { + .navbar-toggle { + display: none; + } +} +.navbar-nav { + margin: 5px -15px; +} +.navbar-nav > li > a { + padding-top: 10px; + padding-bottom: 10px; + line-height: 20px; +} +@media (max-width: 767px) { + .navbar-nav .open .dropdown-menu { + position: static; + float: none; + width: auto; + margin-top: 0; + background-color: transparent; + border: 0; + -webkit-box-shadow: none; + box-shadow: none; + } + .navbar-nav .open .dropdown-menu > li > a, + .navbar-nav .open .dropdown-menu .dropdown-header { + padding: 5px 15px 5px 25px; + } + .navbar-nav .open .dropdown-menu > li > a { + line-height: 20px; + } + .navbar-nav .open .dropdown-menu > li > a:hover, + .navbar-nav .open .dropdown-menu > li > a:focus { + background-image: none; + } +} +@media (min-width: 768px) { + .navbar-nav { + float: left; + margin: 0; + } + .navbar-nav > li { + float: left; + } + .navbar-nav > li > a { + padding-top: 10px; + padding-bottom: 10px; + } +} +.navbar-form { + padding: 10px 15px; + margin-top: 5px; + margin-right: -15px; + margin-bottom: 5px; + margin-left: -15px; + border-top: 1px solid transparent; + border-bottom: 1px solid transparent; + -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1), 0 1px 0 rgba(255, 255, 255, .1); + box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1), 0 1px 0 rgba(255, 255, 255, .1); +} +@media (min-width: 768px) { + .navbar-form .form-group { + display: inline-block; + margin-bottom: 0; + vertical-align: middle; + } + .navbar-form .form-control { + display: inline-block; + width: auto; + vertical-align: middle; + } + .navbar-form .form-control-static { + display: inline-block; + } + .navbar-form .input-group { + display: inline-table; + vertical-align: middle; + } + .navbar-form .input-group .input-group-addon, + .navbar-form .input-group .input-group-btn, + .navbar-form .input-group .form-control { + width: auto; + } + .navbar-form .input-group > .form-control { + width: 100%; + } + .navbar-form .control-label { + margin-bottom: 0; + vertical-align: middle; + } + .navbar-form .radio, + .navbar-form .checkbox { + display: inline-block; + margin-top: 0; + margin-bottom: 0; + vertical-align: middle; + } + .navbar-form .radio label, + .navbar-form .checkbox label { + padding-left: 0; + } + .navbar-form .radio input[type="radio"], + .navbar-form .checkbox input[type="checkbox"] { + position: relative; + margin-left: 0; + } + .navbar-form .has-feedback .form-control-feedback { + top: 0; + } +} +@media (max-width: 767px) { + .navbar-form .form-group { + margin-bottom: 5px; + } + .navbar-form .form-group:last-child { + margin-bottom: 0; + } +} +@media (min-width: 768px) { + .navbar-form { + width: auto; + padding-top: 0; + padding-bottom: 0; + margin-right: 0; + margin-left: 0; + border: 0; + -webkit-box-shadow: none; + box-shadow: none; + } +} +.navbar-nav > li > .dropdown-menu { + margin-top: 0; + border-top-left-radius: 0; + border-top-right-radius: 0; +} +.navbar-fixed-bottom .navbar-nav > li > .dropdown-menu { + margin-bottom: 0; + border-top-left-radius: 4px; + border-top-right-radius: 4px; + border-bottom-right-radius: 0; + border-bottom-left-radius: 0; +} +.navbar-btn { + margin-top: 5px; + margin-bottom: 5px; +} +.navbar-btn.btn-sm { + margin-top: 5px; + margin-bottom: 5px; +} +.navbar-btn.btn-xs { + margin-top: 9px; + margin-bottom: 9px; +} +.navbar-text { + margin-top: 10px; + margin-bottom: 10px; +} +@media (min-width: 768px) { + .navbar-text { + float: left; + margin-right: 15px; + margin-left: 15px; + } +} +@media (min-width: 768px) { + .navbar-left { + float: left !important; + } + .navbar-right { + float: right !important; + margin-right: -15px; + } + .navbar-right ~ .navbar-right { + margin-right: 0; + } +} +.navbar-default { + background-color: #f5f7fa; + border-color: #ebeff2; +} +.navbar-default .navbar-brand { + color: #6c7680; +} +.navbar-default .navbar-brand:hover, +.navbar-default .navbar-brand:focus { + color: #36414c; + background-color: transparent; +} +.navbar-default .navbar-text { + color: #6c7680; +} +.navbar-default .navbar-nav > li > a { + color: #6c7680; +} +.navbar-default .navbar-nav > li > a:hover, +.navbar-default .navbar-nav > li > a:focus { + color: #36414c; + background-color: transparent; +} +.navbar-default .navbar-nav > .active > a, +.navbar-default .navbar-nav > .active > a:hover, +.navbar-default .navbar-nav > .active > a:focus { + color: #555; + background-color: #dfe5ef; +} +.navbar-default .navbar-nav > .disabled > a, +.navbar-default .navbar-nav > .disabled > a:hover, +.navbar-default .navbar-nav > .disabled > a:focus { + color: #ccc; + background-color: transparent; +} +.navbar-default .navbar-toggle { + border-color: #ddd; +} +.navbar-default .navbar-toggle:hover, +.navbar-default .navbar-toggle:focus { + background-color: #ddd; +} +.navbar-default .navbar-toggle .icon-bar { + background-color: #888; +} +.navbar-default .navbar-collapse, +.navbar-default .navbar-form { + border-color: #ebeff2; +} +.navbar-default .navbar-nav > .open > a, +.navbar-default .navbar-nav > .open > a:hover, +.navbar-default .navbar-nav > .open > a:focus { + color: #555; + background-color: #dfe5ef; +} +@media (max-width: 767px) { + .navbar-default .navbar-nav .open .dropdown-menu > li > a { + color: #6c7680; + } + .navbar-default .navbar-nav .open .dropdown-menu > li > a:hover, + .navbar-default .navbar-nav .open .dropdown-menu > li > a:focus { + color: #36414c; + background-color: transparent; + } + .navbar-default .navbar-nav .open .dropdown-menu > .active > a, + .navbar-default .navbar-nav .open .dropdown-menu > .active > a:hover, + .navbar-default .navbar-nav .open .dropdown-menu > .active > a:focus { + color: #555; + background-color: #dfe5ef; + } + .navbar-default .navbar-nav .open .dropdown-menu > .disabled > a, + .navbar-default .navbar-nav .open .dropdown-menu > .disabled > a:hover, + .navbar-default .navbar-nav .open .dropdown-menu > .disabled > a:focus { + color: #ccc; + background-color: transparent; + } +} +.navbar-default .navbar-link { + color: #6c7680; +} +.navbar-default .navbar-link:hover { + color: #36414c; +} +.navbar-default .btn-link { + color: #6c7680; +} +.navbar-default .btn-link:hover, +.navbar-default .btn-link:focus { + color: #36414c; +} +.navbar-default .btn-link[disabled]:hover, +fieldset[disabled] .navbar-default .btn-link:hover, +.navbar-default .btn-link[disabled]:focus, +fieldset[disabled] .navbar-default .btn-link:focus { + color: #ccc; +} +.navbar-inverse { + background-color: #35414b; + border-color: #2a343c; +} +.navbar-inverse .navbar-brand { + color: #9d9d9d; +} +.navbar-inverse .navbar-brand:hover, +.navbar-inverse .navbar-brand:focus { + color: #fff; + background-color: transparent; +} +.navbar-inverse .navbar-text { + color: #b8c2cb; +} +.navbar-inverse .navbar-nav > li > a { + color: #9d9d9d; +} +.navbar-inverse .navbar-nav > li > a:hover, +.navbar-inverse .navbar-nav > li > a:focus { + color: #fff; + background-color: transparent; +} +.navbar-inverse .navbar-nav > .active > a, +.navbar-inverse .navbar-nav > .active > a:hover, +.navbar-inverse .navbar-nav > .active > a:focus { + color: #fff; + background-color: #20272d; +} +.navbar-inverse .navbar-nav > .disabled > a, +.navbar-inverse .navbar-nav > .disabled > a:hover, +.navbar-inverse .navbar-nav > .disabled > a:focus { + color: #475765; + background-color: transparent; +} +.navbar-inverse .navbar-toggle { + border-color: #475765; +} +.navbar-inverse .navbar-toggle:hover, +.navbar-inverse .navbar-toggle:focus { + background-color: #475765; +} +.navbar-inverse .navbar-toggle .icon-bar { + background-color: #fff; +} +.navbar-inverse .navbar-collapse, +.navbar-inverse .navbar-form { + border-color: #262f36; +} +.navbar-inverse .navbar-nav > .open > a, +.navbar-inverse .navbar-nav > .open > a:hover, +.navbar-inverse .navbar-nav > .open > a:focus { + color: #fff; + background-color: #20272d; +} +@media (max-width: 767px) { + .navbar-inverse .navbar-nav .open .dropdown-menu > .dropdown-header { + border-color: #2a343c; + } + .navbar-inverse .navbar-nav .open .dropdown-menu .divider { + background-color: #2a343c; + } + .navbar-inverse .navbar-nav .open .dropdown-menu > li > a { + color: #9d9d9d; + } + .navbar-inverse .navbar-nav .open .dropdown-menu > li > a:hover, + .navbar-inverse .navbar-nav .open .dropdown-menu > li > a:focus { + color: #fff; + background-color: transparent; + } + .navbar-inverse .navbar-nav .open .dropdown-menu > .active > a, + .navbar-inverse .navbar-nav .open .dropdown-menu > .active > a:hover, + .navbar-inverse .navbar-nav .open .dropdown-menu > .active > a:focus { + color: #fff; + background-color: #20272d; + } + .navbar-inverse .navbar-nav .open .dropdown-menu > .disabled > a, + .navbar-inverse .navbar-nav .open .dropdown-menu > .disabled > a:hover, + .navbar-inverse .navbar-nav .open .dropdown-menu > .disabled > a:focus { + color: #475765; + background-color: transparent; + } +} +.navbar-inverse .navbar-link { + color: #9d9d9d; +} +.navbar-inverse .navbar-link:hover { + color: #fff; +} +.navbar-inverse .btn-link { + color: #9d9d9d; +} +.navbar-inverse .btn-link:hover, +.navbar-inverse .btn-link:focus { + color: #fff; +} +.navbar-inverse .btn-link[disabled]:hover, +fieldset[disabled] .navbar-inverse .btn-link:hover, +.navbar-inverse .btn-link[disabled]:focus, +fieldset[disabled] .navbar-inverse .btn-link:focus { + color: #475765; +} +.breadcrumb { + padding: 8px 15px; + margin-bottom: 20px; + list-style: none; + background-color: #f5f5f5; + border-radius: 4px; +} +.breadcrumb > li { + display: inline-block; +} +.breadcrumb > li + li:before { + padding: 0 5px; + color: #ccc; + content: "/\00a0"; +} +.breadcrumb > .active { + color: #777; +} +.pagination { + display: inline-block; + padding-left: 0; + margin: 20px 0; + border-radius: 4px; +} +.pagination > li { + display: inline; +} +.pagination > li > a, +.pagination > li > span { + position: relative; + float: left; + padding: 4px 10px; + margin-left: -1px; + line-height: 1.42857143; + color: #36414c; + text-decoration: none; + background-color: #fff; + border: 1px solid #ddd; +} +.pagination > li:first-child > a, +.pagination > li:first-child > span { + margin-left: 0; + border-top-left-radius: 4px; + border-bottom-left-radius: 4px; +} +.pagination > li:last-child > a, +.pagination > li:last-child > span { + border-top-right-radius: 4px; + border-bottom-right-radius: 4px; +} +.pagination > li > a:hover, +.pagination > li > span:hover, +.pagination > li > a:focus, +.pagination > li > span:focus { + color: #161b1f; + background-color: #eee; + border-color: #ddd; +} +.pagination > .active > a, +.pagination > .active > span, +.pagination > .active > a:hover, +.pagination > .active > span:hover, +.pagination > .active > a:focus, +.pagination > .active > span:focus { + z-index: 2; + color: #fff; + cursor: default; + background-color: #5e64ff; + border-color: #5e64ff; +} +.pagination > .disabled > span, +.pagination > .disabled > span:hover, +.pagination > .disabled > span:focus, +.pagination > .disabled > a, +.pagination > .disabled > a:hover, +.pagination > .disabled > a:focus { + color: #777; + cursor: not-allowed; + background-color: #fff; + border-color: #ddd; +} +.pagination-lg > li > a, +.pagination-lg > li > span { + padding: 10px 16px; + font-size: 18px; +} +.pagination-lg > li:first-child > a, +.pagination-lg > li:first-child > span { + border-top-left-radius: 6px; + border-bottom-left-radius: 6px; +} +.pagination-lg > li:last-child > a, +.pagination-lg > li:last-child > span { + border-top-right-radius: 6px; + border-bottom-right-radius: 6px; +} +.pagination-sm > li > a, +.pagination-sm > li > span { + padding: 5px 10px; + font-size: 12px; +} +.pagination-sm > li:first-child > a, +.pagination-sm > li:first-child > span { + border-top-left-radius: 3px; + border-bottom-left-radius: 3px; +} +.pagination-sm > li:last-child > a, +.pagination-sm > li:last-child > span { + border-top-right-radius: 3px; + border-bottom-right-radius: 3px; +} +.pager { + padding-left: 0; + margin: 20px 0; + text-align: center; + list-style: none; +} +.pager li { + display: inline; +} +.pager li > a, +.pager li > span { + display: inline-block; + padding: 5px 14px; + background-color: #fff; + border: 1px solid #ddd; + border-radius: 15px; +} +.pager li > a:hover, +.pager li > a:focus { + text-decoration: none; + background-color: #eee; +} +.pager .next > a, +.pager .next > span { + float: right; +} +.pager .previous > a, +.pager .previous > span { + float: left; +} +.pager .disabled > a, +.pager .disabled > a:hover, +.pager .disabled > a:focus, +.pager .disabled > span { + color: #777; + cursor: not-allowed; + background-color: #fff; +} +.label { + display: inline; + padding: .2em .6em .3em; + font-size: 75%; + font-weight: bold; + line-height: 1; + color: inherit; + text-align: center; + white-space: nowrap; + vertical-align: baseline; + border-radius: .25em; +} +a.label:hover, +a.label:focus { + color: #fff; + text-decoration: none; + cursor: pointer; +} +.label:empty { + display: none; +} +.btn .label { + position: relative; + top: -1px; +} +.label-default { + background-color: #f0f4f7; +} +.label-default[href]:hover, +.label-default[href]:focus { + background-color: #cfdce5; +} +.label-primary { + background-color: #d9f6ff; +} +.label-primary[href]:hover, +.label-primary[href]:focus { + background-color: #a6eaff; +} +.label-success { + background-color: #e4ffc1; +} +.label-success[href]:hover, +.label-success[href]:focus { + background-color: #ceff8e; +} +.label-info { + background-color: #e8ddff; +} +.label-info[href]:hover, +.label-info[href]:focus { + background-color: #c6aaff; +} +.label-warning { + background-color: #ffe6bf; +} +.label-warning[href]:hover, +.label-warning[href]:focus { + background-color: #ffd28c; +} +.label-danger { + background-color: #ffdcdc; +} +.label-danger[href]:hover, +.label-danger[href]:focus { + background-color: #ffa9a9; +} +.badge { + display: inline-block; + min-width: 10px; + padding: 3px 7px; + font-size: 12px; + font-weight: bold; + line-height: 1; + color: #36414c; + text-align: center; + white-space: nowrap; + vertical-align: baseline; + background-color: #f0f4f7; + border-radius: 10px; +} +.badge:empty { + display: none; +} +.btn .badge { + position: relative; + top: -1px; +} +.btn-xs .badge { + top: 0; + padding: 1px 5px; +} +a.badge:hover, +a.badge:focus { + color: #fff; + text-decoration: none; + cursor: pointer; +} +.list-group-item.active > .badge, +.nav-pills > .active > a > .badge { + color: #36414c; + background-color: #fff; +} +.list-group-item > .badge { + float: right; +} +.list-group-item > .badge + .badge { + margin-right: 5px; +} +.nav-pills > li > a > .badge { + margin-left: 3px; +} +.jumbotron { + padding: 30px 15px; + margin-bottom: 30px; + color: inherit; + background-color: #eee; +} +.jumbotron h1, +.jumbotron .h1 { + color: inherit; +} +.jumbotron p { + margin-bottom: 15px; + font-size: 21px; + font-weight: 200; +} +.jumbotron > hr { + border-top-color: #d5d5d5; +} +.container .jumbotron, +.container-fluid .jumbotron { + border-radius: 6px; +} +.jumbotron .container { + max-width: 100%; +} +@media screen and (min-width: 768px) { + .jumbotron { + padding: 48px 0; + } + .container .jumbotron, + .container-fluid .jumbotron { + padding-right: 60px; + padding-left: 60px; + } + .jumbotron h1, + .jumbotron .h1 { + font-size: 63px; + } +} +.thumbnail { + display: block; + padding: 4px; + margin-bottom: 20px; + line-height: 1.42857143; + background-color: #fff; + border: 1px solid #ddd; + border-radius: 4px; + -webkit-transition: border .2s ease-in-out; + -o-transition: border .2s ease-in-out; + transition: border .2s ease-in-out; +} +.thumbnail > img, +.thumbnail a > img { + margin-right: auto; + margin-left: auto; +} +a.thumbnail:hover, +a.thumbnail:focus, +a.thumbnail.active { + border-color: #36414c; +} +.thumbnail .caption { + padding: 9px; + color: #36414c; +} +.alert { + padding: 10px; + margin-bottom: 20px; + border: 1px solid transparent; + border-radius: 4px; +} +.alert h4 { + margin-top: 0; + color: inherit; +} +.alert .alert-link { + font-weight: bold; +} +.alert > p, +.alert > ul { + margin-bottom: 0; +} +.alert > p + p { + margin-top: 5px; +} +.alert-dismissable, +.alert-dismissible { + padding-right: 30px; +} +.alert-dismissable .close, +.alert-dismissible .close { + position: relative; + top: -2px; + right: -21px; + color: inherit; +} +.alert-success { + color: #98d85b; + background-color: rgba(255, 255, 255, .9); + border-color: #98d85b; +} +.alert-success hr { + border-top-color: #8bd346; +} +.alert-success .alert-link { + color: #7ece32; +} +.alert-info { + color: #935eff; + background-color: rgba(255, 255, 255, .9); + border-color: #935eff; +} +.alert-info hr { + border-top-color: #8244ff; +} +.alert-info .alert-link { + color: #712bff; +} +.alert-warning { + color: #ffa00a; + background-color: rgba(255, 255, 255, .9); + border-color: #ffa00a; +} +.alert-warning hr { + border-top-color: #f09300; +} +.alert-warning .alert-link { + color: #d68300; +} +.alert-danger { + color: #ff5858; + background-color: rgba(255, 255, 255, .9); + border-color: #ff5858; +} +.alert-danger hr { + border-top-color: #ff3f3f; +} +.alert-danger .alert-link { + color: #ff2525; +} +@-webkit-keyframes progress-bar-stripes { + from { + background-position: 40px 0; + } + to { + background-position: 0 0; + } +} +@-o-keyframes progress-bar-stripes { + from { + background-position: 40px 0; + } + to { + background-position: 0 0; + } +} +@keyframes progress-bar-stripes { + from { + background-position: 40px 0; + } + to { + background-position: 0 0; + } +} +.progress { + height: 20px; + margin-bottom: 20px; + overflow: hidden; + background-color: #f5f5f5; + border-radius: 4px; + -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1); + box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1); +} +.progress-bar { + float: left; + width: 0; + height: 100%; + font-size: 12px; + line-height: 20px; + color: #fff; + text-align: center; + background-color: #36414c; + -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15); + box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15); + -webkit-transition: width .6s ease; + -o-transition: width .6s ease; + transition: width .6s ease; +} +.progress-striped .progress-bar, +.progress-bar-striped { + background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + -webkit-background-size: 40px 40px; + background-size: 40px 40px; +} +.progress.active .progress-bar, +.progress-bar.active { + -webkit-animation: progress-bar-stripes 2s linear infinite; + -o-animation: progress-bar-stripes 2s linear infinite; + animation: progress-bar-stripes 2s linear infinite; +} +.progress-bar-success { + background-color: #98d85b; +} +.progress-striped .progress-bar-success { + background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); +} +.progress-bar-info { + background-color: #935eff; +} +.progress-striped .progress-bar-info { + background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); +} +.progress-bar-warning { + background-color: #ffa00a; +} +.progress-striped .progress-bar-warning { + background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); +} +.progress-bar-danger { + background-color: #ff5858; +} +.progress-striped .progress-bar-danger { + background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); + background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent); +} +.media { + margin-top: 15px; +} +.media:first-child { + margin-top: 0; +} +.media, +.media-body { + overflow: hidden; + zoom: 1; +} +.media-body { + width: 10000px; +} +.media-object { + display: block; +} +.media-right, +.media > .pull-right { + padding-left: 10px; +} +.media-left, +.media > .pull-left { + padding-right: 10px; +} +.media-left, +.media-right, +.media-body { + display: table-cell; + vertical-align: top; +} +.media-middle { + vertical-align: middle; +} +.media-bottom { + vertical-align: bottom; +} +.media-heading { + margin-top: 0; + margin-bottom: 5px; +} +.media-list { + padding-left: 0; + list-style: none; +} +.list-group { + padding-left: 0; + margin-bottom: 20px; +} +.list-group-item { + position: relative; + display: block; + padding: 10px 15px; + margin-bottom: -1px; + background-color: #fff; + border: 1px solid #ddd; +} +.list-group-item:first-child { + border-top-left-radius: 4px; + border-top-right-radius: 4px; +} +.list-group-item:last-child { + margin-bottom: 0; + border-bottom-right-radius: 4px; + border-bottom-left-radius: 4px; +} +a.list-group-item { + color: #555; +} +a.list-group-item .list-group-item-heading { + color: #333; +} +a.list-group-item:hover, +a.list-group-item:focus { + color: #555; + text-decoration: none; + background-color: #f5f5f5; +} +.list-group-item.disabled, +.list-group-item.disabled:hover, +.list-group-item.disabled:focus { + color: #777; + cursor: not-allowed; + background-color: #eee; +} +.list-group-item.disabled .list-group-item-heading, +.list-group-item.disabled:hover .list-group-item-heading, +.list-group-item.disabled:focus .list-group-item-heading { + color: inherit; +} +.list-group-item.disabled .list-group-item-text, +.list-group-item.disabled:hover .list-group-item-text, +.list-group-item.disabled:focus .list-group-item-text { + color: #777; +} +.list-group-item.active, +.list-group-item.active:hover, +.list-group-item.active:focus { + z-index: 2; + color: inherit; + background-color: #f0f4f7; + border-color: #f0f4f7; +} +.list-group-item.active .list-group-item-heading, +.list-group-item.active:hover .list-group-item-heading, +.list-group-item.active:focus .list-group-item-heading, +.list-group-item.active .list-group-item-heading > small, +.list-group-item.active:hover .list-group-item-heading > small, +.list-group-item.active:focus .list-group-item-heading > small, +.list-group-item.active .list-group-item-heading > .small, +.list-group-item.active:hover .list-group-item-heading > .small, +.list-group-item.active:focus .list-group-item-heading > .small { + color: inherit; +} +.list-group-item.active .list-group-item-text, +.list-group-item.active:hover .list-group-item-text, +.list-group-item.active:focus .list-group-item-text { + color: #fff; +} +.list-group-item-success { + color: #98d85b; + background-color: transparent; +} +a.list-group-item-success { + color: #98d85b; +} +a.list-group-item-success .list-group-item-heading { + color: inherit; +} +a.list-group-item-success:hover, +a.list-group-item-success:focus { + color: #98d85b; + background-color: rgba(0, 0, 0, 0); +} +a.list-group-item-success.active, +a.list-group-item-success.active:hover, +a.list-group-item-success.active:focus { + color: #fff; + background-color: #98d85b; + border-color: #98d85b; +} +.list-group-item-info { + color: #935eff; + background-color: transparent; +} +a.list-group-item-info { + color: #935eff; +} +a.list-group-item-info .list-group-item-heading { + color: inherit; +} +a.list-group-item-info:hover, +a.list-group-item-info:focus { + color: #935eff; + background-color: rgba(0, 0, 0, 0); +} +a.list-group-item-info.active, +a.list-group-item-info.active:hover, +a.list-group-item-info.active:focus { + color: #fff; + background-color: #935eff; + border-color: #935eff; +} +.list-group-item-warning { + color: #ffa00a; + background-color: transparent; +} +a.list-group-item-warning { + color: #ffa00a; +} +a.list-group-item-warning .list-group-item-heading { + color: inherit; +} +a.list-group-item-warning:hover, +a.list-group-item-warning:focus { + color: #ffa00a; + background-color: rgba(0, 0, 0, 0); +} +a.list-group-item-warning.active, +a.list-group-item-warning.active:hover, +a.list-group-item-warning.active:focus { + color: #fff; + background-color: #ffa00a; + border-color: #ffa00a; +} +.list-group-item-danger { + color: #ff5858; + background-color: transparent; +} +a.list-group-item-danger { + color: #ff5858; +} +a.list-group-item-danger .list-group-item-heading { + color: inherit; +} +a.list-group-item-danger:hover, +a.list-group-item-danger:focus { + color: #ff5858; + background-color: rgba(0, 0, 0, 0); +} +a.list-group-item-danger.active, +a.list-group-item-danger.active:hover, +a.list-group-item-danger.active:focus { + color: #fff; + background-color: #ff5858; + border-color: #ff5858; +} +.list-group-item-heading { + margin-top: 0; + margin-bottom: 5px; +} +.list-group-item-text { + margin-bottom: 0; + line-height: 1.3; +} +.panel { + margin-bottom: 20px; + background-color: #fff; + border: 1px solid transparent; + border-radius: 4px; + -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05); + box-shadow: 0 1px 1px rgba(0, 0, 0, .05); +} +.panel-body { + padding: 15px; +} +.panel-heading { + padding: 10px 15px; + border-bottom: 1px solid transparent; + border-top-left-radius: 3px; + border-top-right-radius: 3px; +} +.panel-heading > .dropdown .dropdown-toggle { + color: inherit; +} +.panel-title { + margin-top: 0; + margin-bottom: 0; + font-size: 16px; + color: inherit; +} +.panel-title > a { + color: inherit; +} +.panel-footer { + padding: 10px 15px; + background-color: #f5f5f5; + border-top: 1px solid #ddd; + border-bottom-right-radius: 3px; + border-bottom-left-radius: 3px; +} +.panel > .list-group, +.panel > .panel-collapse > .list-group { + margin-bottom: 0; +} +.panel > .list-group .list-group-item, +.panel > .panel-collapse > .list-group .list-group-item { + border-width: 1px 0; + border-radius: 0; +} +.panel > .list-group:first-child .list-group-item:first-child, +.panel > .panel-collapse > .list-group:first-child .list-group-item:first-child { + border-top: 0; + border-top-left-radius: 3px; + border-top-right-radius: 3px; +} +.panel > .list-group:last-child .list-group-item:last-child, +.panel > .panel-collapse > .list-group:last-child .list-group-item:last-child { + border-bottom: 0; + border-bottom-right-radius: 3px; + border-bottom-left-radius: 3px; +} +.panel-heading + .list-group .list-group-item:first-child { + border-top-width: 0; +} +.list-group + .panel-footer { + border-top-width: 0; +} +.panel > .table, +.panel > .table-responsive > .table, +.panel > .panel-collapse > .table { + margin-bottom: 0; +} +.panel > .table caption, +.panel > .table-responsive > .table caption, +.panel > .panel-collapse > .table caption { + padding-right: 15px; + padding-left: 15px; +} +.panel > .table:first-child, +.panel > .table-responsive:first-child > .table:first-child { + border-top-left-radius: 3px; + border-top-right-radius: 3px; +} +.panel > .table:first-child > thead:first-child > tr:first-child, +.panel > .table-responsive:first-child > .table:first-child > thead:first-child > tr:first-child, +.panel > .table:first-child > tbody:first-child > tr:first-child, +.panel > .table-responsive:first-child > .table:first-child > tbody:first-child > tr:first-child { + border-top-left-radius: 3px; + border-top-right-radius: 3px; +} +.panel > .table:first-child > thead:first-child > tr:first-child td:first-child, +.panel > .table-responsive:first-child > .table:first-child > thead:first-child > tr:first-child td:first-child, +.panel > .table:first-child > tbody:first-child > tr:first-child td:first-child, +.panel > .table-responsive:first-child > .table:first-child > tbody:first-child > tr:first-child td:first-child, +.panel > .table:first-child > thead:first-child > tr:first-child th:first-child, +.panel > .table-responsive:first-child > .table:first-child > thead:first-child > tr:first-child th:first-child, +.panel > .table:first-child > tbody:first-child > tr:first-child th:first-child, +.panel > .table-responsive:first-child > .table:first-child > tbody:first-child > tr:first-child th:first-child { + border-top-left-radius: 3px; +} +.panel > .table:first-child > thead:first-child > tr:first-child td:last-child, +.panel > .table-responsive:first-child > .table:first-child > thead:first-child > tr:first-child td:last-child, +.panel > .table:first-child > tbody:first-child > tr:first-child td:last-child, +.panel > .table-responsive:first-child > .table:first-child > tbody:first-child > tr:first-child td:last-child, +.panel > .table:first-child > thead:first-child > tr:first-child th:last-child, +.panel > .table-responsive:first-child > .table:first-child > thead:first-child > tr:first-child th:last-child, +.panel > .table:first-child > tbody:first-child > tr:first-child th:last-child, +.panel > .table-responsive:first-child > .table:first-child > tbody:first-child > tr:first-child th:last-child { + border-top-right-radius: 3px; +} +.panel > .table:last-child, +.panel > .table-responsive:last-child > .table:last-child { + border-bottom-right-radius: 3px; + border-bottom-left-radius: 3px; +} +.panel > .table:last-child > tbody:last-child > tr:last-child, +.panel > .table-responsive:last-child > .table:last-child > tbody:last-child > tr:last-child, +.panel > .table:last-child > tfoot:last-child > tr:last-child, +.panel > .table-responsive:last-child > .table:last-child > tfoot:last-child > tr:last-child { + border-bottom-right-radius: 3px; + border-bottom-left-radius: 3px; +} +.panel > .table:last-child > tbody:last-child > tr:last-child td:first-child, +.panel > .table-responsive:last-child > .table:last-child > tbody:last-child > tr:last-child td:first-child, +.panel > .table:last-child > tfoot:last-child > tr:last-child td:first-child, +.panel > .table-responsive:last-child > .table:last-child > tfoot:last-child > tr:last-child td:first-child, +.panel > .table:last-child > tbody:last-child > tr:last-child th:first-child, +.panel > .table-responsive:last-child > .table:last-child > tbody:last-child > tr:last-child th:first-child, +.panel > .table:last-child > tfoot:last-child > tr:last-child th:first-child, +.panel > .table-responsive:last-child > .table:last-child > tfoot:last-child > tr:last-child th:first-child { + border-bottom-left-radius: 3px; +} +.panel > .table:last-child > tbody:last-child > tr:last-child td:last-child, +.panel > .table-responsive:last-child > .table:last-child > tbody:last-child > tr:last-child td:last-child, +.panel > .table:last-child > tfoot:last-child > tr:last-child td:last-child, +.panel > .table-responsive:last-child > .table:last-child > tfoot:last-child > tr:last-child td:last-child, +.panel > .table:last-child > tbody:last-child > tr:last-child th:last-child, +.panel > .table-responsive:last-child > .table:last-child > tbody:last-child > tr:last-child th:last-child, +.panel > .table:last-child > tfoot:last-child > tr:last-child th:last-child, +.panel > .table-responsive:last-child > .table:last-child > tfoot:last-child > tr:last-child th:last-child { + border-bottom-right-radius: 3px; +} +.panel > .panel-body + .table, +.panel > .panel-body + .table-responsive, +.panel > .table + .panel-body, +.panel > .table-responsive + .panel-body { + border-top: 1px solid #d1d8dd; +} +.panel > .table > tbody:first-child > tr:first-child th, +.panel > .table > tbody:first-child > tr:first-child td { + border-top: 0; +} +.panel > .table-bordered, +.panel > .table-responsive > .table-bordered { + border: 0; +} +.panel > .table-bordered > thead > tr > th:first-child, +.panel > .table-responsive > .table-bordered > thead > tr > th:first-child, +.panel > .table-bordered > tbody > tr > th:first-child, +.panel > .table-responsive > .table-bordered > tbody > tr > th:first-child, +.panel > .table-bordered > tfoot > tr > th:first-child, +.panel > .table-responsive > .table-bordered > tfoot > tr > th:first-child, +.panel > .table-bordered > thead > tr > td:first-child, +.panel > .table-responsive > .table-bordered > thead > tr > td:first-child, +.panel > .table-bordered > tbody > tr > td:first-child, +.panel > .table-responsive > .table-bordered > tbody > tr > td:first-child, +.panel > .table-bordered > tfoot > tr > td:first-child, +.panel > .table-responsive > .table-bordered > tfoot > tr > td:first-child { + border-left: 0; +} +.panel > .table-bordered > thead > tr > th:last-child, +.panel > .table-responsive > .table-bordered > thead > tr > th:last-child, +.panel > .table-bordered > tbody > tr > th:last-child, +.panel > .table-responsive > .table-bordered > tbody > tr > th:last-child, +.panel > .table-bordered > tfoot > tr > th:last-child, +.panel > .table-responsive > .table-bordered > tfoot > tr > th:last-child, +.panel > .table-bordered > thead > tr > td:last-child, +.panel > .table-responsive > .table-bordered > thead > tr > td:last-child, +.panel > .table-bordered > tbody > tr > td:last-child, +.panel > .table-responsive > .table-bordered > tbody > tr > td:last-child, +.panel > .table-bordered > tfoot > tr > td:last-child, +.panel > .table-responsive > .table-bordered > tfoot > tr > td:last-child { + border-right: 0; +} +.panel > .table-bordered > thead > tr:first-child > td, +.panel > .table-responsive > .table-bordered > thead > tr:first-child > td, +.panel > .table-bordered > tbody > tr:first-child > td, +.panel > .table-responsive > .table-bordered > tbody > tr:first-child > td, +.panel > .table-bordered > thead > tr:first-child > th, +.panel > .table-responsive > .table-bordered > thead > tr:first-child > th, +.panel > .table-bordered > tbody > tr:first-child > th, +.panel > .table-responsive > .table-bordered > tbody > tr:first-child > th { + border-bottom: 0; +} +.panel > .table-bordered > tbody > tr:last-child > td, +.panel > .table-responsive > .table-bordered > tbody > tr:last-child > td, +.panel > .table-bordered > tfoot > tr:last-child > td, +.panel > .table-responsive > .table-bordered > tfoot > tr:last-child > td, +.panel > .table-bordered > tbody > tr:last-child > th, +.panel > .table-responsive > .table-bordered > tbody > tr:last-child > th, +.panel > .table-bordered > tfoot > tr:last-child > th, +.panel > .table-responsive > .table-bordered > tfoot > tr:last-child > th { + border-bottom: 0; +} +.panel > .table-responsive { + margin-bottom: 0; + border: 0; +} +.panel-group { + margin-bottom: 20px; +} +.panel-group .panel { + margin-bottom: 0; + border-radius: 4px; +} +.panel-group .panel + .panel { + margin-top: 5px; +} +.panel-group .panel-heading { + border-bottom: 0; +} +.panel-group .panel-heading + .panel-collapse > .panel-body, +.panel-group .panel-heading + .panel-collapse > .list-group { + border-top: 1px solid #ddd; +} +.panel-group .panel-footer { + border-top: 0; +} +.panel-group .panel-footer + .panel-collapse .panel-body { + border-bottom: 1px solid #ddd; +} +.panel-default { + border-color: #ced5db; +} +.panel-default > .panel-heading { + color: #333; + background-color: #f7fafc; + border-color: #ced5db; +} +.panel-default > .panel-heading + .panel-collapse > .panel-body { + border-top-color: #ced5db; +} +.panel-default > .panel-heading .badge { + color: #f7fafc; + background-color: #333; +} +.panel-default > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: #ced5db; +} +.panel-primary { + border-color: #5e64ff; +} +.panel-primary > .panel-heading { + color: #fff; + background-color: #5e64ff; + border-color: #5e64ff; +} +.panel-primary > .panel-heading + .panel-collapse > .panel-body { + border-top-color: #5e64ff; +} +.panel-primary > .panel-heading .badge { + color: #5e64ff; + background-color: #fff; +} +.panel-primary > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: #5e64ff; +} +.panel-success { + border-color: rgba(0, 0, 0, 0); +} +.panel-success > .panel-heading { + color: #98d85b; + background-color: transparent; + border-color: rgba(0, 0, 0, 0); +} +.panel-success > .panel-heading + .panel-collapse > .panel-body { + border-top-color: rgba(0, 0, 0, 0); +} +.panel-success > .panel-heading .badge { + color: transparent; + background-color: #98d85b; +} +.panel-success > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: rgba(0, 0, 0, 0); +} +.panel-info { + border-color: rgba(0, 0, 0, 0); +} +.panel-info > .panel-heading { + color: #935eff; + background-color: transparent; + border-color: rgba(0, 0, 0, 0); +} +.panel-info > .panel-heading + .panel-collapse > .panel-body { + border-top-color: rgba(0, 0, 0, 0); +} +.panel-info > .panel-heading .badge { + color: transparent; + background-color: #935eff; +} +.panel-info > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: rgba(0, 0, 0, 0); +} +.panel-warning { + border-color: rgba(0, 0, 0, 0); +} +.panel-warning > .panel-heading { + color: #ffa00a; + background-color: transparent; + border-color: rgba(0, 0, 0, 0); +} +.panel-warning > .panel-heading + .panel-collapse > .panel-body { + border-top-color: rgba(0, 0, 0, 0); +} +.panel-warning > .panel-heading .badge { + color: transparent; + background-color: #ffa00a; +} +.panel-warning > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: rgba(0, 0, 0, 0); +} +.panel-danger { + border-color: rgba(0, 0, 0, 0); +} +.panel-danger > .panel-heading { + color: #ff5858; + background-color: transparent; + border-color: rgba(0, 0, 0, 0); +} +.panel-danger > .panel-heading + .panel-collapse > .panel-body { + border-top-color: rgba(0, 0, 0, 0); +} +.panel-danger > .panel-heading .badge { + color: transparent; + background-color: #ff5858; +} +.panel-danger > .panel-footer + .panel-collapse > .panel-body { + border-bottom-color: rgba(0, 0, 0, 0); +} +.embed-responsive { + position: relative; + display: block; + height: 0; + padding: 0; + overflow: hidden; +} +.embed-responsive .embed-responsive-item, +.embed-responsive iframe, +.embed-responsive embed, +.embed-responsive object, +.embed-responsive video { + position: absolute; + top: 0; + bottom: 0; + left: 0; + width: 100%; + height: 100%; + border: 0; +} +.embed-responsive.embed-responsive-16by9 { + padding-bottom: 56.25%; +} +.embed-responsive.embed-responsive-4by3 { + padding-bottom: 75%; +} +.well { + min-height: 20px; + padding: 19px; + margin-bottom: 20px; + background-color: #fafbfc; + border: 1px solid #d1d8dd; + border-radius: 4px; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05); +} +.well blockquote { + border-color: #ddd; + border-color: rgba(0, 0, 0, .15); +} +.well-lg { + padding: 24px; + border-radius: 6px; +} +.well-sm { + padding: 9px; + border-radius: 3px; +} +.close { + float: right; + font-size: 21px; + font-weight: bold; + line-height: 1; + color: #000; + text-shadow: 0 1px 0 #fff; + filter: alpha(opacity=20); + opacity: .2; +} +.close:hover, +.close:focus { + color: #000; + text-decoration: none; + cursor: pointer; + filter: alpha(opacity=50); + opacity: .5; +} +button.close { + -webkit-appearance: none; + padding: 0; + cursor: pointer; + background: transparent; + border: 0; +} +.modal-open { + overflow: hidden; +} +.modal { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: 1040; + display: none; + overflow: hidden; + -webkit-overflow-scrolling: touch; + outline: 0; +} +.modal.fade .modal-dialog { + -webkit-transition: -webkit-transform .3s ease-out; + -o-transition: -o-transform .3s ease-out; + transition: transform .3s ease-out; + -webkit-transform: translate(0, -25%); + -ms-transform: translate(0, -25%); + -o-transform: translate(0, -25%); + transform: translate(0, -25%); +} +.modal.in .modal-dialog { + -webkit-transform: translate(0, 0); + -ms-transform: translate(0, 0); + -o-transform: translate(0, 0); + transform: translate(0, 0); +} +.modal-open .modal { + overflow-x: hidden; + overflow-y: auto; +} +.modal-dialog { + position: relative; + width: auto; + margin: 10px; +} +.modal-content { + position: relative; + background-color: #fff; + -webkit-background-clip: padding-box; + background-clip: padding-box; + border: 1px solid #999; + border: 1px solid rgba(0, 0, 0, .2); + border-radius: 6px; + outline: 0; + -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, .5); + box-shadow: 0 3px 9px rgba(0, 0, 0, .5); +} +.modal-backdrop { + position: absolute; + top: 0; + right: 0; + left: 0; + background-color: #334143; +} +.modal-backdrop.fade { + filter: alpha(opacity=0); + opacity: 0; +} +.modal-backdrop.in { + filter: alpha(opacity=50); + opacity: .5; +} +.modal-header { + min-height: 16.42857143px; + padding: 15px; + border-bottom: 1px solid #e5e5e5; +} +.modal-header .close { + margin-top: -2px; +} +.modal-title { + margin: 0; + line-height: 1.42857143; +} +.modal-body { + position: relative; + padding: 15px; +} +.modal-footer { + padding: 15px; + text-align: right; + border-top: 1px solid #e5e5e5; +} +.modal-footer .btn + .btn { + margin-bottom: 0; + margin-left: 5px; +} +.modal-footer .btn-group .btn + .btn { + margin-left: -1px; +} +.modal-footer .btn-block + .btn-block { + margin-left: 0; +} +.modal-scrollbar-measure { + position: absolute; + top: -9999px; + width: 50px; + height: 50px; + overflow: scroll; +} +@media (min-width: 768px) { + .modal-dialog { + width: 600px; + margin: 30px auto; + } + .modal-content { + -webkit-box-shadow: 0 5px 15px rgba(0, 0, 0, .5); + box-shadow: 0 5px 15px rgba(0, 0, 0, .5); + } + .modal-sm { + width: 300px; + } +} +@media (min-width: 992px) { + .modal-lg { + width: 900px; + } +} +.tooltip { + position: absolute; + z-index: 1070; + display: block; + font-family: "Helvetica Neue", Helvetica, Arial, "Open Sans", sans-serif; + font-size: 12px; + font-weight: normal; + line-height: 1.4; + visibility: visible; + filter: alpha(opacity=0); + opacity: 0; +} +.tooltip.in { + filter: alpha(opacity=90); + opacity: .9; +} +.tooltip.top { + padding: 5px 0; + margin-top: -3px; +} +.tooltip.right { + padding: 0 5px; + margin-left: 3px; +} +.tooltip.bottom { + padding: 5px 0; + margin-top: 3px; +} +.tooltip.left { + padding: 0 5px; + margin-left: -3px; +} +.tooltip-inner { + max-width: 200px; + padding: 3px 8px; + color: #fff; + text-align: center; + text-decoration: none; + background-color: #000; + border-radius: 4px; +} +.tooltip-arrow { + position: absolute; + width: 0; + height: 0; + border-color: transparent; + border-style: solid; +} +.tooltip.top .tooltip-arrow { + bottom: 0; + left: 50%; + margin-left: -5px; + border-width: 5px 5px 0; + border-top-color: #000; +} +.tooltip.top-left .tooltip-arrow { + right: 5px; + bottom: 0; + margin-bottom: -5px; + border-width: 5px 5px 0; + border-top-color: #000; +} +.tooltip.top-right .tooltip-arrow { + bottom: 0; + left: 5px; + margin-bottom: -5px; + border-width: 5px 5px 0; + border-top-color: #000; +} +.tooltip.right .tooltip-arrow { + top: 50%; + left: 0; + margin-top: -5px; + border-width: 5px 5px 5px 0; + border-right-color: #000; +} +.tooltip.left .tooltip-arrow { + top: 50%; + right: 0; + margin-top: -5px; + border-width: 5px 0 5px 5px; + border-left-color: #000; +} +.tooltip.bottom .tooltip-arrow { + top: 0; + left: 50%; + margin-left: -5px; + border-width: 0 5px 5px; + border-bottom-color: #000; +} +.tooltip.bottom-left .tooltip-arrow { + top: 0; + right: 5px; + margin-top: -5px; + border-width: 0 5px 5px; + border-bottom-color: #000; +} +.tooltip.bottom-right .tooltip-arrow { + top: 0; + left: 5px; + margin-top: -5px; + border-width: 0 5px 5px; + border-bottom-color: #000; +} +.popover { + position: absolute; + top: 0; + left: 0; + z-index: 1060; + display: none; + max-width: 276px; + padding: 1px; + font-family: "Helvetica Neue", Helvetica, Arial, "Open Sans", sans-serif; + font-size: 14px; + font-weight: normal; + line-height: 1.42857143; + text-align: left; + white-space: normal; + background-color: #fff; + -webkit-background-clip: padding-box; + background-clip: padding-box; + border: 1px solid #ccc; + border: 1px solid rgba(0, 0, 0, .2); + border-radius: 6px; + -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2); + box-shadow: 0 5px 10px rgba(0, 0, 0, .2); +} +.popover.top { + margin-top: -10px; +} +.popover.right { + margin-left: 10px; +} +.popover.bottom { + margin-top: 10px; +} +.popover.left { + margin-left: -10px; +} +.popover-title { + padding: 8px 14px; + margin: 0; + font-size: 14px; + background-color: #f7f7f7; + border-bottom: 1px solid #ebebeb; + border-radius: 5px 5px 0 0; +} +.popover-content { + padding: 9px 14px; +} +.popover > .arrow, +.popover > .arrow:after { + position: absolute; + display: block; + width: 0; + height: 0; + border-color: transparent; + border-style: solid; +} +.popover > .arrow { + border-width: 11px; +} +.popover > .arrow:after { + content: ""; + border-width: 10px; +} +.popover.top > .arrow { + bottom: -11px; + left: 50%; + margin-left: -11px; + border-top-color: #999; + border-top-color: rgba(0, 0, 0, .25); + border-bottom-width: 0; +} +.popover.top > .arrow:after { + bottom: 1px; + margin-left: -10px; + content: " "; + border-top-color: #fff; + border-bottom-width: 0; +} +.popover.right > .arrow { + top: 50%; + left: -11px; + margin-top: -11px; + border-right-color: #999; + border-right-color: rgba(0, 0, 0, .25); + border-left-width: 0; +} +.popover.right > .arrow:after { + bottom: -10px; + left: 1px; + content: " "; + border-right-color: #fff; + border-left-width: 0; +} +.popover.bottom > .arrow { + top: -11px; + left: 50%; + margin-left: -11px; + border-top-width: 0; + border-bottom-color: #999; + border-bottom-color: rgba(0, 0, 0, .25); +} +.popover.bottom > .arrow:after { + top: 1px; + margin-left: -10px; + content: " "; + border-top-width: 0; + border-bottom-color: #fff; +} +.popover.left > .arrow { + top: 50%; + right: -11px; + margin-top: -11px; + border-right-width: 0; + border-left-color: #999; + border-left-color: rgba(0, 0, 0, .25); +} +.popover.left > .arrow:after { + right: 1px; + bottom: -10px; + content: " "; + border-right-width: 0; + border-left-color: #fff; +} +.carousel { + position: relative; +} +.carousel-inner { + position: relative; + width: 100%; + overflow: hidden; +} +.carousel-inner > .item { + position: relative; + display: none; + -webkit-transition: .6s ease-in-out left; + -o-transition: .6s ease-in-out left; + transition: .6s ease-in-out left; +} +.carousel-inner > .item > img, +.carousel-inner > .item > a > img { + line-height: 1; +} +@media all and (transform-3d), (-webkit-transform-3d) { + .carousel-inner > .item { + -webkit-transition: -webkit-transform .6s ease-in-out; + -o-transition: -o-transform .6s ease-in-out; + transition: transform .6s ease-in-out; + + -webkit-backface-visibility: hidden; + backface-visibility: hidden; + -webkit-perspective: 1000; + perspective: 1000; + } + .carousel-inner > .item.next, + .carousel-inner > .item.active.right { + left: 0; + -webkit-transform: translate3d(100%, 0, 0); + transform: translate3d(100%, 0, 0); + } + .carousel-inner > .item.prev, + .carousel-inner > .item.active.left { + left: 0; + -webkit-transform: translate3d(-100%, 0, 0); + transform: translate3d(-100%, 0, 0); + } + .carousel-inner > .item.next.left, + .carousel-inner > .item.prev.right, + .carousel-inner > .item.active { + left: 0; + -webkit-transform: translate3d(0, 0, 0); + transform: translate3d(0, 0, 0); + } +} +.carousel-inner > .active, +.carousel-inner > .next, +.carousel-inner > .prev { + display: block; +} +.carousel-inner > .active { + left: 0; +} +.carousel-inner > .next, +.carousel-inner > .prev { + position: absolute; + top: 0; + width: 100%; +} +.carousel-inner > .next { + left: 100%; +} +.carousel-inner > .prev { + left: -100%; +} +.carousel-inner > .next.left, +.carousel-inner > .prev.right { + left: 0; +} +.carousel-inner > .active.left { + left: -100%; +} +.carousel-inner > .active.right { + left: 100%; +} +.carousel-control { + position: absolute; + top: 0; + bottom: 0; + left: 0; + width: 15%; + font-size: 20px; + color: #fff; + text-align: center; + text-shadow: 0 1px 2px rgba(0, 0, 0, .6); + filter: alpha(opacity=50); + opacity: .5; +} +.carousel-control.left { + background-image: -webkit-linear-gradient(left, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .0001) 100%); + background-image: -o-linear-gradient(left, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .0001) 100%); + background-image: -webkit-gradient(linear, left top, right top, from(rgba(0, 0, 0, .5)), to(rgba(0, 0, 0, .0001))); + background-image: linear-gradient(to right, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .0001) 100%); + filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#00000000', GradientType=1); + background-repeat: repeat-x; +} +.carousel-control.right { + right: 0; + left: auto; + background-image: -webkit-linear-gradient(left, rgba(0, 0, 0, .0001) 0%, rgba(0, 0, 0, .5) 100%); + background-image: -o-linear-gradient(left, rgba(0, 0, 0, .0001) 0%, rgba(0, 0, 0, .5) 100%); + background-image: -webkit-gradient(linear, left top, right top, from(rgba(0, 0, 0, .0001)), to(rgba(0, 0, 0, .5))); + background-image: linear-gradient(to right, rgba(0, 0, 0, .0001) 0%, rgba(0, 0, 0, .5) 100%); + filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=1); + background-repeat: repeat-x; +} +.carousel-control:hover, +.carousel-control:focus { + color: #fff; + text-decoration: none; + filter: alpha(opacity=90); + outline: 0; + opacity: .9; +} +.carousel-control .icon-prev, +.carousel-control .icon-next, +.carousel-control .glyphicon-chevron-left, +.carousel-control .glyphicon-chevron-right { + position: absolute; + top: 50%; + z-index: 5; + display: inline-block; +} +.carousel-control .icon-prev, +.carousel-control .glyphicon-chevron-left { + left: 50%; + margin-left: -10px; +} +.carousel-control .icon-next, +.carousel-control .glyphicon-chevron-right { + right: 50%; + margin-right: -10px; +} +.carousel-control .icon-prev, +.carousel-control .icon-next { + width: 20px; + height: 20px; + margin-top: -10px; + font-family: serif; + line-height: 1; +} +.carousel-control .icon-prev:before { + content: '\2039'; +} +.carousel-control .icon-next:before { + content: '\203a'; +} +.carousel-indicators { + position: absolute; + bottom: 10px; + left: 50%; + z-index: 15; + width: 60%; + padding-left: 0; + margin-left: -30%; + text-align: center; + list-style: none; +} +.carousel-indicators li { + display: inline-block; + width: 10px; + height: 10px; + margin: 1px; + text-indent: -999px; + cursor: pointer; + background-color: #000 \9; + background-color: rgba(0, 0, 0, 0); + border: 1px solid #fff; + border-radius: 10px; +} +.carousel-indicators .active { + width: 12px; + height: 12px; + margin: 0; + background-color: #fff; +} +.carousel-caption { + position: absolute; + right: 15%; + bottom: 20px; + left: 15%; + z-index: 10; + padding-top: 20px; + padding-bottom: 20px; + color: #fff; + text-align: center; + text-shadow: 0 1px 2px rgba(0, 0, 0, .6); +} +.carousel-caption .btn { + text-shadow: none; +} +@media screen and (min-width: 768px) { + .carousel-control .glyphicon-chevron-left, + .carousel-control .glyphicon-chevron-right, + .carousel-control .icon-prev, + .carousel-control .icon-next { + width: 30px; + height: 30px; + margin-top: -15px; + font-size: 30px; + } + .carousel-control .glyphicon-chevron-left, + .carousel-control .icon-prev { + margin-left: -15px; + } + .carousel-control .glyphicon-chevron-right, + .carousel-control .icon-next { + margin-right: -15px; + } + .carousel-caption { + right: 20%; + left: 20%; + padding-bottom: 30px; + } + .carousel-indicators { + bottom: 20px; + } +} +.clearfix:before, +.clearfix:after, +.dl-horizontal dd:before, +.dl-horizontal dd:after, +.container:before, +.container:after, +.container-fluid:before, +.container-fluid:after, +.row:before, +.row:after, +.form-horizontal .form-group:before, +.form-horizontal .form-group:after, +.btn-toolbar:before, +.btn-toolbar:after, +.btn-group-vertical > .btn-group:before, +.btn-group-vertical > .btn-group:after, +.nav:before, +.nav:after, +.navbar:before, +.navbar:after, +.navbar-header:before, +.navbar-header:after, +.navbar-collapse:before, +.navbar-collapse:after, +.pager:before, +.pager:after, +.panel-body:before, +.panel-body:after, +.modal-footer:before, +.modal-footer:after { + display: table; + content: " "; +} +.clearfix:after, +.dl-horizontal dd:after, +.container:after, +.container-fluid:after, +.row:after, +.form-horizontal .form-group:after, +.btn-toolbar:after, +.btn-group-vertical > .btn-group:after, +.nav:after, +.navbar:after, +.navbar-header:after, +.navbar-collapse:after, +.pager:after, +.panel-body:after, +.modal-footer:after { + clear: both; +} +.center-block { + display: block; + margin-right: auto; + margin-left: auto; +} +.pull-right { + float: right !important; +} +.pull-left { + float: left !important; +} +.hide { + display: none !important; +} +.show { + display: block !important; +} +.invisible { + visibility: hidden; +} +.text-hide { + font: 0/0 a; + color: transparent; + text-shadow: none; + background-color: transparent; + border: 0; +} +.hidden { + display: none !important; + visibility: hidden !important; +} +.affix { + position: fixed; +} +@-ms-viewport { + width: device-width; +} +.visible-xs, +.visible-sm, +.visible-md, +.visible-lg { + display: none !important; +} +.visible-xs-block, +.visible-xs-inline, +.visible-xs-inline-block, +.visible-sm-block, +.visible-sm-inline, +.visible-sm-inline-block, +.visible-md-block, +.visible-md-inline, +.visible-md-inline-block, +.visible-lg-block, +.visible-lg-inline, +.visible-lg-inline-block { + display: none !important; +} +@media (max-width: 767px) { + .visible-xs { + display: block !important; + } + table.visible-xs { + display: table; + } + tr.visible-xs { + display: table-row !important; + } + th.visible-xs, + td.visible-xs { + display: table-cell !important; + } +} +@media (max-width: 767px) { + .visible-xs-block { + display: block !important; + } +} +@media (max-width: 767px) { + .visible-xs-inline { + display: inline !important; + } +} +@media (max-width: 767px) { + .visible-xs-inline-block { + display: inline-block !important; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .visible-sm { + display: block !important; + } + table.visible-sm { + display: table; + } + tr.visible-sm { + display: table-row !important; + } + th.visible-sm, + td.visible-sm { + display: table-cell !important; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .visible-sm-block { + display: block !important; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .visible-sm-inline { + display: inline !important; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .visible-sm-inline-block { + display: inline-block !important; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .visible-md { + display: block !important; + } + table.visible-md { + display: table; + } + tr.visible-md { + display: table-row !important; + } + th.visible-md, + td.visible-md { + display: table-cell !important; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .visible-md-block { + display: block !important; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .visible-md-inline { + display: inline !important; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .visible-md-inline-block { + display: inline-block !important; + } +} +@media (min-width: 1200px) { + .visible-lg { + display: block !important; + } + table.visible-lg { + display: table; + } + tr.visible-lg { + display: table-row !important; + } + th.visible-lg, + td.visible-lg { + display: table-cell !important; + } +} +@media (min-width: 1200px) { + .visible-lg-block { + display: block !important; + } +} +@media (min-width: 1200px) { + .visible-lg-inline { + display: inline !important; + } +} +@media (min-width: 1200px) { + .visible-lg-inline-block { + display: inline-block !important; + } +} +@media (max-width: 767px) { + .hidden-xs { + display: none !important; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .hidden-sm { + display: none !important; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .hidden-md { + display: none !important; + } +} +@media (min-width: 1200px) { + .hidden-lg { + display: none !important; + } +} +.visible-print { + display: none !important; +} +@media print { + .visible-print { + display: block !important; + } + table.visible-print { + display: table; + } + tr.visible-print { + display: table-row !important; + } + th.visible-print, + td.visible-print { + display: table-cell !important; + } +} +.visible-print-block { + display: none !important; +} +@media print { + .visible-print-block { + display: block !important; + } +} +.visible-print-inline { + display: none !important; +} +@media print { + .visible-print-inline { + display: inline !important; + } +} +.visible-print-inline-block { + display: none !important; +} +@media print { + .visible-print-inline-block { + display: inline-block !important; + } +} +@media print { + .hidden-print { + display: none !important; + } +} +body { + text-rendering: optimizeLegibility; +} +.list-group-item { + padding: 8px 15px; +} +h3, +h4 { + font-weight: bold; +} +ul.with-margin li, +ol.with-margin li { + margin: 7px auto; +} +.form-control, +.btn:active, +.btn:focus, +.btn.active, +.btn-group .dropdown-toggle:active, +.btn-group.open .dropdown-toggle { + -webkit-box-shadow: none; + box-shadow: none; +} +a { + cursor: pointer; +} +a:hover, +a:focus { + text-decoration: underline; +} +.navbar a:hover, +.navbar a:focus, +.dropdown-menu a:hover, +.dropdown-menu a:focus { + border-bottom: none; +} +h4.modal-title { + font-weight: normal; +} +.modal-content { + border-radius: 3px; +} +.navbar-inverse .form-control { + color: #b8c2cb; + background-color: #475765; + border: 1px solid #475765; +} +.navbar-inverse .form-control::-webkit-input-placeholder { + color: #b8c2cb; +} +.navbar-inverse .form-control::-moz-placeholder { + color: #b8c2cb; +} +.navbar-inverse .form-control:-ms-input-placeholder { + color: #b8c2cb; +} +.navbar-inverse .form-control:focus { + -webkit-box-shadow: none; + box-shadow: none; +} +.navbar-search-icon { + color: #b8c2cb; +} diff --git a/xhiveframework/public/css/fonts/fontawesome/FontAwesome.otf b/xhiveframework/public/css/fonts/fontawesome/FontAwesome.otf new file mode 100644 index 0000000..401ec0f Binary files /dev/null and b/xhiveframework/public/css/fonts/fontawesome/FontAwesome.otf differ diff --git a/xhiveframework/public/css/fonts/fontawesome/LICENSE b/xhiveframework/public/css/fonts/fontawesome/LICENSE new file mode 100644 index 0000000..3042281 --- /dev/null +++ b/xhiveframework/public/css/fonts/fontawesome/LICENSE @@ -0,0 +1,4 @@ +The Font Awesome font is licensed under the SIL OFL 1.1: +http://scripts.sil.org/OFL +Font Awesome CSS, LESS, and Sass files are licensed under the MIT License: +https://opensource.org/licenses/mit-license.html diff --git a/xhiveframework/public/css/fonts/fontawesome/font-awesome.min.css b/xhiveframework/public/css/fonts/fontawesome/font-awesome.min.css new file mode 100644 index 0000000..cdd76eb --- /dev/null +++ b/xhiveframework/public/css/fonts/fontawesome/font-awesome.min.css @@ -0,0 +1,4 @@ +/*! + * Font Awesome 4.7.0 by @davegandy - http://fontawesome.io - @fontawesome + * License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License) + */@font-face{font-family:'FontAwesome';src:url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0');src:url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'),url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0') format('woff2'),url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0') format('woff'),url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0') format('truetype'),url('/assets/xhiveframework/css/fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');font-weight:normal;font-style:normal}.fa{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.fa-lg{font-size:1.33333333em;line-height:.75em;vertical-align:-15%}.fa-2x{font-size:2em}.fa-3x{font-size:3em}.fa-4x{font-size:4em}.fa-5x{font-size:5em}.fa-fw{width:1.28571429em;text-align:center}.fa-ul{padding-left:0;margin-left:2.14285714em;list-style-type:none}.fa-ul>li{position:relative}.fa-li{position:absolute;left:-2.14285714em;width:2.14285714em;top:.14285714em;text-align:center}.fa-li.fa-lg{left:-1.85714286em}.fa-border{padding:.2em .25em .15em;border:solid .08em #eee;border-radius:.1em}.fa-pull-left{float:left}.fa-pull-right{float:right}.fa.fa-pull-left{margin-right:.3em}.fa.fa-pull-right{margin-left:.3em}.pull-right{float:right}.pull-left{float:left}.fa.pull-left{margin-right:.3em}.fa.pull-right{margin-left:.3em}.fa-spin{-webkit-animation:fa-spin 2s infinite linear;animation:fa-spin 2s infinite linear}.fa-pulse{-webkit-animation:fa-spin 1s infinite steps(8);animation:fa-spin 1s infinite steps(8)}@-webkit-keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}100%{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}@keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}100%{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}.fa-rotate-90{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=1)";-webkit-transform:rotate(90deg);-ms-transform:rotate(90deg);transform:rotate(90deg)}.fa-rotate-180{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=2)";-webkit-transform:rotate(180deg);-ms-transform:rotate(180deg);transform:rotate(180deg)}.fa-rotate-270{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=3)";-webkit-transform:rotate(270deg);-ms-transform:rotate(270deg);transform:rotate(270deg)}.fa-flip-horizontal{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1)";-webkit-transform:scale(-1, 1);-ms-transform:scale(-1, 1);transform:scale(-1, 1)}.fa-flip-vertical{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=2, mirror=1)";-webkit-transform:scale(1, -1);-ms-transform:scale(1, -1);transform:scale(1, -1)}:root .fa-rotate-90,:root .fa-rotate-180,:root .fa-rotate-270,:root .fa-flip-horizontal,:root .fa-flip-vertical{filter:none}.fa-stack{position:relative;display:inline-block;width:2em;height:2em;line-height:2em;vertical-align:middle}.fa-stack-1x,.fa-stack-2x{position:absolute;left:0;width:100%;text-align:center}.fa-stack-1x{line-height:inherit}.fa-stack-2x{font-size:2em}.fa-inverse{color:#fff}.fa-glass:before{content:"\f000"}.fa-music:before{content:"\f001"}.fa-search:before{content:"\f002"}.fa-envelope-o:before{content:"\f003"}.fa-heart:before{content:"\f004"}.fa-star:before{content:"\f005"}.fa-star-o:before{content:"\f006"}.fa-user:before{content:"\f007"}.fa-film:before{content:"\f008"}.fa-th-large:before{content:"\f009"}.fa-th:before{content:"\f00a"}.fa-th-list:before{content:"\f00b"}.fa-check:before{content:"\f00c"}.fa-remove:before,.fa-close:before,.fa-times:before{content:"\f00d"}.fa-search-plus:before{content:"\f00e"}.fa-search-minus:before{content:"\f010"}.fa-power-off:before{content:"\f011"}.fa-signal:before{content:"\f012"}.fa-gear:before,.fa-cog:before{content:"\f013"}.fa-trash-o:before{content:"\f014"}.fa-home:before{content:"\f015"}.fa-file-o:before{content:"\f016"}.fa-clock-o:before{content:"\f017"}.fa-road:before{content:"\f018"}.fa-download:before{content:"\f019"}.fa-arrow-circle-o-down:before{content:"\f01a"}.fa-arrow-circle-o-up:before{content:"\f01b"}.fa-inbox:before{content:"\f01c"}.fa-play-circle-o:before{content:"\f01d"}.fa-rotate-right:before,.fa-repeat:before{content:"\f01e"}.fa-refresh:before{content:"\f021"}.fa-list-alt:before{content:"\f022"}.fa-lock:before{content:"\f023"}.fa-flag:before{content:"\f024"}.fa-headphones:before{content:"\f025"}.fa-volume-off:before{content:"\f026"}.fa-volume-down:before{content:"\f027"}.fa-volume-up:before{content:"\f028"}.fa-qrcode:before{content:"\f029"}.fa-barcode:before{content:"\f02a"}.fa-tag:before{content:"\f02b"}.fa-tags:before{content:"\f02c"}.fa-book:before{content:"\f02d"}.fa-bookmark:before{content:"\f02e"}.fa-print:before{content:"\f02f"}.fa-camera:before{content:"\f030"}.fa-font:before{content:"\f031"}.fa-bold:before{content:"\f032"}.fa-italic:before{content:"\f033"}.fa-text-height:before{content:"\f034"}.fa-text-width:before{content:"\f035"}.fa-align-left:before{content:"\f036"}.fa-align-center:before{content:"\f037"}.fa-align-right:before{content:"\f038"}.fa-align-justify:before{content:"\f039"}.fa-list:before{content:"\f03a"}.fa-dedent:before,.fa-outdent:before{content:"\f03b"}.fa-indent:before{content:"\f03c"}.fa-video-camera:before{content:"\f03d"}.fa-photo:before,.fa-image:before,.fa-picture-o:before{content:"\f03e"}.fa-pencil:before{content:"\f040"}.fa-map-marker:before{content:"\f041"}.fa-adjust:before{content:"\f042"}.fa-tint:before{content:"\f043"}.fa-edit:before,.fa-pencil-square-o:before{content:"\f044"}.fa-share-square-o:before{content:"\f045"}.fa-check-square-o:before{content:"\f046"}.fa-arrows:before{content:"\f047"}.fa-step-backward:before{content:"\f048"}.fa-fast-backward:before{content:"\f049"}.fa-backward:before{content:"\f04a"}.fa-play:before{content:"\f04b"}.fa-pause:before{content:"\f04c"}.fa-stop:before{content:"\f04d"}.fa-forward:before{content:"\f04e"}.fa-fast-forward:before{content:"\f050"}.fa-step-forward:before{content:"\f051"}.fa-eject:before{content:"\f052"}.fa-chevron-left:before{content:"\f053"}.fa-chevron-right:before{content:"\f054"}.fa-plus-circle:before{content:"\f055"}.fa-minus-circle:before{content:"\f056"}.fa-times-circle:before{content:"\f057"}.fa-check-circle:before{content:"\f058"}.fa-question-circle:before{content:"\f059"}.fa-info-circle:before{content:"\f05a"}.fa-crosshairs:before{content:"\f05b"}.fa-times-circle-o:before{content:"\f05c"}.fa-check-circle-o:before{content:"\f05d"}.fa-ban:before{content:"\f05e"}.fa-arrow-left:before{content:"\f060"}.fa-arrow-right:before{content:"\f061"}.fa-arrow-up:before{content:"\f062"}.fa-arrow-down:before{content:"\f063"}.fa-mail-forward:before,.fa-share:before{content:"\f064"}.fa-expand:before{content:"\f065"}.fa-compress:before{content:"\f066"}.fa-plus:before{content:"\f067"}.fa-minus:before{content:"\f068"}.fa-asterisk:before{content:"\f069"}.fa-exclamation-circle:before{content:"\f06a"}.fa-gift:before{content:"\f06b"}.fa-leaf:before{content:"\f06c"}.fa-fire:before{content:"\f06d"}.fa-eye:before{content:"\f06e"}.fa-eye-slash:before{content:"\f070"}.fa-warning:before,.fa-exclamation-triangle:before{content:"\f071"}.fa-plane:before{content:"\f072"}.fa-calendar:before{content:"\f073"}.fa-random:before{content:"\f074"}.fa-comment:before{content:"\f075"}.fa-magnet:before{content:"\f076"}.fa-chevron-up:before{content:"\f077"}.fa-chevron-down:before{content:"\f078"}.fa-retweet:before{content:"\f079"}.fa-shopping-cart:before{content:"\f07a"}.fa-folder:before{content:"\f07b"}.fa-folder-open:before{content:"\f07c"}.fa-arrows-v:before{content:"\f07d"}.fa-arrows-h:before{content:"\f07e"}.fa-bar-chart-o:before,.fa-bar-chart:before{content:"\f080"}.fa-twitter-square:before{content:"\f081"}.fa-facebook-square:before{content:"\f082"}.fa-camera-retro:before{content:"\f083"}.fa-key:before{content:"\f084"}.fa-gears:before,.fa-cogs:before{content:"\f085"}.fa-comments:before{content:"\f086"}.fa-thumbs-o-up:before{content:"\f087"}.fa-thumbs-o-down:before{content:"\f088"}.fa-star-half:before{content:"\f089"}.fa-heart-o:before{content:"\f08a"}.fa-sign-out:before{content:"\f08b"}.fa-linkedin-square:before{content:"\f08c"}.fa-thumb-tack:before{content:"\f08d"}.fa-external-link:before{content:"\f08e"}.fa-sign-in:before{content:"\f090"}.fa-trophy:before{content:"\f091"}.fa-github-square:before{content:"\f092"}.fa-upload:before{content:"\f093"}.fa-lemon-o:before{content:"\f094"}.fa-phone:before{content:"\f095"}.fa-square-o:before{content:"\f096"}.fa-bookmark-o:before{content:"\f097"}.fa-phone-square:before{content:"\f098"}.fa-twitter:before{content:"\f099"}.fa-facebook-f:before,.fa-facebook:before{content:"\f09a"}.fa-github:before{content:"\f09b"}.fa-unlock:before{content:"\f09c"}.fa-credit-card:before{content:"\f09d"}.fa-feed:before,.fa-rss:before{content:"\f09e"}.fa-hdd-o:before{content:"\f0a0"}.fa-bullhorn:before{content:"\f0a1"}.fa-bell:before{content:"\f0f3"}.fa-certificate:before{content:"\f0a3"}.fa-hand-o-right:before{content:"\f0a4"}.fa-hand-o-left:before{content:"\f0a5"}.fa-hand-o-up:before{content:"\f0a6"}.fa-hand-o-down:before{content:"\f0a7"}.fa-arrow-circle-left:before{content:"\f0a8"}.fa-arrow-circle-right:before{content:"\f0a9"}.fa-arrow-circle-up:before{content:"\f0aa"}.fa-arrow-circle-down:before{content:"\f0ab"}.fa-globe:before{content:"\f0ac"}.fa-wrench:before{content:"\f0ad"}.fa-tasks:before{content:"\f0ae"}.fa-filter:before{content:"\f0b0"}.fa-briefcase:before{content:"\f0b1"}.fa-arrows-alt:before{content:"\f0b2"}.fa-group:before,.fa-users:before{content:"\f0c0"}.fa-chain:before,.fa-link:before{content:"\f0c1"}.fa-cloud:before{content:"\f0c2"}.fa-flask:before{content:"\f0c3"}.fa-cut:before,.fa-scissors:before{content:"\f0c4"}.fa-copy:before,.fa-files-o:before{content:"\f0c5"}.fa-paperclip:before{content:"\f0c6"}.fa-save:before,.fa-floppy-o:before{content:"\f0c7"}.fa-square:before{content:"\f0c8"}.fa-navicon:before,.fa-reorder:before,.fa-bars:before{content:"\f0c9"}.fa-list-ul:before{content:"\f0ca"}.fa-list-ol:before{content:"\f0cb"}.fa-strikethrough:before{content:"\f0cc"}.fa-underline:before{content:"\f0cd"}.fa-table:before{content:"\f0ce"}.fa-magic:before{content:"\f0d0"}.fa-truck:before{content:"\f0d1"}.fa-pinterest:before{content:"\f0d2"}.fa-pinterest-square:before{content:"\f0d3"}.fa-google-plus-square:before{content:"\f0d4"}.fa-google-plus:before{content:"\f0d5"}.fa-money:before{content:"\f0d6"}.fa-caret-down:before{content:"\f0d7"}.fa-caret-up:before{content:"\f0d8"}.fa-caret-left:before{content:"\f0d9"}.fa-caret-right:before{content:"\f0da"}.fa-columns:before{content:"\f0db"}.fa-unsorted:before,.fa-sort:before{content:"\f0dc"}.fa-sort-down:before,.fa-sort-desc:before{content:"\f0dd"}.fa-sort-up:before,.fa-sort-asc:before{content:"\f0de"}.fa-envelope:before{content:"\f0e0"}.fa-linkedin:before{content:"\f0e1"}.fa-rotate-left:before,.fa-undo:before{content:"\f0e2"}.fa-legal:before,.fa-gavel:before{content:"\f0e3"}.fa-dashboard:before,.fa-tachometer:before{content:"\f0e4"}.fa-comment-o:before{content:"\f0e5"}.fa-comments-o:before{content:"\f0e6"}.fa-flash:before,.fa-bolt:before{content:"\f0e7"}.fa-sitemap:before{content:"\f0e8"}.fa-umbrella:before{content:"\f0e9"}.fa-paste:before,.fa-clipboard:before{content:"\f0ea"}.fa-lightbulb-o:before{content:"\f0eb"}.fa-exchange:before{content:"\f0ec"}.fa-cloud-download:before{content:"\f0ed"}.fa-cloud-upload:before{content:"\f0ee"}.fa-user-md:before{content:"\f0f0"}.fa-stethoscope:before{content:"\f0f1"}.fa-suitcase:before{content:"\f0f2"}.fa-bell-o:before{content:"\f0a2"}.fa-coffee:before{content:"\f0f4"}.fa-cutlery:before{content:"\f0f5"}.fa-file-text-o:before{content:"\f0f6"}.fa-building-o:before{content:"\f0f7"}.fa-hospital-o:before{content:"\f0f8"}.fa-ambulance:before{content:"\f0f9"}.fa-medkit:before{content:"\f0fa"}.fa-fighter-jet:before{content:"\f0fb"}.fa-beer:before{content:"\f0fc"}.fa-h-square:before{content:"\f0fd"}.fa-plus-square:before{content:"\f0fe"}.fa-angle-double-left:before{content:"\f100"}.fa-angle-double-right:before{content:"\f101"}.fa-angle-double-up:before{content:"\f102"}.fa-angle-double-down:before{content:"\f103"}.fa-angle-left:before{content:"\f104"}.fa-angle-right:before{content:"\f105"}.fa-angle-up:before{content:"\f106"}.fa-angle-down:before{content:"\f107"}.fa-desktop:before{content:"\f108"}.fa-laptop:before{content:"\f109"}.fa-tablet:before{content:"\f10a"}.fa-mobile-phone:before,.fa-mobile:before{content:"\f10b"}.fa-circle-o:before{content:"\f10c"}.fa-quote-left:before{content:"\f10d"}.fa-quote-right:before{content:"\f10e"}.fa-spinner:before{content:"\f110"}.fa-circle:before{content:"\f111"}.fa-mail-reply:before,.fa-reply:before{content:"\f112"}.fa-github-alt:before{content:"\f113"}.fa-folder-o:before{content:"\f114"}.fa-folder-open-o:before{content:"\f115"}.fa-smile-o:before{content:"\f118"}.fa-frown-o:before{content:"\f119"}.fa-meh-o:before{content:"\f11a"}.fa-gamepad:before{content:"\f11b"}.fa-keyboard-o:before{content:"\f11c"}.fa-flag-o:before{content:"\f11d"}.fa-flag-checkered:before{content:"\f11e"}.fa-terminal:before{content:"\f120"}.fa-code:before{content:"\f121"}.fa-mail-reply-all:before,.fa-reply-all:before{content:"\f122"}.fa-star-half-empty:before,.fa-star-half-full:before,.fa-star-half-o:before{content:"\f123"}.fa-location-arrow:before{content:"\f124"}.fa-crop:before{content:"\f125"}.fa-code-fork:before{content:"\f126"}.fa-unlink:before,.fa-chain-broken:before{content:"\f127"}.fa-question:before{content:"\f128"}.fa-info:before{content:"\f129"}.fa-exclamation:before{content:"\f12a"}.fa-superscript:before{content:"\f12b"}.fa-subscript:before{content:"\f12c"}.fa-eraser:before{content:"\f12d"}.fa-puzzle-piece:before{content:"\f12e"}.fa-microphone:before{content:"\f130"}.fa-microphone-slash:before{content:"\f131"}.fa-shield:before{content:"\f132"}.fa-calendar-o:before{content:"\f133"}.fa-fire-extinguisher:before{content:"\f134"}.fa-rocket:before{content:"\f135"}.fa-maxcdn:before{content:"\f136"}.fa-chevron-circle-left:before{content:"\f137"}.fa-chevron-circle-right:before{content:"\f138"}.fa-chevron-circle-up:before{content:"\f139"}.fa-chevron-circle-down:before{content:"\f13a"}.fa-html5:before{content:"\f13b"}.fa-css3:before{content:"\f13c"}.fa-anchor:before{content:"\f13d"}.fa-unlock-alt:before{content:"\f13e"}.fa-bullseye:before{content:"\f140"}.fa-ellipsis-h:before{content:"\f141"}.fa-ellipsis-v:before{content:"\f142"}.fa-rss-square:before{content:"\f143"}.fa-play-circle:before{content:"\f144"}.fa-ticket:before{content:"\f145"}.fa-minus-square:before{content:"\f146"}.fa-minus-square-o:before{content:"\f147"}.fa-level-up:before{content:"\f148"}.fa-level-down:before{content:"\f149"}.fa-check-square:before{content:"\f14a"}.fa-pencil-square:before{content:"\f14b"}.fa-external-link-square:before{content:"\f14c"}.fa-share-square:before{content:"\f14d"}.fa-compass:before{content:"\f14e"}.fa-toggle-down:before,.fa-caret-square-o-down:before{content:"\f150"}.fa-toggle-up:before,.fa-caret-square-o-up:before{content:"\f151"}.fa-toggle-right:before,.fa-caret-square-o-right:before{content:"\f152"}.fa-euro:before,.fa-eur:before{content:"\f153"}.fa-gbp:before{content:"\f154"}.fa-dollar:before,.fa-usd:before{content:"\f155"}.fa-rupee:before,.fa-inr:before{content:"\f156"}.fa-cny:before,.fa-rmb:before,.fa-yen:before,.fa-jpy:before{content:"\f157"}.fa-ruble:before,.fa-rouble:before,.fa-rub:before{content:"\f158"}.fa-won:before,.fa-krw:before{content:"\f159"}.fa-bitcoin:before,.fa-btc:before{content:"\f15a"}.fa-file:before{content:"\f15b"}.fa-file-text:before{content:"\f15c"}.fa-sort-alpha-asc:before{content:"\f15d"}.fa-sort-alpha-desc:before{content:"\f15e"}.fa-sort-amount-asc:before{content:"\f160"}.fa-sort-amount-desc:before{content:"\f161"}.fa-sort-numeric-asc:before{content:"\f162"}.fa-sort-numeric-desc:before{content:"\f163"}.fa-thumbs-up:before{content:"\f164"}.fa-thumbs-down:before{content:"\f165"}.fa-youtube-square:before{content:"\f166"}.fa-youtube:before{content:"\f167"}.fa-xing:before{content:"\f168"}.fa-xing-square:before{content:"\f169"}.fa-youtube-play:before{content:"\f16a"}.fa-dropbox:before{content:"\f16b"}.fa-stack-overflow:before{content:"\f16c"}.fa-instagram:before{content:"\f16d"}.fa-flickr:before{content:"\f16e"}.fa-adn:before{content:"\f170"}.fa-bitbucket:before{content:"\f171"}.fa-bitbucket-square:before{content:"\f172"}.fa-tumblr:before{content:"\f173"}.fa-tumblr-square:before{content:"\f174"}.fa-long-arrow-down:before{content:"\f175"}.fa-long-arrow-up:before{content:"\f176"}.fa-long-arrow-left:before{content:"\f177"}.fa-long-arrow-right:before{content:"\f178"}.fa-apple:before{content:"\f179"}.fa-windows:before{content:"\f17a"}.fa-android:before{content:"\f17b"}.fa-linux:before{content:"\f17c"}.fa-dribbble:before{content:"\f17d"}.fa-skype:before{content:"\f17e"}.fa-foursquare:before{content:"\f180"}.fa-trello:before{content:"\f181"}.fa-female:before{content:"\f182"}.fa-male:before{content:"\f183"}.fa-gittip:before,.fa-gratipay:before{content:"\f184"}.fa-sun-o:before{content:"\f185"}.fa-moon-o:before{content:"\f186"}.fa-archive:before{content:"\f187"}.fa-bug:before{content:"\f188"}.fa-vk:before{content:"\f189"}.fa-weibo:before{content:"\f18a"}.fa-renren:before{content:"\f18b"}.fa-pagelines:before{content:"\f18c"}.fa-stack-exchange:before{content:"\f18d"}.fa-arrow-circle-o-right:before{content:"\f18e"}.fa-arrow-circle-o-left:before{content:"\f190"}.fa-toggle-left:before,.fa-caret-square-o-left:before{content:"\f191"}.fa-dot-circle-o:before{content:"\f192"}.fa-wheelchair:before{content:"\f193"}.fa-vimeo-square:before{content:"\f194"}.fa-turkish-lira:before,.fa-try:before{content:"\f195"}.fa-plus-square-o:before{content:"\f196"}.fa-space-shuttle:before{content:"\f197"}.fa-slack:before{content:"\f198"}.fa-envelope-square:before{content:"\f199"}.fa-wordpress:before{content:"\f19a"}.fa-openid:before{content:"\f19b"}.fa-institution:before,.fa-bank:before,.fa-university:before{content:"\f19c"}.fa-mortar-board:before,.fa-graduation-cap:before{content:"\f19d"}.fa-yahoo:before{content:"\f19e"}.fa-google:before{content:"\f1a0"}.fa-reddit:before{content:"\f1a1"}.fa-reddit-square:before{content:"\f1a2"}.fa-stumbleupon-circle:before{content:"\f1a3"}.fa-stumbleupon:before{content:"\f1a4"}.fa-delicious:before{content:"\f1a5"}.fa-digg:before{content:"\f1a6"}.fa-pied-piper-pp:before{content:"\f1a7"}.fa-pied-piper-alt:before{content:"\f1a8"}.fa-drupal:before{content:"\f1a9"}.fa-joomla:before{content:"\f1aa"}.fa-language:before{content:"\f1ab"}.fa-fax:before{content:"\f1ac"}.fa-building:before{content:"\f1ad"}.fa-child:before{content:"\f1ae"}.fa-paw:before{content:"\f1b0"}.fa-spoon:before{content:"\f1b1"}.fa-cube:before{content:"\f1b2"}.fa-cubes:before{content:"\f1b3"}.fa-behance:before{content:"\f1b4"}.fa-behance-square:before{content:"\f1b5"}.fa-steam:before{content:"\f1b6"}.fa-steam-square:before{content:"\f1b7"}.fa-recycle:before{content:"\f1b8"}.fa-automobile:before,.fa-car:before{content:"\f1b9"}.fa-cab:before,.fa-taxi:before{content:"\f1ba"}.fa-tree:before{content:"\f1bb"}.fa-spotify:before{content:"\f1bc"}.fa-deviantart:before{content:"\f1bd"}.fa-soundcloud:before{content:"\f1be"}.fa-database:before{content:"\f1c0"}.fa-file-pdf-o:before{content:"\f1c1"}.fa-file-word-o:before{content:"\f1c2"}.fa-file-excel-o:before{content:"\f1c3"}.fa-file-powerpoint-o:before{content:"\f1c4"}.fa-file-photo-o:before,.fa-file-picture-o:before,.fa-file-image-o:before{content:"\f1c5"}.fa-file-zip-o:before,.fa-file-archive-o:before{content:"\f1c6"}.fa-file-sound-o:before,.fa-file-audio-o:before{content:"\f1c7"}.fa-file-movie-o:before,.fa-file-video-o:before{content:"\f1c8"}.fa-file-code-o:before{content:"\f1c9"}.fa-vine:before{content:"\f1ca"}.fa-codepen:before{content:"\f1cb"}.fa-jsfiddle:before{content:"\f1cc"}.fa-life-bouy:before,.fa-life-buoy:before,.fa-life-saver:before,.fa-support:before,.fa-life-ring:before{content:"\f1cd"}.fa-circle-o-notch:before{content:"\f1ce"}.fa-ra:before,.fa-resistance:before,.fa-rebel:before{content:"\f1d0"}.fa-ge:before,.fa-empire:before{content:"\f1d1"}.fa-git-square:before{content:"\f1d2"}.fa-git:before{content:"\f1d3"}.fa-y-combinator-square:before,.fa-yc-square:before,.fa-hacker-news:before{content:"\f1d4"}.fa-tencent-weibo:before{content:"\f1d5"}.fa-qq:before{content:"\f1d6"}.fa-wechat:before,.fa-weixin:before{content:"\f1d7"}.fa-send:before,.fa-paper-plane:before{content:"\f1d8"}.fa-send-o:before,.fa-paper-plane-o:before{content:"\f1d9"}.fa-history:before{content:"\f1da"}.fa-circle-thin:before{content:"\f1db"}.fa-header:before{content:"\f1dc"}.fa-paragraph:before{content:"\f1dd"}.fa-sliders:before{content:"\f1de"}.fa-share-alt:before{content:"\f1e0"}.fa-share-alt-square:before{content:"\f1e1"}.fa-bomb:before{content:"\f1e2"}.fa-soccer-ball-o:before,.fa-futbol-o:before{content:"\f1e3"}.fa-tty:before{content:"\f1e4"}.fa-binoculars:before{content:"\f1e5"}.fa-plug:before{content:"\f1e6"}.fa-slideshare:before{content:"\f1e7"}.fa-twitch:before{content:"\f1e8"}.fa-yelp:before{content:"\f1e9"}.fa-newspaper-o:before{content:"\f1ea"}.fa-wifi:before{content:"\f1eb"}.fa-calculator:before{content:"\f1ec"}.fa-paypal:before{content:"\f1ed"}.fa-google-wallet:before{content:"\f1ee"}.fa-cc-visa:before{content:"\f1f0"}.fa-cc-mastercard:before{content:"\f1f1"}.fa-cc-discover:before{content:"\f1f2"}.fa-cc-amex:before{content:"\f1f3"}.fa-cc-paypal:before{content:"\f1f4"}.fa-cc-stripe:before{content:"\f1f5"}.fa-bell-slash:before{content:"\f1f6"}.fa-bell-slash-o:before{content:"\f1f7"}.fa-trash:before{content:"\f1f8"}.fa-copyright:before{content:"\f1f9"}.fa-at:before{content:"\f1fa"}.fa-eyedropper:before{content:"\f1fb"}.fa-paint-brush:before{content:"\f1fc"}.fa-birthday-cake:before{content:"\f1fd"}.fa-area-chart:before{content:"\f1fe"}.fa-pie-chart:before{content:"\f200"}.fa-line-chart:before{content:"\f201"}.fa-lastfm:before{content:"\f202"}.fa-lastfm-square:before{content:"\f203"}.fa-toggle-off:before{content:"\f204"}.fa-toggle-on:before{content:"\f205"}.fa-bicycle:before{content:"\f206"}.fa-bus:before{content:"\f207"}.fa-ioxhost:before{content:"\f208"}.fa-angellist:before{content:"\f209"}.fa-cc:before{content:"\f20a"}.fa-shekel:before,.fa-sheqel:before,.fa-ils:before{content:"\f20b"}.fa-meanpath:before{content:"\f20c"}.fa-buysellads:before{content:"\f20d"}.fa-connectdevelop:before{content:"\f20e"}.fa-dashcube:before{content:"\f210"}.fa-forumbee:before{content:"\f211"}.fa-leanpub:before{content:"\f212"}.fa-sellsy:before{content:"\f213"}.fa-shirtsinbulk:before{content:"\f214"}.fa-simplybuilt:before{content:"\f215"}.fa-skyatlas:before{content:"\f216"}.fa-cart-plus:before{content:"\f217"}.fa-cart-arrow-down:before{content:"\f218"}.fa-diamond:before{content:"\f219"}.fa-ship:before{content:"\f21a"}.fa-user-secret:before{content:"\f21b"}.fa-motorcycle:before{content:"\f21c"}.fa-street-view:before{content:"\f21d"}.fa-heartbeat:before{content:"\f21e"}.fa-venus:before{content:"\f221"}.fa-mars:before{content:"\f222"}.fa-mercury:before{content:"\f223"}.fa-intersex:before,.fa-transgender:before{content:"\f224"}.fa-transgender-alt:before{content:"\f225"}.fa-venus-double:before{content:"\f226"}.fa-mars-double:before{content:"\f227"}.fa-venus-mars:before{content:"\f228"}.fa-mars-stroke:before{content:"\f229"}.fa-mars-stroke-v:before{content:"\f22a"}.fa-mars-stroke-h:before{content:"\f22b"}.fa-neuter:before{content:"\f22c"}.fa-genderless:before{content:"\f22d"}.fa-facebook-official:before{content:"\f230"}.fa-pinterest-p:before{content:"\f231"}.fa-whatsapp:before{content:"\f232"}.fa-server:before{content:"\f233"}.fa-user-plus:before{content:"\f234"}.fa-user-times:before{content:"\f235"}.fa-hotel:before,.fa-bed:before{content:"\f236"}.fa-viacoin:before{content:"\f237"}.fa-train:before{content:"\f238"}.fa-subway:before{content:"\f239"}.fa-medium:before{content:"\f23a"}.fa-yc:before,.fa-y-combinator:before{content:"\f23b"}.fa-optin-monster:before{content:"\f23c"}.fa-opencart:before{content:"\f23d"}.fa-expeditedssl:before{content:"\f23e"}.fa-battery-4:before,.fa-battery:before,.fa-battery-full:before{content:"\f240"}.fa-battery-3:before,.fa-battery-three-quarters:before{content:"\f241"}.fa-battery-2:before,.fa-battery-half:before{content:"\f242"}.fa-battery-1:before,.fa-battery-quarter:before{content:"\f243"}.fa-battery-0:before,.fa-battery-empty:before{content:"\f244"}.fa-mouse-pointer:before{content:"\f245"}.fa-i-cursor:before{content:"\f246"}.fa-object-group:before{content:"\f247"}.fa-object-ungroup:before{content:"\f248"}.fa-sticky-note:before{content:"\f249"}.fa-sticky-note-o:before{content:"\f24a"}.fa-cc-jcb:before{content:"\f24b"}.fa-cc-diners-club:before{content:"\f24c"}.fa-clone:before{content:"\f24d"}.fa-balance-scale:before{content:"\f24e"}.fa-hourglass-o:before{content:"\f250"}.fa-hourglass-1:before,.fa-hourglass-start:before{content:"\f251"}.fa-hourglass-2:before,.fa-hourglass-half:before{content:"\f252"}.fa-hourglass-3:before,.fa-hourglass-end:before{content:"\f253"}.fa-hourglass:before{content:"\f254"}.fa-hand-grab-o:before,.fa-hand-rock-o:before{content:"\f255"}.fa-hand-stop-o:before,.fa-hand-paper-o:before{content:"\f256"}.fa-hand-scissors-o:before{content:"\f257"}.fa-hand-lizard-o:before{content:"\f258"}.fa-hand-spock-o:before{content:"\f259"}.fa-hand-pointer-o:before{content:"\f25a"}.fa-hand-peace-o:before{content:"\f25b"}.fa-trademark:before{content:"\f25c"}.fa-registered:before{content:"\f25d"}.fa-creative-commons:before{content:"\f25e"}.fa-gg:before{content:"\f260"}.fa-gg-circle:before{content:"\f261"}.fa-tripadvisor:before{content:"\f262"}.fa-odnoklassniki:before{content:"\f263"}.fa-odnoklassniki-square:before{content:"\f264"}.fa-get-pocket:before{content:"\f265"}.fa-wikipedia-w:before{content:"\f266"}.fa-safari:before{content:"\f267"}.fa-chrome:before{content:"\f268"}.fa-firefox:before{content:"\f269"}.fa-opera:before{content:"\f26a"}.fa-internet-explorer:before{content:"\f26b"}.fa-tv:before,.fa-television:before{content:"\f26c"}.fa-contao:before{content:"\f26d"}.fa-500px:before{content:"\f26e"}.fa-amazon:before{content:"\f270"}.fa-calendar-plus-o:before{content:"\f271"}.fa-calendar-minus-o:before{content:"\f272"}.fa-calendar-times-o:before{content:"\f273"}.fa-calendar-check-o:before{content:"\f274"}.fa-industry:before{content:"\f275"}.fa-map-pin:before{content:"\f276"}.fa-map-signs:before{content:"\f277"}.fa-map-o:before{content:"\f278"}.fa-map:before{content:"\f279"}.fa-commenting:before{content:"\f27a"}.fa-commenting-o:before{content:"\f27b"}.fa-houzz:before{content:"\f27c"}.fa-vimeo:before{content:"\f27d"}.fa-black-tie:before{content:"\f27e"}.fa-fonticons:before{content:"\f280"}.fa-reddit-alien:before{content:"\f281"}.fa-edge:before{content:"\f282"}.fa-credit-card-alt:before{content:"\f283"}.fa-codiepie:before{content:"\f284"}.fa-modx:before{content:"\f285"}.fa-fort-awesome:before{content:"\f286"}.fa-usb:before{content:"\f287"}.fa-product-hunt:before{content:"\f288"}.fa-mixcloud:before{content:"\f289"}.fa-scribd:before{content:"\f28a"}.fa-pause-circle:before{content:"\f28b"}.fa-pause-circle-o:before{content:"\f28c"}.fa-stop-circle:before{content:"\f28d"}.fa-stop-circle-o:before{content:"\f28e"}.fa-shopping-bag:before{content:"\f290"}.fa-shopping-basket:before{content:"\f291"}.fa-hashtag:before{content:"\f292"}.fa-bluetooth:before{content:"\f293"}.fa-bluetooth-b:before{content:"\f294"}.fa-percent:before{content:"\f295"}.fa-gitlab:before{content:"\f296"}.fa-wpbeginner:before{content:"\f297"}.fa-wpforms:before{content:"\f298"}.fa-envira:before{content:"\f299"}.fa-universal-access:before{content:"\f29a"}.fa-wheelchair-alt:before{content:"\f29b"}.fa-question-circle-o:before{content:"\f29c"}.fa-blind:before{content:"\f29d"}.fa-audio-description:before{content:"\f29e"}.fa-volume-control-phone:before{content:"\f2a0"}.fa-braille:before{content:"\f2a1"}.fa-assistive-listening-systems:before{content:"\f2a2"}.fa-asl-interpreting:before,.fa-american-sign-language-interpreting:before{content:"\f2a3"}.fa-deafness:before,.fa-hard-of-hearing:before,.fa-deaf:before{content:"\f2a4"}.fa-glide:before{content:"\f2a5"}.fa-glide-g:before{content:"\f2a6"}.fa-signing:before,.fa-sign-language:before{content:"\f2a7"}.fa-low-vision:before{content:"\f2a8"}.fa-viadeo:before{content:"\f2a9"}.fa-viadeo-square:before{content:"\f2aa"}.fa-snapchat:before{content:"\f2ab"}.fa-snapchat-ghost:before{content:"\f2ac"}.fa-snapchat-square:before{content:"\f2ad"}.fa-pied-piper:before{content:"\f2ae"}.fa-first-order:before{content:"\f2b0"}.fa-yoast:before{content:"\f2b1"}.fa-themeisle:before{content:"\f2b2"}.fa-google-plus-circle:before,.fa-google-plus-official:before{content:"\f2b3"}.fa-fa:before,.fa-font-awesome:before{content:"\f2b4"}.fa-handshake-o:before{content:"\f2b5"}.fa-envelope-open:before{content:"\f2b6"}.fa-envelope-open-o:before{content:"\f2b7"}.fa-linode:before{content:"\f2b8"}.fa-address-book:before{content:"\f2b9"}.fa-address-book-o:before{content:"\f2ba"}.fa-vcard:before,.fa-address-card:before{content:"\f2bb"}.fa-vcard-o:before,.fa-address-card-o:before{content:"\f2bc"}.fa-user-circle:before{content:"\f2bd"}.fa-user-circle-o:before{content:"\f2be"}.fa-user-o:before{content:"\f2c0"}.fa-id-badge:before{content:"\f2c1"}.fa-drivers-license:before,.fa-id-card:before{content:"\f2c2"}.fa-drivers-license-o:before,.fa-id-card-o:before{content:"\f2c3"}.fa-quora:before{content:"\f2c4"}.fa-free-code-camp:before{content:"\f2c5"}.fa-telegram:before{content:"\f2c6"}.fa-thermometer-4:before,.fa-thermometer:before,.fa-thermometer-full:before{content:"\f2c7"}.fa-thermometer-3:before,.fa-thermometer-three-quarters:before{content:"\f2c8"}.fa-thermometer-2:before,.fa-thermometer-half:before{content:"\f2c9"}.fa-thermometer-1:before,.fa-thermometer-quarter:before{content:"\f2ca"}.fa-thermometer-0:before,.fa-thermometer-empty:before{content:"\f2cb"}.fa-shower:before{content:"\f2cc"}.fa-bathtub:before,.fa-s15:before,.fa-bath:before{content:"\f2cd"}.fa-podcast:before{content:"\f2ce"}.fa-window-maximize:before{content:"\f2d0"}.fa-window-minimize:before{content:"\f2d1"}.fa-window-restore:before{content:"\f2d2"}.fa-times-rectangle:before,.fa-window-close:before{content:"\f2d3"}.fa-times-rectangle-o:before,.fa-window-close-o:before{content:"\f2d4"}.fa-bandcamp:before{content:"\f2d5"}.fa-grav:before{content:"\f2d6"}.fa-etsy:before{content:"\f2d7"}.fa-imdb:before{content:"\f2d8"}.fa-ravelry:before{content:"\f2d9"}.fa-eercast:before{content:"\f2da"}.fa-microchip:before{content:"\f2db"}.fa-snowflake-o:before{content:"\f2dc"}.fa-superpowers:before{content:"\f2dd"}.fa-wpexplorer:before{content:"\f2de"}.fa-meetup:before{content:"\f2e0"}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);border:0}.sr-only-focusable:active,.sr-only-focusable:focus{position:static;width:auto;height:auto;margin:0;overflow:visible;clip:auto} diff --git a/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.eot b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.eot new file mode 100644 index 0000000..e9f60ca Binary files /dev/null and b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.eot differ diff --git a/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.svg b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.svg new file mode 100644 index 0000000..855c845 --- /dev/null +++ b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.svg @@ -0,0 +1,2671 @@ + + + + +Created by FontForge 20120731 at Mon Oct 24 17:37:40 2016 + By ,,, +Copyright Dave Gandy 2016. All rights reserved. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.ttf b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.ttf new file mode 100644 index 0000000..35acda2 Binary files /dev/null and b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.ttf differ diff --git a/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff new file mode 100644 index 0000000..400014a Binary files /dev/null and b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff differ diff --git a/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff2 b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff2 new file mode 100644 index 0000000..4d13fc6 Binary files /dev/null and b/xhiveframework/public/css/fonts/fontawesome/fontawesome-webfont.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Black.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Black.woff2 new file mode 100644 index 0000000..18b35db Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Black.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-BlackItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-BlackItalic.woff2 new file mode 100644 index 0000000..02c9d8e Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-BlackItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Bold.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Bold.woff2 new file mode 100644 index 0000000..0f1b157 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Bold.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-BoldItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-BoldItalic.woff2 new file mode 100644 index 0000000..bc50f24 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-BoldItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-ExtraBold.woff2 b/xhiveframework/public/css/fonts/inter/Inter-ExtraBold.woff2 new file mode 100644 index 0000000..b113368 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-ExtraBold.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-ExtraBoldItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-ExtraBoldItalic.woff2 new file mode 100644 index 0000000..a5b76ca Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-ExtraBoldItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-ExtraLight.woff2 b/xhiveframework/public/css/fonts/inter/Inter-ExtraLight.woff2 new file mode 100644 index 0000000..1d77ae8 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-ExtraLight.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-ExtraLightItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-ExtraLightItalic.woff2 new file mode 100644 index 0000000..8c68492 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-ExtraLightItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Italic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Italic.woff2 new file mode 100644 index 0000000..4c24ce2 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Italic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Light.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Light.woff2 new file mode 100644 index 0000000..dbe6143 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Light.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-LightItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-LightItalic.woff2 new file mode 100644 index 0000000..a40d042 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-LightItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Medium.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Medium.woff2 new file mode 100644 index 0000000..0fd2ee7 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Medium.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-MediumItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-MediumItalic.woff2 new file mode 100644 index 0000000..9676715 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-MediumItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Regular.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Regular.woff2 new file mode 100644 index 0000000..b8699af Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Regular.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-SemiBold.woff2 b/xhiveframework/public/css/fonts/inter/Inter-SemiBold.woff2 new file mode 100644 index 0000000..95c48b1 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-SemiBold.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-SemiBoldItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-SemiBoldItalic.woff2 new file mode 100644 index 0000000..ddfe19e Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-SemiBoldItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-Thin.woff2 b/xhiveframework/public/css/fonts/inter/Inter-Thin.woff2 new file mode 100644 index 0000000..0790960 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-Thin.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/Inter-ThinItalic.woff2 b/xhiveframework/public/css/fonts/inter/Inter-ThinItalic.woff2 new file mode 100644 index 0000000..a7bf213 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/Inter-ThinItalic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/InterVariable-Italic.woff2 b/xhiveframework/public/css/fonts/inter/InterVariable-Italic.woff2 new file mode 100644 index 0000000..f22ec25 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/InterVariable-Italic.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/InterVariable.woff2 b/xhiveframework/public/css/fonts/inter/InterVariable.woff2 new file mode 100644 index 0000000..22a12b0 Binary files /dev/null and b/xhiveframework/public/css/fonts/inter/InterVariable.woff2 differ diff --git a/xhiveframework/public/css/fonts/inter/inter.css b/xhiveframework/public/css/fonts/inter/inter.css new file mode 100644 index 0000000..c530373 --- /dev/null +++ b/xhiveframework/public/css/fonts/inter/inter.css @@ -0,0 +1,33 @@ +@font-face { + font-family: InterVariable; + font-style: normal; + font-weight: 100 900; + font-display: swap; + src: url("/assets/xhiveframework/css/fonts/inter/InterVariable.woff2") format("woff2"); + } + @font-face { + font-family: InterVariable; + font-style: italic; + font-weight: 100 900; + font-display: swap; + src: url("/assets/xhiveframework/css/fonts/inter/InterVariable-Italic.woff2") format("woff2"); + } + /* static fonts */ + @font-face { font-family: "Inter"; font-style: normal; font-weight: 100; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Thin.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 100; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-ThinItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 200; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-ExtraLight.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 200; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-ExtraLightItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 300; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Light.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 300; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-LightItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 400; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Regular.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 400; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Italic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 500; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Medium.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 500; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-MediumItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 600; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-SemiBold.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 600; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-SemiBoldItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 700; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Bold.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 700; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-BoldItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 800; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-ExtraBold.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 800; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-ExtraBoldItalic.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: normal; font-weight: 900; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-Black.woff2") format("woff2"); } + @font-face { font-family: "Inter"; font-style: italic; font-weight: 900; font-display: swap; src: url("/assets/xhiveframework/css/fonts/inter/Inter-BlackItalic.woff2") format("woff2"); } diff --git a/xhiveframework/public/css/fonts/inter/inter.scss b/xhiveframework/public/css/fonts/inter/inter.scss new file mode 100644 index 0000000..ddb4282 --- /dev/null +++ b/xhiveframework/public/css/fonts/inter/inter.scss @@ -0,0 +1 @@ +@import "xhiveframework/public/css/fonts/inter/inter.css"; diff --git a/xhiveframework/public/css/hljs-night-owl.css b/xhiveframework/public/css/hljs-night-owl.css new file mode 100644 index 0000000..932ad2e --- /dev/null +++ b/xhiveframework/public/css/hljs-night-owl.css @@ -0,0 +1,183 @@ +/* + +Night Owl for highlight.js (c) Carl Baxter + +An adaptation of Sarah Drasner's Night Owl VS Code Theme +https://github.com/sdras/night-owl-vscode-theme + +Copyright (c) 2018 Sarah Drasner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +.hljs { + display: block; + overflow-x: auto; + padding: 1rem 1.25rem; + background: #011627; + color: #d6deeb; + border-radius: 0.5rem; + } + + /* General Purpose */ + .hljs-keyword { + color: #c792ea; + font-style: italic; + } + .hljs-built_in { + color: #addb67; + font-style: italic; + } + .hljs-type { + color: #82aaff; + } + .hljs-literal { + color: #ff5874; + } + .hljs-number { + color: #F78C6C; + } + .hljs-regexp { + color: #5ca7e4; + } + .hljs-string { + color: #ecc48d; + } + .hljs-subst { + color: #d3423e; + } + .hljs-symbol { + color: #82aaff; + } + .hljs-class { + color: #ffcb8b; + } + .hljs-function { + color: #82AAFF; + } + .hljs-title { + color: #DCDCAA; + font-style: italic; + } + .hljs-params { + color: #7fdbca; + } + + /* Meta */ + .hljs-comment { + color: #637777; + font-style: italic; + } + .hljs-doctag { + color: #7fdbca; + } + .hljs-meta { + color: #82aaff; + } + .hljs-meta-keyword { + color: #82aaff; + } + .hljs-meta-string { + color: #ecc48d; + } + + /* Tags, attributes, config */ + .hljs-section { + color: #82b1ff; + } + .hljs-tag, + .hljs-name, + .hljs-builtin-name { + color: #7fdbca; + } + .hljs-attr { + color: #7fdbca; + } + .hljs-attribute { + color: #80cbc4; + } + .hljs-variable { + color: #addb67; + } + + /* Markup */ + .hljs-bullet { + color: #d9f5dd; + } + .hljs-code { + color: #80CBC4; + } + .hljs-emphasis { + color: #c792ea; + font-style: italic; + } + .hljs-strong { + color: #addb67; + font-weight: bold; + } + .hljs-formula { + color: #c792ea; + } + .hljs-link { + color: #ff869a; + } + .hljs-quote { + color: #697098; + font-style: italic; + } + + /* CSS */ + .hljs-selector-tag { + color: #ff6363; + } + + .hljs-selector-id { + color: #fad430; + } + + .hljs-selector-class { + color: #addb67; + font-style: italic; + } + + .hljs-selector-attr, + .hljs-selector-pseudo { + color: #c792ea; + font-style: italic; + } + + /* Templates */ + .hljs-template-tag { + color: #c792ea; + } + .hljs-template-variable { + color: #addb67; + } + + /* diff */ + .hljs-addition { + color: #addb67ff; + font-style: italic; + } + + .hljs-deletion { + color: #EF535090; + font-style: italic; + } diff --git a/xhiveframework/public/css/octicons/LICENSE.txt b/xhiveframework/public/css/octicons/LICENSE.txt new file mode 100755 index 0000000..259b43d --- /dev/null +++ b/xhiveframework/public/css/octicons/LICENSE.txt @@ -0,0 +1,9 @@ +(c) 2012-2014 GitHub + +When using the GitHub logos, be sure to follow the GitHub logo guidelines (https://github.com/logos) + +Font License: SIL OFL 1.1 (http://scripts.sil.org/OFL) +Applies to all font files + +Code License: MIT (http://choosealicense.com/licenses/mit/) +Applies to all other files diff --git a/xhiveframework/public/css/octicons/README.md b/xhiveframework/public/css/octicons/README.md new file mode 100755 index 0000000..1007073 --- /dev/null +++ b/xhiveframework/public/css/octicons/README.md @@ -0,0 +1 @@ +If you intend to install Octicons locally, install `octicons-local.ttf`. It should appear as “github-octicons” in your font list. It is specially designed not to conflict with GitHub's web fonts. diff --git a/xhiveframework/public/css/octicons/octicons-local.ttf b/xhiveframework/public/css/octicons/octicons-local.ttf new file mode 100755 index 0000000..2050d5e Binary files /dev/null and b/xhiveframework/public/css/octicons/octicons-local.ttf differ diff --git a/xhiveframework/public/css/octicons/octicons.css b/xhiveframework/public/css/octicons/octicons.css new file mode 100755 index 0000000..a7f7b1a --- /dev/null +++ b/xhiveframework/public/css/octicons/octicons.css @@ -0,0 +1,221 @@ +@font-face { + font-family: 'octicons'; + src: url('/assets/xhiveframework/css/octicons/octicons.eot?#iefix') format('embedded-opentype'), + url('/assets/xhiveframework/css/octicons/octicons.woff') format('woff'), + url('/assets/xhiveframework/css/octicons/octicons.ttf') format('truetype'), + url('/assets/xhiveframework/css/octicons/octicons.svg#octicons') format('svg'); + font-weight: normal; + font-style: normal; +} + +/* + +.octicon is optimized for 16px. +.mega-octicon is optimized for 32px but can be used larger. + +*/ +.octicon, .mega-octicon { + font: normal normal normal 16px/1 octicons; + display: inline-block; + text-decoration: none; + text-rendering: auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} +.mega-octicon { font-size: 32px; } + +.octicon-alert:before { content: '\f02d'} /*  */ +.octicon-arrow-down:before { content: '\f03f'} /*  */ +.octicon-arrow-left:before { content: '\f040'} /*  */ +.octicon-arrow-right:before { content: '\f03e'} /*  */ +.octicon-arrow-small-down:before { content: '\f0a0'} /*  */ +.octicon-arrow-small-left:before { content: '\f0a1'} /*  */ +.octicon-arrow-small-right:before { content: '\f071'} /*  */ +.octicon-arrow-small-up:before { content: '\f09f'} /*  */ +.octicon-arrow-up:before { content: '\f03d'} /*  */ +.octicon-microscope:before, +.octicon-beaker:before { content: '\f0dd'} /*  */ +.octicon-bell:before { content: '\f0de'} /*  */ +.octicon-book:before { content: '\f007'} /*  */ +.octicon-bookmark:before { content: '\f07b'} /*  */ +.octicon-briefcase:before { content: '\f0d3'} /*  */ +.octicon-broadcast:before { content: '\f048'} /*  */ +.octicon-browser:before { content: '\f0c5'} /*  */ +.octicon-bug:before { content: '\f091'} /*  */ +.octicon-calendar:before { content: '\f068'} /*  */ +.octicon-check:before { content: '\f03a'} /*  */ +.octicon-checklist:before { content: '\f076'} /*  */ +.octicon-chevron-down:before { content: '\f0a3'} /*  */ +.octicon-chevron-left:before { content: '\f0a4'} /*  */ +.octicon-chevron-right:before { content: '\f078'} /*  */ +.octicon-chevron-up:before { content: '\f0a2'} /*  */ +.octicon-circle-slash:before { content: '\f084'} /*  */ +.octicon-circuit-board:before { content: '\f0d6'} /*  */ +.octicon-clippy:before { content: '\f035'} /*  */ +.octicon-clock:before { content: '\f046'} /*  */ +.octicon-cloud-download:before { content: '\f00b'} /*  */ +.octicon-cloud-upload:before { content: '\f00c'} /*  */ +.octicon-code:before { content: '\f05f'} /*  */ +.octicon-color-mode:before { content: '\f065'} /*  */ +.octicon-comment-add:before, +.octicon-comment:before { content: '\f02b'} /*  */ +.octicon-comment-discussion:before { content: '\f04f'} /*  */ +.octicon-credit-card:before { content: '\f045'} /*  */ +.octicon-dash:before { content: '\f0ca'} /*  */ +.octicon-dashboard:before { content: '\f07d'} /*  */ +.octicon-database:before { content: '\f096'} /*  */ +.octicon-clone:before, +.octicon-desktop-download:before { content: '\f0dc'} /*  */ +.octicon-device-camera:before { content: '\f056'} /*  */ +.octicon-device-camera-video:before { content: '\f057'} /*  */ +.octicon-device-desktop:before { content: '\f27c'} /*  */ +.octicon-device-mobile:before { content: '\f038'} /*  */ +.octicon-diff:before { content: '\f04d'} /*  */ +.octicon-diff-added:before { content: '\f06b'} /*  */ +.octicon-diff-ignored:before { content: '\f099'} /*  */ +.octicon-diff-modified:before { content: '\f06d'} /*  */ +.octicon-diff-removed:before { content: '\f06c'} /*  */ +.octicon-diff-renamed:before { content: '\f06e'} /*  */ +.octicon-ellipsis:before { content: '\f09a'} /*  */ +.octicon-eye-unwatch:before, +.octicon-eye-watch:before, +.octicon-eye:before { content: '\f04e'} /*  */ +.octicon-file-binary:before { content: '\f094'} /*  */ +.octicon-file-code:before { content: '\f010'} /*  */ +.octicon-file-directory:before { content: '\f016'} /*  */ +.octicon-file-media:before { content: '\f012'} /*  */ +.octicon-file-pdf:before { content: '\f014'} /*  */ +.octicon-file-submodule:before { content: '\f017'} /*  */ +.octicon-file-symlink-directory:before { content: '\f0b1'} /*  */ +.octicon-file-symlink-file:before { content: '\f0b0'} /*  */ +.octicon-file-text:before { content: '\f011'} /*  */ +.octicon-file-zip:before { content: '\f013'} /*  */ +.octicon-flame:before { content: '\f0d2'} /*  */ +.octicon-fold:before { content: '\f0cc'} /*  */ +.octicon-gear:before { content: '\f02f'} /*  */ +.octicon-gift:before { content: '\f042'} /*  */ +.octicon-gist:before { content: '\f00e'} /*  */ +.octicon-gist-secret:before { content: '\f08c'} /*  */ +.octicon-git-branch-create:before, +.octicon-git-branch-delete:before, +.octicon-git-branch:before { content: '\f020'} /*  */ +.octicon-git-commit:before { content: '\f01f'} /*  */ +.octicon-git-compare:before { content: '\f0ac'} /*  */ +.octicon-git-merge:before { content: '\f023'} /*  */ +.octicon-git-pull-request-abandoned:before, +.octicon-git-pull-request:before { content: '\f009'} /*  */ +.octicon-globe:before { content: '\f0b6'} /*  */ +.octicon-graph:before { content: '\f043'} /*  */ +.octicon-heart:before { content: '\2665'} /* ♥ */ +.octicon-history:before { content: '\f07e'} /*  */ +.octicon-home:before { content: '\f08d'} /*  */ +.octicon-horizontal-rule:before { content: '\f070'} /*  */ +.octicon-hubot:before { content: '\f09d'} /*  */ +.octicon-inbox:before { content: '\f0cf'} /*  */ +.octicon-info:before { content: '\f059'} /*  */ +.octicon-issue-closed:before { content: '\f028'} /*  */ +.octicon-issue-opened:before { content: '\f026'} /*  */ +.octicon-issue-reopened:before { content: '\f027'} /*  */ +.octicon-jersey:before { content: '\f019'} /*  */ +.octicon-key:before { content: '\f049'} /*  */ +.octicon-keyboard:before { content: '\f00d'} /*  */ +.octicon-law:before { content: '\f0d8'} /*  */ +.octicon-light-bulb:before { content: '\f000'} /*  */ +.octicon-link:before { content: '\f05c'} /*  */ +.octicon-link-external:before { content: '\f07f'} /*  */ +.octicon-list-ordered:before { content: '\f062'} /*  */ +.octicon-list-unordered:before { content: '\f061'} /*  */ +.octicon-location:before { content: '\f060'} /*  */ +.octicon-gist-private:before, +.octicon-mirror-private:before, +.octicon-git-fork-private:before, +.octicon-lock:before { content: '\f06a'} /*  */ +.octicon-logo-github:before { content: '\f092'} /*  */ +.octicon-mail:before { content: '\f03b'} /*  */ +.octicon-mail-read:before { content: '\f03c'} /*  */ +.octicon-mail-reply:before { content: '\f051'} /*  */ +.octicon-mark-github:before { content: '\f00a'} /*  */ +.octicon-markdown:before { content: '\f0c9'} /*  */ +.octicon-megaphone:before { content: '\f077'} /*  */ +.octicon-mention:before { content: '\f0be'} /*  */ +.octicon-milestone:before { content: '\f075'} /*  */ +.octicon-mirror-public:before, +.octicon-mirror:before { content: '\f024'} /*  */ +.octicon-mortar-board:before { content: '\f0d7'} /*  */ +.octicon-mute:before { content: '\f080'} /*  */ +.octicon-no-newline:before { content: '\f09c'} /*  */ +.octicon-octoface:before { content: '\f008'} /*  */ +.octicon-organization:before { content: '\f037'} /*  */ +.octicon-package:before { content: '\f0c4'} /*  */ +.octicon-paintcan:before { content: '\f0d1'} /*  */ +.octicon-pencil:before { content: '\f058'} /*  */ +.octicon-person-add:before, +.octicon-person-follow:before, +.octicon-person:before { content: '\f018'} /*  */ +.octicon-pin:before { content: '\f041'} /*  */ +.octicon-plug:before { content: '\f0d4'} /*  */ +.octicon-repo-create:before, +.octicon-gist-new:before, +.octicon-file-directory-create:before, +.octicon-file-add:before, +.octicon-plus:before { content: '\f05d'} /*  */ +.octicon-primitive-dot:before { content: '\f052'} /*  */ +.octicon-primitive-square:before { content: '\f053'} /*  */ +.octicon-pulse:before { content: '\f085'} /*  */ +.octicon-question:before { content: '\f02c'} /*  */ +.octicon-quote:before { content: '\f063'} /*  */ +.octicon-radio-tower:before { content: '\f030'} /*  */ +.octicon-repo-delete:before, +.octicon-repo:before { content: '\f001'} /*  */ +.octicon-repo-clone:before { content: '\f04c'} /*  */ +.octicon-repo-force-push:before { content: '\f04a'} /*  */ +.octicon-gist-fork:before, +.octicon-repo-forked:before { content: '\f002'} /*  */ +.octicon-repo-pull:before { content: '\f006'} /*  */ +.octicon-repo-push:before { content: '\f005'} /*  */ +.octicon-rocket:before { content: '\f033'} /*  */ +.octicon-rss:before { content: '\f034'} /*  */ +.octicon-ruby:before { content: '\f047'} /*  */ +.octicon-screen-full:before { content: '\f066'} /*  */ +.octicon-screen-normal:before { content: '\f067'} /*  */ +.octicon-search-save:before, +.octicon-search:before { content: '\f02e'} /*  */ +.octicon-server:before { content: '\f097'} /*  */ +.octicon-settings:before { content: '\f07c'} /*  */ +.octicon-shield:before { content: '\f0e1'} /*  */ +.octicon-log-in:before, +.octicon-sign-in:before { content: '\f036'} /*  */ +.octicon-log-out:before, +.octicon-sign-out:before { content: '\f032'} /*  */ +.octicon-squirrel:before { content: '\f0b2'} /*  */ +.octicon-star-add:before, +.octicon-star-delete:before, +.octicon-star:before { content: '\f02a'} /*  */ +.octicon-stop:before { content: '\f08f'} /*  */ +.octicon-repo-sync:before, +.octicon-sync:before { content: '\f087'} /*  */ +.octicon-tag-remove:before, +.octicon-tag-add:before, +.octicon-tag:before { content: '\f015'} /*  */ +.octicon-telescope:before { content: '\f088'} /*  */ +.octicon-terminal:before { content: '\f0c8'} /*  */ +.octicon-three-bars:before { content: '\f05e'} /*  */ +.octicon-thumbsdown:before { content: '\f0db'} /*  */ +.octicon-thumbsup:before { content: '\f0da'} /*  */ +.octicon-tools:before { content: '\f031'} /*  */ +.octicon-trashcan:before { content: '\f0d0'} /*  */ +.octicon-triangle-down:before { content: '\f05b'} /*  */ +.octicon-triangle-left:before { content: '\f044'} /*  */ +.octicon-triangle-right:before { content: '\f05a'} /*  */ +.octicon-triangle-up:before { content: '\f0aa'} /*  */ +.octicon-unfold:before { content: '\f039'} /*  */ +.octicon-unmute:before { content: '\f0ba'} /*  */ +.octicon-versions:before { content: '\f064'} /*  */ +.octicon-watch:before { content: '\f0e0'} /*  */ +.octicon-remove-close:before, +.octicon-x:before { content: '\f081'} /*  */ +.octicon-zap:before { content: '\26A1'} /* ⚡ */ diff --git a/xhiveframework/public/css/octicons/octicons.eot b/xhiveframework/public/css/octicons/octicons.eot new file mode 100755 index 0000000..2bf20bc Binary files /dev/null and b/xhiveframework/public/css/octicons/octicons.eot differ diff --git a/xhiveframework/public/css/octicons/octicons.less b/xhiveframework/public/css/octicons/octicons.less new file mode 100755 index 0000000..d1d751e --- /dev/null +++ b/xhiveframework/public/css/octicons/octicons.less @@ -0,0 +1,220 @@ +@octicons-font-path: "."; +@octicons-version: "396334ee3da78f4302d25c758ae3e3ce5dc3c97d"; + +@font-face { + font-family: 'octicons'; + src: ~"url('@{octicons-font-path}/octicons.eot?#iefix&v=@{octicons-version}') format('embedded-opentype')", + ~"url('@{octicons-font-path}/octicons.woff?v=@{octicons-version}') format('woff')", + ~"url('@{octicons-font-path}/octicons.ttf?v=@{octicons-version}') format('truetype')", + ~"url('@{octicons-font-path}/octicons.svg?v=@{octicons-version}#octicons') format('svg')"; + font-weight: normal; + font-style: normal; +} + +// .octicon is optimized for 16px. +// .mega-octicon is optimized for 32px but can be used larger. +.octicon, .mega-octicon { + font: normal normal normal 16px/1 octicons; + display: inline-block; + text-decoration: none; + text-rendering: auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} +.mega-octicon { font-size: 32px; } + +.octicon-alert:before { content: '\f02d'} /*  */ +.octicon-arrow-down:before { content: '\f03f'} /*  */ +.octicon-arrow-left:before { content: '\f040'} /*  */ +.octicon-arrow-right:before { content: '\f03e'} /*  */ +.octicon-arrow-small-down:before { content: '\f0a0'} /*  */ +.octicon-arrow-small-left:before { content: '\f0a1'} /*  */ +.octicon-arrow-small-right:before { content: '\f071'} /*  */ +.octicon-arrow-small-up:before { content: '\f09f'} /*  */ +.octicon-arrow-up:before { content: '\f03d'} /*  */ +.octicon-microscope:before, +.octicon-beaker:before { content: '\f0dd'} /*  */ +.octicon-bell:before { content: '\f0de'} /*  */ +.octicon-book:before { content: '\f007'} /*  */ +.octicon-bookmark:before { content: '\f07b'} /*  */ +.octicon-briefcase:before { content: '\f0d3'} /*  */ +.octicon-broadcast:before { content: '\f048'} /*  */ +.octicon-browser:before { content: '\f0c5'} /*  */ +.octicon-bug:before { content: '\f091'} /*  */ +.octicon-calendar:before { content: '\f068'} /*  */ +.octicon-check:before { content: '\f03a'} /*  */ +.octicon-checklist:before { content: '\f076'} /*  */ +.octicon-chevron-down:before { content: '\f0a3'} /*  */ +.octicon-chevron-left:before { content: '\f0a4'} /*  */ +.octicon-chevron-right:before { content: '\f078'} /*  */ +.octicon-chevron-up:before { content: '\f0a2'} /*  */ +.octicon-circle-slash:before { content: '\f084'} /*  */ +.octicon-circuit-board:before { content: '\f0d6'} /*  */ +.octicon-clippy:before { content: '\f035'} /*  */ +.octicon-clock:before { content: '\f046'} /*  */ +.octicon-cloud-download:before { content: '\f00b'} /*  */ +.octicon-cloud-upload:before { content: '\f00c'} /*  */ +.octicon-code:before { content: '\f05f'} /*  */ +.octicon-color-mode:before { content: '\f065'} /*  */ +.octicon-comment-add:before, +.octicon-comment:before { content: '\f02b'} /*  */ +.octicon-comment-discussion:before { content: '\f04f'} /*  */ +.octicon-credit-card:before { content: '\f045'} /*  */ +.octicon-dash:before { content: '\f0ca'} /*  */ +.octicon-dashboard:before { content: '\f07d'} /*  */ +.octicon-database:before { content: '\f096'} /*  */ +.octicon-clone:before, +.octicon-desktop-download:before { content: '\f0dc'} /*  */ +.octicon-device-camera:before { content: '\f056'} /*  */ +.octicon-device-camera-video:before { content: '\f057'} /*  */ +.octicon-device-desktop:before { content: '\f27c'} /*  */ +.octicon-device-mobile:before { content: '\f038'} /*  */ +.octicon-diff:before { content: '\f04d'} /*  */ +.octicon-diff-added:before { content: '\f06b'} /*  */ +.octicon-diff-ignored:before { content: '\f099'} /*  */ +.octicon-diff-modified:before { content: '\f06d'} /*  */ +.octicon-diff-removed:before { content: '\f06c'} /*  */ +.octicon-diff-renamed:before { content: '\f06e'} /*  */ +.octicon-ellipsis:before { content: '\f09a'} /*  */ +.octicon-eye-unwatch:before, +.octicon-eye-watch:before, +.octicon-eye:before { content: '\f04e'} /*  */ +.octicon-file-binary:before { content: '\f094'} /*  */ +.octicon-file-code:before { content: '\f010'} /*  */ +.octicon-file-directory:before { content: '\f016'} /*  */ +.octicon-file-media:before { content: '\f012'} /*  */ +.octicon-file-pdf:before { content: '\f014'} /*  */ +.octicon-file-submodule:before { content: '\f017'} /*  */ +.octicon-file-symlink-directory:before { content: '\f0b1'} /*  */ +.octicon-file-symlink-file:before { content: '\f0b0'} /*  */ +.octicon-file-text:before { content: '\f011'} /*  */ +.octicon-file-zip:before { content: '\f013'} /*  */ +.octicon-flame:before { content: '\f0d2'} /*  */ +.octicon-fold:before { content: '\f0cc'} /*  */ +.octicon-gear:before { content: '\f02f'} /*  */ +.octicon-gift:before { content: '\f042'} /*  */ +.octicon-gist:before { content: '\f00e'} /*  */ +.octicon-gist-secret:before { content: '\f08c'} /*  */ +.octicon-git-branch-create:before, +.octicon-git-branch-delete:before, +.octicon-git-branch:before { content: '\f020'} /*  */ +.octicon-git-commit:before { content: '\f01f'} /*  */ +.octicon-git-compare:before { content: '\f0ac'} /*  */ +.octicon-git-merge:before { content: '\f023'} /*  */ +.octicon-git-pull-request-abandoned:before, +.octicon-git-pull-request:before { content: '\f009'} /*  */ +.octicon-globe:before { content: '\f0b6'} /*  */ +.octicon-graph:before { content: '\f043'} /*  */ +.octicon-heart:before { content: '\2665'} /* ♥ */ +.octicon-history:before { content: '\f07e'} /*  */ +.octicon-home:before { content: '\f08d'} /*  */ +.octicon-horizontal-rule:before { content: '\f070'} /*  */ +.octicon-hubot:before { content: '\f09d'} /*  */ +.octicon-inbox:before { content: '\f0cf'} /*  */ +.octicon-info:before { content: '\f059'} /*  */ +.octicon-issue-closed:before { content: '\f028'} /*  */ +.octicon-issue-opened:before { content: '\f026'} /*  */ +.octicon-issue-reopened:before { content: '\f027'} /*  */ +.octicon-jersey:before { content: '\f019'} /*  */ +.octicon-key:before { content: '\f049'} /*  */ +.octicon-keyboard:before { content: '\f00d'} /*  */ +.octicon-law:before { content: '\f0d8'} /*  */ +.octicon-light-bulb:before { content: '\f000'} /*  */ +.octicon-link:before { content: '\f05c'} /*  */ +.octicon-link-external:before { content: '\f07f'} /*  */ +.octicon-list-ordered:before { content: '\f062'} /*  */ +.octicon-list-unordered:before { content: '\f061'} /*  */ +.octicon-location:before { content: '\f060'} /*  */ +.octicon-gist-private:before, +.octicon-mirror-private:before, +.octicon-git-fork-private:before, +.octicon-lock:before { content: '\f06a'} /*  */ +.octicon-logo-github:before { content: '\f092'} /*  */ +.octicon-mail:before { content: '\f03b'} /*  */ +.octicon-mail-read:before { content: '\f03c'} /*  */ +.octicon-mail-reply:before { content: '\f051'} /*  */ +.octicon-mark-github:before { content: '\f00a'} /*  */ +.octicon-markdown:before { content: '\f0c9'} /*  */ +.octicon-megaphone:before { content: '\f077'} /*  */ +.octicon-mention:before { content: '\f0be'} /*  */ +.octicon-milestone:before { content: '\f075'} /*  */ +.octicon-mirror-public:before, +.octicon-mirror:before { content: '\f024'} /*  */ +.octicon-mortar-board:before { content: '\f0d7'} /*  */ +.octicon-mute:before { content: '\f080'} /*  */ +.octicon-no-newline:before { content: '\f09c'} /*  */ +.octicon-octoface:before { content: '\f008'} /*  */ +.octicon-organization:before { content: '\f037'} /*  */ +.octicon-package:before { content: '\f0c4'} /*  */ +.octicon-paintcan:before { content: '\f0d1'} /*  */ +.octicon-pencil:before { content: '\f058'} /*  */ +.octicon-person-add:before, +.octicon-person-follow:before, +.octicon-person:before { content: '\f018'} /*  */ +.octicon-pin:before { content: '\f041'} /*  */ +.octicon-plug:before { content: '\f0d4'} /*  */ +.octicon-repo-create:before, +.octicon-gist-new:before, +.octicon-file-directory-create:before, +.octicon-file-add:before, +.octicon-plus:before { content: '\f05d'} /*  */ +.octicon-primitive-dot:before { content: '\f052'} /*  */ +.octicon-primitive-square:before { content: '\f053'} /*  */ +.octicon-pulse:before { content: '\f085'} /*  */ +.octicon-question:before { content: '\f02c'} /*  */ +.octicon-quote:before { content: '\f063'} /*  */ +.octicon-radio-tower:before { content: '\f030'} /*  */ +.octicon-repo-delete:before, +.octicon-repo:before { content: '\f001'} /*  */ +.octicon-repo-clone:before { content: '\f04c'} /*  */ +.octicon-repo-force-push:before { content: '\f04a'} /*  */ +.octicon-gist-fork:before, +.octicon-repo-forked:before { content: '\f002'} /*  */ +.octicon-repo-pull:before { content: '\f006'} /*  */ +.octicon-repo-push:before { content: '\f005'} /*  */ +.octicon-rocket:before { content: '\f033'} /*  */ +.octicon-rss:before { content: '\f034'} /*  */ +.octicon-ruby:before { content: '\f047'} /*  */ +.octicon-screen-full:before { content: '\f066'} /*  */ +.octicon-screen-normal:before { content: '\f067'} /*  */ +.octicon-search-save:before, +.octicon-search:before { content: '\f02e'} /*  */ +.octicon-server:before { content: '\f097'} /*  */ +.octicon-settings:before { content: '\f07c'} /*  */ +.octicon-shield:before { content: '\f0e1'} /*  */ +.octicon-log-in:before, +.octicon-sign-in:before { content: '\f036'} /*  */ +.octicon-log-out:before, +.octicon-sign-out:before { content: '\f032'} /*  */ +.octicon-squirrel:before { content: '\f0b2'} /*  */ +.octicon-star-add:before, +.octicon-star-delete:before, +.octicon-star:before { content: '\f02a'} /*  */ +.octicon-stop:before { content: '\f08f'} /*  */ +.octicon-repo-sync:before, +.octicon-sync:before { content: '\f087'} /*  */ +.octicon-tag-remove:before, +.octicon-tag-add:before, +.octicon-tag:before { content: '\f015'} /*  */ +.octicon-telescope:before { content: '\f088'} /*  */ +.octicon-terminal:before { content: '\f0c8'} /*  */ +.octicon-three-bars:before { content: '\f05e'} /*  */ +.octicon-thumbsdown:before { content: '\f0db'} /*  */ +.octicon-thumbsup:before { content: '\f0da'} /*  */ +.octicon-tools:before { content: '\f031'} /*  */ +.octicon-trashcan:before { content: '\f0d0'} /*  */ +.octicon-triangle-down:before { content: '\f05b'} /*  */ +.octicon-triangle-left:before { content: '\f044'} /*  */ +.octicon-triangle-right:before { content: '\f05a'} /*  */ +.octicon-triangle-up:before { content: '\f0aa'} /*  */ +.octicon-unfold:before { content: '\f039'} /*  */ +.octicon-unmute:before { content: '\f0ba'} /*  */ +.octicon-versions:before { content: '\f064'} /*  */ +.octicon-watch:before { content: '\f0e0'} /*  */ +.octicon-remove-close:before, +.octicon-x:before { content: '\f081'} /*  */ +.octicon-zap:before { content: '\26A1'} /* ⚡ */ diff --git a/xhiveframework/public/css/octicons/octicons.scss b/xhiveframework/public/css/octicons/octicons.scss new file mode 100755 index 0000000..81e7d52 --- /dev/null +++ b/xhiveframework/public/css/octicons/octicons.scss @@ -0,0 +1,548 @@ +$octicons-font-path: "." !default; +$octicons-version: "396334ee3da78f4302d25c758ae3e3ce5dc3c97d"; + +@font-face { + font-family: "octicons"; + src: url("#{$octicons-font-path}/octicons.eot?#iefix&v=#{$octicons-version}") + format("embedded-opentype"), + url("#{$octicons-font-path}/octicons.woff?v=#{$octicons-version}") format("woff"), + url("#{$octicons-font-path}/octicons.ttf?v=#{$octicons-version}") format("truetype"), + url("#{$octicons-font-path}/octicons.svg?v=#{$octicons-version}#octicons") format("svg"); + font-weight: normal; + font-style: normal; +} + +// .octicon is optimized for 16px. +// .mega-octicon is optimized for 32px but can be used larger. +.octicon, +.mega-octicon { + font: normal normal normal 16px/1 octicons; + display: inline-block; + text-decoration: none; + text-rendering: auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} +.mega-octicon { + font-size: 32px; +} + +.octicon-alert:before { + content: "\f02d"; +} /*  */ +.octicon-arrow-down:before { + content: "\f03f"; +} /*  */ +.octicon-arrow-left:before { + content: "\f040"; +} /*  */ +.octicon-arrow-right:before { + content: "\f03e"; +} /*  */ +.octicon-arrow-small-down:before { + content: "\f0a0"; +} /*  */ +.octicon-arrow-small-left:before { + content: "\f0a1"; +} /*  */ +.octicon-arrow-small-right:before { + content: "\f071"; +} /*  */ +.octicon-arrow-small-up:before { + content: "\f09f"; +} /*  */ +.octicon-arrow-up:before { + content: "\f03d"; +} /*  */ +.octicon-microscope:before, +.octicon-beaker:before { + content: "\f0dd"; +} /*  */ +.octicon-bell:before { + content: "\f0de"; +} /*  */ +.octicon-book:before { + content: "\f007"; +} /*  */ +.octicon-bookmark:before { + content: "\f07b"; +} /*  */ +.octicon-briefcase:before { + content: "\f0d3"; +} /*  */ +.octicon-broadcast:before { + content: "\f048"; +} /*  */ +.octicon-browser:before { + content: "\f0c5"; +} /*  */ +.octicon-bug:before { + content: "\f091"; +} /*  */ +.octicon-calendar:before { + content: "\f068"; +} /*  */ +.octicon-check:before { + content: "\f03a"; +} /*  */ +.octicon-checklist:before { + content: "\f076"; +} /*  */ +.octicon-chevron-down:before { + content: "\f0a3"; +} /*  */ +.octicon-chevron-left:before { + content: "\f0a4"; +} /*  */ +.octicon-chevron-right:before { + content: "\f078"; +} /*  */ +.octicon-chevron-up:before { + content: "\f0a2"; +} /*  */ +.octicon-circle-slash:before { + content: "\f084"; +} /*  */ +.octicon-circuit-board:before { + content: "\f0d6"; +} /*  */ +.octicon-clippy:before { + content: "\f035"; +} /*  */ +.octicon-clock:before { + content: "\f046"; +} /*  */ +.octicon-cloud-download:before { + content: "\f00b"; +} /*  */ +.octicon-cloud-upload:before { + content: "\f00c"; +} /*  */ +.octicon-code:before { + content: "\f05f"; +} /*  */ +.octicon-color-mode:before { + content: "\f065"; +} /*  */ +.octicon-comment-add:before, +.octicon-comment:before { + content: "\f02b"; +} /*  */ +.octicon-comment-discussion:before { + content: "\f04f"; +} /*  */ +.octicon-credit-card:before { + content: "\f045"; +} /*  */ +.octicon-dash:before { + content: "\f0ca"; +} /*  */ +.octicon-dashboard:before { + content: "\f07d"; +} /*  */ +.octicon-database:before { + content: "\f096"; +} /*  */ +.octicon-clone:before, +.octicon-desktop-download:before { + content: "\f0dc"; +} /*  */ +.octicon-device-camera:before { + content: "\f056"; +} /*  */ +.octicon-device-camera-video:before { + content: "\f057"; +} /*  */ +.octicon-device-desktop:before { + content: "\f27c"; +} /*  */ +.octicon-device-mobile:before { + content: "\f038"; +} /*  */ +.octicon-diff:before { + content: "\f04d"; +} /*  */ +.octicon-diff-added:before { + content: "\f06b"; +} /*  */ +.octicon-diff-ignored:before { + content: "\f099"; +} /*  */ +.octicon-diff-modified:before { + content: "\f06d"; +} /*  */ +.octicon-diff-removed:before { + content: "\f06c"; +} /*  */ +.octicon-diff-renamed:before { + content: "\f06e"; +} /*  */ +.octicon-ellipsis:before { + content: "\f09a"; +} /*  */ +.octicon-eye-unwatch:before, +.octicon-eye-watch:before, +.octicon-eye:before { + content: "\f04e"; +} /*  */ +.octicon-file-binary:before { + content: "\f094"; +} /*  */ +.octicon-file-code:before { + content: "\f010"; +} /*  */ +.octicon-file-directory:before { + content: "\f016"; +} /*  */ +.octicon-file-media:before { + content: "\f012"; +} /*  */ +.octicon-file-pdf:before { + content: "\f014"; +} /*  */ +.octicon-file-submodule:before { + content: "\f017"; +} /*  */ +.octicon-file-symlink-directory:before { + content: "\f0b1"; +} /*  */ +.octicon-file-symlink-file:before { + content: "\f0b0"; +} /*  */ +.octicon-file-text:before { + content: "\f011"; +} /*  */ +.octicon-file-zip:before { + content: "\f013"; +} /*  */ +.octicon-flame:before { + content: "\f0d2"; +} /*  */ +.octicon-fold:before { + content: "\f0cc"; +} /*  */ +.octicon-gear:before { + content: "\f02f"; +} /*  */ +.octicon-gift:before { + content: "\f042"; +} /*  */ +.octicon-gist:before { + content: "\f00e"; +} /*  */ +.octicon-gist-secret:before { + content: "\f08c"; +} /*  */ +.octicon-git-branch-create:before, +.octicon-git-branch-delete:before, +.octicon-git-branch:before { + content: "\f020"; +} /*  */ +.octicon-git-commit:before { + content: "\f01f"; +} /*  */ +.octicon-git-compare:before { + content: "\f0ac"; +} /*  */ +.octicon-git-merge:before { + content: "\f023"; +} /*  */ +.octicon-git-pull-request-abandoned:before, +.octicon-git-pull-request:before { + content: "\f009"; +} /*  */ +.octicon-globe:before { + content: "\f0b6"; +} /*  */ +.octicon-graph:before { + content: "\f043"; +} /*  */ +.octicon-heart:before { + content: "\2665"; +} /* ♥ */ +.octicon-history:before { + content: "\f07e"; +} /*  */ +.octicon-home:before { + content: "\f08d"; +} /*  */ +.octicon-horizontal-rule:before { + content: "\f070"; +} /*  */ +.octicon-hubot:before { + content: "\f09d"; +} /*  */ +.octicon-inbox:before { + content: "\f0cf"; +} /*  */ +.octicon-info:before { + content: "\f059"; +} /*  */ +.octicon-issue-closed:before { + content: "\f028"; +} /*  */ +.octicon-issue-opened:before { + content: "\f026"; +} /*  */ +.octicon-issue-reopened:before { + content: "\f027"; +} /*  */ +.octicon-jersey:before { + content: "\f019"; +} /*  */ +.octicon-key:before { + content: "\f049"; +} /*  */ +.octicon-keyboard:before { + content: "\f00d"; +} /*  */ +.octicon-law:before { + content: "\f0d8"; +} /*  */ +.octicon-light-bulb:before { + content: "\f000"; +} /*  */ +.octicon-link:before { + content: "\f05c"; +} /*  */ +.octicon-link-external:before { + content: "\f07f"; +} /*  */ +.octicon-list-ordered:before { + content: "\f062"; +} /*  */ +.octicon-list-unordered:before { + content: "\f061"; +} /*  */ +.octicon-location:before { + content: "\f060"; +} /*  */ +.octicon-gist-private:before, +.octicon-mirror-private:before, +.octicon-git-fork-private:before, +.octicon-lock:before { + content: "\f06a"; +} /*  */ +.octicon-logo-github:before { + content: "\f092"; +} /*  */ +.octicon-mail:before { + content: "\f03b"; +} /*  */ +.octicon-mail-read:before { + content: "\f03c"; +} /*  */ +.octicon-mail-reply:before { + content: "\f051"; +} /*  */ +.octicon-mark-github:before { + content: "\f00a"; +} /*  */ +.octicon-markdown:before { + content: "\f0c9"; +} /*  */ +.octicon-megaphone:before { + content: "\f077"; +} /*  */ +.octicon-mention:before { + content: "\f0be"; +} /*  */ +.octicon-milestone:before { + content: "\f075"; +} /*  */ +.octicon-mirror-public:before, +.octicon-mirror:before { + content: "\f024"; +} /*  */ +.octicon-mortar-board:before { + content: "\f0d7"; +} /*  */ +.octicon-mute:before { + content: "\f080"; +} /*  */ +.octicon-no-newline:before { + content: "\f09c"; +} /*  */ +.octicon-octoface:before { + content: "\f008"; +} /*  */ +.octicon-organization:before { + content: "\f037"; +} /*  */ +.octicon-package:before { + content: "\f0c4"; +} /*  */ +.octicon-paintcan:before { + content: "\f0d1"; +} /*  */ +.octicon-pencil:before { + content: "\f058"; +} /*  */ +.octicon-person-add:before, +.octicon-person-follow:before, +.octicon-person:before { + content: "\f018"; +} /*  */ +.octicon-pin:before { + content: "\f041"; +} /*  */ +.octicon-plug:before { + content: "\f0d4"; +} /*  */ +.octicon-repo-create:before, +.octicon-gist-new:before, +.octicon-file-directory-create:before, +.octicon-file-add:before, +.octicon-plus:before { + content: "\f05d"; +} /*  */ +.octicon-primitive-dot:before { + content: "\f052"; +} /*  */ +.octicon-primitive-square:before { + content: "\f053"; +} /*  */ +.octicon-pulse:before { + content: "\f085"; +} /*  */ +.octicon-question:before { + content: "\f02c"; +} /*  */ +.octicon-quote:before { + content: "\f063"; +} /*  */ +.octicon-radio-tower:before { + content: "\f030"; +} /*  */ +.octicon-repo-delete:before, +.octicon-repo:before { + content: "\f001"; +} /*  */ +.octicon-repo-clone:before { + content: "\f04c"; +} /*  */ +.octicon-repo-force-push:before { + content: "\f04a"; +} /*  */ +.octicon-gist-fork:before, +.octicon-repo-forked:before { + content: "\f002"; +} /*  */ +.octicon-repo-pull:before { + content: "\f006"; +} /*  */ +.octicon-repo-push:before { + content: "\f005"; +} /*  */ +.octicon-rocket:before { + content: "\f033"; +} /*  */ +.octicon-rss:before { + content: "\f034"; +} /*  */ +.octicon-ruby:before { + content: "\f047"; +} /*  */ +.octicon-screen-full:before { + content: "\f066"; +} /*  */ +.octicon-screen-normal:before { + content: "\f067"; +} /*  */ +.octicon-search-save:before, +.octicon-search:before { + content: "\f02e"; +} /*  */ +.octicon-server:before { + content: "\f097"; +} /*  */ +.octicon-settings:before { + content: "\f07c"; +} /*  */ +.octicon-shield:before { + content: "\f0e1"; +} /*  */ +.octicon-log-in:before, +.octicon-sign-in:before { + content: "\f036"; +} /*  */ +.octicon-log-out:before, +.octicon-sign-out:before { + content: "\f032"; +} /*  */ +.octicon-squirrel:before { + content: "\f0b2"; +} /*  */ +.octicon-star-add:before, +.octicon-star-delete:before, +.octicon-star:before { + content: "\f02a"; +} /*  */ +.octicon-stop:before { + content: "\f08f"; +} /*  */ +.octicon-repo-sync:before, +.octicon-sync:before { + content: "\f087"; +} /*  */ +.octicon-tag-remove:before, +.octicon-tag-add:before, +.octicon-tag:before { + content: "\f015"; +} /*  */ +.octicon-telescope:before { + content: "\f088"; +} /*  */ +.octicon-terminal:before { + content: "\f0c8"; +} /*  */ +.octicon-three-bars:before { + content: "\f05e"; +} /*  */ +.octicon-thumbsdown:before { + content: "\f0db"; +} /*  */ +.octicon-thumbsup:before { + content: "\f0da"; +} /*  */ +.octicon-tools:before { + content: "\f031"; +} /*  */ +.octicon-trashcan:before { + content: "\f0d0"; +} /*  */ +.octicon-triangle-down:before { + content: "\f05b"; +} /*  */ +.octicon-triangle-left:before { + content: "\f044"; +} /*  */ +.octicon-triangle-right:before { + content: "\f05a"; +} /*  */ +.octicon-triangle-up:before { + content: "\f0aa"; +} /*  */ +.octicon-unfold:before { + content: "\f039"; +} /*  */ +.octicon-unmute:before { + content: "\f0ba"; +} /*  */ +.octicon-versions:before { + content: "\f064"; +} /*  */ +.octicon-watch:before { + content: "\f0e0"; +} /*  */ +.octicon-remove-close:before, +.octicon-x:before { + content: "\f081"; +} /*  */ +.octicon-zap:before { + content: "\26A1"; +} /* ⚡ */ diff --git a/xhiveframework/public/css/octicons/octicons.svg b/xhiveframework/public/css/octicons/octicons.svg new file mode 100755 index 0000000..d932988 --- /dev/null +++ b/xhiveframework/public/css/octicons/octicons.svg @@ -0,0 +1,183 @@ + + + + +(c) 2012-2015 GitHub + +When using the GitHub logos, be sure to follow the GitHub logo guidelines (https://github.com/logos) + +Font License: SIL OFL 1.1 (http://scripts.sil.org/OFL) +Applies to all font files + +Code License: MIT (http://choosealicense.com/licenses/mit/) +Applies to all other files + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xhiveframework/public/css/octicons/octicons.ttf b/xhiveframework/public/css/octicons/octicons.ttf new file mode 100755 index 0000000..32e6720 Binary files /dev/null and b/xhiveframework/public/css/octicons/octicons.ttf differ diff --git a/xhiveframework/public/css/octicons/octicons.woff b/xhiveframework/public/css/octicons/octicons.woff new file mode 100755 index 0000000..cbf9f62 Binary files /dev/null and b/xhiveframework/public/css/octicons/octicons.woff differ diff --git a/xhiveframework/public/css/octicons/sprockets-octicons.scss b/xhiveframework/public/css/octicons/sprockets-octicons.scss new file mode 100755 index 0000000..84cd64d --- /dev/null +++ b/xhiveframework/public/css/octicons/sprockets-octicons.scss @@ -0,0 +1,543 @@ +@font-face { + font-family: "octicons"; + src: font-url("octicons.eot?#iefix") format("embedded-opentype"), + font-url("octicons.woff") format("woff"), font-url("octicons.ttf") format("truetype"), + font-url("octicons.svg#octicons") format("svg"); + font-weight: normal; + font-style: normal; +} + +// .octicon is optimized for 16px. +// .mega-octicon is optimized for 32px but can be used larger. +.octicon, +.mega-octicon { + font: normal normal normal 16px/1 octicons; + display: inline-block; + text-decoration: none; + text-rendering: auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} +.mega-octicon { + font-size: 32px; +} + +.octicon-alert:before { + content: "\f02d"; +} /*  */ +.octicon-arrow-down:before { + content: "\f03f"; +} /*  */ +.octicon-arrow-left:before { + content: "\f040"; +} /*  */ +.octicon-arrow-right:before { + content: "\f03e"; +} /*  */ +.octicon-arrow-small-down:before { + content: "\f0a0"; +} /*  */ +.octicon-arrow-small-left:before { + content: "\f0a1"; +} /*  */ +.octicon-arrow-small-right:before { + content: "\f071"; +} /*  */ +.octicon-arrow-small-up:before { + content: "\f09f"; +} /*  */ +.octicon-arrow-up:before { + content: "\f03d"; +} /*  */ +.octicon-microscope:before, +.octicon-beaker:before { + content: "\f0dd"; +} /*  */ +.octicon-bell:before { + content: "\f0de"; +} /*  */ +.octicon-book:before { + content: "\f007"; +} /*  */ +.octicon-bookmark:before { + content: "\f07b"; +} /*  */ +.octicon-briefcase:before { + content: "\f0d3"; +} /*  */ +.octicon-broadcast:before { + content: "\f048"; +} /*  */ +.octicon-browser:before { + content: "\f0c5"; +} /*  */ +.octicon-bug:before { + content: "\f091"; +} /*  */ +.octicon-calendar:before { + content: "\f068"; +} /*  */ +.octicon-check:before { + content: "\f03a"; +} /*  */ +.octicon-checklist:before { + content: "\f076"; +} /*  */ +.octicon-chevron-down:before { + content: "\f0a3"; +} /*  */ +.octicon-chevron-left:before { + content: "\f0a4"; +} /*  */ +.octicon-chevron-right:before { + content: "\f078"; +} /*  */ +.octicon-chevron-up:before { + content: "\f0a2"; +} /*  */ +.octicon-circle-slash:before { + content: "\f084"; +} /*  */ +.octicon-circuit-board:before { + content: "\f0d6"; +} /*  */ +.octicon-clippy:before { + content: "\f035"; +} /*  */ +.octicon-clock:before { + content: "\f046"; +} /*  */ +.octicon-cloud-download:before { + content: "\f00b"; +} /*  */ +.octicon-cloud-upload:before { + content: "\f00c"; +} /*  */ +.octicon-code:before { + content: "\f05f"; +} /*  */ +.octicon-color-mode:before { + content: "\f065"; +} /*  */ +.octicon-comment-add:before, +.octicon-comment:before { + content: "\f02b"; +} /*  */ +.octicon-comment-discussion:before { + content: "\f04f"; +} /*  */ +.octicon-credit-card:before { + content: "\f045"; +} /*  */ +.octicon-dash:before { + content: "\f0ca"; +} /*  */ +.octicon-dashboard:before { + content: "\f07d"; +} /*  */ +.octicon-database:before { + content: "\f096"; +} /*  */ +.octicon-clone:before, +.octicon-desktop-download:before { + content: "\f0dc"; +} /*  */ +.octicon-device-camera:before { + content: "\f056"; +} /*  */ +.octicon-device-camera-video:before { + content: "\f057"; +} /*  */ +.octicon-device-desktop:before { + content: "\f27c"; +} /*  */ +.octicon-device-mobile:before { + content: "\f038"; +} /*  */ +.octicon-diff:before { + content: "\f04d"; +} /*  */ +.octicon-diff-added:before { + content: "\f06b"; +} /*  */ +.octicon-diff-ignored:before { + content: "\f099"; +} /*  */ +.octicon-diff-modified:before { + content: "\f06d"; +} /*  */ +.octicon-diff-removed:before { + content: "\f06c"; +} /*  */ +.octicon-diff-renamed:before { + content: "\f06e"; +} /*  */ +.octicon-ellipsis:before { + content: "\f09a"; +} /*  */ +.octicon-eye-unwatch:before, +.octicon-eye-watch:before, +.octicon-eye:before { + content: "\f04e"; +} /*  */ +.octicon-file-binary:before { + content: "\f094"; +} /*  */ +.octicon-file-code:before { + content: "\f010"; +} /*  */ +.octicon-file-directory:before { + content: "\f016"; +} /*  */ +.octicon-file-media:before { + content: "\f012"; +} /*  */ +.octicon-file-pdf:before { + content: "\f014"; +} /*  */ +.octicon-file-submodule:before { + content: "\f017"; +} /*  */ +.octicon-file-symlink-directory:before { + content: "\f0b1"; +} /*  */ +.octicon-file-symlink-file:before { + content: "\f0b0"; +} /*  */ +.octicon-file-text:before { + content: "\f011"; +} /*  */ +.octicon-file-zip:before { + content: "\f013"; +} /*  */ +.octicon-flame:before { + content: "\f0d2"; +} /*  */ +.octicon-fold:before { + content: "\f0cc"; +} /*  */ +.octicon-gear:before { + content: "\f02f"; +} /*  */ +.octicon-gift:before { + content: "\f042"; +} /*  */ +.octicon-gist:before { + content: "\f00e"; +} /*  */ +.octicon-gist-secret:before { + content: "\f08c"; +} /*  */ +.octicon-git-branch-create:before, +.octicon-git-branch-delete:before, +.octicon-git-branch:before { + content: "\f020"; +} /*  */ +.octicon-git-commit:before { + content: "\f01f"; +} /*  */ +.octicon-git-compare:before { + content: "\f0ac"; +} /*  */ +.octicon-git-merge:before { + content: "\f023"; +} /*  */ +.octicon-git-pull-request-abandoned:before, +.octicon-git-pull-request:before { + content: "\f009"; +} /*  */ +.octicon-globe:before { + content: "\f0b6"; +} /*  */ +.octicon-graph:before { + content: "\f043"; +} /*  */ +.octicon-heart:before { + content: "\2665"; +} /* ♥ */ +.octicon-history:before { + content: "\f07e"; +} /*  */ +.octicon-home:before { + content: "\f08d"; +} /*  */ +.octicon-horizontal-rule:before { + content: "\f070"; +} /*  */ +.octicon-hubot:before { + content: "\f09d"; +} /*  */ +.octicon-inbox:before { + content: "\f0cf"; +} /*  */ +.octicon-info:before { + content: "\f059"; +} /*  */ +.octicon-issue-closed:before { + content: "\f028"; +} /*  */ +.octicon-issue-opened:before { + content: "\f026"; +} /*  */ +.octicon-issue-reopened:before { + content: "\f027"; +} /*  */ +.octicon-jersey:before { + content: "\f019"; +} /*  */ +.octicon-key:before { + content: "\f049"; +} /*  */ +.octicon-keyboard:before { + content: "\f00d"; +} /*  */ +.octicon-law:before { + content: "\f0d8"; +} /*  */ +.octicon-light-bulb:before { + content: "\f000"; +} /*  */ +.octicon-link:before { + content: "\f05c"; +} /*  */ +.octicon-link-external:before { + content: "\f07f"; +} /*  */ +.octicon-list-ordered:before { + content: "\f062"; +} /*  */ +.octicon-list-unordered:before { + content: "\f061"; +} /*  */ +.octicon-location:before { + content: "\f060"; +} /*  */ +.octicon-gist-private:before, +.octicon-mirror-private:before, +.octicon-git-fork-private:before, +.octicon-lock:before { + content: "\f06a"; +} /*  */ +.octicon-logo-github:before { + content: "\f092"; +} /*  */ +.octicon-mail:before { + content: "\f03b"; +} /*  */ +.octicon-mail-read:before { + content: "\f03c"; +} /*  */ +.octicon-mail-reply:before { + content: "\f051"; +} /*  */ +.octicon-mark-github:before { + content: "\f00a"; +} /*  */ +.octicon-markdown:before { + content: "\f0c9"; +} /*  */ +.octicon-megaphone:before { + content: "\f077"; +} /*  */ +.octicon-mention:before { + content: "\f0be"; +} /*  */ +.octicon-milestone:before { + content: "\f075"; +} /*  */ +.octicon-mirror-public:before, +.octicon-mirror:before { + content: "\f024"; +} /*  */ +.octicon-mortar-board:before { + content: "\f0d7"; +} /*  */ +.octicon-mute:before { + content: "\f080"; +} /*  */ +.octicon-no-newline:before { + content: "\f09c"; +} /*  */ +.octicon-octoface:before { + content: "\f008"; +} /*  */ +.octicon-organization:before { + content: "\f037"; +} /*  */ +.octicon-package:before { + content: "\f0c4"; +} /*  */ +.octicon-paintcan:before { + content: "\f0d1"; +} /*  */ +.octicon-pencil:before { + content: "\f058"; +} /*  */ +.octicon-person-add:before, +.octicon-person-follow:before, +.octicon-person:before { + content: "\f018"; +} /*  */ +.octicon-pin:before { + content: "\f041"; +} /*  */ +.octicon-plug:before { + content: "\f0d4"; +} /*  */ +.octicon-repo-create:before, +.octicon-gist-new:before, +.octicon-file-directory-create:before, +.octicon-file-add:before, +.octicon-plus:before { + content: "\f05d"; +} /*  */ +.octicon-primitive-dot:before { + content: "\f052"; +} /*  */ +.octicon-primitive-square:before { + content: "\f053"; +} /*  */ +.octicon-pulse:before { + content: "\f085"; +} /*  */ +.octicon-question:before { + content: "\f02c"; +} /*  */ +.octicon-quote:before { + content: "\f063"; +} /*  */ +.octicon-radio-tower:before { + content: "\f030"; +} /*  */ +.octicon-repo-delete:before, +.octicon-repo:before { + content: "\f001"; +} /*  */ +.octicon-repo-clone:before { + content: "\f04c"; +} /*  */ +.octicon-repo-force-push:before { + content: "\f04a"; +} /*  */ +.octicon-gist-fork:before, +.octicon-repo-forked:before { + content: "\f002"; +} /*  */ +.octicon-repo-pull:before { + content: "\f006"; +} /*  */ +.octicon-repo-push:before { + content: "\f005"; +} /*  */ +.octicon-rocket:before { + content: "\f033"; +} /*  */ +.octicon-rss:before { + content: "\f034"; +} /*  */ +.octicon-ruby:before { + content: "\f047"; +} /*  */ +.octicon-screen-full:before { + content: "\f066"; +} /*  */ +.octicon-screen-normal:before { + content: "\f067"; +} /*  */ +.octicon-search-save:before, +.octicon-search:before { + content: "\f02e"; +} /*  */ +.octicon-server:before { + content: "\f097"; +} /*  */ +.octicon-settings:before { + content: "\f07c"; +} /*  */ +.octicon-shield:before { + content: "\f0e1"; +} /*  */ +.octicon-log-in:before, +.octicon-sign-in:before { + content: "\f036"; +} /*  */ +.octicon-log-out:before, +.octicon-sign-out:before { + content: "\f032"; +} /*  */ +.octicon-squirrel:before { + content: "\f0b2"; +} /*  */ +.octicon-star-add:before, +.octicon-star-delete:before, +.octicon-star:before { + content: "\f02a"; +} /*  */ +.octicon-stop:before { + content: "\f08f"; +} /*  */ +.octicon-repo-sync:before, +.octicon-sync:before { + content: "\f087"; +} /*  */ +.octicon-tag-remove:before, +.octicon-tag-add:before, +.octicon-tag:before { + content: "\f015"; +} /*  */ +.octicon-telescope:before { + content: "\f088"; +} /*  */ +.octicon-terminal:before { + content: "\f0c8"; +} /*  */ +.octicon-three-bars:before { + content: "\f05e"; +} /*  */ +.octicon-thumbsdown:before { + content: "\f0db"; +} /*  */ +.octicon-thumbsup:before { + content: "\f0da"; +} /*  */ +.octicon-tools:before { + content: "\f031"; +} /*  */ +.octicon-trashcan:before { + content: "\f0d0"; +} /*  */ +.octicon-triangle-down:before { + content: "\f05b"; +} /*  */ +.octicon-triangle-left:before { + content: "\f044"; +} /*  */ +.octicon-triangle-right:before { + content: "\f05a"; +} /*  */ +.octicon-triangle-up:before { + content: "\f0aa"; +} /*  */ +.octicon-unfold:before { + content: "\f039"; +} /*  */ +.octicon-unmute:before { + content: "\f0ba"; +} /*  */ +.octicon-versions:before { + content: "\f064"; +} /*  */ +.octicon-watch:before { + content: "\f0e0"; +} /*  */ +.octicon-remove-close:before, +.octicon-x:before { + content: "\f081"; +} /*  */ +.octicon-zap:before { + content: "\26A1"; +} /* ⚡ */ diff --git a/xhiveframework/public/css/tree.css b/xhiveframework/public/css/tree.css new file mode 100644 index 0000000..8b216bc --- /dev/null +++ b/xhiveframework/public/css/tree.css @@ -0,0 +1,95 @@ +.tree { + padding: 15px; +} +.tree li { + list-style: none; + margin: 2px 0px; +} +ul.tree-children { + padding-left: 20px; +} +.tree-link { + cursor: pointer; + display: inline-block; + padding: 1px; +} +.tree-link .node-parent { + color: #8D99A6; + font-size: 14px; + width: 10px; + text-align: center; +} +.tree-link .node-leaf { + color: #d1d8dd; +} +.tree-link .node-parent, +.tree-link .node-leaf { + margin-right: 8px; +} +.tree-link.active i { + color: #5e64ff; +} +.tree-link.active a { + color: #8D99A6; +} +.tree-hover { + background-color: #F7FAFC; + min-height: 20px; + border: 1px solid #d1d8dd; +} +.tree-node-toolbar { + display: inline-block; + padding: 0px 5px; + margin-left: 15px; + margin-bottom: -4px; + margin-top: -8px; +} +@media (max-width: 767px) { + ul.tree-children { + padding-left: 10px; + } +} +.tree.with-skeleton, +.tree.with-skeleton .tree-node { + position: relative; +} +.tree.with-skeleton.opened::before, +.tree.with-skeleton .tree-node.opened::before, +.tree.with-skeleton:last-child::after, +.tree.with-skeleton .tree-node:last-child::after { + content: ''; + position: absolute; + top: 16px; + left: 8px; + height: calc(100% - 26px); + width: 1px; + background: #d1d8dd; + z-index: -1; +} +.tree.with-skeleton:last-child::after, +.tree.with-skeleton .tree-node:last-child::after { + top: 11px; + left: -13px; + height: calc(100% - 15px); + width: 3px; + background: #fff; +} +.tree.with-skeleton.opened > .tree-children > .tree-node > .tree-link::before, +.tree.with-skeleton .tree-node.opened > .tree-children > .tree-node > .tree-link::before { + content: ''; + position: absolute; + width: 15px; + height: 1px; + top: 10px; + left: -11px; + z-index: -1; + background: #d1d8dd; +} +.tree.with-skeleton.opened::before { + left: 23px; + top: 33px; + height: calc(100% - 67px); +} +.tree-link.active ~ .balance-area { + color: #36414C !important; +} diff --git a/xhiveframework/public/css/tree_grid.css b/xhiveframework/public/css/tree_grid.css new file mode 100644 index 0000000..dc26510 --- /dev/null +++ b/xhiveframework/public/css/tree_grid.css @@ -0,0 +1,15 @@ +.cell-title { + font-weight: bold; +} + +.cell-effort-driven { + text-align: center; +} + +.toggle { + height: 9px; + width: 9px; + display: inline-block; + visibility: visible; +} + diff --git a/xhiveframework/public/html/print_template.html b/xhiveframework/public/html/print_template.html new file mode 100644 index 0000000..a631c3a --- /dev/null +++ b/xhiveframework/public/html/print_template.html @@ -0,0 +1,46 @@ + + + + + + + + + {{ title }} + + + + + + + diff --git a/xhiveframework/public/icons/espresso/icons.svg b/xhiveframework/public/icons/espresso/icons.svg new file mode 100644 index 0000000..3cd91a3 --- /dev/null +++ b/xhiveframework/public/icons/espresso/icons.svg @@ -0,0 +1,1693 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xhiveframework/public/icons/social/facebook.svg b/xhiveframework/public/icons/social/facebook.svg new file mode 100644 index 0000000..9a0c336 --- /dev/null +++ b/xhiveframework/public/icons/social/facebook.svg @@ -0,0 +1,3 @@ + + + diff --git a/xhiveframework/public/icons/social/fair.svg b/xhiveframework/public/icons/social/fair.svg new file mode 100644 index 0000000..60c00dc --- /dev/null +++ b/xhiveframework/public/icons/social/fair.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/xhiveframework/public/icons/social/github.svg b/xhiveframework/public/icons/social/github.svg new file mode 100644 index 0000000..6d981ab --- /dev/null +++ b/xhiveframework/public/icons/social/github.svg @@ -0,0 +1,3 @@ + + + diff --git a/xhiveframework/public/icons/social/google.svg b/xhiveframework/public/icons/social/google.svg new file mode 100644 index 0000000..f58dbf0 --- /dev/null +++ b/xhiveframework/public/icons/social/google.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/xhiveframework/public/icons/social/google_drive.svg b/xhiveframework/public/icons/social/google_drive.svg new file mode 100644 index 0000000..e1a9378 --- /dev/null +++ b/xhiveframework/public/icons/social/google_drive.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/xhiveframework/public/icons/social/office_365.svg b/xhiveframework/public/icons/social/office_365.svg new file mode 100644 index 0000000..77c3559 --- /dev/null +++ b/xhiveframework/public/icons/social/office_365.svg @@ -0,0 +1,53 @@ + + + + Artboard + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xhiveframework/public/icons/social/salesforce.svg b/xhiveframework/public/icons/social/salesforce.svg new file mode 100644 index 0000000..e4ced76 --- /dev/null +++ b/xhiveframework/public/icons/social/salesforce.svg @@ -0,0 +1,3 @@ + + + diff --git a/xhiveframework/public/icons/social/xhiveframework.svg b/xhiveframework/public/icons/social/xhiveframework.svg new file mode 100644 index 0000000..92791e3 --- /dev/null +++ b/xhiveframework/public/icons/social/xhiveframework.svg @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/xhiveframework/public/icons/timeless/icon-check.svg b/xhiveframework/public/icons/timeless/icon-check.svg new file mode 100644 index 0000000..e94d089 --- /dev/null +++ b/xhiveframework/public/icons/timeless/icon-check.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/xhiveframework/public/icons/timeless/icons.svg b/xhiveframework/public/icons/timeless/icons.svg new file mode 100644 index 0000000..c9ed252 --- /dev/null +++ b/xhiveframework/public/icons/timeless/icons.svg @@ -0,0 +1,1009 @@ + diff --git a/xhiveframework/public/icons/timeless/message.svg b/xhiveframework/public/icons/timeless/message.svg new file mode 100644 index 0000000..b056762 --- /dev/null +++ b/xhiveframework/public/icons/timeless/message.svg @@ -0,0 +1,3 @@ + + + diff --git a/xhiveframework/public/icons/timeless/search.svg b/xhiveframework/public/icons/timeless/search.svg new file mode 100644 index 0000000..b99c657 --- /dev/null +++ b/xhiveframework/public/icons/timeless/search.svg @@ -0,0 +1,3 @@ + + + diff --git a/xhiveframework/public/icons/timeless/test.svg b/xhiveframework/public/icons/timeless/test.svg new file mode 100644 index 0000000..9563c82 --- /dev/null +++ b/xhiveframework/public/icons/timeless/test.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/xhiveframework/public/images/background.png b/xhiveframework/public/images/background.png new file mode 100644 index 0000000..45b91e9 Binary files /dev/null and b/xhiveframework/public/images/background.png differ diff --git a/xhiveframework/public/images/color-circle.png b/xhiveframework/public/images/color-circle.png new file mode 100644 index 0000000..1dce698 Binary files /dev/null and b/xhiveframework/public/images/color-circle.png differ diff --git a/xhiveframework/public/images/default-avatar.png b/xhiveframework/public/images/default-avatar.png new file mode 100644 index 0000000..b6413c4 Binary files /dev/null and b/xhiveframework/public/images/default-avatar.png differ diff --git a/xhiveframework/public/images/default-skin.svg b/xhiveframework/public/images/default-skin.svg new file mode 100755 index 0000000..9d5f0c6 --- /dev/null +++ b/xhiveframework/public/images/default-skin.svg @@ -0,0 +1 @@ +default-skin 2 \ No newline at end of file diff --git a/xhiveframework/public/images/fallback-thumbnail.jpg b/xhiveframework/public/images/fallback-thumbnail.jpg new file mode 100644 index 0000000..68a62d7 Binary files /dev/null and b/xhiveframework/public/images/fallback-thumbnail.jpg differ diff --git a/xhiveframework/public/images/form-builder.gif b/xhiveframework/public/images/form-builder.gif new file mode 100644 index 0000000..6c8e1aa Binary files /dev/null and b/xhiveframework/public/images/form-builder.gif differ diff --git a/xhiveframework/public/images/help/print-style-classic.png b/xhiveframework/public/images/help/print-style-classic.png new file mode 100644 index 0000000..e1817aa Binary files /dev/null and b/xhiveframework/public/images/help/print-style-classic.png differ diff --git a/xhiveframework/public/images/help/print-style-modern.png b/xhiveframework/public/images/help/print-style-modern.png new file mode 100644 index 0000000..57917de Binary files /dev/null and b/xhiveframework/public/images/help/print-style-modern.png differ diff --git a/xhiveframework/public/images/help/print-style-monochrome.png b/xhiveframework/public/images/help/print-style-monochrome.png new file mode 100644 index 0000000..ed0a301 Binary files /dev/null and b/xhiveframework/public/images/help/print-style-monochrome.png differ diff --git a/xhiveframework/public/images/help/print-style-standard.png b/xhiveframework/public/images/help/print-style-standard.png new file mode 100644 index 0000000..db1c370 Binary files /dev/null and b/xhiveframework/public/images/help/print-style-standard.png differ diff --git a/xhiveframework/public/images/leaflet/layers-2x.png b/xhiveframework/public/images/leaflet/layers-2x.png new file mode 100644 index 0000000..200c333 Binary files /dev/null and b/xhiveframework/public/images/leaflet/layers-2x.png differ diff --git a/xhiveframework/public/images/leaflet/layers.png b/xhiveframework/public/images/leaflet/layers.png new file mode 100644 index 0000000..1a72e57 Binary files /dev/null and b/xhiveframework/public/images/leaflet/layers.png differ diff --git a/xhiveframework/public/images/leaflet/leafletmarker-icon.png b/xhiveframework/public/images/leaflet/leafletmarker-icon.png new file mode 100644 index 0000000..950edf2 Binary files /dev/null and b/xhiveframework/public/images/leaflet/leafletmarker-icon.png differ diff --git a/xhiveframework/public/images/leaflet/leafletmarker-shadow.png b/xhiveframework/public/images/leaflet/leafletmarker-shadow.png new file mode 100644 index 0000000..9fd2979 Binary files /dev/null and b/xhiveframework/public/images/leaflet/leafletmarker-shadow.png differ diff --git a/xhiveframework/public/images/leaflet/lego.png b/xhiveframework/public/images/leaflet/lego.png new file mode 100644 index 0000000..f173107 Binary files /dev/null and b/xhiveframework/public/images/leaflet/lego.png differ diff --git a/xhiveframework/public/images/leaflet/marker-icon-2x.png b/xhiveframework/public/images/leaflet/marker-icon-2x.png new file mode 100644 index 0000000..e4abba3 Binary files /dev/null and b/xhiveframework/public/images/leaflet/marker-icon-2x.png differ diff --git a/xhiveframework/public/images/leaflet/marker-icon.png b/xhiveframework/public/images/leaflet/marker-icon.png new file mode 100644 index 0000000..950edf2 Binary files /dev/null and b/xhiveframework/public/images/leaflet/marker-icon.png differ diff --git a/xhiveframework/public/images/leaflet/marker-shadow.png b/xhiveframework/public/images/leaflet/marker-shadow.png new file mode 100644 index 0000000..9fd2979 Binary files /dev/null and b/xhiveframework/public/images/leaflet/marker-shadow.png differ diff --git a/xhiveframework/public/images/leaflet/spritesheet-2x.png b/xhiveframework/public/images/leaflet/spritesheet-2x.png new file mode 100644 index 0000000..c45231a Binary files /dev/null and b/xhiveframework/public/images/leaflet/spritesheet-2x.png differ diff --git a/xhiveframework/public/images/leaflet/spritesheet.png b/xhiveframework/public/images/leaflet/spritesheet.png new file mode 100644 index 0000000..97d71c6 Binary files /dev/null and b/xhiveframework/public/images/leaflet/spritesheet.png differ diff --git a/xhiveframework/public/images/leaflet/spritesheet.svg b/xhiveframework/public/images/leaflet/spritesheet.svg new file mode 100644 index 0000000..05feda6 --- /dev/null +++ b/xhiveframework/public/images/leaflet/spritesheet.svg @@ -0,0 +1,156 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xhiveframework/public/images/signature-placeholder.png b/xhiveframework/public/images/signature-placeholder.png new file mode 100644 index 0000000..e3ffdb8 Binary files /dev/null and b/xhiveframework/public/images/signature-placeholder.png differ diff --git a/xhiveframework/public/images/smiley.png b/xhiveframework/public/images/smiley.png new file mode 100644 index 0000000..80aaf48 Binary files /dev/null and b/xhiveframework/public/images/smiley.png differ diff --git a/xhiveframework/public/images/ui-states/404.png b/xhiveframework/public/images/ui-states/404.png new file mode 100644 index 0000000..1cbf7ee Binary files /dev/null and b/xhiveframework/public/images/ui-states/404.png differ diff --git a/xhiveframework/public/images/ui-states/empty-app-state.svg b/xhiveframework/public/images/ui-states/empty-app-state.svg new file mode 100644 index 0000000..b7e346f --- /dev/null +++ b/xhiveframework/public/images/ui-states/empty-app-state.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/xhiveframework/public/images/ui-states/empty.png b/xhiveframework/public/images/ui-states/empty.png new file mode 100644 index 0000000..de2a893 Binary files /dev/null and b/xhiveframework/public/images/ui-states/empty.png differ diff --git a/xhiveframework/public/images/ui-states/event-empty-state.svg b/xhiveframework/public/images/ui-states/event-empty-state.svg new file mode 100644 index 0000000..ef54e60 --- /dev/null +++ b/xhiveframework/public/images/ui-states/event-empty-state.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/xhiveframework/public/images/ui-states/grid-empty-state.svg b/xhiveframework/public/images/ui-states/grid-empty-state.svg new file mode 100644 index 0000000..8a4473d --- /dev/null +++ b/xhiveframework/public/images/ui-states/grid-empty-state.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/xhiveframework/public/images/ui-states/list-empty-state.svg b/xhiveframework/public/images/ui-states/list-empty-state.svg new file mode 100644 index 0000000..067fbf5 --- /dev/null +++ b/xhiveframework/public/images/ui-states/list-empty-state.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/xhiveframework/public/images/ui-states/notification-empty-state.svg b/xhiveframework/public/images/ui-states/notification-empty-state.svg new file mode 100644 index 0000000..0515fd6 --- /dev/null +++ b/xhiveframework/public/images/ui-states/notification-empty-state.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/xhiveframework/public/images/ui-states/search-empty-state.svg b/xhiveframework/public/images/ui-states/search-empty-state.svg new file mode 100644 index 0000000..4e1317f --- /dev/null +++ b/xhiveframework/public/images/ui-states/search-empty-state.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/xhiveframework/public/images/ui-states/success-color.png b/xhiveframework/public/images/ui-states/success-color.png new file mode 100644 index 0000000..39028d0 Binary files /dev/null and b/xhiveframework/public/images/ui-states/success-color.png differ diff --git a/xhiveframework/public/images/ui/ajax-loader.gif b/xhiveframework/public/images/ui/ajax-loader.gif new file mode 100644 index 0000000..148005d Binary files /dev/null and b/xhiveframework/public/images/ui/ajax-loader.gif differ diff --git a/xhiveframework/public/images/ui/bot.png b/xhiveframework/public/images/ui/bot.png new file mode 100644 index 0000000..02aeea0 Binary files /dev/null and b/xhiveframework/public/images/ui/bot.png differ diff --git a/xhiveframework/public/images/ui/bubble-tea-happy.svg b/xhiveframework/public/images/ui/bubble-tea-happy.svg new file mode 100644 index 0000000..3c4e0c7 --- /dev/null +++ b/xhiveframework/public/images/ui/bubble-tea-happy.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/xhiveframework/public/images/ui/bubble-tea-smile.svg b/xhiveframework/public/images/ui/bubble-tea-smile.svg new file mode 100644 index 0000000..457fd70 --- /dev/null +++ b/xhiveframework/public/images/ui/bubble-tea-smile.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/xhiveframework/public/images/ui/bubble-tea-sorry.svg b/xhiveframework/public/images/ui/bubble-tea-sorry.svg new file mode 100644 index 0000000..2f30583 --- /dev/null +++ b/xhiveframework/public/images/ui/bubble-tea-sorry.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/xhiveframework/public/images/up.png b/xhiveframework/public/images/up.png new file mode 100644 index 0000000..949027e Binary files /dev/null and b/xhiveframework/public/images/up.png differ diff --git a/xhiveframework/public/images/workflow-builder.gif b/xhiveframework/public/images/workflow-builder.gif new file mode 100644 index 0000000..0277c3e Binary files /dev/null and b/xhiveframework/public/images/workflow-builder.gif differ diff --git a/xhiveframework/public/images/xhiveframework-favicon.svg b/xhiveframework/public/images/xhiveframework-favicon.svg new file mode 100644 index 0000000..92791e3 --- /dev/null +++ b/xhiveframework/public/images/xhiveframework-favicon.svg @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/xhiveframework/public/images/xhiveframework-framework-logo.png b/xhiveframework/public/images/xhiveframework-framework-logo.png new file mode 100644 index 0000000..fa3e559 Binary files /dev/null and b/xhiveframework/public/images/xhiveframework-framework-logo.png differ diff --git a/xhiveframework/public/images/xhiveframework-framework-logo.svg b/xhiveframework/public/images/xhiveframework-framework-logo.svg new file mode 100644 index 0000000..92791e3 --- /dev/null +++ b/xhiveframework/public/images/xhiveframework-framework-logo.svg @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/xhiveframework/public/images/xhiveframework-logo.png b/xhiveframework/public/images/xhiveframework-logo.png new file mode 100644 index 0000000..fa3e559 Binary files /dev/null and b/xhiveframework/public/images/xhiveframework-logo.png differ diff --git a/xhiveframework/public/js/bootstrap-4-web.bundle.js b/xhiveframework/public/js/bootstrap-4-web.bundle.js new file mode 100644 index 0000000..f63136b --- /dev/null +++ b/xhiveframework/public/js/bootstrap-4-web.bundle.js @@ -0,0 +1,68 @@ +// multilevel dropdown +$(".dropdown-menu a.dropdown-toggle").on("click", function (e) { + e.preventDefault(); + e.stopImmediatePropagation(); + if (!$(this).next().hasClass("show")) { + $(this).parents(".dropdown-menu").first().find(".show").removeClass("show"); + } + var $subMenu = $(this).next(".dropdown-menu"); + $subMenu.toggleClass("show"); + + $(this) + .parents("li.nav-item.dropdown.show") + .on("hidden.bs.dropdown", function () { + $(".dropdown-submenu .show").removeClass("show"); + }); + + return false; +}); + +xhiveframework.get_modal = function (title, content) { + return $( + `` + ); +}; + +xhiveframework.ui.Dialog = class Dialog extends xhiveframework.ui.Dialog { + get_primary_btn() { + return this.$wrapper.find(".modal-footer .btn-primary"); + } + + get_secondary_btn() { + return this.$wrapper.find(".modal-footer .btn-secondary"); + } + + set_primary_action(label, click) { + this.$wrapper.find(".modal-footer").removeClass("hidden"); + return super.set_primary_action(label, click).removeClass("hidden"); + } + + set_secondary_action(click) { + return super.set_secondary_action(click).removeClass("hidden"); + } + + make() { + super.make(); + if (this.fields) { + this.$wrapper.find(".section-body").addClass("w-100"); + } + } +}; diff --git a/xhiveframework/public/js/controls.bundle.js b/xhiveframework/public/js/controls.bundle.js new file mode 100644 index 0000000..ddd9d61 --- /dev/null +++ b/xhiveframework/public/js/controls.bundle.js @@ -0,0 +1,4 @@ +import "air-datepicker/dist/js/datepicker.min.js"; +import "./xhiveframework/form/controls/datepicker_i18n.js"; +import "./xhiveframework/ui/capture.js"; +import "./xhiveframework/form/controls/control.js"; diff --git a/xhiveframework/public/js/data_import_tools.bundle.js b/xhiveframework/public/js/data_import_tools.bundle.js new file mode 100644 index 0000000..51f970a --- /dev/null +++ b/xhiveframework/public/js/data_import_tools.bundle.js @@ -0,0 +1 @@ +import "./xhiveframework/data_import"; diff --git a/xhiveframework/public/js/desk.bundle.js b/xhiveframework/public/js/desk.bundle.js new file mode 100644 index 0000000..a80324d --- /dev/null +++ b/xhiveframework/public/js/desk.bundle.js @@ -0,0 +1,110 @@ +import "./xhiveframework/provide.js"; +import "./xhiveframework/translate.js"; +import "./xhiveframework/class.js"; +import "./xhiveframework/polyfill.js"; +import "./xhiveframework/assets.js"; +import "./xhiveframework/format.js"; +import "./xhiveframework/form/formatters.js"; +import "./xhiveframework/dom.js"; +import "./xhiveframework/ui/messages.js"; +import "./xhiveframework/ui/keyboard.js"; +import "./xhiveframework/ui/colors.js"; +import "./xhiveframework/ui/sidebar.js"; +import "./xhiveframework/ui/link_preview.js"; + +import "./xhiveframework/request.js"; +import "./xhiveframework/socketio_client.js"; +import "./xhiveframework/utils/utils.js"; +import "./xhiveframework/event_emitter.js"; +import "./xhiveframework/router.js"; +import "./xhiveframework/router_history.js"; +import "./xhiveframework/defaults.js"; +import "./xhiveframework/roles_editor.js"; +import "./xhiveframework/module_editor.js"; +import "./xhiveframework/microtemplate.js"; + +import "./xhiveframework/ui/page.html"; +import "./xhiveframework/ui/page.js"; +import "./xhiveframework/ui/slides.js"; +// import "./xhiveframework/ui/onboarding_dialog.js"; +import "./xhiveframework/ui/find.js"; +import "./xhiveframework/ui/iconbar.js"; +import "./xhiveframework/form/layout.js"; +import "./xhiveframework/form/section.js"; +import "./xhiveframework/form/tab.js"; +import "./xhiveframework/form/column.js"; +import "./xhiveframework/ui/field_group.js"; +import "./xhiveframework/form/link_selector.js"; +import "./xhiveframework/form/multi_select_dialog.js"; +import "./xhiveframework/ui/dialog.js"; +import "./xhiveframework/ui/capture.js"; +import "./xhiveframework/ui/app_icon.js"; +import "./xhiveframework/ui/theme_switcher.js"; + +import "./xhiveframework/model/model.js"; +import "./xhiveframework/db.js"; +import "./xhiveframework/model/meta.js"; +import "./xhiveframework/model/sync.js"; +import "./xhiveframework/model/create_new.js"; +import "./xhiveframework/model/perm.js"; +import "./xhiveframework/model/workflow.js"; +import "./xhiveframework/model/user_settings.js"; + +import "./xhiveframework/utils/user.js"; +import "./xhiveframework/utils/common.js"; +import "./xhiveframework/utils/urllib.js"; +import "./xhiveframework/utils/pretty_date.js"; +import "./xhiveframework/utils/tools.js"; +import "./xhiveframework/utils/datetime.js"; +import "./xhiveframework/utils/number_format.js"; +import "./xhiveframework/utils/help.js"; +import "./xhiveframework/utils/help_links.js"; +import "./xhiveframework/utils/address_and_contact.js"; +import "./xhiveframework/utils/preview_email.js"; +import "./xhiveframework/utils/file_manager.js"; +import "./xhiveframework/utils/diffview"; +import "./xhiveframework/utils/datatable.js"; + +import "./xhiveframework/upload.js"; +import "./xhiveframework/ui/tree.js"; + +import "./xhiveframework/views/container.js"; +import "./xhiveframework/views/breadcrumbs.js"; +import "./xhiveframework/views/factory.js"; +import "./xhiveframework/views/pageview.js"; + +import "./xhiveframework/ui/toolbar/awesome_bar.js"; +// import "./xhiveframework/ui/toolbar/energy_points_notifications.js"; +import "./xhiveframework/ui/notifications/notifications.js"; +import "./xhiveframework/ui/toolbar/search.js"; +import "./xhiveframework/ui/toolbar/tag_utils.js"; +import "./xhiveframework/ui/toolbar/search.html"; +import "./xhiveframework/ui/toolbar/search_utils.js"; +import "./xhiveframework/ui/toolbar/about.js"; +import "./xhiveframework/ui/toolbar/navbar.html"; +import "./xhiveframework/ui/toolbar/toolbar.js"; +// import "./xhiveframework/ui/toolbar/notifications.js"; +import "./xhiveframework/views/communication.js"; +import "./xhiveframework/views/translation_manager.js"; +import "./xhiveframework/views/workspace/workspace.js"; +import "./xhiveframework/views/workspace/blocks/index.js"; + +import "./xhiveframework/widgets/widget_group.js"; + +import "./xhiveframework/ui/sort_selector.html"; +import "./xhiveframework/ui/sort_selector.js"; + +import "./xhiveframework/change_log.html"; +import "./xhiveframework/ui/workspace_loading_skeleton.html"; +import "./xhiveframework/ui/workspace_sidebar_loading_skeleton.html"; +import "./xhiveframework/desk.js"; +import "./xhiveframework/query_string.js"; + +// import "./xhiveframework/ui/comment.js"; + +import "./xhiveframework/utils/energy_point_utils.js"; +import "./xhiveframework/utils/dashboard_utils.js"; +import "./xhiveframework/ui/chart.js"; +import "./xhiveframework/ui/datatable.js"; +import "./xhiveframework/ui/driver.js"; +import "./xhiveframework/scanner"; diff --git a/xhiveframework/public/js/dialog.bundle.js b/xhiveframework/public/js/dialog.bundle.js new file mode 100644 index 0000000..d928ad9 --- /dev/null +++ b/xhiveframework/public/js/dialog.bundle.js @@ -0,0 +1,7 @@ +import "./xhiveframework/dom.js"; +import "./xhiveframework/form/formatters.js"; +import "./xhiveframework/form/layout.js"; +import "./xhiveframework/ui/field_group.js"; +import "./xhiveframework/form/link_selector.js"; +import "./xhiveframework/form/multi_select_dialog.js"; +import "./xhiveframework/ui/dialog.js"; diff --git a/xhiveframework/public/js/form.bundle.js b/xhiveframework/public/js/form.bundle.js new file mode 100644 index 0000000..c500940 --- /dev/null +++ b/xhiveframework/public/js/form.bundle.js @@ -0,0 +1,16 @@ +import "./xhiveframework/form/templates/address_list.html"; +import "./xhiveframework/form/templates/contact_list.html"; +import "./xhiveframework/form/templates/form_dashboard.html"; +import "./xhiveframework/form/templates/form_footer.html"; +import "./xhiveframework/form/templates/form_links.html"; +import "./xhiveframework/form/templates/form_sidebar.html"; +import "./xhiveframework/form/templates/print_layout.html"; +import "./xhiveframework/form/templates/report_links.html"; +import "./xhiveframework/form/templates/set_sharing.html"; +import "./xhiveframework/form/templates/timeline_message_box.html"; +import "./xhiveframework/form/templates/users_in_sidebar.html"; + +import "./xhiveframework/views/formview.js"; +import "./xhiveframework/form/form.js"; +import "./xhiveframework/meta_tag.js"; +import "./xhiveframework/doctype/"; diff --git a/xhiveframework/public/js/form_builder/FormBuilder.vue b/xhiveframework/public/js/form_builder/FormBuilder.vue new file mode 100644 index 0000000..63ed4ba --- /dev/null +++ b/xhiveframework/public/js/form_builder/FormBuilder.vue @@ -0,0 +1,282 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/AddFieldButton.vue b/xhiveframework/public/js/form_builder/components/AddFieldButton.vue new file mode 100644 index 0000000..0fce41f --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/AddFieldButton.vue @@ -0,0 +1,139 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Autocomplete.vue b/xhiveframework/public/js/form_builder/components/Autocomplete.vue new file mode 100644 index 0000000..c5bc0f2 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Autocomplete.vue @@ -0,0 +1,160 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Column.vue b/xhiveframework/public/js/form_builder/components/Column.vue new file mode 100644 index 0000000..3fbc67e --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Column.vue @@ -0,0 +1,185 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Dropdown.vue b/xhiveframework/public/js/form_builder/components/Dropdown.vue new file mode 100644 index 0000000..0c4b108 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Dropdown.vue @@ -0,0 +1,129 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/EditableInput.vue b/xhiveframework/public/js/form_builder/components/EditableInput.vue new file mode 100644 index 0000000..3459346 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/EditableInput.vue @@ -0,0 +1,89 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Field.vue b/xhiveframework/public/js/form_builder/components/Field.vue new file mode 100644 index 0000000..1b419f1 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Field.vue @@ -0,0 +1,214 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/FieldProperties.vue b/xhiveframework/public/js/form_builder/components/FieldProperties.vue new file mode 100644 index 0000000..b1f9f0c --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/FieldProperties.vue @@ -0,0 +1,126 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/SearchBox.vue b/xhiveframework/public/js/form_builder/components/SearchBox.vue new file mode 100644 index 0000000..2b22002 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/SearchBox.vue @@ -0,0 +1,34 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Section.vue b/xhiveframework/public/js/form_builder/components/Section.vue new file mode 100644 index 0000000..443642d --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Section.vue @@ -0,0 +1,390 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Sidebar.vue b/xhiveframework/public/js/form_builder/components/Sidebar.vue new file mode 100644 index 0000000..1ae9c0f --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Sidebar.vue @@ -0,0 +1,142 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/Tabs.vue b/xhiveframework/public/js/form_builder/components/Tabs.vue new file mode 100644 index 0000000..881891f --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/Tabs.vue @@ -0,0 +1,337 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/AttachControl.vue b/xhiveframework/public/js/form_builder/components/controls/AttachControl.vue new file mode 100644 index 0000000..6d8718d --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/AttachControl.vue @@ -0,0 +1,20 @@ + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/ButtonControl.vue b/xhiveframework/public/js/form_builder/components/controls/ButtonControl.vue new file mode 100644 index 0000000..00ff6a6 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/ButtonControl.vue @@ -0,0 +1,28 @@ + + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/CheckControl.vue b/xhiveframework/public/js/form_builder/components/controls/CheckControl.vue new file mode 100644 index 0000000..656c1b4 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/CheckControl.vue @@ -0,0 +1,51 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/CodeControl.vue b/xhiveframework/public/js/form_builder/components/controls/CodeControl.vue new file mode 100644 index 0000000..15b32ee --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/CodeControl.vue @@ -0,0 +1,70 @@ + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/DataControl.vue b/xhiveframework/public/js/form_builder/components/controls/DataControl.vue new file mode 100644 index 0000000..dbf4521 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/DataControl.vue @@ -0,0 +1,96 @@ + + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/FetchFromControl.vue b/xhiveframework/public/js/form_builder/components/controls/FetchFromControl.vue new file mode 100644 index 0000000..4635e7a --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/FetchFromControl.vue @@ -0,0 +1,100 @@ + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/GeolocationControl.vue b/xhiveframework/public/js/form_builder/components/controls/GeolocationControl.vue new file mode 100644 index 0000000..a7222c1 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/GeolocationControl.vue @@ -0,0 +1,40 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/ImageControl.vue b/xhiveframework/public/js/form_builder/components/controls/ImageControl.vue new file mode 100644 index 0000000..5e29787 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/ImageControl.vue @@ -0,0 +1,14 @@ + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/LinkControl.vue b/xhiveframework/public/js/form_builder/components/controls/LinkControl.vue new file mode 100644 index 0000000..f798194 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/LinkControl.vue @@ -0,0 +1,90 @@ + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/RatingControl.vue b/xhiveframework/public/js/form_builder/components/controls/RatingControl.vue new file mode 100644 index 0000000..28dd2cc --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/RatingControl.vue @@ -0,0 +1,50 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/SelectControl.vue b/xhiveframework/public/js/form_builder/components/controls/SelectControl.vue new file mode 100644 index 0000000..82e7478 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/SelectControl.vue @@ -0,0 +1,131 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/SignatureControl.vue b/xhiveframework/public/js/form_builder/components/controls/SignatureControl.vue new file mode 100644 index 0000000..f5d361e --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/SignatureControl.vue @@ -0,0 +1,31 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/TableControl.vue b/xhiveframework/public/js/form_builder/components/controls/TableControl.vue new file mode 100644 index 0000000..81ec82b --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/TableControl.vue @@ -0,0 +1,88 @@ + + + + + diff --git a/xhiveframework/public/js/form_builder/components/controls/TextControl.vue b/xhiveframework/public/js/form_builder/components/controls/TextControl.vue new file mode 100644 index 0000000..0947c27 --- /dev/null +++ b/xhiveframework/public/js/form_builder/components/controls/TextControl.vue @@ -0,0 +1,47 @@ + + + + + + + + diff --git a/xhiveframework/public/js/print_format_builder/PrintFormat.vue b/xhiveframework/public/js/print_format_builder/PrintFormat.vue new file mode 100644 index 0000000..a08286d --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/PrintFormat.vue @@ -0,0 +1,132 @@ + + + + + diff --git a/xhiveframework/public/js/print_format_builder/PrintFormatBuilder.vue b/xhiveframework/public/js/print_format_builder/PrintFormatBuilder.vue new file mode 100644 index 0000000..d436b4f --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/PrintFormatBuilder.vue @@ -0,0 +1,67 @@ + + + + + diff --git a/xhiveframework/public/js/print_format_builder/PrintFormatControls.vue b/xhiveframework/public/js/print_format_builder/PrintFormatControls.vue new file mode 100644 index 0000000..89b6e9e --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/PrintFormatControls.vue @@ -0,0 +1,314 @@ + + + + + diff --git a/xhiveframework/public/js/print_format_builder/PrintFormatSection.vue b/xhiveframework/public/js/print_format_builder/PrintFormatSection.vue new file mode 100644 index 0000000..f9d847c --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/PrintFormatSection.vue @@ -0,0 +1,224 @@ + + + + + diff --git a/xhiveframework/public/js/print_format_builder/print_format_builder.bundle.js b/xhiveframework/public/js/print_format_builder/print_format_builder.bundle.js new file mode 100644 index 0000000..989825e --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/print_format_builder.bundle.js @@ -0,0 +1,62 @@ +import { createApp, watch } from "vue"; +import PrintFormatBuilderComponent from "./PrintFormatBuilder.vue"; + +class PrintFormatBuilder { + constructor({ wrapper, page, print_format }) { + this.$wrapper = $(wrapper); + this.page = page; + this.print_format = print_format; + + this.page.clear_actions(); + this.page.clear_icons(); + this.page.clear_custom_actions(); + + this.page.set_title(__("Editing {0}", [this.print_format])); + this.page.set_primary_action(__("Save"), () => { + this.$component.$store.save_changes(); + }); + let $toggle_preview_btn = this.page.add_button(__("Show Preview"), () => { + this.$component.toggle_preview(); + }); + let $reset_changes_btn = this.page.add_button(__("Reset Changes"), () => + this.$component.$store.reset_changes() + ); + this.page.add_menu_item(__("Edit Print Format"), () => { + xhiveframework.set_route("Form", "Print Format", this.print_format); + }); + this.page.add_menu_item(__("Change Print Format"), () => { + xhiveframework.set_route("print-format-builder-beta"); + }); + + let app = createApp(PrintFormatBuilderComponent, { print_format_name: print_format }); + SetVueGlobals(app); + this.$component = app.mount(this.$wrapper.get(0)); + + watch( + () => this.$component.$store.dirty, + (dirty) => { + if (dirty.value) { + this.page.set_indicator(__("Not Saved"), "orange"); + $toggle_preview_btn.hide(); + $reset_changes_btn.show(); + } else { + this.page.clear_indicator(); + $toggle_preview_btn.show(); + $reset_changes_btn.hide(); + } + }, + { deep: true } + ); + + watch( + () => this.$component.show_preview, + (value) => { + $toggle_preview_btn.text(value ? __("Hide Preview") : __("Show Preview")); + } + ); + } +} + +xhiveframework.provide("xhiveframework.ui"); +xhiveframework.ui.PrintFormatBuilder = PrintFormatBuilder; +export default PrintFormatBuilder; diff --git a/xhiveframework/public/js/print_format_builder/store.js b/xhiveframework/public/js/print_format_builder/store.js new file mode 100644 index 0000000..5ffea90 --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/store.js @@ -0,0 +1,159 @@ +import { create_default_layout, pluck } from "./utils"; +import { watch, ref, inject, computed, nextTick } from "vue"; + +export function getStore(print_format_name) { + // variables + let letterhead_name = ref(null); + let print_format = ref(null); + let letterhead = ref(null); + let doctype = ref(null); + let meta = ref(null); + let layout = ref(null); + let dirty = ref(false); + let edit_letterhead = ref(false); + + // methods + function fetch() { + return new Promise((resolve) => { + xhiveframework.model.clear_doc("Print Format", print_format_name); + xhiveframework.model.with_doc("Print Format", print_format_name, () => { + let _print_format = xhiveframework.get_doc("Print Format", print_format_name); + xhiveframework.model.with_doctype(_print_format.doc_type, () => { + meta.value = xhiveframework.get_meta(_print_format.doc_type); + print_format.value = _print_format; + layout.value = get_layout(); + nextTick(() => (dirty.value = false)); + edit_letterhead.value = false; + resolve(); + }); + }); + }); + } + function update({ fieldname, value }) { + print_format.value[fieldname] = value; + } + function save_changes() { + xhiveframework.dom.freeze(__("Saving...")); + + layout.value.sections = layout.value.sections + .filter((section) => !section.remove) + .map((section) => { + section.columns = section.columns.map((column) => { + column.fields = column.fields + .filter((df) => !df.remove) + .map((df) => { + if (df.table_columns) { + df.table_columns = df.table_columns.map((tf) => { + return pluck(tf, [ + "label", + "fieldname", + "fieldtype", + "options", + "width", + "field_template", + ]); + }); + } + return pluck(df, [ + "label", + "fieldname", + "fieldtype", + "options", + "table_columns", + "html", + "field_template", + ]); + }); + return column; + }); + return section; + }); + + print_format.value.format_data = JSON.stringify(layout.value); + + xhiveframework + .call("xhiveframework.client.save", { + doc: print_format.value, + }) + .then(() => { + if (letterhead.value && letterhead.value._dirty) { + return xhiveframework + .call("xhiveframework.client.save", { + doc: letterhead.value, + }) + .then((r) => (letterhead.value = r.message)); + } + }) + .then(() => fetch()) + .always(() => { + xhiveframework.dom.unfreeze(); + }); + } + function reset_changes() { + fetch(); + } + function get_layout() { + if (print_format.value) { + if (typeof print_format.value.format_data == "string") { + return JSON.parse(print_format.value.format_data); + } + return print_format.value.format_data; + } + return null; + } + function get_default_layout() { + return create_default_layout(meta.value, print_format.value); + } + function change_letterhead(_letterhead) { + return xhiveframework.db.get_doc("Letter Head", _letterhead).then((doc) => { + letterhead.value = doc; + }); + } + + // watch + watch(layout, () => { + dirty.value = true; + }); + watch(print_format, () => { + dirty.value = true; + }); + + return { + letterhead_name, + print_format, + letterhead, + doctype, + meta, + layout, + dirty, + edit_letterhead, + fetch, + update, + save_changes, + reset_changes, + get_layout, + get_default_layout, + change_letterhead, + }; +} + +export function useStore() { + // inject store + let store = ref(inject("$store")); + + // computed + let print_format = computed(() => { + return store.value.print_format; + }); + let layout = computed(() => { + return store.value.layout; + }); + let letterhead = computed(() => { + return store.value.letterhead; + }); + let meta = computed(() => { + return store.value.meta; + }); + + return { print_format, layout, letterhead, meta, store }; +} diff --git a/xhiveframework/public/js/print_format_builder/utils.js b/xhiveframework/public/js/print_format_builder/utils.js new file mode 100644 index 0000000..bc5fe2c --- /dev/null +++ b/xhiveframework/public/js/print_format_builder/utils.js @@ -0,0 +1,152 @@ +export function create_default_layout(meta, print_format) { + let layout = { + header: get_default_header(meta), + sections: [], + }; + + let section = null, + column = null; + + function set_column(df) { + if (!section) { + set_section(); + } + column = get_new_column(df); + section.columns.push(column); + } + + function set_section(df) { + section = get_new_section(df); + column = null; + layout.sections.push(section); + } + + function get_new_section(df) { + if (!df) { + df = { label: "" }; + } + return { + label: df.label || "", + columns: [], + }; + } + + function get_new_column(df) { + if (!df) { + df = { label: "" }; + } + return { + label: df.label || "", + fields: [], + }; + } + + for (let df of meta.fields) { + if (df.fieldname) { + // make a copy to avoid mutation bugs + df = JSON.parse(JSON.stringify(df)); + } else { + continue; + } + + if (df.fieldtype === "Section Break") { + set_section(df); + } else if (df.fieldtype === "Column Break") { + set_column(df); + } else if (df.label) { + if (!column) set_column(); + + if (!df.print_hide) { + let field = { + label: df.label, + fieldname: df.fieldname, + fieldtype: df.fieldtype, + options: df.options, + }; + + let field_template = get_field_template(print_format, df.fieldname); + if (field_template) { + field.label = `${__(df.label, null, df.parent)} (${__("Field Template")})`; + field.fieldtype = "Field Template"; + field.field_template = field_template.name; + field.fieldname = df.fieldname = "_template"; + } + + if (df.fieldtype === "Table") { + field.table_columns = get_table_columns(df); + } + + column.fields.push(field); + section.has_fields = true; + } + } + } + + // remove empty sections + layout.sections = layout.sections.filter((section) => section.has_fields); + + return layout; +} + +export function get_table_columns(df) { + let table_columns = []; + let table_fields = xhiveframework.get_meta(df.options).fields; + let total_width = 0; + for (let tf of table_fields) { + if ( + !["Section Break", "Column Break"].includes(tf.fieldtype) && + !tf.print_hide && + df.label && + total_width < 100 + ) { + let width = + typeof tf.width == "number" && tf.width < 100 ? tf.width : tf.width ? 20 : 10; + table_columns.push({ + label: tf.label, + fieldname: tf.fieldname, + fieldtype: tf.fieldtype, + options: tf.options, + width, + }); + total_width += width; + } + } + return table_columns; +} + +function get_field_template(print_format, fieldname) { + let templates = print_format.__onload.print_templates || {}; + for (let template of templates) { + if (template.field === fieldname) { + return template; + } + } + return null; +} + +function get_default_header(meta) { + return `
      +

      ${meta.name}

      +

      {{ doc.name }}

      +
      `; +} + +export function pluck(object, keys) { + let out = {}; + for (let key of keys) { + if (key in object) { + out[key] = object[key]; + } + } + return out; +} + +export function get_image_dimensions(src) { + return new Promise((resolve) => { + let img = new Image(); + img.onload = function () { + resolve({ width: this.width, height: this.height }); + }; + img.src = src; + }); +} diff --git a/xhiveframework/public/js/report.bundle.js b/xhiveframework/public/js/report.bundle.js new file mode 100644 index 0000000..1b01674 --- /dev/null +++ b/xhiveframework/public/js/report.bundle.js @@ -0,0 +1,8 @@ +import "./xhiveframework/views/reports/report_factory.js"; +import "./xhiveframework/views/reports/report_view.js"; +import "./xhiveframework/views/reports/query_report.js"; +import "./xhiveframework/views/reports/print_grid.html"; +import "./xhiveframework/views/reports/print_tree.html"; +import "./xhiveframework/ui/group_by/group_by.html"; +import "./xhiveframework/ui/group_by/group_by.js"; +import "./xhiveframework/views/reports/report_utils.js"; diff --git a/xhiveframework/public/js/sentry.bundle.js b/xhiveframework/public/js/sentry.bundle.js new file mode 100644 index 0000000..3493861 --- /dev/null +++ b/xhiveframework/public/js/sentry.bundle.js @@ -0,0 +1,23 @@ +import * as Sentry from "@sentry/browser"; + +Sentry.init({ + dsn: xhiveframework.boot.sentry_dsn, + release: xhiveframework?.boot?.versions?.xhiveframework, + autoSessionTracking: false, + initialScope: { + // don't use xhiveframework.session.user, it's set much later and will fail because of async loading + user: { id: xhiveframework.boot.sitename }, + tags: { xhiveframework_user: xhiveframework.boot.user.name ?? "Unidentified" }, + }, + beforeSend(event, hint) { + // Check if it was caused by xhiveframework.throw() + if ( + hint.originalException instanceof Error && + hint.originalException.stack && + hint.originalException.stack.includes("xhiveframework.throw") + ) { + return null; + } + return event; + }, +}); diff --git a/xhiveframework/public/js/telemetry.bundle.js b/xhiveframework/public/js/telemetry.bundle.js new file mode 100644 index 0000000..250a1c5 --- /dev/null +++ b/xhiveframework/public/js/telemetry.bundle.js @@ -0,0 +1 @@ +import "./telemetry/index.js"; diff --git a/xhiveframework/public/js/telemetry/index.js b/xhiveframework/public/js/telemetry/index.js new file mode 100644 index 0000000..5b0c053 --- /dev/null +++ b/xhiveframework/public/js/telemetry/index.js @@ -0,0 +1,86 @@ +import "../lib/posthog.js"; + +class TelemetryManager { + constructor() { + this.enabled = false; + + this.project_id = xhiveframework.boot.posthog_project_id; + this.telemetry_host = xhiveframework.boot.posthog_host; + this.site_age = xhiveframework.boot.telemetry_site_age; + if (cint(xhiveframework.boot.enable_telemetry) && this.project_id && this.telemetry_host) { + this.enabled = true; + } + } + + initialize() { + if (!this.enabled) return; + let disable_decide = !this.should_record_session(); + try { + posthog.init(this.project_id, { + api_host: this.telemetry_host, + autocapture: false, + capture_pageview: false, + capture_pageleave: false, + advanced_disable_decide: disable_decide, + }); + posthog.identify(xhiveframework.boot.sitename); + this.send_heartbeat(); + this.register_pageview_handler(); + } catch (e) { + console.trace("Failed to initialize telemetry", e); + this.enabled = false; + } + } + + capture(event, app, props) { + if (!this.enabled) return; + posthog.capture(`${app}_${event}`, props); + } + + disable() { + this.enabled = false; + } + + can_enable() { + if (cint(navigator.doNotTrack)) { + return false; + } + let posthog_available = Boolean(this.telemetry_host && this.project_id); + let sentry_available = Boolean(xhiveframework.boot.sentry_dsn); + return posthog_available || sentry_available; + } + + send_heartbeat() { + const KEY = "ph_last_heartbeat"; + const now = xhiveframework.datetime.system_datetime(true); + const last = localStorage.getItem(KEY); + + if (!last || moment(now).diff(moment(last), "hours") > 12) { + localStorage.setItem(KEY, now.toISOString()); + this.capture("heartbeat", "xhiveframework", { xhiveframework_version: xhiveframework.boot?.versions?.xhiveframework }); + } + } + + register_pageview_handler() { + if (this.site_age && this.site_age > 6) { + return; + } + + xhiveframework.router.on("change", () => { + posthog.capture("$pageview"); + }); + } + + should_record_session() { + let start = xhiveframework.boot.sysdefaults.session_recording_start; + if (!start) return; + + let start_datetime = xhiveframework.datetime.str_to_obj(start); + let now = xhiveframework.datetime.now_datetime(); + // if user allowed recording only record for first 2 hours, never again. + return xhiveframework.datetime.get_minute_diff(now, start_datetime) < 120; + } +} + +xhiveframework.telemetry = new TelemetryManager(); +xhiveframework.telemetry.initialize(); diff --git a/xhiveframework/public/js/user_profile_controller.bundle.js b/xhiveframework/public/js/user_profile_controller.bundle.js new file mode 100644 index 0000000..1bf0b51 --- /dev/null +++ b/xhiveframework/public/js/user_profile_controller.bundle.js @@ -0,0 +1 @@ +import "../../desk/page/user_profile/user_profile_controller"; diff --git a/xhiveframework/public/js/video_player.bundle.js b/xhiveframework/public/js/video_player.bundle.js new file mode 100644 index 0000000..cd3f972 --- /dev/null +++ b/xhiveframework/public/js/video_player.bundle.js @@ -0,0 +1,3 @@ +import Plyr from "plyr/dist/plyr.polyfilled"; + +xhiveframework.Plyr = Plyr; diff --git a/xhiveframework/public/js/web_form.bundle.js b/xhiveframework/public/js/web_form.bundle.js new file mode 100644 index 0000000..6994d9c --- /dev/null +++ b/xhiveframework/public/js/web_form.bundle.js @@ -0,0 +1,7 @@ +import "./controls.bundle.js"; +import "./dialog.bundle.js"; +import "./lib/moment.js"; +import "./xhiveframework/ui/keyboard.js"; +import "./xhiveframework/utils/datetime.js"; +import "./xhiveframework/web_form/webform_script.js"; +import "./bootstrap-4-web.bundle.js"; diff --git a/xhiveframework/public/js/workflow_builder/WorkflowBuilder.vue b/xhiveframework/public/js/workflow_builder/WorkflowBuilder.vue new file mode 100644 index 0000000..1c54c49 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/WorkflowBuilder.vue @@ -0,0 +1,369 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/components/ActionNode.vue b/xhiveframework/public/js/workflow_builder/components/ActionNode.vue new file mode 100644 index 0000000..040b23a --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/ActionNode.vue @@ -0,0 +1,83 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/components/ConnectionLine.vue b/xhiveframework/public/js/workflow_builder/components/ConnectionLine.vue new file mode 100644 index 0000000..33ecbe5 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/ConnectionLine.vue @@ -0,0 +1,34 @@ + + diff --git a/xhiveframework/public/js/workflow_builder/components/Properties.vue b/xhiveframework/public/js/workflow_builder/components/Properties.vue new file mode 100644 index 0000000..f8a9fbe --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/Properties.vue @@ -0,0 +1,97 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/components/Sidebar.vue b/xhiveframework/public/js/workflow_builder/components/Sidebar.vue new file mode 100644 index 0000000..d83ab6d --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/Sidebar.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/components/StateNode.vue b/xhiveframework/public/js/workflow_builder/components/StateNode.vue new file mode 100644 index 0000000..373c7c1 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/StateNode.vue @@ -0,0 +1,90 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/components/TransitionEdge.vue b/xhiveframework/public/js/workflow_builder/components/TransitionEdge.vue new file mode 100644 index 0000000..b1a4e13 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/components/TransitionEdge.vue @@ -0,0 +1,110 @@ + + + + + diff --git a/xhiveframework/public/js/workflow_builder/globals.js b/xhiveframework/public/js/workflow_builder/globals.js new file mode 100644 index 0000000..0356bdc --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/globals.js @@ -0,0 +1,56 @@ +import AttachControl from "../form_builder/components/controls/AttachControl.vue"; +import ButtonControl from "../form_builder/components/controls/ButtonControl.vue"; +import CheckControl from "../form_builder/components/controls/CheckControl.vue"; +import CodeControl from "../form_builder/components/controls/CodeControl.vue"; +import DataControl from "../form_builder/components/controls/DataControl.vue"; +import GeolocationControl from "../form_builder/components/controls/GeolocationControl.vue"; +import ImageControl from "../form_builder/components/controls/ImageControl.vue"; +import LinkControl from "../form_builder/components/controls/LinkControl.vue"; +import RatingControl from "../form_builder/components/controls/RatingControl.vue"; +import SelectControl from "../form_builder/components/controls/SelectControl.vue"; +import SignatureControl from "../form_builder/components/controls/SignatureControl.vue"; +import TableControl from "../form_builder/components/controls/TableControl.vue"; +import TextControl from "../form_builder/components/controls/TextControl.vue"; +import TextEditorControl from "../form_builder/components/controls/TextEditorControl.vue"; + +export function registerGlobalComponents(app) { + app.component("AttachControl", AttachControl) + .component("AttachImageControl", AttachControl) + .component("AutocompleteControl", DataControl) + .component("BarcodeControl", DataControl) + .component("ButtonControl", ButtonControl) + .component("CheckControl", CheckControl) + .component("CodeControl", CodeControl) + .component("ColorControl", DataControl) + .component("CurrencyControl", DataControl) + .component("DataControl", DataControl) + .component("DateControl", DataControl) + .component("DatetimeControl", DataControl) + .component("DurationControl", DataControl) + .component("DynamicLinkControl", DataControl) + .component("FloatControl", DataControl) + .component("GeolocationControl", GeolocationControl) + .component("HeadingControl", ButtonControl) + .component("HTMLControl", DataControl) + .component("HTMLEditorControl", CodeControl) + .component("IconControl", DataControl) + .component("ImageControl", ImageControl) + .component("IntControl", DataControl) + .component("JSONControl", CodeControl) + .component("LinkControl", LinkControl) + .component("LongTextControl", TextControl) + .component("MarkdownEditorControl", CodeControl) + .component("PasswordControl", DataControl) + .component("PercentControl", DataControl) + .component("PhoneControl", DataControl) + .component("ReadOnlyControl", DataControl) + .component("RatingControl", RatingControl) + .component("SelectControl", SelectControl) + .component("SignatureControl", SignatureControl) + .component("SmallTextControl", TextControl) + .component("TableControl", TableControl) + .component("TableMultiSelectControl", DataControl) + .component("TextControl", TextControl) + .component("TextEditorControl", TextEditorControl) + .component("TimeControl", DataControl); +} diff --git a/xhiveframework/public/js/workflow_builder/store.js b/xhiveframework/public/js/workflow_builder/store.js new file mode 100644 index 0000000..3f69a0a --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/store.js @@ -0,0 +1,228 @@ +import { defineStore } from "pinia"; +import { ref } from "vue"; +import { get_workflow_elements, validate_transitions } from "./utils"; +import { useManualRefHistory, onKeyDown } from "@vueuse/core"; + +export const useStore = defineStore("workflow-builder-store", () => { + let workflow_name = ref(null); + let workflow_doc = ref(null); + let workflow_doc_fields = ref([]); + let workflow = ref({ elements: [], selected: null }); + let workflowfields = ref([]); + let statefields = ref([]); + let transitionfields = ref([]); + let ref_history = ref(null); + + async function fetch() { + await xhiveframework.model.clear_doc("Workflow", workflow_name.value); + await xhiveframework.model.with_doc("Workflow", workflow_name.value); + + workflow_doc.value = xhiveframework.get_doc("Workflow", workflow_name.value); + await xhiveframework.model.with_doctype(workflow_doc.value.document_type); + + if (!workflowfields.value.length) { + await xhiveframework.model.with_doctype("Workflow"); + workflowfields.value = xhiveframework.get_meta("Workflow").fields; + } + + if (!statefields.value.length) { + await xhiveframework.model.with_doctype("Workflow Document State"); + statefields.value = xhiveframework.get_meta("Workflow Document State").fields; + } + + if (!transitionfields.value.length) { + await xhiveframework.model.with_doctype("Workflow Transition"); + transitionfields.value = xhiveframework.get_meta("Workflow Transition").fields; + } + + if (!workflow_doc_fields.value.length) { + let doc_type = workflow_doc.value.document_type; + await xhiveframework.model.with_doctype(doc_type); + workflow_doc_fields.value = xhiveframework.meta + .get_docfields(doc_type, null, { + fieldtype: ["not in", xhiveframework.model.no_value_type], + }) + .sort((a, b) => { + if (a.label && b.label) { + return a.label.localeCompare(b.label); + } + }) + .map((df) => ({ + label: `${df.label || __("No Label")} (${df.fieldtype})`, + value: df.fieldname, + })); + } + + const workflow_data = + (workflow_doc.value.workflow_data && + typeof workflow_doc.value.workflow_data == "string" && + JSON.parse(workflow_doc.value.workflow_data)) || + []; + + workflow.value.elements = get_workflow_elements(workflow_doc.value, workflow_data); + + setup_undo_redo(); + setup_breadcrumbs(); + } + + function reset_changes() { + fetch(); + } + + async function save_changes() { + xhiveframework.dom.freeze(__("Saving...")); + + try { + let doc = workflow_doc.value; + doc.states = get_updated_states(); + doc.transitions = get_updated_transitions(); + validate_workflow(doc); + const workflow_data = clean_workflow_data(); + doc.workflow_data = JSON.stringify(workflow_data); + await xhiveframework.call("xhiveframework.client.save", { doc }); + xhiveframework.toast("Workflow updated successfully"); + fetch(); + } catch (e) { + console.error(e); + } finally { + xhiveframework.dom.unfreeze(); + } + } + + function validate_workflow(doc) { + if (doc.is_active && (!doc.states.length || !doc.transitions.length)) { + let message = "Workflow must have atleast one state and transition"; + xhiveframework.throw({ + message: __(message), + title: __("Missing Values Required"), + indicator: "orange", + }); + } + } + + function clean_workflow_data() { + return workflow.value.elements.map((el) => { + const { + selected, + dragging, + resizing, + data, + events, + initialized, + sourceNode, + targetNode, + ...obj + } = el; + + if (el.type == "action") { + obj.data = { + from_id: data.from_id, + to_id: data.to_id, + }; + } + + return obj; + }); + } + + function setup_breadcrumbs() { + let breadcrumbs = ` +
    • ${__("Workflow")}
    • +
    • ${__(workflow_name.value)}
    • +
    • ${__("Workflow Builder")}
    • + `; + xhiveframework.breadcrumbs.clear(); + xhiveframework.breadcrumbs.$breadcrumbs.append(breadcrumbs); + } + + function get_state_df(data) { + let doc_status_map = { + Draft: 0, + Submitted: 1, + Cancelled: 2, + }; + data.doc_status = doc_status_map[data.doc_status]; + return data; + } + + function get_updated_states() { + let states = []; + workflow.value.elements.forEach((element) => { + if (element.type == "state") { + element.data.workflow_builder_id = element.id; + states.push(get_state_df(element.data)); + } + }); + return states; + } + + function get_transition_df(data) { + return data; + } + + function get_updated_transitions() { + let transitions = []; + let actions = []; + + workflow.value.elements.forEach((element) => { + if (element.type == "action") { + element.data.workflow_builder_id = element.id; + actions.push(element); + } + }); + + actions.forEach((action) => { + let states = workflow.value.elements.filter((e) => e.type == "state"); + let state = states.find((state) => state.data.state == action.data.from); + let next_state = states.find((state) => state.data.state == action.data.to); + let error = validate_transitions(state.data, next_state.data); + if (error) { + xhiveframework.throw({ + message: error, + title: __("Invalid Transition"), + }); + } + transitions.push( + get_transition_df({ + ...action.data, + state: action.data.from, + next_state: action.data.to, + }) + ); + }); + + return transitions; + } + + let undo_redo_keyboard_event = () => + onKeyDown(true, (e) => { + if (!ref_history.value) return; + if (e.ctrlKey || e.metaKey) { + if (e.key === "z" && !e.shiftKey && ref_history.value.canUndo) { + ref_history.value.undo(); + } else if (e.key === "z" && e.shiftKey && ref_history.value.canRedo) { + ref_history.value.redo(); + } + } + }); + + function setup_undo_redo() { + ref_history.value = useManualRefHistory(workflow, { clone: true }); + undo_redo_keyboard_event(); + } + + return { + workflow_name, + workflow_doc, + workflow_doc_fields, + workflow, + workflowfields, + statefields, + transitionfields, + ref_history, + fetch, + reset_changes, + save_changes, + setup_undo_redo, + }; +}); diff --git a/xhiveframework/public/js/workflow_builder/utils.js b/xhiveframework/public/js/workflow_builder/utils.js new file mode 100644 index 0000000..6f1fcb9 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/utils.js @@ -0,0 +1,189 @@ +export function get_workflow_elements(workflow, workflow_data) { + let elements = []; + let states = {}; + let actions = {}; + let transitions = {}; + + let x = 150; + let y = 100; + + workflow_data.forEach((node) => { + if (node.type == "state") { + states[node.id] = node; + } else if (node.type == "action") { + actions[node.id] = node; + } else if (node.type == "transition") { + transitions[`edge-${node.source}-${node.target}`] = node; + + if (node.source.startsWith("action-")) { + const action = actions[node.source]; + if (!action.data.to_id) { + action.data.to_id = node.target; + } + node.sourceNode = action; + node.targetNode = states[node.target]; + } else { + const action = actions[node.target]; + if (!action.data.from_id) { + action.data.from_id = node.source; + } + node.targetNode = action; + node.sourceNode = states[node.source]; + } + } + }); + + function state_obj(id, data) { + let state = states[id]; + + if (state) { + state.data = data; + } else { + state = { + id: id.toString(), + type: "state", + position: { x, y }, + data, + }; + } + + Object.assign(state, { + initialized: true, + selected: false, + dragging: false, + resizing: false, + }); + return (states[id] = state); + } + + function action_obj(id, data, position) { + let action = actions[id]; + + if (action) { + data.from_id = action.data.from_id; + (data.to_id = action.data.to_id), (action.data = data); + } else { + action = { + id, + type: "action", + position, + data, + }; + } + + Object.assign(action, { + initialized: true, + selected: false, + dragging: false, + resizing: false, + }); + return (actions[id] = action); + } + + function transition_obj(id, source, target) { + let transition = transitions[id]; + + if (!transition) { + transition = { + id, + type: "transition", + source: source.toString(), + target: target.toString(), + sourceHandle: "right", + targetHandle: "left", + updatable: true, + animated: true, + }; + } + + Object.assign(transition, { + initialized: true, + selected: false, + dragging: false, + resizing: false, + }); + return (transitions[id] = transition); + } + + let state_id = Math.max(...workflow.states.map((state) => state.workflow_builder_id || 0)); + + workflow.states.forEach((state, i) => { + x += 400; + let doc_status_map = { + 0: "Draft", + 1: "Submitted", + 2: "Cancelled", + }; + + const id = state.workflow_builder_id || ++state_id; + elements.push( + state_obj(id, { + ...state, + doc_status: doc_status_map[state.doc_status], + }) + ); + }); + + let action_id = Math.max( + ...workflow.transitions.map( + (transition) => transition.workflow_builder_id?.replace("action-", "") || 0 + ) + ); + + workflow.transitions.forEach((transition, i) => { + const id = transition.workflow_builder_id || "action-" + ++action_id; + let action = actions[id]; + let source, target; + + if (action && action.data.from_id && action.data.to_id) { + source = states[action.data.from_id]; + target = states[action.data.to_id]; + } else { + source = Object.values(states).filter( + (state) => state.data?.state == transition.state + )[0]; + target = Object.values(states).filter( + (state) => state.data?.state == transition.next_state + )[0]; + } + + let position = { x: source.position.x + 250, y: y + 20 }; + let data = { + ...transition, + from_id: source.id, + to_id: target.id, + from: transition.state, + to: transition.next_state, + }; + + elements.push(action_obj(id, data, position)); + elements.push(transition_obj("edge-" + source.id + "-" + id, source.id, id)); + elements.push(transition_obj("edge-" + id + "-" + target.id, id, target.id)); + }); + + return elements; +} + +export function validate_transitions(state, next_state) { + let message; + if (state.doc_status == "Cancelled") { + message = __("Cannot change state of Cancelled Document ({0} State)", [ + state.state, + ]); + } + + if (state.doc_status == "Submitted" && next_state.doc_status == "Draft") { + message = __( + "Submitted document cannot be converted back to draft while transitioning from {0} State to {1} State", + [state.state, next_state.state] + ); + } + + if (state.doc_status == "Draft" && next_state.doc_status == "Cancelled") { + message = __( + "Cannot cancel before submitting while transitioning from {0} State to {1} State", + [state.state, next_state.state] + ); + } + return message; +} diff --git a/xhiveframework/public/js/workflow_builder/workflow_builder.bundle.js b/xhiveframework/public/js/workflow_builder/workflow_builder.bundle.js new file mode 100644 index 0000000..ab4d921 --- /dev/null +++ b/xhiveframework/public/js/workflow_builder/workflow_builder.bundle.js @@ -0,0 +1,69 @@ +import { createApp } from "vue"; +import { createPinia } from "pinia"; +import { useStore } from "./store"; +import WorkflowBuilderComponent from "./WorkflowBuilder.vue"; +import { registerGlobalComponents } from "./globals.js"; + +class WorkflowBuilder { + constructor({ wrapper, page, workflow }) { + this.$wrapper = $(wrapper); + this.page = page; + this.workflow = workflow; + + this.page.set_indicator("Beta", "orange"); + + this.init(); + } + + init() { + // set page title + this.page.set_title(__("Editing {0}", [this.workflow])); + + this.setup_page_actions(); + this.setup_app(); + } + + setup_page_actions() { + // clear actions + this.page.clear_actions(); + this.page.clear_menu(); + this.page.clear_custom_actions(); + + // setup page actions + this.primary_btn = this.page.set_primary_action(__("Save"), () => + this.store.save_changes() + ); + + this.reset_changes_btn = this.page.add_button(__("Reset Changes"), () => { + this.store.reset_changes(); + }); + + this.go_to_doctype_btn = this.page.add_menu_item(__("Go to Workflow"), () => + xhiveframework.set_route("Form", "Workflow", this.workflow) + ); + } + + setup_app() { + // create a pinia instance + let pinia = createPinia(); + + // create a vue instance + let app = createApp(WorkflowBuilderComponent, { workflow: this.workflow }); + SetVueGlobals(app); + app.use(pinia); + + // create a store + this.store = useStore(); + this.store.workflow_name = this.workflow; + + // register global components + registerGlobalComponents(app); + + // mount the app + this.$workflow_builder = app.mount(this.$wrapper.get(0)); + } +} + +xhiveframework.provide("xhiveframework.ui"); +xhiveframework.ui.WorkflowBuilder = WorkflowBuilder; +export default WorkflowBuilder; diff --git a/xhiveframework/public/js/xhiveframework-web.bundle.js b/xhiveframework/public/js/xhiveframework-web.bundle.js new file mode 100644 index 0000000..0bcebfc --- /dev/null +++ b/xhiveframework/public/js/xhiveframework-web.bundle.js @@ -0,0 +1,26 @@ +import "./libs.bundle.js"; +import "./xhiveframework/class.js"; +import "./xhiveframework/polyfill.js"; +import "./xhiveframework/provide.js"; +import "./xhiveframework/translate.js"; +import "./xhiveframework/form/formatters.js"; +import "./xhiveframework/format.js"; +import "./xhiveframework/utils/number_format.js"; +import "./xhiveframework/utils/utils.js"; +import "./xhiveframework/utils/common.js"; +import "./xhiveframework/ui/messages.js"; +import "./xhiveframework/utils/pretty_date.js"; +import "./xhiveframework/utils/datetime.js"; +import "./xhiveframework/microtemplate.js"; +import "./xhiveframework/query_string.js"; + +import "./xhiveframework/upload.js"; + +import "./xhiveframework/model/meta.js"; +import "./xhiveframework/model/model.js"; +import "./xhiveframework/model/perm.js"; + +import "./bootstrap-4-web.bundle"; + +import "../../website/js/website.js"; +import "./xhiveframework/socketio_client.js"; diff --git a/xhiveframework/public/js/xhiveframework/assets.js b/xhiveframework/public/js/xhiveframework/assets.js new file mode 100644 index 0000000..f84285c --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/assets.js @@ -0,0 +1,213 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +// library to mange assets (js, css, models, html) etc in the app. +// will try and get from localStorage if latest are available +// depends on xhiveframework.versions to manage versioning + +xhiveframework.require = function (items, callback) { + if (typeof items === "string") { + items = [items]; + } + items = items.map((item) => xhiveframework.assets.bundled_asset(item)); + + return new Promise((resolve) => { + xhiveframework.assets.execute(items, () => { + resolve(); + callback && callback(); + }); + }); +}; + +xhiveframework.assets = { + check: function () { + // if version is different then clear localstorage + if (window._version_number != localStorage.getItem("_version_number")) { + xhiveframework.assets.clear_local_storage(); + console.log("Cleared App Cache."); + } + + if (localStorage._last_load) { + let not_updated_since = new Date() - new Date(localStorage._last_load); + // Evict cache every 2 days + // Evict cache if page is reloaded within 10 seconds. Which could be user trying to + // refresh if things feel broken. + if ((not_updated_since < 5000 && is_reload()) || not_updated_since > 2 * 86400000) { + xhiveframework.assets.clear_local_storage(); + } + } else { + xhiveframework.assets.clear_local_storage(); + } + + xhiveframework.assets.init_local_storage(); + }, + + init_local_storage: function () { + localStorage._last_load = new Date(); + localStorage._version_number = window._version_number; + if (xhiveframework.boot) localStorage.metadata_version = xhiveframework.boot.metadata_version; + }, + + clear_local_storage: function () { + $.each( + ["_last_load", "_version_number", "metadata_version", "page_info", "last_visited"], + function (i, key) { + localStorage.removeItem(key); + } + ); + + // clear assets + for (var key in localStorage) { + if ( + key.indexOf("desk_assets:") === 0 || + key.indexOf("_page:") === 0 || + key.indexOf("_doctype:") === 0 || + key.indexOf("preferred_breadcrumbs:") === 0 + ) { + localStorage.removeItem(key); + } + } + console.log("localStorage cleared"); + }, + + // keep track of executed assets + executed_: [], + + // pass on to the handler to set + execute: function (items, callback) { + var to_fetch = []; + for (var i = 0, l = items.length; i < l; i++) { + if (!xhiveframework.assets.exists(items[i])) { + to_fetch.push(items[i]); + } + } + if (to_fetch.length) { + xhiveframework.assets.fetch(to_fetch, function () { + xhiveframework.assets.eval_assets(items, callback); + }); + } else { + xhiveframework.assets.eval_assets(items, callback); + } + }, + + eval_assets: function (items, callback) { + for (var i = 0, l = items.length; i < l; i++) { + // execute js/css if not already. + var path = items[i]; + if (xhiveframework.assets.executed_.indexOf(path) === -1) { + // execute + xhiveframework.assets.handler[xhiveframework.assets.extn(path)](xhiveframework.assets.get(path), path); + xhiveframework.assets.executed_.push(path); + } + } + callback && callback(); + }, + + // check if the asset exists in + // localstorage + exists: function (src) { + if (xhiveframework.assets.executed_.indexOf(src) !== -1) { + return true; + } + if (xhiveframework.boot.developer_mode) { + return false; + } + if (xhiveframework.assets.get(src)) { + return true; + } else { + return false; + } + }, + + // load an asset via + fetch: function (items, callback) { + // this is virtual page load, only get the the source + // *without* the template + + if (items.length === 0) { + callback(); + return; + } + + const version_string = + xhiveframework.boot.developer_mode || window.dev_server ? Date.now() : window._version_number; + + async function fetch_item(item) { + // Add the version to the URL to bust the cache for non-bundled assets + let url = new URL(item, window.location.origin); + + if (!item.includes(".bundle.") && !url.searchParams.get("v")) { + url.searchParams.append("v", version_string); + } + const response = await fetch(url.toString()); + const body = await response.text(); + xhiveframework.assets.add(item, body); + } + + xhiveframework.dom.freeze(); + const fetch_promises = items.map(fetch_item); + Promise.all(fetch_promises).then(() => { + xhiveframework.dom.unfreeze(); + callback(); + }); + }, + + add: function (src, txt) { + if ("localStorage" in window) { + try { + xhiveframework.assets.set(src, txt); + } catch (e) { + // if quota is exceeded, clear local storage and set item + xhiveframework.assets.clear_local_storage(); + xhiveframework.assets.set(src, txt); + } + } + }, + + get: function (src) { + return localStorage.getItem("desk_assets:" + src); + }, + + set: function (src, txt) { + localStorage.setItem("desk_assets:" + src, txt); + }, + + extn: function (src) { + if (src.indexOf("?") != -1) { + src = src.split("?").slice(-1)[0]; + } + return src.split(".").slice(-1)[0]; + }, + + handler: { + js: function (txt, src) { + xhiveframework.dom.eval(txt); + }, + css: function (txt, src) { + xhiveframework.dom.set_style(txt); + }, + }, + + bundled_asset(path, is_rtl = null) { + if (!path.startsWith("/assets") && path.includes(".bundle.")) { + if (path.endsWith(".css") && is_rtl) { + path = `rtl_${path}`; + } + path = xhiveframework.boot.assets_json[path] || path; + return path; + } + return path; + }, +}; + +function is_reload() { + try { + return window.performance + ?.getEntriesByType("navigation") + .map((nav) => nav.type) + .includes("reload"); + } catch (e) { + // Safari probably + return true; + } +} diff --git a/xhiveframework/public/js/xhiveframework/build_events/BuildError.vue b/xhiveframework/public/js/xhiveframework/build_events/BuildError.vue new file mode 100644 index 0000000..1eb20bf --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/build_events/BuildError.vue @@ -0,0 +1,107 @@ + + + + + diff --git a/xhiveframework/public/js/xhiveframework/build_events/BuildSuccess.vue b/xhiveframework/public/js/xhiveframework/build_events/BuildSuccess.vue new file mode 100644 index 0000000..2fa3848 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/build_events/BuildSuccess.vue @@ -0,0 +1,56 @@ + + + + + diff --git a/xhiveframework/public/js/xhiveframework/build_events/build_events.bundle.js b/xhiveframework/public/js/xhiveframework/build_events/build_events.bundle.js new file mode 100644 index 0000000..d24be62 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/build_events/build_events.bundle.js @@ -0,0 +1,66 @@ +import { createApp } from "vue"; +import BuildError from "./BuildError.vue"; +import BuildSuccess from "./BuildSuccess.vue"; + +let $container = $("#build-events-overlay"); +let success = null; +let error = null; + +xhiveframework.realtime.on("build_event", (data) => { + if (data.success) { + // remove executed cache for rebuilt files + let changed_files = data.changed_files; + if (Array.isArray(changed_files)) { + for (let file of changed_files) { + if (file.includes(".bundle.")) { + let parts = file.split(".bundle."); + if (parts.length === 2) { + let filename = parts[0].split("/").slice(-1)[0]; + + xhiveframework.assets.executed_ = xhiveframework.assets.executed_.filter( + (asset) => !asset.includes(`${filename}.bundle`) + ); + } + } + } + } + // update assets json + xhiveframework.call("xhiveframework.sessions.get_boot_assets_json").then((r) => { + if (r.message) { + xhiveframework.boot.assets_json = r.message; + + if (xhiveframework.hot_update) { + xhiveframework.hot_update.forEach((callback) => { + callback(); + }); + } + } + }); + show_build_success(data); + } else if (data.error) { + show_build_error(data); + } +}); + +function show_build_success(data) { + if (error) { + error.hide(); + } + + if (!success) { + let target = $('
      ').appendTo($container).get(0); + success = createApp(BuildSuccess).mount(target); + } + success.show(data); +} + +function show_build_error(data) { + if (success) { + success.hide(); + } + if (!error) { + let target = $('
      ').appendTo($container).get(0); + error = createApp(BuildError).mount(target); + } + error.show(data); +} diff --git a/xhiveframework/public/js/xhiveframework/change_log.html b/xhiveframework/public/js/xhiveframework/change_log.html new file mode 100644 index 0000000..10e8c5d --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/change_log.html @@ -0,0 +1,18 @@ +{% for (var i=0, l=change_log.length; i < l; i++) { + var app_info = change_log[i]; %} + {% if (i > 0) { %}
      {% } %} +
      +

      + {{ app_info.title }} + {{ __("updated to {0}", [app_info.version]) }} +

      +
      + {% for (var x=0, y=app_info.change_log.length; x < y; x++) { + var version_info = app_info.change_log[x]; + if(version_info) { %} +

      {{ xhiveframework.markdown(version_info[1]) }}

      + {% } + } %} +
      +
      +{% } %} diff --git a/xhiveframework/public/js/xhiveframework/class.js b/xhiveframework/public/js/xhiveframework/class.js new file mode 100644 index 0000000..56665ee --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/class.js @@ -0,0 +1,92 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +/* + +Inheritence "Class" +------------------- +see: http://ejohn.org/blog/simple-javascript-inheritance/ +To subclass, use: + + var MyClass = Class.extend({ + init: function + }) + +*/ +// https://stackoverflow.com/a/15052240/5353542 + +/* Simple JavaScript Inheritance for ES 5.1 + * based on http://ejohn.org/blog/simple-javascript-inheritance/ + * (inspired by base2 and Prototype) + * MIT Licensed. + */ +(function (global) { + "use strict"; + var fnTest = /xyz/.test(function () { + xyz; + }) + ? /\b_super\b/ + : /.*/; + + // The base Class implementation (does nothing) + function Class() {} + + // Create a new Class that inherits from this class + Class.extend = function (props) { + var _super = this.prototype; + + // Set up the prototype to inherit from the base class + // (but without running the init constructor) + var proto = Object.create(_super); + + // Copy the properties over onto the new prototype + for (var name in props) { + // Check if we're overwriting an existing function + proto[name] = + typeof props[name] === "function" && + typeof _super[name] == "function" && + fnTest.test(props[name]) + ? (function (name, fn) { + return function () { + var tmp = this._super; + + // Add a new ._super() method that is the same method + // but on the super-class + this._super = _super[name]; + + // The method only need to be bound temporarily, so we + // remove it when we're done executing + var ret = fn.apply(this, arguments); + this._super = tmp; + + return ret; + }; + })(name, props[name]) + : props[name]; + } + + // The new constructor + var newClass = + typeof proto.init === "function" + ? proto.hasOwnProperty("init") + ? proto.init // All construction is actually done in the init method + : function SubClass() { + _super.init.apply(this, arguments); + } + : function EmptyClass() {}; + + // Populate our constructed prototype object + newClass.prototype = proto; + + // Enforce the constructor to be what we expect + proto.constructor = newClass; + + // And make this class extendable + newClass.extend = Class.extend; + + return newClass; + }; + + // export + global.Class = Class; +})(window); diff --git a/xhiveframework/public/js/xhiveframework/color_picker/color_picker.js b/xhiveframework/public/js/xhiveframework/color_picker/color_picker.js new file mode 100644 index 0000000..73a260e --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/color_picker/color_picker.js @@ -0,0 +1,203 @@ +import utils from "./utils"; + +class Picker { + constructor(opts) { + this.parent = opts.parent; + this.width = opts.width; + this.height = opts.height; + this.set_color(opts.color); + this.swatches = opts.swatches; + this.setup_picker(); + } + + refresh() { + this.set_selector_position(true); + this.update_color_map(); + } + + setup_picker() { + let color_picker_template = document.createElement("template"); + color_picker_template.innerHTML = ` +
      +
      + ${__("SWATCHES")}
      +
      +
      + ${__("COLOR PICKER")}
      +
      +
      +
      +
      +
      +
      +
      + `.trim(); + this.color_picker_wrapper = + color_picker_template.content.firstElementChild.cloneNode(true); + this.parent.appendChild(this.color_picker_wrapper); + this.color_map = this.color_picker_wrapper.getElementsByClassName("color-map")[0]; + this.color_selector_circle = this.color_map.getElementsByClassName("color-selector")[0]; + this.hue_map = this.color_picker_wrapper.getElementsByClassName("hue-map")[0]; + this.swatches_wrapper = this.color_picker_wrapper.getElementsByClassName("swatches")[0]; + this.hue_selector_circle = this.hue_map.getElementsByClassName("hue-selector")[0]; + this.refresh(); + this.setup_events(); + this.setup_swatches(); + } + + setup_events() { + this.setup_hue_event(); + this.setup_color_event(); + } + + setup_swatches() { + let swatch_template = document.createElement("template"); + swatch_template.innerHTML = '
      '; + this.swatches.forEach((color) => { + let swatch = swatch_template.content.firstElementChild.cloneNode(true); + this.swatches_wrapper.appendChild(swatch); + const set_values = () => { + this.set_color(color); + this.set_selector_position(); + this.update_color_map(); + }; + swatch.addEventListener("click", () => { + set_values(); + }); + swatch.onkeydown = (e) => { + const key_code = e.keyCode; + if ([13, 32].includes(key_code)) { + e.preventDefault(); + set_values(); + } + }; + swatch.style.backgroundColor = color; + }); + } + + set_selector_position(silent) { + this.hue = utils.get_hue(this.get_color()); + this.color_selector_position = this.get_pointer_coords(); + this.hue_selector_position = { + x: (this.hue * this.hue_map.offsetWidth) / 360, + y: this.hue_map.offsetHeight / 2, + }; + this.update_color_selector(silent); + this.update_hue_selector(silent); + } + + setup_color_event() { + let on_drag = (x, y) => { + this.color_selector_position.x = x; + this.color_selector_position.y = y; + this.update_color(); + this.update_color_selector(); + }; + this.setup_drag_event(this.color_map, on_drag); + } + + update_color() { + let x = this.color_selector_position.x; + let y = this.color_selector_position.y; + let w = this.color_map.offsetWidth; + let h = this.color_map.offsetHeight; + let color = utils.hsv_to_hex( + this.hue, + Math.round((x / w) * 100), + Math.round((1 - y / h) * 100) + ); + this.set_color(color); + } + + update_color_selector(silent) { + let x = this.color_selector_position.x; + let y = this.color_selector_position.y; + // set color selector position and background + this.color_selector_circle.style.top = + y - this.color_selector_circle.offsetHeight / 2 + "px"; + this.color_selector_circle.style.left = + x - this.color_selector_circle.offsetWidth / 2 + "px"; + this.color_map.style.color = this.get_color(); + !silent && this.on_change && this.on_change(this.get_color()); + } + + setup_hue_event() { + let on_drag = (x, y) => { + this.hue_selector_position.x = x; + this.hue = Math.round((x * 360) / this.hue_map.offsetWidth); + this.update_color_map(); + this.update_hue_selector(); + this.update_color(); + this.update_color_selector(); + }; + this.setup_drag_event(this.hue_map, on_drag); + } + + update_color_map() { + this.color_map.style.background = ` + linear-gradient(0deg, black, transparent), + linear-gradient(90deg, white, transparent), + hsl(${this.hue}, 100%, 50%) + `; + } + + update_hue_selector() { + let x = this.hue_selector_position.x - 1; + let y = this.hue_map.offsetHeight / 2 - 1; + // set color selector position and background + this.hue_selector_circle.style.top = y - this.hue_selector_circle.offsetHeight / 2 + "px"; + this.hue_selector_circle.style.left = x - this.hue_selector_circle.offsetWidth / 2 + "px"; + this.hue_map.style.color = `hsl(${this.hue}, 100%, 50%)`; + } + + get_pointer_coords() { + let h, s, v; + [h, s, v] = utils.get_hsv(this.get_color()); + let width = this.color_map.offsetWidth; + let height = this.color_map.offsetHeight; + let x = utils.clamp(0, (s * width) / 100, width); + let y = utils.clamp(0, (1 - v / 100) * height, height); + return { x, y }; + } + + setup_drag_event(element, callback) { + let on_drag = (event, force) => { + if (element.drag_enabled || force) { + event.preventDefault(); + event = event.touches ? event.touches[0] : event; + let element_bounds = element.getBoundingClientRect(); + let x = event.clientX - element_bounds.left; + let y = event.clientY - element_bounds.top; + x = utils.clamp(0, x, element_bounds.width); + y = utils.clamp(0, y, element_bounds.height); + callback(x, y); + } + }; + + element.addEventListener("mousedown", () => (element.drag_enabled = true)); + document.addEventListener("mouseup", () => (element.drag_enabled = false)); + document.addEventListener("mousemove", on_drag); + element.addEventListener("click", (event) => on_drag(event, true)); + + element.addEventListener("touchstart", () => (element.drag_enabled = true)); + element.addEventListener("touchend", () => (element.drag_enabled = false)); + element.addEventListener("touchcancel", () => (element.drag_enabled = false)); + element.addEventListener("touchmove", (event) => { + if (event.touches.length == 1) { + on_drag(event); + } else { + element.drag_enabled = false; + } + }); + } + + set_color(color) { + this.color = color || "#ffffff"; + } + + get_color() { + return this.color || "#ffffff"; + } +} + +export default Picker; diff --git a/xhiveframework/public/js/xhiveframework/color_picker/utils.js b/xhiveframework/public/js/xhiveframework/color_picker/utils.js new file mode 100644 index 0000000..70dfee2 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/color_picker/utils.js @@ -0,0 +1,115 @@ +export default { + get_hue(rgb) { + return this.get_hsv(rgb)[0]; + }, + + get_hsv(rgb) { + if (typeof rgb === "string") { + if (rgb.startsWith("#")) { + rgb = this.hex_to_rgb_values(rgb); + } else { + rgb = this.get_rgb_values(rgb); + } + } + return this.rgb_to_hsv(...rgb); + }, + + get_rgb_values(rgb_string) { + return rgb_string + .replace(/[()rgb\s]/g, "") + .split(",") + .map((s) => parseInt(s)); + }, + + rgb_to_hsv(r, g, b) { + r /= 255; + g /= 255; + b /= 255; + + let max = Math.max(r, g, b), + min = Math.min(r, g, b); + let h, + s, + v = max; + + let d = max - min; + s = max == 0 ? 0 : d / max; + + if (max == min) { + h = 0; // achromatic + } else { + switch (max) { + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + } + + h /= 6; + } + return [Math.round(h * 360), Math.round(s * 100), Math.round(v * 100)]; + }, + + component_to_hex(c) { + const hex = c.toString(16); + return hex.length == 1 ? "0" + hex : hex; + }, + + rgb_to_hex(r, g, b) { + return ( + "#" + this.component_to_hex(r) + this.component_to_hex(g) + this.component_to_hex(b) + ); + }, + + hex_to_rgb_values(hex) { + const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)]; + }, + + hsv_to_hex(h, s, v) { + s /= 100; + v /= 100; + h /= 360; + + let r, g, b; + let i = Math.floor(h * 6); + let f = h * 6 - i; + let p = v * (1 - s); + let q = v * (1 - f * s); + let t = v * (1 - (1 - f) * s); + + switch (i % 6) { + case 0: + (r = v), (g = t), (b = p); + break; + case 1: + (r = q), (g = v), (b = p); + break; + case 2: + (r = p), (g = v), (b = t); + break; + case 3: + (r = p), (g = q), (b = v); + break; + case 4: + (r = t), (g = p), (b = v); + break; + case 5: + (r = v), (g = p), (b = q); + break; + } + r = Math.round(r * 255); + g = Math.round(g * 255); + b = Math.round(b * 255); + return this.rgb_to_hex(r, g, b); + }, + + clamp(min, val, max) { + return val > max ? max : val < min ? min : val; + }, +}; diff --git a/xhiveframework/public/js/xhiveframework/data_import/data_exporter.js b/xhiveframework/public/js/xhiveframework/data_import/data_exporter.js new file mode 100644 index 0000000..684cb28 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/data_import/data_exporter.js @@ -0,0 +1,353 @@ +xhiveframework.provide("xhiveframework.data_import"); + +xhiveframework.data_import.DataExporter = class DataExporter { + constructor(doctype, exporting_for) { + this.doctype = doctype; + this.exporting_for = exporting_for; + xhiveframework.model.with_doctype(doctype, () => { + this.make_dialog(); + }); + } + + make_dialog() { + this.dialog = new xhiveframework.ui.Dialog({ + title: __("Export Data"), + fields: [ + { + fieldtype: "Select", + fieldname: "file_type", + label: __("File Type"), + options: ["Excel", "CSV"], + default: "CSV", + }, + { + fieldtype: "Select", + fieldname: "export_records", + label: __("Export Type"), + options: [ + { + label: __("All Records"), + value: "all", + }, + { + label: __("Filtered Records"), + value: "by_filter", + }, + { + label: __("5 Records"), + value: "5_records", + }, + { + label: __("Blank Template"), + value: "blank_template", + }, + ], + default: + this.exporting_for === "Insert New Records" ? "blank_template" : "all", + change: () => { + this.update_record_count_message(); + }, + }, + { + fieldtype: "HTML", + fieldname: "filter_area", + depends_on: (doc) => doc.export_records === "by_filter", + }, + { + fieldtype: "Section Break", + }, + { + fieldtype: "HTML", + fieldname: "select_all_buttons", + }, + { + label: __(this.doctype), + fieldname: this.doctype, + fieldtype: "MultiCheck", + columns: 2, + on_change: () => this.update_primary_action(), + options: this.get_multicheck_options(this.doctype), + }, + ...xhiveframework.meta.get_table_fields(this.doctype).map((df) => { + let doctype = df.options; + let child_fieldname = df.fieldname; + let label = df.reqd + ? // prettier-ignore + __('{0} ({1}) (1 row mandatory)', [__(df.label || df.fieldname, null, df.parent), __(doctype)]) + : __("{0} ({1})", [ + __(df.label || df.fieldname, null, df.parent), + __(doctype), + ]); + return { + label, + fieldname: child_fieldname, + fieldtype: "MultiCheck", + columns: 2, + on_change: () => this.update_primary_action(), + options: this.get_multicheck_options(doctype, child_fieldname), + }; + }), + ], + primary_action_label: __("Export"), + primary_action: (values) => this.export_records(values), + on_page_show: () => this.select_mandatory(), + }); + + this.make_filter_area(); + this.make_select_all_buttons(); + this.update_record_count_message(); + + this.dialog.show(); + } + + export_records() { + let method = "/api/method/xhiveframework.core.doctype.data_import.data_import.download_template"; + + let multicheck_fields = this.dialog.fields + .filter((df) => df.fieldtype === "MultiCheck") + .map((df) => df.fieldname); + + let values = this.dialog.get_values(); + + let doctype_field_map = Object.assign({}, values); + for (let key in doctype_field_map) { + if (!multicheck_fields.includes(key)) { + delete doctype_field_map[key]; + } + } + + let filters = null; + if (values.export_records === "by_filter") { + filters = this.get_filters(); + } + + open_url_post(method, { + doctype: this.doctype, + file_type: values.file_type, + export_records: values.export_records, + export_fields: doctype_field_map, + export_filters: filters, + }); + } + + make_filter_area() { + this.filter_group = new xhiveframework.ui.FilterGroup({ + parent: this.dialog.get_field("filter_area").$wrapper, + doctype: this.doctype, + on_change: () => { + this.update_record_count_message(); + }, + }); + } + + make_select_all_buttons() { + let for_insert = this.exporting_for === "Insert New Records"; + let section_title = for_insert + ? __("Select Fields To Insert") + : __("Select Fields To Update"); + let $select_all_buttons = $(` +
      +
      ${section_title}
      + + ${ + for_insert + ? `` + : "" + } + +
      + `); + xhiveframework.utils.bind_actions_with_object($select_all_buttons, this); + this.dialog.get_field("select_all_buttons").$wrapper.html($select_all_buttons); + } + + select_all() { + this.dialog.$wrapper.find(":checkbox").prop("checked", true).trigger("change"); + } + + select_mandatory() { + let mandatory_table_fields = xhiveframework.meta + .get_table_fields(this.doctype) + .filter((df) => df.reqd) + .map((df) => df.fieldname); + mandatory_table_fields.push(this.doctype); + + let multicheck_fields = this.dialog.fields + .filter((df) => df.fieldtype === "MultiCheck") + .map((df) => df.fieldname) + .filter((doctype) => mandatory_table_fields.includes(doctype)); + + let checkboxes = [].concat( + ...multicheck_fields.map((fieldname) => { + let field = this.dialog.get_field(fieldname); + return field.options + .filter((option) => option.danger) + .map((option) => option.$checkbox.find("input").get(0)); + }) + ); + + this.unselect_all(); + $(checkboxes).prop("checked", true).trigger("change"); + } + + unselect_all() { + let update_existing_records = + this.dialog.get_value("exporting_for") == "Update Existing Records"; + this.dialog.$wrapper + .find(`:checkbox${update_existing_records ? ":not([data-unit=name])" : ""}`) + .prop("checked", false) + .trigger("change"); + } + + update_record_count_message() { + let export_records = this.dialog.get_value("export_records"); + let count_method = { + all: () => xhiveframework.db.count(this.doctype), + by_filter: () => + xhiveframework.db.count(this.doctype, { + filters: this.get_filters(), + }), + blank_template: () => Promise.resolve(0), + "5_records": () => Promise.resolve(5), + }; + + count_method[export_records]().then((value) => { + let message = ""; + value = parseInt(value, 10); + if (value === 0) { + message = __("No records will be exported"); + } else if (value === 1) { + message = __("1 record will be exported"); + } else { + message = __("{0} records will be exported", [value]); + } + this.dialog.set_df_property("export_records", "description", message); + + this.update_primary_action(value); + }); + } + + update_primary_action(no_of_records) { + let $primary_action = this.dialog.get_primary_btn(); + + if (no_of_records != null) { + let label = ""; + if (no_of_records === 0) { + label = __("Export"); + } else if (no_of_records === 1) { + label = __("Export 1 record"); + } else { + label = __("Export {0} records", [no_of_records]); + } + $primary_action.html(label); + } else { + let parent_fields = this.dialog.get_value(this.doctype); + $primary_action.prop("disabled", parent_fields.length === 0); + } + } + + get_filters() { + return this.filter_group.get_filters().map((filter) => { + return filter.slice(0, 4); + }); + } + + get_multicheck_options(doctype, child_fieldname = null) { + if (!this.column_map) { + this.column_map = get_columns_for_picker(this.doctype); + } + + let autoname_field = null; + let meta = xhiveframework.get_meta(doctype); + if (meta.autoname && meta.autoname.startsWith("field:")) { + let fieldname = meta.autoname.slice("field:".length); + autoname_field = xhiveframework.meta.get_field(doctype, fieldname); + } + + let fields = child_fieldname ? this.column_map[child_fieldname] : this.column_map[doctype]; + + let is_field_mandatory = (df) => { + if (df.reqd && this.exporting_for == "Insert New Records") { + return true; + } + if (autoname_field && df.fieldname == autoname_field.fieldname) { + return true; + } + if (df.fieldname === "name") { + return true; + } + return false; + }; + + return fields + .filter((df) => { + if (autoname_field && df.fieldname === "name") { + return false; + } + return true; + }) + .map((df) => { + return { + label: __(df.label, null, df.parent), + value: df.fieldname, + danger: is_field_mandatory(df), + checked: false, + description: `${df.fieldname} ${df.reqd ? __("(Mandatory)") : ""}`, + }; + }); + } +}; + +export function get_columns_for_picker(doctype) { + let out = {}; + + const exportable_fields = (df) => { + let keep = true; + if (xhiveframework.model.no_value_type.includes(df.fieldtype)) { + keep = false; + } + if (["lft", "rgt"].includes(df.fieldname)) { + keep = false; + } + if (df.is_virtual) { + keep = false; + } + return keep; + }; + + // parent + let doctype_fields = xhiveframework.meta.get_docfields(doctype).filter(exportable_fields); + + out[doctype] = [ + { + label: __("ID"), + fieldname: "name", + fieldtype: "Data", + reqd: 1, + }, + ].concat(doctype_fields); + + // children + const table_fields = xhiveframework.meta.get_table_fields(doctype); + table_fields.forEach((df) => { + const cdt = df.options; + const child_table_fields = xhiveframework.meta.get_docfields(cdt).filter(exportable_fields); + + out[df.fieldname] = [ + { + label: __("ID"), + fieldname: "name", + fieldtype: "Data", + reqd: 1, + }, + ].concat(child_table_fields); + }); + + return out; +} diff --git a/xhiveframework/public/js/xhiveframework/data_import/import_preview.js b/xhiveframework/public/js/xhiveframework/data_import/import_preview.js new file mode 100644 index 0000000..eb41cb2 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/data_import/import_preview.js @@ -0,0 +1,352 @@ +import DataTable from "xhiveframework-datatable"; +import { get_columns_for_picker } from "./data_exporter"; + +xhiveframework.provide("xhiveframework.data_import"); + +xhiveframework.data_import.ImportPreview = class ImportPreview { + constructor({ wrapper, doctype, preview_data, frm, import_log, events = {} }) { + this.wrapper = wrapper; + this.doctype = doctype; + this.preview_data = preview_data; + this.events = events; + this.import_log = import_log; + this.frm = frm; + + xhiveframework.model.with_doctype(doctype, () => { + this.refresh(); + }); + } + + refresh() { + this.data = this.preview_data.data; + this.make_wrapper(); + this.prepare_columns(); + this.prepare_data(); + this.render_datatable(); + this.setup_styles(); + this.add_actions(); + } + + make_wrapper() { + this.wrapper.html(` +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + `); + xhiveframework.utils.bind_actions_with_object(this.wrapper, this); + + this.$table_preview = this.wrapper.find(".table-preview"); + } + + prepare_columns() { + this.columns = this.preview_data.columns.map((col, i) => { + let df = col.df; + let column_width = 120; + if (col.header_title === "Sr. No") { + return { + id: "srno", + name: "Sr. No", + content: "Sr. No", + editable: false, + focusable: false, + align: "left", + width: 60, + }; + } + + if (col.skip_import) { + let show_warnings_button = ``; + if (!col.df) { + // increase column width for unidentified columns + column_width += 50; + } + let column_title = ` + ${col.header_title || `${__("Untitled Column")}`} + ${!col.df ? show_warnings_button : ""} + `; + return { + id: xhiveframework.utils.get_random(6), + name: col.header_title || (df ? df.label : "Untitled Column"), + content: column_title, + skip_import: true, + editable: false, + focusable: false, + align: "left", + width: column_width, + format: (value) => `
      ${value}
      `, + }; + } + + let date_format = col.date_format + ? col.date_format + .replace("%Y", "yyyy") + .replace("%y", "yy") + .replace("%m", "mm") + .replace("%d", "dd") + .replace("%H", "HH") + .replace("%M", "mm") + .replace("%S", "ss") + .replace("%b", "Mon") + : null; + + let column_title = ` + ${col.header_title || df.label} + ${date_format ? `(${date_format})` : ""} + `; + + return { + id: df.fieldname, + name: col.header_title, + content: column_title, + df: df, + editable: false, + align: "left", + width: column_width, + }; + }); + } + + prepare_data() { + this.data = this.data.map((row) => { + return row.map((cell) => { + if (cell == null) { + return ""; + } + return cell; + }); + }); + } + + render_datatable() { + if (this.datatable) { + this.datatable.destroy(); + } + + this.datatable = new DataTable(this.$table_preview.get(0), { + data: this.data, + columns: this.columns, + layout: this.columns.length < 10 ? "fluid" : "fixed", + cellHeight: 35, + language: xhiveframework.boot.lang, + translations: xhiveframework.utils.datatable.get_translations(), + serialNoColumn: false, + checkboxColumn: false, + noDataMessage: __("No Data"), + disableReorderColumn: true, + }); + + let { max_rows_exceeded, max_rows_in_preview, total_number_of_rows } = this.preview_data; + if (max_rows_exceeded) { + let parts = [max_rows_in_preview, total_number_of_rows]; + this.wrapper.find(".table-message").html(` +
      + ${__("Showing only first {0} rows out of {1}", parts)} +
      + `); + } + + if (this.data.length === 0) { + this.datatable.style.setStyle(".dt-scrollable", { + height: "auto", + }); + } + + this.datatable.style.setStyle(".dt-dropdown", { + display: "none", + }); + } + + setup_styles() { + // import success checkbox + this.datatable.style.setStyle(`svg.import-success`, { + width: "16px", + fill: xhiveframework.ui.color.get_color_shade("green", "dark"), + }); + // make successfully imported rows readonly + let row_classes = this.datatable + .getRows() + .filter((row) => this.is_row_imported(row)) + .map((row) => row.meta.rowIndex) + .map((i) => `.dt-row-${i} .dt-cell`) + .join(","); + this.datatable.style.setStyle(row_classes, { + pointerEvents: "none", + backgroundColor: xhiveframework.ui.color.get_color_shade("gray", "extra-light"), + color: xhiveframework.ui.color.get_color_shade("gray", "dark"), + }); + } + + add_actions() { + let actions = [ + { + label: __("Map Columns"), + handler: "show_column_mapper", + condition: this.frm.doc.status !== "Success", + }, + { + label: __("Export Errored Rows"), + handler: "export_errored_rows", + condition: this.import_log.filter((log) => !log.success).length > 0, + }, + { + label: __("Show Warnings"), + handler: "show_warnings", + condition: this.preview_data.warnings.length > 0, + }, + ]; + + let html = actions + .filter((action) => action.condition) + .map((action) => { + return ` + `; + }); + + this.wrapper.find(".table-actions").html(html); + } + + export_errored_rows() { + this.frm.trigger("export_errored_rows"); + } + + show_warnings() { + this.frm.scroll_to_field("import_warnings"); + } + + show_column_warning(_, $target) { + let $warning = this.frm + .get_field("import_warnings") + .$wrapper.find(`[data-col=${$target.data("col")}]`); + xhiveframework.utils.scroll_to($warning, true, 30); + } + + show_column_mapper() { + let column_picker_fields = get_columns_for_picker(this.doctype); + let changed = []; + let fields = this.preview_data.columns.map((col, i) => { + let df = col.df; + if (col.header_title === "Sr. No") return []; + + let fieldname; + if (!df) { + fieldname = null; + } else if (col.map_to_field) { + fieldname = col.map_to_field; + } else if (col.is_child_table_field) { + fieldname = `${col.child_table_df.fieldname}.${df.fieldname}`; + } else { + fieldname = df.fieldname; + } + return [ + { + label: "", + fieldtype: "Data", + default: col.header_title, + fieldname: `Column ${i}`, + read_only: 1, + }, + { + fieldtype: "Column Break", + }, + { + fieldtype: "Autocomplete", + fieldname: i, + label: "", + max_items: Infinity, + options: [ + { + label: __("Don't Import"), + value: "Don't Import", + }, + ].concat(get_fields_as_options(this.doctype, column_picker_fields)), + default: fieldname || "Don't Import", + change() { + changed.push(i); + }, + }, + { + fieldtype: "Section Break", + }, + ]; + }); + // flatten the array + fields = fields.reduce((acc, curr) => [...acc, ...curr]); + let file_name = (this.frm.doc.import_file || "").split("/").pop(); + let parts = [file_name.bold(), this.doctype.bold()]; + fields = [ + { + fieldtype: "HTML", + fieldname: "heading", + options: ` +
      + ${__("Map columns from {0} to fields in {1}", parts)} +
      + `, + }, + { + fieldtype: "Section Break", + }, + ].concat(fields); + + let dialog = new xhiveframework.ui.Dialog({ + title: __("Map Columns"), + fields, + primary_action: (values) => { + let changed_map = {}; + changed.map((i) => { + let header_row_index = i - 1; + changed_map[header_row_index] = values[i]; + }); + if (changed.length > 0) { + this.events.remap_column(changed_map); + } + dialog.hide(); + }, + }); + dialog.$body.addClass("map-columns"); + dialog.show(); + } + + is_row_imported(row) { + let serial_no = row[0].content; + return this.import_log.find((log) => { + return log.success && JSON.parse(log.row_indexes || "[]").includes(serial_no); + }); + } +}; + +function get_fields_as_options(doctype, column_map) { + let keys = [doctype]; + xhiveframework.meta.get_table_fields(doctype).forEach((df) => { + keys.push(df.fieldname); + }); + // flatten array + return [].concat( + ...keys.map((key) => { + return column_map[key].map((df) => { + let label = __(df.label, null, df.parent); + let value = df.fieldname; + if (doctype !== key) { + let table_field = xhiveframework.meta.get_docfield(doctype, key); + label = `${__(df.label, null, df.parent)} (${__(table_field.label)})`; + value = `${table_field.fieldname}.${df.fieldname}`; + } + return { + label, + value, + description: value, + }; + }); + }) + ); +} diff --git a/xhiveframework/public/js/xhiveframework/data_import/index.js b/xhiveframework/public/js/xhiveframework/data_import/index.js new file mode 100644 index 0000000..8f49e4b --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/data_import/index.js @@ -0,0 +1,2 @@ +import "./import_preview"; +import "./data_exporter"; diff --git a/xhiveframework/public/js/xhiveframework/db.js b/xhiveframework/public/js/xhiveframework/db.js new file mode 100644 index 0000000..cabf89b --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/db.js @@ -0,0 +1,134 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.db = { + get_list: function (doctype, args) { + if (!args) { + args = {}; + } + args.doctype = doctype; + if (!args.fields) { + args.fields = ["name"]; + } + if (!("limit" in args)) { + args.limit = 20; + } + return new Promise((resolve) => { + xhiveframework.call({ + method: "xhiveframework.desk.reportview.get_list", + args: args, + type: "GET", + callback: function (r) { + resolve(r.message); + }, + }); + }); + }, + exists: function (doctype, name) { + return new Promise((resolve) => { + xhiveframework.db.get_value(doctype, { name: name }, "name").then((r) => { + r.message && r.message.name ? resolve(true) : resolve(false); + }); + }); + }, + get_value: function (doctype, filters, fieldname, callback, parent_doc) { + return xhiveframework.call({ + method: "xhiveframework.client.get_value", + type: "GET", + args: { + doctype: doctype, + fieldname: fieldname, + filters: filters, + parent: parent_doc, + }, + callback: function (r) { + callback && callback(r.message); + }, + }); + }, + get_single_value: (doctype, field) => { + return new Promise((resolve) => { + xhiveframework + .call({ + method: "xhiveframework.client.get_single_value", + args: { doctype, field }, + type: "GET", + }) + .then((r) => resolve(r ? r.message : null)); + }); + }, + set_value: function (doctype, docname, fieldname, value, callback) { + return xhiveframework.call({ + method: "xhiveframework.client.set_value", + args: { + doctype: doctype, + name: docname, + fieldname: fieldname, + value: value, + }, + callback: function (r) { + callback && callback(r.message); + }, + }); + }, + get_doc: function (doctype, name, filters) { + return new Promise((resolve, reject) => { + xhiveframework + .call({ + method: "xhiveframework.client.get", + type: "GET", + args: { doctype, name, filters }, + callback: (r) => { + xhiveframework.model.sync(r.message); + resolve(r.message); + }, + }) + .fail(reject); + }); + }, + insert: function (doc) { + return xhiveframework.xcall("xhiveframework.client.insert", { doc }); + }, + delete_doc: function (doctype, name) { + return new Promise((resolve) => { + xhiveframework.call("xhiveframework.client.delete", { doctype, name }, (r) => resolve(r.message)); + }); + }, + count: function (doctype, args = {}) { + let filters = args.filters || {}; + let limit = args.limit; + + // has a filter with childtable? + const distinct = + Array.isArray(filters) && + filters.some((filter) => { + return filter[0] !== doctype; + }); + + const fields = []; + + return xhiveframework.xcall("xhiveframework.desk.reportview.get_count", { + doctype, + filters, + fields, + distinct, + limit, + }); + }, + get_link_options(doctype, txt = "", filters = {}) { + return new Promise((resolve) => { + xhiveframework.call({ + type: "GET", + method: "xhiveframework.desk.search.search_link", + args: { + doctype, + txt, + filters, + }, + callback(r) { + resolve(r.message); + }, + }); + }); + }, +}; diff --git a/xhiveframework/public/js/xhiveframework/defaults.js b/xhiveframework/public/js/xhiveframework/defaults.js new file mode 100644 index 0000000..3a311d9 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/defaults.js @@ -0,0 +1,140 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt + +xhiveframework.defaults = { + get_user_default: function (key) { + let defaults = xhiveframework.boot.user.defaults; + let d = defaults[key]; + if (!d && xhiveframework.defaults.is_a_user_permission_key(key)) { + d = defaults[xhiveframework.model.scrub(key)]; + // Check for default user permission values + let user_default = this.get_user_permission_default(key, defaults); + if (user_default) d = user_default; + } + if ($.isArray(d)) d = d[0]; + + if (!xhiveframework.defaults.in_user_permission(key, d)) { + return; + } + + return d; + }, + + get_user_permission_default: function (key, defaults) { + let permissions = this.get_user_permissions(); + let user_default = null; + if (permissions[key]) { + permissions[key].forEach((item) => { + if (defaults[key] == item.doc) { + user_default = item.doc; + } + }); + + permissions[key].forEach((item) => { + if (item.is_default) { + user_default = item.doc; + } + }); + } + + return user_default; + }, + + get_user_defaults: function (key) { + var defaults = xhiveframework.boot.user.defaults; + var d = defaults[key]; + + if (xhiveframework.defaults.is_a_user_permission_key(key)) { + if (d && $.isArray(d) && d.length === 1) { + // Use User Permission value when only when it has a single value + d = d[0]; + } else { + d = defaults[key] || defaults[xhiveframework.model.scrub(key)]; + } + } + if (!$.isArray(d)) d = [d]; + + // filter out values which are not permitted to the user + d.filter((item) => { + if (xhiveframework.defaults.in_user_permission(key, item)) { + return item; + } + }); + return d; + }, + get_global_default: function (key) { + var d = xhiveframework.sys_defaults[key]; + if ($.isArray(d)) d = d[0]; + return d; + }, + get_global_defaults: function (key) { + var d = xhiveframework.sys_defaults[key]; + if (!$.isArray(d)) d = [d]; + return d; + }, + set_user_default_local: function (key, value) { + xhiveframework.boot.user.defaults[key] = value; + }, + get_default: function (key) { + var defaults = xhiveframework.boot.user.defaults; + var value = defaults[key]; + if (xhiveframework.defaults.is_a_user_permission_key(key)) { + if (value && $.isArray(value) && value.length === 1) { + value = value[0]; + } else { + value = defaults[xhiveframework.model.scrub(key)]; + } + } + + if (!xhiveframework.defaults.in_user_permission(key, value)) { + return; + } + + if (value) { + try { + return JSON.parse(value); + } catch (e) { + return value; + } + } + }, + + is_a_user_permission_key: function (key) { + return key.indexOf(":") === -1 && key !== xhiveframework.model.scrub(key); + }, + + in_user_permission: function (key, value) { + let user_permission = this.get_user_permissions()[xhiveframework.model.unscrub(key)]; + + if (user_permission && user_permission.length) { + return user_permission.some((perm) => { + return perm.doc === value; + }); + } else { + // there is no user permission for this doctype + // so we can allow this doc i.e., value + return true; + } + }, + + get_user_permissions: function () { + return this._user_permissions || {}; + }, + + update_user_permissions: function () { + const method = "xhiveframework.core.doctype.user_permission.user_permission.get_user_permissions"; + xhiveframework.call(method).then((r) => { + if (r.message) { + this._user_permissions = Object.assign({}, r.message); + } + }); + }, + + load_user_permission_from_boot: function () { + if (xhiveframework.boot.user.user_permissions) { + this._user_permissions = Object.assign({}, xhiveframework.boot.user.user_permissions); + } else { + xhiveframework.defaults.update_user_permissions(); + } + }, +}; diff --git a/xhiveframework/public/js/xhiveframework/desk.js b/xhiveframework/public/js/xhiveframework/desk.js new file mode 100644 index 0000000..eaae625 --- /dev/null +++ b/xhiveframework/public/js/xhiveframework/desk.js @@ -0,0 +1,554 @@ +// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors +// MIT License. See license.txt +/* eslint-disable no-console */ + +// __('Modules') __('Domains') __('Places') __('Administration') # for translation, don't remove + +xhiveframework.start_app = function () { + if (!xhiveframework.Application) return; + xhiveframework.assets.check(); + xhiveframework.provide("xhiveframework.app"); + xhiveframework.provide("xhiveframework.desk"); + xhiveframework.app = new xhiveframework.Application(); +}; + +$(document).ready(function () { + if (!xhiveframework.utils.supportsES6) { + xhiveframework.msgprint({ + indicator: "red", + title: __("Browser not supported"), + message: __( + "Some of the features might not work in your browser. Please update your browser to the latest version." + ), + }); + } + xhiveframework.start_app(); +}); + +xhiveframework.Application = class Application { + constructor() { + this.startup(); + } + + startup() { + xhiveframework.realtime.init(); + xhiveframework.model.init(); + + this.load_bootinfo(); + this.load_user_permissions(); + this.make_nav_bar(); + this.set_favicon(); + this.set_fullwidth_if_enabled(); + this.add_browser_class(); + this.setup_energy_point_listeners(); + this.setup_copy_doc_listener(); + + xhiveframework.ui.keys.setup(); + + xhiveframework.ui.keys.add_shortcut({ + shortcut: "shift+ctrl+g", + description: __("Switch Theme"), + action: () => { + if (xhiveframework.theme_switcher && xhiveframework.theme_switcher.dialog.is_visible) { + xhiveframework.theme_switcher.hide(); + } else { + xhiveframework.theme_switcher = new xhiveframework.ui.ThemeSwitcher(); + xhiveframework.theme_switcher.show(); + } + }, + }); + + xhiveframework.ui.add_system_theme_switch_listener(); + const root = document.documentElement; + + const observer = new MutationObserver(() => { + xhiveframework.ui.set_theme(); + }); + observer.observe(root, { + attributes: true, + attributeFilter: ["data-theme-mode"], + }); + + xhiveframework.ui.set_theme(); + + // page container + this.make_page_container(); + if ( + !window.Cypress && + xhiveframework.boot.onboarding_tours && + xhiveframework.boot.user.onboarding_status != null + ) { + let pending_tours = !xhiveframework.boot.onboarding_tours.every( + (tour) => xhiveframework.boot.user.onboarding_status[tour[0]]?.is_complete + ); + if (pending_tours && xhiveframework.boot.onboarding_tours.length > 0) { + xhiveframework.require("onboarding_tours.bundle.js", () => { + xhiveframework.utils.sleep(1000).then(() => { + xhiveframework.ui.init_onboarding_tour(); + }); + }); + } + } + this.set_route(); + + // trigger app startup + $(document).trigger("startup"); + + $(document).trigger("app_ready"); + + if (xhiveframework.boot.messages) { + xhiveframework.msgprint(xhiveframework.boot.messages); + } + + if (xhiveframework.user_roles.includes("System Manager")) { + // delayed following requests to make boot faster + setTimeout(() => { + this.show_change_log(); + this.show_update_available(); + }, 1000); + } + + if (!xhiveframework.boot.developer_mode) { + let console_security_message = __( + "Using this console may allow attackers to impersonate you and steal your information. Do not enter or paste code that you do not understand." + ); + console.log(`%c${console_security_message}`, "font-size: large"); + } + + this.show_notes(); + + if (xhiveframework.ui.startup_setup_dialog && !xhiveframework.boot.setup_complete) { + xhiveframework.ui.startup_setup_dialog.pre_show(); + xhiveframework.ui.startup_setup_dialog.show(); + } + + xhiveframework.realtime.on("version-update", function () { + var dialog = xhiveframework.msgprint({ + message: __( + "The application has been updated to a new version, please refresh this page" + ), + indicator: "green", + title: __("Version Updated"), + }); + dialog.set_primary_action(__("Refresh"), function () { + location.reload(true); + }); + dialog.get_close_btn().toggle(false); + }); + + // listen to build errors + this.setup_build_events(); + + if (xhiveframework.sys_defaults.email_user_password) { + var email_list = xhiveframework.sys_defaults.email_user_password.split(","); + for (var u in email_list) { + if (email_list[u] === xhiveframework.user.name) { + this.set_password(email_list[u]); + } + } + } + + // REDESIGN-TODO: Fix preview popovers + this.link_preview = new xhiveframework.ui.LinkPreview(); + } + + set_route() { + if (xhiveframework.boot && localStorage.getItem("session_last_route")) { + xhiveframework.set_route(localStorage.getItem("session_last_route")); + localStorage.removeItem("session_last_route"); + } else { + // route to home page + xhiveframework.router.route(); + } + xhiveframework.router.on("change", () => { + $(".tooltip").hide(); + }); + } + + set_password(user) { + var me = this; + xhiveframework.call({ + method: "xhiveframework.core.doctype.user.user.get_email_awaiting", + args: { + user: user, + }, + callback: function (email_account) { + email_account = email_account["message"]; + if (email_account) { + var i = 0; + if (i < email_account.length) { + me.email_password_prompt(email_account, user, i); + } + } + }, + }); + } + + email_password_prompt(email_account, user, i) { + var me = this; + const email_id = email_account[i]["email_id"]; + let d = new xhiveframework.ui.Dialog({ + title: __("Password missing in Email Account"), + fields: [ + { + fieldname: "password", + fieldtype: "Password", + label: __( + "Please enter the password for: {0}", + [email_id], + "Email Account" + ), + reqd: 1, + }, + { + fieldname: "submit", + fieldtype: "Button", + label: __("Submit", null, "Submit password for Email Account"), + }, + ], + }); + d.get_input("submit").on("click", function () { + //setup spinner + d.hide(); + var s = new xhiveframework.ui.Dialog({ + title: __("Checking one moment"), + fields: [ + { + fieldtype: "HTML", + fieldname: "checking", + }, + ], + }); + s.fields_dict.checking.$wrapper.html(''); + s.show(); + xhiveframework.call({ + method: "xhiveframework.email.doctype.email_account.email_account.set_email_password", + args: { + email_account: email_account[i]["email_account"], + password: d.get_value("password"), + }, + callback: function (passed) { + s.hide(); + d.hide(); //hide waiting indication + if (!passed["message"]) { + xhiveframework.show_alert( + { message: __("Login Failed please try again"), indicator: "error" }, + 5 + ); + me.email_password_prompt(email_account, user, i); + } else { + if (i + 1 < email_account.length) { + i = i + 1; + me.email_password_prompt(email_account, user, i); + } + } + }, + }); + }); + d.show(); + } + load_bootinfo() { + if (xhiveframework.boot) { + this.setup_workspaces(); + xhiveframework.model.sync(xhiveframework.boot.docs); + this.check_metadata_cache_status(); + this.set_globals(); + this.sync_pages(); + xhiveframework.router.setup(); + this.setup_moment(); + if (xhiveframework.boot.print_css) { + xhiveframework.dom.set_style(xhiveframework.boot.print_css, "print-style"); + } + xhiveframework.user.name = xhiveframework.boot.user.name; + xhiveframework.router.setup(); + } else { + this.set_as_guest(); + } + } + + setup_workspaces() { + xhiveframework.modules = {}; + xhiveframework.workspaces = {}; + for (let page of xhiveframework.boot.allowed_workspaces || []) { + xhiveframework.modules[page.module] = page; + xhiveframework.workspaces[xhiveframework.router.slug(page.name)] = page; + } + } + + load_user_permissions() { + xhiveframework.defaults.load_user_permission_from_boot(); + + xhiveframework.realtime.on( + "update_user_permissions", + xhiveframework.utils.debounce(() => { + xhiveframework.defaults.update_user_permissions(); + }, 500) + ); + } + + check_metadata_cache_status() { + if (xhiveframework.boot.metadata_version != localStorage.metadata_version) { + xhiveframework.assets.clear_local_storage(); + xhiveframework.assets.init_local_storage(); + } + } + + set_globals() { + xhiveframework.session.user = xhiveframework.boot.user.name; + xhiveframework.session.logged_in_user = xhiveframework.boot.user.name; + xhiveframework.session.user_email = xhiveframework.boot.user.email; + xhiveframework.session.user_fullname = xhiveframework.user_info().fullname; + + xhiveframework.user_defaults = xhiveframework.boot.user.defaults; + xhiveframework.user_roles = xhiveframework.boot.user.roles; + xhiveframework.sys_defaults = xhiveframework.boot.sysdefaults; + + xhiveframework.ui.py_date_format = xhiveframework.boot.sysdefaults.date_format + .replace("dd", "%d") + .replace("mm", "%m") + .replace("yyyy", "%Y"); + xhiveframework.boot.user.last_selected_values = {}; + } + sync_pages() { + // clear cached pages if timestamp is not found + if (localStorage["page_info"]) { + xhiveframework.boot.allowed_pages = []; + var page_info = JSON.parse(localStorage["page_info"]); + $.each(xhiveframework.boot.page_info, function (name, p) { + if (!page_info[name] || page_info[name].modified != p.modified) { + delete localStorage["_page:" + name]; + } + xhiveframework.boot.allowed_pages.push(name); + }); + } else { + xhiveframework.boot.allowed_pages = Object.keys(xhiveframework.boot.page_info); + } + localStorage["page_info"] = JSON.stringify(xhiveframework.boot.page_info); + } + set_as_guest() { + xhiveframework.session.user = "Guest"; + xhiveframework.session.user_email = ""; + xhiveframework.session.user_fullname = "Guest"; + + xhiveframework.user_defaults = {}; + xhiveframework.user_roles = ["Guest"]; + xhiveframework.sys_defaults = {}; + } + make_page_container() { + if ($("#body").length) { + $(".splash").remove(); + xhiveframework.temp_container = $("