This commit is contained in:
Igor Lobanov 2022-10-21 00:05:37 +02:00
parent 3ebe067dc6
commit d34260e336
3 changed files with 19 additions and 23 deletions

View File

@ -4,26 +4,22 @@ 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
subject = "Confirm email"
tmplt = """<html><body>
Follow the <a href='%s'>link</a> to authorize
</body></html>
"""
async def send_auth_email(user, token):
try:
to = "%s <%s>" % (user.username, user.email)
url_with_token = "https://newapi.discours.io/confirm/" + token
to = "%s <%s>" % (user.name, user.email)
# TODO: i18n
subject = "Confirm email"
template = "email_confirmation_ru"
response = requests.post(
api_url,
auth=("api", MAILGUN_API_KEY),
data={
"from": noreply,
"to": to,
"subject": subject,
"html": tmplt % url_with_token,
},
data={"from": noreply,
"to": to,
"subject": subject,
"template": template,
"h:X-Mailgun-Variables": "{ \"token\": \"%s\" }" % token}
)
response.raise_for_status()
except Exception as e:

View File

@ -80,8 +80,8 @@ def create_user(user_dict):
return user
def generate_unique_slug(username):
slug = translit(username, "ru", reversed=True).replace(".", "-").lower()
def generate_unique_slug(name):
slug = translit(name, "ru", reversed=True).replace(".", "-").lower()
with local_session() as session:
c = 1
user = session.query(User).where(User.slug == slug).first()
@ -95,26 +95,26 @@ def generate_unique_slug(username):
@mutation.field("registerUser")
async def register(_, _info, email: str, password: str = "", username: str = ""):
async def register(_, _info, email: str, password: str = "", name: str = ""):
"""creates new user account"""
with local_session() as session:
user = session.query(User).filter(User.email == email).first()
if user:
raise OperationNotAllowed("User already exist")
else:
username = username or email.split("@")[0]
slug = generate_unique_slug(name)
user_dict = {
"email": email,
"username": username,
"slug": generate_unique_slug(username)
"username": email,
"name": name,
"slug": slug
}
if password:
user_dict["password"] = Password.encode(password)
user = create_user(user_dict)
if not password:
user = await auth_send_link(_, _info, email)
await auth_send_link(_, _info, email)
return {"user": user}

View File

@ -154,7 +154,7 @@ type Mutation {
# auth
refreshSession: AuthResult!
registerUser(email: String!, password: String, username: String): AuthResult!
registerUser(email: String!, password: String, name: String): AuthResult!
sendLink(email: String!): Result!
confirmEmail(code: String!): AuthResult!