2016-05-27 15:42:29 +01:00
|
|
|
import iso8601
|
|
|
|
|
|
|
|
|
2017-01-19 18:25:22 +00:00
|
|
|
def get_attribute(cls, attr, default=None):
|
|
|
|
return getattr(cls, attr, default)
|
|
|
|
|
|
|
|
|
2016-05-27 15:42:29 +01:00
|
|
|
def format_datetime(value):
|
2016-06-03 22:12:34 +01:00
|
|
|
return iso8601.parse_date(str(value)).strftime("%x")
|
2016-05-27 23:19:03 +01:00
|
|
|
|
|
|
|
|
2016-05-28 21:21:17 +01:00
|
|
|
def category_find(categories, name):
|
|
|
|
for category_name, articles in categories:
|
|
|
|
if category_name == name:
|
|
|
|
return articles
|
|
|
|
return []
|
2016-06-03 22:12:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def limit(line, length):
|
2016-07-04 23:17:15 +01:00
|
|
|
if isinstance(line, str):
|
|
|
|
if len(line) <= length:
|
|
|
|
return line
|
|
|
|
return " ".join(line.split(" ")[:length]) + '...'
|
|
|
|
elif isinstance(line, list):
|
|
|
|
return line[:length]
|
2017-01-19 18:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_title(instance):
|
2017-01-19 21:40:01 +00:00
|
|
|
return (
|
|
|
|
get_attribute(instance, 'title') or (hasattr(instance, 'page') and get_attribute(instance.page, 'name')) or get_attribute(instance, 'name') or ''
|
2017-01-19 22:21:55 +00:00
|
|
|
)
|
2017-01-19 18:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_html_title(instance):
|
2017-01-19 21:40:01 +00:00
|
|
|
return (
|
|
|
|
get_attribute(instance, 'html_title') or get_title(instance)
|
2017-01-19 22:21:55 +00:00
|
|
|
)
|
2017-01-19 18:30:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_image(instance):
|
|
|
|
return get_attribute(instance, 'image') or (hasattr(instance, 'page') and get_attribute(instance.page, 'name')) or ''
|
2017-02-09 21:52:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def encode_text(text):
|
|
|
|
return " ".join([str(ord(c)) for c in text])
|