src/jocondelab/management/commands/export_rdf.py
changeset 376 2ad94c31d330
parent 375 4889e93fcfad
equal deleted inserted replaced
375:4889e93fcfad 376:2ad94c31d330
   178 
   178 
   179     def define_extensions(self, g):
   179     def define_extensions(self, g):
   180         self.addN(g, [
   180         self.addN(g, [
   181             (JOCONDELAB_NS.wikipediaLabel, RDFS.subPropertyOf, SKOS.altLabel),
   181             (JOCONDELAB_NS.wikipediaLabel, RDFS.subPropertyOf, SKOS.altLabel),
   182             (JOCONDELAB_NS.wikipediaAlternativeLabel, RDFS.subPropertyOf, SKOS.altLabel),
   182             (JOCONDELAB_NS.wikipediaAlternativeLabel, RDFS.subPropertyOf, SKOS.altLabel),
   183             # JOCONDELAB_NS.Thesaurus -> SKOS.ConceptScheme
       
   184             # JOCONDELAB_NS.Term -> SKOS.Concept
       
   185             # JOCONDELAB_NS.normalizedLabel -> SKOS.hiddenLabel
       
   186             # JOCONDELAB_NS.wikipediaPage -> range foaf:Document
       
   187             # <owl:ObjectProperty rdf:about="#wikipediaArticle">
       
   188             #     <rdfs:comment xml:lang="en">A Wikipedia article of which subject is the resource.</rdfs:comment>
       
   189             #     <rdfs:domain rdf:resource="#Feature"/>
       
   190             #     <rdfs:label xml:lang="en">wikipedia article</rdfs:label>
       
   191             #     <rdfs:range rdf:resource="#WikipediaArticle"/>
       
   192             # </owl:ObjectProperty>
       
   193             # JOCONDELAB_NS.wikipediaPageID ??
       
   194             # JOCONDELAB_NS.wikipediaPageRevision ??
       
   195             # JOCONDELAB_NS.alternativeWikipediaPage -> range foaf:Document
       
   196             # JOCONDELAB_NS.alternativeWikipediaPageID ??
       
   197             # JOCONDELAB_NS.dbpediaResource
       
   198             # JOCONDELAB_NS.urlStatus  cf: src/core/models/term.py
       
   199             #   (0, "null_result"),
       
   200             #   (1, "redirection"),
       
   201             #   (2, "homonyme"),
       
   202             #   (3, "match"),
       
   203             #   (4, "unsematized"),
       
   204             # JOCONDELAB_NS.linkSemanticLevel cf: src/core/models/term.py
       
   205             #   TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES = (
       
   206             #   (0, "--"),
       
   207             #   (1, "EE"),
       
   208             #   (2, "EI"),
       
   209             #   (3, "BM"),
       
   210             #   (4, "NM"))
       
   211             # JOCONDELAB_NS.linkValidated
       
   212             # JOCONDELAB_NS.linkValidationDate
       
   213             # JOCONDELAB_NS.linkValidator
       
   214             # JOCONDELAB_NS.wikipediaEdition
       
   215             # JOCONDELAB_NS.DbpediaField
       
   216             # JOCONDELAB_NS.dbpediaField
       
   217             # JOCONDELAB_NS.dbpediaFieldThumbnail
       
   218             # JOCONDELAB_NS.dbpediaFieldLabel
       
   219             # JOCONDELAB_NS.dbpediaFieldAbstract
       
   220             # JOCONDELAB_NS.DbpediaYear
       
   221             # JOCONDELAB_NS.dbpediaYear
       
   222             # JOCONDELAB_NS.dbpediaYearStart
       
   223             # JOCONDELAB_NS.dbpediaYearEnd
       
   224             # JOCONDELAB_NS.DbpediaGeo
       
   225             # JOCONDELAB_NS.dbpediaGeo
       
   226             # JOCONDELAB_NS.Notice
       
   227             # JOCONDELAB_NS.NoticeImage
       
   228 
       
   229             # JOCONDELAB_NS.YearInfo
       
   230             # JOCONDELAB_NS.YearInfoStart
       
   231             # JOCONDELAB_NS.YearInfoEnd
       
   232 
       
   233             # JOCONDELAB_NS.noticeRef
       
   234             # JOCONDELAB_NS.noticeAdpt
       
   235             # JOCONDELAB_NS.noticeAppl
       
   236             # JOCONDELAB_NS.noticeAptn
       
   237             # JOCONDELAB_NS.noticeAttr
       
   238             # JOCONDELAB_NS.noticeAutr
       
   239             # JOCONDELAB_NS.noticeAutrTerm
       
   240             # JOCONDELAB_NS.noticeBibl
       
   241             # JOCONDELAB_NS.noticeComm
       
   242             # JOCONDELAB_NS.noticeContact
       
   243             # JOCONDELAB_NS.noticeCoor
       
   244             # JOCONDELAB_NS.noticeCopy
       
   245             # JOCONDELAB_NS.noticeDacq
       
   246             # JOCONDELAB_NS.noticeCata
       
   247             # JOCONDELAB_NS.noticeDation
       
   248             # JOCONDELAB_NS.noticeDdpt
       
   249             # JOCONDELAB_NS.noticeDecv
       
   250             # JOCONDELAB_NS.noticeDeno
       
   251             # JOCONDELAB_NS.noticeDepo
       
   252             # JOCONDELAB_NS.noticeDesc
       
   253             # JOCONDELAB_NS.noticeDesy
       
   254             # JOCONDELAB_NS.noticeDims
       
   255             # JOCONDELAB_NS.noticeDmaj
       
   256             # JOCONDELAB_NS.noticeDmis
       
   257             # JOCONDELAB_NS.noticeDomn
       
   258             # JOCONDELAB_NS.noticeDomnTerm
       
   259             # JOCONDELAB_NS.noticeDrep
       
   260             # JOCONDELAB_NS.noticeEcol
       
   261             # JOCONDELAB_NS.noticeEcolTerm
       
   262             # JOCONDELAB_NS.noticeEpoq
       
   263             # JOCONDELAB_NS.noticeEpoqTerm
       
   264             # JOCONDELAB_NS.noticeEtat
       
   265             # JOCONDELAB_NS.noticeExpo
       
   266             # JOCONDELAB_NS.noticeGene
       
   267             # JOCONDELAB_NS.noticeGeohi
       
   268             # JOCONDELAB_NS.noticeHist
       
   269             # JOCONDELAB_NS.noticeImage
       
   270             # JOCONDELAB_NS.noticeInsc
       
   271             # JOCONDELAB_NS.noticeInv
       
   272             # JOCONDELAB_NS.noticeLabel
       
   273             # JOCONDELAB_NS.noticeLabo
       
   274             # JOCONDELAB_NS.noticeLieux
       
   275             # JOCONDELAB_NS.noticeLieuxTerm
       
   276             # JOCONDELAB_NS.noticeLoca
       
   277             # JOCONDELAB_NS.noticeLoca2
       
   278             # JOCONDELAB_NS.noticeMill
       
   279             # JOCONDELAB_NS.noticeMilu
       
   280             # JOCONDELAB_NS.noticeMosa
       
   281             # JOCONDELAB_NS.noticeMsgcom
       
   282             # JOCONDELAB_NS.noticeMuseo
       
   283             # JOCONDELAB_NS.noticeNsda
       
   284             # JOCONDELAB_NS.noticeOnom
       
   285             # JOCONDELAB_NS.noticePaut
       
   286             # JOCONDELAB_NS.noticePdat
       
   287             # JOCONDELAB_NS.noticePdec
       
   288             # JOCONDELAB_NS.noticePeoc
       
   289             # JOCONDELAB_NS.noticePeri
       
   290             # JOCONDELAB_NS.noticePeriTerm
       
   291             # JOCONDELAB_NS.noticePeru
       
   292             # JOCONDELAB_NS.noticePhot
       
   293             # JOCONDELAB_NS.noticePins
       
   294             # JOCONDELAB_NS.noticePlieux
       
   295             # JOCONDELAB_NS.noticePrep
       
   296             # JOCONDELAB_NS.noticePuti
       
   297             # JOCONDELAB_NS.noticeReda
       
   298             # JOCONDELAB_NS.noticeRefim
       
   299             # JOCONDELAB_NS.noticeRepr
       
   300             # JOCONDELAB_NS.noticeReprTerm
       
   301             # JOCONDELAB_NS.noticeSrep
       
   302             # JOCONDELAB_NS.noticeSrepTerm
       
   303             # JOCONDELAB_NS.noticeStat
       
   304             # JOCONDELAB_NS.noticeTech
       
   305             # JOCONDELAB_NS.noticeTico
       
   306             # JOCONDELAB_NS.noticeTitr
       
   307             # JOCONDELAB_NS.noticeUtil
       
   308             # JOCONDELAB_NS.noticeVideo
       
   309             # JOCONDELAB_NS.noticeWww
       
   310 
       
   311             # JOCONDELAB_NS.noticeImage
       
   312             # JOCONDELAB_NS.noticeImageUrl
       
   313 
       
   314             # JOCONDELAB_NS.noticeYear
       
   315             # JOCONDELAB_NS.noticeYearStart
       
   316             # JOCONDELAB_NS.noticeYearEnd
       
   317 
       
   318             # JOCONDELAB_NS.ContributedTerm
       
   319             # JOCONDELAB_NS.Contribution
       
   320             # JOCONDELAB_NS.contributionTerm
       
   321             # JOCONDELAB_NS.contributionThesaurus
       
   322             # JOCONDELAB_NS.contributionNotice
       
   323             # JOCONDELAB_NS.contributionCount
       
   324 
       
   325         ])
   183         ])
   326 
   184 
   327     def add_n(self, g, triples):
   185     def add_n(self, g, triples):
   328         for triple in triples:
   186         for triple in triples:
   329             g.add(triple)
   187             g.add(triple)
   483         for fieldName in [
   341         for fieldName in [
   484             'ref', 'adpt', 'appl', 'aptn', 'attr', 'autr', 'bibl',
   342             'ref', 'adpt', 'appl', 'aptn', 'attr', 'autr', 'bibl',
   485             'comm', 'contact', 'coor', 'copy', 'dacq', 'data',
   343             'comm', 'contact', 'coor', 'copy', 'dacq', 'data',
   486             'dation', 'ddpt', 'decv', 'deno', 'depo', 'desc', 'desy',
   344             'dation', 'ddpt', 'decv', 'deno', 'depo', 'desc', 'desy',
   487             'dims', 'dmaj', 'dmis', 'domn', 'drep', 'ecol', 'epoq',
   345             'dims', 'dmaj', 'dmis', 'domn', 'drep', 'ecol', 'epoq',
   488             'etat', 'expo', 'gene', 'geohi', 'hist', 'image', 'insc',
   346             'etat', 'expo', 'gene', 'geohi', 'hist', 'insc',
   489             'inv', 'label', 'labo', 'lieux', 'loca', 'loca2', 'mill',
   347             'inv', 'label', 'labo', 'lieux', 'loca', 'loca2', 'mill',
   490             'milu', 'mosa', 'msgcom', 'museo', 'nsda', 'onom', 'paut',
   348             'milu', 'mosa', 'msgcom', 'museo', 'nsda', 'onom', 'paut',
   491             'pdat', 'pdec', 'peoc', 'peri', 'peru', 'phot', 'pins',
   349             'pdat', 'pdec', 'peoc', 'peri', 'peru', 'phot', 'pins',
   492             'plieux', 'prep', 'puti', 'reda', 'refim', 'repr', 'srep',
   350             'plieux', 'prep', 'puti', 'reda', 'refim', 'repr', 'srep',
   493             'stat', 'tech', 'tico', 'titr', 'util', 'video', 'www'
   351             'stat', 'tech', 'tico', 'titr', 'util', 'video', 'www'
   494         ]:
   352         ]:
   495             fieldValue = getattr(notice, fieldName)
   353             fieldValue = getattr(notice, fieldName)
   496             if fieldValue:
   354             if fieldValue:
   497                 g.add((notice_ref, getattr(JOCONDELAB_NS, "notice"+fieldName.capitalize()), Literal(fieldValue)))
   355                 g.add((notice_ref, getattr(JOCONDELAB_NS, "notice"+fieldName.capitalize()), Literal(fieldValue)))
   498 
   356 
   499         termNbs = NoticeTerm.objects.filter(notice=notice).count()
   357         # termNbs = NoticeTerm.objects.filter(notice=notice).count()
   500         totalTermNb = 0
   358         # totalTermNb = 0
   501         for fieldName in ['autr', 'domn', 'ecol', 'epoq', 'lieux', 'peri', 'repr', 'srep']:
   359         # for fieldName in ['autr', 'domn', 'ecol', 'epoq', 'lieux', 'peri', 'repr', 'srep']:
   502 
   360 
   503             termQuery = getattr(notice, fieldName + "_terms")
   361         #     termQuery = getattr(notice, fieldName + "_terms")
   504             for term in termQuery.all():
   362         #     for term in termQuery.select_related('thesaurus').all():
   505                 if term.thesaurus.label.lower() == fieldName:
   363         #         if term.thesaurus.label.lower() == fieldName:
   506                     totalTermNb += 1
   364         #             totalTermNb += 1
   507                     g.add((notice_ref, getattr(JOCONDELAB_NS, "notice"+fieldName.capitalize()+"Term"), URIRef(term.uri)))
   365         #             g.add((notice_ref, getattr(JOCONDELAB_NS, "notice"+fieldName.capitalize()+"Term"), URIRef(term.uri)))
   508 
   366 
   509         if totalTermNb != termNbs:
   367         # if totalTermNb != termNbs:
   510             logger.critical("Bad term count for notice %s should be %s and is %s", notice_uri, termNbs, totalTermNb)
   368         #     logger.critical("Bad term count for notice %s should be %s and is %s", notice_uri, termNbs, totalTermNb)
   511 
   369         # noticeTerms = NoticeTerm.objects.filter(notice=notice).select_related('term', 'term__thesaurus')
       
   370         for nterm in notice.noticeterm_set.all():
       
   371             fieldName = nterm.term.thesaurus.label.lower().capitalize()
       
   372             g.add((notice_ref, getattr(JOCONDELAB_NS, "notice"+fieldName+"Term"), URIRef(nterm.term.uri)))
       
   373 
       
   374 
       
   375         g.add((notice_ref, JOCONDELAB_NS.noticeHasImage, Literal(notice.image)))
   512         for notice_image in notice.images.all():
   376         for notice_image in notice.images.all():
   513             notice_image_bnode = BNode()
   377             notice_image_bnode = BNode()
   514             g.add((notice_ref, JOCONDELAB_NS.noticeImage, notice_image_bnode))
   378             g.add((notice_ref, JOCONDELAB_NS.noticeImage, notice_image_bnode))
   515             g.add((notice_image_bnode, RDF.type, JOCONDELAB_NS.NoticeImage))
   379             g.add((notice_image_bnode, RDF.type, JOCONDELAB_NS.NoticeImage))
       
   380             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImagePath, Literal(notice_image.relative_url)))
   516             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageUrl, URIRef(notice_image.url)))
   381             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageUrl, URIRef(notice_image.url)))
   517             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageOrder, Literal(notice_image.order)))
   382             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageOrder, Literal(notice_image.order)))
   518             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageIsMain, Literal(notice_image.main)))
   383             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageIsMain, Literal(notice_image.main)))
   519             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageIsLarge, Literal(notice_image.large)))
   384             g.add((notice_image_bnode, JOCONDELAB_NS.noticeImageIsLarge, Literal(notice_image.large)))
   520 
   385 
   601             #open_method = codecs.open
   466             #open_method = codecs.open
   602             open_method = open
   467             open_method = open
   603             #open_args = [filepath, 'wb', "utf-8"]
   468             #open_args = [filepath, 'wb', "utf-8"]
   604             open_args = [filepath, 'wb']
   469             open_args = [filepath, 'wb']
   605 
   470 
   606         term_query = Term.objects.select_related('thesaurus', 'alternative_labels', 'dbpedia_fields').order_by('id')
   471         term_query = Term.objects.prefetch_related('dbpedia_fields', 'geo', 'years', 'alternative_labels').select_related('thesaurus', 'validator', 'parent').order_by('id')
   607 
   472 
   608         if self.limit >= 0:
   473         if self.limit >= 0:
   609             notice_term_query_filter = Q()
   474             notice_term_query_filter = Q()
   610             for klass in [NoticeTerm, AutrNoticeTerm, DomnNoticeTerm, EcolNoticeTerm, EpoqNoticeTerm, LieuxNoticeTerm, PeriNoticeTerm, ReprNoticeTerm, SrepNoticeTerm]:
   475             for klass in [NoticeTerm, AutrNoticeTerm, DomnNoticeTerm, EcolNoticeTerm, EpoqNoticeTerm, LieuxNoticeTerm, PeriNoticeTerm, ReprNoticeTerm, SrepNoticeTerm]:
   611                 notice_term_query = klass.objects.filter(notice__id__in=Notice.objects.order_by('id')[:self.limit].values_list('id', flat=True))
   476                 notice_term_query = klass.objects.filter(notice__id__in=Notice.objects.order_by('id')[:self.limit].values_list('id', flat=True))
   616         with open_method(*open_args) as dest_file:
   481         with open_method(*open_args) as dest_file:
   617             self.export_namespaces(dest_file)
   482             self.export_namespaces(dest_file)
   618             for query, namespaces, build_method in [
   483             for query, namespaces, build_method in [
   619                 (Thesaurus.objects.all(), 'thesaurus', self.export_thesaurus),
   484                 (Thesaurus.objects.all(), 'thesaurus', self.export_thesaurus),
   620                 (term_query, 'term', self.export_term),
   485                 (term_query, 'term', self.export_term),
   621                 (Notice.objects.order_by('id').select_related().all(), 'notice', self.export_notice),
   486                 (Notice.objects.order_by('id').select_related().prefetch_related('images', 'noticeterm_set', 'noticeterm_set__term', 'noticeterm_set__term__thesaurus', 'years').all(), 'notice', self.export_notice),
   622                 (ContributedTerm.objects.select_related().order_by('id').all(), 'contributed_term', self.export_contributed_term),
   487                 (ContributedTerm.objects.select_related().prefetch_related('dbpedia_fields').order_by('id').all(), 'contributed_term', self.export_contributed_term),
   623                 (Contribution.objects.select_related().order_by('id').all(), 'contribution', self.export_contribution)
   488                 (Contribution.objects.select_related('thesaurus', 'notice', 'term').order_by('id').all(), 'contribution', self.export_contribution)
   624             ]:
   489             ]:
   625                 self.export_objects(query, namespaces, build_method, dest_file)
   490                 self.export_objects(query, namespaces, build_method, dest_file)
   626 
   491 
   627 
   492 
   628 # list of objects
       
   629 
       
   630 # User
       
   631 
       
   632 # Notice
       
   633 
       
   634 # Term
       
   635 
       
   636 # TermLabel
       
   637 
       
   638 # Thesaurus
       
   639 
       
   640 # NoticeImage
       
   641 # NoticeTerm
       
   642 
       
   643 # Country
       
   644 
       
   645 # DbpediaFields
       
   646 
       
   647 # TermLinks
       
   648 
       
   649 # DbpediaYears
       
   650 # DbpediaGeo
       
   651 
       
   652 # ContributedTerm
       
   653 # ContributedFields
       
   654 # Contribution
       
   655 # ContributableTerm
       
   656 # TagcloudTerm
       
   657 # NoticeYears
       
   658 
       
   659 # TERM_URL_STATUS_CHOICES
       
   660 # TERM_URL_STATUS_CHOICES_TRANS
       
   661 # TERM_URL_STATUS_DICT
       
   662 # TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS
       
   663 # TERM_WK_LINK_SEMANTIC_LEVEL_DICT
       
   664 
       
   665 
       
   666 # export : thesaurus,
       
   667 
       
   668 
       
   669 
       
   670 # ref
       
   671 # adpt
       
   672 # appl
       
   673 # aptn
       
   674 # attr
       
   675 # bibl
       
   676 # comm
       
   677 # contact
       
   678 # coor
       
   679 # copy
       
   680 # dacq
       
   681 # data
       
   682 # dation
       
   683 # ddpt
       
   684 # decv
       
   685 # deno
       
   686 # depo
       
   687 # desc
       
   688 # desy
       
   689 # dims
       
   690 # dmaj
       
   691 # dmis
       
   692 # drep
       
   693 # etat
       
   694 # expo
       
   695 # gene
       
   696 # geohi
       
   697 # hist
       
   698 # image
       
   699 # insc
       
   700 # inv
       
   701 # label
       
   702 # labo
       
   703 # loca
       
   704 # loca2
       
   705 # mill
       
   706 # milu
       
   707 # mosa
       
   708 # msgcom
       
   709 # museo
       
   710 # nsda
       
   711 # onom
       
   712 # paut
       
   713 # pdat
       
   714 # pdec
       
   715 # peoc
       
   716 # peru
       
   717 # phot
       
   718 # pins
       
   719 # plieux
       
   720 # prep
       
   721 # puti
       
   722 # reda
       
   723 # refim
       
   724 # stat
       
   725 # tech
       
   726 # tico
       
   727 # titr
       
   728 # util
       
   729 # video
       
   730 # www
       
   731 # autr
       
   732 # domn
       
   733 # ecol
       
   734 # epoq
       
   735 # lieux
       
   736 # peri
       
   737 # repr
       
   738 # srep
       
   739 
       
   740