Ejemplo n.º 1
0
def register_user(requesting_user):
    if not requesting_user.can_register_users:
        return json.dumps({"error":"YOU_CANT_REGISTER_USERS"}), 403

    teacher_username = requesting_user.username
    college_group    = request.form["college"].strip()
    first_name       = request.form["first_name"].strip()
    last_name        = request.form["last_name"].strip()
    email            = request.form["email"].strip()
    team             = request.form["team"].strip()

    if College(college_group) not in requesting_user.colleges:
        return json.dumps({"error":"BAD_COLLEGE"}), 403

    if team not in [t.name for t in College(college_group).teams]:
        return json.dumps({"error":"BAD_TEAM"}), 403

    if not helpers.is_email_valid(email):
        return json.dumps({"error":"BAD_EMAIL"}), 403

    if not helpers.is_name_valid(first_name):
        return json.dumps({"error":"BAD_FIRST_NAME"}), 403

    if not helpers.is_name_valid(last_name):
        return json.dumps({"error":"BAD_LAST_NAME"}), 403

    if User.name_used(first_name, last_name) or helpers.email_used(email):
        return json.dumps({"error":"DETAILS_ALREADY_USED"}), 403

    u = User.create_new_user(requesting_user, college_group, first_name, last_name)
    verify_code = helpers.create_verify_code(u.username, email)

    pu = PendingUser(u.username)
    pu.teacher_username = teacher_username
    pu.college = college_group
    pu.email = email
    pu.team = team
    pu.verify_code = verify_code
    pu.save()

    log_action('registering user', pu)

    url = url_for('activate_account', username=u.username, code=verify_code, _external=True)
    pu.send_welcome_email(first_name, url)

    rqu_email_vars = { 'name': requesting_user.first_name,
            'activation_days': ACTIVATION_DAYS,
              'pu_first_name': first_name,
               'pu_last_name': last_name,
                'pu_username': pu.username,
                 'pu_college': College(pu.college).name,
                   'pu_email': pu.email,
                    'pu_team': pu.team
                      }
    mailer.email_template(requesting_user.email, 'user_requested', rqu_email_vars)

    return "{}", 202
Ejemplo n.º 2
0
    def test_is_email_used_pending_email_change(self):
        email = '*****@*****.**'
        pe = PendingEmail('pu')
        pe.verify_code = 'vc'
        pe.new_email = email
        pe.save()

        used = helpers.email_used(email)
        assert used == True
Ejemplo n.º 3
0
    def test_is_email_used_pending_email_change(self):
        email = '*****@*****.**'
        pe = PendingEmail('pu')
        pe.verify_code = 'vc'
        pe.new_email = email
        pe.save()

        used = helpers.email_used(email)
        assert used == True
Ejemplo n.º 4
0
    def test_is_email_used_pending_user(self):
        email = '*****@*****.**'
        pu = PendingUser('pu')
        pu.college = 'c'
        pu.team = 't'
        pu.teacher_username = '******'
        pu.verify_code = 'vc'
        pu.email = email
        pu.save()

        used = helpers.email_used(email)
        assert used == True
Ejemplo n.º 5
0
    def test_is_email_used_pending_user(self):
        email = '*****@*****.**'
        pu = PendingUser('pu')
        pu.college = 'c'
        pu.team = 't'
        pu.teacher_username = '******'
        pu.verify_code = 'vc'
        pu.email = email
        pu.save()

        used = helpers.email_used(email)
        assert used == True
