Merge branch 'public-corpus' of gitlab.ub.uni-bielefeld.de:sfb1288inf/nopaque into public-corpus

This commit is contained in:
Inga Kirschnick 2023-02-24 09:30:43 +01:00
commit d0b369efaf
3 changed files with 46 additions and 48 deletions

View File

@ -37,6 +37,16 @@ class CorpusStatus(IntEnum):
RUNNING_ANALYSIS_SESSION = 8 RUNNING_ANALYSIS_SESSION = 8
CANCELING_ANALYSIS_SESSION = 9 CANCELING_ANALYSIS_SESSION = 9
@staticmethod
def get(corpus_status: Union['CorpusStatus', int, str]) -> 'CorpusStatus':
if isinstance(corpus_status, CorpusStatus):
return corpus_status
if isinstance(corpus_status, int):
return CorpusStatus(corpus_status)
if isinstance(corpus_status, str):
return CorpusStatus[corpus_status]
raise TypeError('corpus_status must be CorpusStatus, int, or str')
class JobStatus(IntEnum): class JobStatus(IntEnum):
INITIALIZING = 1 INITIALIZING = 1
@ -48,6 +58,16 @@ class JobStatus(IntEnum):
COMPLETED = 7 COMPLETED = 7
FAILED = 8 FAILED = 8
@staticmethod
def get(job_status: Union['JobStatus', int, str]) -> 'JobStatus':
if isinstance(job_status, JobStatus):
return job_status
if isinstance(job_status, int):
return JobStatus(job_status)
if isinstance(job_status, str):
return JobStatus[job_status]
raise TypeError('job_status must be JobStatus, int, or str')
class Permission(IntEnum): class Permission(IntEnum):
''' '''
@ -68,6 +88,7 @@ class Permission(IntEnum):
return Permission[permission] return Permission[permission]
raise TypeError('permission must be Permission, int, or str') raise TypeError('permission must be Permission, int, or str')
class UserSettingJobStatusMailNotificationLevel(IntEnum): class UserSettingJobStatusMailNotificationLevel(IntEnum):
NONE = 1 NONE = 1
END = 2 END = 2
@ -90,14 +111,14 @@ class CorpusFollowerPermission(IntEnum):
UPDATE_FOLLOWER = 64 UPDATE_FOLLOWER = 64
@staticmethod @staticmethod
def get(permission: Union['CorpusFollowerPermission', int, str]) -> 'CorpusFollowerPermission': def get(corpus_follower_permission: Union['CorpusFollowerPermission', int, str]) -> 'CorpusFollowerPermission':
if isinstance(permission, CorpusFollowerPermission): if isinstance(corpus_follower_permission, CorpusFollowerPermission):
return permission return corpus_follower_permission
if isinstance(permission, int): if isinstance(corpus_follower_permission, int):
return CorpusFollowerPermission(permission) return CorpusFollowerPermission(corpus_follower_permission)
if isinstance(permission, str): if isinstance(corpus_follower_permission, str):
return CorpusFollowerPermission[permission] return CorpusFollowerPermission[corpus_follower_permission]
raise TypeError('permission must be CorpusFollowerPermission, int, or str') raise TypeError('corpus_follower_permission must be CorpusFollowerPermission, int, or str')
# endregion enums # endregion enums
@ -357,8 +378,7 @@ class CorpusFollowerRole(HashidMixin, db.Model):
# Relationships # Relationships
corpus_follower_associations = db.relationship( corpus_follower_associations = db.relationship(
'CorpusFollowerAssociation', 'CorpusFollowerAssociation',
back_populates='role', back_populates='role'
lazy='dynamic'
) )
def __repr__(self): def __repr__(self):
@ -460,11 +480,9 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
if self.role is None:
self.role = CorpusFollowerRole.query.filter_by(default=True).first()
def __repr__(self): def __repr__(self):
return f'<CorpusFollowerAssociation {self.follower.__repr__()} ~ {self.corpus.__repr__()}>' return f'<CorpusFollowerAssociation {self.follower.__repr__()} ~ {self.role.__repr__()} ~ {self.corpus.__repr__()}>'
def to_json_serializeable(self, backrefs=False, relationships=False): def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = { json_serializeable = {
@ -524,8 +542,7 @@ class User(HashidMixin, UserMixin, db.Model):
) )
followed_corpora = association_proxy( followed_corpora = association_proxy(
'corpus_follower_associations', 'corpus_follower_associations',
'corpus', 'corpus'
creator=lambda c: CorpusFollowerAssociation(corpus=c)
) )
jobs = db.relationship( jobs = db.relationship(
'Job', 'Job',
@ -555,7 +572,7 @@ class User(HashidMixin, UserMixin, db.Model):
cascade='all, delete-orphan', cascade='all, delete-orphan',
lazy='dynamic' lazy='dynamic'
) )
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
if self.role is not None: if self.role is not None:
@ -757,9 +774,11 @@ class User(HashidMixin, UserMixin, db.Model):
self.profile_privacy_settings = 0 self.profile_privacy_settings = 0
#endregion Profile Privacy settings #endregion Profile Privacy settings
def follow_corpus(self, corpus): def follow_corpus(self, corpus, role=None):
if not self.is_following_corpus(corpus): if role is None:
self.followed_corpora.append(corpus) r = CorpusFollowerRole.query.filter_by(default=True).first()
cfa = CorpusFollowerAssociation(corpus=corpus, role=r, follower=self)
db.session.add(cfa)
def unfollow_corpus(self, corpus): def unfollow_corpus(self, corpus):
if self.is_following_corpus(corpus): if self.is_following_corpus(corpus):
@ -1509,8 +1528,7 @@ class Corpus(HashidMixin, db.Model):
) )
followers = association_proxy( followers = association_proxy(
'corpus_follower_associations', 'corpus_follower_associations',
'follower', 'follower'
creator=lambda u: CorpusFollowerAssociation(followers=u)
) )
user = db.relationship('User', back_populates='corpora') user = db.relationship('User', back_populates='corpora')
# "static" attributes # "static" attributes

