From e2ddbf26f1faf6d9b3adcdfa48bd8279514160fa Mon Sep 17 00:00:00 2001 From: Inga Kirschnick Date: Fri, 24 Feb 2023 15:22:26 +0100 Subject: [PATCH] Update User Card and Share link with specific role --- app/corpora/routes.py | 15 ++++-- app/models.py | 8 ++-- .../js/RessourceDisplays/CorpusDisplay.js | 5 +- app/static/js/Utils.js | 6 +-- app/templates/corpora/corpus.html.j2 | 14 +++--- app/templates/corpora/public_corpus.html.j2 | 46 +++++++++++++++++-- 6 files changed, 69 insertions(+), 25 deletions(-) diff --git a/app/corpora/routes.py b/app/corpora/routes.py index fd718145..ecb74cae 100644 --- a/app/corpora/routes.py +++ b/app/corpora/routes.py @@ -68,11 +68,12 @@ def disable_corpus_is_public(corpus_id): @bp.route('//follow/') @login_required def follow_corpus(corpus_id, token): - corpus = Corpus.query.get_or_404(corpus_id) + corpus = current_user.verify_follow_corpus_token(token)['corpus'] + role = current_user.verify_follow_corpus_token(token)['role'] if not (current_user.is_authenticated and current_user.verify_follow_corpus_token(token)): abort(403) if not current_user.is_following_corpus(corpus) and current_user != corpus.user: - current_user.follow_corpus(corpus) + current_user.follow_corpus(corpus, role) db.session.commit() flash(f'You are following {corpus.title} now', category='corpus') return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) @@ -164,20 +165,24 @@ def create_corpus(): def corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) exp_date = (datetime.utcnow() + timedelta(days=7)).strftime('%b %d, %Y') + roles = [x.name for x in CorpusFollowerRole.query.all()] if corpus.user == current_user or current_user.is_administrator(): return render_template( 'corpora/corpus.html.j2', corpus=corpus, exp_date=exp_date, + roles=roles, title='Corpus' ) if current_user.is_following_corpus(corpus) or corpus.is_public: corpus_files = [x.to_json_serializeable() for x in corpus.files] + owner = corpus.user.to_json_serializeable() return render_template( 'corpora/public_corpus.html.j2', corpus=corpus, corpus_files=corpus_files, - title='Corpus' + owner=owner, + title='Corpus', ) abort(403) @@ -185,10 +190,10 @@ def corpus(corpus_id): @login_required def generate_corpus_share_link(corpus_id): data = request.get_json('data') - # permission = data['permission'] + role = data['role'] exp_data = data['expiration'] expiration = datetime.strptime(exp_data, '%b %d, %Y') - token = current_user.generate_follow_corpus_token(corpus_id, expiration) + token = current_user.generate_follow_corpus_token(corpus_id, role, expiration) link = url_for('corpora.follow_corpus', corpus_id=corpus_id, token=token, _external=True) return link diff --git a/app/models.py b/app/models.py index 74d82410..8fc08c18 100644 --- a/app/models.py +++ b/app/models.py @@ -789,13 +789,14 @@ class User(HashidMixin, UserMixin, db.Model): def is_following_corpus(self, corpus): return corpus in self.followed_corpora - def generate_follow_corpus_token(self, corpus_id, expiration=7): + def generate_follow_corpus_token(self, corpus_id, role, expiration=7): now = datetime.utcnow() payload = { 'exp': expiration, 'iat': now, 'iss': current_app.config['SERVER_NAME'], - 'sub': corpus_id + 'sub': corpus_id, + 'role': role } return jwt.encode( payload, @@ -816,9 +817,10 @@ class User(HashidMixin, UserMixin, db.Model): return False corpus_id = payload.get('sub') corpus = Corpus.query.get_or_404(corpus_id) + role = CorpusFollowerRole.query.filter_by(name=payload.get('role')).first() if corpus is None: return False - return True + return {'corpus': corpus, 'role': role} def to_json_serializeable(self, backrefs=False, relationships=False, filter_by_privacy_settings=False): json_serializeable = { diff --git a/app/static/js/RessourceDisplays/CorpusDisplay.js b/app/static/js/RessourceDisplays/CorpusDisplay.js index d42fa20c..4e8e8a9a 100644 --- a/app/static/js/RessourceDisplays/CorpusDisplay.js +++ b/app/static/js/RessourceDisplays/CorpusDisplay.js @@ -124,13 +124,12 @@ class CorpusDisplay extends RessourceDisplay { let copyShareLinkButton = this.displayElement.querySelector('#copy-share-link-button'); let shareLinkInput = this.displayElement.querySelector('#share-link-input'); let shareLinkContainer = this.displayElement.querySelector('#share-link-container'); - // let permissionSelect = this.displayElement.querySelector('#permission-select'); + let roleSelect = this.displayElement.querySelector('#role-select'); let expirationDate = this.displayElement.querySelector('#expiration'); generateShareLinkButton.addEventListener('click', () => { - // Utils.generateCorpusShareLinkRequest(`${this.corpusId}`, permissionSelect.value, expirationDate.value) - Utils.generateCorpusShareLinkRequest(`${this.corpusId}`, expirationDate.value) + Utils.generateCorpusShareLinkRequest(`${this.corpusId}`, roleSelect.value, expirationDate.value) .then((shareLink) => { shareLinkContainer.classList.remove('hide'); shareLinkInput.value = shareLink; diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js index f8dbf2d0..865ea0d4 100644 --- a/app/static/js/Utils.js +++ b/app/static/js/Utils.js @@ -757,11 +757,9 @@ class Utils { }); } - // static generateCorpusShareLinkRequest(corpusId, permission, expiration) { - static generateCorpusShareLinkRequest(corpusId, expiration) { + static generateCorpusShareLinkRequest(corpusId, role, expiration) { return new Promise((resolve, reject) => { - // const data = {permission: permission, expiration: expiration}; - const data = {expiration: expiration}; + const data = {role: role, expiration: expiration}; fetch(`/corpora/${corpusId}/generate-corpus-share-link`, {method: 'POST', headers: {Accept: 'text/plain'}, body: JSON.stringify(data)}) .then( (response) => { diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index 196faef2..84950403 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -96,18 +96,18 @@

- {#
+
- + {% for role in roles%} + + {% endfor %} - +
-
#} +
diff --git a/app/templates/corpora/public_corpus.html.j2 b/app/templates/corpora/public_corpus.html.j2 index 63744441..17a7ae07 100644 --- a/app/templates/corpora/public_corpus.html.j2 +++ b/app/templates/corpora/public_corpus.html.j2 @@ -11,7 +11,7 @@
{% if current_user.is_following_corpus(corpus) %} - addUnfollow Corpus + closeUnfollow Corpus {% endif %} {% if corpus.status.name in ['BUILT', 'STARTING_ANALYSIS_SESSION', 'RUNNING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'] and current_user.is_following_corpus(corpus) %} Analyze @@ -42,10 +42,50 @@
- Corpus files -
+ Corpus Owner +
+
+ + + + + + +
+ {% if corpus.user.avatar %} + user-image + {% else %} + user-image + {% endif %} + +
    +
  • {{ owner.username }}
  • + {% if owner.full_name %} +
  • {{ owner.full_name }}
  • + {% endif %} + {% if owner.show_email %} +
  • {{ owner.email }} + {% endif %} +
+
+ {% if not current_user.is_following_corpus(corpus) %} +
+

+ Request Corpus + {% endif %} +
+
+ + {% if current_user.is_following_corpus(corpus)%} +
+
+ Corpus files +
+
+
+ {% endif %}