1 from django.db import models
2 from django.utils.translation import ugettext_lazy as _
5 class ButtonGroup(models.Model):
6 name = models.CharField(max_length=32)
7 slug = models.SlugField()
8 position = models.IntegerField(default=0)
11 ordering = ('position', 'name',)
12 verbose_name, verbose_name_plural = _('button group'), _('button groups')
14 def __unicode__(self):
17 def to_dict(self, with_buttons=False):
18 d = {'name': self.name, 'position': self.position}
21 d['buttons'] = [ b.to_dict() for b in self.button_set.all() ]
25 #class ButtonGroupManager(models.Manager):
27 # def with_buttons(self):
28 # from django.db import connection
29 # cursor = connection.cursor()
31 # SELECT g.name, g.slug, CONCAT(b.slug),
32 # FROM toolbar_buttongroup as g LEFT JOIN toolbar_button as b
34 # WHERE p.id = r.poll_id
38 # for row in cursor.fetchall():
39 # p = self.model(id=row[0], question=row[1], poll_date=row[2])
40 # p.num_responses = row[3]
41 # result_list.append(p)
44 class Button(models.Model):
45 label = models.CharField(max_length=32)
46 slug = models.SlugField(unique=True) #unused
49 params = models.TextField(default='[]') # TODO: should be a JSON field
50 scriptlet = models.ForeignKey('Scriptlet', null=True, blank=True)
51 link = models.CharField(max_length=256, blank=True, default='')
54 key = models.CharField(blank=True, max_length=1)
55 key_mod = models.PositiveIntegerField(blank=True, default=1)
56 tooltip = models.CharField(blank=True, max_length=120)
58 # Why the button is restricted to have the same position in each group ?
59 # position = models.IntegerField(default=0)
60 group = models.ManyToManyField(ButtonGroup)
64 verbose_name, verbose_name_plural = _('button'), _('buttons')
66 def hotkey_code(self):
67 return ord(self.key.upper()) | (self.key_mod << 8)
69 def hotkey_name(self):
74 if self.key_mod & 0x01: mods.append('Alt')
75 if self.key_mod & 0x02: mods.append('Ctrl')
76 if self.key_mod & 0x04: mods.append('Shift')
77 mods.append(str(self.key))
78 return '[' + '+'.join(mods) + ']'
83 'tooltip': (self.tooltip or '') + self.hotkey_name(),
85 'key_mod': self.key_mod,
86 'params': self.params,
87 'scriptlet_id': self.scriptlet_id
90 def __unicode__(self):
93 class Scriptlet(models.Model):
94 name = models.CharField(max_length=64, primary_key=True)
95 code = models.TextField()
97 # TODO: add this later and remap code property to this
98 # code_min = models.TextField()
100 def __unicode__(self):
101 return _(u'javascript')+u':'+self.name