Move db.sql.models* to db.models*
[mediagoblin.git] / mediagoblin / plugins / oauth / migrations.py
CommitLineData
88a9662b
JW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
316e1dfd
E
17from datetime import datetime, timedelta
18from sqlalchemy import (MetaData, Table, Column,
19 Integer, Unicode, Enum, DateTime, ForeignKey)
20from sqlalchemy.ext.declarative import declarative_base
88a9662b 21
1e46dc25 22from mediagoblin.db.util import RegisterMigration
b0c8328e 23from mediagoblin.db.models import User
88a9662b 24
88a9662b
JW
25
26MIGRATIONS = {}
27
28
316e1dfd
E
29class OAuthClient_v0(declarative_base()):
30 __tablename__ = 'oauth__client'
31
32 id = Column(Integer, primary_key=True)
33 created = Column(DateTime, nullable=False,
34 default=datetime.now)
35
36 name = Column(Unicode)
37 description = Column(Unicode)
38
39 identifier = Column(Unicode, unique=True, index=True)
40 secret = Column(Unicode, index=True)
41
42 owner_id = Column(Integer, ForeignKey(User.id))
43 redirect_uri = Column(Unicode)
44
45 type = Column(Enum(
46 u'confidential',
47 u'public',
48 name=u'oauth__client_type'))
49
50
51class OAuthUserClient_v0(declarative_base()):
52 __tablename__ = 'oauth__user_client'
53 id = Column(Integer, primary_key=True)
54
55 user_id = Column(Integer, ForeignKey(User.id))
56 client_id = Column(Integer, ForeignKey(OAuthClient_v0.id))
57
58 state = Column(Enum(
59 u'approved',
60 u'rejected',
61 name=u'oauth__relation_state'))
62
63
64class OAuthToken_v0(declarative_base()):
65 __tablename__ = 'oauth__tokens'
66
67 id = Column(Integer, primary_key=True)
68 created = Column(DateTime, nullable=False,
69 default=datetime.now)
70 expires = Column(DateTime, nullable=False,
71 default=lambda: datetime.now() + timedelta(days=30))
72 token = Column(Unicode, index=True)
73 refresh_token = Column(Unicode, index=True)
74
75 user_id = Column(Integer, ForeignKey(User.id), nullable=False,
76 index=True)
77
78 client_id = Column(Integer, ForeignKey(OAuthClient_v0.id), nullable=False)
79
80 def __repr__(self):
81 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
82 self.__class__.__name__,
83 self.id,
84 self.expires.isoformat(),
85 self.user,
86 self.client)
87
88
89class OAuthCode_v0(declarative_base()):
90 __tablename__ = 'oauth__codes'
91
92 id = Column(Integer, primary_key=True)
93 created = Column(DateTime, nullable=False,
94 default=datetime.now)
95 expires = Column(DateTime, nullable=False,
96 default=lambda: datetime.now() + timedelta(minutes=5))
97 code = Column(Unicode, index=True)
98
99 user_id = Column(Integer, ForeignKey(User.id), nullable=False,
100 index=True)
101
102 client_id = Column(Integer, ForeignKey(OAuthClient_v0.id), nullable=False)
103
104
88a9662b
JW
105@RegisterMigration(1, MIGRATIONS)
106def remove_and_replace_token_and_code(db):
107 metadata = MetaData(bind=db.bind)
108
109 token_table = Table('oauth__tokens', metadata, autoload=True,
110 autoload_with=db.bind)
111
112 token_table.drop()
113
114 code_table = Table('oauth__codes', metadata, autoload=True,
115 autoload_with=db.bind)
116
117 code_table.drop()
118
316e1dfd
E
119 OAuthClient_v0.__table__.create(db.bind)
120 OAuthUserClient_v0.__table__.create(db.bind)
121 OAuthToken_v0.__table__.create(db.bind)
122 OAuthCode_v0.__table__.create(db.bind)
88a9662b
JW
123
124 db.commit()