Commit f9053aa0 by Xu Zhou

1. 把views拆分成两个文件,一个返回页面的,一个API的。2. 写了一部分代码注释。

parent 529effef
......@@ -37,7 +37,7 @@
{% for user in users %}
<ul>
<li>
<a href="{% url 'hunter:detail' user.userID %}" target="_blank">{{ user.name_hanzi }}</a>
<a href="{% url 'hunter:resume' user.userID %}" target="_blank">{{ user.name_hanzi }}</a>
</li>
<li class="user_mail">{{ user.mailAddress }}</li>
</ul>
......@@ -134,7 +134,7 @@
{% for user in users %}
<ul>
<li>
<a href="{% url 'hunter:detail' user.userID %}" target="_blank">{{ user.name_hanzi }}</a>
<a href="{% url 'hunter:resume' user.userID %}" target="_blank">{{ user.name_hanzi }}</a>
</li>
<li class="user_mail">{{ user.mailAddress }}</li>
</ul>
......
......@@ -48,7 +48,7 @@
<a href="{% url 'hunter:profile_edit' user.userID %}" class="main_bar userID" id="">修改个人信息</a>
<a href="{% url 'hunter:detail' user.userID %}" class="main_bar" >查看个人详情页</a>
<a href="{% url 'hunter:resume' user.userID %}" class="main_bar" >查看个人详情页</a>
{% if user.identity == '1' %}
<a href="javascript:;" class="main_bar" onclick="document.getElementById('form_post_admin').submit();">管理员</a>
<form id="form_post_admin" action="../../ht/admin/" method="post">
......@@ -57,7 +57,7 @@
</form>
{% endif %}
<span> (<a href="/get_user_scores"> {{user.score}} </a> 分)</span>
<a href="{% url 'hunter:detail' user.userID %}" id="username" class="main_bar" >{{ usr.name }}</a>
<a href="{% url 'hunter:resume' user.userID %}" id="username" class="main_bar" >{{ usr.name }}</a>
</div>
</div>
......
......@@ -3,22 +3,32 @@ from django.urls import path
from . import views
app_name = 'hunter'
urlpatterns = [
path('', views.index, name='index'),
path('index/', views.index, name='index'),
# path('<int:pk>/', views.UserDetail.as_view(), name='user'),
# path('<int:pk>/article/', views.ArticleDetail.as_view(), name='article'),
path('user/<int:user_id>/', views.user, name='user'),
#首页
path('', views.index_html, name='index'),
path('index/', views.index_html, name='index'),
#用户页面
path('user/<int:user_id>/', views.user_html, name='user'),
#修改个人信息页面
path('user/edit/profile/<int:user_id>/', views.profile_edit, name='profile_edit'),
#api
path('profile_submit/', views.profile_submit, name='profile_submit'),
path('user/detail/<int:user_id>/', views.detail, name='detail'),
#个人主页(resume)
path('user/detail/<int:user_id>/', views.resume_html, name='resume'),
path('write/', views.write_art, name='write_art'),
#新建文章页面
path('write/', views.write_art_html, name='write_art'),
#查看文章页面
path('article/<int:article_id>/', views.article_html, name='article'),
#编辑文章页面
path('edit/article/<int:article_id>/', views.edit_art_html, name='edit_art'),
#api
path('save/edit/article/', views.save_edit_art, name='save_edit_art'),
path('uploadImg/', views.uploadImg, name='uploadImg'),
path('article/<int:article_id>/', views.article, name='article'),
path('edit/article/<int:article_id>/', views.edit_art, name='edit_art'),
path('add/article/url/', views.add_url_article, name='add_url_article'),
# API
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
path('delete/publication/', views.delete_publication, name='delete_publication'),
......@@ -31,6 +41,7 @@ urlpatterns = [
path('edit/cve/', views.edit_cve, name='edit_cve'),
path('add/article/', views.add_article, name='add_article'),
path('ht/admin/', views.admin, name='admin'),
path('operator/', views.operator, name='operator'),
path('add_user/', views.add_user, name='add_user'),
......
from .view_page import *
from .view_api import *
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseNotFound
from ..models import *
from django.core.exceptions import ObjectDoesNotExist
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.template.context_processors import csrf
import os
from django.db.utils import ProgrammingError
import datetime
from operator import itemgetter, attrgetter
from django.shortcuts import redirect
from django.core.exceptions import ValidationError
import json
from django.views import generic
from django.db.models.aggregates import Count
from django.db.models import Count
from django.views.decorators.csrf import ensure_csrf_cookie
@csrf_exempt
def login(request):
username = request.POST['username']
password = request.POST['password']
user_id=""
try:
db_info = User.objects.get(name=username)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": "用户不存在"})
return response
else:
if db_info.pwd == password:
user_id = db_info.userID
request.session['user_id'] = user_id
response = JsonResponse({"message": "success", "userid": user_id})
else:
response = JsonResponse({"message": "密码错误"})
return response
@csrf_exempt
def logout(request):
print('call logout')
request.session['user_id'] = None
response = JsonResponse({"message": "success"})
return response
@csrf_exempt
def profile_submit(request):
userID=request.POST["userID"]
name_hanzi=request.POST["name_hanzi"]
name_eng=request.POST["name_eng"]
pwd=request.POST["pwd"]
mail=request.POST["mail"]
website=request.POST["website"]
length = request.POST["length"]
profile = request.POST['profile']
print(profile)
edus = {}
if int(length)>0:
edu_arr = request.POST['edu_arr']
print(edu_arr)
edu_arr = edu_arr.split('#')
if Education.objects.filter(userID=userID).count()>0:
Education.objects.filter(userID=userID).delete()
for index in range(len(edu_arr)-1):
values = edu_arr[index]
item = str(values).split('&')
edus[index]=str(values).split('&')
try:
Education.objects.create(userID=userID,start=item[0], stop=item[1], university=item[2], degree=item[3])
except ProgrammingError as e:
return JsonResponse({"message": 0, 'error': e})
print(edus)
if len(str(pwd).strip()) > 0:
try:
User.objects.filter(userID=userID).update(name_hanzi=name_hanzi, name_eng=name_eng, pwd=pwd, mailAddress= mail, personal_website=website, profile=profile)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({"message":0,'error':e})
else:
try:
User.objects.filter(userID=userID).update(name_hanzi=name_hanzi, name_eng=name_eng, mailAddress= mail, personal_website=website, profile=profile)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({"message":0,'error':e})
@csrf_exempt
def save_edit_art(request):
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile']
articleID = request.POST.get('articleID')
link=""
try:
a = Articles.objects.get(articleID=articleID)
authorID = a.authorID
print(authorID)
print(userID)
if int(authorID) != int(userID):
response = JsonResponse({"message": 0, 'error': '文章作者与当前用户不符'})
return response
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '文章不存在,请检查链接'})
return response
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'articles', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination, 'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Articles.objects.filter(articleID=articleID).update(title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
else:
try:
Articles.objects.filter(articleID=articleID).update(title=title, content=content,
editDateTime=editdatetime)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
@csrf_exempt
def add_url_article(request):
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
editdatetime = datetime.datetime.now().date()
url = request.POST['url']
if Articles.objects.last() is None:
articleID = 1
else:
articleID = Articles.objects.last().articleID + 1
try:
authorID = User.objects.get(userID=userID)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
return response
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title,
editDateTime=editdatetime,
url=url)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
@csrf_exempt
def delete_publication(request):
pub_id = request.POST['pub_id']
print(pub_id)
try:
PubToUser.objects.filter(pubID=pub_id).delete()
Publications.objects.filter(pubID=pub_id).delete()
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0})
return response
response = JsonResponse({"message": 1})
return response
@csrf_exempt
def delete_cve(request):
cve_id = request.POST['cve_id']
try:
Bugs.objects.filter(identifier=cve_id).delete()
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def delete_article(request):
article_id = request.POST['article_id']
try:
Articles.objects.filter(articleID=article_id).delete()
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def change_pub_status(request):
pubID = request.POST['pubID']
isOpen = request.POST['isOpen']
print(isOpen)
try:
Publications.objects.filter(pubID=pubID).update(isOpen=isOpen)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({'message':0,'error':e})
@csrf_exempt
def add_publication(request):
title = request.POST['title']
print(title)
authors = request.POST['authors']
uploadby = request.POST['username']
publishType = request.POST['type']
myfile = request.FILES['myfile']
print(authors)
s=','
authors = authors.replace(s, ',')
print(authors)
journalname = request.POST.get('journalname')
date = request.POST.get('date')
print(date)
index = request.POST.get('index')
isPublish=request.POST['isPublish']
if Publications.objects.filter(title=title).count() > 0:
return JsonResponse({'message': 0, "error": "该标题已存在"})
if Publications.objects.last() is None:
pubID = 1
else:
pubID = Publications.objects.last().pubID+1
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR,'hunter', 'static', 'publications', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination,'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Publications.objects.create(pubID=pubID, title=title,link=link, messages='kidding', authors = authors,journalname=journalname,uploadByUser=uploadby,date=date,publishType=publishType, isOpen=isPublish)
except ProgrammingError as e:
return JsonResponse({"message": 0})
authorArr = authors.split(',')
print(authorArr)
for author in authorArr:
users_list = User.objects.all().values('name')
user_list=[]
for user in users_list:
user_list.append(user['name'])
author = str(author).lower().strip()
if ' ' in author:
author_version1 = author.replace(' ','')
author_version2 = author.split(' ')[1]+str(author).lower().strip().split(' ')[0]
else:
author_version1 = author.replace(' ', '')
author_version2 = author
print(user_list)
print(author_version1)
print(author_version2)
if author_version1 in user_list:
userID = User.objects.filter(name=author_version1).values('userID')[0]['userID']
try:
print("测试:LIU Yingying")
PubToUser.objects.create(pubID = pubID,username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
if author_version2 in user_list:
print("测试:Yingying LIU")
userID = User.objects.filter(name=author_version2).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID = pubID,username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
PubToUser.objects.create(pubID = pubID,username=author)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
# message["warning"] = "上传成功"
# return HttpResponse(json.dumps(message), content_type='application/json')
return response
@csrf_exempt
def edit_publication(request):
pubID = request.POST['pubID']
title = request.POST['title']
authors = request.POST['authors']
uploadby = request.POST['username']
publishType = request.POST['type']
isHaveFile = request.POST['isHaveFile']
s=','
authors = authors.replace(s, ',')
journalname = request.POST.get('journalname')
date = request.POST.get('date')
index = request.POST.get('index')
isPublish=request.POST['isPublish']
origin_author = Publications.objects.get(pubID=pubID).authors
if Publications.objects.filter(pubID=pubID).count() <=0:
return JsonResponse({'message': 0, "error": "该出版不存在"})
if isHaveFile == '1':
myfile = request.FILES['myfile']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR,'hunter', 'static', 'publications', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination,'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Publications.objects.filter(pubID=pubID).update(title=title,link=link, authors=authors,
journalname=journalname,uploadByUser=uploadby,
date=date, publishType=publishType, isOpen=isPublish)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
Publications.objects.filter(pubID=pubID).update(title=title, authors=authors, journalname=journalname,
uploadByUser=uploadby, date=date, publishType=publishType,
isOpen=isPublish)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
authorArr = authors.split(',')
originArr = origin_author.split(',')
if authorArr != originArr:
print(authorArr)
print(originArr)
PubToUser.objects.filter(pubID=pubID).delete()
for author in authorArr:
users_list = User.objects.all().values('name')
user_list = []
for user in users_list:
user_list.append(user['name'])
author = str(author).lower().strip()
if ' ' in author:
author_version1 = author.replace(' ', '')
author_version2 = author.split(' ')[1] + str(author).lower().strip().split(' ')[0]
else:
author_version1 = author.replace(' ', '')
author_version2 = author
print(user_list)
print(author_version1)
print(author_version2)
if author_version1 in user_list:
userID = User.objects.filter(name=author_version1).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID=pubID, username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
if author_version2 in user_list:
userID = User.objects.filter(name=author_version2).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID=pubID, username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
PubToUser.objects.create(pubID=pubID, username=author)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
return response
@csrf_exempt
def add_cve(request):
owner = request.POST['owner']
bugType = request.POST['bugType']
identifier = request.POST['cve_id']
datetime = request.POST['date']
software = request.POST['software']
url = request.POST['url']
if Bugs.objects.filter(identifier=identifier).count() > 0:
return JsonResponse({'message': 0, "error": "该CVE已存在"})
try:
Bugs.objects.create(owner=owner, bugType=bugType, identifier=identifier, datetime=datetime,
software=software, url=url)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def edit_cve(request):
bugType = request.POST['bugType']
identifier = request.POST['cve_id']
software = request.POST['software']
url = request.POST['url']
if Bugs.objects.filter(identifier=identifier).count() <= 0:
return JsonResponse({'message': 0, "error": "CVE不存在"})
try:
Bugs.objects.filter(identifier=identifier).update(bugType=bugType, software=software, url=url)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def update_article(request):
art_id = request.POST['art_id']
print('update_article, art_id = ', art_id)
art = Articles.objects.get(articleID=int(art_id))
print(art)
art_pub = request.POST.get('publish')
if art_pub != None:
art.publish = art_pub
art.save()
return JsonResponse({"message": "success"})
return JsonResponse({"message": "failed"})
@csrf_exempt
def add_article(request):
return 0
\ No newline at end of file
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseNotFound
from .models import *
from ..models import *
from django.core.exceptions import ObjectDoesNotExist
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
......@@ -18,9 +18,8 @@ from django.db.models import Count
from django.views.decorators.csrf import ensure_csrf_cookie
# Create your views here.
def index(request):
# 首页
def index_html(request):
user_id = request.session.get('user_id')
print("request index.html, user_id = ", user_id)
user_list = User.objects.filter(isCurrent=1).all()
......@@ -130,33 +129,8 @@ def index(request):
return render(request, 'index.html', context)
@csrf_exempt
def login(request):
username = request.POST['username']
password = request.POST['password']
user_id=""
try:
db_info = User.objects.get(name=username)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": "用户不存在"})
return response
else:
if db_info.pwd == password:
user_id = db_info.userID
request.session['user_id'] = user_id
response = JsonResponse({"message": "success", "userid": user_id})
else:
response = JsonResponse({"message": "密码错误"})
return response
@csrf_exempt
def logout(request):
print('call logout')
request.session['user_id'] = None
response = JsonResponse({"message": "success"})
return response
def user(request, user_id):
# 用户页面
def user_html(request, user_id):
usr = get_object_or_404(User, pk=user_id)
print(usr.name)
username = usr.name
......@@ -212,8 +186,8 @@ def user(request, user_id):
print("display user: ", usr)
return render(request, 'user.html', context)
def detail(request, user_id):
# 个人简历 (resume or CV)
def resume_html(request, user_id):
usr = get_object_or_404(User, pk=user_id)
username = usr.name
my_publication_year_list = {}
......@@ -267,16 +241,17 @@ def detail(request, user_id):
}
return render(request, 'detail.html', context)
def article(request, article_id):
# 文章展示页面
def article_html(request, article_id):
art = get_object_or_404(Articles, pk=article_id)
author = get_object_or_404(User, pk=art.authorID)
art.author = author.name
return render(request, 'article.html', {'article': art})
# 新建文章页面
@csrf_exempt
def write_art(request):
def write_art_html(request):
if request.is_ajax():
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
......@@ -319,83 +294,12 @@ def write_art(request):
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return render(request, 'write.html')\
@csrf_exempt
def save_edit_art(request):
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile']
articleID = request.POST.get('articleID')
link=""
try:
a = Articles.objects.get(articleID=articleID)
authorID = a.authorID
print(authorID)
print(userID)
if int(authorID) != int(userID):
response = JsonResponse({"message": 0, 'error': '文章作者与当前用户不符'})
return response
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '文章不存在,请检查链接'})
return response
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'articles', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination, 'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Articles.objects.filter(articleID=articleID).update(title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
else:
try:
Articles.objects.filter(articleID=articleID).update(title=title, content=content,
editDateTime=editdatetime)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return render(request, 'write.html')
@csrf_exempt
def add_url_article(request):
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
editdatetime = datetime.datetime.now().date()
url = request.POST['url']
if Articles.objects.last() is None:
articleID = 1
else:
articleID = Articles.objects.last().articleID + 1
try:
authorID = User.objects.get(userID=userID)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
return response
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title,
editDateTime=editdatetime,
url=url)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
#编辑文章页面
def edit_art_html(request, article_id):
art = get_object_or_404(Articles, pk=article_id)
return render(request, 'editArticle.html', {'article': art})
@csrf_exempt
......@@ -410,268 +314,6 @@ def uploadImg(request):
return HttpResponse(link_path)
@csrf_exempt
def delete_publication(request):
pub_id = request.POST['pub_id']
print(pub_id)
try:
PubToUser.objects.filter(pubID=pub_id).delete()
Publications.objects.filter(pubID=pub_id).delete()
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0})
return response
response = JsonResponse({"message": 1})
return response
@csrf_exempt
def delete_cve(request):
cve_id = request.POST['cve_id']
try:
Bugs.objects.filter(identifier=cve_id).delete()
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def delete_article(request):
article_id = request.POST['article_id']
try:
Articles.objects.filter(articleID=article_id).delete()
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def change_pub_status(request):
pubID = request.POST['pubID']
isOpen = request.POST['isOpen']
print(isOpen)
try:
Publications.objects.filter(pubID=pubID).update(isOpen=isOpen)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({'message':0,'error':e})
@csrf_exempt
def add_publication(request):
title = request.POST['title']
print(title)
authors = request.POST['authors']
uploadby = request.POST['username']
publishType = request.POST['type']
myfile = request.FILES['myfile']
print(authors)
s=','
authors = authors.replace(s, ',')
print(authors)
journalname = request.POST.get('journalname')
date = request.POST.get('date')
print(date)
index = request.POST.get('index')
isPublish=request.POST['isPublish']
if Publications.objects.filter(title=title).count() > 0:
return JsonResponse({'message': 0, "error": "该标题已存在"})
if Publications.objects.last() is None:
pubID = 1
else:
pubID = Publications.objects.last().pubID+1
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR,'hunter', 'static', 'publications', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination,'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Publications.objects.create(pubID=pubID, title=title,link=link, messages='kidding', authors = authors,journalname=journalname,uploadByUser=uploadby,date=date,publishType=publishType, isOpen=isPublish)
except ProgrammingError as e:
return JsonResponse({"message": 0})
authorArr = authors.split(',')
print(authorArr)
for author in authorArr:
users_list = User.objects.all().values('name')
user_list=[]
for user in users_list:
user_list.append(user['name'])
author = str(author).lower().strip()
if ' ' in author:
author_version1 = author.replace(' ','')
author_version2 = author.split(' ')[1]+str(author).lower().strip().split(' ')[0]
else:
author_version1 = author.replace(' ', '')
author_version2 = author
print(user_list)
print(author_version1)
print(author_version2)
if author_version1 in user_list:
userID = User.objects.filter(name=author_version1).values('userID')[0]['userID']
try:
print("测试:LIU Yingying")
PubToUser.objects.create(pubID = pubID,username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
if author_version2 in user_list:
print("测试:Yingying LIU")
userID = User.objects.filter(name=author_version2).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID = pubID,username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
PubToUser.objects.create(pubID = pubID,username=author)
response = JsonResponse({"message": 1, 'pubID':pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
# message["warning"] = "上传成功"
# return HttpResponse(json.dumps(message), content_type='application/json')
return response
@csrf_exempt
def edit_publication(request):
pubID = request.POST['pubID']
title = request.POST['title']
authors = request.POST['authors']
uploadby = request.POST['username']
publishType = request.POST['type']
isHaveFile = request.POST['isHaveFile']
s=','
authors = authors.replace(s, ',')
journalname = request.POST.get('journalname')
date = request.POST.get('date')
index = request.POST.get('index')
isPublish=request.POST['isPublish']
origin_author = Publications.objects.get(pubID=pubID).authors
if Publications.objects.filter(pubID=pubID).count() <=0:
return JsonResponse({'message': 0, "error": "该出版不存在"})
if isHaveFile == '1':
myfile = request.FILES['myfile']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR,'hunter', 'static', 'publications', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination,'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
try:
Publications.objects.filter(pubID=pubID).update(title=title,link=link, authors=authors,
journalname=journalname,uploadByUser=uploadby,
date=date, publishType=publishType, isOpen=isPublish)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
Publications.objects.filter(pubID=pubID).update(title=title, authors=authors, journalname=journalname,
uploadByUser=uploadby, date=date, publishType=publishType,
isOpen=isPublish)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
authorArr = authors.split(',')
originArr = origin_author.split(',')
if authorArr != originArr:
print(authorArr)
print(originArr)
PubToUser.objects.filter(pubID=pubID).delete()
for author in authorArr:
users_list = User.objects.all().values('name')
user_list = []
for user in users_list:
user_list.append(user['name'])
author = str(author).lower().strip()
if ' ' in author:
author_version1 = author.replace(' ', '')
author_version2 = author.split(' ')[1] + str(author).lower().strip().split(' ')[0]
else:
author_version1 = author.replace(' ', '')
author_version2 = author
print(user_list)
print(author_version1)
print(author_version2)
if author_version1 in user_list:
userID = User.objects.filter(name=author_version1).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID=pubID, username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
if author_version2 in user_list:
userID = User.objects.filter(name=author_version2).values('userID')[0]['userID']
try:
PubToUser.objects.create(pubID=pubID, username=author, userID=userID)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
else:
try:
PubToUser.objects.create(pubID=pubID, username=author)
response = JsonResponse({"message": 1, 'pubID': pubID})
except ProgrammingError as e:
return JsonResponse({"message": 0})
return response
@csrf_exempt
def add_cve(request):
owner = request.POST['owner']
bugType = request.POST['bugType']
identifier = request.POST['cve_id']
datetime = request.POST['date']
software = request.POST['software']
url = request.POST['url']
if Bugs.objects.filter(identifier=identifier).count() > 0:
return JsonResponse({'message': 0, "error": "该CVE已存在"})
try:
Bugs.objects.create(owner=owner, bugType=bugType, identifier=identifier, datetime=datetime,
software=software, url=url)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def edit_cve(request):
bugType = request.POST['bugType']
identifier = request.POST['cve_id']
software = request.POST['software']
url = request.POST['url']
if Bugs.objects.filter(identifier=identifier).count() <= 0:
return JsonResponse({'message': 0, "error": "CVE不存在"})
try:
Bugs.objects.filter(identifier=identifier).update(bugType=bugType, software=software, url=url)
response = JsonResponse({"message": 1})
return response
except ProgrammingError as e:
response = JsonResponse({"message": 0})
return response
@csrf_exempt
def add_article(request):
return 0
def profile_edit(request, user_id):
user = get_object_or_404(User, pk=user_id)
edu = Education.objects.filter(userID=user_id).order_by("-start")
......@@ -681,52 +323,9 @@ def profile_edit(request, user_id):
return render(request, 'profile.html', {'user': user, 'edus':edu})
@csrf_exempt
def profile_submit(request):
userID=request.POST["userID"]
name_hanzi=request.POST["name_hanzi"]
name_eng=request.POST["name_eng"]
pwd=request.POST["pwd"]
mail=request.POST["mail"]
website=request.POST["website"]
length = request.POST["length"]
profile = request.POST['profile']
print(profile)
edus = {}
if int(length)>0:
edu_arr = request.POST['edu_arr']
print(edu_arr)
edu_arr = edu_arr.split('#')
if Education.objects.filter(userID=userID).count()>0:
Education.objects.filter(userID=userID).delete()
for index in range(len(edu_arr)-1):
values = edu_arr[index]
item = str(values).split('&')
edus[index]=str(values).split('&')
try:
Education.objects.create(userID=userID,start=item[0], stop=item[1], university=item[2], degree=item[3])
except ProgrammingError as e:
return JsonResponse({"message": 0, 'error': e})
print(edus)
if len(str(pwd).strip()) > 0:
try:
User.objects.filter(userID=userID).update(name_hanzi=name_hanzi, name_eng=name_eng, pwd=pwd, mailAddress= mail, personal_website=website, profile=profile)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({"message":0,'error':e})
else:
try:
User.objects.filter(userID=userID).update(name_hanzi=name_hanzi, name_eng=name_eng, mailAddress= mail, personal_website=website, profile=profile)
return JsonResponse({'message':1})
except ObjectDoesNotExist as e:
return JsonResponse({"message":0,'error':e})
def edit_art(request, article_id):
art = get_object_or_404(Articles, pk=article_id)
return render(request, 'editArticle.html', {'article': art})
def check_admin_privilage(user_id):
try:
......@@ -902,19 +501,6 @@ def add_user(request):
return render(request, 'admin.html',{'message':'添加失败'})
return render(request, 'admin.html')
@csrf_exempt
def update_article(request):
art_id = request.POST['art_id']
print('update_article, art_id = ', art_id)
art = Articles.objects.get(articleID=int(art_id))
print(art)
art_pub = request.POST.get('publish')
if art_pub != None:
art.publish = art_pub
art.save()
return JsonResponse({"message": "success"})
return JsonResponse({"message": "failed"})
@csrf_exempt
def add_sub_score(request):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment