From c6182c89849bc5b2bdcfcd9049fcbe297e8722bd Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 23 Jan 2020 06:54:34 +0000 Subject: [PATCH] Allow importing the old portal to be optional --- apiserver/apiserver/api/models.py | 2 -- apiserver/apiserver/api/serializers.py | 3 +-- apiserver/apiserver/api/utils.py | 14 +++++++++++++- apiserver/import_old_portal.py | 3 +-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index 2f6457b..e21269e 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -3,8 +3,6 @@ from django.db import models from django.contrib.auth.models import User from django.utils.timezone import now -from . import old_models - IGNORE = '+' class Member(models.Model): diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 53b2a89..3a6a7a8 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -6,10 +6,9 @@ from rest_framework.validators import UniqueValidator from rest_auth.registration.serializers import RegisterSerializer from rest_auth.serializers import UserDetailsSerializer -from . import models, old_models, fields, utils +from . import models, fields, utils from .. import settings - class TransactionSerializer(serializers.ModelSerializer): # fields directly from old portal. replace with slugs we want account_type = serializers.ChoiceField([ diff --git a/apiserver/apiserver/api/utils.py b/apiserver/apiserver/api/utils.py index d500ffd..7e20cb1 100644 --- a/apiserver/apiserver/api/utils.py +++ b/apiserver/apiserver/api/utils.py @@ -7,7 +7,11 @@ from bleach.sanitizer import Cleaner from django.db.models import Sum -from . import models, old_models +from . import models +try: + import old_models +except ModuleNotFoundError: + old_models = None def num_months_spanned(d1, d2): ''' @@ -192,7 +196,14 @@ def link_old_member(data, user): If a member claims they have an account on the old protospace portal, go through and link their objects to their new user using the member_id found with their email as a hint + + Since this runs AFTER registration, we need to delete the user on any + failures or else the username will be taken when they try again ''' + if not old_models: + user.delete() + raise ValidationError(dict(email='Unable to link, old DB wasn\'t imported.')) + old_members = old_models.Members.objects.using('old_portal') try: @@ -204,6 +215,7 @@ def link_old_member(data, user): member = models.Member.objects.get(id=old_member.id) if member.user: + user.delete() raise ValidationError(dict(email='Old member already claimed.')) member.user = user diff --git a/apiserver/import_old_portal.py b/apiserver/import_old_portal.py index f5187cc..0fcd3b9 100755 --- a/apiserver/import_old_portal.py +++ b/apiserver/import_old_portal.py @@ -4,7 +4,6 @@ django.setup() import datetime from apiserver.api import models, old_models, utils -from apiserver.api.serializers import process_image MEMBER_FIELDS = [ 'id', @@ -101,7 +100,7 @@ for o in old: if str(o.id) in photo_folders: folder = 'old_photos/' + str(o.id) if 'photo.jpg' in os.listdir(folder): - small, medium, large = process_image(folder + '/photo.jpg') + small, medium, large = utils.process_image(folder + '/photo.jpg') print('Found a photo') models.Member.objects.create(photo_small=small, photo_medium=medium, photo_large=large, **new)