Commits

Ed Brannin committed eeb70a9

Various updates, mainly concerning Book-location suppoert.

Comments (0)

Files changed (7)

Baptism-Records/baptism_records/controllers/christian.py

 from baptism_records.controllers.secure import SecureController
 
 
-__all__ = ['CRUDdyChristianController', 'ChristianController', 'CompleteController']
+__all__ = ['CRUDdyChristianController', 'ChristianController', 'CompleteController', 'BookListController']
 
 from tg import tmpl_context, redirect, validate
 from baptism_records.widgets.christian_form import create_christian_form, edit_christian_form, add_note_form
 from sqlalchemy.orm.exc import NoResultFound
 from sqlalchemy import or_
 
+import datetime
+
 import pprint
 pp = pprint.PrettyPrinter(indent=4)
 
         log.debug("In new()")
         log.debug("====================================================================================")
         tmpl_context.form = create_christian_form
+        
+        last_book_location = self.last_book_location()
+        if last_book_location is None:
+            last_book_location = BookLocation(book=1, page=1, number=0)
+        
+        last_christian = self.last_christian()
+        if last_christian is None:
+            today = datetime.date.today()
+            last_christian = Christian(birthday=today, baptized_on=today)
+        
         return dict(modelname='Christian',
             page='New Baptism Record',
-            form=dict(),
+            form=dict(
+                baptized_on=last_christian.baptized_on,
+                book=last_book_location.book,
+                page=last_book_location.page,
+                number=last_book_location.number + 1
+                ),
             ministers=DBSession.query(Minister.id, Minister),
             churches=DBSession.query(Church.id, Church.name)
             )
     
     def update_from_form(self, christian, **kw):
+        date_format = '%m/%d/%Y'
         christian.first_name = kw['first_name']
         christian.middle_name = kw['middle_name']
         christian.last_name = kw['last_name']
         christian.sponsor_1 = kw['sponsor_1']
         christian.sponsor_2 = kw['sponsor_2']
         christian.birth_hospital = kw['birth_hospital']
-        christian.baptized_on = kw['baptized_on']
-        # christian.baptized_by = kw['baptized_by']
-        # christian.baptized_at = kw['baptized_at']
-        christian.birthday = kw['birthday']
+        try:
+            christian.baptized_on = datetime.datetime.strptime(kw['baptized_on'], date_format)
+        except ValueError:
+            flash("Baptism date not saved: %s.  Please use the format MM/DD/YYYY.")
+        
+        if 'baptized_by' in kw:
+            christian.baptized_by_id = kw['baptized_by']
+        if 'baptized_at' in kw:
+            christian.baptized_at = kw['baptized_at']
+
+        try:
+            christian.birthday = datetime.datetime.strptime(kw['birthday'], date_format)
+        except ValueError:
+            flash("Birthday not saved: %s.  Please use the format MM/DD/YYYY.")
+        
+        if len(christian.book_locations) == 0:
+            christian.book_locations.append(BookLocation())
+        bl = christian.book_locations[0]
+        bl.book = kw['book']
+        bl.page = kw['page']
+        bl.number = kw['number']
         
         postal_code = None
         try:
         christian.postal_code = postal_code
     
     @expose()
-    @validate(create_christian_form, error_handler=new)
+    @validate(create_christian_form)
     def create(self, *args, **kw):
         log.debug("====================================================================================")
-        log.debug("In create()")
+        log.debug("In create(%s, %s)" % (repr(args), pp.pformat(kw)))
         log.debug("====================================================================================")
         c = Christian()
         self.update_from_form(c, **kw)
             redirect('index')
     
     @expose()
-    @validate(edit_christian_form, error_handler=new)
+    @validate(edit_christian_form)
     def save(self, id=None, **kw):
         log.debug("====================================================================================")
         log.debug("In save()")
         log.debug("====================================================================================")
         tmpl_context.form = edit_christian_form
         c = DBSession.query(Christian).filter_by(id=id).one()
+        form = c.to_dict()
+        try:
+            book = c.book_locations[0]
+            form.update(book=book.book, page=book.page, number=book.number)
+        except:
+            log.exception("Error trying to load book location for christian #%d" % c.id)
         return dict(
                     page="Edit %s" % c.full_name,
-                    form=dict(c.to_dict()),
+                    form=form,
                     ministers=DBSession.query(Minister.id, Minister),
                     churches=DBSession.query(Church.id, Church.name)
                     )
         flash("Added %s to %s" % (note_type, c.full_name))
         redirect(christian_id)
         
