diff --git a/kyokusui/controller.py b/kyokusui/controller.py index 71bca4c..59d14d0 100644 --- a/kyokusui/controller.py +++ b/kyokusui/controller.py @@ -1,16 +1,22 @@ from mitama.app import Controller from mitama.app.http import Response from mitama.models import User, Node, Role, is_admin -from datetime import datetime import json import re import markdown import magic from .model import db, Board, Thread, Res, Permission -from .forms import CreateBoardForm, CreateThreadForm, SettingForm, UpdateThreadForm, UpdateBoardForm +from .forms import ( + CreateBoardForm, + CreateThreadForm, + SettingForm, + UpdateThreadForm, + UpdateBoardForm +) from .utils import hiroyuki + class HomeController(Controller): def handle(self, request): template = self.view.get_template("home.html") @@ -24,14 +30,17 @@ def handle(self, request): "threads": threads, "boards": Board.list_subscribed(request.user) }) + def settings(self, request): template = self.view.get_template("settings.html") error = "" if request.method == "POST": form = SettingForm(request.post()) - for permission_screen_name, permission_roles in form['permissions'].items(): - permission = Permission.retrieve(screen_name = permission_screen_name) - permission.roles = [Role.retrieve(screen_name = r) for r in permission_roles] + for screen_name, roles in form['permissions'].items(): + permission = Permission.retrieve(screen_name=screen_name) + permission.roles = [ + Role.retrieve(screen_name=r) for r in roles + ] permission.update() error = "保存しました" return Response.render(template, { @@ -40,6 +49,7 @@ def settings(self, request): "error": error }) + class BoardController(Controller): def create(self, request): form = CreateBoardForm(request.post()) @@ -91,6 +101,7 @@ def unsubscribe(self, request): } }) + class UserController(Controller): def icon(self, request): user = User.retrieve(request.params['user']) @@ -98,6 +109,7 @@ def icon(self, request): mime = f.from_buffer(self.icon) return Response(user._icon, content_type=mime) + class ThreadController(Controller): def create(self, request): form = CreateThreadForm(request.post()) @@ -109,9 +121,12 @@ def create(self, request): thread.create() for user in board.subscribers: user.push({ - "title": res.thread.title + " - Kyokusui", + "title": thread.title + " - Kyokusui", "body": "スレッドが立ちました", - "icon": self.app.convert_fullurl(request, "/user/{}/icon".format(requst.user._id)) + "icon": self.app.convert_fullurl( + request, + "/user/{}/icon".format(request.user._id) + ) }) return Response.json({ "_id": thread._id, @@ -137,8 +152,10 @@ def update(self, request): "title": thread.title, }) + class WebSocketController(Controller): streams = {} + def handle(self, request): ws = request.websocket board = Board.retrieve(request.params['board']) @@ -162,14 +179,22 @@ def handle(self, request): res.user = request.user res.thread = Thread.retrieve(request.params['thread']) res.create() - mentions = re.findall(r"\>\>([0-9a-zA-Z.-_]+)", data['data']['message']) + mentions = re.findall( + r"\>\>([0-9a-zA-Z.-_]+)", + data['data']['message'] + ) for mention in mentions: try: - user = User.retrieve(screen_name = mention) + user = User.retrieve( + screen_name=mention + ) user.push({ "title": res.thread.title + " - Kyokusui", "body": res.user.name+"さんがメンションしました", - "icon": self.app.convert_fullurl(request, "/user/{}/icon".format(res.user._id)) + "icon": self.app.convert_fullurl( + request, + "/user/{}/icon".format(res.user._id) + ) }) except Exception as err: print(err, mention) @@ -181,7 +206,10 @@ def handle(self, request): "type": "message", "_id": res._id, "data": { - "message": markdown.markdown(hiroyuki(res.parsed_data["message"]), extensions=['fenced_code']), + "message": markdown.markdown( + hiroyuki(res.parsed_data["message"]), + extensions=['fenced_code'] + ), "images": res.parsed_data["images"] }, "user": { @@ -189,7 +217,9 @@ def handle(self, request): "name": res.user.name, "screen_name": res.user.screen_name }, - "datetime": res.datetime.strftime("%Y-%m-%d %H:%M:%S"), + "datetime": res.datetime.strftime( + "%Y-%m-%d %H:%M:%S" + ), "thread": thread._id, })) except Exception as err: diff --git a/kyokusui/model.py b/kyokusui/model.py index 557d6d0..0a2181c 100644 --- a/kyokusui/model.py +++ b/kyokusui/model.py @@ -1,7 +1,7 @@ from mitama.db import BaseDatabase, Table, relationship from mitama.db.model import UUID -from mitama.db.types import * -from mitama.models import User, Group, Node, permission +from mitama.db.types import Column, String, ForeignKey, DateTime, Boolean, Text +from mitama.models import User, Node, permission from datetime import datetime import json @@ -12,6 +12,7 @@ class Database(BaseDatabase): db = Database(prefix="kyokusui") + class Board(db.Model): name = Column(String(255)) owner_id = Column(String(64), ForeignKey("mitama_node._id")) @@ -24,10 +25,15 @@ def list_subscribed(cls, user): boards_ = cls.list() boards = list() for board in boards_: - if board.owner.object == user or (isinstance(board.owner.object, Group) and board.owner.object.is_in(user)) or user in board.subscribers: + if ( + board.owner.object == user or + board.owner.is_in(user) or + user in board.subscribers + ): boards.append(board) return boards + class Thread(db.Model): board_id = Column(String(64), ForeignKey("kyokusui_board._id")) board = relationship(Board, backref="threads") @@ -41,6 +47,7 @@ def close(self): self.closed = True self.update() + class Res(db.Model): thread_id = Column(String(64), ForeignKey("kyokusui_thread._id")) thread = relationship(Thread, backref="res") @@ -53,13 +60,24 @@ class Res(db.Model): def parsed_data(self): return json.loads(self.data) -subscribe_table = Table("kyokusui_subscribe", + +subscribe_table = Table( + "kyokusui_subscribe", db.metadata, Column("_id", String(64), default=UUID(), primary_key=True), - Column("user_id", String(64), ForeignKey("mitama_user._id", ondelete="CASCADE")), - Column("board_id", String(64), ForeignKey("kyokusui_board._id", ondelete="CASCADE")) + Column( + "user_id", + String(64), + ForeignKey("mitama_user._id", ondelete="CASCADE") + ), + Column( + "board_id", + String(64), + ForeignKey("kyokusui_board._id", ondelete="CASCADE") + ) ) + class Subscribe(db.Model): __table__ = subscribe_table user_id = subscribe_table.c.user_id, @@ -67,6 +85,7 @@ class Subscribe(db.Model): user = relationship(User) board = relationship(Board) + Permission = permission(db, [ { "name": "板を立てる", diff --git a/pyproject.toml b/pyproject.toml index 6ccd88c..0907f24 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ include = [ [tool.poetry.dependencies] python = "^3.6" -mitama = "^4.5.8" +mitama = "^4.5.12" [tool.poetry.dev-dependencies] unittest = "^0.0"