No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

em_simple.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #!/usr/bin/python3
  2. #
  3. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  4. #
  5. # Copyright (C) 2015-2017 Cléo UMS-3287
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. import sys
  21. import os, os.path
  22. sys.path.append(os.path.dirname(os.getcwd()+'/..'))
  23. from lodel.context import LodelContext
  24. LodelContext.init()
  25. from lodel.settings.settings import Settings as settings
  26. settings('globconf.d')
  27. from lodel.settings import Settings
  28. from lodel.editorial_model.components import *
  29. from lodel.editorial_model.exceptions import *
  30. from lodel.editorial_model.model import EditorialModel
  31. em = EditorialModel('simpleem', 'Simple editorial model')
  32. base_group = em.new_group( 'base_group',
  33. display_name = 'Base group',
  34. help_text = 'Base group that implements base EM features (like classtype)'
  35. )
  36. ####################
  37. # Lodel Object #
  38. ####################
  39. em_abstract = em.new_class( 'abstract_object',
  40. display_name = 'Abstract lodel object',
  41. help_text = 'For testing purpose',
  42. group = base_group,
  43. abstract = True)
  44. em_object = em.new_class( 'object',
  45. display_name = 'Object',
  46. help_text = 'Main class for all Em objects',
  47. group = base_group,
  48. abstract = True,
  49. parents = em_abstract,
  50. )
  51. em_object.new_field( 'lodel_id',
  52. display_name = 'Lodel identifier',
  53. help_text = 'Uniq ID that identify every lodel object',
  54. group = base_group,
  55. data_handler = 'uniqid',
  56. internal = True,
  57. )
  58. em_object.new_field( 'date_create',
  59. display_name = 'Creation date',
  60. group = base_group,
  61. data_handler = 'datetime',
  62. now_on_create = True,
  63. internal = True,
  64. )
  65. em_object.new_field( 'date_update',
  66. display_name = 'Last update',
  67. group = base_group,
  68. data_handler = 'datetime',
  69. now_on_update = True,
  70. internal = True,
  71. )
  72. entitie = em.new_class( 'entitie',
  73. display_name = 'entitie',
  74. help_text = 'Replace old entity classtype',
  75. abstract = True,
  76. group = base_group,
  77. parents = em_object,
  78. )
  79. ########################
  80. # Base group
  81. ########################
  82. person = em.new_class( 'person',
  83. display_name = 'Person',
  84. help_text = 'Person type',
  85. abstract = False,
  86. group = base_group,
  87. parents = em_object,
  88. )
  89. person.new_field( 'firstname',
  90. display_name = {
  91. 'eng': 'Firstname',
  92. 'fre': 'Prénom',
  93. },
  94. data_handler = 'varchar',
  95. group = base_group,
  96. )
  97. person.new_field( 'lastname',
  98. display_name = {
  99. 'eng': 'Lastname',
  100. 'fre': 'Nom de famille',
  101. },
  102. data_handler = 'varchar',
  103. group = base_group,
  104. )
  105. # person.new_field( 'role',
  106. # display_name = {
  107. # 'eng': 'Role',
  108. # 'fre': 'Rôle',
  109. # },
  110. # data_handler = 'varchar',
  111. # group = base_group,
  112. # )
  113. entry = em.new_class( 'entry',
  114. display_name = 'Entry',
  115. help_text = 'Entry type',
  116. abstract = False,
  117. group = base_group,
  118. parents = em_object,
  119. )
  120. entry.new_field( 'name',
  121. display_name = {
  122. 'eng': 'Name',
  123. 'fre': 'Nom',
  124. },
  125. data_handler = 'varchar',
  126. group = base_group,
  127. )
  128. entry.new_field( 'description',
  129. display_name = {
  130. 'eng': 'Description',
  131. 'fre': 'Description',
  132. },
  133. data_handler = 'text',
  134. group = base_group,
  135. )
  136. entry.new_field( 'role',
  137. display_name = {
  138. 'eng': 'Role',
  139. 'fre': 'Rôle',
  140. },
  141. data_handler = 'varchar',
  142. group = base_group,
  143. )
  144. #####################
  145. # Editorial classes #
  146. #####################
  147. editorial_group = em.new_group( 'editorial_abstract',
  148. display_name = 'Editorial base',
  149. help_text = {
  150. 'eng': 'Contains abstract class to handler editorial contents',
  151. 'fre': 'Contient les classes abstraites permetant la gestion de contenu éditorial'
  152. },
  153. depends = (base_group,)
  154. )
  155. ################################ Texts #############################
  156. # Classe texte
  157. text = em.new_class( 'text',
  158. display_name = 'Text',
  159. help_text = 'Abstract class that represent texts',
  160. group = editorial_group,
  161. abstract = True,
  162. parents = entitie,
  163. )
  164. text.new_field( 'title',
  165. display_name = {'eng': 'Title', 'fre': 'Titre'},
  166. group = editorial_group,
  167. data_handler = 'text',
  168. nullable = True,)
  169. text.new_field( 'subtitle',
  170. display_name = {
  171. 'eng': 'Subtitle',
  172. 'fre': 'Sous-titre',
  173. },
  174. group = editorial_group,
  175. data_handler = 'text',
  176. nullable = True,
  177. default = None)
  178. text.new_field( 'language',
  179. display_name = {
  180. 'eng': 'Language',
  181. 'fre': 'Langue',
  182. },
  183. group = editorial_group,
  184. data_handler = 'varchar',
  185. nullable = True,
  186. default = None)
  187. text.new_field( 'text',
  188. display_name = {
  189. 'eng': 'Text',
  190. 'fre': 'Texte',
  191. },
  192. group = editorial_group,
  193. data_handler = 'text',
  194. nullable = True,
  195. default = None)
  196. text.new_field( 'pub_date',
  197. display_name = {
  198. 'eng': 'Publication date',
  199. 'fre': 'Date de publication',
  200. },
  201. group = editorial_group,
  202. data_handler = 'datetime',
  203. nullable = True,
  204. default = None)
  205. text.new_field( 'footnotes',
  206. display_name = {
  207. 'eng': 'Footnotes',
  208. 'fre': 'Notes de bas de page',
  209. },
  210. group = editorial_group,
  211. data_handler = 'text',
  212. nullable = True,
  213. default = None)
  214. text.new_field( 'linked_entries',
  215. display_name = {
  216. 'eng': 'Related entries',
  217. 'fre': 'Indices liés',
  218. },
  219. group = editorial_group,
  220. data_handler = 'list',
  221. nullable = True,
  222. allowed_classes = [entry],
  223. back_reference = ('entry', 'linked_texts'),
  224. default = None
  225. )
  226. entry.new_field( 'linked_texts',
  227. display_name = {
  228. 'eng': 'Related text',
  229. 'fre': 'Texte lié ',
  230. },
  231. data_handler = 'list',
  232. nullable = True,
  233. allowed_classes = [text],
  234. group = editorial_group,
  235. back_reference = ('text', 'linked_entries'),
  236. default = None
  237. )
  238. # Classe article
  239. article = em.new_class( 'article',
  240. display_name = 'Article',
  241. group = editorial_group,
  242. abstract = False,
  243. parents = text)
  244. article.new_field( 'abstract',
  245. display_name = {
  246. 'eng': 'Abstract',
  247. 'fre': 'Résumé',
  248. },
  249. group = editorial_group,
  250. data_handler = 'text'
  251. )
  252. article.new_field( 'appendix',
  253. display_name = {
  254. 'eng': 'Appendix',
  255. 'fre': 'Appendice',
  256. },
  257. group = editorial_group,
  258. data_handler = 'text'
  259. )
  260. article.new_field( 'bibliography',
  261. display_name = {
  262. 'eng': 'Bibliography',
  263. 'fre': 'Bibliographie',
  264. },
  265. group = editorial_group,
  266. data_handler = 'text'
  267. )
  268. article.new_field( 'author_note',
  269. display_name = {
  270. 'eng': 'Author note',
  271. 'fre': "Note de l'auteur",
  272. },
  273. group = editorial_group,
  274. data_handler = 'text'
  275. )
  276. # Classe Review
  277. review = em.new_class( 'review',
  278. display_name = 'Review',
  279. group = editorial_group,
  280. abstract = False,
  281. parents = text)
  282. review.new_field( 'reference',
  283. display_name = {
  284. 'eng': 'Reference',
  285. 'fre': "Référence",
  286. },
  287. group = editorial_group,
  288. data_handler = 'text'
  289. )
  290. ###################################### Containers ##################
  291. # Classe container
  292. container = em.new_class( 'container',
  293. display_name = 'Container',
  294. group = editorial_group,
  295. abstract = True,
  296. parents = entitie)
  297. container.new_field( 'title',
  298. display_name = 'Title',
  299. group = editorial_group,
  300. data_handler = 'text'
  301. )
  302. container.new_field( 'subtitle',
  303. display_name = 'Subtitle',
  304. group = editorial_group,
  305. data_handler = 'text'
  306. )
  307. container.new_field( 'language',
  308. display_name = {
  309. 'eng' : 'Language',
  310. 'fre' : 'Langue',
  311. },
  312. group = editorial_group,
  313. data_handler = 'varchar'
  314. )
  315. container.new_field( 'linked_directors',
  316. display_name = {
  317. 'eng': 'Directors',
  318. 'fre': 'Directeurs',
  319. },
  320. group = editorial_group,
  321. data_handler = 'list',
  322. nullable = True,
  323. allowed_classes = [person],
  324. back_reference = ('person', 'linked_containers'),
  325. default = None
  326. )
  327. person.new_field( 'linked_containers',
  328. display_name = {
  329. 'eng': 'Director of ',
  330. 'fre': 'Directeur de ',
  331. },
  332. group = editorial_group,
  333. data_handler = 'list',
  334. nullable = True,
  335. allowed_classes = [container],
  336. back_reference = ('container', 'linked_directors'),
  337. default = None
  338. )
  339. container.new_field( 'description',
  340. display_name = {
  341. 'eng': 'Description',
  342. 'fre': 'Description',
  343. },
  344. data_handler = 'text',
  345. group = editorial_group,
  346. )
  347. container.new_field( 'publisher_note',
  348. display_name = {
  349. 'eng': 'Publisher note',
  350. 'fre': "Note de l'éditeur",
  351. },
  352. data_handler = 'text',
  353. group = editorial_group,
  354. )
  355. # Classe collection
  356. collection = em.new_class( 'collection',
  357. display_name = 'Collection',
  358. group = editorial_group,
  359. abstract = False,
  360. parents = container)
  361. collection.new_field( 'issn',
  362. display_name = {
  363. 'eng': 'ISSN',
  364. 'fre': "ISSN",
  365. },
  366. data_handler = 'varchar',
  367. group = editorial_group,
  368. )
  369. # Classe Publication : Pour gérer les back_references vers issue ou part
  370. publication = em.new_class( 'publication',
  371. display_name = 'Publication',
  372. group = editorial_group,
  373. abstract = True,
  374. parents = container)
  375. publication.new_field( 'linked_texts',
  376. display_name = {
  377. 'eng': 'Text',
  378. 'fre': 'Texte',
  379. },
  380. data_handler = 'list',
  381. nullable = True,
  382. allowed_classes = [text],
  383. group = editorial_group,
  384. back_reference = ('text', 'linked_container'),
  385. default = None
  386. )
  387. text.new_field( 'linked_container',
  388. display_name = {
  389. 'eng': 'Container',
  390. 'fre': 'Conteneur',
  391. },
  392. data_handler = 'link',
  393. nullable = True,
  394. allowed_classes = [publication],
  395. group = editorial_group,
  396. back_reference = ('publication', 'linked_texts'),
  397. default = None
  398. )
  399. # Classe Issue
  400. issue = em.new_class( 'issue',
  401. display_name = 'Issue',
  402. group = editorial_group,
  403. abstract = False,
  404. parents = publication)
  405. issue.new_field( 'isbn',
  406. display_name = 'ISBN',
  407. data_handler = 'varchar',
  408. group = editorial_group,
  409. )
  410. issue.new_field( 'print_isbn',
  411. display_name = {
  412. 'eng': 'Printed ISBN',
  413. 'fre': "ISBN imprimé",
  414. },
  415. data_handler = 'varchar',
  416. group = editorial_group,
  417. )
  418. issue.new_field( 'number',
  419. display_name = {
  420. 'eng': 'Number',
  421. 'fre': 'Numéro',
  422. },
  423. data_handler = 'varchar',
  424. group = editorial_group,
  425. )
  426. issue.new_field( 'cover',
  427. display_name = {
  428. 'eng': 'Cover',
  429. 'fre': 'Couverture',
  430. },
  431. data_handler = 'varchar',
  432. group = editorial_group,
  433. )
  434. issue.new_field( 'print_pub_date',
  435. display_name = {
  436. 'eng': 'Print date',
  437. 'fre': "Date d'impression",
  438. },
  439. data_handler = 'datetime',
  440. group = editorial_group,
  441. )
  442. issue.new_field( 'e_pub_date',
  443. display_name = {
  444. 'eng': 'Electronic publication date',
  445. 'fre': 'Date de publication électronique',
  446. },
  447. data_handler = 'datetime',
  448. group = editorial_group,
  449. )
  450. issue.new_field( 'abstract',
  451. display_name = {
  452. 'eng': 'Abstract',
  453. 'fre': 'Résumé',
  454. },
  455. data_handler = 'text',
  456. group = editorial_group,
  457. )
  458. issue.new_field( 'collection',
  459. display_name = {
  460. 'eng': 'Collection',
  461. 'fre': 'Collection',
  462. },
  463. data_handler = 'link',
  464. nullable = True,
  465. allowed_classes = [collection],
  466. group = editorial_group,
  467. back_reference = ('collection', 'linked_issues'),
  468. default = None
  469. )
  470. collection.new_field( 'linked_issues',
  471. display_name = {
  472. 'eng': 'Linked issues',
  473. 'fre': 'Numéros',
  474. },
  475. data_handler = 'hierarch',
  476. back_reference = ('Issue', 'collection'),
  477. group = editorial_group,
  478. allowed_classes = [issue],
  479. default = None,
  480. nullable = True)
  481. # Classe Part
  482. part = em.new_class( 'part',
  483. display_name = 'Part',
  484. group = editorial_group,
  485. abstract = False,
  486. parents = publication,)
  487. part.new_field( 'publication',
  488. display_name = {
  489. 'eng': 'Publication',
  490. 'fre': 'Publication',
  491. },
  492. data_handler = 'link',
  493. nullable = True,
  494. allowed_classes = [publication],
  495. group = editorial_group,
  496. back_reference = ('publication', 'linked_parts'),
  497. default = None
  498. )
  499. publication.new_field( 'linked_parts',
  500. display_name = {
  501. 'eng': 'Parts',
  502. 'fre': 'Parties',
  503. },
  504. data_handler = 'hierarch',
  505. nullable = True,
  506. allowed_classes = [part],
  507. group = editorial_group,
  508. back_reference = ('part', 'publication'),
  509. default = None
  510. )
  511. #####################
  512. # Persons & authors #
  513. #####################
  514. editorial_person_group = em.new_group( 'editorial_person',
  515. display_name = 'Editorial person',
  516. help_text = {
  517. 'eng': 'Introduce the concept of editorial person (author, translator, director)',
  518. 'fre': 'Contient les classes servant à la gestion des personnes éditoriales (auteur, traducteur, directeur...)',
  519. },
  520. depends = (editorial_group,)
  521. )
  522. text_person = em.new_class( 'text_person',
  523. display_name = {
  524. 'eng': 'TextPerson',
  525. 'fre': 'TextePersonne',
  526. },
  527. help_text = {
  528. 'eng': 'Represent a link between someone and a text',
  529. 'fre': 'Représente un lien entre une personne et un texte',
  530. },
  531. group = editorial_person_group,
  532. abstract = True,
  533. parents = entitie,
  534. )
  535. bref_textperson_text = text_person.new_field( 'text',
  536. display_name = {
  537. 'eng': 'Linked text',
  538. 'fre': 'Texte lié',
  539. },
  540. data_handler = 'link',
  541. allowed_classes = [text],
  542. group = editorial_person_group
  543. )
  544. bref_textperson_person = text_person.new_field( 'person',
  545. display_name = {
  546. 'eng': 'Linked person',
  547. 'fre': 'Personne liée',
  548. },
  549. data_handler = 'link',
  550. allowed_classes = [person],
  551. group = editorial_person_group,
  552. )
  553. text_person.new_field( 'role',
  554. display_name = {
  555. 'eng': 'Person role',
  556. 'fre': 'Role de la personne',
  557. },
  558. data_handler = 'varchar',
  559. group = editorial_person_group
  560. )
  561. # simple example of linked text / person
  562. person.new_field( 'linked_texts',
  563. display_name = {
  564. 'eng': 'Linked texts',
  565. 'fre': 'Textes liés',
  566. },
  567. data_handler = 'list',
  568. back_reference = ('Text', 'linked_persons'),
  569. group = editorial_person_group,
  570. allowed_classes = [text],
  571. default = None,
  572. nullable = True)
  573. text.new_field( 'linked_persons',
  574. display_name = {
  575. 'eng': 'Linked persons',
  576. 'fre': 'Personnes liées',
  577. },
  578. data_handler = 'list',
  579. back_reference = ('Person', 'linked_texts'),
  580. group = editorial_person_group,
  581. allowed_classes = [person],
  582. default = None,
  583. nullable = True)
  584. #####################
  585. # Index classes # <--- Note : using a different datasource for testing
  586. ##################### purpose
  587. index_group = em.new_group( 'index_group',
  588. display_name = 'Indexes',
  589. help_text = {
  590. 'eng': 'EM class that represents indexes'},
  591. depends = (editorial_group,))
  592. index_abstract = em.new_class(
  593. 'indexAbs',
  594. display_name = {'eng': 'Abstract Index'},
  595. help_text = {'eng': 'Abstract class common to each Index classes'},
  596. abstract = True,
  597. group = index_group,
  598. datasources = 'dummy2',
  599. parents = em_object)
  600. index_name = index_abstract.new_field(
  601. 'name',
  602. display_name = {
  603. 'eng': 'name',
  604. 'fre': 'nom'},
  605. data_handler = 'text',
  606. group = index_group)
  607. index_value = index_abstract.new_field(
  608. 'value',
  609. display_name = {
  610. 'eng': 'value',
  611. 'fre': 'valeur'},
  612. data_handler = 'varchar',
  613. group = index_group)
  614. text.new_field( 'indexes',
  615. display_name = {
  616. 'eng': 'Indexes',
  617. 'fre': 'Indexes'},
  618. data_handler = 'list',
  619. back_reference = ('Indexabs', 'texts'),
  620. allowed_classes = [index_abstract],
  621. default = None,
  622. nullable = True,
  623. group = index_group)
  624. index_abstract.new_field( 'texts',
  625. display_name = {
  626. 'eng': 'Text referenced by this index',
  627. 'fre': 'Texte contenant cette index'},
  628. data_handler = 'list',
  629. back_reference = ('Text', 'indexes'),
  630. allowed_classes = [text],
  631. group = index_group)
  632. index_theme = em.new_class(
  633. 'indexTheme',
  634. display_name = {
  635. 'eng': 'Thematic index',
  636. 'fre': 'Index thématique'},
  637. group = index_group,
  638. datasources = 'dummy2',
  639. parents = index_abstract)
  640. index_theme_theme = index_abstract.new_field(
  641. 'theme',
  642. display_name = {
  643. 'eng': 'theme'},
  644. data_handler = 'varchar',
  645. group = index_group)
  646. #############
  647. # USERS #
  648. #############
  649. user_group = em.new_group(
  650. 'users', display_name = 'Lodel users',
  651. help_text = 'Group that handle users en perm')
  652. user = em.new_class(
  653. 'user', display_name = 'Lodel user', help_text = 'Represent a lodel user',
  654. group = user_group, abstract = False)
  655. user.new_field(
  656. 'id', display_name = 'user identifier', help_text = 'Uniq ID',
  657. group = user_group, data_handler = 'uniqid', internal = True)
  658. user.new_field(
  659. 'firstname', display_name = 'Firstname',
  660. group = user_group, data_handler = 'varchar', internal = False)
  661. user.new_field(
  662. 'lastname', display_name = 'Lastname',
  663. group = user_group, data_handler = 'varchar', internal = False)
  664. user.new_field(
  665. 'login', display_name = 'user login', help_text = 'login',
  666. group = user_group, data_handler = 'varchar', uniq = True, internal = False)
  667. user.new_field(
  668. 'password', display_name = 'Password',
  669. group = user_group, data_handler = 'password', internal = False)
  670. em.save('xmlfile', filename = 'editorial_models/em_simple.xml')
  671. pickle_file = 'examples/em_simple.pickle'
  672. em.save('picklefile', filename = pickle_file)
  673. print("Output written in %s" % pickle_file)