Re: Optional URL parameters in django.urls.path

2022-10-02 Thread Carlton Gibson
Hi.

Re-reading again, I'm still not sure (personally) of the need/motivation
given either of the "use two entries" or "use re_path()" options.
Maybe it would be a nice-to-have but I'm not seeing the benefit as
outweighing the complexity (remembering docs, and tests, and related
functionality such as reverse() that would also need updating.)

(IRL I've rarely needed this pattern. I've used both the options above
without issue)

So, I'd probably be -1 at this point.

Kind regards
Carlton


On Sun, 2 Oct 2022 at 08:16, Meiyer  wrote:

> Is there any interest in implementing this feature? If yes, what would be
> the syntax for the path components that would follow the optional parameter
> (only if it is present)? I thought about something along the lines of
>  with the characters after the second colon having to
> appear literally in the URL.
> For example:
> path(format_lazy('{org}/',
> org=pgettext_lazy('URL', 'event/organizers/')), OrganizersView.as_vew())
> would match both
> /üritus/korraldajad/perekonnanimi/ and
> /üritus/korraldajad/FI/perekonnanimi/ (in Estonian).
> On Monday, September 19, 2022 at 4:07:26 PM UTC+2 matthew.pava wrote:
>
>> I wonder if maybe you should look at the APPEND_SLASH setting.
>>
>> https://docs.djangoproject.com/en/4.1/ref/settings/#append-slash
>>
>>
>>
>>
>>
>> *From:* django-d...@googlegroups.com  *On
>> Behalf Of *Meiyer
>> *Sent:* Monday, September 19, 2022 8:59 AM
>> *To:* Django developers (Contributions to Django itself) <
>> django-d...@googlegroups.com>
>> *Subject:* Re: Optional URL parameters in django.urls.path
>>
>>
>>
>> > One approach is to route two patterns (one with the parameter and one
>> without) to the same URL. Some duplication but (perhaps) easier to read at
>> a glance than either an optional `?` operator.
>>
>>
>>
>> I thought about that, but it won't work if I want both cases to be named
>> similarly (via the `name` parameter). It also feels as duplicating the same
>> line.
>>
>>
>>
>> > Given that — and the alternative re_path() approach being available —
>>  unless there was a particularly simple implementation on display I'd
>> likely be sceptical about an addition here… 🤔
>>
>>
>>
>> I did a bit of digging in the Django code and in fact it might be quite
>> easy (it is also possible I am missing the elephant). django.urls.resolvers
>> defines the syntax for the path parameters on line 245 [1] and converts the
>> matches to regular expressions on line 290 [2]. Looks like an addition of
>> the '?' to the regex after the closing bracket would do it..?
>>
>>
>>
>>
>>
>> [1]
>> https://github.com/django/django/blob/baf9604ed8fed3e6e7ddfaca2d83c377c81399ae/django/urls/resolvers.py#L245
>>
>> [2]
>> https://github.com/django/django/blob/baf9604ed8fed3e6e7ddfaca2d83c377c81399ae/django/urls/resolvers.py#L290
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-develop...@googlegroups.com.
>>
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/9e0828d7-6434-4fd1-b605-8113342fcba9n%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/43b40cdf-2f3d-4a28-9208-af16dd68ede0n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAJwKpyQRwLrfhFVLzxMuw3_ycqt0eaDaKHD7habPKPnhRAAJHQ%40mail.gmail.com.


`call_command` raises ValueError during testing

2022-10-02 Thread Ferran Jovell
Hello everyone,

I was working on a admin command today, and when setting up tests and
fixtures for running the command during testing I found this error:

Traceback (most recent call last):
  File "/home/fjm/code/django-stuff/tutorial/polls/tests.py", line 10, in
test_command_output
call_command("closepoll", poll_ids=1, stdout=out)
  File
"/home/fjm/.local/share/virtualenvs/tutorial/lib/python3.10/site-packages/django/core/management/__init__.py",
line 168, in call_command
parse_args.append(min(opt.option_strings), default=0)
ValueError: min() arg is an empty sequence

However, what is weird is that when run from with the manage.py script,
everything seems to be working just fine.

This is the command code

from django.core.management.base import BaseCommand


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("poll_ids", nargs="+", type=int)

def handle(self, *args, **options):
pass

And this is the test code:
from io import StringIO

from django.core.management import call_command
from django.test import SimpleTestCase


class ClosepollTest(SimpleTestCase):
def test_command_output(self):
out = StringIO()
call_command("closepoll", poll_ids=1, stdout=out)
self.assertIn("Expected output", out.getvalue())

I set up a repo with the minimal code to reproduce this:
https://github.com/mrswats/django-polls-test
Is this a bug? Am I doing something wrong? Have I missed anything?

I checked the bug tracker but could not find anything related to this and
before opening an issue I figured I'd ask. If it is indeed a bug I will
happily write a ticket in the tracker.

Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAEKh-skbGsRO7%2B1H9CBJQEsztwhTUTMus8vuTkQQUUvmXMAj0w%40mail.gmail.com.


Re: `call_command` raises ValueError during testing

2022-10-02 Thread Tim Graham
Hi Ferran,

It looks like opt.option_strings is empty because "poll_ids" (in 
parser.add_argument()) isn't prefixed with a dash or double dash. 

You could instead use call_command() like this: call_command("closepoll", 1)

But I think your invocation could be fixed (or at least not fail so 
obscurely).
On Sunday, October 2, 2022 at 7:49:26 AM UTC-4 Ferran Jovell wrote:

> Hello everyone,
>
> I was working on a admin command today, and when setting up tests and 
> fixtures for running the command during testing I found this error:
>
> Traceback (most recent call last):
>   File "/home/fjm/code/django-stuff/tutorial/polls/tests.py", line 10, in 
> test_command_output
> call_command("closepoll", poll_ids=1, stdout=out)
>   File 
> "/home/fjm/.local/share/virtualenvs/tutorial/lib/python3.10/site-packages/django/core/management/__init__.py",
>  
> line 168, in call_command
> parse_args.append(min(opt.option_strings), default=0)
> ValueError: min() arg is an empty sequence
>
> However, what is weird is that when run from with the manage.py script, 
> everything seems to be working just fine.
>
> This is the command code
>
> from django.core.management.base import BaseCommand
>
>
> class Command(BaseCommand):
> def add_arguments(self, parser):
> parser.add_argument("poll_ids", nargs="+", type=int)
>
> def handle(self, *args, **options):
> pass
>
> And this is the test code:
> from io import StringIO
>
> from django.core.management import call_command
> from django.test import SimpleTestCase
>
>
> class ClosepollTest(SimpleTestCase):
> def test_command_output(self):
> out = StringIO()
> call_command("closepoll", poll_ids=1, stdout=out)
> self.assertIn("Expected output", out.getvalue())
>
> I set up a repo with the minimal code to reproduce this:
> https://github.com/mrswats/django-polls-test
> Is this a bug? Am I doing something wrong? Have I missed anything?
>
> I checked the bug tracker but could not find anything related to this and 
> before opening an issue I figured I'd ask. If it is indeed a bug I will 
> happily write a ticket in the tracker.
>
> Thanks in advance.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/da3b35c5-eaed-49e8-9b61-eec4b3fc4736n%40googlegroups.com.