def create_studies(study_settings): """ Create Study entries in the database for the supplied study_settings """ try: study = Study.objects.get(name=study_settings.name) except Study.DoesNotExist: study = Study(name=study_settings.name) study.stub = study_settings.name_stub study.description = study_settings.description #study.start_date = Date #study.end_date = Date study.started = True study.consent = study_settings.informed_consent study.instructions = study_settings.instructions study.eligibility = study_settings.eligibility study.reward = study_settings.reward study.task_session_dur = 1 study.assess_blocks = 1 study.assess_trials = 1 study.save() return study
def handle(self, *args, **options): user = User.objects.get(id=1) diccio_f = open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'regionalismos.json'))) diccio = json.load(diccio_f) with transaction.commit_on_success(): st = Study (title = 'Regionalismos', user = user, authors = "Javier De la Rosa y Roberto Ulloa") st.save() self.create_location(user,name=u'Área del Río de la Plata') self.create_location(user,name=u'América') self.create_location(user,name=u'América Central') self.create_location(user,name=u'América Meridional') print "loading the objects into a dictionary..." grs = dict ((o.title, o) for o in GeospatialReference.objects.all()) i=0 for entry in diccio: #grs = GeospatialReference.objects.filter(title=entry['user']) #if len(grs) > 0: #gr = grs[0] if entry['user'] in grs: gr = grs[entry['user']] elif entry['user'] in [u'Ciudad de México', u'Guadalajara']: gr = grs['Mexico'] #gr = GeospatialReference.objects.get(title='Mexico') else: gr = grs['Spain'] #gr = GeospatialReference.objects.get(title='Spain') spkrs = Speaker.objects.filter(location=gr) if len(spkrs) > 0: spkr = spkrs[0] else: spkr = self.create_speaker(user, st ,gr, entry['user']) self.create_production(user, spkr, entry['word'], entry['ipa'], entry['rfe'], gr, entry['definition']) i+=1 if i%100 == 0: print str(i) + ' productions'
def index(request): if request.user.is_authenticated: if request.method == "POST": studyName = request.POST.get('studyName', None) patientName = request.POST.get('patientName', None) studyID = request.POST.get('studyID', None) fileUpload = request.FILES.get('fileUpload', None) if studyID is not None and fileUpload is not None: qs = Study.objects.filter(studyID=studyID) if qs: study = qs[0] saveName = str(patientName) + '_' + str( studyName) + '_' + str(newStudy.id) + '.tar.xz' fs = FileSystemStorage() saveName = fs.save(saveName, fileUpload) payload = {'studyID': str(studyID)} headers = { 'Authorization': 'Token ' + settings.PROCESSING_SERVER_TOKEN } with open( os.path.join(settings.UNPROC_DATA_SAVE_DIR, saveName), 'rb') as f: res = requests.post(settings.PROCESSING_SERVER + 'task', headers=headers, data=payload, files={'fileUpload': f}) return HttpResponse(status=res.status_code) else: HttpResponse(status=404) if studyName is None or patientName is None or fileUpload is None: return HttpResponse(status=400) newStudy = Study(pub_date=datetime.datetime.now(), name=studyName, patient=patientName) newStudy.save() saveName = str(patientName) + '_' + str(studyName) + '_' + str( newStudy.id) + '.tar.xz' print('1:' + saveName) fs = FileSystemStorage() saveName = fs.save(saveName, fileUpload) print('2:' + saveName) ##need to add doctor to the study somehow?? ##post request to other server now payload = { 'studyName': str(studyName), 'studyID': str(newStudy.id), 'patientName': str(patientName) } headers = { 'Authorization': 'Token ' + settings.PROCESSING_SERVER_TOKEN } with open(os.path.join(settings.UNPROC_DATA_SAVE_DIR, saveName), 'rb') as f: requests.post(settings.PROCESSING_SERVER + '/task', headers=headers, data=payload, files={'fileUpload': f}) return HttpResponse(status=200) elif request.method != "GET": #no other requests allowed return HttpResponse(status=400) #below handle Handle GET for /studies and studyID = request.GET.get('studyID', None) studies = request.user.study_set.all( ) #gets only the studies the user has accessed to if studyID is None: #print all studies this user can see s = list() for study in studies: s.append({ "studyName": str(study.name), "patientName": str(study.patient), "studyID": str(study.id), "studyStatus": str(study.status) }) return JsonResponse(s, safe=False) else: # handle /studies?studyID=num try: studyID = int( studyID ) #studyID comes in as string for some reason, must cast it. except: return HttpResponse( status=404 ) #if it can't cast, invalid query sent so we will return not found for study in studies: if studyID == study.id: if not study.available: if study.isProcessing: return HttpResponse('Not ready') else: return HttpResponse( 'No data processing and no study available') my_data = open(study.data_loc, 'rb') response = HttpResponse( my_data.read(), content_type='application/octet-stream') response['Content-Encoding'] = 'tar' response[ 'Content-Disposition'] = 'attachment; filename="study' + str( study.id) + '.tar.xz"' saveAccess = StudyAccess(study=study, time=datetime.datetime.now(), user=request.user) saveAccess.save() return response return HttpResponse(status=404) #not found else: return HttpResponse(status=401)