class Taxonomy(db.Model): """ Table containing the taxonomy of a Node. Each term that can be assigned to the node is listed here. """ __tablename__ = "taxonomy" id = db.Column(db.Integer, primary_key=True, autoincrement=True) node_id = db.Column(db.Integer, db.ForeignKey("node.id"), nullable=False) term_id = db.Column(db.Integer, db.ForeignKey("term.id"), nullable=False) created = db.Column(db.Integer, nullable=False) @staticmethod def add(**params): params['created'] = int(time.time()) taxonomy_inst = Taxonomy(**params) db.session.add(taxonomy_inst) db.session.commit() db.session.refresh(taxonomy_inst) return taxonomy_inst.id @staticmethod def delete(**params): taxonomy_inst = Taxonomy.query.filter_by( node_id=params["node_id"], term_id=params["term_id"]).one() db.session.delete(taxonomy_inst) db.session.commit() return True
class Lophoto(db.Model): """ Table containing information about the local pictures. """ __tablename__ = "lophoto" id = db.Column(db.Integer, primary_key=True, autoincrement=True) node_id = db.Column(db.Integer, db.ForeignKey('node.id')) filename = db.Column(db.Text, nullable=False) uri = db.Column(db.Text, nullable=False) created = db.Column(db.Integer, nullable=False)
class Term(db.Model): """ Table containing the Terms from a Vocabulary. """ __tablename__ = "term" id = db.Column(db.Integer, primary_key=True) vocabulary_id = db.Column(db.Integer, db.ForeignKey("vocabulary.id"), nullable=False) name = db.Column(db.Text, nullable=False) description = db.Column(db.Text) @staticmethod def add(**params): term_inst = Term(**params) db.session.add(term_inst) db.session.commit() db.session.refresh(term_inst) return term_inst.nid
class Content(db.Model): """ Table with Node Title and Node Contents. """ __tablename__ = "content" id = db.Column(db.Integer, primary_key=True, autoincrement=True) node_id = db.Column(db.Integer, db.ForeignKey('node.id')) title = db.Column(db.Text, nullable=False) body = db.Column(db.Text) @staticmethod def delete(nid): try: content_inst = Content.query.filter_by(node_id=nid).one() except NoResultFound: current_app.logger.info( "Trying to remove content record for node id {nid}, but no record found..." .format(nid=nid)) else: db.session.delete(content_inst) db.session.commit() return True @staticmethod def update(**params): """ This method will add or edit the node title or body. :param params: Dictionary with node_id, title and body as fields. :return: """ try: content_inst = db.session.query(Content).filter_by( node_id=params['node_id']).one() content_inst.title = params["title"] content_inst.body = params["body"] except NoResultFound: content_inst = Content(**params) db.session.add(content_inst) db.session.commit() db.session.refresh(content_inst) return content_inst.id
class Photo(db.Model): """ Table containing information about the pictures on pcloud. A picture can be used by a single node only. """ __tablename__ = "photo" id = db.Column(db.Integer, primary_key=True, autoincrement=True) node_id = db.Column(db.Integer, db.ForeignKey('node.id')) filename = db.Column(db.Text, nullable=False) orig_filename = db.Column(db.Text) created = db.Column(db.Integer, nullable=False) fresh = db.Column(db.Integer) @staticmethod def add(**params): photo_inst = Photo(**params) db.session.add(photo_inst) db.session.commit() db.session.refresh(photo_inst) return photo_inst.id @staticmethod def delete(node_id): """ This method will delete the node - photo record. This happens when the node is deleted or if the node changes from type photo to type blog or when a new blog record is created, so delete may be called for non-existing records. The photo record is deleted but the photo is not removed from storage (pcloud). :param node_id: ID of the node attached to this picture.. :return: 1 - integer. """ try: photo_inst = db.session.query(Photo).filter_by( node_id=node_id).one() except NoResultFound: current_app.logger.info( "Got a request to delete Photo record for node ID {nid}, but nid not found" .format(nid=node_id)) else: current_app.logger.info( "Delete Node {nid} with Photo ID {pid} from Photo".format( nid=node_id, pid=photo_inst.id)) db.session.delete(photo_inst) db.session.commit() return 1 @staticmethod def edit(**params): """ This method will edit the photo record, based on the Node ID. Attributes in params are set, attributes not in params are left untouched. :param params: Dictionary with node_id as identifying record. :return: """ photo_inst = db.session.query(Photo).filter_by( node_id=params['node_id']).first() for k, v in params.items(): setattr(photo_inst, k, v) db.session.commit() return @staticmethod def get_node_id(filename): """ This method returns the node_id for the Photo record for picture filename, or None if the filename is not found. :param filename: Filename for which node_id needs to be returned. :return: node_id - or None if node_id is not found. """ photo_inst = db.session.query(Photo).filter_by( filename=filename).first() if photo_inst: return photo_inst.node_id else: return None
class Node(db.Model): """ Table containing the information of the database. Relationship type is called: Adjacency list. """ __tablename__ = "node" id = db.Column(db.Integer, primary_key=True, autoincrement=True) parent_id = db.Column(db.Integer, db.ForeignKey('node.id'), nullable=False) created = db.Column(db.Integer, nullable=False) modified = db.Column(db.Integer, nullable=False) revcnt = db.Column(db.Integer) type = db.Column(db.Text) children = db.relationship("Node", backref=db.backref('parent', remote_side=[id])) content = db.relationship("Content", backref="node", uselist=False) lophoto = db.relationship("Lophoto", backref="node", uselist=False) photo = db.relationship("Photo", backref="node", uselist=False) terms = db.relationship("Term", secondary="taxonomy", backref="nodes") @staticmethod def add(**params): params['created'] = int(time.time()) params['modified'] = params['created'] params['revcnt'] = 1 if "parent_id" not in params: params["parent_id"] = -1 node_inst = Node(**params) db.session.add(node_inst) db.session.commit() db.session.refresh(node_inst) return node_inst.id @staticmethod def edit(node_id): """ This method will edit the node by updating modified timestamp and revision count. :param node_id: ID of the node to be modified. :return: """ node_inst = db.session.query(Node).filter_by(id=node_id).first() node_inst.modified = int(time.time()) node_inst.revcnt += 1 # If node has a photo attached to it, set fresh to 0 if node_inst.photo: node_inst.photo.fresh = 0 db.session.commit() return @staticmethod def outline(**params): """ This method will update the parent for the node. params needs to have nid and parent_id as keys. :param params: Dictionary with nid and parent_id as keys. :return: """ node_inst = db.session.query(Node).filter_by(nid=params['nid']).first() node_inst.parent_id = params['parent_id'] node_inst.modified = int(time.time()) node_inst.revcnt += 1 db.session.commit() return @staticmethod def delete(nid): try: node_inst = Node.query.filter_by(id=nid).one() except NoResultFound: current_app.logger.info( "Trying to remove node record for node ID {nid}, but no record found" .format(nid=nid)) else: if node_inst.type == "photo": Photo.delete(nid) # Remove Content record Content.delete(nid) # Then remove node record. db.session.delete(node_inst) db.session.commit() return True @staticmethod def set_created(nid): # Set node created date to Create Date picture node_inst = Node.query.filter_by(id=nid).one() nc = node_inst.created created = node_inst.photo.created current_app.logger.info( "Node created datestamp from {nc} to {fd}".format(nc=datestamp(nc), fd=created)) node_inst.created = created db.session.commit() return