Commit 3ffad0ed authored by sim's avatar sim

Clean legacy stuff in Node model

parent f7a2666f
from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import relationship, validates
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.types import TypeDecorator, \
Integer, REAL, Boolean, DateTime, String, Text
from sqlalchemy_utils.types import TSVectorType
......@@ -9,7 +10,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import text
__all__ = ["Column", "ForeignKey", "UniqueConstraint", "Index", "relationship",
"text",
"text", "flag_modified",
"validates", "ValidatorMixin",
"Integer", "Float", "Boolean", "DateTime", "String", "Text",
"TSVectorType",
......
from gargantext.core.db import session
from gargantext.constants import NODETYPES, LISTTYPES
from datetime import datetime
from gargantext.constants import NODETYPES
from .base import Base, Column, ForeignKey, relationship, TypeDecorator, Index, \
Integer, Float, String, DateTime, JSONB, TSVectorType, \
MutableList, MutableDict, validates, ValidatorMixin, text
MutableList, MutableDict, validates, ValidatorMixin, text, \
flag_modified
from .users import User
__all__ = ['Node', 'NodeNode', 'CorpusNode']
......@@ -118,107 +116,14 @@ class Node(ValidatorMixin, Base):
def validate_name(self, key, value):
return self.enforce_length(key, value)
@property
def ngrams(self):
"""Pseudo-attribute allowing to retrieve a node's ngrams.
Returns a query (which can be further filtered), of which returned rows
are the ngram's weight for this node and the ngram.
"""
from . import NodeNgram, Ngram
query = (session
.query(NodeNgram.weight, Ngram)
.select_from(NodeNgram)
.join(Ngram)
.filter(NodeNgram.node_id == self.id)
)
return query
def as_list(self):
"""Retrieve the current node as a list/matrix of ngrams identifiers.
See `gargantext.util.lists` and `gargantext.constants.LISTTYPES`
for more info.
"""
try:
return LISTTYPES[self.typename](self.id)
except KeyError:
raise ValueError('This node\'s typename is not convertible to a list: %s (accepted values: %s)' % (
self.typename,
', '.join(LISTTYPES.keys())
))
def save_hyperdata(self):
"""This is a necessary, yet ugly trick.
Indeed, PostgreSQL does not yet manage incremental updates (see
https://bashelton.com/2014/03/updating-postgresql-json-fields-via-sqlalchemy/)
"""
from sqlalchemy.orm.attributes import flag_modified
flag_modified(self, 'hyperdata')
# # previous trick (even super-uglier)
# hyperdata = self.hyperdata
# self.hyperdata = None
# session.add(self)
# session.commit()
# self.hyperdata = hyperdata
# session.add(self)
# session.commit()
def children(self, typename=None, order=None):
"""Return a query to all the direct children of the current node.
Allows filtering by typename (see `constants.py`)
"""
query = session.query(Node).filter(Node.parent_id == self.id)
if typename is not None:
query = query.filter(Node.typename == typename)
if order is not None:
query = query.order_by(Node.name)
return query
def add_child(self, **kwargs):
"""Create and return a new direct child of the current node.
"""
return Node(
user_id = self.user_id,
parent_id = self.id,
**kwargs
)
def status(self, action=None, progress=0, complete=False, error=None):
"""Get or update the status of the given action.
If no action is given, the status of the first uncomplete or last item
is returned.
The `complete` parameter should be a boolean.
The `error` parameter should be an exception.
"""
date = datetime.now()
# if the hyperdata do not have data about status
if 'statuses' not in self.hyperdata:
self['statuses'] = MutableList()
# if no action name is given, return the last appended status
if action is None:
for status in self['statuses']:
if not status['complete']:
return status
if len(self['statuses']):
return self['statuses'][-1]
return None
# retrieve the status concerning by the given action name
for status in self['statuses']:
if status['action'] == action:
if error:
status['error'] = error
if progress:
status['progress'] = progress
if complete:
status['complete'] = complete
if error or progress or complete:
status['date'] = date
return status
# if no status has been found for the action, append a new one
self['statuses'].append(MutableDict(
{'action':action, 'progress':progress, 'complete':complete, 'error':error, 'date':date}
))
return self['statuses'][-1]
class CorpusNode(Node):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment