def test_meta_skip(self): """Test a model with the attribute listed under denorm_always_skip.""" comment = models.SkipCommentWithAttributeSkip( post=self.post, text='Yup, and they have wings!') comment.save() denorm.flush()
def recalculate_competitor_task(self, user_attendance_pk): from . import results user_attendance = UserAttendance.objects.get(pk=user_attendance_pk) if user_attendance.team is not None: util.rebuild_denorm_models([user_attendance.team]) denorm.flush() results.recalculate_result_competitor_nothread(user_attendance)
def test_m2m_relation(self): f1 = models.Forum.objects.create(title="forumone") p1 = models.Post.objects.create(forum=f1, title="post1") m1 = models.Member.objects.create(first_name="first1", name="last1") denorm.flush() m1.bookmarks.add(p1) denorm.flush() self.assertTrue('post1' in models.Member.objects.get(id=m1.id).bookmark_titles) p1.title = "othertitle" p1.save() denorm.flush() self.assertTrue('post1' not in models.Member.objects.get(id=m1.id).bookmark_titles) self.assertTrue('othertitle' in models.Member.objects.get(id=m1.id).bookmark_titles) p2 = models.Post.objects.create(forum=f1, title="thirdtitle") m1.bookmarks.add(p2) denorm.flush() self.assertTrue('post1' not in models.Member.objects.get(id=m1.id).bookmark_titles) self.assertTrue('othertitle' in models.Member.objects.get(id=m1.id).bookmark_titles) self.assertTrue('thirdtitle' in models.Member.objects.get(id=m1.id).bookmark_titles) m1.bookmarks.remove(p1) denorm.flush() self.assertTrue('othertitle' not in models.Member.objects.get(id=m1.id).bookmark_titles) self.assertTrue('thirdtitle' in models.Member.objects.get(id=m1.id).bookmark_titles)
def change_invoice_payments_status(sender, instance, changed_fields=None, **kwargs): field, (old, new) = next(iter(changed_fields.items())) if new is not None: for payment in instance.payment_set.all(): payment.status = Status.INVOICE_PAID payment.save() denorm.flush()
def rebuild_denorm_models(models): for model in models: content_type = contenttypes.models.ContentType.objects.get_for_model(model.__class__) denorm.models.DirtyInstance.objects.create( content_type=content_type, object_id=model.pk, ) denorm.flush()
def add_payments(self): assert len(self.payment_set.all()) == 0 payments = self.payments_to_add() self.payment_set.set(payments) for payment in payments: payment.status = Status.INVOICE_MADE payment.save() denorm.flush()
def test_no_dependency(self): m1 = models.Member.objects.create(first_name="first", name="last") denorm.flush() self.assertEqual(models.Member.objects.get(id=m1.id).full_name, "first last") models.Member.objects.filter(id=m1.id).update(first_name="second") denorm.flush() self.assertEqual(models.Member.objects.get(id=m1.id).full_name, "second last")
def test_payment_waiting(self): payment = models.Payment.objects.get(pk=4) payment.status = 1 payment.save() denorm.flush() user = models.UserAttendance.objects.get(pk=1115) self.assertEqual(user.payment_status, "waiting") self.assertEqual(user.representative_payment, payment) self.assertEqual(user.payment_class(), "warning") self.assertEqual(str(user.get_payment_status_display()), "nepotvrzeno")
def touch_items(self, pks, object_app_label, object_model_name): for pk in pks: content_type = contenttypes.models.ContentType.objects.get( app_label=object_app_label, model=object_model_name) denorm.models.DirtyInstance.objects.create( content_type=content_type, object_id=pk, ) denorm.flush() return len(pks)
def test_payment_unknown(self): payment = models.Payment.objects.get(pk=4) payment.status = 123 payment.save() denorm.flush() user = models.UserAttendance.objects.get(pk=1115) self.assertEqual(user.payment_status, 'unknown') self.assertEqual(user.representative_payment, payment) self.assertEqual(user.payment_class(), 'warning') self.assertEqual(str(user.get_payment_status_display()), 'neznámý')
def test_no_payment_no_admission(self): campaign = models.Campaign.objects.get(pk=339) price_level_models.PriceLevel.objects.all().delete() campaign.save() models.UserAttendance.objects.get(pk=1115).save() denorm.flush() user = models.UserAttendance.objects.get(pk=1115) self.assertEqual(user.payment_status, 'no_admission') self.assertEqual(user.representative_payment, None) self.assertEqual(user.payment_class(), 'success') self.assertEqual(str(user.get_payment_status_display()), 'neplatí se')
def test_self_backward_relation(self): f1 = models.Forum.objects.create(title="forumone") p1 = models.Post.objects.create(forum=f1, ) p2 = models.Post.objects.create(forum=f1, response_to=p1) p3 = models.Post.objects.create(forum=f1, response_to=p1) p4 = models.Post.objects.create(forum=f1, response_to=p2) denorm.flush() self.assertEqual(models.Post.objects.get(id=p1.id).response_count, 3) self.assertEqual(models.Post.objects.get(id=p2.id).response_count, 1) self.assertEqual(models.Post.objects.get(id=p3.id).response_count, 0) self.assertEqual(models.Post.objects.get(id=p4.id).response_count, 0)
def test_answer_post_save_single_user(self): competition = models.Competition.objects.create( competitor_type="single_user", competition_type="questionnaire", campaign_id=339, ) question = models.Question.objects.create(competition=competition) models.CompetitionResult.objects.filter().delete() models.UserAttendance.objects.get(pk=1115).save() denorm.flush() self.assertFalse(models.CompetitionResult.objects.exists()) models.Answer.objects.create(question=question, user_attendance_id=1115) self.assertEquals(models.CompetitionResult.objects.get().result, 0.0) self.assertEquals(models.CompetitionResult.objects.get().user_attendance_id, 1115)
def test_foreignkey(self): f1 = models.Forum.objects.create(title="forumone") f2 = models.Forum.objects.create(title="forumtwo") m1 = models.Member.objects.create(first_name="first1", name="last1") p1 = models.Post.objects.create(forum=f1, author=m1) a1 = models.Attachment.objects.create(post=p1) self.assertEqual(models.Attachment.objects.get(id=a1.id).forum, f1) a2 = models.Attachment.objects.create() self.assertEqual(models.Attachment.objects.get(id=a2.id).forum, None) # Change forum p1.forum = f2 p1.save() denorm.flush() self.assertEqual(models.Attachment.objects.get(id=a1.id).forum, f2)
def test_dependency_chains(self): # create a forum, a member and a post f1 = models.Forum.objects.create(title="forumone") m1 = models.Member.objects.create(name="memberone") models.Post.objects.create(forum=f1, author=m1) denorm.flush() # check the forums author list contains the member self.assertEqual(models.Forum.objects.get(id=f1.id).author_names, "memberone") # change the member's name m1.name = "membertwo" m1.save() denorm.flush() # check again self.assertEqual(models.Forum.objects.get(id=f1.id).author_names, "membertwo")
def test_depends_related(self): """ Test the DependsOnRelated stuff. """ # Make a forum, check it's got no posts f1 = models.Forum.objects.create(title="forumone") self.assertEqual(f1.post_count, 0) # Check its database copy too self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 0) # Add a post p1 = models.Post.objects.create(forum=f1) # Has the post count updated? self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 1) denorm.flush() # Check its title, in p1 and the DB self.assertEqual(p1.forum_title, "forumone") self.assertEqual( models.Post.objects.get(id=p1.id).forum_title, "forumone") # Update the forum title f1.title = "forumtwo" f1.save() denorm.flush() # Has the post's title changed? self.assertEqual( models.Post.objects.get(id=p1.id).forum_title, "forumtwo") # Add and remove some posts and check the post count models.Post.objects.create(forum=f1) self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 2) models.Post.objects.create(forum=f1) self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 3) p1.delete() self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 2) # Delete everything, check once more. models.Post.objects.all().delete() self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 0)
def test_member_count_update(self): team = models.Team.objects.get(id=1) self.assertEqual(team.member_count, 3) campaign = models.Campaign.objects.get(pk=339) user = models.User.objects.create(first_name="Third", last_name="User", username="******") userprofile = models.UserProfile.objects.create(user=user) user_attendance = models.UserAttendance.objects.create( team=team, campaign=campaign, userprofile=userprofile, approved_for_team='approved') models.Payment.objects.create(status=99, amount=1, user_attendance=user_attendance) denorm.flush() team = models.Team.objects.get(id=1) self.assertEqual(team.member_count, 4)
def test_depends_related(self): """ Test the DependsOnRelated stuff. """ # Make a forum, check it's got no posts f1 = models.Forum.objects.create(title="forumone") self.assertEqual(f1.post_count, 0) # Check its database copy too self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 0) # Add a post p1 = models.Post.objects.create(forum=f1) # Has the post count updated? self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 1) denorm.flush() # Check its title, in p1 and the DB self.assertEqual(p1.forum_title, "forumone") self.assertEqual(models.Post.objects.get(id=p1.id).forum_title, "forumone") # Update the forum title f1.title = "forumtwo" f1.save() denorm.flush() # Has the post's title changed? self.assertEqual(models.Post.objects.get(id=p1.id).forum_title, "forumtwo") # Add and remove some posts and check the post count models.Post.objects.create(forum=f1) self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 2) models.Post.objects.create(forum=f1) self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 3) p1.delete() self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 2) # Delete everything, check once more. models.Post.objects.all().delete() self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 0)
def test_trees(self): f1 = models.Forum.objects.create(title="forumone") f2 = models.Forum.objects.create(title="forumtwo", parent_forum=f1) f3 = models.Forum.objects.create(title="forumthree", parent_forum=f2) denorm.flush() self.assertEqual(f1.path, '/forumone/') self.assertEqual(f2.path, '/forumone/forumtwo/') self.assertEqual(f3.path, '/forumone/forumtwo/forumthree/') f1.title = 'someothertitle' f1.save() denorm.flush() f1 = models.Forum.objects.get(id=f1.id) f2 = models.Forum.objects.get(id=f2.id) f3 = models.Forum.objects.get(id=f3.id) self.assertEqual(f1.path, '/someothertitle/') self.assertEqual(f2.path, '/someothertitle/forumtwo/') self.assertEqual(f3.path, '/someothertitle/forumtwo/forumthree/')
def test_bulk_update(self): """ Test the DependsOnRelated stuff. """ f1 = models.Forum.objects.create(title="forumone") f2 = models.Forum.objects.create(title="forumtwo") p1 = models.Post.objects.create(forum=f1) p2 = models.Post.objects.create(forum=f2) denorm.flush() self.assertEqual(models.Post.objects.get(id=p1.id).forum_title, "forumone") self.assertEqual(models.Post.objects.get(id=p2.id).forum_title, "forumtwo") self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 1) self.assertEqual(models.Forum.objects.get(id=f2.id).post_count, 1) models.Post.objects.update(forum=f1) denorm.flush() self.assertEqual(models.Post.objects.get(id=p1.id).forum_title, "forumone") self.assertEqual(models.Post.objects.get(id=p2.id).forum_title, "forumone") self.assertEqual(models.Forum.objects.get(id=f1.id).post_count, 2) self.assertEqual(models.Forum.objects.get(id=f2.id).post_count, 0) models.Forum.objects.update(title="oneforall") denorm.flush() self.assertEqual(models.Post.objects.get(id=p1.id).forum_title, "oneforall") self.assertEqual(models.Post.objects.get(id=p2.id).forum_title, "oneforall")
def test_m2m(self): f1 = models.Forum.objects.create(title="forumone") m1 = models.Member.objects.create(name="memberone") models.Post.objects.create(forum=f1, author=m1) denorm.flush() # check the forums author list contains the member self.assertTrue(m1 in models.Forum.objects.get(id=f1.id).authors.all()) m2 = models.Member.objects.create(name="membertwo") p2 = models.Post.objects.create(forum=f1, author=m2) denorm.flush() self.assertTrue(m1 in models.Forum.objects.get(id=f1.id).authors.all()) self.assertTrue(m2 in models.Forum.objects.get(id=f1.id).authors.all()) p2.delete() denorm.flush() self.assertTrue(m2 not in models.Forum.objects.get(id=f1.id).authors.all())
def get_queryset(self): denorm.flush() return UserAttendance.objects.all().select_related("userprofile__user")
def test_meta_skip(self): """Test a model with the attribute listed under denorm_always_skip.""" comment = models.SkipCommentWithAttributeSkip(post=self.post, text='Yup, and they have wings!') comment.save() denorm.flush()
def test_with_skip(self): # This should not result in an endless loop. comment = models.SkipCommentWithSkip(post=self.post, text='Oh really?') comment.save() denorm.flush()
def process_response(self, request, response): flush() return response
def process_response(self, request, response): try: flush() except DatabaseError as e: logger.error(e) return response
def test_reverse_fk_null(self): f1 = models.Forum.objects.create(title="forumone") m1 = models.Member.objects.create(name="memberone") models.Post.objects.create(forum=f1, author=m1) models.Attachment.objects.create() denorm.flush()
def get_queryset(self): denorm.flush() return UserAttendance.objects.filter( id=self.ua().id, ).select_related("userprofile__user")
def user_attendance_pre_delete(sender, instance, *args, **kwargs): for payment in instance.payment_set.all(): payment.status = Status.COMPANY_ACCEPTS payment.save() denorm.flush()