Ejemplo n.º 1
0
    def get(self, book_id=None):
        user = self.get_current_user()
        expand = ''
        if user is None:
            self.redirect('/books/{}'.format(book_id))
            return

        if book_id is not None:
            book_id = Book.get_long_uid(book_id)
            book = self.api.get_by_id(book_id)

            if not user.is_admin and user.uid not in book.authors:
                self.redirect('/books/{}'.format(book_id))

            book.book_image = util.extract_value_from_json(
                book.book_image, 'link')
            icon_link = util.extract_value_from_json(book.icon, 'link')
            book.icon = icon_link + '?size=360' if icon_link else icon_link
            book_json = json.dumps(book.to_client_dict())

        expand = Chapter.get_long_uid(self.request.get('expand'))

        self.write(
            'book-manage.html',
            book=book.to_client_dict(),
            book_json=book_json,
            expand=expand,
        )
Ejemplo n.º 2
0
 def get(self, book_id):
     uid = self.request.get('uid')
     token = self.request.get('token')
     user = AcceptAuthorshipToken.get_user_from_token_string(token)
     valid = user is not None
     logging.info('book_id={}, uid={}, token={}, user={}'.format(
         book_id, uid, token, user))
     if book_id is not None:
         book_id = Book.get_long_uid(book_id)
         book = self.api.get_by_id(book_id)
         authors = book.authors
         # Add to authors if token is valid
         if valid and uid not in authors:
             authors.append(uid)
             book.authors = authors
             book.put()
             AcceptAuthorshipToken.delete(token)
             self.redirect('/books/{}'.format(book_id), permanent=True)
         else:
             self.write('404.html', message='invalid token')
             # self.response.write(json.dumps(
             #     {'error': True, 'message': 'invalid token'}))
     else:
         self.response.write(
             json.dumps({
                 'error': True,
                 'message': 'invalid parameters'
             }))
Ejemplo n.º 3
0
 def head(self, book_id):
     # Include an override here to not confuse bad subdirectories as 200 OK
     id = Book.get_long_uid(book_id)
     book = self.api.get_by_id(id)
     if book is not None:
         self.response.clear()
     else:
         self.error(404)
Ejemplo n.º 4
0
 def options(self, book_id):
     # Include an override here to not confuse bad subdirectories as 200 OK
     id = Book.get_long_uid(book_id)
     book = self.api.get_by_id(id)
     if book is not None:
         self.response.set_status(200)
         self.response.headers['Allow'] = 'GET,HEAD,OPTIONS'
     else:
         self.error(404)
Ejemplo n.º 5
0
    def get(self, book_id, chapter_id, page_id):
        # Add info to dicts that assist templates with displaying correct UI elements.
        # Takes data param (page data) and a property_dict, the flat dictionary
        # that contains metadata for the content fields to display in this view.
        def process_property_dicts(data, property_dict):
            val = getattr(data, property_dict['data_prop'])
            if (val == property_dict['default_value']):
                property_dict['is_empty'] = True
                # set display value even though it will be hidden
                property_dict['display_value'] = property_dict['default_value']
            else:
                property_dict['is_empty'] = False
                has_suffix = property_dict['value_suffix'] is not None
                property_dict['display_value'] = '{} {}'.format(
                    val, property_dict['value_suffix']) if has_suffix else val

        id = Page.get_long_uid(page_id)
        page = self.api.get_by_id(id)
        authors = []
        if page:
            # These lists of dictionaries are used to store metadata about the
            # page's display fields.
            # - data_prop and scope_prop are used to help map angular values with
            #   their respective data values.
            # - value_suffix can be given a value to add a simple string to the end
            #   of the display value.
            # - Note: before being passed to the template, these dicts can/are
            #   expected to be further processed by adding data (such as the final
            #   display value)
            ui_props = page.ui_props.copy()
            first_section_keys = (
                'time_required',
                'required_materials',
                'preconditions_for_success',
                'advances_equity',
                'evidence_of_effectiveness',
            )
            first_section_props = [ui_props[k] for k in first_section_keys]
            second_section_keys = (
                'associated_measures',
                'acknowledgements',
                'preferred_citation',
            )
            second_section_props = [ui_props[k] for k in second_section_keys]

            # Increment view counts on the practice
            view_counter.increment(id)

            # TODO: Refactor this. Not sure what kind of author data we want to display/send to template yet.
            if page.authors:
                authors_data = User.get_by_id(page.authors)
                for a in authors_data:
                    authors.append(a.to_client_dict())

            display_first_section_heading = False
            for property_dict in first_section_props:
                process_property_dicts(page, property_dict)
                if not property_dict['is_empty']:
                    display_first_section_heading = True

            for property_dict in second_section_props:
                process_property_dicts(page, property_dict)

            last_editor = User.get_by_id(
                page.last_edited_by) if page.last_edited_by else None
            user = self.get_current_user()
            is_author = user.uid in page.authors if user is not None else False

        full_book_id = Book.get_long_uid(book_id)
        book = self.api.get_by_id(full_book_id)

        full_chapter_id = Chapter.get_long_uid(chapter_id)
        chapter = self.api.get_by_id(full_chapter_id)

        full_page_id = Page.get_long_uid(page_id)
        page = self.api.get_by_id(full_page_id)

        creator = authors[0]
        last_editor = User.get_by_id(
            page.last_edited_by) if page.last_edited_by else None
        user = self.get_current_user()
        is_author = user.uid in page.authors if user is not None else False
        additional_authors = authors[1:].remove(
            last_editor
        ) if last_editor and last_editor in authors[1:] else authors[1:]
        breadcrumb_title = book.title + ' / ' + chapter.title

        # check all content objects were found
        if page is None or chapter is None or book is None:
            return self.http_not_found()

        # Increment view counts on the page
        view_counter.increment(full_page_id)
        view_counter.increment('{}:{}:{}'.format(full_book_id, full_chapter_id,
                                                 full_page_id))

        authors = []
        if page.authors:
            authors_data = User.get_by_id(page.authors)
            for a in authors_data:
                authors.append(a.to_client_dict())

        # Get other chapters in book for navigating
        chapters = []
        if book.chapters:
            chapters = self.api.get_by_id(book.chapters)

        # get other pages in chapter for navigating
        pages = []
        # first check for bad chapter--page match
        if chapter.pages:
            pages = self.api.get_by_id(chapter.pages)

            # get chapter index and previous and next lessons
            page_index = 0
            chapter_index = 0
            if chapter.uid in book.chapters:
                chapter_index = book.chapters.index(chapter.uid)
            if page.uid in chapter.pages:
                page_index = chapter.pages.index(page.uid)

            # get next chapter from current or next topic
            next_page = ''
            next_page_url = ''
            next_chapter = ''
            next_chapter_url = ''
            next_url = ''
            if page_index < len(pages) - 1:
                next_page = pages[page_index + 1]
                next_page_url = '/books/{}/{}/{}'.format(
                    book.short_uid, chapter.short_uid, next_page.short_uid)
                next_url = next_page_url
            elif chapter_index < len(book.chapters) - 1:
                next_chapter = chapters[chapter_index + 1]
                next_chapter_url = '/books/{}/{}'.format(
                    book.short_uid, next_chapter.short_uid)
                next_url = next_chapter_url

        # Get translated text and locale
        if book.locale in config.available_locales:
            locale = book.locale
        else:
            locale = config.default_locale

        color = '#58b070'  #TODO

        self.write(
            '/page.html'.format(page.short_uid),
            book=book,
            chapter=chapter,
            page=page,
            pages=pages,
            page_index=page_index,
            next_page=next_page,
            next_page_url=next_page_url,
            next_chapter=next_chapter,
            next_chapter_url=next_chapter_url,
            next_url=next_url,
            color=color,
            audience='',
            locale=locale,
            translation=locales.translations[locale]["pages"],
            creator=creator,
            last_editor=last_editor,
            tags=page.tags,
            license=page.use_license,
            first_section_props=first_section_props,
            second_section_props=second_section_props,
            display_first_section_heading=display_first_section_heading,
            breadcrumb_title=breadcrumb_title,
            is_author=is_author or (user and user.is_admin),
            is_admin=user and user.is_admin,
            additional_authors=additional_authors,
            entity_type='page',
        )
