def populate_pages(self, parent=None, child=5, depth=5):
     """Create a population of :class:`Page <pages.models.Page>`
     for testing purpose."""
     User = get_user_model()
     from basic_cms.models import Content
     author = User.objects.all()[0]
     if depth == 0:
         return
     p = self.model(parent=parent, author=author, status=self.model.PUBLISHED)
     p.save()
     p = self.get(id=p.id)
     Content(body='page-' + str(p.id), type='title', language=settings.PAGE_DEFAULT_LANGUAGE, page=p).save()
     Content(body='page-' + str(p.id), type='slug', language=settings.PAGE_DEFAULT_LANGUAGE, page=p).save()
     for child in range(1, child + 1):
         self.populate_pages(parent=p, child=child, depth=(depth - 1))
 def custom_get_user_by_email(email):
     """
     Allow the user profile class to look up a user by email
     address
     """
     User = get_user_model()
     # bit of an unpleasant hack that requres the logged-in
     # user has a profile, but I don't want to reproduce the
     # code in get_profile() here
     try:
         profile = user.get_profile()
     except (SiteProfileNotAvailable, ObjectDoesNotExist, AttributeError):
         return User.objects.get(email=email)
     get_user_by_email = getattr(profile, 'get_user_by_email', None)
     if get_user_by_email:
         return get_user_by_email(email)
     return User.objects.get(email=email)
Exemple #3
0
 def custom_get_user_by_email(email):
     """
     Allow the user profile class to look up a user by email
     address
     """
     User = get_user_model()
     # bit of an unpleasant hack that requres the logged-in
     # user has a profile, but I don't want to reproduce the
     # code in get_profile() here
     try:
         profile = user.get_profile()
     except (SiteProfileNotAvailable, ObjectDoesNotExist,
             AttributeError):
         return User.objects.get(email=email)
     get_user_by_email = getattr(profile, 'get_user_by_email', None)
     if get_user_by_email:
         return get_user_by_email(email)
     return User.objects.get(email=email)
Exemple #4
0
 def populate_pages(self, parent=None, child=5, depth=5):
     """Create a population of :class:`Page <pages.models.Page>`
     for testing purpose."""
     User = get_user_model()
     from basic_cms.models import Content
     author = User.objects.all()[0]
     if depth == 0:
         return
     p = self.model(parent=parent,
                    author=author,
                    status=self.model.PUBLISHED)
     p.save()
     p = self.get(id=p.id)
     Content(body='page-' + str(p.id),
             type='title',
             language=settings.PAGE_DEFAULT_LANGUAGE,
             page=p).save()
     Content(body='page-' + str(p.id),
             type='slug',
             language=settings.PAGE_DEFAULT_LANGUAGE,
             page=p).save()
     for child in range(1, child + 1):
         self.populate_pages(parent=p, child=child, depth=(depth - 1))
    def create_and_update_from_json_data(self, d, user):
        """
        Create or update page based on python dict d loaded from JSON data.
        This applies all data except for redirect_to, which is done in a
        second pass after all pages have been imported,

        user is the User instance that will be used if the author can't
        be found in the DB.

        returns (page object, created, messages).

        created is True if this was a new page or False if an existing page
        was updated.

        messages is a list of strings warnings/messages about this import
        """
        User = get_user_model()
        page = None
        parent = None
        parent_required = True
        created = False
        messages = []

        page_languages = set(lang[0] for lang in settings.PAGE_LANGUAGES)

        for lang, s in d['complete_slug'].items():
            if lang not in page_languages:
                messages.append(_("Language '%s' not imported") % (lang,))
                continue

            page = self.from_path(s, lang, exclude_drafts=False)
            if page:
                break
            if parent_required and parent is None:
                if '/' in s:
                    parent = self.from_path(s.rsplit('/', 1)[0], lang, exclude_drafts=False)
                else:
                    parent_required = False
        else:
            # can't find an existing match, need to create a new Page
            page = self.model(parent=parent)
            created = True

        def custom_get_user_by_email(email):
            """
            Allow the user profile class to look up a user by email
            address
            """
            User = get_user_model()
            # bit of an unpleasant hack that requres the logged-in
            # user has a profile, but I don't want to reproduce the
            # code in get_profile() here
            try:
                profile = user.get_profile()
            except (SiteProfileNotAvailable, ObjectDoesNotExist, AttributeError):
                return User.objects.get(email=email)
            get_user_by_email = getattr(profile, 'get_user_by_email', None)
            if get_user_by_email:
                return get_user_by_email(email)
            return User.objects.get(email=email)

        try:
            page.author = custom_get_user_by_email(d['author_email'])
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            page.author = user
            messages.append(_("Original author '%s' not found") % (d['author_email'],))

        page.creation_date = datetime.strptime(d['creation_date'],
                                               ISODATE_FORMAT)
        page.publication_date = datetime.strptime(d['publication_date'],
                                                  ISODATE_FORMAT) if d['publication_date'] else None
        page.publication_end_date = datetime.strptime(d['publication_end_date'],
                                                      ISODATE_FORMAT) if d['publication_end_date'] else None
        page.last_modification_date = datetime.strptime(
            d['last_modification_date'], ISODATE_FORMAT)
        page.status = {
            'published': self.model.PUBLISHED,
            'hidden': self.model.HIDDEN,
            'draft': self.model.DRAFT
        }[d['status']]
        page.template = d['template']
        page.redirect_to_url = d['redirect_to_url']
        page.save()

        # Add tags
        tags = d.get('tags', [])
        page.tags.clear()
        if tags:
            for tag in tags:
                Tag.objects.get_or_create(name=tag)
                page.tags.add(tag)
            page.save()

        if settings.PAGE_USE_SITE_ID and not settings.PAGE_HIDE_SITES:
            if d['sites']:
                for site in d['sites']:
                    try:
                        page.sites.add(Site.objects.get(domain=site))
                    except Site.DoesNotExist:
                        messages.append(_("Could not add site '%s' to page") % (site,))
            if not page.sites.count():  # need at least one site
                page.sites.add(Site.objects.get(pk=global_settings.SITE_ID))

        from basic_cms.models import Content

        def create_content(lang, ctype, body):
            Content.objects.create_content_if_changed(page, lang, ctype, body)

        for lang in d['content_language_updated_order']:
            if lang not in page_languages:
                continue
            create_content(lang, 'slug', d['complete_slug'][lang].rsplit('/', 1)[-1])
            create_content(lang, 'title', d['title'][lang])
            for ctype, langs_bodies in d['content'].items():
                create_content(lang, ctype, langs_bodies[lang])

        return page, created, messages
