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, send_tnps 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 creater_profile = user.userprofile_set.first() content = request.POST.get('content') url = request.POST.get('url') if not content: return JsonResponse({'status': 'error', 'message': '内容不能为空'}) if not content: return JsonResponse({'status': 'error', 'message': 'URL地址不能为空'}) groups = request.POST.getlist('groups', []) print(groups) task = Task.objects.create(created_by=user, content=content) task.add_groups(groups) task.add_task_message(user.id) # 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_id=group) for u in users: profile = Userprofile.objects.filter(user_id=u.user_id).first() if profile and profile.status == 1: content = '%s在群组“%s”发布了任务,请查收消息' % (creater_profile, u.group.name) Notice.create_normal_notice(u.user_id, content, group) phones = [u.user.username for u in users] send_tnps(phones, '任务', content) 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'})