Commit 183064c5 authored by gijs's avatar gijs
Browse files

More contenttypes. Different way of setting contenttype.

parent 8abed1a5
from . import fields
from .utils import info, debug, CMAGENTA
from .utils import info, debug, CMAGENTA, keyFilter
import os.path
import datetime
import re
......@@ -21,15 +21,6 @@ from generator.settings import SITE_URL
The result of a reference depends the type, many objects will result
in a link, while some will result in a tag.
"""
def keyFilter (value):
if type(value) is list:
return '--'.join([keyFilter(v) for v in value])
elif type(value) is str:
return re.sub(r'[^a-z0-9-]', '', re.sub(r'\s+', '-', value.lower()))
else:
return value
class UnknownContentTypeError(Exception):
def __init__(self, contentType):
self.contentType = contentType
......@@ -51,17 +42,18 @@ class Link(object):
self.contentType = contentType
self.reverse = reverse
def __call__ (self, targetKey, source):
debug('Link target {}'.format(targetKey), color=CMAGENTA)
target = collectionFor(self.contentType).get(targetKey)
def __call__ (self, targetLabel, source):
debug('Link target {}'.format(targetLabel), color=CMAGENTA)
target = collectionFor(self.contentType).get(label=targetLabel)
if self.reverse:
self.reverse(target, source)
return target
class MultiLink(Link):
def __call__ (self, targetKeys, source):
debug('Link target keys', targetKeys, color=CMAGENTA)
targets = [ collectionFor(self.contentType).get(targetKey) for targetKey in filter(None, targetKeys) ]
def __call__ (self, targetLabels, source):
debug('Link target keys', targetLabels, color=CMAGENTA)
# Filter out empty string keys
targets = [ collectionFor(self.contentType).get(label=targetLabel) for targetLabel in filter(None, targetLabels) ]
if self.reverse:
for target in targets:
......@@ -148,9 +140,9 @@ def parseReferenceMetadata (raw):
def parseReference(match):
contentType = match.group(1)
key = match.group(2)
label = match.group(2)
metadata = parseReferenceMetadata(match.group(3)) if match.group(3) else None
target = collectionFor(contentType).get(key)
target = collectionFor(contentType).get(label=label)
debug('Metadata in reference: {}, source: {}'.format(metadata, match.group(0)))
# debug('Rendered reference ', renderReference(target))
......@@ -172,6 +164,9 @@ def parseReference(match):
# switch between reference type and inclusion types
def expandTags (content):
return re.sub(r'\[\[([\w\._\-]+)\]\]', '[[tags: \\1]]', content)
def resolveReferences (content):
# return content
if content:
......@@ -315,8 +310,14 @@ class Collection(object):
Retreive a model from the collection with the given label.
If instantiate is set to true an empty model will be created.
"""
def get (self, label):
key = keyFilter(label)
def get (self, key = None, label = None):
if not label and not key:
raise(AttributeError('Can not retreive a model without a key or a label.'))
elif not label:
label = key
elif not key:
key = keyFilter(label)
if self.has(key):
debug('Found entry for {}'.format(key))
return self.index[key]
......@@ -364,7 +365,28 @@ class Event (Model):
metadataFields = {
'date': fields.Single(fields.DateField()),
'end_date': fields.Single(fields.DateField()),
'time': fields.Single(fields.TimeField()),
'produser': multiLinkMultiReverse('produser', 'events'),
'participant': multiLinkMultiReverse('produser', 'events_participant'),
'event': fields.Single(fields.StringField()),
'title': fields.Single(fields.StringField()),
'summary': fields.Single(fields.MarkdownField()),
'location': fields.Single(fields.StringField()),
'address': fields.StringField(),
'tags': multiLinkMultiReverse('tag', 'events'),
'bibliography': multiLinkMultiReverse('bibliography', 'events'),
}
class ProgrammeItem (Model):
contentType = 'programme_item'
metadataFields = {
'date': fields.Single(fields.DateField()),
'end_date': fields.Single(fields.DateField()),
'time': fields.Single(fields.TimeField()),
'produser': multiLinkMultiReverse('produser', 'events'),
'participant': multiLinkMultiReverse('produser', 'events_participant'),
'event': fields.Single(fields.StringField()),
'title': fields.Single(fields.StringField()),
'summary': fields.Single(fields.MarkdownField()),
......@@ -485,11 +507,35 @@ class Audio (Model):
'produser': multiLinkMultiReverse('produser', 'audio')
}
class Image (Model):
contentType = 'image'
keyField = 'image'
labelField = 'image'
metadataFields = {
'image': fields.Single(fields.StringField()),
'tags': multiLinkMultiReverse('tag', 'image'),
'produser': multiLinkMultiReverse('produser', 'image')
}
class Text (Model):
contentType = 'text'
keyField = 'title'
labelField = 'title'
metadataFields = {
'title': fields.Single(fields.StringField()),
'tags': multiLinkMultiReverse('tag', 'image'),
'produser': multiLinkMultiReverse('produser', 'text'),
'event': multiLinkMultiReverse('event', 'text')
}
# Perhaps include the sort in the collection?
# Might also need to include the outputfolder here
# rather than on the model?
contentTypes = {
'event': { 'model': Event, 'collection': Collection(Event) },
'programme-item': { 'model': ProgrammeItem, 'collection': Collection(ProgrammeItem) },
'produser': { 'model': Produser, 'collection': Collection(Produser) },
'trajectory': { 'model': Trajectory, 'collection': Collection(Trajectory) },
'pad': { 'model': Pad, 'collection': Collection(Pad) },
......@@ -498,16 +544,21 @@ contentTypes = {
'bibliography': { 'model': Bibliography, 'collection': Collection(Bibliography) },
'video': { 'model': Video, 'collection': Collection(Video) },
'audio': { 'model': Audio, 'collection': Collection(Audio) },
'image': { 'model': Image, 'collection': Collection(Image) },
'text': { 'model': Text, 'collection': Collection(Text) },
'note': { 'model': Note, 'collecion': Collection(Note) },
}
knownContentTypes = contentTypes.keys()
def collectionFor (contentType):
if contentType in contentTypes:
if contentType in knownContentTypes:
return contentTypes[contentType]['collection']
else:
raise UnknownContentTypeError(contentType)
def modelFor (contentType):
if contentType in contentTypes:
if contentType in knownContentTypes:
return contentTypes[contentType]['model']
else:
raise UnknownContentTypeError(contentType)
\ No newline at end of file
raise UnknownContentTypeError(contentType)
......@@ -2,8 +2,8 @@ import markdown
import os.path
import urllib
from .models import modelFor, collectionFor, UnknownContentTypeError
from .utils import info, debug, error
from .models import modelFor, collectionFor, UnknownContentTypeError, knownContentTypes
from .utils import info, debug, error, warn, keyFilter
from markdown.extensions.toc import TocExtension
from py_etherpad import EtherpadLiteClient
......@@ -12,6 +12,8 @@ from django.core.management.base import BaseCommand, CommandError
from django.utils.safestring import mark_safe
from etherpadlite.models import Pad
from .settings import DEFAULT_CONTENT_TYPE
from ethertoff.settings import PAD_NAMESPACE_SEPARATOR, BASE_DIR, DEBUG
"""
......@@ -29,6 +31,10 @@ from ethertoff.settings import PAD_NAMESPACE_SEPARATOR, BASE_DIR, DEBUG
before linking is performed.
If both keys and labels are used to address models. Depending the order of
encountering we might create an instance for the label and another for the
key. Especially when the label / title is later changed.
"""
def parse_pads ():
......@@ -52,18 +58,31 @@ def parse_pads ():
meta = md.Meta
meta['pk'] = pad.pk
if 'type' not in meta:
meta['type'] = ['pad']
if meta['type'] == ['biography']:
meta['type'] = ['produser']
# if the first line of the metadata is a known contenttype
# use it as such. It's value becomes the key and potetntially
# the label
firstMetaKey, firstMetaValue = list(meta.items())[0]
if firstMetaKey in knownContentTypes:
contentType = firstMetaKey
key = keyFilter(firstMetaValue)
collection = collectionFor(meta['type'][0])
key = modelFor(meta['type'][0]).extractKey(meta)
if 'type' in meta:
warn('Both valid contenttype present in the first row ({0}) as well as a type declaration ({1}), using {0}'.format(contentType, meta['type'][0]), pad.display_slug)
else:
if 'type' in meta:
if meta['type'] == ['biography']:
meta['type'] = ['produser']
contentType = meta['type'][0]
else:
contentType = DEFAULT_CONTENT_TYPE
key = modelFor(contentType).extractKey(meta)
collection = collectionFor(contentType)
debug('Extracted key: {}'.format(key))
model = collection.get(key)
model = collection.get(key=key)
if model.empty:
debug('Filling model {}'.format(key))
model.fill(metadata=meta, content=content, source_path=pad.display_slug)
......
......@@ -9,9 +9,9 @@ MENU_ITEMS = [
]
SITE_URL = ''
SHOW_LOG_MESSAGES = DEBUG
SHOW_DEBUG_MESSAGES = DEBUG
DEFAULT_CONTENT_TYPE = 'pad'
SHOW_LOG_MESSAGES = True
SHOW_DEBUG_MESSAGES = True
try:
LOCAL_SETTINGS
......
from .settings import SHOW_LOG_MESSAGES
from .settings import SHOW_DEBUG_MESSAGES
import re
CRED = '\033[91m'
CGREEN = '\033[92m'
CYELLOW = '\033[93m'
......@@ -12,10 +14,13 @@ def info(*args):
if SHOW_LOG_MESSAGES:
print(*args)
def debug(*args, color=CYELLOW):
def debug(*args, color=CCYAN):
if SHOW_DEBUG_MESSAGES:
print(color, *args, CEND)
def warn(*args):
print(CYELLOW, *args, CEND)
def error(*args):
print(CRED, *args, CEND)
......@@ -38,9 +43,19 @@ def regroup (iterable, field):
return grouped
def try_attributes (obj, attributes):
for attr in attributes:
if hasattr(obj, attr):
return getattr(obj, attr)
return None
def keyFilter (value):
if type(value) is list:
return '--'.join([keyFilter(v) for v in value])
elif type(value) is str:
return re.sub(r'[^a-z0-9-]', '', re.sub(r'\s+', '-', value.lower()))
else:
return value
\ No newline at end of file
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