MVC Architecture and Python Code Examples

MVC Architecture

XML – The view and the controller, via XML, views (forms, lists, calendars, graphs, menus).
Python – Design of the controller (business logic).

  • View Fields: Obtains a list of the fields of the current view, with corresponding parameters.
  • Fields View Get: Shows the XML generated by the current view.
  • Manage Views: Shows a list of the views related to the current view.
  • Edit TreeView, Edit SearchView, Edit Action, and Edit Workflow: These are used to access the editing of the views related to the current view.
  • View Log (perm_read): Shows information related to the current record.
  • Log_access: Creates entries in the database tables.


Python Code Example 1: Kartingbarranco Racer

class kartingbarranco_racer(osv.osv):
   def _get_name(self, cr, uid, ids, field_name, arg, context=None):
       result = {}
       for h in self.browse(cr, uid, ids, context=context):
           result[h.id] = h.last_name + ', ' + h.first_name
       return result
   _name = 'kartingbarranco.racer'
   _columns = {
       'name': fields.function(_get_name, type='char', size=60, string='Name', store=False, required=True),
       'address': fields.char('Address', size=70),
       'paco': fields.integer('Paco'),
       'state_id': fields.many2one('res.country.state', 'State'),
       'race_ids': fields.one2many('kartingbarranco.diary.racer', 'racer_id', 'Races', readonly=True),
       'active': fields.boolean('Active')
   }

Python Code Example 2: Time Formatting

def _get_name(self, cr, uid, ids, field_name, arg, context):
       result = {}
       for h in self.browse(cr, uid, ids, context=context):
           result[h.id] = h.diary_name + " - " + "%02d:%02d" % (int(h.time), (h.time - int(h.time)) * 60)
       return result

Python Code Example 3: Age Calculation

def onchange_birthdate(self, cr, uid, ids, birthdate, arg=None, context=None):
       result = {}
       result['of_legal_age'] = (age(date.today(), parser.parse(birthdate)) >= 18)
       return {'value': result}


Python Code Example 4: Kartingbarranco Racer with Age Verification

class kartingbarranco_racer(osv.osv):
    def _get_name(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        for h in self.browse(cr, uid, ids, context=context):
            result[h.id] = h.last_name + ', ' + h.first_name
        return result
    def _of_legal_age(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        for h in self.browse(cr, uid, ids, context=context):
            if h.birthdate:
                current_date = date.today()
                current_year = current_date.year
                birth_date = parser.parse(h.birthdate)
                current_age = current_year - birth_date.year
                if current_age > 17:
                result[h.id] = True
                else:
                result[h.id] = False
        return result
    _name = 'kartingbarranco.racer'
    _columns = {
        'name': fields.function(_get_name, type='char', size=60, string='Name'),
        'of_legal_age': fields.function(_of_legal_age, type='boolean', string='Of legal age'),
        'address': fields.char('Address', size=70),
        'state_id': fields.many2one('res.country.state', 'State'),
        'race_ids': fields.one2many('kartingbarranco.diary.racer', 'racer_id', 'Races', readonly=True),
        'active': fields.boolean('Active')
    }
    _defaults = {
        'active': True
    }