def inspect_content(request, project_id): current_project = Project.objects.get(pk=project_id) # Check if the user is a manager for the current project # if False, return HTTPResponseForbidden() if not is_manager_of(request.user, project_id): return HttpResponseForbidden() # Check if there are reported messages and redirect to the settings page # if there are none reported = current_project.message_set.filter(reported=True) if not reported.exists(): redirect('project_settings', project_id=project_id) # Check the action (if any) and either delete or ignore a message if 'action' in request.POST: message = reported.first() if request.POST['action'] == 'Delete': message.delete() if request.POST['action'] == 'Ignore': message.reported = False message.save() reported = current_project.message_set.filter(reported=True) if not reported.exists(): return redirect('project_settings', project_id=project_id) return render(request, 'codegui/inspect_content.html', {'project': current_project, 'message': reported.first(), })
def manage_questionnaire(request, project_id): current_project = Project.objects.get(pk=project_id) # Check if the user is a manager for the current project # if False, return HTTPResponseForbidden() if not is_manager_of(request.user, project_id): return HttpResponseForbidden() questions = current_project.question_set.all() if request.method == 'POST': if 'action' in request.POST: if request.POST['action'] == 'Discard changes': return redirect(project_settings, project_id=project_id) if request.POST['action'] == 'Save changes': # Dis gon' be fun yall save_question_changes_from_post(project_id, request.POST) return render(request, 'codegui/manage_questionnaire.html', {'project': current_project, 'questions': questions, })
def project_settings(request, project_id): current_project = Project.objects.get(pk=project_id) # Check if the user is a manager for the current project # if False, return HTTPResponseForbidden() if not is_manager_of(request.user, project_id): return HttpResponseForbidden() # If a form was submitted, check for validity and save changes. # Otherwise, return form based on current project settings. if request.method == 'POST': form = ProjectSettingsForm(request.POST) if 'action' in request.POST: # If the delete button was pressed delete the project and crawler and send the user back to the dashboard if request.POST['action'] == 'Delete project': if current_project.crawler is not None: # TODO Shut down the crawler properly before deleting current_project.crawler.delete() delete_project_data(current_project) current_project.delete() return redirect('dashboard') # Generate a list of separate values per row in the csv. Use the Echo object as a dummy to write to # the stream. Using generators the page will perform better due to live evaluation in stead of pre-loading # the csv, possibly causing a timeout error. if request.POST['action'] == 'Download data': headers = [['id', 'message_content', 'coder', 'question', 'value']] rows = (header for header in headers) # For each code in the project, convert its values to a list and add it to rows rows2 = (["{}".format(code.id), str(code.message.content), str(code.coder), str(code.answer.question.name), str(code.answer.value)] for code in Code.objects.filter(message__in= current_project.message_set.all())) pseudo_buffer = Echo() writer = csv.writer(pseudo_buffer) response = StreamingHttpResponse((writer.writerow(row) for row in chain(rows, rows2)), content_type="text/csv") response['Content-Disposition'] = 'attachment; filename="' + current_project.title + '_' \ + timezone.now().date().__str__() + '.csv"' return response # Open the page for the interface in which the manager can see and deal with reported messages if request.POST['action'] == 'Inspect content': return redirect('inspect_content', project_id=current_project.id) # Open the page for the interface in which the manager can manage questions and answers if request.POST['action'] == 'Manage questionnaire': return redirect('manage_questionnaire', project_id=current_project.id) # Clear all the data in the project if request.POST['action'] == 'Wipe project data': delete_project_data(current_project) # If the form is valid save the data in the current_project if form.is_valid(): current_project.title = form.cleaned_data['title'] current_project.description = form.cleaned_data['description'] current_project.save() else: form = ProjectSettingsForm(initial={ 'title': current_project.title, 'description': current_project.description, }) x_reported = current_project.message_set.filter(reported=True).count() return render(request, 'codegui/project_settings.html', {'project_id': project_id, 'form': form, 'x_reported': x_reported, })