forked from nkovacne/pcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbschema.py
47 lines (38 loc) · 1.57 KB
/
dbschema.py
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
# -*- coding: utf-8 -*-
from sqlalchemy import Table, Column, Integer, String, Boolean, DateTime, MetaData
from sqlalchemy.orm import mapper
import datetime
# Metadata object for storing DB schema
metadata = MetaData()
# Schema of the delivery table
delivery_table = Table('delivery', metadata,
Column('id', Integer, primary_key=True, nullable=False, autoincrement=True),
Column('when', DateTime, nullable=False, default=datetime.datetime.utcnow),
Column('sender', String(length=100), nullable=False),
Column('destination', String(length=100), nullable=False),
Column('country', String(length=2), nullable=False),
Column('valid', Boolean, nullable=False, default=True)
)
# Schema of the blocked table
blocked_table = Table('blocked', metadata,
Column('id', Integer, primary_key=True, nullable=False, autoincrement=True),
Column('username', String(length=100), nullable=False),
Column('when', DateTime, nullable=False, default=datetime.datetime.utcnow)
)
# Mapping class Delivery (ORM)
class Delivery(object):
def __init__(self, sender, destination, country):
self.sender = sender
self.destination = destination
self.country = country
def __repr__(self):
return "<%s, %s, %s>" % (self.sender, self.destination, self.country)
# Mapping class Blocked (ORM)
class Blocked(object):
def __init__(self, username):
self.username = username
def __repr__(self):
return "<%s>" % (self.username)
# Establishing the mapping between schema <-> class
mapper(Delivery, delivery_table)
mapper(Blocked, blocked_table)