Corpus first share link

This commit is contained in:
Inga Kirschnick 2023-02-10 09:37:31 +01:00
parent e03b54cfcd
commit 33a54b6206
3 changed files with 59 additions and 17 deletions

View File

@ -1,3 +1,4 @@
from datetime import datetime, timedelta
from flask import ( from flask import (
abort, abort,
current_app, current_app,
@ -11,6 +12,7 @@ from flask import (
) )
from flask_login import current_user, login_required from flask_login import current_user, login_required
from threading import Thread from threading import Thread
import jwt
import os import os
from app import db, hashids from app import db, hashids
from app.models import Corpus, CorpusFile, CorpusStatus, CorpusFollowerAssociation, User from app.models import Corpus, CorpusFile, CorpusStatus, CorpusFollowerAssociation, User
@ -57,6 +59,7 @@ def create_corpus():
@bp.route('/<hashid:corpus_id>', methods=['GET', 'POST']) @bp.route('/<hashid:corpus_id>', methods=['GET', 'POST'])
@login_required @login_required
def corpus(corpus_id): def corpus(corpus_id):
print(corpus_id)
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.user == current_user if not (corpus.user == current_user
or current_user.is_administrator() or current_user.is_administrator()
@ -72,11 +75,23 @@ def corpus(corpus_id):
db.session.commit() db.session.commit()
flash('Your changes have been saved') flash('Your changes have been saved')
return redirect(url_for('.corpus', corpus_id=corpus.id)) return redirect(url_for('.corpus', corpus_id=corpus.id))
now = datetime.utcnow()
payload = {
'iat': now,
'iss': current_app.config['SERVER_NAME'],
'sub': corpus.hashid
}
token = jwt.encode(
payload,
current_app.config['SECRET_KEY'],
algorithm='HS256'
)
if corpus.user == current_user: if corpus.user == current_user:
return render_template( return render_template(
'corpora/corpus.html.j2', 'corpora/corpus.html.j2',
corpus_settings_form=corpus_settings_form, corpus_settings_form=corpus_settings_form,
corpus=corpus, corpus=corpus,
token=token,
title='Corpus' title='Corpus'
) )
else: else:
@ -88,6 +103,21 @@ def corpus(corpus_id):
title='Corpus' title='Corpus'
) )
@bp.route('/share/<token>', methods=['GET', 'POST'])
def share_corpus(token):
try:
payload = jwt.decode(
token,
current_app.config['SECRET_KEY'],
algorithms=['HS256'],
issuer=current_app.config['SERVER_NAME'],
options={'require': ['iat', 'iss', 'sub']}
)
except jwt.PyJWTError:
return False
corpus_hashid = payload.get('sub')
corpus_id = hashids.decode(corpus_hashid)
return redirect(url_for('.corpus', corpus_id=corpus_id))
# @bp.route('/<hashid:corpus_id>/update') # @bp.route('/<hashid:corpus_id>/update')
@ -296,7 +326,7 @@ def follow_corpus(corpus_id):
if not user.is_following_corpus(corpus): if not user.is_following_corpus(corpus):
user.follow_corpus(corpus) user.follow_corpus(corpus)
db.session.commit() db.session.commit()
# flash('Hallo Inga Kirschnick') flash(f'You are following {corpus.title} now', category='corpus')
return {}, 202 return {}, 202
@bp.route('/<hashid:corpus_id>/unfollow', methods=['GET', 'POST']) @bp.route('/<hashid:corpus_id>/unfollow', methods=['GET', 'POST'])
@ -315,6 +345,7 @@ def unfollow_corpus(corpus_id):
if user.is_following_corpus(corpus): if user.is_following_corpus(corpus):
user.unfollow_corpus(corpus) user.unfollow_corpus(corpus)
db.session.commit() db.session.commit()
flash(f'You are not following {corpus.title} anymore', category='corpus')
return {}, 202 return {}, 202
@bp.route('/add_permission/<hashid:corpus_id>/<hashid:user_id>/<int:permission>') @bp.route('/add_permission/<hashid:corpus_id>/<hashid:user_id>/<int:permission>')

View File

@ -86,21 +86,13 @@
</div> </div>
{% if current_user.can(Permission.ADMINISTRATE) or current_user.hashid == corpus.user.hashid %} {% if current_user.can(Permission.ADMINISTRATE) or current_user.hashid == corpus.user.hashid %}
<div class="col s12"> <div class="col s12">
<form method="POST">
{{ corpus_settings_form.hidden_tag() }}
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
<span class="card-title" id="files">Corpus settings</span> <a class="action-button btn waves-effect waves-light" id="generate-share-link-button">Generate Share Link</a>
<br> <div id="share-link"></div>
<p></p> <a class="action-button btn-small waves-effect waves-light hide" id="copy-share-link-button">Copy</a>
{{ wtf.render_field(corpus_settings_form.is_public) }}
<br>
</div>
<div class="card-action right-align">
{{ wtf.render_field(corpus_settings_form.submit, material_icon='send') }}
</div> </div>
</div> </div>
</form>
</div> </div>
{% endif %} {% endif %}
<div class="col s12"> <div class="col s12">
@ -119,5 +111,24 @@
{{ super() }} {{ super() }}
<script> <script>
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display')); let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
let generateShareLinkButton = document.querySelector('#generate-share-link-button');
let copyShareLinkButton = document.querySelector('#copy-share-link-button');
let shareLink = document.querySelector('#share-link');
let linkValue = '{{ url_for('corpora.share_corpus', token=token, _external=True) }}';
generateShareLinkButton.addEventListener('click', () => {
let shareLinkElement = document.createElement('input');
shareLinkElement.value = linkValue;
shareLinkElement.setAttribute('readonly', '');
shareLink.appendChild(shareLinkElement);
copyShareLinkButton.classList.remove('hide');
});
copyShareLinkButton.addEventListener('click', () => {
let shareLinkElement = document.querySelector('#share-link input');
shareLinkElement.select();
document.execCommand('copy');
app.flash(`Copied!`, 'success');
});
</script> </script>
{% endblock scripts %} {% endblock scripts %}

View File

@ -7,7 +7,7 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
<h1>{{ title }} </h1> <h1>{{ corpus.title }} </h1>
<div class="row"> <div class="row">
<div class="col s8 m9 l10"> <div class="col s8 m9 l10">
<a class="btn waves-effect waves-light" id="follow-corpus-request"> <a class="btn waves-effect waves-light" id="follow-corpus-request">