예제 #1
0
def get(this, **x):
    # Проверка параметров

    check_params(x, (('id', True, int), ))

    # Нет доступа

    if this.user['admin'] < 3:
        raise ErrorAccess('token')

    #

    db_filter = {
        '_id': False,
    }

    space = db['space'].find_one({'id': x['id']}, db_filter)

    if not space:
        raise ErrorWrong('id')

    # Ответ

    res = {
        'space': space,
    }

    return res
예제 #2
0
def delete(this, **x):
    # Проверка параметров

    check_params(x, (('id', True, int), ))

    #

    task = db['tasks'].find_one({'id': x['id']})

    if this.user['admin'] < 3 or task['token'] != this.user['token']:
        raise ErrorAccess('token')

    db['tasks'].delete_one(task)
예제 #3
0
def start(this, **x):
    # Проверка параметров

    check_params(x, (('id', True, int), ))

    # Не авторизован

    if this.user['admin'] < 3:
        raise ErrorAccess('token')

    #

    db_filter = {
        '_id': False,
        'user': True,
    }
    task = db['tasks'].find_one({'id': x['id']}, db_filter)

    # Неправильное задание

    if not task:
        raise ErrorWrong('task')

    #

    space_id = next_id('space')

    space = {
        'id': space_id,
        'teacher': this.user['token'],
        'student': task['user'],
        'task': x['id'],
        # 'price': price,
        'time': this.timestamp,
        'status': 0,
        'messages': [],
    }

    #

    db['space'].insert_one(space)

    # Запрос ученику

    this.socketio.emit('student_accept', {
        'id': space_id,
        'user': task['user'],
    },
                       namespace='/main')

    # Удалить онлайн задания

    user = db['users'].find_one({'token': task['user']}, {
        '_id': False,
        'tasks': True
    })

    db_condition = {
        'id': {
            '$in': this.user['tasks'] + user['tasks']
        },
    }

    db_filter = {
        '_id': False,
        'id': True,
    }

    tasks = [i for i in db['tasks'].find(db_condition, db_filter) if i]

    this.socketio.emit('tasks_del', tasks, namespace='/main')

    # Ответ

    res = {
        'id': space_id,
    }

    return res
예제 #4
0
def edit(this, **x):
    # Проверка параметров

    # Редактирование
    if 'id' in x:
        check_params(x, (
            ('id', True, int),
            ('image', False, str, True),
            ('text', False, str, True),
            ('tags', False, list, str, True),
        ))

    # Добавление
    else:
        check_params(x, (
            ('image', False, str, True),
            ('file', False, str, True),
            ('text', False, str, True),
            ('tags', False, list, str, True),
        ))

    # Не авторизован

    if this.user['admin'] < 3:
        raise ErrorAccess('add / edit')

    # Редактирование
    if 'id' in x:
        query = db['tasks'].find_one({'id': x['id']})

        # Неправильный id
        if not query:
            raise ErrorWrong('tasks')

        # Чужое задание
        if query['token'] != this.user['token']:
            raise ErrorAccess('token')

        process_edit = True

    # Создание
    else:
        # Нет содержимого

        cond_text = 'text' not in x or not x['text']
        cond_img = 'image' not in x or not x['image']

        if cond_text and cond_img:
            raise ErrorInvalid('image / text')

        #

        query = {
            'id': next_id('tasks'),
            'time': this.timestamp,
            'text': x['text'] if 'text' in x else '',
            'tags': x['tags'] if 'tags' in x else [],
            'user': this.user['token'],
        }

        process_edit = False

    # Загрузка картинки

    if 'image' in x:
        try:
            file_type = x['file'].split('.')[-1]

        # Неправильное расширение
        except:
            raise ErrorInvalid('file')

        try:
            load_image('app/static/tasks', x['image'], query['id'], file_type)

        # Ошибка загрузки изображения
        except:
            raise ErrorUpload('image')

    # Поля

    for par in ('text', 'tags'):
        if par in x:
            query[par] = x[par]

    db['tasks'].save(query)

    # Прикрепление задания к пользователю

    this.user['tasks'].append(query['id'])
    db['users'].save(this.user)

    # Обновление онлайн заданий

    if not process_edit:
        # ???
        if '_id' in query:
            del query['_id']

        query['image'] = get_preview('tasks', query['id'])

        this.socketio.emit('tasks_add', [query], namespace='/main')

    # Ответ

    res = {
        'id': query['id'],
    }

    return res
예제 #5
0
def get(this, **x):
    # Проверка параметров

    check_params(x, (
        ('id', False, (int, list, tuple), int),
        ('my', False, bool),
        ('count', False, int),
    ))

    # Мои задания

    if 'my' not in x:
        x['my'] = False

    if x['my'] and this.user['admin'] < 3:
        raise ErrorAccess('token')

    # Условия

    count = x['count'] if 'count' in x else None

    db_condition = dict()

    if 'id' in x:
        if type(x['id']) == int:
            db_condition['id'] = x['id']

        else:
            db_condition['id'] = {'$in': x['id']}

    else:
        if x['my']:
            db_condition['id'] = {'$in': this.user['tasks']}

    #

    db_filter = {
        '_id': False,
    }

    tasks = [i for i in db['tasks'].find(db_condition, db_filter) if i]

    # Выборка

    ind = 0
    while ind < len(tasks):
        # Только чужие

        if not x['my'] and tasks[ind]['user'] == this.user['token']:
            del tasks[ind]
            continue

        # Только онлайн

        db_filter = {
            '_id': False,
            'online': True,
        }

        user = db['users'].find_one({'token': tasks[ind]['user']}, db_filter)

        if not user['online']:
            del tasks[ind]
            continue

        ind += 1

    # Количество

    tasks = tasks[:count]

    # Изображение

    for i in range(len(tasks)):
        tasks[i]['image'] = get_preview('tasks', tasks[i]['id'])

    # Ответ

    res = {
        'tasks': tasks,
    }

    return res