Пример #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 not task:
        raise ErrorWrong('id')

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

    db['tasks'].delete_one(task)
Пример #3
0
def search(discussion_id):
    db_filter = {
        '_id': False,
        'tags': True,
    }

    discussion = db['discussions'].find_one({'id': discussion_id}, db_filter)

    if not discussion:
        raise ErrorWrong('id')

    tags = discussion['tags']

    for tag in tags:
        for post in tg_search(tag):
            try:
                message = get_json(post, tag)
            except:
                continue

            message['discussion'] = discussion_id

            db['messages'].insert_one(message)
Пример #4
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
Пример #5
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
Пример #6
0
def get(this, **x):
    db_condition = {
        'tags': [unquote(x['tag'])],
        'status': 5,
    }
    db_filter = {
        '_id': False,
        'id': True,
        'tags': True,
        'topics': True,
        'timeline': True,
        'result': True,
    }
    heatmap = db['discussions'].find_one(db_condition, db_filter)

    if not heatmap:
        raise ErrorWrong('tag')

    # Обработка

    heatmap['result'] = list(
        map(lambda y: list(map(lambda x: round(x, 4), y)), heatmap['result']))

    # Ответ

    res = {
        'heatmap': heatmap,
    }

    return res


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

# 	check_params(x, (
# 		('tags', True, (str, list), str),
# 	))

# 	if type(x['tags']) == str:
# 		x['tags'] = [x['tags']]

# 	# Сохранение

# 	discussion_id = next_id('discussions')

# 	discussion = {
# 		'id': discussion_id,
# 		'tags': x['tags'],
# 		'time': this.timestamp,
# 		'user': this.token,
# 	}

# 	db['discussions'].insert_one(discussion)

# 	# Обработка

# 	search(discussion_id)

# 	# Ответ

# 	res = {
# 		'id': discussion_id,
# 	}

# 	return res