Example #1
0
def authenticated_user(token: str = Depends(oauth2_schema)) -> UserSchema:
    with session_scope() as session:
        user = session.query(User).filter(User.token == token).first()
        if user is None:
            raise HTTPException(status.HTTP_401_UNAUTHORIZED, 'Invalid token.')

        return UserSchema.from_orm(user)
Example #2
0
async def login(credentials: OAuth2PasswordRequestForm = Depends()):
    with session_scope() as session:
        user = session.query(User).filter(User.username == credentials.username).first()
        if user is None or not pbkdf2_sha256.verify(credentials.password, user.password_hash):
            raise HTTPException(status.HTTP_401_UNAUTHORIZED, 'Invalid credentials')

        if user.token is None:
            user.token = secrets.token_hex(32)

        return AuthResponse(access_token=user.token, token_type='bearer')
Example #3
0
async def register(user: UserRegistration):
    with session_scope() as session:
        exists_user = session.query(User).filter(User.username == user.username).first()

        if exists_user is not None:
            raise HTTPException(status.HTTP_400_BAD_REQUEST, 'Пользователь уже существует')

        new_user = User(
            username=user.username,
            password_hash=pbkdf2_sha256.hash(user.password),
            token=secrets.token_hex(32),
            normal_threshold=100,
            warning_threshold=120,
        )
        session.add(new_user)

        return AuthResponse(access_token=new_user.token, token_type='bearer')
Example #4
0
async def add_measurement(data: MeasurementSchema):
    with session_scope() as session:
        time = data.timings[-1]
        wps = len(data.said_words) / time
        said_symbols = 0
        for word in data.said_words:
            said_symbols = len(word)
        sps = said_symbols / time
        session.add(
            Measurement(
                user_id=data.user_id,
                accuracy=data.accuracy,
                words=data.words,
                said_words=data.said_words,
                timings=data.timings,
                sps=sps,
                wps=wps,
            )
        )
Example #5
0
async def get_self_info(user: UserSchema = authenticated_user):
    with session_scope() as session:
        user = session.query(User).options(joinedload(User.measurements)).get(user.id)
        session.expunge(user)

        # Разбиение по дням
        user.daily_stats = form_daily_stats(user.measurements)

        # Плавающее среднее
        measurement_sum = 0
        user.sl_mean = []
        for i, measurement in enumerate(user.measurements):
            measurement_sum += measurement.value
            user.sl_mean += [int(measurement_sum / (i + 1))]

        # Экстраполяция
        user.extra = None
        if len(user.measurements) > 4:
            x = [i.time for i in user.measurements]
            y = [i.value for i in user.measurements]
            user.extra = extrapolation(x, y)

        return user
Example #6
0
async def list_users():
    with session_scope() as session:
        users = session.query(User).options(joinedload(User.measurements)).all()
        session.expunge_all()

        return {'objects': users}
Example #7
0
async def add_measurement(value: int, user: UserSchema = authenticated_user):
    with session_scope() as session:
        user = session.query(User).get(user.id)
        user.measurements.append(Measurement(time=int(round(time.time() * 1000)), value=value))
Example #8
0
from app.models.db import session_scope, engine, Base, User, Measurement
import time
import random
import os

if __name__ == '__main__':
    if os.path.isfile('db.db'):
        os.remove('db.db')

    Base.metadata.create_all(engine)

    start = (1605571200 - 3 * 3600) * 1000

    with session_scope() as session:
        measurements = [
            Measurement(time=start + 10 * 3600 * 1000, value=95),
            Measurement(time=start + 18 * 3600 * 1000, value=105),
            Measurement(time=start + 1 * 24 * 3600 * 1000 + 10 * 3600 * 1000,
                        value=103),
            Measurement(time=start + 2 * 24 * 3600 * 1000 + 20 * 3600 * 1000,
                        value=115),
            Measurement(time=start + 4 * 24 * 3600 * 1000 + 5 * 3600 * 1000,
                        value=110),
            Measurement(time=start + 4 * 24 * 3600 * 1000 + 19 * 3600 * 1000,
                        value=93),
        ]

        user = User(
            username='******',
            password_hash=
            '$pbkdf2-sha256$29000$s1aq1fo/x1jL.b93rpXSWg$eUFDmZJzw3sWLtj0rwuI90vBZ1hTW9ElEL9i.qBXfgc',