def form_valid( self, form: MembershipInformationForm ) -> Optional[MembershipInformation]: # Create the stripe customer customer, err = stripewrapper.create_customer(self.request.user.alumni) if err is not None: form.add_error( None, 'Something went wrong when talking to our payment service provider. Please try again later or contact support. ' ) return None # store the information instance = form.save(commit=False) instance.member = self.request.user.alumni instance.customer = customer instance.save() # if we selected the starter tier, create subscription information now if instance.tier == TierField.STARTER: SubscriptionInformation.create_starter_subscription( self.request.user.alumni) return instance
def make_user(given_name: str, middle_name: str, family_name: str, email: str, nationality: Union[str, List[str]], birthday: datetime, member_type: str, member_tier: str, skip_stripe: bool) -> User: """ This function creates an returns a new alumni user. All exceptions should be caught by the parent """ # first generate a new username username = generate_username( given_name, middle_name, family_name, ) with transaction.atomic(): # create a user, may fail because of race conditions # but that failure should be caught by the caller user = User.objects.create_user(username=username) # create an alumni # may fail, as the email is duplicate-free alumni = Alumni.objects.create( profile=user, givenName=given_name, middleName=middle_name, familyName=family_name, email=email, sex=GenderField.UNSPECIFIED, birthday=birthday, nationality=nationality, # TODO: Need to add this field category=member_type, ) # create all the objects related to the alumni # assumed to never fail approval = Approval.objects.create( member=alumni, approval=False, gsuite=None) address = Address.objects.create(member=alumni) socials = SocialMedia.objects.create(member=alumni) jacobs = JacobsData.objects.create(member=alumni) job = JobInformation.objects.create(member=alumni) skills = Skills.objects.create(member=alumni) atlas = AtlasSettings.objects.create(member=alumni) # if needed, create a starter subscription if member_tier == TierField.STARTER: SubscriptionInformation.create_starter_subscription(alumni) # which may fail, and if it does should add an error stripe_customer = "" if not skip_stripe: stripe_customer, err = stripewrapper.create_customer(alumni) if err is not None: raise CustomerCreationFailed() membership = MembershipInformation.objects.create( member=alumni, tier=member_tier, customer=stripe_customer) return user
def form_valid(self, form: RegistrationForm) -> Optional[True]: """ Called when the form is valid and an instance is to be created """ # extract out the cleaned data cleaned_data = form.cleaned_data given_name = cleaned_data['givenName'] middle_name = cleaned_data['middleName'] family_name = cleaned_data['familyName'] email = cleaned_data['email'] nationality = cleaned_data['nationality'] birthday = cleaned_data['birthday'] member_type = cleaned_data['memberCategory'] member_tier = cleaned_data['memberTier'] # generate a username username = generate_username( given_name, middle_name, family_name, ) # create a user = None try: with transaction.atomic(): # create a user, may fail because of race conditions # but that failure isn't important user = User.objects.create_user(username=username) # create an alumni # may fail, as the email is duplicate-free alumni = Alumni.objects.create( profile=user, givenName=given_name, middleName=middle_name, familyName=family_name, email=email, sex=GenderField.UNSPECIFIED, birthday=birthday, nationality=nationality, # TODO: Need to add this field category=member_type, ) # create all the objects related to the alumni # assumed to never fail approval = Approval.objects.create(member=alumni, approval=False, gsuite=None) address = Address.objects.create(member=alumni) socials = SocialMedia.objects.create(member=alumni) jacobs = JacobsData.objects.create(member=alumni) job = JobInformation.objects.create(member=alumni) skills = Skills.objects.create(member=alumni) atlas = AtlasSettings.objects.create(member=alumni) # if needed, create a starter subscription if member_tier == TierField.STARTER: SubscriptionInformation.create_starter_subscription(alumni) # which may fail, and if it does should add an error stripe_customer, err = stripewrapper.create_customer(alumni) if err is not None: raise CustomerCreationFailed() membership = MembershipInformation.objects.create( member=alumni, tier=member_tier, customer=stripe_customer) # FIXME: This integrity error is currently assumed to be an already existing email except IntegrityError as ie: form.add_error( 'email', 'An Alumni with that email address already exists. Did you mean to login instead? ' ) return None except CustomerCreationFailed: form.add_error( None, 'Error when talking to our payment provider. Please try again later or contact support. ' ) return None # if all that worked, login the user login(self.request, user, backend='django.contrib.auth.backends.ModelBackend') # and finally return return True