def test_project_create_and_save(self): self.assertTrue(self.client.login(username=self.student_one.username, password='******')) data = {u'title': [u'']} response = self.client.post('/project/create/', data, follow=True) self.assertEquals(response.status_code, 200) self.assertTrue(response.redirect_chain[0][0].startswith( 'http://testserver/project/view/')) project = Project.objects.get(course=self.sample_course, title='Untitled') versions = reversion.get_for_object(project).get_unique() self.assertEquals(sum(1 for v in versions), 1) self.assertIsNone(project.date_submitted) self.assertIn(self.student_one, project.participants.all()) self.assertEquals(project.author, self.student_one) data = {u'body': [u'<p>abcdefghi</p>'], u'participants': [self.student_one.id], u'publish': [u'InstructorShared'], u'title': [u'Student Essay']} url = '/project/save/%s/' % project.id response = self.client.post(url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEquals(response.status_code, 200) project = Project.objects.get(title='Student Essay') versions = reversion.get_for_object(project).get_unique() self.assertEquals(sum(1 for v in versions), 2) self.assertIsNotNone(project.date_submitted)
def testRevisionsSavedNotOnAddButOnChange(self): self.assertEqual(AutoInitialAdminModel.objects.count(), 0) # Create an instance via the admin. response = self.client.post("/admin/auth/autoinitialadminmodel/add/", { "name": "instance1 version1", "_continue": 1, }) self.assertEqual(response.status_code, 302) obj_pk = response["Location"].split("/")[-2] obj = AutoInitialAdminModel.objects.get(id=obj_pk) # Check that no version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 0) # Save a new version. response = self.client.post("/admin/auth/autoinitialadminmodel/%s/" % obj_pk, { "name": "instance1 version2", "_continue": 1, }) self.assertEqual(response.status_code, 302) # Check that 2 versions were created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 2) self.assertEqual(versions[0].field_dict["name"], "instance1 version2") self.assertEqual(versions[1].field_dict["name"], "instance1 version1") # Delete the object. response = self.client.post("/admin/auth/autoinitialadminmodel/%s/delete/" % obj_pk, { "_continue": 1, }) self.assertEqual(response.status_code, 302) # Check that only 1 version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 3)
def test_people_revision_is_reverted(self): rev_1_values = self.make_new_values( self.data_raw['person']['en'], 1) # rev 1 self.person1.set_current_language('en') self.create_revision(self.person1, **rev_1_values) self.assertEqual(len(reversion.get_for_object(self.person1)), 1) # check that values are actually changed self.assertEqual(self.person1.function, rev_1_values['function']) self.assertEqual(self.person1.description, rev_1_values['description']) # rev 2 rev_2_values = self.make_new_values( self.data_raw['person']['en'], 2) self.create_revision(self.person1, **rev_2_values) self.assertEqual(len(reversion.get_for_object(self.person1)), 2) # check that values are actually changed self.assertEqual(self.person1.function, rev_2_values['function']) self.assertEqual(self.person1.description, rev_2_values['description']) # revert self.revert_to(self.person1, 1) self.person1 = Person.objects.get(pk=self.person1.pk) # check previous values self.assertEqual( self.person1.function, rev_1_values['function']) self.assertEqual( self.person1.description, rev_1_values['description'])
def test_project_save_valid(self): versions = reversion.get_for_object(self.project_private).get_unique() self.assertEquals(sum(1 for v in versions), 1) self.assertTrue(self.client.login(username=self.student_one.username, password='******')) data = {u'body': [u'<p>abcdefghi</p>'], u'participants': [self.student_one.id, self.student_two.id], u'publish': [u'PrivateEditorsAreOwners'], u'title': [u'Private Student Essay']} url = '/project/save/%s/' % self.project_private.id response = self.client.post(url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEquals(response.status_code, 200) the_json = json.loads(response.content) self.assertEquals(the_json["status"], "success") self.assertFalse(the_json["is_essay_assignment"]) self.assertEquals(the_json["title"], "Private Student Essay") self.assertEquals(the_json["revision"]["visibility"], "Draft") self.assertIsNone(the_json["revision"]["public_url"]) self.assertEquals(the_json["revision"]["due_date"], "") project = Project.objects.get(id=self.project_private.id) self.assertEquals(project.author, self.student_one) self.assertIn(self.student_one, project.participants.all()) self.assertIn(self.student_two, project.participants.all()) versions = reversion.get_for_object(self.project_private).get_unique() self.assertEquals(sum(1 for v in versions), 2)
def testCanRevertRevisionWithDeletedVersions(self): self.assertEqual(ReversionTestModel1.objects.count(), 2) self.assertEqual(ReversionTestModel2.objects.count(), 2) with reversion.create_revision(): self.test11.name = "model1 instance1 version3" self.test11.save() self.test12.delete() self.test21.name = "model2 instance1 version3" self.test21.save() self.test22.delete() self.assertEqual(ReversionTestModel1.objects.count(), 1) self.assertEqual(ReversionTestModel2.objects.count(), 1) with reversion.create_revision(): self.test11.name = "model1 instance1 version4" self.test11.save() self.test21.name = "model2 instance1 version4" self.test21.save() self.assertEqual(ReversionTestModel1.objects.count(), 1) self.assertEqual(ReversionTestModel2.objects.count(), 1) # Revert to a revision where some deletes were logged. reversion.get_for_object(self.test11)[1].revision.revert() self.assertEqual(ReversionTestModel1.objects.count(), 1) self.assertEqual(ReversionTestModel2.objects.count(), 1) self.assertEqual(ReversionTestModel1.objects.get(id=self.test11.id).name, "model1 instance1 version3") self.assertEqual(ReversionTestModel2.objects.get(id=self.test21.id).name, "model2 instance1 version3") # Revert the a revision before the deletes were logged. reversion.get_for_object(self.test11)[2].revision.revert() self.assertEqual(ReversionTestModel1.objects.count(), 2) self.assertEqual(ReversionTestModel2.objects.count(), 2)
def test_edit_plugin_directly(self): content0 = self.rand_str(prefix='content0_') content1 = self.rand_str(prefix='content1_') content2 = self.rand_str(prefix='content2_') article = self.create_article(content=content0) # Revision 1 self.create_revision(article, content=content1) self.assertEqual( len(reversion.get_for_object(article)), 1) # Revision 2 with transaction.atomic(): plugins = article.content.get_plugins() plugin = plugins[0].get_plugin_instance()[0] plugin.body = content2 plugin.save() create_revision(article) self.assertEqual( len(reversion.get_for_object(article)), 2) response = self.client.get(article.get_absolute_url()) self.assertContains(response, content2) self.assertNotContains(response, content1) # Revert to revision 1 self.revert_to(article, 1) response = self.client.get(article.get_absolute_url()) self.assertContains(response, content1) self.assertNotContains(response, content2)
def last_edited_at(self): versions = reversion.get_for_object(self) if versions: version = reversion.get_for_object(self)[0] return version.revision.date_created else: return None
def current_version_id(self): versions = reversion.get_for_object(self) if versions: version = reversion.get_for_object(self)[0] return "V{0} | R{1}".format(version.pk, version.revision.pk) else: return None
def last_edited_by(self): versions = reversion.get_for_object(self) if versions: version = reversion.get_for_object(self)[0] return version.revision.user else: return None
def test_edit_plugin_directly(self): content1 = 'Content 1 text' content2 = 'Content 2 text' with force_language('en'): event = self.create_default_event() # revision 1 self.create_revision(event, content1) self.assertEqual(len(reversion.get_for_object(event)), 1) # revision 2 with transaction.atomic(): with reversion.create_revision(): plugins = event.description.get_plugins().filter( language=event.get_current_language()) plugin = plugins[0].get_plugin_instance()[0] plugin.body = content2 plugin.save() create_revision_with_placeholders(event) self.assertEqual(len(reversion.get_for_object(event)), 2) response = self.client.get(event.get_absolute_url()) self.assertContains(response, content2) self.assertNotContains(response, content1) self.revert_to(event, 1) event = Event.objects.get(pk=event.pk) response = self.client.get(event.get_absolute_url()) self.assertContains(response, content1) self.assertNotContains(response, content2)
def test_save_version(self): # first get all current versions of the document from the database document = Document.objects.get() versions = reversion.get_for_object(document) self.assertEqual(len(versions), 1) # get the editor page and add a new revision response = self.app.get(reverse("information_pages:edit", args=[self.document.url_title]), user=self.user) self.assertEqual(response.status_code, 200) form = response.form new_string = self.document.text + "\nHallo Bibi Blocksberg!!" form.set("text", new_string) form.set("comment", "hallo Bibi Blocksberg") form.set("url_title", "bibi-blocksberg") response = form.submit().follow() self.assertEqual(response.status_code, 200) # check whether number of versions increased versions = reversion.get_for_object(self.document) self.assertEqual(len(versions), 2) # check whether the comment of the version correct self.assertEqual(versions[0].revision.comment, "hallo Bibi Blocksberg") self.assertEqual(versions[1].revision.comment, "test version")
def testReverseFollowRevertWithDelete(self): with reversion.create_revision(): follow2 = TestFollowModel.objects.create(name="related instance2 version 1", test_model_1=self.test11) # Test that a revert with delete works. follow2_pk = follow2.pk reversion.get_for_object(self.test11)[1].revision.revert(delete=True) self.assertEqual(TestFollowModel.objects.count(), 1) self.assertRaises(TestFollowModel.DoesNotExist, lambda: TestFollowModel.objects.get(id=follow2_pk))
def test_create_revision(self): import reversion version_list = reversion.get_for_object(self.lex) assert len(version_list) == 0 self.client.login(username="******", password="******") response = self.client.post(self.url, self.get_post_data(self.lex), follow=True) version_list = reversion.get_for_object(self.lex) assert len(version_list) == 1
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) self.object.get_parsed_content() extensions = Extension.objects.filter(public=True) | self.object.extensions.all() if not self.request.user.is_anonymous(): extensions |= Extension.objects.filter(author=self.request.user) extensions = extensions.distinct() context["extensions"] = [e.as_json() for e in extensions] context["editable"] = self.editable context["can_delete"] = self.can_delete context["can_copy"] = self.can_copy context["navtab"] = "questions" if not self.request.user.is_anonymous(): context["starred"] = self.request.user.userprofile.favourite_questions.filter(pk=self.object.pk).exists() else: context["starred"] = False context["access_rights"] = [ {"id": qa.user.pk, "name": qa.user.get_full_name(), "access_level": qa.access} for qa in QuestionAccess.objects.filter(question=self.object) ] versions = [version_json(v, self.user) for v in reversion.get_for_object(self.object)] licences = [licence.as_json() for licence in Licence.objects.all()] question_json = context["question_json"] = { "questionJSON": json.loads(self.object.as_json()), "editable": self.editable, "licences": licences, "numbasExtensions": context["extensions"], "previewURL": reverse("question_preview", args=(self.object.pk, self.object.slug)), "previewWindow": str(calendar.timegm(time.gmtime())), "starred": context["starred"], "versions": versions, "timeline": timeline_json(self.object.timeline, self.user), } if self.editable: question_json["public_access"] = self.object.public_access question_json["access_rights"] = context["access_rights"] context["versions"] = reversion.get_for_object(self.object) part_type_path = "question/part_types/" + ("editable" if self.editable else "noneditable") context["partNames"] = [ (name, "{}/{}.html".format(part_type_path, name)) for name in "jme", "gapfill", "numberentry", "patternmatch", "1_n_2", "m_n_2", "m_n_x", "matrix" ] context["stamp_choices"] = STAMP_STATUS_CHOICES return context
def testCanGetUniqueForObject(self): with reversion.create_revision(): self.test11.save() self.test21.save() # Test a model with an int pk. self.assertEqual(reversion.get_for_object(self.test11).count(), 3) self.assertEqual(len(reversion.get_unique_for_object(self.test11)), 2) # Test a model with a str pk. self.assertEqual(reversion.get_for_object(self.test21).count(), 3) self.assertEqual(len(reversion.get_unique_for_object(self.test21)), 2)
def testCanGetForObject(self): # Can get version for proxy model proxy_versions = reversion.get_for_object(self.proxy) self.assertEqual(len(proxy_versions), 2) self.assertEqual(proxy_versions[0].field_dict["name"], self.proxy.name) self.assertEqual(proxy_versions[1].field_dict["name"], self.concrete.name) # Can get the same version for concrete model concrete_versions = reversion.get_for_object(self.concrete) self.assertEqual(list(concrete_versions), list(proxy_versions))
def test_create_revision(self): import reversion version_list = reversion.get_for_object(self.lex1) assert len(version_list) == 0 self.client.login(username="******", password="******") postdata = self.get_post_data(self.items) postdata['form-0-entry'] = 'apricot' # replace 'sausage' with 'apricot' response = self.client.post(self.url, postdata, follow=True) version_list = reversion.get_for_object(self.lex1) assert len(version_list) == 1
def setUp(self): super(CBViewTest, self).setUp() test_data = TestData(verbose=False) self.item1, self.item2 = test_data.create_Simple_data() queryset = get_for_object(self.item1) self.version_ids1 = queryset.values_list("pk", flat=True) queryset = get_for_object(self.item2) self.version_ids2 = queryset.values_list("pk", flat=True)
def get_context_data(self, **kwargs): context = super(PortalRevisionDiffView, self).get_context_data(**kwargs) try: revids = sorted(self.request.GET.getlist("r")) context["newversion"] = reversion.get_for_object(self.object).get( id=revids[0]) context["oldversion"] = reversion.get_for_object(self.object).get( id=revids[1]) except reversion.revisions.Revision.DoesNotExist, IndexError: raise Http404
def moderate_accept(self, user): self.on_moderate_accept() self.moderation_status = MODERATION_STATUS['OK'] self.moderation_user = user if reversion.get_for_object(self).count(): self.moderation_last_ok_revision = reversion.get_for_object(self)[0] else: self.moderation_last_ok_revision = None super(Moderated, self).save() content_type = ContentType.objects.get_for_model(self) # get(app_label="unravelling", model="item") LogEntry.objects.log_action(user.id, content_type.id, self.id, unicode(self), CHANGE, "MODERATION ACCEPT")
def testCanGetForObject(self): # Test a model with an int pk. versions = reversion.get_for_object(self.test11) self.assertEqual(len(versions), 2) self.assertEqual(versions[0].field_dict["name"], "model1 instance1 version2") self.assertEqual(versions[1].field_dict["name"], "model1 instance1 version1") # Test a model with a str pk. versions = reversion.get_for_object(self.test21) self.assertEqual(len(versions), 2) self.assertEqual(versions[0].field_dict["name"], "model2 instance1 version2") self.assertEqual(versions[1].field_dict["name"], "model2 instance1 version1")
def post(self, request, *args, **kwargs): object = self.get_object() print(request.POST) description = request.POST.get('text') print(reversion.get_for_object(object)) revision = reversion.get_for_object(object).first().revision restore_point = self.item = RestorePoint.objects.create(user=request.user,object=object.editoritem,description=description,revision=revision) return self.response()
def testCanSaveAutoInitialOnChange(self): self.assertEqual(reversion.get_for_object(self.test11).count(), 0) with reversion.create_revision(): reversion.set_auto_initial(True, [ReversionTestModel1, ReversionTestModel2]) self.test11.name = "model1 instance1 version2" self.test11.save() self.test12.save() self.test21.save() self.test22.save() # Test that an initial is save because there's a data change and no previous revision self.assertEqual(reversion.get_for_object(self.test11).count(), 2)
def testRevisionSavedOnPost(self): self.assertEqual(ChildTestAdminModel.objects.count(), 0) # Create an instance via the admin. response = self.client.post("/admin/auth/childtestadminmodel/add/", { "parent_name": "parent instance1 version1", "child_name": "child instance1 version1", "_continue": 1, }) self.assertEqual(response.status_code, 302) obj_pk = response["Location"].split("/")[-2] obj = ChildTestAdminModel.objects.get(id=obj_pk) # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 1) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version1") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version1") # Save a new version. response = self.client.post("/admin/auth/childtestadminmodel/%s/" % obj_pk, { "parent_name": "parent instance1 version2", "child_name": "child instance1 version2", "_continue": 1, }) self.assertEqual(response.status_code, 302) # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 2) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version2") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version2") # Check that the versions can be listed. response = self.client.get("/admin/auth/childtestadminmodel/%s/history/" % obj_pk) self.assertContains(response, "child instance1 version2") self.assertContains(response, "child instance1 version1") # Check that a version can be rolled back. response = self.client.post("/admin/auth/childtestadminmodel/%s/history/%s/" % (obj_pk, versions[1].pk), { "parent_name": "parent instance1 version3", "child_name": "child instance1 version3", }) self.assertEqual(response.status_code, 302) # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 3) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version3") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version3") # Check that a deleted version can be viewed. obj.delete() response = self.client.get("/admin/auth/childtestadminmodel/recover/") self.assertContains(response, "child instance1 version3") # Check that a deleted version can be recovered. response = self.client.post("/admin/auth/childtestadminmodel/recover/%s/" % versions[0].pk, { "parent_name": "parent instance1 version4", "child_name": "child instance1 version4", }) obj = ChildTestAdminModel.objects.get(id=obj_pk)
def test_restore(self): """Test restoring of an object.""" import reversion obj = self.model.objects.get(slug=self.slug) history = reversion.get_for_object(obj) self.assertTrue(len(history) > 0) revcount = len(history) response = self.client.post(reverse(self.urlprefix + "_restore", kwargs={ "slug": self.slug, "revision": history[0].id, })) self.assertEqual(response.status_code, 302) self.assertEqual(len(reversion.get_for_object(obj)), revcount + 1)
def get_context_data(self, **kwargs): context = super(PortalRevisionDiffView, self).get_context_data(**kwargs) try: revids = sorted(self.request.GET.getlist("r")) context["newversion"] = reversion.get_for_object(self.object).get( id=revids[0]) context["oldversion"] = reversion.get_for_object(self.object).get( id=revids[1]) except (reversion.revisions.Revision.DoesNotExist, IndexError): raise Http404 messages.add_message(self.request, messages.WARNING, "You are comparing " "revisions %s and %s" % (context["newversion"].id, context["oldversion"].id)) return context
def test_revert_document(self): document = Document.objects.get() versions = reversion.get_for_object(document) self.assertEqual(len(versions), 2) # second step try to revert to old version response = self.app.post(reverse('documents:revert'), {'id': versions[1].pk, 'url_title': document.url_title}, user=self.user, xhr=True) self.assertEqual(response.status_code, 200) versions = reversion.get_for_object(document) self.assertEqual(len(versions), 3) self.assertEqual(versions[0].object.text, "text") self.assertEqual(versions[0].revision.comment, 'reverted to revision "test version"')
def test_creates_reversion(self): form_data = self.form_data form_data['c-%d' % self.lex_b.id] = "%s" % self.cogset1.id form_data['c-%d' % self.lex_a.id] = "-%s" % self.cogset1.id response = self.AuthenticatedClient.post(self.url, form_data, follow=True) version_list = reversion.get_for_object(self.cogset1) assert len(version_list) == 1, "Cognate Version List Missing" version_list = reversion.get_for_object(self.lex_b) assert len(version_list) == 1, "Lex B Version List Missing" assert len(reversion.get_deleted(Cognate)) == 1
def test_initial_state(self): self.assertTrue(reversion.is_registered(SimpleModel)) self.assertEqual(SimpleModel.objects.count(), 2) self.assertEqual(SimpleModel.objects.all()[0].text, "version two") self.assertEqual(reversion.get_for_object(self.item1).count(), 2) self.assertEqual(reversion.get_for_object(self.item2).count(), 5) self.assertEqual(Revision.objects.all().count(), 7) self.assertEqual(Version.objects.all().count(), 7) # query.ValuesListQuerySet() -> list(): self.assertEqual(list(self.version_ids1), [2, 1]) self.assertEqual(list(self.version_ids2), [7, 6, 5, 4, 3])
def given_i_process_a_facility(step): facility_json = json.loads('{ "uuid": "1234", "name": " Some HOSPITAL", "active": true, "href": "http://dhis/api-fred/v1/facilities/123", "createdAt": "2013-01-15T11:14:02.863+0000", "updatedAt": "2013-01-15T11:14:02.863+0000", "coordinates": [34.19622, 0.70331], "identifiers": [{ "agency": "DHIS2", "context": "DHIS2_UID", "id": "123" }], "properties": { "dataSets": ["123456"], "level": 5, "ownership": "Private Not For Profit", "parent": "56789", "type": "General Hospital" }}') facility_json['name'] = "Apo" + str(randint(1,9999)) facility = HealthFacility(name="ThoughtWorks facility", uuid = "1234") create_facility(facility) reversion.get_for_object(facility).delete() facility = HealthFacilityBase.objects.get(uuid="1234") assert facility.name != facility_json['name'] process_facility(facility_json) facility = HealthFacilityBase.objects.get(uuid="1234") assert facility.name == facility_json['name']
def modified_by(self): version = reversion.get_for_object(self).first() return version.revision.user
def get_queryset(self): """ Obtiene el user story y sus versiones """ return reversion.get_for_object(self.us)
def _get_versions_obj(obj): return reversion.get_for_object(obj).order_by("id")
def revert_to(self, article, revision): reversion.get_for_object(article)[revision].revision.revert()
def testCanRevertVersion(self): reversion.get_for_object(self.test11)[1].revert() self.assertEqual( ReversionTestModel1.objects.get(id=self.test11.pk).name, "model1 instance1 version1")
def testCanRetreiveFullFieldDict(self): self.assertEqual( reversion.get_for_object(self.testchild1)[0].field_dict["name"], "modelchild1 instance1 version 1", )
def testRevisionSavedOnPost(self): self.assertEqual(ChildTestAdminModel.objects.count(), 0) # Create an instance via the admin. response = self.client.post( "/admin/test_app/childtestadminmodel/add/", { "parent_name": "parent instance1 version1", "child_name": "child instance1 version1", "_continue": 1, }) self.assertEqual(response.status_code, 302) obj_pk = resolve(response["Location"].replace("http://testserver", "")).args[0] obj = ChildTestAdminModel.objects.get(id=obj_pk) # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 1) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version1") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version1") # Save a new version. response = self.client.post( reverse("admin:test_app_childtestadminmodel_change", args=(obj_pk, )), { "parent_name": "parent instance1 version2", "child_name": "child instance1 version2", "_continue": 1, }) self.assertEqual(response.status_code, 302) # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 2) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version2") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version2") # Check that the versions can be listed. response = self.client.get( reverse("admin:test_app_childtestadminmodel_history", args=(obj_pk, ))) self.assertContains(response, "child instance1 version2") self.assertContains(response, "child instance1 version1") # Check that version data can be loaded. response = self.client.get( reverse("admin:test_app_childtestadminmodel_revision", args=(obj_pk, versions[1].pk))) self.assertContains(response, "parent instance1 version1") self.assertContains(response, "child instance1 version1") # Check that loading the version data didn't roll it back! obj = ChildTestAdminModel.objects.get(pk=obj.pk) self.assertEqual(obj.child_name, "child instance1 version2") self.assertEqual(obj.parent_name, "parent instance1 version2") self.assertEqual(reversion.get_for_object(obj).count(), 2) # Check that a version can be rolled back. response = self.client.post( reverse("admin:test_app_childtestadminmodel_revision", args=(obj_pk, versions[1].pk)), { "parent_name": "parent instance1 version3", "child_name": "child instance1 version3", }, ) self.assertEqual(response.status_code, 302) # Check that the models were rolled back. obj = ChildTestAdminModel.objects.get(pk=obj.pk) self.assertEqual(obj.child_name, "child instance1 version3") self.assertEqual(obj.parent_name, "parent instance1 version3") # Check that a version is created. versions = reversion.get_for_object(obj) self.assertEqual(versions.count(), 3) self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version3") self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version3") # Check that a deleted version can be viewed in the list. obj.delete() response = self.client.get( "/admin/test_app/childtestadminmodel/recover/") self.assertContains(response, "child instance1 version3") # Check that a delete version can be viewed in detail. response = self.client.get( reverse("admin:test_app_childtestadminmodel_recover", args=(versions[0].pk, ))) self.assertContains(response, "parent instance1 version3") self.assertContains(response, "child instance1 version3") # Check that a deleted version can be recovered. response = self.client.post( reverse("admin:test_app_childtestadminmodel_recover", args=(versions[0].pk, )), { "parent_name": "parent instance1 version4", "child_name": "child instance1 version4", }) # Check that the models were rolled back. obj = ChildTestAdminModel.objects.get(pk=obj_pk) self.assertEqual(obj.child_name, "child instance1 version4") self.assertEqual(obj.parent_name, "parent instance1 version4") # Check that a version is created. versions = reversion.get_for_object_reference(ChildTestAdminModel, obj_pk) self.assertEqual(versions.count(), 4) self.assertEqual(versions[0].field_dict["parent_name"], "parent instance1 version4") self.assertEqual(versions[0].field_dict["child_name"], "child instance1 version4")
def setUp(self): super(PatchTest, self).setUp() with reversion.create_revision(): self.test11.name = "model1 instance1 version2" self.test11.save() self.version2, self.version1 = reversion.get_for_object(self.test11)
def created_by(self): version = reversion.get_for_object(self).last() return version.revision.user
def test_mc_get_unique_versions(self): """ Because MCQ and FRQ use the same get_unique_versions function, this test does not repeat tests done by test_fr_get_unique_versions """ exam = Exam.objects.create(name='Test Exam', description='an exam for testing') concept_type = ContentType.objects.get_for_model(DummyConcept) option_type = ContentType.objects.get_for_model(MultipleChoiceOption) concept = DummyConcept.objects.get(name="Concept A") # Check that initial version appears in the list with reversion.create_revision(): question = MultipleChoiceQuestion.objects.create( exam=exam, question="Version 1 ?", number=1, content_type=concept_type, object_id=concept.id, id=1578) option_1 = MultipleChoiceOption.objects.create(question=question, index=1, text="A v1") option_2 = MultipleChoiceOption.objects.create(question=question, index=2, text="B v1") self.assertEqual(len(question.get_unique_versions()), 1) self.assertEqual(question.get_unique_versions()[0], reversion.get_for_object(question)[0]) # Change an option, should make a new unique version with reversion.create_revision(): option_1.text = "A v2" option_1.save() question.save() self.assertEqual(len(question.get_unique_versions()), 2) # Add an option, should make a new unique version with reversion.create_revision(): option_3 = MultipleChoiceOption.objects.create(question=question, index=3, text="C v1") option_3.save() question.save() self.assertEqual(len(question.get_unique_versions()), 3) option_versions = question.get_unique_versions( )[0].revision.version_set.filter(content_type__pk=option_type.id) options = (option.object for option in option_versions) self.assertIn(option_3, options) # Save question and options, should not make a new unique version with reversion.create_revision(): option_1.save() option_2.save() option_3.save() question.save() self.assertEqual(len(question.get_unique_versions()), 3) # Revert, should delete an option but not make new unique version with reversion.create_revision(): revision = question.get_unique_versions()[1].revision question.revision_revert(revision) self.assertEqual(len(question.get_unique_versions()), 3) self.assertNotIn(option_3, MultipleChoiceOption.objects.all()) # Revert back, option_3 should exist again with reversion.create_revision(): revision = question.get_unique_versions()[1].revision question.revision_revert(revision) self.assertEqual(len(question.get_unique_versions()), 3) self.assertIn(option_3, MultipleChoiceOption.objects.all()) # Delete option_3 manally, doesn't create a new unique version with reversion.create_revision(): option_3.delete() question.save() self.assertEqual(len(question.get_unique_versions()), 3) # Revert back, option_3 is recreated # Note, we have to 'get' the object because out local variable points to # an object that was deleted with reversion.create_revision(): revision = question.get_unique_versions()[1].revision question.revision_revert(revision) self.assertEqual(len(question.get_unique_versions()), 3) option_3 = MultipleChoiceOption.objects.get(text="C v1") self.assertIn(option_3, MultipleChoiceOption.objects.all()) # Delete an option manually, creates new unique version with reversion.create_revision(): option_1.delete() question.save() self.assertEqual(len(question.get_unique_versions()), 4) self.assertNotIn(option_1, MultipleChoiceOption.objects.all()) # Swap option texts. This should create a new unique version, because option # versions are compared by their serialized data, not just text with reversion.create_revision(): option_2.text = "C v1" option_3.text = "B v1" option_2.save() option_3.save() question.save() self.assertEqual(len(question.get_unique_versions()), 5) # Swap option order with reversion.create_revision(): option_2.index = 2 option_3.index = 1 option_2.save() option_3.save() question.save() self.assertEqual(len(question.get_unique_versions()), 6)
def revision(self): return reversion.get_for_object(self).order_by( '-revision__date_created').latest( 'revision__date_created').revision