newmediamonitoring/polls/views/group.py

137 lines
4.6 KiB
Python
Raw Normal View History

2020-09-20 11:14:54 +00:00
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
2020-10-10 14:44:15 +00:00
from datetime import datetime
2020-10-10 07:46:04 +00:00
from django.db.models import Q
2020-09-20 11:14:54 +00:00
from polls.decorators import polls_login_required
from django.core.exceptions import ObjectDoesNotExist
2020-10-10 07:46:04 +00:00
from dashboard.models import Group, Userprofile
2020-10-10 14:44:15 +00:00
from polls.models import GroupRecord
2020-09-20 11:14:54 +00:00
2020-09-22 07:39:33 +00:00
def room(request):
return render(request, 'polls/chat_text.html')
2020-10-10 07:46:04 +00:00
@csrf_exempt
@polls_login_required
def is_level1_or_leve2(request):
if request.method == 'POST':
return HttpResponse(status=405)
user = request.user
profile = user.userprofile_set.first()
result = dict()
result['admin'] = profile.is_level1() or profile.is_level2()
return JsonResponse({'status': 'success', 'message': result}, safe=False)
2020-09-20 11:14:54 +00:00
@csrf_exempt
@polls_login_required
def groups(request):
if request.method == 'POST':
return HttpResponse(status=405)
2020-10-09 12:09:15 +00:00
user_id = request.user.id
2020-10-10 07:46:04 +00:00
category = request.GET.get('category')
q = Group.objects.filter(Q(status=1), Q(
2020-10-14 16:02:09 +00:00
group_user__user__id=user_id) | Q(group_admin__user__id=user_id)).distinct()
2020-10-10 07:46:04 +00:00
groups = q.filter(classify=category) if category else q
2020-09-20 11:14:54 +00:00
results = []
for o in groups:
result = dict()
result['id'] = str(o.id)
2020-10-14 16:02:09 +00:00
print(o.id, o.name)
2020-09-20 11:14:54 +00:00
result['name'] = o.name
result['image'] = request.build_absolute_uri(o.image.url)
results.append(result)
2020-09-23 09:47:05 +00:00
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-10-10 07:46:04 +00:00
2020-10-22 05:36:23 +00:00
@csrf_exempt
@polls_login_required
def pick_groups(request):
if request.method == 'POST':
return HttpResponse(status=405)
user_id = request.user.id
category = request.GET.get('category')
q = Group.objects.filter(status=1, group_admin__user__id=user_id).distinct()
groups = q.filter(classify=category) if category else q
results = []
for o in groups:
result = dict()
result['id'] = str(o.id)
print(o.id, o.name)
result['name'] = o.name
result['image'] = request.build_absolute_uri(o.image.url)
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-10-10 07:46:04 +00:00
@csrf_exempt
@polls_login_required
def group_manager(request):
if request.method == 'POST':
return HttpResponse(status=405)
group_id = request.GET.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
managers = Userprofile.objects.filter(user__group_admin__group_id=group_id)
results = []
for m in managers:
result = dict()
result['id'] = m.user.id
result['name'] = m.name
result['thumbnail'] = request.build_absolute_uri(
m.image.url) if m.image else None
result['organization'] = m.organization.name
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def group_member(request):
if request.method == 'POST':
return HttpResponse(status=405)
group_id = request.GET.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
managers = Userprofile.objects.filter(user__group_user__group_id=group_id)
results = []
for m in managers:
result = dict()
result['id'] = m.user.id
result['name'] = m.name
result['thumbnail'] = request.build_absolute_uri(
m.image.url) if m.image else None
result['organization'] = m.organization.name
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-10-10 14:44:15 +00:00
@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'})