Exemplo n.º 1
0
def migrate_from_amazon(request):
    """
    Helps student migrate from account using Amazon Login
    :param request: any request
    :return: a form to migrate account (similar to registration)
        or a redirect to CERN dashboard
    """
    form = StudentMigrateForm()

    if request.method == 'POST':
        form = StudentMigrateForm(request.POST)

        if form.is_valid():
            email = (form.cleaned_data.get('email_address') or "").strip().lower()
            password = form.cleaned_data.get('password')

            try:
                stu = form.student
            except AttributeError:
                user = User.objects.get(username=email)
            else:
                user = stu.user
                user.username = email

            user.password = password
            user.save()
            messages.success(request, "<h4><i class='fa fa-check'></i> Your password has been updated.</h4>")

            for s in Student.objects.filter(user=user):
                s.migrated = True

            # Can't use a get_or_create bc don't know required field "username"
            try:
                fan = FanPage.objects.get(fan=user)
            except FanPage.DoesNotExist:
                stu = Student.objects.filter(user=user)[0]
                username = "******" % (user_name(user), "Head " if stu.isHead else "", stu.school.name)
                fan = FanPage(fan=user, username=username)
                fan.save()

                # create fan role
                role_controller = RoleController(user)
                fan_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_FAN,
                    fan.id,
                    RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)
                )

            change_current_role(request, RoleController.ENTITY_FAN, fan.id)

            user.backend = "django.contrib.auth.backends.ModelBackend"
            login(request, user)

            return HttpResponseRedirect('/challenges/students')

    return render(request, 'spuddercern/pages/migrate_from_amazon.html', {'form': form})
Exemplo n.º 2
0
def register_school(request, school_id, referral_id=None):
    """
    Renders the signup page for a new student at a certain school

    :param request: request to render page
    :param school_id: the ID of the school which the student will
        register with
    :param referral_id: an optional param which indicates a referral by
        another student
    :return: a simple register form (email + password)
    """
    try:
        school = School.objects.get(id=school_id)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/cern/register/')
    else:
        referrer = None
        if referral_id:
            try:
                referrer = Student.objects.get(id=referral_id)
            except ObjectDoesNotExist:
                referral_id = None

        form = StudentRegistrationForm()
        if request.method == 'POST':
            form = StudentRegistrationForm(request.POST)
            if form.is_valid():
                # Create and login user
                email = form.cleaned_data.get('email_address')
                password = form.cleaned_data.get('password')
                user = User(username=email, email=email, password=password)
                user.save()

                # Create student
                stu = Student(school=school, referred_id=referral_id, user=user)
                if school.num_students() == 0:
                    stu.isHead = True
                stu.save()

                signed_up(stu)
                if referral_id:
                    referrer = Student.objects.get(id=referral_id)
                    recruited_new_student(referrer, school)

                # Create fan for user
                username = "******" % (user_name(user), "Head " if stu.isHead else "", stu.school.name)
                fan = FanPage(fan=user, username=username)
                fan.save()

                user.backend = "django.contrib.auth.backends.ModelBackend"
                login(request, user)

                role_controller = RoleController(user)
                # create the student role
                student_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_STUDENT,
                    stu.id,
                    RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT)
                )
                # create fan role
                fan_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_FAN,
                    fan.id,
                    RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)
                )

                # activate highest priority role (fan)
                change_current_role(request)

                return HttpResponseRedirect('/challenges/students')
        return render(request, 'spuddercern/pages/register_login.html', {
            'school': school,
            'referrer': referrer,
            'form': form,
            'base_url': settings.SPUDMART_BASE_URL
        })
Exemplo n.º 3
0
def register_school(request, school_id, referral_id=None):
    """
    Renders the signup page for a new student at a certain school

    :param request: request to render page
    :param school_id: the ID of the school which the student will
        register with
    :param referral_id: an optional param which indicates a referral by
        another student
    :return: a simple register form (email + password)
    """
    try:
        school = School.objects.get(id=school_id)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/cern/register/')
    else:
        referrer = None
        if referral_id:
            try:
                referrer = Student.objects.get(id=referral_id)
            except ObjectDoesNotExist:
                referral_id = None

        form = StudentRegistrationForm()
        if request.method == 'POST':
            form = StudentRegistrationForm(request.POST)
            if form.is_valid():
                # Create and login user
                email = form.cleaned_data.get('email_address')
                password = form.cleaned_data.get('password')
                user = User(username=email, email=email, password=password)
                user.save()

                # Create student
                stu = Student(school=school,
                              referred_id=referral_id,
                              user=user)
                if school.num_students() == 0:
                    stu.isHead = True
                stu.save()

                signed_up(stu)
                if referral_id:
                    referrer = Student.objects.get(id=referral_id)
                    recruited_new_student(referrer, school)

                # Create fan for user
                username = "******" % (user_name(
                    user), "Head " if stu.isHead else "", stu.school.name)
                fan = FanPage(fan=user, username=username)
                fan.save()

                user.backend = "django.contrib.auth.backends.ModelBackend"
                login(request, user)

                role_controller = RoleController(user)
                # create the student role
                student_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_STUDENT, stu.id,
                    RoleBase.RoleWrapperByEntityType(
                        RoleController.ENTITY_STUDENT))
                # create fan role
                fan_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_FAN, fan.id,
                    RoleBase.RoleWrapperByEntityType(
                        RoleController.ENTITY_FAN))

                # activate highest priority role (fan)
                change_current_role(request)

                return HttpResponseRedirect('/challenges/students')
        return render(
            request, 'spuddercern/pages/register_login.html', {
                'school': school,
                'referrer': referrer,
                'form': form,
                'base_url': settings.SPUDMART_BASE_URL
            })
Exemplo n.º 4
0
def migrate_from_amazon(request):
    """
    Helps student migrate from account using Amazon Login
    :param request: any request
    :return: a form to migrate account (similar to registration)
        or a redirect to CERN dashboard
    """
    form = StudentMigrateForm()

    if request.method == 'POST':
        form = StudentMigrateForm(request.POST)

        if form.is_valid():
            email = (form.cleaned_data.get('email_address')
                     or "").strip().lower()
            password = form.cleaned_data.get('password')

            try:
                stu = form.student
            except AttributeError:
                user = User.objects.get(username=email)
            else:
                user = stu.user
                user.username = email

            user.password = password
            user.save()
            messages.success(
                request,
                "<h4><i class='fa fa-check'></i> Your password has been updated.</h4>"
            )

            for s in Student.objects.filter(user=user):
                s.migrated = True

            # Can't use a get_or_create bc don't know required field "username"
            try:
                fan = FanPage.objects.get(fan=user)
            except FanPage.DoesNotExist:
                stu = Student.objects.filter(user=user)[0]
                username = "******" % (user_name(
                    user), "Head " if stu.isHead else "", stu.school.name)
                fan = FanPage(fan=user, username=username)
                fan.save()

                # create fan role
                role_controller = RoleController(user)
                fan_role = role_controller.role_by_entity_type_and_entity_id(
                    RoleController.ENTITY_FAN, fan.id,
                    RoleBase.RoleWrapperByEntityType(
                        RoleController.ENTITY_FAN))

            change_current_role(request, RoleController.ENTITY_FAN, fan.id)

            user.backend = "django.contrib.auth.backends.ModelBackend"
            login(request, user)

            return HttpResponseRedirect('/challenges/students')

    return render(request, 'spuddercern/pages/migrate_from_amazon.html',
                  {'form': form})