PyZipline/pyzipline/models.py

348 lines
12 KiB
Python

"""This is a list of all the models used in PyZipline. They are used to represent the data returned from the Zipline API."""
from typing import List, Dict, Optional
from datetime import datetime
from pyzipline.utils import convert_str_to_datetime
class File:
"""File object used for uploading files to Zipline
Attributes:
created_at (datetime.datetime): Datetime object of when the file was created
id (int): ID of the file
mimetype (str): String of the file's mimetype
views (int): Integer of the number of views the file has
name (str): String of the file's name
size (int): Integer of the file's size in bytes
favorite (bool): Boolean of whether the file is favorited
original_name (Optional[str]): String of the file's original name
url (Optional[str]): String of the file's URL
max_views (Optional[int]): Integer of the file's maximum number of views
expired_at (Optional[datetime]): Datetime object of when the file will expire
thumbnail (Optional[str]): String of the file's thumbnail URL
folder_id (Optional[int]): Integer of the file's folder ID
"""
def __init__(
self,
createdAt: datetime,
id: int, # pylint: disable=redefined-builtin
mimetype: str,
views: int,
name: str,
size: int,
favorite: bool,
originalName: str = None,
url: str = None,
maxViews: int = None,
expiredAt: datetime = None,
thumbnail: str = None,
folderId: int = None,
**kwargs
):
self.created_at = createdAt
self.id = id
self.mimetype = mimetype
self.views = views
self.name = name
self.size = size
self.favorite = favorite
self.original_name = originalName
self.url = url
self.max_views = maxViews
self.expired_at = expiredAt
self.thumbnail = thumbnail
self.folder_id = folderId
self.__dict__.update(kwargs)
def __str__(self):
return self.name
class Result:
"""Result returned from low-level RestAdapter
Attributes:
success (bool): Boolean of whether the request was successful
status_code (int): Standard HTTP Status code
message (str = ''): Human readable result
data (Union[List[Dict], Dict]): Python List of Dictionaries (or maybe just a single Dictionary on error)
"""
def __init__(self, success: bool, status_code: int, message: str = '', data: List[Dict] = None):
self.success = success
self.status_code = status_code
self.message = message
self.data = data if data else {}
def __str__(self):
return f"{self.status_code}: {self.message}\n{self.data}"
class Invite:
"""Invite object used for managing invites
Attributes:
id (int): Integer ID of the invite
code (str): String of the invite's code
created_at (datetime): Datetime object of when the invite was created
expires_at (datetime): Datetime object of when the invite will expire
used (bool): Boolean of whether the invite has been used
created_by_id (int): Integer ID of the user who created the invite
"""
def __init__(
self,
id: int, # pylint: disable=redefined-builtin
code: str,
createdAt: str,
expiresAt: str,
used: bool,
createdById: int,
**kwargs
):
self.id = id
self.code = code
self.created_at = convert_str_to_datetime(createdAt)
self.expires_at = convert_str_to_datetime(expiresAt)
self.used = used
self.created_by_id = createdById
self.__dict__.update(kwargs)
def __str__(self):
return self.code
class Stats:
"""Stats object used for retrieving stats
Attributes:
id (int): Integer ID of the stats
createdAt (datetime): Datetime object of when the stats were created
max_timestamp (Optional[datetime]): Datetime object of the maximum timestamp of the stats
size (str): String of the size of the files
size_num (int): Integer of the size of the files in bytes
count (int): Integer of the number of files
count_users (int): Integer of the number of users
views_count (int): Integer of the number of views
types_count (Optional[List[Mimetype]]): List of Mimetype objects
count_by_user (Optional[List[CountByUser]]): List of CountByUser objects
"""
def __init__(
self,
id: int, # pylint: disable=redefined-builtin
createdAt: datetime,
data: dict,
max_timestamp: Optional[datetime] = None
):
self.id = id
self.createdAt = createdAt
self._data = data
self.max_timestamp = max_timestamp
self.size = self._data['size']
self.size_num = self._data['size_num']
self.count = self._data['count']
self.count_users = self._data['count_users']
self.views_count = self._data['views_count']
self.types_count: list = self._data['types_count']
self.count_by_user: list = self._data['count_by_user']
if self.types_count is not None:
new_types_count = []
for mimetype_entry in self.types_count:
if isinstance(mimetype_entry, dict):
m = self.Mimetype(**mimetype_entry)
new_types_count.append(m)
self.types_count = new_types_count
if self.count_by_user is not None:
new_count_by_user = []
for count_by_user_entry in self.count_by_user:
if isinstance(count_by_user_entry, dict):
c = self.CountByUser(**count_by_user_entry)
new_count_by_user.append(c)
self.count_by_user = new_count_by_user
def __str__(self):
return str(self.id)
class Mimetype:
"""Object used in [Stats](.#pyzipline.models.Stats) for storing the number of files with a specific mimetype
Attributes:
mimetype (str): String of the mimetype
count (int): Integer of the number of files with this mimetype"""
def __init__(
self,
mimetype: str,
count: int
):
self.mimetype = mimetype
self.count = count
def __str__(self):
return f"{self.mimetype}: {self.count}"
class CountByUser:
"""Object used in [Stats](.#pyzipline.models.Stats) for storing the number of files uploaded by a user
Attributes:
username (str): String of the username
count (int): Integer of the number of files uploaded by this user"""
def __init__(
self,
username: str,
count: int
):
self.username = username
self.count = count
def __str__(self):
return f"{self.username}: {self.count}"
class OAuth:
"""OAuth object used for managing OAuth
Attributes:
id (int): Integer ID of the OAuth
provider (str): String of the OAuth's provider, one of 'DISCORD', 'GITHUB', 'GOOGLE'
user_id (int): Integer ID of the user who owns the OAuth
oauth_id (str): String of the OAuth's provider ID
username (str): String of the OAuth's connected account's username
token (str): String of the OAuth's access token
refresh (Optional[str]): String of the OAuth's refresh token
"""
def __init__(
self,
id: int, # pylint: disable=redefined-builtin
provider: str,
oauthId: int,
providerId: str,
username: str,
token: str,
refresh: Optional[str],
**kwargs
):
self.id = id
self.provider = provider
self.oauth_id = oauthId
self.provider_id = providerId
self.username = username
self.token = token
self.refresh = refresh
self.__dict__.update(kwargs)
def __str__(self):
return self.provider
class User:
"""Object containing user information
/// admonition | Contains Sensitive Information
type: danger
Please be mindful of how you use/store this object, as it contains sensitive information such as the user's token and OAuth/TOTP information.
///
Attributes:
id (int): Integer ID of the user
uuid (str): String of the user's UUID
username (str): String of the user's username
avatar (Optional[str]): String of the user's avatar, base64 encoded
token (str): String of the user's token
administrator (bool): Boolean of whether the user is an administrator
super_admin (bool): Boolean of whether the user is a super administrator
system_theme (str): String of the user's system theme
embed (Embed): Embed object of the user's embed
totp_secret (Optional[str]): String of the user's TOTP secret
domains (List[str]): List of Strings of the user's domains
oauth (Optional[List[OAuth]]): List of [OAuth](.#pyzipline.models.OAuth) objects
ratelimit (Optional[datetime]): Datetime object of when the user's ratelimit expires
"""
def __init__(
self,
id: int, # pylint: disable=redefined-builtin
uuid: str,
username: str,
avatar: Optional[str],
token: str,
administrator: bool,
superAdmin: bool,
systemTheme: str,
embed: 'Embed',
totpSecret: Optional[str],
domains: List[str],
oauth: Optional[List['OAuth']] = None,
ratelimit: Optional[datetime] = None,
**kwargs
):
self.id = id
self.uuid = uuid
self.username = username
self.avatar = avatar
self.token = token
self.administrator = administrator
self.super_admin = superAdmin
self.system_theme = systemTheme
self.embed = self.Embed(**embed)
self.totp_secret = totpSecret
self.domains = domains
self.oauth = oauth
self.ratelimit = ratelimit
self.__dict__.update(kwargs)
if self.oauth is not None:
for oauth_entry in self.oauth:
self.oauth.remove(oauth_entry)
o = OAuth(**oauth_entry)
self.oauth.append(o)
def __str__(self):
return self.username
class Embed:
"""Object containing a user's embed settings
Attributes:
color (Optional[str]): String of the embed's color
title (Optional[str]): String of the embed's title
site_name (Optional[str]): String of the embed's site name
description (Optional[str]): String of the embed's description
"""
def __init__(
self,
color: str = None,
title: str = None,
siteName: str = None,
description: str = None,
**kwargs
):
self.color = color
self.title = title
self.site_name = siteName
self.description = description
self.__dict__.update(kwargs)
def __str__(self):
if self.title is None:
return "None"
return self.title
class Version:
"""Object containing the current, stable, and upstream versions of Zipline
Attributes:
is_upstream (bool): Boolean of whether the current version is upstream (`trunk` branch)
update_to_type (str): String of the type of update available, one of 'stable' or 'upstream'
stable (str): String of the stable version
upstream (str): String of the upstream version
current (str): String of the current version
"""
def __init__(
self,
isUpstream: bool,
updateToType: str,
versions: {dict}
):
self.is_upstream = isUpstream
self.update_to_type = updateToType
self._versions = versions
self.stable = self._versions['stable']
self.upstream = self._versions['upstream']
self.current = self._versions['upstream']
def __str__(self):
return self.current