def create(self, request): template = self.view.get_template("create.html") if request.method == "POST": try: form = ProfileForm(request.post()) prof = Profile() prof.name = form["name"] prof.epoch = form["epoch"] prof.description = form["description"] prof.image = form["image"] for tag_ in [tag.strip() for tag in form["tags"].split(",")]: tag = Tag(tag_) Tag.query.session.add(tag) prof.tags.append(tag) Tag.query.session.commit() prof.lcm = form["lcm"] == "on" prof.mentor = form["mentor"] == "on" prof.alumnight = form["alumnight"] == "on" prof.create() template = self.view.get_template("thanks.html") return Response.render(template, { "title": "図鑑登録", }) except ValidationError as err: return Response.render(template, { "title": "図鑑登録", "error": err.message, }) return Response.render(template)
def settings(self, request): template = self.view.get_template("settings.html") try: with open(self.app.project_dir / "welcome_message.md", "r") as f: welcome_message = f.read() except Exception: welcome_message = """ # Mitamaへようこそ このページに社内システムの使い方を記述しておきましょう! """ if request.method == "POST": try: form = SettingsForm(request.post()) welcome_message_ = form["welcome_message"] if form["role_screen_name"] is not None: role = Role() role.name = form['role_name'] role.create() for screen_name, roles in form['permission'].items(): permission = Permission.retrieve(screen_name=screen_name) permission.roles = [ Role.retrieve(screen_name=role) for role in roles ] permission.update() if form["inner_role_screen_name"] is not None: inner_role = InnerRole() inner_role.name = form['inner_role_name'] inner_role.create() for screen_name, roles in form['inner_permission'].items(): inner_permission = InnerPermission.retrieve( screen_name=screen_name) inner_permission.roles = [ InnerRole.retrieve(role) for role in roles ] inner_permission.update() with open(self.app.project_dir / "welcome_message.md", "w") as f: f.write(welcome_message_) error = "変更を保存しました" except ValidationError as err: error = str(err) welcome_message = welcome_message_ return Response.render( template, { "welcome_message": welcome_message, 'roles': Role.list(), 'permissions': Permission.list(), 'inner_roles': InnerRole.list(), 'inner_permissions': InnerPermission.list(), "error": error }) return Response.render( template, { "welcome_message": welcome_message, 'roles': Role.list(), 'permissions': Permission.list(), 'inner_roles': InnerRole.list(), 'inner_permissions': InnerPermission.list(), })
def create(self, request): template = self.view.get_template("repo/create.html") nodes = [*Group.list(), *User.list()] try: if request.method == 'POST': body = request.post() repo = Repo() repo.name = body['name'] repo.owner = Node.retrieve(body['owner']) repo.create() if not (self.app.project_dir / 'git_template').is_dir: git.Repo.init(self.app.project_dir / 'git_template', bare=True) git.Repo.init(self.app.project_dir / ('repos/' + repo.name + '.git'), bare=True, template=self.app.project_dir / 'git_template') return Response.redirect(self.app.convert_url('/' + repo.name)) except Exception as err: error = str(err) traceback.print_exc(err) return Response.render(template, { 'post': body, 'error': error, 'nodes': nodes }) return Response.render(template, {'post': dict(), 'nodes': nodes})
def create(self, request): template = self.view.get_template("create/profile.html") if request.method == "POST": try: form = ProfileForm(request.post()) prof = Profile() prof.name = form["name"] prof.ruby = form["ruby"] prof.epoch = form["epoch"] prof.image = resize(form["image"]) extra = form["extra"] prof.email = form["email"] prof.contactOption = int(form["contactOption"]) prof.contactIdent = form["contactIdent"] prof.create() careers = form["careers"] histories = form["histories"] for career_ in careers.values(): career = Career() career.type = int(career_["type"]) career.grade = int(career_["grade"]) career.profile = prof career.create() for history_ in histories.values(): history = TravelHistory() history.year = history_["year"] history.place = history_["place"] history.profile = prof history.description = history_["description"] history.create() for colid, value in extra.items(): col = ExtraColumn.retrieve(colid) val = ExtraColumnValue() val.column = col val.profile = prof val.value = value val.create() template = self.view.get_template("thanks.html") return Response.render(template, { "title": "図鑑登録", "extra_columns": ExtraColumn.list() }) except ValidationError as err: return Response.render( template, { "title": "図鑑登録", "error": err.message, "extra_columns": ExtraColumn.list() }) return Response.render(template, { "title": "図鑑登録", "extra_columns": ExtraColumn.list() })
def login(self, request): template = self.view.get_template("login.html") if request.method == "POST": try: form = LoginForm(request.post()) result = User.password_auth(form["screen_name"], form["password"]) sess = request.session() sess["jwt_token"] = result.get_jwt() redirect_to = request.query.get("redirect_to", ["/"])[0] return Response.redirect(redirect_to) except Exception as err: return Response.render(template, {"error": err}, status=401) return Response.render(template, status=401)
def blob(self, request): template = self.view.get_template("repo/blob.html") repo = Repo.retrieve(name=request.params['repo']) head = request.params.get('head', 'master') entity = git.Repo( self.app.project_dir / 'repos/{}.git'.format(repo.name), ) current_head = getattr(entity.heads, head) if hasattr( entity.heads, head) else None tree = current_head.commit.tree or None data = tree / request.params['object'] content = (data.data_stream.read().decode("utf-8") if isinstance( data, git.objects.blob.Blob) else None) above = Path("/" + request.params['object']) / '../' readme = None if isinstance(data, git.objects.tree.Tree): for blob in data: if blob.name.startswith('README'): readme = blob.data_stream.read().decode('utf-8') return Response.render( template, { 'repo': repo, 'current_head': head, 'tree': tree, 'entity': entity, 'name': request.params['object'], 'data': data, 'above': str(above.resolve()), 'content': content, 'readme': readme })
def first_create(self, request): template = self.view.get_template("create/profile.html") error = "" if request.method == "POST": try: form = ProfileForm(request.post()) prof = Profile() prof.name = form["name"] prof.ruby = form["ruby"] prof.epoch = form["epoch"] prof.image = resize(form["image"]) extra = form["extra"] prof.email = form["email"] prof.contactOption = int(form["contactOption"]) prof.contactIdent = form["contactIdent"] prof.create() for colid, value in extra.items(): col = ExtraColumn.retrieve(colid) val = ExtraColumnValue() val.column = col val.profile = prof val.value = value val.create() sess = request.session() sess["profile_id"] = prof._id return Response.redirect( self.app.convert_url("/create/second")) except Exception as err: error = str(err) return Response.render(template, { "title": "図鑑登録", "error": error, "extra_columns": ExtraColumn.list() })
def retrieve(self, request): template = self.view.get_template("repo/retrieve.html") repo = Repo.retrieve(name=request.params['repo']) current_head = request.params.get('head', 'master') entity = git.Repo( self.app.project_dir / 'repos/{}.git'.format(repo.name), ) head = getattr(entity.heads, current_head) if hasattr( entity.heads, current_head) else None commit = None tree = None readme = None if head: commit = head.commit if commit: tree = commit.tree if tree: for blob in tree: if blob.name.startswith('README'): readme = blob.data_stream.read().decode('utf-8') return Response.render( template, { 'repo': repo, 'current_head': current_head, 'head': head, 'tree': tree, 'entity': entity, 'readme': readme })
def update(self, request): template = self.view.get_template('hook/update.html') repo = Repo.retrieve(name=request.params['repo']) if (repo.owner.object != request.user and not InnerPermission.is_accepted('update_repository', repo.owner, request.user)): return Response.redirect( self.app.convert_url('/' + request.params['repo'] + '/hook')) error = '' if request.method == 'POST': form = HookCreateForm(request.post()) shutil.move( self.app.project_dir / 'repos/{}.git/hooks/{}'.format( repo.name, request.params['hook']), self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, form['name'])) with open( self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, form['name']), 'w') as f: f.write(form['code']) return Response.redirect( self.app.convert_url('/{}/hook/{}'.format( repo.name, form['name']))) with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format( repo.name, request.params['hook'])) as f: code = f.read() return Response.render( template, { 'repo': repo, 'name': request.params['hook'], 'code': code, 'error': error })
def update_profile(self, req): template = self.view.get_template("user/update/profile.html") user = User.retrieve(screen_name=req.params["id"]) roles = Role.list() error = "" if req.method == "POST": try: form = UserUpdateForm(req.post()) user.screen_name = form["screen_name"] user.name = form["name"] user.icon = (resize_icon(form["icon"]) if form["icon"] is not None else user.icon) roles_ = [] for role in form["roles"]: roles_.append(Role.retrieve(role)) user.roles = roles_ user.update() return Response.redirect( self.app.convert_url("/users/" + user.screen_name + "/settings")) except Exception as err: error = str(err) return Response.render( template, { "user": user, "screen_name": user.screen_name, "name": user.name, "icon": user.icon, "roles": roles, "error": error }, )
def handle(self, req: Request): for path in self.paths: path_ = req.params["path"] if path_ == "/": path_ = "./" elif path_[0] == "/": path_ = path_[1:] filename = path / path_ if filename.is_file(): mime = guess_type( str(filename)) or ("application/octet-stream", ) with open(filename, "rb") as f: return Response(body=f.read(), content_type=mime[0]) elif (filename / 'index.html').is_file(): mime = guess_type(str(filename / 'index.html')) with open(filename / 'index.html', "rb") as f: return Response(body=f.read(), content_type=mime[0]) for path in self.paths: filename = path / "404.html" if filename.is_file(): with open(filename) as f: return Response( text=f.read(), status=404, headers={"content-type": "text/html"}, ) template = self.view.get_template("404.html") return Response.render(template, status=404)
def update_notification(self, req): template = self.view.get_template("user/update/notification.html") user = User.retrieve(screen_name=req.params["id"]) if user._id != req.user._id: return Response.redirect(self.app.convert_url("/")) error = "" if req.method == "POST": try: form = SubscriptionForm(req.post()) if form["action"] == "subscribe": subscription = PushSubscription() subscription.subscription = form["subscription"] subscription.create() user.subscriptions.append(subscription) user.update() error = "通知の購読を設定しました" else: subscription = PushSubscription.retrieve( subscription=form["subscription"]) subscription.delete() error = "通知の購読を解除しました" except Exception as err: error = str(err) return Response.render( template, { "user": user, "vapid_public_key": self.app.project.vapid["public_key"], "error": error }, )
def handle(self, request): if not is_admin(request.user): return Response.redirect(self.app.convert_url('/')) error = "" if request.method == "POST": try: form = SettingsForm(request.post()) for screen_name, permission_roles in form['permission'].items( ): permission = InnerPermission.retrieve( screen_name=screen_name) permission.roles = [ InnerRole.retrieve(screen_name=role) for role in permission_roles ] permission.update() error = "変更を保存しました" except Exception as err: error = str(err) template = self.view.get_template('settings.html') return Response.render( template, { "roles": InnerRole.list(), "permissions": InnerPermission.list(), "error": error })
def create(self, request): template = self.view.get_template('merge/create.html') repo = Repo.retrieve(name=request.params['repo']) error = "" if request.method == "POST": try: form = MergeCreateForm(request.post()) merge = Merge() merge.base = form['base'] merge.compare = form['compare'] merge.title = form['title'] merge.body = form['body'] merge.repo = repo merge.user = request.user merge.create() return Response.redirect( self.app.convert_url('/' + repo.name + '/merge/' + merge._id)) except Exception as err: error = str(err) return Response.render(template, { "repo": repo, "entity": repo.entity, "error": error })
def delete(self, request): template = self.view.get_template("repo/delete.html") repo = Repo.retrieve(name=request.params['repo']) if (repo.owner != request.user and not InnerPermission.is_accepted( 'delete_repository', repo.owner.object, request.user)): return Response.redirect(self.app.convert_url('/')) try: if request.method == 'POST': if not request.user.password_check(request.post()['password']): raise Exception('wrong password') shutil.rmtree(self.app.project_dir / ('repos/' + repo.name + '.git')) repo.delete() return Response.redirect(self.app.convert_url('/')) except Exception as err: error = str(err) return Response.render(template, {'repo': repo, 'error': error}) return Response.render(template, {'repo': repo})
def handle(self, request): template = self.view.get_template('hook/list.html') repo = Repo.retrieve(name=request.params['repo']) hooks = list() for hook in glob.glob( str(self.app.project_dir / 'repos/{}.git/hooks'.format(repo.name)) + '/*'): hooks.append(os.path.basename(hook)) return Response.render(template, {'repo': repo, 'hooks': hooks})
def list(self, req): template = self.view.get_template("group/list.html") groups = Group.tree() return Response.render( template, { "groups": groups, }, )
def list(self, req): template = self.view.get_template("apps/list.html") apps = AppRegistry() return Response.render( template, { "apps": apps, }, )
def retrieve(self, req): template = self.view.get_template("group/retrieve.html") group = Group.retrieve(screen_name=req.params["id"]) return Response.render( template, { "group": group, }, )
def list(self, req): template = self.view.get_template("user/list.html") users = User.list() return Response.render( template, { "users": users, }, )
def retrieve(self, req): template = self.view.get_template("user/retrieve.html") user = User.retrieve(screen_name=req.params["id"]) return Response.render( template, { "user": user, }, )
def signup(self, request): sess = request.session() template = self.view.get_template("signup.html") invite = UserInvite.retrieve(token=request.query["token"][0]) if request.method == "POST": try: form = RegisterForm(request.post()) user = User() user.email = invite.email user.set_password(form["password"]) user.screen_name = form["screen_name"] user.name = form["name"] user.icon = (resize_icon(form["icon"]) if form["icon"] is not None else user.icon) user.create() sess["jwt_token"] = user.get_jwt() roles = invite.roles if len(roles) > 0: for role_id in invite.roles.split(":"): role = Role.retrieve(role_id) role.append(user) invite.delete() return Response.redirect(self.app.convert_url("/")) except (ValidationError, ValueError) as err: icon = form["icon"] return Response.render( template, { "error": err.messsage, "name": form["name"] or invite.name, "screen_name": (form["screen_name"] or invite.screen_name), "password": form["password"] or "", "icon": icon, }, ) return Response.render( template, { "icon": invite.icon, "name": invite.name or "", "screen_name": invite.screen_name or "", }, )
def search(self, request): template = self.view.get_template("search.html") wordsets = [ wordset.split(' ') for wordset in request.query['words'][0].split(',') ] return Response.render(template, { "title": "「" + request.query + "」の検索結果", "profs": profs })
def retrieve(self, request): template = self.view.get_template("retrieve.html") prof = Profile.retrieve(request.params['id']) extra_columns = ExtraColumnValue.query.filter( ExtraColumnValue.profile == prof) return Response.render(template, { "title": prof.name, "prof": prof, "extra_columns": extra_columns })
def update(self, req): template = self.view.get_template("group/update.html") group = Group.retrieve(screen_name=req.params["id"]) groups = list() for g in Group.list(): if not (group.is_ancestor(g) or group.is_descendant(g) or g == group): groups.append(g) users = list() error = "" message = "" for u in User.list(): if not group.is_in(u): users.append(u) if req.method == "POST": try: form = GroupUpdateForm(req.post()) icon = resize_icon( form["icon"]) if form["icon"] is not None else group.icon group.screen_name = form["screen_name"] group.name = form["name"] group.parent = Group.retrieve( form["parent"]) if form["parent"] is not None else None for role in form["roles"]: group.roles.append(Role.retrieve(screen_name=role)) group.icon = icon group.users = [User.retrieve(user) for user in form['users']] if form['new_user'] is not None: group.users.append(User.retrieve(form['new_user'])) for user, roles in form['inner_roles'].items(): rel = UserGroup.retrieve(group=group, user=User.retrieve(user)) rel.roles = [InnerRole.retrieve(role) for role in roles] group.update() message = "変更を保存しました" except ValidationError as err: error = str(err) roles = Role.list() inner_roles = InnerRole.list() return Response.render( template, { "message": message, "error": error, "group": group, "all_groups": groups, "all_users": users, "screen_name": group.screen_name, "name": group.name, "icon": group.icon, "roles": roles, "inner_roles": inner_roles }, )
def update(self, request): template = self.view.get_template("repo/update.html") repo = Repo.retrieve(name=request.params['repo']) if (repo.owner != request.user and not InnerPermission.is_accepted( 'update_repository', repo.owner.object, request.user)): return Response.redirect(self.app.convert_url('/')) try: if request.method == 'POST': body = request.post() name = repo.name repo.name = body['name'] repo.owner = Node(body['owner']) repo.update() os.rename( self.app.project_dir / ('repos/' + name + '.git'), self.app.project_dir / ('repos/' + repo.name + '.git')) except Exception as err: error = str(err) return Response.render(template, {'repo': repo, 'error': error}) return Response.render(template, {'repo': repo})
def retrieve(self, request): template = self.view.get_template('hook/retrieve.html') repo = Repo.retrieve(name=request.params['repo']) with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format( repo.name, request.params['hook'])) as f: code = f.read() return Response.render(template, { 'repo': repo, 'name': request.params['hook'], 'code': code })
def create(self, req): template = self.view.get_template("user/create.html") invites = User.query.filter(User.password == None).all() if req.method == "POST": try: form = InviteForm(req.post()) user = UserInvite() user.email = form["email"] user.name = form["name"] user.screen_name = form["screen_name"] user._icon = resize_icon(form["icon"]) user.roles = ":".join(form["roles"]) user.create() user.mail( "Mitamaに招待されています", "下記リンクから、Mitamaに参加しましょう\n{}".format( self.app.convert_fullurl(req, "/signup?token=" + user.token))) return Response.render( template, { "invites": invites, "roles": Role.list(), "icon": load_noimage_user() }) except Exception as err: error = str(err) return Response.render( template, { "invites": invites, "roles": Role.list(), "name": form["name"], "screen_name": form["screen_name"], "icon": resize_icon(form["icon"]), "error": error, }, ) return Response.render(template, { "invites": invites, "roles": Role.list(), "icon": load_noimage_user() })
def handle(self, request): template = self.view.get_template('home.html') try: with open(self.app.project_dir / "welcome_message.md", "r") as f: welcome_message = f.read() except Exception: welcome_message = """ # Mitamaへようこそ このページに社内システムの使い方を記述しておきましょう! """ return Response.render(template, {"welcome_message": welcome_message})
def log(self, request): template = self.view.get_template("repo/log.html") repo = Repo.retrieve(name=request.params['repo']) current_head = request.params.get('head', 'master') commits = repo.entity.iter_commits(current_head) if hasattr( repo.entity.heads, current_head) else None return Response.render( template, { 'repo': repo, 'entity': repo.entity, 'current_head': current_head, 'commits': commits })