@@ -52,13 +52,62 @@ def __init__(self, *args, **kwargs):
52
52
self .user = kwargs .pop ("user" , None )
53
53
super ().__init__ (* args , ** kwargs )
54
54
55
+ self .fields ["repo" ].widget .attrs ["placeholder" ] = self .placehold_repo ()
56
+ self .fields ["repo" ].widget .attrs ["required" ] = True
57
+
58
+ queryset = RemoteRepository .objects .for_project_linking (self .user )
59
+ current_remote_repo = (
60
+ self .instance .remote_repository if self .instance .pk else None
61
+ )
62
+ # If there is a remote repo attached to the project, add it to the queryset,
63
+ # since the current user might not have access to it.
64
+ if current_remote_repo :
65
+ queryset |= RemoteRepository .objects .filter (
66
+ pk = current_remote_repo .pk
67
+ ).distinct ()
68
+ self .fields ["remote_repository" ].queryset = queryset
69
+ self .fields ["remote_repository" ].empty_label = _ ("No connected repository" )
70
+
55
71
def save (self , commit = True ):
56
72
project = super ().save (commit )
57
73
if commit :
58
74
if self .user and not project .users .filter (pk = self .user .pk ).exists ():
59
75
project .users .add (self .user )
60
76
return project
61
77
78
+ def clean_name (self ):
79
+ name = self .cleaned_data .get ("name" , "" )
80
+ if not self .instance .pk :
81
+ potential_slug = slugify (name )
82
+ if Project .objects .filter (slug = potential_slug ).exists ():
83
+ raise forms .ValidationError (
84
+ _ ("Invalid project name, a project already exists with that name" ),
85
+ ) # yapf: disable # noqa
86
+ if not potential_slug :
87
+ # Check the generated slug won't be empty
88
+ raise forms .ValidationError (
89
+ _ ("Invalid project name" ),
90
+ )
91
+
92
+ return name
93
+
94
+ def clean_repo (self ):
95
+ repo = self .cleaned_data .get ("repo" , "" )
96
+ return repo .rstrip ("/" )
97
+
98
+ def placehold_repo (self ):
99
+ return choice (
100
+ [
101
+ "https://bitbucket.org/cherrypy/cherrypy" ,
102
+ "https://bitbucket.org/birkenfeld/sphinx" ,
103
+ "https://bitbucket.org/hpk42/tox" ,
104
+ "https://github.com/zzzeek/sqlalchemy.git" ,
105
+ "https://github.com/django/django.git" ,
106
+ "https://github.com/fabric/fabric.git" ,
107
+ "https://github.com/ericholscher/django-kong.git" ,
108
+ ]
109
+ )
110
+
62
111
63
112
class ProjectTriggerBuildMixin :
64
113
@@ -313,73 +362,13 @@ class ProjectBasicsForm(ProjectForm):
313
362
314
363
class Meta :
315
364
model = Project
316
- fields = ("name" , "repo" , "default_branch" , "language" )
317
-
318
- remote_repository = forms .IntegerField (
319
- widget = forms .HiddenInput (),
320
- required = False ,
321
- )
365
+ fields = ("name" , "repo" , "default_branch" , "language" , "remote_repository" )
322
366
323
367
def __init__ (self , * args , ** kwargs ):
324
368
super ().__init__ (* args , ** kwargs )
325
369
self .fields ["repo" ].widget .attrs ["placeholder" ] = self .placehold_repo ()
326
370
self .fields ["repo" ].widget .attrs ["required" ] = True
327
-
328
- def save (self , commit = True ):
329
- """Add remote repository relationship to the project instance."""
330
- instance = super ().save (commit )
331
- remote_repo = self .cleaned_data .get ("remote_repository" , None )
332
- if remote_repo :
333
- if commit :
334
- remote_repo .projects .add (self .instance )
335
- remote_repo .save ()
336
- else :
337
- instance .remote_repository = remote_repo
338
- return instance
339
-
340
- def clean_name (self ):
341
- name = self .cleaned_data .get ("name" , "" )
342
- if not self .instance .pk :
343
- potential_slug = slugify (name )
344
- if Project .objects .filter (slug = potential_slug ).exists ():
345
- raise forms .ValidationError (
346
- _ ("Invalid project name, a project already exists with that name" ),
347
- ) # yapf: disable # noqa
348
- if not potential_slug :
349
- # Check the generated slug won't be empty
350
- raise forms .ValidationError (
351
- _ ("Invalid project name" ),
352
- )
353
-
354
- return name
355
-
356
- def clean_repo (self ):
357
- repo = self .cleaned_data .get ("repo" , "" )
358
- return repo .rstrip ("/" )
359
-
360
- def clean_remote_repository (self ):
361
- remote_repo = self .cleaned_data .get ("remote_repository" , None )
362
- if not remote_repo :
363
- return None
364
- try :
365
- return RemoteRepository .objects .for_project_linking (self .user ).get (
366
- pk = remote_repo ,
367
- )
368
- except RemoteRepository .DoesNotExist as exc :
369
- raise forms .ValidationError (_ ("Repository invalid" )) from exc
370
-
371
- def placehold_repo (self ):
372
- return choice (
373
- [
374
- "https://bitbucket.org/cherrypy/cherrypy" ,
375
- "https://bitbucket.org/birkenfeld/sphinx" ,
376
- "https://bitbucket.org/hpk42/tox" ,
377
- "https://github.com/zzzeek/sqlalchemy.git" ,
378
- "https://github.com/django/django.git" ,
379
- "https://github.com/fabric/fabric.git" ,
380
- "https://github.com/ericholscher/django-kong.git" ,
381
- ]
382
- )
371
+ self .fields ["remote_repository" ].widget = forms .HiddenInput ()
383
372
384
373
385
374
class ProjectConfigForm (forms .Form ):
@@ -394,7 +383,7 @@ def __init__(self, *args, **kwargs):
394
383
395
384
class UpdateProjectForm (
396
385
ProjectTriggerBuildMixin ,
397
- ProjectBasicsForm ,
386
+ ProjectForm ,
398
387
ProjectPRBuildsMixin ,
399
388
):
400
389
@@ -406,6 +395,7 @@ class Meta:
406
395
# Basics and repo settings
407
396
"name" ,
408
397
"repo" ,
398
+ "remote_repository" ,
409
399
"language" ,
410
400
"default_version" ,
411
401
"privacy_level" ,
0 commit comments