Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
create board units, create discussions, list discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
cbroms committed Feb 11, 2021
1 parent f134f03 commit 96f9e95
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 157 deletions.
2 changes: 2 additions & 0 deletions backend/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
make_error,
DictEncoder,
)

from managers.global_manager import GlobalManager
import schema.board_requests as breq
import schema.board_responses as bres
import schema.discussion_requests as dreq
import schema.discussion_responses as dres


gm = GlobalManager()
sio = gm.sio

Expand Down
8 changes: 8 additions & 0 deletions backend/src/make_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
gm = GlobalManager()
gm.start()

gm.boards.remove({})
gm.discussions.remove({})
gm.units.remove({})
gm.links.remove({})
gm.transclusions.remove({})
gm.unit_updates.remove({})
gm.users.remove({})

board = gm.board_manager.create()
board_id = board["board_id"]

Expand Down
9 changes: 5 additions & 4 deletions backend/src/managers/board_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def load_board(self, board_id, user_id):
)
user = self.gm.users.find_one({"_id": user_id, "board_id": board_id})

unit_ids = self.gm.boards.find_one({"_id": board_id})["units"]
units_output = [self.gm._get_basic_units(board_id, unit_id) \
for unit_id in unit_ids]
units = self.gm.units.find({}, {"board_id": board_id})
units_output = [self.gm._get_basic_unit(board_id, unit["_id"]) \
for unit in units]

return {"nickname": user["nickname"], "units": units_output}

Expand Down Expand Up @@ -137,6 +137,7 @@ def get_unit(self, board_id, unit_id):
@Checker._check_unit_id
def create_disc(self, board_id, unit_id):
discussion = Discussion(board_id=board_id, focused=[unit_id])
utils.logger.info("create_disc: {}".format(discussion.to_mongo()))
self.gm.discussions.insert_one(discussion.to_mongo())
self._record_unit_update(board_id, unit_id)
return {"discussion_id": discussion["_id"]}
return {"discussion": self.gm._get_disc(board_id, discussion.id)}
20 changes: 18 additions & 2 deletions backend/src/managers/global_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socketio

import constants
from utils import utils

from models.unit import Unit
from models.transclusion import Transclusion
Expand Down Expand Up @@ -89,12 +90,26 @@ def _get_links_from(self, board_id, unit_id):
} for l in links]
return link_list

def _get_disc(self, board_id, discussion_id):
discussion = self.discussions.find_one({"_id": discussion_id, "board_id": board_id})
return {
"id": discussion_id,
"created": str(discussion["created"])
}

def _get_discussions(self, board_id, unit_id):
# focused includes unit
discussions = self.discussions.find(
{}, {"focused": unit_id, "board_id": board_id}
)
discussions_list = [d["_id"] for d in discussions]
try:
utils.logger.info("D: {}".format([d for d in discussions]))
discussions_list = [{ \
"id": d["_id"], \
"created": str(d["created"]) \
} for d in discussions]
except Exception as e:
utils.logger.info(e)
return discussions_list

def _get_pith(self, board_id, text):
Expand Down Expand Up @@ -152,9 +167,10 @@ def _get_chat_unit(self, board_id, unit_id):
}

def _get_basic_unit(self, board_id, unit_id):
unit = self.units.find_one({"_id": unit_id, "board_id": board_id})
return {
"id": unit_id,
"pith": self.units.find_one({"_id": unit_id, "board_id": board_id})["pith"],
"pith": unit["pith"],
"transclusions": self._get_transclusion_map(board_id, unit_id)
}

Expand Down
11 changes: 2 additions & 9 deletions backend/src/models/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@ class Board(Document):

