1 | # components to store tickets in a database |
---|
2 | # we use sqlite3 |
---|
3 | import time |
---|
4 | from sqlalchemy import ( |
---|
5 | create_engine, Column, Integer, Float, String, MetaData) |
---|
6 | from sqlalchemy.ext.declarative import declarative_base |
---|
7 | from sqlalchemy.orm import sessionmaker, scoped_session |
---|
8 | |
---|
9 | |
---|
10 | Base = declarative_base() |
---|
11 | |
---|
12 | |
---|
13 | class ServiceTicket(Base): |
---|
14 | __tablename__ = 'service_tickets' |
---|
15 | |
---|
16 | id = Column(Integer, primary_key=True) |
---|
17 | ticket = Column(String(255), nullable=False) |
---|
18 | ts = Column(Float, nullable=False) |
---|
19 | service = Column(String(2048), nullable=True) |
---|
20 | user = Column(String(512), nullable=False) |
---|
21 | |
---|
22 | def __init__(self, ticket, user, service=None, timestamp=None): |
---|
23 | if timestamp is None: |
---|
24 | timestamp = time.time() |
---|
25 | self.ticket = ticket |
---|
26 | self.ts = timestamp |
---|
27 | self.service = service |
---|
28 | self.user = user |
---|
29 | |
---|
30 | def __repr__(self): |
---|
31 | return "ServiceTicket('%s', '%s', '%s', %s)" % ( |
---|
32 | self.ticket, self.user, self.service, self.ts) |
---|
33 | |
---|
34 | |
---|
35 | class LoginTicket(Base): |
---|
36 | __tablename__ = 'login_tickets' |
---|
37 | |
---|
38 | ticket = Column(String(255), primary_key=True) |
---|
39 | ts = Column(Float, nullable=False) |
---|
40 | |
---|
41 | def __init__(self, ticket, timestamp=None): |
---|
42 | if timestamp is None: |
---|
43 | timestamp = time.time() |
---|
44 | self.ticket = ticket |
---|
45 | self.ts = timestamp |
---|
46 | |
---|
47 | def __repr__(self): |
---|
48 | return "LoginTicket('%s', %s)" % (self.ticket, self.ts) |
---|
49 | |
---|
50 | |
---|
51 | class TicketGrantingCookie(Base): |
---|
52 | __tablename__ = 'ticket_granting_cookies' |
---|
53 | |
---|
54 | value = Column(String(255), primary_key=True) |
---|
55 | ts = Column(Float, nullable=False) |
---|
56 | |
---|
57 | def __init__(self, value, timestamp=None): |
---|
58 | if timestamp is None: |
---|
59 | timestamp = time.time() |
---|
60 | self.value = value |
---|
61 | self.ts = timestamp |
---|
62 | |
---|
63 | def __repr__(self): |
---|
64 | return "TicketGrantingCookie('%s', %s)" % (self.value, self.ts) |
---|
65 | |
---|
66 | |
---|
67 | Session = scoped_session(sessionmaker()) |
---|
68 | |
---|
69 | |
---|
70 | class DB(object): |
---|
71 | """Abstract database to make data persistent. |
---|
72 | |
---|
73 | Creates a database identified by `connection_string` if it does |
---|
74 | not exist and initializes sessions. |
---|
75 | """ |
---|
76 | @property |
---|
77 | def session(self): |
---|
78 | return Session |
---|
79 | |
---|
80 | def __init__(self, connection_string): |
---|
81 | self.engine = create_engine(connection_string) |
---|
82 | self.metadata = MetaData() |
---|
83 | Base.metadata.create_all(self.engine) |
---|
84 | Session.configure(bind=self.engine) |
---|
85 | |
---|
86 | def add(self, item): |
---|
87 | """Insert `item` into database. |
---|
88 | |
---|
89 | `item` must be an instance of the database content types |
---|
90 | defined in this module, normally some `Ticket` instance. |
---|
91 | """ |
---|
92 | Session.add(item) |
---|
93 | Session.commit() |
---|
94 | |
---|
95 | def delete(self, item): |
---|
96 | """Delete `item` from database. |
---|
97 | |
---|
98 | `item must be an instance of the database content types |
---|
99 | defined in this module, normally some `Ticket` instance. |
---|
100 | """ |
---|
101 | Session.delete(item) |
---|
102 | Session.commit() |
---|
103 | |
---|
104 | def query(self, *args, **kw): |
---|
105 | return Session.query(*args, **kw) |
---|
106 | |
---|
107 | |
---|
108 | class DBSessionContext(object): |
---|
109 | """A context manager providing database sessions. |
---|
110 | |
---|
111 | Creates a new (threadlocal) database session when entering and |
---|
112 | tears down the session after leaving the context. |
---|
113 | |
---|
114 | Tearing down includes committing transactions and closing the |
---|
115 | connection. |
---|
116 | |
---|
117 | Meant to be used as a wrapper for web request handlers, such, that |
---|
118 | a session is created when a request comes in and released when |
---|
119 | the response is ready to be delivered. |
---|
120 | |
---|
121 | This context manager does *not* catch any exceptions. |
---|
122 | """ |
---|
123 | def __enter__(self): |
---|
124 | return Session() |
---|
125 | |
---|
126 | def __exit__(self, *args, **kw): |
---|
127 | if args or kw: |
---|
128 | Session.rollback() |
---|
129 | Session.remove() |
---|
130 | return False |
---|