Exemplo n.º 1
0
def create_dummy_documents(files, request_type, request, app=None):
    """Helper method to create dummy documents for a request.
    """
    if request_type == 'Registration':
        current_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        for file in files:
            document = Documents.get_document_by_name(file.get('label'), 1)
            reg_doc = RegDocuments(filename='{0}_{1}'.format(current_time, file.get('file_name')))
            reg_doc.reg_details_id = request.id
            reg_doc.document_id = document.id
            reg_doc.save()

            file_path = '{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, file.get('file_name'))
            if not os.path.exists(os.path.dirname(file_path)):
                os.makedirs(os.path.dirname(file_path))
            with open(file_path, 'wb') as f:
                f.seek(1073741824-1)
                f.write(b"\0")
    else:
        current_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        for file in files:
            document = Documents.get_document_by_name(file.get('label'), 2)
            dereg_doc = DeRegDocuments(filename='{0}_{1}'.format(current_time, file.get('file_name')))
            dereg_doc.dereg_id = request.id
            dereg_doc.document_id = document.id
            dereg_doc.save()

            file_path = '{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, file.get('file_name'))
            if not os.path.exists(os.path.dirname(file_path)):
                os.makedirs(os.path.dirname(file_path))
            with open(file_path, 'wb') as f:
                f.seek(1073741824-1)
                f.write(b"\0")
    return request
def test_get_document_by_name(session):
    """Verify that the get_document_by_name() returns a document provided the name."""
    docs = [
        Documents(id=11101, label='shp doc', type=1, required=True),
        Documents(id=21102, label='ath doc', type=2, required=True)
    ]
    session.bulk_save_objects(docs)
    session.commit()
    assert Documents.get_document_by_name('shp doc', 1)
    assert Documents.get_document_by_name('ath doc', 2)
    assert Documents.get_document_by_name('88668', 1) is None
 def bulk_create(cls, documents, dereg_details, time):
     """Create/save documents in bulk."""
     created_documents = []
     for name in documents:
         document = Documents.get_document_by_name(name, 2)
         dereg_document = DeRegDocuments(
             filename='{0}_{1}'.format(time, documents[name].filename))
         dereg_document.dereg_id = dereg_details.id
         dereg_document.document_id = document.id
         dereg_document.save()
         created_documents.append(dereg_document)
     return created_documents
 def bulk_create(cls, documents, reg_details, time):
     """Create documents for a request in bulk."""
     try:
         created_documents = []
         for name in documents:
             document = Documents.get_document_by_name(name, 1)
             reg_document = cls(
                 filename='{0}_{1}'.format(time, documents[name].filename))
             reg_document.reg_details_id = reg_details.id
             reg_document.document_id = document.id
             reg_document.save()
             created_documents.append(reg_document)
         return created_documents
     except Exception:
         raise Exception
 def bulk_update(cls, documents, dereg_details, time):
     """Update the documents in bulk."""
     try:
         updated_documents = []
         for name in documents:
             document = Documents.get_document_by_name(name, 2)
             cls.trash_document(document.id, dereg_details)
             dereg_document = cls(
                 filename='{0}_{1}'.format(time, documents[name].filename))
             dereg_document.dereg_id = dereg_details.id
             dereg_document.document_id = document.id
             dereg_document.save()
             updated_documents.append(dereg_document)
         return updated_documents
     except Exception:
         raise Exception
 def bulk_update(cls, documents, reg_details, time):
     """Update documents of a request in bulk."""
     try:
         updated_documents = []
         for name in documents:
             document = Documents.get_document_by_name(name, 1)
             cls.trash_document(document.id, reg_details)
             reg_document = cls(
                 filename='{0}_{1}'.format(time, documents[name].filename))
             reg_document.reg_details_id = reg_details.id
             reg_document.document_id = document.id
             reg_document.save()
             updated_documents.append(reg_document)
         return updated_documents
     except Exception:
         raise Exception