~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/database/samodels.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/databse/alchemy.py
 
2
#
 
3
#
 
4
 
 
5
""" alchemy interface. """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
## gozerbot imports
 
10
 
 
11
from gozerbot.utils.log import rlog
 
12
 
 
13
## sqlalchemy imports
 
14
 
 
15
from sqlalchemy.ext.declarative import declarative_base
 
16
from sqlalchemy.ext.associationproxy import association_proxy
 
17
from sqlalchemy.ext.orderinglist import ordering_list
 
18
from sqlalchemy import Text, Integer, Sequence, ForeignKey, DateTime, Column, String, Table
 
19
from sqlalchemy.orm import relation
 
20
 
 
21
## basic imports
 
22
 
 
23
import sqlalchemy
 
24
import os
 
25
import time
 
26
import logging
 
27
from datetime import datetime
 
28
 
 
29
created = []
 
30
 
 
31
def create_all(plugname='all', base=None):
 
32
    rlog(10, 'alchemy', 'running create_all (%s)' % plugname)
 
33
    if plugname not in created:
 
34
        created.append(plugname)
 
35
        if not base:
 
36
            base = Base
 
37
        try: base.metadata.create_all()
 
38
        except Exception, ex: rlog(10, 'alchemy', 'problem creating %s tables: %s' % (str(base), str(ex)))
 
39
    else:
 
40
        rlog(10, 'alchemy', '%s tables already created' % plugname)
 
41
 
 
42
# defines 
 
43
 
 
44
Base = declarative_base()
 
45
 
 
46
## USER MODEL
 
47
 
 
48
user = Table('user', Base.metadata,
 
49
    Column('name', String(255), primary_key=True)
 
50
)
 
51
 
 
52
email = Table('email', Base.metadata,
 
53
    Column('name', String(255), ForeignKey(user.c.name), nullable=False),
 
54
    Column('email', String(255), nullable=False),
 
55
    Column('order', Integer, nullable=False)
 
56
)
 
57
 
 
58
class User(Base):
 
59
    __table__ = user
 
60
    _userhosts = relation("UserHost", backref="user", cascade="all, delete-orphan")
 
61
    _perms = relation("Perms", backref="user", cascade="all, delete-orphan")
 
62
    _permits = relation("Permits", backref="user",cascade="all, delete-orphan" )
 
63
    _statuses = relation("Statuses", backref="user", cascade="all, delete-orphan")
 
64
    _pasword = relation("Passwords", backref="user", cascade="all, delete-orphan")
 
65
    _email = relation("Email", backref="user", collection_class=ordering_list('order'),
 
66
                        cascade="all, delete-orphan", order_by=[email.c.order])
 
67
    email = association_proxy('_email', 'email')
 
68
    userhosts = association_proxy('_userhosts', 'userhost')
 
69
    perms = association_proxy('_perms', 'perm')
 
70
    permits = association_proxy('_permits', 'permit')
 
71
    statuses = association_proxy('_statuses', 'status')
 
72
    password = association_proxy('_password', 'passwd')
 
73
 
 
74
class Email(Base):
 
75
    __table__ = email
 
76
    __mapper_args__ = {'primary_key':[email.c.name,email.c.email]}
 
77
 
 
78
    def __init__(self, email):
 
79
        self.email = email
 
80
 
 
81
class UserHost(Base):
 
82
    __tablename__ = 'userhosts'
 
83
    userhost = Column('userhost', String(255), primary_key=True)
 
84
    name = Column('name', String(255), ForeignKey('user.name'), nullable=False)
 
85
 
 
86
    def __init__(self, userhost):
 
87
        self.userhost = userhost
 
88
 
 
89
class Perms(Base):
 
90
    __tablename__ = 'perms'
 
91
    name = Column('name', String(255), ForeignKey('user.name'), nullable=False)
 
92
    perm = Column('perm', String(255), nullable=False)
 
93
    __mapper_args__ = {'primary_key':[name,perm]}
 
94
 
 
95
    def __init__(self, perm):
 
96
        self.perm = perm
 
97
 
 
98
class Permits(Base):
 
99
    __tablename__ = 'permits'
 
100
    name = Column('name', String(255), ForeignKey('user.name'), nullable=False)
 
101
    permit = Column('permit', String(255), nullable=False)
 
102
    __mapper_args__ = {'primary_key':[name,permit]}
 
103
 
 
104
    def __init__(self, permit):
 
105
        self.permit = permit
 
106
 
 
107
class Statuses(Base):    
 
108
    __tablename__ = 'statuses'
 
109
    name = Column('name', String(255), ForeignKey('user.name'), nullable=False)
 
110
    status = Column('status', String(255), nullable=False)
 
111
    __mapper_args__ = {'primary_key':[name,status]}
 
112
 
 
113
    def __init__(self, status):
 
114
        self.status = status
 
115
 
 
116
class Passwords(Base):    
 
117
    __tablename__ = 'passwords'
 
118
    name = Column('name', String(255), ForeignKey('user.name'), primary_key=True)
 
119
    passwd = Column('passwd', String(255), nullable=False)
 
120
 
 
121
    def __init__(self, passwd):
 
122
        self.passwd = passwd
 
123
 
 
124
## Birthday Model
 
125
 
 
126
class Birthday(Base):
 
127
    __tablename__ = 'birthday'
 
128
    __table_args__ = {'useexisting': True}
 
129
    name = Column('name', String(255), primary_key=True)
 
130
    birthday = Column('birthday', String(255), nullable=False)
 
131
 
 
132
    def __init__(self, name, birthday):
 
133
        self.name = name
 
134
        self.birthday = birthday
 
135
 
 
136
 
 
137
## Quote Model
 
