0001-public-move-page-resolve-code-to-utils-57672.patch
combo/data/utils.py | ||
---|---|---|
27 | 27 |
from combo.apps.assets.models import Asset |
28 | 28 |
from combo.apps.assets.utils import add_tar_content, clean_assets_files, tar_assets_files, untar_assets_files |
29 | 29 | |
30 |
from .models import Page |
|
30 |
from .models import Page, extract_context_from_sub_slug |
|
31 | ||
32 | ||
33 |
class MissingSubSlug(Exception): |
|
34 |
pass |
|
31 | 35 | |
32 | 36 | |
33 | 37 |
class ImportSiteError(Exception): |
... | ... | |
196 | 200 |
data.update(untar_assets_files(tar, overwrite=overwrite)) |
197 | 201 |
pages = import_site(data, if_empty=if_empty, clean=clean, request=request) |
198 | 202 |
return pages |
203 | ||
204 | ||
205 |
def get_page_from_url_parts(parts, request=None): |
|
206 |
pages = {} |
|
207 |
for page in Page.objects.filter(slug__in=parts): |
|
208 |
if not page.slug in pages: |
|
209 |
pages[page.slug] = [] |
|
210 |
pages[page.slug].append(page) |
|
211 | ||
212 |
if not pages: |
|
213 |
return |
|
214 | ||
215 |
i = 0 |
|
216 |
hierarchy_ids = [None] |
|
217 |
while i < len(parts): |
|
218 |
slug_pages = pages.get(parts[i]) |
|
219 |
if slug_pages is None or len(slug_pages) == 0: |
|
220 |
page = None |
|
221 |
break |
|
222 |
if len(slug_pages) == 1: |
|
223 |
page = slug_pages[0] |
|
224 |
else: |
|
225 |
# multiple pages with same slugs |
|
226 |
try: |
|
227 |
page = [x for x in slug_pages if x.parent_id == hierarchy_ids[-1]][0] |
|
228 |
except IndexError: |
|
229 |
page = None |
|
230 |
break |
|
231 |
if page.parent_id != hierarchy_ids[-1]: |
|
232 |
if i == 0: |
|
233 |
# root page should be at root but maybe the page is a child of |
|
234 |
# /index/, and as /index/ is silent the page would appear |
|
235 |
# directly under /; this is not a suggested practice. |
|
236 |
if page.parent.slug != 'index' and page.parent.parent_id is not None: |
|
237 |
page = None |
|
238 |
break |
|
239 |
else: |
|
240 |
page = None |
|
241 |
break |
|
242 |
if page.sub_slug: |
|
243 |
if parts[i + 1 :] == []: |
|
244 |
raise MissingSubSlug |
|
245 |
extra = extract_context_from_sub_slug(page.sub_slug, parts[i + 1]) |
|
246 |
if extra is None: |
|
247 |
page = None |
|
248 |
break |
|
249 |
if request: |
|
250 |
request.extra_context_data.update(extra) |
|
251 |
parts = parts[: i + 1] + parts[i + 2 :] # skip variable component |
|
252 |
i += 1 |
|
253 |
hierarchy_ids.append(page.id) |
|
254 | ||
255 |
return page |
combo/public/views.py | ||
---|---|---|
55 | 55 |
PostException, |
56 | 56 |
Redirect, |
57 | 57 |
TextCell, |
58 |
extract_context_from_sub_slug, |
|
59 | 58 |
) |
59 |
from combo.data.utils import MissingSubSlug, get_page_from_url_parts |
|
60 | 60 |
from combo.profile.models import Profile |
61 | 61 |
from combo.profile.utils import get_user_from_name_id |
62 | 62 | |
... | ... | |
481 | 481 |
request.session['visited'] = True |
482 | 482 |
return HttpResponseRedirect(settings.COMBO_WELCOME_PAGE_PATH) |
483 | 483 | |
484 |
pages = {} |
|
485 |
for page in Page.objects.filter(slug__in=parts): |
|
486 |
if not page.slug in pages: |
|
487 |
pages[page.slug] = [] |
|
488 |
pages[page.slug].append(page) |
|
484 |
try: |
|
485 |
page = get_page_from_url_parts(parts, request) |
|
486 |
except MissingSubSlug: |
|
487 |
# a sub slug is expected but was not found; redirect to parent |
|
488 |
# page as a mitigation. |
|
489 |
return HttpResponseRedirect('..') |
|
489 | 490 | |
490 |
if pages == {} and parts == ['index'] and Page.objects.count() == 0:
|
|
491 |
if page is None and parts == ['index'] and Page.objects.count() == 0:
|
|
491 | 492 |
return empty_site(request) |
492 | 493 | |
493 |
i = 0 |
|
494 |
hierarchy_ids = [None] |
|
495 |
while i < len(parts): |
|
496 |
slug_pages = pages.get(parts[i]) |
|
497 |
if slug_pages is None or len(slug_pages) == 0: |
|
498 |
page = None |
|
499 |
break |
|
500 |
if len(slug_pages) == 1: |
|
501 |
page = slug_pages[0] |
|
502 |
else: |
|
503 |
# multiple pages with same slugs |
|
504 |
try: |
|
505 |
page = [x for x in slug_pages if x.parent_id == hierarchy_ids[-1]][0] |
|
506 |
except IndexError: |
|
507 |
page = None |
|
508 |
break |
|
509 |
if page.parent_id != hierarchy_ids[-1]: |
|
510 |
if i == 0: |
|
511 |
# root page should be at root but maybe the page is a child of |
|
512 |
# /index/, and as /index/ is silent the page would appear |
|
513 |
# directly under /; this is not a suggested practice. |
|
514 |
if page.parent.slug != 'index' and page.parent.parent_id is not None: |
|
515 |
page = None |
|
516 |
break |
|
517 |
else: |
|
518 |
page = None |
|
519 |
break |
|
520 |
if page.sub_slug: |
|
521 |
if parts[i + 1 :] == []: |
|
522 |
# a sub slug is expected but was not found; redirect to parent |
|
523 |
# page as a mitigation. |
|
524 |
return HttpResponseRedirect('..') |
|
525 |
extra = extract_context_from_sub_slug(page.sub_slug, parts[i + 1]) |
|
526 |
if extra is None: |
|
527 |
page = None |
|
528 |
break |
|
529 |
request.extra_context_data.update(extra) |
|
530 |
parts = parts[: i + 1] + parts[i + 2 :] # skip variable component |
|
531 |
i += 1 |
|
532 |
hierarchy_ids.append(page.id) |
|
533 | ||
534 | 494 |
if not url.endswith('/') and settings.APPEND_SLASH: |
535 | 495 |
# this is useful to allow /login, /manage, and other non-page URLs to |
536 | 496 |
# work. re.sub is used to replace repeated slashes by single ones, |
537 |
- |