refactor: Delete unused code

This commit is contained in:
Suraj Shetty 2021-05-19 19:08:22 +05:30
parent c26c5d9ef0
commit adb4bc8e30
4 changed files with 14 additions and 185 deletions

View file

@ -1,23 +1,19 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import print_function, unicode_literals
import re
import requests
import requests.exceptions
from jinja2.exceptions import TemplateSyntaxError
import frappe
from frappe.utils import get_datetime, now, strip_html, quoted
from frappe import _
from frappe.utils import get_datetime, now, quoted, 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,
get_html_content_based_on_type)
from frappe.website.website_generator import WebsiteGenerator
from frappe.utils.safe_exec import safe_exec
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
from frappe.website.utils import (extract_title, find_first_image,
get_comment_list, get_html_content_based_on_type)
from frappe.website.website_generator import WebsiteGenerator
class WebPage(WebsiteGenerator):
@ -184,32 +180,6 @@ def check_publish_status():
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):
for link in re.findall('href=["\']([^"\']*)["\']', p.main_section):
if link.startswith("http"):
try:
res = requests.get(link)
except requests.exceptions.SSLError:
res = frappe._dict({"status_code": "SSL Error"})
except requests.exceptions.ConnectionError:
res = frappe._dict({"status_code": "Connection Error"})
if res.status_code!=200:
print("[{0}] {1}: {2}".format(res.status_code, p.name, link))
cnt += 1
else:
link = link[1:] # remove leading /
link = link.split("#")[0]
if not resolve_route(link):
print(p.name + ":" + link)
cnt += 1
print("{0} links broken".format(cnt))
def get_web_blocks_html(blocks):
'''Converts a list of blocks into Raw HTML and extracts out their scripts for deduplication'''

View file

@ -1,15 +1,13 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from six.moves.urllib.parse import quote
from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.utils import get_request_site_address, encode
from frappe.model.document import Document
from six.moves.urllib.parse import quote
from frappe.website.router import resolve_route
from frappe.website.doctype.website_theme.website_theme import add_website_theme
from frappe.integrations.doctype.google_settings.google_settings import get_auth_url
from frappe.model.document import Document
from frappe.utils import encode, get_request_site_address
from frappe.website.doctype.website_theme.website_theme import add_website_theme
INDEXING_SCOPES = "https://www.googleapis.com/auth/indexing"
@ -23,7 +21,8 @@ class WebsiteSettings(Document):
def validate_home_page(self):
if frappe.flags.in_install:
return
if self.home_page and not resolve_route(self.home_page):
from frappe.website.path_resolver import PathResolver
if self.home_page and not PathResolver(self.home_page).is_valid_path():
frappe.msgprint(_("Invalid Home Page") + " (Standard pages - index, login, products, blog, about, contact)")
self.home_page = ''

View file

@ -5,130 +5,9 @@ import io
import os
import re
from werkzeug.routing import Map, NotFound, Rule
import frappe
from frappe.model.document import get_controller
from frappe.website.utils import can_cache, extract_comment_tag, extract_title
def resolve_route(path):
"""Returns the page route object based on searching in pages and generators.
The `www` folder is also a part of generator **Web Page**.
The only exceptions are `/about` and `/contact` these will be searched in Web Pages
first before checking the standard pages."""
if path not in ("about", "contact"):
context = get_page_info_from_template(path)
if context:
return context
return get_page_context_from_doctype(path)
else:
context = get_page_context_from_doctype(path)
if context:
return context
return get_page_info_from_template(path)
def get_page_context(path):
page_context = None
if can_cache():
page_context_cache = frappe.cache().hget("page_context", path) or {}
page_context = page_context_cache.get(frappe.local.lang, None)
if not page_context:
page_context = make_page_context(path)
if can_cache(page_context.no_cache):
page_context_cache[frappe.local.lang] = page_context
frappe.cache().hset("page_context", path, page_context_cache)
return page_context
def make_page_context(path):
context = resolve_route(path)
if not context:
raise frappe.PageDoesNotExistError
context.doctype = context.ref_doctype
if context.page_title:
context.title = context.page_title
context.pathname = frappe.local.path
return context
def get_page_info_from_template(path):
'''Return page_info from path'''
for app in frappe.get_installed_apps(frappe_last=True):
app_path = frappe.get_app_path(app)
folders = get_start_folders()
for start in folders:
search_path = os.path.join(app_path, start, path)
options = (search_path, search_path + '.html', search_path + '.md',
search_path + '/index.html', search_path + '/index.md')
for o in options:
option = frappe.as_unicode(o)
if os.path.exists(option) and not os.path.isdir(option):
return get_page_info(option, app, start, app_path=app_path)
return None
def get_page_context_from_doctype(path):
page_info = get_page_info_from_doctypes(path)
if not page_info:
page_info = get_page_info_from_web_page_with_dynamic_routes(path)
if page_info:
return frappe.get_doc(page_info.get("doctype"),
page_info.get("name")).get_page_info()
def get_all_page_context_from_doctypes():
'''
Get all doctype generated routes (for sitemap.xml)
'''
routes = frappe.cache().get_value("website_generator_routes")
if not routes:
routes = get_page_info_from_doctypes()
frappe.cache().set_value("website_generator_routes", routes)
return routes
def get_page_info_from_doctypes(path=None):
'''
Find a document with matching `route` from all doctypes with `has_web_view`=1
'''
routes = {}
for doctype in get_doctypes_with_web_view():
filters = {}
controller = get_controller(doctype)
meta = frappe.get_meta(doctype)
condition_field = (meta.is_published_field or
# custom doctypes dont have controllers and no website attribute
(controller.website.condition_field if not meta.custom else None))
if condition_field:
filters[condition_field] = 1
if path:
filters['route'] = path
try:
for r in frappe.get_all(doctype, fields = ['name', 'route', 'modified'],
filters = filters, limit = 1):
routes[r.route] = {"doctype": doctype, "name": r.name, "modified": r.modified}
# just want one path, return it!
if path:
return routes[r.route]
except Exception as e:
if not frappe.db.is_missing_column(e): raise e
return routes
from frappe.website.utils import extract_comment_tag, extract_title
from werkzeug.routing import Map, Rule, NotFound
def get_page_info_from_web_page_with_dynamic_routes(path):
'''

View file

@ -371,25 +371,6 @@ def extract_comment_tag(source, tag):
return None
def add_missing_headers():
'''Walk and add missing headers in docs (to be called from bench execute)'''
path = frappe.get_app_path('erpnext', 'docs')
for basepath, folders, files in os.walk(path):
for fname in files:
if fname.endswith('.md'):
with open(os.path.join(basepath, fname), 'r') as f:
content = frappe.as_unicode(f.read())
if not content.startswith('# ') and not '<h1>' in content:
with open(os.path.join(basepath, fname), 'w') as f:
if fname=='index.md':
fname = os.path.basename(basepath)
else:
fname = fname[:-3]
h = fname.replace('_', ' ').replace('-', ' ').title()
content = '# {0}\n\n'.format(h) + content
f.write(content.encode('utf-8'))
def get_html_content_based_on_type(doc, fieldname, content_type):
'''
Set content based on content_type