示例#1
0
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import UUID

from flask_setup import application

SQLALCHEMY_DATABASE_URI = os.getenv(
    "SQLALCHEMY_DATABASE_URI",
    'postgresql://postgres@localhost/notification_api')

application.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI

db = SQLAlchemy(application)

NOTIFICATION_TYPE = ['email', 'sms']
notification_types = db.Enum(*NOTIFICATION_TYPE, name='notification_type')


class NotificationHistory(db.Model):
    __tablename__ = 'notifications'  # history table empty locally

    id = db.Column(UUID(as_uuid=True), primary_key=True)
    service_id = db.Column(UUID(as_uuid=True),
                           db.ForeignKey('services.id'),
                           index=True,
                           unique=False)
    service = db.relationship('Service')
    notification_type = db.Column(notification_types,
                                  index=True,
                                  nullable=False)
    created_at = db.Column(db.DateTime,
示例#2
0
文件: model.py 项目: benoit-bst/kirin
    import uuid
    return str(uuid.uuid4())


class TimestampMixin(object):
    created_at = db.Column(db.DateTime(),
                           default=datetime.datetime.utcnow,
                           nullable=False)
    updated_at = db.Column(db.DateTime(),
                           default=None,
                           onupdate=datetime.datetime.utcnow)


ModificationType = db.Enum('add',
                           'delete',
                           'update',
                           'none',
                           name='modification_type')


def get_utc_localized_timestamp_safe(timestamp):
    '''
    Return a UTC-localized timestamp (easier to manipulate)
    Result is a changed datetime if it was in another timezone,
        or just explicitly timezoned in UTC otherwise.
    :param timestamp: datetime considered UTC if not timezoned
    :return: datetime brought to UTC
    '''
    if timestamp.tzinfo is None or timestamp.tzinfo.utcoffset(
            timestamp) is None:
        return utc.localize(timestamp)
示例#3
0
文件: model.py 项目: woshilapin/kirin
def gen_uuid():
    """
    Generate uuid as string
    """
    import uuid

    return str(uuid.uuid4())


class TimestampMixin(object):
    created_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow, nullable=False)
    updated_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)


Db_TripEffect = db.Enum(*[e.name for e in TripEffect], name="trip_effect")
Db_ModificationType = db.Enum(*[t.name for t in ModificationType], name="modification_type")
Db_ConnectorType = db.Enum(*[c.value for c in ConnectorType], name="connector_type")


class VehicleJourney(db.Model):  # type: ignore
    """
    Base-schedule Vehicle Journey on a given day (navitia VJ + UTC datetime of first stop)
    """

    id = db.Column(postgresql.UUID, default=gen_uuid, primary_key=True)
    navitia_trip_id = db.Column(db.Text, nullable=False)

    # timestamp of base-schedule VJ's start (stored in UTC to be safe with db without timezone)
    start_timestamp = db.Column(db.DateTime, nullable=False)
    db.Index("start_timestamp_idx", start_timestamp)
示例#4
0
    Generate uuid as string
    """
    import uuid
    return str(uuid.uuid4())


class TimestampMixin(object):
    created_at = db.Column(db.DateTime(),
                           default=datetime.datetime.utcnow,
                           nullable=False)
    updated_at = db.Column(db.DateTime(),
                           default=None,
                           onupdate=datetime.datetime.utcnow)


Db_TripEffect = db.Enum(*[e.name for e in TripEffect], name='trip_effect')
Db_ModificationType = db.Enum(*[t.name for t in ModificationType],
                              name='modification_type')


def get_utc_localized_timestamp_safe(timestamp):
    '''
    Return a UTC-localized timestamp (easier to manipulate)
    Result is a changed datetime if it was in another timezone,
        or just explicitly timezoned in UTC otherwise.
    :param timestamp: datetime considered UTC if not timezoned
    :return: datetime brought to UTC
    '''
    if timestamp.tzinfo is None or timestamp.tzinfo.utcoffset(
            timestamp) is None:
        return utc.localize(timestamp)
示例#5
0
文件: models.py 项目: Amertz08/Robot
    '''Read left to right and thus node_a to node_b'''
    ns = 'north/south'
    ew = 'east/west'


node_connections = db.Table(
    'node_connections',
    db.Column('node_a',
              db.Integer,
              db.ForeignKey('nodes.id'),
              primary_key=True),
    db.Column('node_b',
              db.Integer,
              db.ForeignKey('nodes.id'),
              primary_key=True),
    db.Column('direction', db.Enum(DirectionType), nullable=False))


class Node(db.Model):
    __tablename__ = 'nodes'
    id = db.Column(db.Integer, primary_key=True)
    layout_id = db.Column(db.Integer,
                          db.ForeignKey('layouts.id'),
                          nullable=False)
    name = db.Column(db.String(255), nullable=False)
    connections = db.relationship(
        'Node',
        secondary=node_connections,
        primaryjoin=node_connections.c.node_a == id,
        secondaryjoin=node_connections.c.node_b == id)
示例#6
0
migrate = Migrate(app, db)

#----------------------------------------------------------------------------#
# Models.
#----------------------------------------------------------------------------#

gtype = db.Enum(
            ('Alternative', 'Alternative'),
            ('Blues', 'Blues'),
            ('Classical', 'Classical'),
            ('Country', 'Country'),
            ('Electronic', 'Electronic'),
            ('Folk', 'Folk'),
            ('Funk', 'Funk'),
            ('Hip-Hop', 'Hip-Hop'),
            ('Heavy Metal', 'Heavy Metal'),
            ('Instrumental', 'Instrumental'),
            ('Jazz', 'Jazz'),
            ('Musical Theatre', 'Musical Theatre'),
            ('Pop', 'Pop'),
            ('Punk', 'Punk'),
            ('R&B', 'R&B'),
            ('Reggae', 'Reggae'),
            ('Rock n Roll', 'Rock n Roll'),
            ('Soul', 'Soul'),
            ('Other', 'Other'))

class Venue(db.Model):
    __tablename__ = 'venue'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    @property
    def attribute_class(self):
        return FIELDTYPE_ATTRIBUTECLASS_MAP[self]


FIELDTYPE_ATTRIBUTECLASS_MAP = {
    FieldType.UnicodeText: AttributeUnicodeText,
    FieldType.Boolean: AttributeBoolean,
    FieldType.Integer: AttributeInteger,
    FieldType.Float: AttributeFloat,
    FieldType.Date: AttributeDate,
    FieldType.DateTime: AttributeDateTime,
    FieldType.Time: AttributeTime,
}

saEnumFieldType = sa.Enum(FieldType)  # to be imported in another module


class Field(sa.Model):
    __tablename__ = "field"
    field_id = sa.Column(sa.Integer(), primary_key=True)
    name = sa.Column(sa.UnicodeText(), nullable=False)
    type_ = sa.Column(saEnumFieldType, nullable=False)

    def __repr__(self):
        return f"<{self.__class__.__name__} {self.name!r} {self.type_name!r}>"

    @property
    def type_name(self):
        # used in __repr__()
        try: