-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: CMD-187 autocreate search page #885
base: main
Are you sure you want to change the base?
Conversation
So that it can be loaded from CDN. Use cases are: - if search template is broken - for custom search page - for non-Core website
Compared to "cms-search", "google-search" is: - more accurate - more obvious to those not familiar with Core-CMS
So dev sees 'google-search' from CSS before seeing it in HTML `id` attr.
Caused server error.
This had create dbreadcrumsb before.
Final markup undecided, so keep it simple, and easy to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any thoughts on why create_page()
is run twice?
I have checks for existing page and fallbacks for missing page.
def ready(self): | ||
if settings.SEARCH_PAGE_AUTO_SETUP: | ||
from .utils import create_page | ||
create_page() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
This create_page
function is run twice on website startup.
1. Initial Deploy on Existing Site (When App Has Not Yet Been Installed).
Internal Server Error. Re-deploy required.
Log
Note: treebeard
is an application that manages pages.
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg/cursor.py", line 732, in execute
raise ex.with_traceback(None)
psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "cms_treenode_path_key"
DETAIL: Key (path)=(0004) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/code/taccsite_cms/wsgi.py", line 16, in <module>
application = get_wsgi_application()
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.11/site-packages/django/apps/registry.py", line 124, in populate
app_config.ready()
File "/code/apps/search_page/apps.py", line 10, in ready
create_page()
File "/code/apps/search_page/utils.py", line 46, in create_page
page = create_cms_page(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/contextlib.py", line 81, in inner
return func(*args, **kwds)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/cms/api.py", line 183, in create_page
page.set_tree_node(site=site, target=target_node, position=position)
File "/usr/local/lib/python3.11/site-packages/cms/models/pagemodel.py", line 474, in set_tree_node
self.node = TreeNode.add_root(instance=new_node)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/treebeard/mp_tree.py", line 626, in add_root
return MP_AddRootHandler(cls, **kwargs).process()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/treebeard/mp_tree.py", line 344, in process
newobj.save()
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
self.save_base(
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 877, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 1020, in _save_table
results = self._do_insert(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 1061, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1805, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1822, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg/cursor.py", line 732, in execute
raise ex.with_traceback(None)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "cms_treenode_path_key"
DETAIL: Key (path)=(0004) already exists.
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1)
spawned uWSGI worker 1 (pid: 16, cores: 20)
spawned uWSGI worker 2 (pid: 24, cores: 20)
spawned uWSGI worker 3 (pid: 37, cores: 20)
spawned uWSGI worker 4 (pid: 66, cores: 20)
--- no python application found, check your startup logs for errors ---
[pid: 66|app: -1|req: -1/1] 129.114.111.125 () {68 vars in 2061 bytes} [Fri Oct 25 10:01:21 2024] GET /admin/cms/page/?language=en => generated 21 bytes in 0 msecs (HTTP/2.0 500) 2 headers in 83 bytes (0 switches on core 2)
--- no python application found, check your startup logs for errors ---
[pid: 16|app: -1|req: -1/2] 129.114.111.125 () {66 vars in 1951 bytes} [Fri Oct 25 10:01:26 2024] GET / => generated 21 bytes in 0 msecs (HTTP/2.0 500) 2 headers in 83 bytes (0 switches on core 1)
2. Re-Deploy.
Page is created and works. Logs note existing page.
Log
[DJANGO] INFO 2024-10-25 10:03:53,120 UTC utils portal.apps.search_page.utils.create_page:62: Found existing search page at "search"
3. Delete Search Page. Deploy Again.
Page is created and works. Logs note existing page.
Log
[DJANGO] INFO 2024-10-25 10:03:53,120 UTC utils portal.apps.search_page.utils.create_page:62: Found existing search page at "search"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jarosenb gave me a good internal review:
It sounds like what you're trying to create is a custom migration, if this thing really needs to be a CMS page.
@taoteg suggested a guide:
A.I. has given me starter code.
I have the page creation function I can adapt. I believe I can replace the complexity of the app with one file.
I'll help you create a Django CMS Page using a database migration. This is useful for automatically creating pages during deployment.
Here's how to create a new migration file that will create a CMS page:
XXXX_create_custom_page.py from django.db import migrations from cms.api import create_page from cms.models import Page def create_cms_page(apps, schema_editor): # Skip if page already exists if not Page.objects.filter(title_set__title='Your Page Title').exists(): page = create_page( title='Your Page Title', template='page.html', # Use your template language='en', slug='your-page-slug', published=True, in_navigation=True, ) page.publish('en') def remove_cms_page(apps, schema_editor): # Delete the page if it exists Page.objects.filter(title_set__title='Your Page Title').delete() class Migration(migrations.Migration): dependencies = [ ('cms', '0022_auto_20180620_1551'), # Adjust this to your last migration ] operations = [ migrations.RunPython(create_cms_page, remove_cms_page) ]To use this:
- Create a new migration file in your CMS app's migrations folder
- Replace
XXXX
in the filename with the next available migration number- Adjust the
dependencies
to point to your last migration- Customize the page properties in
create_cms_page
:
title
: The page titletemplate
: Your template nameslug
: URL-friendly version of the titlein_navigation
: Whether to show in menusThen run:
python manage.py migrateThe migration will create the page if it doesn't exist already. The
remove_cms_page
function handles rollback if you need to reverse the migration.Note: Make sure you have the necessary imports and that your template exists before running the migration.
Overview
Support a Google Search page with less effort.
What Effort?
Every Time:
code→ ID.SEARCH
settings.Previously:
/search
page.Now, Instead:
Caution
Do not merge.
Warning
Successful run requires two deploys. Help wanted.
Related
Instances:
Private, so no results.
Private, but has results from public LCCF.
Private still, so no results.
Changes
search_page
appSEARCH_AUTO_SETUP
settingGOOGLE_SEARCH_ENGINE_ID
settingTesting
GOOGLE_SEARCH_ENGINE_ID
setting e.g.GOOGLE_SEARCH_ENGINE_ID = 'b099996c09ebd4ece'
SEARCH_AUTO_SETUP = True
setting.GOOGLE_SEARCH_ENGINE_ID
setting.UI
GOOGLE_SEARCH_ENGINE_ID
GOOGLE_SEARCH_ENGINE_ID