1 from django.db import models
2 from django.db.models import signals
3 from django.dispatch import dispatcher
5 # Create your models here.
7 INTERVAL_HELP_TEXT = 'examples: "1 hour", "14 days", "3 months", "DD:HH:MM:SS.ms"'
8 PG_SCHEMAS = "actor, permission, public, config"
11 # ---------------------------------------------------------------------
12 # Here we run some SQL to manually set the postgres schema search-paths
13 # ---------------------------------------------------------------------
15 from django.db import connection
16 cursor = connection.cursor()
17 print "SET search_path TO %s" % PG_SCHEMAS
18 cursor.execute("SET search_path TO %s" % PG_SCHEMAS)
19 dispatcher.connect(setSearchPath, signal=signals.class_prepared)
20 dispatcher.connect(setSearchPath, signal=signals.pre_init)
23 class GrpTree(models.Model):
24 name = models.CharField(maxlength=100)
25 parent_id = models.ForeignKey('self', null=True, related_name='children', db_column='parent')
26 description = models.CharField(blank=True, maxlength=200)
27 perm_interval = models.CharField(blank=True, maxlength=100, help_text=INTERVAL_HELP_TEXT)
28 application_perm = models.CharField(blank=True, maxlength=100)
29 usergroup = models.BooleanField()
31 list_display = ('name', 'description')
32 list_filter = ['parent_id']
33 search_fields = ['name', 'description']
37 verbose_name = 'User Group'
41 class OrgUnitType(models.Model):
42 name = models.CharField(maxlength=100)
43 opac_label = models.CharField(maxlength=100)
44 depth = models.IntegerField()
45 parent_id = models.ForeignKey('self', null=True, related_name='children', db_column='parent')
46 can_have_vols = models.BooleanField()
47 can_have_users = models.BooleanField()
49 db_table = 'org_unit_type'
50 verbose_name = 'Library Type'
52 list_display = ('name', 'depth')
53 list_filter = ['parent_id']
58 class PermList(models.Model):
59 code = models.CharField(maxlength=100)
60 description = models.CharField(blank=True, maxlength=200)
62 list_display = ('code','description')
63 search_fields = ['code']
65 db_table = 'perm_list'
67 verbose_name = 'Permission'
71 class GrpPermMap(models.Model):
72 grp_id = models.ForeignKey(GrpTree, db_column='grp')
73 perm_id = models.ForeignKey(PermList, db_column='perm')
74 depth_id = models.ForeignKey(OrgUnitType, to_field='depth', db_column='depth')
75 grantable = models.BooleanField()
77 list_filter = ['grp_id']
78 list_display = ('perm_id', 'grp_id', 'depth_id')
80 db_table = 'grp_perm_map'
81 ordering = ['perm_id', 'grp_id']
82 verbose_name = 'Permission Setting'
84 return str(self.grp_id)+' -> '+str(self.perm_id)
88 """ There's no way to do user-based mangling given the size of the data without custom handling.
90 class User(models.Model):
91 card_id = models.ForeignKey('Card', db_column='card')
92 profile_id = models.ForeignKey(GrpTree, db_column='profile')
93 usrname = models.CharField(blank=False, null=False, maxlength=200)
95 return "%s (%s)" % ( str(self.card_id), str(self.usrname))
100 class UsrPermMap(models.Model):
101 usr_id = models.ForeignKey(User, db_column='usr')
102 perm_id = models.ForeignKey(PermList, db_column='perm')
103 depth_id = models.ForeignKey(OrgUnitType, to_field='depth', db_column='depth')
104 grantable = models.BooleanField()
106 search_fields = ['usr_id', 'perm_id'] # we need text fields to search...
108 db_table = 'usr_perm_map'
109 verbose_name = 'User Permission'
111 return "%s -> %s" % ( str(self.usr_id), str(self.perm_id) )
114 class Card(models.Model):
115 usr_id = models.ForeignKey(User, db_column='usr')
116 barcode = models.CharField(blank=False, null=False, maxlength=200)
117 active = models.BooleanField()
122 verbose_name = 'Card'
128 class OrgAddress(models.Model):
129 valid = models.BooleanField()
130 org_unit_id = models.ForeignKey('OrgUnit', db_column='org_unit')
131 address_type = models.CharField(blank=False, maxlength=200, default='MAILING')
132 street1 = models.CharField(blank=False, maxlength=200)
133 street2 = models.CharField(maxlength=200)
134 city = models.CharField(blank=False, maxlength=200)
135 county = models.CharField(maxlength=200)
136 state = models.CharField(blank=False, maxlength=200)
137 country = models.CharField(blank=False, maxlength=200)
138 post_code = models.CharField(blank=False, maxlength=200)
140 search_fields = ['street1', 'city', 'post_code']
141 list_filter = ['org_unit_id']
142 list_display = ('street1', 'street2', 'city', 'county', 'state', 'post_code')
145 db_table = 'org_address'
146 verbose_name = 'Library Address'
148 return self.street1+' '+self.city+', '+self.state+' '+self.post_code
150 class OrgUnit(models.Model):
151 parent_ou_id = models.ForeignKey('self', null=True, related_name='children', db_column='parent_ou')
152 ou_type_id = models.ForeignKey(OrgUnitType, db_column='ou_type')
153 shortname = models.CharField(maxlength=200)
154 name = models.CharField(maxlength=200)
155 email = models.EmailField(null=True, blank=True)
156 phone = models.CharField(maxlength=200, null=True, blank=True)
157 ill_address_id = models.ForeignKey(OrgAddress, db_column='ill_address', null=True, blank=True)
158 holds_address_id = models.ForeignKey(OrgAddress, db_column='holds_address', null=True, blank=True)
159 mailing_address_id = models.ForeignKey(OrgAddress, db_column='mailing_address', null=True, blank=True)
160 billing_address_id = models.ForeignKey(OrgAddress, db_column='billing_address', null=True, blank=True)
162 search_fields = ['name', 'shortname']
163 #list_filter = ['parent_ou_id'] # works, but shows all libs as options, so ruins the point
164 list_display = ('shortname', 'name')
166 db_table = 'org_unit'
167 ordering = ['shortname']
168 verbose_name = 'Library'
170 return self.shortname
173 class RuleCircDuration(models.Model):
174 name = models.CharField(maxlength=200)
175 extended = models.CharField(maxlength=200, help_text=INTERVAL_HELP_TEXT);
176 normal = models.CharField(maxlength=200, help_text=INTERVAL_HELP_TEXT);
177 shrt = models.CharField(maxlength=200, help_text=INTERVAL_HELP_TEXT);
178 max_renewals = models.IntegerField()
180 search_fields = ['name']
181 list_display = ('name','extended','normal','shrt','max_renewals')
183 db_table = 'rule_circ_duration'
185 verbose_name = 'Circ Duration Rule'
190 class RuleMaxFine(models.Model):
191 name = models.CharField(maxlength=200)
192 amount = models.FloatField(max_digits=6, decimal_places=2)
194 search_fields = ['name']
195 list_display = ('name','amount')
197 db_table = 'rule_max_fine'
199 verbose_name = 'Circ Max Fine Rule'
203 class RuleRecurringFine(models.Model):
204 name = models.CharField(maxlength=200)
205 high = models.FloatField(max_digits=6, decimal_places=2)
206 normal = models.FloatField(max_digits=6, decimal_places=2)
207 low = models.FloatField(max_digits=6, decimal_places=2)
209 search_fields = ['name']
210 list_display = ('name','high', 'normal', 'low')
212 db_table = 'rule_recuring_fine'
214 verbose_name = 'Circ Recurring Fine Rule'
218 class IdentificationType(models.Model):
219 name = models.CharField(maxlength=200)
221 search_fields = ['name']
223 db_table = 'identification_type'
225 verbose_name = 'Identification Type'
230 class RuleAgeHoldProtect(models.Model):
231 name = models.CharField(maxlength=200)
232 age = models.CharField(blank=True, maxlength=100, help_text=INTERVAL_HELP_TEXT)
233 prox = models.IntegerField()
235 search_fields = ['name']
237 db_table = 'rule_age_hold_protect'
239 verbose_name = 'Hold Age Protection Rule'