# -*- 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"""
Hi,
Thank you for choosing Mie Mapan. Use the following OTP to complete your request. This OTP is valid for 15 minutes.
Regards,
Mie Mapan Team
Mie Mapan Inc