bug fixes and unfollow_corpus update

This commit is contained in:
Inga Kirschnick 2023-03-06 12:27:24 +01:00
parent 8a55ce902e
commit fecbb50d39
5 changed files with 16 additions and 18 deletions

View File

@ -15,7 +15,7 @@ from flask_login import current_user, login_required
from threading import Thread from threading import Thread
import os import os
from .decorators import corpus_follower_permission_required, corpus_owner_or_admin_required from .decorators import corpus_follower_permission_required, corpus_owner_or_admin_required
from app import db from app import db, hashids
from app.models import ( from app.models import (
Corpus, Corpus,
CorpusFile, CorpusFile,
@ -76,6 +76,7 @@ def add_corpus_followers(corpus_id):
@bp.route('/<hashid:corpus_id>/follow/<token>') @bp.route('/<hashid:corpus_id>/follow/<token>')
@login_required @login_required
def follow_corpus(corpus_id, token): def follow_corpus(corpus_id, token):
corpus = Corpus.query.get_or_404(corpus_id)
if current_user.follow_corpus_by_token(token): if current_user.follow_corpus_by_token(token):
db.session.commit() db.session.commit()
flash(f'You are following {corpus.title} now', category='corpus') flash(f'You are following {corpus.title} now', category='corpus')
@ -98,20 +99,8 @@ def unfollow_corpus(corpus_id, follower_id):
return '', 204 return '', 204
@bp.route('/<hashid:corpus_id>/unfollow', methods=['POST'])
@login_required
def current_user_unfollow_corpus(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
if not current_user.is_following_corpus(corpus):
abort(409) # 'You are not following the corpus'
current_user.unfollow_corpus(corpus)
db.session.commit()
flash(f'You are not following {corpus.title} anymore', category='corpus')
return '', 204
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/role', methods=['POST']) @bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/role', methods=['POST'])
@corpus_follower_permission_required('REMOVE_FOLLOWER', 'UPDATE_FOLLOWER') @corpus_follower_permission_required('UPDATE_FOLLOWER')
def add_permission(corpus_id, follower_id): def add_permission(corpus_id, follower_id):
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404() corpus_follower_association = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404()
if not (corpus_follower_association.corpus.user == current_user or current_user.is_administrator()): if not (corpus_follower_association.corpus.user == current_user or current_user.is_administrator()):
@ -196,11 +185,12 @@ def corpus(corpus_id):
@login_required @login_required
@corpus_follower_permission_required('GENERATE_SHARE_LINK') @corpus_follower_permission_required('GENERATE_SHARE_LINK')
def generate_corpus_share_link(corpus_id): def generate_corpus_share_link(corpus_id):
corpus_hashid = hashids.encode(corpus_id)
data = request.get_json('data') data = request.get_json('data')
role = data['role'] role_name = data['role']
exp_data = data['expiration'] exp_data = data['expiration']
expiration = datetime.strptime(exp_data, '%b %d, %Y') expiration = datetime.strptime(exp_data, '%b %d, %Y')
token = current_user.generate_follow_corpus_token(corpus_id, role, expiration) token = current_user.generate_follow_corpus_token(corpus_hashid, role_name, expiration)
link = url_for('corpora.follow_corpus', corpus_id=corpus_id, token=token, _external=True) link = url_for('corpora.follow_corpus', corpus_id=corpus_id, token=token, _external=True)
return link return link

View File

@ -12,6 +12,8 @@
<a class="modal-trigger" href="#cql-tutorial-modal" style="margin-left: 40px;"><i class="material-icons" style="font-size: inherit;">help</i> Corpus Query Language tutorial</a> <a class="modal-trigger" href="#cql-tutorial-modal" style="margin-left: 40px;"><i class="material-icons" style="font-size: inherit;">help</i> Corpus Query Language tutorial</a>
<span> | </span> <span> | </span>
<a class="modal-trigger" href="#tagsets-modal"><i class="material-icons" style="font-size: inherit;">info</i> Tagsets</a> <a class="modal-trigger" href="#tagsets-modal"><i class="material-icons" style="font-size: inherit;">info</i> Tagsets</a>
<span> | </span>
<a class="modal-trigger" href="#example-modal"><i class="material-icons" style="font-size: inherit;">info</i> Examples</a>
</div> </div>
<div class="input-field col s12 m3"> <div class="input-field col s12 m3">
<i class="material-icons prefix">arrow_forward</i> <i class="material-icons prefix">arrow_forward</i>

View File

@ -245,6 +245,12 @@
</div> </div>
</div> </div>
<div class="modal" id="example-modal">
<div class="modal-content">
<h4>Examples</h4>
</div>
</div>
<div class="modal" id="concordance-query-builder"> <div class="modal" id="concordance-query-builder">
<div class="modal-content"> <div class="modal-content">
<div> <div>

View File

@ -166,7 +166,7 @@
<i class="material-icons prefix">badge</i> <i class="material-icons prefix">badge</i>
<select id="share-link-modal-corpus-follower-role-select"> <select id="share-link-modal-corpus-follower-role-select">
{% for corpus_follower_role in corpus_follower_roles %} {% for corpus_follower_role in corpus_follower_roles %}
<option value="{{ corpus_follower_role.hashid }}">{{ corpus_follower_role.name }}</option> <option value="{{ corpus_follower_role.name }}">{{ corpus_follower_role.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
<label>Role</label> <label>Role</label>

View File

@ -111,7 +111,7 @@
let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]'); let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]');
unfollowRequestElement.addEventListener('click', () => { unfollowRequestElement.addEventListener('click', () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch('{{ url_for("corpora.current_user_unfollow_corpus", corpus_id=corpus.id) }}', {method: 'POST', headers: {Accept: 'application/json'}}) fetch('{{ url_for("corpora.unfollow_corpus", corpus_id=corpus.id, follower_id=current_user.id) }}', {method: 'POST', headers: {Accept: 'application/json'}})
.then( .then(
(response) => { (response) => {
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}