Example #1
0
    def test_utils_build_cancel_path(self):
        from creme.creme_core.views.utils import build_cancel_path

        factory = RequestFactory()
        path = '/foobar'

        # -------------------------
        request = factory.get('/')
        self.assertIsNone(build_cancel_path(request))

        # -------------------------
        request = factory.get('/')
        request.META['HTTP_REFERER'] = 'http://testserver' + path
        self.assertEqual(path, build_cancel_path(request))

        # -------------------------
        request.META['HTTP_REFERER'] = 'http://otherserver' + path
        self.assertIsNone(build_cancel_path(request))

        # -------------------------
        request.META['HTTP_REFERER'] = 'http://testserver:8005' + path
        self.assertIsNone(build_cancel_path(request))

        # -------------------------
        request.META['HTTP_REFERER'] = 'http://testserver:80' + path
        self.assertEqual(path, build_cancel_path(request))

        # -------------------------
        request.META['HTTP_REFERER'] = 'https://testserver' + path
        self.assertIsNone(build_cancel_path(request))

        # -------------------------
        request.get_host = lambda: 'testserver:8005'
        request.META['HTTP_REFERER'] = 'http://testserver:8005' + path
        self.assertEqual(path, build_cancel_path(request))
Example #2
0
def abstract_vcf_import(
        request,
        file_form=VcfForm,
        import_form=VcfImportForm,
        template='creme_core/generics/blockform/add.html',
        title=_('Import contact from VCF file'),
):
    user = request.user
    submit_label = Contact.save_label

    if request.method == 'POST':
        POST = request.POST
        step = get_from_POST_or_404(POST, 'vcf_step', cast=int, default=0)
        form_instance = file_form(user=user, data=POST, files=request.FILES)

        if step == 0:
            if form_instance.is_valid():
                form_instance = import_form(
                    user=user,
                    vcf_data=form_instance.cleaned_data['vcf_file'],
                    initial={'vcf_step': 1},
                )
            else:
                submit_label = _('Import this VCF file')
        else:
            if step != 1:
                raise Http404('"vcf_step" must be in {0, 1}')

            form_instance = import_form(user=user, data=POST)

            if form_instance.is_valid():
                contact = form_instance.save()
                return redirect(contact)

        cancel_url = POST.get('cancel_url')
    else:
        form_instance = file_form(user=user, initial={'vcf_step': 0})
        submit_label = _('Import this VCF file')
        cancel_url = build_cancel_path(request)

    return render(
        request,
        template,
        {
            'form': form_instance,
            'title': title,
            'submit_label': submit_label,
            'cancel_url': cancel_url,
        },
    )
Example #3
0
def _add_activity(
        request,
        form_class,
        content_template='activities/frags/activity_form_content.html',
        type_id=None,
        **form_args):
    warnings.warn('activities.views.activity._add_activity() is deprecated.',
                  DeprecationWarning)

    if request.method == 'POST':
        POST = request.POST
        form = form_class(activity_type_id=type_id,
                          user=request.user,
                          data=POST,
                          **form_args)

        if form.is_valid():
            form.save()
            entity = form_args.get('related_entity', form.instance)

            return redirect(entity)

        cancel_url = POST.get('cancel_url')
    else:
        form = form_class(activity_type_id=type_id,
                          user=request.user,
                          **form_args)
        cancel_url = build_cancel_path(request)

    return render(
        request,
        'activities/add_activity_form.html',
        {
            'form': form,
            'title': Activity.get_creation_title(type_id),
            'content_template': content_template,
            'submit_label': Activity.save_label,
            'cancel_url': cancel_url,
        },
    )
Example #4
0
def _get_page_url(request):
    return build_cancel_path(request) or reverse('mobile__portal')