Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add abstract xmlElement model
  • Loading branch information
mvogl committed Jul 28, 2017
1 parent 411f1fc commit 1d62fe7
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 10 deletions.
85 changes: 75 additions & 10 deletions eoa/models.py
@@ -1,6 +1,7 @@
from django.db import models
from django.utils import timezone
from django.core.validators import URLValidator, RegexValidator
from django.contrib.postgres.fields import HStoreField

#################################################
#
Expand Down Expand Up @@ -48,13 +49,39 @@ def filepath(instance, filename):
('it', 'Italian'),
)

tagname_choice = (
('hi', 'Highlight'),
('foreign', 'Foreign'),
('ref', 'Reference'),
('note','Note'),
('label','Label'),
('language','Language'),
('graphic','Graphic'),
)

default_char = {
'max_length':200,
'default':None,
'blank':True,
'null':True,
}

validkeys = [
'place',
'type',
'target',
'indexname',
'sortkey',
'xml:lang',
'pages',
'n',
'url',
'rendition',
'rend',
'height',
'notation'
]

##########################################
#
# basic information on publications, citations and authors
Expand Down Expand Up @@ -99,26 +126,27 @@ class Meta:


class Citation(models.Model):
pub_type = models.CharField(
pubtype = models.CharField(
max_length=20,
choices=citation_choice,
default='book'
)
title = models.CharField(max_length=200)
titlea = models.CharField(**default_char)
titlem = models.CharField(**default_char)
date = models.IntegerField(
blank=True,
null=False,
validators=[RegexValidator(r'^\d{4}$')])
place = models.CharField(max_length=200)
publisher = models.CharField(max_length=200)
pupPlace = models.CharField(**default_char)
publisher = models.CharField(**default_char)
volume = models.CharField(**default_char)
issue = models.CharField(**default_char)
pages = models.CharField(max_length=20, blank=True)
# No automatic idstring here, is there a way to achive this?
idstring = models.CharField(max_length=200)
page = models.CharField(**default_char)
idstring = models.CharField(max_length=200,blank=False)
note = models.TextField(blank=True)

def __str__(self):
return self.title
return self.idstring + ': ' + self.title

class Meta:
ordering = ('title',)
Expand All @@ -143,6 +171,10 @@ class Author(models.Model):
Citation,
blank=True,
related_name='authors')
editions = models.ManyToManyField(
Citation,
blank=True,
related_name='editors')
born = models.DateField(
blank=True, null=True)
foto = models.ImageField(upload_to=filepath, blank=True)
Expand Down Expand Up @@ -218,6 +250,27 @@ class Subsubsection(CommonTextPart):
section = models.ForeignKey(Section, blank=False)
subsection = models.ForeignKey(Subsection, blank=False)

class MixedContent(models.Model):
parent = models.ForeignKey(MixedContent, blank=True)
position = model.PositiveIntegerField(blank=False)
tagname = models.CharField(**default_char,
choices=tagname_choice,
default='de')
attributes = HStoreField(
validators=[
KeysValidator(
validkeys,
strict=True,
messages = {
'missing_keys': _('Some keys were missing: %(keys)s'),
'extra_keys': _('Some unknown keys were provided: %(keys)s'),
}
)
]
)

class Meta:
abstract = True

class Paragraph(models.Model):
publication = models.ForeignKey(Publication)
Expand All @@ -227,10 +280,22 @@ class Paragraph(models.Model):
subsection = models.ForeignKey(Subsection, blank=True, null=True)
subsubsection = models.ForeignKey(Subsubsection, blank=True, null=True)
idstring = models.CharField(max_length=100,blank=True)
text = models.TextField(blank=False)
text = models.ForeignKey(MixedContent,blank=False)

def __str__(self):
return (self.publication.__str__() + ' ' + str(self.text)[:30])
return (self.publication.__str__() + ' ' + str(self.idstring))

class Meta:
ordering = ('idstring',)

class Text(models.Model):
parent = models.ForeignKey(MixedContent)
position = models.PositiveIntegerField(blank=False)
text = models.TextField(blank=True)
pass

class Table(models.Model):
pass

class Figure(models.Model):
pass
67 changes: 67 additions & 0 deletions publications/management/commands/_importer_class.py
Expand Up @@ -215,6 +215,64 @@ def paragraphs(self):
pass
return ret

def biblio(self):
ret = []
for path in self.returnPath(['div','body'],self.dataDict):
for result in self.getFromDict(self.dataDict,path):
try:
if result['head']['#text'] == 'Bibliography':
for bib in result['listBibl']['biblStruct']:
#print(bib.keys())
retBib = {}
retBib['id'] = bib['@id']
retBib['pubtype'] = bib['@type']
try:
retBib['author'] = bib['analytic']['author']['surname'] + ',' + bib['analytic']['author']['forename']
retBib['titlea'] = bib['analytic']['title']['#text']
except:
pass
try:
retBib['author'] = bib['monogr']['author']['surname'] + ',' + bib['monogr']['author']['forename']
except:
pass
try:
retBib['editor'] = bib['monogr']['editor']['surname'] + ',' + bib['monogr']['editor']['forename']
except:
pass
try:
retBib['titlem'] = bib['monogr']['title']['#text']
except:
pass
for key in ['publisher','pubPlace','date']:
try:
retBib[key] = (bib['monogr']['imprint'][key])
except:
pass
try:
retBib['note'] = bib['monogr']['imprint']['note']['p']['#text']
except:
pass
try:
resTemp = bib['monogr']['imprint']['biblScope']
if type(resTemp)==list:
for dic in resTemp:
retBib[dic['@unit']] = dic['#text']
elif isinstance(resTemp,dict):
retBib[resTemp['@unit']] = resTemp['#text']
except:
pass
ret.append(retBib)
return ret
except:
pass

########################
#
# Helper functions to write to db
#
########################


def createAuthors(text,publication):
authorObjects = []
authors = text.authors()
Expand Down Expand Up @@ -339,3 +397,12 @@ def createParagraphs(text,publication):
)
parList.append(parTemp)
return parList

def createCitations(text,publication):
citList = []
for cit in text.biblio():
citTemp, created = Citation.objects.update_or_create(
**cit
)
citList.append(citTemp)
return citList
2 changes: 2 additions & 0 deletions publications/management/commands/tei_import.py
Expand Up @@ -57,6 +57,8 @@ def handle(self,*args, **options):

paragraphs = createParagraphs(text,pub)

citations = createCitations(text,pub)

self.stdout.write(
'Successfully read TEI file, by {0}'.format(text.authors())
)

0 comments on commit 1d62fe7

Please sign in to comment.