metatroncubeswdev db50c3f15c feat(event_qr_ticketing): signed QR tickets + mobile check-in (Phase 2)
Extends stock event.registration with a signed QR ticket system, built to
complement rather than duplicate Odoo 19's existing barcode/badge
infrastructure. ticket_ref ('TIX-{event}-{seq}') and a QR code encoding
"ticket_ref|hmac_token" are added; the HMAC is signed with Odoo's own
per-database secret (ir.config_parameter 'database.secret', the same
mechanism core uses for password-reset tokens), so a copied/edited
ticket_ref without the matching signature is rejected as forged. A "Event
Ticket (QR)" PDF report is auto-attached to the core registration
confirmation email by adding it to event.event_subscription's
report_template_ids - no override of core mail-sending logic needed.

Adds the missing piece core doesn't provide: a mobile-friendly staff
check-in page at /event/checkin (gated on event.group_event_registration_desk),
built as a v19 "Interaction" (registry.category("public.interactions"),
the current replacement for legacy publicWidget) with manual ticket-ref
entry always available and camera scanning via the browser's native
BarcodeDetector API - avoiding a third-party CDN dependency and its
security/offline-reliability tradeoffs. Guards against forged tokens and
double check-in; a live per-event registered-vs-checked-in dashboard.

Two more real Odoo 19 surprises hit here: event.event has no more `state`
field at all (replaced by a stage_id/event.stage kanban system - my
check-in page's event picker now filters by date_end instead), and a
route type='json' should be type='jsonrpc' (json still works but is
deprecated).

Verified against a live Odoo 19 + Postgres 16 container: 9/9 tests pass
(ticket generation/uniqueness, valid/duplicate/forged/tampered token
handling, manual lookup), plus a full manual live run over HTTP - created
an event and registration via JSON-RPC, confirmed ticket_ref generation,
authenticated a session, hit /event/checkin/scan for a real check-in and
duplicate rejection, confirmed dashboard counts update, and loaded the
actual /event/checkin page (title, camera-button markup, and our JS
present in the served frontend bundle).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-17 21:29:40 -04:00

109 lines
4.0 KiB
Python

import base64
import hashlib
import hmac
import io
import qrcode
from odoo import api, fields, models
TICKET_REF_SEQUENCE_CODE = 'event_qr_ticketing.ticket_ref'
class EventRegistration(models.Model):
_inherit = 'event.registration'
ticket_ref = fields.Char(string='Ticket Reference', readonly=True, copy=False, index=True)
ticket_qr = fields.Binary(string='Ticket QR', compute='_compute_ticket_qr')
checked_in = fields.Boolean(string='Checked In', default=False, copy=False, readonly=True)
check_in_time = fields.Datetime(string='Check-in Time', readonly=True, copy=False)
check_in_user_id = fields.Many2one('res.users', string='Checked In By', readonly=True, copy=False)
_ticket_ref_uniq = models.Constraint('unique(ticket_ref)', 'Ticket reference must be unique.')
@api.model_create_multi
def create(self, vals_list):
registrations = super().create(vals_list)
for registration in registrations:
if not registration.ticket_ref:
registration.ticket_ref = registration._generate_ticket_ref()
return registrations
def _generate_ticket_ref(self):
self.ensure_one()
seq_number = self.env['ir.sequence'].next_by_code(TICKET_REF_SEQUENCE_CODE) or '1'
return f"TIX-{self.event_id.id}-{seq_number}"
def _get_signing_secret(self):
secret = self.env['ir.config_parameter'].sudo().get_param('database.secret')
return secret.encode()
def _compute_ticket_token(self):
"""HMAC of event_id:registration_id:ticket_ref, signed with the per-database secret."""
self.ensure_one()
payload = f"{self.event_id.id}:{self.id}:{self.ticket_ref}".encode()
return hmac.new(self._get_signing_secret(), payload, hashlib.sha256).hexdigest()[:16]
def _get_ticket_qr_value(self):
self.ensure_one()
return f"{self.ticket_ref}|{self._compute_ticket_token()}"
def _compute_ticket_qr(self):
for registration in self:
if not registration.ticket_ref:
registration.ticket_qr = False
continue
qr = qrcode.QRCode(box_size=6, border=2)
qr.add_data(registration._get_ticket_qr_value())
qr.make(fit=True)
image = qr.make_image(fill_color='black', back_color='white')
buffer = io.BytesIO()
image.save(buffer, format='PNG')
registration.ticket_qr = base64.b64encode(buffer.getvalue())
@api.model
def _verify_scanned_token(self, scanned_value):
"""Verify a scanned QR payload ('ticket_ref|token').
Returns (status, registration) where status is one of:
'ok', 'already', 'invalid' (bad/forged token), 'not_found'.
"""
parts = (scanned_value or '').split('|')
if len(parts) != 2 or not parts[0]:
return 'invalid', self.env['event.registration']
ticket_ref, token = parts
registration = self.search([('ticket_ref', '=', ticket_ref)], limit=1)
if not registration:
return 'not_found', registration
expected_token = registration._compute_ticket_token()
if not hmac.compare_digest(expected_token, token):
return 'invalid', registration
if registration.checked_in:
return 'already', registration
return 'ok', registration
@api.model
def _lookup_by_ticket_ref(self, ticket_ref):
"""Manual entry lookup: staff-trusted, no signature check required."""
registration = self.search([('ticket_ref', '=', ticket_ref)], limit=1)
if not registration:
return 'not_found', registration
if registration.checked_in:
return 'already', registration
return 'ok', registration
def action_check_in(self):
self.ensure_one()
self.write({
'checked_in': True,
'check_in_time': fields.Datetime.now(),
'check_in_user_id': self.env.user.id,
})
if self.state != 'done':
self.action_set_done()
return True