Ejemplo n.º 1
0
    def post(self):
        template = dao.get_template_by_id(self.request.get(u'template_id'))
        if not dao.test_template_permissions(
                template, [dao.TEMPLATE_OWN, dao.TEMPLATE_EDIT]):
            webapp2.abort(401)
        error_msg = None

        # Handle contribute request
        if self.request.get(u'contribute'):
            try:
                name = self.request.get(u'name').strip()
                if not name:
                    raise Exception(
                        u'You must provide a name for the public template')
                if dao.get_public_template_by_name(name):
                    raise Exception(u'Sorry, that name is taken')

                new_template = dao.Project(
                    name=name,
                    project_type=dao.PUBLIC_TEMPLATE,
                    status=dao.STATUS_PENDING,
                    description=self.request.get(u'description'))
                new_template.put()

                for assignment_entity in dao.get_assignments(template):
                    assignment_entity.clone(new_template).put()

                for document_entity in dao.get_documents(template):
                    template_document_entity = document_entity.clone(
                        new_template)
                    template_document_entity.put()
                    for document_item_entity in dao.get_document_items(
                            document_entity):
                        document_item_entity.clone(
                            template_document_entity).put()

                for interview_entity in dao.get_interviews(template):
                    cloned_interview_entity = interview_entity.clone(
                        new_template)
                    cloned_interview_entity.put()

                for style_entity in dao.get_styles(template):
                    style_entity.clone(new_template).put()

                for variable_entity in dao.get_variables(template):
                    variable_entity.clone(new_template).put()

                self.redirect("/template?template_id={}".format(
                    template.key.id()))
                return
            except Exception as e:
                error_msg = u'Contributing project failed: {}'.format(e)

        # Display the webpage
        self.render(template, error_msg)
Ejemplo n.º 2
0
 def get_documents(self, template, variable):
     documents = u''
     variable_name = variable["name"]
     for document in dao.get_documents(template):
         for document_item in dao.get_document_items(document):
             if document_item.variable_name == variable_name:
                 if len(documents):
                     documents += u', '
                 documents += u'<a href="/template/document/structure?template_id={}&document_id={}">{}</a>'.format(
                     template.key.id(), document.key.id(), document.name)
     return documents
Ejemplo n.º 3
0
 def get_documents(self, project, variable):
     documents = u''
     variable_name = variable["name"]
     for document in dao.get_documents(project):
         for document_item in dao.get_document_items(document):
             if document_item.variable_name == variable_name:
                 if len(documents):
                     documents += u', '
                 documents += u'<a href="/project/document/structure?project_id={}&document_id={}">{}</a>'.format(
                     project.key.id(), document.key.id(), document.name)
     return documents
Ejemplo n.º 4
0
    def render(self, template, document_entity, error_msg):
        # Create template and template values, render the page
        jinja_template = ui.get_template(self, u'template/document/structure.html')

        jinja_template_values = dao.get_standard_template_values(template)
        jinja_template_values[u'error_msg'] = error_msg
        jinja_template_values[u'document_id'] = document_entity.key.id()
        jinja_template_values[u'name'] = document_entity.name
        jinja_template_values[u'items'] = dao.get_document_items(document_entity)
        jinja_template_values[u'single_names'] = dao.get_single_names(template)
        jinja_template_values[u'repeating_names'] = dao.get_repeating_names(template)
        jinja_template_values[u'style_names'] = dao.get_style_names(template)

        self.response.out.write(jinja_template.render(jinja_template_values))
