Skip to content

Commit

Permalink
refactor: deduplicate during repost background job
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Nov 25, 2021
1 parent 55631dd commit ed94f0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt


import datetime

import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import (
cint,
get_datetime,
get_link_to_form,
get_time,
get_weekday,
now,
nowtime,
today,
)
from frappe.utils import cint, get_link_to_form, get_weekday, now, nowtime, today
from frappe.utils.user import get_users_with_role
from rq.timeouts import JobTimeoutException

Expand Down Expand Up @@ -58,7 +46,6 @@ def set_status(self, status=None, write=True):
self.db_set('status', self.status)

def on_submit(self):
self.deduplicate_similar_repost()
if not frappe.flags.in_test or self.flags.dont_run_in_test:
return

Expand All @@ -76,30 +63,27 @@ def deduplicate_similar_repost(self):
if self.based_on != "Item and Warehouse":
return

queued = frappe.db.get_value(
"Repost Item Valuation",
filters={
"docstatus": 1,
"status": "Queued",
"item_code": self.item_code,
"warehouse": self.warehouse,
"based_on": self.based_on,
"name": ("!=", self.name)
},
fieldname=["name", "posting_date", "posting_time"],
as_dict=True
)
if not queued:
return

posting_timestamp = datetime.datetime.combine(get_datetime(self.posting_date), get_time(self.posting_time))
queued_timestamp = datetime.datetime.combine(get_datetime(queued.posting_date), get_time(queued.posting_time))

if posting_timestamp > queued_timestamp:
self.set_status("Skipped")
else:
frappe.db.set_value("Repost Item Valuation", queued.name, "status", "Skipped")

filters = {
"item_code": self.item_code,
"warehouse": self.warehouse,
"name": self.name,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
}

frappe.db.sql("""
update `tabRepost Item Valuation`
set status = 'Skipped'
WHERE item_code = %(item_code)s
and warehouse = %(warehouse)s
and name != %(name)s
and TIMESTAMP(posting_date, posting_time) > TIMESTAMP(%(posting_date)s, %(posting_time)s)
and docstatus = 1
and status = 'Queued'
and based_on = 'Item and Warehouse'
""",
filters
)

def on_doctype_update():
frappe.db.add_index("Repost Item Valuation", ["warehouse", "item_code"], "item_warehouse")
Expand Down Expand Up @@ -182,6 +166,7 @@ def repost_entries():
for row in riv_entries:
doc = frappe.get_doc('Repost Item Valuation', row.name)
if doc.status in ('Queued', 'In Progress'):
doc.deduplicate_similar_repost()
repost(doc)

riv_entries = get_repost_item_valuation_entries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,22 @@ def _assert_status(doc, status):
riv2 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-03"}))
riv2.flags.dont_run_in_test = True
riv2.submit()
riv1.deduplicate_similar_repost()
_assert_status(riv2, "Skipped")

# older than exisitng duplicate - riv1
riv3 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-01"}))
riv3.flags.dont_run_in_test = True
riv3.submit()
riv3.deduplicate_similar_repost()
_assert_status(riv3, "Queued")
_assert_status(riv1, "Skipped")

# unrelated reposts, shouldn't do anything to others.
riv4 = frappe.get_doc(riv_args.update({"warehouse": "Stores - _TC"}))
riv4.flags.dont_run_in_test = True
riv4.submit()
riv4.deduplicate_similar_repost()
_assert_status(riv4, "Queued")
_assert_status(riv3, "Queued")

Expand Down

0 comments on commit ed94f0f

Please sign in to comment.