示例#1
0
def data(request, id):
    batch = None
    data = None
    try:
        batch = ApiBatch.objects.get(user=request.user, id=int(id))
        if batch.success:
            try:
                data = ApiBatchData.objects.get(batch=batch)
            except:
                pass
            if not data:
                client = Client(CAPTRICITY_API_TOKEN)
                BATCH_NAME = batch.name
                related_job_id = ast.literal_eval(
                    batch.submit)['related_job_id']
                csv_out = client.read_job_results_csv(related_job_id)
                data = ApiBatchData.objects.create(batch=batch, text=csv_out)
                data.save()
        else:
            raise Exception("Batch was not successfull")
    except:

        batch.status = "Error while extracting data"
        batch.save()
        messages.error(request, "There was an error in the batch")
        return HttpResponseRedirect("/home/")
    data = data.text.splitlines()
    for n, d in enumerate(data):
        data[n] = d.split(",")
    messages.success(request, "The data was extracted successfully")
    return render(request, "api/data.html", {"data": data, "batch": batch})
示例#2
0
def data(request,id):
    batch = None
    data = None
    try:
        batch = ApiBatch.objects.get(user = request.user,id=int(id))
        if batch.success:
            try:
                data = ApiBatchData.objects.get(batch = batch) 
            except:
                pass      
            if not data:                
                client = Client(CAPTRICITY_API_TOKEN)
                BATCH_NAME = batch.name                
                related_job_id = ast.literal_eval(batch.submit)['related_job_id']                              
                csv_out = client.read_job_results_csv(related_job_id)                              
                data = ApiBatchData.objects.create(batch=batch,text = csv_out)               
                data.save()              
        else:
            raise Exception("Batch was not successfull")          
    except:
        
        batch.status = "Error while extracting data"
        batch.save()
        messages.error(request, "There was an error in the batch")
        return HttpResponseRedirect("/home/")
    data = data.text.splitlines()
    for n,d in enumerate(data):
        data[n] = d.split(",")    
    messages.success(request, "The data was extracted successfully")        
    return render(request,"api/data.html",{"data":data,"batch":batch})
示例#3
0
def mailUser():
    from api.models import ApiBatch, ApiBatchData
    from captools.api import Client 
    from Captricity.settings import CAPTRICITY_API_TOKEN
    from django.core.mail import send_mail
    import ast
    batches = ApiBatch.objects.filter(success = True)
   
    for batch in batches:
        data  = ApiBatchData.objects.filter(batch = batch)
        if data.count() == 0:
            
            client = Client(CAPTRICITY_API_TOKEN)                          
            related_job_id = ast.literal_eval(batch.submit)['related_job_id'] 
           
            csv_out = client.read_job_results_csv(related_job_id)               
            data = ApiBatchData.objects.create(batch=batch,text = csv_out)               
            data.save()
            send_mail('Captricity data completed.', 'Your data is completed. The link for the data is http://localhost:8000/api/data/'+str(batch.id)+"/", '*****@*****.**',
                      [batch.user.email], fail_silently=False) 
            
            
        else:
            pass
        break 


# Digitization time varies between a few minutes to a few hours depending on your Job's size. 
# You can track the progress of your Job using the percent_completed property of the Job resource.
# Once your Job is complete, you can use the Captricity API to extract your results.
# If you are interested in the results from a certain Instance Set, you can pull the digitized 
#   data for just the Shreds in that Instance Set. 
# First, find your Instance Set's id by listing all the Instance Sets in the Job:

instance_sets = client.read_instance_sets(submitted_batch['related_job_id'])


# Once you have found the id of your Instance Set, you can then list all the Shreds 
# in that Instance Set using the Instance Set Shreds resource:

print
print client.read_instance_set_shreds(instance_sets[0]['id'])
print 

# The result of the digitization is in the best_estimate property of each Shred.
# If you'd like the whole dataset, grab the CSV export from the Job Results CSV resource.

csv_out = client.read_job_results_csv(submitted_batch['related_job_id'])

print "Data out:"
print "========="
print csv_out


import sys
from captools.api import Client

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'You must pass in a job id'
        sys.exit(0)

    from api_token import API_TOKEN
    client = Client(API_TOKEN)

    job_id = sys.argv[1]
    if client.read_document(client.read_job(job_id)['document_id'])['name'] != 'Example School Survey Template':
        print 'You must choose a job that is using the example school survey template'
        sys.exit(0)

    instance_sets = client.read_instance_sets(job_id)

    shreds = client.read_instance_set_shreds(instance_sets[0]['id'])
    print 'Sample result of one cell:', shreds[0]

    csv_out = client.read_job_results_csv(job_id)
    print 'CSV result:'
    print csv_out