Created
March 11, 2022 13:10
-
-
Save aliwo/ec9a93ebba062d739819e2f247fc637a to your computer and use it in GitHub Desktop.
tutoring_03_11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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