restore password

This commit is contained in:
Diego Calvo
2021-03-31 10:14:36 +02:00
parent 373e892463
commit 7399b6a480
4 changed files with 725 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ urlpatterns = [
path('api/v1/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path('api/v1/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/v1/token/verify/', TokenVerifyView.as_view(), name='token_verify'), path('api/v1/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
path('api/v1/user/change_password/<int:pk>/', core_views.ChangeUserPasswordView.as_view(), name="change-password"), path('api/v1/user/change_password/<int:pk>/', core_views.ChangeUserPasswordView.as_view(), name="change-password"),
path('api/v1/forgotten_password/', core_views.forgotten_password, name="forgotten-password"),
path('api/v1/admin_stats/', core_views.admin_stats, name='admin-stats'), path('api/v1/admin_stats/', core_views.admin_stats, name='admin-stats'),
path('api/v1/load_coops/', core_views.load_coop_managers, name='coop-loader'), path('api/v1/load_coops/', core_views.load_coop_managers, name='coop-loader'),
path('api/v1/load_products/', product_views.load_coop_products, name='product-loader'), path('api/v1/load_products/', product_views.load_coop_products, name='product-loader'),

View File

@@ -1,4 +1,6 @@
import logging import logging
import random
import string
from io import BytesIO from io import BytesIO
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@@ -199,3 +201,7 @@ def coop_loader(csv_reader, request=None):
logging.error(f"Could not parse {row}") logging.error(f"Could not parse {row}")
return coop_counter, user_counter return coop_counter, user_counter
def generate_password(length):
result_str = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(length))
return result_str

View File

@@ -15,6 +15,8 @@ from django.db import IntegrityError
from django.contrib.gis.geos import Point, GEOSGeometry from django.contrib.gis.geos import Point, GEOSGeometry
from django.shortcuts import redirect from django.shortcuts import redirect
from django.conf import settings from django.conf import settings
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from rest_framework import status from rest_framework import status
from rest_framework import viewsets from rest_framework import viewsets
@@ -203,6 +205,42 @@ def activate_user(request, uidb64, token):
else: else:
return Response({"error": f"Tu token de verificacion no coincide con ningún usuario registrado"}, status=status.HTTP_406_NOT_ACCEPTABLE) return Response({"error": f"Tu token de verificacion no coincide con ningún usuario registrado"}, status=status.HTTP_406_NOT_ACCEPTABLE)
@api_view(['POST'])
@permission_classes([AllowAny,])
def forgotten_password(request):
"""Set new password for registered user and send email
"""
if 'email' not in request.data:
return Response({"error": "Missing parameter: email"}, status=400)
email = request.data['email']
user = User.objects.get(email=email)
if user:
try:
password = utils.generate_password(12)
print(password)
user.set_password(password)
user.save()
except:
return Response({'error': f"Could not set new password [{str(type(e))}]: {str(e)}"}, status=500)
try:
message = render_to_string('forgotten_password.html', {
'user': user,
'password': password,
})
subject = "[latienda.coop] Contraseña restablecida"
email = EmailMessage(subject, message, to=[email])
email.content_subtype = "html"
email.send()
logging.info(f"Email sent to {email}")
except Exception as e:
return Response({'error': f"Could not send emails [{str(type(e))}]: {str(e)}"}, status=500)
else:
return Response({'error': f"This email has no user related to it"}, status=404)
return Response()
@api_view(['GET',]) @api_view(['GET',])
@permission_classes([IsAdminUser,]) @permission_classes([IsAdminUser,])

View File

@@ -0,0 +1,680 @@
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
>
<head>
<title> </title>
<!--[if !mso]><!-- -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!--<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
#outlook a {
padding: 0;
}
body {
margin: 0;
padding: 0;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
table,
td {
border-collapse: collapse;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}
img {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
-ms-interpolation-mode: bicubic;
}
p {
display: block;
margin: 13px 0;
}
</style>
<!--[if mso]>
<xml>
<o:OfficeDocumentSettings>
<o:AllowPNG />
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml>
<![endif]-->
<!--[if lte mso 11]>
<style type="text/css">
.mj-outlook-group-fix {
width: 100% !important;
}
</style>
<![endif]-->
<!--[if !mso]><!-->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap"
rel="stylesheet"
type="text/css"
/>
<link
href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap"
rel="stylesheet"
type="text/css"
/>
<style type="text/css">
@import url(https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap);
@import url(https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap);
</style>
<!--<![endif]-->
<style type="text/css">
@media only screen and (min-width: 480px) {
.mj-column-per-100 {
width: 100% !important;
max-width: 100%;
}
.mj-column-per-50 {
width: 50% !important;
max-width: 50%;
}
}
</style>
<style type="text/css">
@media only screen and (max-width: 480px) {
table.mj-full-width-mobile {
width: 100% !important;
}
td.mj-full-width-mobile {
width: auto !important;
}
}
</style>
</head>
<body style="background-color: #8cead8">
<div style="background-color: #8cead8">
<!--[if mso | IE]>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div
style="
background: #ffffff;
background-color: #ffffff;
margin: 0px auto;
max-width: 600px;
"
>
<table
align="center"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
background: #ffffff;
background-color: #ffffff;
width: 100%;
"
>
<tbody>
<tr>
<td
style="
direction: ltr;
font-size: 0px;
padding: 20px 0;
padding-bottom: 20px;
padding-top: 20px;
text-align: center;
"
>
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:600px;"
>
<![endif]-->
<div
class="mj-column-per-100 mj-outlook-group-fix"
style="
font-size: 0px;
text-align: left;
direction: ltr;
display: inline-block;
vertical-align: top;
width: 100%;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="vertical-align: top"
width="100%"
>
<tr>
<td
align="center"
style="
font-size: 0px;
padding: 10px 25px;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 0px;
word-break: break-word;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
border-collapse: collapse;
border-spacing: 0px;
"
>
<tbody>
<tr>
<td
style="
width: 250px;
"
>
<a
href="https://latienda.coop/"
target="_blank"
>
<img
alt=""
height="auto"
width="25"
align="left"
src="https://latienda.coop/_nuxt/img/latienda-logo.dea6549.png"
style="
border: none;
display: block;
outline: none;
text-decoration: none;
height: auto;
width: 100%;
font-size: 13px;
"
/>
</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div
style="
background: #ffffff;
background-color: #ffffff;
margin: 0px auto;
max-width: 600px;
"
>
<table
align="center"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
background: #ffffff;
background-color: #ffffff;
width: 100%;
"
>
<tbody>
<tr>
<td
style="
direction: ltr;
font-size: 0px;
padding: 20px 0;
padding-bottom: 0px;
padding-top: 0;
text-align: center;
"
>
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:600px;"
>
<![endif]-->
<div
class="mj-column-per-100 mj-outlook-group-fix"
style="
font-size: 0px;
text-align: left;
direction: ltr;
display: inline-block;
vertical-align: top;
width: 100%;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="vertical-align: top"
width="100%"
>
<tr>
<td
align="center"
style="
font-size: 0px;
padding: 10px 25px;
padding-top: 28px;
padding-right: 25px;
padding-bottom: 18px;
padding-left: 25px;
word-break: break-word;
"
>
<div
style="
font-family: Poppins,
Arial, sans-serif;
font-size: 20px;
font-weight: bold;
line-height: 1;
text-align: center;
color: #374493;
"
>
Contraseña restablecida
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div
style="
background: #ffffff;
background-color: #ffffff;
margin: 0px auto;
max-width: 600px;
"
>
<table
align="center"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
background: #ffffff;
background-color: #ffffff;
width: 100%;
"
>
<tbody>
<tr>
<td
style="
direction: ltr;
font-size: 0px;
padding: 20px 0;
padding-bottom: 5px;
padding-top: 0;
text-align: center;
"
>
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:600px;"
>
<![endif]-->
<div
class="mj-column-per-100 mj-outlook-group-fix"
style="
font-size: 0px;
text-align: left;
direction: ltr;
display: inline-block;
vertical-align: top;
width: 100%;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="vertical-align: top"
width="100%"
>
<tr>
<td
align="center"
style="
font-size: 0px;
padding: 10px 25px;
padding-right: 0px;
padding-left: 0px;
word-break: break-word;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
border-collapse: collapse;
border-spacing: 0px;
"
>
<tbody>
<tr>
<td
style="
width: 250px;
"
></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td
align="center"
width="50"
style="
font-size: 0px;
padding: 10px 25px;
padding-top: 28px;
padding-right: 25px;
padding-bottom: 28px;
padding-left: 25px;
word-break: break-word;
"
>
<p
style="
font-family: Noto Sans,
sans-serif;
font-size: 17px;
line-height: 1.5;
text-align: center;
color: #374493;
margin: 0 50px;
"
>
Hola {{ user.full_name }},
<br /><br />
Se ha restablecido la contraseña para tu usuario.
A partir de ahora puedes entrar en LaTienda.COOP con la siguiente contraseña:
<br/> <br />
<a href="{{verification_url}}">{{password}}</a>
<br />
</p>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div
style="
background: #ffffff;
background-color: #ffffff;
margin: 0px auto;
max-width: 600px;
"
>
<table
align="center"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="
background: #ffffff;
background-color: #ffffff;
width: 100%;
"
>
<tbody>
<tr>
<td
style="
direction: ltr;
font-size: 0px;
padding: 20px 0;
padding-bottom: 15px;
text-align: center;
"
>
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:300px;"
>
<![endif]-->
<div
class="mj-column-per-50 mj-outlook-group-fix"
style="
font-size: 0px;
text-align: left;
direction: ltr;
display: inline-block;
vertical-align: top;
width: 100%;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="vertical-align: top"
width="100%"
></table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<!--[if mso | IE]>
</td>
</tr>
</table>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div
style="
background: #ffffff;
background-color: #ffffff;
margin: 0px auto;
max-width: 600px;
"
>
<table
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="vertical-align: top"
width="100%"
>
<tr>
<td
align="center"
style="
font-size: 0px;
padding: 10px 25px;
padding-top: 20px;
padding-right: 25px;
padding-bottom: 20px;
padding-left: 25px;
word-break: break-word;
"
>
<div
style="
font-family: Poppins, Arial, sans-serif;
font-size: 15px;
line-height: 1;
text-align: center;
color: #374493;
"
>
2021 La Tienda.coop
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</div>
</body>
</html>