Ejemplo n.º 5
0
    def post(self):
        template = dao.get_template_by_id(self.request.get(u'template_id'))
        if not dao.test_template_permissions(template, [dao.TEMPLATE_OWN, dao.TEMPLATE_EDIT]):
            webapp2.abort(401)

        document_entity = dao.get_document_by_id(template, self.request.get(u'document_id'))
        error_msg = None if document_entity else u'Unable to access specified document'

        if document_entity:
            # Handle add request
            item_type = None
            if self.request.get(u'add_text'):
                item_type = dao.TEXT
            elif self.request.get(u'add_single_variable'):
                item_type = dao.SINGLE_VARIABLE
            elif self.request.get(u'add_repeating_variable'):
                item_type = dao.REPEATING_VARIABLE
            if item_type:
                try:
                    dao.DocumentItem(item_type=item_type, position=dao.get_document_item_count(document_entity) + 1,
                                     parent=document_entity.key).put()
                except Exception as e:
                    error_msg = u'Adding document item failed: {}'.format(e)

            # Handle request
            items = [item for item in dao.get_document_items(document_entity)]
            # Update items
            for item in items:
                index = item.position - 1
                if item.item_type == dao.SINGLE_VARIABLE:
                    item.variable_name = self.request.get(u'single_name.{}'.format(index))
                elif item.item_type == dao.REPEATING_VARIABLE:
                    item.variable_name = self.request.get(u'repeating_name.{}'.format(index))
                elif item.item_type == dao.TEXT:
                    item.text = self.request.get(u'text.{}'.format(index))
                item.style_name = self.request.get(u'style_name.{}'.format(index))
                item.flow_control = self.request.get(u'flow_control.{}'.format(index))
                try:
                    item.put()
                except Exception as e:
                    error_msg = u'Updating document failed: {}'.format(e)
                    break
            for request in self.request.arguments():
                # Handle change-position request
                change_position = change_pattern.match(request)
                if change_position:
                    old_index = int(change_position.group(1))
                    new_position_string = None
                    try:
                        new_position_string = self.request.get(u'position.{}'.format(old_index))
                        new_position = int(new_position_string)
                    except:
                        error_msg = u'Invalid position specified: {}'.format(new_position_string)
                        break
                    if new_position < 1:
                        new_position = 1
                    if new_position > len(items):
                        new_position = len(items)
                    new_index = new_position - 1
                    compressed_items = items[:old_index] + items[old_index + 1:]
                    new_items = compressed_items[:new_index]
                    new_items.append(items[old_index])
                    new_items += compressed_items[new_index:]
                    self.compute_positions(new_items)
                    # Handle remove request
                remove_position = remove_pattern.match(request)
                if remove_position:
                    index = int(remove_position.group(1))
                    item = items[index]
                    item.key.delete()
                    new_items = items[:index] + items[index + 1:]
                    self.compute_positions(new_items)

        if self.request.get(u'update') and not error_msg:
            self.redirect("/template/document?template_id={}".format(template.key.id()))
            return

        # Display the webpage
        self.render(template, document_entity, error_msg)
    def post(self):
        if not dao.test_current_user_registered():
            webapp2.abort(401)

        current_user = users.get_current_user()
        current_email = current_user.email().lower()

        # Attempt to create a new project
        try:
            name = self.request.get(u'name').strip()
            if not name:
                raise Exception(u'You must provide a name for your project')
            for project in dao.get_projects_by_name(name):
                if dao.test_email_is_project_owner(project, current_email):
                    raise Exception(
                        u'Sorry, you already own a project by that name')

            # Create the new Project entity
            project = dao.Project(name=name,
                                  project_type=dao.PROJECT,
                                  description=self.request.get(u'description'))
            project_key = project.put()

            # Create a ProjectUser entity, making the current user owner of the new project
            dao.ProjectUser(email=dao.get_current_site_user().email,
                            permissions=[dao.PROJECT_OWN],
                            parent=project_key).put()

            # Get the selected template ID
            template_id = self.request.get(u'template_id')

            if template_id:
                # Generate HTML for the template
                template_entity = dao.get_template_by_id(template_id)
                html_generator_service = HtmlGeneratorService(template_entity)
                html_generator_service.generate_all_html()

                # Copy entities owned by the template entity into the project
                for assignment_entity in dao.get_assignments(template_entity):
                    assignment_entity.clone(project).put()

                for document_entity in dao.get_documents(template_entity):
                    template_document_entity = document_entity.clone(project)
                    template_document_entity.put()
                    for document_item_entity in dao.get_document_items(
                            document_entity):
                        document_item_entity.clone(
                            template_document_entity).put()

                for interview_entity in dao.get_interviews(template_entity):
                    cloned_interview_entity = interview_entity.clone(project)
                    if cloned_interview_entity.auto_assign:
                        cloned_interview_entity.assigned_email = current_email
                    cloned_interview_entity.put()

                for style_entity in dao.get_styles(template_entity):
                    style_entity.clone(project).put()

                for variable_entity in dao.get_variables(template_entity):
                    variable_entity.clone(project).put()

            self.redirect("/project?project_id={}".format(project_key.id()))
            return
        except Exception as e:
            error_msg = u'Creating project failed: {}'.format(e)

        # Display the webpage
        self.render(error_msg)
Ejemplo n.º 7
0
    def generate_document_html(self, document):
        html = u''
        style = dao.get_style_by_name(self.template, document.style_name)
        if style:
            html += u'<div style="{}">'.format(style.css)
        else:
            html += u'<div>'
        begin_repeating_group = False
        within_repeating_group = False
        end_repeating_group = False
        index_variable_name = None
        document_items = dao.get_document_items(document)
        for index in range(len(document_items)):
            document_item = document_items[index]
            item_type = document_item.item_type
            if item_type != dao.TEXT and not document_item.variable_name:
                continue
            flow_control = document_item.flow_control
            # Set begin/within/end repeating group and index_variable_name
            if within_repeating_group:
                if flow_control == dao.END_REPEATING_GROUP:
                    end_repeating_group = True
            else:
                if flow_control == dao.CONTINUE_FLOW and item_type == dao.REPEATING_VARIABLE:
                    begin_repeating_group = True
                    within_repeating_group = True
                    end_repeating_group = True
                    index_variable_name = document_item.variable_name
                elif flow_control == dao.BEGIN_REPEATING_GROUP:
                    for index2 in range(index + 1, len(document_items)):
                        document_item2 = document_items[index2]
                        if not index_variable_name and document_item2.item_type == dao.REPEATING_VARIABLE:
                            index_variable_name = document_item2.variable_name
                            break
                        if document_item2.flow_control == dao.END_REPEATING_GROUP:
                            break
                    if index_variable_name:
                        begin_repeating_group = True
                        within_repeating_group = True

            # Generate HTML for the document item
            if begin_repeating_group:
                internal_index_variable_name = dao.convert_name_to_internal_name(
                    index_variable_name)
                html += u'{{% if {}_count %}}'.format(
                    internal_index_variable_name)
                html += u'{{% for index in range(1, {}_count) %}}'.format(
                    internal_index_variable_name)
                begin_repeating_group = False
            style = dao.get_style_by_name(self.template,
                                          document_item.style_name)
            if item_type == dao.TEXT:
                html = self.update_html(
                    html, document_item.text, style
                )  # TODO How do we guard against cross-site scripting?
            else:
                assignment = dao.get_assignment_for_variable_name(
                    self.template, document_item.variable_name)
                if assignment:
                    html += u'{% if is_manager %}'
                    html += u'<div class="hover-highlight" ondblclick="document.location=\'/project/conduct_interview?_project_id={{{{ project.key.id() }}}}&_interview_name=write_{}&_from_document_id={{{{ document.key.id() }}}}\'">'.format(
                        assignment.name)
                    html += u'{% else %}<div>{% endif %}'
                internal_variable_name = dao.convert_name_to_internal_name(
                    document_item.variable_name)
                if item_type == dao.SINGLE_VARIABLE:
                    content = u'{{{{ {} }}}}'.format(
                        internal_variable_name
                    )  # TODO How do we guard against cross-site scripting?
                    html = self.update_html(html, content, style)
                else:
                    content = u'{{{{ get_indexed_variable(project, "{}", index) }}}}'.format(
                        internal_variable_name
                    )  # TODO How do we guard against cross-site scripting?
                    html = self.update_html(html, content, style)
                if assignment:
                    html += u'</div>'
            if end_repeating_group:
                html += u'{% endfor %}'
                html += u'{% endif %}'
                begin_repeating_group = False
                within_repeating_group = False
                end_repeating_group = False
                index_variable_name = None
        html += u'</div>'
        document.content = html
        document.put()