Merge branch 'redakcja-api'
[wolnelektury.git] / scripts / import_links.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.insert(0, '../apps')
8 sys.path.insert(0, '../lib')
9 sys.path.insert(0, '../wolnelektury')
10 sys.path.insert(0, '..')
11
12 from django.core.management import setup_environ
13 from wolnelektury import settings
14 import sys
15
16 setup_environ(settings)
17
18 from catalogue.models import Book, Tag
19
20
21 def import_links(file_name, attribute):
22     for line in file(file_name):
23         slug, link = line.split()
24         link = link.strip('\n')
25         try:
26             book = Book.objects.get(slug=slug)
27             setattr(book, attribute, link)
28             book.save()
29             print 'Link %s for book %s added!' % (link, book)
30         except Book.DoesNotExist:
31             try:
32                 tag = Tag.objects.get(slug=slug)
33                 setattr(tag, attribute, link)
34                 tag.save()
35                 print 'Link %s for tag %s added!' % (link, tag)
36             except Tag.DoesNotExist:
37                 print 'Invalid slug %s!' % slug
38
39
40 import_links('gazeta-links', 'gazeta_link')
41 import_links('wiki-links', 'wiki_link')