2020-09-18 06:03:50 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
import datetime
|
2020-09-20 11:14:54 +00:00
|
|
|
from channels.layers import get_channel_layer
|
|
|
|
from asgiref.sync import async_to_sync
|
2020-09-18 06:03:50 +00:00
|
|
|
|
|
|
|
from polls.decorators import polls_login_required
|
2020-09-26 07:40:15 +00:00
|
|
|
from polls.models import Message, Notice, Task, TaskAddition
|
2020-09-20 11:14:54 +00:00
|
|
|
from polls.tasks import process_task
|
2020-09-18 06:03:50 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2020-09-20 11:14:54 +00:00
|
|
|
from polls.utils import model_to_dict, queryset_to_list
|
|
|
|
|
2020-09-18 06:03:50 +00:00
|
|
|
|
|
|
|
@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 = []
|
2020-09-26 10:22:59 +00:00
|
|
|
for g in o.groups.all():
|
2020-09-18 06:03:50 +00:00
|
|
|
n = model_to_dict(g, ['id', 'name'])
|
|
|
|
m.append(n)
|
|
|
|
result['groups'] = m
|
2020-09-26 10:22:59 +00:00
|
|
|
result['content'] = o.content
|
2020-09-18 06:03:50 +00:00
|
|
|
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
results.append(result)
|
2020-09-26 10:22:59 +00:00
|
|
|
return JsonResponse({'status': 'success', 'message':results}, safe=False)
|
2020-09-18 07:52:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@polls_login_required
|
|
|
|
def create_task(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
2020-09-18 16:11:19 +00:00
|
|
|
user = request.user
|
2020-09-26 07:40:15 +00:00
|
|
|
profile = user.userprofile_set.first()
|
2020-09-18 07:52:06 +00:00
|
|
|
content = request.POST.get('content')
|
|
|
|
if not content:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
|
2020-09-18 16:11:19 +00:00
|
|
|
groups = request.POST.getlist('groups', [])
|
|
|
|
task = Task.objects.create(created_by=user, content=content)
|
2020-09-26 05:51:36 +00:00
|
|
|
task.add_groups(groups)
|
2020-09-18 07:52:06 +00:00
|
|
|
url = request.POST.get('url')
|
2020-09-26 04:27:19 +00:00
|
|
|
print(groups, content, url)
|
2020-09-18 07:52:06 +00:00
|
|
|
file = request.FILES.get('file')
|
|
|
|
picture = request.FILES.get('picture')
|
2020-09-20 11:14:54 +00:00
|
|
|
if url:
|
2020-09-18 16:11:19 +00:00
|
|
|
urlAddtion = TaskAddition.objects.create(
|
|
|
|
task=task, category=0, url=url)
|
2020-09-26 04:48:48 +00:00
|
|
|
|
2020-09-20 11:14:54 +00:00
|
|
|
if file:
|
2020-09-18 16:11:19 +00:00
|
|
|
fileAddtion = TaskAddition.objects.create(
|
|
|
|
task=task, category=1, file=file)
|
2020-09-20 11:14:54 +00:00
|
|
|
if picture:
|
2020-09-18 16:11:19 +00:00
|
|
|
pictureAddtion = TaskAddition.objects.create(
|
|
|
|
task=task, category=2, image=picture)
|
2020-09-26 04:48:48 +00:00
|
|
|
|
|
|
|
for group in groups:
|
2020-09-26 05:51:36 +00:00
|
|
|
normalMessage = Message.objects.create(
|
2020-09-26 07:40:15 +00:00
|
|
|
type=4, send_from_id=user.id, send_to_id=group, task=task, content=content)
|
|
|
|
content = '用户%s发布了任务,请查收消息' % (profile.name,)
|
2020-09-26 08:19:27 +00:00
|
|
|
Notice.create_normal_notice(user.id, content)
|
2020-09-26 07:40:15 +00:00
|
|
|
|
2020-09-18 16:11:19 +00:00
|
|
|
return JsonResponse({'status': 'success'})
|
2020-09-22 07:39:33 +00:00
|
|
|
|
2020-09-26 05:51:36 +00:00
|
|
|
|
2020-09-22 07:39:33 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def create_test_task(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
channel_layer = get_channel_layer()
|
2020-09-26 05:51:36 +00:00
|
|
|
async_to_sync(channel_layer.group_send)(
|
|
|
|
"testroom", {"type": "chat.message", "message": "helo"})
|
2020-09-22 07:39:33 +00:00
|
|
|
return JsonResponse({'status': 'success'})
|