Skip to content

Commit 9e8ad79

Browse files
committed
Almost there, just need to writ this test
1 parent 72df7a3 commit 9e8ad79

File tree

6 files changed

+58
-25
lines changed

6 files changed

+58
-25
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.6.0
3+
rev: v5.0.0
44
hooks:
55
- id: trailing-whitespace
66
- id: mixed-line-ending
@@ -15,7 +15,7 @@ repos:
1515
exclude: .pre-commit-config.yaml
1616
- id: pt_structure
1717
- repo: https://github.com/astral-sh/ruff-pre-commit
18-
rev: v0.6.8
18+
rev: v0.6.9
1919
hooks:
2020
- id: ruff
2121
args: [ "--fix" ]
@@ -33,7 +33,7 @@ repos:
3333
- id: sass-lint
3434
files: '^src/.*\.scss'
3535
- repo: https://github.com/pre-commit/mirrors-eslint
36-
rev: v9.11.1
36+
rev: v9.12.0
3737
hooks:
3838
- id: eslint
3939
files: '^src/.*\.jsx?$'

src/onegov/org/locale/de_CH/LC_MESSAGES/onegov.org.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,9 +5858,9 @@ msgstr "Total ${number} Links gefunden"
58585858

58595859
#, python-format
58605860
msgid ""
5861-
"Migrates links from the given domain to the current domain \"${domain}\"."
5861+
"Migrates links from the given domain to the current domain \"${domain}\". The form must be used *after* the DNS changeover to \"${domain}\"."
58625862
msgstr ""
5863-
"Migriert Links von der angegebenen Domain zur aktuellen Domain \"${domain}\"."
5863+
"Migriert Links von der angegebenen Domain zur aktuellen Domain \"${domain}\". Das Formular muss *nach* der DNS-Umstellung auf \"${domain}\" verwendet werden."
58645864

58655865
msgid "OneGov API"
58665866
msgstr "OneGov API"

src/onegov/org/locale/fr_CH/LC_MESSAGES/onegov.org.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5877,9 +5877,9 @@ msgstr "Total de ${number} liens trouvés"
58775877

58785878
#, python-format
58795879
msgid ""
5880-
"Migrates links from the given domain to the current domain \"${domain}\"."
5880+
"Migrates links from the given domain to the current domain \"${domain}\". The form must be used *after* the DNS changeover to \"${domain}\"."
58815881
msgstr ""
5882-
"Fait migrer les liens du domaine donné vers le domaine actuel \"${domain}\"."
5882+
"Fait migrer les liens du domaine donné vers le domaine actuel \"${domain}\". Le formulaire doit être utilisé *après* le passage du DNS à \"${domain}\"."
58835883

58845884
msgid "OneGov API"
58855885
msgstr "API OneGov"

src/onegov/org/management.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from aiohttp import ClientTimeout
66
from sqlalchemy.orm import object_session
77
from urlextract import URLExtract
8-
from sqlalchemy import text, bindparam
8+
from sqlalchemy import text, bindparam, String
99
from onegov.async_http.fetch import async_aiohttp_get_all
1010
from onegov.core.utils import normalize_for_url
1111
from onegov.org.models import SiteCollection
@@ -135,8 +135,12 @@ def migrate_site_collection(
135135
return total, grouped
136136

137137
def migrate_content_mixin(self) -> None:
138-
""" Updates the JSON and text columns defined in models using the
139-
ContentMixin to replace old URIs with new ones across multiple tables.
138+
""" A catch-all function to migrate content not covered by
139+
migrate_site_collection.
140+
141+
This function was added to handle links that were not processed by the
142+
migrate_site_collection function.
143+
140144
141145
Generates SQL of the following form:
142146
@@ -173,17 +177,20 @@ def replace_text(col: str) -> str:
173177

174178
sql = text(
175179
f"UPDATE {table} SET {', '.join(sql_parts)}" # nosec:B608
180+
).bindparams(
181+
bindparam('old_uri', type_=String),
182+
bindparam('new_uri', type_=String)
176183
)
177184

178185
self.request.session.execute(
179186
sql,
180187
{
181-
'old_uri': bindparam('old_uri', self.old_uri),
182-
'new_uri': bindparam('new_uri', self.new_uri)
188+
'old_uri': self.old_uri,
189+
'new_uri': self.new_uri
183190
}
184191
)
185192

186-
self.request.session.commit()
193+
transaction.commit()
187194

188195

189196
class PageNameChange(ModelsWithLinksMixin):

src/onegov/org/views/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ def handle_migrate_links(
376376
'button_text': button_text,
377377
'callout': _(
378378
'Migrates links from the given domain to the current domain '
379-
'"${domain}".',
379+
'"${domain}". The form must be used *after* the DNS changeover '
380+
'to "${domain}".',
380381
mapping={'domain': domain}
381382
),
382383
}

tests/onegov/org/test_views_settings.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from onegov.api.models import ApiKey
2+
import pytest
3+
from onegov.page import Page
24
from onegov.org.theme.org_theme import HELVETICA
35
from xml.etree.ElementTree import tostring
46
from onegov.org.models import SiteCollection
@@ -146,28 +148,51 @@ def test_switch_languages(client):
146148
assert 'Deutsch' not in page
147149

148150

151+
@pytest.mark.skip('wip')
149152
def test_migrate_links(client):
150153
session = client.app.session()
151154

152155
sitecollection = SiteCollection(session)
153156
objects = sitecollection.get()
154157

155-
# pages = client.app.session().query(Topic).defer(Topic.)
156-
# topics = session.query(Topic)
157-
#
158-
# topics = topics.options(defer(Topic.meta))
159-
# topics = topics.options(defer(Topic.content))
160-
# topics = topics.options(defer(Topic.order))
158+
foo = [page.content for page in objects['topics']]
161159

160+
# set one of the pages content and set
161+
# content.text to something that contains an url.
162+
163+
# Set up test data
164+
old_domain = 'localhost'
165+
new_domain = '127.0.0.1'
166+
167+
# Create test objects with content containing the old domain
168+
page = Page(
169+
name='page',
170+
title='Page',
171+
content={'text': f'A link to http://{old_domain}/page'},
172+
)
173+
# form = Form(meta={'url': f'http://{old_domain}/form'})
174+
# event = Event(content={'description': f'Visit us at {old_domain}'})
175+
# resource = Resource(meta={'link': f'https://{old_domain}/resource'})
176+
# person = Person(picture_url=f'http://{old_domain}/person.jpg')
177+
# org = Organisation(logo_url=f'http://{old_domain}/logo.png')
178+
# directory = Directory(content={'entries': [f'http://{old_domain}/entry1',
179+
# f'http://{old_domain}/entry2']})
180+
# ticket = Ticket(snapshot={'url': f'http://{old_domain}/ticket'})
181+
182+
# external_link = ExternalLink(url=f'http://{old_domain}/external')
183+
184+
# todo: this is hard to test, need to bypass form validation
185+
# Die Domain muss einen Punkt enthalten
186+
session.add_all([page])
187+
session.flush()
162188
client.login_admin()
163189
page = client.get('/migrate-links')
190+
164191
assert (
165192
'Migriert Links von der angegebenen Domain zur aktuellen Domain '
166193
'"localhost"' in page
167194
)
168-
169-
# migrate from 'localhost' to 127.0.0.1
170-
# (pointless but enough for test)
171-
page.form['old_domain'] = '127.0.0.1'
172-
page.form.submit()
195+
page.form['old_domain'] = old_domain
196+
page.form['test'] = False
197+
page = page.form.submit().maybe_follow()
173198
page.showbrowser()

0 commit comments

Comments
 (0)