i18n email templates

This commit is contained in:
tonyrewin 2022-10-21 08:47:58 +03:00
parent 27cb1c0a69
commit efe60cb0c8
3 changed files with 14 additions and 10 deletions

View File

@ -5,12 +5,14 @@ from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
api_url = "https://api.mailgun.net/v3/%s/messages" % MAILGUN_DOMAIN
noreply = "discours.io <noreply@%s>" % MAILGUN_DOMAIN
async def send_auth_email(user, token):
async def send_auth_email(user, token, lang="ru"):
try:
to = "%s <%s>" % (user.name, user.email)
# TODO: i18n
subject = "Confirm email"
template = "email_confirmation_ru"
if lang not in ['ru', 'en']:
lang = 'ru'
template = "email_confirmation_" + lang
response = requests.post(
api_url,

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from urllib.parse import quote_plus
from datetime import datetime
@ -91,7 +93,7 @@ def generate_unique_slug(name):
c += 1
if not user:
unique_slug = slug
return quote_plus(unique_slug)
return quote_plus(unique_slug).replace('+', '-')
@mutation.field("registerUser")
@ -120,7 +122,7 @@ async def register(_, _info, email: str, password: str = "", name: str = ""):
@mutation.field("sendLink")
async def auth_send_link(_, _info, email):
async def auth_send_link(_, _info, email, lang="ru"):
"""send link with confirm code to email"""
with local_session() as session:
user = session.query(User).filter(User.email == email).first()
@ -128,12 +130,12 @@ async def auth_send_link(_, _info, email):
raise ObjectNotExist("User not found")
else:
token = await TokenStorage.create_onetime(user)
await send_auth_email(user, token)
await send_auth_email(user, token, lang)
return user
@query.field("signIn")
async def login(_, _info, email: str, password: str = ""):
async def login(_, _info, email: str, password: str = "", lang: str = "ru"):
with local_session() as session:
orm_user = session.query(User).filter(User.email == email).first()
@ -145,7 +147,7 @@ async def login(_, _info, email: str, password: str = ""):
if not password:
print(f"[auth] send confirm link to {email}")
token = await TokenStorage.create_onetime(orm_user)
await send_auth_email(orm_user, token)
await send_auth_email(orm_user, token, lang)
# FIXME: not an error, warning
return {"error": "no password, email link was sent"}

View File

@ -155,7 +155,7 @@ type Mutation {
# auth
refreshSession: AuthResult!
registerUser(email: String!, password: String, name: String): AuthResult!
sendLink(email: String!): Result!
sendLink(email: String!, lang: String): Result!
confirmEmail(code: String!): AuthResult!
# shout
@ -212,7 +212,7 @@ type Query {
# auth
isEmailUsed(email: String!): Boolean!
signIn(email: String!, password: String): AuthResult!
signIn(email: String!, password: String, lang: String): AuthResult!
signOut: AuthResult!
# profile