meta = {'collection': 'boards'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
:default: Automatically generated.
"""

units = ListField(StringField(), default=[]) # unit ids
"""
:type: *List[str]*
:required: False
:default: []
"""
4 changes: 2 additions & 2 deletions backend/src/models/discussion.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Discussion(Document):

meta = {'collection': 'discussions'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class Link(Document):

meta = {'collection': 'links'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/transclusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class Transclusion(Document):

meta = {'collection': 'transclusions'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Unit(Document):

meta = {'collection': 'units'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/unit_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class UnitUpdate(Document):

meta = {'collection': 'unit_updates'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)
"""
:type: *str*
:required: False
:default: Automatically generated.
"""

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class User(Document):

meta = {'collection': 'users'}

id = StringField(default=utils.gen_key(), primary_key=True)
id = StringField(default=lambda: utils.gen_key(), primary_key=True)

created = DateTimeField(default=utils.get_time())
created = DateTimeField(default=lambda: utils.get_time())
"""
:type: *datetime*
:required: False
Expand Down
4 changes: 2 additions & 2 deletions backend/src/schema/board/responses/create_disc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "object",
"properties": {
"discussion_id": {"type": "string"}
"discussion": {"$ref": "file:disc.json#/disc"}
},
"required": ["discussion_id"]
"required": ["discussion"]
}
2 changes: 1 addition & 1 deletion backend/src/schema/board_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

for schema_name in schema_names:
with open(path + "/board/requests/{}.json".format(schema_name)) as file:
schema[schema_name] = absolute_file(load(file))
schema[schema_name] = utils.absolute_file(load(file), path)
4 changes: 1 addition & 3 deletions backend/src/schema/board_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from utils import utils


path = os.path.dirname(os.path.realpath(__file__))
schema = {}
schema_names = [
Expand All @@ -20,7 +19,6 @@
"create_disc",
]


for schema_name in schema_names:
with open(path + "/board/responses/{}.json".format(schema_name)) as file:
schema[schema_name] = absolute_file(load(file))
schema[schema_name] = utils.absolute_file(load(file), path)
10 changes: 10 additions & 0 deletions backend/src/schema/disc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"disc": {
"type": "object",
"properties": {
"id": {"type": "string"},
"created": {"type": "string"}
},
"required": ["id", "created"]
}
}
2 changes: 1 addition & 1 deletion backend/src/schema/discussion_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

for schema_name in schema_names:
with open(path + "/discussion/requests/{}.json".format(schema_name)) as file:
schema[schema_name] = absolute_file(load(file))
schema[schema_name] = utils.absolute_file(load(file), path)
2 changes: 1 addition & 1 deletion backend/src/schema/discussion_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

for schema_name in schema_names:
with open(path + "/discussion/responses/{}.json".format(schema_name)) as file:
schema[schema_name] = absolute_file(load(file))
schema[schema_name] = utils.absolute_file(load(file), path)
2 changes: 1 addition & 1 deletion backend/src/schema/unit.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"discussions": {
"type": "array",
"items": {"type": "string"}
"items": {"$ref": "file:disc.json#/disc"}
}
},
"required": ["id", "pith", "transclusions"]
Expand Down
20 changes: 12 additions & 8 deletions backend/src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from nltk.stem import PorterStemmer
from operator import or_
import string
import secrets
from typing import (
Any,
Dict,
Expand All @@ -18,13 +19,15 @@
from uuid import uuid4


def absolute_file(schema):
for key in schema:
if key == "$ref":
schema[key] = schema[key].replace("file:", "file:" + path + "/")
elif isinstance(schema[key], dict):
schema[key] = absolute(schema[key])
return schema
def absolute_file(schema, path):
def helper(S):
for key in S:
if key == "$ref":
S[key] = S[key].replace("file:", "file:" + path + "/")
elif isinstance(S[key], dict):
S[key] = helper(S[key])
return S
return helper(schema)

def get_room(board_id, discussion_id):
return "{}:{}".format(board_id, discussion_id)
Expand All @@ -33,7 +36,8 @@ def get_time():
return datetime.datetime.utcnow()

def gen_key():
return uuid4().hex[-12:] # hack
return ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(12))
#return uuid4().hex

class DictEncoder(JSONEncoder):
def default(self, obj):
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/sections/Board.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script>
import { boardStore } from "../../stores/boardStore";
import BoardUnit from "../unit/BoardUnit.svelte";
export let id;
let content = "";
Expand All @@ -17,6 +20,9 @@
</script>

<div>
{#each $boardStore.units as unit (unit.id)}
<BoardUnit {unit} focus edit links />
{/each}
<input
placeholder="type a unit..."
bind:value={content}
Expand Down
Loading

0 comments on commit 96f9e95

Please sign in to comment.