From fa5e52fd3a5de6b096c44590f5eb2c315e82ee56 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 7 Jun 2017 14:25:27 +0100 Subject: [PATCH] add title editing --- sourcing/model.py | 10 ++++++---- sourcing/templates/view.html | 17 ++++++++++++----- sourcing/view.py | 12 +++++------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/sourcing/model.py b/sourcing/model.py index 8c55023..1fd1759 100644 --- a/sourcing/model.py +++ b/sourcing/model.py @@ -181,8 +181,8 @@ class Item(TimeStampedModel): return url_for('.edit_item', username=self.user.username, hashid=self.hashid) @property - def add_title_url(self): - return url_for('.add_title', username=self.user.username, hashid=self.hashid) + def set_title_url(self): + return url_for('.set_title', username=self.user.username, hashid=self.hashid) def title(self, titles=None): if not titles: @@ -191,10 +191,12 @@ class Item(TimeStampedModel): def has_title(self): titles = XanaLink.get_all_titles() - print(titles) return self in titles - def add_title(self, title, user): + def title_from_link(self): + return XanaLink.get_all_titles().get(self) + + def set_title(self, title, user): title_source_doc = SourceDoc(text=title, user=user) session.add(title_source_doc) session.commit() diff --git a/sourcing/templates/view.html b/sourcing/templates/view.html index 98b1b05..3b4cd4d 100644 --- a/sourcing/templates/view.html +++ b/sourcing/templates/view.html @@ -4,6 +4,8 @@ {% block content %} +{% if current_user.is_authenticated and doc.user == current_user %} +{% set title = doc.title_from_link() %} +{% endif %}

{{ self.title() }} - {% if current_user.is_authenticated and doc.user == current_user and not doc.has_title() %} - + {% if current_user.is_authenticated and doc.user == current_user %} + {% endif %}

{% if version %} diff --git a/sourcing/view.py b/sourcing/view.py index b50d58e..4ab9664 100644 --- a/sourcing/view.py +++ b/sourcing/view.py @@ -199,12 +199,12 @@ def fulfil(username, hashid): item=item, doc=fulfil_edl_with_sources(item.text)) -@bp.route('///add_title', methods=['POST']) -def add_title(username, hashid): +@bp.route('///set_title', methods=['POST']) +def set_title(username, hashid): item = get_item(username, hashid) - title = request.form['title'] - item.add_title(title, current_user) - flash('title added') + has_title = item.has_title + item.set_title(request.form['title'], current_user) + flash('title change saved' if has_title else 'title added') return redirect(item.url) @bp.route('//') @@ -255,7 +255,6 @@ def edit_item(username, hashid): form = SourceDocForm(obj=obj) if form.validate_on_submit(): form.populate_obj(obj) - session.add(obj) session.commit() flash('Changes to {} saved.'.format(obj.type)) return redirect(obj.url) @@ -273,7 +272,6 @@ def account_settings(): form = AccountSettingsForm(obj=current_user) if form.validate_on_submit(): form.populate_obj(current_user) - session.add(current_user) session.commit() flash('Account details updated.') return redirect(url_for(request.endpoint))