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 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 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_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 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.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.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 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 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 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_get_hierarchy(self): self.assertEqual(self.h.id, Hierarchy.get_hierarchy("main").id)
def default_hierarchy(self): return Hierarchy.get_hierarchy(self.language)
def default_hierarchy(self): return Hierarchy.get_hierarchy(self.research_group)
def test_get_hierarchy(self): self.assertEqual( self.h.id, Hierarchy.get_hierarchy("main").id)