cms.utils.page: 47 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/cms/utils/page.py

Stats: 0 executed, 42 missed, 5 excluded, 39 ignored

  1. # -*- coding: utf-8 -*-
  2. from django.conf import settings
  3. from django.db.models import Q
  4. from django.core.exceptions import ValidationError
  5. import re
  6. APPEND_TO_SLUG = "-copy"
  7. COPY_SLUG_REGEX = re.compile(r'^.*-copy(?:-(\d)*)?$')
  8. def is_valid_page_slug(page, parent, lang, slug, site, path=None):
  9. """Validates given slug depending on settings.
  10. """
  11. from cms.models import Title
  12. # Exclude the page with the publisher_state == page.PUBLISHER_STATE_DELETE
  13. qs = Title.objects.filter(page__site=site).exclude(
  14. Q(page=page) |
  15. Q(page=page.publisher_public) |
  16. Q(page__publisher_state=page.PUBLISHER_STATE_DELETE)
  17. )
  18. if settings.i18n_installed:
  19. qs = qs.filter(language=lang)
  20. if not settings.CMS_FLAT_URLS:
  21. if parent:
  22. if parent.is_home():
  23. qs = qs.filter(Q(page__parent=parent) |
  24. Q(page__parent__isnull=True))
  25. else:
  26. qs = qs.filter(page__parent=parent)
  27. else:
  28. qs = qs.filter(page__parent__isnull=True)
  29. if page.pk:
  30. qs = qs.exclude(language=lang, page=page)
  31. ## Check for slugs
  32. if qs.filter(slug=slug).count():
  33. return False
  34. ## Check for path
  35. if path and qs.filter(path=path).count():
  36. return False
  37. return True
  38. def get_available_slug(title, new_slug=None):
  39. """Smart function generates slug for title if current title slug cannot be
  40. used. Appends APPEND_TO_SLUG to slug and checks it again.
  41. (Used in page copy function)
  42. Returns: slug
  43. """
  44. rewrite_slug = False
  45. slug = new_slug or title.slug
  46. # We need the full path for the title to check for conflicting urls
  47. title.slug = slug
  48. title.update_path()
  49. path = title.path
  50. # This checks for conflicting slugs/overwrite_url, for both published and unpublished pages
  51. # This is a simpler check than in page_resolver.is_valid_url which
  52. # takes into account actualy page URL
  53. if not is_valid_page_slug(title.page, title.page.parent, title.language, slug, title.page.site, path):
  54. # add nice copy attribute, first is -copy, then -copy-2, -copy-3, ....
  55. match = COPY_SLUG_REGEX.match(slug)
  56. if match:
  57. try:
  58. next = int(match.groups()[0]) + 1
  59. slug = "-".join(slug.split('-')[:-1]) + "-%d" % next
  60. except TypeError:
  61. slug = slug + "-2"
  62. else:
  63. slug = slug + APPEND_TO_SLUG
  64. return get_available_slug(title, slug)
  65. else:
  66. return slug
  67. def check_title_slugs(page):
  68. """Checks page title slugs for duplicity if required, used after page move/
  69. cut/paste.
  70. """
  71. for title in page.title_set.all():
  72. old_slug = title.slug
  73. title.slug = get_available_slug(title)
  74. if title.slug != old_slug:
  75. title.save()