Skip to content

Commit

Permalink
[NEW] lcc_wallet_qr: create add-on
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphan Sainléger committed Mar 6, 2024
1 parent ad413f3 commit 8eb7de8
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lcc_wallet_qr/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
=============
lcc_wallet_qr
=============

Module to generate a QR code containing the wallet information
It's part of Lokavaluto Project (https://lokavaluto.fr)

Installation
============

Just install lcc_wallet_qr, all dependencies will be installed by default.

Configuration
=============

No configuration needed on this addon.

Usage
=====

To generate the QR code of a wallet, go on the form view of the wallet, anc click on the button "Generate QR".
You will be able to download a pdf page containing the QR code, and you will find the PNG image in the "QR code" notebook page.

Known issues / Roadmap
======================

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/Lokavaluto/lokavaluto-addons/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Images
------

* Lokavaluto: `Icon <https://lokavaluto.fr/web/image/res.company/1/logo?unique=f3db262>`_.

Contributors
------------

* Stéphan SAINLEGER <https://github.com/stephansainleger>
* Nicolas JEUDY <https://github.com/njeudy>
* Lokavaluto Teams

Funders
-------

The development of this module has been financially supported by:

* Lokavaluto (https://lokavaluto.fr)
* Mycéliandre (https://myceliandre.fr)
* Elabore (https://elabore.coop)

Maintainer
----------

This module is maintained by LOKAVALUTO.

LOKAVALUTO, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and ecosystem for local complementary currency organizations.
2 changes: 2 additions & 0 deletions lcc_wallet_qr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
20 changes: 20 additions & 0 deletions lcc_wallet_qr/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "lcc_wallet_qr",
"summary": "Generation of a QR code for LCC numeric wallets",
"author": "Lokavaluto",
"website": "https://lokavaluto.fr",
"category": "Website",
"version": "12.0.1.0.0",
# any module necessary for this one to work correctly
"depends": [
"base",
"lcc_lokavaluto_app_connection",
],
# always loaded
"data": [
"views/lcc_backend.xml",
"data/res_partner_backend_data.xml",
],
# only loaded in demonstration mode
"demo": [],
}
14 changes: 14 additions & 0 deletions lcc_wallet_qr/data/res_partner_backend_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record id="ir_cron_generate_missing_wallet_qr_codes" model="ir.cron">
<field name="name">Wallet: generate missing QR codes</field>
<field name="model_id" ref="lcc_lokavaluto_app_connection.model_res_partner_backend" />
<field name="state">code</field>
<field name="code">model._cron_generate_missing_qr()</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="active">0</field>
</record>
</data>
</odoo>
2 changes: 2 additions & 0 deletions lcc_wallet_qr/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import res_partner_backend
68 changes: 68 additions & 0 deletions lcc_wallet_qr/models/res_partner_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-

try:
import qrcode
except ImportError:
qrcode = None
try:
import base64
except ImportError:
base64 = None
from io import BytesIO

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


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

qr = fields.Binary(string="Wallet QR code", store=True, copy=False)

@api.model
def create(self, vals):
res = super(ResPartnerBackend, self).create(vals)
res.generate_qr()
return res

def _get_qr_content(self):
self.ensure_one()
content = {
"rp": self.partner_id.id,
"rpb": self.name,
}
return content

def generate_qr(self):
if qrcode and base64:
for record in self:
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
qr_content = record._get_qr_content()

qr.add_data(qr_content)
qr.make(fit=True)

img = qr.make_image()
temp = BytesIO()
img.save(temp, format="PNG")
qr_image = base64.b64encode(temp.getvalue())
record.write(
{
"qr": qr_image,
}
)
else:
raise UserError(
_(
"Necessary requirements are not satisfied - Need qrcode and base64 Python libraries"
)
)

@api.model
def _cron_generate_missing_qr(self):
self.search([("qr", "=", False)]).generate_qr()
18 changes: 18 additions & 0 deletions lcc_wallet_qr/views/lcc_backend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="lcc_backend_form_qr_view" model="ir.ui.view">
<field name="name">lcc.backend.qr.form.view</field>
<field name="model">res.partner.backend</field>
<field name="inherit_id" ref="lcc_lokavaluto_app_connection.lcc_backend_form_view" />
<field name="arch" type="xml">
<xpath expr="//group[@name='general_info']" position="before">
<button name="generate_qr" type="object" class="btn-box ml-3" icon="fa-qrcode">
Generate new QR
</button>
</xpath>
<xpath expr="//field[@name='status']" position="after">
<field name="qr" readonly="True" />
</xpath>
</field>
</record>
</odoo>

0 comments on commit 8eb7de8

Please sign in to comment.