Ejemplo n.º 1
0
    def post(self, request):
        # data = JSONDeserializer().deserialize(request.body)
        datatype_factory = DataTypeFactory()
        template_id = request.POST.get('template_id')
        parenttile_id = request.POST.get('parenttile_id')
        resourceinstance_id = request.POST.get('resourceinstance_id', None)
        self.resource = Resource.objects.get(
            resourceinstanceid=resourceinstance_id)
        self.resource.load_tiles()

        template_name = self.get_template_path(template_id)
        template_path = os.path.join(settings.APP_ROOT, 'docx', template_name)

        if os.path.exists(
                os.path.join(settings.APP_ROOT, 'uploadedfiles',
                             'docx')) is False:
            os.mkdir(os.path.join(settings.APP_ROOT, 'uploadedfiles', 'docx'))

        self.doc = Document(template_path)
        self.edit_letter(self.resource, datatype_factory)

        date = datetime.today()
        date = date.strftime("%Y") + '-' + date.strftime(
            "%m") + '-' + date.strftime("%d")
        new_file_name = date + '_' + template_name
        new_file_path = os.path.join(settings.APP_ROOT, 'uploadedfiles/docx',
                                     new_file_name)

        new_req = HttpRequest()
        new_req.method = 'POST'
        new_req.user = request.user
        new_req.POST['data'] = None
        host = request.get_host()

        self.doc.save(new_file_path)
        saved_file = open(new_file_path, 'rb')
        stat = os.stat(new_file_path)
        file_data = UploadedFile(saved_file)
        file_list_node_id = "96f8830a-8490-11ea-9aba-f875a44e0e11"  # Digital Object

        tile = json.dumps({
            "tileid": None,
            "data": {
                file_list_node_id: [{
                    "name":
                    new_file_name,
                    "accepted":
                    True,
                    "height":
                    0,
                    "lastModified":
                    stat.st_mtime,
                    "size":
                    stat.st_size,
                    "status":
                    "queued",
                    "type":
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "width":
                    0,
                    "url":
                    None,
                    "file_id":
                    None,
                    "index":
                    0,
                    "content":
                    "blob:" + host + "/{0}".format(uuid.uuid4())
                }]
            },
            "nodegroup_id": "7db68c6c-8490-11ea-a543-f875a44e0e11",
            "parenttile_id": None,
            "resourceinstance_id": "",
            "sortorder": 0,
            "tiles": {}
        })

        new_req = HttpRequest()
        new_req.method = 'POST'
        new_req.user = request.user
        new_req.POST['data'] = tile
        new_req.FILES['file-list_' + file_list_node_id] = file_data
        new_tile = TileData()
        new_tile.action = "update_tile"

        response = TileData.post(new_tile, new_req)
        if response.status_code == 200:
            tile = json.loads(response.content)
            return JSONResponse({'tile': tile, 'status': 'success'})

        return HttpResponseNotFound(response.status_code)
Ejemplo n.º 2
0
    def post(self, request):
        # data = JSONDeserializer().deserialize(request.body)
        datatype_factory = DataTypeFactory()
        template_id = request.POST.get('template_id')
        parenttile_id = request.POST.get('parenttile_id')
        resourceinstance_id = request.POST.get('resourceinstance_id', None)
        self.resource = Resource.objects.get(
            resourceinstanceid=resourceinstance_id)
        self.resource.load_tiles()

        template_name = self.get_template_path(template_id)
        template_path = os.path.join(settings.APP_ROOT, 'docx', template_name)

        if os.path.exists(
                os.path.join(settings.APP_ROOT, 'uploadedfiles',
                             'docx')) is False:
            os.mkdir(os.path.join(settings.APP_ROOT, 'uploadedfiles', 'docx'))

        self.doc = Document(template_path)

        if template_name == 'GLAAS Planning Letter A - No Progression - template.docx':
            self.edit_letter_A(self.resource, datatype_factory)
        elif template_name == 'GLAAS Planning Letter B2 - Predetermination - template.docx':
            self.edit_letter_B2(self.resource, datatype_factory)

        date = datetime.today()
        date = date.strftime("%Y") + '-' + date.strftime(
            "%m") + '-' + date.strftime("%d")
        new_file_name = date + '_' + template_name
        new_file_path = os.path.join(settings.APP_ROOT, 'uploadedfiles/docx',
                                     new_file_name)

        new_req = HttpRequest()
        new_req.method = 'POST'
        new_req.user = request.user
        new_req.POST['data'] = None
        host = request.get_host()

        self.doc.save(new_file_path)
        saved_file = open(new_file_path, 'rt')
        stat = os.stat(new_file_path)
        file_data = UploadedFile(saved_file)
        file_list_node_id = "8d41e4d1-a250-11e9-9a12-00224800b26d"

        tile = json.dumps({
            "tileid": None,
            "data": {
                file_list_node_id: [{
                    "name":
                    new_file_name,
                    "accepted":
                    True,
                    "height":
                    0,
                    "lastModified":
                    stat.st_mtime,
                    "size":
                    stat.st_size,
                    "status":
                    "queued",
                    "type":
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "width":
                    0,
                    "url":
                    None,
                    "file_id":
                    None,
                    "index":
                    0,
                    "content":
                    "blob:" + host + "/{0}".format(uuid.uuid4())
                }]
            },
            "nodegroup_id": "8d41e4d1-a250-11e9-9a12-00224800b26d",
            "parenttile_id": parenttile_id,
            "resourceinstance_id": resourceinstance_id,
            "sortorder": 0,
            "tiles": {}
        })

        new_req = HttpRequest()
        new_req.method = 'POST'
        new_req.user = request.user
        new_req.POST['data'] = tile
        new_req.FILES['file-list_' + file_list_node_id] = file_data
        new_tile_data_instance = TileData()

        post_resp = TileData.post(new_tile_data_instance, new_req)

        if post_resp.status_code == 200:
            return JSONResponse({'tile': tile, 'status': 'success'})

        return HttpResponseNotFound("Error: " + post_resp.status_code)