def setUp(self): self.user = User.objects.create_user("test_student", "*****@*****.**", "testpassword") UserProfile.objects.get_or_create(user=self.user, gender="M", is_faculty="ST", institute="I1", specialty="S2", hispanic_latino="Y", year_of_graduation=2015, consent_participant=True, consent_not_participant=False) self.hierarchy = Hierarchy(name="main", base_url="/") self.hierarchy.save() self.root = Section.add_root(label="Root", slug="", hierarchy=self.hierarchy) self.root.append_child("Section 1", "section-1") self.root.append_child("Section 2", "section-2") self.section1 = Section.objects.get(slug="section-1") self.section2 = Section.objects.get(slug="section-2")
def handle(self, *args, **options): print "fetching content from forest..." url = "http://wacep2.forest.ccnmtl.columbia.edu/pagetree/export/" url = url + "?hierarchy=" + "wacep2.forest.ccnmtl.columbia.edu" d = requests.get(url).json() print "removing old pagetree hierarchy..." Hierarchy.objects.all().delete() print "importing the new one..." Hierarchy.from_dict(d) # override the hierarchy name (since it's coming from forest) h = Hierarchy.objects.all()[0] h.name = "main" h.save() print "pulling down uploaded files..." base_len = len("http://wacep2.forest.ccnmtl.columbia.edu/uploads/") for upload in d.get('resources', []): relative_path = upload[base_len:] relative_dir = os.path.join(*os.path.split(relative_path)[:-1]) full_dir = os.path.join(settings.MEDIA_ROOT, relative_dir) try: os.makedirs(full_dir) except OSError: pass with open(os.path.join(settings.MEDIA_ROOT, relative_path), "w") as f: print " writing %s to %s" % (upload, relative_path) f.write(requests.get(upload).text) print "done"
def page(request, path): h = Hierarchy.get_hierarchy('main') current_root = h.get_section_from_path(path) section = h.get_first_leaf(current_root) ancestors = section.get_ancestors() module = None if not section.is_root() and len(ancestors) > 1: module = ancestors[1] asset_type = request.GET.get('type', None) asset_id = request.GET.get('id', None) context = dict(section=section, module=module, root=ancestors[0]) if asset_type and asset_id: try: model = get_model("portal", asset_type) context['selected'] = model.objects.get(id=asset_id) except: msg = "We were unable to locate a <b>%s</b> at this address." context['error'] = msg % (asset_type) return context
def page(request, path): h = Hierarchy.get_hierarchy('main') current_root = h.get_section_from_path(path) section = h.get_first_leaf(current_root) ancestors = section.get_ancestors() module = None if not section.is_root() and len(ancestors) > 1: module = ancestors[1] asset_type = request.GET.get('type', None) asset_id = request.GET.get('id', None) root = None if len(ancestors) > 0: root = ancestors[0] context = dict(section=section, module=module, root=root) if asset_type and asset_id: try: model = apps.get_model("portal", asset_type) context['selected'] = model.objects.get(id=asset_id) except (ObjectDoesNotExist, LookupError, ValueError): msg = "We were unable to locate a <b>%s</b> at this address." context['error'] = msg % (asset_type) return context
def last_location_url(self): if self.percent_complete() == 0: hierarchy = Hierarchy.get_hierarchy(self.research_group) section = hierarchy.get_root().get_first_child() else: section = self.last_location() return section.get_absolute_url()
def percent_complete(self): hierarchy = Hierarchy.get_hierarchy(self.research_group) pages = hierarchy.get_root().get_descendants().count() visits = UserPageVisit.objects.filter( user=self.user, section__hierarchy=hierarchy).count() if pages > 0: return int(visits / float(pages) * 100) else: return 0
def percent_complete_hierarchy(self, hierarchy_name): hierarchy = Hierarchy.get_hierarchy(hierarchy_name) pages = hierarchy.get_root().get_descendants().count() visits = UserPageVisit.objects.filter( user=self.user, section__hierarchy=hierarchy).count() if pages > 0: return int(visits / float(pages) * 100) else: return 0
def last_location(self): hierarchy = Hierarchy.get_hierarchy(self.language) upv = UserPageVisit.objects.filter( user=self.user, section__hierarchy=hierarchy).order_by( "-last_visit") if upv.count() < 1: return hierarchy.get_root() else: return upv[0].section
def last_location(self): hierarchy = Hierarchy.get_hierarchy(self.role()) visits = UserPageVisit.objects.filter( user=self.user).order_by('-last_visit') if visits.count() < 1: return hierarchy.get_first_leaf(hierarchy.get_root()) else: return visits.first().section
def percent_complete(self): hierarchy = Hierarchy.get_hierarchy(self.role()) pages = len(hierarchy.get_root().get_descendants()) + 1 visits = UserPageVisit.objects.filter(user=self.user, section__hierarchy=hierarchy) if pages: return int(len(visits) / float(pages) * 100) else: return 0
def last_location(self): hierarchy = Hierarchy.get_hierarchy(self.research_group) upv = UserPageVisit.objects.filter( user=self.user, section__hierarchy=hierarchy).order_by('-last_visit') if upv.count() < 1: return hierarchy.get_root() else: return upv[0].section
def last_access_hierarchy(self, hierarchy_name): hierarchy = Hierarchy.get_hierarchy(hierarchy_name) upv = UserPageVisit.objects.filter( user=self.user, section__hierarchy=hierarchy).order_by( "-last_visit") if upv.count() < 1: return None else: return upv[0].last_visit
def next_unlocked_section_url(self): hierarchy = Hierarchy.get_hierarchy(self.research_group) last_section = hierarchy.get_last_leaf(hierarchy.get_root()) (visited, next_section) = last_section.gate_check(self.user) if visited: return last_section.get_absolute_url() else: return next_section.get_absolute_url()
def setUp(self): self.c = Client() self.factory = RequestFactory() self.user = User.objects.create_user('test_student', '*****@*****.**', 'testpassword') self.user.save() self.hierarchy = Hierarchy(name="main", base_url="/") self.hierarchy.save() root = Section.add_root(label="Root", slug="", hierarchy=self.hierarchy) root.append_child("Section 1", "section-1") root.append_child("Section 2", "section-2") self.section1 = Section.objects.get(slug="section-1") self.section2 = Section.objects.get(slug="section-2")
def time_spent(self, hierarchy_name): hierarchy = Hierarchy.get_hierarchy(hierarchy_name) visits = UserPageVisit.objects.filter(user=self.user, section__hierarchy=hierarchy) seconds = 0 if (visits.count() > 0): start = visits.order_by('first_visit')[0].first_visit end = visits.order_by('-last_visit')[0].last_visit seconds = (end - start).total_seconds() / 60 return seconds
def form_valid(self, form): hierarchy_id = self.kwargs.get('hierarchy_id') original = get_object_or_404(Hierarchy, id=hierarchy_id) name = form.cleaned_data['name'] base_url = form.cleaned_data['base_url'] clone = Hierarchy.clone(original, name, base_url) self.success_url = clone.get_root().get_edit_url() return super(CloneHierarchyView, self).form_valid(form)
def handle(self, *args, **options): if not settings.DEBUG: print("this should never be run on production") return if not hasattr(settings, 'PROD_BASE_URL'): print("you must set PROD_BASE_URL") return print("fetching content from prod...") url = settings.PROD_BASE_URL + "_pagetree/export/" if args: url = url + "?hierarchy=" + args[0] d = loads(GET(url)) print("removing old pagetree hierarchy...") Hierarchy.objects.all().delete() print("importing the new one...") Hierarchy.from_dict(d) if not hasattr(settings, 'PROD_MEDIA_BASE_URL'): print("in order to pull down uploaded files,") print("you must set PROD_MEDIA_BASE_URL") print("pulling down uploaded files...") base_len = len(settings.PROD_MEDIA_BASE_URL) for upload in d.get('resources', []): relative_path = upload[base_len:] relative_dir = os.path.join(*os.path.split(relative_path)[:-1]) full_dir = os.path.join(settings.MEDIA_ROOT, relative_dir) try: os.makedirs(full_dir) except OSError: pass with open(os.path.join(settings.MEDIA_ROOT, relative_path), "w") as f: print(" writing %s to %s" % (upload, relative_path)) f.write(GET(upload)) print("done")
def handle(self, *args, **options): if not settings.DEBUG: print "this should never be run on production" return if not hasattr(settings, 'PROD_BASE_URL'): print "you must set PROD_BASE_URL" return print "fetching content from prod..." url = settings.PROD_BASE_URL + "_pagetree/export/" if args: url = url + "?hierarchy=" + args[0] d = loads(GET(url)) print "removing old pagetree hierarchy..." Hierarchy.objects.all().delete() print "importing the new one..." Hierarchy.from_dict(d) if not hasattr(settings, 'PROD_MEDIA_BASE_URL'): print "in order to pull down uploaded files," print "you must set PROD_MEDIA_BASE_URL" print "pulling down uploaded files..." base_len = len(settings.PROD_MEDIA_BASE_URL) for upload in d.get('resources', []): relative_path = upload[base_len:] relative_dir = os.path.join(*os.path.split(relative_path)[:-1]) full_dir = os.path.join(settings.MEDIA_ROOT, relative_dir) try: os.makedirs(full_dir) except OSError: pass with open(os.path.join(settings.MEDIA_ROOT, relative_path), "w") as f: print " writing %s to %s" % (upload, relative_path) f.write(GET(upload)) print "done"
def page(request, path): h = Hierarchy.get_hierarchy('main') current_root = h.get_section_from_path(path) section = h.get_first_leaf(current_root) ss = SiteState.objects.get_or_create(user=request.user)[0] # Skip to the first leaf, make sure to mark these sections as visited if (current_root != section): ancestors = section.get_ancestors() ss.set_has_visited(ancestors) return HttpResponseRedirect(section.get_absolute_url()) # the previous node is the last leaf, if one exists. prev = section.sitesection.get_previous_site_section() next = section.sitesection.get_next_site_section() # Is this section unlocked now? can_access = _unlocked(section, request.user, prev, ss) if can_access: # just to avoid drama, only save last location if the section # is available on both sites. ss.save_last_location(request.path, section) # construct the left nav up here too. depth = section.depth() if depth == 3: parent = section.get_parent() elif depth == 4: parent = section.get_parent().get_parent() elif depth == 5: parent = section.get_parent().get_parent().get_parent() else: parent = section leftnav = _construct_menu(request.user, parent, section, ss) # ok let's try this ss.set_has_visited([section]) return render(request, 'carr_main/page.html', dict(section=section, accessible=can_access, previous=prev, next=next, depth=depth, leftnav=leftnav))
def test_clone_hierarchy(self): self.section1.add_pageblock_from_dict({ 'block_type': 'Test Block', 'body': 'test body', }) duplicate = Hierarchy.clone(self.h, 'foo', '/foo/') descendants = duplicate.get_root().get_descendants() self.assertTrue(descendants[0].label, 'Section 1') self.assertTrue(descendants[0].depth, 1) self.assertEquals(descendants[0].pageblock_set.count(), 1) self.assertTrue(descendants[1].label, 'Section 2') self.assertTrue(descendants[1].depth, 1) self.assertTrue(descendants[2].label, 'Section 3') self.assertTrue(descendants[2].depth, 1) self.assertTrue(descendants[3].label, 'Section 4') self.assertTrue(descendants[3].depth, 2) self.assertTrue(descendants[4].label, 'Section 5') self.assertTrue(descendants[4].depth, 3)
class UserProfileTest(TestCase): def setUp(self): self.user = User.objects.create_user("test_student", "*****@*****.**", "testpassword") UserProfile.objects.get_or_create(user=self.user, gender="M", is_faculty="ST", institute="I1", specialty="S2", hispanic_latino="Y", year_of_graduation=2015, consent_participant=True, consent_not_participant=False) self.hierarchy = Hierarchy(name="main", base_url="/") self.hierarchy.save() self.root = Section.add_root(label="Root", slug="", hierarchy=self.hierarchy) self.root.append_child("Section 1", "section-1") self.root.append_child("Section 2", "section-2") self.section1 = Section.objects.get(slug="section-1") self.section2 = Section.objects.get(slug="section-2") def tearDown(self): self.user.delete() self.hierarchy.delete() def test_set_has_visited(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertFalse(profile.get_has_visited(self.section1)) self.assertFalse(profile.get_has_visited(self.section2)) profile.set_has_visited([self.section1, self.section2]) self.assertTrue(profile.get_has_visited(self.section1)) self.assertTrue(profile.get_has_visited(self.section2)) def test_last_location(self): user = User.objects.get(username="******") # By default, the 1st leaf is returned if there are no visits self.assertEquals(user.profile.last_location(), self.section1) user.profile.set_has_visited([self.section1]) self.assertEquals(user.profile.last_location(), self.section1) user.profile.set_has_visited([self.section2]) self.assertEquals(user.profile.last_location(), self.section2) user.profile.set_has_visited([self.section1]) self.assertEquals(user.profile.last_location(), self.section1) def test_user_unicode(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) uni_name = UserProfile.__unicode__(profile) self.assertEqual(uni_name, "test_student") def test_user_display_name(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) display_name = UserProfile.display_name(profile) self.assertEqual(display_name, "test_student") def test_percent_complete(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertEquals(0, profile.percent_complete()) profile.set_has_visited([self.root, self.section1]) self.assertEquals(66, profile.percent_complete()) profile.set_has_visited([self.section2]) self.assertEquals(100, profile.percent_complete()) def test_percent_complete_null_hierarchy(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) profile.speciality = "pediatrics" self.assertEquals(0, profile.percent_complete()) def test_is_role_student(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) profile.is_faculty = "FA" profile.save() self.assertFalse(profile.is_role_student()) self.assertTrue(profile.is_role_faculty()) profile.is_faculty = "OT" profile.save() self.assertFalse(profile.is_role_student()) self.assertFalse(profile.is_role_faculty()) profile.is_faculty = "ST" profile.save() self.assertTrue(profile.is_role_student()) self.assertFalse(profile.is_role_faculty()) def test_default_role(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertEquals("main", profile.role()) def test_role(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) # pre-doctoral self.assertEquals(profile.role(), "main") # verify being a faculty doesn't change this selection profile.is_faculty = "FA" profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S1" profile.save() self.assertEquals(profile.role(), "general") profile.specialty = "S2" # Pre-Doctoral Student" profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S3" # Endodontics profile.save() self.assertEquals(profile.role(), "endodontics") profile.specialty = "S4" # Oral and Maxillofacial Surgery" profile.save() self.assertEquals(profile.role(), "surgery") profile.specialty = "S5" # Pediatric Dentistry profile.save() self.assertEquals(profile.role(), "pediatrics") profile.specialty = "S6" # Periodontics profile.save() self.assertEquals(profile.role(), "perio") profile.specialty = "S7" # Prosthodontics profile.save() self.assertEquals(profile.role(), "general") profile.specialty = "S8" # Orthodontics profile.save() self.assertEquals(profile.role(), "orthodontics") profile.specialty = "S9" # Other profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S10" # Dental Public Health profile.save() self.assertEquals(profile.role(), "main")
def test_edit_form(self): h = Hierarchy.from_dict({'name': 'main', 'base_url': ""}) root = h.get_root() sc = SectionCss.objects.create(section_css=root, css_field="") self.assertTrue(sc.edit_form() is not None)
def test_get_hierarchy(self): self.assertEqual(self.h.id, Hierarchy.get_hierarchy("main").id)
def setUp(self): self.h = Hierarchy.from_dict({'name': "main", 'base_url': ""})
def test_get_hierarchy(self): self.assertEqual( self.h.id, Hierarchy.get_hierarchy("main").id)
class UserProfileTest(TestCase): def setUp(self): self.user = User.objects.create_user("test_student", "*****@*****.**", "testpassword") UserProfile.objects.get_or_create(user=self.user, gender="M", is_faculty="ST", institute="I1", specialty="S2", hispanic_latino="Y", year_of_graduation=2015, consent_participant=True, consent_not_participant=False) self.hierarchy = Hierarchy(name="main", base_url="/") self.hierarchy.save() self.root = Section.add_root(label="Root", slug="", hierarchy=self.hierarchy) self.root.append_child("Section 1", "section-1") self.root.append_child("Section 2", "section-2") self.section1 = Section.objects.get(slug="section-1") self.section2 = Section.objects.get(slug="section-2") def tearDown(self): self.user.delete() self.hierarchy.delete() def test_set_has_visited(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertFalse(profile.get_has_visited(self.section1)) self.assertFalse(profile.get_has_visited(self.section2)) profile.set_has_visited([self.section1, self.section2]) self.assertTrue(profile.get_has_visited(self.section1)) self.assertTrue(profile.get_has_visited(self.section2)) def test_last_location(self): user = User.objects.get(username="******") # By default, the 1st leaf is returned if there are no visits self.assertEquals(user.profile.last_location(), self.section1) user.profile.set_has_visited([self.section1]) self.assertEquals(user.profile.last_location(), self.section1) user.profile.set_has_visited([self.section2]) self.assertEquals(user.profile.last_location(), self.section2) user.profile.set_has_visited([self.section1]) self.assertEquals(user.profile.last_location(), self.section1) def test_user_unicode(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) uni_name = smart_text(profile) self.assertEqual(uni_name, "test_student") def test_user_display_name(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) display_name = UserProfile.display_name(profile) self.assertEqual(display_name, "test_student") def test_percent_complete(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertEquals(0, profile.percent_complete()) profile.set_has_visited([self.root, self.section1]) self.assertEquals(66, profile.percent_complete()) profile.set_has_visited([self.section2]) self.assertEquals(100, profile.percent_complete()) def test_percent_complete_null_hierarchy(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) profile.speciality = "pediatrics" self.assertEquals(0, profile.percent_complete()) def test_is_role_student(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) profile.is_faculty = "FA" profile.save() self.assertFalse(profile.is_role_student()) self.assertTrue(profile.is_role_faculty()) profile.is_faculty = "OT" profile.save() self.assertFalse(profile.is_role_student()) self.assertFalse(profile.is_role_faculty()) profile.is_faculty = "ST" profile.save() self.assertTrue(profile.is_role_student()) self.assertFalse(profile.is_role_faculty()) def test_default_role(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) self.assertEquals("main", profile.role()) def test_role(self): user = User.objects.get(username="******") profile = UserProfile.objects.get(user=user) # pre-doctoral self.assertEquals(profile.role(), "main") # verify being a faculty doesn't change this selection profile.is_faculty = "FA" profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S1" profile.save() self.assertEquals(profile.role(), "general") profile.specialty = "S2" # Pre-Doctoral Student" profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S3" # Endodontics profile.save() self.assertEquals(profile.role(), "endodontics") profile.specialty = "S4" # Oral and Maxillofacial Surgery" profile.save() self.assertEquals(profile.role(), "surgery") profile.specialty = "S5" # Pediatric Dentistry profile.save() self.assertEquals(profile.role(), "pediatrics") profile.specialty = "S6" # Periodontics profile.save() self.assertEquals(profile.role(), "perio") profile.specialty = "S7" # Prosthodontics profile.save() self.assertEquals(profile.role(), "general") profile.specialty = "S8" # Orthodontics profile.save() self.assertEquals(profile.role(), "orthodontics") profile.specialty = "S9" # Other profile.save() self.assertEquals(profile.role(), "main") profile.specialty = "S10" # Dental Public Health profile.save() self.assertEquals(profile.role(), "main")
def default_hierarchy(self): return Hierarchy.get_hierarchy(self.language)
def test_edit_form(self): h = Hierarchy.from_dict({'name': 'main', 'base_url': ""}) root = h.get_root() m = ModuleType.objects.create(module_type=root, info="") self.assertTrue(m.edit_form() is not None)
def default_hierarchy(self): return Hierarchy.get_hierarchy(self.research_group)
def test_edit_form(self): h = Hierarchy.from_dict({'name': 'main', 'base_url': ""}) root = h.get_root() d = DashboardInfo.objects.create(dashboard=root, info="") f = d.edit_form() self.assertTrue(f is not None)
class TestViews(TestCase): def setUp(self): self.c = Client() self.factory = RequestFactory() self.user = User.objects.create_user('test_student', '*****@*****.**', 'testpassword') self.user.save() self.hierarchy = Hierarchy(name="main", base_url="/") self.hierarchy.save() root = Section.add_root(label="Root", slug="", hierarchy=self.hierarchy) root.append_child("Section 1", "section-1") root.append_child("Section 2", "section-2") self.section1 = Section.objects.get(slug="section-1") self.section2 = Section.objects.get(slug="section-2") def tearDown(self): self.user.delete() def test_index(self): # it should redirect us somewhere. response = self.c.get("/") self.assertEquals(response.status_code, 302) # for now, we don't care where. really, we # are just making sure it's not a 500 error # at this point def test_smoke(self): # run the smoketests. we don't care if they pass # or fail, we just want to make sure that the # smoketests themselves don't have an error response = self.c.get("/smoketest/") self.assertEquals(response.status_code, 200) def test_accessible(self): pass # Need better test... def test_is_accessible(self): pass def test_clear_state(self): pass def test_consent_no_profile(self): self.c = Client() self.c.login(username='******', password='******') self.response = self.c.get('/', follow=True) self.assertEqual(self.response.status_code, 200) self.assertEquals(self.response.redirect_chain[0][1], 302) self.assertEquals(self.response.templates[0].name, "main/create_profile.html") def test_consent_incomplete_profile(self): # User has a profile, but does not have "consent" or other # special fields filled out. Redirect to create profile UserProfile.objects.create(user=self.user, gender='M', is_faculty='ST', institute='I1', specialty='S1', hispanic_latino='Y', year_of_graduation=2015, consent_participant=False, consent_not_participant=False) self.c = Client() self.c.login(username='******', password='******') self.response = self.c.get('/', follow=True) self.assertEqual(self.response.status_code, 200) self.assertEquals(self.response.templates[0].name, "main/create_profile.html") self.assertEquals(self.response.redirect_chain[0][1], 302) def test_consent_complete_profile(self): # User has a complete profile profile = UserProfile.objects.create(user=self.user, gender='M', is_faculty='ST', institute='I1', specialty='S1', hispanic_latino='Y', year_of_graduation=2015, consent_participant=True, consent_not_participant=False) profile.gender = 'F' profile.is_faculty = 'ST' profile.specialty = 'S10' profile.year_of_graduation = 2014 profile.consent_participant = True profile.consent_not_participant = False profile.save() self.c = Client() self.c.login(username='******', password='******') self.response = self.c.get('/', follow=True) self.assertEqual(self.response.status_code, 200) self.assertEquals(self.response.templates[0].name, "main/index.html") self.assertEquals(len(self.response.redirect_chain), 0)