def date_page(request, page_code, page_type_code=None): if page_type_code is None: page_type = return_default_page_type() else: try: page_type = PageType.objects.get(code=page_type_code) except: return None try: thepage = Page.object.get(code=page_code, page_type=page_type) except: return None latest_date=thepage.date_modified try: last_image=thepage.image_set.latest("date_modified").date_modified latest_date=max(latest_date,last_image) except: pass try: last_applet=thepage.applet_set.latest("date_modified").date_modified latest_date=max(latest_date,last_applet) except: pass try: last_video=thepage.video_set.latest("date_modified").date_modified latest_date=max(latest_date,last_video) except: pass return latest_date
def pageview(request, page_code, page_type_code=None, overview=False): if page_type_code is None: page_type = return_default_page_type() else: page_type = get_object_or_404(PageType, code=page_type_code) if page_type.default: # for default type, don't allow longer URL with page_type_code # so that don't have two URLs pointing to same page from django.http import Http404 raise Http404('No Page matches the given query.') thepage = get_object_or_404(Page, code=page_code, page_type=page_type) # get related pages max_keyword_matches=5 max_total_related=10 related_pages = get_all_related_pages(thepage,max_keyword_matches,max_total_related) if related_pages.get('generic') or related_pages.get('lighter') \ or related_pages.get('depth'): manual_links=True else: manual_links=False # turn off google analytics for localhost/staging or hidden noanalytics=False if settings.SITE_ID <= 2 or thepage.hidden: noanalytics=True if request.method == 'GET': if "logout" in request.GET: auth.logout(request) notation_system=None notation_config=None notation_system_form=None if(thepage.notation_systems.all()): class NotationSystemForm(forms.Form): notation_system = forms.ModelChoiceField(queryset=thepage.notation_systems.all(), initial=1) # if submitted the notation_system form if request.method == 'POST': notation_system_form = NotationSystemForm(request.POST) if notation_system_form.is_valid(): notation_system = notation_system_form.cleaned_data['notation_system'] request.session['notation_config'] = notation_system.configfile notation_config = notation_system.configfile # if invalid form, get notation config from session else: notation_config = request.session.get('notation_config', '') # if did not submit the notation_system form else: notation_config = request.session.get('notation_config', None) if(notation_config): try: notation_system = thepage.notation_systems.get(configfile=notation_config) except: notation_system = None notation_config = None # if got the notation sytem from the session, # show that system selected in the form if(notation_system): notation_system_form = NotationSystemForm({'notation_system': notation_system.pk}) # otherwise show an unbound form with default as initial value else: notation_system_form = NotationSystemForm() # if have midefault system, delete it from notation_config # since template already has midefault hard coded in it if(notation_config=='midefault'): notation_config=None from micourses.utils import find_last_course last_course = find_last_course(user=request.user, request=request) # find thread content from active threads. # put thread content from any # enrolled course or last course viewed at top thread_content_list = list(thepage.thread_content_set\ .filter(course__active=True)) if last_course: for tc in thepage.thread_content_set.filter(course=last_course)\ .reverse(): try: thread_content_list.remove(tc) except ValueError: pass thread_content_list.insert(0,tc) # render page text with extra template tags context = Context({}) from midocs.functions import return_new_auxiliary_data context['_auxiliary_data_'] = return_new_auxiliary_data() context['_auxiliary_data_']['page_type'] = page_type.code context['thepage'] = thepage context['notation_system']=notation_system context['STATIC_URL'] = settings.STATIC_URL context['MEDIA_URL'] = settings.MEDIA_URL context['last_course'] = last_course context['thread_content_list'] = thread_content_list if thepage.text: try: rendered_text = Template("{% load mi_tags question_tags %}"+thepage.text).render(context) except Exception as e: rendered_text = "Template error in text (TMPLERR): %s" % e else: rendered_text = "" if thepage.header: try: rendered_header = Template("{% load mi_tags question_tags %}"+thepage.header).render(context) except Exception as e: rendered_header = "" rendered_text = "<p>Template error in text (TMPLERR): %s</p> %s" \ % (e, rendered_text) else: rendered_header = "" if thepage.javascript: try: rendered_javascript = Template("{% load mi_tags question_tags %}"+thepage.javascript).render(context) except Exception as e: rendered_javascript = "" rendered_text = "<p>Template error in text (TMPLERR): %s</p> %s" \ % (e, rendered_text) else: rendered_javascript = "" templates = ["midocs/%s_detail.html" % page_type.code, "midocs/page_detail.html"] if request.GET.get("bare"): templates = ["midocs/%s_bare.html" % page_type.code, "midocs/page_bare.html"] + templates context.update({'related_pages': related_pages, 'manual_links': manual_links, 'notation_config': notation_config, 'notation_system_form': notation_system_form, 'noanalytics': noanalytics, 'rendered_text': rendered_text, 'rendered_header': rendered_header, 'rendered_javascript': rendered_javascript, }); return render(request, templates, context=context.flatten() )