Ejemplo n.º 6
0
def register_user():
    ah = AuthHelper(request)
    if ah.auth_will_succeed:
        requesting_user = ah.user
        if requesting_user.can_register_users:
            teacher_username = requesting_user.username
            college_group    = request.form["college"].strip()
            first_name       = request.form["first_name"].strip()
            last_name        = request.form["last_name"].strip()
            email            = request.form["email"].strip()
            team             = request.form["team"].strip()

            if College(college_group) not in requesting_user.colleges:
                return json.dumps({"error":"BAD_COLLEGE"}), 403

            if team not in [t.name for t in College(college_group).teams]:
                return json.dumps({"error":"BAD_TEAM"}), 403

            if User.name_used(first_name, last_name) or helpers.email_used(email):
                return json.dumps({"error":"DETAILS_ALREADY_USED"}), 403

            u = User.create_new_user(requesting_user, college_group, first_name, last_name)
            verify_code = helpers.create_verify_code(u.username, email)

            pu = PendingUser(u.username)
            pu.teacher_username = teacher_username
            pu.college = college_group
            pu.email = email
            pu.team = team
            pu.verify_code = verify_code
            pu.save()

            url = url_for('activate_account', username=u.username, code=verify_code, _external=True)
            pu.send_welcome_email(first_name, url)

            rqu_email_vars = { 'name': requesting_user.first_name,
                      'pu_first_name': first_name,
                       'pu_last_name': last_name,
                        'pu_username': pu.username,
                         'pu_college': College(pu.college).name,
                           'pu_email': pu.email,
                            'pu_team': pu.team
                              }
            mailer.email_template(requesting_user.email, 'user_requested', rqu_email_vars)

            return "{}", 202
        else:
            return json.dumps({"error":"YOU_CANT_REGISTER_USERS"}),403
    else:
        return ah.auth_error_json, 403
Ejemplo n.º 7
0
 def test_is_email_used_full_user(self):
     email = '*****@*****.**' # student_coll2_2
     used = helpers.email_used(email)
     assert used == True
Ejemplo n.º 8
0
 def test_is_email_used_no(self):
     email = '*****@*****.**'
     used = helpers.email_used(email)
     assert used == False
Ejemplo n.º 9
0
 def test_is_email_used_full_user(self):
     email = '*****@*****.**'  # student_coll2_2
     used = helpers.email_used(email)
     assert used == True
Ejemplo n.º 10
0
 def test_is_email_used_no(self):
     email = '*****@*****.**'
     used = helpers.email_used(email)
     assert used == False
Ejemplo n.º 11
0
def register_user():
    ah = AuthHelper(request)

    if not ah.auth_will_succeed:
        return ah.auth_error_json, 403

    requesting_user = ah.user
    if not requesting_user.can_register_users:
        return json.dumps({"error": "YOU_CANT_REGISTER_USERS"}), 403

    teacher_username = requesting_user.username
    college_group = request.form["college"].strip()
    first_name = request.form["first_name"].strip()
    last_name = request.form["last_name"].strip()
    email = request.form["email"].strip()
    team = request.form["team"].strip()

    if College(college_group) not in requesting_user.colleges:
        return json.dumps({"error": "BAD_COLLEGE"}), 403

    if team not in [t.name for t in College(college_group).teams]:
        return json.dumps({"error": "BAD_TEAM"}), 403

    if not helpers.is_email_valid(email):
        return json.dumps({"error": "BAD_EMAIL"}), 403

    if not helpers.is_name_valid(first_name):
        return json.dumps({"error": "BAD_FIRST_NAME"}), 403

    if not helpers.is_name_valid(last_name):
        return json.dumps({"error": "BAD_LAST_NAME"}), 403

    if User.name_used(first_name, last_name) or helpers.email_used(email):
        return json.dumps({"error": "DETAILS_ALREADY_USED"}), 403

    u = User.create_new_user(requesting_user, college_group, first_name,
                             last_name)
    verify_code = helpers.create_verify_code(u.username, email)

    pu = PendingUser(u.username)
    pu.teacher_username = teacher_username
    pu.college = college_group
    pu.email = email
    pu.team = team
    pu.verify_code = verify_code
    pu.save()

    log_action('registering user', pu)

    url = url_for('activate_account',
                  username=u.username,
                  code=verify_code,
                  _external=True)
    pu.send_welcome_email(first_name, url)

    rqu_email_vars = {
        'name': requesting_user.first_name,
        'pu_first_name': first_name,
        'pu_last_name': last_name,
        'pu_username': pu.username,
        'pu_college': College(pu.college).name,
        'pu_email': pu.email,
        'pu_team': pu.team
    }
    mailer.email_template(requesting_user.email, 'user_requested',
                          rqu_email_vars)

    return "{}", 202