Skip to content

feat!: rm user db_constraing in CourseEnrollment#78

Merged
johanseto merged 1 commit into
open-release/teak.nelpfrom
jlc/course-enrollment-user-rm-foreing-key
Jun 24, 2026
Merged

feat!: rm user db_constraing in CourseEnrollment#78
johanseto merged 1 commit into
open-release/teak.nelpfrom
jlc/course-enrollment-user-rm-foreing-key

Conversation

@johanseto

@johanseto johanseto commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Description

feat!: rm user db_constraing in CourseEnrollment

Consideration

The model get_or_create receives an user object. So is controlled that user is used to create a course_enrollment without using a random int.

@classmethod
def get_or_create_enrollment(cls, user, course_key):
"""
Create an enrollment for a user in a class. By default *this enrollment
is not active*. This is useful for when an enrollment needs to go
through some sort of approval process before being activated. If you
don't need this functionality, just call `enroll()` instead.
Returns a CourseEnrollment object.
`user` is a Django User object. If it hasn't been saved yet (no `.id`
attribute), this method will automatically save it before
adding an enrollment for it.
`course_key` must be a opaque_keys CourseKey object.
It is expected that this method is called from a method which has already
verified the user authentication and access.
If the enrollment is done due to a CourseEnrollmentAllowed, the CEA will be
linked to the user being enrolled so that it can't be used by other users.
"""
# If we're passing in a newly constructed (i.e. not yet persisted) User,
# save it to the database so that it can have an ID that we can throw
# into our CourseEnrollment object. Otherwise, we'll get an
# IntegrityError for having a null user_id.
assert isinstance(course_key, CourseKey)
if user.id is None:
user.save()
enrollment, __ = cls.objects.get_or_create(
user=user,

Test

Run migrations

image

@johanseto johanseto requested a review from andrey-canon June 23, 2026 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants