/usr/share/pyshared/app_plugins/models.py is in python-django-app-plugins 0.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from library import get_library
import re
ENABLED = 0
DISABLED = 1
REMOVED = 2
STATUS_CHOICES = (
(ENABLED, _('Enabled')),
(DISABLED, _('Disabled')),
(REMOVED, _('Removed'))
)
LABEL_RE = re.compile('^[a-zA-Z_][a-zA-Z_0-9.]*$')
def is_valid_label(name):
return bool(LABEL_RE.match(name))
def construct_template_path(app, name, ext='.html'):
if not is_valid_label(name): raise RuntimeError, u"invalid label: " + name
if not is_valid_label(app): raise RuntimeError, u"invalid label: " + app
return '/'.join([app.split('.')[-1], 'plugins', name.replace('.','/')])+ext
class PluginPoint(models.Model):
label = models.CharField(max_length=255, unique=True,
help_text=_("The label for the plugin point."))
index = models.IntegerField(default=0)
registered = models.BooleanField(default=False,
help_text=_("is this a registered plugin point with a "
"library entry?"))
status = models.SmallIntegerField(choices=STATUS_CHOICES,
default=ENABLED)
class Meta:
ordering = ('index', 'id')
def __unicode__(self):
return u'plugin_point:' + self.label
@property
def app(self):
if not self.registered: return ''
return self.label.rsplit('.', 1)[0]
@property
def name(self):
return self.label.rsplit('.', 1)[-1]
def get_plugins(self, user=None):
"""get all the plugins in appropriate order"""
if self.status: return []
if user is None:
return self.plugin_set.filter(status=ENABLED).order_by('index','id')
upref = user.userpluginpreference_set.filter(plugin__status=ENABLED,
plugin__point=self) #.order_by('index', 'id').select_related()
visible = [up.plugin for up in
upref.filter(Q(visible=True)|Q(plugin__required=True))]
plugins = self.plugin_set.filter(status=ENABLED).exclude(
id__in=[x['plugin'] for x in upref.values('plugin')]
).order_by('index', 'id')
return visible + list(plugins)
def get_options(self):
if not self.registered: return {}
lib = get_library(self.app)
call = lib.get_plugin_point_call(self.name)
return call.options
# __call__ not allowed
def call(self, context, user, **args):
if not self.registered: return {}
lib = get_library(self.app)
call = lib.get_plugin_point_call(self.name)
options = call.options
base = [self,]
if options.get('takes_context', False):
base.append(context)
if options.get('takes_user', False):
base.append(user)
if options.get('takes_args', False):
return call(*base, **args)
return call(self, *base)
class Plugin(models.Model):
point = models.ForeignKey(PluginPoint)
label = models.CharField(max_length=255, unique=True,
help_text=_("The label for the plugin point."))
index = models.IntegerField(default=0)
registered = models.BooleanField(default=False,
help_text=_("is this a registered plugin?"))
status = models.SmallIntegerField(choices=STATUS_CHOICES,
default=ENABLED)
required = models.BooleanField(default=False,
help_text=_("users can not remove this plugin."))
template = models.TextField(
help_text=_("template to load for the plugin."))
class Meta:
order_with_respect_to = 'point'
ordering = ('point', 'index', 'id')
def __unicode__(self):
return u'plugin:' + self.label
@property
def app(self):
#if not self.registered: return ''
return self.label[:-(len(self.point.label)+1)]
@property
def name(self):
return self.point.label
def get_options(self):
if not self.registered: return {}
lib = get_library(self.app)
call = lib.get_plugin_call(self.name)
return call.options
#__call__ not allowed...
def call(self, context, user, **args):
if not self.registered: return {}
lib = get_library(self.app)
call = lib.get_plugin_call(self.name)
options = call.options
base = [self,]
if options.get('takes_context', False):
base.append(context)
if options.get('takes_user', False):
base.append(user)
if options.get('takes_args', False):
return call(*base, **args)
return call(self, *base)
class UserPluginPreference(models.Model):
user = models.ForeignKey(User)
plugin = models.ForeignKey(Plugin)
visible = models.BooleanField(default=True)
index = models.IntegerField(default=0)
class Meta:
unique_together = ['user', 'plugin']
order_with_respect_to = 'plugin'
ordering = ('plugin', 'user', 'index', 'id')
def __unicode__(self):
return u':'.join(['pluginpref', self.user.username, self.plugin.label])
|