def test_create_aldryn_revision(self): # would be used to get admin instance with_placeholder_version = get_version_for_object( self.with_placeholder) admin_instance = self.get_admin_instance_for_object( with_placeholder_version) plugin = api.add_plugin(self.with_placeholder.content, 'TextPlugin', language='en') plugin.body = 'Initial text' plugin.save() # ensure there was no versions for plugin before self.assertEqual( default_revision_manager.get_for_object(plugin).count(), 0) with transaction.atomic(): with revision_context_manager.create_revision(): admin_instance._create_aldryn_revision( plugin.placeholder, comment='New aldryn revision with initial plugin') # ensure there is at least one version after create aldryn revision self. assertEqual( default_revision_manager.get_for_object(plugin).count(), 1) new_plugin_text = 'test plugin content was changed' plugin.body = new_plugin_text plugin.save() with transaction.atomic(): with revision_context_manager.create_revision(): admin_instance._create_aldryn_revision( plugin.placeholder, comment='New aldryn revision with initial plugin') # ensure there is at least one version after create aldryn revision self. assertEqual( default_revision_manager.get_for_object(plugin).count(), 2) latest_plugin = plugin._meta.model.objects.get(pk=plugin.pk) # ensure text is latest self.assertEqual(latest_plugin.body, new_plugin_text) # ensure text is initial if reverted to previous revision prev_version = default_revision_manager.get_for_object( self.with_placeholder)[1] prev_version.revision.revert() # refresh from db latest_plugin = plugin._meta.model.objects.get(pk=plugin.pk) # ensure plugin text was chagned. Note however that there might be # different paths to ensure that text is chagned for CMSPlugin # This only checks that plugin content (which is text plugin not the cms # is reverted, so be careful. self.assertEqual(latest_plugin.body, 'Initial text')
def save(self, *args, **kwargs): # save both forms profile = self.profileForm.save(commit=False) """ Ensure we create a revision for reversion. """ person = super(PersonForm, self).save(commit=False) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): profile.save() person.profile = profile person.save() self.save_m2m() if self.user: revision_context_manager.set_user(self.user) object_repr = build_obj_repr(person) + build_obj_repr(profile) translation_info = get_translation_info_message(person) revision_context_manager.set_comment( ugettext( "Initial version of {object_repr}. {trans_info}".format( object_repr=object_repr, trans_info=translation_info))) return person
def save(self, commit=True): article = super(CreateNewsBlogArticleForm, self).save(commit=False) # Set owner to current user article.owner = self.user # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField content = clean_html(self.cleaned_data.get('content', ''), False) if content and permissions.has_plugin_permission( self.user, 'TextPlugin', 'add'): # If the article has not been saved, then there will be no # Placeholder set-up for this article yet, so, ensure we have saved # first. if not article.pk: article.save() if article and article.content: add_plugin( placeholder=article.content, plugin_type='TextPlugin', language=self.language_code, body=content, ) if ENABLE_REVERSION: from reversion.revisions import revision_context_manager with transaction.atomic(): with revision_context_manager.create_revision(): article.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return article
def save(self, commit=True): article = super(CreateNewsBlogArticleForm, self).save(commit=False) # Set owner to current user article.owner = self.user # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField content = clean_html(self.cleaned_data.get('content', ''), False) if content and permissions.has_plugin_permission( self.user, 'TextPlugin', 'add'): # If the article has not been saved, then there will be no # Placeholder set-up for this article yet, so, ensure we have saved # first. if not article.pk: article.save() if article and article.content: add_plugin( placeholder=article.content, plugin_type='TextPlugin', language=self.language_code, body=content, ) with transaction.atomic(): with revision_context_manager.create_revision(): article.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return article
def save(self, commit=True): job_opening = super(CreateJobOpeningForm, self).save(commit=False) # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField content = clean_html(self.cleaned_data.get('content', ''), False) content_plugin = get_cms_setting('WIZARD_CONTENT_PLUGIN') if content and permissions.has_plugin_permission( self.user, 'TextPlugin', 'add'): # If the job_opening has not been saved, then there will be no # Placeholder set-up for this question yet, so, ensure we have saved # first. if not job_opening.pk: job_opening.save() if job_opening and job_opening.content: plugin_kwargs = { 'placeholder': job_opening.content, 'plugin_type': content_plugin, 'language': self.language_code, get_cms_setting('WIZARD_CONTENT_PLUGIN_BODY'): content, } add_plugin(**plugin_kwargs) with transaction.atomic(): with revision_context_manager.create_revision(): job_opening.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return job_opening
def process_request(self, request): if request_creates_revision(request): context = revision_context_manager.create_revision() context.__enter__() revision_context_manager.set_user(request.user) if not hasattr(request, "_revision_middleware"): setattr(request, "_revision_middleware", {}) request._revision_middleware[self] = context
def process_request(self, request): if request_creates_revision(request): context = revision_context_manager.create_revision() context.__enter__() revision_context_manager.set_user(request.user) if not hasattr(request, "_revision_middleware"): setattr(request, "_revision_middleware", {}) request._revision_middleware[self] = context
def create_with_revision(self, kls, language='en', **kwargs): """ Create an instance of class kls with passing kwargs to kls.objects.create(**kwargs) """ with transaction.atomic(): with revision_context_manager.create_revision(): with override(language): return kls.objects.create(**kwargs)
def create_revision(obj, user=None, comment=None): with revision_context_manager.create_revision(): if user: revision_context_manager.set_user(user) if comment: revision_context_manager.set_comment(comment) _add_to_context(obj) if hasattr(obj._meta, 'placeholder_field_names'): add_placeholders_to_revision(instance=obj)
def create_translation_with_revision(self, language, obj, **kwargs): """ Just creates a translation under revision manager. Returns nothing. """ # create translation outside of revision manager, and save the object # after that, so that revision would contain full set of related # objects, instead of containing only translation and no master object. obj.create_translation(language, **kwargs) with transaction.atomic(): with revision_context_manager.create_revision(): obj.save()
def create_revision(obj, user=None, comment=None): with revision_context_manager.create_revision(): if user: revision_context_manager.set_user(user) if comment: revision_context_manager.set_comment(comment) _add_to_context(obj) if hasattr(obj._meta, 'placeholder_field_names'): add_placeholders_to_revision(instance=obj)
def save(self, commit=True): category = super(CreateJobCategoryForm, self).save(commit=False) with transaction.atomic(): with revision_context_manager.create_revision(): category.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return category
def save(self, commit=True): event = super(CreateEventForm, self).save(commit=False) if not commit: return event # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField description = clean_html( self.cleaned_data.get('description', ''), False) try: # CMS >= 3.3.x content_plugin = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN') except KeyError: # CMS <= 3.2.x content_plugin = get_cms_setting('WIZARD_CONTENT_PLUGIN') try: # CMS >= 3.3.x content_field = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY') except KeyError: # CMS <= 3.2.x content_field = get_cms_setting('WIZARD_CONTENT_PLUGIN_BODY') if description and permissions.has_plugin_permission( self.user, content_plugin, 'add'): # If the event has not been saved, then there will be no # Placeholder set-up for this event yet, so, ensure we have saved # first. if not event.pk: event.save() if event and event.description: # we have to use kwargs because we don't know in advance what # is the 'body' field for configured plugin plugin_kwargs = { 'placeholder': event.description, 'plugin_type': content_plugin, 'language': self.language_code, content_field: description, } add_plugin(**plugin_kwargs) with transaction.atomic(): with revision_context_manager.create_revision(): event.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return event
def save(self, commit=True): category = super(CreateJobCategoryForm, self).save(commit=False) with transaction.atomic(): with revision_context_manager.create_revision(): category.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return category
def save(self, commit=True): event = super(CreateEventForm, self).save(commit=False) if not commit: return event # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField description = clean_html(self.cleaned_data.get('description', ''), False) try: # CMS >= 3.3.x content_plugin = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN') except KeyError: # CMS <= 3.2.x content_plugin = get_cms_setting('WIZARD_CONTENT_PLUGIN') try: # CMS >= 3.3.x content_field = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY') except KeyError: # CMS <= 3.2.x content_field = get_cms_setting('WIZARD_CONTENT_PLUGIN_BODY') if description and permissions.has_plugin_permission( self.user, content_plugin, 'add'): # If the event has not been saved, then there will be no # Placeholder set-up for this event yet, so, ensure we have saved # first. if not event.pk: event.save() if event and event.description: # we have to use kwargs because we don't know in advance what # is the 'body' field for configured plugin plugin_kwargs = { 'placeholder': event.description, 'plugin_type': content_plugin, 'language': self.language_code, content_field: description, } add_plugin(**plugin_kwargs) with transaction.atomic(): with revision_context_manager.create_revision(): event.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return event
def save(self, commit=True): question = super(CreateFaqQuestionForm, self).save(commit=False) # If 'content' field has value, create a TextPlugin with same and add # it to the PlaceholderField answer = clean_html(self.cleaned_data.get('answer', ''), False) try: # CMS >= 3.3.x content_plugin = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN') except KeyError: # CMS <= 3.2.x content_plugin = get_cms_setting('WIZARD_CONTENT_PLUGIN') try: # CMS >= 3.3.x content_field = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY') except KeyError: # CMS <= 3.2.x content_field = get_cms_setting('WIZARD_CONTENT_PLUGIN_BODY') if answer and permissions.has_plugin_permission( self.user, content_plugin, 'add'): # If the question has not been saved, then there will be no # Placeholder set-up for this question yet, so, ensure we have saved # first. if not question.pk: question.save() if question and question.answer: plugin_kwarg = { 'placeholder': question.answer, 'plugin_type': content_plugin, 'language': self.language_code, content_field: answer, } add_plugin(**plugin_kwarg) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): question.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return question
def save(self, commit=True): """ Ensure we create a revision for reversion. """ category = super(CreateFaqCategoryForm, self).save(commit=False) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): category.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return category
def create_revision(self, obj, content=None, **kwargs): with transaction.atomic(): with revision_context_manager.create_revision(): # populate event with new values for property, value in six.iteritems(kwargs): setattr(obj, property, value) if content: # get correct plugin for language. do not update the same # one. language = obj.get_current_language() plugins = obj.content.get_plugins().filter( language=language) plugin = plugins[0].get_plugin_instance()[0] plugin.body = content plugin.save() obj.save()
def save(self, commit=True): """ Ensure we create a revision for reversion. """ group = super(CreatePeopleGroupForm, self).save(commit=False) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): group.save() if self.user: revision_context_manager.set_user(self.user) revision_context_manager.set_comment( ugettext("Initial version.")) return group
def save(self, commit=True): """ Ensure we create a revision for reversion. """ person = super(CreatePeoplePersonForm, self).save(commit=False) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): person.save() self.save_m2m() if self.user: revision_context_manager.set_user(self.user) object_repr = build_obj_repr(person) translation_info = get_translation_info_message(person) revision_context_manager.set_comment( ugettext("Initial version of {object_repr}. {trans_info}". format(object_repr=object_repr, trans_info=translation_info))) return person
def save(self, commit=True): """ Ensure we create a revision for reversion. """ person = super(CreatePeoplePersonForm, self).save(commit=False) # Ensure we make an initial revision with transaction.atomic(): with revision_context_manager.create_revision(): person.save() self.save_m2m() if self.user: revision_context_manager.set_user(self.user) object_repr = build_obj_repr(person) translation_info = get_translation_info_message(person) revision_context_manager.set_comment( ugettext( "Initial version of {object_repr}. {trans_info}".format( object_repr=object_repr, trans_info=translation_info))) return person
def do_revision_view(request, *args, **kwargs): if request_creates_revision(request): with revision_context_manager.create_revision(): revision_context_manager.set_user(request.user) return func(request, *args, **kwargs) return func(request, *args, **kwargs)
def do_revision_view(request, *args, **kwargs): if request_creates_revision(request): with revision_context_manager.create_revision(): revision_context_manager.set_user(request.user) return func(request, *args, **kwargs) return func(request, *args, **kwargs)