+    @expose()
+    def baptism_certificate(self, christian_id=None):
+        c = DBSession.query(Christian).filter_by(id=christian_id).one()
+        return dict(christian=c)
     
+    def last_book_location(self):
+        return DBSession.query(BookLocation).order_by(-BookLocation.id).first()
     
+    def last_christian(self):
+        return DBSession.query(Christian).order_by(-Christian.id).first()
 
 class CompleteController(BaseController):
     @expose('json')
     class table_filler_type(TableFiller):
         __model__ = Christian
 
+
+class BookListController(BaseController):
+    # TODO: Extend SecureController
+    @expose('baptism_records.templates.christian.list')
+    def default(self, book=None, page=None, number=None):
+        log.debug("====================================================================================")
+        log.debug("In BookListController.default(%s, %s, %s)" % (book, page, number))
+        log.debug("====================================================================================")
+        
+        # book_locations = DBSession.query(BookLocation)
+        christians = DBSession.query(Christian).select_from(Christian,BookLocation)
+        if book is not None:
+            christians = christians.filter(BookLocation.book == book)
+        if page is not None:
+            christians = christians.filter(BookLocation.page == page)
+        if number is not None:
+            christians = christians.filter(BookLocation.number == number)
+        
+        christians = christians.order_by(BookLocation.book, BookLocation.page, BookLocation.number)
+        
+        christians = christians.filter(BookLocation.christian_id==Christian.id)
+        
+        return dict(christians=christians, book=book, page=page, number=number)

Baptism-Records/baptism_records/controllers/root.py

     
     complete = CompleteController()
     
+    book = BookListController()
+    
     @expose('baptism_records.templates.index')
     def index(self):
         """Handle the front-page."""

Baptism-Records/baptism_records/model/baptism_model.py

 class BookLocation(DeclarativeBase):
     __tablename__ = 'book_location'
     id = Column(Integer, nullable=False, primary_key=True)
-    book_number = Column(Integer, nullable=False)
+    book = Column(Integer, nullable=False)
     page = Column(Integer, nullable=False)
-    record = 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_number, self.page, self.record)
+        return u'Book %s, Page %s, Row %s' % (self.book, self.page, self.number)
 

Baptism-Records/baptism_records/templates/christian/list.html

 
     <body>
         <div class="main_content">
-            <h1>Baptism Records</h1>
-            <a id="add_new_button" href="${tg.url('new')}">Add Christian</a>
+            <h1>Baptism Records<span py:if="book is not None"> in Book ${book}</span><span py:if="page is not None">, Page ${page}</span></h1>
+            <a id="add_new_button" href="${tg.url('/christians/new')}">Add Christian</a>
             <div py:choose="christians.count()">
-              <h2 py:when="0">No Christians Found.  Add one with the link above.</h2>
+              <h2 py:when="0">No Christians Found<span py:if="book is not None"> in Book ${book}</span><span py:if="page is not None">, Page ${page}</span>.  Add one with the link above.</h2>
               <table class="list christians" py:otherwise="" >
                 <tr>
                   <th>First Name</th>

Baptism-Records/baptism_records/templates/christian/show.html

             </ul>
           </div>
           <h1 py:content="christian.full_name">Full Name</h1>
-          <p>Postal code: <span py:replace="christian.postal_code">POSTAL CODE HERE</span></p>
+          
+          <p py:for="bl in christian.book_locations" class="book_location">
+            <a href="/book/${bl.book}">Book ${bl.book}</a>, 
+            <a href="/book/${bl.book}/${bl.page}">Page ${bl.page}</a>,
+            <a href="/book/${bl.book}/${bl.page}/${bl.number}">Number ${bl.number}</a>
+          </p>
           
           <dl class="table-display">
             <dt>First Name</dt>

Baptism-Records/baptism_records/templates/index.html

   <a href="http://commons.wikimedia.org/wiki/File:Baptesme-transyl.jpg" id="baptism_big">
     <img id="baptism_icon" src="/images/Baptesme-transyl.jpg" title="Icon of Christ's Baptism, from Wikimedia Commons." />
   </a>
-    ${sidebar_top()}
   <div id="getting_started">
     <h2>Baptism Records</h2>
     <p>TurboGears 2 is rapid web application development toolkit designed to make your life easier.</p>

Baptism-Records/baptism_records/widgets/christian_form.py

     
     fields = [
         HiddenField('id'),
+        TextField('book', validator=Int),
+        TextField('page', validator=Int),
+        TextField('number', validator=Int),
+        
         TextField('first_name', validator=NotEmpty),
         TextField('middle_name'),
         TextField('last_name', validator=NotEmpty),