normalize template parameters from database

This commit is contained in:
Patrick Jentsch 2023-03-28 14:11:46 +02:00
parent 09b3afc880
commit b41436c844
10 changed files with 195 additions and 171 deletions

View File

@ -54,7 +54,7 @@ def create_corpus():
def corpus(corpus_id): def corpus(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
corpus_follower_roles = CorpusFollowerRole.query.all() corpus_follower_roles = CorpusFollowerRole.query.all()
users = [u.to_json_serializeable() for u in User.query.filter(User.is_public == True, User.id != current_user.id).all()] users = User.query.filter(User.is_public == True, User.id != current_user.id).all()
# TODO: Add URL query option to toggle view # TODO: Add URL query option to toggle view
if corpus.user == current_user or current_user.is_administrator(): if corpus.user == current_user or current_user.is_administrator():
return render_template( return render_template(
@ -66,14 +66,10 @@ def corpus(corpus_id):
) )
if current_user.is_following_corpus(corpus) or corpus.is_public: if current_user.is_following_corpus(corpus) or corpus.is_public:
cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first_or_404() cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first_or_404()
corpus_files = [x.to_json_serializeable() for x in corpus.files]
owner = corpus.user.to_json_serializeable()
return render_template( return render_template(
'corpora/public_corpus.html.j2', 'corpora/public_corpus.html.j2',
corpus=corpus, corpus=corpus,
corpus_files=corpus_files,
cfa=cfa, cfa=cfa,
owner=owner,
title=corpus.title title=corpus.title
) )
abort(403) abort(403)

View File

@ -61,14 +61,16 @@ def terms_of_use():
@bp.route('/social-area') @bp.route('/social-area')
@register_breadcrumb(bp, '.social_area', '<i class="material-icons left">group</i>Social Area') @register_breadcrumb(bp, '.social_area', '<i class="material-icons left">group</i>Social Area')
def social_area(): def social_area():
users = [ # corpora = [
u.to_json_serializeable(relationships=True, filter_by_privacy_settings=True,) for u # c.to_json_serializeable() for c
in User.query.filter(User.is_public == True, User.id != current_user.id).all() # in Corpus.query.filter(Corpus.is_public == True, Corpus.user != current_user).all()
] # ]
corpora = [ corpora = Corpus.query.filter(Corpus.is_public == True, Corpus.user != current_user).all()
c.to_json_serializeable() for c # users = [
in Corpus.query.filter(Corpus.is_public == True, Corpus.user != current_user).all() # u.to_json_serializeable(relationships=True, filter_by_privacy_settings=True,) for u
] # in User.query.filter(User.is_public == True, User.id != current_user.id).all()
# ]
users = User.query.filter(User.is_public == True, User.id != current_user.id).all()
return render_template( return render_template(
'main/social_area.html.j2', 'main/social_area.html.j2',
users=users, users=users,

View File

@ -21,8 +21,7 @@
corpusList.add( corpusList.add(
[ [
{% for corpus in corpora %} {% for corpus in corpora %}
{{ corpus.to_json_serializeable(backrefs=True)|tojson }} {{ corpus.to_json_serializeable(backrefs=True)|tojson }},
{% if not loop.last %},{% endif %}
{% endfor %} {% endfor %}
] ]
); );

View File

@ -26,8 +26,7 @@
adminUserList.add( adminUserList.add(
[ [
{% for user in users %} {% for user in users %}
{{ user.to_json_serializeable(backrefs=True)|tojson }} {{ user.to_json_serializeable(backrefs=True)|tojson }},
{% if not loop.last %},{% endif %}
{% endfor %} {% endfor %}
] ]
); );

View File

@ -216,3 +216,137 @@
</div> </div>
</div> </div>
{% endblock modals %} {% endblock modals %}
{% block scripts %}
{{ super() }}
<script>
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
// #region Publishing
let publishingModalIsPublicSwitchElement = document.querySelector('#publishing-modal-is-public-switch');
publishingModalIsPublicSwitchElement.addEventListener('change', (event) => {
let newIsPublic = publishingModalIsPublicSwitchElement.checked;
Requests.corpora.entity.isPublic.update({{ corpus.hashid|tojson }}, newIsPublic)
.catch((response) => {
publishingModalIsPublicSwitchElement.checked = !newIsPublic;
});
});
// #endregion Publishing
// #region Delete
let deleteModalDeleteButtonElement = document.querySelector('#delete-modal-delete-button');
deleteModalDeleteButtonElement.addEventListener('click', (event) => {
Requests.corpora.entity.delete({{ corpus.hashid|tojson }})
.then((response) => {
window.location.href = {{ url_for('main.dashboard')|tojson }};
});
});
// #endregion Delete
// #region Invite users
let inviteUserModalElement = document.querySelector('#invite-user-modal');
let inviteUserModalSearchElement = document.querySelector('#invite-user-modal-search');
let inviteUserModalInviteButtonElement = document.querySelector('#invite-user-modal-invite-button');
let users = {
{% for user in users %}
{{ user.username|tojson }}: {{ url_for('users.user_avatar', user_id=user.id)|tojson }}
{% if not loop.last %},{% endif %}
{% endfor %}
};
let inviteUserModalSearch = M.Chips.init(
inviteUserModalSearchElement,
{
autocompleteOptions: {
data: users
},
limit: 3,
onChipAdd: (a, chipElement) => {
if (!(chipElement.firstChild.data in inviteUserModalSearch.autocomplete.options.data)) {
chipElement.firstElementChild.click();
}
},
placeholder: 'Enter username',
secondaryPlaceholder: 'Add more users'
}
);
M.Modal.init(
inviteUserModalElement,
{
onOpenStart: (modalElement, modalTriggerElement) => {
while (inviteUserModalSearch.chipsData.length > 0) {
inviteUserModalSearch.deleteChip(0);
}
}
}
)
inviteUserModalInviteButtonElement.addEventListener('click', (event) => {
let usernames = inviteUserModalSearch.chipsData.map((chipData) => chipData.tag);
Requests.corpora.entity.followers.add({{ corpus.hashid|tojson }}, usernames);
});
// #endregion Invite users
// #region Share link
let shareLinkModalElement = document.querySelector('#share-link-modal');
let shareLinkModalCorpusFollowerRoleSelectElement = document.querySelector('#share-link-modal-corpus-follower-role-select');
let shareLinkModalExpirationDateDatepickerElement = document.querySelector('#share-link-modal-expiration-date-datepicker');
let shareLinkModalCreateButtonElement = document.querySelector('#share-link-modal-create-button');
let shareLinkModalOutputContainerElement = document.querySelector('#share-link-modal-output-container');
let shareLinkModalOutputFieldElement = document.querySelector('#share-link-modal-output-field');
let shareLinkModalOutputCopyButtonElement = document.querySelector('#share-link-modal-output-copy-button');
let today = new Date();
let tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
let oneWeekLater = new Date();
oneWeekLater.setDate(today.getDate() + 7);
let fourWeeksLater = new Date();
fourWeeksLater.setDate(today.getDate() + 28);
M.Datepicker.init(
shareLinkModalExpirationDateDatepickerElement,
{
container: document.querySelector('main'),
defaultDate: oneWeekLater,
setDefaultDate: true,
minDate: tomorrow,
maxDate: fourWeeksLater
}
);
M.Modal.init(
shareLinkModalElement,
{
onOpenStart: (modalElement, modalTriggerElement) => {
shareLinkModalOutputFieldElement.value = '';
shareLinkModalOutputContainerElement.classList.add('hide');
}
}
)
shareLinkModalCreateButtonElement.addEventListener('click', (event) => {
let role = shareLinkModalCorpusFollowerRoleSelectElement.value;
let expiration = shareLinkModalExpirationDateDatepickerElement.value
Requests.corpora.entity.generateShareLink({{ corpus.hashid|tojson }}, role, expiration)
.then((response) => {
response.json()
.then((json) => {
shareLinkModalOutputContainerElement.classList.remove('hide');
shareLinkModalOutputFieldElement.value = json.corpusShareLink;
});
});
});
shareLinkModalOutputCopyButtonElement.addEventListener('click', (event) => {
navigator.clipboard.writeText(shareLinkModalOutputFieldElement.value)
.then(
() => {app.flash('Copied!');},
() => {app.flash('Could not copy to clipboard. Please copy manually.', 'error');}
);
});
// #endregion Share link
</script>
{% endblock scripts %}

View File

@ -1,128 +0,0 @@
let corpusId = {{ corpus.hashid|tojson }};
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
// #region Publishing
let publishingModalIsPublicSwitchElement = document.querySelector('#publishing-modal-is-public-switch');
publishingModalIsPublicSwitchElement.addEventListener('change', (event) => {
let newIsPublic = publishingModalIsPublicSwitchElement.checked;
Requests.corpora.entity.isPublic.update(corpusId, newIsPublic)
.catch((response) => {
publishingModalIsPublicSwitchElement.checked = !newIsPublic;
});
});
// #endregion Publishing
// #region Delete
let deleteModalDeleteButtonElement = document.querySelector('#delete-modal-delete-button');
deleteModalDeleteButtonElement.addEventListener('click', (event) => {
Requests.corpora.entity.delete(corpusId)
.then((response) => {
window.location.href = {{ url_for('main.dashboard')|tojson }};
});
});
// #endregion Delete
// #region Invite users
let inviteUserModalElement = document.querySelector('#invite-user-modal');
let inviteUserModalSearchElement = document.querySelector('#invite-user-modal-search');
let inviteUserModalInviteButtonElement = document.querySelector('#invite-user-modal-invite-button');
const users = {};
for (let user of {{ users|tojson }}) {
users[user.username] = user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png';
}
let inviteUserModalSearch = M.Chips.init(
inviteUserModalSearchElement,
{
autocompleteOptions: {
data: users
},
limit: 3,
onChipAdd: (a, chipElement) => {
if (!(chipElement.firstChild.data in inviteUserModalSearch.autocomplete.options.data)) {
chipElement.firstElementChild.click();
}
},
placeholder: 'Enter username',
secondaryPlaceholder: 'Add more users'
}
);
M.Modal.init(
inviteUserModalElement,
{
onOpenStart: (modalElement, modalTriggerElement) => {
while (inviteUserModalSearch.chipsData.length > 0) {
inviteUserModalSearch.deleteChip(0);
}
}
}
)
inviteUserModalInviteButtonElement.addEventListener('click', (event) => {
let usernames = inviteUserModalSearch.chipsData.map((chipData) => chipData.tag);
Requests.corpora.entity.followers.add(corpusId, usernames);
});
// #endregion Invite users
// #region Share link
let shareLinkModalElement = document.querySelector('#share-link-modal');
let shareLinkModalCorpusFollowerRoleSelectElement = document.querySelector('#share-link-modal-corpus-follower-role-select');
let shareLinkModalExpirationDateDatepickerElement = document.querySelector('#share-link-modal-expiration-date-datepicker');
let shareLinkModalCreateButtonElement = document.querySelector('#share-link-modal-create-button');
let shareLinkModalOutputContainerElement = document.querySelector('#share-link-modal-output-container');
let shareLinkModalOutputFieldElement = document.querySelector('#share-link-modal-output-field');
let shareLinkModalOutputCopyButtonElement = document.querySelector('#share-link-modal-output-copy-button');
let today = new Date();
let tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
let oneWeekLater = new Date();
oneWeekLater.setDate(today.getDate() + 7);
let fourWeeksLater = new Date();
fourWeeksLater.setDate(today.getDate() + 28);
M.Datepicker.init(
shareLinkModalExpirationDateDatepickerElement,
{
container: document.querySelector('main'),
defaultDate: oneWeekLater,
setDefaultDate: true,
minDate: tomorrow,
maxDate: fourWeeksLater
}
);
M.Modal.init(
shareLinkModalElement,
{
onOpenStart: (modalElement, modalTriggerElement) => {
shareLinkModalOutputFieldElement.value = '';
shareLinkModalOutputContainerElement.classList.add('hide');
}
}
)
shareLinkModalCreateButtonElement.addEventListener('click', (event) => {
let role = shareLinkModalCorpusFollowerRoleSelectElement.value;
let expiration = shareLinkModalExpirationDateDatepickerElement.value
Requests.corpora.entity.generateShareLink(corpusId, role, expiration)
.then((response) => {
response.json()
.then((json) => {
shareLinkModalOutputContainerElement.classList.remove('hide');
shareLinkModalOutputFieldElement.value = json.corpusShareLink;
});
});
});
shareLinkModalOutputCopyButtonElement.addEventListener('click', (event) => {
navigator.clipboard.writeText(shareLinkModalOutputFieldElement.value)
.then(
() => {app.flash('Copied!');},
() => {app.flash('Could not copy to clipboard. Please copy manually.', 'error');}
);
});
// #endregion Share link

View File

@ -53,12 +53,12 @@
<td></td> <td></td>
<td> <td>
<ul> <ul>
<li><b>{{ owner.username }}</b></li> <li><b>{{ corpus.user.username }}</b></li>
{% if owner.full_name %} {% if corpus.user.full_name %}
<li>{{ owner.full_name }}</li> <li>{{ corpus.user.full_name }}</li>
{% endif %} {% endif %}
{% if owner.show_email %} {% if corpus.user.show_email %}
<li></li><a href="mailto:{{ owner.email }}">{{ owner.email }}</a></li> <li></li><a href="mailto:{{ corpus.user.email }}">{{ corpus.user.email }}</a></li>
{% endif %} {% endif %}
</ul> </ul>
</td> </td>
@ -78,8 +78,8 @@
{% if cfa.role.has_permission('VIEW') %} {% if cfa.role.has_permission('VIEW') %}
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
<span class="card-title" id="files">Corpus files</span> <span class="card-title" id="corpus-files">Corpus files</span>
<div class="corpus-file-list no-autoinit" data-user-id="{{ owner.hashid }}" data-corpus-id="{{ corpus.hashid }}"></div> <div class="corpus-file-list no-autoinit" id="corpus-file-list"></div>
</div> </div>
</div> </div>
{% endif %} {% endif %}
@ -87,8 +87,8 @@
{% if cfa.role.has_permission('UPDATE_FOLLOWER') %} {% if cfa.role.has_permission('UPDATE_FOLLOWER') %}
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
<span class="card-title" id="files">Corpus followers</span> <span class="card-title" id="corpus-followers">Corpus followers</span>
<div class="corpus-follower-list" data-user-id="{{ corpus.user.hashid }}" data-corpus-id="{{ corpus.hashid }}"></div> <div class="corpus-follower-list no-autoinit"></div>
</div> </div>
</div> </div>
{% endif %} {% endif %}
@ -97,3 +97,26 @@
</div> </div>
</div> </div>
{% endblock page_content %} {% endblock page_content %}
{% block scripts %}
{{ super() }}
<script>
let publicCorpusFileList = new PublicCorpusFileList(document.querySelector('#corpus-file-list'));
publicCorpusFileList.add(
[
{% for corpus_file in corpus.files %}
{{ corpus_file.to_json_serializeable()|tojson }},
{% endfor %}
]
);
let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]');
unfollowRequestElement.addEventListener('click', () => {
Requests.corpora.entity.followers.entity.delete({{ corpus.hashid|tojson }}, {{ corpus.user.hashid|tojson }})
.then((response) => {
window.location.href = {{ url_for('main.dashboard')|tojson }};
});
});
</script>
{% endblock scripts %}

View File

@ -1,11 +0,0 @@
let corpusId = {{ corpus.hashid|tojson }};
let corpusFileList = new PublicCorpusFileList(document.querySelector('.corpus-file-list'));
corpusFileList.add({{ corpus_files|tojson }});
let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]');
unfollowRequestElement.addEventListener('click', () => {
Requests.corpora.entity.followers.entity.delete(corpusId, currentUserId)
.then((response) => {
window.location.href = {{ url_for('main.dashboard')|tojson }};
});
});

View File

@ -63,8 +63,20 @@
{{ super() }} {{ super() }}
<script> <script>
let userList = new UserList(document.querySelector('.user-list')); let userList = new UserList(document.querySelector('.user-list'));
userList.add({{ users|tojson }}); userList.add(
[
{% for user in users %}
{{ user.to_json_serializeable(relationships=True, filter_by_privacy_settings=True)|tojson }},
{% endfor %}
]
);
let publicCorpusList = new PublicCorpusList(document.querySelector('.public-corpus-list')); let publicCorpusList = new PublicCorpusList(document.querySelector('.public-corpus-list'));
publicCorpusList.add({{ corpora|tojson }}); publicCorpusList.add(
[
{% for corpus in corpora %}
{{ corpus.to_json_serializeable()|tojson }},
{% endfor %}
]
);
</script> </script>
{% endblock scripts %} {% endblock scripts %}

View File

@ -123,8 +123,7 @@ let followedCorpusList = new FollowedCorpusList(document.querySelector('.followe
followedCorpusList.add( followedCorpusList.add(
[ [
{% for corpus in user.followed_corpora %} {% for corpus in user.followed_corpora %}
{{ corpus.to_json_serializeable()|tojson }} {{ corpus.to_json_serializeable()|tojson }},
{% if not loop.last %},{% endif %}
{% endfor %} {% endfor %}
] ]
); );
@ -132,8 +131,7 @@ let publicCorpusList = new PublicCorpusList(document.querySelector('.public-corp
publicCorpusList.add( publicCorpusList.add(
[ [
{% for corpus in user.corpora if corpus.is_public %} {% for corpus in user.corpora if corpus.is_public %}
{{ corpus.to_json_serializeable()|tojson }} {{ corpus.to_json_serializeable()|tojson }},
{% if not loop.last %},{% endif %}
{% endfor %} {% endfor %}
] ]
); );