-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorm.py
397 lines (301 loc) · 14.1 KB
/
orm.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import datetime, os, hashlib
from PIL import Image
from sqlalchemy import and_
from sqlalchemy.orm import mapper, relation, backref
import tables, strorder, cfg, passwd
class Instance(object):
def __init__(self, name):
self.name = name
def insert_member(self, pos, member):
strorder.insert(self, pos, member)
def append_member(self, member):
strorder.append(self, member)
def insert_tag(self, pos, tag):
strorder.insert(self, pos, tag)
def append_tag(self, tag):
strorder.append(self, tag)
def insert_circular(self, pos, circular):
strorder.insert(self, pos, circular)
def append_circular(self, circular):
strorder.append(self, circular)
def insert_entrance(self, pos, entrance):
strorder.insert(self, pos, entrance)
def append_entrance(self, entrance):
strorder.append(self, entrance)
class Member(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __unicode__(self):
s = "\n".join(u"%s=%s" % (key, getattr(self, str(key).split(".", 1)[1]))
for key in tables.member_table.columns
if not str(key).startswith('subscription_'))
s += "\ntags=%s" % " ".join(tag.name for tag in self.tags)
return s
@property
def salutation(self):
if self.gender == 'female':
return 'Liebe %s' % self.firstname
elif self.gender == 'male':
return 'Lieber %s' % self.firstname
return 'Liebe/r %s' % self.firstname
class Change(object):
def __init__(self, text, note, member, editor):
self.text = text
self.note = note
self.member_id = member.id
self.editor_id = editor.id
class Tag(object):
def __init__(self, name, description, visible, photopath, photographer, ticket_title, ticket_description, ticket_latexname, recording):
self.name = name
self.description = description
self.visible = visible
self.photopath = photopath
self.photographer = photographer
self.ticket_title = ticket_title
self.ticket_description = ticket_description
self.ticket_latexname = ticket_latexname
self.recording = recording
class Entrance(object):
def __init__(self, url, expire):
self.url = url
self.expire = expire
class Link(object):
def __init__(self, uuid, member, entrance):
self.uuid = uuid
self.member_id = member.id
self.entrance_id = entrance.id
class Circular(object):
def __init__(self, name, title, html, email):
self.name = name
self.title = title
self.html = html
self.email = email
def insert_attachment(self, pos, attachment):
strorder.insert(self, pos, attachment)
def append_attachment(self, attachment):
strorder.append(self, attachment)
class Message(object):
def __init__(self, member, circular, access_by=None):
self.member_id = member.id
self.circular_id = circular.id
if access_by:
self.access_by = access_by
self.access_when = datetime.datetime.now()
class Attachment(object):
def __init__(self, name, mimetype, data):
self.name = name
self.mimetype = mimetype
self.data = data
class Photo(object):
def __init__(self, name, tag, allow_labels):
self.name = name
self.tag = tag
self.tag_id = tag.id
self.hash = None
self.allow_labels = allow_labels
self.refresh()
def refresh(self):
i = Image.open(self.filename)
md5 = hashlib.md5()
f = open(self.filename)
chunk = f.read(2**16)
while chunk:
md5.update(chunk)
chunk = f.read()
f.close()
hash = unicode(md5.hexdigest())
if self.hash != hash:
self.hash = hash
self.width, self.height = i.size
self.size = os.path.getsize(self.filename)
if not os.path.exists(self.midname):
i.thumbnail((cfg.mid_size, cfg.mid_size), Image.ANTIALIAS)
i.save(self.midname, "JPEG")
self.midwidth, self.midheight = i.size
self.midsize = os.path.getsize(self.midname)
if not os.path.exists(self.thumbname):
i.thumbnail((cfg.thumb_size, cfg.thumb_size), Image.ANTIALIAS)
i.save(self.thumbname, "JPEG")
self.thumbwidth, self.thumbheight = i.size
self.thumbsize = os.path.getsize(self.thumbname)
def _filename(self):
return os.path.join(self.tag.photopath, self.name)
def _midname(self):
return os.path.join(cfg.midpath, "%s.jpg" % self.hash)
def _thumbname(self):
return os.path.join(cfg.thumbpath, "%s.jpg" % self.hash)
filename = property(_filename)
midname = property(_midname)
thumbname = property(_thumbname)
class PhotoLabel(object):
def __init__(self, photo, top, left, width, height, text):
self.photo_id = photo.id
self.top = top
self.left = left
self.width = width
self.height = height
self.text = text
class Subscription(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Recording(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Ticket(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Sold(object):
def __init__(self, **kwargs):
self.bankcode = passwd.generate()
self.pickupcode = passwd.generate()
for key, value in kwargs.items():
setattr(self, key, value)
class Coupon(object):
def __init__(self, amount, tag):
self.amount = amount
self.tag = tag
self.code = passwd.generate()
class Newsletter(object):
def __init__(self, gender, name, email, instance):
self.gender = gender
self.name = name
self.email = email
self.instance = instance
mapper(Instance, tables.instance_table,
properties={"members": relation(Member,
backref="instance",
order_by=[tables.member_table.c.instance_order]),
"tags": relation(Tag,
primaryjoin=tables.instance_table.c.id==tables.tag_table.c.instance_id,
backref="instance",
order_by=[tables.tag_table.c.instance_order]),
"onsale": relation(Tag,
primaryjoin=tables.instance_table.c.onsale_id==tables.tag_table.c.id),
"circulars": relation(Circular,
backref="instance",
order_by=[tables.circular_table.c.instance_order]),
"entrances": relation(Entrance,
backref="instance",
order_by=[tables.entrance_table.c.instance_order]),
"newsletter": relation(Newsletter,
backref="instance",
order_by=[tables.newsletter_table.c.id])})
mapper(Member, tables.member_table,
properties={"changes": relation(Change,
backref="member",
primaryjoin=tables.member_table.c.id==tables.change_table.c.member_id,
order_by=[tables.change_table.c.timestamp]),
"edits": relation(Change,
backref="editor",
primaryjoin=tables.member_table.c.id==tables.change_table.c.editor_id,
order_by=[tables.change_table.c.timestamp]),
"links": relation(Link,
backref="member",
primaryjoin=and_(tables.member_table.c.id==tables.link_table.c.member_id,
tables.entrance_table.c.id==tables.link_table.c.entrance_id),
order_by=[tables.entrance_table.c.instance_order]),
"tags": relation(Tag,
secondary=tables.member_tag_table,
backref=backref("members", order_by=tables.member_table.c.instance_order),
order_by=[tables.tag_table.c.instance_order]),
"entrances": relation(Entrance,
secondary=tables.link_table,
backref=backref("members", order_by=tables.member_table.c.instance_order),
order_by=[tables.entrance_table.c.instance_order]),
"messages": relation(Message,
backref="member",
primaryjoin=and_(tables.member_table.c.id==tables.message_table.c.member_id,
tables.circular_table.c.id==tables.message_table.c.circular_id),
order_by=[tables.circular_table.c.instance_order]),
"subscriptions": relation(Subscription,
backref="member"),
"recordings": relation(Recording,
backref="member")})
mapper(Change, tables.change_table)
mapper(Tag, tables.tag_table,
properties={"photos": relation(Photo,
backref="tag",
order_by=[tables.photo_table.c.name]),
"recordings": relation(Recording,
backref="tag"),
"tickets": relation(Ticket,
backref="tag"),
"solds": relation(Sold,
backref="tag"),
"coupons": relation(Coupon,
backref="tag")})
mapper(Entrance, tables.entrance_table,
properties={"links": relation(Link,
backref="entrance",
primaryjoin=and_(tables.member_table.c.id==tables.link_table.c.member_id,
tables.entrance_table.c.id==tables.link_table.c.entrance_id),
order_by=[tables.member_table.c.instance_order])})
mapper(Link, tables.link_table)
mapper(Circular, tables.circular_table,
properties={"messages": relation(Message,
backref="circular",
primaryjoin=and_(tables.member_table.c.id==tables.message_table.c.member_id,
tables.circular_table.c.id==tables.message_table.c.circular_id),
order_by=[tables.member_table.c.instance_order]),
"attachments": relation(Attachment,
backref="circular",
order_by=[tables.attachment_table.c.circular_order])})
mapper(Message, tables.message_table)
mapper(Attachment, tables.attachment_table)
mapper(Photo, tables.photo_table,
properties={"labels": relation(PhotoLabel,
backref="photo")})
mapper(PhotoLabel, tables.photo_label_table)
mapper(Subscription, tables.subscription_table)
mapper(Recording, tables.recording_table)
mapper(Ticket, tables.ticket_table)
mapper(Sold, tables.sold_table,
properties={"tickets": relation(Ticket,
backref="sold",
order_by=[tables.ticket_table.c.id]),
"coupons": relation(Coupon,
backref="sold",
order_by=[tables.coupon_table.c.id])})
mapper(Coupon, tables.coupon_table)
mapper(Newsletter, tables.newsletter_table)
if __name__ == "__main__":
import xml.sax
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import cfg
engine = create_engine(cfg.db)
Session = sessionmaker(bind=engine)
session = Session()
instance = Instance(cfg.instance)
session.add(instance)
class LoadHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.tags = {}
def startElement(self, name, attrs):
if name == "tag":
tag = Tag(attrs["name"], attrs["description"], attrs["visible"] == "True", None, None)
self.tags[attrs["name"]] = tag
instance.append_tag(tag)
if name == "member":
data = dict([(str(key), key != "birthday" and value or datetime.date(*map(int, value.split(".")[::-1])))
for key, value in attrs.items()
if key not in ["tags"]])
if data.has_key("passwd"):
print "creating initial user account for %(firstname)s %(lastname)s, login %(login)s, password %(passwd)s" % data
data["salt"] = unicode(os.urandom(16).encode("hex"))
data["passwd"] = unicode(hashlib.md5((u"%s%s" % (data["salt"], data["passwd"])).encode("utf-8")).hexdigest())
self.member = Member(**data)
instance.append_member(self.member)
for tag in attrs["tags"].split():
self.member.tags.append(self.tags[tag])
def characters(self, content):
if content.strip():
self.member.notes = content
xml.sax.parse(open(cfg.init), LoadHandler())
session.commit()
session.close()
engine.dispose()