-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
models.py
249 lines (216 loc) · 7.2 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""OAuth service models."""
from allauth.socialaccount.models import SocialAccount
from django.contrib.auth.models import User
from django.core.validators import URLValidator
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django_extensions.db.models import TimeStampedModel
from readthedocs.projects.constants import REPO_CHOICES
from readthedocs.projects.models import Project
from .constants import VCS_PROVIDER_CHOICES
from .querysets import RemoteOrganizationQuerySet, RemoteRepositoryQuerySet
class RemoteOrganization(TimeStampedModel):
"""
Organization from remote service.
This encapsulates both Github and Bitbucket
"""
users = models.ManyToManyField(
User,
verbose_name=_("Users"),
related_name="oauth_organizations",
through="RemoteOrganizationRelation",
)
slug = models.CharField(_("Slug"), max_length=255)
name = models.CharField(_("Name"), max_length=255, null=True, blank=True)
email = models.EmailField(_("Email"), max_length=255, null=True, blank=True)
avatar_url = models.URLField(
_("Avatar image URL"),
null=True,
blank=True,
max_length=255,
)
url = models.URLField(
_("URL to organization page"),
max_length=200,
null=True,
blank=True,
)
# VCS provider organization id
remote_id = models.CharField(db_index=True, max_length=128)
vcs_provider = models.CharField(
_("VCS provider"), choices=VCS_PROVIDER_CHOICES, max_length=32
)
objects = RemoteOrganizationQuerySet.as_manager()
class Meta:
ordering = ["name"]
unique_together = (
"remote_id",
"vcs_provider",
)
db_table = "oauth_remoteorganization_2020"
def __str__(self):
return self.slug
def get_remote_organization_relation(self, user, social_account):
"""Return RemoteOrganizationRelation object for the remote organization."""
(
remote_organization_relation,
_,
) = RemoteOrganizationRelation.objects.get_or_create(
remote_organization=self, user=user, account=social_account
)
return remote_organization_relation
class RemoteOrganizationRelation(TimeStampedModel):
remote_organization = models.ForeignKey(
RemoteOrganization,
related_name="remote_organization_relations",
on_delete=models.CASCADE,
)
user = models.ForeignKey(
User, related_name="remote_organization_relations", on_delete=models.CASCADE
)
account = models.ForeignKey(
SocialAccount,
verbose_name=_("Connected account"),
related_name="remote_organization_relations",
on_delete=models.CASCADE,
)
class Meta:
unique_together = (
"remote_organization",
"account",
)
class RemoteRepository(TimeStampedModel):
"""
Remote importable repositories.
This models Github and Bitbucket importable repositories
"""
# This should now be a OneToOne
users = models.ManyToManyField(
User,
verbose_name=_("Users"),
related_name="oauth_repositories",
through="RemoteRepositoryRelation",
)
organization = models.ForeignKey(
RemoteOrganization,
verbose_name=_("Organization"),
related_name="repositories",
null=True,
blank=True,
on_delete=models.CASCADE,
)
name = models.CharField(_("Name"), max_length=255)
full_name = models.CharField(
_("Full Name"),
max_length=255,
db_index=True,
)
description = models.TextField(
_("Description"),
blank=True,
null=True,
help_text=_("Description of the project"),
)
avatar_url = models.URLField(
_("Owner avatar image URL"),
null=True,
blank=True,
max_length=255,
)
ssh_url = models.URLField(
_("SSH URL"),
max_length=512,
blank=True,
validators=[URLValidator(schemes=["ssh"])],
)
clone_url = models.URLField(
_("Repository clone URL"),
max_length=512,
blank=True,
validators=[
URLValidator(schemes=["http", "https", "ssh", "git"]),
],
)
html_url = models.URLField(_("HTML URL"), null=True, blank=True)
private = models.BooleanField(_("Private repository"), default=False)
vcs = models.CharField(
_("vcs"),
max_length=200,
blank=True,
choices=REPO_CHOICES,
)
default_branch = models.CharField(
_("Default branch of the repository"),
max_length=150,
null=True,
blank=True,
)
# VCS provider repository id
remote_id = models.CharField(db_index=True, max_length=128)
vcs_provider = models.CharField(
_("VCS provider"), choices=VCS_PROVIDER_CHOICES, max_length=32
)
objects = RemoteRepositoryQuerySet.as_manager()
class Meta:
ordering = ["full_name"]
verbose_name_plural = "remote repositories"
unique_together = (
"remote_id",
"vcs_provider",
)
db_table = "oauth_remoterepository_2020"
def __str__(self):
return self.html_url or self.full_name
def matches(self, user):
"""Existing projects connected to this RemoteRepository."""
# TODO: remove this method and refactor the API response in ``/api/v2/repos/``
# (or v3) to just return the linked Project (slug, url) if the ``RemoteRepository``
# connection exists. Note the frontend needs to be simplified as well in
# ``import.js`` and ``project_import.html``.
projects = (
Project.objects.public(user)
.filter(
remote_repository=self,
)
.values("slug")
)
return [
{
"id": project["slug"],
"url": reverse(
"projects_detail",
kwargs={
"project_slug": project["slug"],
},
),
}
for project in projects
]
def get_remote_repository_relation(self, user, social_account):
"""Return RemoteRepositoryRelation object for the remote repository."""
remote_repository_relation, _ = RemoteRepositoryRelation.objects.get_or_create(
remote_repository=self, user=user, account=social_account
)
return remote_repository_relation
class RemoteRepositoryRelation(TimeStampedModel):
remote_repository = models.ForeignKey(
RemoteRepository,
related_name="remote_repository_relations",
on_delete=models.CASCADE,
)
user = models.ForeignKey(
User, related_name="remote_repository_relations", on_delete=models.CASCADE
)
account = models.ForeignKey(
SocialAccount,
verbose_name=_("Connected account"),
related_name="remote_repository_relations",
on_delete=models.CASCADE,
)
admin = models.BooleanField(_("Has admin privilege"), default=False)
class Meta:
unique_together = (
"remote_repository",
"account",
)