from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt 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): return render(request, 'polls/chat_text.html') @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) @csrf_exempt @polls_login_required def groups(request): if request.method == 'POST': return HttpResponse(status=405) user_id = request.user.id category = request.GET.get('category') q = Group.objects.filter(Q(status=1), Q( group_user__user__id=user_id) | Q(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) @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) @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).order_by('user__group_admin__created') 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['phone'] = m.user.username 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['phone'] = m.user.username 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'})