def get_error_report(from_date=None, to_date=None, limit=10): from webnotes.utils import get_url, now_datetime, add_days if not from_date: from_date = add_days(now_datetime().date(), -1) if not to_date: to_date = add_days(now_datetime().date(), -1) errors = get_errors(from_date, to_date, limit) if errors: return 1, """<h4>Scheduler Failed Events (max {limit}):</h4> <p>URL: <a href="{url}" target="_blank">{url}</a></p><hr>{errors}""".format( limit=limit, url=get_url(), errors="<hr>".join(errors)) else: return 0, "<p>Scheduler didn't encounter any problems.</p>"
def get_post_list_html(group, view, limit_start=0, limit_length=20): access = get_access(group) if isinstance(view, basestring): view = get_views()[view] view = webnotes._dict(view) # verify permission for paging if webnotes.local.form_dict.cmd == "get_post_list_html": if not access.get("read"): return webnotes.PermissionError if view.name == "feed": order_by = "p.creation desc" else: now = get_datetime_str(now_datetime()) order_by = """(p.upvotes + post_reply_count - (timestampdiff(hour, p.creation, \"{}\") / 2)) desc, p.creation desc""".format(now) posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name, (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count from `tabPost` p, `tabProfile` pr where p.website_group = %s and pr.name = p.owner and ifnull(p.parent_post, '')='' order by {order_by} limit %s, %s""".format(order_by=order_by), (group, int(limit_start), int(limit_length)), as_dict=True) context = {"posts": posts, "limit_start": limit_start, "view": view} return webnotes.get_template("templates/includes/post_list.html").render(context)
def get_post_list_html(group, view, limit_start=0, limit_length=20): access = get_access(group) if isinstance(view, basestring): view = get_views()[view] view = webnotes._dict(view) # verify permission for paging if webnotes.local.form_dict.cmd == "get_post_list_html": if not access.get("read"): return webnotes.PermissionError if view.name=="upcoming": condition = "and p.event_datetime >= %s" order_by = "p.event_datetime asc" else: condition = "and p.event_datetime < %s" order_by = "p.event_datetime desc" # should show based on time upto precision of hour # because the current hour should also be in upcoming now = now_datetime().replace(minute=0, second=0, microsecond=0) posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name, (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count from `tabPost` p, `tabProfile` pr where p.website_group = %s and pr.name = p.owner and ifnull(p.parent_post, '')='' and p.is_event=1 {condition} order by {order_by} limit %s, %s""".format(condition=condition, order_by=order_by), (group, now, int(limit_start), int(limit_length)), as_dict=True) context = {"posts": posts, "limit_start": limit_start, "view": view} return webnotes.get_template("templates/includes/post_list.html").render(context)
def get_creation_count(self, doctype, minutes): """get count of records created in the last x minutes""" from webnotes.utils import now_datetime from dateutil.relativedelta import relativedelta return webnotes.conn.sql("""select count(name) from `tab{doctype}` where creation >= %s""".format(doctype=doctype), now_datetime() - relativedelta(minutes=minutes))[0][0]
def send(): from webnotes.model.code import get_obj now_date = now_datetime().date() for ed in webnotes.conn.sql("""select name from `tabEmail Digest` where enabled=1 and docstatus<2""", as_list=1): ed_obj = get_obj('Email Digest', ed[0]) if (now_date == ed_obj.get_next_sending()): ed_obj.send()
def validate_end_of_life(item_code, end_of_life=None, verbose=1): if not end_of_life: end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life") from webnotes.utils import getdate, now_datetime, formatdate if end_of_life and getdate(end_of_life) <= now_datetime().date(): msg = (_("Item") + " %(item_code)s: " + _("reached its end of life on") + \ " %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \ "in Item master") % { "item_code": item_code, "date": formatdate(end_of_life), "end_of_life_label": webnotes.get_doctype("Item").get_label("end_of_life") } _msgprint(msg, verbose)
def send(): from webnotes.model.code import get_obj from webnotes.utils import getdate now_date = now_datetime().date() from webnotes import conf if "expires_on" in conf and now_date > getdate(conf.expires_on): # do not send email digests to expired accounts return for ed in webnotes.conn.sql("""select name from `tabEmail Digest` where enabled=1 and docstatus<2""", as_list=1): ed_obj = get_obj('Email Digest', ed[0]) if (now_date == ed_obj.get_next_sending()): ed_obj.send()
def validate_end_of_life(item_code, end_of_life=None, verbose=1): if not end_of_life: end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life") from webnotes.utils import getdate, now_datetime, formatdate if end_of_life and getdate(end_of_life) > now_datetime().date(): msg = (_("Item") + " %(item_code)s: " + _("reached its end of life on") + \ " %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \ "in Item master") % { "item_code": item_code, "date": formatdate(end_of_life), "end_of_life_label": webnotes.get_doctype("Item").get_label("end_of_life") } _msgprint(msg, verbose)
def validate_hour(self): """check if user is logging in during restricted hours""" login_before = int(webnotes.conn.get_value('Profile', self.user, 'login_before', ignore=True) or 0) login_after = int(webnotes.conn.get_value('Profile', self.user, 'login_after', ignore=True) or 0) if not (login_before or login_after): return from webnotes.utils import now_datetime current_hour = int(now_datetime().strftime('%H')) if login_before and current_hour > login_before: webnotes.msgprint('Not allowed to login after restricted hour', raise_exception=1) if login_after and current_hour < login_after: webnotes.msgprint('Not allowed to login before restricted hour', raise_exception=1)
def get_from_to_date(self): today = now_datetime().date() # decide from date based on email digest frequency if self.doc.frequency == "Daily": # from date, to_date is yesterday from_date = to_date = today - timedelta(days=1) elif self.doc.frequency == "Weekly": # from date is the previous week's monday from_date = today - timedelta(days=today.weekday(), weeks=1) # to date is sunday i.e. the previous day to_date = from_date + timedelta(days=6) else: # from date is the 1st day of the previous month from_date = today - relativedelta(days=today.day - 1, months=1) # to date is the last day of the previous month to_date = today - relativedelta(days=today.day) return from_date, to_date
def get_future_from_to_date(self): today = now_datetime().date() # decide from date based on email digest frequency if self.doc.frequency == "Daily": # from date, to_date is today from_date = to_date = today elif self.doc.frequency == "Weekly": # from date is the current week's monday from_date = today - timedelta(days=today.weekday()) # to date is the current week's sunday to_date = from_date + timedelta(days=6) else: # from date is the 1st day of the current month from_date = today - relativedelta(days=today.day-1) # to date is the last day of the current month to_date = from_date + relativedelta(days=-1, months=1) return from_date, to_date
def get_from_to_date(self): today = now_datetime().date() # decide from date based on email digest frequency if self.doc.frequency == "Daily": # from date, to_date is yesterday from_date = to_date = today - timedelta(days=1) elif self.doc.frequency == "Weekly": # from date is the previous week's monday from_date = today - timedelta(days=today.weekday(), weeks=1) # to date is sunday i.e. the previous day to_date = from_date + timedelta(days=6) else: # from date is the 1st day of the previous month from_date = today - relativedelta(days=today.day-1, months=1) # to date is the last day of the previous month to_date = today - relativedelta(days=today.day) return from_date, to_date
def get_start_end_dates(self): """ Returns start and end date depending on the frequency of email digest """ from datetime import datetime, date, timedelta from webnotes.utils import now_datetime today = now_datetime().date() year, month, day = today.year, today.month, today.day if self.doc.frequency == 'Daily': if self.sending: start_date = end_date = today - timedelta(days=1) else: start_date = end_date = today elif self.doc.frequency == 'Weekly': if self.sending: start_date = today - timedelta(days=today.weekday(), weeks=1) end_date = start_date + timedelta(days=6) else: start_date = today - timedelta(days=today.weekday()) end_date = start_date + timedelta(days=6) else: import calendar if self.sending: if month == 1: year = year - 1 prev_month = 12 else: prev_month = month - 1 start_date = date(year, prev_month, 1) last_day = calendar.monthrange(year, prev_month)[1] end_date = date(year, prev_month, last_day) else: start_date = date(year, month, 1) last_day = calendar.monthrange(year, month)[1] end_date = date(year, month, last_day) return start_date, end_date
def test_get_value(self): from webnotes.utils import now_datetime import time webnotes.conn.sql("""delete from `tabProfile` where name not in ('Administrator', 'Guest')""") now = now_datetime() self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["=", "Administrator"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["like", "Admin%"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["!=", "Guest"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"modified": ["<", now]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"modified": ["<=", now]}), "Administrator") time.sleep(2) if "Profile" in webnotes.local.test_objects: del webnotes.local.test_objects["Profile"] make_test_records("Profile") self.assertEquals("*****@*****.**", webnotes.conn.get_value("Profile", {"modified": [">", now]})) self.assertEquals("*****@*****.**", webnotes.conn.get_value("Profile", {"modified": [">=", now]}))
def test_get_value(self): from webnotes.utils import now_datetime import time webnotes.conn.sql("""delete from `tabProfile` where name not in ('Administrator', 'Guest')""") now = now_datetime() self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["=", "Administrator"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["like", "Admin%"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"name": ["!=", "Guest"]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"modified": ["<", now]}), "Administrator") self.assertEquals(webnotes.conn.get_value("Profile", {"modified": ["<=", now]}), "Administrator") time.sleep(2) if "Profile" in webnotes.test_objects: del webnotes.test_objects["Profile"] make_test_records("Profile") self.assertEquals("*****@*****.**", webnotes.conn.get_value("Profile", {"modified": [">", now]})) self.assertEquals("*****@*****.**", webnotes.conn.get_value("Profile", {"modified": [">=", now]}))
def send(): """ """ edigest_list = webnotes.conn.sql(""" SELECT name FROM `tabEmail Digest` WHERE enabled=1 and docstatus<2 """, as_list=1) from webnotes.model.code import get_obj from webnotes.utils import now_datetime now_date = now_datetime().date() for ed in edigest_list: if ed[0]: ed_obj = get_obj('Email Digest', ed[0]) ed_obj.sending = True send_date = ed_obj.get_next_sending() #webnotes.msgprint([ed[0], now_date, send_date]) if (now_date == send_date): ed_obj.send()
def test_recurring_invoice(self): from webnotes.utils import now_datetime, get_first_day, get_last_day, add_to_date today = now_datetime().date() base_si = webnotes.bean(copy=test_records[0]) base_si.doc.fields.update({ "convert_into_recurring_invoice": 1, "recurring_type": "Monthly", "notification_email_address": "[email protected], [email protected], [email protected]", "repeat_on_day_of_month": today.day, "posting_date": today, "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(today) }) # monthly si1 = webnotes.bean(copy=base_si.doclist) si1.insert() si1.submit() self._test_recurring_invoice(si1, True) # monthly without a first and last day period si2 = webnotes.bean(copy=base_si.doclist) si2.doc.fields.update({ "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, days=30) }) si2.insert() si2.submit() self._test_recurring_invoice(si2, False) # quarterly si3 = webnotes.bean(copy=base_si.doclist) si3.doc.fields.update({ "recurring_type": "Quarterly", "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(add_to_date(today, months=3)) }) si3.insert() si3.submit() self._test_recurring_invoice(si3, True) # quarterly without a first and last day period si4 = webnotes.bean(copy=base_si.doclist) si4.doc.fields.update({ "recurring_type": "Quarterly", "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, months=3) }) si4.insert() si4.submit() self._test_recurring_invoice(si4, False) # yearly si5 = webnotes.bean(copy=base_si.doclist) si5.doc.fields.update({ "recurring_type": "Yearly", "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(add_to_date(today, years=1)) }) si5.insert() si5.submit() self._test_recurring_invoice(si5, True) # yearly without a first and last day period si6 = webnotes.bean(copy=base_si.doclist) si6.doc.fields.update({ "recurring_type": "Yearly", "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, years=1) }) si6.insert() si6.submit() self._test_recurring_invoice(si6, False) # change posting date but keep recuring day to be today si7 = webnotes.bean(copy=base_si.doclist) si7.doc.fields.update({ "posting_date": add_to_date(today, days=-3) }) si7.insert() si7.submit() # setting so that _test function works si7.doc.posting_date = today self._test_recurring_invoice(si7, True)
def validate_posting_time(self): if not self.doc.posting_time: self.doc.posting_time = now_datetime().strftime('%H:%M:%S')
def test_recurring_invoice(self): from webnotes.utils import now_datetime, get_first_day, get_last_day, add_to_date today = now_datetime().date() base_si = webnotes.bean(copy=test_records[0]) base_si.doc.fields.update({ "convert_into_recurring_invoice": 1, "recurring_type": "Monthly", "notification_email_address": "[email protected], [email protected], [email protected]", "repeat_on_day_of_month": today.day, "posting_date": today, "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(today) }) # monthly si1 = webnotes.bean(copy=base_si.doclist) si1.insert() si1.submit() self._test_recurring_invoice(si1, True) # monthly without a first and last day period si2 = webnotes.bean(copy=base_si.doclist) si2.doc.fields.update({ "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, days=30) }) si2.insert() si2.submit() self._test_recurring_invoice(si2, False) # quarterly si3 = webnotes.bean(copy=base_si.doclist) si3.doc.fields.update({ "recurring_type": "Quarterly", "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(add_to_date(today, months=3)) }) si3.insert() si3.submit() self._test_recurring_invoice(si3, True) # quarterly without a first and last day period si4 = webnotes.bean(copy=base_si.doclist) si4.doc.fields.update({ "recurring_type": "Quarterly", "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, months=3) }) si4.insert() si4.submit() self._test_recurring_invoice(si4, False) # yearly si5 = webnotes.bean(copy=base_si.doclist) si5.doc.fields.update({ "recurring_type": "Yearly", "invoice_period_from_date": get_first_day(today), "invoice_period_to_date": get_last_day(add_to_date(today, years=1)) }) si5.insert() si5.submit() self._test_recurring_invoice(si5, True) # yearly without a first and last day period si6 = webnotes.bean(copy=base_si.doclist) si6.doc.fields.update({ "recurring_type": "Yearly", "invoice_period_from_date": today, "invoice_period_to_date": add_to_date(today, years=1) }) si6.insert() si6.submit() self._test_recurring_invoice(si6, False) # change posting date but keep recuring day to be today si7 = webnotes.bean(copy=base_si.doclist) si7.doc.fields.update({"posting_date": add_to_date(today, days=-1)}) si7.insert() si7.submit() # setting so that _test function works si7.doc.posting_date = today self._test_recurring_invoice(si7, True)