mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
Merge branch 'public-corpus' of gitlab.ub.uni-bielefeld.de:sfb1288inf/nopaque into public-corpus
This commit is contained in:
commit
288014969a
@ -1,4 +1,4 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from flask import (
|
from flask import (
|
||||||
abort,
|
abort,
|
||||||
current_app,
|
current_app,
|
||||||
@ -8,7 +8,8 @@ from flask import (
|
|||||||
redirect,
|
redirect,
|
||||||
render_template,
|
render_template,
|
||||||
request,
|
request,
|
||||||
send_from_directory
|
send_from_directory,
|
||||||
|
url_for
|
||||||
)
|
)
|
||||||
from flask_login import current_user, login_required
|
from flask_login import current_user, login_required
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
@ -63,22 +64,20 @@ def disable_corpus_is_public(corpus_id):
|
|||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
# @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):
|
||||||
# try:
|
try:
|
||||||
# payload = jwt.decode(
|
payload = jwt.decode(
|
||||||
# token,
|
token,
|
||||||
# current_app.config['SECRET_KEY'],
|
current_app.config['SECRET_KEY'],
|
||||||
# algorithms=['HS256'],
|
algorithms=['HS256'],
|
||||||
# issuer=current_app.config['SERVER_NAME'],
|
issuer=current_app.config['SERVER_NAME'],
|
||||||
# options={'require': ['iat', 'iss', 'sub']}
|
options={'require': ['iat', 'iss', 'sub']}
|
||||||
# )
|
)
|
||||||
# except jwt.PyJWTError:
|
except jwt.PyJWTError:
|
||||||
# return False
|
return False
|
||||||
# corpus_hashid = payload.get('sub')
|
return redirect(url_for('.corpus', corpus_id=corpus_id))
|
||||||
# corpus_id = hashids.decode(corpus_hashid)
|
|
||||||
# return redirect(url_for('.corpus', corpus_id=corpus_id))
|
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/unfollow', methods=['POST'])
|
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/unfollow', methods=['POST'])
|
||||||
@ -176,27 +175,16 @@ def create_corpus():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>', methods=['GET', 'POST'])
|
@bp.route('/<hashid:corpus_id>')
|
||||||
@login_required
|
@login_required
|
||||||
def corpus(corpus_id):
|
def corpus(corpus_id):
|
||||||
corpus = Corpus.query.get_or_404(corpus_id)
|
corpus = Corpus.query.get_or_404(corpus_id)
|
||||||
|
exp_date = (datetime.utcnow() + timedelta(days=7)).strftime('%b %d, %Y')
|
||||||
if corpus.user == current_user or current_user.is_administrator():
|
if corpus.user == current_user or current_user.is_administrator():
|
||||||
# now = datetime.utcnow()
|
|
||||||
# payload = {
|
|
||||||
# 'exp': now + timedelta(weeks=1),
|
|
||||||
# 'iat': now,
|
|
||||||
# 'iss': current_app.config['SERVER_NAME'],
|
|
||||||
# 'sub': corpus.hashid
|
|
||||||
# }
|
|
||||||
# token = jwt.encode(
|
|
||||||
# payload,
|
|
||||||
# current_app.config['SECRET_KEY'],
|
|
||||||
# algorithm='HS256'
|
|
||||||
# )
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'corpora/corpus.html.j2',
|
'corpora/corpus.html.j2',
|
||||||
corpus=corpus,
|
corpus=corpus,
|
||||||
# token=token,
|
exp_date=exp_date,
|
||||||
title='Corpus'
|
title='Corpus'
|
||||||
)
|
)
|
||||||
if current_user.is_following_corpus(corpus) or corpus.is_public:
|
if current_user.is_following_corpus(corpus) or corpus.is_public:
|
||||||
@ -209,6 +197,28 @@ def corpus(corpus_id):
|
|||||||
)
|
)
|
||||||
abort(403)
|
abort(403)
|
||||||
|
|
||||||
|
@bp.route('/<hashid:corpus_id>/generate-corpus-share-link', methods=['GET', 'POST'])
|
||||||
|
@login_required
|
||||||
|
def generate_corpus_share_link(corpus_id):
|
||||||
|
data = request.get_json('data')
|
||||||
|
permission = data['permission']
|
||||||
|
expiration = data['expiration']
|
||||||
|
corpus = Corpus.query.get_or_404(corpus_id)
|
||||||
|
now = datetime.utcnow()
|
||||||
|
payload = {
|
||||||
|
'exp': expiration,
|
||||||
|
'iat': now,
|
||||||
|
'iss': current_app.config['SERVER_NAME'],
|
||||||
|
'sub': permission
|
||||||
|
}
|
||||||
|
token = jwt.encode(
|
||||||
|
payload,
|
||||||
|
current_app.config['SECRET_KEY'],
|
||||||
|
algorithm='HS256'
|
||||||
|
)
|
||||||
|
link = url_for('corpora.follow_corpus', corpus_id=corpus_id, token=token, _external=True)
|
||||||
|
return link
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>', methods=['DELETE'])
|
@bp.route('/<hashid:corpus_id>', methods=['DELETE'])
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -777,4 +777,34 @@ class Utils {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static generateCorpusShareLinkRequest(corpusId, permission, expiration) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const data = {permission: permission, expiration: expiration};
|
||||||
|
fetch(`/corpora/${corpusId}/generate-corpus-share-link`, {method: 'POST', headers: {Accept: 'text/plain'}, body: JSON.stringify(data)})
|
||||||
|
.then(
|
||||||
|
(response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
app.flash(`Something went wrong: ${response.status} ${response.statusText}`, 'error');
|
||||||
|
reject(response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return response.text();
|
||||||
|
},
|
||||||
|
(response) => {
|
||||||
|
// Something went wrong during the HTTP request
|
||||||
|
app.flash('Something went wrong', 'error');
|
||||||
|
reject(response);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.then(
|
||||||
|
(corpusShareLink) => {resolve(corpusShareLink);},
|
||||||
|
(error) => {
|
||||||
|
// Something went wrong during ReadableStream processing
|
||||||
|
app.flash('Something went wrong', 'error');
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,10 @@
|
|||||||
<div class="col s12">
|
<div class="col s12">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
<div class="action-switch switch center-align" data-action="toggle-is-public">
|
<span class="card-title">Share your Corpus</span>
|
||||||
|
<br>
|
||||||
|
<p></p>
|
||||||
|
<div class="action-switch switch" data-action="toggle-is-public">
|
||||||
<span class="share"></span>
|
<span class="share"></span>
|
||||||
<label>
|
<label>
|
||||||
<input {% if corpus.is_public %}checked{% endif %} type="checkbox">
|
<input {% if corpus.is_public %}checked{% endif %} type="checkbox">
|
||||||
@ -91,14 +94,38 @@
|
|||||||
public
|
public
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
<br>
|
||||||
{# <a class="action-button btn waves-effect waves-light" id="generate-share-link-button">Generate Share Link</a>
|
<p></p>
|
||||||
<div id="share-link"></div>
|
<div class="row">
|
||||||
<a class="action-button btn-small waves-effect waves-light hide" id="copy-share-link-button">Copy</a> #}
|
<div class="col s4">
|
||||||
|
<div class="input-field">
|
||||||
|
<select id="permission-select">
|
||||||
|
<option value="view" selected>View</option>
|
||||||
|
<option value="contribute">Contribute</option>
|
||||||
|
<option value="administrate">Administrate</option>
|
||||||
|
</select>
|
||||||
|
<label>Permission</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col s4">
|
||||||
|
<div class="input-field">
|
||||||
|
<input type="text" class="datepicker" value="{{exp_date}}" id="expiration">
|
||||||
|
<label for="expiration-date">Expiration date</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col s12">
|
||||||
|
<a class="action-button btn waves-effect waves-light" id="generate-share-link-button">Generate Share Link</a>
|
||||||
|
<div id="share-link"></div>
|
||||||
|
<a class="action-button btn-small waves-effect waves-light hide" id="copy-share-link-button">Copy</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col s12">
|
<div class="col s12">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
@ -115,19 +142,23 @@
|
|||||||
{{ super() }}
|
{{ super() }}
|
||||||
<script>
|
<script>
|
||||||
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
|
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
|
||||||
</script>
|
|
||||||
{# <script>
|
|
||||||
let generateShareLinkButton = document.querySelector('#generate-share-link-button');
|
let generateShareLinkButton = document.querySelector('#generate-share-link-button');
|
||||||
let copyShareLinkButton = document.querySelector('#copy-share-link-button');
|
let copyShareLinkButton = document.querySelector('#copy-share-link-button');
|
||||||
let shareLink = document.querySelector('#share-link');
|
let shareLink = document.querySelector('#share-link');
|
||||||
let linkValue = '{{ url_for('corpora.share_corpus', token=token, _external=True) }}';
|
let permissionSelect = document.querySelector('#permission-select');
|
||||||
|
let expirationDate = document.querySelector('#expiration');
|
||||||
|
|
||||||
|
|
||||||
generateShareLinkButton.addEventListener('click', () => {
|
generateShareLinkButton.addEventListener('click', () => {
|
||||||
let shareLinkElement = document.createElement('input');
|
Utils.generateCorpusShareLinkRequest('{{ corpus.hashid }}', permissionSelect.value, expirationDate.value)
|
||||||
shareLinkElement.value = linkValue;
|
.then((corpusShareLink) => {
|
||||||
shareLinkElement.setAttribute('readonly', '');
|
console.log(corpusShareLink);
|
||||||
shareLink.appendChild(shareLinkElement);
|
let shareLinkElement = document.createElement('input');
|
||||||
copyShareLinkButton.classList.remove('hide');
|
shareLinkElement.value = corpusShareLink;
|
||||||
|
shareLinkElement.setAttribute('readonly', '');
|
||||||
|
shareLink.appendChild(shareLinkElement);
|
||||||
|
copyShareLinkButton.classList.remove('hide');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
copyShareLinkButton.addEventListener('click', () => {
|
copyShareLinkButton.addEventListener('click', () => {
|
||||||
@ -136,5 +167,5 @@
|
|||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
app.flash(`Copied!`, 'success');
|
app.flash(`Copied!`, 'success');
|
||||||
});
|
});
|
||||||
</script> #}
|
</script>
|
||||||
{% endblock scripts %}
|
{% endblock scripts %}
|
||||||
|
Loading…
Reference in New Issue
Block a user