This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
tabellio.documents/tabellio/documents/common.py

65 lines
2.2 KiB
Python

import random
from Products.CMFCore.utils import getToolByName
import tabellio.config.utils
class BasePublication(object):
@property
def date_str(self):
return self.date.strftime('%d/%m/%Y')
def get_authors_as_string(self):
if not self.authors:
return ''
return ', '.join([x.to_object.title for x in self.authors])
@property
def authors_html_str(self):
if not self.authors:
return ''
return ', '.join([u'<a href="%s">%s</a>' % (x.to_object.absolute_url(), x.to_object.title)
for x in self.authors])
@property
def topic_titles(self):
from plone.i18n.normalizer.fr import normalizer
topic_dict = tabellio.config.utils.get_topics_dict()
def cmp_topic(x, y):
return cmp(normalizer.normalize(x).lower(),
normalizer.normalize(y).lower())
return sorted([topic_dict.get(x, [x])[0] for x in self.topics], cmp_topic)
@property
def polgroups_str(self):
if not self.polgroups:
return ''
return ', '.join([x.to_object.title for x in self.polgroups])
def related_elements(self, max_items=5, closeness=0.5):
if not self.topics:
return []
topics = self.topics[:]
# try to get other elements sharing the same topics
catalog = getToolByName(self, 'portal_catalog')
previous_result = None
r = []
while len(topics) > len(self.topics)*closeness:
brains = catalog(portal_type=self.portal_type,
topics={'query': topics, 'operator': 'and'})
if len(brains) > max_items:
brains = list(brains)
random.shuffle(brains)
if previous_result:
brains = list(previous_result) + brains
r = brains[:max_items]
break
elif len(brains) > max_items/2:
r = brains
break
previous_result = brains
del topics[random.randint(0, len(topics)-1)]
doc_ids = [self.id]
return [x for x in r if ((type(x.getId) is str) and x.getId or x.getId()) not in doc_ids]