Skip to content
Merged
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
5 changes: 5 additions & 0 deletions apps/knowledge/views/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ def post(self, request: Request, workspace_id: str, knowledge_id: str):
[PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission()], CompareConstants.AND),
)
def get(self, request: Request, workspace_id: str, knowledge_id: str):
raw_tags = request.query_params.getlist("tags[]")
return result.success(DocumentSerializers.Query(
data={
'workspace_id': workspace_id,
'knowledge_id': knowledge_id,
'folder_id': request.query_params.get('folder_id'),
'name': request.query_params.get('name'),
'tag': request.query_params.get('tag'),
'tag_exclude': request.query_params.get('tag_exclude'),
'tag_ids': [tag for tag in raw_tags if tag != 'NO_TAG'],
'no_tag': 'NO_TAG' in raw_tags,
'desc': request.query_params.get("desc"),
'user_id': request.query_params.get('user_id')
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize, but I can't fully review the code snippet you provided because it's incomplete. Without more context, including full import statements, actual function definitions, or additional parts of the code, it's difficult to determine what might be wrong with it.

The post method seems fine at this point, but there are a few suggestions:

  1. Permissions Logic: The permission logic appears correct, but make sure that all permissions specified in PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission() exist and have been correctly defined elsewhere in your project.

  2. Field Handling:

    • "tags[]" should be handled similar to other query parameters in terms of type conversion (e.g., converting from a string list to a comma-separated string).
    • Ensure that any filtering conditions (tag, tag_exclude) use proper field lookups if they're not already being used in Django ORM queries.
  3. Parameter Naming Consistency: It would help readability if parameter names like user_id were consistently named throughout the API endpoints.

Here is an example how some improvements could be made based on these observations:

from typing import List

class QueryData:
    def __init__(
        self,
        workspace_id: str,
        knowledge_id: str,
        folder_id: str = '',
        name: str = None,
        tags: List[str] = [],
        no_tag: bool = False,
        desc: str = None,
        user_id: str = ''
    ):
        self.workspace_id = workspace_id
        self.knowledge_id = knowledge_id
        self.folder_id = folder_id
        self.name = name
        self.tags = tags
        self.no_tag = no_tag
        self.desc = desc
        self.user_id = user_id

# Simplify processing tags
def parse_tags(query_params) -> (List[int], str):
    raw_tags = query_params.getlist("tags[]")
    return [int(tag.strip()) for tag in raw_tags if tag.isnumeric()] + ['NO_TAG'], 'NO_TAG' in raw_tags

@app.post("/workspaces/{workspace_id}/knowledge/{knowledge_id}")
@permission_required([PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission()], CompareConstants.AND)
def post(request: Request, workspace_id: str, knowledge_id: str):
    # ... existing code ...

@app.get("/workspaces/{workspace_id}/knowledge/{knowledge_id}")
def get(request: Request, workspace_id: str, knowledge_id: str):
    raw_tags, no_tag_value = parse_tags(request.query_params)
    
    data = {
        "workspace_id": workspace_id,
        "knowledge_id": knowledge_id,
        "folder_id": request.query_params.get("folder_id", ''),
        "name": request.query_params.get("name", None),
        "tags": raw_tags[::],
        "no_tag": no_tag_value,
        "desc": request.query_params.get("desc", None),
        "user_id": request.query_params.get("user_id", '')
    }
    
    return result.success(DocumentSerializers.Query(data=data))

These changes improve readability, ensure consistent naming conventions, and handle parsing of tags more robustly. Please replace placeholders like result.success and other non-existent functions/variables with real implementations and ensure all necessary imports are included.

Expand Down