Пример #1
0
    def test_release_loads_content_from_locked_content_if_released(
            self, mock_file_service):
        """
        When content is requested for a release after it is released it should load the content from the locked content
        not from the releated tables.
        """
        initial_title = 'Test page'
        page = create_test_page(OneYouPage, None, initial_title)
        revision = page.get_latest_revision()

        release_time = timezone.now() + timedelta(days=-1)
        release = create_test_release(release_date=release_time)
        populate_release_if_required(release)

        loaded_release = Release.objects.get(id=release.id)

        page.release = release
        page.save_revision()

        second_title = 'Altered page'
        page.title = second_title
        page.save_revision()
        revision.content_json = page.to_json()
        revision.save()

        release_page_content = loaded_release.get_content_for(page.id)

        self.assertIsNotNone(release.content_status, 1)
        self.assertIsNotNone(release_page_content)
        self.assertNotEqual(
            json.loads(revision.content_json)['title'], initial_title)
        self.assertEqual(release_page_content['title'], initial_title)
Пример #2
0
def site_view(request, site_identifier):
    site = get_site_or_404(site_identifier)

    current_release = get_latest_live_release(site.site.pk)
    if not current_release:
        return JsonResponse(
            {'message': 'The current site has no live releases'}, status=404)

    populate_release_if_required(current_release)

    json_response = JsonResponse(current_release.get_content_for('site_json'))
    if current_release.content_status == 1:
        json_response['Cache-Control'] = 'max-age=900'
    return json_response
Пример #3
0
    def test_release_locks_content_after_its_release_time(
            self, mock_file_service):
        """
        when a release is requested after its release date, it should save the content into json.
        """
        create_test_page(OneYouPage)

        release_name = 'Past release'
        release_date = timezone.now() + timedelta(days=-1)

        release = create_test_release(release_name, release_date)
        populate_release_if_required(release)

        loaded_release = Release.objects.get(release_name=release_name)

        self.assertEqual(loaded_release.content.count(), 1)
        self.assertEqual(loaded_release.content_status, 1)
Пример #4
0
    def test_submitformoderation_freezerelease(self, mock_file_service):
        # publish
        self.page.release = self.release
        self.page.title = 'Published'
        self.page.save_revision(self.user)

        # submit for moderation
        self.page.release = self.release
        self.page.title = 'Submitted for moderation'
        revision = self.page.save_revision(self.user, True)

        release_page = ReleasePage.objects.filter(release=self.release,
                                                  revision__page=self.page)
        release_content = self.release.content.first()
        release_content_dict = json.loads(release_content.content)

        self.assertEqual(release_page.count(), 2)
        self.assertEqual(
            release_page.filter(submitted_for_moderation=True).count(), 1)
        self.assertEqual(
            release_page.filter(submitted_for_moderation=False).count(), 1)
        self.assertEqual(len(release_content_dict), 1)
        self.assertEqual(str(self.page.id),
                         list(release_content_dict.keys())[0])
        self.assertEqual(release_content_dict[str(self.page.id)]['title'],
                         'Published')

        #freeze release
        release_date = timezone.now() - timedelta(days=1)
        self.release.release_time = release_date
        self.release.save()
        populate_release_if_required(self.release)

        release_content = self.release.content.first()
        release_content_dict = json.loads(release_content.content)
        release_content_dict.pop('site_json')

        self.assertEqual(release_page.count(), 1)
        self.assertEqual(
            release_page.filter(submitted_for_moderation=False).count(), 1)
        self.assertEqual(len(release_content_dict), 1)
        self.assertEqual(str(self.page.id),
                         list(release_content_dict.keys())[0])
        self.assertEqual(release_content_dict[str(self.page.id)]['title'],
                         'Published')
Пример #5
0
def page_detail(request,
                site_identifier,
                release_uuid,
                page_pk=None,
                page_slug_path=None,
                is_preview=False,
                page_revision=None):
    """RETURN PAGE DETAILS IN API"""
    # Match variants, a variant's slug should end with -v and then a truncated hash of 6 characters
    # variant_regex = re.compile(r'-v[a-zA-Z0-9]{6}$')

    if page_slug_path:
        # if variant_regex.search(page_slug_path):
        #     variant = True
        #     not_found_msg = 'Variant Not Found'
        # else:
        # variant = False
        not_found_msg = 'Page Not Found'

        try:
            # This somewhat defeats the point of freezing content
            # TODO: Fix this, you can do this by freezing content with slug keys
            # You would then only need to get the page object for variants
            from release.models import Release
            site = Release.objects.get(uuid=release_uuid).site
            homepage = site.root_page
            # homepage = SiteSettings.objects.get(uid=site_identifier).site.root_page
            if page_slug_path == 'home' or page_slug_path == 'oneyou':
                page = homepage
                page_pk = homepage.pk
            else:
                if homepage.url_path != page_slug_path:
                    if page_slug_path.startswith('oneyou|'):
                        page_slug_path = page_slug_path[7:]
                    path = '/{}/{}/'.format(
                        homepage.slug,
                        unquote(page_slug_path).replace('|', '/'))
                    page = Page.objects.get(url_path=path)
                    page_pk = page.pk
        except ObjectDoesNotExist:
            try:
                #legacy to support frontend v1 call to the api using only the slug
                page = Page.objects.get(slug=page_slug_path)
                page_pk = page.pk
            except ObjectDoesNotExist:
                return JsonResponse({'message': not_found_msg}, status=404)
                # try:
                #     if variant:  # Try and get parent page
                #         page = Page.objects.get(slug=page_slug_path[:-8])
                #         page_pk = page.pk
                #         variant = False
                #     else:
                #         return JsonResponse({'message': not_found_msg}, status=404)
                # except ObjectDoesNotExist:
                #     return JsonResponse({'message': not_found_msg}, status=404)
    else:  # If there is no slug it cannot be a variant
        # variant = False
        not_found_msg = 'Page Not Found'

    get_site_or_404(site_identifier)

    if is_preview:
        content_to_serialize = None
        if page_revision and page_revision != 'latest':
            revision = PageRevision.objects.get(id=page_revision)
            content_to_serialize = revision.as_page_object()
        else:
            content_to_serialize = page.specific.get_latest_revision_as_page()

        Serializer = page.specific.__class__.get_serializer()
        serialized_page = Serializer(instance=content_to_serialize)
        json_response = JsonResponse(serialized_page.data)
    else:
        release = get_release_object(release_uuid)
        if not release:
            return JsonResponse({'message': 'Release not found'}, status=404)
        populate_release_if_required(release)

        # if variant:
        #     try:
        #         experiments_content = ExperimentsContent.objects.first()
        #         if experiments_content:
        #             if page.specific.is_live:
        #                 page_content = experiments_content.get_content_for(page_pk)
        #             else:
        #                 # Get parent page content
        #                 print('scenario 2')
        #                 page_content = release.get_content_for(page.get_parent().id)
        #         else:
        #             return JsonResponse({'message': 'No content for any experiment found'}, status=500)
        #     except KeyError:
        #         return JsonResponse({'message': not_found_msg}, status=404)

        # else:
        try:
            page_content = release.get_content_for(page_pk)
        except KeyError:
            return JsonResponse({'message': not_found_msg}, status=404)

        json_response = JsonResponse(page_content)
        if release.content_status == 1:
            json_response['Cache-Control'] = 'max-age=3600'

    return json_response