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 accesskey = models.CharField(null = True, max_length = 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 full_tooltip(self):
73 return u"%s %s" % (self.tooltip, "[Alt+%s]" % self.accesskey if self.accesskey else "")
78 'tooltip': self.tooltip,
79 'accesskey': self.accesskey,
80 'params': self.params,
81 'scriptlet_id': self.scriptlet_id
84 def __unicode__(self):
87 class Scriptlet(models.Model):
88 name = models.CharField(max_length = 64, primary_key = True)
89 code = models.TextField()
91 def __unicode__(self):
92 return _(u'javascript') + u':' + self.name