Models.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_SECRET = None # type: Optional[str]
  15. CLIENT_SCOPES = None # type: Optional[str]
  16. CALLBACK_URL = None # type: Optional[str]
  17. AUTH_DATA_PREFERENCE_KEY = "" # type: str
  18. AUTH_SUCCESS_REDIRECT = "https://ultimaker.com" # type: str
  19. AUTH_FAILED_REDIRECT = "https://ultimaker.com" # type: str
  20. class UserProfile(BaseModel):
  21. """User profile data template."""
  22. user_id = None # type: Optional[str]
  23. username = None # type: Optional[str]
  24. profile_image_url = None # type: Optional[str]
  25. organization_id = None # type: Optional[str]
  26. subscriptions = None # type: Optional[List[Dict[str, Any]]]
  27. class AuthenticationResponse(BaseModel):
  28. """Authentication data template."""
  29. # Data comes from the token response with success flag and error message added.
  30. success = True # type: bool
  31. token_type = None # type: Optional[str]
  32. expires_in = None # type: Optional[str]
  33. scope = None # type: Optional[str]
  34. err_message = None # type: Optional[str]
  35. received_at = None # type: Optional[str]
  36. access_token = KeyringAttribute()
  37. refresh_token = KeyringAttribute()
  38. def __init__(self, **kwargs: Any) -> None:
  39. self.access_token = kwargs.pop("access_token", None)
  40. self.refresh_token = kwargs.pop("refresh_token", None)
  41. super(AuthenticationResponse, self).__init__(**kwargs)
  42. def dump(self) -> Dict[str, Union[bool, Optional[str]]]:
  43. """
  44. Dumps the dictionary of Authentication attributes. KeyringAttributes are transformed to public attributes
  45. If the keyring was used, these will have a None value, otherwise they will have the secret value
  46. :return: Dictionary of Authentication attributes
  47. """
  48. dumped = deepcopy(vars(self))
  49. dumped["access_token"] = dumped.pop("_access_token")
  50. dumped["refresh_token"] = dumped.pop("_refresh_token")
  51. return dumped
  52. class ResponseStatus(BaseModel):
  53. """Response status template."""
  54. code = 200 # type: int
  55. message = "" # type: str
  56. class ResponseData(BaseModel):
  57. """Response data template."""
  58. status = None # type: ResponseStatus
  59. data_stream = None # type: Optional[bytes]
  60. redirect_uri = None # type: Optional[str]
  61. content_type = "text/html" # type: str
  62. HTTP_STATUS = {
  63. """Possible HTTP responses."""
  64. "OK": ResponseStatus(code = 200, message = "OK"),
  65. "NOT_FOUND": ResponseStatus(code = 404, message = "NOT FOUND"),
  66. "REDIRECT": ResponseStatus(code = 302, message = "REDIRECT")
  67. } # type: Dict[str, ResponseStatus]