Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Procházet všechna odvětví
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Služby pro partnery
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

Controller

Odebírat

Get notified when there's activity on this post

This question has been flagged
urlcontrollersredirection
2998 Zobrazení
Avatar
Ravi Bhatt

***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. 

0
Avatar
Zrušit
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
change website product page URL
url redirection
Avatar
0
úno 21
3814
i want to access of querystring in controller Vyřešeno
url controllers odoo10
Avatar
Avatar
Avatar
3
čvc 19
16571
Odoo V10: Disable '?debug' in the url
debug url controllers
Avatar
0
bře 18
365
Open an url using python, without using return
models actions url controllers
Avatar
Avatar
1
čvc 21
5973
i want to access model objects in odoo controlle Vyřešeno
query url controllers odoo10
Avatar
Avatar
2
led 20
8354
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now