Skip to content

Commit

Permalink
fix: With persisted data, seeds might crash on previously added data
Browse files Browse the repository at this point in the history
We fix this by explicitly setting an ID, and by allowing multiple
accounts with the demo emailaddress to exist. We then pick the first.
  • Loading branch information
berkes committed Feb 13, 2025
1 parent c604da6 commit 2953dec
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
3 changes: 3 additions & 0 deletions apps/mainsite/seeds/01_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@


setattr(settings, 'SUPPRESS_EMAILS', 1)
badgr_app_id = getattr(settings, 'BADGR_APP_ID')


main_badgr_app, _ = BadgrApp.objects.get_or_create(
id=badgr_app_id,
is_active=1,
cors=settings.UI_URL,
email_confirmation_redirect="{}/login/".format(settings.UI_URL),
Expand Down
8 changes: 2 additions & 6 deletions apps/mainsite/seeds/04_badgeinstances.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from django.conf import settings

from badgeuser.models import BadgeUser
Expand All @@ -19,10 +18,7 @@
REVOKED_STUDENT_EMAIL

super_user = BadgeUser.objects.get(username=settings.SUPERUSER_NAME)
badgr_app = BadgrApp.objects.first()
if not badgr_app:
sys.stderr.write("No BadgrApp found. Did other seeds fail to run?")
sys.exit(1)
badgr_app = BadgrApp.objects.get(id=getattr(settings, 'BADGR_APP_ID'))

def create_badge_instance(user, badge_class, revoked, acceptance="Unaccepted"):
badge_class.issue(recipient=user, created_by=super_user, allow_uppercase=True,
Expand All @@ -37,7 +33,7 @@ def create_enrollments_badge_instances(user, bc_names, revoked, acceptance="Unac
create_badge_instance(user, bc, revoked, acceptance)

# Create enrollments
demo_user = BadgeUser.objects.get(email=DEMO_STUDENT_EMAIL)
demo_user = BadgeUser.objects.filter(email=DEMO_STUDENT_EMAIL).first()
create_enrollments_badge_instances(demo_user,
[BADGE_CLASS_COGNITIVE_PSYCHOLOGY, BADGE_CLASS_INTRODUCTION_TO_PSYCHOLOGY],
False,
Expand Down

0 comments on commit 2953dec

Please sign in to comment.