-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
624eeff
commit 424e9ed
Showing
12 changed files
with
217 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
from os import environ | ||
|
||
from db import client | ||
from exceptions import DatabaseDisabled, Forbidden, NotFound | ||
from nasse import Response, Request | ||
from nasse.models import Dynamic, Endpoint, Error, Login, Param, Return | ||
from nasse.utils.boolean import to_bool | ||
from translatepy.server.server import app | ||
|
||
from datetime import datetime | ||
|
||
from yuno.security.hash import Hasher | ||
from yuno.security.token import TokenManager | ||
from yuno.security.encrypt import AES | ||
|
||
hasher = Hasher() | ||
aes = AES(client, prefix="translatepy") | ||
token_manager = TokenManager(key=client, sign=client) | ||
|
||
base = Endpoint( | ||
section="Stars", | ||
errors=Error(name="DATABASE_DISABLED", description="When the server disabled any database interaction", code=501), | ||
login=Login(no_login=True) | ||
) | ||
|
||
|
||
if not to_bool(environ.get("TRANSLATEPY_DB_DISABLED", False)): | ||
stars = client.translatepy.stars | ||
else: | ||
stars = {} | ||
|
||
|
||
@app.route("/stars", description="Get all starred translations") | ||
def stars_handler(request: Request): | ||
if to_bool(environ.get("TRANSLATEPY_DB_DISABLED", False)): | ||
raise DatabaseDisabled | ||
|
||
query = stars.find({ | ||
"users.{hash}".format( | ||
hash=hasher.hash_string( | ||
"{ip}{salt}".format( | ||
ip=request.client_ip, | ||
salt=environ.get("TRANSLATEPY_SALT", "") | ||
) | ||
) | ||
): { | ||
"$exists": True | ||
} | ||
}) | ||
results = [] | ||
for document in query: | ||
result = document.copy() | ||
result["users"] = len(result["users"]) | ||
results.append(result) | ||
return Response( | ||
data={ | ||
"stars": results | ||
}, | ||
message="Here are your starred translations" | ||
) | ||
|
||
|
||
def TranslationToken(value: str): | ||
return token_manager.decode(value, encryption=aes) | ||
|
||
|
||
@app.route("/stars/<translation_id>", Endpoint( | ||
methods=["GET", "POST", "DELETE"], | ||
description={ | ||
"GET": "Get the stars for a translation", | ||
"POST": "Star a translation", | ||
"DELETE": "Unstar a translation" | ||
}, | ||
params=[ | ||
Param(name="token", description="The token to authenticate the translation", type=TranslationToken, methods="POST") | ||
], | ||
dynamics=[ | ||
Dynamic(name="translation_id", description="The ID of the translation to star", methods="POST"), | ||
Dynamic(name="translation_id", description="The ID of the translation to get", methods="GET"), | ||
Dynamic(name="translation_id", description="The ID of the translation to unstar", methods="DELETE"), | ||
], | ||
errors=[ | ||
Error(name="FORBIDDEN", description="You are not allowed to star this translation", code=403), | ||
Error(name="NOT_FOUND", description="The translation could not be found", code=404) | ||
] + base.errors, | ||
returns=[ | ||
Return(name="source", description="The source text", methods=["GET", "POST"]), | ||
Return(name="result", description="The result text", methods=["GET", "POST"]), | ||
Return(name="language", description="The translation languages", children=[ | ||
Return(name="source", description="The source language", methods=["GET", "POST"]), | ||
Return(name="dest", description="The destination language", methods=["GET", "POST"]) | ||
], methods=["GET", "POST"]), | ||
Return(name="users", description="The number of users who starred the translation", type=int, methods=["GET", "POST"]) | ||
] | ||
)) | ||
def stars__translation_id__(request: Request, method: str, translation_id: str, token: dict = None): | ||
if to_bool(environ.get("TRANSLATEPY_DB_DISABLED", False)): | ||
raise DatabaseDisabled | ||
|
||
current_ip_hash = hasher.hash_string( | ||
"{ip}{salt}".format( | ||
ip=request.client_ip, | ||
salt=environ.get("TRANSLATEPY_SALT", "") | ||
) | ||
) | ||
|
||
if method == "DELETE": | ||
stars.update({ | ||
"_id": translation_id # query | ||
}, { | ||
"$unset": { # command | ||
"users.{hash}".format(hash=current_ip_hash): "" | ||
} | ||
}) | ||
return "Removed the star" | ||
|
||
try: | ||
translation = stars[translation_id] | ||
except KeyError as err: | ||
raise NotFound("We couldn't find the given translation") from err | ||
|
||
if method == "POST": | ||
# token body | ||
# { | ||
# "sub": "user hash", | ||
# "source": "source text", | ||
# "result": "result text", | ||
# "language": { | ||
# "source": "source language", | ||
# "dest": "destination language" | ||
# } | ||
# } | ||
if current_ip_hash != token["sub"]: | ||
raise Forbidden("You are not allowed to star this translation") | ||
|
||
if len(translation.users) <= 0: | ||
translation.source = token["source"] | ||
translation.result = token["result"] | ||
translation.language = token["language"] | ||
|
||
translation.users[current_ip_hash] = datetime.utcnow() | ||
|
||
result = translation.copy() | ||
result["users"] = len(result["users"]) | ||
return Response(result) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from nasse.exceptions import NasseException | ||
|
||
|
||
class DatabaseDisabled(NasseException): | ||
STATUS_CODE = 501 | ||
MESSAGE = "The database is currently disabled on the server" | ||
EXCEPTION_NAME = "DATABASE_DISABLED" | ||
LOG = False | ||
|
||
|
||
class Forbidden(NasseException): | ||
STATUS_CODE = 403 | ||
MESSAGE = "You do not have the rights to access this endpoint" | ||
EXCEPTION_NAME = "FORBIDDEN" | ||
LOG = False | ||
|
||
|
||
class NotFound(NasseException): | ||
STATUS_CODE = 404 | ||
MESSAGE = "We couldn't find the resource you were looking for" | ||
EXCEPTION_NAME = "NOT_FOUND" | ||
LOG = False |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from datetime import datetime | ||
|
||
|
||
class Datetime(datetime): | ||
def __new__(self, *args, **kwargs) -> datetime: | ||
print(args, kwargs) | ||
if len(args) > 0 and isinstance(args[0], datetime): | ||
return args[0] | ||
return datetime(*args, **kwargs) |
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
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
424e9ed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
translate – ./
translatepy.vercel.app
translatep.vercel.app
translate-ruby.vercel.app
translate-animenosekai.vercel.app
translate-git-website-animenosekai.vercel.app