Exemplo n.º 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})
class ApiTokenDialog(PySide.QtGui.QDialog):
    def __init__(self, parent=None):
        super(ApiTokenDialog, self).__init__(parent)
        self.resize(250, 100)

        self.setWindowTitle('API Token')
        self.instructions = PySide.QtGui.QLabel('Please enter your Captricity API Token', self)
        self.api_token_text = PySide.QtGui.QLineEdit(self)
        self.ok_button = PySide.QtGui.QPushButton('Ok', self)
        self.ok_button.clicked.connect(self.handle_ok)

        actions_layout = PySide.QtGui.QHBoxLayout()
        actions_layout.addStretch(1)
        actions_layout.addWidget(self.ok_button)

        layout = PySide.QtGui.QVBoxLayout()
        layout.addWidget(self.instructions)
        layout.addWidget(self.api_token_text)
        layout.addLayout(actions_layout)
        self.setLayout(layout)

    def handle_ok(self):
        self.api_token = str(self.api_token_text.text()).strip()
        try:
            self.client = Client(self.api_token)
            self.client.read_user_profile()
            keyring.set_password(APP_NAME, username, self.api_token)
            self.accept()
        except:
            PySide.QtGui.QMessageBox.warning(self, 'Required API Token', 'A valid Captricity API Token is required to use the Captricity Desktop Client')
Exemplo n.º 3
0
def addall(request):
    try:
        images= HomeImages.objects.filter(user=request.user,is_active = True)
    except:
        messages.error(request, "Error. Did not find the image object")
        return HttpResponseRedirect("/home/")
    client = Client(CAPTRICITY_API_TOKEN_SUBMIT)        
    BATCH_NAME = get_random_batch_name(STORED_BATCH_NAME)

    batch_id = create_or_get_batch(client,BATCH_NAME)        
    documents = client.read_documents()
    
    document_id = filter(lambda x: x['name'] == CAPTRICITY_TEMPLATE_NAME, documents).pop()['id']
    client.update_batch(batch_id, { 'documents': document_id, 'name': BATCH_NAME })
    batchObject = ApiBatch.objects.create(name=BATCH_NAME,user = request.user)
  
    batchObject.save()
    for image in images:
        f = open(image.image.url, 'rb')
        try:
            batch_file = client.create_batch_files(batch_id, {'uploaded_file': f})
            imageObject = ApiBatchImage.objects.create(batch = batchObject, image = image)
            imageObject.save()
        except:
            pass            
    try:
        batchObject.status = status(client,batch_id)
        batchObject.submit = submit(client,batch_id)
        batchObject.success = True
        messages.success(request, "All images were successfully added to batch "+batchObject.name+" total images "+str(images.count()))
    except Exception, e:
        batchObject.status = str(e)
        batchObject.submit = ""
        batchObject.success = False
        messages.success(request, "There was an Error in the batch "+batchObject.name+" "+str(e))
Exemplo n.º 4
0
def api_test_task(name):
    client = Client(settings.CAPTRICITY_API_TOKEN)
    jobs = client.read_jobs()
    print 'Enqueued object name: %s' % name
    print 'Your jobs are:'
    for job in jobs:
        print '\t%s' % job.get('name')
Exemplo n.º 5
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})
Exemplo n.º 6
0
def old_test():

  client = Client(api_token)

  # figure out the last time we downloaded data
  try:
    datefile = open(os.path.expanduser("~/.scaleupbrazil/lastdownload"))
    lastcheck = datefile.readline().strip()
    datefile.close()  
  except IOError:
    print "Error reading last download date -- using default."
    lastcheck = "20120101"
    
  lastcheck = dateutil.parser.parse(lastcheck)

  print 'last download was on ', str(lastcheck)

  ##jobs = get_jobs(client, since_date=lastcheck, only_complete=True)
  jobs = get_jobs(client, only_complete=True)  
  checktime = datetime.datetime.now()
  
  # download the data for each job
  datasets = []
  dataset_names = []

  for job in jobs:

    print '\tdownloading: ', job['name']

    for idx, ds in enumerate(client.read_datasets(job['id'])):
      nextdata = client.read_dataset(ds['id'], accept="text/csv")
      datasets.append(nextdata)
      dataset_names.append(str(job['id']) + '-' + str(idx))

  print 'saving datasets to disk...'
  for idx, ds in enumerate(datasets):
    try:
      fn = os.path.expanduser("~/.scaleupbrazil/downloaded-data/" +
                              dataset_names[idx] + ".csv")
      datafile = open(fn, 'w')
      datafile.write(datasets[idx])
      datafile.close()
    except:
      print 'Error writing to file! Offending path: ', fn
      sys.exit()
      
  # update the last check date
  print 'updating date of last download'
  datefile = open(os.path.expanduser("~/.scaleupbrazil/lastdownload"), "w")
  datefile.write(str(checktime))
  datefile.close()

  print 'done.'
