def get_board(board_name, page=1): try: board = Board.get(Board.name == board_name) except: abort(404, "This page doesn't exist.") current_user = get_current_user(request) per_page = int(config['threads.per_page']) query = board.posts.where(Post.is_reply == False).order_by( Post.pinned.desc(), Post.bumped_at.desc()) threads = query.paginate(page, per_page) return dict(board_name=board.name, board_title=board.title, threads=threads, board=board, current_page=page, is_detail=False, current_user=current_user, thread_count=query.count(), max_file_size=config['uploads.upload_max_size'], maxlength=config['threads.content_max_length'], per_page=per_page, basename=basename)
def post(self): email = self.request.get('email') user = User.query(User.email == email).get() monsterlist = Monster.query(Monster.user == user.key).fetch() if user is not None and monsterlist is not None: playlist = [] for monst in monsterlist: dbplay = Player.query(Player.monster == monst.key).get() if dbplay is not None: playlist.append(dbplay) pl = [] logging.warning(playlist) for play in playlist: monster = Monster.query(Monster.key == play.monster).get() board = Board.get(play.board) if monster.properties is None: monster.properties = json.dumps({}) pd = {'key': play.key.urlsafe(), 'monster': {'key': monster.key.urlsafe(), 'name': monster.name, 'properties': json.loads(monster.properties), 'date': str(monster.date)}, 'board': {'key': play.board.urlsafe()}} pl.append(pd) if user.gamelimit is None: user.gamelimit = 1 user.put() logging.warning({'status': 1, 'games': pl, 'gamelimit': user.gamelimit}) self.response.out.write(json.dumps({'status': 1, 'games': pl, 'gamelimit': user.gamelimit})) else: self.response.out.write(json.dumps({'status': -1, 'message': 'User not found.'}))
def on_message(self, message): data = json.loads(message) if "update_type" in data and data["update_type"] == "draw": board_id = data["board_id"] broadcastData = data self.broadcast(board_id, broadcastData, write_to_self=False) return message_type = data.get('message_type') board_id = data["board_id"] if message_type and message_type == 'user_update': self._update_user(board_id, data['username']) self._broadcast_user_display(board_id) elif message_type and message_type == 'board_name_update': name = self._update_board_name(board_id, data['name']) self._broadcast_board_name_update(board_id, name) else: board = Board.get(board_id) item = Item(**data["item"]) board.updateItem(item) board.save() broadcastData = { "update_type": "pos_change", "item": json.loads(item.to_json()) } self.broadcast(board_id, broadcastData, write_to_self=False)
def put(self): board_id = self.get_argument('board_id') item_id = self.get_argument('id') b = Board.get(board_id) for item in b.items: if item_id == str(item.id): b.items.remove(item) b.save() data = {'update_type': 'remove_item', 'item_id': item_id} for socket in sockets[board_id]: EchoWebSocket.safe_write_to_socket(socket, json.dumps(data))
def school_main(user: User, school: School, school_name: str): boards: list[Board] = Board.get(school_name) form: LoginForm = LoginForm(request.form) mfa_passed: bool = False error: str = "" if len(boards) == 0: abort(404) if request.method == "POST" and form.validate_on_submit(): username: str = form.username.data password: str = form.password.data if user.username != username: error = "현재 로그인 되어 있는 사용자의 이름을 입력해주세요." else: requested_user: User = User.query.filter( User.username == username, User.password == authenticator.hash(password)).first() mfa_passed = requested_user is not None if not mfa_passed: error = "인증에 실패했습니다." elif requested_user.school_name != school_name: mfa_passed = False error = f"{school_name}을(를) 열람할 권한이 없습니다." return render_template( "pages/school.html", form=form, school_name=school_name, boards=[ board.render( visible=not (board.member_only and school.name != school_name)) for board in Board.get(school_name) ], mfa_passed=mfa_passed, error=error, )
def thread_close(board_name, refnum): if f':{board_name}:' not in get_current_user(request).mod: return abort(404, "This page doesn't exist.") board = Board.get(Board.name == board_name) thread = board.posts.where(Post.refnum == refnum).get() if thread.closed: thread.closed = False else: thread.closed = True thread.save() return redirect(f'{basename}/{board_name}/')
def open(self, board_id): user_id = self._generate_user_id() sockets[board_id].append(self) socket_to_board_id[self] = board_id socket_to_user_id[self] = user_id users[board_id].append(user_id) board = Board.get(board_id) if board: broadcastData = json.dumps({ 'board': json.loads(board.to_json()) }) self.safe_write_to_socket(self, broadcastData) self._broadcast_user_display(board_id)
def catalog(board_name): try: board = Board.get(Board.name == board_name) except: return abort(404, "This page doesn't exist.") query = board.posts.where(Post.is_reply == False).order_by( Post.pinned.desc(), Post.bumped_at.desc()) return dict(threads=query, board_name=board.name, board_title=board.title, board=board, current_user=get_current_user(request), basename=basename)
def del_board(board_name): if check_admin(request) == 1: return abort(403, "You are not allowed to do this.") for anon in Anon.select().where(Anon.mod != ""): anon.mod = anon.mod.replace(f':{board_name}:', '') anon.save() board = Board.get(Board.name == board_name) Post.delete().where(Post.board_id == board.id).execute() board.delete_instance() board_directory(board_name, remove=True) return redirect(f'{basename}/admin')
def get_thread(board_name, refnum): try: board = Board.get(Board.name == board_name) except: return abort(404, "This page doesn't exist.") try: thread = board.posts.where(Post.refnum == refnum).get() except: abort(404, "This page doesn't exist.") return dict(board_name=board.name, thread=thread, board=board, is_detail=True, current_user=get_current_user(request), max_file_size=config['uploads.upload_max_size'], maxlength=config['threads.content_max_length'], basename=basename)
def put(self, id=None, *args, **kwargs): board_id = self.get_argument('board_id') url = self.get_argument('url') image_url = self.get_argument('image_url') #tags = self.get_arguments('tags[]', []) tags = self.request.arguments.get('tags[]'); #tags = self.get_argument('tags[]', []) pos_x = self.get_argument('pos_x') pos_y = self.get_argument('pos_y') scale = self.get_argument('scale') locked = self.get_argument('locked') item = Item(id=uuid.uuid4(), url=url, image_url=image_url, tags=tags, pos_x=pos_x, pos_y=pos_y, scale=scale, locked=locked) b = Board.get(board_id) b.items.append(item) b.save() data = {'update_type': 'add_item', 'item': json.loads(item.to_json())} for socket in sockets[board_id]: EchoWebSocket.safe_write_to_socket(socket, json.dumps(data))
def reports(board_name): try: board = Board.get(Board.name == board_name) except: return abort(404, "This page doesn't exist.") current_user = get_current_user(request) if f':{board_name}:' not in current_user.mod: return redirect(f'{basename}/{board_name}/') report_reasons = loads(config['reports.reasons']) return dict(board=board, bans=Anon.select().where(Anon.banned == True), current_user=current_user, board_name=board_name, reasons=report_reasons, reports=board.reports, basename=basename)
def delete_post(board_name): current_user = get_current_user(request) board = Board.get(Board.name == board_name) form = dict(request.forms) if bool(form.get('report')): reason = form.get('report') report_reasons = loads(config['reports.reasons']) if reason not in report_reasons: return redirect(f'{basename}/{board_name}/') for refnum in list(form)[:-1]: report = Report(reason=reason, refnum=refnum, board=board, date=datetime.now().replace(microsecond=0)) report.save() else: for refnum in form: thread = board.posts.where(Post.refnum == refnum).get() if (thread.author == current_user or f':{board_name}:' in current_user.mod): remove_textual_refs(board, thread) if thread.image: remove_media(thread.image) if not thread.is_reply: for reply in board.posts.where( Post.replyrefnum == thread.refnum): if reply.image: remove_media(reply.image) reply.delete_instance() thread.delete_instance() Report.delete().where(refnum == thread.refnum).execute() redirect(f'{basename}/{board_name}/')
def post_thread(board_name): current_user = get_current_user(request) if get_current_user(request).banned: return redirect(f'{basename}/ban_info') board = Board.get(Board.name == board_name) title = request.forms.get('title') content = request.forms.get('content') upload = request.files.get('upload') if not all([title, content]): return abort(400, "Incomplete post.") if len(content) > int(config['threads.content_max_length']): return abort(400, "The content exeeds the maximum length.") author = current_user refnum = board.lastrefnum save_path = file_validation(board_name, refnum, upload, (config['uploads.strip_metadata'] == 'True')) if len(content.split('\n')) < 10: short_content = ' '.join(content.split(' ')[:200]) else: if any((len(item) > 200 for item in content.split(' '))): short_content = ' '.join(content.split(' ')[:200]) else: short_content = '\n'.join(content.split('\n')[:10]) if save_path == 1: return redirect(f'{basename}/{board_name}/') by_mod = (f':{board_name}:' in current_user.mod) data = { "board": board, "author": author, "refnum": refnum, "date": datetime.now().strftime("%d/%m/%Y %H:%M"), "bumped_at": datetime.now().replace(microsecond=0), "filename": upload.filename, "image": save_path, "title": title, "content": content, "short_content": short_content, "by_mod": by_mod } thread = Post(**data) thread.save() board.lastrefnum += 1 board.save() max_active_threads = int(config['threads.max_active']) query = board.posts.where(Post.is_reply == False).order_by( Post.pinned.desc(), Post.bumped_at.desc()) if query.count() >= max_active_threads: threads_to_delete = query.offset(max_active_threads) for thread in threads_to_delete: remove_textual_refs(board, thread) thread.delete_instance() remove_media(thread.image) for reply in board.posts.where(Post.replyrefnum == thread.refnum): reply.delete_instance() remove_media(reply.image) redirect(f'{basename}/{board_name}/')
def get_board(): board = Board() page, _, start, end = request_get(request.args) paging, data_list = board.get(page=page) return render_template('report/board.html', **locals())
def post_reply(board_name, refnum): current_user = get_current_user(request) if get_current_user(request).banned: return redirect(f'{basename}/ban_info') board = Board.get(Board.name == board_name) thread = board.posts.where(Post.refnum == refnum).get() if thread.closed: return abort(423, "You cannot reply because this thread is locked.") content = request.forms.get('content') if not bool(content): return redirect(f'{basename}/{board_name}/') if len(content) > int(config['threads.content_max_length']): return abort(400, "The content exeeds the maximum length.") if len(content.split('\n')) < 10: short_content = ' '.join(content.split(' ')[:200]) else: if any((len(item) > 200 for item in content.split(' '))): short_content = ' '.join(content.split(' ')[:200]) else: short_content = '\n'.join(content.split('\n')[:10]) upload = request.files.get('upload') author = current_user no = board.lastrefnum filename = "" save_path = "" by_mod = (f':{board_name}:' in current_user.mod) if upload is not None: save_path = file_validation( board_name, no, upload, (config['uploads.strip_metadata'] == 'True'), is_reply=True) if save_path == 1: return redirect(f'{basename}/{board_name}/thread/{refnum}') filename = upload.filename data = { "board": board, "author": author, "refnum": no, "is_reply": True, "replyrefnum": refnum, "date": datetime.now().strftime("%d/%m/%Y %H:%M"), "filename": filename, "image": save_path, "content": content, "short_content": short_content, "by_mod": by_mod } reply = Post(**data) reply.save() for word in content.split(): if word[:2] == ">>": ref = word[2:] if ref.rstrip().isdigit(): ref = int(ref) try: thread_ref = board.posts.where(Post.refnum == ref).get() replylist = loads(thread_ref.replylist) if no not in replylist: replylist.append(no) thread_ref.replylist = dumps(replylist) thread_ref.save() except: pass thread.bumped_at = datetime.now().replace(microsecond=0) thread.save() board.lastrefnum += 1 board.save() redirect(f'{basename}/{board_name}/')
def get(self, board_id): board = Board.get(board_id) if not board: raise tornado.web.HTTPError(404) self.render('index.html', board=board, debug=options.cli_args.debug)
def _update_board_name(self, board_id, name): board = Board.get(board_id) board.name = name[:16] board.save() return board.name