share toggle update in contributions

This commit is contained in:
Inga Kirschnick 2022-11-17 12:11:21 +01:00
parent f1677e2931
commit d412f2c61a
4 changed files with 166 additions and 16 deletions

View File

@ -134,6 +134,15 @@ def create_tesseract_ocr_pipeline_model():
title='Create Tesseract OCR Pipeline Model' title='Create Tesseract OCR Pipeline Model'
) )
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/toggle-public-status', methods=['POST'])
def share_tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
if not (tesseract_ocr_pipeline_model.user == current_user or current_user.is_administrator()):
abort(403)
tesseract_ocr_pipeline_model.shared = not tesseract_ocr_pipeline_model.shared
db.session.commit()
return {}, 201
@bp.route('/spacy-nlp-pipeline-models') @bp.route('/spacy-nlp-pipeline-models')
def spacy_nlp_pipeline_models(): def spacy_nlp_pipeline_models():
@ -239,3 +248,12 @@ def create_spacy_nlp_pipeline_model():
form=form, form=form,
title='Create SpaCy NLP Pipeline Model' title='Create SpaCy NLP Pipeline Model'
) )
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>/toggle-public-status', methods=['POST'])
def share_spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (spacy_nlp_pipeline_model.user == current_user or current_user.is_administrator()):
abort(403)
spacy_nlp_pipeline_model.shared = not spacy_nlp_pipeline_model.shared
db.session.commit()
return {}, 201

View File

@ -16,9 +16,8 @@ class SpaCyNLPPipelineModelList extends RessourceList {
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Title</th> <th>Title and Description</th>
<th>Description</th> <th>Publisher</th>
<th>Biblio</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@ -29,9 +28,18 @@ class SpaCyNLPPipelineModelList extends RessourceList {
}, },
item: ` item: `
<tr class="clickable hoverable"> <tr class="clickable hoverable">
<td><span class="title"></span></td> <td><b><span class="title"></span> <span class="version"></span></b><br><i><span class="description"></span></i></td>
<td><span class="description"></span></td> <td><a class="publisher-url"><span class="publisher"></span></a> (<span class="publishing-year"></span>)<br><a class="publishing-url"><span class="publishing-url-2"></span></a></td>
<td><a class="publisher-url"><span class="publisher"></span></a> (<span class="publishing-year"></span>), <span class="title-2"></span> <span class="version"></span>, <a class="publishing-url"><span class="publishing-url-2"></span></a></td> <td>
<div class="switch action-switch center-align" data-action="share-request">
<span class="share"></span>
<label>
<input type="checkbox" class="shared">
<span class="lever"></span>
shared
</label>
</div>
</td>
<td class="right-align"> <td class="right-align">
<a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</i></a> <a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating service-color darken waves-effect waves-light service-2" data-action="view"><i class="material-icons">send</i></a> <a class="action-button btn-floating service-color darken waves-effect waves-light service-2" data-action="view"><i class="material-icons">send</i></a>
@ -50,7 +58,8 @@ class SpaCyNLPPipelineModelList extends RessourceList {
'publishing-year': spaCyNLPPipelineModel.publishing_year, 'publishing-year': spaCyNLPPipelineModel.publishing_year,
'title': spaCyNLPPipelineModel.title, 'title': spaCyNLPPipelineModel.title,
'title-2': spaCyNLPPipelineModel.title, 'title-2': spaCyNLPPipelineModel.title,
'version': spaCyNLPPipelineModel.version 'version': spaCyNLPPipelineModel.version,
'shared': spaCyNLPPipelineModel.shared ? 'True' : 'False'
}; };
}, },
sortArgs: ['creation-date', {order: 'desc'}], sortArgs: ['creation-date', {order: 'desc'}],
@ -65,19 +74,31 @@ class SpaCyNLPPipelineModelList extends RessourceList {
'publishing-year', 'publishing-year',
'title', 'title',
'title-2', 'title-2',
'version' 'version',
{name: 'shared', attr: 'data-checked'}
] ]
}; };
constructor(listElement, options = {}) { constructor(listElement, options = {}) {
super(listElement, {...SpaCyNLPPipelineModelList.options, ...options}); super(listElement, {...SpaCyNLPPipelineModelList.options, ...options});
this.listjs.list.addEventListener('change', (event) => {this.onChange(event)});
} }
init (user) { init (user) {
this._init(user.spacy_nlp_pipeline_models); this._init(user.spacy_nlp_pipeline_models);
} }
_init(ressources) {
super._init(ressources);
for (let uncheckedCheckbox of this.listjs.list.querySelectorAll('input[data-checked="True"]')) {
uncheckedCheckbox.setAttribute('checked', '');
}
}
onClick(event) { onClick(event) {
if (event.target.closest('.action-switch')) {
return;
}
let actionButtonElement = event.target.closest('.action-button'); let actionButtonElement = event.target.closest('.action-button');
let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action; let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action;
let spaCyNLPPipelineModelElement = event.target.closest('tr'); let spaCyNLPPipelineModelElement = event.target.closest('tr');
@ -96,4 +117,26 @@ class SpaCyNLPPipelineModelList extends RessourceList {
} }
} }
} }
onChange(event) {
let actionSwitchElement = event.target.closest('.action-switch');
let action = actionSwitchElement.dataset.action;
let spaCyNLPPipelineModelElement = event.target.closest('tr');
let spaCyNLPPipelineModelId = spaCyNLPPipelineModelElement.dataset.id;
switch (action) {
case 'share-request': {
let shared;
if (actionSwitchElement.querySelector('input').checked) {
shared = true;
} else {
shared = false;
}
Utils.shareSpaCyNLPPipelineModelRequest(this.userId, spaCyNLPPipelineModelId, shared);
break;
}
default: {
break;
}
}
}
} }

