### Create, Read, and Search Odoo Course Records Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Demonstrates ORM operations for creating, reading, and searching records in the 'openacademy.course' model. Includes examples of SQL constraints for unique titles and title-description uniqueness. ```python # Create a course via the ORM (e.g., in a custom script or another module) Course = env['openacademy.course'] # Create python_course = Course.create({ 'name': 'Python Fundamentals', 'description': 'Introduction to Python programming.', 'responsible_id': env.ref('base.user_admin').id, }) # => openacademy.course(1,) # Read / search courses = Course.search([('responsible_id', '=', env.uid)]) for c in courses: print(c.name, c.responsible_id.name) # => Python Fundamentals Administrator # SQL constraints in action — duplicate name raises IntegrityError try: Course.create({'name': 'Python Fundamentals', 'description': 'Duplicate'}) except Exception as e: print(e) # => The course title must be unique # Title == description constraint try: Course.create({'name': 'Same Text', 'description': 'Same Text'}) except Exception as e: print(e) # => The title of the course should not be the description ``` -------------------------------- ### Odoo XML: Session Calendar View Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Configures a calendar view for sessions, using start and end dates for display and coloring events by instructor. Supports day, week, and month scales. ```xml ``` -------------------------------- ### Odoo Session Model Operations and Constraints Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Illustrates the creation and manipulation of 'openacademy.session' records, including computed fields for seat occupancy and end dates, inverse fields, and validation constraints. Demonstrates archiving records. ```python Session = env['openacademy.session'] Partner = env['res.partner'] instructor = Partner.search([('instructor', '=', True)], limit=1) attendees = Partner.search([('id', '!=', instructor.id)], limit=10) session = Session.create({ 'name': 'Python Bootcamp – June', 'course_id': env.ref('open_academy.course0').id, 'start_date': '2024-06-01', 'duration': 5, # days; end_date auto-computed as 2024-06-05 'seats': 20, 'instructor_id': instructor.id, 'attendee_ids': [(6, 0, attendees.ids)], }) # Computed fields print(session.taken_seats) # e.g. 50.0 (10 attendees / 20 seats * 100) print(session.end_date) # 2024-06-05 print(session.attendees_count) # 10 # Inverse field: set end_date → duration recalculates session.write({'end_date': '2024-06-10'}) print(session.duration) # 10 # Constraint: instructor cannot be an attendee from odoo.exceptions import ValidationError try: session.write({'attendee_ids': [(4, instructor.id)]}) except ValidationError as e: print(e) # => A session's instructor can't be an attendee # Archive (soft-delete via active flag) session.write({'active': False}) print(session.active) # False ``` -------------------------------- ### Odoo QWeb PDF Report Configuration Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This XML configures an Odoo report action for 'openacademy.session'. It specifies the report name, model, type (qweb-pdf), and binding to the session model, making it available in the session's print menu. ```xml Session Report openacademy.session qweb-pdf open_academy.report_session_view report ``` -------------------------------- ### Render PDF Report Programmatically Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Generates a PDF report for a given set of session IDs and saves it to a file. Ensure the report template and session IDs are correctly specified. ```python session_ids = env['openacademy.session'].search([], limit=2).ids pdf_content, mime = env.ref('open_academy.report_session') \ ._render_qweb_pdf(session_ids) with open('/tmp/sessions_report.pdf', 'wb') as f: f.write(pdf_content) ``` -------------------------------- ### Odoo res.partner Extension for Instructor Flag Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This snippet shows how to extend the `res.partner` model in Odoo by adding an `instructor` boolean field and a back-reference to sessions attended. It demonstrates flagging a partner as an instructor and searching for all instructors. ```python Partner = env['res.partner'] # Flag an existing contact as an instructor alice = Partner.search([('name', '=', 'Alice')], limit=1) alice.write({'instructor': True}) # Search for all instructors instructors = Partner.search([('instructor', '=', True)]) print(instructors.mapped('name')) # ['Alice', 'Bob'] # View all sessions a partner has attended (readonly, set via session model) partner = Partner.browse(5) for s in partner.session_ids: print(s.name, s.start_date) ``` -------------------------------- ### Navigate to Dashboard Action Programmatically Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Retrieves the action definition for the session dashboard programmatically. This can be used to open the dashboard from other parts of the application. ```python # Navigate to the dashboard action programmatically action = env.ref('open_academy.open_board_session').read()[0] # action = {'name': 'Session Dashboard', 'res_model': 'board.board', # 'view_mode': 'form', 'view_id': , ...} ``` -------------------------------- ### Odoo Wizard for Bulk Attendee Registration Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This TransientModel wizard allows registering multiple attendees to multiple sessions programmatically. It pre-populates session IDs from the active selection and uses `with_context` to set the active sessions for the wizard. ```python # Calling the wizard programmatically (mirrors what the UI does) Wizard = env['openacademy.wizard'] session_ids = env['openacademy.session'].search([], limit=3).ids attendee_ids = env['res.partner'].search([('instructor', '=', False)], limit=5).ids wizard = Wizard.with_context(active_ids=session_ids).create({ 'session_ids': [(6, 0, session_ids)], 'attendee_ids': [(6, 0, attendee_ids)], }) wizard.subscribe() # Attendees are now added to all three sessions via |= (union assignment) # Verify for s in env['openacademy.session'].browse(session_ids): print(s.name, len(s.attendee_ids)) # => Python Bootcamp – June 5 # => Advanced ORM 5 # => Website Module Dev 5 ``` -------------------------------- ### Odoo XML: Session Tree View with Conditional Formatting Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Defines a tree view for sessions with conditional formatting. Sessions shorter than 5 days are displayed in bold, and longer than 15 days in italics. ```xml ``` -------------------------------- ### Odoo XML: Session Graph View Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Defines a graph view to visualize attendee counts grouped by course. This view is suitable for analyzing participation trends across different courses. ```xml ``` -------------------------------- ### Odoo XML: Session Dashboard Form Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Defines the structure for a session dashboard using the 'board' module. It arranges action panels for attendees by course, sessions, and courses in a two-column layout. ```xml
``` -------------------------------- ### Odoo Security - Record Rule for Course Modification (XML) Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This XML defines an Odoo record rule that restricts write and unlink access to the 'openacademy.course' model. Access is granted only to managers who are also the 'responsible_id' of the course, or if the 'responsible_id' is not set. ```xml Only Responsible can modify Course ['|', ('responsible_id','=',False), ('responsible_id','=',user.id)] ``` -------------------------------- ### Odoo Computed Field: Seat Occupancy Calculation Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Shows how the `_taken_seats` computed field calculates the percentage of occupied seats based on `seats` and `attendee_ids`. Includes a guard against division by zero when `seats` is 0. ```python # Trigger recompute by updating seats session = env['openacademy.session'].browse(1) session.write({'seats': 50}) session.write({'attendee_ids': [(6, 0, env['res.partner'].search([], limit=25).ids)]}) print(session.taken_seats) # 50.0 # Zero-seat guard session.write({'seats': 0}) print(session.taken_seats) # 0.0 (avoids ZeroDivisionError) ``` -------------------------------- ### Odoo Bidirectional Date and Duration Fields Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt Demonstrates the bidirectional computed field pair `_get_end_date` and `_set_end_date` which keeps `end_date` and `duration` synchronized. Shows updating duration to recompute end date, and setting end date to recalculate duration. ```python session = env['openacademy.session'].browse(1) # Direction 1: duration → end_date session.write({'start_date': '2024-09-01', 'duration': 3}) print(session.end_date) # 2024-09-03 (Mon + 3 days - 1 sec = Wed) # Direction 2: end_date → duration (inverse) session.end_date = '2024-09-08' # or session.write({'end_date': '2024-09-08'}) print(session.duration) # 8 ``` -------------------------------- ### Odoo Security - Access Control List (CSV) Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This CSV file defines access control lists (ACLs) for Odoo models, specifying read, write, create, and unlink permissions for different security groups. It includes rules for courses, sessions, and wizards. ```csv # open_academy/security/ir.model.access.csv id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink course_manager,course manager,model_openacademy_course,group_manager,1,1,1,1 session_manager,session manager,model_openacademy_session,group_manager,1,1,1,1 course_read_all,course all,model_openacademy_course,,1,0,0,0 # public read session_read_all,session all,model_openacademy_session,,1,0,0,0 # public read wizard_manager,wizard session manager,model_openacademy_wizard,group_manager,1,1,1,1 ``` -------------------------------- ### Odoo Onchange Handler for Seat Validation Source: https://context7.com/cybroodoo/odoodevelopmenttutorial/llms.txt This onchange handler validates seat counts for Odoo sessions, providing interactive warnings in the UI for negative seats or insufficient seats for attendees. It's typically triggered by user input in a form view. ```python # Simulated onchange call (normally triggered by the UI) session = env['openacademy.session'].new({ 'name': 'Test', 'course_id': 1, 'seats': -1, 'attendee_ids': [], }) result = session._verify_valid_seats() # => {'warning': {'title': "Incorrect 'seats' value", # 'message': "The number of available seats may not be negative"}} session.seats = 2 session.attendee_ids = env['res.partner'].search([], limit=5) result = session._verify_valid_seats() # => {'warning': {'title': 'Too many attendees', # 'message': 'Increase seats or remove excess attendees'}} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.