138
 
 
139
class Quotes(Base):
 
140
    __tablename__ = 'quotes'
 
141
    __table_args__ = {'useexisting': True }
 
142
    indx = Column('indx', Integer, Sequence('quotes_indx_seq', optional=True), primary_key=True)
 
143
    quote = Column('quote', Text, nullable=False)
 
144
    userhost = Column('userhost', String(255), ForeignKey('userhosts.userhost'), nullable=False)
 
145
    createtime = Column('createtime', DateTime, nullable=False)
 
146
    nick = Column('nick', String(255), nullable=False)
 
147
 
 
148
    def __init__(self, quote, userhost, createtime, nick):
 
149
        self.quote = quote
 
150
        self.userhost = userhost
 
151
        self.createtime = createtime
 
152
        self.nick = nick
 
153
 
 
154
## Infoitems Model
 
155
 
 
156
class InfoItems(Base):
 
157
    __tablename__ = 'infoitems'
 
158
    __table_args__ = {'useexisting': True}
 
159
    indx = Column('indx', Integer, Sequence('infoitems_indx_seq', optional=True), primary_key=True)
 
160
    item = Column('item', String(255), nullable=False)
 
161
    description = Column('description', Text, nullable=False)
 
162
    userhost = Column('userhost', String(255), ForeignKey('userhosts.userhost'), nullable=False)
 
163
    time = Column('time', DateTime, nullable=False)
 
164
 
 
165
    def __init__(self, item, description, userhost, ttime=None):
 
166
        self.time = ttime and ttime or datetime.now()
 
167
        self.item = item.lower()
 
168
        self.description = description
 
169
        self.userhost = userhost
 
170
 
 
171
 
 
172
## Karma Model
 
173
 
 
174
class Karma(Base):
 
175
    __tablename__ = 'karma'
 
176
    __table_args__ = {'useexisting': True}
 
177
    item = Column('item', String(255), primary_key=True)
 
178
    value = Column('value', Integer, nullable=False)
 
179
 
 
180
    def __init__(self, item, value):  
 
181
        self.item = item
 
182
        self.value = value
 
183
 
 
184
class WhyKarma(Base):
 
185
    __tablename__ = 'whykarma'
 
186
    __table_args__ = {'useexisting': True}
 
187
    item = Column('item', String(255), nullable=False)
 
188
    updown = Column('updown', String(10), nullable=False)
 
189
    why = Column('why', Text, nullable=False)
 
190
    __mapper_args__ = {'primary_key':[item,updown,why]}
 
191
 
 
192
    def __init__(self, item, updown, why):
 
193
        self.item = item
 
194
        self.updown = updown
 
195
        self.why = why
 
196
 
 
197
class WhoKarma(Base):
 
198
    __tablename__ = 'whokarma'
 
199
    __table_args__ = {'useexisting': True}
 
200
    item = Column('item', String(255), nullable=False)
 
201
    nick = Column('nick', String(255), nullable=False)
 
202
    updown = Column('updown', String(10), nullable=False)
 
203
    __mapper_args__ = {'primary_key': [item, nick, updown]}
 
204
 
 
205
    def __init__(self, item, nick, updown):
 
206
        self.item = item
 
207
        self.nick = nick
 
208
        self.updown = updown
 
209
 
 
210
 
 
211
## Todo Model
 
212
 
 
213
if True:
 
214
 
 
215
    class Todo(Base):
 
216
        __tablename__ = 'todo'
 
217
        __table_args__ = {'useexisting': True}
 
218
        indx = Column('indx', Integer, Sequence('todo_indx_seq', optional=True), primary_key=True)
 
219
        name = Column('name', String(255), nullable=False)
 
220
        time = Column('time', DateTime)
 
221
        duration = Column('duration', Integer)
 
222
        warnsec = Column('warnsec', Integer)
 
223
        descr = Column('descr', Text, nullable=False)
 
224
        priority = Column('priority', Integer)
 
225
 
 
226
        def __init__(self, name, time, duration, warnsec, descr, priority):
 
227
            self.name = name
 
228
            self.time = time
 
229
            self.duration = duration
 
230
            self.warnsec = warnsec
 
231
            self.descr = descr
 
232
            self.priority = priority
 
233
 
 
234
## Lists Model
 
235
 
 
236
class Lists(Base):
 
237
    __tablename__ = 'list'
 
238
    __table_args__ = {'useexisting': True}
 
239
    indx = Column('indx', Integer, Sequence('list_indx_seq', optional=True), primary_key=True)
 
240
    username = Column('username', String(255), nullable=False)
 
241
    listname = Column('listname', String(255), nullable=False)
 
242
    item = Column('item', Text, nullable=False)
 
243
 
 
244
    def __init__(self, username, listname, item):
 
245
        self.username = username
 
246
        self.listname = listname
 
247
        self.item = item
 
248
 
 
249
## IRCLogs Model
 
250
 
 
251
class ChatLog(Base):
 
252
    __tablename__ = 'chatlog'
 
253
    __table_args__ = {'useexisting': True}
 
254
    id = Column('id', Integer, primary_key=True)
 
255
    time = Column('time', DateTime, nullable=False, default=datetime.now)
 
256
    network = Column('network', String(256), nullable=False, default='') 
 
257
    target = Column('target', String(256), nullable=False, default='')   
 
258
    nick = Column('nick', String(256), nullable=False, default='')
 
259
    type = Column('type', String(256), nullable=False, default='')
 
260
    message = Column('message', Text, nullable=False, default='') 
 
261
 
 
262
    def __init__(self, **kwargs):
 
263
        for k, v in kwargs.items():
 
264
            setattr(self, k, v)