def get_queryset(self): room = Room.objects.get(pk=self.kwargs.get('pk', None)) group_name = room.legislative_body_initials if belongs_to_group(self.request.user, group_name): return Room.objects.filter(pk=self.kwargs.get('pk', None)) else: raise Http404()
def delete_video(request, video_id): if request.user.is_authenticated(): video = Video.objects.get(pk=video_id) group_name = video.room.legislative_body_initials if belongs_to_group(request.user, group_name): video.delete() return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def delete_attachment(request, attachment_id): if request.user.is_authenticated(): attachment = RoomAttachment.objects.get(pk=attachment_id) group_name = attachment.room.legislative_body_initials if belongs_to_group(request.user, group_name): attachment.delete() return redirect('video_room', pk=attachment.room.id) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def remove_external_link(request, room_id): if request.user.is_authenticated(): room = Room.objects.get(pk=room_id) group_name = room.legislative_body_initials if belongs_to_group(request.user, group_name): room.external_link = '' room.save() return redirect('video_room', pk=room.id) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def set_answer_time(request, question_id): if request.user.is_authenticated() and request.method == 'POST': answer_time = request.POST.get('answer_time') video_id = request.POST.get('video_id') if answer_time: question = Question.objects.get(pk=question_id) group_name = question.room.legislative_body_initials if belongs_to_group(request.user, group_name): if answer_time == '0': question.answer_time = None question.answered = False else: video = Video.objects.filter(video_id=video_id).first() question.answer_time = answer_time question.answered = True question.video = video question.save() vote_list = [] for vote in question.votes.all(): vote_list.append(encrypt(str(vote.user.id).rjust(10))) html = question.html_question_body(request.user, 'room') text = { 'question': True, 'html': html, 'id': question.id, 'voteList': vote_list, 'answered': question.answered, 'groupName': group_name, 'handlerAction': encrypt(str(request.user.id).rjust(10)), } Group(question.room.group_room_name).send( {'text': json.dumps(text)} ) html_question_panel = question.html_question_body( request.user, 'question-panel') text_question_panel = { 'html': html_question_panel, 'id': question.id } Group(question.room.group_room_questions_name).send( {'text': json.dumps(text_question_panel)} ) return HttpResponse(status=200) else: return HttpResponseForbidden() else: return HttpResponseBadRequest('Invalid format.') else: return HttpResponseForbidden()
def create_attachment(request, room_id): if request.user.is_authenticated() and request.method == 'POST': room = Room.objects.get(pk=room_id) group_name = room.legislative_body_initials if belongs_to_group(request.user, group_name): form = RoomAttachmentForm(request.POST) if form.is_valid(): attachment = form.save(commit=False) attachment.room = room attachment.save() return redirect('video_room', pk=room.id) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def create_video_attachment(request, room_id): if request.user.is_authenticated() and request.method == 'POST': room = Room.objects.get(pk=room_id) group_name = room.legislative_body_initials if belongs_to_group(request.user, group_name): form = VideoForm(request.POST) if form.is_valid(): video = form.save(commit=False) video.room = room video.is_attachment = True video.order = room.get_attachment_videos().count() + 1 video.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def set_priotity(request, question_id): if request.user.is_authenticated() and request.method == 'POST': is_priority = request.POST.get('is_priority') question = Question.objects.get(pk=question_id) group_name = question.room.legislative_body_initials if belongs_to_group(request.user, group_name): if is_priority == 'true': question.is_priority = True else: question.is_priority = False question.save() vote_list = [] for vote in question.votes.all(): vote_list.append(encrypt(str(vote.user.id).rjust(10))) html = question.html_question_body(request.user, 'room') text = { 'question': True, 'html': html, 'id': question.id, 'voteList': vote_list, 'answered': question.answered, 'groupName': group_name, 'handlerAction': encrypt(str(request.user.id).rjust(10)), } Group(question.room.group_room_name).send( {'text': json.dumps(text)} ) html_question_panel = question.html_question_body( request.user, 'question-panel') text_question_panel = { 'html': html_question_panel, 'id': question.id } Group(question.room.group_room_questions_name).send( {'text': json.dumps(text_question_panel)} ) return HttpResponse(status=200) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def order_videos(request, room_id): if request.user.is_authenticated() and request.method == 'POST': room = Room.objects.get(pk=room_id) group_name = room.legislative_body_initials if belongs_to_group(request.user, group_name): videos = json.loads(request.POST['data']) for video in videos: Video.objects.filter(id=video['id']).update( order=video['order']) text = { 'video': True, 'ordered': True, 'thumbs_html': room.html_room_thumbnails(), } Group(room.group_room_name).send({'text': json.dumps(text)}) return HttpResponse(status=200) else: return HttpResponseForbidden() else: return HttpResponseForbidden()
def send_participants_notification(request, room_id): room = Room.objects.get(id=room_id) group_name = room.legislative_body_initials if request.POST and belongs_to_group(request.user, group_name): questions_id = Question.objects.filter(room=room).values_list( 'id', flat=True) votes_users = UpDownVote.objects.filter( question_id__in=questions_id).values_list('user_id', flat=True) questions_users = Question.objects.filter(room=room).values_list( 'user_id', flat=True) messages_users = Message.objects.filter(room=room).values_list( 'user_id', flat=True) all_users = set( list(votes_users) + list(questions_users) + list(messages_users)) users_email = get_user_model().objects.filter( id__in=all_users).values_list('email', flat=True) subject = request.POST.get('subject') content = request.POST.get('content') # Definir template do corpo de email html = render_to_string('email/participants-notification.html', { 'room': room, 'content': content }) # Definir assunto do email email_subject = u'[Audiências Interativas] %s' % subject for email in users_email: mail = EmailMultiAlternatives(email_subject, '', settings.EMAIL_HOST_USER, [email]) mail.attach_alternative(html, 'text/html') mail.send() notification = ParticipantNotification() notification.room = room notification.emails = ', '.join([str(i) for i in users_email]) notification.content = content notification.subject = subject notification.save() return redirect('video_room', pk=room_id)