Skip to content

Commit f684dc9

Browse files
authored
Add relation between project and remote repoistory objects (#2745)
* Add relation between project and remote repoistory objects This will be used with the webhook additions to more tightly couple projects and data from GitHub. For webhooks, this work will allow us to explicitly tell how the project is connected to the GitHub/Bitbucket/etc repo, and will allow for explicit resyncing of the webhook as well. * Alter relation on delete
1 parent 98c1e21 commit f684dc9

File tree

12 files changed

+220
-122
lines changed

12 files changed

+220
-122
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.12 on 2017-03-22 20:10
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('oauth', '0007_org_slug_nonunique'),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name='remoterepository',
18+
name='project',
19+
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='remote_repository', to='projects.Project'),
20+
),
21+
]

readthedocs/oauth/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class RemoteRepository(models.Model):
8484
related_name='repositories', null=True, blank=True)
8585
active = models.BooleanField(_('Active'), default=False)
8686

87+
project = models.OneToOneField(Project, on_delete=models.SET_NULL,
88+
related_name='remote_repository', null=True,
89+
blank=True)
8790
name = models.CharField(_('Name'), max_length=255)
8891
full_name = models.CharField(_('Full Name'), max_length=255)
8992
description = models.TextField(_('Description'), blank=True, null=True,

readthedocs/projects/forms.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from readthedocs.builds.constants import TAG
1717
from readthedocs.core.utils import trigger_build, slugify
1818
from readthedocs.redirects.models import Redirect
19+
from readthedocs.oauth.models import RemoteRepository
1920
from readthedocs.projects import constants
2021
from readthedocs.projects.exceptions import ProjectSpamError
2122
from readthedocs.projects.models import Project, EmailHook, WebHook, Domain
@@ -74,6 +75,11 @@ class Meta:
7475
model = Project
7576
fields = ('name', 'repo', 'repo_type')
7677

78+
remote_repository = forms.CharField(
79+
widget=forms.HiddenInput(),
80+
required=False,
81+
)
82+
7783
def __init__(self, *args, **kwargs):
7884
show_advanced = kwargs.pop('show_advanced', False)
7985
super(ProjectBasicsForm, self).__init__(*args, **kwargs)
@@ -85,6 +91,18 @@ def __init__(self, *args, **kwargs):
8591
self.fields['repo'].widget.attrs['placeholder'] = self.placehold_repo()
8692
self.fields['repo'].widget.attrs['required'] = True
8793

94+
def save(self, commit=True):
95+
"""Add remote repository relationship to the project instance"""
96+
instance = super(ProjectBasicsForm, self).save(commit)
97+
remote_repo = self.cleaned_data.get('remote_repository', None)
98+
if remote_repo:
99+
if commit:
100+
remote_repo.project = self.instance
101+
remote_repo.save()
102+
else:
103+
instance.remote_repository = remote_repo
104+
return instance
105+
88106
def clean_name(self):
89107
name = self.cleaned_data.get('name', '')
90108
if not self.instance.pk:
@@ -105,6 +123,18 @@ def clean_repo(self):
105123
u'public (http:// or git://) clone url'))
106124
return repo
107125

126+
def clean_remote_repository(self):
127+
remote_repo = self.cleaned_data.get('remote_repository', None)
128+
if not remote_repo:
129+
return None
130+
try:
131+
return RemoteRepository.objects.get(
132+
pk=remote_repo,
133+
users=self.user,
134+
)
135+
except RemoteRepository.DoesNotExist:
136+
raise forms.ValidationError(_(u'Repository invalid'))
137+
108138
def placehold_repo(self):
109139
return choice([
110140
'https://bitbucket.org/cherrypy/cherrypy',
@@ -126,7 +156,8 @@ class Meta:
126156
fields = (
127157
'description',
128158
'documentation_type',
129-
'language', 'programming_language',
159+
'language',
160+
'programming_language',
130161
'project_url',
131162
'tags',
132163
)

readthedocs/projects/static-src/projects/js/import.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function Project (instance, view) {
9090
repo_type: self.vcs(),
9191
description: self.description(),
9292
project_url: self.html_url(),
93+
remote_repository: self.id(),
9394
},
9495
form = $('<form />');
9596

readthedocs/projects/static/projects/js/import.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/projects/views/private.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,8 @@ def get_form_kwargs(self, step=None):
227227
kwargs['user'] = self.request.user
228228
if step == 'basics':
229229
kwargs['show_advanced'] = True
230-
if step == 'extra':
231-
extra_form = self.get_form_from_step('basics')
232-
project = extra_form.save(commit=False)
233-
kwargs['instance'] = project
234230
return kwargs
235231

236-
def get_form_from_step(self, step):
237-
form = self.form_list[step](
238-
data=self.get_cleaned_data_for_step(step),
239-
**self.get_form_kwargs(step)
240-
)
241-
form.full_clean()
242-
return form
243-
244232
def get_template_names(self):
245233
"""Return template names based on step name"""
246234
return 'projects/import_{0}.html'.format(self.steps.current)
@@ -370,7 +358,7 @@ def get(self, request, *args, **kwargs):
370358
def post(self, request, *args, **kwargs):
371359
initial_data = {}
372360
initial_data['basics'] = {}
373-
for key in ['name', 'repo', 'repo_type']:
361+
for key in ['name', 'repo', 'repo_type', 'remote_repository']:
374362
initial_data['basics'][key] = request.POST.get(key)
375363
initial_data['extra'] = {}
376364
for key in ['description', 'project_url']:

0 commit comments

Comments
 (0)