Exemplo n.º 7
0
def create_fancy_csv_from_job(job_id, name):
    # Initialize Captricity Python Client (installation instructions in README
    # at https://github.com/Captricity/captools)
    start = time.time()
    client = Client(api_token=CAP_API_TOKEN)
    
    # Read all the Instance Sets associated with this job
    isets = client.read_instance_sets(job_id)

    # For each Instance Set, we will pull down all the Shreds and record the
    # transcribed value and generate a link to the Shred image.
    all_iset_data = []
    fields = {}
    fields['0'] = 'Form Name'
    fields['0.5'] = 'Form Name Image Link'
    for iset in isets:
        shreds = client.read_instance_set_shreds(iset['id'])
        iset_data = {}
        iset_data['0'] = iset['name']
        for shred in shreds:
            if '0.5' not in iset_data:
                iset_data['0.5'] = 'https://shreddr.captricity.com/api/v1/instance/%s/aligned-image' % shred['instance_id']
            # Key on Field id because Field name can be duplicated
            field_id = shred['field']['id']
            iset_data[str(field_id)] = shred['best_estimate'].encode('utf-8') if shred['best_estimate'] else None
            iset_data[str(field_id + 0.5)] = 'https://shreddr.captricity.com/api/v1/shred/%s/image' % shred['id']
            # We'll order export by Field ID, links will be (field_id + 0.5) so they will be next to the Field in CSV
            fields[str(field_id)] = shred['field']['name']
            fields[str(field_id + 0.5)] = shred['field']['name'] + ' Image Link'
        all_iset_data.append(iset_data)
        if len(all_iset_data) % 5 == 0:
            print 'Done with %s Instance Sets from Job %s in %s sec, %s to go' % (len(all_iset_data), job_id, time.time() - start, len(isets) - len(all_iset_data))
    
    # Export all the data as CSV
    data_out = [fields] + all_iset_data
    header = sorted(fields.keys())
    if job_id in [3968, 4606]:
        # No depivot for cover page or addenda
        buffer = open('%s.csv' % name, 'w')
    else:
        buffer = StringIO()
    csv_writer = csv.DictWriter(buffer, header, restval=u'--no data--')
    csv_writer.writerows(data_out)
    if job_id in [3968, 4606]:
        buffer.close()
    else:
        buffer.seek(0)
        depivot_data(csv.reader(buffer), '%s.csv' % name)
Exemplo n.º 8
0
def api_proxy(request):
    client = Client(settings.CAPTRICITY_APPLICATION_TOKEN)

    req = json.loads(request.body)

    method = getattr(client, req['method'], None)
    resp = method(*req.get('args', []))

    return HttpResponse(json.dumps(resp), content_type='application/json')
 def handle_ok(self):
     self.api_token = str(self.api_token_text.text()).strip()
     try:
         self.client = Client(self.api_token)
         self.client.read_user_profile()
         keyring.set_password(APP_NAME, username, self.api_token)
         self.accept()
     except:
         PySide.QtGui.QMessageBox.warning(self, 'Required API Token', 'A valid Captricity API Token is required to use the Captricity Desktop Client')
Exemplo n.º 10
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
Exemplo n.º 11
0
def addall(request):
    try:
        images = HomeImages.objects.filter(user=request.user, is_active=True)
    except:
        messages.error(request, "Error. Did not find the image object")
        return HttpResponseRedirect("/home/")
    client = Client(CAPTRICITY_API_TOKEN_SUBMIT)
    BATCH_NAME = get_random_batch_name(STORED_BATCH_NAME)

    batch_id = create_or_get_batch(client, BATCH_NAME)
    documents = client.read_documents()

    document_id = filter(lambda x: x['name'] == CAPTRICITY_TEMPLATE_NAME,
                         documents).pop()['id']
    client.update_batch(batch_id, {
        'documents': document_id,
        'name': BATCH_NAME
    })
    batchObject = ApiBatch.objects.create(name=BATCH_NAME, user=request.user)

    batchObject.save()
    for image in images:
        f = open(image.image.url, 'rb')
        try:
            batch_file = client.create_batch_files(batch_id,
                                                   {'uploaded_file': f})
            imageObject = ApiBatchImage.objects.create(batch=batchObject,
                                                       image=image)
            imageObject.save()
        except:
            pass
    try:
        batchObject.status = status(client, batch_id)
        batchObject.submit = submit(client, batch_id)
        batchObject.success = True
        messages.success(
            request, "All images were successfully added to batch " +
            batchObject.name + " total images " + str(images.count()))
    except Exception, e:
        batchObject.status = str(e)
        batchObject.submit = ""
        batchObject.success = False
        messages.success(
            request, "There was an Error in the batch " + batchObject.name +
            " " + str(e))
