54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse, JsonResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.forms.models import model_to_dict
|
|
import datetime
|
|
|
|
from polls.decorators import polls_login_required
|
|
from polls.models import Task, TaskAddition
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
|
@csrf_exempt
|
|
@polls_login_required
|
|
def tasks(request):
|
|
if request.method == 'POST':
|
|
return HttpResponse(status=405)
|
|
id = request.user.id
|
|
tasks = Task.objects.filter(created_by__id=id)
|
|
results = []
|
|
for o in tasks:
|
|
result = dict()
|
|
result['id'] = o.id
|
|
m = []
|
|
for g in o.groups:
|
|
n = model_to_dict(g, ['id', 'name'])
|
|
m.append(n)
|
|
result['groups'] = m
|
|
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
|
|
results.append(result)
|
|
return JsonResponse(results, safe=False)
|
|
|
|
|
|
@csrf_exempt
|
|
@polls_login_required
|
|
def create_task(request):
|
|
if request.method == 'GET':
|
|
return HttpResponse(status=405)
|
|
user_id = request.user.id
|
|
content = request.POST.get('content')
|
|
if not content:
|
|
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
|
|
groups = request.POST.getlist('groups',[])
|
|
task = Task.objects.create(created_by__id=user_id, content=content)
|
|
task.add_groups(groups)
|
|
url = request.POST.get('url')
|
|
file = request.FILES.get('file')
|
|
picture = request.FILES.get('picture')
|
|
if not url:
|
|
urlAddtion = TaskAddition.objects.create(task=task,category=0, url=url)
|
|
if not file:
|
|
fileAddtion = TaskAddition.objects.create(task=task,category=1, file=file)
|
|
if not picture:
|
|
pictureAddtion = TaskAddition.objects.create(task=task,category=2, image=picture)
|
|
return JsonResponse({'status': 'success'}) |