getUser back to CorpusList

This commit is contained in:
Inga Kirschnick 2023-06-07 13:39:04 +02:00
parent 62d20409ea
commit 4944d31dd5
6 changed files with 56 additions and 31 deletions

View File

@ -39,10 +39,8 @@ def faq():
@register_breadcrumb(bp, '.dashboard', '<i class="material-icons left">dashboard</i>Dashboard') @register_breadcrumb(bp, '.dashboard', '<i class="material-icons left">dashboard</i>Dashboard')
@login_required @login_required
def dashboard(): def dashboard():
corpora = Corpus.query.filter(or_(Corpus.followers.any(id=current_user.id), Corpus.user == current_user)).all()
return render_template( return render_template(
'main/dashboard.html.j2', 'main/dashboard.html.j2',
corpora=corpora,
title='Dashboard' title='Dashboard'
) )

View File

@ -493,7 +493,7 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
def to_json_serializeable(self, backrefs=False, relationships=False): def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = { json_serializeable = {
'id': self.hashid, 'id': self.hashid,
'corpus': self.corpus.to_json_serializeable(), 'corpus': self.corpus.to_json_serializeable(backrefs=True),
'follower': self.follower.to_json_serializeable(), 'follower': self.follower.to_json_serializeable(),
'role': self.role.to_json_serializeable() 'role': self.role.to_json_serializeable()
} }

View File

@ -20,6 +20,43 @@ class CorpusList extends ResourceList {
if (this.isInitialized) {this.onPatch(patch);} if (this.isInitialized) {this.onPatch(patch);}
}); });
}); });
app.getUser(this.userId).then((user) => {
this.add(this.equalizer(user));
this.isInitialized = true;
});
}
equalizer(user) {
const equalizedData = [];
for (let corpus of Object.values(user.corpora)) {
equalizedData.push(
{
'id': corpus.id,
'creation-date': corpus.creation_date,
'description': corpus.description,
'status': corpus.status,
'title': corpus.title,
'owner': user.username,
'is-owner': true,
'current-user-is-following': false
}
);
}
for (let cfa of Object.values(user.corpus_follower_associations)) {
equalizedData.push(
{
'id': cfa.corpus.id,
'creation-date': cfa.corpus.creation_date,
'description': cfa.corpus.description,
'status': cfa.corpus.status,
'title': cfa.corpus.title,
'owner': cfa.corpus.user.username,
'is-owner': false,
'current-user-is-following': true
}
);
}
return equalizedData;
} }
// #region Mandatory getters and methods to implement // #region Mandatory getters and methods to implement
@ -93,17 +130,8 @@ class CorpusList extends ResourceList {
`.trim(); `.trim();
} }
mapResourceToValue(corpus) { mapResourceToValue(equalizedData) {
return { return equalizedData;
'id': corpus.id,
'creation-date': corpus.creation_date,
'description': corpus.description,
'status': corpus.status,
'title': corpus.title,
'owner': corpus.user.username,
'is-owner': corpus.user.id === this.userId ? true : false,
'current-user-is-following': Object.values(corpus.corpus_follower_associations).some(association => association.follower.id === currentUserId)
};
} }
sort() { sort() {

View File

@ -14,6 +14,19 @@ class PublicCorpusList extends CorpusList {
}; };
} }
mapResourceToValue(corpus) {
return {
'id': corpus.id,
'creation-date': corpus.creation_date,
'description': corpus.description,
'status': corpus.status,
'title': corpus.title,
'owner': corpus.user.username,
'is-owner': corpus.user.id === this.userId ? true : false,
'current-user-is-following': Object.values(corpus.corpus_follower_associations).some(association => association.follower.id === currentUserId)
};
}
initListContainerElement() { initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) { if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = Utils.generateElementId('corpus-list-'); this.listContainerElement.id = Utils.generateElementId('corpus-list-');

View File

@ -15,7 +15,7 @@
<div class="col s12"> <div class="col s12">
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
<div class="corpus-list no-autoinit" data-user-id="{{ current_user.hashid }}"></div> <div class="corpus-list" data-user-id="{{ current_user.hashid }}"></div>
</div> </div>
<div class="card-action right-align"> <div class="card-action right-align">
<a class="btn disabled waves-effect waves-light" href="{{ url_for('corpora.import_corpus') }}">Import Corpus<i class="material-icons right">import_export</i></a> <a class="btn disabled waves-effect waves-light" href="{{ url_for('corpora.import_corpus') }}">Import Corpus<i class="material-icons right">import_export</i></a>
@ -135,17 +135,3 @@
</div> </div>
</div> </div>
{% endblock modals %} {% endblock modals %}
{% block scripts %}
{{ super() }}
<script>
let corpusList = new CorpusList(document.querySelector('.corpus-list'));
corpusList.add(
[
{% for corpus in corpora %}
{{ corpus.to_json_serializeable(backrefs=True, relationships=True)|tojson }},
{% endfor %}
]
);
</script>
{% endblock scripts %}

View File

@ -70,7 +70,7 @@
{% endfor %} {% endfor %}
] ]
); );
let publicCorpusList = new DetailledPublicCorpusList(document.querySelector('.public-corpus-list')); let publicCorpusList = new PublicCorpusList(document.querySelector('.public-corpus-list'));
publicCorpusList.add( publicCorpusList.add(
[ [
{% for corpus in corpora %} {% for corpus in corpora %}