Skip to content

Commit

Permalink
🎉 partner_telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
yelizariev committed May 19, 2024
1 parent da6f707 commit 13ca571
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 0 deletions.
3 changes: 3 additions & 0 deletions partner_telegram/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# WIP

TODO: we can't open telegram contact by url if we have Telegram ID only
2 changes: 2 additions & 0 deletions partner_telegram/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License MIT (https://opensource.org/licenses/MIT).
from . import models
23 changes: 23 additions & 0 deletions partner_telegram/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>
# License MIT (https://opensource.org/licenses/MIT).

{
"name": "Telegram Contact Field",
"summary": """Join the Amazing 😍 Community ⤵️""",
"category": "VooDoo ✨ Magic",
"version": "16.0.1.0.0",
"author": "Ivan Kropotkin",
"support": "[email protected]",
"website": "https://sync_studio.t.me/",
"license": "Other OSI approved licence", # MIT
"data": [
"views/res_partner_views.xml",
],
"assets": {
"web.assets_backend": [
"partner_telegram/static/src/js/telegram_widget.js",
"partner_telegram/static/src/xml/telegram_widget.xml",
],
},
"installable": True,
}
2 changes: 2 additions & 0 deletions partner_telegram/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License MIT (https://opensource.org/licenses/MIT).
from . import res_partner
21 changes: 21 additions & 0 deletions partner_telegram/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>
import re

from odoo import _, api, exceptions, fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

telegram = fields.Char(string="Telegram")

@api.constrains("telegram")
def _check_telegram_format(self):
telegram_format = re.compile(r"^(\d+|@\w+)$")
for record in self:
if record.telegram and not telegram_format.match(record.telegram):
raise exceptions.ValidationError(
_(
"Telegram contact must be a number (ID) or a string prefixed with '@' (username)."
)
)
Binary file added partner_telegram/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions partner_telegram/static/src/js/telegram_widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @odoo-module **/
/** Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev> **/

import { EmailField } from "@web/views/fields/email/email_field";
import { registry } from "@web/core/registry";

class TelegramField extends EmailField {
_formatValue(value) {
if (value.startsWith("@")) {
return `https://t.me/${value.substring(1)}`;
}
return `tg://user?id=${value}`;
}
}

TelegramField.template = "partner_contact.TelegramField";

class FormTelegramField extends TelegramField {}
FormTelegramField.template = "partner_contact.FormTelegramField";

registry.category("fields").add("telegram", TelegramField);
registry.category("fields").add("form.telegram", FormTelegramField);
49 changes: 49 additions & 0 deletions partner_telegram/static/src/xml/telegram_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>
License MIT (https://opensource.org/licenses/MIT). -->
<templates xml:space="preserve">
<t t-name="partner_contact.TelegramField" owl="1">
<t t-if="props.readonly">
<div class="d-grid">
<a
class="o_form_uri o_text_overflow"
t-on-click.stop=""
t-att-href="props.value ? 'https://t.me/'+props.value.substring(1) : undefined"
t-esc="props.value || ''"
/>
</div>
</t>
<t t-else="">
<div class="d-inline-flex w-100">
<input
class="o_input"
t-att-id="props.id"
type="text"
autocomplete="off"
t-att-placeholder="props.placeholder"
t-att-required="props.required"
t-ref="input"
/>
</div>
</t>
</t>
<t
t-name="partner_contact.FormTelegramField"
t-inherit="partner_contact.TelegramField"
t-inherit-mode="primary"
>
<xpath expr="//input" position="after">
<a
t-if="props.value"
t-att-href="'https://t.me/'+props.value.substring(1)"
class="ms-3 d-inline-flex align-items-center"
>
<i
class="fa fa-telegram"
data-tooltip="Open in Telegram"
aria-label="Open in Telegram"
/>
</a>
</xpath>
</t>
</templates>
15 changes: 15 additions & 0 deletions partner_telegram/views/res_partner_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>
License MIT (https://opensource.org/licenses/MIT). -->
<odoo>
<record id="view_res_partner_form_telegram" model="ir.ui.view">
<field name="name">res.partner.form.telegram</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='website']" position="before">
<field name="telegram" widget="telegram" />
</xpath>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions setup/partner_telegram/odoo/addons/partner_telegram
6 changes: 6 additions & 0 deletions setup/partner_telegram/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 13ca571

Please sign in to comment.