2016-04-25 10:38:37 +01:00
|
|
|
import uuid
|
2016-08-02 16:23:14 +01:00
|
|
|
|
2017-02-14 11:05:23 +00:00
|
|
|
from sqlalchemy import desc
|
2017-02-14 14:22:52 +00:00
|
|
|
from sqlalchemy.sql.expression import bindparam
|
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)
|
2017-03-08 13:03:44 +00:00
|
|
|
def dao_create_template(template):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-02-14 14:22:52 +00:00
|
|
|
def dao_get_templates_for_cache(cache):
|
2017-02-13 18:47:29 +00:00
|
|
|
if not cache or len(cache) == 0:
|
|
|
|
|
return []
|
2017-02-14 11:05:23 +00:00
|
|
|
|
|
|
|
|
# First create a subquery that is a union select of the cache values
|
|
|
|
|
# Then join templates to the subquery
|
|
|
|
|
cache_queries = [
|
2017-02-14 14:22:52 +00:00
|
|
|
db.session.query(bindparam("template_id" + str(i),
|
|
|
|
|
uuid.UUID(template_id.decode())).label('template_id'),
|
|
|
|
|
bindparam("count" + str(i), int(count.decode())).label('count'))
|
2017-02-14 11:05:23 +00:00
|
|
|
for i, (template_id, count) in enumerate(cache)]
|
|
|
|
|
cache_subq = cache_queries[0].union(*cache_queries[1:]).subquery()
|
|
|
|
|
query = db.session.query(Template.id.label('template_id'),
|
|
|
|
|
Template.template_type,
|
|
|
|
|
Template.name,
|
|
|
|
|
cache_subq.c.count.label('count')
|
|
|
|
|
).join(cache_subq,
|
|
|
|
|
Template.id == cache_subq.c.template_id
|
|
|
|
|
).order_by(Template.name)
|
|
|
|
|
|
|
|
|
|
return query.all()
|