Beispiel #1
0
def household(request):
    enrollment = get_or_create_enrollment(request)

    if enrollment.state == 'payment':
        # Payment has been initiated but the user goes back here - why?
        # Reset the state and let them reinitiate payment when they're ready.
        enrollment.state = 'registration'
        enrollment.save()
    elif enrollment.state == 'complete':
        # A previous registration has been completed, so why would the user come directly here?
        # Just redirect them back to registration which will restart a new registration.
        return redirect("enrollment:registration")

    errors = invalid_location in request.GET
    if request.method == 'POST':
        enrollment.country = request.POST['country']
        enrollment.address1 = polite_title(request.POST['address1'])
        enrollment.address2 = polite_title(request.POST['address2'])
        enrollment.address3 = polite_title(request.POST['address3'])
        enrollment.zipcode = request.POST['zipcode']
        enrollment.area = request.POST.get('area', '')
        enrollment.existing_memberid = request.POST.get('existing', '')[:50]
        enrollment.wants_yearbook = enrollment.country != 'NO' and 'yearbook' in request.POST
        enrollment.attempted_yearbook = False
        if enrollment.wants_yearbook:
            if enrollment.existing_memberid != '' or not enrollment.has_potential_main_member():
                enrollment.wants_yearbook = False
                enrollment.attempted_yearbook = True
        enrollment.save()

        if enrollment.validate_location():
            if enrollment.country != 'NO':
                return redirect('enrollment:verification')
            else:
                try:
                    focus_zipcode = FocusZipcode.objects.get(zipcode=enrollment.zipcode)
                    Forening.objects.get(focus_id=focus_zipcode.main_forening_id) # Verify that the Forening exists
                    return redirect('enrollment:verification')
                except FocusZipcode.DoesNotExist:
                    # We know that this zipcode exists in Zipcode, because validate_location validated, and it checks
                    # for that
                    logger.warning(
                        "Postnummer finnes i Zipcode, men ikke i Focus!",
                        exc_info=sys.exc_info(),
                        extra={
                            'request': request,
                            'postnummer': enrollment.zipcode
                        }
                    )
                    messages.error(request, 'focus_zipcode_missing')
                except Forening.DoesNotExist:
                    logger.warning(
                        "Focus-postnummer mangler foreningstilknytning!",
                        exc_info=sys.exc_info(),
                        extra={'request': request}
                    )
                    messages.error(request, 'focus_zipcode_missing')
        else:
            errors = True

    context = {
        'enrollment': enrollment,
        'invalid_existing': invalid_existing in request.GET,
        'countries': FocusCountry.get_sorted(),
        'foreign_shipment_price': FOREIGN_SHIPMENT_PRICE,
        'errors': errors,
    }
    context.update(current_template_layout(request))
    return render(request, 'central/enrollment/household.html', context)
