Commit ca8d19f6 by liuyingying

资源存储路径

parent 49e5cb4c
...@@ -90,54 +90,54 @@ def profile_submit(request): ...@@ -90,54 +90,54 @@ def profile_submit(request):
def save_article(request): def save_article(request):
userID = request.POST.get('userID', '') userID = request.POST.get('userID', '')
title = request.POST.get('title', '') title = request.POST.get('title', '')
content = request.POST.get('content', '') content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date() editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile'] is_have_file = request.POST['isHaveFile']
link = "" link = ""
# 加入判断题目是否已存在 # 加入判断题目是否已存在
is_have = Articles.objects.filter(title=title).count() is_have = Articles.objects.filter(title=title).count()
if is_have > 0: if is_have > 0:
return JsonResponse({'message': 0, 'error': '该题目已存在'}) return JsonResponse({'message': 0, 'error': '该题目已存在'})
# 是否上传附件 # 是否上传附件
if int(is_have_file) > 0: if int(is_have_file) > 0:
myfile = request.FILES['myfile'] myfile = request.FILES['myfile']
if Articles.objects.filter(attachment=myfile.name).count(): if Articles.objects.filter(attachment=myfile.name).count():
return JsonResponse({'message': 0, 'error': '文件已存在'}) return JsonResponse({'message': 0, 'error': '文件已存在'})
else:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'hunter/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
# 写入数据库
if Articles.objects.last() is None:
articleID = 1
else: else:
articleID = Articles.objects.last().articleID + 1 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, '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: if Articles.objects.last() is None:
authorID = User.objects.get(userID=userID) articleID = 1
except ObjectDoesNotExist as e: else:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'}) articleID = Articles.objects.last().articleID + 1
return response
# 将文章写入数据库 # 判断作者是否存在
try: try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title, content=content, authorID = User.objects.get(userID=userID)
editDateTime=editdatetime, except ObjectDoesNotExist as e:
attachment=link) response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return response return response
# 将文章写入数据库
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return response
# 保存修改后的文章 # 保存修改后的文章
...@@ -528,4 +528,4 @@ def add_sub_score(request): ...@@ -528,4 +528,4 @@ def add_sub_score(request):
return response return response
else: else:
response = JsonResponse({"message":"用户不存在"}) response = JsonResponse({"message":"用户不存在"})
return response return response
\ No newline at end of file \ No newline at end of file
...@@ -220,49 +220,6 @@ def article_html(request, article_id): ...@@ -220,49 +220,6 @@ def article_html(request, article_id):
# 写文章页面 # 写文章页面
def write_art_html(request): def write_art_html(request):
if request.is_ajax():
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']
link = ""
# 加入判断题目是否相同
is_have = Articles.objects.filter(title=title).count()
if is_have > 0:
return JsonResponse({'message': 0, 'error': '该题目已存在'})
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
if Articles.objects.filter(attachment=myfile.name).count():
return JsonResponse({'message': 0, 'error': '文件已存在'})
else:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'hunter/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
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, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return response
return render(request, 'write.html') return render(request, 'write.html')
...@@ -277,7 +234,7 @@ def edit_art_html(request, article_id): ...@@ -277,7 +234,7 @@ def edit_art_html(request, article_id):
def uploadImg(request): def uploadImg(request):
file_obj = request.FILES.get("image") file_obj = request.FILES.get("image")
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
file_path = os.path.join(BASE_DIR, 'hunter', 'static', 'articles/images', file_obj.name) file_path = os.path.join(BASE_DIR, 'static', 'articles/images', file_obj.name)
link_path = os.path.join('/static', 'articles/images', file_obj.name) link_path = os.path.join('/static', 'articles/images', file_obj.name)
with open(file_path, 'wb') as f: with open(file_path, 'wb') as f:
for chunk in file_obj.chunks(): for chunk in file_obj.chunks():
......
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