Example #1
0
    def show(self, id):
        """Return a collection.
        
        :URL: ``GET /collections/id``
        :param str id: the ``id`` value of the collection to be returned.
        :returns: a collection model object.

        .. note::

            Returns all of the forms of the collection, unlike the other
            collections actions.

            If there is a truthy GET param with key 'latex' and if the markup
            language is reStructuredText, then the collection's
            contents_unpacked value will be returned as a LaTeX string in the
            'latex' attribute.

        """

        collection = h.eagerload_collection(Session.query(Collection),
                                            eagerload_forms=True).get(id)
        if collection:
            unrestricted_users = h.get_unrestricted_users()
            user = session['user']
            if h.user_is_authorized_to_access_model(user, collection,
                                                    unrestricted_users):
                result = collection.get_full_dict()
                # TODO: deal with markdown2latex ...
                if request.GET.get('latex') and \
                collection.markup_language == 'reStructuredText':
                    result['latex'] = h.rst2latex(collection.contents_unpacked)
                return result
            else:
                response.status_int = 403
                return h.unauthorized_msg
        else:
            response.status_int = 404
            return {'error': 'There is no collection with id %s' % id}
Example #2
0
    def show(self, id):
        """Return a collection.
        
        :URL: ``GET /collections/id``
        :param str id: the ``id`` value of the collection to be returned.
        :returns: a collection model object.

        .. note::

            Returns all of the forms of the collection, unlike the other
            collections actions.

            If there is a truthy GET param with key 'latex' and if the markup
            language is reStructuredText, then the collection's
            contents_unpacked value will be returned as a LaTeX string in the
            'latex' attribute.

        """

        collection = h.eagerload_collection(Session.query(Collection),
                                           eagerload_forms=True).get(id)
        if collection:
            unrestricted_users = h.get_unrestricted_users()
            user = session['user']
            if h.user_is_authorized_to_access_model(user, collection, unrestricted_users):
                result = collection.get_full_dict()
                # TODO: deal with markdown2latex ...
                if request.GET.get('latex') and \
                collection.markup_language == 'reStructuredText':
                    result['latex'] = h.rst2latex(collection.contents_unpacked)
                return result
            else:
                response.status_int = 403
                return h.unauthorized_msg
        else:
            response.status_int = 404
            return {'error': 'There is no collection with id %s' % id}
Example #3
0
        def exporter_function(input_):
            def get_form_from_list_of_forms(form_list, match_obj):
                """Input is a list of Forms and a re Match object.  (The form_id will be
                recoverable as the first backreference of the Match object.)  The output
                is the Form in the list whose ID is the ID extracted from the match_obj

                """

                form_id = int(match_obj.group(1))
                form_dict = dict((form.id, form) for form in form_list)
                return form_dict.get(form_id, None)

            # Get the Collection's contents data
            collection = self.get_collection(input_)
            content = collection.contents

            # Convert to LaTeX (actually, a hacky XeLaTeX-processable document)
            content = h.rst2latex(content, igt_package=igt_package)

            # Insert a title into the (Xe)LaTeX string
            date_modified = collection.datetimeModified.strftime("%b %d, %Y")
            title = (
                "\\title{%s}\n\\author{\\textit{entered by:} %s \\\\ \n \
                \\textit{exported by:} %s} \n\\date{%s}\n\\maketitle"
                % (
                    collection.title,
                    "%s %s" % (collection.enterer.firstName, collection.enterer.lastName),
                    "%s %s" % (session["user_firstName"], session["user_lastName"]),
                    date_modified,
                )
            )
            content = content.replace("\\begin{document}", "\\begin{document}\n\n%s" % title)

            # Replace Form references with Covington-styled IGT examples
            patt = re.compile("[Ff]orm\{\[\}([0-9]+)\{\]\}")
            return patt.sub(lambda x: form_formatter(get_form_from_list_of_forms(collection.forms, x)), content)
Example #4
0
        def exporter_function(input_):
            def get_form_from_list_of_forms(form_list, match_obj):
                """Input is a list of Forms and a re Match object.  (The form_id will be
                recoverable as the first backreference of the Match object.)  The output
                is the Form in the list whose ID is the ID extracted from the match_obj

                """

                form_id = int(match_obj.group(1))
                form_dict = dict((form.id, form) for form in form_list)
                return form_dict.get(form_id, None)

            # Get the Collection's contents data
            collection = self.get_collection(input_)
            content = collection.contents

            # Convert to LaTeX (actually, a hacky XeLaTeX-processable document)
            content = h.rst2latex(content, igt_package=igt_package)

            # Insert a title into the (Xe)LaTeX string
            date_modified = collection.datetimeModified.strftime('%b %d, %Y')
            title = '\\title{%s}\n\\author{\\textit{entered by:} %s \\\\ \n \
                \\textit{exported by:} %s} \n\\date{%s}\n\\maketitle' % (
                collection.title, '%s %s' %
                (collection.enterer.firstName, collection.enterer.lastName),
                '%s %s' %
                (session['user_firstName'], session['user_lastName']),
                date_modified)
            content = content.replace('\\begin{document}', '\\begin{document}\n\n%s' % \
                                      title)

            # Replace Form references with Covington-styled IGT examples
            patt = re.compile('[Ff]orm\{\[\}([0-9]+)\{\]\}')
            return patt.sub(
                lambda x: form_formatter(
                    get_form_from_list_of_forms(collection.forms, x)), content)