diff --git a/classquiz/cache.py b/classquiz/cache.py index 21680cf..ba085ef 100644 --- a/classquiz/cache.py +++ b/classquiz/cache.py @@ -12,14 +12,14 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]: if criteria == "email": try: - res = await User.objects.get(email=content) + res = await User.objects.get(email=content, verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) return res elif criteria == "username": try: - res = await User.objects.get(username=content) + res = await User.objects.get(username=content, verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) @@ -27,7 +27,7 @@ async def cache_account(criteria: str, content: str) -> Union[User, None]: elif criteria == "id": try: - res = await User.objects.get(id=uuid.UUID(content)) + res = await User.objects.get(id=uuid.UUID(content), verified=True) except ormar.exceptions.NoMatch: return None await insert_into_redis(res, content) diff --git a/classquiz/emails/__init__.py b/classquiz/emails/__init__.py index 32cf08d..35af7a7 100644 --- a/classquiz/emails/__init__.py +++ b/classquiz/emails/__init__.py @@ -1,4 +1,40 @@ +from jinja2 import Environment, PackageLoader, select_autoescape +from classquiz.db.models import User + +from email.mime.multipart import MIMEMultipart +from classquiz.config import settings +from email.mime.text import MIMEText +import smtplib +import ssl + +jinja = Environment( + loader=PackageLoader('classquiz.emails', 'templates'), + autoescape=select_autoescape(['html', 'xml']), + enable_async=True) + + +def _sendMail(template: str, to: str, subject: str): + msg = MIMEMultipart('alternative') + msg['Subject'] = subject + msg['From'] = settings.mail_address + msg['To'] = to + msg.attach(MIMEText(template, 'html')) + context = ssl.SSLContext(ssl.PROTOCOL_TLS) + server = smtplib.SMTP(host=settings.mail_server, port=settings.mail_port) + server.ehlo() + server.starttls(context=context) + server.ehlo() + server.login(settings.mail_username, settings.mail_password) + server.sendmail(settings.mail_address, to, msg.as_string()) async def send_mail(email: str): - pass \ No newline at end of file + user = await User.objects.get_or_none(email=email, verified=False) + if user is None: + raise ValueError('User not found') + template = jinja.get_template('register.jinja2') + template = await template.render_async( + base_url=settings.root_address, + token=user.verify_key + ) + _sendMail(template=template, to=email, subject='Verify your email') diff --git a/classquiz/emails/templates/footer.jinja2 b/classquiz/emails/templates/footer.jinja2 new file mode 100644 index 0000000..732e987 --- /dev/null +++ b/classquiz/emails/templates/footer.jinja2 @@ -0,0 +1,24 @@ + +
|
+ + 💌 Send with love by Mawoka + |
+
|
- - 💌 Send with love by Mawoka - |
-