***model.py***
from odoo import api, fields, modelsfrom odoo.exceptions import ValidationErrorimport re
class UrlRedirect(models.Model): _name = 'url.redirect' _description = "Url redirect"
name = fields.Char(string='Name', required=True)
source_url = fields.Char(string='Identifier', index=True)
destination_url = fields.Char(string="URL to redirect")
active = fields.Boolean(default=True)
visit_count = fields.Integer(string='Visit Count', default=0)
visitor_id = fields.Many2one('res.users', string='Partner')
source_url_with_prefix = fields.Char(string='Identifier with Prefix', compute="_compute_source_url_with_prefix")
@api.depends('source_url') def _compute_source_url_with_prefix(self): for record in self: if record.source_url and not record.source_url.startswith('r-'): record.source_url_with_prefix = 'r-' + record.source_url else: record.source_url_with_prefix = record.source_url
@api.constrains('source_url') def _check_source_url(self): for record in self: if not re.match(r'^[a-zA-Z0-9\-]+$', record.source_url): raise ValidationError("The source URL can only contain a-z, A-Z, 0-9, and hyphens.")
***controller.py***
from odoo import http
from odoo.http import request
import re
def is_valid_url(source_url):
import re
return re.match(r'^[a-zA-Z0-9\-]+$', source_url)
class URLRedirectController(http.Controller):
@http.route('/', type='http', auth='public', website=True)
def url_redirect(self, source_url):
if not is_valid_url(source_url):
return "Invalid URL. Only letters, digits, and hyphens are allowed."
try:
redirect_record = request.env['url.redirect'].search([('source_url', '=', source_url)], limit=1)
if redirect_record:
redirect_record.visit_count += 1
destination_url = redirect_record.destination_url
return request.redirect(destination_url, local=False)
else:
return request.not_found()
except Exception as e:
return str(e)
else:
return request.not_found()
the thing i want to do is when i enter the source URL in search bar i should be redirected to the destination URL that is working properly but i also want to count the number of visits on that specific URL which i took it as a string but when i try to enter the URL and successfully redirected to destination URL visitor counter increases by 2 instead of 1 this is probably happening because of the destination URL is also type of string so to avoid this thing i tried to added prefix r- before / so i replaced first line like this """@http.route('/r-', type='http', auth='public', website=True)""" but now i have to manually enter the r-prefix whenever i try to enter source URL and fullfill the purpose i previously did, is there anyway by which i can always get prefix r- in the url even if i just enter some string (for example """/shop""" should be automatically converted into """/r-shop""") and also i should be redirected to the destination location. ***model.py***
from odoo import api, fields, modelsfrom odoo.exceptions import ValidationErrorimport re
class UrlRedirect(models.Model): _name = 'url.redirect' _description = "Url redirect"
name = fields.Char(string='Name', required=True) website_id = fields.Many2one('website', string="Website", ondelete='cascade', index=True) source_url = fields.Char(string='Identifier', index=True) destination_url = fields.Char(string="URL to redirect") # route_id = fields.Many2one('website.route') active = fields.Boolean(default=True) visit_count = fields.Integer(string='Visit Count', default=0) visitor_id = fields.Many2one('res.users', string='Partner') source_url_with_prefix = fields.Char(string='Identifier with Prefix', compute="_compute_source_url_with_prefix")
@api.depends('source_url') def _compute_source_url_with_prefix(self): for record in self: if record.source_url and not record.source_url.startswith('r-'): record.source_url_with_prefix = 'r-' + record.source_url else: record.source_url_with_prefix = record.source_url
@api.constrains('source_url') def _check_source_url(self): for record in self: if not re.match(r'^[a-zA-Z0-9\-]+$', record.source_url): raise ValidationError("The source URL can only contain a-z, A-Z, 0-9, and hyphens.")
***controller.py***
from odoo import http
from odoo.http import request
import re
def is_valid_url(source_url):
import re
return re.match(r'^[a-zA-Z0-9\-]+$', source_url)
class URLRedirectController(http.Controller):
@http.route('/', type='http', auth='public', website=True)
def url_redirect(self, source_url):
if not is_valid_url(source_url):
return "Invalid URL. Only letters, digits, and hyphens are allowed."
try:
redirect_record = request.env['url.redirect'].search([('source_url', '=', source_url)], limit=1)
if redirect_record:
redirect_record.visit_count += 1
destination_url = redirect_record.destination_url
return request.redirect(destination_url, local=False)
else:
return request.not_found()
except Exception as e:
return str(e)
else:
return request.not_found()
the thing i want to do is when i enter the source URL in search bar i should be redirected to the destination URL that is working properly but i also want to count the number of visits on that specific URL which i took it as a string but when i try to enter the URL and successfully redirected to destination URL visitor counter increases by 2 instead of 1 this is probably happening because of the destination URL is also type of string so to avoid this thing i tried to added prefix r- before / so i replaced first line like this """@http.route('/r-', type='http', auth='public', website=True)""" but now i have to manually enter the r-prefix whenever i try to enter source URL and fullfill the purpose i previously did, is there anyway by which i can always get prefix r- in the url even if i just enter some string (for example """/shop""" should be automatically converted into """/r-shop""") and also i should be redirected to the destination location.