Browse Source

Merge pull request #16165 from gavindsouza/creation-doc-man

fix: Set creation dates of standard DocTypes correctly
version-14
gavin 3 years ago
committed by GitHub
parent
commit
873c4ad0ec
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 8 deletions
  1. +1
    -1
      frappe/model/document.py
  2. +23
    -7
      frappe/modules/patch_handler.py
  3. +1
    -0
      frappe/patches.txt
  4. +41
    -0
      frappe/patches/v14_0/reset_creation_datetime.py

+ 1
- 1
frappe/model/document.py View File

@@ -471,7 +471,7 @@ class Document(BaseDocument):


# We'd probably want the creation and owner to be set via API # 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 # or Data import at some point, that'd have to be handled here
if self.is_new() and not (frappe.flags.in_patch or frappe.flags.in_migrate):
if self.is_new() and not (frappe.flags.in_install or frappe.flags.in_patch or frappe.flags.in_migrate):
self.creation = self.modified self.creation = self.modified
self.owner = self.modified_by self.owner = self.modified_by




+ 23
- 7
frappe/modules/patch_handler.py View File

@@ -37,6 +37,7 @@ patches by using INI like file format:
import configparser import configparser
import time import time
from enum import Enum from enum import Enum
from textwrap import dedent, indent
from typing import List, Optional from typing import List, Optional


import frappe import frappe
@@ -148,21 +149,36 @@ def run_single(patchmodule=None, method=None, methodargs=None, force=False):
def execute_patch(patchmodule, method=None, methodargs=None): def execute_patch(patchmodule, method=None, methodargs=None):
"""execute the patch""" """execute the patch"""
block_user(True) block_user(True)
frappe.db.begin()

if patchmodule.startswith("execute:"):
has_patch_file = False
patch = patchmodule.split("execute:")[1]
docstring = ""
else:
has_patch_file = True
patch = f"{patchmodule.split()[0]}.execute"
_patch = frappe.get_attr(patch)
docstring = _patch.__doc__ or ""

if docstring:
docstring = "\n" + indent(dedent(docstring), "\t")

print(f"Executing {patchmodule or methodargs} in {frappe.local.site} ({frappe.db.cur_db_name}){docstring}")

start_time = time.time() start_time = time.time()
frappe.db.begin()
try: try:
print('Executing {patch} in {site} ({db})'.format(patch=patchmodule or str(methodargs),
site=frappe.local.site, db=frappe.db.cur_db_name))
if patchmodule: if patchmodule:
if patchmodule.startswith("finally:"): if patchmodule.startswith("finally:"):
# run run patch at the end # run run patch at the end
frappe.flags.final_patches.append(patchmodule) frappe.flags.final_patches.append(patchmodule)
else: else:
if patchmodule.startswith("execute:"):
exec(patchmodule.split("execute:")[1],globals())
if has_patch_file:
_patch()
else: else:
frappe.get_attr(patchmodule.split()[0] + ".execute")()
exec(patch, globals())
update_patch_log(patchmodule) update_patch_log(patchmodule)

elif method: elif method:
method(**methodargs) method(**methodargs)


@@ -174,7 +190,7 @@ def execute_patch(patchmodule, method=None, methodargs=None):
frappe.db.commit() frappe.db.commit()
end_time = time.time() end_time = time.time()
block_user(False) block_user(False)
print('Success: Done in {time}s'.format(time = round(end_time - start_time, 3)))
print(f"Success: Done in {round(end_time - start_time, 3)}s")


return True return True




+ 1
- 0
frappe/patches.txt View File

@@ -189,6 +189,7 @@ frappe.patches.v14_0.update_workspace2 # 20.09.2021
frappe.patches.v14_0.save_ratings_in_fraction #23-12-2021 frappe.patches.v14_0.save_ratings_in_fraction #23-12-2021
frappe.patches.v14_0.transform_todo_schema frappe.patches.v14_0.transform_todo_schema
frappe.patches.v14_0.remove_post_and_post_comment frappe.patches.v14_0.remove_post_and_post_comment
frappe.patches.v14_0.reset_creation_datetime


[post_model_sync] [post_model_sync]
frappe.patches.v14_0.drop_data_import_legacy frappe.patches.v14_0.drop_data_import_legacy


+ 41
- 0
frappe/patches/v14_0/reset_creation_datetime.py View File

@@ -0,0 +1,41 @@
import glob
import json
import frappe
import os
from frappe.query_builder import DocType as _DocType


def execute():
"""Resetting creation datetimes for DocTypes"""
DocType = _DocType("DocType")
doctype_jsons = glob.glob(
os.path.join("..", "apps", "frappe", "frappe", "**", "doctype", "**", "*.json")
)

frappe_modules = frappe.get_all(
"Module Def", filters={"app_name": "frappe"}, pluck="name"
)
site_doctypes = frappe.get_all(
"DocType",
filters={"module": ("in", frappe_modules), "custom": False},
fields=["name", "creation"],
)

for dt_path in doctype_jsons:
with open(dt_path) as f:
try:
file_schema = frappe._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:
frappe.qb.update(DocType).set(
DocType.creation, file_schema.creation
).where(DocType.name == file_schema.name).run()

Loading…
Cancel
Save