Skip to content

Improve usability of created/created_by/set_at/set_by in the publishing API ? #549

Open

Description

@bradenmacdonald
  • If you call create_publishable_entity, specifying created (timestamp) and created_by is mandatory, and gets set directly on the new PublishableEntity model.
    • If you do this within a bulk_draft_changes_for/DraftChangeLogContext, the changed_at/changed_by from the context does not override the created/created_by specified on create_publishable_entity(). Note also that simply creating an entity without any version does not affect the changelog nor create a change log record (expected).
  • If you call create_publishable_entity_version, specifying created (timestamp) and created_by is mandatory.
    • If you do this within a bulk_draft_changes_for/DraftChangeLogContext, the changed_at/changed_by from the context overrides the the created/created_by specified here. But you still have to specify it.
  • If you call soft_delete_draft, specifying deleted_by (user id) is optional, and specifying a timestamp is not possible.
    • If you do this within a bulk_draft_changes_for/DraftChangeLogContext, the changed_at/changed_by from the context overrides the the deleted_by specified here.
  • If you call reset_drafts_to_published, specifying reset_at/reset_by is optional.
    • If you do this within a bulk_draft_changes_for/DraftChangeLogContext, the changed_at/changed_by from the context overrides them, and they are ignored.
  • If you call set_draft_version directly, specifying set_at/set_by is optional.
    • If you do this within a bulk_draft_changes_for/DraftChangeLogContext, the reset_at/reset_by from the context overrides them, and they are ignored.

The Containers and Components APIs also often accept/require created/created_by arguments, which are sometimes mandatory and sometimes optional, and follow the same patterns as above.

The create_x_and_version APIs are generally structured so that the created timestamp is mandatory, passing it through the layers so that the X and XVersion both have the same created timestamp. The created_by is more likely to be optional because it will still be consistently None even if omitted at the top level.

When the user ID is optional, the API cannot detect/model the difference between "I forgot to specify the user" and "this is a system action with no associated user" - both use user_id=None.

Names of the timestamp variable: changed_at, created, reset_at, set_at

Names of the user ID variable: changed_by, created_by, reset_by, set_by, deleted_by

Activity

  1. bradenmacdonald commented on Apr 16, 2026

    @bradenmacdonald
    ContributorAuthor

    Suggestion: make all the parameters optional, but throw an error if they are not specified when used outside of bulk_draft_changes_for. Make the default value of the user _by parameters a sentinel value like AUTO (get from the bulk context) so that user_id=None is always a very explicit choice.

  2. kdmccormick commented on Apr 24, 2026

    @kdmccormick
    Member

    I propose that we should define, either in openedx_django_lib or in publishing/api.py:

    type Author = (
      int  # A specific user's ID (in the future would be good to change this to UserID)
      | AUTHOR_AUTO  # The bulk-context user. ValueError if not in a bulk-context.
      | AUTHOR_NONE  # Exceptional cases where there's nobody to attribute authorship to.
    )
    type Timestamp = (
        datetime # Use a specific time.
        | TIMESTAMP_AUTO  # Use the bulk-context time, or use now() if not in a bulk context.
    )

    and then all relevant API functions could take:

    def verb_something(
        ...,
        `<verbed>_by`: Author = AUTHOR_AUTO,
        `<verbed>_at`: Timestamp  = TIMESTAMP_AUTO,
    )

    Notes:

    • bulk_draft_changes_for should just take int | AUTHOR_NONE, because there's nothing for it to get "AUTO" from.
    • Generally, within a bulk_draft_changes_for, functions can accept overrides to their Author and Timestamp, and those overrides will be saved on the models (ComponentVersion, etc.). But some functions (e.g. set_draft_version, soft_delete_draft, reset_drafts_to_published) are just operations on the Draft and DraftChangeLogEntry, and thus have nowhere to write the overrides. So, inside a bulk context, they'd raise ValueError unless they have AUTHOR_AUTO and TIMESTAMP_AUTO.
  3. bradenmacdonald commented on Apr 24, 2026

    @bradenmacdonald
    ContributorAuthor

    @kdmccormick I was thinking vaguely along similar lines and I really like your proposal. The main things I want to avoid are forgetting to specify the user (if we left the default as None) and duplicate specifying in the bulk context and the individual APIs, and your suggestion fixes both. I think the <verbed>_by: Author and <verbed>_at: Timestamp also make the fields more understandable because I do find the "verbed_by" and "verbed_at" to be slightly vague/ambiguous as descriptors.

  4. self-assigned this
    on Apr 24, 2026
  5. kdmccormick commented on Apr 24, 2026

    @kdmccormick
    Member

    Great. As part of this, I'll also fix the Collections API

  6. kdmccormick commented on Apr 29, 2026

    @kdmccormick
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions