1. Ed Brannin
  2. baptism_records

Source

baptism_records / Baptism-Records / baptism_records / model / baptism_model.py

# -*- coding: utf-8 -*-

from sqlalchemy import *
from sqlalchemy.orm import mapper, relation, relation
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode
#from sqlalchemy.orm import relation, backref

relationship = relation

from baptism_records.model import DeclarativeBase, metadata, DBSession

class Minister(DeclarativeBase):
    __tablename__ = 'minister'
    id = Column(Integer, primary_key=True)
    prefix = Column(Unicode(255), nullable=True)
    first_name = Column(Unicode(255), nullable=False)
    middle_name = Column(Unicode(255), nullable=True)
    last_name = Column(Unicode(255), nullable=False)
    suffix = Column(Unicode(255), nullable=False)
    notes = Column(Unicode(4096), nullable=True)
    
    def __unicode__(self):
        return "%s %s %s %s %s" % (self.prefix, self.first_name, self.middle_name, self.last_name, self.suffix)

class PostalCode(DeclarativeBase):
    __tablename__ = 'postal_code'
    postal_code = Column(Unicode(255), primary_key=True)
    city = Column(Unicode(255), nullable=True)
    state = Column(Unicode(255), nullable=True)
    
    def __unicode__(self):
        return self.postal_code

class Church(DeclarativeBase):
    __tablename__ = 'church'
    
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255), nullable=False)
    street_address = Column(Unicode(255), nullable=True)
    postal_code_id = Column(Unicode(255), ForeignKey('postal_code.postal_code'))
    postal_code = relationship(PostalCode, backref="churches")
    
    def city_state_zip(self): #TODO
        return ""
    

# TODO: Book, Page, Number (polymorphic?)

class Christian(DeclarativeBase):
    __tablename__ = 'christian'
    
    #{ Columns
    
    id = Column(Integer, primary_key=True)
    
    first_name = Column(Unicode(255), nullable=False)
    middle_name = Column(Unicode(255), nullable=True)
    last_name = Column(Unicode(255), nullable=False)

    birthday = Column(Date(), nullable=True)
    
    father_name = Column(Unicode(255), nullable=False)
    mother_maiden_name = Column(Unicode(255), nullable=False)
    
    sponsor_1 = Column(Unicode(255), nullable=True)
    sponsor_2 = Column(Unicode(255), nullable=True)
    birth_hospital = Column(Unicode(255), nullable=False)
    postal_code_id = Column(Unicode(255), ForeignKey('postal_code.postal_code'))
    postal_code = relationship(PostalCode, backref="christians")
    
    baptized_by_id = Column(Integer, ForeignKey('minister.id'))
    baptized_by = relationship(Minister, backref="baptized")
    baptized_on = Column(Date(), nullable=False)
    baptized_at_id = Column(Integer, ForeignKey('church.id'))
    baptized_at = relationship(Church, backref="baptized")
    

    # confirmed_by = relationship("Christian", backref="confirmed_by")
    
    # TODO: Confirmer, confirmation place & date, confirmed-boolean

    @property
    def full_name(self):
        if self.middle_name is None:
            return "%s %s" % (self.first_name, self.last_name)
        else:
            return "%s %s %s" % (self.first_name, 
                self.middle_name, self.last_name)
    
    # Thanks to http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict/2237320#2237320
    def to_dict(self):
        return dict([(k, getattr(self, k)) for k in self.__dict__.keys() if not k.startswith("_")])

# See http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration
class Note(DeclarativeBase):
    __tablename__ = 'notes'
    id = Column(Integer, primary_key=True)
    christian_id = Column(Integer, ForeignKey('christian.id'))
    note_type = Column(Unicode(255), nullable=False)
    note = Column(Unicode(1024), nullable=False)
    __mapper_args__ = {'polymorphic_on': note_type}


class Marriage(Note):
    __mapper_args__ = {'polymorphic_identity': 'marriage'}
    christian = relationship(Christian, backref='marriages')


class Religious(Note):
    __mapper_args__ = {'polymorphic_identity': 'religious'}
    christian = relationship(Christian, backref='religious')


class Remark(Note):
    __mapper_args__ = {'polymorphic_identity': 'remark'}
    christian = relationship(Christian, backref='remarks')

class BookLocation(DeclarativeBase):
    __tablename__ = 'book_location'
    id = Column(Integer, nullable=False, primary_key=True)
    book = Column(Integer, nullable=False)
    page = Column(Integer, nullable=False)
    number = Column(Integer, nullable=False)
    christian_id = Column(Integer, ForeignKey('christian.id'))
    christian = relationship(Christian, backref='book_locations')
    
    def __unicode__(self):
        return u'Book %s, Page %s, Row %s' % (self.book, self.page, self.number)