View File

@ -16,9 +16,8 @@ class TesseractOCRPipelineModelList extends RessourceList {
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Title</th> <th>Title and Description</th>
<th>Description</th> <th>Publisher</th>
<th>Biblio</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@ -29,9 +28,18 @@ class TesseractOCRPipelineModelList extends RessourceList {
}, },
item: ` item: `
<tr class="clickable hoverable"> <tr class="clickable hoverable">
<td><span class="title"></span></td> <td><b><span class="title"></span> <span class="version"></span></b><br><i><span class="description"></span></i></td>
<td><span class="description"></span></td> <td><a class="publisher-url"><span class="publisher"></span></a> (<span class="publishing-year"></span>)<br><a class="publishing-url"><span class="publishing-url-2"></span></a></td>
<td><a class="publisher-url"><span class="publisher"></span></a> (<span class="publishing-year"></span>), <span class="title-2"></span> <span class="version"></span>, <a class="publishing-url"><span class="publishing-url-2"></span></a></td> <td>
<div class="switch action-switch center-align" data-action="share-request">
<span class="share"></span>
<label>
<input type="checkbox" class="shared">
<span class="lever"></span>
shared
</label>
</div>
</td>
<td class="right-align"> <td class="right-align">
<a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</i></a> <a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating service-color darken waves-effect waves-light service-2" data-action="view"><i class="material-icons">send</i></a> <a class="action-button btn-floating service-color darken waves-effect waves-light service-2" data-action="view"><i class="material-icons">send</i></a>
@ -50,7 +58,8 @@ class TesseractOCRPipelineModelList extends RessourceList {
'publishing-year': tesseractOCRPipelineModel.publishing_year, 'publishing-year': tesseractOCRPipelineModel.publishing_year,
'title': tesseractOCRPipelineModel.title, 'title': tesseractOCRPipelineModel.title,
'title-2': tesseractOCRPipelineModel.title, 'title-2': tesseractOCRPipelineModel.title,
'version': tesseractOCRPipelineModel.version 'version': tesseractOCRPipelineModel.version,
'shared': tesseractOCRPipelineModel.shared ? 'True' : 'False'
}; };
}, },
sortArgs: ['creation-date', {order: 'desc'}], sortArgs: ['creation-date', {order: 'desc'}],
@ -65,19 +74,31 @@ class TesseractOCRPipelineModelList extends RessourceList {
'publishing-year', 'publishing-year',
'title', 'title',
'title-2', 'title-2',
'version' 'version',
{name: 'shared', attr: 'data-checked'}
] ]
}; };
constructor(listElement, options = {}) { constructor(listElement, options = {}) {
super(listElement, {...TesseractOCRPipelineModelList.options, ...options}); super(listElement, {...TesseractOCRPipelineModelList.options, ...options});
this.listjs.list.addEventListener('change', (event) => {this.onChange(event)});
} }
init (user) { init (user) {
this._init(user.tesseract_ocr_pipeline_models); this._init(user.tesseract_ocr_pipeline_models);
} }
_init(ressources) {
super._init(ressources);
for (let uncheckedCheckbox of this.listjs.list.querySelectorAll('input[data-checked="True"]')) {
uncheckedCheckbox.setAttribute('checked', '');
}
}
onClick(event) { onClick(event) {
if (event.target.closest('.action-switch')) {
return;
}
let actionButtonElement = event.target.closest('.action-button'); let actionButtonElement = event.target.closest('.action-button');
let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action; let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action;
let tesseractOCRPipelineModelElement = event.target.closest('tr'); let tesseractOCRPipelineModelElement = event.target.closest('tr');
@ -96,4 +117,26 @@ class TesseractOCRPipelineModelList extends RessourceList {
} }
} }
} }
onChange(event) {
let actionSwitchElement = event.target.closest('.action-switch');
let action = actionSwitchElement.dataset.action;
let tesseractOCRPipelineModelElement = event.target.closest('tr');
let tesseractOCRPipelineModelId = tesseractOCRPipelineModelElement.dataset.id;
switch (action) {
case 'share-request': {
let shared;
if (actionSwitchElement.querySelector('input').checked) {
shared = true;
} else {
shared = false;
}
Utils.shareTesseractOCRPipelineModelRequest(this.userId, tesseractOCRPipelineModelId, shared);
break;
}
default: {
break;
}
}
}
} }

