Skip to content

Commit

Permalink
cleanup/refactor/format
Browse files Browse the repository at this point in the history
  • Loading branch information
Avantol13 committed Jun 15, 2019
1 parent 13ad657 commit 1cbb19e
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 501 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

## Milestone 1: Grid and Simple Blocks

* Draggable grid and movable entities that snap to grid
* Entities expose attributes used for drawing on canvas
* Can CRUD entity attributes
* Rudimentary dialog box with editable fields
* Can Create/Delete entire entities
* Simple navbar
* Any entity update (movement/attribute change) reflected on all clients
* using websockets
- [x] Draggable grid and movable entities that snap to grid
- [x] Entities expose attributes used for drawing on canvas (color, location, etc.)
- [x] Can CRUD entity attributes (dialog box with editable fields)
- [x] Can Create/Delete entire entities
- [x] Simple navbar
- [x] Any entity update (movement/attribute change) reflected on all clients using websockets

## Milestone 2: Map Mode

Expand Down
28 changes: 20 additions & 8 deletions main.py → app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Flask, render_template, jsonify
from flask_socketio import SocketIO, emit
from mongoengine import *
from mongo import Entity
from mongo import Entity, DoesNotExist
from config import config
import json

Expand All @@ -12,13 +12,16 @@
app.config["SECRET_KEY"] = config.SECRET_KEY
socketio = SocketIO(app)


@app.route("/")
def index():
return render_template("index.html")

@app.route("/api/entities", methods=['GET'])

@app.route("/api/entities", methods=["GET"])
def get_entities():
return jsonify({ "entities": [entity.to_dict() for entity in Entity.objects.all()] })
return jsonify({"entities": [entity.to_dict() for entity in Entity.objects.all()]})


@socketio.on("delete entity", namespace="/test")
def delete_entity(entity_id):
Expand All @@ -42,23 +45,32 @@ def update_entity(data):

# get current fields to diff against fields provided
# any missing fields will be removed by using mongo's "unset" option
current = Entity.objects.get(entity_id=entity_id).to_json()
current.pop("id")
to_remove = {"unset__{}".format(key):1 for key in set(current.keys()) - set(new.keys())}
new.update(to_remove)
try:
current = Entity.objects.get(entity_id=entity_id).to_json()
current.pop("id")
to_remove = {
"unset__{}".format(key): 1 for key in set(current.keys()) - set(new.keys())
}
new.update(to_remove)
except DoesNotExist:
# no existing record, so create everything
pass

Entity.objects(entity_id=entity_id).update(**new, upsert=True)
entity = Entity.objects.get(entity_id=entity_id)

emit("updated entity", entity.to_json(), broadcast=True)


@socketio.on("connect", namespace="/test")
def test_connect():
emit("connected", {"data": "Connected"})


@socketio.on("disconnect", namespace="/test")
def test_disconnect():
print("Client disconnected")


if __name__ == "__main__":
socketio.run(app)
socketio.run(app)
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

# from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -8,4 +9,5 @@
class Config(object):
SECRET_KEY = os.environ.get("SECRET_KEY") or "you-will-never-guess"

config = Config()

config = Config()
17 changes: 3 additions & 14 deletions mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@

# Defining a Document
class Entity(DynamicDocument):
entity_id = StringField(
required=True, unique=True,
default=str(uuid.uuid4())
)
entity_id = StringField(required=True, unique=True, default=str(uuid.uuid4()))

def to_dict(self):
output = {
key:value
for key, value in
self.to_mongo().to_dict().items()
}
output = {key: value for key, value in self.to_mongo().to_dict().items()}
# return an "id" field from the entity_id field
del output["_id"]
output["id"] = output["entity_id"]
Expand All @@ -24,11 +17,7 @@ def to_dict(self):
return output

def to_json(self):
output = {
key:value
for key, value in
json.loads(super().to_json()).items()
}
output = {key: value for key, value in json.loads(super().to_json()).items()}
# return an "id" field from the entity_id field
del output["_id"]
output["id"] = output["entity_id"]
Expand Down
Loading

0 comments on commit 1cbb19e

Please sign in to comment.