Skip to content

Instantly share code, notes, and snippets.

@aliwo
Created March 11, 2022 13:10
Show Gist options
  • Save aliwo/ec9a93ebba062d739819e2f247fc637a to your computer and use it in GitHub Desktop.
Save aliwo/ec9a93ebba062d739819e2f247fc637a to your computer and use it in GitHub Desktop.
tutoring_03_11
from typing import Any
from pydantic import BaseModel
class User:
id: int
is_admin: bool
username: str
password: str
# def json(self, requester: 'User') -> dict[str, Any]:
# result = {
# 'username': self.username,
# }
#
# if requester.is_admin:
# result['password'] = self.password
# elif requester.id == self.id:
# result['password'] = self.password
#
# return result
class UserWithPassword(BaseModel):
id: int
is_admin: bool
username: str
password: str
class UserWithoutPassword(BaseModel):
id: int
is_admin: bool
username: str
class UserForXXX(BaseModel):
pass
def admin_view(user: User):
return UserWithPassword.parse_obj(user)
def self_view(user: User):
return UserWithPassword.parse_obj(user)
def user_view(user: User):
return UserWithoutPassword.parse_obj(user)
def new_user_view(user: User):
return UserForXXX.parse_obj(user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment