Пример #1
0
 def get(self, page_id):
     uid = self.request.get('uid')
     token = self.request.get('token')
     user = AcceptAuthorshipToken.get_user_from_token_string(token)
     valid = user is not None
     if page_id is not None:
         page_id = Page.get_long_uid(page_id)
         page = self.api.get_by_id(page_id)
         authors = page.authors
         # Add to authors if token is valid
         if valid and uid not in authors:
             authors.append(uid)
             page.authors = authors
             page.put()
             AcceptAuthorshipToken.delete(token)
             self.redirect('/pages/{}'.format(page_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'
             }))
Пример #2
0
    def get(self, page_id=None):
        user = self.get_current_user()
        full_chapter_id = ""
        book_id = ""
        if user is None:
            self.redirect('/discover')
            return

        # Check if user is practice creator or admin

        page_json = "''"
        if page_id:
            page = self.api.get_by_id(Page.get_long_uid(page_id))
            pageDict = page.to_client_dict()
            if page.icon:
                icon_path = util.extract_value_from_json(page.icon, 'link')
                pageDict[
                    'iconPath'] = icon_path + '?size=360' if icon_path else icon_path
            page_json = json.dumps(pageDict)

        if not user.is_admin and page_id:
            if user.uid not in page.authors:
                self.redirect('/pages/{}'.format(page_id))

        chapter_id = self.request.get('chapter_id')
        if chapter_id:
            full_chapter_id = Chapter.get_long_uid(chapter_id)
            book_id = Chapter.get_by_id(full_chapter_id).books[0]

        self.write(
            'page-upload.html',
            page_id=page_id,
            page_json=page_json,
            chapter_id=full_chapter_id,
            book_id=book_id,
        )
Пример #3
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',
        )
Пример #4
0
    def get(self, 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'] = u'{} {}'.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 not page:
            # 404 if theme cannot be found
            return self.http_not_found()

        # 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)

        relatedPages = []
        if len(page.related_pages) > 0:
            relatedPages = Page.get_by_id(page.related_pages)

        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:]

        self.write(
            'page.html',
            page=page,
            page_json=json.dumps(page.to_client_dict()),
            related_pages=relatedPages,
            authors=authors,
            authors_json=json.dumps(authors),
            first_author=creator,
            creator=creator,
            additional_authors=additional_authors,
            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=None,
            is_author=is_author or (user and user.is_admin),
            is_admin=user and user.is_admin,
            entity_type='page',
        )