def get_publish(self, document, group):
        document = Document.get_by_id(document)
        if not document:
            raise HttpErrorException.bad_request('invalid document id')
        if not document.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        group = Group.get_by_id(group)
        if not group:
            raise HttpErrorException.bad_request('invalid group id')

        self.project = document.project.get()
        version = self.request.get('v', 'latest')
        pub = PublishDocument.get(document, group, version)

        if not pub:
            raise HttpErrorException.not_found()

        if pub.group not in self.user.groups and pub.group != Group.get_worldshare_key(
        ):
            raise HttpErrorException.forbidden()

        self._create_analytic_session()
        self.project.record_analytic('pro_opn', self.analytic_session)

        template_index = JINJA_ENVIRONMENT.get_template('document_public.html')
        return template_index.render({
            'title': self.project.title,
            'version': pub.version,
            'created_at': pub.created_ts,
            'published_to': pub.group.get().name,
            'an_token': self.analytic_session.key.id(),
            'project_id': self.project.id,
            'html': pub.html,
        })
    def delete(self, publish):
        publish = PublishDocument.get_by_id(publish)
        if not publish:
            raise HttpErrorException.bad_request('invalid publish id')

        document = publish.document.get()
        if not document:
            raise HttpErrorException.bad_request('document does not exists')
        if not document.has_permission_write(self.user):
            raise HttpErrorException.forbidden()

        if publish.key in document.published:
            document.published.remove(publish.key)
            document.put()

        publish.key.delete()
    def _delete_document(self):
        pro = self.document.project.get()
        pro.remove_document(self.document.key)
        q = Concept.query()
        q = q.filter(Concept.project == pro.key)

        for concept in q.iter():
            concept.remove_document(self.document.key)

        q = Marker.query()
        q = q.filter(Marker.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        q = PublishDocument.query()
        q = q.filter(PublishDocument.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        q = PublishSummary.query()
        q = q.filter(PublishSummary.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        q = PublishPresentation.query()
        q = q.filter(PublishPresentation.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        q = PresentationDocument.query()
        q = q.filter(PresentationDocument.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        q = SummaryDocument.query()
        q = q.filter(SummaryDocument.document == self.document.key)
        for m in q.iter():
            m.key.delete()

        self.document.permissions.delete()
        self.document.key.delete()
        indexes = self.user.get_indexes(create_new=False)
        self.document.index_delete(indexes)
    def get(self, project, document, group):
        if not project:
            raise HttpErrorException.bad_request('invalid project given')

        project = Project.get_by_id(project)
        if not project:
            raise HttpErrorException.bad_request('invalid project given')
        if not project.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        if not document:
            raise HttpErrorException.bad_request('invalid document given')

        document = Document.get_by_id(document)
        if not document:
            raise HttpErrorException.bad_request('invalid document given')
        if not document.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        if not group:
            raise HttpErrorException.bad_request('invalid group given')

        group = Group.get_by_id(group)
        if not group:
            raise HttpErrorException.bad_request('invalid group given')

        if document.key not in project.documents and document.key != project.distilled_document:
            raise HttpErrorException.bad_request(
                'document does not belong to project')

        temp_user = User()
        temp_user.groups = [group.key, Group.get_worldshare().key]
        org = self.user.organization if self.user.organization else None

        if org:
            temp_user.organization = org

        if not document.has_permission_read(temp_user):
            raise HttpErrorException.bad_request(
                'Group does not have permission to read the document')

        pubs = document.get_presentation_published(group=group)
        version_int = PublishDocument.get_largest_version(pubs)

        if version_int is None:
            version_int = 1
        else:
            version_int += 1

        version = self.request.get('version', str(version_int))
        if version == 'latest':
            raise HttpErrorException.bad_request('invalid version given')

        for pub in pubs:
            if pub.version == version:
                raise HttpErrorException.bad_request(
                    'version name already taken')

        try:
            slide_count = int(self.request.get('slide_count', 15))
        except ValueError:
            raise HttpErrorException.bad_request(
                'invalid slide count, must be integer')
        if slide_count < 1:
            raise HttpErrorException.bad_request(
                'invalid slide_count given min 1')
        if slide_count > 100:
            raise HttpErrorException.bad_request(
                'invalid slide_count given max 100')

        try:
            min_bullet = int(self.request.get('min_bullet', 4))
        except ValueError:
            raise HttpErrorException.bad_request(
                'invalid min bullet, must be integer')
        if min_bullet < 1:
            raise HttpErrorException.bad_request(
                'invalid min_bullet given min 1')
        if min_bullet > 15:
            raise HttpErrorException.bad_request(
                'invalid min_bullet given max 15')

        try:
            max_bullet = int(self.request.get('max_bullet', 6))
        except ValueError:
            raise HttpErrorException.bad_request(
                'invalid max bullet, must be integer')
        if max_bullet < 1:
            raise HttpErrorException.bad_request(
                'invalid max_bullet given min 1')
        if max_bullet > 15:
            raise HttpErrorException.bad_request(
                'invalid max_bullet given max 15')

        if min_bullet > max_bullet:
            raise HttpErrorException.bad_request(
                'min_bullet can not be greater than max_bullet')

        publisher = PresentationPublisherThread()
        publisher.id = server.create_uuid()
        publisher.request = self
        publisher.project = project
        publisher.document = document
        publisher.slide_count = slide_count
        publisher.min_bullet = min_bullet
        publisher.max_bullet = max_bullet
        publisher.group = group
        publisher.user = self.user
        publisher.version = version

        if publisher.is_lock():
            raise HttpErrorException.bad_request('publisher already running')
        publisher.start()

        self.write_json_response({'id': publisher.id})

        self.get_analytic_session()
        document.record_analytic('pres_publish',
                                 self.analytic_session,
                                 project=project.key)
    def run(self):
        try:
            log.info('Publisher Stated')
            self.set_lock()

            org = self.user.organization.get(
            ) if self.user.organization else None

            publisher = DocumentPublisherEngine(self.project,
                                                self.document,
                                                self.group,
                                                organization=org)

            publisher.render()

            pubs = self.document.get_published(group=self.group)
            version_int = PublishDocument.get_largest_version(pubs)
            if version_int is None:
                version_int = 1
            else:
                version_int += 1

            pub_doc = PublishDocument(key=PublishDocument.create_key())
            pub_doc.project = self.project.key
            pub_doc.document = self.document.key
            pub_doc.group = self.group.key
            pub_doc.version = self.version
            pub_doc.version_int = version_int
            pub_doc.html = publisher.html
            pub_doc.owner.append(self.user.key)

            if self.user.organization:
                pub_doc.organization = self.user.organization

            pub_doc.put()
            pub_doc.cache(latest=True)
            self.document.published.append(pub_doc.key)
            self.document.put()

            action_data = {'publish': pub_doc.to_dict(html=False)}
            trans = Transaction(action='doc_published',
                                user=self.user.key,
                                artifact=self.document.key,
                                project=self.project.key,
                                action_data=action_data)
            trans.put()

            self.project.pw_modified_ts = datetime.now()
            self.project.put()

            self.cache_publish(pub_doc)

        except:
            self.cache_error('500')
            raise

        finally:
            self.release_lock()
            log.info('Publisher Finished')
    def get(self, project, document, group):
        if not project:
            raise HttpErrorException.bad_request('invalid project given')

        project = Project.get_by_id(project)
        if not project:
            raise HttpErrorException.bad_request('invalid project given')
        if not project.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        if not document:
            raise HttpErrorException.bad_request('invalid document given')

        document = Document.get_by_id(document)
        if not document:
            raise HttpErrorException.bad_request('invalid document given')
        if not document.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        if not group:
            raise HttpErrorException.bad_request('invalid group given')

        group = Group.get_by_id(group)
        if not group:
            raise HttpErrorException.bad_request('invalid group given')

        temp_user = User()
        temp_user.groups = [group.key, Group.get_worldshare().key]
        org = self.user.organization if self.user.organization else None

        if org:
            temp_user.organization = org

        if not document.has_permission_read(temp_user):
            raise HttpErrorException.bad_request(
                'Group does not have permission to read the document')

        if document.key not in project.documents and document.key != project.distilled_document:
            raise HttpErrorException.bad_request(
                'document does not belong to project')

        pubs = document.get_published(group=group)
        version_int = PublishDocument.get_largest_version(pubs)

        if version_int is None:
            version_int = 1
        else:
            version_int += 1

        version = self.request.get('version', str(version_int))
        if version == 'latest':
            raise HttpErrorException.bad_request('invalid version given')

        pubs = document.get_published(group=group)
        for pub in pubs:
            if pub.version == version:
                raise HttpErrorException.bad_request(
                    'version name already taken')

        publisher = DocumentPublisherThread()
        publisher.id = server.create_uuid()
        publisher.request = self
        publisher.project = project
        publisher.document = document
        publisher.group = group
        publisher.user = self.user
        publisher.version = version

        if publisher.is_lock():
            raise HttpErrorException.bad_request('publisher already running')
        publisher.start()

        self.write_json_response({'id': publisher.id})

        self.get_analytic_session()
        document.record_analytic('doc_publish',
                                 self.analytic_session,
                                 project=project.key)