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

Create LocalTOPs on creation of their parents

Create LocalTOPs when creating Protocols or DefaultTOPs, not on demand
when necessary, since that created double entries.

reference #96
parent a2eb028c
......@@ -146,7 +146,7 @@ class Protocol(DatabaseModel):
date = db.Column(db.Date)
start_time = db.Column(db.Time)
end_time = db.Column(db.Time)
done = db.Column(db.Boolean)
done = db.Column(db.Boolean, nullable=False, default=False)
public = db.Column(db.Boolean)
pad_identifier = db.Column(db.String)
......@@ -167,6 +167,13 @@ class Protocol(DatabaseModel):
return Error(protocol_id=self.id, action=action, name=name,
datetime=now, description=description)
def create_localtops(self):
local_tops = []
for default_top in self.protocoltype.default_tops:
local_tops.append(LocalTOP(defaulttop_id=defaul_top.id,
protocol_id=self.id, description=""))
return local_tops
def fill_from_remarks(self, remarks):
def _date_or_lazy(key, get_date=False, get_time=False):
formats = []
......@@ -320,14 +327,8 @@ class DefaultTOP(DatabaseModel):
return self.number > 0
def get_localtop(self, protocol):
localtop = LocalTOP.query.filter_by(defaulttop_id=self.id,
return LocalTOP.query.filter_by(defaulttop_id=self.id,
protocol_id=protocol.id).first()
if localtop is None:
localtop = LocalTOP(protocol_id=protocol.id, defaulttop_id=self.id,
description="")
db.session.add(localtop)
db.session.commit()
return localtop
def get_top(self, protocol):
localtop = self.get_localtop(protocol)
......
......@@ -307,6 +307,12 @@ def new_default_top(protocoltype):
form.populate_obj(defaulttop)
db.session.add(defaulttop)
db.session.commit()
for protocol in protocoltype.protocols:
if not protocol.done:
localtop = LocalTOP(protocol_id=protocol.id,
defaulttop_id=defaultop.id, description="")
db.session.add(localtop)
db.session.commit()
flash("Der Standard-TOP {} wurde für dem Protokolltyp {} hinzugefügt.".format(defaulttop.name, protocoltype.name), "alert-success")
return redirect(request.args.get("next") or url_for("index"))
return render_template("default-top-new.html", form=form, protocoltype=protocoltype)
......@@ -471,6 +477,9 @@ def new_protocol():
form.populate_obj(protocol)
db.session.add(protocol)
db.session.commit()
for local_top in protocol.create_localtops:
db.session.add(local_top)
db.session.commit()
tasks.push_tops_to_calendar(protocol)
return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol.id))
type_id = request.args.get("protocoltype_id")
......@@ -570,6 +579,9 @@ def upload_new_protocol():
protocol = Protocol(protocoltype_id=protocoltype.id, source=source)
db.session.add(protocol)
db.session.commit()
for local_top in protocol.create_localtops:
db.session.add(local_top)
db.session.commit()
tasks.parse_protocol(protocol)
return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol.id))
return redirect(request.args.get("fail") or url_for("new_protocol"))
......@@ -596,6 +608,9 @@ def upload_new_protocol_by_file():
protocol = Protocol(protocoltype_id=protocoltype.id, date=datetime.now().date(), done=True)
db.session.add(protocol)
db.session.commit()
for local_top in protocol.create_localtops:
db.session.add(local_top)
db.session.commit()
document = Document(protocol_id=protocol.id, name=filename,
filename="", is_compiled=False)
form.populate_obj(document)
......
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