def handle_page_reverted_to_live(**kwargs): operation = kwargs.get('operation') if operation == REVERT_PAGE_TRANSLATION_TO_LIVE: page = kwargs.get('obj') from cms.models import Page if not type(page) == type(Page): return translation = kwargs.get('translation', None) if not translation: title = page.title_set.all()[0] language = title.language else: language = translation.language from djangocms_reversion2.models import PageVersion # if a page draft is replaced by the currently published page, then we have to make a backup and also # set the active flag correctly try: PageVersion.create_version(draft=page, language=language, version_parent=None, title='Auto - before revert', comment='Auto before revert to live', version_id=None) make_page_version_dirty(page, language) except AssertionError: # AssertionError page is not dirty pass
def test_b_revise_page(self): language = 'en' draft = create_page(title='next', template='page.html', language=language).get_draft_object() # create initial version pv = PageVersion.create_version(draft, language, version_parent=None, comment='next', title='') # we have a revised page containing the text 'initial' and add the text 'next' testutils.add_text(draft, language, content=u"next") html = testutils.get_html(request=self.get_page_request(draft, self.user)) self.assertIn('next', html, msg='could not add content') # we check that the the revision does not contain 'next' draft.refresh_from_db() html = testutils.get_html(self.get_page_request(draft.page_versions.last().hidden_page, self.user)) self.assertNotIn('next', html, msg='content should not be added to an old revision') try: # now we create a new version pv = PageVersion.create_version(draft, language, version_parent=None, comment='next', title='') except AssertionError: self.fail('Expected the page to be dirty, but it\'s clean') # this version should contain the new text draft.refresh_from_db() html = testutils.get_html(request=self.get_page_request(pv.hidden_page, self.user)) self.assertIn('next', html, msg='new content is not in the latest version') # now we revert to the old date revert_page(draft.page_versions.first(), language) html = testutils.get_html(request=self.get_page_request(draft, self.user)) self.assertNotIn('next', html, msg='new content is still in the page')
def handle_page_publish(**kwargs): language = kwargs.get('language') page = kwargs.get('instance') # when the page is published create a backup automatically from djangocms_reversion2.models import PageVersion try: PageVersion.create_version(page, language, version_parent=None, comment='Auto before publish', title='auto') make_page_version_dirty(page, language) except AssertionError: # AssertionError page is not dirty pass
def test_a_revise_page(self): language = 'en' page = create_page(title='test_a_revise_page', template='page.html', language=language) testutils.add_text(page, language, content=u"initial") page_version = PageVersion.create_version(page.get_draft_object(), language, version_parent=None, comment='', title='') self.assertIsNotNone(page_version, msg='PageVersion creation failed')
def save(self, commit=True): self.save_m2m = lambda: None data = self.cleaned_data version_id = data.get('version_id', '') title = data.get('title', '') comment = data.get('comment', '') draft = data['draft'] language = data.get('language', '') # Detect case when editing version is_version_page = draft.get_root().title_set.filter( title__in=[BIN_ROOT_TITLE, VERSION_ROOT_TITLE]).exists() if not is_version_page: # Publish page first... if hasattr(self, 'publish_on_save') and self.publish_on_save: from cms.utils.permissions import get_current_user user = get_current_user() if isinstance(user, string_types): user = User.objects.get(username=user) publish_page(draft, user, language) # Create Version second... return PageVersion.create_version(draft, language, version_parent=None, comment=comment, title=title, version_id=version_id)
def revert(self, request, **kwargs): page_pk = kwargs.get('page_pk') to_version_pk = kwargs.get('version_pk') language = request.GET.get('language') page = get_object_or_404(Page, pk=page_pk) page_version = get_object_or_404(PageVersion, pk=to_version_pk) # when the page_version you want to use as target and the current page mismatch # -> don't know why this should happen (but we can keep it as a guard) if not page_version.draft == page: raise Http404 # check if the current user is allowed to revert the page by checking the page publish permission user = get_current_user() if not user_can_publish_page(user, page_version.draft): messages.error( request, _('Missing permission to publish this page which is necessary for the rollback' )) prev = request.META.get('HTTP_REFERER') if prev: return redirect(prev) else: raise Http404 # Create a page_version of the page if it is dirty # prior to reverting try: PageVersion.create_version(page, language, version_parent=None, title='auto', comment='Auto before revert', version_id=None) except AssertionError as e: # AssertionError == page is not dirty pass revert_page(page_version, language) make_page_version_dirty(page, language) messages.info( request, _(u'You have succesfully reverted to {rev}').format( rev=page_version)) return self.render_close_frame()
def clean(self): self.cleaned_data = super(PageVersionForm, self).clean() # Check Latest Version is incremented latest_version = PageVersion.get_latest_version_id( language=self.data['language'], draft=self.data['draft']) if latest_version and 'version_id' in self.cleaned_data: new_version = self.cleaned_data['version_id'] if latest_version >= new_version: raise forms.ValidationError("Version Id is not increased")
def handle_page_publish(**kwargs): language = kwargs.get('language') page = kwargs.get('instance') # when the page is published create a backup automatically from djangocms_reversion2.models import PageVersion try: # Only trigger publish signal if not prompting for version if (ADD_VERSION_ON_PUBLISH and not PROMPT_VERSION_ON_PUBLISH ) and not page.application_namespace: PageVersion.create_version(page, language, version_parent=None, title='auto', comment='Auto before publish') make_page_version_dirty(page, language) except AssertionError: # AssertionError page is not dirty pass
def handle_page_reverted_to_live(**kwargs): page = kwargs.get('obj') translation = kwargs.get('translation') language = translation.language operation = kwargs.get('operation') if operation == REVERT_PAGE_TRANSLATION_TO_LIVE: from djangocms_reversion2.models import PageVersion # if a page draft is replaced by the currently published page, then we have to make a backup and also # set the active flag correctly try: PageVersion.create_version(page, language, version_parent=None, comment='Auto before revert to live', title='auto') make_page_version_dirty(page, language) except AssertionError: # AssertionError page is not dirty pass
def page_versions(context, hidden_page=None, draft=None): request = context['request'] language = request.LANGUAGE_CODE if not draft and not hidden_page: draft = request.current_page.get_draft_object() page_versions = PageVersion.get_versions(language, hidden_page=hidden_page, draft=draft) if page_versions.exists(): return page_versions
def save(self, commit=True): self.save_m2m = lambda: None data = self.cleaned_data comment = data.get('comment', '') title = data.get('title', '') draft = data['draft'] language = data.get('language', '') # TODO detect case when editing version return PageVersion.create_version(draft, language, version_parent=None, comment=comment, title=title)
def __init__(self, *args, **kwargs): super(PageVersionForm, self).__init__(*args, **kwargs) # Set Title as required if not ALLOW_BLANK_TITLE: self.fields['title'].required = True # Get Latest Version Id if kwargs.get('initial'): draft_page = kwargs.get('initial').get('draft') language = kwargs.get('initial').get('language') latest_version = PageVersion.get_latest_version_id( language=language, draft=draft_page) or VERSION_START_VALUE if latest_version: self.fields['version_id'].initial = str(latest_version)