Merge lp:~dholbach/developer-ubuntu-com/1565639 into lp:developer-ubuntu-com

Proposed by Daniel Holbach
Status: Merged
Approved by: David Callé
Approved revision: 211
Merged at revision: 206
Proposed branch: lp:~dholbach/developer-ubuntu-com/1565639
Merge into: lp:developer-ubuntu-com
Diff against target: 205 lines (+98/-19)
5 files modified
md_importer/importer/publish.py (+16/-16)
md_importer/importer/repo.py (+8/-2)
md_importer/importer/tools.py (+12/-0)
md_importer/tests/test_branch_import.py (+44/-1)
md_importer/tests/test_website_import.py (+18/-0)
To merge this branch: bzr merge lp:~dholbach/developer-ubuntu-com/1565639
Reviewer Review Type Date Requested Status
Ubuntu App Developer site developers Pending
Review via email: mp+290855@code.launchpad.net

Description of the change

Test case http://paste.ubuntu.com/15613355/

Just create a root page and a child of it called snappy.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'md_importer/importer/publish.py'
2--- md_importer/importer/publish.py 2016-03-08 13:07:19 +0000
3+++ md_importer/importer/publish.py 2016-04-04 14:07:41 +0000
4@@ -1,11 +1,12 @@
5 from md_importer.importer import (
6 DEFAULT_LANG,
7 DEFAULT_TEMPLATE,
8- HOME_PAGE_URL,
9 )
10+from md_importer.importer.tools import remove_leading_and_trailing_slash
11
12 from cms.api import create_page, add_plugin
13-from cms.models import Title
14+from cms.models import Page, Title
15+from cms.utils.page_resolver import get_page_from_path
16 from djangocms_text_ckeditor.html import clean_html
17
18 from bs4 import BeautifulSoup
19@@ -25,22 +26,21 @@
20
21
22 def _find_parent(full_url):
23- if full_url == HOME_PAGE_URL:
24- # If we set up the homepage, we don't need a parent.
25- return None
26- parent_url = re.sub(
27- r'^\/{}\/'.format(DEFAULT_LANG),
28+ parent_url = remove_leading_and_trailing_slash(re.sub(
29+ r'^\/None|{}\/'.format(DEFAULT_LANG),
30 '',
31- os.path.dirname(full_url))
32-
33- parent_pages = Title.objects.select_related('page').filter(
34- path__regex=parent_url, language=DEFAULT_LANG).filter(
35- publisher_is_draft=True)
36- if not parent_pages:
37- logging.error('Parent {} not found.'.format(
38- parent_url))
39+ os.path.dirname(full_url)))
40+ parent_url = os.path.dirname(full_url)
41+ if not parent_url:
42+ root = Page.objects.get_home()
43+ if not root:
44+ return None
45+ return root.get_draft_object()
46+ parent = get_page_from_path(parent_url, draft=True)
47+ if not parent:
48+ logging.error('Parent {} not found.'.format(parent_url))
49 return None
50- return parent_pages[0].page
51+ return parent
52
53
54 def find_text_plugin(page):
55
56=== modified file 'md_importer/importer/repo.py'
57--- md_importer/importer/repo.py 2016-03-09 14:14:08 +0000
58+++ md_importer/importer/repo.py 2016-04-04 14:07:41 +0000
59@@ -76,8 +76,14 @@
60 def execute_import_directives(self):
61 import_list = []
62 # Import single files first
63- for directive in [d for d in self.directives
64- if os.path.isfile(d['import_from'])]:
65+ single_files = [d for d in self.directives
66+ if os.path.isfile(d['import_from'])]
67+ # Sort by number of '/' in write_to - this should ensure that we
68+ # first import entries closer to the root.
69+ single_files.sort(
70+ cmp=lambda x, y:
71+ x['write_to'].count('/')-y['write_to'].count('/'))
72+ for directive in single_files:
73 import_list += [
74 (directive['import_from'], directive['write_to'],
75 directive['advertise'], directive['template'])
76
77=== added file 'md_importer/importer/tools.py'
78--- md_importer/importer/tools.py 1970-01-01 00:00:00 +0000
79+++ md_importer/importer/tools.py 2016-04-04 14:07:41 +0000
80@@ -0,0 +1,12 @@
81+def remove_leading_and_trailing_slash(url):
82+ if url.startswith('/'):
83+ url = url[1:]
84+ if url.endswith('/'):
85+ url = url[:-1]
86+ return url
87+
88+
89+def remove_trailing_slash(url):
90+ if url.endswith('/'):
91+ url = url[:-1]
92+ return url
93
94=== modified file 'md_importer/tests/test_branch_import.py'
95--- md_importer/tests/test_branch_import.py 2016-03-08 13:07:19 +0000
96+++ md_importer/tests/test_branch_import.py 2016-04-04 14:07:41 +0000
97@@ -1,4 +1,5 @@
98 from datetime import datetime
99+import os
100 import pytz
101 import shutil
102
103@@ -10,10 +11,51 @@
104 )
105 from md_importer.importer.article import Article
106 from md_importer.importer.publish import find_text_plugin
107+from md_importer.importer.tools import remove_trailing_slash
108
109 from .utils import TestLocalBranchImport
110
111
112+class TestImportDirectivesBuildHierarchyImport(TestLocalBranchImport):
113+ '''
114+ Build a article tree structure from files in the snapcraft tree.
115+ Make sure the top-most articles (in the tree hierarchy) are imported
116+ first, so the tree looks like this:
117+ a
118+ +-- b
119+ +------ c
120+ | +-- d
121+ | | +-- e
122+ | | +-- f
123+ +-- c2 |
124+ +-- d2
125+ Files in the import directives are random in the beginning.
126+ '''
127+ def runTest(self):
128+ self.create_repo('data/snapcraft-test')
129+ self.repo.add_directive('docs/debug.md', 'a/b/c/d')
130+ self.repo.add_directive('docs/intro.md', 'a/b')
131+ self.repo.add_directive('docs/mir-snaps.md', 'a/b/c/d/e/f')
132+ self.repo.add_directive('docs/reference.md', 'a')
133+ self.repo.add_directive('docs/snapcraft-advanced-features.md', 'a/b/c')
134+ self.repo.add_directive('docs/snapcraft-syntax.md', 'a/b/c2')
135+ self.repo.add_directive('docs/upload-your-snap.md', 'a/b/c/d2')
136+ self.repo.add_directive('docs/get-started.md', 'a/b/c/d/e')
137+ self.assertEqual(len(self.repo.directives), 8)
138+ self.assertTrue(self.repo.execute_import_directives())
139+ self.assertEqual(len(self.repo.imported_articles), 8)
140+ self.assertTrue(self.repo.publish())
141+ pages = Page.objects.all()
142+ self.assertGreater(pages.count(), len(self.repo.imported_articles))
143+ # Make sure we got the parents right
144+ for article in self.repo.imported_articles:
145+ parent_url = remove_trailing_slash(
146+ article.page.parent.get_absolute_url())
147+ url = remove_trailing_slash(article.page.get_absolute_url())
148+ self.assertEqual(parent_url, os.path.split(url)[0])
149+ self.assertIsInstance(article, Article)
150+
151+
152 class TestOneDirImport(TestLocalBranchImport):
153 def runTest(self):
154 self.create_repo('data/snapcraft-test')
155@@ -117,7 +159,8 @@
156 self.assertTrue(self.repo.publish())
157 for page in Page.objects.filter(publisher_is_draft=False):
158 if page.parent is not None:
159- self.assertEqual(page.parent_id, self.root.id)
160+ parent = page.parent.get_public_object()
161+ self.assertEqual(parent.id, self.root.id)
162 self.assertTrue(page.in_navigation)
163
164
165
166=== modified file 'md_importer/tests/test_website_import.py'
167--- md_importer/tests/test_website_import.py 2016-03-22 10:23:18 +0000
168+++ md_importer/tests/test_website_import.py 2016-04-04 14:07:41 +0000
169@@ -51,18 +51,36 @@
170 '/en/snappy/start/',
171 '/en/snappy/start/as-dev/',
172 '/en/snappy/start/as-dev/16-04/',
173+ '/en/snappy/start/as-dev/16-04/step2-setup-azure-macos/',
174+ '/en/snappy/start/as-dev/16-04/step2-setup-azure-ubuntu/',
175+ '/en/snappy/start/as-dev/16-04/step2-setup-azure-windows/',
176 '/en/snappy/start/as-dev/16-04/step2-setup-beaglebone-macos/',
177 '/en/snappy/start/as-dev/16-04/step2-setup-beaglebone-ubuntu/',
178 '/en/snappy/start/as-dev/16-04/step2-setup-beaglebone-windows/',
179 '/en/snappy/start/as-dev/16-04/step2-setup-dragonboard-macos/',
180 '/en/snappy/start/as-dev/16-04/step2-setup-dragonboard-ubuntu/',
181 '/en/snappy/start/as-dev/16-04/step2-setup-dragonboard-windows/',
182+ '/en/snappy/start/as-dev/16-04/step2-setup-ec2-macos/',
183+ '/en/snappy/start/as-dev/16-04/step2-setup-ec2-ubuntu/',
184+ '/en/snappy/start/as-dev/16-04/step2-setup-ec2-windows/',
185+ '/en/snappy/start/as-dev/16-04/step2-setup-gce-macos/',
186+ '/en/snappy/start/as-dev/16-04/step2-setup-gce-ubuntu/',
187+ '/en/snappy/start/as-dev/16-04/step2-setup-gce-windows/',
188 '/en/snappy/start/as-dev/16-04/step2-setup-intel-nuc-macos/',
189 '/en/snappy/start/as-dev/16-04/step2-setup-intel-nuc-ubuntu/',
190 '/en/snappy/start/as-dev/16-04/step2-setup-intel-nuc-windows/',
191+ '/en/snappy/start/as-dev/16-04/step2-setup-kvm-macos/',
192+ '/en/snappy/start/as-dev/16-04/step2-setup-kvm-ubuntu/',
193+ '/en/snappy/start/as-dev/16-04/step2-setup-kvm-windows/',
194 '/en/snappy/start/as-dev/16-04/step2-setup-rpi2-macos/',
195 '/en/snappy/start/as-dev/16-04/step2-setup-rpi2-ubuntu/',
196 '/en/snappy/start/as-dev/16-04/step2-setup-rpi2-windows/',
197+ '/en/snappy/start/as-dev/16-04/step2-setup-vagrant-macos/',
198+ '/en/snappy/start/as-dev/16-04/step2-setup-vagrant-ubuntu/',
199+ '/en/snappy/start/as-dev/16-04/step2-setup-vagrant-windows/',
200+ '/en/snappy/start/as-dev/16-04/step2-setup-x86-device-macos/',
201+ '/en/snappy/start/as-dev/16-04/step2-setup-x86-device-ubuntu/',
202+ '/en/snappy/start/as-dev/16-04/step2-setup-x86-device-windows/',
203 '/en/snappy/start/as-dev/16-04/step3-get-familiar/',
204 '/en/snappy/start/as-dev/16-04/step4-first-snap/',
205 '/en/snappy/start/as-dev/16-04/step5-further-readings/',

Subscribers

People subscribed via source and target branches