From 726e781692f5971e65ed460e6e8d070326c19e9f Mon Sep 17 00:00:00 2001 From: Inga Kirschnick Date: Tue, 21 Feb 2023 16:18:04 +0100 Subject: [PATCH] share link generator update --- app/corpora/routes.py | 74 ++++++++++++++++------------ app/static/js/Utils.js | 30 +++++++++++ app/templates/corpora/corpus.html.j2 | 65 +++++++++++++++++------- 3 files changed, 120 insertions(+), 49 deletions(-) diff --git a/app/corpora/routes.py b/app/corpora/routes.py index 8d2dd45f..55de99f3 100644 --- a/app/corpora/routes.py +++ b/app/corpora/routes.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from flask import ( abort, current_app, @@ -7,7 +7,8 @@ from flask import ( redirect, render_template, request, - send_from_directory + send_from_directory, + url_for ) from flask_login import current_user, login_required from threading import Thread @@ -52,22 +53,20 @@ def disable_corpus_is_public(corpus_id): return '', 204 -# @bp.route('//follow/') -# @login_required -# def follow_corpus(corpus_id, 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('//follow/') +@login_required +def follow_corpus(corpus_id, 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 + return redirect(url_for('.corpus', corpus_id=corpus_id)) @bp.route('//unfollow', methods=['GET', 'POST']) @@ -157,27 +156,16 @@ def create_corpus(): ) -@bp.route('/', methods=['GET', 'POST']) +@bp.route('/') @login_required def corpus(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(): - # 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( 'corpora/corpus.html.j2', corpus=corpus, - # token=token, + exp_date=exp_date, title='Corpus' ) if current_user.is_following_corpus(corpus) or corpus.is_public: @@ -190,6 +178,28 @@ def corpus(corpus_id): ) abort(403) +@bp.route('//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('/', methods=['DELETE']) @login_required diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js index e2ac84ab..b3da4ca1 100644 --- a/app/static/js/Utils.js +++ b/app/static/js/Utils.js @@ -751,4 +751,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); + } + ); + }); + } } diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index 34228347..c84d6c0e 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -80,10 +80,13 @@ - {#
+
-
+ Share your Corpus +
+

+
- - Generate Share Link - - Copy +
+

+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
-
@@ -106,7 +133,7 @@
-
#} +
{% endblock page_content %} @@ -115,19 +142,23 @@ {{ super() }} -{# #} + {% endblock scripts %}