Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable custom roles API & fix role locales #28

Merged
merged 10 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ babel-update-po:
venv/bin/pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot app
venv/bin/pybabel update -i messages.pot -d app/translations

babel-update-mo:
babel-update-mo: babel-update-po
venv/bin/pybabel compile -d app/translations

babel-translate-po:
Expand Down
2 changes: 1 addition & 1 deletion app/apis/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
}
"""
if not self.current_user.can(file.project, ProjectPermission.ACCESS):
raise NoPermissionError(gettext("您没有权限移动文件"))
raise NoPermissionError(gettext("您没有此项目的访问权限"))

Check warning on line 159 in app/apis/file.py

View check run for this annotation

Codecov / codecov/patch

app/apis/file.py#L159

Added line #L159 was not covered by tests
query = self.get_query({}, FileGetSchema())
data = file.to_api()
data["project_id"] = str(file.project.id)
Expand Down
7 changes: 5 additions & 2 deletions app/apis/role.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from typing import Union

Check warning on line 1 in app/apis/role.py

View check run for this annotation

Codecov / codecov/patch

app/apis/role.py#L1

Added line #L1 was not covered by tests
from app.core.responses import MoePagination
from app.core.views import MoeAPIView
from app.decorators.auth import token_required
from app.decorators.url import fetch_group
from app.exceptions import NoPermissionError
from app.models.team import Team
from app.models.project import Project

Check warning on line 8 in app/apis/role.py

View check run for this annotation

Codecov / codecov/patch

app/apis/role.py#L7-L8

Added lines #L7 - L8 were not covered by tests
from app.validators.role import RoleSchema


class RoleListAPI(MoeAPIView):
@token_required
@fetch_group
def get(self, group):
def get(self, group: Union[Team, Project]):

Check warning on line 15 in app/apis/role.py

View check run for this annotation

Codecov / codecov/patch

app/apis/role.py#L15

Added line #L15 was not covered by tests
"""
@api {get} /v1/<group_type>/<group_id>/roles 获取自定义角色
@apiVersion 1.0.0
Expand Down Expand Up @@ -71,7 +74,7 @@
class RoleAPI(MoeAPIView):
@token_required
@fetch_group
def put(self, group, role_id):
def put(self, group: Union[Team, Project], role_id: str):

Check warning on line 77 in app/apis/role.py

View check run for this annotation

Codecov / codecov/patch

app/apis/role.py#L77

