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
|
|
|
|
|
|
|
|
from polls.decorators import polls_login_required
|
2020-11-23 07:43:53 +00:00
|
|
|
from polls.models import Message, Notice, Task, TaskAddition, TaskRecord
|
2020-09-18 06:03:50 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-01-10 13:22:39 +00:00
|
|
|
from polls.utils import model_to_dict, queryset_to_list, parse, send_tnps
|
2020-10-17 16:00:23 +00:00
|
|
|
from dashboard.models import Group, Group_user, Userprofile
|
2020-09-20 11:14:54 +00:00
|
|
|
|
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-10-10 14:44:15 +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
|
2021-02-01 01:35:29 +00:00
|
|
|
creater_profile = user.userprofile_set.first()
|
2020-09-18 07:52:06 +00:00
|
|
|
content = request.POST.get('content')
|
2021-03-12 06:00:01 +00:00
|
|
|
url = request.POST.get('url')
|
2020-09-18 07:52:06 +00:00
|
|
|
if not content:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
|
2021-03-12 06:00:01 +00:00
|
|
|
if not content:
|
|
|
|
return JsonResponse({'status': 'error', 'message': 'URL地址不能为空'})
|
2020-09-18 16:11:19 +00:00
|
|
|
groups = request.POST.getlist('groups', [])
|
2021-04-08 02:56:26 +00:00
|
|
|
# print(groups)
|
2020-09-18 16:11:19 +00:00
|
|
|
task = Task.objects.create(created_by=user, content=content)
|
2020-09-26 05:51:36 +00:00
|
|
|
task.add_groups(groups)
|
2020-10-10 17:42:12 +00:00
|
|
|
task.add_task_message(user.id)
|
|
|
|
# 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-10-10 17:42:12 +00:00
|
|
|
og_title, og_description, og_url, og_image = parse(url)
|
2021-04-08 02:56:26 +00:00
|
|
|
# print(user.id, og_title, og_description, og_url, og_image)
|
2020-10-10 17:42:12 +00:00
|
|
|
task.add_url_message(
|
|
|
|
user.id, og_title, og_description, og_url, og_image)
|
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-10-17 16:00:23 +00:00
|
|
|
for group in groups:
|
2021-01-30 13:46:16 +00:00
|
|
|
users = Group_user.objects.filter(group_id=group)
|
2021-03-29 13:02:26 +00:00
|
|
|
phones = []
|
2020-10-17 16:00:23 +00:00
|
|
|
for u in users:
|
2020-12-05 08:14:34 +00:00
|
|
|
profile = Userprofile.objects.filter(user_id=u.user_id).first()
|
|
|
|
if profile and profile.status == 1:
|
2021-04-07 10:49:36 +00:00
|
|
|
g = Group.objects.get(id=group)
|
|
|
|
content = '%s在群组“%s”发布了任务,请查收消息' % (creater_profile, g.name)
|
2020-12-05 08:14:34 +00:00
|
|
|
Notice.create_normal_notice(u.user_id, content, group)
|
2021-04-08 02:56:26 +00:00
|
|
|
# print(u.user.username)
|
2021-03-29 13:02:26 +00:00
|
|
|
phones.append(u.user.username)
|
2021-02-03 03:47:59 +00:00
|
|
|
send_tnps(phones, '任务', content)
|
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-10-10 14:44:15 +00:00
|
|
|
@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()
|
2020-10-17 16:00:23 +00:00
|
|
|
result['isOwner'] = task.created_by_id == id
|
|
|
|
records = task.records()
|
|
|
|
n = []
|
|
|
|
for r in records:
|
|
|
|
p = dict()
|
2020-11-23 07:43:53 +00:00
|
|
|
p['id'] = r.user_id
|
2021-02-01 01:35:29 +00:00
|
|
|
is_finished = TaskRecord.objects.filter(
|
|
|
|
user_id=r.user_id, task_id=task_id)
|
2020-11-23 07:43:53 +00:00
|
|
|
p['status'] = '完成' if is_finished else '未完成'
|
2020-10-17 16:00:23 +00:00
|
|
|
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
|
2020-10-10 14:44:15 +00:00
|
|
|
result['added'] = task.added.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
return JsonResponse({'status': 'success', 'message': result})
|
|
|
|
|
2021-02-01 01:35:29 +00:00
|
|
|
|
2020-12-05 06:51:51 +00:00
|
|
|
@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'})
|