Commit 69f14561 authored by gijs's avatar gijs
Browse files

Restructuring of links

parent a3a1bcac
......@@ -74,16 +74,29 @@ class DateRange (object):
class Field (object):
def __init__ (self, default = []):
self.default = default
self.value = None
# no-op
def parse (self, value):
return value
def __call__ (self, value):
if value:
return [self.parse(v) for v in value]
else:
return self.default
def set (self, value):
self.value = [self.parse(v) for v in value]
def __repr__ (self):
return repr(self.value)
def __str__ (self):
return str(self.value)
def __iter__ (self):
return iter(self.value)
# def __call__ (self, value):
# if value:
# return [self.parse(v) for v in value]
# else:
# return self.default
"""
Wrapper for a field object to turn it into a single field
......@@ -92,14 +105,22 @@ class Single(object):
def __init__ (self, field):
self.field = field
def __call__ (self, value):
result = self.field(value)
def set (self, value):
self.field.set(value)
if len(result) > 0:
return result[0]
@property
def value (self):
if self.field.value:
return self.field.value[0]
else:
return None
def __repr__ (self):
return repr(self.value)
def __str__ (self):
return str(self.value)
class DateField (Field):
def isRange (self, value):
......@@ -145,6 +166,7 @@ class DateField (Field):
return None
class DateTimeField (Field):
def parse (self, value):
try:
......
from generator.models import collectionFor, knownContentTypes, is_link, Model
from generator.models import collectionFor, knownContentTypes, \
is_link, is_multi_link, is_reverse_link, is_reverse_multi_link, \
Model
from generator.utils import try_attributes
def display_link (direction, label, link=None):
arrow = '→' if direction == 'out' else '←'
if link:
return '<dd>{direction} {arrow} <a href="https://ethertoff.caveat.be/w/{link}">{label}</a></dd>'.format(
direction=direction,
arrow=arrow,
label=label,
link=link.replace('#', '%23')
)
LINK_DIRECTION_OUT = 'out'
LINK_DIRECTION_IN = 'in'
def display_field (field):
return '<dd class="attribute">{label}</dd>'.format(label=str(field))
def display_empty ():
return '<dd class="attribute empty-attribute">not set</dd>'.format()
# def display_link (direction = LINK_DIRECTION_OUT, label, url=None):
# The source is already listed, so we care about the target
def display_link (link):
if not link:
return display_empty()
if link.reverse:
direction = LINK_DIRECTION_IN
else:
return '<dd>{direction} {arrow} {label}</dd>'.format(
direction=direction,
arrow=arrow,
label=label,
)
direction = LINK_DIRECTION_OUT
arrow = '→' if direction == LINK_DIRECTION_OUT else '←'
# Label of target?
# If there is a sourcepath include it as well
# Mark whether it is an inline link
if link.broken:
return '<dd class="link link-broken">{direction} {arrow} {label} [broken, unable to resolve]</dd>'.format(
direction=direction,
arrow=arrow,
label=link.target
)
elif not link.resolved:
return '<dd class="link link-unresolved">{direction} {arrow} {label} [unresolved]</dd>'.format(
direction=direction,
arrow=arrow,
label=link.target
)
else:
padname = link.target.source_path
if padname:
return '<dd>{direction} {arrow} <a href="https://ethertoff.caveat.be/w/{padname}">{label}</a></dd>'.format(
direction=direction,
arrow=arrow,
label=str(link.target),
padname=padname.replace('#', '%23')
)
else:
return '<dd>{direction} {arrow} {label}</dd>'.format(
direction=direction,
arrow=arrow,
label=str(link.target),
)
def make_index (models):
buff = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><ul>'
for contentType in knownContentTypes:
for contentType in knownContentTypes():
collection = collectionFor(contentType)
for obj in collection.models:
if obj.source_path:
......@@ -34,33 +75,46 @@ def make_index (models):
type=obj.contentType
)
for attr in dir(obj):
# Attributes noted in the metafields list
# can be an outgoing links
if attr in obj.metadataFields \
and is_link(obj.metadataFields[attr]):
val = getattr(obj, attr)
buff += '<dt>{}</dt>'.format(attr)
# Attributes noted in the metafields list, plus content,
# the link property and the sourcepath
buff += '<dt>{attr}</dt>'.format(attr=attr)
if hasattr(obj, attr):
field = getattr(obj, attr)
if is_multi_link(field) or is_reverse_multi_link(field):
for link in field.value:
buff += display_link(link)
elif is_link(field) or is_reverse_link(field):
buff += display_link(field.value)
else:
buff += display_field(field)
else:
buff += display_empty()
# if attr in obj.metadata \
# and is_link(obj.metadata[attr]):
# val = getattr(obj, attr)
# buff += '<dt>{}</dt>'.format(attr)
# print('Outgoing link(s)', attr)
if type(val) is list:
for entry in val:
if isinstance(entry, Model):
buff += display_link('out', try_attributes(entry, [entry.labelField, 'pk']), entry.source_path)
elif isinstance(val, Model):
buff += display_link('out', try_attributes(val, [val.labelField, 'pk']), val.source_path)
# # print('Outgoing link(s)', attr)
# if type(val) is list:
# for entry in val:
# if isinstance(entry, Model):
# buff += display_link('out', try_attributes(entry, [entry.labelField, 'pk']), entry.source_path)
# elif isinstance(val, Model):
# buff += display_link('out', try_attributes(val, [val.labelField, 'pk']), val.source_path)
# As the attribute is not in the metadataFields
# it almost certainly is an incoming link
elif not attr in obj.metadataFields and attr not in ['pk', 'content', 'source_path']:
val = getattr(obj, attr)
buff += '<dt>{}</dt>'.format(attr)
if type(val) is list:
for entry in val:
if isinstance(entry, Model):
buff += display_link('in', try_attributes(entry, [entry.labelField, 'pk']), entry.source_path)
elif isinstance(val, Model):
buff += display_link('in', try_attributes(val, [val.labelField, 'pk']), val.source_path)
# # As the attribute is not in the metadataFields
# # it almost certainly is an incoming link
# elif not attr in obj.metadata and attr not in ['pk', 'content', 'source_path']:
# val = getattr(obj, attr)
# buff += '<dt>{}</dt>'.format(attr)
# if type(val) is list:
# for entry in val:
# if isinstance(entry, Model):
# buff += display_link('in', try_attributes(entry, [entry.labelField, 'pk']), entry.source_path)
# elif isinstance(val, Model):
# buff += display_link('in', try_attributes(val, [val.labelField, 'pk']), val.source_path)
buff += '</li>'
buff += '</ul><style>li { margin-top: 1em; }</style></body></html>'
return buff
......
......@@ -8,7 +8,7 @@ from math import inf
from generator.index import make_index
from generator.parse import parse_pads
from generator.models import collectionFor, initContentTypes
from generator.models import collectionFor, resetCollections, contentTypes
from generator.utils import info, regroup, try_attributes, render_to_string
from django.core.management.base import BaseCommand
......@@ -16,6 +16,8 @@ from django.core.management import call_command
from django.conf import settings
from generator.templatetags.generator_utils import link_iterator, link_target_iterator
FIELD_SINGLE = 'FIELD_SINGLE'
FIELD_ITERABLE = 'FIELD_ITERABLE'
......@@ -72,13 +74,15 @@ def datetimesorter (obj):
return datetime.datetime.combine(date, time)
def groupedProgrammeItems(event):
programmeItems = sorted(event.programmeItems, key=datetimesorter)
# print(list(link_target_iterator(event.programmeItems)))
programmeItems = sorted(list(link_target_iterator(event.programmeItems)), key=datetimesorter)
return regroup(programmeItems, lambda e: datesorter(e).strftime(DATE_OUTPUT_FORMAT))
produser_role_sorting = ['artist', 'co-producer', 'other professional', 'team', 'partner']
def generate ():
initContentTypes()
# Clear existing collections
resetCollections(contentTypes)
basedir = os.path.join(settings.BASE_DIR, 'generator')
staticdir = os.path.join(basedir, 'templates', 'static')
outputdir = os.path.join(basedir, 'static', 'generated')
......@@ -112,13 +116,29 @@ def generate ():
notes = collectionFor('notes')
trajectories = collectionFor('trajectory')
grouped_produsers = sorted(regroup(sorted(produsers.models, key=lambda produser: try_attributes(produser, ['sortname', 'name', 'produser']).lower()), 'role'), key=lambda group: produser_role_sorting.index(group[0]) if group[0] in produser_role_sorting else inf)
def getProduserSortKey (produser):
attr = try_attributes(produser, ['sortname', 'name', 'produser'])
if attr and attr.value:
return attr.value.lower()
else:
return None
def getLabelAsSortKey (model):
label = getattr(model, model.labelField)
if label and label.value:
return label.value.lower()
else:
return ''
grouped_produsers = sorted(regroup(sorted(produsers.models, key=getProduserSortKey), 'role'), key=lambda group: produser_role_sorting.index(group[0]) if group[0] in produser_role_sorting else inf)
output(os.path.join(outputdir, 'produsers.html'), 'produsers.html', { 'produsers': sorted(produsers.models, key=lambda r: str(try_attributes(r, ['sortname', 'name', 'produser', 'key'])).lower()), 'grouped_produsers': grouped_produsers })
# output(os.path.join(outputdir, 'produsers.layout.html'), 'produsers.layout.html', { 'produsers': sorted(produsers.models, key=lambda r: str(r.key)), 'grouped_produsers': grouped_produsers })
output(os.path.join(outputdir, 'tags.html'), 'tags.html', { 'tags': sorted(tags.models, key=lambda m: getattr(m, m.labelField)) })
output(os.path.join(outputdir, 'bibliography.html'), 'bibliography.html', { 'bibliography': sorted(bibliography.models, key=lambda m: getattr(m, m.labelField)) })
output(os.path.join(outputdir, 'external-projects.html'), 'external-projects.html', { 'externalProjects': sorted(externalProjects.models, key=lambda m: getattr(m, m.labelField)) })
output(os.path.join(outputdir, 'tags.html'), 'tags.html', { 'tags': sorted(tags.models, key=getLabelAsSortKey) })
output(os.path.join(outputdir, 'bibliography.html'), 'bibliography.html', { 'bibliography': sorted(bibliography.models, key=getLabelAsSortKey) })
output(os.path.join(outputdir, 'external-projects.html'), 'external-projects.html', { 'externalProjects': sorted(externalProjects.models, key=getLabelAsSortKey) })
output(os.path.join(outputdir, 'trajectories.html'), 'trajectories.html', { 'trajectories': trajectories.models })
# for produser in produsers.models:
......
This diff is collapsed.
......@@ -2,7 +2,7 @@ import markdown
import os.path
import urllib
from .models import modelFor, collectionFor, UnknownContentTypeError, knownContentTypes, resolveReferences
from .models import modelFor, collectionFor, UnknownContentTypeError, knownContentTypes, resolveReferences, knownContentType
from .utils import info, debug, warn, keyFilter
from markdown.extensions.toc import TocExtension
......@@ -69,7 +69,7 @@ def parse_pads ():
# the label
firstMetaKey, firstMetaValue = list(meta.items())[0]
if firstMetaKey in knownContentTypes:
if knownContentType(firstMetaKey):
contentType = firstMetaKey
key = keyFilter(firstMetaValue)
label = firstMetaValue
......@@ -121,7 +121,8 @@ def parse_pads ():
model.resolveLinks()
if model.content:
content, _ = resolveReferences(m.content, model=m) # Second return are the collected references
# Render inline references
content, _ = resolveReferences(model) # Second return are the collected references
# render markdown
md = markdown.Markdown(extensions=['extra', TocExtension(baselevel=2), 'attr_list'])
model.content = mark_safe(md.convert(content))
......
{% extends "generator_base_two_columns.html" %}
{% load generator_utils %}
{% block title %}Caveat → activities → {{ event.title|striptags }}{% endblock %}
......@@ -25,7 +26,7 @@
{% endif %}
</p>
<p class="event-infos">
{% for produser in event.produser %}
{% for produser in event.produser|link_target_iterator %}
<a href="{{ produser.link }}" class="produser">{{ produser }}</a>
{% endfor %}
</p>
......@@ -43,7 +44,7 @@
{% if event.participant %}
<h4>Participants</h4>
<p>
{% for produser in event.produser %}
{% for produser in event.produser|link_target_iterator %}
<a href="{{ produser.link }}" class="produser">
{{ produser }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
......@@ -61,7 +62,7 @@
<li id="{{ item.key }}">
<h2>{{ item.time }} {{ item }}</h2>
<p class="event-infos">
{% for produser in item.produser %}
{% for produser in item.produser|link_target_iterator %}
<a href="{{ produser.link }}" class="produser">{{ produser }}</a>
{% endfor %}
</p>
......@@ -73,7 +74,7 @@
{% if item.notes %}
<h4>Related documents</h4>
<ul class="simplelist">
{% for note in item.notes %}
{% for note in item.notes|link_target_iterator %}
<li><a href="{{ note.link }}" class="note">{{ note }}</a></li>
{% endfor %}
</ul>
......
......@@ -19,13 +19,13 @@
<p>
{{ event.location }}<br />
{% if event.address %}
{% for addressline in event.address %}
{% for addressline in event.address.value %}
{{ addressline }}<br />
{% endfor %}
{% endif %}
</p>
<p class="event-infos">
{% for produser in event.produser %}
{% for produser in event.produser.value %}
<a href="{{ produser.link }}" class="produser">{{ produser }}</a>
{% endfor %}
</p>
......@@ -35,7 +35,7 @@
{% if event.notes %}
<h4>Related documents</h4>
<ul class="simplelist">
{% for note in event.notes %}
{% for note in event.notes.value %}
<li><a href="{{ note.link }}" class="note">{{ note }}</a></li>
{% endfor %}
</ul>
......@@ -43,7 +43,7 @@
{% if event.participant %}
<h4>Participants</h4>
<p>
{% for produser in event.participant %}
{% for produser in event.participant.value %}
<a href="{{ produser.link }}" class="produser">
{{ produser }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
......
<html>
{% load generator_utils %}
<html>
<head>
<meta charset="utf-8">
......@@ -57,11 +58,11 @@
<span class="event-detail">
<p class="event-infos">{{ event.location }}</p>
{% for line in event.address %}
{% for line in event.address.value %}
<p class="event-infos">{{ line }}</p>
{% endfor %}
<p class="event-infos">
{% for produser in event.produser %}
{% for link, source, produser in event.produser|link_iterator %}
<a href="{{ produser.link }}">{{ produser }}</a>
{% endfor %}
</p>
......
{% extends "generator_base.html" %}
{% load generator_utils %}
{% block title %}Caveat → Produsers → {% if produser.name %}{{ produser.name|striptags }}{% else %}{{ produser.produser|striptags }}{% endif %}{% endblock %}
{% block content %}
<nav class="breadcrumbs">
......@@ -23,7 +24,7 @@
{% if produser.events %}
<h2>Activities</h2>
<ul class="simplelist">
{% for event in produser.events %}
{% for event in produser.events|link_target_iterator %}
<li>
<section class="event-preview">
<a href="{{ event.link }}">{{ event }}</a>
......@@ -37,7 +38,7 @@
{% if produser.bibliography %}
<h2>Bibliography</h2>
<ul class="simplelist">
{% for entry in produser.bibliography %}
{% for entry in produser.bibliography.value %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
......
{% extends "generator_base.html" %}
{% load generator_utils %}
{% block title %}
Caveat → {{ tag.tag|striptags }}
{% endblock %}
......@@ -16,8 +17,8 @@
{% if tag.produsers %}
<h2>Produsers</h2>
<ul class="simplelist">
{% for produser in tag.produsers %}
<li><a href="{{ produser.link }}">{{ produser }}</a></li>
{% for link, _, produser in tag.produsers|link_iterator %}
<li><a href="{{ produser.link }}#{{ link.id }}">{{ produser }}</a></li>
{% endfor %}
</ul>
{% endif %}
......@@ -25,8 +26,8 @@
{% if tag.trajectories %}
<h2>Trajectories</h2>
<ul class="simplelist">
{% for trajectory in tag.trajectories %}
<li><a href="{{ trajectory.produser.link }}">{{ trajectory.produser }}</a></li>
{% for link, _, trajectory in tag.trajectories|link_iterator %}
<li><a href="{{ trajectory.produser.value.target.link }}#{{ link.id }}">{{ trajectory.produser }}</a></li>
{% endfor %}
</ul>
{% endif %}
......@@ -34,8 +35,8 @@
{% if tag.events %}
<h2>Activities</h2>
<ul class="simplelist">
{% for event in tag.events %}
<li><a href="{{ event.link }}">{{ event }}</a></li>
{% for link, _, event in tag.events|link_iterator %}
<li><a href="{{ event.link }}#{{ link.id }}">{{ event }}</a></li>
{% endfor %}
</ul>
{% endif %}
......@@ -44,8 +45,8 @@
<ul class="simplelist">
<h2>Pads</h2>
<ul class="simplelist">
{% for pad in tag.pads %}
<li><a href="{{ pad.link }}">{{ pad.key }}</a></li>
{% for link, _, pad in tag.pads|link_iterator %}
<li><a href="{{ pad }}#{{ link.id }}">{{ pad.key }}</a></li>
{% endfor %}
</ul>
{% endif %}
......@@ -53,7 +54,7 @@
{% if tag.bibliography %}
<h2>Bibliography</h2>
<ul class="simplelist">
{% for bibliography in tag.bibliography %}
{% for link, _, bibliography in tag.bibliography|link_iterator %}
<li>{{ bibliography }}</li>
{% endfor %}
</ul>
......@@ -62,7 +63,7 @@
{% if tag.video %}
<h2>Video</h2>
<ul class="simplelist">
{% for video in tag.video %}
{% for video in tag.video|link_target_iterator %}
<li>
<video controls>
<source type="{{ video.type }}" src="{{ video.video }}">
......@@ -75,7 +76,7 @@
{% if tag.audio %}
<h2>Audio</h2>
<ul class="simplelist">
{% for audio in tag.audio %}
{% for audio in tag.audio|link_target_iterator %}
<li>
<audio controls>
<source type="{{ audio.type }}" src="{{ audio.audio }}">
......@@ -87,9 +88,9 @@
{% if tag.notes %}
<h2>Notes</h2>
<ul>
{% for note in tag.notes %}
{% for link, _, note in tag.notes|link_iterator %}
<li>
<a href="{{ note.link }}" class="note">{{ note }}</a>
<a href="{{ note.link }}#{{ link.id }}" class="note">{{ note }}</a>
</li>
{% endfor %}
</ul>
......
......@@ -20,5 +20,5 @@
{% endif %}
</section>
{% endfor %}
</section>
</section>
{% endblock %}
\ No newline at end of file
# -*- coding: utf-8 -*-
from django import template
from django.template.defaultfilters import stringfilter
from django.conf import settings
register = template.Library()
# @register.filter
# def sorted_images(project):
# return project.images.all().order_by('projectimage__image_order')
# loop through the multilink field
# for each row return the link, the source, the target
@register.filter
def link_iterator (field):
for link in field:
if link:
yield (link, link.source, link.target)
@register.filter
def link_target_iterator (field):
for link in field:
if link:
yield link.target
......@@ -54,7 +54,7 @@ def regroup (iterable, field):
def try_attributes (obj, attributes):
for attr in attributes:
if hasattr(obj, attr):
if hasattr(obj, attr) and getattr(obj, attr).value:
return getattr(obj, attr)
return None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment