Exemplo n.º 1
0
    def post_doc_data(self, bundle):

        # get the basic information needed from the request
        # and add the epoch time to the title of the document
        user_id, GET_data = bundle.request.user.id, bundle.data
        post_data, doc_title, file_type = \
            GET_data['docfile'], GET_data['doc_title'] \
            + '-' + str(int(time.time())), GET_data['file_type']

        # this handles the fact that test posts are different from
        # application posts. Need to investigate.
        if post_data == 'data:' or len(post_data) == 0:
            raise RhizomeApiException(
                message='file is empty please check the upload and try again')

        # when posting from ODK cronjob, we dont add the file_meta. but we do
        # from the webapp.  Look into changing so the requests are consistent
        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data

        file_header = None
        file_content = None

        if '.csv' in doc_title:
            new_file_path = None
            file_content = ContentFile(base64.b64decode(base64data))
            file_header = file_content.readline()
        elif '.xlsx' in doc_title or '.xls' in doc_title:
            # workaround - need to create the xls file in order to read from it
            new_file_path = settings.MEDIA_ROOT + doc_title
            new_file = open(new_file_path, 'w')
            new_file.write(base64.b64decode(base64data))
            new_file.close()
            the_file = open(new_file_path)
            try:
                file_df = read_excel(the_file)
            except Exception:
                os.remove(new_file_path)
                raise RhizomeApiException(
                    message='There was an error with your file. Please check \\\
                        the upload and try again')
            file_content = ContentFile(file_df.to_csv())
            file_header = file_content.readline()
        else:
            RhizomeApiException(
                message='Please upload either xls, xlsx or csv file formats')

        sd = Document.objects.create(
            **{
                'doc_title': doc_title,
                'file_type': file_type,
                'created_by_id': user_id,
                'file_header': file_header
            })
        sd.docfile.save(sd.guid, file_content)

        return sd
Exemplo n.º 2
0
    def post_doc_data(self, bundle):

        # get the basic information needed from the request
        # and add the epoch time to the title of the document
        user_id, GET_data = bundle.request.user.id, bundle.data
        post_data, doc_title, file_type = \
            GET_data['docfile'], GET_data['doc_title'] \
            + '-' + str(int(time.time())), GET_data['file_type']

        # this handles the fact that test posts are different from
        # application posts. Need to investigate.
        if post_data == 'data:' or len(post_data) == 0:
            raise RhizomeApiException(
                message='file is empty please check the upload and try again')

        # when posting from ODK cronjob, we dont add the file_meta. but we do
        # from the webapp.  Look into changing so the requests are consistent
        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data

        file_header = None
        file_content = None

        if '.csv' in doc_title:
            new_file_path = None
            file_content = ContentFile(base64.b64decode(base64data))
            file_header = file_content.readline()
        elif '.xlsx' in doc_title or '.xls' in doc_title:
            # workaround - need to create the xls file in order to read from it
            new_file_path = settings.MEDIA_ROOT + doc_title
            new_file = open(new_file_path, 'w')
            new_file.write(base64.b64decode(base64data))
            new_file.close()
            the_file = open(new_file_path)
            try:
                file_df = read_excel(the_file)
            except Exception:
                os.remove(new_file_path)
                raise RhizomeApiException(
                    message='There was an error with your file. Please check \\\
                        the upload and try again')
            file_content = ContentFile(file_df.to_csv())
            file_header = file_content.readline()
        else:
            RhizomeApiException(
                message='Please upload either xls, xlsx or csv file formats')

        sd = Document.objects.create(**{
             'doc_title': doc_title,
             'file_type': file_type,
             'created_by_id': user_id,
             'file_header': file_header
             })
        sd.docfile.save(sd.guid, file_content)

        return sd
Exemplo n.º 3
0
    def post_doc_data(self, post_data, user_id, doc_title, doc_id):

        # when posting from ODK, i dont add the file_meta.. from the webapp
        # i do.  I should change so the post requests are consistent but
        # tryign to get this working for now.

        #TODO: better exception handling. This is kind of lame but handles the fact that test posts are different from
        #application posts. Need to investigate.
        if post_data == 'data:' or len(post_data) == 0:
            raise DatapointsException(
                message='file is empty please check the upload and try again')
        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data
        file_header = None
        file_content = None
        if '.csv' in doc_title:
            file_content = ContentFile(base64.b64decode(base64data))
            file_header = file_content.readline()
        elif '.xlsx' in doc_title or '.xls' in doc_title:
            # workaround-- need to create the excel file in order to read from it
            new_file_path = settings.MEDIA_ROOT + doc_title
            new_file = open(new_file_path, 'wr')
            new_file.write(base64.b64decode(base64data))
            new_file.close()
            the_file = open(new_file_path)
            try:
                file_df = read_excel(the_file)
            except Exception as err:
                os.remove(new_file_path)
                raise DatapointsException(
                    message=
                    'There was an error with your file. Please check the upload and try again'
                )
            file_content = ContentFile(file_df.to_csv())
            file_header = file_content.readline()
            # delete the excel file
            os.remove(new_file_path)
        sd, created = Document.objects.update_or_create(
            id=doc_id,
            defaults={'doc_title': doc_title, 'created_by_id': user_id, \
                'file_header': file_header}
        )
        sd.docfile.save(sd.guid, file_content)
        return sd
Exemplo n.º 4
0
    def post_doc_data(self, post_data, user_id, doc_title, doc_id):

        # when posting from ODK, i dont add the file_meta.. from the webapp
        # i do.  I should change so the post requests are consistent but
        # tryign to get this working for now.

        #TODO: better exception handling. This is kind of lame but handles the fact that test posts are different from
        #application posts. Need to investigate.
        if post_data == 'data:' or len(post_data) == 0:
            raise DatapointsException(message='file is empty please check the upload and try again')
        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data
        file_header = None
        file_content = None
        if '.csv' in doc_title:
            file_content = ContentFile(base64.b64decode(base64data))
            file_header = file_content.readline()
        elif '.xlsx' in doc_title or '.xls' in doc_title:
            # workaround-- need to create the excel file in order to read from it
            new_file_path = settings.MEDIA_ROOT+doc_title
            new_file = open(new_file_path, 'wr')
            new_file.write(base64.b64decode(base64data))
            new_file.close()
            the_file = open(new_file_path)
            try:
                file_df=read_excel(the_file)
            except Exception as err:
                os.remove(new_file_path)
                raise DatapointsException(message='There was an error with your file. Please check the upload and try again')
            file_content = ContentFile(file_df.to_csv())
            file_header = file_content.readline()
            # delete the excel file
            os.remove(new_file_path)
        sd, created = Document.objects.update_or_create(
            id=doc_id,
            defaults={'doc_title': doc_title, 'created_by_id': user_id, \
                'file_header': file_header}
        )
        sd.docfile.save(sd.guid, file_content)
        return sd
Exemplo n.º 5
0
    def post_doc_data(self, post_data, user_id, doc_title, doc_id):

        # when posting from ODK, i dont add the file_meta.. from the webapp
        # i do.  I should change so the post requests are consistent but
        # tryign to get this working for now.

        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data

        file_content = ContentFile(base64.b64decode(base64data))
        file_header = file_content.readline()

        sd, created = Document.objects.update_or_create(
            id=doc_id,
            defaults={'doc_title': doc_title, 'created_by_id': user_id, \
                'file_header': file_header}
        )

        sd.docfile.save(sd.guid, file_content)

        return sd
Exemplo n.º 6
0
    def post_doc_data(self, post_data, user_id, doc_title, doc_id):

        # when posting from ODK, i dont add the file_meta.. from the webapp
        # i do.  I should change so the post requests are consistent but
        # tryign to get this working for now.

        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data

        file_content = ContentFile(base64.b64decode(base64data))
        file_header = file_content.readline()

        sd, created = Document.objects.update_or_create(
            id=doc_id,
            defaults={'doc_title': doc_title, 'created_by_id': user_id, \
                'file_header': file_header}
        )

        sd.docfile.save(sd.guid, file_content)

        return sd