Ejemplo n.º 1
0
class UploadDocument(AdvancedMultiplePermissionsRequiredMixin, CreateView):
    GET_permissions = {'any': ('auth.can_submit_responses', 'auth.can_view_users', 'auth.can_edit_questionnaire')}
    POST_permissions = {'any': ('auth.can_submit_responses', )}
    model = SupportDocument
    template_name = 'questionnaires/entry/upload.html'
    form_class = SupportDocumentUploadForm
    success_url = None

    def get(self, request, *args, **kwargs):
        self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
        self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
        self.user_questionnaire_service = UserQuestionnaireService(get_country(self.request), self.questionnaire)
        return super(UploadDocument, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
        self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
        self.user_questionnaire_service = UserQuestionnaireService(get_country(self.request), self.questionnaire)
        return super(UploadDocument, self).post(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UploadDocument, self).get_context_data(**kwargs)
        upload_data_initial = {'questionnaire': self.questionnaire, 'country': get_country(self.request)}
        attachments = self.user_questionnaire_service.attachments()
        initial = {'questionnaire': self.questionnaire, 'user': self.request.user}
        context.update({'upload_form': self.form_class(initial=upload_data_initial),
                        'button_label': 'Upload', 'id': 'id-upload-form', 'questionnaire': self.questionnaire,
                        'action': reverse('upload_document', args=(self.questionnaire.id,)),
                        'documents': attachments,
                        'section_form': SectionForm(initial=initial),
                        'ordered_sections': self.questionnaire.sections.order_by('order'),
                        'preview': self._check_preview_mode(),
                        'new_section_action': reverse("new_section_page", args=(self.questionnaire.id,))})
        return context

    def _check_preview_mode(self):
        return self.user_questionnaire_service.preview() or self.questionnaire.is_finalized() or \
               self.questionnaire.is_published() or 'preview' in self.request.GET

    def form_valid(self, form):
        messages.success(self.request, "File was uploaded successfully")
        return super(UploadDocument, self).form_valid(form)

    def form_invalid(self, form):
        return render(self.request, self.template_name, {'upload_form': form,
                                                         'button_label': 'Upload', 'id': 'id-upload-form',
                                                         'questionnaire': self.questionnaire,
                                                         'documents': self.user_questionnaire_service.attachments(),
                                                         'ordered_sections': self.questionnaire.sections.order_by(
                                                             'order')})
Ejemplo n.º 2
0
    def test_user_knows_its_documents_in_a_questionnaire(self):
        m = mock_open()
        with patch('__main__.open', m, create=True):
            filename = "some_filename.txt"
            with open(filename, 'w') as document:
                document.write("Some stuff")
            self.document = open(filename, 'rb')


        document_in = SupportDocument.objects.create(path=File(self.document), country=self.country,
                                                   questionnaire=self.questionnaire)
        questionnaire_2 = Questionnaire.objects.create(name="haha", year=2013)
        document_not_in = SupportDocument.objects.create(path=File(self.document), country=self.country,
                                                   questionnaire=questionnaire_2)


        user_service = UserQuestionnaireService(self.country, self.questionnaire)
        documents = user_service.attachments()

        self.assertEqual(1, documents.count())
        self.assertIn(document_in, documents)
        self.assertNotIn(document_not_in, documents)

        os.system("rm -rf %s" % filename)
        os.system("rm -rf media/user_uploads/*")
Ejemplo n.º 3
0
class UploadDocument(CreateView):
    model = SupportDocument
    template_name = 'questionnaires/entry/upload.html'
    form_class = SupportDocumentUploadForm
    success_url = None

    def get(self, request, *args, **kwargs):
        self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
        self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
        self.user_questionnaire_service = UserQuestionnaireService(self.request.user.user_profile.country, self.questionnaire)
        return super(UploadDocument, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
        self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
        self.user_questionnaire_service = UserQuestionnaireService(self.request.user.user_profile.country, self.questionnaire)
        return super(UploadDocument, self).post(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UploadDocument, self).get_context_data(**kwargs)
        users_country = self.request.user.user_profile.country
        upload_data_initial = {'questionnaire': self.questionnaire, 'country': users_country}
        context.update({'upload_form': self.form_class(initial=upload_data_initial),
                        'button_label': 'Upload', 'id': 'id-upload-form', 'questionnaire': self.questionnaire,
                        'documents': self.user_questionnaire_service.attachments(),
                        'ordered_sections': self.questionnaire.sections.order_by('order'),
                        'preview': self.user_questionnaire_service.preview()})
        return context

    def form_valid(self, form):
        messages.success(self.request, "File was uploaded successfully")
        return super(UploadDocument, self).form_valid(form)

    def form_invalid(self, form):
        return render(self.request, self.template_name, {'upload_form': form,
                        'button_label': 'Upload', 'id': 'id-upload-form', 'questionnaire': self.questionnaire,
                        'documents': self.user_questionnaire_service.attachments(),
                        'ordered_sections': self.questionnaire.sections.order_by('order')})
Ejemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id'])
        section = Section.objects.get(id=self.kwargs['section_id'])
        user_questionnaire_service = UserQuestionnaireService(self.request.user.user_profile.country, questionnaire)
        initial = {'country': self.request.user.user_profile.country, 'status': 'Draft',
                   'version': user_questionnaire_service.POST_version, 'questionnaire': questionnaire}
        formsets = QuestionnaireEntryFormService(section, initial=initial, data=request.POST,
                                                 edit_after_submit=user_questionnaire_service.edit_after_submit)

        context = {'questionnaire': questionnaire, 'section': section,
                   'formsets': formsets, 'ordered_sections': questionnaire.ordered_sections(),
                   'form': SectionForm(initial={'questionnaire': questionnaire, 'user': request.user}),
                   'new_section_action': reverse('new_section_page', args=(questionnaire.id, )),
                   'subsection_form': SubSectionForm(),
                   'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)),
                   'documents': user_questionnaire_service.attachments()}

        if formsets.is_valid():
            return self._form_valid(request, formsets, context)
        return self._form_invalid(request, context)
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id'])
        section = Section.objects.get(id=self.kwargs['section_id'])
        user_questionnaire_service = UserQuestionnaireService(self.request.user.user_profile.country, questionnaire)
        initial = {'status': 'Draft', 'country': self.request.user.user_profile.country,
                   'version': user_questionnaire_service.GET_version, 'questionnaire': questionnaire}
        required_answers = 'show' in request.GET
        formsets = QuestionnaireEntryFormService(section, initial=initial, highlight=required_answers,
                                                 edit_after_submit=user_questionnaire_service.edit_after_submit)

        printable = 'printable' in request.GET
        preview = user_questionnaire_service.preview() or 'preview' in request.GET

        context = {'questionnaire': questionnaire,
                   'section': section, 'printable': printable,
                   'preview': preview, 'formsets': formsets,
                   'ordered_sections': questionnaire.sections.order_by('order'),
                   'form': SectionForm(initial={'questionnaire': questionnaire}),
                   'action': reverse('new_section_page', args=(questionnaire.id, )),
                   'subsection_form': SubSectionForm(),
                   'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)),
                   'documents': user_questionnaire_service.attachments()}

        return self.render_to_response(context)
Ejemplo n.º 6
0
class Entry(DoesNotExistExceptionHandlerMixin, AdvancedMultiplePermissionsRequiredMixin, FormView):
    template_name = 'questionnaires/entry/index.html'
    model = Questionnaire
    GET_permissions = {'any': ('auth.can_submit_responses', 'auth.can_view_users', 'auth.can_edit_questionnaire')}
    POST_permissions = {'any': ('auth.can_submit_responses', )}

    def get(self, request, *args, **kwargs):
        query_params = {'id': self.kwargs['questionnaire_id'],
                        'region__in': get_regions(request), 'status__in': get_questionnaire_status(request)}
        query_params = filter_empty_values(query_params)
        questionnaire = Questionnaire.objects.get(**query_params)
        section = Section.objects.get(id=self.kwargs['section_id'])
        country = get_country(self.request)
        self.user_questionnaire_service = UserQuestionnaireService(country, questionnaire, request.GET.get("version"))
        get_version = self.user_questionnaire_service.GET_version
        initial = {'status': 'Draft', 'country': country,
                   'version': get_version, 'questionnaire': questionnaire}
        required_answers = 'show' in request.GET
        formsets = QuestionnaireEntryFormService(section, initial=initial, highlight=required_answers,
                                                 edit_after_submit=self.user_questionnaire_service.edit_after_submit)
        printable = 'printable' in request.GET
        version = request.GET.get('version', None)
        preview = self._check_preview_mode(questionnaire)
        region = self.request.user.user_profile.region

        section_initial = {'questionnaire': questionnaire, 'region': region, 'user': request.user}
        context = {'questionnaire': questionnaire,
                   'section': section, 'printable': printable,
                   'preview': preview, 'formsets': formsets,
                   'ordered_sections': questionnaire.sections.order_by('order'),
                   'section_form': SectionForm(initial=section_initial),
                   'new_section_action': reverse('new_section_page', args=(questionnaire.id, )),
                   'subsection_form': SubSectionForm(initial=section_initial),
                   'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)),
                   'the_version': version or get_version,
                   'country': country,
                   'documents': self.user_questionnaire_service.attachments()}
        # slow in some instances take 1.5 secs
        return self.render_to_response(context)

    def _check_preview_mode(self, questionnaire):
        user = self.request.user
        perm = None
        if user.has_perm('auth.can_edit_questionnaire'):
            perm = questionnaire.is_finalized() or questionnaire.is_published()
        return perm or self.user_questionnaire_service.preview() or 'preview' in self.request.GET

    def post(self, request, *args, **kwargs):
        questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id'])
        section = Section.objects.get(id=self.kwargs['section_id'])
        user_questionnaire_service = UserQuestionnaireService(self.request.user.user_profile.country, questionnaire)
        initial = {'country': self.request.user.user_profile.country, 'status': 'Draft',
                   'version': user_questionnaire_service.POST_version, 'questionnaire': questionnaire}
        formsets = QuestionnaireEntryFormService(section, initial=initial, data=request.POST,
                                                 edit_after_submit=user_questionnaire_service.edit_after_submit)

        context = {'questionnaire': questionnaire, 'section': section,
                   'formsets': formsets, 'ordered_sections': questionnaire.ordered_sections(),
                   'form': SectionForm(initial={'questionnaire': questionnaire, 'user': request.user}),
                   'new_section_action': reverse('new_section_page', args=(questionnaire.id, )),
                   'subsection_form': SubSectionForm(),
                   'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)),
                   'documents': user_questionnaire_service.attachments()}

        if formsets.is_valid():
            return self._form_valid(request, formsets, context)
        return self._form_invalid(request, context)

    def _form_valid(self, request, formsets, context):
        formsets.save()
        message = 'Draft saved.'
        messages.success(request, message)
        if request.POST.get('redirect_url', None):
            return HttpResponseRedirect(request.POST['redirect_url'].replace('preview=1', ''))
        return self.render_to_response(context)

    def _form_invalid(self, request, context):
        message = 'Draft NOT saved. See errors below.'
        messages.error(request, message)
        return self.render_to_response(context)