forked from YenTheFirst/card_scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
65 lines (48 loc) · 1.95 KB
/
models.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
from elixir import metadata, Entity, Field, using_options
from elixir import Integer, UnicodeText, BLOB, Enum, DateTime
from elixir import ManyToOne, OneToMany, OneToOne
metadata.bind = "sqlite:///inventory.sqlite3"
class InvCard(Entity):
name = Field(UnicodeText, index=True)
set_name = Field(UnicodeText)
box = Field(UnicodeText)
scan_png = Field(BLOB)
box_index = Field(Integer)
recognition_status = Field(Enum('scanned','candidate_match','incorrect_match','verified'))
inventory_status = Field(Enum('present', 'temporarily_out', 'permanently_gone'))
inv_logs = OneToMany('InvLog')
fix_log = OneToOne('FixLog')
rowid = Field(Integer, primary_key=True)
using_options(tablename='inv_cards')
def most_recent_log(self):
return sorted(self.inv_logs, key = lambda x: x.date)[-1]
def __unicode__(self):
return "<%s/%s (%s/%s)>" % (self.set_name, self.name, self.box, self.box_index)
def __str__(self):
return unicode(self).encode(sys.stdout.encoding)
class InvLog(Entity):
card = ManyToOne('InvCard')
direction = Field(Enum('added', 'removed'))
reason = Field(UnicodeText)
date = Field(DateTime)
#I wish I had a ActiveRecord-like 'timestamps' here
rowid = Field(Integer, primary_key = True)
using_options(tablename='inv_logs')
def __repr__(self):
if self.direction == u'added':
dir_text = 'added to'
else:
dir_text = 'removed_from'
card_repr = "%s/%s(%d)" % (self.card.set_name, self.card.name, self.card.rowid)
return "<%s: %s %s %s. \"%s\">" % (self.date, card_repr, dir_text, self.card.box, self.reason)
class FixLog(Entity):
card = ManyToOne('InvCard')
orig_set = Field(UnicodeText)
orig_name = Field(UnicodeText)
new_set = Field(UnicodeText)
new_name = Field(UnicodeText)
scan_png = Field(BLOB)
rowid = Field(Integer, primary_key = True)
using_options(tablename='fix_log')
def __repr__(self):
return "<card %d was corrected from %s/%s to %s/%s>" % (self.card.rowid, self.orig_set, self.orig_name, self.new_set, self.new_name)