Get the cms up to the point of rendering a template
This commit is contained in:
committed by
Matthew Mongeau
parent
e845221f4a
commit
5ff05f7a95
0
common/lib/util/__init__.py
Normal file
0
common/lib/util/__init__.py
Normal file
16
common/lib/util/cache.py
Normal file
16
common/lib/util/cache.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
This module aims to give a little more fine-tuned control of caching and cache
|
||||
invalidation. Import these instead of django.core.cache.
|
||||
|
||||
Note that 'default' is being preserved for user session caching, which we're
|
||||
not migrating so as not to inconvenience users by logging them all out.
|
||||
"""
|
||||
from django.core import cache
|
||||
|
||||
# If we can't find a 'general' CACHE defined in settings.py, we simply fall back
|
||||
# to returning the default cache. This will happen with dev machines.
|
||||
try:
|
||||
cache = cache.get_cache('general')
|
||||
except Exception:
|
||||
cache = cache.cache
|
||||
|
||||
20
common/lib/util/memcache.py
Normal file
20
common/lib/util/memcache.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
This module provides a KEY_FUNCTION suitable for use with a memcache backend
|
||||
so that we can cache any keys, not just ones that memcache would ordinarily accept
|
||||
"""
|
||||
from django.utils.encoding import smart_str
|
||||
import hashlib
|
||||
import urllib
|
||||
|
||||
def fasthash(string):
|
||||
m = hashlib.new("md4")
|
||||
m.update(string)
|
||||
return m.hexdigest()
|
||||
|
||||
def safe_key(key, key_prefix, version):
|
||||
safe_key = urllib.quote_plus(smart_str(key))
|
||||
|
||||
if len(safe_key) > 250:
|
||||
safe_key = fasthash(safe_key)
|
||||
|
||||
return ":".join([key_prefix, str(version), safe_key])
|
||||
16
common/lib/util/middleware.py
Normal file
16
common/lib/util/middleware.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponseServerError
|
||||
|
||||
log = logging.getLogger("mitx")
|
||||
|
||||
class ExceptionLoggingMiddleware(object):
|
||||
"""Just here to log unchecked exceptions that go all the way up the Django
|
||||
stack"""
|
||||
|
||||
if not settings.TEMPLATE_DEBUG:
|
||||
def process_exception(self, request, exception):
|
||||
log.exception(exception)
|
||||
return HttpResponseServerError("Server Error - Please try again later.")
|
||||
|
||||
3
common/lib/util/models.py
Normal file
3
common/lib/util/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
16
common/lib/util/tests.py
Normal file
16
common/lib/util/tests.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
||||
Replace this with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
||||
88
common/lib/util/views.py
Normal file
88
common/lib/util/views.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import datetime
|
||||
import json
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.context_processors import csrf
|
||||
from django.core.mail import send_mail
|
||||
from django.http import Http404
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from mitxmako.shortcuts import render_to_response, render_to_string
|
||||
|
||||
import capa.calc
|
||||
import track.views
|
||||
|
||||
def calculate(request):
|
||||
''' Calculator in footer of every page. '''
|
||||
equation = request.GET['equation']
|
||||
try:
|
||||
result = capa.calc.evaluator({}, {}, equation)
|
||||
except:
|
||||
event = {'error':map(str,sys.exc_info()),
|
||||
'equation':equation}
|
||||
track.views.server_track(request, 'error:calc', event, page='calc')
|
||||
return HttpResponse(json.dumps({'result':'Invalid syntax'}))
|
||||
return HttpResponse(json.dumps({'result':str(result)}))
|
||||
|
||||
def send_feedback(request):
|
||||
''' Feeback mechanism in footer of every page. '''
|
||||
try:
|
||||
username = request.user.username
|
||||
email = request.user.email
|
||||
except:
|
||||
username = "anonymous"
|
||||
email = "anonymous"
|
||||
|
||||
try:
|
||||
browser = request.META['HTTP_USER_AGENT']
|
||||
except:
|
||||
browser = "Unknown"
|
||||
|
||||
feedback = render_to_string("feedback_email.txt",
|
||||
{"subject":request.POST['subject'],
|
||||
"url": request.POST['url'],
|
||||
"time": datetime.datetime.now().isoformat(),
|
||||
"feedback": request.POST['message'],
|
||||
"email":email,
|
||||
"browser":browser,
|
||||
"user":username})
|
||||
|
||||
send_mail("MITx Feedback / " +request.POST['subject'],
|
||||
feedback,
|
||||
settings.DEFAULT_FROM_EMAIL,
|
||||
[ settings.DEFAULT_FEEDBACK_EMAIL ],
|
||||
fail_silently = False
|
||||
)
|
||||
return HttpResponse(json.dumps({'success':True}))
|
||||
|
||||
def info(request):
|
||||
''' Info page (link from main header) '''
|
||||
return render_to_response("info.html", {})
|
||||
|
||||
# From http://djangosnippets.org/snippets/1042/
|
||||
def parse_accept_header(accept):
|
||||
"""Parse the Accept header *accept*, returning a list with pairs of
|
||||
(media_type, q_value), ordered by q values.
|
||||
"""
|
||||
result = []
|
||||
for media_range in accept.split(","):
|
||||
parts = media_range.split(";")
|
||||
media_type = parts.pop(0)
|
||||
media_params = []
|
||||
q = 1.0
|
||||
for part in parts:
|
||||
(key, value) = part.lstrip().split("=", 1)
|
||||
if key == "q":
|
||||
q = float(value)
|
||||
else:
|
||||
media_params.append((key, value))
|
||||
result.append((media_type, tuple(media_params), q))
|
||||
result.sort(lambda x, y: -cmp(x[2], y[2]))
|
||||
return result
|
||||
|
||||
def accepts(request, media_type):
|
||||
"""Return whether this request has an Accept header that matches type"""
|
||||
accept = parse_accept_header(request.META.get("HTTP_ACCEPT", ""))
|
||||
return media_type in [t for (t, p, q) in accept]
|
||||
Reference in New Issue
Block a user