add task record

This commit is contained in:
baoliang 2020-10-10 22:44:15 +08:00
parent 4dc1ff018a
commit 85c9958a59
11 changed files with 110 additions and 8 deletions

View File

@ -52,6 +52,9 @@ class Group(models.Model):
created = models.DateTimeField('创建时间', auto_now_add=True)
updated = models.DateTimeField('更新时间', auto_now=True)
def total_user(self):
return self.group_user_set.count()
def __str__(self):
return self.name

View File

@ -92,6 +92,12 @@ class Task(models.Model):
for g in groups:
self.groups.add(g)
def total(self):
return sum(g.total_user() for g in self.groups.all())
def record(self):
return TaskRecord.objects.filter(task=self).count()
class Meta:
ordering = ["-added"]
@ -124,8 +130,9 @@ TASK_RECORD_STATUS_CHOICES = (
class TaskRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
status = models.IntegerField(
'状态', choices=TASK_RECORD_STATUS_CHOICES, default=0)
'状态', choices=TASK_RECORD_STATUS_CHOICES, default=1)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

View File

@ -41,6 +41,7 @@ urlpatterns = [
name='polls_monitor_result'),
path('tasks/list/', views.tasks, name='polls_tasks_list'),
path('tasks/create/', views.create_task, name='polls_tasks_create'),
path('tasks/get/', views.get_task, name='polls_tasks_get'),
path('tasks/create_test/', views.create_test_task,
name='polls_tasks_create_test'),
path('groups/list/', views.groups, name='polls_groups_list'),
@ -48,6 +49,8 @@ urlpatterns = [
path('groups/test-room/', views.room, name='polls_group_test_room'),
path('groups/manager/', views.group_manager, name='polls_groups_manager'),
path('groups/member/', views.group_member, name='polls_groups_member'),
path('groups/enter/', views.group_enter, name='polls_groups_enter'),
path('groups/leave/', views.group_leave, name='polls_groups_leave'),
path('compartments/list/', views.compartments,
name='polls_compartments_list'),
path('organizations/list/', views.organizations,
@ -57,5 +60,11 @@ urlpatterns = [
path('messages/list/last/', views.last_messages,
name='polls_messages_list_last'),
path('messages/send/text/', views.send_text_message,
name='polls_message_send_text')
name='polls_message_send_text'),
path('messages/check/', views.is_read_message,
name='polls_message_check'),
path('messages/read/', views.read_message,
name='polls_message_read')
]

View File

@ -3,8 +3,8 @@ from .notice import notices, notice_top, read_notice, pass_notice, reject_notice
from .media import medias, my_medias, create_media, update_media, media_detail, delete_media
from .news import news_list, news_top, news_detail
from .monitor import monitor_statistics, monitor_result, media_statistics, city_ranking
from .task import tasks, create_task, create_test_task
from .group import groups, room, is_level1_or_leve2, group_member, group_manager
from .task import tasks, create_task, create_test_task, get_task
from .group import groups, room, is_level1_or_leve2, group_member, group_manager, group_enter, group_leave
from .compartment import compartments
from .organizations import organizations, organization_level
from .message import last_messages, send_text_message
from .message import last_messages, send_text_message, is_read_message, read_message

View File

@ -1,12 +1,13 @@
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import datetime
from datetime import datetime
from django.db.models import Q
from polls.decorators import polls_login_required
from django.core.exceptions import ObjectDoesNotExist
from dashboard.models import Group, Userprofile
from polls.models import GroupRecord
def room(request):
@ -85,3 +86,32 @@ def group_member(request):
result['organization'] = m.organization.name
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def group_enter(request):
if request.method == 'GET':
return HttpResponse(status=405)
user = request.user
group_id = request.POST.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
obj,created = GroupRecord.objects.get_or_create(user=user, group_id=group_id)
return JsonResponse({'status': 'success'})
@csrf_exempt
@polls_login_required
def group_leave(request):
if request.method == 'GET':
return HttpResponse(status=405)
user = request.user
group_id = request.POST.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
obj,created = GroupRecord.objects.get_or_create(user=user, group_id=group_id)
if not created:
obj.leave_at = datetime.now()
obj.save()
return JsonResponse({'status': 'success'})

View File

@ -5,7 +5,7 @@ import datetime
from polls.decorators import polls_login_required
from django.core.exceptions import ObjectDoesNotExist
from polls.models import Message
from polls.models import Message, TaskRecord
from dashboard.models import Userprofile
@ -56,3 +56,32 @@ def send_text_message(request):
normalMessage = Message.objects.create(
type=0, send_from_id=user.id, send_to_id=group_id, content=content)
return JsonResponse({'status': 'success'})
@csrf_exempt
@polls_login_required
def is_read_message(request):
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
message_id = request.GET.get('message_id')
if not message_id:
return JsonResponse({'status': 'success', 'message': False}, safe=False)
is_exists = TaskRecord.objects.filter(
task__message__id=message_id).exists()
return JsonResponse({'status': 'success', 'message': is_exists}, safe=False)
@csrf_exempt
@polls_login_required
def read_message(request):
if request.method == 'GET':
return HttpResponse(status=405)
user = request.user
message_id = request.POST.get('message_id')
if not message_id:
return JsonResponse({'status': 'success', 'message': False}, safe=False)
message = Message.objects.get(id=message_id)
obj, created = TaskRecord.objects.get_or_create(user=user, task=message.task)
return JsonResponse({'status': 'success', 'message': True}, safe=False)

View File

@ -31,7 +31,7 @@ def tasks(request):
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)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@ -71,6 +71,30 @@ def create_task(request):
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['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':