2016-04-25 10:38:37 +01:00
|
|
|
import uuid
|
2016-08-02 16:23:14 +01:00
|
|
|
|
2017-02-14 09:59:33 +00:00
|
|
|
from sqlalchemy import (desc, text)
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-08-02 16:23:14 +01:00
|
|
|
from app import db
|
|
|
|
|
from app.models import (Template, TemplateHistory)
|
2016-04-25 10:38:37 +01:00
|
|
|
from app.dao.dao_utils import (
|
|
|
|
|
transactional,
|
|
|
|
|
version_class
|
|
|
|
|
)
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
@transactional
|
2016-08-02 16:23:14 +01:00
|
|
|
@version_class(Template, TemplateHistory)
|
2016-02-22 09:46:16 +00:00
|
|
|
def dao_create_template(template):
|
2016-04-25 10:38:37 +01:00
|
|
|
template.id = uuid.uuid4() # must be set now so version history model can use same id
|
2016-05-16 16:16:14 +01:00
|
|
|
template.archived = False
|
2016-02-22 09:46:16 +00:00
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
@transactional
|
2016-08-02 16:23:14 +01:00
|
|
|
@version_class(Template, TemplateHistory)
|
2016-02-22 09:46:16 +00:00
|
|
|
def dao_update_template(template):
|
|
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
|
|
|
|
|
if version is not None:
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
2016-05-06 15:42:43 +01:00
|
|
|
id=template_id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
version=version).one()
|
2016-03-11 12:39:55 +00:00
|
|
|
return Template.query.filter_by(id=template_id, service_id=service_id).one()
|
2016-02-22 09:46:16 +00:00
|
|
|
|
|
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
def dao_get_template_by_id(template_id, version=None):
|
|
|
|
|
if version is not None:
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
2016-05-06 15:42:43 +01:00
|
|
|
id=template_id,
|
|
|
|
|
version=version).one()
|
2016-03-11 15:34:20 +00:00
|
|
|
return Template.query.filter_by(id=template_id).one()
|
2016-02-24 11:51:02 +00:00
|
|
|
|
|
|
|
|
|
2016-02-22 09:46:16 +00:00
|
|
|
def dao_get_all_templates_for_service(service_id):
|
2016-04-11 17:39:49 +01:00
|
|
|
return Template.query.filter_by(
|
2016-05-23 14:00:28 +01:00
|
|
|
service_id=service_id,
|
|
|
|
|
archived=False
|
2016-04-11 17:39:49 +01:00
|
|
|
).order_by(
|
2016-06-06 10:32:24 +01:00
|
|
|
desc(Template.created_at)
|
2016-04-11 17:39:49 +01:00
|
|
|
).all()
|
2016-05-09 15:59:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_template_versions(service_id, template_id):
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
|
|
|
|
service_id=service_id, id=template_id
|
|
|
|
|
).order_by(
|
|
|
|
|
desc(TemplateHistory.version)
|
|
|
|
|
).all()
|
2017-02-13 18:47:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_templates_by_for_cache(cache):
|
|
|
|
|
if not cache or len(cache) == 0:
|
|
|
|
|
return []
|
|
|
|
|
txt = "( " + " Union all ".join(
|
|
|
|
|
"select '{}'::text as template_id, {} as count".format(x.decode(),
|
|
|
|
|
y.decode()) for x, y in cache) + " ) as cache"
|
|
|
|
|
txt = "Select t.id as template_id, t.template_type, t.name, cache.count from templates t, " + \
|
|
|
|
|
txt + " where t.id::text = cache.template_id order by t.name"
|
|
|
|
|
stmt = text(txt)
|
|
|
|
|
|
|
|
|
|
return db.session.execute(stmt).fetchall()
|