Beispiel #2
0
def registration(request, user):
    enrollment = get_or_create_enrollment(request)

    if enrollment.state == 'payment':
        # Payment has been initiated but the user goes back to the registration page - why?
        # Maybe it failed, and they want to retry registration?
        # Reset the state and let them reinitiate payment when they're ready.
        enrollment.state = 'registration'
        enrollment.save()
    elif enrollment.state == 'complete':
        # A previous registration has been completed, but a new one has been initiated.
        # Remove the old one and start over.
        del request.session['enrollment']
        enrollment = get_or_create_enrollment(request)

    if request.method == 'GET':
        if user is not None:
            try:
                user = enrollment.users.all().get(id=user)
            except EnrollmentUser.DoesNotExist:
                return redirect('enrollment:registration')

        # Display the registration form for the first registration, and when editing a user; hide otherwise
        show_registration_form = len(enrollment.users.all()) == 0 or user is not None

        context = {
            'enrollment': enrollment,
            'current_user': user,
            'show_registration_form': show_registration_form,
            'confirmation_age_min': min(settings.MEMBERSHIP['FAMILY']['YOUTH_CONFIRMATION_AGE_RANGE']),
            'confirmation_age_max': max(settings.MEMBERSHIP['FAMILY']['YOUTH_CONFIRMATION_AGE_RANGE']),
            'phone_required': json.dumps(len(enrollment.users.all()) == 0),
            'email_required': json.dumps(True),
        }
        context.update(current_template_layout(request))
        return render(request, 'central/enrollment/registration.html', context)

    elif request.method == 'POST':
        # We want to check provided user details if:
        # - The user has explicitly clicked the save button
        # - They're not editing a user, but added a new member and clicked continue
        # - They're editing an existing user and clicked continue
        just_save = request.POST['button'] == 'save'
        continue_ = request.POST['button'] == 'continue'
        editing_user = '******' in request.POST
        name_defined = len(request.POST['name'].strip()) > 0
        save_and_continue = continue_ and (name_defined or editing_user)

        # Save terms & conditions accepted
        enrollment.accepts_conditions = request.POST.get('conditions') == 'on'
        enrollment.save()

        # Save partneroffers optin
        enrollment.partneroffers_optin = 'partneroffers_optin' in request.POST
        enrollment.save()

        if just_save or save_and_continue:
            if editing_user:
                try:
                    user = enrollment.users.all().get(id=request.POST['user'])
                except EnrollmentUser.DoesNotExist:
                    # They're trying to save a non-existing user - maybe they deleted it in another tab? Just create a
                    # new user
                    user = EnrollmentUser(enrollment=enrollment)
            else:
                user = EnrollmentUser(enrollment=enrollment)

            # Titleize name and strip whitespace before/after dash
            user.name = re.sub('\s*-\s*', '-', polite_title(request.POST['name'].strip()))
            user.phone = request.POST['phone'].strip()
            user.email = request.POST['email'].lower().strip()
            user.gender = request.POST.get('gender', '')
            try:
                user.dob = datetime.strptime(request.POST['dob'], "%d.%m.%Y").date()
            except ValueError:
                user.dob = None
            user.save()

            if not user.is_valid():
                messages.error(request, 'user_invalid')
                return redirect('enrollment:registration', user.id)
            elif user.requires_family_membership_confirmation() and not user.has_confirmation_info():
                messages.error(request, 'user_requires_confirmation_info')
                return redirect('enrollment:registration', user.id)
            else:
                enrollment.users.add(user)
                # The user was saved successfully, so clear the form for the next user
                user = None

        if continue_:
            return redirect("enrollment:household")
        else:
            return redirect('enrollment:registration')
    else:
        raise PermissionDenied
Beispiel #3
0
def registration(request, user):
    enrollment = get_or_create_enrollment(request)

    if enrollment.state == 'payment':
        # Payment has been initiated but the user goes back to the registration page - why?
        # Maybe it failed, and they want to retry registration?
        # Reset the state and let them reinitiate payment when they're ready.
        enrollment.state = 'registration'
        enrollment.save()
    elif enrollment.state == 'complete':
        # A previous registration has been completed, but a new one has been initiated.
        # Remove the old one and start over.
        del request.session['enrollment']
        enrollment = get_or_create_enrollment(request)

    # User set via URL means a GET request to view some user
    if user is not None:
        try:
            user = enrollment.users.all().get(id=user)
        except EnrollmentUser.DoesNotExist:
            return redirect('enrollment.views.registration')

    errors = False
    if request.method == 'POST':

        # We want to check provided user details if:
        # - The user has explicitly clicked the save button
        # - They're not editing a user, but added a new member and clicked continue
        # - They're editing an existing user and clicked continue
        just_save = request.POST['button'] == 'save'
        continue_ = request.POST['button'] == 'continue'
        editing_user = '******' in request.POST
        name_defined = len(request.POST['name'].strip()) > 0
        save_and_continue = continue_ and (name_defined or editing_user)

        if just_save or save_and_continue:
            if editing_user:
                try:
                    user = enrollment.users.all().get(id=request.POST['user'])
                except EnrollmentUser.DoesNotExist:
                    # They're trying to save a non-existing user - maybe they deleted it in
                    # another tab or something - just create a new one
                    user = EnrollmentUser(enrollment=enrollment)
            else:
                user = EnrollmentUser(enrollment=enrollment)

            try:
                dob = datetime.strptime(request.POST['dob'], "%d.%m.%Y").date()
            except ValueError:
                dob = None

            # Titleize and strip whitespace before/after dash
            user.name = re.sub('\s*-\s*', '-', polite_title(request.POST['name'].strip()))
            user.phone = request.POST['phone'].strip()
            user.email = request.POST['email'].lower().strip()
            user.gender = request.POST.get('gender', '')
            user.key = request.POST.get('key') == 'on'
            user.dob = dob
            user.save()

            if user.is_valid():
                enrollment.users.add(user)
                # The user was saved successfully, so clear the form for the next user
                user = None
            else:
                messages.error(request, 'user_invalid')
                errors = True

        # Save partneroffers optin
        enrollment.partneroffers_optin = 'partneroffers_optin' in request.POST
        enrollment.save()

        if not errors and request.POST['button'] == 'continue':
            return redirect("enrollment.views.household")

    context = {
        'enrollment': enrollment,
        'current_user': user,
    }
    context.update(current_template_layout(request))
    return render(request, 'central/enrollment/registration.html', context)