summaryrefslogtreecommitdiff
path: root/cli/app/sql/models/page.py
blob: 35efa394aa54ce25439470a99c8cd7defb2d899c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from sqlalchemy import create_engine, Table, Column, Text, String, Integer, DateTime, JSON, ForeignKey
from sqlalchemy.orm import relationship, foreign, remote
import sqlalchemy.sql.functions as func
from sqlalchemy_utc import UtcDateTime, utcnow
from wtforms_alchemy import ModelForm

from app.sql.common import db, Base, Session
from app.sql.models.tile import Tile

from app.settings import app_cfg

class Page(Base):
  """Table for storing references to pages"""
  __tablename__ = 'page'
  id = Column(Integer, primary_key=True)
  graph_id = Column(Integer, ForeignKey('graph.id'), nullable=True)
  path = Column(String(64, convert_unicode=True), nullable=False)
  title = Column(String(64, convert_unicode=True), nullable=False)
  username = Column(String(32, convert_unicode=True), nullable=False)
  description = Column(Text(convert_unicode=True), nullable=False)
  settings = Column(JSON, default={}, nullable=True)
  created_at = Column(UtcDateTime(), default=utcnow())
  updated_at = Column(UtcDateTime(), onupdate=utcnow())

  tiles = relationship("Tile", foreign_keys="Tile.page_id", lazy='dynamic', order_by="asc(Tile.sort_order)")
  backlinks = relationship("Tile", primaryjoin=id == foreign(Tile.target_page_id), lazy='dynamic')

  def toJSON(self):
    return {
      'id': self.id,
      'graph_id': self.graph_id,
      'path': self.path,
      'title': self.title,
      'username': self.username,
      'description': self.description,
      'settings': self.settings,
      'created_at': self.created_at,
      'updated_at': self.updated_at,
    }

  def toLinkJSON(self):
    data = self.toJSON()
    data['backlinks'] = [ tile.toJSON() for tile in self.backlinks ]
    return data

  def toFullJSON(self):
    data = self.toJSON()
    data['tiles'] = [ tile.toJSON() for tile in self.tiles ]
    return data

class PageForm(ModelForm):
  class Meta:
    model = Page
    exclude = ['description', 'settings', 'created_at', 'updated_at']
  def get_session():
    return Session()