Ejemplo n.º 6
0
    def get(self, book_id):
        user = self.get_current_user()
        section_props = [{
            'default_value': '',
            'data_prop': 'acknowledgements',
            'scope_prop': 'pageAcknowledgements',
            'heading_title': 'Acknowledgements',
            'value_suffix': None,
        }, {
            'default_value': '',
            'data_prop': 'preferred_citation',
            'scope_prop': 'pageCitations',
            'heading_title': 'Preferred Citation',
            'value_suffix': None,
        }]
        id = Book.get_long_uid(book_id)
        book = self.api.get_by_id(id)
        first_chapter_link = ''
        first_page_link = ''
        authors = []
        if book is not None:
            if book.authors:
                authors_data = User.get_by_id(book.authors)
                for a in authors_data:
                    authors.append(a.to_client_dict())
            chapter_page_data = book.get_chapters_with_pages()
            pages_count = 0
            if book.chapters:
                for chapter in chapter_page_data:
                    pages_count += len(chapter['pages'])
                    # Only pass page data if they are an author
                    chapter_pages = []
                    for chapter_page in chapter['pages']:
                        if chapter_page['status'] == 'approved':
                            chapter_pages.append(chapter_page)
                        elif user and (user.is_admin
                                       or user.uid in chapter_page['authors']):
                            chapter_pages.append(chapter_page)
                    chapter.pages = chapter_pages
                first_page_link = '/books/{}'.format(book.short_uid)
                has_first_page = chapter_page_data and chapter_page_data[
                    0] and chapter_page_data[0]['pages']
                book.page_count = pages_count
                if has_first_page:
                    first_page_link = '/{}/{}/{}'.format(
                        first_page_link, chapter_page_data[0]['short_uid'],
                        chapter_page_data[0]['pages'][0]['short_uid'])
            for file_prop in Book.file_props:
                json_file = getattr(book, file_prop)
                setattr(book, file_prop,
                        util.extract_value_from_json(json_file, 'link'))

            book.color = '#58b070'
            #TODO
            book.target_audience = 'TODO'
            book.estimated_duration = 9999
            #TODO: should you just sum pages durations? Any default value?

            # Get translated text and locale
            if book.locale in config.available_locales:
                locale = book.locale
            else:
                locale = default_locale

            locale = 'en'

            creator = authors[0]
            last_editor = User.get_by_id(
                book.last_edited_by) if book.last_edited_by else None
            user = self.get_current_user()
            is_author = user.uid in book.authors if user is not None else False
            additional_authors = authors[1:].remove(
                last_editor
            ) if last_editor and last_editor in authors[1:] else authors[1:]

            self.write(
                'book-page.html',
                book=book,
                first_page_link=first_page_link,
                locale=locale,
                translation=locales.translations[locale]["books"],
                chapters_json=json.dumps(book.chapters),
                chapter_page_data=chapter_page_data,
                chapter_page_data_json=json.dumps(chapter_page_data),
                section_props=section_props,
                last_editor=last_editor,
                creator=creator,
                additional_authors=additional_authors,
                is_author=is_author or (user and user.is_admin),
                is_admin=user and user.is_admin,
                entity_type='book',
            )
        else:
            return self.http_not_found()