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')
67 def hotkey_code(self):
68 return ord(self.key.upper()) | (self.key_mod << 8)
71 def hotkey_name(self):
76 if self.key_mod & 0x01: mods.append('Alt')
77 if self.key_mod & 0x02: mods.append('Ctrl')
78 if self.key_mod & 0x04: mods.append('Shift')
79 mods.append(str(self.key))
80 return '[' + '+'.join(mods) + ']'
83 def full_tooltip(self):
84 return self.tooltip + (' ' + self.hotkey_name if self.key else '')
89 'tooltip': (self.tooltip or '') + self.hotkey_name(),
91 'key_mod': self.key_mod,
92 'params': self.params,
93 'scriptlet_id': self.scriptlet_id
96 def __unicode__(self):
99 class Scriptlet(models.Model):
100 name = models.CharField(max_length = 64, primary_key = True)
101 code = models.TextField()
103 # TODO: add this later and remap code property to this
104 # code_min = models.TextField()
106 def __unicode__(self):
107 return _(u'javascript') + u':' + self.name