From 62fb95df6ef88f5b0d622046c8c22be730b64817 Mon Sep 17 00:00:00 2001 From: Robert Tice Date: Mon, 27 Nov 2017 17:44:27 -0500 Subject: [PATCH] Removing static book_list.yaml and creating workflow like we have for yannicks code examples. README and design notes have been updated with info. An example book is sitting in resources/books. --- README.md | 5 ++ .../app/management/commands/fill_books.py | 59 +++++++++++++++++++ compile_server/app/models.py | 18 ++++++ compile_server/app/serializers.py | 8 ++- compile_server/app/templates/book_list.html | 13 +++- compile_server/app/templates/readerpage.html | 8 +-- compile_server/app/views.py | 46 +++++++-------- design/notes.txt | 13 ++++ resources/books/book_list.yaml | 7 --- resources/books/example/chapters.yaml | 18 ++++++ .../books/example/pages/part1-chapter1.md | 12 ++++ .../books/example/pages/part1-chapter2.rst | 12 ++++ .../books/example/pages/part2-chapter1.md | 12 ++++ .../books/example/pages/part2-chapter2.rst | 12 ++++ 14 files changed, 205 insertions(+), 38 deletions(-) create mode 100644 compile_server/app/management/commands/fill_books.py delete mode 100644 resources/books/book_list.yaml create mode 100644 resources/books/example/chapters.yaml create mode 100644 resources/books/example/pages/part1-chapter1.md create mode 100644 resources/books/example/pages/part1-chapter2.rst create mode 100644 resources/books/example/pages/part2-chapter1.md create mode 100644 resources/books/example/pages/part2-chapter2.rst diff --git a/README.md b/README.md index fba5175..a6146bd 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ To enter some examples in the database, do this: ./manage.py fill_examples --dir=resources/example/hello_world ``` +To enter some books in the database, do this: +```sh +./manage.py fill_books --dir=resources/books/example +``` + To launch the server, do this: ```sh ./manage.py runserver diff --git a/compile_server/app/management/commands/fill_books.py b/compile_server/app/management/commands/fill_books.py new file mode 100644 index 0000000..a8a30a1 --- /dev/null +++ b/compile_server/app/management/commands/fill_books.py @@ -0,0 +1,59 @@ +# The manage.py command to enter books in the database. +# This is meant to be used by the administrators of the project only. + +import os +import yaml +from django.core.management.base import BaseCommand + +from compile_server.app.models import Book + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('--remove_all', const=True, default=False, + action='store_const', + help='remove all books from the database') + + parser.add_argument('--dir', nargs=1, type=str, + help='add the book found in the given dir') + + def handle(self, *args, **options): + + if options.get('remove_all', False): + # Remove all books from the database + Book.objects.all().delete() + + if options.get('dir', None): + d = os.path.abspath(options.get('dir')[0]) + + # Consider directory as the book + if not os.path.isdir(d): + print "{} is not a valid directory".format(d) + return + + # Look for 'chapters.yaml' + chapters_yaml = os.path.join(d, 'chapters.yaml') + if not os.path.isfile(chapters_yaml): + print 'There is no "chapters.yaml" in {}'.format(d) + return + + # Check contents of chapters.yaml + with open(chapters_yaml, 'rb') as f: + try: + metadata = yaml.load(f.read()) + except: + print format_traceback + print 'Could not decode yaml in {}'.format(chapters_yaml) + return + for field in ['title', 'description']: + if field not in metadata: + print 'chapters.yaml should contain a field {}'.format( + field) + return + + b = Book(description=metadata['description'], + directory=d, + subpath=os.path.basename(os.path.normpath(d)), + title=metadata['title']) + b.save() diff --git a/compile_server/app/models.py b/compile_server/app/models.py index cdf7923..eee203b 100644 --- a/compile_server/app/models.py +++ b/compile_server/app/models.py @@ -48,3 +48,21 @@ class Example(models.Model): # An example is a contains a set of resources resources = models.ManyToManyField(Resource) + + +class Book(models.Model): + """ The represents a book """ + + # the directory in relation to resources/books + # that has the resources for this book + directory = models.TextField() + + # This is the name of the book formatted as a url subdomain + # This is be the same name of the folder where the book lives under resources + subpath = models.TextField() + + # A description of the book + description = models.TextField() + + # the title of the book + title = models.TextField() \ No newline at end of file diff --git a/compile_server/app/serializers.py b/compile_server/app/serializers.py index 2653296..69e3577 100644 --- a/compile_server/app/serializers.py +++ b/compile_server/app/serializers.py @@ -1,6 +1,6 @@ from django.contrib.auth.models import User, Group from rest_framework import serializers -from compile_server.app.models import Resource, Example +from compile_server.app.models import Resource, Example, Book class UserSerializer(serializers.HyperlinkedModelSerializer): @@ -37,3 +37,9 @@ class ExampleSerializer(serializers.ModelSerializer): class Meta: model = Example fields = ('name', 'description', 'main') + + +class BookSerializer(serializers.ModelSerializer): + class Meta: + model = Book + fields = ('title', 'description', 'directory', 'subpath') diff --git a/compile_server/app/templates/book_list.html b/compile_server/app/templates/book_list.html index b6569b2..881b0fe 100644 --- a/compile_server/app/templates/book_list.html +++ b/compile_server/app/templates/book_list.html @@ -19,8 +19,9 @@ {% for b in books %} - - {{ b.title }} + + {{ b.title }} + {{ b.description }} {% endfor %} @@ -33,6 +34,14 @@ + + diff --git a/compile_server/app/templates/readerpage.html b/compile_server/app/templates/readerpage.html index 4fbac0d..799f140 100644 --- a/compile_server/app/templates/readerpage.html +++ b/compile_server/app/templates/readerpage.html @@ -6,7 +6,7 @@ {% block sidebar %}