Commit b03b40da authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Model decision categories as many to many relation

/close #53
parent e5f5020f
"""empty message
Revision ID: 60a730d37c9a
Revises: ac54a2c29610
Create Date: 2017-04-28 22:10:24.694024
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '60a730d37c9a'
down_revision = 'ac54a2c29610'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('decisions_category_id_fkey', 'decisions', type_='foreignkey')
op.drop_column('decisions', 'category_id')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('decisions', sa.Column('category_id', sa.INTEGER(), autoincrement=False, nullable=True))
op.create_foreign_key('decisions_category_id_fkey', 'decisions', 'decisioncategories', ['category_id'], ['id'])
# ### end Alembic commands ###
"""empty message
Revision ID: ac54a2c29610
Revises: c59998057d39
Create Date: 2017-04-20 19:30:26.887146
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'ac54a2c29610'
down_revision = 'c59998057d39'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('decisioncategoryassociations',
sa.Column('decision_id', sa.Integer(), nullable=False),
sa.Column('decisioncategory_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['decision_id'], ['decisions.id'], ),
sa.ForeignKeyConstraint(['decisioncategory_id'], ['decisioncategories.id'], ),
sa.PrimaryKeyConstraint('decision_id', 'decisioncategory_id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('decisioncategoryassociations')
# ### end Alembic commands ###
......@@ -610,15 +610,18 @@ class Decision(DatabaseModel):
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
content = db.Column(db.String)
category_id = db.Column(db.Integer, db.ForeignKey("decisioncategories.id"), nullable=True)
document = relationship("DecisionDocument", backref=backref("decision"), cascade="all, delete-orphan", uselist=False)
categories = relationship("DecisionCategory", secondary="decisioncategoryassociations")
likes = relationship("Like", secondary="likedecisionassociations")
def get_parent(self):
return self.protocol
def get_categories_str(self):
return ", ".join(map(lambda c: c.name, self.categories))
class DecisionCategory(DatabaseModel):
__tablename__ = "decisioncategories"
__model_name__ = "decisioncategory"
......@@ -626,11 +629,14 @@ class DecisionCategory(DatabaseModel):
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
name = db.Column(db.String)
decisions = relationship("Decision", backref=backref("category"), order_by="Decision.id")
def get_parent(self):
return self.protocoltype
class DecisionCategoryAssociation(DatabaseModel):
__tablename__ = "decisioncategoryassociations"
decision_id = db.Column(db.Integer, db.ForeignKey("decisions.id"), primary_key=True)
decisioncategory_id = db.Column(db.Integer, db.ForeignKey("decisioncategories.id"), primary_key=True)
class MeetingReminder(DatabaseModel):
__tablename__ = "meetingreminders"
__model_name__ = "meetingreminder"
......
......@@ -217,10 +217,12 @@ class Tag:
return ""
return self.todo.render_latex(current_protocol=protocol)
elif self.name == "beschluss":
result = r"\textbf{{Beschluss:}} {}".format(self.decision.content)
if self.decision.category is not None:
result = r"{} \textit{{({})}}".format(result, self.decision.category.name)
return result
parts = [r"\textbf{{Beschluss:}} {}".format(self.decision.content)]
if len(self.decision.categories):
parts.append(
r"\textit{{({})}}".format(self.decision.get_categories_str())
)
return " ".join(parts)
return r"\textbf{{{}:}} {}".format(escape_tex(self.name.capitalize()), escape_tex(";".join(self.values)))
elif render_type == RenderType.plaintext:
if self.name == "url":
......@@ -251,8 +253,9 @@ class Tag:
elif self.name == "beschluss":
if getattr(self, "decision", None) is not None:
parts = ["<b>Beschluss:</b>", self.decision.content]
if self.decision.category is not None:
parts.append("<i>{}</i>".format(self.decision.category.name))
if len(self.decision.categories) > 0:
parts.append("<i>{}</i>".format(
self.decision.get_categories_str()))
return " ".join(parts)
else:
return "<b>Beschluss:</b> {}".format(self.values[0])
......
......@@ -1034,8 +1034,7 @@ def list_decisions():
if decisioncategory_id is not None and decisioncategory_id != -1:
decisions = [
decision for decision in decisions
if decision.category is not None
and decision.category.id == decisioncategory_id
if decisioncategory_id in map(lambda d: d.id, decision.categories)
]
if search_term is not None and len(search_term.strip()) > 0:
decisions = [
......
......@@ -50,8 +50,6 @@ if raw_additional_packages is not None:
if "{" not in package:
package = "{{{}}}".format(package)
additional_packages.append(package)
print(raw_additional_packages)
print(additional_packages)
texenv.globals["additional_packages"] = additional_packages
latex_pagestyle = getattr(config, "LATEX_PAGESTYLE", None)
if latex_pagestyle is not None:
......@@ -314,9 +312,8 @@ def parse_protocol_async_inner(protocol, encoded_kwargs):
db.session.commit()
return
decision_content = decision_tag.values[0]
decision_category_id = None
if len(decision_tag.values) > 1:
decision_category_name = decision_tag.values[1]
decision_categories = []
for decision_category_name in decision_tag.values[1:]:
decision_category = DecisionCategory.query.filter_by(protocoltype_id=protocol.protocoltype.id, name=decision_category_name).first()
if decision_category is None:
category_candidates = DecisionCategory.query.filter_by(protocoltype_id=protocol.protocoltype.id).all()
......@@ -336,11 +333,13 @@ def parse_protocol_async_inner(protocol, encoded_kwargs):
db.session.commit()
return
else:
decision_category_id = decision_category.id
decision_categories.append(decision_category)
decision = Decision(protocol_id=protocol.id,
content=decision_content, category_id=decision_category_id)
content=decision_content)
db.session.add(decision)
db.session.commit()
for decision_category in decision_categories:
decision.categories.append(decision_category)
decision_tag.decision = decision
decisions_to_render.append((decision, decision_tag))
for decision, decision_tag in decisions_to_render:
......
......@@ -41,7 +41,7 @@
\begin{itemize}
\ENV{if protocol.decisions|length > 0}
\ENV{for decision in protocol.decisions}
\item \VAR{decision.content|escape_tex}\ENV{if decision.category is not none and show_private} \textit{(\VAR{decision.category.name})}\ENV{endif}
\item \VAR{decision.content|escape_tex}\ENV{if decision.categories|length > 0 and show_private} \textit{(\VAR{decision.get_categories_str()})}\ENV{endif}
\ENV{endfor}
\ENV{else}
\item Keine Beschlüsse
......
......@@ -384,7 +384,7 @@ class DecisionsTable(Table):
super().__init__("Beschlüsse", decisions)
self.category_present = len([
decision for decision in decisions
if decision.category is not None
if len(decision.categories) > 0
]) > 0
def headers(self):
......@@ -401,7 +401,7 @@ class DecisionsTable(Table):
Table.link(url_for("show_protocol", protocol_id=decision.protocol.id), decision.protocol.get_short_identifier()),
decision.content
]
category_part = [decision.category.name if decision.category is not None else ""]
category_part = [decision.get_categories_str()]
if not self.category_present:
category_part = []
action_part = [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment