Skip to content

Commit

Permalink
Merge pull request #221 from carinedengler/220
Browse files Browse the repository at this point in the history
added preferences view and allow for updating delimiter (#closes 220)
  • Loading branch information
MrGecko authored Oct 26, 2020
2 parents 90b758e + 6a88718 commit deb2589
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/main/views/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


from app import db

from app.models import CorpusUser, ControlLists, ControlListsUser, WordToken, ChangeRecord, Bookmark, Favorite, User

from .utils import render_template_with_nav_info
from app.utils import ValidationError
from app.utils.forms import create_input_format_convertion, read_input_tokens
Expand All @@ -17,6 +19,7 @@
from ...errors import MissingTokenColumnValue, NoTokensInput
from .utils import requires_corpus_admin_access, requires_corpus_access
from ..forms import Delete
from app.utils import PreferencesUpdateError

AUTOCOMPLETE_LIMIT = 20

Expand Down Expand Up @@ -342,3 +345,40 @@ def search_value_api(corpus_id, allowed_type):
if result is not None
]
)


@main.route("/corpus/<int:corpus_id>/preferences", methods=["GET", "POST"])
@login_required
@requires_corpus_access("corpus_id")
def preferences(corpus_id: int):
"""Show preferences view."""
corpus = Corpus.query.get_or_404(corpus_id)
corpus_user = CorpusUser.query.filter(
CorpusUser.corpus_id == corpus_id,
CorpusUser.user_id == current_user.id
).one_or_none()
if corpus_user:
is_owner = corpus_user.is_owner
else:
is_owner = current_user.id in (admin.id for admin in User.get_admins())
if is_owner and request.method == "POST":
try:
corpus.update_delimiter_token(
delimiter_token=request.form.get("sep_token", "").strip()
)
except PreferencesUpdateError as exception:
flash(
f"Faild to update preferences: {exception}",
category="error"
)
else:
flash(
f"Updated preferences",
category="success"
)
return render_template_with_nav_info(
"main/corpus_preferences.html",
sep_token=corpus.delimiter_token or "",
read_only=not is_owner,
corpus_id=corpus_id
)
18 changes: 18 additions & 0 deletions app/models/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ..utils.forms import strip_or_none
from ..utils.tsv import TSV_CONFIG
from ..errors import MissingTokenColumnValue, NoTokensInput
from app.utils import PreferencesUpdateError
# Models
from .user import User
from .control_lists import ControlLists, AllowedPOS, AllowedMorph, AllowedLemma, PublicationStatus
Expand Down Expand Up @@ -398,6 +399,23 @@ def toggle_favorite(self, user_id: int) -> bool:
)
db.session.commit()

def update_delimiter_token(self, delimiter_token: Optional[str] = None):
"""Update delimiter token.
:param str delimiter_token: token
"""
if delimiter_token != self.delimiter_token:
try:
self.delimiter_token = delimiter_token
db.session.commit()
except Exception:
db.session.rollback()
raise PreferencesUpdateError(
f"cannot set delimiter token to '{delimiter_token}'"
)



class WordToken(db.Model):
""" A word token is a word from a corpus with primary annotation
Expand Down
3 changes: 3 additions & 0 deletions app/templates/macros/nav_macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
<a id="overview_{{corpus.id}}" href="{{url_for("main.corpus_get", corpus_id=corpus.id)}}">
{{corpus.name}}</a>
</header>
<a class="nav-link" href="{{url_for("main.preferences", corpus_id=corpus.id)}}">
<i class="fa fa-cog"></i> Preferences
</a>
Quick links
<ul class="nav flex-column">
<li class="nav-item">
Expand Down
31 changes: 31 additions & 0 deletions app/templates/main/corpus_preferences.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "layouts/base.html" %}
{% block content %}
<form method="POST" action="{{ url_for("main.preferences", corpus_id=corpus_id) }}">
<h1>
<i class="fa fa-cog"></i> Preferences
</h1>
<fieldset class="form-fieldset">
<legend>Metadata</legend>
<div class="form-group">
<label for="sep_token">Delimiter token</label>
<input
name="sep_token"
type="text"
class="form-control"
id="sep_token"
aria-describedby="sep_tokenHelp"
value="{{ sep_token }}"
{% if read_only %}
readOnly="true"
{% endif %}
/>
<small id="sep_tokenHelp" class="form-text text-muted">
[Optional] A specific token that is used as a delimiter
between passages.
</small>
</div>
</fieldset>
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
5 changes: 4 additions & 1 deletion app/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class PyrrhaError(Exception):
"""Raised when errors specific to this application occur."""
pass

class PreferencesUpdateError(PyrrhaError):
"""Raised when preferences could not be updated."""
pass

class ValidationError(PyrrhaError):
"""Raised when constraints on data types are not respected."""
pass


def validate_length(k: str, v: str, lengths: Dict[str, int]):
"""Enforce length constraints on data types to ensure
portability of the database schema.
Expand All @@ -30,3 +32,4 @@ def validate_length(k: str, v: str, lengths: Dict[str, int]):
raise ValidationError(
f"column '{k}': '{v}' is too long (maximum {lengths[k]} characters)"
)

0 comments on commit deb2589

Please sign in to comment.