web/ldt/text/utils.py
author wakimd
Tue, 16 Nov 2010 15:35:12 +0100
changeset 10 000f3ca19eaa
parent 9 22ab430e9b64
child 21 1a061f244254
child 22 83b28fc0d731
permissions -rw-r--r--
Added 404 templates + some views corrections
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     1
import uuid
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     2
import django.core.urlresolvers
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     3
from django.conf import settings
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     4
from ldt.text.models import *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     5
import urllib
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     6
import datetime
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     7
import lxml.etree
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     8
import base64
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    10
__BOOLEAN_DICT = {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    11
    'false':False,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    12
    'true':True,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    13
    '0':False,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    14
    '1':True,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    15
    't': True,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
    'f':False
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    18
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    19
def boolean_convert(bool):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
    if bool is None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    21
        return False
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
    if bool is True or bool is False:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    23
        return bool
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    24
    key = str(bool).lower()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    25
    return __BOOLEAN_DICT.get(key, False)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    26
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
def generate_uuid():
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
    return unicode(uuid.uuid1())
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    30
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    31
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
def normalize_tags(list):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    33
    nlist=[]
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    34
    for tag in list:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    35
        tag = tag.lower()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    36
        nlist.append(tag)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    37
    taglist = dict().fromkeys(nlist).keys()    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    38
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    39
    return taglist
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    40
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    41
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    42
def create_empty_annotation():
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    43
    iri = lxml.etree.Element('iri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    44
    doc = lxml.etree.ElementTree(iri)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    45
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    46
    textannotation = lxml.etree.SubElement(iri, 'text-annotation')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    47
    id = lxml.etree.SubElement(textannotation,'id')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
    uri = lxml.etree.SubElement(textannotation,'uri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    49
    tags = lxml.etree.SubElement(textannotation,'tags')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    50
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    51
    content = lxml.etree.SubElement(textannotation,'content')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    52
    color = lxml.etree.SubElement(content,'color')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    53
    description = lxml.etree.SubElement(content,'description')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    54
    title = lxml.etree.SubElement(content,'title')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    55
    text = lxml.etree.SubElement(content,'text')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    56
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    57
    meta = lxml.etree.SubElement(textannotation,'meta')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    58
    contributor = lxml.etree.SubElement(meta, "contributor")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
    creator = lxml.etree.SubElement(meta, "creator")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    60
    creationdate = lxml.etree.SubElement(meta, "created")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    61
    updatedate = lxml.etree.SubElement(meta, "modified")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
    return doc
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64