def __call__(self, request): try: response = self.get_response(request) except NoInstalledApplicationError: import madliar if settings.PROJECT_CWD != madliar.__path__: response = HttpResponse( "<center><h3>Error happend!</h3>" "<p>An error happend when madliar load the url map from " "`application.urls` module so that the server cannot work regularly. " "please check it or send an email to author.</p></center>") else: response = HttpResponse( "<center><h3>It works!</h3><p>You must install at least one app in your project.</p></center>" ) except Exception as e: if settings.DEBUG: response = HttpResponse( "<center><h3>An error happend !</h3><p>Error: %s</p></center>" % e) else: response = Http500Response() if settings.ENABLE_SYS_LOG: from madliar.config.log4 import logging from madliar.utils import get_traceback traceback = get_traceback() logging.error( "An error caused internal server error: %s, \n%s" % (e, traceback)) return response
def add_chat_log(request): room_id = request.POST.get("room_id") try: raw_msg_list = request.POST.get("msg_list") msg_list = json.loads(raw_msg_list) except Exception as e: return HttpResponse("ERROR: %s" % e) log_contents = [] for msg in msg_list: datetime_str = msg.get("datetime_str") user = msg.get("user") ul = msg.get("ul") decoration = msg.get("decoration") dl = msg.get("dl") raw_msg = msg.get("msg") if not user or not raw_msg: continue log_contents.append("[%s][%-5s][%s %s] %s -> %s\n" % (datetime_str, ul, decoration, dl, user, raw_msg)) if not log_contents: return HttpResponse("") file_name = os.path.join(LOG_PATH, "chat_%s.log" % room_id) content = "".join(log_contents).strip("\n") if not isinstance(content, unicode): content = content.decode("utf-8", errors="replace") with open(file_name, "ab") as f: print >> f, content.encode("utf-8", errors="replace") return HttpResponse(content)
def record(request): if request.method.lower() != "post": return HttpResponse("") action = request.POST.get("action") try: http_response = supported_action.run(action, request) except supported_action.ActionDoesNotExisted: http_response = HttpResponse("Action does not support.") return http_response
def timeout_response(request): try: wait_time = int(request.GET.get("time_out")) except Exception: wait_time = 65 import time time.sleep(wait_time) return HttpResponse(content="OK", content_type="text/plain")
def add_prize_log(request): try: count = int(request.POST.get("count")) except Exception: return HttpResponse("Error count") datetime_str = str(datetime.datetime.now())[:-3] provider = request.POST.get("provider") prize_type = request.POST.get("type") title = request.POST.get("title") p_url = request.POST.get("url") file_name = os.path.join(LOG_PATH, "prize_accept.log") content = "[%s][%s][%s][%s][%s][%s]" % (datetime_str, count, prize_type, provider, p_url, title) if not isinstance(content, unicode): content = content.decode("utf-8", errors="replace") with open(file_name, "ab") as f: print >> f, content.encode("utf-8", errors="replace") return HttpResponse(content)
def s(request, key): path = dao.get_shared_file(key) if not path or not os.path.exists(path): return Http404Response() try: with open(path, "rb") as f: content = f.read() except Exception: # TODO: add log. return Http500Response() base_name, ex_name = os.path.splitext(path) ex_name = ex_name.lstrip(".") content_type = "application/octet-stream" if not ex_name: content_type = "text/plain" elif ex_name.lower() in ("md", "markdown"): content_type = "text/plain" else: for ex, content_t in STATICS_FILE_MIME_TYPE: if ex_name in ex.split(" "): content_type = content_t break if not content_type.startswith("text"): return HttpResponse(content, content_type=content_type) if type(content) != unicode: try: content = content.decode("utf-8") except Exception: content = u"未能读取文件内容,其中含有不能识别的编码。" title = base_name.split("\\" if os.name in ("nt", ) else "/")[-1] context_data = { "title": title, "detail": content, "need_trans": ex_name.lower() in ("md", "markdown"), "CDN_URL": CDN_URL } return render("template/notebook/share.html", context=context_data)
def ppy(request): file_path = "/home/wwwroot/notebook/[email protected]/papaya/api.json" with open(file_path, "rb") as f: content = f.read() return HttpResponse(content=content, content_type="application/json")
def json_to_response(data): return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")
def robots_response(request): response = HttpResponse(content=("User-agent: *\n" "Disallow: /static/\n"), content_type="text/plain", charset="utf-8") return response
def favicon_response(request): with open("static/img/favicon.png", "rb") as f: content = f.read() return HttpResponse(content, content_type="image/x-icon")