2019-11-05 09:32:42 +00:00
|
|
|
|
from datetime import datetime
|
2020-12-03 14:13:24 +00:00
|
|
|
|
from flask import current_app, url_for
|
2019-07-09 13:41:16 +00:00
|
|
|
|
from flask_login import UserMixin, AnonymousUserMixin
|
2019-08-22 07:35:23 +00:00
|
|
|
|
from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
|
2020-07-09 13:07:43 +00:00
|
|
|
|
from time import sleep
|
2019-07-05 12:47:35 +00:00
|
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
2020-07-10 09:36:54 +00:00
|
|
|
|
import xml.etree.ElementTree as ET
|
2020-04-23 06:24:11 +00:00
|
|
|
|
from . import db, login_manager
|
2020-11-13 09:01:51 +00:00
|
|
|
|
import logging
|
2020-07-09 13:07:43 +00:00
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
class Permission:
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Defines User permissions as integers by the power of 2. User permission
|
|
|
|
|
can be evaluated using the bitwise operator &. 3 equals to CREATE_JOB and
|
|
|
|
|
DELETE_JOB and so on.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-04-22 07:29:45 +00:00
|
|
|
|
MANAGE_CORPORA = 1
|
|
|
|
|
MANAGE_JOBS = 2
|
|
|
|
|
# PERMISSION_NAME = 4
|
|
|
|
|
# PERMISSION_NAME = 8
|
2019-07-09 13:41:16 +00:00
|
|
|
|
ADMIN = 16
|
|
|
|
|
|
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
|
class Role(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-22 07:35:23 +00:00
|
|
|
|
Model for the different roles Users can have. Is a one-to-many
|
|
|
|
|
relationship. A Role can be associated with many User rows.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-05 12:47:35 +00:00
|
|
|
|
__tablename__ = 'roles'
|
2019-08-06 09:47:04 +00:00
|
|
|
|
# Primary key
|
2019-07-05 12:47:35 +00:00
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2019-07-09 13:41:16 +00:00
|
|
|
|
default = db.Column(db.Boolean, default=False, index=True)
|
2019-08-06 09:47:04 +00:00
|
|
|
|
name = db.Column(db.String(64), unique=True)
|
2020-11-13 14:01:53 +00:00
|
|
|
|
permissions = db.Column(db.Integer)
|
2019-08-06 09:47:04 +00:00
|
|
|
|
# Relationships
|
2019-07-09 13:41:16 +00:00
|
|
|
|
users = db.relationship('User', backref='role', lazy='dynamic')
|
|
|
|
|
|
2021-08-23 14:31:06 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
2020-04-29 10:17:16 +00:00
|
|
|
|
return {'id': self.id,
|
|
|
|
|
'default': self.default,
|
|
|
|
|
'name': self.name,
|
|
|
|
|
'permissions': self.permissions}
|
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super(Role, self).__init__(**kwargs)
|
|
|
|
|
if self.permissions is None:
|
|
|
|
|
self.permissions = 0
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
String representation of the Role. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<Role {}>'.format(self.name)
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
def add_permission(self, perm):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Add new permission to Role. Input is a Permission.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
if not self.has_permission(perm):
|
|
|
|
|
self.permissions += perm
|
|
|
|
|
|
|
|
|
|
def remove_permission(self, perm):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Removes permission from a Role. Input a Permission.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
if self.has_permission(perm):
|
|
|
|
|
self.permissions -= perm
|
|
|
|
|
|
|
|
|
|
def reset_permissions(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Resets permissions to zero. Zero equals no permissions at all.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
self.permissions = 0
|
|
|
|
|
|
|
|
|
|
def has_permission(self, perm):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-09-10 11:49:01 +00:00
|
|
|
|
Checks if a Role has a specific Permission. Does this with the bitwise
|
2019-07-11 13:33:48 +00:00
|
|
|
|
operator.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
return self.permissions & perm == perm
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def insert_roles():
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-04-22 07:29:45 +00:00
|
|
|
|
Inserts roles into the database. This has to be executed befor Users
|
|
|
|
|
are added to the database. Otherwiese Users will not have a Role
|
|
|
|
|
assigned to them. Order of the roles dictionary determines the ID of
|
|
|
|
|
each role. Users have the ID 1 and Administrators have the ID 2.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-04-22 07:29:45 +00:00
|
|
|
|
roles = {'User': [Permission.MANAGE_CORPORA, Permission.MANAGE_JOBS],
|
|
|
|
|
'Administrator': [Permission.MANAGE_CORPORA,
|
|
|
|
|
Permission.MANAGE_JOBS, Permission.ADMIN]}
|
2019-07-09 13:41:16 +00:00
|
|
|
|
default_role = 'User'
|
|
|
|
|
for r in roles:
|
|
|
|
|
role = Role.query.filter_by(name=r).first()
|
|
|
|
|
if role is None:
|
|
|
|
|
role = Role(name=r)
|
|
|
|
|
role.reset_permissions()
|
|
|
|
|
for perm in roles[r]:
|
|
|
|
|
role.add_permission(perm)
|
|
|
|
|
role.default = (role.name == default_role)
|
|
|
|
|
db.session.add(role)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
|
|
|
|
class User(UserMixin, db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Model for Users that are registered to Opaque.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-05 12:47:35 +00:00
|
|
|
|
__tablename__ = 'users'
|
2019-08-06 09:47:04 +00:00
|
|
|
|
# Primary key
|
2019-07-05 12:47:35 +00:00
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2019-08-06 09:47:04 +00:00
|
|
|
|
confirmed = db.Column(db.Boolean, default=False)
|
2019-09-12 12:24:43 +00:00
|
|
|
|
email = db.Column(db.String(254), unique=True, index=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
|
2020-04-27 11:50:54 +00:00
|
|
|
|
member_since = db.Column(db.DateTime(), default=datetime.utcnow)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
password_hash = db.Column(db.String(128))
|
2020-04-27 08:19:20 +00:00
|
|
|
|
setting_dark_mode = db.Column(db.Boolean, default=False)
|
2020-04-27 11:50:54 +00:00
|
|
|
|
setting_job_status_mail_notifications = db.Column(db.String(16),
|
|
|
|
|
default='end')
|
|
|
|
|
setting_job_status_site_notifications = db.Column(db.String(16),
|
|
|
|
|
default='all')
|
2020-04-29 10:17:16 +00:00
|
|
|
|
username = db.Column(db.String(64), unique=True, index=True)
|
2019-08-06 09:47:04 +00:00
|
|
|
|
# Relationships
|
2019-08-15 13:57:27 +00:00
|
|
|
|
corpora = db.relationship('Corpus', backref='creator', lazy='dynamic',
|
|
|
|
|
cascade='save-update, merge, delete')
|
|
|
|
|
jobs = db.relationship('Job', backref='creator', lazy='dynamic',
|
|
|
|
|
cascade='save-update, merge, delete')
|
2020-07-13 13:33:00 +00:00
|
|
|
|
query_results = db.relationship('QueryResult',
|
|
|
|
|
backref='creator',
|
|
|
|
|
cascade='save-update, merge, delete',
|
|
|
|
|
lazy='dynamic')
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
2020-11-19 08:41:22 +00:00
|
|
|
|
return os.path.join(current_app.config['NOPAQUE_DATA_DIR'],
|
|
|
|
|
str(self.id))
|
2020-11-13 09:01:51 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def password(self):
|
|
|
|
|
raise AttributeError('password is not a readable attribute')
|
|
|
|
|
|
|
|
|
|
@password.setter
|
|
|
|
|
def password(self, password):
|
|
|
|
|
self.password_hash = generate_password_hash(password)
|
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
|
|
|
|
dict_user = {
|
|
|
|
|
'id': self.id,
|
|
|
|
|
'role_id': self.role_id,
|
|
|
|
|
'confirmed': self.confirmed,
|
|
|
|
|
'email': self.email,
|
2021-09-08 10:56:51 +00:00
|
|
|
|
'last_seen': self.last_seen.isoformat(),
|
|
|
|
|
'member_since': self.member_since.isoformat(),
|
2021-08-18 13:09:56 +00:00
|
|
|
|
'settings': {'dark_mode': self.setting_dark_mode,
|
|
|
|
|
'job_status_mail_notifications':
|
|
|
|
|
self.setting_job_status_mail_notifications,
|
|
|
|
|
'job_status_site_notifications':
|
|
|
|
|
self.setting_job_status_site_notifications},
|
|
|
|
|
'username': self.username,
|
|
|
|
|
'role': self.role.to_dict()
|
|
|
|
|
}
|
|
|
|
|
if include_relationships:
|
|
|
|
|
dict_user['corpora'] = {corpus.id: corpus.to_dict()
|
|
|
|
|
for corpus in self.corpora}
|
|
|
|
|
dict_user['jobs'] = {job.id: job.to_dict() for job in self.jobs}
|
|
|
|
|
dict_user['query_results'] = {
|
|
|
|
|
query_result.id: query_result.to_dict()
|
|
|
|
|
for query_result in self.query_results
|
|
|
|
|
}
|
|
|
|
|
return dict_user
|
2020-04-29 10:17:16 +00:00
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
String representation of the User. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<User {}>'.format(self.username)
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super(User, self).__init__(**kwargs)
|
|
|
|
|
if self.role is None:
|
2020-11-13 12:33:32 +00:00
|
|
|
|
if self.email == current_app.config['NOPAQUE_ADMIN']:
|
2019-07-09 13:41:16 +00:00
|
|
|
|
self.role = Role.query.filter_by(name='Administrator').first()
|
|
|
|
|
if self.role is None:
|
|
|
|
|
self.role = Role.query.filter_by(default=True).first()
|
|
|
|
|
|
2019-07-08 13:59:15 +00:00
|
|
|
|
def generate_confirmation_token(self, expiration=3600):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Generates a confirmation token for user confirmation via email.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-22 07:35:23 +00:00
|
|
|
|
s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'],
|
|
|
|
|
expiration)
|
2019-07-08 13:59:15 +00:00
|
|
|
|
return s.dumps({'confirm': self.id}).decode('utf-8')
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
2019-07-08 11:55:56 +00:00
|
|
|
|
def generate_reset_token(self, expiration=3600):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Generates a reset token for password reset via email.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-22 07:35:23 +00:00
|
|
|
|
s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'],
|
|
|
|
|
expiration)
|
2019-07-08 11:55:56 +00:00
|
|
|
|
return s.dumps({'reset': self.id}).decode('utf-8')
|
|
|
|
|
|
2019-07-08 13:59:15 +00:00
|
|
|
|
def confirm(self, token):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Confirms User if the given token is valid and not expired.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-22 07:35:23 +00:00
|
|
|
|
s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'])
|
2019-07-08 13:59:15 +00:00
|
|
|
|
try:
|
|
|
|
|
data = s.loads(token.encode('utf-8'))
|
2019-08-22 07:35:23 +00:00
|
|
|
|
except BadSignature:
|
2019-07-08 13:59:15 +00:00
|
|
|
|
return False
|
|
|
|
|
if data.get('confirm') != self.id:
|
|
|
|
|
return False
|
|
|
|
|
self.confirmed = True
|
|
|
|
|
db.session.add(self)
|
|
|
|
|
return True
|
|
|
|
|
|
2019-07-08 13:13:32 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def reset_password(token, new_password):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Resets password for User if the given token is valid and not expired.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-22 07:35:23 +00:00
|
|
|
|
s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'])
|
2019-07-08 13:13:32 +00:00
|
|
|
|
try:
|
|
|
|
|
data = s.loads(token.encode('utf-8'))
|
2019-08-22 07:35:23 +00:00
|
|
|
|
except BadSignature:
|
2019-07-08 13:13:32 +00:00
|
|
|
|
return False
|
|
|
|
|
user = User.query.get(data.get('reset'))
|
|
|
|
|
if user is None:
|
|
|
|
|
return False
|
|
|
|
|
user.password = new_password
|
|
|
|
|
db.session.add(user)
|
|
|
|
|
return True
|
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
|
def verify_password(self, password):
|
|
|
|
|
return check_password_hash(self.password_hash, password)
|
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
def can(self, perm):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Checks if a User with its current role can doe something. Checks if the
|
|
|
|
|
associated role actually has the needed Permission.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
return self.role is not None and self.role.has_permission(perm)
|
|
|
|
|
|
|
|
|
|
def is_administrator(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Checks if User has Admin permissions.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-09 13:41:16 +00:00
|
|
|
|
return self.can(Permission.ADMIN)
|
|
|
|
|
|
2019-11-14 08:48:30 +00:00
|
|
|
|
def delete(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-11-14 08:48:30 +00:00
|
|
|
|
Delete the user and its corpora and jobs from database and filesystem.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
shutil.rmtree(self.path, ignore_errors=True)
|
2019-09-17 14:31:41 +00:00
|
|
|
|
db.session.delete(self)
|
2019-09-11 12:51:59 +00:00
|
|
|
|
|
2019-09-09 14:17:59 +00:00
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
class AnonymousUser(AnonymousUserMixin):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-07-11 13:33:48 +00:00
|
|
|
|
Model replaces the default AnonymousUser.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-06 12:26:22 +00:00
|
|
|
|
|
2019-07-09 13:41:16 +00:00
|
|
|
|
def can(self, permissions):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def is_administrator(self):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 14:52:05 +00:00
|
|
|
|
class JobInput(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-17 11:26:20 +00:00
|
|
|
|
Class to define JobInputs.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-16 14:52:05 +00:00
|
|
|
|
__tablename__ = 'job_inputs'
|
|
|
|
|
# Primary key
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2020-04-29 10:17:16 +00:00
|
|
|
|
filename = db.Column(db.String(255))
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def download_url(self):
|
2020-12-07 15:10:40 +00:00
|
|
|
|
return url_for('jobs.download_job_input', job_id=self.job_id,
|
2020-12-03 14:13:24 +00:00
|
|
|
|
job_input_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/jobs/{}/inputs/{}'.format(self.job_id, self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
return os.path.join(self.job.path, self.filename)
|
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('jobs.job', job_id=self.job_id,
|
|
|
|
|
_anchor='job-{}-input-{}'.format(self.job_id, self.id))
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def user_id(self):
|
|
|
|
|
return self.job.user_id
|
|
|
|
|
|
2019-10-17 11:26:20 +00:00
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-17 11:26:20 +00:00
|
|
|
|
String representation of the JobInput. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<JobInput {}>'.format(self.filename)
|
2019-10-17 11:26:20 +00:00
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
2020-12-03 14:13:24 +00:00
|
|
|
|
return {'download_url': self.download_url,
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
2020-04-29 10:17:16 +00:00
|
|
|
|
'job_id': self.job_id,
|
|
|
|
|
'filename': self.filename}
|
2019-10-17 11:26:20 +00:00
|
|
|
|
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
|
|
|
|
class JobResult(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-17 11:26:20 +00:00
|
|
|
|
Class to define JobResults.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-16 14:52:05 +00:00
|
|
|
|
__tablename__ = 'job_results'
|
|
|
|
|
# Primary key
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2020-04-29 10:17:16 +00:00
|
|
|
|
filename = db.Column(db.String(255))
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def download_url(self):
|
2020-12-07 15:10:40 +00:00
|
|
|
|
return url_for('jobs.download_job_result', job_id=self.job_id,
|
2020-12-03 14:13:24 +00:00
|
|
|
|
job_result_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/jobs/{}/results/{}'.format(self.job_id, self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
2020-12-02 13:26:17 +00:00
|
|
|
|
return os.path.join(self.job.path, 'output', self.filename)
|
2020-11-13 09:01:51 +00:00
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('jobs.job', job_id=self.job_id,
|
|
|
|
|
_anchor='job-{}-result-{}'.format(self.job_id, self.id))
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def user_id(self):
|
|
|
|
|
return self.job.user_id
|
|
|
|
|
|
2019-10-17 11:26:20 +00:00
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-17 11:26:20 +00:00
|
|
|
|
String representation of the JobResult. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<JobResult {}>'.format(self.filename)
|
2019-10-17 11:26:20 +00:00
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
2020-12-03 14:13:24 +00:00
|
|
|
|
return {'download_url': self.download_url,
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
2020-04-29 10:17:16 +00:00
|
|
|
|
'job_id': self.job_id,
|
|
|
|
|
'filename': self.filename}
|
2019-10-17 11:26:20 +00:00
|
|
|
|
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
2019-08-06 09:47:04 +00:00
|
|
|
|
class Job(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-05 14:45:38 +00:00
|
|
|
|
Class to define Jobs.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-05 14:45:38 +00:00
|
|
|
|
__tablename__ = 'jobs'
|
2019-08-06 09:47:04 +00:00
|
|
|
|
# Primary key
|
2019-08-05 14:45:38 +00:00
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2019-08-06 12:26:22 +00:00
|
|
|
|
creation_date = db.Column(db.DateTime(), default=datetime.utcnow)
|
2019-08-09 09:48:43 +00:00
|
|
|
|
description = db.Column(db.String(255))
|
2019-08-09 13:59:53 +00:00
|
|
|
|
end_date = db.Column(db.DateTime())
|
2019-08-06 09:47:04 +00:00
|
|
|
|
service = db.Column(db.String(64))
|
|
|
|
|
'''
|
2019-08-09 09:48:43 +00:00
|
|
|
|
' Service specific arguments as string list.
|
2020-07-02 10:01:40 +00:00
|
|
|
|
' Example: ["-l eng", "--binarize"]
|
2019-08-06 09:47:04 +00:00
|
|
|
|
'''
|
|
|
|
|
service_args = db.Column(db.String(255))
|
2019-08-09 09:48:43 +00:00
|
|
|
|
service_version = db.Column(db.String(16))
|
|
|
|
|
status = db.Column(db.String(16))
|
2019-08-06 09:47:04 +00:00
|
|
|
|
title = db.Column(db.String(32))
|
2019-10-16 14:52:05 +00:00
|
|
|
|
# Relationships
|
2019-11-14 08:48:30 +00:00
|
|
|
|
inputs = db.relationship('JobInput', backref='job', lazy='dynamic',
|
2019-10-16 14:52:05 +00:00
|
|
|
|
cascade='save-update, merge, delete')
|
2019-11-14 08:48:30 +00:00
|
|
|
|
results = db.relationship('JobResult', backref='job', lazy='dynamic',
|
2019-10-16 14:52:05 +00:00
|
|
|
|
cascade='save-update, merge, delete')
|
2020-11-13 09:01:51 +00:00
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/jobs/{}'.format(self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
return os.path.join(self.creator.path, 'jobs', str(self.id))
|
2019-08-05 14:45:38 +00:00
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
2020-12-07 15:10:40 +00:00
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('jobs.job', job_id=self.id)
|
2020-12-03 14:13:24 +00:00
|
|
|
|
|
2019-08-05 14:45:38 +00:00
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-05 14:45:38 +00:00
|
|
|
|
String representation of the Job. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<Job {}>'.format(self.title)
|
2019-08-05 14:45:38 +00:00
|
|
|
|
|
2019-11-14 08:48:30 +00:00
|
|
|
|
def delete(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-04-21 16:34:21 +00:00
|
|
|
|
Delete the job and its inputs and results from the database.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-07-09 13:07:43 +00:00
|
|
|
|
if self.status not in ['complete', 'failed']:
|
|
|
|
|
self.status = 'canceling'
|
|
|
|
|
db.session.commit()
|
|
|
|
|
while self.status != 'canceled':
|
|
|
|
|
# In case the daemon handled a job in any way
|
|
|
|
|
if self.status != 'canceling':
|
|
|
|
|
self.status = 'canceling'
|
|
|
|
|
db.session.commit()
|
|
|
|
|
sleep(1)
|
|
|
|
|
db.session.refresh(self)
|
2020-11-13 09:01:51 +00:00
|
|
|
|
shutil.rmtree(self.path, ignore_errors=True)
|
2019-11-14 08:48:30 +00:00
|
|
|
|
db.session.delete(self)
|
|
|
|
|
|
2020-07-09 07:42:30 +00:00
|
|
|
|
def restart(self):
|
|
|
|
|
'''
|
2021-05-03 09:12:40 +00:00
|
|
|
|
Restart a job - only if the status is complete or failed
|
2020-07-09 07:42:30 +00:00
|
|
|
|
'''
|
|
|
|
|
|
2021-05-03 09:12:40 +00:00
|
|
|
|
if self.status not in ['complete', 'failed']:
|
|
|
|
|
raise Exception('Could not restart job: status is not "complete/failed"') # noqa
|
2020-11-13 09:01:51 +00:00
|
|
|
|
shutil.rmtree(os.path.join(self.path, 'output'), ignore_errors=True)
|
|
|
|
|
shutil.rmtree(os.path.join(self.path, 'pyflow.data'), ignore_errors=True) # noqa
|
2021-08-23 14:31:06 +00:00
|
|
|
|
for result in self.results:
|
|
|
|
|
db.session.delete(result)
|
2020-07-09 07:42:30 +00:00
|
|
|
|
self.end_date = None
|
|
|
|
|
self.status = 'submitted'
|
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
|
|
|
|
dict_job = {
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
|
|
|
|
'user_id': self.user_id,
|
2021-09-08 10:56:51 +00:00
|
|
|
|
'creation_date': self.creation_date.isoformat(),
|
2021-08-18 13:09:56 +00:00
|
|
|
|
'description': self.description,
|
2021-09-08 10:56:51 +00:00
|
|
|
|
'end_date': self.end_date.isoformat() if self.end_date else None,
|
2021-08-18 13:09:56 +00:00
|
|
|
|
'service': self.service,
|
|
|
|
|
'service_args': self.service_args,
|
|
|
|
|
'service_version': self.service_version,
|
|
|
|
|
'status': self.status,
|
|
|
|
|
'title': self.title,
|
|
|
|
|
}
|
|
|
|
|
if include_relationships:
|
|
|
|
|
dict_job['inputs'] = {input.id: input.to_dict()
|
|
|
|
|
for input in self.inputs}
|
|
|
|
|
dict_job['results'] = {result.id: result.to_dict()
|
|
|
|
|
for result in self.results}
|
|
|
|
|
return dict_job
|
2019-08-16 07:49:27 +00:00
|
|
|
|
|
2019-08-05 14:45:38 +00:00
|
|
|
|
|
2019-10-16 14:52:05 +00:00
|
|
|
|
class CorpusFile(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-16 14:52:05 +00:00
|
|
|
|
Class to define Files.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-10-16 14:52:05 +00:00
|
|
|
|
__tablename__ = 'corpus_files'
|
|
|
|
|
# Primary key
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2020-01-08 15:02:42 +00:00
|
|
|
|
address = db.Column(db.String(255))
|
|
|
|
|
author = db.Column(db.String(255))
|
|
|
|
|
booktitle = db.Column(db.String(255))
|
|
|
|
|
chapter = db.Column(db.String(255))
|
|
|
|
|
editor = db.Column(db.String(255))
|
2019-10-28 14:46:25 +00:00
|
|
|
|
filename = db.Column(db.String(255))
|
2020-01-08 15:02:42 +00:00
|
|
|
|
institution = db.Column(db.String(255))
|
|
|
|
|
journal = db.Column(db.String(255))
|
|
|
|
|
pages = db.Column(db.String(255))
|
|
|
|
|
publisher = db.Column(db.String(255))
|
2019-10-28 14:46:25 +00:00
|
|
|
|
publishing_year = db.Column(db.Integer)
|
2020-01-08 15:02:42 +00:00
|
|
|
|
school = db.Column(db.String(255))
|
|
|
|
|
title = db.Column(db.String(255))
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def download_url(self):
|
|
|
|
|
return url_for('corpora.download_corpus_file',
|
|
|
|
|
corpus_id=self.corpus_id, corpus_file_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/corpora/{}/files/{}'.format(self.corpus_id, self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
return os.path.join(self.corpus.path, self.filename)
|
|
|
|
|
|
2020-12-03 14:13:24 +00:00
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('corpora.corpus_file', corpus_id=self.corpus_id,
|
|
|
|
|
corpus_file_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def user_id(self):
|
|
|
|
|
return self.corpus.user_id
|
|
|
|
|
|
2019-10-30 07:28:52 +00:00
|
|
|
|
def delete(self):
|
2020-07-10 09:36:54 +00:00
|
|
|
|
try:
|
2020-11-13 09:01:51 +00:00
|
|
|
|
os.remove(self.path)
|
2020-07-10 09:36:54 +00:00
|
|
|
|
except OSError:
|
2020-11-13 09:01:51 +00:00
|
|
|
|
logging.error('Removing {} led to an OSError!'.format(self.path))
|
2020-07-10 09:36:54 +00:00
|
|
|
|
pass
|
2019-10-30 07:28:52 +00:00
|
|
|
|
db.session.delete(self)
|
2020-07-10 09:36:54 +00:00
|
|
|
|
self.corpus.status = 'unprepared'
|
2019-10-30 07:28:52 +00:00
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
2020-12-03 14:13:24 +00:00
|
|
|
|
return {'download_url': self.download_url,
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
2020-04-29 10:17:16 +00:00
|
|
|
|
'corpus_id': self.corpus_id,
|
2020-04-15 12:30:41 +00:00
|
|
|
|
'address': self.address,
|
|
|
|
|
'author': self.author,
|
|
|
|
|
'booktitle': self.booktitle,
|
|
|
|
|
'chapter': self.chapter,
|
|
|
|
|
'editor': self.editor,
|
|
|
|
|
'filename': self.filename,
|
|
|
|
|
'institution': self.institution,
|
|
|
|
|
'journal': self.journal,
|
|
|
|
|
'pages': self.pages,
|
|
|
|
|
'publisher': self.publisher,
|
|
|
|
|
'publishing_year': self.publishing_year,
|
|
|
|
|
'school': self.school,
|
2020-04-29 10:17:16 +00:00
|
|
|
|
'title': self.title}
|
2020-04-15 12:30:41 +00:00
|
|
|
|
|
2019-10-16 14:52:05 +00:00
|
|
|
|
|
2019-08-06 10:06:41 +00:00
|
|
|
|
class Corpus(db.Model):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-06 10:06:41 +00:00
|
|
|
|
Class to define a corpus.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2019-08-06 10:06:41 +00:00
|
|
|
|
__tablename__ = 'corpora'
|
|
|
|
|
# Primary key
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-04-29 10:17:16 +00:00
|
|
|
|
# Foreign keys
|
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
|
2020-04-27 08:30:38 +00:00
|
|
|
|
# Fields
|
2019-08-06 13:41:07 +00:00
|
|
|
|
creation_date = db.Column(db.DateTime(), default=datetime.utcnow)
|
2021-01-28 10:33:04 +00:00
|
|
|
|
current_nr_of_tokens = db.Column(db.Integer, default=0)
|
2019-08-12 06:57:21 +00:00
|
|
|
|
description = db.Column(db.String(255))
|
2020-04-30 09:32:48 +00:00
|
|
|
|
last_edited_date = db.Column(db.DateTime(), default=datetime.utcnow)
|
2021-01-28 10:33:04 +00:00
|
|
|
|
max_nr_of_tokens = 2147483647
|
2020-11-13 09:01:51 +00:00
|
|
|
|
status = db.Column(db.String(16), default='unprepared')
|
2019-08-06 10:06:41 +00:00
|
|
|
|
title = db.Column(db.String(32))
|
2020-10-29 14:20:30 +00:00
|
|
|
|
archive_file = db.Column(db.String(255))
|
2019-10-16 14:52:05 +00:00
|
|
|
|
# Relationships
|
2019-11-14 08:48:30 +00:00
|
|
|
|
files = db.relationship('CorpusFile', backref='corpus', lazy='dynamic',
|
2019-10-16 14:52:05 +00:00
|
|
|
|
cascade='save-update, merge, delete')
|
2019-08-06 10:06:41 +00:00
|
|
|
|
|
2020-12-07 15:10:40 +00:00
|
|
|
|
@property
|
|
|
|
|
def analysis_url(self):
|
|
|
|
|
return url_for('corpora.analyse_corpus', corpus_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/corpora/{}'.format(self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
return os.path.join(self.creator.path, 'corpora', str(self.id))
|
|
|
|
|
|
2020-12-04 13:16:00 +00:00
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('corpora.corpus', corpus_id=self.id)
|
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
|
|
|
|
dict_corpus = {
|
|
|
|
|
'analysis_url': self.analysis_url,
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
|
|
|
|
'user_id': self.user_id,
|
2021-09-08 10:56:51 +00:00
|
|
|
|
'creation_date': self.creation_date.isoformat(),
|
2021-08-18 13:09:56 +00:00
|
|
|
|
'current_nr_of_tokens': self.current_nr_of_tokens,
|
|
|
|
|
'description': self.description,
|
|
|
|
|
'status': self.status,
|
2021-09-08 10:56:51 +00:00
|
|
|
|
'last_edited_date': self.last_edited_date.isoformat(),
|
2021-08-18 13:09:56 +00:00
|
|
|
|
'max_nr_of_tokens': self.max_nr_of_tokens,
|
|
|
|
|
'title': self.title,
|
|
|
|
|
}
|
|
|
|
|
if include_relationships:
|
|
|
|
|
dict_corpus['files'] = {file.id: file.to_dict()
|
|
|
|
|
for file in self.files}
|
|
|
|
|
return dict_corpus
|
2019-08-06 10:06:41 +00:00
|
|
|
|
|
2020-07-10 09:36:54 +00:00
|
|
|
|
def build(self):
|
2020-11-13 09:01:51 +00:00
|
|
|
|
output_dir = os.path.join(self.path, 'merged')
|
2020-07-10 09:36:54 +00:00
|
|
|
|
shutil.rmtree(output_dir, ignore_errors=True)
|
|
|
|
|
os.mkdir(output_dir)
|
|
|
|
|
master_element_tree = ET.ElementTree(
|
|
|
|
|
ET.fromstring('<corpus>\n</corpus>')
|
|
|
|
|
)
|
|
|
|
|
for corpus_file in self.files:
|
2020-11-13 09:01:51 +00:00
|
|
|
|
element_tree = ET.parse(corpus_file.path)
|
2020-07-10 09:36:54 +00:00
|
|
|
|
text_node = element_tree.find('text')
|
|
|
|
|
text_node.set('address', corpus_file.address or "NULL")
|
|
|
|
|
text_node.set('author', corpus_file.author)
|
|
|
|
|
text_node.set('booktitle', corpus_file.booktitle or "NULL")
|
|
|
|
|
text_node.set('chapter', corpus_file.chapter or "NULL")
|
|
|
|
|
text_node.set('editor', corpus_file.editor or "NULL")
|
|
|
|
|
text_node.set('institution', corpus_file.institution or "NULL")
|
|
|
|
|
text_node.set('journal', corpus_file.journal or "NULL")
|
|
|
|
|
text_node.set('pages', corpus_file.pages or "NULL")
|
|
|
|
|
text_node.set('publisher', corpus_file.publisher or "NULL")
|
|
|
|
|
text_node.set('publishing_year', str(corpus_file.publishing_year))
|
|
|
|
|
text_node.set('school', corpus_file.school or "NULL")
|
|
|
|
|
text_node.set('title', corpus_file.title)
|
2020-11-13 09:01:51 +00:00
|
|
|
|
element_tree.write(corpus_file.path)
|
2020-07-10 09:36:54 +00:00
|
|
|
|
master_element_tree.getroot().insert(1, text_node)
|
|
|
|
|
output_file = os.path.join(output_dir, 'corpus.vrt')
|
|
|
|
|
master_element_tree.write(output_file,
|
|
|
|
|
xml_declaration=True,
|
|
|
|
|
encoding='utf-8')
|
|
|
|
|
self.last_edited_date = datetime.utcnow()
|
|
|
|
|
self.status = 'submitted'
|
|
|
|
|
|
2019-10-30 07:28:52 +00:00
|
|
|
|
def delete(self):
|
2020-11-13 09:01:51 +00:00
|
|
|
|
shutil.rmtree(self.path, ignore_errors=True)
|
2019-09-24 12:04:49 +00:00
|
|
|
|
db.session.delete(self)
|
|
|
|
|
|
2020-04-21 16:34:21 +00:00
|
|
|
|
def __repr__(self):
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-04-21 16:34:21 +00:00
|
|
|
|
String representation of the corpus. For human readability.
|
2020-05-14 13:30:13 +00:00
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
return '<Corpus {}>'.format(self.title)
|
2019-11-04 14:06:54 +00:00
|
|
|
|
|
2019-08-22 07:35:23 +00:00
|
|
|
|
|
2020-07-13 13:33:00 +00:00
|
|
|
|
class QueryResult(db.Model):
|
|
|
|
|
'''
|
|
|
|
|
Class to define a corpus analysis result.
|
|
|
|
|
'''
|
|
|
|
|
__tablename__ = 'query_results'
|
|
|
|
|
# Primary key
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
# Foreign keys
|
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
|
|
|
|
|
# Fields
|
|
|
|
|
description = db.Column(db.String(255))
|
|
|
|
|
filename = db.Column(db.String(255))
|
|
|
|
|
query_metadata = db.Column(db.JSON())
|
|
|
|
|
title = db.Column(db.String(32))
|
|
|
|
|
|
2020-12-04 13:16:00 +00:00
|
|
|
|
@property
|
|
|
|
|
def download_url(self):
|
|
|
|
|
return url_for('corpora.download_query_result',
|
|
|
|
|
query_result_id=self.id)
|
|
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
@property
|
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
|
return '/query_results/{}'.format(self.id)
|
|
|
|
|
|
2020-11-13 09:01:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
2020-12-02 13:26:17 +00:00
|
|
|
|
return os.path.join(
|
|
|
|
|
self.creator.path, 'query_results', str(self.id), self.filename)
|
2020-11-13 09:01:51 +00:00
|
|
|
|
|
2020-12-04 13:16:00 +00:00
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
|
|
|
|
return url_for('corpora.query_result', query_result_id=self.id)
|
|
|
|
|
|
2020-07-13 13:33:00 +00:00
|
|
|
|
def delete(self):
|
2020-11-13 09:01:51 +00:00
|
|
|
|
shutil.rmtree(self.path, ignore_errors=True)
|
2020-07-13 13:33:00 +00:00
|
|
|
|
db.session.delete(self)
|
|
|
|
|
|
2021-08-18 13:09:56 +00:00
|
|
|
|
def to_dict(self, include_relationships=True):
|
2020-12-04 13:16:00 +00:00
|
|
|
|
return {'download_url': self.download_url,
|
|
|
|
|
'url': self.url,
|
|
|
|
|
'id': self.id,
|
2020-07-13 13:33:00 +00:00
|
|
|
|
'user_id': self.user_id,
|
2020-12-07 15:10:40 +00:00
|
|
|
|
'corpus_title': self.query_metadata['corpus_name'],
|
2020-07-13 13:33:00 +00:00
|
|
|
|
'description': self.description,
|
|
|
|
|
'filename': self.filename,
|
2020-12-07 15:10:40 +00:00
|
|
|
|
'query': self.query_metadata['query'],
|
2020-07-15 09:07:03 +00:00
|
|
|
|
'query_metadata': self.query_metadata,
|
2020-07-13 13:33:00 +00:00
|
|
|
|
'title': self.title}
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
'''
|
2020-11-13 09:01:51 +00:00
|
|
|
|
String representation of the QueryResult. For human readability.
|
2020-07-13 13:33:00 +00:00
|
|
|
|
'''
|
|
|
|
|
return '<QueryResult {}>'.format(self.title)
|
|
|
|
|
|
|
|
|
|
|
2019-08-06 09:47:04 +00:00
|
|
|
|
'''
|
|
|
|
|
' Flask-Login is told to use the application’s custom anonymous user by setting
|
|
|
|
|
' its class in the login_manager.anonymous_user attribute.
|
|
|
|
|
'''
|
|
|
|
|
login_manager.anonymous_user = AnonymousUser
|
2019-07-09 13:41:16 +00:00
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
|
def load_user(user_id):
|
|
|
|
|
return User.query.get(int(user_id))
|