205 lines
6.5 KiB
Python
205 lines
6.5 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2010 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from django.utils.encoding import force_text
|
|
from quixote import get_publisher
|
|
|
|
from .qommon import _, misc
|
|
from .qommon.storage import StorableObject
|
|
|
|
|
|
class Role(StorableObject):
|
|
_names = 'roles'
|
|
_indexes = ['uuid', 'slug']
|
|
xml_root_node = 'role'
|
|
|
|
name = None
|
|
uuid = None
|
|
slug = None
|
|
internal = False
|
|
details = None
|
|
emails = None
|
|
emails_to_members = False
|
|
allows_backoffice_access = True
|
|
|
|
TEXT_ATTRIBUTES = ['name', 'uuid', 'slug', 'details', 'emails']
|
|
BOOLEAN_ATTRIBUTES = ['internal', 'emails_to_members', 'allows_backoffice_access']
|
|
|
|
def __init__(self, name=None, id=None):
|
|
StorableObject.__init__(self, id=id)
|
|
self.name = name
|
|
|
|
def __eq__(self, other):
|
|
return bool(self.__class__ is other.__class__ and self.id == other.id)
|
|
|
|
def migrate(self):
|
|
changed = False
|
|
if not self.slug:
|
|
# .store() will take care of setting the slug
|
|
changed = True
|
|
if changed:
|
|
self.store()
|
|
|
|
def store(self):
|
|
if self.slug is None:
|
|
# set slug if it's not yet there
|
|
self.slug = self.get_new_slug()
|
|
super().store()
|
|
|
|
def get_new_slug(self):
|
|
new_slug = misc.simplify(self.name)
|
|
base_new_slug = new_slug
|
|
suffix_no = 0
|
|
while True:
|
|
try:
|
|
role = self.get_on_index(new_slug, 'slug', ignore_migration=True)
|
|
except KeyError:
|
|
break
|
|
if role.id == self.id:
|
|
break
|
|
suffix_no += 1
|
|
new_slug = '%s-%s' % (base_new_slug, suffix_no)
|
|
return new_slug
|
|
|
|
def get_emails(self):
|
|
emails = self.emails or []
|
|
if not self.emails_to_members:
|
|
return emails
|
|
users_with_roles = get_publisher().user_class.get_users_with_role(self.id)
|
|
emails.extend([x.email for x in users_with_roles if x.email and x.is_active])
|
|
return emails
|
|
|
|
def is_internal(self):
|
|
return self.internal
|
|
|
|
def get_substitution_variables(self, prefix=''):
|
|
data = {}
|
|
data[prefix + 'name'] = self.name
|
|
data[prefix + 'details'] = self.details or ''
|
|
data[prefix + 'emails'] = ', '.join(self.emails or [])
|
|
return data
|
|
|
|
def get_json_export_dict(self):
|
|
charset = get_publisher().site_charset
|
|
return {
|
|
'name': force_text(self.name, charset),
|
|
'text': force_text(self.name, charset), # generic key
|
|
'allows_backoffice_access': self.allows_backoffice_access,
|
|
'emails': [force_text(email, charset) for email in self.emails or []],
|
|
'details': force_text(self.details or '', charset),
|
|
'emails_to_members': self.emails_to_members,
|
|
'slug': force_text(self.slug, charset),
|
|
'id': self.id,
|
|
}
|
|
|
|
def export_to_xml(self, include_id=False):
|
|
charset = get_publisher().site_charset
|
|
root = ET.Element(self.xml_root_node)
|
|
if include_id and self.id:
|
|
root.attrib['id'] = str(self.id)
|
|
for text_attribute in list(self.TEXT_ATTRIBUTES):
|
|
if not hasattr(self, text_attribute) or not getattr(self, text_attribute):
|
|
continue
|
|
ET.SubElement(root, text_attribute).text = force_text(getattr(self, text_attribute), charset)
|
|
for boolean_attribute in self.BOOLEAN_ATTRIBUTES:
|
|
if not hasattr(self, boolean_attribute):
|
|
continue
|
|
value = getattr(self, boolean_attribute)
|
|
if value:
|
|
value = 'true'
|
|
else:
|
|
value = 'false'
|
|
ET.SubElement(root, boolean_attribute).text = value
|
|
return root
|
|
|
|
@classmethod
|
|
def import_from_xml(cls, fd, charset=None, include_id=False):
|
|
try:
|
|
tree = ET.parse(fd)
|
|
except Exception:
|
|
raise ValueError()
|
|
|
|
if charset is None:
|
|
charset = get_publisher().site_charset
|
|
assert charset == 'utf-8'
|
|
role = cls()
|
|
|
|
# if the tree we get is actually a ElementTree for real, we get its
|
|
# root element and go on happily.
|
|
if not ET.iselement(tree):
|
|
tree = tree.getroot()
|
|
|
|
if include_id and tree.attrib.get('id'):
|
|
role.id = tree.attrib.get('id')
|
|
for text_attribute in list(cls.TEXT_ATTRIBUTES):
|
|
value = tree.find(text_attribute)
|
|
if value is None or value.text is None:
|
|
continue
|
|
setattr(role, text_attribute, misc.xml_node_text(value))
|
|
|
|
for boolean_attribute in cls.BOOLEAN_ATTRIBUTES:
|
|
value = tree.find(boolean_attribute)
|
|
if value is None:
|
|
continue
|
|
setattr(role, boolean_attribute, value.text == 'true')
|
|
|
|
return role
|
|
|
|
@classmethod
|
|
def resolve(cls, uuid, slug=None, name=None):
|
|
try:
|
|
return cls.get_on_index(uuid, 'uuid')
|
|
except KeyError:
|
|
pass
|
|
try:
|
|
return cls.get(uuid)
|
|
except KeyError:
|
|
pass
|
|
try:
|
|
return cls.get_on_index(uuid, 'slug')
|
|
except KeyError:
|
|
pass
|
|
if slug:
|
|
try:
|
|
return cls.get_on_index(slug, 'slug')
|
|
except KeyError:
|
|
pass
|
|
if name:
|
|
for role in cls.select():
|
|
if role.name == name:
|
|
return role
|
|
return None
|
|
|
|
|
|
def logged_users_role():
|
|
volatile_role = Role.volatile()
|
|
volatile_role.id = 'logged-users'
|
|
volatile_role.name = _('Logged Users')
|
|
return volatile_role
|
|
|
|
|
|
def get_user_roles():
|
|
t = sorted(
|
|
[
|
|
(misc.simplify(x.name), x.id, x.name, x.id)
|
|
for x in get_publisher().role_class.select()
|
|
if not x.is_internal()
|
|
]
|
|
)
|
|
return [x[1:] for x in t]
|