def put(self, project_id=None):
        if self.json_request.get('title') is None:
            raise HttpErrorException.bad_request('invalid project title')

        if self.json_request.get('distilled_document') is None:
            raise HttpErrorException.bad_request('invalid document')

        distilled_document = self.json_request.get('distilled_document')
        if not distilled_document.get('title'):
            raise HttpErrorException.bad_request('invalid document title')

        doc_perm = Permission(permissions=Permission.init_perm_struct(
            Document.operations_list),
                              key=Permission.create_key())

        doc = Document(
            key=Document.create_key(),
            title=distilled_document.get('title'),
            subtitle=distilled_document.get('subtitle'),
            author=distilled_document.get('author', self.user.full_name),
            version=distilled_document.get('version', 'v0.1'),
            date=distilled_document.get('date',
                                        datetime.datetime.now().year),
            copyright_text=distilled_document.get('copyright', ''),
            description=distilled_document.get('description', ''),
            owner=[self.user.key],
            permissions=doc_perm.key,
        )

        doc_perm.artifact = doc.key
        pro_perm = Permission(permissions=Permission.init_perm_struct(
            Project.operations_list),
                              key=Permission.create_key())

        pro = Project(
            key=Project.create_key(),
            title=self.json_request.get('title'),
            distilled_document=doc.key,
            permissions=pro_perm.key,
            owner=[self.user.key],
        )

        pro_perm.artifact = pro.key
        pro_perm.project = pro.key
        doc_perm.project = pro.key
        doc.project = pro.key

        doc.parent_perms.append(pro_perm.key)

        if self.user.in_org():
            doc.organization = self.user.organization
            pro.organization = self.user.organization

        ndb.put_multi([doc_perm, doc, pro_perm, pro])

        index = self.user.get_put_index()
        doc.index(index)
        pro.index(index)

        self.write_json_response(pro.to_dict(self.user))
    def put(self, document_id=None):
        if not self.json_request.get('project') and not Project.valid_id(
                self.json_request.get('project')):
            raise HttpErrorException.bad_request('invalid project id')

        pro = Project.get_by_id(self.json_request.get('project'))
        if not pro:
            raise HttpErrorException.bad_request('invalid project id')
        if not pro.has_permission_read(self.user):
            raise HttpErrorException.forbidden()

        if not self.json_request.get('title'):
            raise HttpErrorException.bad_request('invalid title')

        doc = Document(key=Document.create_key())
        doc.project = pro.key
        doc.title = self.json_request.get('title')
        doc.subtitle = self.json_request.get('subtitle')
        doc.author = self.json_request.get('author')
        doc.version = self.json_request.get('version')
        doc.date = self.json_request.get('date')
        doc.copyright_text = self.json_request.get('copyright')
        doc.description = self.json_request.get('description')
        doc.owner.append(self.user.key)

        doc_perm = Permission(permissions=Permission.init_perm_struct(
            Document.operations_list),
                              key=Permission.create_key(),
                              project=pro.key)
        doc_perm.artifact = doc.key
        doc_perm.put()
        doc.permissions = doc_perm.key

        if self.user.in_org():
            doc.organization = self.user.organization

        doc.parent_perms = [
            pro.permissions,
            pro.distilled_document.get().permissions
        ]
        doc.put()

        pro.documents.append(doc.key)

        indexes = self.user.get_put_index()
        doc.index(indexes)

        pro.pw_modified_ts = datetime.datetime.now()
        pro.put()

        self.write_json_response(doc.to_dict(self.user))

        action_data = {'document': doc.to_dict(self.user)}

        trans = Transaction(action='doc_new',
                            user=self.user.key,
                            artifact=doc.key,
                            project=pro.key,
                            action_data=action_data)
        trans.put()

        self.get_channel_token()
        channel_tokens = ChannelToken.get_by_project_key(
            pro.key, self.user_channel_token)
        channel_tokens = ChannelToken.remove_unauthorized_users(
            channel_tokens, [doc])

        for channel_token in channel_tokens:
            trans.action_data['document'] = doc.to_dict(
                channel_token.user.get())

            message = {
                'user': self.get_user_channel_data(),
                'transaction': trans.to_dict(self.user)
            }
            ChannelToken.broadcast_message([channel_token], message)
Exemple #3
0
    def restore_v1(self, project_json, request_user, zip_file):
        doc_perm = Permission(key=Permission.create_key(),
                              permissions=Permission.init_perm_struct(
                                  Document.operations_list))
        pro_perm = Permission(key=Permission.create_key(),
                              permissions=Permission.init_perm_struct(
                                  Project.operations_list))
        document = Document(
            key=Document.create_key(),
            title=project_json['distilled_document']['title'],
            author=project_json['distilled_document']['author'],
            subtitle=project_json['distilled_document']['subtitle'],
            date=project_json['distilled_document']['date'],
            copyright_text=project_json['distilled_document']
            ['copyright_text'],
            description=project_json['distilled_document']['description'],
            owner=[request_user.key],
            permissions=doc_perm.key)
        project = Project(key=Project.create_key(),
                          title=project_json['title'],
                          distilled_document=document.key,
                          permissions=pro_perm.key,
                          owner=[request_user.key])
        document.project = project.key
        document.parent_perms.append(pro_perm.key)
        if request_user.in_org():
            document.organization = request_user.organization
            project.organization = request_user.organization
        doc_perm.artifact = document.key
        doc_perm.project = project.key
        pro_perm.artifact = project.key
        pro_perm.project = project.key
        ndb.put_multi([doc_perm, pro_perm, document, project])

        # We do not want to re-use the old project, document, concept, ... ids so we will create a mapping
        # from old to new :)
        id_mapping = {
            project_json['id']:
            project.key.id(),
            project_json['distilled_document']['id']:
            project.distilled_document.id()
        }

        documents = {
            project.distilled_document.id(): project.distilled_document.get()
        }
        for doc_json in project_json['documents']:
            doc_json['project_id'] = project.key.id()
            doc_perm = Permission(key=Permission.create_key(),
                                  project=project.key,
                                  permissions=Permission.init_perm_struct(
                                      Document.operations_list))
            doc_obj = Document(key=Document.create_key(),
                               title=doc_json['title'],
                               author=doc_json['author'],
                               subtitle=doc_json['subtitle'],
                               date=doc_json['date'],
                               copyright_text=doc_json['copyright_text'],
                               description=doc_json['description'],
                               owner=[request_user.key],
                               permissions=doc_perm.key,
                               project=project.key)

            doc_perm.artifact = doc_obj.key
            doc_obj.put()
            doc_perm.put()
            project.documents.append(doc_obj.key)
            id_mapping[doc_json['id']] = doc_obj.key.id()
            documents[doc_obj.key.id()] = doc_obj
        project.put()
        model_array = []
        project.children = self.restore_concept_from_json_v1(
            project, project_json['concepts'], id_mapping, request_user,
            model_array, None, documents, zip_file)

        ndb.put_multi(model_array)
        Phrasing.index_multi(model_array, request_user)
        project.put()
        return project