Yann Weber 8 лет назад
Родитель
Сommit
6a7e6718f3
6 измененных файлов: 178 добавлений и 0 удалений
  1. 1
    0
      Lodel/settings/defaults.py
  2. 0
    0
      poc/__init__.py
  3. 3
    0
      poc/admin.py
  4. 168
    0
      poc/models.py
  5. 3
    0
      poc/tests.py
  6. 3
    0
      poc/views.py

+ 1
- 0
Lodel/settings/defaults.py Просмотреть файл

@@ -58,6 +58,7 @@ INSTALLED_APPS = (
58 58
     'django.contrib.sessions',
59 59
     'django.contrib.messages',
60 60
     'django.contrib.staticfiles',
61
+    'poc'
61 62
 )
62 63
 
63 64
 MIDDLEWARE_CLASSES = (

+ 0
- 0
poc/__init__.py Просмотреть файл


+ 3
- 0
poc/admin.py Просмотреть файл

@@ -0,0 +1,3 @@
1
+from django.contrib import admin
2
+
3
+# Register your models here.

+ 168
- 0
poc/models.py Просмотреть файл

@@ -0,0 +1,168 @@
1
+import imp
2
+import importlib
3
+import sys
4
+import types
5
+import collections
6
+
7
+import django
8
+from django.db import models
9
+from django.contrib import admin
10
+
11
+
12
+from EditorialModel.classes import EmClass
13
+from EditorialModel.types import EmType
14
+from EditorialModel.fields import EmField
15
+from EditorialModel.fieldtypes import EmFieldType
16
+
17
+##
18
+# @source https://code.djangoproject.com/wiki/DynamicModels
19
+#
20
+def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None):
21
+    class Meta:
22
+        # Using type('Meta', ...) gives a dictproxy error during model creation
23
+        pass
24
+
25
+    if app_label:
26
+        # app_label must be set using the Meta inner class
27
+        setattr(Meta, 'app_label', app_label)
28
+
29
+    # Update Meta with any options that were provided
30
+    if options is not None:
31
+        for key, value in options.iteritems():
32
+            setattr(Meta, key, value)
33
+
34
+    # Set up a dictionary to simulate declarations within a class
35
+    attrs = {'__module__': module, 'Meta':Meta}
36
+
37
+    # Add in any fields that were provided
38
+    if fields:
39
+        attrs.update(fields)
40
+
41
+    # Create the class, which automatically triggers ModelBase processing
42
+    model = type(name, (models.Model,), attrs)
43
+
44
+    # Create an Admin class if admin options were provided
45
+    if admin_opts is not None:
46
+        class Admin(admin.ModelAdmin):
47
+            pass
48
+        for key, value in admin_opts:
49
+            setattr(Admin, key, value)
50
+        admin.site.register(model, Admin)
51
+    return model
52
+
53
+
54
+module = 'poc'
55
+
56
+#Definition degueu du ME de test (degueu car orderedDict)
57
+em_example = collections.OrderedDict()
58
+
59
+em_example['publication'] = {
60
+        'fields': {
61
+            'title': {
62
+                'fieldtype': 'CharField',
63
+                'opts': {'max_length':32},
64
+            },
65
+            'doi': {
66
+                'fieldtype': 'CharField',
67
+                'opts': {'max_length':32, 'default':None},
68
+                'optionnal': True,
69
+            },
70
+        },
71
+        'types': collections.OrderedDict([
72
+            ('collection', {
73
+                'sups': ['collection'],
74
+                'opt_field': (),
75
+            }),
76
+            ('numero', {
77
+                'sups': ['collection'],
78
+                'opt_field': ('doi'),
79
+            }),
80
+        ]),
81
+    }
82
+
83
+em_example['texte'] = {
84
+        'fields': {
85
+            'title': {
86
+                'fieldtype': 'CharField',
87
+                'opts': {'max_length':32},
88
+            },
89
+            'content': {
90
+                'fieldtype': 'TextField',
91
+                'opts': {},
92
+            },
93
+            'doi': {
94
+                'fieldtype': 'CharField',
95
+                'opts': {'max_length':32},
96
+                'optionnal': True,
97
+            },
98
+        },
99
+        'types': {
100
+            'article': {
101
+                'sups': ['numero'],
102
+                'opt_field': ('doi'),
103
+            },
104
+            'preface': {
105
+                'sups': ['numero'],
106
+                'opt_field': (),
107
+            },
108
+        },
109
+    }
110
+
111
+em_example['contributor'] = {
112
+        'fields': {
113
+            'name':{
114
+                'fieldtype': 'CharField',
115
+                'opts': {'max_length':32},
116
+            },
117
+            'linked_text': {
118
+                'fieldtype': 'ManyToManyField',
119
+                'opts': {'to': 'texte'},
120
+            },
121
+        },
122
+        'types': {
123
+            'author': { 'sups': [] },
124
+            'director': { 'sups': [] },
125
+        },
126
+    }
127
+
128
+#
129
+# Fin ME
130
+#
131
+
132
+# LeObject model
133
+leobj_attr = {
134
+    'lodel_id': models.IntegerField(primary_key=True),
135
+}
136
+
137
+leobject = create_model('leobject', leobj_attr, module)
138
+
139
+# Create all the models from the EM datas
140
+def em_to_models(em):
141
+    result = {}
142
+    for cname, em_class in em.items():
143
+        cls_attr = { 'lodel_id': models.ForeignKey(leobject, primary_key=True) }
144
+        result[cname] = create_model(cname, cls_attr, module)
145
+            
146
+        #Creating type table
147
+        for tname, em_type in em_class['types'].items():
148
+            print('type : ', tname)
149
+
150
+            type_attr = { 'lodel_id': models.ForeignKey(leobject, primary_key=True)}
151
+
152
+            if 'sups' in em_type:
153
+                for superior in em_type['sups']:
154
+                    type_attr[superior+'_superior'] = models.ForeignKey( 'self' if superior == tname else result[superior])
155
+
156
+            for fname, em_field in em_class['fields'].items():
157
+                if not 'optionnal' in em_field or not em_field['optionnal'] or ('opt_field' in em_type and fname in em_type['opt_field']):
158
+                    field = getattr(models, em_field['fieldtype'])
159
+                    if 'opts' in em_field and len(em_field['opts']):
160
+                        opts = em_field['opts']
161
+                    else:
162
+                        opts = {}
163
+                    type_attr[fname] = field(**opts)
164
+            print("Debug : adding ", tname)
165
+            result[tname] = create_model(tname, type_attr, module)
166
+    return result
167
+
168
+models = em_to_models(em_example)

+ 3
- 0
poc/tests.py Просмотреть файл

@@ -0,0 +1,3 @@
1
+from django.test import TestCase
2
+
3
+# Create your tests here.

+ 3
- 0
poc/views.py Просмотреть файл

@@ -0,0 +1,3 @@
1
+from django.shortcuts import render
2
+
3
+# Create your views here.

Загрузка…
Отмена
Сохранить