def test_caching(self): # to enable caching frappe.flags.force_website_cache = True print(frappe.session.user) clear_website_cache() # first response no-cache pages = frappe.get_all('Blog Post', fields=['name', 'route'], filters={ 'published': 1, 'title': "_Test Blog Post" }, limit=1) route = pages[0].route set_request(path=route) # response = get_response() response = get_response() # TODO: enable this assert # self.assertIn(('X-From-Cache', 'False'), list(response.headers)) set_request(path=route) response = get_response() self.assertIn(('X-From-Cache', 'True'), list(response.headers)) frappe.flags.force_website_cache = True
def clear_global_cache(): from frappe.website.utils import clear_website_cache clear_doctype_cache() clear_website_cache() frappe.cache().delete_value(global_cache_keys) frappe.cache().delete_value(bench_cache_keys) frappe.setup_module_map()
def test_json_sidebar_data(self): frappe.flags.look_for_sidebar = False content = get_response_content('/_test/_test_folder/_test_page') self.assertNotIn('Test Sidebar', content) clear_website_cache() frappe.flags.look_for_sidebar = True content = get_response_content('/_test/_test_folder/_test_page') self.assertIn('Test Sidebar', content) frappe.flags.look_for_sidebar = False
def post_install(rebuild_website=False): from frappe.website.utils import clear_website_cache if rebuild_website: clear_website_cache() init_singles() frappe.db.commit() frappe.clear_cache()
def clear_website_cache(context): "Clear website cache" from frappe.website.utils import clear_website_cache for site in context.sites: try: frappe.init(site=site) frappe.connect() clear_website_cache() finally: frappe.destroy() if not context.sites: raise SiteNotSpecifiedError
def test_caching(self): # to enable caching frappe.flags.force_website_cache = True clear_website_cache() # first response no-cache response = get_response('/_test/_test_folder/_test_page') self.assertIn(('X-From-Cache', 'False'), list(response.headers)) # first response returned from cache response = get_response('/_test/_test_folder/_test_page') self.assertIn(('X-From-Cache', 'True'), list(response.headers)) frappe.flags.force_website_cache = False
def clear_cache(context): "Clear cache, doctype cache and defaults" import frappe.sessions from frappe.website.utils import clear_website_cache from frappe.desk.notifications import clear_notifications for site in context.sites: try: frappe.connect(site) frappe.clear_cache() clear_notifications() clear_website_cache() finally: frappe.destroy() if not context.sites: raise SiteNotSpecifiedError
def migrate(verbose=True, skip_failing=False, skip_search_index=False): '''Migrate all apps to the current version, will: - run before migrate hooks - run patches - sync doctypes (schema) - sync dashboards - sync jobs - sync fixtures - sync customizations - sync languages - sync web pages (from /www) - run after migrate hooks ''' service_status = check_connection(redis_services=["redis_cache"]) if False in service_status.values(): for service in service_status: if not service_status.get(service, True): print("{} service is not running.".format(service)) print("""Cannot run bench migrate without the services running. If you are running bench in development mode, make sure that bench is running: $ bench start Otherwise, check the server logs and ensure that all the required services are running.""") sys.exit(1) touched_tables_file = frappe.get_site_path('touched_tables.json') if os.path.exists(touched_tables_file): os.remove(touched_tables_file) try: add_column(doctype="DocType", column_name="migration_hash", fieldtype="Data") frappe.flags.touched_tables = set() frappe.flags.in_migrate = True clear_global_cache() #run before_migrate hooks for app in frappe.get_installed_apps(): for fn in frappe.get_hooks('before_migrate', app_name=app): frappe.get_attr(fn)() # run patches frappe.modules.patch_handler.run_all(skip_failing) # sync frappe.model.sync.sync_all() frappe.translate.clear_cache() sync_jobs() sync_fixtures() sync_dashboards() sync_customizations() sync_languages() frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu() # syncs statics clear_website_cache() # updating installed applications data frappe.get_single('Installed Applications').update_versions() #run after_migrate hooks for app in frappe.get_installed_apps(): for fn in frappe.get_hooks('after_migrate', app_name=app): frappe.get_attr(fn)() # build web_routes index if not skip_search_index: # Run this last as it updates the current session print('Building search index for {}'.format(frappe.local.site)) build_index_for_all_routes() frappe.db.commit() clear_notifications() frappe.publish_realtime("version-update") frappe.flags.in_migrate = False finally: with open(touched_tables_file, 'w') as f: json.dump(list(frappe.flags.touched_tables), f, sort_keys=True, indent=4) frappe.flags.touched_tables.clear()
def test_home_page(self): frappe.set_user('Administrator') # test home page via role user = frappe.get_doc(dict( doctype='User', email='*****@*****.**', first_name='test')).insert(ignore_if_duplicate=True) role = frappe.get_doc(dict( doctype = 'Role', role_name = 'home-page-test', desk_access = 0, )).insert(ignore_if_duplicate=True) user.add_roles(role.name) user.save() frappe.db.set_value('Role', 'home-page-test', 'home_page', 'home-page-test') frappe.set_user('*****@*****.**') self.assertEqual(get_home_page(), 'home-page-test') frappe.set_user('Administrator') frappe.db.set_value('Role', 'home-page-test', 'home_page', '') # home page via portal settings frappe.db.set_value('Portal Settings', None, 'default_portal_home', 'test-portal-home') frappe.set_user('*****@*****.**') frappe.cache().hdel('home_page', frappe.session.user) self.assertEqual(get_home_page(), 'test-portal-home') frappe.db.set_value("Portal Settings", None, "default_portal_home", '') clear_website_cache() # home page via website settings frappe.db.set_value("Website Settings", None, "home_page", 'contact') self.assertEqual(get_home_page(), 'contact') frappe.db.set_value("Website Settings", None, "home_page", None) clear_website_cache() # fallback homepage self.assertEqual(get_home_page(), 'me') # fallback homepage for guest frappe.set_user('Guest') self.assertEqual(get_home_page(), 'login') frappe.set_user('Administrator') # test homepage via hooks clear_website_cache() set_home_page_hook('get_website_user_home_page', 'frappe.www._test._test_home_page.get_website_user_home_page') self.assertEqual(get_home_page(), '_test/_test_folder') clear_website_cache() set_home_page_hook('website_user_home_page', 'login') self.assertEqual(get_home_page(), 'login') clear_website_cache() set_home_page_hook('home_page', 'about') self.assertEqual(get_home_page(), 'about') clear_website_cache() set_home_page_hook('role_home_page', {'home-page-test': 'home-page-test'}) self.assertEqual(get_home_page(), 'home-page-test')