示例#1
0
async def add(sid, data):
    sid_data = state.sid_map[sid]
    user = sid_data["user"]
    room = sid_data["room"]
    location = sid_data["location"]

    label = Label.get_or_none(uuid=data)

    if label is not None:
        logger.warn(
            f"{user.name} tried to add a label with an id that already exists."
        )
        return

    if data["user"] != user.name:
        logger.warn(f"{user.name} tried to add a label for someone else.")
        return

    data["user"] = User.by_name(data["user"])
    label = Label.create(**data)

    for psid in state.get_sids(skip_sid=sid, room=room):
        if state.get_user(psid) == user or label.visible:
            await sio.emit("Label.Add",
                           label.as_dict(),
                           room=psid,
                           namespace="/planarally")
示例#2
0
def saveLabel():
    #Saving Label#
    print 'saveLabel is called'
    print request.data
    #print request.form['category']
    #print request.form['label']
    myDB.connect()
    record = json.loads(request.data)
    user = Label.create(
        category=record['category'],
        label=record['label']
    )
    myDB.close()
    return 'Label saved'
示例#3
0
文件: view.py 项目: xinyu0601/posts
def label_add():
    """
    Add new Label
    :return: label
    """
    if not request.method == "GET":
        return MethodNotAllowed

    text = request.args.get("text", "", str=type)
    if text is "":
        raise BadRequest('No <text> provided!')
    try:
        label = Label.create(text=text, user=User.get(User.id == 1))
    except Exception as e:
        raise InternalServerError(str(e))

    res = {"success": True, "data": label}
    return res
示例#4
0
async def add(sid: str, data: Dict[str, Any]):
    pr: PlayerRoom = game_state.get(sid)

    label = Label.get_or_none(uuid=data)

    if label is not None:
        logger.warn(
            f"{pr.player.name} tried to add a label with an id that already exists."
        )
        return

    if data["user"] != pr.player.name:
        logger.warn(f"{pr.player.name} tried to add a label for someone else.")
        return

    data["user"] = User.by_name(data["user"])
    label = Label.create(**data)

    for psid in game_state.get_sids(skip_sid=sid, room=pr.room):
        if game_state.get_user(psid) == pr.player or label.visible:
            await sio.emit("Label.Add",
                           label.as_dict(),
                           room=psid,
                           namespace=GAME_NS)
示例#5
0
async def update_shape(sid: int, data: Dict[str, Any]):
    pr: PlayerRoom = game_state.get(sid)

    if data["temporary"] and not has_ownership_temp(data["shape"], pr):
        logger.warning(
            f"User {pr.player.name} tried to update a shape it does not own.")
        return

    # todo clean up this mess that deals with both temporary and non temporary shapes
    shape, layer = await _get_shape(data, pr)

    # Overwrite the old data with the new data
    if not data["temporary"]:
        if not has_ownership(shape, pr):
            logger.warning(
                f"User {pr.player.name} tried to update a shape it does not own."
            )
            return
        with db.atomic():
            # Shape
            update_model_from_dict(shape,
                                   reduce_data_to_model(Shape, data["shape"]))
            shape.save()
            # Subshape
            type_instance = shape.subtype
            # no backrefs on these tables
            type_instance.update_from_dict(data["shape"], ignore_unknown=True)
            type_instance.save()
            # Trackers
            old_trackers = {tracker.uuid for tracker in shape.trackers}
            new_trackers = {
                tracker["uuid"]
                for tracker in data["shape"]["trackers"]
            }
            for tracker_id in old_trackers | new_trackers:
                remove = tracker_id in old_trackers - new_trackers
                if not remove:
                    tracker = next(tr for tr in data["shape"]["trackers"]
                                   if tr["uuid"] == tracker_id)
                    reduced = reduce_data_to_model(Tracker, tracker)
                    reduced["shape"] = shape
                if tracker_id in new_trackers - old_trackers:
                    Tracker.create(**reduced)
                    continue
                tracker_db = Tracker.get(uuid=tracker_id)
                if remove:
                    tracker_db.delete_instance(True)
                else:
                    update_model_from_dict(tracker_db, reduced)
                    tracker_db.save()

            # Auras
            old_auras = {aura.uuid for aura in shape.auras}
            new_auras = {aura["uuid"] for aura in data["shape"]["auras"]}
            for aura_id in old_auras | new_auras:
                remove = aura_id in old_auras - new_auras
                if not remove:
                    aura = next(au for au in data["shape"]["auras"]
                                if au["uuid"] == aura_id)
                    reduced = reduce_data_to_model(Aura, aura)
                    reduced["shape"] = shape
                if aura_id in new_auras - old_auras:
                    Aura.create(**reduced)
                    continue
                aura_db = Aura.get_or_none(uuid=aura_id)
                if remove:
                    aura_db.delete_instance(True)
                else:
                    update_model_from_dict(aura_db, reduced)
                    aura_db.save()
            # Labels
            for label in data["shape"]["labels"]:
                label_db = Label.get_or_none(uuid=label["uuid"])
                reduced = reduce_data_to_model(Label, label)
                reduced["user"] = User.by_name(reduced["user"])
                if label_db:
                    update_model_from_dict(label_db, reduced)
                    label_db.save()
                else:
                    Label.create(**reduced)
            old_labels = {
                shape_label.label.uuid
                for shape_label in shape.labels
            }
            new_labels = set(label["uuid"]
                             for label in data["shape"]["labels"])
            for label in old_labels ^ new_labels:
                if label == "":
                    continue
                if label in new_labels:
                    ShapeLabel.create(shape=shape, label=Label.get(uuid=label))
                else:
                    ShapeLabel.get(label=Label.get(uuid=label),
                                   shape=shape).delete_instance(True)

    await sync_shape_update(layer, pr, data, sid, shape)
