Beispiel #1
0
    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
            },
        )
Beispiel #2
0
 def retrieve(self, request):
     try:
         id = request.params["id"]
         if id.isdigit():
             user = User.retrieve(int(id))
         else:
             user = User.retrieve(screen_name=id)
         return Response.json(user.to_dict())
     except KeyError as err:
         error = err
         return Response.json({
             "error": err.__class__.__name__,
             "message": err.message,
             "args": err.args,
         })
Beispiel #3
0
 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
         },
     )
Beispiel #4
0
 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
         },
     )
Beispiel #5
0
 def delete(self, request):
     try:
         id = request.params["id"]
         if id.isdigit():
             user = User.retrieve(int(id))
         else:
             user = User.retrieve(screen_name=id)
         user.delete()
         return Response.json({"_id": user._id})
     except Exception as err:
         error = err
         return Response.json({
             "error": err.__class__.__name__,
             "message": err.message,
             "args": err.args,
         })
Beispiel #6
0
 def icon(self, request):
     try:
         id = request.params["id"]
         if id.isdigit():
             user = User.retrieve(int(id))
         else:
             user = User.retrieve(screen_name=id)
         f = magic.Magic(mime=True, uncompress=True)
         mime = f.from_buffer(group.icon)
         return Response(body=user.icon, content_type=mime)
     except KeyError as err:
         return Response.error({
             "error": err.__class__.__name__,
             "message": err.message,
             "args": err.args,
         })
Beispiel #7
0
    def process_result_value(self, value, dialect):
        from mitama.models import User

        if value == None:
            return None
        else:
            user = User.retrieve(value)
            return user
Beispiel #8
0
 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,
         },
     )
Beispiel #9
0
 def remove_user(self, req):
     try:
         group = Group.retrieve(screen_name=req.params["id"])
         child = User.retrieve(req.params["uid"])
         group.remove(child)
     except Exception as err:
         print(err)
         pass
     finally:
         return Response.redirect(
             self.app.convert_url("/groups/" + group.screen_name +
                                  "/settings"))
Beispiel #10
0
    def process_result_value(self, value, dialect):
        prefix = value.split("-")[0]
        if prefix == "user":
            from mitama.models import User

            node = User.retrieve(value)
        elif prefix == "group":
            from mitama.models import Group

            node = Group.retrieve(value)
        else:
            raise TypeError("Invalid ID format")
        return node
Beispiel #11
0
 def update(self, request):
     try:
         post = request.post()
         id = request.params["id"]
         if id.isdigit():
             user = User.retrieve(int(id))
         else:
             user = User.retrieve(screen_name=id)
         if "screen_name" in post:
             user.screen_name = post["screen_name"]
         if "name" in post:
             user.name = post["name"]
         if "password" in post:
             user.set_password(post["password"])
         user.update()
         return Response.json(user.to_dict())
     except KeyError as err:
         error = err
         return Response.json({
             "error": err.__class__.__name__,
             "message": err.message,
             "args": err.args,
         })
Beispiel #12
0
 def update_password(self, req):
     template = self.view.get_template("user/update/password.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 = UserPasswordUpdateForm(req.post())
             if form["password"] != form["password_"]:
                 raise Exception("パスワードが一致しません")
             user.set_password(form["password"])
             user.update()
             error = "パスワードを変更しました"
         except Exception as err:
             error = str(err)
     return Response.render(
         template,
         {
             "user": user,
             "error": error
         },
     )
Beispiel #13
0
 def append(self, req):
     form = req.post()
     try:
         group = Group.retrieve(screen_name=req.params["id"])
         nodes = list()
         if "user" in form:
             for uid in form.getlist("user"):
                 try:
                     nodes.append(User.retrieve(uid))
                 except Exception:
                     pass
         if "group" in form:
             for gid in form.getlist("group"):
                 try:
                     nodes.append(Group.retrieve(gid))
                 except Exception:
                     pass
         group.append_all(nodes)
     except Exception:
         pass
     finally:
         return Response.redirect(
             self.app.convert_url("/groups/" + group.screen_name +
                                  "/settings"))