View File

@ -41,9 +41,9 @@ class CorpusFollowerList extends ResourceList {
<td> <td>
<div class="input-field disable-on-click list-action-trigger" data-list-action="update-role"> <div class="input-field disable-on-click list-action-trigger" data-list-action="update-role">
<select> <select>
<option value="Viewer" ${values['role.name'] === 'Viewer' ? 'selected' : ''}>Viewer</option> <option value="Viewer" ${values['role-name'] === 'Viewer' ? 'selected' : ''}>Viewer</option>
<option value="Contributor" ${values['role.name'] === 'Contributor' ? 'selected' : ''}>Contributor</option> <option value="Contributor" ${values['role-name'] === 'Contributor' ? 'selected' : ''}>Contributor</option>
<option value="Administrator" ${values['role.name'] === 'Administrator' ? 'selected' : ''}>Administrator</option> <option value="Administrator" ${values['role-name'] === 'Administrator' ? 'selected' : ''}>Administrator</option>
</select> </select>
</div> </div>
</td> </td>
@ -176,6 +176,7 @@ class CorpusFollowerList extends ResourceList {
case 'replace': { case 'replace': {
let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)/role$`); let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)/role$`);
if (re.test(operation.path)) { if (re.test(operation.path)) {
console.log('role updated');
let [match, jobId, valueName] = operation.path.match(re); let [match, jobId, valueName] = operation.path.match(re);
this.replace(jobId, valueName, operation.value); this.replace(jobId, valueName, operation.value);
} }

View File

@ -69,13 +69,13 @@ class Utils {
return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2)); return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2));
} }
static addCorpusFollowerPermissionRequest(corpusId, followerId, permission) { static updateCorpusFollowerRole(corpusId, followerId, roleName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch(`/corpora/${corpusId}/followers/${followerId}/permissions/${permission}/add`, {method: 'POST', headers: {Accept: 'application/json'}}) fetch(`/corpora/${corpusId}/followers/${followerId}/role`, {method: 'POST', headers: {Accept: 'application/json', 'Content-Type': 'application/json'}, body: JSON.stringify({role: roleName})})
.then( .then(
(response) => { (response) => {
if (response.ok) { if (response.ok) {
app.flash(`Permission added`, 'corpus'); app.flash('Role updated', 'corpus');
resolve(response); resolve(response);
return; return;
} else { } else {
@ -91,27 +91,6 @@ class Utils {
}); });
} }
static removeCorpusFollowerPermissionRequest(corpusId, followerId, permission) {
return new Promise((resolve, reject) => {
fetch(`/corpora/${corpusId}/followers/${followerId}/permissions/${permission}/remove`, {method: 'POST', headers: {Accept: 'application/json'}})
.then(
(response) => {
if (response.ok) {
app.flash(`Permission removed`, 'corpus');
resolve(response);
} else {
app.flash(`${response.statusText}`, 'error');
reject(response);
}
},
(response) => {
app.flash('Something went wrong', 'error');
reject(response);
}
);
});
}
static enableCorpusIsPublicRequest(userId, corpusId) { static enableCorpusIsPublicRequest(userId, corpusId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let corpus; let corpus;