add title editing

This commit is contained in:
Edward Betts 2017-06-07 14:25:27 +01:00
parent 8a60c1000b
commit fa5e52fd3a
3 changed files with 23 additions and 16 deletions

View file

@ -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()

View file

@ -4,6 +4,8 @@
{% block content %}
{% if current_user.is_authenticated and doc.user == current_user %}
{% set title = doc.title_from_link() %}
<div class="modal fade" id="title-modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
@ -13,19 +15,24 @@
<span aria-hidden="true">&times;</span>
</button>
</div>
<form method="POST" action="{{ doc.add_title_url }}">
<form method="POST" action="{{ doc.set_title_url }}">
<div class="modal-body">
<label for="form-title">title</label>
{% if title %}
<input name="title" id="form-title" class="form-control" value="{{ title }}">
{% else %}
<input name="title" id="form-title" class="form-control">
{% endif %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add title</button>
<button type="submit" class="btn btn-primary">{{ 'Edit' if title else 'Add' }} title</button>
</div>
</form>
</div>
</div>
</div>
{% endif %}
<ul class="nav nav-tabs">
<li class="nav-item">
@ -48,8 +55,8 @@
</ul>
<h1 class="mt-3">{{ self.title() }}
{% if current_user.is_authenticated and doc.user == current_user and not doc.has_title() %}
<button class="btn btn-primary" data-toggle="modal" data-target="#title-modal">add title</button>
{% if current_user.is_authenticated and doc.user == current_user %}
<button class="btn btn-primary" data-toggle="modal" data-target="#title-modal">{{ 'edit' if title else 'add' }} title</button>
{% endif %}
</h1>
{% if version %}

View file

@ -199,12 +199,12 @@ def fulfil(username, hashid):
item=item,
doc=fulfil_edl_with_sources(item.text))
@bp.route('/<username>/<hashid>/add_title', methods=['POST'])
def add_title(username, hashid):
@bp.route('/<username>/<hashid>/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('/<username>/<hashid>')
@ -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))