View File

@ -419,4 +419,50 @@ class Utils {
modal.open(); modal.open();
}); });
} }
static shareTesseractOCRPipelineModelRequest(userId, tesseractOCRPipelineModelId, shared) {
return new Promise((resolve, reject) => {
let tesseractOCRPipelineModel = app.data.users[userId].tesseract_ocr_pipeline_models[tesseractOCRPipelineModelId];
let msg = '';
if (shared) {
msg = `Model "${tesseractOCRPipelineModel.title}" is now public`;
} else {
msg = `Model "${tesseractOCRPipelineModel.title}" is now private`;
}
fetch(`/contributions/spacy-nlp-pipeline-models/${tesseractOCRPipelineModel.id}/toggle-public-status`, {method: 'POST', headers: {Accept: 'application/json'}})
.then(
(response) => {
app.flash(msg, 'corpus');
resolve(response);
},
(response) => {
if (response.status === 403) {app.flash('Forbidden', 'error');}
reject(response);
}
);
});
}
static shareSpaCyNLPPipelineModelRequest(userId, spaCyNLPPipelineModelId, shared) {
return new Promise((resolve, reject) => {
let spaCyNLPPipelineModel = app.data.users[userId].spacy_nlp_pipeline_models[spaCyNLPPipelineModelId];
let msg = '';
if (shared) {
msg = `Model "${spaCyNLPPipelineModel.title}" is now public`;
} else {
msg = `Model "${spaCyNLPPipelineModel.title}" is now private`;
}
fetch(`/contributions/spacy-nlp-pipeline-models/${spaCyNLPPipelineModel.id}/toggle-public-status`, {method: 'POST', headers: {Accept: 'application/json'}})
.then(
(response) => {
app.flash(msg, 'corpus');
resolve(response);
},
(response) => {
if (response.status === 403) {app.flash('Forbidden', 'error');}
reject(response);
}
);
});
}
} }