from django.contrib.auth import authenticate from django.db.models import F from django.http import JsonResponse from django.shortcuts import HttpResponse, render, redirect, reverse from django.contrib.auth.decorators import login_required from django.views.decorators.cache import cache_page from django.core.cache import cache from django.conf import settings from .forms import * from .models import * from .qingcloud import putImage import datetime, requests, json from django.views.decorators.csrf import csrf_exempt from django.views.decorators.clickjacking import xframe_options_sameorigin from .csdn import get_image #@cache_page(60 * 15) def index(request): name = None if request.user.is_authenticated: name = User.last_name data = cache.get('csdn') if data is None: with open('App/csdn.json', 'r') as f: data = json.load(f) return render(request, 'index.html', {'name': name, 'data': data}) def login(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('loginName') password = form.cleaned_data.get('loginPass') user = authenticate(username=username, password=password) auth.login(request, user) info = getAddr(request.META['REMOTE_ADDR']) if info['status'] == 'success': Login.objects.create(username=username, client_addr=info['query'], city=info['city'], country=info['country'], region=info['regionName'], latitude=info['lat'], longitude=info['lon']).save() else: Login.objects.create(username=username, client_addr=request.META['REMOTE_ADDR']).save() url = request.GET.get('next', '/') return redirect(url) else: return render(request, 'login.html', {'errors': form.errors}) else: return render(request, 'login.html') @login_required def userinfo(request): login_data = Login.objects.filter(username=request.user.username).order_by('-login_time')[:5] image_data = Upload.objects.filter(username=request.user.username).order_by('-upload_time')[:5] blog_data = paper.objects.filter(username=request.user.username).order_by('-paper_time')[:5] task_data = Task.objects.filter(username=request.user.username).order_by('-task_time')[:5] return render(request, 'function/user.html', {'name': auth.get_user(request).username, 'login_data': login_data, 'image_data': image_data, 'blog_data': blog_data, 'task_data': task_data, 'regtime': auth.get_user(request).date_joined}) def logout(request): auth.logout(request) return redirect(reverse('App:index')) @login_required def repassword(request): return HttpResponse('功能尚未支持,修改密码请联系管理员') def register(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): first_name = form.cleaned_data.get('fname') last_name = form.cleaned_data.get('lname') username = form.cleaned_data.get('username') email = request.POST.get('email') password = form.cleaned_data.get('password') likes = request.POST.getlist('like') role = request.POST.get('role') try: User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name, is_staff=role, likes=likes) except Exception as e: return render(request, 'register.html', {'errors': e}) return redirect(reverse('App:login')) else: return render(request, 'register.html', {'errors': form.errors}) else: return render(request, 'register.html') # @cache_page(60 * 15) @login_required def dashboard(request, type): username = auth.get_user(request).username if request.GET.get('time') == 'week': t = 6 elif request.GET.get('time') == 'month': t = 29 elif request.GET.get('time') == 'year': t = 364 elif request.GET.get('time') == 'quarter': t = 90 else: t = 6 if type == 'login': data = getData(username, t) table = Login.objects.filter(username=username).order_by('-login_time').all()[:30] elif type == 'docs': data = getData(username, t, 'docs') table = Upload.objects.filter(username=username).order_by('-upload_time').all()[:100] elif type == 'blog': data = getData(username, t, 'blog') table = paper.objects.order_by('-paper_time').all()[:100] else: data = table = "" return render(request, 'nav/dashboard.html', {'data': data, 'table': table, 'type': type}) @xframe_options_sameorigin @csrf_exempt @login_required def upload(request): if request.method == 'POST': if request.GET.get('type') == 'editmd': file = request.FILES.get('editormd-image-file') url_path = putImage(file) if Upload.objects.filter(username=auth.get_user(request).username, file_name=file.name,file_size=file.size).exists(): Upload.objects.filter(file_name=file.name, file_size=file.size).update(upload_time=datetime.datetime.now()) else: Upload.objects.create(username=auth.get_user(request).username, file_name=file.name, file_path=file.name,file_size=file.size, url_path=url_path['url']).save() data = {'success': 1, 'message': "上传成功", 'url': url_path['url']} return JsonResponse(data) else: file = request.FILES.getlist('file', None) data = {'status': 'success', 'msg': []} for i in file: if Upload.objects.filter(username=auth.get_user(request).username,file_name=i.name,file_size=i.size).exists(): Upload.objects.filter(file_name=i.name,file_size=i.size).update(upload_time=datetime.datetime.now()) else: url_path = putImage(i) data['msg'].append(url_path) Upload.objects.create(username=auth.get_user(request).username, file_name=i.name, file_path=i.name, file_size=i.size, url_path=url_path['url']).save() if data: return HttpResponse(json.dumps(data)) return render(request, 'nav/upload.html') # 功能函数 def getData(username, time, type='login'): data = {} for i in range(time, -1, -1): date = datetime.date.today() - datetime.timedelta(days=i) if type == 'login': data.update({str(date): Login.objects.filter(username=username, login_time__startswith=date).count()}) elif type == 'docs': data.update({str(date): Upload.objects.filter(username=username, upload_time__startswith=date).count()}) elif type == 'blog': data.update({str(date): paper.objects.filter(paper_time__startswith=date).count()}) return data def getAddr(ip): ip = requests.get('https://httpbin.org/ip').json()['origin'] info = requests.get('http://ip-api.com/json/' + ip + '?lang=zh-CN') info = info.json() return info @login_required def listImage(request): image = Upload.objects.filter(username=auth.get_user(request).username).all() return render(request, 'nav/image.html', {'image': image}) @login_required def listTask(request): task = Task.objects.all() return render(request, 'task/tasklist.html', {'task': task}) def addTask(request): if request.method == 'POST': task_title = request.POST.get('title') image = request.POST.get('bimage') if not image : image = 'https://py-put.gd2.qingstor.com/0bb68da4b7edd5ad21bee1f9ece0bdca5871f5a9.jpg' data = {} now = datetime.datetime.now() data.update({'backend_image': image, 'userlist': {}.fromkeys(request.POST.get('userlist').split(','),{'status':'未完成','update_time': str(now)}), 'desc': request.POST.get('desc')}) Task.objects.create(username=auth.get_user(request).username,task_title=task_title, task_content=json.dumps(data, ensure_ascii=False),).save() return redirect(reverse('App:listTask'), {'messages': task_title + '添加成功'}) return render(request, 'task/taskcreate.html') @login_required def checkTask(request, id): if request.method == 'POST': task = Task.objects.get(id=id) jsondata = json.loads(task.task_content) jsondata['userlist'][request.GET.get('name')] = {'status': '已完成', 'update_time': str(datetime.datetime.now())} Task.objects.filter(id=id).update(task_content=json.dumps(jsondata, ensure_ascii=False)) return redirect(reverse('App:checkTask', kwargs={'id': id})) data = Task.objects.filter(id=id).get() userdata = json.loads(data.task_content) return render(request, 'task/detailed.html', {'data': userdata['userlist'],'id':id}) @csrf_exempt @login_required def editMD(request, id=None): if request.method == 'POST': title = request.POST.get('title') desc = request.POST.get('desc') text = request.POST.get('test-editormd-markdown-doc') image = get_image('200x300') if paper.objects.filter(username=auth.get_user(request).username, paper_title=title).exists(): paper.objects.filter(username=auth.get_user(request).username,paper_title=title).update(paper_desc=desc, paper_content=text, paper_time=datetime.datetime.now()) else: paper.objects.create(username=auth.get_user(request).username, paper_title=title, paper_desc=desc, paper_content=text, paper_image=image).save() return redirect(reverse('App:paperList')) else: data = {} if f'{id}'.isdigit(): data = paper.objects.filter(id=id).get() return render(request, 'function/edit.html', {'data': data}) def paperList(request): data = paper.objects.all() thumbs_up = {} for i in data: thumbs_up[i.id] = thumbs.objects.filter(paper_id=i.id).count() return render(request, 'nav/paperlist.html', {'data': data, 'thumbs': thumbs_up, 'username': auth.get_user(request).username}) def paperDetail(request, id): if request.method == 'POST': if thumbs.objects.filter(username=auth.get_user(request).username, paper_id=id).exists(): thumbs.objects.filter(username=auth.get_user(request).username, paper_id=id).delete() else: thumbs.objects.create(username=auth.get_user(request).username, paper_id=paper.objects.filter(id=id).get()).save() return redirect(reverse('App:paperDetail', kwargs={'id': id})) data = paper.objects.filter(id=id).get() if data: paper.objects.filter(id=id).update(views=paper.objects.filter(id=id).get().views+1) return render(request, 'function/viewmd.html', {'data': data, 'thumbs': thumbs.objects.filter(paper_id=id).count()})