Models.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, Dict, Any, List, Union
  4. from copy import deepcopy
  5. from cura.OAuth2.KeyringAttribute import KeyringAttribute
  6. class BaseModel:
  7. def __init__(self, **kwargs: Any) -> None:
  8. self.__dict__.update(kwargs)
  9. class OAuth2Settings(BaseModel):
  10. """OAuth OAuth2Settings data template."""
  11. CALLBACK_PORT = None # type: Optional[int]
  12. OAUTH_SERVER_URL = None # type: Optional[str]
  13. CLIENT_ID = None # type: Optional[str]
  14. CLIENT_SCOPES = None # type: Optional[str]
  15. CALLBACK_URL = None # type: Optional[str]
  16. AUTH_DATA_PREFERENCE_KEY = "" # type: str
  17. AUTH_SUCCESS_REDIRECT = "https://ultimaker.com" # type: str
  18. AUTH_FAILED_REDIRECT = "https://ultimaker.com" # type: str
  19. class UserProfile(BaseModel):
  20. """User profile data template."""
  21. user_id = None # type: Optional[str]
  22. username = None # type: Optional[str]
  23. profile_image_url = None # type: Optional[str]
  24. organization_id = None # type: Optional[str]
  25. subscriptions = None # type: Optional[List[Dict[str, Any]]]
  26. class AuthenticationResponse(BaseModel):
  27. """Authentication data template."""
  28. # Data comes from the token response with success flag and error message added.
  29. success = True # type: bool
  30. token_type = None # type: Optional[str]
  31. expires_in = None # type: Optional[str]
  32. scope = None # type: Optional[str]
  33. err_message = None # type: Optional[str]
  34. received_at = None # type: Optional[str]
  35. access_token = KeyringAttribute()
  36. refresh_token = KeyringAttribute()
  37. def __init__(self, **kwargs: Any) -> None:
  38. self.access_token = kwargs.pop("access_token", None)
  39. self.refresh_token = kwargs.pop("refresh_token", None)
  40. super(AuthenticationResponse, self).__init__(**kwargs)
  41. def dump(self) -> Dict[str, Union[bool, Optional[str]]]:
  42. """
  43. Dumps the dictionary of Authentication attributes. KeyringAttributes are transformed to public attributes
  44. If the keyring was used, these will have a None value, otherwise they will have the secret value
  45. :return: Dictionary of Authentication attributes
  46. """
  47. dumped = deepcopy(vars(self))
  48. dumped["access_token"] = dumped.pop("_access_token")
  49. dumped["refresh_token"] = dumped.pop("_refresh_token")
  50. return dumped
  51. class ResponseStatus(BaseModel):
  52. """Response status template."""
  53. code = 200 # type: int
  54. message = "" # type: str
  55. class ResponseData(BaseModel):
  56. """Response data template."""
  57. status = None # type: ResponseStatus
  58. data_stream = None # type: Optional[bytes]
  59. redirect_uri = None # type: Optional[str]
  60. content_type = "text/html" # type: str
  61. HTTP_STATUS = {
  62. """Possible HTTP responses."""
  63. "OK": ResponseStatus(code = 200, message = "OK"),
  64. "NOT_FOUND": ResponseStatus(code = 404, message = "NOT FOUND"),
  65. "REDIRECT": ResponseStatus(code = 302, message = "REDIRECT")
  66. } # type: Dict[str, ResponseStatus]