- for field in ('slug', 'title', 'description',
- 'extra_info', 'gazeta_link', 'wiki_link'):
- if getattr(book, field):
- obj[field] = getattr(book, field)
- for field in ('created_at', 'changed_at'):
- obj[field] = timestamp(getattr(book, field))
- for field in ('xml', 'epub', 'txt', 'pdf', 'html'):
- f = getattr(book, field+'_file')
- if f:
- obj[field] = f.url
- for media in book.medias.all():
- obj.setdefault(media.type, []).append(media.file.url)
- if book.parent:
- obj['parent'] = book.parent.id
- obj['parent_number'] = book.parent_number
- if fields is not None:
- for key in obj.keys():
- if key not in fields:
- del obj[key]
-
- # if there's still extra_info, we can parse it
- if 'extra_info' in obj:
- extra = json.loads(obj['extra_info'])
- if extra_fields is not None:
- for key in extra.keys():
- if key not in extra_fields:
- del extra[key]
- obj['extra_info'] = extra
+ for field in fields:
+
+ if field in ('xml', 'epub', 'txt', 'pdf', 'html'):
+ f = getattr(book, field+'_file')
+ if f:
+ obj[field] = {
+ 'url': f.url,
+ 'size': f.size,
+ }
+
+ elif field in ('mp3', 'ogg', 'daisy'):
+ media = []
+ for m in book.media.filter(type=field):
+ media.append({
+ 'url': m.file.url,
+ 'size': m.file.size,
+ })
+ if media:
+ obj[field] = media
+
+ elif field == 'url':
+ obj[field] = book.get_absolute_url()
+
+ elif field == 'tags':
+ obj[field] = [t.id for t in book.tags.exclude(category__in=('book', 'set'))]
+
+ elif field == 'author':
+ obj[field] = ", ".join(t.name for t in book.tags.filter(category='author'))
+
+ elif field == 'parent':
+ obj[field] = book.parent_id
+
+ elif field in ('license', 'license_description', 'source_name',
+ 'technical_editors', 'editors'):
+ f = extra_info.get(field)
+ if f:
+ obj[field] = f
+
+ else:
+ f = getattr(book, field)
+ if f:
+ obj[field] = f