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 6e39e22
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 0 deletions.
11 changes: 11 additions & 0 deletions partner_telegram/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Telegram Contact Field

.. image:: static/description/icon.png

This module adds `telegram fields<models/res_partner.py>`_ to the partner form.

It works similarly to the `email` widget and opens the Telegram app when clicking the Telegram icon.

The Telegram value can be either a username or a mobile phone number prefixed with `+`.

An additional field, `telegram_ID`, is used for technical purposes and doesn't affect the partner form.
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
24 changes: 24 additions & 0 deletions partner_telegram/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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",
"partner_telegram/static/src/scss/telegram_widget.scss",
],
},
"installable": True,
}
4 changes: 4 additions & 0 deletions partner_telegram/doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`1.0.0`
-------

- **Init version**
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
38 changes: 38 additions & 0 deletions partner_telegram/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>

from odoo import api, fields, models


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

telegram_ID = fields.Char()
telegram = fields.Char(
string="Telegram", compute="_compute_telegram", inverse="_inverse_telegram"
)
telegram_username = fields.Char()
telegram_mobile = fields.Char()
telegram_url = fields.Char(compute="_compute_telegram")

@api.depends("telegram_username", "telegram_mobile")
def _compute_telegram(self):
for record in self:
if record.telegram_username:
record.telegram_url = f"https://t.me/{record.telegram_username}"
record.telegram = record.telegram_username
elif record.telegram_mobile:
record.telegram_url = f"https://t.me/{record.telegram_mobile}"
record.telegram = record.telegram_mobile
else:
record.telegram_url = ""

def _inverse_telegram(self):
for record in self:
value = record.telegram
if value.startswith("@"):
value = value[1:]
elif value.startswith("+"):
value = value.replace("-", "").replace(" ", "")
elif value.startswith("https://t.me/"):
value = value[len("https://t.me/") :]
record.telegram_mobile = value
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.
15 changes: 15 additions & 0 deletions partner_telegram/static/src/js/telegram_widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @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 {}

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);
7 changes: 7 additions & 0 deletions partner_telegram/static/src/scss/telegram_widget.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body:not(.o_touch_device) .o_field_telegram {
&:not(:hover):not(:focus-within) {
& input:not(:hover) ~ a {
display: none !important;
}
}
}
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.record.data.telegram_url"
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="props.record.data.telegram_url"
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>
17 changes: 17 additions & 0 deletions partner_telegram/views/res_partner_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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" />
<field name="telegram_url" invisible="1" />
<field name="telegram_username" invisible="1" />
</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 6e39e22

Please sign in to comment.