Exemple #6
0
    def create_and_update_from_json_data(self, d, user):
        """
        Create or update page based on python dict d loaded from JSON data.
        This applies all data except for redirect_to, which is done in a
        second pass after all pages have been imported,

        user is the User instance that will be used if the author can't
        be found in the DB.

        returns (page object, created, messages).

        created is True if this was a new page or False if an existing page
        was updated.

        messages is a list of strings warnings/messages about this import
        """
        User = get_user_model()
        page = None
        parent = None
        parent_required = True
        created = False
        messages = []

        page_languages = set(lang[0] for lang in settings.PAGE_LANGUAGES)

        for lang, s in d['complete_slug'].items():
            if lang not in page_languages:
                messages.append(_("Language '%s' not imported") % (lang, ))
                continue

            page = self.from_path(s, lang, exclude_drafts=False)
            if page:
                break
            if parent_required and parent is None:
                if '/' in s:
                    parent = self.from_path(s.rsplit('/', 1)[0],
                                            lang,
                                            exclude_drafts=False)
                else:
                    parent_required = False
        else:
            # can't find an existing match, need to create a new Page
            page = self.model(parent=parent)
            created = True

        def custom_get_user_by_email(email):
            """
            Allow the user profile class to look up a user by email
            address
            """
            User = get_user_model()
            # bit of an unpleasant hack that requres the logged-in
            # user has a profile, but I don't want to reproduce the
            # code in get_profile() here
            try:
                profile = user.get_profile()
            except (SiteProfileNotAvailable, ObjectDoesNotExist,
                    AttributeError):
                return User.objects.get(email=email)
            get_user_by_email = getattr(profile, 'get_user_by_email', None)
            if get_user_by_email:
                return get_user_by_email(email)
            return User.objects.get(email=email)

        try:
            page.author = custom_get_user_by_email(d['author_email'])
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            page.author = user
            messages.append(
                _("Original author '%s' not found") % (d['author_email'], ))

        page.creation_date = datetime.strptime(d['creation_date'],
                                               ISODATE_FORMAT)
        page.publication_date = datetime.strptime(
            d['publication_date'],
            ISODATE_FORMAT) if d['publication_date'] else None
        page.publication_end_date = datetime.strptime(
            d['publication_end_date'],
            ISODATE_FORMAT) if d['publication_end_date'] else None
        page.last_modification_date = datetime.strptime(
            d['last_modification_date'], ISODATE_FORMAT)
        page.status = {
            'published': self.model.PUBLISHED,
            'hidden': self.model.HIDDEN,
            'draft': self.model.DRAFT
        }[d['status']]
        page.template = d['template']
        page.redirect_to_url = d['redirect_to_url']
        page.save()

        # Add tags
        tags = d.get('tags', [])
        page.tags.clear()
        if tags:
            for tag in tags:
                Tag.objects.get_or_create(name=tag)
                page.tags.add(tag)
            page.save()

        if settings.PAGE_USE_SITE_ID and not settings.PAGE_HIDE_SITES:
            if d['sites']:
                for site in d['sites']:
                    try:
                        page.sites.add(Site.objects.get(domain=site))
                    except Site.DoesNotExist:
                        messages.append(
                            _("Could not add site '%s' to page") % (site, ))
            if not page.sites.count():  # need at least one site
                page.sites.add(Site.objects.get(pk=global_settings.SITE_ID))

        from basic_cms.models import Content

        def create_content(lang, ctype, body):
            Content.objects.create_content_if_changed(page, lang, ctype, body)

        for lang in d['content_language_updated_order']:
            if lang not in page_languages:
                continue
            create_content(lang, 'slug',
                           d['complete_slug'][lang].rsplit('/', 1)[-1])
            create_content(lang, 'title', d['title'][lang])
            for ctype, langs_bodies in d['content'].items():
                create_content(lang, ctype, langs_bodies[lang])

        return page, created, messages