示例#6
0
async def update_shape(sid, data):
    sid_data = state.sid_map[sid]
    user = sid_data["user"]
    room = sid_data["room"]
    location = sid_data["location"]

    shape, layer = await _get_shape(data, location, user)

    if not await has_ownership(layer, room, data, user, shape):
        return

    # Overwrite the old data with the new data
    if not data["temporary"]:
        with db.atomic():
            data["shape"]["layer"] = Layer.get(location=location,
                                               name=data["shape"]["layer"])
            # Shape
            update_model_from_dict(shape,
                                   reduce_data_to_model(Shape, data["shape"]))
            shape.save()
            # Subshape
            type_instance = shape.subtype
            # no backrefs on these tables
            type_instance.update_from_dict(data["shape"], ignore_unknown=True)
            type_instance.save()
            # Owners
            old_owners = {owner.user.name for owner in shape.owners}
            new_owners = set(data["shape"]["owners"])
            for owner in old_owners ^ new_owners:
                if owner == "":
                    continue
                delta_owner = User.by_name(owner)
                if owner in new_owners:
                    ShapeOwner.create(shape=shape, user=delta_owner)
                else:
                    ShapeOwner.get(shape=shape,
                                   user=delta_owner).delete_instance(True)
                await send_client_initiatives(room, location, delta_owner)
            # Trackers
            old_trackers = {tracker.uuid for tracker in shape.trackers}
            new_trackers = {
                tracker["uuid"]
                for tracker in data["shape"]["trackers"]
            }
            for tracker_id in old_trackers | new_trackers:
                remove = tracker_id in old_trackers - new_trackers
                if not remove:
                    tracker = next(tr for tr in data["shape"]["trackers"]
                                   if tr["uuid"] == tracker_id)
                    reduced = reduce_data_to_model(Tracker, tracker)
                    reduced["shape"] = shape
                if tracker_id in new_trackers - old_trackers:
                    Tracker.create(**reduced)
                    continue
                tracker_db = Tracker.get(uuid=tracker_id)
                if remove:
                    tracker_db.delete_instance(True)
                else:
                    update_model_from_dict(tracker_db, reduced)
                    tracker_db.save()

            # Auras
            old_auras = {aura.uuid for aura in shape.auras}
            new_auras = {aura["uuid"] for aura in data["shape"]["auras"]}
            for aura_id in old_auras | new_auras:
                remove = aura_id in old_auras - new_auras
                if not remove:
                    aura = next(au for au in data["shape"]["auras"]
                                if au["uuid"] == aura_id)
                    reduced = reduce_data_to_model(Aura, aura)
                    reduced["shape"] = shape
                if aura_id in new_auras - old_auras:
                    Aura.create(**reduced)
                    continue
                aura_db = Aura.get_or_none(uuid=aura_id)
                if remove:
                    aura_db.delete_instance(True)
                else:
                    update_model_from_dict(aura_db, reduced)
                    aura_db.save()
            # Labels
            for label in data["shape"]["labels"]:
                label_db = Label.get_or_none(uuid=label["uuid"])
                reduced = reduce_data_to_model(Label, label)
                reduced["user"] = User.by_name(reduced["user"])
                if label_db:
                    update_model_from_dict(label_db, reduced)
                    label_db.save()
                else:
                    Label.create(**reduced)
                shape_label_db = ShapeLabel.get_or_none(shape=shape,
                                                        label=label_db)
            old_labels = {
                shape_label.label.uuid
                for shape_label in shape.labels
            }
            new_labels = set(label["uuid"]
                             for label in data["shape"]["labels"])
            for label in old_labels ^ new_labels:
                if label == "":
                    continue
                if label in new_labels:
                    ShapeLabel.create(shape=shape, label=Label.get(uuid=label))
                else:
                    ShapeLabel.get(label=Label.get(uuid=label),
                                   shape=shape).delete_instance(True)

    await sync_shape_update(layer, room, data, sid, shape)