|
28 | 28 | from readthedocs.builds.models import Build, Version
|
29 | 29 | from readthedocs.core.utils import trigger_build
|
30 | 30 | from readthedocs.core.utils.extend import SettingsOverrideObject
|
| 31 | +from readthedocs.core.views.hooks import trigger_sync_versions |
31 | 32 | from readthedocs.notifications.models import Notification
|
32 | 33 | from readthedocs.oauth.models import (
|
33 | 34 | RemoteOrganization,
|
@@ -161,6 +162,9 @@ def get_serializer_class(self):
|
161 | 162 | if self.action in ("update", "partial_update"):
|
162 | 163 | return ProjectUpdateSerializer
|
163 | 164 |
|
| 165 | + # Default serializer so that sync_versions works with the BrowseableAPI |
| 166 | + return ProjectSerializer |
| 167 | + |
164 | 168 | def get_queryset(self):
|
165 | 169 | # Allow hitting ``/api/v3/projects/`` to list their own projects
|
166 | 170 | if self.basename == "projects" and self.action == "list":
|
@@ -218,6 +222,27 @@ def superproject(self, request, project_slug):
|
218 | 222 | except Exception:
|
219 | 223 | return Response(status=status.HTTP_404_NOT_FOUND)
|
220 | 224 |
|
| 225 | + @action(detail=True, methods=["post"], url_path="sync-versions") |
| 226 | + def sync_versions(self, request, project_slug): |
| 227 | + """ |
| 228 | + Kick off a task to sync versions for a project. |
| 229 | +
|
| 230 | + POST to this endpoint to trigger a task that syncs versions for the project. |
| 231 | +
|
| 232 | + This will be used in a button in the frontend, |
| 233 | + but also can be used to trigger a sync from the API. |
| 234 | + """ |
| 235 | + project = self.get_object() |
| 236 | + triggered = trigger_sync_versions(project) |
| 237 | + data = {} |
| 238 | + if triggered: |
| 239 | + data.update({"triggered": True}) |
| 240 | + code = status.HTTP_202_ACCEPTED |
| 241 | + else: |
| 242 | + data.update({"triggered": False}) |
| 243 | + code = status.HTTP_400_BAD_REQUEST |
| 244 | + return Response(data=data, status=code) |
| 245 | + |
221 | 246 |
|
222 | 247 | class ProjectsViewSet(SettingsOverrideObject):
|
223 | 248 | _default_class = ProjectsViewSetBase
|
|
0 commit comments