Exemplo n.º 1
0
def disconnect_deleted_room(sender, object, *args, **kwargs):
    """
    Disconnect all users from the room that was just deleted.

    :param object: the room deleted
    :param args: additional arguments
    :param kwargs: additional keyword arguments
    """
    socketio.emit("delete", object.id, namespace="/rooms", room=object.id)
    socketio.close_room(object.id, namespace="/rooms")
Exemplo n.º 2
0
def signal_deleted_question(sender, object, *args, **kwargs):
    """
    Notify users that a question was destroyed

    :param object: the question deleted
    :param args: additional arguments
    :param kwargs: additional keyword arguments
    """
    socketio.emit("delete",
                  object.id,
                  namespace="/questions",
                  room=object.poll_id)
Exemplo n.º 3
0
def new_question(sender, object, *args, **kwargs):
    """
    Notify users that a new question was added in one of their polls

    :param object: newly created question
    :param args: additional arguments
    :param kwargs: additional keyword arguments
    """
    socketio.emit("item",
                  object.id,
                  namespace="/questions",
                  room=object.poll_id)
Exemplo n.º 4
0
def signal_deleted_poll(sender, object, *args, **kwargs):
    """
    Notify users that the poll was destroyed

    :param object: the poll deleted
    :param args: additional arguments
    :param kwargs: additional keyword arguments
    """
    if object.visible:
        socketio.emit("delete",
                      object.id,
                      namespace="/polls",
                      room=object.room_id)
    socketio.emit("delete",
                  object.id,
                  namespace="/polls",
                  room="{}-admin".format(object.room_id))
Exemplo n.º 5
0
def new_poll(sender, object, *args, **kwargs):
    """
    Notify users that a new poll was created in one of their room.

    :param object: newly created poll
    :param args: additional arguments
    :param kwargs: additional keyword arguments
    """
    if object.visible:
        socketio.emit("item",
                      object.id,
                      namespace="/polls",
                      room=object.room_id)
    socketio.emit("item",
                  object.id,
                  namespace="/polls",
                  room="{}-admin".format(object.room_id))
Exemplo n.º 6
0
def answer_question(_id):
    # FIXME : this could be done in a smarter way, without redoing everything
    question = Question.query \
        .filter(Question.id == _id) \
        .filter(Question.is_open) \
        .one_or_none()

    if question is None or not question.poll.visible or \
            (current_user.is_authenticated and current_user not in question.poll.room.participants) or \
            (current_user.is_anonymous and question.poll.room_id not in session.get("rooms", [])):
        raise NotFoundException()

    try:
        data = request.json
        data = list(map(int, data))
    except Exception:
        raise BadRequestException(invalid_json)

    if question.type == QuestionType.UNIQUE and len(data) != 1:
        raise BadRequestException(invalid_json)

    if current_user.is_authenticated:
        Answer.query.filter(Answer.user_id == current_user.id) \
            .filter(Answer.choice_id.in_(db_session.query(Choice.id).filter(Choice.question_id == question.id))) \
            .delete(synchronize_session="fetch")

        for answer in data:
            db_session.add(Answer(user_id=current_user.id, choice_id=answer))
    else:
        Answer.query.filter(Answer.anonymous_id == session["id"]) \
            .filter(Answer.choice_id.in_(db_session.query(Choice.id).filter(Choice.question_id == question.id))) \
            .delete(synchronize_session="fetch")

        for answer in data:
            db_session.add(Answer(anonymous_id=session["id"],
                                  choice_id=answer))

    db_session.commit()

    # FIXME : using a signal would be cleaner
    socketio.emit("item",
                  question.id,
                  namespace="/questions",
                  room=question.poll_id)

    return jsonify({}), 200
Exemplo n.º 7
0
def updated_poll(sender, object, *args, **kwargs):
    """

    :param sender:
    :param object:
    :param args:
    :param kwargs:
    :return:
    """
    if not object.visible:
        if object._visible:  # the object was visible but is not anymore
            socketio.emit("delete",
                          object.id,
                          namespace="/polls",
                          room=object.room_id)

    else:
        socketio.emit("item",
                      object.id,
                      namespace="/polls",
                      room=object.room_id)

    socketio.emit("item",
                  object.id,
                  namespace="/polls",
                  room="{}-admin".format(object.room_id))
Exemplo n.º 8
0
def updated_question(sender, object, *args, **kwargs):
    # FIXME : we should filter depending on whether the poll is visible or not
    socketio.emit("item",
                  object.id,
                  namespace="/questions",
                  room=object.poll_id)