mapan_loyalty_push/models/loyalty_verification_otp.py

98 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
import random
from datetime import datetime, timedelta
from odoo import api, fields, models
class LoyaltyVerificationOtp(models.Model):
_name = 'loyalty.verification.otp'
_description = 'Loyalty Verification OTP'
_order = 'create_date desc'
email = fields.Char(string='Email')
phone = fields.Char(string='Phone')
otp_code = fields.Char(string='OTP Code', required=True)
expiry_time = fields.Datetime(string='Expiry Time', required=True)
otp_type = fields.Selection([
('signup', 'Sign-up'),
('activation', 'Activation'),
('reset_password', 'Reset Password')
], string='OTP Type', required=True)
is_verified = fields.Boolean(string='Is Verified', default=False)
@api.model
def generate_otp(self, email=None, phone=None, otp_type=None):
if not otp_type:
raise ValueError("OTP type is required.")
# Generate a 6-digit random code
otp_code = str(random.randint(100000, 999999))
expiry_time = fields.Datetime.now() + timedelta(minutes=15)
# Create record
otp_record = self.sudo().create({
'email': email,
'phone': phone,
'otp_code': otp_code,
'expiry_time': expiry_time,
'otp_type': otp_type,
})
# Send Email
if email:
subject = "Verification Code - Mie Mapan"
if otp_type == 'reset_password':
subject = "Reset Password Verification Code - Mie Mapan"
elif otp_type == 'activation':
subject = "Account Activation Verification Code - Mie Mapan"
body_html = f"""
<div style="font-family: Helvetica, Arial, sans-serif; min-width: 1000px; overflow: auto; line-height: 2">
<div style="margin: 50px auto; width: 70%; padding: 20px 0">
<div style="border-bottom: 1px solid #eee">
<a href="" style="font-size: 1.4em; color: #ff5722; text-decoration: none; font-weight: 600">Mie Mapan</a>
</div>
<p style="font-size: 1.1em">Hi,</p>
<p>Thank you for choosing Mie Mapan. Use the following OTP to complete your request. This OTP is valid for 15 minutes.</p>
<h2 style="background: #ff5722; margin: 0 auto; width: max-content; padding: 0 10px; color: #fff; border-radius: 4px;">{otp_code}</h2>
<p style="font-size: 0.9em;">Regards,<br />Mie Mapan Team</p>
<hr style="border: none; border-top: 1px solid #eee" />
<div style="float: right; padding: 8px 0; color: #aaa; font-size: 0.8em; line-height: 1; font-weight: 300">
<p>Mie Mapan Inc</p>
</div>
</div>
</div>
"""
mail_values = {
'subject': subject,
'body_html': body_html,
'email_to': email,
'email_from': self.env.user.email or 'no-reply@miemapan.com',
}
# Send using sudo context
self.env['mail.mail'].sudo().create(mail_values).send()
return otp_record
@api.model
def verify_otp(self, otp_code, email=None, phone=None, otp_type=None):
if not otp_code:
return False
domain = [
('otp_code', '=', otp_code),
('otp_type', '=', otp_type),
('is_verified', '=', False),
('expiry_time', '>=', fields.Datetime.now())
]
if email:
domain.append(('email', '=', email))
if phone:
domain.append(('phone', '=', phone))
record = self.sudo().search(domain, order='create_date desc', limit=1)
if record:
record.write({'is_verified': True})
return True
return False