Merge lp:~jameinel/maas/space-from-name into lp:~maas-committers/maas/trunk

Proposed by John A Meinel
Status: Rejected
Rejected by: MAAS Lander
Proposed branch: lp:~jameinel/maas/space-from-name
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 40 lines (+9/-3)
2 files modified
src/maasserver/forms_subnet.py (+2/-1)
src/maasserver/models/space.py (+7/-2)
To merge this branch: bzr merge lp:~jameinel/maas/space-from-name
Reviewer Review Type Date Requested Status
MAAS Maintainers Pending
Review via email: mp+276632@code.launchpad.net

Description of the change

This is a couple of steps to make the API a bit nicer for humans to interact with. It allows looking up spaces and subnets by name instead of by ID, and allows forms to be filled in also with the name.

I'd like to get some feedback if this is actually interesting, or if it is too much of an API break (I didn't see a way to have Forms allow *either* an ID or a name, but "to_field_name" works to select what you want.)

The other issue is that space names are not currently Unique, which seems very strange to me. Certainly Juju isn't going to handle that well "juju deploy --constraints spaces=foo" is going to select "1 of the spaces named foo" ?

If you're interested in something like this, I'm interested in pushing it a bit further, and I'm happy to add tests, etc, but I'd want to know I was going in an acceptable direction.

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

Transitioned to Git.

lp:maas has now moved from Bzr to Git.
Please propose your branches with Launchpad using Git.

git clone https://git.launchpad.net/maas

Unmerged revisions

4458. By John A Meinel

allow friendlier names to be passed to the API instead of requiring them to be ids

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/forms_subnet.py'
2--- src/maasserver/forms_subnet.py 2015-09-15 15:51:00 +0000
3+++ src/maasserver/forms_subnet.py 2015-11-04 10:14:07 +0000
4@@ -39,7 +39,8 @@
5 min_value=0, max_value=4095, required=False)
6
7 space = forms.ModelChoiceField(
8- queryset=Space.objects.all(), required=False)
9+ queryset=Space.objects.all(), required=False,
10+ to_field_name="name")
11
12 class Meta:
13 model = Subnet
14
15=== modified file 'src/maasserver/models/space.py'
16--- src/maasserver/models/space.py 2015-10-21 19:35:30 +0000
17+++ src/maasserver/models/space.py 2015-11-04 10:14:07 +0000
18@@ -81,7 +81,12 @@
19 docs.djangoproject.com/en/dev/topics/http/views/
20 #the-http404-exception
21 """
22- space = get_object_or_404(self.model, id=id)
23+ try:
24+ int_id = int(id)
25+ space = get_object_or_404(self.model, id=int_id)
26+ except ValueError as e:
27+ # The id passed is not a valid integer, so assume it is a name
28+ space = get_object_or_404(self.model, name=id)
29 if user.has_perm(perm, space):
30 return space
31 else:
32@@ -103,7 +108,7 @@
33 objects = SpaceManager()
34
35 name = CharField(
36- max_length=256, editable=True, null=True, blank=True, unique=False,
37+ max_length=256, editable=True, null=True, blank=True, unique=True,
38 validators=[validate_space_name])
39
40 def __unicode__(self):