Added line #L77 was not covered by tests
"""
@api {put} /v1/<group_type>/<group_id>/roles/<role_id> 修改自定义角色
@apiVersion 1.0.0
Expand Down
24 changes: 13 additions & 11 deletions app/apis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
ProjectTargetOutputListAPI,
)
from app.apis.project_set import ProjectSetAPI
from app.apis.role import RoleAPI, RoleListAPI

# from app.apis.role import RoleAPI, RoleListAPI
from app.apis.source import FileSourceListAPI, SourceAPI, SourceRankAPI
from app.apis.team import (
TeamInsightProjectListAPI,
Expand Down Expand Up @@ -386,16 +387,17 @@
methods=["PUT", "DELETE", "PATCH", "OPTIONS"],
view_func=ApplicationAPI.as_view("application"),
)
group.add_url_rule(
"/<group_type>/<group_id>/roles",
methods=["GET", "POST", "OPTIONS"],
view_func=RoleListAPI.as_view("role_list"),
)
group.add_url_rule(
"/<group_type>/<group_id>/roles/<role_id>",
methods=["PUT", "DELETE", "OPTIONS"],
view_func=RoleAPI.as_view("role"),
)
# disable custom role APIs for now
# group.add_url_rule(
# "/<group_type>/<group_id>/roles",
# methods=["GET", "POST", "OPTIONS"],
# view_func=RoleListAPI.as_view("role_list"),
# )
# group.add_url_rule(
# "/<group_type>/<group_id>/roles/<role_id>",
# methods=["PUT", "DELETE", "OPTIONS"],
# view_func=RoleAPI.as_view("role"),
# )
group.add_url_rule(
"/<group_type>/<group_id>/users",
methods=["GET", "OPTIONS"],
Expand Down
10 changes: 6 additions & 4 deletions app/core/rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class RoleMixin:
create_time: datetime = DateTimeField(
db_field="m_c", default=datetime.datetime.utcnow
)
system_role_data = {} # type: dict
system_role_data: List[dict] = []

def clean(self):
# ==处理permission==
Expand Down Expand Up @@ -185,9 +185,9 @@ def init_system_roles(cls: Type[Document]):
system_code=role["system_code"],
intro=role.get("intro", ""),
).save()
logger.info(f"初始化{cls._class_name}表完成")
logger.info(f"Populated {cls._class_name} with default roles")
else:
logger.info(f"已存在{cls._class_name}表,跳过初始化")
logger.info(f"{cls._class_name} already populated")

@classmethod
def system_roles(cls: Type[Document], without_creator=False):
Expand All @@ -214,9 +214,11 @@ def by_id(cls: Type[Document], id):
return role

def to_api(self: Document):
name = gettext(self.name)
logger.debug('RoleMixin.to_api: name="%s" %s', self.name, name)
return {
"id": str(self.id),
"name": self.name,
"name": gettext(self.name),
"level": self.level,
"system": self.system,
"system_code": self.system_code,
Expand Down
10 changes: 6 additions & 4 deletions app/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ def create_default_team(admin_user):
from app.models.site_setting import SiteSetting

if Team.objects().count() == 0:
logger.debug("已建立默认团队")
logger.debug("creating default team")
team = Team.create(
name="默认团队",
name="Default Team",
creator=admin_user,
)
team.intro = "所有新用户会自动加入此团队,如不需要,站点管理员可以在“站点管理-自动加入的团队 ID”中删除此团队 ID。"
team.intro = """
All new users will automatically join this team. This can be disabled by site administrator.
所有新用户会自动加入此团队,如不需要,站点管理员可以在“站点管理-自动加入的团队 ID”中删除此团队 ID。""".strip()
team.allow_apply_type = AllowApplyType.ALL
team.application_check_type = ApplicationCheckType.ADMIN_CHECK
team.default_role = TeamRole.by_system_code("member")
Expand All @@ -131,7 +133,7 @@ def create_default_team(admin_user):
site_setting.auto_join_team_ids = [team.id]
site_setting.save()
else:
logger.debug("已有团队,跳过建立默认团队")
logger.debug("default team existed")


def init_db(app: Flask):
Expand Down
2 changes: 1 addition & 1 deletion app/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def rename(self, name):
file.save()

@need_activated
def move_to(self, parent: Union[str, ObjectId, "File"]):
def move_to(self, parent: Union[str, ObjectId, "File", None]):
"""将某个文件或文件夹移动到另一个地方"""
# 强制从数据库更新自身,以免之前有删除、建立文件夹操作,影响最后的计数缓存
self.reload()
Expand Down
26 changes: 18 additions & 8 deletions app/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@
class ProjectRole(RoleMixin, Document):
permission_cls = ProjectPermission
group = ReferenceField("Project", db_field="g")
system_role_data = [
system_role_data: List[dict] = [
{
"name": gettext("创建人"),
"name": "创建人",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.CHANGE,
Expand Down Expand Up @@ -173,7 +173,7 @@
"system_code": "creator",
},
{
"name": gettext("管理员"),
"name": "管理员",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.CHANGE,
Expand Down Expand Up @@ -204,7 +204,7 @@
"system_code": "admin",
},
{
"name": gettext("监理"),
"name": "监理",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.ADD_FILE,
Expand All @@ -229,7 +229,7 @@
"system_code": "coordinator",
},
{
"name": gettext("校对"),
"name": "校对",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.OUTPUT_TRA,
Expand All @@ -244,7 +244,7 @@
"system_code": "proofreader",
},
{
"name": gettext("翻译"),
"name": "翻译",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.OUTPUT_TRA,
Expand All @@ -257,7 +257,7 @@
"system_code": "translator",
},
{
"name": gettext("嵌字"),
"name": "嵌字",
"permissions": [
ProjectPermission.ACCESS,
ProjectPermission.OUTPUT_TRA,
Expand All @@ -269,13 +269,23 @@
"system_code": "picture_editor",
},
{
"name": gettext("见习翻译"),
"name": "见习翻译",
"permissions": [ProjectPermission.ACCESS, ProjectPermission.ADD_TRA],
"level": 100,
"system_code": "supporter",
},
]

@classmethod
def unused_provide_babel_names(cls):
gettext("创建人")
gettext("管理员")
gettext("监理")
gettext("校对")
gettext("翻译")
gettext("嵌字")
gettext("见习翻译")

Check warning on line 287 in app/models/project.py

View check run for this annotation

Codecov / codecov/patch

app/models/project.py#L281-L287

Added lines #L281 - L287 were not covered by tests


class ProjectSet(Document):
"""项目集"""
Expand Down
23 changes: 16 additions & 7 deletions app/models/team.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import datetime
import re
from typing import List

from flask import current_app
from flask_babel import gettext, lazy_gettext
from flask_babel import lazy_gettext, gettext
from mongoengine import (
CASCADE,
DENY,
Expand Down Expand Up @@ -130,9 +131,9 @@
class TeamRole(RoleMixin, Document):
permission_cls = TeamPermission
group = ReferenceField("Team", db_field="g")
system_role_data = [
system_role_data: List[dict] = [
{
"name": gettext("创建人"),
"name": "创建人",
"permissions": [
TeamPermission.ACCESS,
TeamPermission.DELETE,
Expand Down Expand Up @@ -164,7 +165,7 @@
"system_code": "creator",
},
{
"name": gettext("管理员"),
"name": "管理员",
"permissions": [
TeamPermission.ACCESS,
TeamPermission.CHANGE,
Expand Down Expand Up @@ -195,7 +196,7 @@
"system_code": "admin",
},
{
"name": gettext("资深成员"),
"name": "资深成员",
"permissions": [
TeamPermission.ACCESS,
TeamPermission.CREATE_TERM_BANK,
Expand All @@ -216,7 +217,7 @@
"system_code": "senior",
},
{
"name": gettext("成员"),
"name": "成员",
"permissions": [
TeamPermission.ACCESS,
TeamPermission.CREATE_TERM_BANK,
Expand All @@ -235,7 +236,7 @@
"system_code": "member",
},
{
"name": gettext("见习成员"),
"name": "见习成员",
"permissions": [TeamPermission.ACCESS],
"level": 100,
"system_code": "beginner",
Expand All @@ -247,6 +248,14 @@
if self.has_permission(self.permission_cls.AUTO_BECOME_PROJECT_ADMIN):
return ProjectRole.by_system_code("admin")

@classmethod
def unused_provide_babel_names(cls):
gettext("创建人")
gettext("管理员")
gettext("资深成员")
gettext("成员")
gettext("见习成员")

Check warning on line 257 in app/models/team.py

View check run for this annotation

Codecov / codecov/patch

app/models/team.py#L253-L257

Added lines #L253 - L257 were not covered by tests


class Team(GroupMixin, Document):
name = StringField(db_field="n", unique=True) # 名称
Expand Down
10 changes: 8 additions & 2 deletions app/models/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from app.models.output import Output
import datetime
import re
from typing import NoReturn, Union
from typing import NoReturn, Optional, Union

from flask import current_app, g
from flask_babel import gettext
Expand Down Expand Up @@ -243,7 +243,13 @@ def to_api(self):
return data

# =====团队操作=====
def teams(self, role=None, skip: int = None, limit: int = None, word: str = None):
def teams(
self,
role=None,
skip: Optional[int] = None,
limit: Optional[int] = None,
word: Optional[str] = None,
):
"""
获取自己加入的团队

Expand Down
Loading