1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from datetime import datetime
10 from django.core.management.base import BaseCommand
12 from api.helpers import timestamp
13 from api.settings import MOBILE_INIT_DB
14 from catalogue.models import Book, Tag
17 class Command(BaseCommand):
18 help = 'Creates an initial SQLite file for the mobile app.'
20 def handle(self, **options):
21 # those should be versioned
22 last_checked = timestamp(datetime.now())
23 db = init_db(last_checked)
24 for b in Book.objects.all():
26 for t in Tag.objects.exclude(category__in=('book', 'set', 'theme')):
27 # only add non-empty tags
35 def pretty_size(size):
36 """ Turns size in bytes into a prettier string.
38 >>> pretty_size(100000)
43 units = ['B', 'KiB', 'MiB', 'GiB']
46 while size > 1000 and units:
50 return "%.1f %s" % (size, unit)
51 return "%d %s" % (size, unit)
54 if not isinstance(value, unicode):
55 value = unicode(value, 'utf-8')
57 # try to replace chars
58 value = re.sub('[^a-zA-Z0-9\\s\\-]{1}', replace_char, value)
60 value = re.sub(r'[^a-z0-9{|}]+', '~', value)
62 return value.encode('ascii', 'ignore')
66 def init_db(last_checked):
67 if not os.path.isdir(MOBILE_INIT_DB):
68 os.makedirs(MOBILE_INIT_DB)
69 db = sqlite3.connect(os.path.join(MOBILE_INIT_DB, 'initial.db-%d' % last_checked))
73 id INTEGER PRIMARY KEY,
76 html_file_size INTEGER,
78 parent_number INTEGER,
85 CREATE INDEX IF NOT EXISTS book_title_index ON book (sort_key);
86 CREATE INDEX IF NOT EXISTS book_title_index ON book (title);
87 CREATE INDEX IF NOT EXISTS book_parent_index ON book (parent);
90 id INTEGER PRIMARY KEY,
95 CREATE INDEX IF NOT EXISTS tag_name_index ON tag (name);
96 CREATE INDEX IF NOT EXISTS tag_category_index ON tag (category);
97 CREATE INDEX IF NOT EXISTS tag_sort_key_index ON tag (sort_key);
99 CREATE TABLE state (last_checked INTEGER);
102 db.executescript(schema)
103 db.execute("INSERT INTO state VALUES (:last_checked)", locals())
107 def current(last_checked):
108 target = os.path.join(MOBILE_INIT_DB, 'initial.db')
111 'initial.db-%d' % last_checked,
119 (id, title, html_file, html_file_size, parent, parent_number, sort_key, pretty_size, authors)
121 (:id, :title, :html_file, :html_file_size, :parent, :parent_number, :sort_key, :size_str, :authors);
123 book_tag_sql = "INSERT INTO book_tag (book, tag) VALUES (:book, :tag);"
126 (id, category, name, sort_key, books)
128 (:id, :category, :name, :sort_key, :book_ids);
130 categories = {'author': 'autor',
138 def add_book(db, book):
142 html_file = book.html_file.url
143 html_file_size = book.html_file.size
145 html_file = html_file_size = None
146 parent = book.parent_id
147 parent_number = book.parent_number
148 sort_key = book.sort_key
149 size_str = pretty_size(html_file_size)
150 authors = ", ".join(t.name for t in book.tags.filter(category='author'))
151 db.execute(book_sql, locals())
154 def add_tag(db, tag):
156 category = categories[tag.category]
158 sort_key = tag.sort_key
160 books = Book.tagged_top_level([tag])
161 book_ids = ','.join(str(b.id) for b in books)
162 db.execute(tag_sql, locals())