コード例 #1
0
    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))
コード例 #2
0
    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)