123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- # -*-coding:utf-8 -*-
- import json
- from django.shortcuts import render
- from django.http import HttpResponse
- from django.core.serializers.json import DjangoJSONEncoder
- def get_user_from_request(request):
- user = request.session.get("user", None)
- return user
- def JsonResponse(data):
- response = HttpResponse(json.dumps(data, cls=DjangoJSONEncoder),
- content_type="application/json")
- response['Access-Control-Allow-Origin'] = '*'
- return response
- def params(func):
- def _wrapper(*args, **kargs):
- request = args[0]
- if request.method == "GET":
- request.PARAMS = request.GET
- elif request.method == "POST" or request.method == "DELETE":
- if request.META.get('CONTENT_TYPE') == "application/json":
- request.PARAMS = json.loads(request.body) if request.body else {}
- else:
- request.PARAMS = request.POST
- elif request.method == "OPTIONS":
- return JsonResponse({"code": 0, "msg": "success11"})
- else:
- request.PARAMS = {}
- return func(*args, **kargs)
- return _wrapper
- def login_required(func):
- def _wrapper(*args, **kargs):
- request = args[0]
- print request.session.keys()
- if not ("user" in request.session):
- # return HttpResponseRedirect("/accounts/login/")
- res = {"code": 2, "msg": u"您尚未登录,请先登录!"}
- return JsonResponse(res)
- return func(*args, **kargs)
- return _wrapper
|