Fix for a unicode fix.
[redakcja.git] / scripts / import_image.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import sys
7 sys.path.append('.')
8 sys.path.append('./apps')
9 sys.path.append('./lib')
10
11 from django.core.management import setup_environ
12 from redakcja import settings
13 from redakcja import localsettings
14
15 setup_environ(settings)
16 settings.CATALOGUE_REPO_PATH = localsettings.CATALOGUE_REPO_PATH
17 settings.CATALOGUE_IMAGE_REPO_PATH = localsettings.CATALOGUE_IMAGE_REPO_PATH
18 settings.MEDIA_ROOT = localsettings.MEDIA_ROOT
19 settings.STATIC_ROOT = localsettings.STATIC_ROOT
20
21
22 from catalogue.models import  Image
23 from django.core.files import File
24 import re
25 from os import path
26 from django.contrib.auth.models import User
27 from django.conf import settings
28
29
30
31 user = {
32     'obj': User.objects.get(username='marcinkoziej'),
33     'name': 'Marcin Koziej',
34     'email': 'marcinkoziej@nowoczesnapolska.org.pl'
35     }
36
37 files = sys.argv[1:]
38
39 xml = open(path.dirname(__file__)+"/image.xml").read().decode('utf-8')
40
41 for filename in files:
42     dfile = File(open(filename))
43     img = Image()
44     name = path.splitext(path.basename(filename))[0]
45     print filename, name
46     try:
47         old = Image.objects.get(slug=name)
48         print "deleting old %s" % name
49         old.delete()
50     except:
51         pass
52     
53
54     img.slug = name
55     img.title = name
56     img.image.save(filename, dfile)
57
58     img.save()
59     img.commit(xml, author=user['obj'], author_name=user['name'], author_email=user['email'])
60
61
62