Home > python, wxpython > wxPython: Rubrica- Model

wxPython: Rubrica- Model

22 Maggio 2011

Terminate le view, sono passato al modulo Data, nel quale ho creato una classe Model, dove dichiarare tutte le variabili che mi serviranno come appoggio per la gestione dei dati (Controller).
Poi per poter utilizzare l’ORM di SQLAlchemy ho creato la classe Contact, definendo di fatto la tabella e i campi che utilizzerò per il database (sqlite3 nel mio caso).

il codice:

#!/usr/bin/python
#Data.py
'''Data module for sql alchemy ORM'''

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

class Model(object):
    '''Model class with common application data'''
    base = declarative_base()
    engine = create_engine('sqlite:///contacts.db', echo = True)
    metadata = base.metadata
    def __init__(self):
        self.name = None
        self.surname = None
        self.address = None
        self.mail = None
        self.mobile = None
        self.phone = None
    def get_name(self):
        '''get the contact name'''
        return self.name
    def get_surname(self):
        '''get the contact surname'''
        return self.surname
    def get_address(self):
        '''get the contact address'''
        return self.address
    def get_mail(self):
        '''get the contact mail'''
        return self.mail
    def get_phone(self):
        '''get the contact phone number'''
        return self.phone
    def get_mobile(self):
        '''get the contact mobile number'''
        return self.mobile
    def set_name(self, value):
        '''Sets the contact name to the given value'''
        self.name = value
    def set_surname(self, value):
        '''Sets contact surname to the given value'''
        self.surname = value
    def set_address(self, value):
        '''Sets contact address to the given value'''
        self.address = value
    def set_mail(self, value):
        '''Sets contact mail to the given value'''
        self.mail = value
    def set_phone(self, value):
        '''Sets contact phone to the given value'''
        self.phone = value
    def set_mobile(self, value):
        '''Sets contact mobile to the given value'''
        self.mobile = value

class Contact(Model.base):
    '''Contact class for ORM Mapping'''
    __tablename__ = 'contacts'
    id = Column(Integer, primary_key = True)
    name = Column(String)
    surname = Column(String)
    address = Column(String)
    mail = Column(String)
    mobile = Column(String)
    phone = Column(String)
    
    def __init__(self, name, surname, address, mail, mobile, phone):
        self.name = name
        self.surname = surname
        self.address = address
        self.mail = mail
        self.mobile = mobile
        self.phone = phone
    def __repr__(self):
        return "<Contact ('%s %s')>" % (self.surname, self.name)

infine il modulo controller

Categorie:python, wxpython Tag:
I commenti sono chiusi.