class StudyTests(TestCase): def setUp(self): # create a clinical study self.study = Study( title="Can teeth bite?", slug="can-teeth-bite", date=datetime.now(), ) def test_generic_attributes(self): self.study.save() # the item has no informative content self.assertEqual(self.study.is_uninformative, True) # no Entities in the database, so this can't be hosted_by anything self.assertEqual(self.study.hosted_by, None) # no Entities in the database, so default to settings's template self.assertEqual( self.study.get_template, settings.CMS_TEMPLATES[0][0] ) def test_link_to_more(self): self.assertEqual( self.study.auto_page_view_name, "clinical-studies" ) self.study.hosted_by = Entity(slug="slug") self.assertEqual( self.study.link_to_more(), "/clinical-studies/slug/" )
def setUp(self): # create a clinical study self.study = Study( title="Can teeth bite?", slug="can-teeth-bite", date=datetime.now(), )
def setUp(self): self.item1 = Study( title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1" ) self.item1.save() self.item2 = Study( title="older", in_lists=True, published=True, date=datetime.now()-timedelta(days=200), slug="item2" ) self.item2.save() self.item3 = Study( title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3" ) self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/"))
def setUp(self): home_page = create_page("School home page", "null.html", "en", published=True) self.school = Entity(name="School of Medicine", slug="medicine", auto_news_page=True, website=home_page) self.item1 = Study(title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1") self.item1.save() self.item2 = Study(title="older", in_lists=True, published=True, date=datetime.now() - timedelta(days=200), slug="item2") self.item2.save() self.item3 = Study(title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3") self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/"))
def setUp(self): self.item1 = Study(title="newer", slug="item1") self.adminuser = User.objects.create_user('arkestra', '*****@*****.**', 'arkestra') self.adminuser.is_staff = True self.adminuser.save()
def new_study(request): if request.method == 'POST': sform = StudyForm(request.POST, instance=Study()) if sform.is_valid(): sform.save() return HttpResponseRedirect('/viewer/new_study/') else: sform = StudyForm(instance=Study()) context = {'study_form': sform} studies = Study.objects.all() context['studies'] = studies context.update(csrf(request)) return render_to_response('viewer/new_study.html', context, context_instance=RequestContext(request))
def aggregatable_study_properties(): """ Same behaviour as /properties, but excludes fields that it doesn't make sense to generate charts / aggregations from """ app.logger.info("/aggregatable_properties") return jsonify({"properties": Study.valid_fields(True)})
def get_studies_json(): try: update_lock.acquire() global studies_text return [Study(text).enriched_json() for text in studies_text] finally: update_lock.release()
def valid_study_properties(): """ Return all the valid properties that can be used in filters and 'field' query params in the REST API. Useful in combination with '/values' to allow the user to dynamically choose fields to generate a visualization from. """ app.logger.info("/properties") return jsonify({"properties": Study.valid_fields(False)})
def setUp(self): home_page = create_page( "School home page", "null.html", "en", published=True ) self.school = Entity( name="School of Medicine", slug="medicine", auto_news_page=True, website=home_page ) self.item1 = Study( title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1" ) self.item1.save() self.item2 = Study( title="older", in_lists=True, published=True, date=datetime.now()-timedelta(days=200), slug="item2" ) self.item2.save() self.item3 = Study( title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3" ) self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/"))
class ClinicalEntityPagesTests(TestCase): def setUp(self): home_page = create_page( "School home page", "null.html", "en", published=True ) self.school = Entity( name="School of Medicine", slug="medicine", auto_news_page=True, website=home_page ) self.item1 = Study( title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1" ) self.item1.save() self.item2 = Study( title="older", in_lists=True, published=True, date=datetime.now()-timedelta(days=200), slug="item2" ) self.item2.save() self.item3 = Study( title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3" ) self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/")) def test_main_url(self): self.school.save() response = self.client.get('/clinical-studies/') self.assertEqual(response.status_code, 200) self.assertItemsEqual( response.context["lister"].lists[0].items, [self.item1, self.item2] ) def test_entity_url(self): self.school.save() response = self.client.get('/clinical-studies/medicine/') self.assertEqual(response.status_code, 200)
def setUp(self): self.item1 = Study( title="newer", slug="item1" ) self.adminuser = User.objects.create_user( 'arkestra', '*****@*****.**', 'arkestra' ) self.adminuser.is_staff = True self.adminuser.save()
def setUp(self): self.item1 = Study(title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1") self.item1.save() self.item2 = Study(title="older", in_lists=True, published=True, date=datetime.now() - timedelta(days=200), slug="item2") self.item2.save() self.item3 = Study(title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3") self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/"))
class StudyTests(TestCase): def setUp(self): # create a clinical study self.study = Study( title="Can teeth bite?", slug="can-teeth-bite", date=datetime.now(), ) def test_generic_attributes(self): self.study.save() # the item has no informative content self.assertEqual(self.study.is_uninformative, True) # no Entities in the database, so this can't be hosted_by anything self.assertEqual(self.study.hosted_by, None) # no Entities in the database, so default to settings's template self.assertEqual(self.study.get_template, settings.CMS_TEMPLATES[0][0]) def test_link_to_more(self): self.assertEqual(self.study.auto_page_view_name, "clinical-studies") self.study.hosted_by = Entity(slug="slug") self.assertEqual(self.study.link_to_more(), "/clinical-studies/slug/")
def study(): ret = {} try: # count the no of lines with open('uploads/study.csv') as f: for i, l in enumerate(f): pass total_lines = i + 1 # getting the data from the csv file csv_file = open('uploads/study.csv', 'r') line = csv_file.readline() cnt = 1 while cnt < total_lines: line = csv_file.readline() s = line.split(',') id = s[0] Paragraph_Number = id Paragraph_Text = s[1] Date_of_Upload = s[2] Paragraph_Type = s[3] Word_Count = s[4] Status = s[5] GCS_Output = s[6] GCS_Acc = s[7] GCS_Conf = s[8] AH_Output = s[9] AH_Acc = s[10] AH_Conf = s[11] Speaker = s[12] study = Study(id, Paragraph_Number, Paragraph_Text, Date_of_Upload, Paragraph_Type, Word_Count, Status, GCS_Output, GCS_Acc, GCS_Conf, AH_Output, AH_Acc, AH_Conf, Speaker) db.session.add(study) db.session.commit() ret['success'] = True ret['msg'] = 'Csv file is stored in the psql' cnt += 1 except Exception as exp: ret['success'] = False ret['error'] = '%s' % exp return jsonify(ret)
class StudyDetailTests(TestCase): def setUp(self): self.item1 = Study( title="newer", slug="item1" ) self.adminuser = User.objects.create_user( 'arkestra', '*****@*****.**', 'arkestra' ) self.adminuser.is_staff = True self.adminuser.save() def test_unpublished_clinical_study_404(self): self.item1.save() response = self.client.get("/clinical-study/item1/") self.assertEqual(response.status_code, 404) def test_unpublished_clinical_study_200_for_admin(self): self.item1.save() # log in a staff user self.client.login(username='******', password='******') response = self.client.get('/clinical-study/item1/') self.assertEqual(response.status_code, 200) def test_published_clinical_study_200_for_everyone(self): self.item1.published = True self.item1.save() # Check that the response is 200 OK. response = self.client.get('/clinical-study/item1/') self.assertEqual(response.status_code, 200) def test_published_clinical_study_context(self): self.item1.published = True self.item1.save() response = self.client.get('/clinical-study/item1/') self.assertEqual(response.context['study'], self.item1)
class StudyListTests(TestCase): def setUp(self): self.item1 = Study( title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1" ) self.item1.save() self.item2 = Study( title="older", in_lists=True, published=True, date=datetime.now()-timedelta(days=200), slug="item2" ) self.item2.save() self.item3 = Study( title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3" ) self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/")) def test_build(self): self.itemlist.build() self.assertItemsEqual( self.itemlist.items, [self.item1, self.item2] ) def test_lister_has_list(self): lister = StudiesLister(request=RequestFactory().get("/")) self.assertIsInstance(lister.lists[0], StudiesList) def test_list_has_correct_filter_fields(self): self.assertItemsEqual( self.itemlist.filter_set.fields, ["date", "status", "studytype"] )
class ClinicalEntityPagesTests(TestCase): def setUp(self): home_page = create_page("School home page", "null.html", "en", published=True) self.school = Entity(name="School of Medicine", slug="medicine", auto_news_page=True, website=home_page) self.item1 = Study(title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1") self.item1.save() self.item2 = Study(title="older", in_lists=True, published=True, date=datetime.now() - timedelta(days=200), slug="item2") self.item2.save() self.item3 = Study(title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3") self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/")) def test_main_url(self): self.school.save() response = self.client.get('/clinical-studies/') self.assertEqual(response.status_code, 200) self.assertItemsEqual(response.context["lister"].lists[0].items, [self.item1, self.item2]) def test_entity_url(self): self.school.save() response = self.client.get('/clinical-studies/medicine/') self.assertEqual(response.status_code, 200)
class StudyDetailTests(TestCase): def setUp(self): self.item1 = Study(title="newer", slug="item1") self.adminuser = User.objects.create_user('arkestra', '*****@*****.**', 'arkestra') self.adminuser.is_staff = True self.adminuser.save() def test_unpublished_clinical_study_404(self): self.item1.save() response = self.client.get("/clinical-study/item1/") self.assertEqual(response.status_code, 404) def test_unpublished_clinical_study_200_for_admin(self): self.item1.save() # log in a staff user self.client.login(username='******', password='******') response = self.client.get('/clinical-study/item1/') self.assertEqual(response.status_code, 200) def test_published_clinical_study_200_for_everyone(self): self.item1.published = True self.item1.save() # Check that the response is 200 OK. response = self.client.get('/clinical-study/item1/') self.assertEqual(response.status_code, 200) def test_published_clinical_study_context(self): self.item1.published = True self.item1.save() response = self.client.get('/clinical-study/item1/') self.assertEqual(response.context['study'], self.item1)
class StudyListTests(TestCase): def setUp(self): self.item1 = Study(title="newer", in_lists=True, published=True, date=datetime.now(), slug="item1") self.item1.save() self.item2 = Study(title="older", in_lists=True, published=True, date=datetime.now() - timedelta(days=200), slug="item2") self.item2.save() self.item3 = Study(title="unpublished", in_lists=True, published=False, date=datetime.now(), slug="item3") self.item1.save() self.itemlist = StudiesList(request=RequestFactory().get("/")) def test_build(self): self.itemlist.build() self.assertItemsEqual(self.itemlist.items, [self.item1, self.item2]) def test_lister_has_list(self): lister = StudiesLister(request=RequestFactory().get("/")) self.assertIsInstance(lister.lists[0], StudiesList) def test_list_has_correct_filter_fields(self): self.assertItemsEqual(self.itemlist.filter_set.fields, ["date", "status", "studytype"])
def add_studies(request): titles = request.POST['titles'].replace('\r', '').split('\n') pmids = request.POST['pmids'].replace('\r', '').split('\n') taxid = request.POST.get('species', None) if taxid: species = Species.objects.get(taxid=taxid) comment = request.POST['comment'] or "Adding studies" succieded = [] warned = [] failed = [] with reversion.create_revision(): #print "Titles", titles #print "pmids", pmids for title in titles: if not title: continue #print "Title:", title #study, created = Study.objects.get_or_create(title=title) study = Study(title=title) study.save() try: print vars(study) except UnicodeEncodeError as e: print e print study.reference_was_created if not study.reference_was_created:#study.reference.authors: warned.append(title) messages.add_message(request, messages.WARNING, ugettext("Already in db: %s" % title)) created = False else: created = True try: study_confirm = Study.objects.get(title__icontains=title) if study_confirm: succieded.append(title) print "Authors:", study.reference.authors messages.add_message(request, messages.SUCCESS, ugettext("Succeeded: %s" % title)) else: failed.append(title) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % title)) if taxid: study_confirm.species.add(species) print("Adding species") #study_confirm.save() except Exception as e: print e failed.append(title) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % title)) messages.add_message(request, messages.WARNING, ugettext("However added %s" % study.title)) if taxid: print("Adding species") try: study.species.add(species) except ValueError as e: #Exception Value: 'Study' instance needs to have a primary key value before a many-to-many relationship can be used. messages.add_message(request, messages.ERROR, ugettext("Error: %s" % e)) log(request, study, comment) for pmid in pmids: if not pmid: continue print "PMID:", pmid study = Study(pmid=pmid) study.save() #print "views.add.studies for pmid in pmids:", vars(study) if not study.reference_was_created: warned.append(pmid) messages.add_message(request, messages.WARNING, ugettext("Already in db: %s" % pmid)) created = False else: created = True try: study_confirm = Study.objects.get(pmid=pmid) if study_confirm: succieded.append(pmid) messages.add_message(request, messages.SUCCESS, ugettext("Succeeded: %s" % pmid)) else: failed.append(pmid) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % pmid)) if taxid: print("Adding species") study_confirm.species.add(species) except Exception as e: print e failed.append(pmid) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % pmid)) messages.add_message(request, messages.WARNING, ugettext("However added %s" % study.pmid)) if taxid: print("Adding species") try: study.species.add(species) except ValueError as e: messages.add_message(request, messages.ERROR, ugettext("Error: %s" % e)) log(request, study, comment) if isinstance(request.user, AnonymousUser): request.user = User.objects.get(username="******") reversion.set_user(request.user) reversion.set_comment(comment) if succieded: msg = "Successfully added the following %i studies: \n%s" % (len(succieded), "\n".join(succieded)) messages.add_message(request, messages.SUCCESS, ugettext(msg)) if warned: msg = "Found already in db the following %i studies: \n%s" % (len(warned), "\n".join(warned)) messages.add_message(request, messages.WARNING, ugettext(msg)) if failed: msg = "Failed to fetch information on the following %i studies: \n%s" % (len(failed), "\n".join(failed)) messages.add_message(request, messages.ERROR, ugettext(msg)) return HttpResponseRedirect('/lifespan/studies/')
def add_studies(request): titles = request.POST['titles'].replace('\r', '').split('\n') pmids = request.POST['pmids'].replace('\r', '').split('\n') taxid = request.POST.get('species', None) if taxid: species = Species.objects.get(taxid=taxid) comment = request.POST['comment'] or "Adding studies" succieded = [] warned = [] failed = [] with reversion.create_revision(): #print "Titles", titles #print "pmids", pmids for title in titles: if not title: continue #print "Title:", title #study, created = Study.objects.get_or_create(title=title) study = Study(title=title) study.save() try: print vars(study) except UnicodeEncodeError as e: print e print study.reference_was_created if not study.reference_was_created: #study.reference.authors: warned.append(title) messages.add_message(request, messages.WARNING, ugettext("Already in db: %s" % title)) created = False else: created = True try: study_confirm = Study.objects.get(title__icontains=title) if study_confirm: succieded.append(title) print "Authors:", study.reference.authors messages.add_message(request, messages.SUCCESS, ugettext("Succeeded: %s" % title)) else: failed.append(title) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % title)) if taxid: study_confirm.species.add(species) print("Adding species") #study_confirm.save() except Exception as e: print e failed.append(title) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % title)) messages.add_message( request, messages.WARNING, ugettext("However added %s" % study.title)) if taxid: print("Adding species") try: study.species.add(species) except ValueError as e: #Exception Value: 'Study' instance needs to have a primary key value before a many-to-many relationship can be used. messages.add_message(request, messages.ERROR, ugettext("Error: %s" % e)) log(request, study, comment) for pmid in pmids: if not pmid: continue print "PMID:", pmid study = Study(pmid=pmid) study.save() #print "views.add.studies for pmid in pmids:", vars(study) if not study.reference_was_created: warned.append(pmid) messages.add_message(request, messages.WARNING, ugettext("Already in db: %s" % pmid)) created = False else: created = True try: study_confirm = Study.objects.get(pmid=pmid) if study_confirm: succieded.append(pmid) messages.add_message(request, messages.SUCCESS, ugettext("Succeeded: %s" % pmid)) else: failed.append(pmid) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % pmid)) if taxid: print("Adding species") study_confirm.species.add(species) except Exception as e: print e failed.append(pmid) messages.add_message(request, messages.ERROR, ugettext("Failed: %s" % pmid)) messages.add_message(request, messages.WARNING, ugettext("However added %s" % study.pmid)) if taxid: print("Adding species") try: study.species.add(species) except ValueError as e: messages.add_message(request, messages.ERROR, ugettext("Error: %s" % e)) log(request, study, comment) if isinstance(request.user, AnonymousUser): request.user = User.objects.get(username="******") reversion.set_user(request.user) reversion.set_comment(comment) if succieded: msg = "Successfully added the following %i studies: \n%s" % ( len(succieded), "\n".join(succieded)) messages.add_message(request, messages.SUCCESS, ugettext(msg)) if warned: msg = "Found already in db the following %i studies: \n%s" % ( len(warned), "\n".join(warned)) messages.add_message(request, messages.WARNING, ugettext(msg)) if failed: msg = "Failed to fetch information on the following %i studies: \n%s" % ( len(failed), "\n".join(failed)) messages.add_message(request, messages.ERROR, ugettext(msg)) return HttpResponseRedirect('/lifespan/studies/')