# Path: template_maker/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
# class SurrogatePK(object):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
#     def get_by_id(cls, id):
# def ReferenceCol(tablename, nullable=False, ondelete=None, pk_name='id', **kwargs):
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description


import datetime
from template_maker.database import db
from template_maker.builder.models import TemplateBase
    return TemplateBase.query.filter(TemplateBase.published==True).all()

def get_single_template(template_id):
    '''
    Returns a single template from a template_id
    '''
    return TemplateBase.query.get(template_id)

def create_new_template(data):
    '''
    Creates a new template from a dictionary or request.form
    object
    '''
    now = datetime.datetime.utcnow()
    template_base = TemplateBase(
        created_at = now,
        updated_at = now,
        title = data.get('title'),
        description = data.get('description')
    )
    db.session.add(template_base)
    db.session.commit()

    return template_base.id

def delete_template(template):
    '''
    Deletes the passed Template object
    '''
    db.session.delete(template)

# Path: template_maker/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
# class SurrogatePK(object):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
#     def get_by_id(cls, id):
# def ReferenceCol(tablename, nullable=False, ondelete=None, pk_name='id', **kwargs):
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description


# Path: template_maker/data/templates.py
import datetime
from template_maker.database import db
from template_maker.builder.models import TemplateBase

    return TemplateBase.query.filter(TemplateBase.published==True).all()

def get_single_template(template_id):
    '''
    Returns a single template from a template_id
    '''
    return TemplateBase.query.get(template_id)

def create_new_template(data):
    '''
    Creates a new template from a dictionary or request.form
    object
    '''
    now = datetime.datetime.utcnow()
    template_base = TemplateBase(
        created_at = now,
        updated_at = now,
        title = data.get('title'),
        description = data.get('description')
    )
    db.session.add(template_base)
    db.session.commit()

    return template_base.id

def delete_template(template):
    '''
    Deletes the passed Template object
    '''
    db.session.delete(template)

    db.session.commit()