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

Refactor database lookups

parent 36f4d4d7
from flask import redirect, flash, request
from functools import wraps
ID_KEY = "id"
KEY_NOT_PRESENT_MESSAGE = "Missing {}_id."
OBJECT_DOES_NOT_EXIST_MESSAGE = "There is no {} with id {}."
def default_redirect():
return redirect(request.args.get("next") or url_for("index"))
def db_lookup(*models, check_exists=True):
def _decorator(function):
@wraps(function)
def _decorated_function(*args, **kwargs):
for model in models:
key = model.__object_name__
id_key = "{}_{}".format(key, ID_KEY)
if id_key not in kwargs:
flash(KEY_NOT_PRESENT_MESSAGE.format(key), "alert-error")
return default_redirect()
obj_id = kwargs[id_key]
obj = model.query.filter_by(id=obj_id).first()
if check_exists and obj is None:
model_name = model.__class__.__name__
flash(OBJECT_DOES_NOT_EXIST_MESSAGE.format(model_name, obj_id),
"alert-error")
return default_redirect()
kwargs[key] = obj
kwargs.pop(id_key)
return function(*args, **kwargs)
return _decorated_function
return _decorator
......@@ -20,6 +20,7 @@ from todostates import make_states
class ProtocolType(db.Model):
__tablename__ = "protocoltypes"
__object_name__ = "protocoltype"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True)
short_name = db.Column(db.String, unique=True)
......@@ -139,6 +140,7 @@ class ProtocolType(db.Model):
class Protocol(db.Model):
__tablename__ = "protocols"
__object_name__ = "protocol"
id = db.Column(db.Integer, primary_key=True)
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
source = db.Column(db.String)
......@@ -303,6 +305,7 @@ def on_protocol_delete(mapper, connection, protocol):
class DefaultTOP(db.Model):
__tablename__ = "defaulttops"
__object_name__ = "defaulttop"
id = db.Column(db.Integer, primary_key=True)
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
name = db.Column(db.String)
......@@ -322,6 +325,7 @@ class DefaultTOP(db.Model):
class TOP(db.Model):
__tablename__ = "tops"
__object_name__ = "top"
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
name = db.Column(db.String)
......@@ -342,6 +346,7 @@ class TOP(db.Model):
class Document(db.Model):
__tablename__ = "documents"
__object_name__ = "document"
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
name = db.Column(db.String)
......@@ -376,6 +381,7 @@ def on_document_delete(mapper, connection, document):
class DecisionDocument(db.Model):
__tablename__ = "decisiondocuments"
__object_name__ = "decisiondocument"
id = db.Column(db.Integer, primary_key=True)
decision_id = db.Column(db.Integer, db.ForeignKey("decisions.id"))
name = db.Column(db.String)
......@@ -483,6 +489,7 @@ class TodoState(Enum):
class Todo(db.Model):
__tablename__ = "todos"
__object_name__ = "todo"
id = db.Column(db.Integer, primary_key=True)
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
number = db.Column(db.Integer)
......@@ -582,6 +589,7 @@ class TodoProtocolAssociation(db.Model):
class Decision(db.Model):
__tablename__ = "decisions"
__object_name__ = "decision"
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
content = db.Column(db.String)
......@@ -598,6 +606,7 @@ class Decision(db.Model):
class MeetingReminder(db.Model):
__tablename__ = "meetingreminders"
__object_name__ = "meetingreminder"
id = db.Column(db.Integer, primary_key=True)
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
days_before = db.Column(db.Integer)
......@@ -618,6 +627,7 @@ class MeetingReminder(db.Model):
class Error(db.Model):
__tablename__ = "errors"
__object_name__ = "error"
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
action = db.Column(db.String)
......@@ -644,6 +654,7 @@ class Error(db.Model):
class TodoMail(db.Model):
__tablename__ = "todomails"
__object_name__ = "todomail"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True)
mail = db.Column(db.String)
......@@ -661,6 +672,7 @@ class TodoMail(db.Model):
class OldTodo(db.Model):
__tablename__ = "oldtodos"
__object_name__ = "oldtodo"
id = db.Column(db.Integer, primary_key=True)
old_id = db.Column(db.Integer)
who = db.Column(db.String)
......@@ -680,6 +692,7 @@ class OldTodo(db.Model):
class DefaultMeta(db.Model):
__tablename__ = "defaultmetas"
__object_name__ = "defaultmeta"
id = db.Column(db.Integer, primary_key=True)
protocoltype_id = db.Column(db.Integer, db.ForeignKey("protocoltypes.id"))
key = db.Column(db.String)
......@@ -696,6 +709,7 @@ class DefaultMeta(db.Model):
class Meta(db.Model):
__tablename__ = "metas"
__object_name__ = "meta"
id = db.Column(db.Integer, primary_key=True)
protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
name = db.Column(db.String)
......
This diff is collapsed.
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("edit_default_top", type_id=protocoltype.id, top_id=default_top.id, next=url_for("show_type", type_id=protocoltype.id)), action_text="Ändern")}}
{{render_form(form, action_url=url_for("edit_default_top", type_id=protocoltype.id, defaulttop_id=defaulttop.id, next=url_for("show_type", protocoltype_id=protocoltype.id)), action_text="Ändern")}}
</div>
{% endblock %}
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("new_default_top", type_id=protocoltype.id, next=url_for("show_type", type_id=protocoltype.id)), action_text="Anlegen")}}
{{render_form(form, action_url=url_for("new_default_top", type_id=protocoltype.id, next=url_for("show_type", protocoltype_id=protocoltype.id)), action_text="Anlegen")}}
</div>
{% endblock %}
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("edit_defaultmeta", meta_id=meta.id, next=url_for("show_type", type_id=meta.protocoltype.id)), action_text="Ändern")}}
{{render_form(form, action_url=url_for("edit_defaultmeta", meta_id=meta.id, next=url_for("show_type", protocoltype_id=meta.protocoltype.id)), action_text="Ändern")}}
</div>
{% endblock %}
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("new_defaultmeta", type_id=protocoltype.id, next=url_for("show_type", type_id=protocoltype.id)), action_text="Anlegen")}}
{{render_form(form, action_url=url_for("new_defaultmeta", protocoltype_id=protocoltype.id, next=url_for("show_type", protocoltype_id=protocoltype.id)), action_text="Anlegen")}}
</div>
{% endblock %}
......@@ -35,7 +35,7 @@
<a class="btn btn-default" href="{{url_for("publish_protocol", protocol_id=protocol.id)}}">Veröffentlichen</a>
{% endif %}
{% endif %}
<a class="btn btn-default" href="{{url_for("show_type", type_id=protocol.protocoltype.id)}}">Typ</a>
<a class="btn btn-default" href="{{url_for("show_type", protocoltype_id=protocol.protocoltype.id)}}">Typ</a>
{% if protocol.has_compiled_document() %}
<a class="btn btn-success" href="{{url_for("download_document", document_id=protocol.get_compiled_document().id)}}">Download</a>
{% endif %}
......
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("edit_reminder", type_id=protocoltype.id, reminder_id=reminder.id, next=url_for("show_type", type_id=protocoltype.id)), action_text="Ändern")}}
{{render_form(form, action_url=url_for("edit_reminder", protocoltype_id=protocoltype.id, meetingreminder_id=meetingreminder.id, next=url_for("show_type", protocoltype_id=protocoltype.id)), action_text="Ändern")}}
</div>
{% endblock %}
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("new_reminder", type_id=protocoltype.id, next=url_for("show_type", type_id=protocoltype.id)), action_text="Anlegen")}}
{{render_form(form, action_url=url_for("new_reminder", protocoltype_id=protocoltype.id, next=url_for("show_type", protocoltype_id=protocoltype.id)), action_text="Anlegen")}}
</div>
{% endblock %}
......@@ -4,6 +4,6 @@
{% block content %}
<div class="container">
{{render_form(form, action_url=url_for("edit_type", type_id=protocoltype.id), action_text="Ändern")}}
{{render_form(form, action_url=url_for("edit_type", protocoltype_id=protocoltype.id), action_text="Ändern")}}
</div>
{% endblock %}
......@@ -80,7 +80,7 @@ class ProtocolsTable(Table):
result.append(Markup(self.search_results[protocol]))
if check_login():
if user is not None and protocol.protocoltype.has_private_view_right(user):
result.append(Table.link(url_for("show_type", type_id=protocol.protocoltype.id), protocol.protocoltype.short_name))
result.append(Table.link(url_for("show_type", protocoltype_id=protocol.protocoltype.id), protocol.protocoltype.short_name))
if protocol.protocoltype.has_admin_right(user):
result.append(Table.link(url_for("delete_protocol", protocol_id=protocol.id), "Löschen", confirm="Bist du dir sicher, dass du das Protokoll {} löschen möchtest?".format(protocol.get_identifier())))
else:
......@@ -99,7 +99,7 @@ class ProtocolTypesTable(Table):
user = current_user()
has_modify_right = protocoltype.has_modify_right(user)
return [
Table.link(url_for("show_type", type_id=protocoltype.id), protocoltype.short_name) if has_modify_right else protocoltype.short_name,
Table.link(url_for("show_type", protocoltype_id=protocoltype.id), protocoltype.short_name) if has_modify_right else protocoltype.short_name,
protocoltype.name,
Table.link(url_for("show_protocol", protocol_id=protocol.id), protocol.get_identifier()) if protocol is not None else "Noch kein Protokoll",
Table.link(url_for("new_protocol", type_id=protocoltype.id), "Neues Protokoll") if has_modify_right else ""
......@@ -108,7 +108,7 @@ class ProtocolTypesTable(Table):
class ProtocolTypeTable(SingleValueTable):
def __init__(self, protocoltype):
super().__init__(protocoltype.name, protocoltype, newlink=url_for("edit_type", type_id=protocoltype.id))
super().__init__(protocoltype.name, protocoltype, newlink=url_for("edit_type", protocoltype_id=protocoltype.id))
def headers(self):
general_headers = ["Name", "Abkürzung", "Organisation", "Beginn",
......@@ -166,7 +166,7 @@ class ProtocolTypeTable(SingleValueTable):
Table.bool(self.value.restrict_networks),
self.value.allowed_networks
]
action_part = [Table.link(url_for("delete_type", type_id=self.value.id), "Löschen", confirm="Bist du dir sicher, dass du den Protokolltype {} löschen möchtest?".format(self.value.name))]
action_part = [Table.link(url_for("delete_type", protocoltype_id=self.value.id), "Löschen", confirm="Bist du dir sicher, dass du den Protokolltype {} löschen möchtest?".format(self.value.name))]
if not self.value.has_admin_right(user):
action_part = [""]
return (general_part + mail_part + printing_part + wiki_part +
......@@ -194,7 +194,7 @@ class DefaultTOPsTable(Table):
class MeetingRemindersTable(Table):
def __init__(self, reminders, protocoltype=None):
super().__init__("Einladungsmails", reminders, newlink=url_for("new_reminder", type_id=protocoltype.id) if protocoltype is not None else None)
super().__init__("Einladungsmails", reminders, newlink=url_for("new_reminder", protocoltype_id=protocoltype.id) if protocoltype is not None else None)
self.protocoltype = protocoltype
def headers(self):
......@@ -208,8 +208,8 @@ class MeetingRemindersTable(Table):
reminder.additional_text or ""
]
action_links = [
Table.link(url_for("edit_reminder", type_id=self.protocoltype.id, reminder_id=reminder.id), "Ändern"),
Table.link(url_for("delete_reminder", type_id=self.protocoltype.id, reminder_id=reminder.id), "Löschen", confirm="Bist du dir sicher, dass du die Einladungsmail {} Tage vor der Sitzung löschen willst?".format(reminder.days_before))
Table.link(url_for("edit_reminder", protocoltype_id=self.protocoltype.id, meetingreminder_id=reminder.id), "Ändern"),
Table.link(url_for("delete_reminder", protocoltype_id=self.protocoltype.id, meetingreminder_id=reminder.id), "Löschen", confirm="Bist du dir sicher, dass du die Einladungsmail {} Tage vor der Sitzung löschen willst?".format(reminder.days_before))
]
action_part = [Table.concat(action_links)]
return general_part + action_part
......@@ -373,7 +373,7 @@ class DefaultMetasTable(Table):
super().__init__(
"Metadatenfelder",
metas,
url_for("new_defaultmeta", type_id=protocoltype.id)
url_for("new_defaultmeta", protocoltype_id=protocoltype.id)
)
def headers(self):
......
Supports Markdown
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