1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- from django.shortcuts import render
- from .models import PublicKey
- from .form import AddPublicKeyForm
- from django.http import HttpResponse
- from django.contrib.auth.decorators import login_required
- # from dwebsocket.decorators import accept_websocket
- import json
- DEBUG = True
- # WS_DIC = {}
- #
- #
- # # 接收ws连接
- # @accept_websocket
- # def connect_user(request):
- # if request.is_websocket():
- # WS_DIC[request.user.username] = request.websocket
- # else:
- # return HttpResponse(status=400)
- #
- #
- # def knock_knock(username, flag):
- # if username in WS_DIC.keys:
- # websocket = WS_DIC[username]
- # websocket.send(flag)
- # return True
- # else:
- # return False
- @login_required(login_url='/account/login/')
- def test(request):
- return render(request, 'test.html')
- @login_required(login_url='/account/login/')
- def add_public_key(request):
- if request.method == 'POST':
- add_public_key_form = AddPublicKeyForm(data=request.POST)
- if add_public_key_form.is_valid():
- # 新建公钥表
- data = add_public_key_form.cleaned_data
- new_public_key = PublicKey()
- new_public_key.public_key = data['public_key']
- new_public_key.user = request.user
- # 保存
- new_public_key.save()
- return HttpResponse(status=200)
- else:
- return HttpResponse(status=400)
- # 用于测试
- elif request.method == 'GET':
- if DEBUG:
- return render(request, 'add_public_key.html')
- else:
- response = ['illegal method, please use post']
- return HttpResponse(json.dumps(response))
- @login_required(login_url='/account/login/')
- def get_public_key(request):
- if request.method == 'POST':
- data = request.POST
- # 筛选出此用户的公钥
- public_keys = PublicKey.objects.filter(user__username=data['username'])
- # 将筛选得到的此用户的公钥放入列表中并用json返回
- response = []
- for public_key in public_keys:
- response.append(public_key.public_key)
- return HttpResponse(json.dumps(response), status=200)
- # 用于测试
- elif request.method == 'GET':
- if DEBUG:
- return render(request, 'get_public_key.html')
- else:
- return HttpResponse(status=400)
|