[Improvements] Auto-publish pages with date ranges (#5430)

* Add feature for auto-(un)publishing Web Pages

* Move some publish logic to frontend

* Cleanup some logic

* Remove arbitrary limits and add check for start date
This commit is contained in:
Alchez 2018-05-30 11:34:05 +05:30 committed by Rushabh Mehta
parent c71c132ba2
commit 0dcf3ca61d
4 changed files with 131 additions and 11 deletions

View file

@ -141,7 +141,8 @@ scheduler_events = {
"frappe.oauth.delete_oauth2_data",
"frappe.integrations.doctype.razorpay_settings.razorpay_settings.capture_payment",
"frappe.twofactor.delete_all_barcodes_for_users",
"frappe.integrations.doctype.gcalendar_settings.gcalendar_settings.sync"
"frappe.integrations.doctype.gcalendar_settings.gcalendar_settings.sync",
"frappe.website.doctype.web_page.web_page.check_publish_status"
],
"hourly": [
"frappe.model.utils.link_count.update_link_count",

View file

@ -29,3 +29,20 @@ $.extend(cur_frm.cscript, {
cur_frm.cscript.layout(doc);
}
});
frappe.ui.form.on("Web Page", {
published: function (frm) {
// If current date is before end date,
// and web page is manually unpublished,
// set end date to current date.
if (!frm.doc.published && frm.doc.end_date) {
var end_date = frappe.datetime.str_to_obj(frappe.datetime.now_datetime());
// Set date a few seconds in the future to avoid throwing
// start and end date validation error
end_date.setSeconds(end_date.getSeconds() + 5)
frm.set_value("end_date", end_date);
}
}
})

View file

@ -221,6 +221,66 @@
"set_only_once": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "start_date",
"fieldtype": "Datetime",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Start Date",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"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
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "end_date",
"fieldtype": "Datetime",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "End Date",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"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
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
@ -834,7 +894,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 20,
"modified": "2017-09-24 20:58:41.588919",
"modified": "2018-01-15 02:16:05.251006",
"modified_by": "Administrator",
"module": "Website",
"name": "Web Page",

View file

@ -1,19 +1,28 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals, print_function
import frappe, re
import requests, requests.exceptions
from frappe.utils import strip_html
from frappe.website.website_generator import WebsiteGenerator
from frappe.website.router import resolve_route
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
from frappe.website.utils import find_first_image, get_comment_list, extract_title
from frappe.utils.jinja import render_template
from __future__ import print_function, unicode_literals
import re
import requests
import requests.exceptions
from jinja2.exceptions import TemplateSyntaxError
import frappe
from frappe import _
from frappe.utils import get_datetime, now, strip_html
from frappe.utils.jinja import render_template
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
from frappe.website.router import resolve_route
from frappe.website.utils import extract_title, find_first_image, get_comment_list
from frappe.website.website_generator import WebsiteGenerator
class WebPage(WebsiteGenerator):
def validate(self):
self.validate_dates()
def get_feed(self):
return self.title
@ -121,6 +130,39 @@ class WebPage(WebsiteGenerator):
if image:
context.metatags["image"] = image
def validate_dates(self):
if self.end_date:
if self.start_date and get_datetime(self.end_date) < get_datetime(self.start_date):
frappe.throw(_("End Date cannot be before Start Date!"))
# If the current date is past end date, and
# web page is published, empty the end date
if self.published and now() > self.end_date:
self.end_date = None
frappe.msgprint(_("Clearing end date, as it cannot be in the past for published pages."))
def check_publish_status():
web_pages = frappe.get_all("Web Page", fields=["name", "published", "start_date", "end_date"])
now_date = get_datetime(now())
for page in web_pages:
start_date = page.start_date if page.start_date else ""
end_date = page.end_date if page.end_date else ""
if page.published:
# Unpublish pages that are outside the set date ranges
if (start_date and now_date < start_date) or (end_date and now_date > end_date):
frappe.db.set_value("Web Page", page.name, "published", 0)
else:
# Publish pages that are inside the set date ranges
if start_date:
if not end_date or (end_date and now_date < end_date):
frappe.db.set_value("Web Page", page.name, "published", 1)
def check_broken_links():
cnt = 0
for p in frappe.db.sql("select name, main_section from `tabWeb Page`", as_dict=True):