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) return url_for('.edit_item', username=self.user.username, hashid=self.hashid)
@property @property
def add_title_url(self): def set_title_url(self):
return url_for('.add_title', username=self.user.username, hashid=self.hashid) return url_for('.set_title', username=self.user.username, hashid=self.hashid)
def title(self, titles=None): def title(self, titles=None):
if not titles: if not titles:
@ -191,10 +191,12 @@ class Item(TimeStampedModel):
def has_title(self): def has_title(self):
titles = XanaLink.get_all_titles() titles = XanaLink.get_all_titles()
print(titles)
return self in 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) title_source_doc = SourceDoc(text=title, user=user)
session.add(title_source_doc) session.add(title_source_doc)
session.commit() session.commit()

View file

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

View file

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