コード例 #1
0
def test_get_changes_from_prev_history_obj_wrong_type(db):
    """
    Passing the wrong type of object to get_changes_from_prev_history_obj() raises an error.

    The get_changes_from_prev_history_obj() function expects its only argument to
    be an instance of one of the 'historial' models that django_simple_history creates.
    """
    organization = OrganizationFactory()
    with pytest.raises(AttributeError):
        get_changes_from_prev_history_obj(organization)
コード例 #2
0
ファイル: signals.py プロジェクト: newsrevenuehub/rev-engine
def post_create_historical_record_callback(sender, **kwargs):
    """
    Create a RevEngineHistoricalChange object for this change.

    Whenever django_simple_history creates an instance of one of its historical_*
    objects, we also create an instance of a RevEngineHistoricalChange.
    """
    history_instance = kwargs["history_instance"]
    changes_list = get_changes_from_prev_history_obj(history_instance)

    # Decide if a RevEngineHistoricalChange object should be created:
    #   - if the user has made changes, then create a RevEngineHistoricalChange object
    #   - if the user is creating or deleting an object, then create a
    #     RevEngineHistoricalChange object
    #   - otherwise, do not create a RevEngineHistoricalChange object
    need_to_create_historical_obj = False
    if changes_list:
        need_to_create_historical_obj = True
    elif history_instance.history_type in [
            RevEngineHistoricalChange.SIMPLE_HISTORY_TYPE_CREATED,
            RevEngineHistoricalChange.SIMPLE_HISTORY_TYPE_DELETED,
    ]:
        need_to_create_historical_obj = True
    # Create a RevEngineHistoricalChange.
    if need_to_create_historical_obj:
        RevEngineHistoricalChange.objects.create(
            app_label=kwargs["instance"]._meta.app_label,
            model=kwargs["instance"]._meta.model_name,
            object_id=kwargs["instance"].id,
            history_date=history_instance.history_date,
            history_type=history_instance.history_type,
            history_user=history_instance.history_user,
            history_change_reason=history_instance.history_change_reason or "",
            changes_html=".<br><br>".join(changes_list),
        )
コード例 #3
0
 def changes(self, obj):
     """Show the changes for this object in relation to the previous history object."""
     changes_list = get_changes_from_prev_history_obj(obj)
     if obj.prev_record and changes_list:
         return format_html(".<br><br>".join(changes_list))
     elif obj.prev_record:
         return "No changes"
     else:
         return "No previous record"
コード例 #4
0
def test_get_changes_from_prev_history_obj_change_no_diff(db):
    """A historical change to update an object with no changes returns no changes."""
    # Create an Organization.
    organization = OrganizationFactory()
    historial_change_create = organization.history.first()
    # Save the Organization, but don't change any of the values.
    organization.save()
    historical_change_update = organization.history.exclude(
        history_id=historial_change_create.history_id).first()
    # The changes from the update should equal [].
    assert get_changes_from_prev_history_obj(historical_change_update) == []
コード例 #5
0
def test_get_changes_from_prev_history_obj_change_with_diff_jsonfield(db):
    """Historical change to update a JSONField only returns the field name in list of changes."""
    # Create a DonationPage.
    donation_page = DonationPageFactory()
    historial_change_create = donation_page.history.first()
    # Update the DonationPage's 'elements' field, which is a JSONField.
    donation_page.elements[0]["content"] += "something"
    donation_page.save()
    historical_change_update = donation_page.history.exclude(
        history_id=historial_change_create.history_id).first()
    # The changes from the update should only say the name of the 'elements' field.
    assert get_changes_from_prev_history_obj(historical_change_update) == [
        "'elements' changed"
    ]
コード例 #6
0
def test_get_changes_from_prev_history_obj_change_with_diff(db):
    """A historical change to update an object with a change returns a list of changes."""
    # Create an Organization.
    organization = OrganizationFactory(salesforce_id="", stripe_account_id="1")
    historial_change_create = organization.history.first()
    # Save the Organization and change some of the values.
    organization.salesforce_id = "1"
    organization.stripe_account_id = "2"
    organization.save()
    historical_change_update = organization.history.exclude(
        history_id=historial_change_create.history_id).first()
    # The changes from the update should equal be the changes that were made.
    assert get_changes_from_prev_history_obj(historical_change_update) == [
        "'Salesforce ID' changed from '' to '1'",
        "'stripe account id' changed from '1' to '2'",
    ]
コード例 #7
0
def test_get_changes_from_prev_history_obj_change_delete(db):
    """A historical change to delete an object returns no changes."""
    # Create an Organization.
    organization = OrganizationFactory(salesforce_id="", stripe_account_id="1")
    historial_change_create = organization.history.first()
    # Create a HistoricalOrganization object to delete the organization.
    HistoricalOrganization = historial_change_create.__class__
    historical_change_delete = HistoricalOrganization.objects.create(
        id=organization.id,
        history_date=historial_change_create.history_date,
        history_type="-",
        history_user=historial_change_create.history_user,
        history_change_reason=historial_change_create.history_change_reason
        or "",
    )
    # The changes from the delete should equal [], since the historical_change_delete
    # indicates that the organization should now be deleted.
    assert get_changes_from_prev_history_obj(historical_change_delete) == []
コード例 #8
0
def test_get_changes_from_prev_history_obj_create(db):
    """A historical change to create an object returns no changes."""
    organization = OrganizationFactory()
    historial_change_create = organization.history.first()
    assert get_changes_from_prev_history_obj(historial_change_create) == []