Exemplo n.º 12
0
def uploadAllImages():
    from captools.api import Client    
    from home.models import HomeImages
    from django.contrib.auth.models import User
    from Captricity.settings import CAPTRICITY_API_TOKEN, BATCH_NAME as STORED_BATCH_NAME, CAPTRICITY_TEMPLATE_NAME
    from api.views import get_random_batch_name,create_or_get_batch, status, submit
    from api.models import ApiBatch, ApiBatchImage
    
    users = User.objects.all()
    for user in users:
        images= HomeImages.objects.filter(user=user,is_active = True)            
        client = Client(CAPTRICITY_API_TOKEN)        
        BATCH_NAME = get_random_batch_name(STORED_BATCH_NAME)
    
        batch_id = create_or_get_batch(client,BATCH_NAME)        
        documents = client.read_documents()        
        document_id = filter(lambda x: x['name'] == CAPTRICITY_TEMPLATE_NAME, documents).pop()['id']
        client.update_batch(batch_id, { 'documents': document_id, 'name': BATCH_NAME })
        batchObject = ApiBatch.objects.create(name=BATCH_NAME,user = user)      
        batchObject.save()
        for image in images:
            f = open(PROJECT_PATH+"/../"+image.image.url, 'rb')
            try:
                batch_file = client.create_batch_files(batch_id, {'uploaded_file': f})
                imageObject = ApiBatchImage.objects.create(batch = batchObject, image = image)
                imageObject.save()
            except:
                pass            
        try:
            batchObject.status = status(client,batch_id)
            batchObject.submit = submit(client,batch_id)
            batchObject.success = True   
            print "Image saved"           
        except Exception, e:
            batchObject.status = str(e)
            batchObject.submit = ""
            batchObject.success = False    
            print "Big werror"        
        batchObject.save()
def upload_file(api_token, batch_id, f):
    client = Client(api_token)
    client.create_batch_files(batch_id, {'uploaded_file': open(f)})
Exemplo n.º 14
0
from captools.api import Client

if __name__ == '__main__':
    from api_token import API_TOKEN
    client = Client(API_TOKEN)

    documents = client.read_documents()
    document_id = filter(
        lambda x: x['name'] == 'Example School Survey Template',
        documents).pop()['id']

    job = client.create_jobs({'document_id': document_id})
    print job
import sys
from captools.api import Client

FORM_PAGE_0 = "assets/example_page1.png"
FORM_PAGE_1 = "assets/example_page2.png"

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)

    iset = client.create_instance_sets(job_id, {"name": "New Iset"})

    instances = client.read_instance_set_instances(iset["id"])
    assert len(instances) == 0

    instance1 = client.create_instance_set_instances(
        iset["id"], {"page_number": "0", "image_file": open(FORM_PAGE_0, "rb")}
    )
    instance2 = client.create_instance_set_instances(
        iset["id"], {"page_number": "1", "image_file": open(FORM_PAGE_1, "rb")}
    )
from captools.api import Client

if __name__ == '__main__':
    from api_token import API_TOKEN
    client = Client(API_TOKEN)

    documents = client.read_documents()
    document_id = filter(lambda x: x['name'] == 'Example School Survey Template', documents).pop()['id']

    job = client.create_jobs({'document_id': document_id})
    print job
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)

    job_readiness = client.read_job_readiness(job_id)
    print 'Is job ready to submit?:', job_readiness['is_ready_to_submit']

    job_price = client.read_job_price(job_id)
    print 'How much does the job cost?:', job_price['total_job_cost_in_cents']

    client.submit_job(job_id, {})
    print 'Job submitted successfully and your account has been charged'
import sys
from captools.api import Client

FORM_PAGE_0 = 'assets/example_page1.png'
FORM_PAGE_1 = 'assets/example_page2.png'

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)

    iset = client.create_instance_sets(job_id, {'name': 'New Iset'})

    instances = client.read_instance_set_instances(iset['id'])
    assert len(instances) == 0

    instance1 = client.create_instance_set_instances(
        iset['id'], {
            'page_number': '0',
            'image_file': open(FORM_PAGE_0, 'rb')
        })
    instance2 = client.create_instance_set_instances(
Exemplo n.º 19
0
 def __init__(self, configdir=os.path.expanduser("~/.scaleupbrazil/")):
   self.token = sec.get_token(configdir)
   Client.__init__(self, self.token)
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
Exemplo n.º 21
0
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)

    job_readiness = client.read_job_readiness(job_id)
    print 'Is job ready to submit?:', job_readiness['is_ready_to_submit']

    job_price = client.read_job_price(job_id)
    print 'How much does the job cost?:', job_price['total_job_cost_in_cents']

    client.submit_job(job_id, {})
    print 'Job submitted successfully and your account has been charged'
Exemplo n.º 22
0
import sys
import glob
import time
import captools.api
import json
from captools.api import Client

BATCH_NAME = 'test-20141027164230'
TEMPLATE_NAME = 'challenge'
API_TOKEN = os.environ.get("CAPTRICITY_API_TOKEN", None)

print "API Token: ", API_TOKEN
print

# For this example and in future examples, client refers to a Client object from captools.api.Client 
client = Client(API_TOKEN)

# The Batch resource is a collection of Batch Files. 
# The first step to digitizing your forms is to create a new batch:
### only on first run
# batch = client.create_batches({'name': BATCH_NAME})


batches = client.read_batches()
batch_id = filter(lambda x: x['name'] == BATCH_NAME, batches).pop()['id']
print "Batch ID: ", batch_id
print

# batch = client.read_batch(batch_id)
# print 'Batch: ', batch['name'], '-', batch['id']
# print