예제 #1
0
def input_referral_code():
    try:
        payload = get_payload()
    except jwt.exceptions.PyJWTError:
        # TODO: explain the error more obviously
        return format_response(data=None, success=False, message=_('You are not authorized'), status_code=401)

    form = InputReferralCodeForm(context={'user_id': payload['id']})
    data = form.load(request.json or {})
    user_referral_code = form.context.get('user_referral_code')

    transaction = g.db.begin()
    g.db.execute(
        UserHasReferralCodes.insert().values(
            redeemed_referral_code=data['referral_code'],
            redeemed_user_id=user_referral_code,
            redeemer_user_id=payload['id']
        )
    )
    transaction.commit()

    return format_response(
        data=None,
        message=_('Successfully redeemed referal code')
    )
예제 #2
0
def get_user_profile():
    try:
        payload = get_payload()
    except jwt.exceptions.PyJWTError:
        # TODO: explain the error more obviously
        return format_response(data=None,
                               success=False,
                               message=_('You are not authorized'),
                               status_code=401)

    user_id = payload['id']
    connect_db()
    user = g.db.execute(sa.select([User
                                   ]).where(User.c.id == user_id)).fetchone()

    result = {
        'username':
        user.username,
        'name':
        user.name,
        'email':
        user.email,
        'referral_code':
        user.referral_code,
        'registered_at':
        user.created.strftime('%Y-%m-%d %H:%M:%S')
        if user.created is not None else None
    }

    return format_response(data=result)
예제 #3
0
def update_user_profile():
    try:
        payload = get_payload()
    except jwt.exceptions.PyJWTError:
        # TODO: explain the error more obviously
        return format_response(data=None,
                               success=False,
                               message=_('You are not authorized'),
                               status_code=401)

    user_id = payload['id']
    connect_db()
    user = g.db.execute(sa.select([User
                                   ]).where(User.c.id == user_id)).fetchone()

    form = ProfileForm(context={'user': user}, partial=True)
    data = form.load(request.json or {})

    values_to_update = {'modified': sa.func.NOW()}
    for key in data:
        if key in ['username', 'name', 'email']:
            values_to_update.update({key: data[key]})
        elif key == 'password':
            values_to_update.update(
                {'password': generate_password_hash(data[key])})

    transaction = g.db.begin()
    g.db.execute(
        User.update().values(**values_to_update).where(User.c.id == user_id))
    transaction.commit()

    return format_response(data=None)
예제 #4
0
def get_user_list():
    # TODO: pagination
    try:
        get_payload()
    except jwt.exceptions.PyJWTError:
        # TODO: explain the error more obviously
        return format_response(data=None, success=False, message=_('You are not authorized'), status_code=401)
    
    redis = connect_redis()
    q = ''
    if request.method == 'POST':
        try:
            params = request.json or {}
            q = params.get('q', '')
        except:
            # TODO: except more correctly
            q = ''
    redis_key = f'user-list:q-{q}'
    if redis.exists(redis_key):
        result = redis.get(redis_key)
    else:
        connect_db()
        sql = sa.select([User]).order_by(sa.desc(User.c.created))
        if q:
            sql = sql.where(
                sa.or_(
                    User.c.name.like(f'%%{q}%%'),
                    User.c.username.like(f'%%{q}%%')
                )
                
            )
        
        user_list = g.db.execute(sql).fetchall()

        result = []
        for user in user_list:
            result.append({
                'id': user.id,
                'username': user.username,
                'name': user.name,
                'registered_at': user.created.strftime('%Y-%m-%d %H:M:S') if user.created is None else None
            })
        
        redis.set(redis_key, result)
    
    return format_response(data=result)
예제 #5
0
def get_user_detail(username):
    try:
        get_payload()
    except jwt.exceptions.PyJWTError:
        # TODO: explain the error more obviously
        return format_response(data=None,
                               success=False,
                               message=_('You are not authorized'),
                               status_code=401)

    # TODO: redis
    redis = connect_redis()
    redis_key = f'user-detail-{username}'
    if redis.exists(redis_key):
        result = redis.get(redis_key)
    else:
        connect_db()
        user = g.db.execute(
            sa.select([User]).where(User.c.username == username)).fetchone()

        if not user:
            return format_response(data=None,
                                   success=False,
                                   message=_('User not found'),
                                   status_code=404)

        result = {
            'username':
            user.username,
            'name':
            user.name,
            'email':
            user.email,
            # 'referral_code': user.referral_code,
            'registered_at':
            user.created.strftime('%Y-%m-%d %H:%M:%S') if user.created
            is not None else None  # TODO: may other users see this?
        }

        redis.set(redis_key, result)

    return format_response(data=result)