2022-02-08 11:26:20 +00:00
|
|
|
class QueryResult(FileMixin, HashidMixin, db.Model):
|
|
|
|
__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))
|
|
|
|
query_metadata = db.Column(db.JSON())
|
|
|
|
title = db.Column(db.String(32))
|
|
|
|
# Backrefs: user: User
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
'''
|
|
|
|
String representation of the QueryResult. For human readability.
|
|
|
|
'''
|
|
|
|
return f'<QueryResult {self.title}>'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def download_url(self):
|
|
|
|
return url_for(
|
|
|
|
'corpora.download_query_result', query_result_id=self.id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
return f'{self.user.jsonpatch_path}/query_results/{self.hashid}'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return os.path.join(
|
|
|
|
self.user.path, 'query_results', str(self.id), self.filename)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return url_for('corpora.query_result', query_result_id=self.id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user_hashid(self):
|
|
|
|
return self.user.hashid
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
shutil.rmtree(self.path, ignore_errors=True)
|
|
|
|
db.session.delete(self)
|
|
|
|
|
2022-09-02 11:07:30 +00:00
|
|
|
def to_json(self, backrefs=False, relationships=False):
|
|
|
|
_json = {
|
2022-02-08 11:26:20 +00:00
|
|
|
'id': self.hashid,
|
|
|
|
'corpus_title': self.query_metadata['corpus_name'],
|
|
|
|
'description': self.description,
|
|
|
|
'filename': self.filename,
|
|
|
|
'query': self.query_metadata['query'],
|
|
|
|
'query_metadata': self.query_metadata,
|
|
|
|
'title': self.title,
|
2022-09-02 11:07:30 +00:00
|
|
|
**self.file_mixin_to_json(
|
2022-02-08 11:26:20 +00:00
|
|
|
backrefs=backrefs, relationships=relationships)
|
|
|
|
}
|
|
|
|
if backrefs:
|
2022-09-02 11:07:30 +00:00
|
|
|
_json['user'] = self.user.to_json(backrefs=True, relationships=False)
|