Example #1
0
def user(request, uid):
    if not request.user.profile.flags['dev']:
        raise PermissionDenied()

    user = User.objects.filter(username=uid, profile__test_only=True).first()
    if not user and uid != 'add':
        raise Http404

    if request.method == 'POST':
        first_name = request.POST.get('first_name', 'TEST')
        last_name = request.POST.get('last_name', 'TEST')
        gender = request.POST.get('gender', '*H')
        birthday = request.POST.get('birthday', None)
        if not birthday:
            birthday = None
        point_test = int(request.POST.get('point_test', '0'))

        try:
            kaist_info = json.loads(request.POST.get('kaist_info', ''))
            kaist_id = kaist_info['kaist_uid']
        except:
            kaist_info = {}
            kaist_id = ""

        if not user:
            while True:
                seed = token_hex(4)
                email = 'test-{}@sso.sparcs.org'.format(seed)
                if not User.objects.filter(email=email).count():
                    break

            while True:
                username = '******'.format(token_hex(8))
                if not User.objects.filter(username=username).count():
                    break

            user = User.objects.create_user(username=username,
                                            first_name=first_name,
                                            last_name=last_name,
                                            email=email,
                                            password=seed)
            profile = UserProfile(user=user,
                                  email_authed=True,
                                  test_enabled=True,
                                  test_only=True)
            logger.warn('user.create: uid=%s' % username, {'r': request})
        else:
            profile = user.profile
            logger.info('user.modify: uid=%s' % uid, {'r': request})

        user.first_name = first_name
        user.last_name = last_name
        user.save()

        profile.gender = gender
        profile.birthday = birthday
        profile.point_test = point_test
        profile.set_kaist_info({'userid': kaist_id, 'kaist_info': kaist_info})
        profile.save()

        return redirect('/dev/main/')

    return render(request, 'dev/user.html', {'tuser': user})
Example #2
0
def user(request, uid):
    user = User.objects.filter(username=uid, profile__test_only=True).first()
    if not user and uid != 'add':
        raise Http404

    if request.method == 'POST':
        first_name = request.POST.get('first_name', 'TEST')
        last_name = request.POST.get('last_name', 'TEST')

        if not user:
            while True:
                seed = token_hex(4)
                email = f'test-{seed}@sso.sparcs.org'
                if not User.objects.filter(email=email).count():
                    break

            while True:
                username = f'test{token_hex(10)}'
                if not User.objects.filter(username=username).count():
                    break

            user = User.objects.create_user(username=username,
                                            first_name=first_name,
                                            last_name=last_name,
                                            email=email,
                                            password=seed)
            profile = UserProfile(user=user,
                                  email_authed=True,
                                  test_enabled=True,
                                  test_only=True)
        else:
            profile = user.profile

        user.first_name = first_name
        user.last_name = last_name
        user.save()

        birthday = request.POST.get('birthday', None)
        profile.gender = request.POST.get('gender', '*H')
        profile.birthday = birthday if birthday else None
        profile.point_test = int(request.POST.get('point_test', '0'))
        profile.save()

        try:
            kaist_info = json.loads(request.POST.get('kaist_info', ''))
            profile.save_kaist_info({
                'userid': kaist_info['kaist_uid'],
                'kaist_info': kaist_info,
            })
        except Exception:
            pass

        log_msg = 'create' if uid == 'add' else 'update'
        logger.warning(
            f'account.{log_msg}', {
                'r': request,
                'extra': [
                    ('uid', user.username),
                    ('email', user.email),
                ],
            })
        return redirect('/dev/main/')

    return render(request, 'dev/user.html', {'tuser': user})