122 lines
4.3 KiB
Python
122 lines
4.3 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse, JsonResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
import datetime
|
|
|
|
from polls.decorators import polls_login_required
|
|
from polls.models import Message, Notice, Task, TaskAddition, TaskRecord
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from polls.utils import model_to_dict, queryset_to_list, parse
|
|
from dashboard.models import Group, Group_user, Userprofile
|
|
|
|
|
|
@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.all():
|
|
n = model_to_dict(g, ['id', 'name'])
|
|
m.append(n)
|
|
result['groups'] = m
|
|
result['content'] = o.content
|
|
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
|
|
results.append(result)
|
|
return JsonResponse({'status': 'success', 'message': results}, safe=False)
|
|
|
|
|
|
@csrf_exempt
|
|
@polls_login_required
|
|
def create_task(request):
|
|
if request.method == 'GET':
|
|
return HttpResponse(status=405)
|
|
user = request.user
|
|
profile = user.userprofile_set.first()
|
|
content = request.POST.get('content')
|
|
if not content:
|
|
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
|
|
groups = request.POST.getlist('groups', [])
|
|
task = Task.objects.create(created_by=user, content=content)
|
|
task.add_groups(groups)
|
|
task.add_task_message(user.id)
|
|
url = request.POST.get('url')
|
|
# print(groups, content, url)
|
|
file = request.FILES.get('file')
|
|
picture = request.FILES.get('picture')
|
|
if url:
|
|
urlAddtion = TaskAddition.objects.create(
|
|
task=task, category=0, url=url)
|
|
og_title, og_description, og_url, og_image = parse(url)
|
|
print(og_title, og_description, og_url, og_image)
|
|
task.add_url_message(
|
|
user.id, og_title, og_description, og_url, og_image)
|
|
if file:
|
|
fileAddtion = TaskAddition.objects.create(
|
|
task=task, category=1, file=file)
|
|
if picture:
|
|
pictureAddtion = TaskAddition.objects.create(
|
|
task=task, category=2, image=picture)
|
|
|
|
for group in groups:
|
|
users = Group_user.objects.filter(group=group)
|
|
for u in users:
|
|
g = Group.objects.get(id=group)
|
|
profile = Userprofile.objects.filter(user_id=u.user_id).first()
|
|
if profile and profile.status == 1:
|
|
content = '%s在群组“%s”发布了任务,请查收消息' % (profile, g.name)
|
|
Notice.create_normal_notice(u.user_id, content, group)
|
|
return JsonResponse({'status': 'success'})
|
|
|
|
|
|
@csrf_exempt
|
|
@polls_login_required
|
|
def get_task(request):
|
|
if request.method == 'POST':
|
|
return HttpResponse(status=405)
|
|
id = request.user.id
|
|
task_id = request.GET.get('task_id')
|
|
if not task_id:
|
|
return JsonResponse({'status': 'error', 'message': '参数错误'})
|
|
task = Task.objects.get(id=task_id)
|
|
result = dict()
|
|
result['id'] = task.id
|
|
m = []
|
|
for g in task.groups.all():
|
|
n = model_to_dict(g, ['id', 'name'])
|
|
m.append(n)
|
|
result['groups'] = m
|
|
result['content'] = task.content
|
|
result['total'] = task.total()
|
|
result['record'] = task.record()
|
|
result['isOwner'] = task.created_by_id == id
|
|
records = task.records()
|
|
n = []
|
|
for r in records:
|
|
p = dict()
|
|
p['id'] = r.user_id
|
|
is_finished = TaskRecord.objects.filter(user_id=r.user_id, task_id=task_id)
|
|
p['status'] = '完成' if is_finished else '未完成'
|
|
profile = Userprofile.objects.filter(user_id=r.user_id).first()
|
|
p['name'] = profile.name
|
|
p['organization'] = profile.organization.name
|
|
n.append(p)
|
|
result['records'] = n
|
|
result['added'] = task.added.strftime("%Y-%m-%d %H:%M:%S")
|
|
return JsonResponse({'status': 'success', 'message': result})
|
|
|
|
@csrf_exempt
|
|
def create_test_task(request):
|
|
# if request.method == 'GET':
|
|
# return HttpResponse(status=405)
|
|
# channel_layer = get_channel_layer()
|
|
# async_to_sync(channel_layer.group_send)(
|
|
# "testroom", {"type": "chat.message", "message": "helo"})
|
|
return JsonResponse({'status': 'success'})
|