3 from zlib import compress
9 from django.db import models
10 from south.db import db
11 from south.v2 import DataMigration
13 from django.conf import settings
14 from slugify import slugify
16 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
17 STAGE_TAGS_RE = re.compile(r'^#stage-finished: (.*)$', re.MULTILINE)
18 AUTHOR_RE = re.compile(r'\s*(.*?)\s*<(.*)>\s*')
24 # >>> urlunquote('Za%C5%BC%C3%B3%C5%82%C4%87_g%C4%99%C5%9Bl%C4%85_ja%C5%BA%C5%84')
25 # u'Za\u017c\xf3\u0142\u0107_g\u0119\u015bl\u0105 ja\u017a\u0144'
27 return unicode(urllib.unquote(url), 'utf-8', 'ignore')
31 parts = name.split('__')
35 def file_to_title(fname):
36 """ Returns a title-like version of a filename. """
37 parts = (p.replace('_', ' ').title() for p in fname.split('__'))
38 return ' / '.join(parts)
42 return re.sub(META_REGEX, '', text, 1)
45 def gallery(slug, text):
48 m = re.match(META_REGEX, text)
50 for line in m.group(1).split('\n'):
52 k, v = line.split(':', 1)
53 result[k.strip()] = v.strip()
57 gallery = result.get('gallery', slugify(slug))
59 if gallery.startswith('/'):
60 gallery = os.path.basename(gallery)
65 def migrate_file_from_hg(orm, fname, entry):
66 fname = urlunquote(fname)
68 if fname.endswith('.xml'):
70 title = file_to_title(fname)
71 fname = slugify(fname)
73 # create all the needed objects
74 # what if it already exists?
75 book = orm.Book.objects.create(
78 chunk = orm.Chunk.objects.create(
83 chunk.stage = orm.ChunkTag.objects.order_by('ordering')[0]
87 maxrev = entry.filerev()
90 # this will fail if directory exists
91 os.makedirs(os.path.join(settings.CATALOGUE_REPO_PATH, str(chunk.pk)))
93 for rev in xrange(maxrev + 1):
94 fctx = entry.filectx(rev)
96 gallery_link = gallery(fname, data)
97 data = plain_text(data)
99 # get tags from description
100 description = fctx.description().decode("utf-8", 'replace')
101 tags = STAGE_TAGS_RE.findall(description)
102 tags = [orm.ChunkTag.objects.get(slug=slug.strip()) for slug in tags]
105 max_ordering = max(tags, key=lambda x: x.ordering).ordering
107 chunk.stage = orm.ChunkTag.objects.filter(ordering__gt=max_ordering).order_by('ordering')[0]
111 description = STAGE_TAGS_RE.sub('', description)
113 author = author_name = author_email = None
114 author_desc = fctx.user().decode("utf-8", 'replace')
115 m = AUTHOR_RE.match(author_desc)
118 author = orm['auth.User'].objects.get(username=m.group(1), email=m.group(2))
119 except orm['auth.User'].DoesNotExist:
120 author_name = m.group(1)
121 author_email = m.group(2)
123 author_name = author_desc
125 head = orm.ChunkChange.objects.create(
128 created_at=datetime.datetime.fromtimestamp(fctx.date()[0]),
129 description=description,
131 author_name=author_name,
132 author_email=author_email,
136 path = "%d/%d" % (chunk.pk, head.pk)
137 abs_path = os.path.join(settings.CATALOGUE_REPO_PATH, path)
138 f = open(abs_path, 'wb')
139 f.write(compress(data))
150 book.gallery = gallery_link
154 class Migration(DataMigration):
156 def forwards(self, orm):
158 hg_path = settings.WIKI_REPOSITORY_PATH
160 print 'repository not configured, skipping'
162 from mercurial import hg, ui
164 print 'migrate from', hg_path
165 repo = hg.repository(ui.ui(), hg_path)
168 if fname.startswith('.'):
170 migrate_file_from_hg(orm, fname, tip[fname])
173 def backwards(self, orm):
174 "Write your backwards methods here."
180 'Meta': {'object_name': 'Group'},
181 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
182 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
183 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
186 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
187 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
188 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
189 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
190 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
193 'Meta': {'object_name': 'User'},
194 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
195 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
196 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
197 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
198 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
199 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
200 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
201 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
202 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
203 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
204 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
205 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
206 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
209 'Meta': {'ordering': "['parent_number', 'title']", 'object_name': 'Book'},
210 '_new_publishable': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
211 '_published': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
212 '_short_html': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
213 '_single': ('django.db.models.fields.NullBooleanField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
214 'gallery': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
215 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
216 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}),
217 'parent_number': ('django.db.models.fields.IntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
218 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '128', 'db_index': 'True'}),
219 'title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'})
221 'catalogue.bookpublishrecord': {
222 'Meta': {'ordering': "['-timestamp']", 'object_name': 'BookPublishRecord'},
223 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'publish_log'", 'to': "orm['catalogue.Book']"}),
224 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
225 'timestamp': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
226 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
229 'Meta': {'ordering': "['number']", 'unique_together': "[['book', 'number'], ['book', 'slug']]", 'object_name': 'Chunk'},
230 '_changed': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
231 '_hidden': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
232 '_short_html': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
233 'book': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['catalogue.Book']"}),
234 'creator': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'created_documents'", 'null': 'True', 'to': "orm['auth.User']"}),
235 'head': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['catalogue.ChunkChange']", 'null': 'True', 'blank': 'True'}),
236 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
237 'number': ('django.db.models.fields.IntegerField', [], {}),
238 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
239 'stage': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['catalogue.ChunkTag']", 'null': 'True', 'blank': 'True'}),
240 'title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
241 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
243 'catalogue.chunkchange': {
244 'Meta': {'ordering': "('created_at',)", 'unique_together': "(['tree', 'revision'],)", 'object_name': 'ChunkChange'},
245 'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
246 'author_email': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
247 'author_name': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
248 'created_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
249 'data': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}),
250 'description': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
251 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
252 'merge_parent': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'merge_children'", 'null': 'True', 'blank': 'True', 'to': "orm['catalogue.ChunkChange']"}),
253 'parent': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'children'", 'null': 'True', 'blank': 'True', 'to': "orm['catalogue.ChunkChange']"}),
254 'publishable': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
255 'revision': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}),
256 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'change_set'", 'symmetrical': 'False', 'to': "orm['catalogue.ChunkTag']"}),
257 'tree': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'change_set'", 'to': "orm['catalogue.Chunk']"})
259 'catalogue.chunkpublishrecord': {
260 'Meta': {'object_name': 'ChunkPublishRecord'},
261 'book_record': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['catalogue.BookPublishRecord']"}),
262 'change': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'publish_log'", 'to': "orm['catalogue.ChunkChange']"}),
263 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
265 'catalogue.chunktag': {
266 'Meta': {'ordering': "['ordering']", 'object_name': 'ChunkTag'},
267 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
268 'name': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
269 'ordering': ('django.db.models.fields.IntegerField', [], {}),
270 'slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '64', 'unique': 'True', 'null': 'True', 'blank': 'True'})
272 'contenttypes.contenttype': {
273 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
274 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
275 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
276 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
277 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
281 complete_apps = ['catalogue']