1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.db import models
7 from django.utils.translation import ugettext_lazy as _
10 class ButtonGroup(models.Model):
11 name = models.CharField(max_length = 32)
12 slug = models.SlugField()
13 position = models.IntegerField(default = 0)
16 ordering = ('position', 'name',)
17 verbose_name, verbose_name_plural = _('button group'), _('button groups')
19 def __unicode__(self):
22 def to_dict(self, with_buttons = False):
23 d = {'name': self.name, 'position': self.position}
26 d['buttons'] = [ b.to_dict() for b in self.button_set.all() ]
30 #class ButtonGroupManager(models.Manager):
32 # def with_buttons(self):
33 # from django.db import connection
34 # cursor = connection.cursor()
36 # SELECT g.name, g.slug, CONCAT(b.slug),
37 # FROM toolbar_buttongroup as g LEFT JOIN toolbar_button as b
39 # WHERE p.id = r.poll_id
43 # for row in cursor.fetchall():
44 # p = self.model(id=row[0], question=row[1], poll_date=row[2])
45 # p.num_responses = row[3]
46 # result_list.append(p)
49 class Button(models.Model):
50 label = models.CharField(max_length = 32)
51 slug = models.SlugField(unique = True) #unused
54 params = models.TextField(default = '[]') # TODO: should be a JSON field
55 scriptlet = models.ForeignKey('Scriptlet', null = True, blank = True)
56 link = models.CharField(max_length = 256, blank = True, default = '')
59 key = models.CharField(blank = True, max_length = 1)
60 key_mod = models.PositiveIntegerField(blank = True, default = 1)
61 tooltip = models.CharField(blank = True, max_length = 120)
63 # Why the button is restricted to have the same position in each group ?
64 # position = models.IntegerField(default=0)
65 group = models.ManyToManyField(ButtonGroup)
69 verbose_name, verbose_name_plural = _('button'), _('buttons')
72 def hotkey_code(self):
73 return ord(self.key.upper()) | (self.key_mod << 8)
76 def hotkey_name(self):
81 if self.key_mod & 0x01: mods.append('Alt')
82 if self.key_mod & 0x02: mods.append('Ctrl')
83 if self.key_mod & 0x04: mods.append('Shift')
84 mods.append(str(self.key))
85 return '[' + '+'.join(mods) + ']'
88 def full_tooltip(self):
89 return self.tooltip + (' ' + self.hotkey_name if self.key else '')
94 'tooltip': (self.tooltip or '') + self.hotkey_name(),
96 'key_mod': self.key_mod,
97 'params': self.params,
98 'scriptlet_id': self.scriptlet_id
101 def __unicode__(self):
104 class Scriptlet(models.Model):
105 name = models.CharField(max_length = 64, primary_key = True)
106 code = models.TextField()
108 # TODO: add this later and remap code property to this
109 # code_min = models.TextField()
111 def __unicode__(self):
112 return _(u'javascript') + u':' + self.name